m34tcode
2012-04-07 14:26:37
Wrote a new verison of poxel shift. This one will, over a duration of time, watch for all the pixels that never changed.
When finished, it counts the pixels that did change color, beyond a specified tolerance.
While it is similar to the standard PixelShift, It is far more effective through lag. Tested and it works perfectly now.
Also, as the function rules out pixels, it will get faster. So in a real application, and without DEBUG defined, it runs in less than a third of a second per iteration, on my old and slow pc. Newer computers should be even better.
Code:
When finished, it counts the pixels that did change color, beyond a specified tolerance.
While it is similar to the standard PixelShift, It is far more effective through lag. Tested and it works perfectly now.
Also, as the function rules out pixels, it will get faster. So in a real application, and without DEBUG defined, it runs in less than a third of a second per iteration, on my old and slow pc. Newer computers should be even better.
Code:
{$DEFINE DEBUG}
{$DEFINE SMART}
{$i srl/srl.simba}
{$i srl/srl/misc/paintsmart.simba}
function GetColArr (Box : TBox) : T2DIntegerArray;
var
x,y : Integer;
begin
SetArrayLength(Result,box.X2-box.X1);
for x := 0 to box.X2-box.X1-1 do
begin
SetArrayLength(Result[x],box.Y2-box.Y1);
for y := 0 to box.Y2-box.Y1-1 do
begin
Result[x][y] := GetColor(x+box.X1,y+box.Y1);
end;
end;
end;
procedure ValidatePixels(Box : TBox; OrigCols : T2DIntegerArray; BoolArr : array of TBoolArray; Tolerance : Integer);
var
X,Y : Integer;
NewCols : T2DIntegerArray;
begin
NewCols := GetColArr(Box);
for X := 0 to Box.X2 - Box.X1-1 do
for Y := 0 to Box.Y2 - Box.Y1-1 do
begin
if not BoolArr[x][y] then
continue;
boolArr[x][y] := SimilarColors(origCols[x][y],NewCols[x][y],Tolerance);
end;
end;
function InitBoolArr(Width,Height : Integer) : array of TBoolArray;
var x,y : Integer;
begin
SetArrayLength(Result,Width);
for x := 0 to Width-1 do
begin
SetArrayLength(Result[x],Height);
for Y := 0 to Height-1 do
begin
Result[x][y] := True;
end;
end;
end;
function countBools(BoolArr : Array of TBoolArray; CountTrues : Boolean) : Integer;
var
X,Y,W,H,C : Integer;
begin
W := GetArrayLength(BoolArr);
H := GetArrayLength(BoolArr[0]);
For X := 0 to W-1 do
for Y := 0 to H-2 do
if BoolArr[X][Y] = CountTrues then
Inc(C);
Result := C;
end;
function CalcNonStaticPixelCount(Box : TBox; ShiftInterval, Repetitions, Tolerance : Integer) : Integer;
var
I,X,Y : Integer;
T,TT : LongInt;
OrigColArr : T2DIntegerArray;
BoolArr : array of TBoolArray;
begin
OrigColArr := GetColArr(Box);
BoolArr := initBoolArr(Box.X2-Box.X1,Box.Y2-Box.Y1);
MarkTime(T);
MarkTime(TT);
for I := 0 to Repetitions-1 do
begin
if (TimeFromMark(T) > (Repetitions*ShiftInterval)) then
begin
srl_Warn('CalcNonStaticPixelCount','Completed only ' + IntToStr(I) + ' of '
+ IntToStr(Repetitions) + '. Try increasing the interval',1);
break;
end;
{$IFDEF DEBUG}
if (I > 0) then
writeln('CalcNonStaticPixelCount: Iteration ' + IntToStr(I) + ' took '
+ FloatToStr(TimeFromMark(TT)/1000.0) + ' seconds to complete.');
{$ENDIF}
MarkTime(TT);
wait(Max(0,ShiftInterval-TimeFromMark(TT)));
validatePixels(Box,OrigColArr,BoolArr,Tolerance);
{$IFDEF DEBUG}
SMART_ClearCanvas;
for x := 0 to box.x2-box.x1-1 do
for y := 0 to box.y2-box.y1-1 do
if boolArr[x][y] then
SMART_DrawDot(false,Point(x+box.x1,box.y1+y),clRed);
{$ENDIF}
end;
Result := countBools(BoolArr,False);
end;
var
T : Longint;
Box : TBox;
begin
{$ifdef SMART}
Smart_Members := True;
Smart_Server := 100;
Smart_Signed := False;
Smart_SuperDetail := False;
{$endif}
SetupSRL;
SMART_ClearCanvas;
MarkTime(T);
box := IntToBox(236,97,271,175);
writeln('Found ' + IntToStr(CalcNonStaticPixelCount(box,100,90,10)) + ' nonstatic pixels.');
writeln('Executed in ' + FloatToStr(TimeFromMark(T)/1000.0));
end.