Skip to content

Commit ab20227

Browse files
committed
Fix RecognizeStatic
1 parent 01dc7e5 commit ab20227

File tree

7 files changed

+90
-61
lines changed

7 files changed

+90
-61
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,38 @@ jobs:
2121
config:
2222
- name: Windows 64
2323
os: windows-latest
24-
cpu: x86_64
2524
build-mode: WIN64
2625
binary: libsimpleocr64.dll
2726

2827
- name: Windows 32
2928
os: windows-latest
30-
cpu: i386
3129
build-mode: WIN32
3230
binary: libsimpleocr32.dll
3331

3432
- name: Linux 64
3533
os: ubuntu-latest
36-
cpu: x86_64
3734
build-mode: LINUX64
3835
binary: libsimpleocr64.so
3936

4037
- name: AArch64
4138
os: ubuntu-latest
42-
cpu: aarch64
4339
build-mode: AARCH64
4440
binary: libsimpleocr64.so.aarch64
4541

4642
- name: MacOS 64
4743
os: macos-latest
48-
cpu: x86_64
4944
build-mode: DARWIN64
5045
binary: libsimpleocr64.dylib
5146

5247
steps:
53-
- uses: actions/checkout@v2.3.4
48+
- uses: actions/checkout@v3.0.2
5449

5550
- name: Install Lazarus
56-
uses: ollydev/setup-lazarus-fpcup@v2.1
51+
uses: ollydev/setup-lazarus-fpcup@v2.5
5752
with:
58-
cpu: ${{ matrix.config.cpu }}
59-
laz-branch: lazarus_2_2_0_RC1
60-
fpc-branch: release_3_2_2_rc1
53+
laz-branch: lazarus_2_2_2
54+
fpc-branch: release_3_2_2
55+
fpcup-release: v2.2.0h
6156

6257
- name: Build SimpleOCR
6358
run: |
@@ -72,7 +67,7 @@ jobs:
7267
./tester.exe
7368
7469
- name: Upload Binary
75-
uses: actions/upload-artifact@v2.2.4
70+
uses: actions/upload-artifact@v2.3.1
7671
with:
7772
name: ${{ matrix.config.binary }}
7873
path: ${{ matrix.config.binary }}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
SimpleOCR is a Simba plugin for reading text in Old School RuneScape originally developed by [@slackydev](https://github.com/slackydev/SimpleOCR).
44

5-
The algorithm is very much designed for the games "blocky" text where no anti aliasing is applied. Every pixel of the glyph must match for for a character to be recognized.
5+
The algorithm is very much designed for OSRS (blocky text with no anti-aliasing). Every pixel of the glyph must match for for a character to be recognized.
66

7-
The actual character recognition is quite similar to calling Simba's `FindBitmap` for every character in the fontset.
7+
The actual character recognition is quite similar to using Simba's `FindBitmap` for every character in the fontset.
88

99
----
1010

SimpleOCR.lpr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ initialization
130130
'end;',
131131
'TSimpleOCR');
132132

133-
addGlobalFunc('procedure TFontSet.Load(constref Font: String; constref Space: Integer = 4); native;', @TFontSet_Load);
133+
addGlobalFunc('procedure TFontSet.Load(constref Font: String; Space: Integer = 4); native;', @TFontSet_Load);
134134

135135
addGlobalFunc('function TSimpleOCR.TextToMatrix(constref Text: String; constref Font: TFontSet): T2DIntegerArray; native;', @TSimpleOCR_TextToMatrix);
136136
addGlobalFunc('function TSimpleOCR.TextToTPA(constref Text: String; constref Font: TFontSet): TPointArray; native;', @TSimpleOCR_TextToTPA);
@@ -141,8 +141,8 @@ initialization
141141
addGlobalFunc('function TSimpleOCR._Recognize(constref Filter: TOCRFilter; constref Font: TFontSet): String; native;', @TSimpleOCR_Recognize);
142142
addGlobalFunc('function TSimpleOCR._RecognizeLines(constref Filter: TOCRFilter; constref Font: TFontSet; out Bounds: TBoxArray): TStringArray; overload; native;', @TSimpleOCR_RecognizeLines);
143143
addGlobalFunc('function TSimpleOCR._RecognizeLines(constref Filter: TOCRFilter; constref Font: TFontSet): TStringArray; overload; native;', @TSimpleOCR_RecognizeLinesEx);
144-
addGlobalFunc('function TSimpleOCR._RecognizeStatic(constref Filter: TOCRFilter; constref Font: TFontSet; constref MaxWalk: Integer = 20): String; native;', @TSimpleOCR_RecognizeStatic);
145-
addGlobalFunc('function TSimpleOCR._RecognizeUpText(constref Filter: TOCRFilter; constref Font: TFontSet; constref MaxWalk: Integer = 20): String; native;', @TSimpleOCR_RecognizeUpText);
144+
addGlobalFunc('function TSimpleOCR._RecognizeStatic(constref Filter: TOCRFilter; constref Font: TFontSet; MaxWalk: Integer = 20): String; native;', @TSimpleOCR_RecognizeStatic);
145+
addGlobalFunc('function TSimpleOCR._RecognizeUpText(constref Filter: TOCRFilter; constref Font: TFontSet; MaxWalk: Integer = 20): String; native;', @TSimpleOCR_RecognizeUpText);
146146

