Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Dec 16, 2024
1 parent 4f040fc commit 32e0785
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 68 deletions.
16 changes: 16 additions & 0 deletions DocGen/rundocgen.simba
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var
p: TRunningProcessPiped;
s: String;
begin
p := StartProcessPiped('python.exe', ['-u', 'docgen.py'], 'DocGen');
while p.Running do
begin
s := p.ReadString;
if (s <> '') then
WriteLn(s);
Sleep(100);
end;
p.Free();

WriteLn('Link: "' + PathNormalize('DocGen/build/index.html') + '"');
end;
21 changes: 20 additions & 1 deletion DocGen/source/tutorials/Targets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,23 @@ You can declare as many TTarget variables as you like, all with different target
begin
MyOtherTarget.SetWindow(12345); // some window handle
WriteLn MyOtherTarget.CountColor($0000FF, 10); // Count a red color, with 10 tolerance.
end;
end;
----

Searching on a image
====================

:code:`TImage` has basic :code:`FindColor` and :code:`FindImage` methods however if you need the full finder methods you can do:

.. code-block::
var MyTarget: TTarget;
MyTarget.SetImage(MyImage);
Edges := MyTarget.FindEdges(5);
Or use the :code:`TImage.Target` property which returns a :code:`TTarget` already targeted to the image.

.. code-block::
Edges := MyImage.Target.FindEdges(5);
2 changes: 0 additions & 2 deletions Source/script/imports/simba.import_base.pas
Original file line number Diff line number Diff line change
Expand Up @@ -801,9 +801,7 @@ procedure ImportBase(Script: TSimbaScript);
addGlobalType('(__LT__, __GT__, __EQ__, __LE__, __GE__, __NE__)', 'EComparator');

addGlobalType('enum(Unknown, Unassigned, Null, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Single, Double, DateTime, Currency, Boolean, Variant, AString, UString, WString)', 'EVariantVarType');

addGlobalFunc('function Variant.VarType: EVariantVarType;', @_LapeVariantVarType);

addGlobalFunc('function Variant.IsNumeric: Boolean;', @_LapeVariantIsNumeric);
addGlobalFunc('function Variant.IsInteger: Boolean;', @_LapeVariantIsInteger);
addGlobalFunc('function Variant.IsFloat: Boolean;', @_LapeVariantIsFloat);
Expand Down
80 changes: 52 additions & 28 deletions Source/script/imports/simba.import_image.pas
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface

uses
Classes, SysUtils,
simba.base, simba.script;
simba.base, simba.baseclass, simba.script;

procedure ImportSimbaImage(Script: TSimbaScript);

Expand Down Expand Up @@ -1664,63 +1664,74 @@ procedure _LapeImage_FromLazBitmap(const Params: PParamArray); LAPE_WRAPPER_CALL
end;

