@@ -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+
127139function TFontSet.GetCharacterPoints (Character: Char): Integer;
128140begin
129141 if (Character in [FONTSET_START..FONTSET_END]) then
@@ -163,6 +175,11 @@ procedure TFontSet.Load(FontPath: String; Space: Integer);
163175 FontChar: TFontCharacter;
164176begin
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();
228245end ;
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;
231248begin
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 ;
247280end ;
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-
276282function 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
609615function TSimpleOCR.LocateText (const Text: String; const FontSet: TFontSet; const Filter: TOCRFilter; out Bounds: TBox): Single;
610616begin
611617 Result := 0 ;
612- if Self.Init(FontSet, Filter) then
618+ if Self.Init(FontSet, Filter, True ) then
613619 Result := LocateText(Text, FontSet, Bounds);
614620end ;
615621
@@ -619,7 +625,7 @@ function TSimpleOCR.Recognize(const Filter: TOCRFilter; const FontSet: TFontSet)
619625 Bounds: TBox;
620626begin
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);
624630end ;
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;
768774begin
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);
772778end ;
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
0 commit comments