147147
addCode(
148148
'type TOCRUpTextFilter = type TOCRFilter; // 0 ' + LineEnding +
@@ -237,14 +237,14 @@ initialization
237237
' Result := Self._Recognize(Filter, Font); ' + LineEnding +
238238
'end; ' + LineEnding +
239239
' ' + LineEnding +
240-
'function TSimpleOCR.RecognizeStatic(constref Area: TBox; constref Filter: TOCRFilter; constref Font: TFontSet; constref MaxWalk: Integer = 20): String; ' + LineEnding +
240+
'function TSimpleOCR.RecognizeStatic(constref Area: TBox; constref Filter: TOCRFilter; constref Font: TFontSet; MaxWalk: Integer = 20): String; ' + LineEnding +
241241
'begin ' + LineEnding +
242242
' Self.Client := GetColorsMatrix(Area.X1, Area.Y1, Area.X2, Area.Y2); ' + LineEnding +
243243
' ' + LineEnding +
244244
' Result := Self._RecognizeStatic(Filter, Font, MaxWalk); ' + LineEnding +
245245
'end; ' + LineEnding +
246246
' ' + LineEnding +
247-
'function TSimpleOCR.RecognizeLines(constref Area: TBox; constref Filter: TOCRFilter; constref Font: TFontSet; var Bounds: TBoxArray): TStringArray; overload; ' + LineEnding +
247+
'function TSimpleOCR.RecognizeLines(constref Area: TBox; constref Filter: TOCRFilter; constref Font: TFontSet; out Bounds: TBoxArray): TStringArray; overload; ' + LineEnding +
248248
'var ' + LineEnding +
249249
' I: Integer; ' + LineEnding +
250250
'begin ' + LineEnding +
@@ -282,14 +282,14 @@ initialization
282282
' Result := StrToInt(Text); ' + LineEnding +
283283
'end; ' + LineEnding +
284284
' ' + LineEnding +
285-
'function TSimpleOCR.RecognizeUpText(const Area: TBox; const Filter: TOCRFilter; const Font: TFontSet; const MaxWalk: Integer = 20): String; ' + LineEnding +
285+
'function TSimpleOCR.RecognizeUpText(constref Area: TBox; constref Filter: TOCRFilter; constref Font: TFontSet; MaxWalk: Integer = 20): String; ' + LineEnding +
286286
'begin ' + LineEnding +
287287
' Self.Client := GetColorsMatrix(Area.X1, Area.Y1, Area.X2, Area.Y2); ' + LineEnding +
288288
' ' + LineEnding +
289289
' Result := Self._RecognizeUpText(Filter, Font, MaxWalk); ' + LineEnding +
290290
'end; ' + LineEnding +
291291
' ' + LineEnding +
292-
'function TSimpleOCR.LocateText(constref Area: TBox; constref Text: String; constref Font: TFontSet; constref Filter: TOCRFilter; out Bounds: TBox): Single; overload; ' + LineEnding +
292+
'function TSimpleOCR.LocateText(constref Area: TBox; Text: String; constref Font: TFontSet; constref Filter: TOCRFilter; out Bounds: TBox): Single; overload; ' + LineEnding +
293293
'begin ' + LineEnding +
294294
' Self.Client := GetColorsMatrix(Area.X1, Area.Y1, Area.X2, Area.Y2); ' + LineEnding +
295295
' ' + LineEnding +
@@ -301,7 +301,7 @@ initialization
301301
' Bounds.Y2 += Area.Y1; ' + LineEnding +
302302
'end; ' + LineEnding +
303303
' ' + LineEnding +
304-
'function TSimpleOCR.LocateText(constref Area: TBox; constref Text: String; constref Font: TFontSet; out Bounds: TBox): Single; overload; ' + LineEnding +
304+
'function TSimpleOCR.LocateText(constref Area: TBox; Text: String; constref Font: TFontSet; out Bounds: TBox): Single; overload; ' + LineEnding +
305305
'begin ' + LineEnding +
306306
' Self.Client := GetColorsMatrix(Area.X1, Area.Y1, Area.X2, Area.Y2); ' + LineEnding +
307307
' ' + LineEnding +

simpleocr.engine.pas

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ interface
8686
end;
8787

8888
MinCharacterMatch: Char;
89+
90+
function IsEmpty: Boolean;
91+
class function Empty: TOCRFilter; static;
8992
end;
9093

9194
PSimpleOCR = ^TSimpleOCR;
@@ -97,8 +100,7 @@ interface
97100
FHeight: Integer;
98101
FSearchArea: TBox;
99102

100-
function Init(const FontSet: TFontSet; const Static: Boolean): Boolean;
101-
function Init(const FontSet: TFontSet; const Filter: TOCRFilter): Boolean;
103+
function Init(const FontSet: TFontSet; const Filter: TOCRFilter; Static: Boolean): Boolean;
102104

103105
function _RecognizeX(Bounds: TBox; const MinCharacterCount, MaxWalk: Integer; out TextHits: Integer; out TextBounds: TBox): String;
104106
function _RecognizeXY(Bounds: TBox; const MinCharacterCount, MaxWalk: Integer; out TextHits: Integer; out TextBounds: TBox): String;
@@ -124,6 +126,16 @@ implementation
124126
graphtype, intfgraphics, graphics, lazfileutils, math,
125127
simpleocr.filters;
126128

129+
function TOCRFilter.IsEmpty: Boolean;
130+
begin
131+
Result := CompareMem(@Self, @Self.Empty, SizeOf(Self));
132+
end;
133+
134+
class function TOCRFilter.Empty: TOCRFilter;
135+
begin
136+
Result := Default(TOCRFilter);
137+
end;
138+
127139
function TFontSet.GetCharacterPoints(Character: Char): Integer;
128140
begin
129141
if (Character in [FONTSET_START..FONTSET_END]) then
@@ -163,6 +175,11 @@ procedure TFontSet.Load(FontPath: String; Space: Integer);
163175
FontChar: TFontCharacter;
164176
begin
165177
FontPath := IncludeTrailingPathDelimiter(ExpandFileName(FontPath));
178+
if (not DirectoryExists(FontPath)) then
179+
begin
180+
WriteLn('TFontSet.Load: Font does not exist "' + FontPath + '"');
181+
Halt(1);
182+
end;
166183

167184
Self := Default(TFontSet);
168185
Self.Name := ExtractFileNameOnly(FontPath);
@@ -227,17 +244,33 @@ procedure TFontSet.Load(FontPath: String; Space: Integer);
227244
Image.Free();
228245
end;
229246

230-
function TSimpleOCR.Init(const FontSet: TFontSet; const Static: Boolean): Boolean;
247+
function TSimpleOCR.Init(const FontSet: TFontSet; const Filter: TOCRFilter; Static: Boolean): Boolean;
231248
begin
232249
Result := MatrixDimensions(FClient, FWidth, FHeight);
233250

234251
if Result then
235252
begin
236253
FFontSet := FontSet;
237-
FSearchArea := Box(0, 0, FWidth - 1, FHeight - 1);
238254

239-
if not Static then
255+
if not Filter.IsEmpty() then
256+
begin
257+
case Filter.FilterType of
258+
EOCRFilterType.COLOR, EOCRFilterType.INVERT_COLOR:
259+
Result := SimpleOCRFilter.ApplyColorRule(FClient, TColorRuleArray(Filter.ColorRule.Colors), Filter.ColorRule.Invert, FSearchArea);
260+
261+
EOCRFilterType.THRESHOLD:
262+
Result := SimpleOCRFilter.ApplyThresholdRule(FClient, Filter.ThresholdRule.Invert, Filter.ThresholdRule.Amount, FSearchArea);
263+
264+
EOCRFilterType.SHADOW:
265+
Result := SimpleOCRFilter.ApplyShadowRule(FClient, Filter.ShadowRule.MaxShadowValue, Filter.ShadowRule.Tolerance, FSearchArea);
266+
end;
267+
end;
268+
269+
if Static then
270+
FSearchArea := Box(0, 0, FWidth - 1, FHeight - 1)
271+
else
240272
begin
273+
// Filter sets the bounds
241274
FSearchArea.X1 -= FontSet.MaxWidth div 2;
242275
FSearchArea.Y1 -= FFontSet.MaxHeight div 2;
243276
FSearchArea.X2 += FontSet.MaxWidth div 2;
@@ -246,33 +279,6 @@ function TSimpleOCR.Init(const FontSet: TFontSet; const Static: Boolean): Boolea
246279
end;
247280
end;
248281

249-
function TSimpleOCR.Init(const FontSet: TFontSet; const Filter: TOCRFilter): Boolean;
250-
begin
251-
Result := MatrixDimensions(FClient, FWidth, FHeight);
252-
253-
if Result then
254-
begin
255-
FFontSet := FontSet;
256-
257-
case Filter.FilterType of
258-
EOCRFilterType.COLOR,
259-
EOCRFilterType.INVERT_COLOR:
260-
Result := SimpleOCRFilter.ApplyColorRule(FClient, TColorRuleArray(Filter.ColorRule.Colors), Filter.ColorRule.Invert, FSearchArea);
261-
262-
EOCRFilterType.THRESHOLD:
263-
Result := SimpleOCRFilter.ApplyThresholdRule(FClient, Filter.ThresholdRule.Invert, Filter.ThresholdRule.Amount, FSearchArea);
264-
265-
EOCRFilterType.SHADOW:
266-
Result := SimpleOCRFilter.ApplyShadowRule(FClient, Filter.ShadowRule.MaxShadowValue, Filter.ShadowRule.Tolerance, FSearchArea);
267-
end;
268-
269-
FSearchArea.X1 -= FontSet.MaxWidth div 2;
270-
FSearchArea.Y1 -= FFontSet.MaxHeight div 2;
271-
FSearchArea.X2 += FontSet.MaxWidth div 2;
272-
FSearchArea.Y2 += FFontSet.MaxHeight div 2;
273-
end;
274-
end;
275-
276282
function TSimpleOCR._RecognizeX(Bounds: TBox; const MinCharacterCount, MaxWalk: Integer; out TextHits: Integer; out TextBounds: TBox): String;
277283

278284
function CompareChar(const Character: TFontCharacter; const OffsetX, OffsetY: Integer): Integer; inline;
@@ -521,7 +527,7 @@ function TSimpleOCR.LocateText(const Text: String; const FontSet: TFontSet; out
521527
TextMatrix := Self.TextToMatrix(Text, FontSet);
522528
if not MatrixDimensions(TextMatrix, TextWidth, TextHeight) then
523529
Exit;
524-
if not Self.Init(FontSet, False) then
530+
if not Self.Init(FontSet, TOCRFilter.Empty, True) then
525531
Exit;
526532

527533
SetLength(CharacterIndices, TextWidth * TextHeight);
@@ -609,7 +615,7 @@ function TSimpleOCR.LocateText(const Text: String; const FontSet: TFontSet; out
609615
function TSimpleOCR.LocateText(const Text: String; const FontSet: TFontSet; const Filter: TOCRFilter; out Bounds: TBox): Single;
610616
begin
611617
Result := 0;
612-
if Self.Init(FontSet, Filter) then
618+
if Self.Init(FontSet, Filter, True) then
613619
Result := LocateText(Text, FontSet, Bounds);
614620
end;
615621

@@ -619,7 +625,7 @@ function TSimpleOCR.Recognize(const Filter: TOCRFilter; const FontSet: TFontSet)
619625
Bounds: TBox;
620626
begin
621627
Result := '';
622-
if Self.Init(FontSet, Filter) then
628+
if Self.Init(FontSet, Filter, False) then
623629
Result := _RecognizeXY(FSearchArea, FontSet.CharacterPoints[Filter.MinCharacterMatch], $FFFFFF, Hits, Bounds);
624630
end;
625631

@@ -719,7 +725,7 @@ function TSimpleOCR.RecognizeUpText(const Filter: TOCRFilter; const FontSet: TFo
719725
Halt(1);
720726
end;
721727

722-
if Self.Init(FontSet, True) then
728+
if Self.Init(FontSet, TOCRFilter.Empty, True) then
723729
begin
724730
MinPointsNeeded := FontSet.CharacterPoints[Filter.MinCharacterMatch];
725731
Space := 0;
@@ -767,7 +773,7 @@ function TSimpleOCR.RecognizeStatic(const Filter: TOCRFilter; const FontSet: TFo
767773
Bounds: TBox;
768774
begin
769775
Result := '';
770-
if Self.Init(FontSet, Filter) then
776+
if Self.Init(FontSet, Filter, True) then
771777
Result := Self._RecognizeX(FSearchArea, FontSet.CharacterPoints[Filter.MinCharacterMatch], MaxWalk, Hits, Bounds);
772778
end;
773779

@@ -781,7 +787,7 @@ function TSimpleOCR.RecognizeLines(const Filter: TOCRFilter; const FontSet: TFon
781787
Result := nil;
782788
TextBounds := nil;
783789

784-
if Self.Init(FontSet, Filter) then
790+
if Self.Init(FontSet, Filter, False) then
785791
begin
786792
MinCharacterPoints := FontSet.CharacterPoints[','];
787793

test/images/chat.png

-18.1 KB
Loading

test/images/options.png

18.4 KB
Loading

0 commit comments

Comments
 (0)