Skip to content

Commit

Permalink
PixelOCR
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Jan 2, 2025
1 parent 208d711 commit 692e98e
Show file tree
Hide file tree
Showing 5 changed files with 869 additions and 4 deletions.
122 changes: 122 additions & 0 deletions Source/script/imports/simba.import_pixelocr.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
unit simba.import_pixelocr;

{$i simba.inc}

interface

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

procedure ImportPixelOCR(Script: TSimbaScript);

implementation

uses
lptypes, lpvartypes,
simba.pixelocr,
simba.image;

type
PPixelFont = ^TPixelFont;
PPixelOCR = ^TPixelOCR;

procedure _LapePixelOCR_LoadFont(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PPixelFont(Result)^ := PPixelOCR(Params^[0])^.LoadFont(PString(Params^[1])^, PInteger(Params^[2])^);
end;

procedure _LapePixelOCR_TextToTPA(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PPointArray(Result)^ := PPixelOCR(Params^[0])^.TextToTPA(PPixelFont(Params^[1])^, PString(Params^[2])^);
end;

procedure _LapePixelOCR_Locate(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PSingle(Result)^ := PPixelOCR(Params^[0])^.Locate(PSimbaImage(Params^[1])^, PPixelFont(Params^[2])^, PString(Params^[3])^);
end;

procedure _LapePixelOCR_Recognize1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PString(Result)^ := PPixelOCR(Params^[0])^.Recognize(PSimbaImage(Params^[1])^, PPixelFont(Params^[2])^, PPoint(Params^[3])^);
end;

procedure _LapePixelOCR_Recognize2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PString(Result)^ := PPixelOCR(Params^[0])^.Recognize(PSimbaImage(Params^[1])^, PPixelFont(Params^[2])^, PBox(Params^[3])^);
end;

procedure _LapePixelOCR_RecognizeLines(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PStringArray(Result)^ := PPixelOCR(Params^[0])^.RecognizeLines(PSimbaImage(Params^[1])^, PPixelFont(Params^[2])^, PBox(Params^[3])^);
end;

procedure ImportPixelOCR(Script: TSimbaScript);
begin
with Script.Compiler do
begin
addGlobalType([
'record',
' Glyphs: array of record',
' Value: Char;',
' ImgWidth: Integer;',
' ImgHeight: Integer;',
' Width: Integer;',
' Height: Integer;',
'',
' points: TPointArray;',
' shadow: TPointArray;',
' background: TPointArray;',
' backgrounddiv2: Integer;',
' BestMatch: Integer;',
'',
' InnerBounds: TBox;',
' end;',
'',
' SpaceWidth: Integer;',
' MaxGlyphHeight: Integer;',
' MaxGlyphWidth: Integer;',
'end;'],
'TPixelFont'
);

addGlobalType([
'record',
' Text: String;',
' Hits: Integer;',
' Bounds: TBox;',
'end;'],
'TPixelOCRMatch'
);

addGlobalType([
'record',
' Tolerance: Single;',
' ShadowTolerance: Single;',
' Whitelist: set of Char;',
' MaxWalk: Integer;',
' MaxLen: Integer;',
' Matches: array of TPixelOCRMatch;',
'end'],
'TPixelOCR'
);

if (getGlobalType('TPixelFont').Size <> SizeOf(TPixelFont)) then
SimbaException('TPixelFont import is wrong');
if (getGlobalType('TPixelOCRMatch').Size <> SizeOf(TPixelOCRMatch)) then
SimbaException('TPixelOCRMatch import is wrong');
if (getGlobalType('TPixelOCR').Size <> SizeOf(TPixelOCR)) then
SimbaException('TPixelOCR import is wrong');

addGlobalFunc('function TPixelOCR.LoadFont(dir: String; SpaceWidth: Integer): TPixelFont;', @_LapePixelOCR_LoadFont);
addGlobalFunc('function TPixelOCR.TextToTPA(font: TPixelFont; Text: String): TPointArray;', @_LapePixelOCR_TextToTPA);
addGlobalFunc('function TPixelOCR.Locate(img: TImage; font: TPixelFont; text: String): Single;', @_LapePixelOCR_Locate);

addGlobalFunc('function TPixelOCR.Recognize(img: TImage; font: TPixelFont; p: TPoint): String; overload;', @_LapePixelOCR_Recognize1);
addGlobalFunc('function TPixelOCR.Recognize(img: TImage; font: TPixelFont; bounds: TBox): String; overload;', @_LapePixelOCR_Recognize2);
addGlobalFunc('function TPixelOCR.RecognizeLines(img: TImage; font: TPixelFont; bounds: TBox): TStringArray;', @_LapePixelOCR_RecognizeLines);
end;
end;

end.

4 changes: 2 additions & 2 deletions Source/script/simba.script_imports.pas
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ implementation
simba.import_encoding, simba.import_file, simba.import_process,
simba.import_target, simba.import_math, simba.import_misc, simba.import_slacktree, simba.import_kdtree, simba.import_string,
simba.import_random, simba.import_debugimage, simba.import_web, simba.import_threading,
simba.import_async,
simba.import_async, simba.import_pixelocr,

// Simba shapes
simba.import_quad, simba.import_triangle, simba.import_box, simba.import_point, simba.import_circle,


// Simba classes
simba.import_image, simba.import_externalcanvas, simba.import_dtm, simba.import_matchtemplate,
Expand Down Expand Up @@ -89,6 +88,7 @@ procedure AddSimbaImports(Script: TSimbaScript);
ImportExternalCanvas(Script);
ImportMatchTemplate(Script);
ImportJSON(Script);
ImportPixelOCR(Script);

ImportDateTime(Script);
ImportEncoding(Script);
Expand Down
19 changes: 17 additions & 2 deletions Source/simba.image.pas
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ TSimbaImage = class(TSimbaBaseClass)
procedure Pad(Amount: Integer);
procedure Offset(X, Y: Integer);

function isBinary: Boolean;

function GetPixels(Points: TPointArray): TColorArray;
procedure SetPixels(Points: TPointArray; Color: TColor); overload;
procedure SetPixels(Points: TPointArray; Colors: TColorArray); overload;
Expand Down Expand Up @@ -258,8 +260,8 @@ TSimbaImage = class(TSimbaBaseClass)
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;
function FindColor(Color: TColor; Tolerance: Single = 0): TPointArray;
function FindImage(Image: TSimbaImage; Tolerance: Single = 0): TPoint;
end;

PSimbaImage = ^TSimbaImage;
Expand Down Expand Up @@ -1749,6 +1751,19 @@ function TSimbaImage.Blur(Algo: EImageBlurAlgo; Radius: Integer): TSimbaImage;
end;
end;

function TSimbaImage.isBinary: Boolean;
var
Ptr: PColorBGRA;
Upper: PtrUInt;
begin
Ptr := FData;
Upper := PtrUInt(FData) + FDataSize;
while (PtrUInt(Ptr) < Upper) and ((Ptr^.R = 0) and (Ptr^.G = 0) and (Ptr^.B = 0)) or ((Ptr^.R = 255) and (Ptr^.G = 255) and (Ptr^.B = 255)) do
Inc(Ptr);

Result := PtrUInt(Ptr) = Upper;
end;

function TSimbaImage.GetPixels(Points: TPointArray): TColorArray;
var
I: Integer;
Expand Down
Loading

0 comments on commit 692e98e

Please sign in to comment.