Skip to content

Commit

Permalink
HasImage etc
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Feb 27, 2024
1 parent 9cf7444 commit d27490a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 93 deletions.
40 changes: 18 additions & 22 deletions Source/finders/simba.finder_color.pas
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@ function FindColorsOnBuffer(Formula: EColorSpace; Color: TColor; Tolerance: Sing
Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer; OffsetX, OffsetY: Integer): TPointArray;

function CountColorsOnTarget(Target: TSimbaTarget; Bounds: TBox;
Formula: EColorSpace; Color: TColor; Tolerance: Single; Multipliers: TChannelMultipliers): Integer;
Formula: EColorSpace; Color: TColor; Tolerance: Single; Multipliers: TChannelMultipliers;
MaxToFind: Integer = -1): Integer;

function CountColorsOnBuffer(Formula: EColorSpace; Color: TColor; Tolerance: Single; Multipliers: TChannelMultipliers;
function CountColorsOnBuffer(var Limit: TSimpleThreadsafeLimit;
Formula: EColorSpace; Color: TColor; Tolerance: Single; Multipliers: TChannelMultipliers;
Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer): Integer;

procedure HasColorOnBuffer(Formula: EColorSpace; Color: TColor; Tolerance: Single; Multipliers: TChannelMultipliers;
Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer; var Limit: TSimpleThreadsafeLimit);

function HasColorOnTarget(Target: TSimbaTarget; Bounds: TBox;
Formula: EColorSpace; Color: TColor; Tolerance: Single; Multipliers: TChannelMultipliers;
MinCount: Integer): Boolean;

function MatchColorsOnBuffer(Formula: EColorSpace; Color: TColor; Multipliers: TChannelMultipliers;
Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer): TSingleMatrix;

Expand Down Expand Up @@ -205,21 +200,22 @@ function FindColorsOnBuffer(Formula: EColorSpace; Color: TColor; Tolerance: Sing
}
MACRO_FINDCOLORS

function CountColorsOnBuffer(Formula: EColorSpace; Color: TColor; Tolerance: Single; Multipliers: TChannelMultipliers;
function CountColorsOnBuffer(var Limit: TSimpleThreadsafeLimit;
Formula: EColorSpace; Color: TColor; Tolerance: Single; Multipliers: TChannelMultipliers;
Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer): Integer;

{$DEFINE MACRO_FINDCOLORS_BEGIN :=
Result := 0;
}
{$DEFINE MACRO_FINDCOLORS_COMPARE :=
if (Cache.Dist <= Tolerance) then
Inc(Result);
Limit.Inc();
}
{$DEFINE MACRO_FINDCOLORS_ROW :=
// Nothing
if Limit.Reached() then Exit;
}
{$DEFINE MACRO_FINDCOLORS_END :=
// Nothing
Result := Limit.Counter;
}
MACRO_FINDCOLORS

Expand Down Expand Up @@ -263,16 +259,17 @@ function FindColorsOnTarget(Target: TSimbaTarget; Bounds: TBox;
end;
end;

function CountColorsOnTarget(Target: TSimbaTarget; Bounds: TBox; Formula: EColorSpace; Color: TColor; Tolerance: Single; Multipliers: TChannelMultipliers): Integer;
function CountColorsOnTarget(Target: TSimbaTarget; Bounds: TBox; Formula: EColorSpace; Color: TColor; Tolerance: Single; Multipliers: TChannelMultipliers; MaxToFind: Integer): Integer;
var
Limit: TSimpleThreadsafeLimit;

Buffer: PColorBGRA;
BufferWidth: Integer;

SliceResults: TIntegerArray;

procedure Execute(const Index, Lo, Hi: Integer);
begin
SliceResults[Index] := CountColorsOnBuffer(
CountColorsOnBuffer(
Limit,
Formula, Color, Tolerance, Multipliers,
@Buffer[Lo * BufferWidth], BufferWidth, Bounds.Width, (Hi - Lo) + 1
);
Expand All @@ -291,10 +288,9 @@ function CountColorsOnTarget(Target: TSimbaTarget; Bounds: TBox; Formula: EColor
T := HighResolutionTime();
{$ENDIF}

SetLength(SliceResults, CalculateSlices(Bounds.Width, Bounds.Height)); // Cannot exceed this
ThreadsUsed := SimbaThreadPool.RunParallel(Length(SliceResults), 0, Bounds.Height - 1, @Execute);
for I := 0 to High(SliceResults) do
Result += SliceResults[I];
Limit := TSimpleThreadsafeLimit.Create(MaxToFind);
ThreadsUsed := SimbaThreadPool.RunParallel(CalculateSlices(Bounds.Width, Bounds.Height), 0, Bounds.Height - 1, @Execute);
Result := Limit.Counter;

{$IFDEF SIMBA_BENCHMARKS}
DebugLn('CountColors: ColorSpace=%s Width=%d Height=%d ThreadsUsed=%d Time=%f', [Formula.AsString(), Bounds.Width, Bounds.Height, ThreadsUsed, HighResolutionTime() - T]);
Expand Down Expand Up @@ -425,7 +421,7 @@ function HasColorOnTarget(Target: TSimbaTarget; Bounds: TBox; Formula: EColorSpa

initialization
ColorFinderMultithreadOpts.Enabled := True;
ColorFinderMultithreadOpts.SliceWidth := 125;
ColorFinderMultithreadOpts.SliceWidth := 250;
ColorFinderMultithreadOpts.SliceHeight := 250;

end.
29 changes: 13 additions & 16 deletions Source/finders/simba.finder_image.pas
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ interface
simba.base, simba.colormath, simba.colormath_distance, simba.target,
simba.image, simba.colormath_distance_unrolled, simba.simplelock;

function FindBitmapOnTarget(Target: TSimbaTarget; Image: TSimbaImage; Bounds: TBox;
Formula: EColorSpace; Tolerance: Single; Multipliers: TChannelMultipliers; MaxToFind: Integer = -1): TPointArray;
function FindImageOnTarget(Target: TSimbaTarget; Image: TSimbaImage; Bounds: TBox;
Formula: EColorSpace; Tolerance: Single; Multipliers: TChannelMultipliers; MaxToFind: Integer = -1): TPointArray;

function FindBitmapOnBuffer(var Limit: TSimpleThreadsafeLimit;
Image: TSimbaImage;
ColorSpace: EColorSpace; Tolerance: Single; Multipliers: TChannelMultipliers;
Buffer: PColorBGRA; BufferWidth: Integer;
SearchWidth, SearchHeight: Integer): TPointArray;
function FindImageOnBuffer(var Limit: TSimpleThreadsafeLimit;
Image: TSimbaImage;
ColorSpace: EColorSpace; Tolerance: Single; Multipliers: TChannelMultipliers;
Buffer: PColorBGRA; BufferWidth: Integer;
SearchWidth, SearchHeight: Integer): TPointArray;

var
BitmapFinderMultithreadOpts: record
Expand All @@ -53,7 +53,7 @@ function CalculateSlices(SearchWidth, SearchHeight: Integer): Integer;
Exit(I);
end;

// not possible to slice into at least `MatchTemplateMT_SliceHeight` pixels
// not possible to slice into at least `SliceHeight` pixels
end;

const
Expand Down Expand Up @@ -96,7 +96,7 @@ function ConvertBitmapColors(Image: TSimbaImage; ColorSpace: EColorSpace): PByte
end;
end;

function FindBitmapOnTarget(Target: TSimbaTarget; Image: TSimbaImage; Bounds: TBox; Formula: EColorSpace; Tolerance: Single; Multipliers: TChannelMultipliers; MaxToFind: Integer): TPointArray;
function FindImageOnTarget(Target: TSimbaTarget; Image: TSimbaImage; Bounds: TBox; Formula: EColorSpace; Tolerance: Single; Multipliers: TChannelMultipliers; MaxToFind: Integer): TPointArray;
var
Buffer: PColorBGRA;
BufferWidth: Integer;
Expand All @@ -109,7 +109,7 @@ function FindBitmapOnTarget(Target: TSimbaTarget; Image: TSimbaImage; Bounds: TB
var
TPA: TPointArray;
begin
TPA := FindBitmapOnBuffer(
TPA := FindImageOnBuffer(
Limit,
Image, Formula, Tolerance, Multipliers,
@Buffer[Lo * BufferWidth], BufferWidth, Bounds.Width, (Hi - Lo) + Image.Height
Expand Down Expand Up @@ -139,14 +139,14 @@ function FindBitmapOnTarget(Target: TSimbaTarget; Image: TSimbaImage; Bounds: TB
SetLength(Result, MaxToFind);

{$IFDEF SIMBA_BENCHMARKS}
DebugLn('FindBitmap: ColorSpace=%s Width=%d Height=%d ThreadsUsed=%d Time=%f', [Formula.AsString(), Bounds.Width, Bounds.Height, ThreadsUsed, HighResolutionTime() - T]);
DebugLn('FindImage: ColorSpace=%s Width=%d Height=%d ThreadsUsed=%d Time=%f', [Formula.AsString(), Bounds.Width, Bounds.Height, ThreadsUsed, HighResolutionTime() - T]);
{$ENDIF}
finally
Target.FreeImageData(Buffer);
end;
end;

function FindBitmapOnBuffer(var Limit: TSimpleThreadsafeLimit; Image: TSimbaImage; ColorSpace: EColorSpace; Tolerance: Single; Multipliers: TChannelMultipliers; Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer): TPointArray;
function FindImageOnBuffer(var Limit: TSimpleThreadsafeLimit; Image: TSimbaImage; ColorSpace: EColorSpace; Tolerance: Single; Multipliers: TChannelMultipliers; Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer): TPointArray;
var
BitmapColors: PByte;

Expand Down Expand Up @@ -236,9 +236,6 @@ function FindBitmapOnBuffer(var Limit: TSimpleThreadsafeLimit; Image: TSimbaImag
CompareFunc := TColorDistanceFunc(@DistanceDeltaE_UnRolled);
MaxDistance := DistanceDeltaE_Max(Multipliers);
end;

else
SimbaException('FindBitmapOnBuffer: Formula invalid!');
end;

BitmapColors := ConvertBitmapColors(Image, ColorSpace);
Expand Down Expand Up @@ -276,7 +273,7 @@ function FindBitmapOnBuffer(var Limit: TSimpleThreadsafeLimit; Image: TSimbaImag

initialization
BitmapFinderMultithreadOpts.Enabled := True;
BitmapFinderMultithreadOpts.SliceHeight := 125;
BitmapFinderMultithreadOpts.SliceHeight := 250;
BitmapFinderMultithreadOpts.SliceWidth := 250;

end.
Expand Down
53 changes: 51 additions & 2 deletions Source/script/imports/simba.import_finder.pas
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ procedure _LapeFinder_FindImage1(const Params: PParamArray; const Result: Pointe
PPoint(Result)^ := PSimbaFinder(Params^[0])^.FindImage(PSimbaImage(Params^[1])^, PSingle(Params^[2])^, PBox(Params^[3])^);
end;

(*
TFinder.HasImage
----------------
> function TFinder.HasImage(Image: TSimbaImage; Tolerance: Single; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers; MinCount: Integer = 1; Bounds: TBox = [-1,-1,-1,-1]): Boolean;
*)
procedure _LapeFinder_HasImage1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBoolean(Result)^ := PSimbaFinder(Params^[0])^.HasImage(PSimbaImage(Params^[1])^, PSingle(Params^[2])^, PColorSpace(Params^[3])^, PChannelMultipliers(Params^[4])^, PInteger(Params^[5])^, PBox(Params^[6])^);
end;

(*
TFinder.HasImage
----------------
> function TFinder.HasImage(Image: TSimbaImage; Tolerance: Single; MinCount: Integer = 1; Bounds: TBox = [-1,-1,-1,-1]): Boolean;
*)
procedure _LapeFinder_HasImage2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBoolean(Result)^ := PSimbaFinder(Params^[0])^.HasImage(PSimbaImage(Params^[1])^, PSingle(Params^[2])^, PInteger(Params^[3])^, PBox(Params^[4])^);
end;

(*
TFinder.FindImage
-----------------
Expand Down Expand Up @@ -209,16 +229,31 @@ procedure _LapeFinder_CountColor3(const Params: PParamArray; const Result: Point
PInteger(Result)^ := PSimbaFinder(Params^[0])^.CountColor(PColorTolerance(Params^[1])^, PBox(Params^[2])^);
end;

(*
TFinder.HasColor
----------------
> function TFinder.HasColor(Color: TColor; Tolerance: Single; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers; MinCount: Integer = 1; Bounds: TBox = [-1,-1,-1,-1]): Boolean;
*)
procedure _LapeFinder_HasColor1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBoolean(Result)^ := PSimbaFinder(Params^[0])^.HasColor(PColor(Params^[1])^, PSingle(Params^[2])^, PColorSpace(Params^[3])^, PChannelMultipliers(Params^[4])^, PInteger(Params^[5])^, PBox(Params^[6])^);
end;

(*
TFinder.HasColor
----------------
> function TFinder.HasColor(Color: TColor; Tolerance: Single; MinCount: Integer = 1; Bounds: TBox = [-1,-1,-1,-1]): Boolean;
*)
procedure _LapeFinder_HasColor2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBoolean(Result)^ := PSimbaFinder(Params^[0])^.HasColor(PColor(Params^[1])^, PSingle(Params^[2])^, PInteger(Params^[3])^, PBox(Params^[4])^);
end;

(*
TFinder.HasColor
----------------
> function TFinder.HasColor(Color: TColorTolerance; MinCount: Integer = 1; Bounds: TBox = [-1,-1,-1,-1]): Boolean; overload;
*)
procedure _LapeFinder_HasColor3(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBoolean(Result)^ := PSimbaFinder(Params^[0])^.HasColor(PColorTolerance(Params^[1])^, PInteger(Params^[2])^, PBox(Params^[3])^);
Expand Down Expand Up @@ -327,13 +362,23 @@ procedure _LapeFinder_PeakBrightness(const Params: PParamArray; const Result: Po
(*
TFinder.FindTemplate
--------------------
> function TFinder.FindTemplate(Image: TImage; MinMatch: Single; Bounds: TBox = [-1,-1,-1,-1]): TPoint;
> function TFinder.FindTemplate(Image: TImage; out Match: Single; Bounds: TBox = [-1,-1,-1,-1]): TPoint;
*)
procedure _LapeFinder_FindTemplate(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PPoint(Result)^ := PSimbaFinder(Params^[0])^.FindTemplate(PSimbaImage(Params^[1])^, PSingle(Params^[2])^, PBox(Params^[3])^);
end;

(*
TFinder.HasTemplate
--------------------
> function TFinder.HasTemplate(Image: TImage; MinMatch: Single; Bounds: TBox = [-1,-1,-1,-1]): Boolean;
*)
procedure _LapeFinder_HasTemplate(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBoolean(Result)^ := PSimbaFinder(Params^[0])^.HasTemplate(PSimbaImage(Params^[1])^, PSingle(Params^[2])^, PBox(Params^[3])^);
end;

procedure ImportFinder(Compiler: TSimbaScript_Compiler);

procedure addFinderMethod(Header: lpString; Addr: Pointer);
Expand Down Expand Up @@ -407,7 +452,11 @@ procedure ImportFinder(Compiler: TSimbaScript_Compiler);
addFinderMethod('function TFinder.FindImage(Image: TImage; Tolerance: Single; Bounds: TBox = [-1,-1,-1,-1]): TPoint; overload', @_LapeFinder_FindImage1);
addFinderMethod('function TFinder.FindImage(Image: TImage; Tolerance: Single; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers; Bounds: TBox = [-1,-1,-1,-1]): TPoint; overload', @_LapeFinder_FindImage2);

addFinderMethod('function TFinder.FindTemplate(Image: TImage; MinMatch: Single; Bounds: TBox = [-1,-1,-1,-1]): TPoint', @_LapeFinder_FindTemplate);
addFinderMethod('function TFinder.HasImage(Image: TImage; Tolerance: Single; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers; MinCount: Integer = 1; Bounds: TBox = [-1,-1,-1,-1]): TPoint; overload', @_LapeFinder_HasImage1);
addFinderMethod('function TFinder.HasImage(Image: TImage; Tolerance: Single; MinCount: Integer = 1; Bounds: TBox = [-1,-1,-1,-1]): TPoint; overload', @_LapeFinder_HasImage2);

addFinderMethod('function TFinder.FindTemplate(Image: TImage; out Match: Single; Bounds: TBox = [-1,-1,-1,-1]): TPoint', @_LapeFinder_FindTemplate);
addFinderMethod('function TFinder.HasTemplate(Image: TImage; MinMatch: Single; Bounds: TBox = [-1,-1,-1,-1]): Boolean', @_LapeFinder_HasTemplate);

addFinderMethod('function TFinder.MatchColor(Color: TColor; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers; Bounds: TBox = [-1,-1,-1,-1]): TSingleMatrix', @_LapeFinder_MatchColor);

Expand Down
Loading

0 comments on commit d27490a

Please sign in to comment.