Simba Code:
{$DEFINE SMART}
{$i srl/srl.simba}
{$i srl/srl/misc/paintsmart.simba}
type
pixelCol = record
x,y,Color:Integer;
end;
function newPixelCol(x,y,col:Integer):pixelCol;
begin
Result.Color := col;
Result.x := x;
Result.y := y;
end;
function pixelColInRange(pixelCol1,pixelCol2:pixelCol; tol:Integer):Boolean;
begin
result := SimilarColors(pixelCol1.Color,pixelCol2.Color,tol);
end;
function locIsSame(pixelCol1,pixelCol2:pixelCol):Boolean;
begin
result := (pixelCol1.x = pixelCol2.x) and (pixelCol1.y = pixelCol2.y);
end;
function getColArr(b:TBox):array of pixelCol;
var
width,height,x,y:Integer;
colArr : array of pixelCol;
colBitmap,I ,dbg: Integer;
begin
colBitmap := BitmapFromClient(b.X1,b.Y1,b.X2,b.Y2);
width := Round(Abs(b.X2-b.X1));
height := Round(Abs(b.Y2-b.Y1));
SetArrayLength(colArr,width*height);
for x := 0 to width-1 do
begin
for y := 0 to height-1 do
begin
colArr[i] := newPixelCol(x, y, FastGetPixel(colBitmap, x, y));
inc(i);
end;
end;
result := colArr;
end;
function arrayCopy(cols: array of pixelCol;sx,ex:Integer):array of pixelCol;
var
newArr : array of pixelCol;
I, lengthOptimisation: Integer;
begin
SetLength(newArr,(ex-sx));
lenghtOptimisation:= length(newArr); // optimisation here.
for I := 0 to length(newArr)-1 do
newArr[I] := cols[sx+I];
result := newArr;
end;
function pixelIntersect(colArr1,colArr2:array of pixelCol; boundingBox : TBox; tol:Integer):array of pixelCol;
var
pixels : T2DIntegerArray;
x1,x2,y1,y2,I,II
,curIndex,boundingWidth:Integer;
smallArr,largeArr,intersectArr : array of pixelCol;
begin
curIndex := 0;
boundingWidth := boundingBox.X2-boundingBox.X1;
if (length(colArr1) < length(colArr2)) then
begin
smallArr := colArr1;
largeArr := colArr2;
end else
begin
smallArr := colArr2;
largeArr := colArr1;
end;
writeln(smallArr);
writeln(largearr);
SetArrayLength(intersectArr,length(largeArr));
writeln('1: '+inttostr(length(smallArr)));
writeln('2: '+inttostr(length(largeArr)));
while true do
begin
if (locIsSame(smallArr[I],largeArr[II])) then
begin
if pixelColInRange(smallArr[I],largeArr[II],tol) then
begin
intersectArr[curIndex] := smallArr[I];
curIndex := curIndex+1;
end;
end;
if ((smallArr[I].x*boundingWidth+smallArr[I].y)
< (largeArr[II].x*boundingWidth+largeArr[II].y)) and (I < (Length(smallArr)-1)) then
begin
inc(I)
end else if ((smallArr[I].x*boundingWidth+smallArr[I].y)
>= (largeArr[II].x*boundingWidth+largeArr[II].y)) and (II < (Length(largeArr)-1)) then
begin
Inc(II);
end else
break;
end;
result := arrayCopy(intersectArr, 0, Max(0,curIndex)); // Copying an array, now that looks coputer intensive.
end;
function calcPixShift(b:TBox;interval,repetitions,tol:Integer):Integer;
var
staticPixels : TPointArray;
T,tt : LongInt;
StaticColArr, TempColArr : array of pixelCol;
I : Integer;
begin
StaticColArr := getColArr(b);
MarkTime(T);
tt := T;
for I := 0 to repetitions-1 do
begin
TempColArr := getColArr(b);
StaticColArr := pixelIntersect(StaticColArr,TempColArr,b,tol);
writeln('pixIntersectCalc time: '+inttostr(timefrommark(tt)));
if (TimeFromMark(T) > (repetitions*interval)) then
begin
writeln('Completed only ' + IntToStr(i+1) + ' of '
+ IntToStr(repetitions) + '. Try increasing the interval');
break;
end;
if I < repetitions-1 then
begin
wait(Max(0,interval-TimeFromMark(tt)));
MarkTime(tt);
end;
end;
for I := 0 to length(StaticColArr)-1 do // Again we shoudl do optimisation here
SMART_DrawDot(false,Point(StaticColArr[i].x+b.x1,StaticColArr[i].y+b.y1),clRed); // Drawing takes alot of time, this should only be used for debuging.
result := ((b.X2-b.X1)*(b.Y2-b.Y1))-length(StaticColArr); // We could send the variable used to store the length we would have created for the previous optimisation
end;
begin
{$ifdef SMART}
Smart_Members := True;
Smart_Server := 100;
Smart_Signed := False;
Smart_SuperDetail := False;
writeln('hiiii');
{$endif}
SetupSRL;
writeln(calcPixShift(IntToBox(250,100,270,120),3000,10,0));
end.