-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOCRBitmap.pas
287 lines (250 loc) · 6.63 KB
/
OCRBitmap.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
unit OCRBitmap;
{==============================================================================]
Copyright (c) 2016, Jarl `slacky` Holta
Project: SimpleOCR
Project URL: https://github.com/WarPie/SimpleOCR
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
[==============================================================================}
{$mode objfpc}{$H+}
{$macro on}
{$inline on}
{$modeswitch advancedrecords}
interface
uses
SysUtils,
Graphics,
LCLType,
LCLIntf,
FPImage,
IntfGraphics,
graphtype,
OCRTypes;
type
TThreshMethod = (tmMean, tmMinMax);
TMiniBitmap = record
Loaded: Boolean;
Width, Height:Int32;
FData: PRGB32;
function Open(f:String): Boolean;
function FromClient(constref Client:TTarget; B:TBox): Boolean;
function FromWindow(wnd:Int32; B:TBox): Boolean;
procedure Free();
function GetPixel(x,y:Int32): TRGB32; inline;
procedure SetPixel(x,y:Int32; Color:TRGB32); inline;
property Pixel[x,y:Int32]: TRGB32 read GetPixel write SetPixel; default;
function FindColor(var TPA:TPointArray; Color:Int32): Boolean;
procedure ThresholdAdaptive(Alpha, Beta: Byte; InvertIt: Boolean; Method: TThreshMethod; C: Integer);
function ToMatrix(): TIntMatrix;
end;
function BGRToRGB(BGR: TRGB32): Int32;
function NewBitmap(): TMiniBitmap;
implementation
uses
OCRUtils{$IFDEF WINDOWS}, Windows{$ENDIF};
function NewBitmap(): TMiniBitmap;
begin
Result.FData := nil;
Result.Loaded:= False;
Result.Height:= 0;
Result.Width := 0;
end;
function BGRToRGB(BGR: TRGB32): Int32;
begin;
Result := BGR.R or BGR.g shl 8 or BGR.b shl 16;
end;
function TMiniBitmap.Open(f:String): Boolean;
var
Image : TLazIntfImage;
Desc : TRawImageDescription;
begin
Self.Free();
Result := FileExists(f);
if Result then
begin
Image := TLazIntfImage.Create(0,0);
Desc.Init_BPP32_B8G8R8_BIO_TTB(Image.Width, Image.Height);
Image.DataDescription := Desc;
Image.LoadFromFile(f);
Self.Width := Image.Width;
Self.Height := Image.Height;
Self.FData := GetMem(Self.Width * Self.Height * SizeOf(TRGB32));
Move(Image.PixelData[0], Self.FData[0], Self.Width * Self.Height * SizeOf(TRGB32));
Image.Free;
end else
WriteLn('TMiniBitmap.Open -> Unable to load file: ', f);
Loaded := Result;
end;
function TMiniBitmap.FromClient(constref Client:TTarget; B:TBox): Boolean;
var
RetData:TRetData;
y:Int32;
begin
Self.Free();
Width := B.x2-B.x1+1;
Height := B.y2-B.y1+1;
try
{note: check if dimensions are within range}
RetData := Client.ReturnData(Client.Target, B.x1, B.y1, width, height);
Self.FData := GetMem(Width*Height * SizeOf(TRGB32));
for y:=0 to height-1 do
Move(RetData.Ptr[y*RetData.rowLen], Self.FData[y*width], Width * SizeOf(TRGB32));
Loaded := True;
except on E:Exception do
begin
WriteLn('Exception ('+ E.ClassName +', E: '+ E.Message +')');
raise Exception.Create('Exception ('+ E.ClassName +', E: '+ E.Message +')');
end;
end;
Client.FreeReturnData(Client.Target);
Result := Loaded;
if not(Result) then
Self.Free();
end;
function TMiniBitmap.FromWindow(wnd:Int32; B:TBox): Boolean;
{$IFDEF WINDOWS}
var
handle: HWND;
winDC,cDC: HDC;
BMP: HBITMAP;
begin
Self.Free();
Width := B.x2-B.x1+1;
Height := B.y2-B.y1+1;
handle := HWND(wnd);
winDC := Windows.GetWindowDC(handle);
cDC := Windows.CreateCompatibleDC(winDC);
bmp := Windows.CreateCompatibleBitmap(winDC, Width,Height);
Windows.SelectObject(cDC, bmp);
Windows.BitBlt(cDC, 0,0, Width,Height, winDC, B.x1,B.y1, SRCCOPY);
// from bitmapbuffer
Self.FData := GetMem(Self.Width * Self.Height * SizeOf(TRGB32));
Result := Windows.GetBitmapBits(bmp, width*height*SizeOf(TRGB32), FData) > 0;
if not(Result) then
Self.Free();
Loaded := Result;
// free
Windows.DeleteObject(Windows.SelectObject(cDC, bmp));
Windows.DeleteDC(cDC);
Windows.ReleaseDC(handle, winDC);
end;
{$ELSE}
begin
Self.Free();
//...
end;
{$ENDIF}
procedure TMiniBitmap.Free();
begin
if (Self.FData <> nil) and Self.Loaded then
begin
FreeMem(Self.FData);
Self.FData := nil;
end;
Self.Width := 0;
Self.Height := 0;
Loaded := False;
end;
function TMiniBitmap.GetPixel(x,y:Int32): TRGB32;
begin
Result := FData[y*Width+x];
end;
procedure TMiniBitmap.SetPixel(x,y:Int32; Color:TRGB32);
begin
FData[y*Width+x] := Color;
end;
function TMiniBitmap.FindColor(var TPA:TPointArray; Color:Int32): Boolean;
var
x,y,idx,c:Int32;
Target: TRGB32;
begin
Target := RGB32(Color);
c := 0;
idx := 0;
SetLength(TPA, Width*Height);
for y:=0 to Height-1 do
for x:=0 to Width-1 do
begin
if (FData[idx].R = Target.R) and (FData[idx].G = Target.G) and (FData[idx].B = Target.B) then
begin
TPA[c].x := x;
TPA[c].y := y;
Inc(c);
end;
Inc(idx);
end;
SetLength(TPA, c);
Result := c > 0;
end;
procedure TMiniBitmap.ThresholdAdaptive(Alpha, Beta: Byte; InvertIt: Boolean; Method: TThreshMethod; C: Integer);
var
i,size: Int32;
upper: PtrUInt;
vMin,vMax,threshold: UInt8;
Counter: Int64;
Tab: Array [0..256] of UInt8;
ptr: PRGB32;
begin
if Alpha = Beta then Exit;
if InvertIt then Exch(Alpha, Beta);
size := (Self.Width * Self.Height) - 1;
upper := PtrUInt(@Self.FData[size]);
//Finding the threshold - While at it set blue-scale to the RGB mean (needed for later).
Threshold := 0;
case Method of
//Find the Arithmetic Mean / Average.
tmMean:
begin
Counter := 0;
ptr := Self.FData;
while PtrUInt(Ptr) <= upper do
begin
Ptr^.B := (Ptr^.B + Ptr^.G + Ptr^.R) div 3;
Counter += Ptr^.B;
Inc(Ptr);
end;
Threshold := (Counter div size) + C;
end;
//Middle of Min- and Max-value
tmMinMax:
begin
vMin := 255;
vMax := 0;
ptr := Self.FData;
while PtrUInt(Ptr) <= upper do
begin
ptr^.B := (ptr^.B + ptr^.G + ptr^.R) div 3;
if ptr^.B < vMin then
vMin := ptr^.B
else if ptr^.B > vMax then
vMax := ptr^.B;
Inc(ptr);
end;
Threshold := ((vMax+Int32(vMin)) shr 1) + C;
end;
end;
for i:=0 to (Threshold-1) do Tab[i] := Alpha;
for i:=Threshold to 255 do Tab[i] := Beta;
ptr := Self.FData;
while PtrUInt(Ptr) <= upper do
begin
ptr^.R := Tab[Ptr^.B];
ptr^.G := 0;
ptr^.B := 0;
ptr^.A := 0;
Inc(ptr);
end;
end;
function TMiniBitmap.ToMatrix(): TIntMatrix;
var
i,x,y: Integer;
begin
SetLength(Result, Height, Width);
i := 0;
for y:=0 to Height-1 do
for x:=0 to Width-1 do
begin
Result[y,x] := BGRToRGB(Self.FData[i]);
Inc(i);
end;
end;
end.