Skip to content

Commit

Permalink
Move FindEdges, AverageBrightness, PeakBrightness to target
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Nov 23, 2024
1 parent 3fb4239 commit 9323686
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 159 deletions.
33 changes: 4 additions & 29 deletions Source/script/imports/simba.import_image.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1711,34 +1711,15 @@ procedure _LapeImage_Fonts(const Params: PParamArray; const Result: Pointer); LA
PStringArray(Result)^ := TSimbaImage.Fonts();
end;

procedure _LapeImage_FindEdges1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PPointArray(Result)^ := PSimbaImage(Params^[0])^.FindEdges(PSingle(Params^[1])^, PColorSpace(Params^[2])^, PChannelMultipliers(Params^[3])^);
end;

procedure _LapeImage_FindEdges2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PPointArray(Result)^ := PSimbaImage(Params^[0])^.FindEdges(PSingle(Params^[1])^);
end;

procedure _LapeImage_PeakBrightness(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PInteger(Result)^ := PSimbaImage(Params^[0])^.PeakBrightness();
end;

procedure _LapeImage_AverageBrightness(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PInteger(Result)^ := PSimbaImage(Params^[0])^.AverageBrightness();
end;

(*
TImage.Finder
TImage.Target
-------------
```
function TImage.Finder: TSimbaFinder;
function TImage.Target: TTarget;
```
Returns a TSimbaFinder which is targetted to the image.
Returns a target which is targetted to the image.
Use this to find colors and such on a image.
*)

(*
Expand Down Expand Up @@ -1820,12 +1801,9 @@ procedure ImportSimbaImage(Script: TSimbaScript);
addProperty('TImage', 'Width', 'Integer', @_LapeImage_Width_Read);
addProperty('TImage', 'Height', 'Integer', @_LapeImage_Height_Read);
addProperty('TImage', 'Center', 'TPoint', @_LapeImage_Center_Read);

addProperty('TImage', 'DefaultPixel', 'TColorBGRA', @_LapeImage_DefaultPixel_Read, @_LapeImage_DefaultPixel_Write);

addProperty('TImage', 'DrawColor', 'TColor', @_LapeImage_DrawColor_Read, @_LapeImage_DrawColor_Write);
addProperty('TImage', 'DrawAlpha', 'Byte', @_LapeImage_DrawAlpha_Read, @_LapeImage_DrawAlpha_Write);

addProperty('TImage', 'FontName', 'String', @_LapeImage_FontName_Read, @_LapeImage_FontName_Write);
addProperty('TImage', 'FontSize', 'Single', @_LapeImage_FontSize_Read, @_LapeImage_FontSize_Write);
addProperty('TImage', 'FontAntialiasing', 'Boolean', @_LapeImage_FontAntialiasing_Read, @_LapeImage_FontAntialiasing_Write);
Expand Down Expand Up @@ -1961,9 +1939,6 @@ procedure ImportSimbaImage(Script: TSimbaScript);

addGlobalFunc('procedure TImage.SaveUnfreedImagesInDir(Directory: String); static;', @_LapeImage_SaveUnfreedImagesInDir);

addGlobalFunc('function TImage.FindEdges(MinDiff: Single; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers): TPointArray; overload;', @_LapeImage_FindEdges1);
addGlobalFunc('function TImage.FindEdges(MinDiff: Single): TPointArray; overload;', @_LapeImage_FindEdges2);

DumpSection := '';
end;
end;
Expand Down
4 changes: 2 additions & 2 deletions Source/script/imports/simba.import_target.pas
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ procedure _Lape_Target_RemoveTargetChangeEvent(const Params: PParamArray); LAPE_
TTarget.FreezeImage
-------------------
```
procedure TTarget.FreezeImage(ABounds: TBox);
procedure TTarget.FreezeImage(Bounds: TBox = [-1,-1,-1,-1]);
```
*)
procedure _LapeTarget_FreezeImage(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
Expand Down Expand Up @@ -1169,7 +1169,7 @@ procedure ImportTarget(Script: TSimbaScript);
addGlobalFunc('procedure TTarget.RemoveTargetChangeEvent(Event: TTargetEvent)', @_Lape_Target_RemoveTargetChangeEvent);
addGlobalFunc('procedure TTarget.RemoveTargetInvalidEvent(Event: TTargetEvent)', @_Lape_Target_RemoveTargetInvalidEvent);

addGlobalFunc('procedure TTarget.FreezeImage(ABounds: TBox);', @_LapeTarget_FreezeImage);
addGlobalFunc('procedure TTarget.FreezeImage(Bounds: TBox = [-1,-1,-1,-1]);', @_LapeTarget_FreezeImage);
addGlobalFunc('procedure TTarget.UnFreezeImage;', @_LapeTarget_UnFreezeImage);
addGlobalFunc('function TTarget.IsImageFrozen: Boolean;', @_LapeTarget_IsImageFrozen);
addGlobalFunc('function TTarget.GetImage(Bounds: TBox = [-1,-1,-1,-1]): TImage', @_LapeTarget_GetImage);
Expand Down
108 changes: 108 additions & 0 deletions Source/simba.finder_color.pas
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ function GetColorsMatrixOnBuffer(Buffer: PColorBGRA; BufferWidth: Integer; Searc

function GetColorsMatrixOnTarget(constref Target: TSimbaTarget; Bounds: TBox): TIntegerMatrix;

function FindEdgesOnBuffer(Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer;
MinDiff: Single; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers): TPointArray;

function FindEdgesOnTarget(constref Target: TSimbaTarget; Bounds: TBox;
MinDiff: Single; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers): TPointArray;

function PeakBrightnessOnBuffer(Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer): Integer;
function PeakBrightnessOnTarget(constref Target: TSimbaTarget; Bounds: TBox): Integer;

function AverageBrightnessOnBuffer(Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer): Integer;
function AverageBrightnessOnTarget(constref Target: TSimbaTarget; Bounds: TBox): Integer;

var
ColorFinderMultithreadOpts: record
Enabled: Boolean;
Expand Down Expand Up @@ -522,6 +534,102 @@ function GetColorsMatrixOnTarget(constref Target: TSimbaTarget; Bounds: TBox): T
end;
end;

function FindEdgesOnBuffer(Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer;
MinDiff: Single; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers): TPointArray;
var
X, Y, W, H: Integer;
PointBuffer: TSimbaPointBuffer;
First, Second, Third: TColor;
begin
W := SearchWidth - 2;
H := SearchHeight - 2;
for Y := 0 to H do
for X := 0 to W do
begin
First := TSimbaColorConversion.BGRAToColor(Buffer[Y * BufferWidth + X]);
Second := TSimbaColorConversion.BGRAToColor(Buffer[Y * BufferWidth + (X+1)]);
Third := TSimbaColorConversion.BGRAToColor(Buffer[(Y+1) * BufferWidth + X]);

if (not SimilarColors(First, Second, MinDiff, ColorSpace, Multipliers)) or
(not SimilarColors(First, Third, MinDiff, ColorSpace, Multipliers)) then
PointBuffer.Add(X, Y);
end;

Result := PointBuffer.ToArray(False);
end;

function FindEdgesOnTarget(constref Target: TSimbaTarget; Bounds: TBox;
MinDiff: Single; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers): TPointArray;
var
Buffer: PColorBGRA;
BufferWidth: Integer;
begin
if Target.GetImageData(Bounds, Buffer, BufferWidth) then
try
Result := FindEdgesOnBuffer(Buffer, BufferWidth, Bounds.Width, Bounds.Height, MinDiff, ColorSpace, Multipliers);
finally
Target.FreeImageData(Buffer);
end;
end;

function PeakBrightnessOnBuffer(Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer): Integer;
var
X, Y, Grey: Integer;
begin
Result := 0;

for Y := 0 to SearchHeight - 1 do
for X := 0 to SearchWidth - 1 do
with Buffer[Y * BufferWidth + X] do
begin
Grey := Round(R * 0.299 + G * 0.587 + B * 0.114);
if (Grey > Result) then
Result := Grey;
end;
end;

function PeakBrightnessOnTarget(constref Target: TSimbaTarget; Bounds: TBox): Integer;
var
Buffer: PColorBGRA;
BufferWidth: Integer;
begin
if Target.GetImageData(Bounds, Buffer, BufferWidth) then
try
Result := PeakBrightnessOnBuffer(Buffer, BufferWidth, Bounds.Width, Bounds.Height);
finally
Target.FreeImageData(Buffer);
end;
end;

function AverageBrightnessOnBuffer(Buffer: PColorBGRA; BufferWidth: Integer; SearchWidth, SearchHeight: Integer): Integer;
var
X, Y: Integer;
Sum: UInt64;
begin
Result := 0;
Sum := 0;

for Y := 0 to SearchHeight - 1 do
for X := 0 to SearchWidth - 1 do
with Buffer[Y * BufferWidth + X] do
Sum += Round(R * 0.299 + G * 0.587 + B * 0.114);

Result := Sum div (SearchWidth * SearchHeight);
end;

function AverageBrightnessOnTarget(constref Target: TSimbaTarget; Bounds: TBox): Integer;
var
Buffer: PColorBGRA;
BufferWidth: Integer;
begin
if Target.GetImageData(Bounds, Buffer, BufferWidth) then
try
Result := AverageBrightnessOnBuffer(Buffer, BufferWidth, Bounds.Width, Bounds.Height);
finally
Target.FreeImageData(Buffer);
end;
end;

initialization
ColorFinderMultithreadOpts.Enabled := True;
ColorFinderMultithreadOpts.SliceWidth := 250;
Expand Down
74 changes: 0 additions & 74 deletions Source/simba.image.pas
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,6 @@ TSimbaImage = class(TSimbaBaseClass)
// Laz bridge
function ToLazBitmap: TBitmap;
procedure FromLazBitmap(LazBitmap: TBitmap);

// Finders
function FindEdges(MinDiff: Single; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers): TPointArray; overload;
function FindEdges(MinDiff: Single): TPointArray; overload;

function PeakBrightness: Integer;
function AverageBrightness: Integer;
end;

PSimbaImage = ^TSimbaImage;
Expand Down Expand Up @@ -852,73 +845,6 @@ procedure TSimbaImage.FromLazBitmap(LazBitmap: TBitmap);
TempBitmap.Free();
end;

function TSimbaImage.FindEdges(MinDiff: Single; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers): TPointArray;
var
X, Y, W, H: Integer;
Buffer: TSimbaPointBuffer;
First, Second, Third: TColor;
begin
Buffer.Init();

W := FWidth - 2;
H := FHeight - 2;
for Y := 0 to H do
for X := 0 to W do
begin
First := TSimbaColorConversion.BGRAToColor(FData[Y * FWidth + X]);
Second := TSimbaColorConversion.BGRAToColor(FData[Y * FWidth + (X+1)]);
Third := TSimbaColorConversion.BGRAToColor(FData[(Y+1) * FWidth + X]);

if (not SimilarColors(First, Second, MinDiff, ColorSpace, Multipliers)) or
(not SimilarColors(First, Third, MinDiff, ColorSpace, Multipliers)) then
Buffer.Add(X, Y);
end;

Result := Buffer.ToArray(False);
end;

function TSimbaImage.FindEdges(MinDiff: Single): TPointArray;
begin
Result := FindEdges(MinDiff, DefaultColorSpace, DefaultMultipliers);
end;

function TSimbaImage.PeakBrightness: Integer;
var
X, Y: Integer;
begin
Result := 0;

for Y := 0 to FHeight - 1 do
for X := 0 to FWidth - 1 do
with FData[Y * FWidth + X] do
begin
if (R > Result) then Result := R;
if (G > Result) then Result := G;
if (B > Result) then Result := B;
end;

Result := Round(Result / 255 * 100);
end;

function TSimbaImage.AverageBrightness: Integer;
var
X, Y, Sum: Integer;
begin
Result := 0;

for Y := 0 to FHeight - 1 do
begin
Sum := 0;
for X := 0 to FWidth - 1 do
with FData[Y * FWidth + X] do
Sum += Round((R + G + B) / 3 * 0.392);

Result += Sum div FWidth;
end;

Result := Round(Result / FHeight);
end;

procedure TSimbaImage.DrawTPA(TPA: TPointArray);
begin
if (FDrawAlpha = ALPHA_OPAQUE) then
Expand Down
36 changes: 4 additions & 32 deletions Source/simba.target.pas
Original file line number Diff line number Diff line change
Expand Up @@ -689,51 +689,23 @@ function TSimbaTarget.GetPixelDifferenceTPA(WaitTime: Integer; Tolerance: Single
end;

function TSimbaTarget.AverageBrightness(ABounds: TBox): Integer;
var
Image: TSimbaImage;
begin
if GetImageDataAsImage(ABounds, Image) then
try
Result := Image.AverageBrightness();
finally
Image.Free();
end;
Result := AverageBrightnessOnTarget(Self, Bounds);
end;

function TSimbaTarget.PeakBrightness(ABounds: TBox): Integer;
var
Image: TSimbaImage;
begin
if GetImageDataAsImage(ABounds, Image) then
try
Result := Image.PeakBrightness();
finally
Image.Free();
end;
Result := PeakBrightnessOnTarget(Self, Bounds);
end;

function TSimbaTarget.FindEdges(MinDiff: Single; ColorSpace: EColorSpace; Multipliers: TChannelMultipliers; ABounds: TBox): TPointArray;
var
Image: TSimbaImage;
begin
if GetImageDataAsImage(ABounds, Image) then
try
Result := Image.FindEdges(MinDiff, ColorSpace, Multipliers);
finally
Image.Free();
end;
Result := FindEdgesOnTarget(Self, ABounds, MinDiff, ColorSpace, Multipliers);
end;

function TSimbaTarget.FindEdges(MinDiff: Single; ABounds: TBox): TPointArray;
var
Image: TSimbaImage;
begin
if GetImageDataAsImage(ABounds, Image) then
try
Result := Image.FindEdges(MinDiff);
finally
Image.Free();
end;
Result := FindEdgesOnTarget(Self, ABounds, MinDiff, DefaultColorSpace, DefaultMultipliers);
end;

function TSimbaTarget.AddTargetChangeEvent(Event: TTargetEvent): TTargetEvent;
Expand Down
22 changes: 0 additions & 22 deletions Tests/finder.simba

This file was deleted.

0 comments on commit 9323686

Please sign in to comment.