-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocr.pas
1416 lines (1148 loc) · 37.8 KB
/
ocr.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
This file is part of the Mufasa Macro Library (MML)
Copyright (c) 2009-2012 by Raymond van Venetië and Merlijn Wajer
MML is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MML is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MML. If not, see <http://www.gnu.org/licenses/>.
See the file COPYING, included in this distribution,
for details about the copyright.
OCR class for the Mufasa Macro Library
}
unit ocr;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, MufasaTypes,MufasaBase, bitmaps, math, ocrutil, fontloader,
{Begin To-Remove units. Replace ReadBmp with TMufasaBitmap stuff later.}
graphtype, intfgraphics,graphics;
{End To-Remove unit}
(*
.. _mmlref-ocr:
TMOCR Class
===========
The TMOCR class uses the powerful ``ocrutil`` unit to create some default but
useful functions that can be used to create and identify text. It also contains
some functions used in special cases to filter noise. Specifically, these are
all the ``Filter*`` functions.
*)
type
{ TMOCR }
TMOCR = class(TObject)
private
Client: TObject;
FFonts: TMFonts;
filterdata: TOCRFilterDataArray;
function GetFonts:TMFonts;
procedure SetFonts(const NewFonts: TMFonts);
public
constructor Create(Owner: TObject);
destructor Destroy; override;
function InitTOCR(const path: string): boolean;
function getTextPointsIn(sx, sy, w, h: Integer; shadow: boolean;
var _chars, _shadows: T2DPointArray): Boolean;
function GetUpTextAtEx(atX, atY: integer; shadow: boolean; fontname: string): string;
function GetUpTextAt(atX, atY: integer; shadow: boolean): string;
procedure CreateDefaultFilter;
procedure SetFilter(filter: TOCRFilterDataArray);
procedure ResetFilter;
procedure FilterUpTextByColour(bmp: TMufasaBitmap);
procedure FilterUpTextByCharacteristics(bmp: TMufasaBitmap);
procedure FilterUpTextByColourMatches(bmp: TMufasaBitmap);
procedure FilterShadowBitmap(bmp: TMufasaBitmap);
procedure FilterCharsBitmap(bmp: TMufasaBitmap);
function GetTextAt(atX, atY, minvspacing, maxvspacing, hspacing,
color, tol, len: integer; font: string): string;overload;
function GetTextAt(xs, ys, xe, ye, minvspacing, maxvspacing, hspacing,
color, tol: integer; font: string): string;overload;
function GetTextATPA(const ATPA: T2DPointArray; const maxvspacing: integer; font: string): string;
function TextToFontTPA(Text, font: String; out w, h: integer): TPointArray;
function TextToFontBitmap(Text, font: String): TMufasaBitmap;
function TextToMask(Text, font: String): TMask;
property Fonts : TMFonts read GetFonts write SetFonts;
{$IFDEF OCRDEBUG}
procedure DebugToBmp(bmp: TMufasaBitmap; hmod,h: integer);
public
debugbmp: TMufasaBitmap;
{$ENDIF}
end;
{$IFDEF OCRDEBUG}
{$IFDEF LINUX}
const OCRDebugPath = '/tmp/';
{$ELSE}
const OCRDebugPath = '';
{$ENDIF}
{$ENDIF}
implementation
uses
colour_conv, client, files, tpa, mufasatypesutil, iomanager;
const
{ Very rough limits for R, G, B }
ocr_Limit_High = 190;
ocr_Limit_Med = 130;
ocr_Limit_Low = 65;
{ `base' Colours of the Uptext }
{ White }
ocr_White = 16777215;
{ Level < Your Level }
ocr_Green = 65280;
{ Level > Your Level }
ocr_Red = 255;
{ Interact or Level = Your Level }
ocr_Yellow = 65535;
{ Object }
ocr_Blue = 16776960;
{ Item }
ocr_ItemC = 16744447;
{ Item }
ocr_ItemC2 = clRed or clGreen;
{ Shadow }
ocr_Purple = 8388736;
const
OF_LN = 256;
OF_HN = -1;
{ Constructor }
constructor TMOCR.Create(Owner: TObject);
begin
inherited Create;
Self.Client := Owner;
Self.FFonts := TMFonts.Create(Owner);
CreateDefaultFilter;
end;
{ Destructor }
destructor TMOCR.Destroy;
begin
SetLength(filterdata, 0);
Self.FFonts.Free;
inherited Destroy;
end;
(*
InitTOCR
~~~~~~~~
.. code-block:: pascal
function TMOCR.InitTOCR(const path: string): boolean;
InitTOCR loads all fonts in path
We don't do this in the constructor because we may not yet have the path.
*)
function TMOCR.InitTOCR(const path: string): boolean;
var
dirs: array of string;
i: longint;
{$IFDEF FONTDEBUG}
fonts_loaded: String = '';
{$ENDIF}
begin
// We're going to load all fonts now
FFonts.Path := path;
dirs := GetDirectories(path);
Result := false;
for i := 0 to high(dirs) do
begin
{$IFDEF FONTDEBUG}
// REMOVING THIS WILL CAUSE FONTS_LOADED NOT BE ADDED SOMEHOW?
writeln('Loading: ' + dirs[i]);
{$ENDIF}
if FFonts.LoadFont(dirs[i], false) then
begin
{$IFDEF FONTDEBUG}
fonts_loaded := fonts_loaded + dirs[i] + ', ';
{$ENDIF}
result := true;
end;
end;
{$IFDEF FONTDEBUG}
if length(fonts_loaded) > 2 then
begin
writeln(fonts_loaded);
setlength(fonts_loaded,length(fonts_loaded)-2);
TClient(Self.Client).WriteLn('Loaded fonts: ' + fonts_loaded);
end;
{$ENDIF}
{ TODO FIXME CHANGE TO NEW CHARS? }
If DirectoryExists(path + 'UpChars') then
FFonts.LoadFont('UpChars', true); // shadow
end;
{ Get the current pointer to our list of Fonts }
function TMOCR.GetFonts:TMFonts;
begin
Result := Self.FFonts;
end;
{ Set new Fonts. We set it to a Copy of NewFonts }
procedure TMOCR.SetFonts(const NewFonts: TMFonts);
begin
if (Self.FFonts <> nil) then
Self.FFonts.Free;
Self.FFonts := NewFonts.Copy(Self.Client);
end;
{
Filter UpText by a very rough colour comparison / range check.
We first convert the colour to RGB, and if it falls into the following
defined ranges, it may be part of the uptext. Also get the possible
shadows.
We have large ranges because we rather have extra (fake) pixels than less
uptext pixels... This because we can filter most of the noise out easily.
Non optimised. We can make it use direct data instead of fastgetpixel and
fastsetpixel, but speed isn't really an issue. The entire algorithm is still
fast enough.
There are some issues with this method; since later on the algorithm
(if I recall correctly) throws aways some pixels if a character isn't just
one colour)
We will match shadow as well; we need it later on.
}
{
We're going to filter the bitmap solely on colours first.
If we found one, we set it to it's `normal' colour.
Note that these values aren't randomly chosen, but I didn't spent too
much time on finding the exact values either.
We will iterate over each pixel in the bitmap, and if it matches any of the
``rules'' for the colour; we will set it to a constant colour which
represents this colour (and corresponding rule). Usually the ``base''
colour.
If it doesn't match any rule, we can safely make the pixel black.
To my knowledge this algorithm doesn't remove any valid points. It does
not remove *all* invalid points either; but that is simply not possible
based purely on the colour. (If someone has a good idea, let me know)
}
(*
FilterUpTextByColour
~~~~~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure TMOCR.FilterUpTextByColour(bmp: TMufasaBitmap);
*)
procedure TMOCR.CreateDefaultFilter;
function load0(r_low,r_high,g_low,g_high,b_low,b_high,set_col: integer;
is_text_color: boolean): tocrfilterdata;
begin
result.r_low := r_low;
result.r_high := r_high;
result.g_low := g_low;
result.g_high := g_high;
result.b_low := b_low;
result.b_high := b_high;
result.set_col := set_col;
result._type := 0;
result.is_text_color:= is_text_color;
end;
begin
setlength(filterdata, 9);
filterdata[0] := load0(65, OF_HN, OF_LN, 190, OF_LN, 190, ocr_Blue, True); // blue
filterdata[1] := load0(65, OF_HN, OF_LN, 190, 65, OF_HN, ocr_Green, True); // green
filterdata[2] := load0(OF_LN, 190, 220, 100, 127, 40, ocr_ItemC, True); // itemC
filterdata[3] := load0(OF_LN, 190, OF_LN, 190, 65, OF_HN, ocr_Yellow, True); // yellow
filterdata[4] := load0(OF_LN, 190, 65, OF_HN, 65, OF_HN, ocr_Red, True); // red
filterdata[5] := load0(OF_LN, 190, OF_LN, 65, 65, OF_HN, ocr_Red, True); // red 2
filterdata[6] := load0(190 + 10, 130, OF_LN, 65 - 10, 20, OF_HN, ocr_Green, True); // green 2
filterdata[7] := load0(190, 140, 210, 150, 200, 160, ocr_ItemC2, True); // item2, temp item_c
filterdata[8] := load0(65, OF_HN, 65, OF_HN, 65, OF_HN, ocr_Purple, False); // shadow
end;
procedure TMOCR.SetFilter(filter: TOCRFilterDataArray);
begin
SetLength(filterdata, 0);
filterdata := copy(filter);
end;
procedure TMOCR.ResetFilter;
begin
SetLength(filterdata, 0);
CreateDefaultFilter;
end;
procedure TMOCR.FilterUpTextByColour(bmp: TMufasaBitmap);
var
x, y,r, g, b, i: Integer;
found: Boolean;
begin
for y := 0 to bmp.Height - 1 do
for x := 0 to bmp.Width - 1 do
begin
colortorgb(bmp.fastgetpixel(x,y),r,g,b);
{ white, need this for now since it cannot be expressed in filterocrdata }
if (r > ocr_Limit_High) and (g > ocr_Limit_High) and (b > ocr_Limit_High)
and (abs(r-g) + abs(r-b) + abs(g-b) < 55) then
begin
bmp.fastsetpixel(x,y,ocr_White);
continue;
end;
found := False;
for i := 0 to high(filterdata) do
begin
if filterdata[i]._type = 0 then
if (r < filterdata[i].r_low) and (r > filterdata[i].r_high) and (g < filterdata[i].g_low)
and (g > filterdata[i].g_high) and (b < filterdata[i].b_low) and (b > filterdata[i].b_high) then
begin
bmp.fastsetpixel(x, y, filterdata[i].set_col);
found := True;
break;
end;
{ else if type = 1 }
{ XXX TODO ^ }
end;
if found then
continue;
// Black if no match
bmp.fastsetpixel(x, y ,0);
end;
{
Make outline black for shadow characteristics filter
First and last horiz line = 0. This makes using the shadow algo easier.
We don't really throw away information either since we already took the
bitmap a bit larger than required.
}
for x := 0 to bmp.width -1 do
bmp.fastsetpixel(x,0,0);
for x := 0 to bmp.width -1 do
bmp.fastsetpixel(x,bmp.height-1,0);
// Same for vertical lines
for y := 0 to bmp.Height -1 do
bmp.fastsetpixel(0, y, 0);
for y := 0 to bmp.Height -1 do
bmp.fastsetpixel(bmp.Width-1, y, 0);
end;
{
This filter assumes the previous colour filter has been applied first.
I like to call this the `characteristics' filter because we really only filter
on characteristics.
For the uptext, a few things apply...
*** Remove False Shadow ***
if shadow[x,y] then not shadow[x-1,y-1]
If there is a shadow at x,y then there should not be a shadow at x-1, y-1; if
there is one, then shadow[x, y] is not a shadow.
(One could also say, if shadow[x,y] and shadow[x+1,y+1] then shadow[x+1,y+1]
is no shadow; because it essentially means the same. However, a smart mind
will soon see that this algorithm will be a *lot* more efficient if we
start at the right bottom, instead of the left top. Which means we should
work with x-1 and y-1, rather than x+1,y+1
)
*** UpText chars identity 1 and 2 ***
if UpTextChar[x,y] then (UpTextChar[x+1,y+1] or shadow[x+1,y+1])
If this is not true, then UpTextChar[x,y] cannot be part of uptext - it
has no shadow, and it doesn't have a `friend' (at x+1,y+1) either.
We don't need to do this from the right bottom to left top.
}
(*
FilterUpTextByCharacteristics
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure TMOCR.FilterUpTextByCharacteristics(bmp: TMufasaBitmap; w,h: integer);
*)
procedure TMOCR.FilterUpTextByCharacteristics(bmp: TMufasaBitmap);
var
x,y: Integer;
begin
{ Filter 2
This performs a `simple' filter.
What we are doing here is simple checking that if Colour[x,y] is part
of the uptext, then so must Colour[x+1,y+1], or Colour[x+1,y+1] is a shadow.
if it is neither, we can safely remove it.
XXX: This causes the previously mentioned fuckup.
}
for y := 0 to bmp.Height - 2 do
for x := 0 to bmp.Width - 2 do
begin
if bmp.fastgetpixel(x,y) = clPurple then
continue;
if bmp.fastgetpixel(x,y) = clBlack then
continue;
if (bmp.fastgetpixel(x,y) <> bmp.fastgetpixel(x+1,y+1)) and
(bmp.fastgetpixel(x+1,y+1) <> clpurple) then
bmp.fastsetpixel(x,y,{clAqua}0);
end;
{
Remove false shadow. (A shadow isn't larger than 1 pixel. So if x, y is
shadow, thex x+1, y+1 can't be shadow. If it is, we can safely remove it.
(As well as x+2, y+2, etc).
The tricky part of the algorithm is that it starts at the bottom,
removing shadow point x,y if x-1,y-1 is also shadow.
}
for y := bmp.Height - 1 downto 1 do
for x := bmp.Width - 1 downto 1 do
begin
if bmp.fastgetpixel(x,y) <> clPurple then
continue;
if bmp.fastgetpixel(x,y) = bmp.fastgetpixel(x-1,y-1) then
begin
bmp.fastsetpixel(x,y,clSilver);
continue;
end;
if bmp.fastgetpixel(x-1,y-1) = 0 then
bmp.fastsetpixel(x,y,clSilver);
end;
// Now we do another filter, with uptext chars identity 1 and 2.
for y := bmp.Height - 2 downto 0 do
for x := bmp.Width - 2 downto 0 do
begin
if bmp.fastgetpixel(x,y) = clPurple then
continue;
if bmp.fastgetpixel(x,y) = clBlack then
continue;
// identity 1
if (bmp.fastgetpixel(x,y) = bmp.fastgetpixel(x+1,y+1) ) then
continue;
// identity 2
if bmp.fastgetpixel(x+1,y+1) <> clPurple then
begin
bmp.fastsetpixel(x,y,clOlive);
continue;
end;
// If we make it to here, it means the pixel is part of the uptext.
end;
end;
{$IFDEF OCRDEBUG}
{ Write to our debugbmp }
procedure TMOCR.DebugToBmp(bmp: TMufasaBitmap; hmod, h: integer);
var
x,y: integer;
begin
for y := 0 to bmp.height - 1 do
for x := 0 to bmp.width - 1 do
debugbmp.fastsetpixel(x,y + hmod *h,bmp.fastgetpixel(x,y));
end;
{$ENDIF}
{
Return the shadows of the points in charpoint on bitmap shadowsbmp.
Pseudo:
if shadow[charpoint[i].x+1, charpoint[i].y+1] then addtoResult;
}
function getshadows(shadowsbmp:TMufasaBitmap; charpoint: tpointarray): tpointarray;
var
i,c:integer;
begin
setlength(result,length(charpoint));
c:=0;
for i := 0 to high(charpoint) do
begin
if shadowsbmp.fastgetpixel(charpoint[i].x+1,charpoint[i].y+1) = clPurple then
begin
result[c]:=point(charpoint[i].x+1, charpoint[i].y+1);
inc(c);
end;
end;
setlength(result,c);
end;
procedure TMOCR.FilterUpTextByColourMatches(bmp: TMufasaBitmap);
var
TPA: TPointArray;
ATPA: T2DPointArray;
OldTarget: Integer;
c, i, j, k: Integer;
begin
TClient(Client).IOManager.GetImageTarget(OldTarget);
TClient(Client).IOManager.SetTarget(bmp);
for i := 0 to high(filterdata) do
begin
if not filterdata[i].is_text_color then
continue;
c := filterdata[i].set_col;
TClient(Client).MFinder.FindColors(TPA, c, 0, 0, bmp.width - 1, bmp.height - 1);
ATPA := SplitTPAEx(TPA, 2, 6);
for j := 0 to high(atpa) do
begin
if length(ATPA[j]) > 70 then
for k := 0 to high(ATPA[j]) do
bmp.FastSetPixel(ATPA[j][k].x, ATPA[j][k].y, 0);
if length(ATPA[j]) < 4 then
for k := 0 to high(ATPA[j]) do
bmp.FastSetPixel(ATPA[j][k].x, ATPA[j][k].y, 0);
end;
end;
TClient(Client).IOManager.SetImageTarget(OldTarget);
end;
(*
FilterShadowBitmap
~~~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure TMOCR.FilterShadowBitmap(bmp: TMufasaBitmap);
Remove anything but the shadows on the bitmap (Shadow = clPurple)
*)
procedure TMOCR.FilterShadowBitmap(bmp: TMufasaBitmap);
var
x,y:integer;
begin
for y := 0 to bmp.Height - 1 do
for x := 0 to bmp.Width - 1 do
begin
if bmp.fastgetpixel(x,y) <> clPurple then
begin
bmp.FastSetPixel(x,y,0);
continue;
end;
end;
end;
(*
FilterCharsBitmap
~~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure TMOCR.FilterCharsBitmap(bmp: TMufasaBitmap);
Remove all but uptext colours clWhite,clGreen, etc.
This assumes that the bitmap only consists of colour 0, and the other
constants founds above the functions
*)
procedure TMOCR.FilterCharsBitmap(bmp: TMufasaBitmap);
var
x,y: integer;
begin
begin
for y := 0 to bmp.Height - 1 do
for x := 0 to bmp.Width - 1 do
begin
if bmp.fastgetpixel(x,y) = clPurple then
begin
bmp.FastSetPixel(x,y,0);
continue;
end;
if bmp.fastgetpixel(x,y) = clOlive then
begin
bmp.FastSetPixel(x,y,0);
continue;
end;
if bmp.fastgetpixel(x,y) = clSilver then
begin
bmp.FastSetPixel(x,y,0);
continue;
end;
end;
end;
end;
{
}
(*
getTextPointsIn
~~~~~~~~~~~~~~~
.. code-block:: pascal
function TMOCR.getTextPointsIn(sx, sy, w, h: Integer; shadow: boolean;
var _chars, _shadows: T2DPointArray): Boolean;
This uses the two filters, and performs a split on the bitmap.
A split per character, that is. So we can more easily identify it.
TODO:
*
Remove more noise after we have split, it should be possible to identify
noise; weird positions or boxes compared to the rest, etc.
*
Split each colours seperately, and combine only later, after removing noise.
*)
function TMOCR.getTextPointsIn(sx, sy, w, h: Integer; shadow: boolean;
var _chars, _shadows: T2DPointArray): Boolean;
var
bmp, shadowsbmp, charsbmp: TMufasaBitmap;
x,y: integer;
{$IFDEF OCRDEBUG}
dx,dy: integer;
{$ENDIF}
shadows: T2DPointArray;
helpershadow: TPointArray;
chars: TPointArray;
charscount: integer;
chars_2d, chars_2d_b, finalchars: T2DPointArray;
pc: integer;
bb: Tbox;
begin
bmp := TMufasaBitmap.Create;
{ Increase to create a black horizonal line at the top and at the bottom }
{ This so the crappy algo can do it's work correctly. }
bmp.SetSize(w + 2, h + 2);
// Copy the client to out working bitmap.
bmp.CopyClientToBitmap(TClient(Client).IOManager, False, 1{0},1, sx, sy, sx + w - 1, sy + h - 1);
{$IFDEF OCRSAVEBITMAP}
bmp.SaveToFile(OCRDebugPath + 'ocrinit.bmp');
{$ENDIF}
{$IFDEF OCRDEBUG}
debugbmp := TMufasaBitmap.Create;
debugbmp.SetSize(w + 2, (h + 2) * 8);
debugbmp.FastDrawClear(clBlack);
{$ENDIF}
{$IFDEF OCRDEBUG}
DebugToBmp(bmp,0,h);
{$ENDIF}
{
Filter 1, remove most colours we don't want.
}
FilterUpTextByColour(bmp);
{$IFDEF OCRSAVEBITMAP}
bmp.SaveToFile(OCRDebugPath + 'ocrcol.bmp');
{$ENDIF}
{$IFDEF OCRDEBUG}
DebugToBmp(bmp,1,h);
{$ENDIF}
// new filter
FilterUpTextByColourMatches(bmp);
{$IFDEF OCRSAVEBITMAP}
bmp.SaveToFile(OCRDebugPath + 'ocrcolourmatches.bmp');
{$ENDIF}
{$IFDEF OCRDEBUG}
DebugToBmp(bmp,2,h);
{$ENDIF}
{
Filter 2, remove all false shadow points and points that have no shadow
after false shadow is removed.
}
// Filter 2
FilterUpTextByCharacteristics(bmp);
{$IFDEF OCRSAVEBITMAP}
bmp.SaveToFile(OCRDebugPath + 'ocrdebug.bmp');
{$ENDIF}
{$IFDEF OCRDEBUG}
DebugToBmp(bmp,3,h);
{$ENDIF}
{
We've now passed the two filters.
We need to extract the characters and return a TPA of each character
}
{ Create a bitmap with only the shadows on it }
shadowsbmp := bmp.copy;
FilterShadowBitmap(shadowsbmp);
{$IFDEF OCRDEBUG}
DebugToBmp(shadowsbmp,4,h);
{$ENDIF}
{ create a bitmap with only the chars on it }
charsbmp := bmp.copy;
FilterCharsBitmap(charsbmp);
{$IFDEF OCRDEBUG}
DebugToBmp(charsbmp,5,h);
{$ENDIF}
{ Now actually extract the characters }
// TODO XXX FIXME:
// We should make a different TPA
// for each colour, rather than put them all in one. Noise can be of a
// different colour.
SetLength(chars, charsbmp.height * charsbmp.width);
charscount := 0;
for y := 0 to charsbmp.height - 1 do
for x := 0 to charsbmp.width - 1 do
begin
{ If the colour > 0, it is a valid point of a character }
if charsbmp.fastgetpixel(x,y) > 0 then
begin
{ Add the point }
chars[charscount] := point(x,y);
inc(charscount);
end;
end;
setlength(chars,charscount);
{ Limit to charpoint amount of points }
{ Now that the points of all the characters are in array ``chars'', split }
chars_2d := SplitTPAEx(chars,1,charsbmp.height);
{ Each char now has its own TPA in chars_2d[n] }
{ TODO: This only sorts the points in every TPA }
SortATPAFrom(chars_2d, point(0,0));
{$IFDEF OCRDEBUG}
for x := 0 to high(chars_2d) do
begin
pc := random(clWhite);
for y := 0 to high(chars_2d[x]) do
charsbmp.FastSetPixel(chars_2d[x][y].x, chars_2d[x][y].y, pc);
end;
DebugToBmp(charsbmp,6,h);
{$ENDIF}
for y := 0 to high(chars_2d) do
begin
{ bb is the bounding box of one character }
bb:=gettpabounds(chars_2d[y]);
{ Character is way too large }
if (bb.x2 - bb.x1 > 10) or (length(chars_2d[y]) > 70) then
begin // more than one char
{$IFDEF OCRDEBUG}
if length(chars_2d[y]) > 70 then
mDebugLn('more than one char at y: ' + inttostr(y));
if (bb.x2 - bb.x1 > 10) then
mDebugLn('too wide at y: ' + inttostr(y));
{$ENDIF}
{ TODO: Remove all the stuff below? }
{ Store the shadow of each char in helpershadow }
helpershadow:=getshadows(shadowsbmp,chars_2d[y]);
{ Split helpershadow as well }
chars_2d_b := splittpaex(helpershadow,2,shadowsbmp.height);
//writeln('chars_2d_b length: ' + inttostr(length(chars_2d_b)));
{ Draw on shadowsbmp }
shadowsbmp.DrawATPA(chars_2d_b);
for x := 0 to high(chars_2d_b) do
begin
setlength(shadows,length(shadows)+1);
shadows[high(shadows)] := ConvTPAArr(chars_2d_b[x]);
end;
end else if length(chars_2d[y]) < 70 then
begin
{ Character fits, get the shadow and store it in shadows }
setlength(shadows,length(shadows)+1);
shadows[high(shadows)] := getshadows(shadowsbmp, chars_2d[y]);
end;
end;
{
Put the characters back in proper order,
SplitTPA messes with the order of them.
Perform some more length checks as well. (TODO: WHY?)
Store the resulting tpa in ``finalchars''.
}
SortATPAFromFirstPoint(chars_2d, point(0,0));
for y := 0 to high(chars_2d) do
begin
if length(chars_2d[y]) > 70 then
continue;
setlength(finalchars,length(finalchars)+1);
finalchars[high(finalchars)] := chars_2d[y];
end;
{ Sort the shadows as well because splitTPA messes with the order }
SortATPAFromFirstPoint(shadows, point(0,0));
for x := 0 to high(shadows) do
begin
pc:=0;
{ Draw some stuff? TODO WHY }
pc := random(clWhite);
for y := 0 to high(shadows[x]) do
shadowsbmp.FastSetPixel(shadows[x][y].x, shadows[x][y].y, pc);
end;
{$IFDEF OCRDEBUG}
DebugToBmp(shadowsbmp,7,h);
{$ENDIF}
{
Return finalized characters.
They are not yet trimmed. They will be trimmed later on.
}
_chars := finalchars;
_shadows := shadows;
bmp.Free;
charsbmp.Free;
shadowsbmp.Free;
Result := true;
end;
(*
GetUpTextAtEx
~~~~~~~~~~~~~
.. code-block:: pascal
function TMOCR.GetUpTextAtEx(atX, atY: integer; shadow: boolean): string;
GetUpTextAtEx will identify each character, and also keep track of the previous
chars' final *x* bounds. If the difference between the .x2 of the previous
character and the .x1 of the current character is bigger than 5, then there
was a space between them. (Add ' ' to result)
*)
function TMOCR.GetUpTextAtEx(atX, atY: integer; shadow: boolean; fontname: string): string;
var
n:Tnormarray;
ww, hh,i,j,nl: integer;
font: TocrData;
chars, shadows, thachars: T2DPointArray;
t:Tpointarray;
b,lb:tbox;
lbset: boolean;
begin
result:='';
ww := 400;
hh := 20;
{ Return all the character points to chars & shadow }
getTextPointsIn(atX, atY, ww, hh, shadow, chars, shadows);
// Get font data for analysis.
font := FFonts.GetFont(fontname);
if shadow then
begin
font := FFonts.GetFont(fontname + '_s');
thachars := shadows;
end
else
begin
font := FFonts.GetFont(fontname);
thachars := chars;
end;
lbset:=false;
setlength(n, (font.width+1) * (font.height+1));
nl := high(n);
for j := 0 to high(thachars) do
begin
for i := 0 to nl do
n[i] := 0;
t:= thachars[j];
b:=gettpabounds(t);
if not lbset then
begin
lb:=b;
lbset:=true;
end else
begin
{ This is a space }
if b.x1 - lb.x2 > 5 then
result:=result+' ';
{ TODO: Don't we need a continue; here ? }
lb:=b;
end;
{ ``Normalize'' the Character. THIS IS IMPORTANT }
for i := 0 to high(t) do
t[i] := t[i] - point(b.x1,b.y1);
{
TODO: If the TPA is too large, we can still go beyond n's bounds.
We should check the bounds in GetTextPointsIn
}
for i := 0 to high(thachars[j]) do
begin
if (thachars[j][i].x) + ((thachars[j][i].y) * font.width) <= nl then
n[(thachars[j][i].x) + ((thachars[j][i].y) * font.width)] := 1;
end;
{ Analyze the final glyph }
result := result + GuessGlyph(n, font);
end;
end;
(*
GetUpTextAt
~~~~~~~~~~~
.. code-block:: pascal
function TMOCR.GetUpTextAt(atX, atY: integer; shadow: boolean): string;
Retreives the (special) uptext.
*)
function TMOCR.GetUpTextAt(atX, atY: integer; shadow: boolean): string;
begin
result := GetUpTextAtEx(atX, atY, shadow, 'UpChars')
end;
(*
GetTextATPA
~~~~~~~~~~~
.. code-block:: pascal
function TMOCR.GetTextATPA(const ATPA : T2DPointArray;const maxvspacing : integer; font: string): string;
Returns the text defined by the ATPA. Each TPA represents one character,
approximately.
*)
function TMOCR.GetTextATPA(const ATPA : T2DPointArray;const maxvspacing : integer; font: string): string;
var
b, lb: TBox;
i, j, w, h: Integer;
lbset: boolean;
n: TNormArray;