-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimpleocr.types.pas
268 lines (231 loc) · 5.58 KB
/
simpleocr.types.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
unit simpleocr.types;
{==============================================================================]
Copyright (c) 2021, Jarl `slacky` Holta
Project: SimpleOCR
Project URL: https://github.com/slackydev/SimpleOCR
License: GNU Lesser GPL (http://www.gnu.org/licenses/lgpl.html)
[==============================================================================}
{$i simpleocr.inc}
interface
type
PPoint = ^TPoint;
TPoint = packed record
X, Y: Integer;
end;
PBox = ^TBox;
TBox = packed record
X1, Y1, X2, Y2: Integer;
end;
PRGB32 = ^TRGB32;
TRGB32 = packed record
B, G, R, A: UInt8;
end;
PPointArray = ^TPointArray;
TPointArray = array of TPoint;
PBoxArray = ^TBoxArray;
TBoxArray = array of TBox;
PIntegerArray = ^TIntegerArray;
TIntegerArray = array of Integer;
PIntegerMatrix = ^TIntegerMatrix;
TIntegerMatrix = array of TIntegerArray;
PStringArray = ^TStringArray;
TStringArray = array of String;
procedure Swap(var A, B: TPoint); inline;
procedure Swap(var A, B: Integer); inline;
function Point(const X, Y: Integer): TPoint; inline;
function Box(const X1, Y1, X2, Y2: Integer): TBox; inline;
function MatrixDimensions(const Matrix: TIntegerMatrix; out Width, Height: Integer): Boolean;
function TPABounds(const TPA: TPointArray): TBox;
function InvertTPA(const TPA: TPointArray): TPointArray;
procedure OffsetTPA(var TPA: TPointArray; SX, SY: Integer);
procedure InsSortTPA(var Arr :TPointArray; Weight: TIntegerArray; Left, Right: Integer);
procedure SortTPAbyColumn(var Arr: TPointArray);
function Mode(Self: TIntegerArray; Hi: Integer): Integer;
implementation
procedure Swap(var A, B: TPoint);
var
C: TPoint;
begin
C := A;
A := B;
B := C;
end;
procedure Swap(var A, B: Integer);
var
C: Integer;
begin
C := A;
A := B;
B := C;
end;
function Point(const X, Y: Integer): TPoint;
begin
Result.X := X;
Result.Y := Y;
end;
function Box(const X1, Y1, X2, Y2: Integer): TBox;
begin
Result.X1 := X1;
Result.Y1 := Y1;
Result.X2 := X2;
Result.Y2 := Y2;
end;
function MatrixDimensions(const Matrix: TIntegerMatrix; out Width, Height: Integer): Boolean;
begin
Result := True;
Height := Length(Matrix);
if (Height = 0) then
Exit(False);
Width := Length(Matrix[0]);
if (Width = 0) then
Exit(False);
end;
//Return the largest and the smallest numbers for x, and y-axis in TPA.
function TPABounds(const TPA: TPointArray): TBox;
var
I,L : Integer;
begin
Result := Default(TBox);
L := High(TPA);
if (l < 0) then Exit;
Result.x1 := TPA[0].x;
Result.y1 := TPA[0].y;
Result.x2 := TPA[0].x;
Result.y2 := TPA[0].y;
for I:= 1 to L do
begin
if TPA[i].x > Result.x2 then
Result.x2 := TPA[i].x
else if TPA[i].x < Result.x1 then
Result.x1 := TPA[i].x;
if TPA[i].y > Result.y2 then
Result.y2 := TPA[i].y
else if TPA[i].y < Result.y1 then
Result.y1 := TPA[i].y;
end;
end;
{*
Returns the points not in the TPA within the area the TPA covers.
*}
function InvertTPA(const TPA: TPointArray): TPointArray;
var
Matrix: TIntegerMatrix;
i,h,x,y: Integer;
Area: TBox;
begin
Area := TPABounds(TPA);
Area.X2 := (Area.X2-Area.X1);
Area.Y2 := (Area.Y2-Area.Y1);
SetLength(Matrix, Area.Y2+1, Area.X2+1);
H := High(TPA);
for i:=0 to H do
Matrix[TPA[i].y-Area.y1][TPA[i].x-Area.x1] := 1;
SetLength(Result, (Area.X2+1)*(Area.Y2+1) - H);
i := 0;
for y:=0 to Area.Y2 do
for x:=0 to Area.X2 do
if Matrix[y][x] <> 1 then
begin
Result[i].X := x+Area.x1;
Result[i].Y := y+Area.y1;
Inc(i);
end;
SetLength(Result, i);
SetLength(Matrix, 0);
end;
{*
Moves the TPA by SX, and SY points.
*}
procedure OffsetTPA(var TPA: TPointArray; SX, SY: Integer);
var
I: Integer;
begin
for I := 0 to High(TPA) do
begin
TPA[I].X := TPA[I].X + SX;
TPA[I].Y := TPA[I].Y + SY;
end;
end;
//Fast TPointArray sorting for small arrays.
procedure InsSortTPA(var Arr: TPointArray; Weight: TIntegerArray; Left, Right: Integer);
var
i, j: Integer;
begin
for i := Left to Right do
for j := i downto Left + 1 do
begin
if not (Weight[j] < Weight[j - 1]) then
Break;
Swap(Arr[j-1], Arr[j]);
Swap(Weight[j-1], Weight[j]);
end;
end;
//Sort small TPA by Column.
procedure SortTPAbyColumn(var Arr: TPointArray);
var
i,Hi: Integer;
Weight: TIntegerArray;
Area : TBox;
begin
Hi := High(Arr);
if Hi < 0 then Exit;
Area := TPABounds(Arr);
SetLength(Weight, Hi+1);
for i := 0 to Hi do
Weight[i] := (Arr[i].x * (Area.Y2-Area.Y1) + Arr[i].y);
InsSortTPA(Arr, Weight, 0, Hi);
SetLength(Weight, 0);
end;
procedure QuickSort(var A: TIntegerArray; iLo, iHi: Integer);
var
Lo, Hi, Pivot, T: Integer;
begin
Lo := iLo;
Hi := iHi;
Pivot := A[(Lo + Hi) div 2];
repeat
while A[Lo] < Pivot do Inc(Lo);
while A[Hi] > Pivot do Dec(Hi);
if Lo <= Hi then
begin
T := A[Lo];
A[Lo] := A[Hi];
A[Hi] := T;
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then QuickSort(A, iLo, Hi);
if Lo < iHi then QuickSort(A, Lo, iHi);
end;
function Mode(Self: TIntegerArray; Hi: Integer): Integer;
var
I, Hits, Best: Integer;
Cur: Integer;
begin
Result := 0;
if (Length(Self) > 0) then
begin
QuickSort(Self, 0, Hi);
Cur := Self[0];
Hits := 1;
Best := 0;
for I := 1 to Hi do
begin
if (Self[I] <> Cur) then
begin
if (Hits > Best) then
begin
Best := Hits;
Result := Cur;
end;
Hits := 0;
Cur := Self[I];
end;
Inc(Hits);
end;
if (Hits > Best) then
Result := Cur;
end;
end;
end.