(*
TImage.SaveUnfreedImagesInDir
-----------------------------
TImage.LoadFonts
----------------
```
procedure TImage.SaveUnfreedImagesInDir(Directory: String); static;
function TImage.LoadFonts(Dir: String): Boolean; static;
```
On script terminate if any images have not been freed save them to `Directory` for debugging ease.
Example:
Loads all ".ttf" fonts in the given directory.
*)
procedure _LapeImage_LoadFonts(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBoolean(Result)^ := TSimbaImage.LoadFontsInDir(PString(Params^[0])^);
end;

(*
TImage.Fonts
------------
```
TImage.SaveUnfreedImagesInDir('some/directory/');
function TImage.Fonts: TStringArray; static;
```
Returns all the loaded font names.
*)
procedure _LapeImage_SaveUnfreedImagesInDir(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
procedure _LapeImage_Fonts(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
TSimbaImage.SaveUnfreedImages := PString(Params^[0])^;
PStringArray(Result)^ := TSimbaImage.Fonts();
end;

(*
TImage.LoadFontsInDir
---------------------
TImage.FindColor
----------------
```
function TImage.LoadFontsInDir(Dir: String): Boolean; static;
function TImage.FindColor(Color: TColor; Tolerance: Single = 0): TPointArray;
```
Loads all ".ttf" fonts in the given directory.
Returns all the loaded font names.
*)
procedure _LapeImage_LoadFontsInDir(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
procedure _LapeImage_FindColor(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBoolean(Result)^ := TSimbaImage.LoadFontsInDir(PString(Params^[0])^);
PPointArray(Result)^ := PSimbaImage(Params^[0])^.FindColor(PColor(Params^[1])^, PSingle(Params^[2])^);
end;

(*
TImage.Fonts
------------
TImage.FindImage
----------------
```
function TImage.Fonts: TStringArray; static;
function TImage.FindImage(Image: TImage; Tolerance: Single = 0): TPoint;
```
Returns all the loaded font names.
*)
procedure _LapeImage_Fonts(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
procedure _LapeImage_FindImage(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PStringArray(Result)^ := TSimbaImage.Fonts();
PPoint(Result)^ := PSimbaImage(Params^[0])^.FindImage(PSimbaImage(Params^[1])^, PSingle(Params^[2])^);
end;

(*
TImage.Target
-------------
TImage.GetLoadedImages
----------------------
```
function TImage.Target: TTarget;
function GetLoadedImages: TImageArray;
```
Returns a target which is targetted to the image.
Use this to find colors and such on a image.
Returns an array of all the loaded images.
*)
procedure _LapeImage_GetLoadedImages(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PSimbaImageArray(Result)^ := TSimbaImageArray(GetSimbaObjectsOfClass(TSimbaImage));
end;

(*
TImage.CreateFromTarget
Expand Down Expand Up @@ -1773,6 +1784,16 @@ procedure TImage.Show(EnsureVisible: Boolean = True);
Show a image on the debug image.
*)

(*
TImage.Target
-------------
```
property TImage.Target: TTarget;
```
Returns a target which is targetted to the image.
*)

procedure ImportSimbaImage(Script: TSimbaScript);
begin
with Script.Compiler do
Expand Down Expand Up @@ -1935,11 +1956,14 @@ procedure ImportSimbaImage(Script: TSimbaScript);
addGlobalFunc('procedure TImage.FromLazBitmap(LazBitmap: TLazBitmap);', @_LapeImage_FromLazBitmap);

addGlobalFunc('function TImage.Fonts: TStringArray; static;', @_LapeImage_Fonts);
addGlobalFunc('function TImage.LoadFontsInDir(Dir: String): Boolean; static;', @_LapeImage_LoadFontsInDir);
addGlobalFunc('function TImage.LoadFonts(Dir: String): Boolean; static;', @_LapeImage_LoadFonts);

addGlobalFunc('procedure TImage.SaveUnfreedImagesInDir(Directory: String); static;', @_LapeImage_SaveUnfreedImagesInDir);
addGlobalFunc('function TImage.FindColor(Color: TColor; Tolerance: Single = 0): TPointArray;', @_LapeImage_FindColor);
addGlobalFunc('function TImage.FindImage(Image: TImage; Tolerance: Single = 0): TPoint;', @_LapeImage_FindImage);

DumpSection := '';

addGlobalFunc('function GetLoadedImages: TImageArray', @_LapeImage_GetLoadedImages);
end;
end;

Expand Down
2 changes: 1 addition & 1 deletion Source/script/imports/simba.import_target.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1229,9 +1229,9 @@ procedure ImportTarget(Script: TSimbaScript);
' Result.SetImage(Self);',
'end;'
]);

DumpSection := '';
end;
end;

end.

22 changes: 21 additions & 1 deletion Source/simba.baseclass.pas
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface
simba.base, simba.containers, simba.threading;

type

TSimbaBaseClass = class
protected
FName: String;
Expand All @@ -32,6 +33,8 @@ TSimbaBaseClass = class
property Name: String read GetName write SetName;
property FreeOnTerminate: Boolean read FFreeOnTerminate write FFreeOnTerminate;
end;
TSimbaBaseClassType = class of TSimbaBaseClass;
TSimbaBaseClassArray = array of TSimbaBaseClass;

TSimbaBaseThread = class(TThread)
protected
Expand All @@ -49,6 +52,8 @@ TSimbaBaseThread = class(TThread)
procedure PrintUnfinishedThreads;
procedure PrintUnfreedThreads;

function GetSimbaObjectsOfClass(ClassType: TSimbaBaseClassType): TSimbaBaseClassArray;

implementation

type
Expand Down Expand Up @@ -131,6 +136,22 @@ procedure PrintUnfreedThreads;
end;
end;

function GetSimbaObjectsOfClass(ClassType: TSimbaBaseClassType): TSimbaBaseClassArray;
var
I: Integer;
begin
Result := [];

TrackedObjects.Lock();
try
for I := 0 to TrackedObjects.Count - 1 do
if (TrackedObjects[I] is ClassType) then
Result := Result + [TrackedObjects[I]];
finally
TrackedObjects.UnLock();
end;
end;

procedure TSimbaBaseClass.NotifyUnfreed;
begin
DebugLn([EDebugLn.YELLOW], ' ' + ClassName + ' (' + HexStr(Self) + ')' + IfThen(Name <> '', ' "' + Name + '"', ''));
Expand Down Expand Up @@ -197,4 +218,3 @@ finalization
TrackedObjects.First.Free();

end.

53 changes: 24 additions & 29 deletions Source/simba.image.pas
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ TSimbaImage = class(TSimbaBaseClass)
procedure DrawDataAlpha(TheData: PColorBGRA; DataW, DataH: Integer; P: TPoint; Alpha: Byte);

procedure RaiseOutOfImageException(X, Y: Integer);
procedure NotifyUnfreed; override;

function GetPixel(const X, Y: Integer): TColor;
function GetAlpha(const X, Y: Integer): Byte;
Expand All @@ -77,7 +76,6 @@ TSimbaImage = class(TSimbaBaseClass)
procedure SetFontBold(Value: Boolean);
procedure SetFontItalic(Value: Boolean);
public
class var SaveUnfreedImages: ShortString;
class function LoadFontsInDir(Dir: String): Boolean;
class function Fonts: TStringArray;
public
Expand Down Expand Up @@ -247,20 +245,21 @@ TSimbaImage = class(TSimbaBaseClass)
function Save(FileName: String; OverwriteIfExists: Boolean = False): Boolean;
function SaveToString: String;

// Difference
function Equals(Other: TObject): Boolean; override;
function Equals(Other: TSimbaImage): Boolean; overload;

// Compare/Difference
function Equals(Other: TSimbaImage): Boolean; reintroduce;
function Compare(Other: TSimbaImage): Single;

function PixelDifference(Other: TSimbaImage): Integer; overload;
function PixelDifference(Other: TSimbaImage; Tolerance: Single): Integer; overload;
function PixelDifferenceTPA(Other: TSimbaImage): TPointArray; overload;
function PixelDifferenceTPA(Other: TSimbaImage): TPointArray; overload;
function PixelDifferenceTPA(Other: TSimbaImage; Tolerance: Single): TPointArray; overload;

// Laz bridge
function ToLazBitmap: TBitmap;
procedure FromLazBitmap(LazBitmap: TBitmap);

// Basic finders, use Target.SetTarget(img) for all
function FindColor(Color: TColor; Tolerance: Single): TPointArray;
function FindImage(Image: TSimbaImage; Tolerance: Single): TPoint;
end;

PSimbaImage = ^TSimbaImage;
Expand Down Expand Up @@ -611,14 +610,6 @@ function TSimbaImage.SaveToString: String;
Result := SimbaImage_ToString(Self);
end;

function TSimbaImage.Equals(Other: TObject): Boolean;
begin
if (Other is TSimbaImage) then
Result := Equals(TSimbaImage(Other))
else
Result := inherited Equals(Other);
end;

// Compare without alpha
function TSimbaImage.Equals(Other: TSimbaImage): Boolean;
var
Expand Down Expand Up @@ -845,6 +836,22 @@ procedure TSimbaImage.FromLazBitmap(LazBitmap: TBitmap);
TempBitmap.Free();
end;

function TSimbaImage.FindColor(Color: TColor; Tolerance: Single): TPointArray;
var
Target: TSimbaTarget;
begin
Target.SetImage(Self);
Result := Target.FindColor(Color, Tolerance, Target.Bounds);
end;

function TSimbaImage.FindImage(Image: TSimbaImage; Tolerance: Single): TPoint;
var
Target: TSimbaTarget;
begin
Target.SetImage(Self);
Result := Target.FindImage(Image, Tolerance, Target.Bounds);
end;

procedure TSimbaImage.DrawTPA(TPA: TPointArray);
begin
if (FDrawAlpha = ALPHA_OPAQUE) then
Expand Down Expand Up @@ -1885,19 +1892,6 @@ procedure TSimbaImage.RaiseOutOfImageException(X, Y: Integer);
SimbaException('%d,%d is outside the image bounds (0,0,%d,%d)', [X, Y, FWidth-1, FHeight-1]);
end;

procedure TSimbaImage.NotifyUnfreed;
begin
inherited NotifyUnfreed();

if (SaveUnfreedImages <> '') then
try
Save(IncludeTrailingPathDelimiter(SetDirSeparators(SaveUnfreedImages)) + IntToStr(PtrUInt(Self)) + '.bmp');
except
on E: Exception do
DebugLn(E.ToString);
end;
end;

function TSimbaImage.GetPixel(const X, Y: Integer): TColor;
begin
if (X < 0) or (Y < 0) or (X >= FWidth) or (Y >= FHeight) then
Expand Down Expand Up @@ -2048,3 +2042,4 @@ destructor TSimbaImage.Destroy;

end.


3 changes: 1 addition & 2 deletions Source/simba.target.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1166,8 +1166,7 @@ function TSimbaTarget.ToString: String;

class operator TSimbaTarget.Initialize(var Self: TSimbaTarget);
begin
Self := Default(TSimbaTarget);
FillByte(Self, SizeOf(TSimbaTarget), 0);
end;

end.

5 changes: 3 additions & 2 deletions Tests/externalcanvas.simba
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ begin

ExternalCanvas.EndUpdate();

Assert(Img.Target.FindColor($FFFFFF, 0).Bounds() = [10,10,90,90]);
Assert(Img.Target.FindColor($7F7F7F, 0).Bounds() = [5,5,95,95]);
Assert(Img.FindColor($FFFFFF, 0).Bounds() = [10,10,90,90]);
Assert(Img.FindColor($7F7F7F, 0).Bounds() = [5,5,95,95]);

ExternalCanvas.Free();
Img.Free();
end;

Loading

0 comments on commit 32e0785

Please sign in to comment.