This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocr.c
310 lines (275 loc) · 11.1 KB
/
ocr.c
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
//
// flare16x core
// Developed 2019 by Benedikt Muessig <[email protected]>
// Licensed under GPLv3
//
// ocr.c: Optical character recognition functions for the OSD text
//
#include <stdint.h>
#include <string.h>
#include "error.h"
#include "canvas.h"
#include "ocr.h"
// Attempts to detect a single large font character
flare16x_error flare16x_ocr_large_char(uint16_t offset_x, uint16_t offset_y, flare16x_canvas* canvas,
char* result_char)
{
// Verify that the canvas and output char are not null pointers
if (canvas == NULL || result_char == NULL || canvas->pixels == NULL)
return flare16x_error_make(FLARE16X_ERROR_NULL, FLARE16X_ERROR_SOURCE_OCR);
// Check, that the width and height are valid
if (canvas->width == 0 || canvas->height == 0)
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_OCR);
// Also check that the offsets, width and height are correct
if (offset_x + FLARE16X_OCR_LARGE_WIDTH > canvas->width || offset_y + FLARE16X_OCR_LARGE_HEIGHT > canvas->height)
return flare16x_error_make(FLARE16X_ERROR_IMAGE, FLARE16X_ERROR_SOURCE_OCR);
// Next, calculate the signature
uint8_t signature = 0;
uint16_t search_color = FLARE16X_OCR_LARGE_COLOR;
if (flare16x_canvas_raw(offset_x + 10, offset_y + 1, canvas) == search_color)
signature |= 1 << 0;
if (flare16x_canvas_raw(offset_x + 16, offset_y + 1, canvas) == search_color)
signature |= 1 << 1;
if (flare16x_canvas_raw(offset_x + 3, offset_y + 4, canvas) == search_color)
signature |= 1 << 2;
if (flare16x_canvas_raw(offset_x + 15, offset_y + 4, canvas) == search_color)
signature |= 1 << 3;
if (flare16x_canvas_raw(offset_x + 12, offset_y + 7, canvas) == search_color)
signature |= 1 << 4;
if (flare16x_canvas_raw(offset_x + 8, offset_y + 11, canvas) == search_color)
signature |= 1 << 5;
if (flare16x_canvas_raw(offset_x + 16, offset_y + 14, canvas) == search_color)
signature |= 1 << 6;
if (flare16x_canvas_raw(offset_x + 8, offset_y + 18, canvas) == search_color)
signature |= 1 << 7;
// Finally, match the signature against known values
switch (signature)
{
case 0x41:
*result_char = '0';
break;
case 0x11:
*result_char = '1';
break;
case 0x8d:
*result_char = '2';
break;
case 0x35:
*result_char = '3';
break;
case 0x51:
*result_char = '4';
break;
case 0x01:
*result_char = '5';
break;
case 0x69:
*result_char = '6';
break;
case 0xbb:
*result_char = '7';
break;
case 0x7d:
*result_char = '8';
break;
case 0x25:
*result_char = '9';
break;
case 0x00:
*result_char = ' ';
break;
case 0x28:
*result_char = 'C';
break;
case 0x30:
*result_char = 'F';
break;
case 0x80:
*result_char = '.';
break;
case 0x84:
*result_char = 'L';
break;
case 0x20:
*result_char = '-';
break;
case 0xcc:
*result_char = 'O';
break;
default:
*result_char = 0;
return flare16x_error_make(FLARE16X_ERROR_UNKNOWN, FLARE16X_ERROR_SOURCE_OCR);
}
return flare16x_error_make(FLARE16X_ERROR_NONE, FLARE16X_ERROR_SOURCE_OCR);
}
// Attempts to detect a large string (with capacity length + 1) of length characters
flare16x_error flare16x_ocr_large_string(uint16_t offset_x, uint16_t offset_y, uint16_t pitch, uint16_t length,
uint16_t max_unknown, flare16x_canvas* canvas, char* result_string)
{
// Verify that the canvas and output char are not null pointers
if (canvas == NULL || result_string == NULL || canvas->pixels == NULL)
return flare16x_error_make(FLARE16X_ERROR_NULL, FLARE16X_ERROR_SOURCE_OCR);
// Check, that the width and height are valid
if (canvas->width == 0 || canvas->height == 0)
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_OCR);
// Also check that the offsets, width and height are correct
if (length == 0 || (FLARE16X_OCR_LARGE_WIDTH + pitch) * length + offset_x > canvas->width + pitch ||
offset_y + FLARE16X_OCR_LARGE_HEIGHT > canvas->height)
return flare16x_error_make(FLARE16X_ERROR_RANGE, FLARE16X_ERROR_SOURCE_OCR);
// Now, clear the output string
memset(result_string, 0, length + 1);
// Next, loop through the characters and end on the first error
int src_digit, dst_digit;
for (src_digit = 0, dst_digit = 0; src_digit < length; src_digit++)
{
flare16x_error error;
error = flare16x_ocr_large_char((FLARE16X_OCR_LARGE_WIDTH + pitch) * src_digit + offset_x, offset_y,
canvas, result_string + dst_digit);
switch (flare16x_error_reason(error))
{
case FLARE16X_ERROR_NONE:
// No error, move on to the next digit
dst_digit++;
break;
case FLARE16X_ERROR_UNKNOWN:
// Unknown digit, don't output it
// If the maximum number of unknown digits is reached, throw an error
if (max_unknown == 0)
return error;
// Otherwise, decrement the remaining counter of unknown values and continue
max_unknown--;
continue;
default:
return error;
}
}
// Fall through to success
return flare16x_error_make(FLARE16X_ERROR_NONE, FLARE16X_ERROR_SOURCE_OCR);
}
// Attempts to detect a single small font character
flare16x_error flare16x_ocr_small_char(uint16_t offset_x, uint16_t offset_y, flare16x_canvas* canvas,
char* result_char)
{
// Verify that the canvas and output char are not null pointers
if (canvas == NULL || result_char == NULL || canvas->pixels == NULL)
return flare16x_error_make(FLARE16X_ERROR_NULL, FLARE16X_ERROR_SOURCE_OCR);
// Check, that the width and height are valid
if (canvas->width == 0 || canvas->height == 0)
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_OCR);
// Also check that the offsets, width and height are correct
if (offset_x + FLARE16X_OCR_SMALL_WIDTH > canvas->width || offset_y + FLARE16X_OCR_SMALL_HEIGHT > canvas->height)
return flare16x_error_make(FLARE16X_ERROR_IMAGE, FLARE16X_ERROR_SOURCE_OCR);
// Next, calculate the signature
uint8_t signature = 0;
uint16_t search_color = FLARE16X_OCR_SMALL_COLOR;
if (flare16x_canvas_raw(offset_x + 3, offset_y + 1, canvas) == search_color)
signature |= 1 << 0;
if (flare16x_canvas_raw(offset_x + 5, offset_y + 2, canvas) == search_color)
signature |= 1 << 1;
if (flare16x_canvas_raw(offset_x + 1, offset_y + 4, canvas) == search_color)
signature |= 1 << 2;
if (flare16x_canvas_raw(offset_x + 6, offset_y + 5, canvas) == search_color)
signature |= 1 << 3;
if (flare16x_canvas_raw(offset_x + 4, offset_y + 8, canvas) == search_color)
signature |= 1 << 4;
if (flare16x_canvas_raw(offset_x + 7, offset_y + 8, canvas) == search_color)
signature |= 1 << 5;
if (flare16x_canvas_raw(offset_x + 5, offset_y + 10, canvas) == search_color)
signature |= 1 << 6;
if (flare16x_canvas_raw(offset_x + 7, offset_y + 10, canvas) == search_color)
signature |= 1 << 7;
// Finally, match the signature against known values
switch (signature)
{
case 0x25:
*result_char = '0';
break;
case 0x52:
*result_char = '1';
break;
case 0xd0:
*result_char = '2';
break;
case 0x89:
*result_char = '3';
break;
case 0xb2:
*result_char = '4';
break;
case 0x29:
*result_char = '5';
break;
case 0x6d:
*result_char = '6';
break;
case 0x19:
*result_char = '7';
break;
case 0x21:
*result_char = '8';
break;
case 0xc0:
*result_char = '9';
break;
case 0x00:
*result_char = ' ';
break;
case 0x40:
*result_char = '.';
break;
case 0x12:
*result_char = ':';
break;
case 0xc9:
*result_char = 'E';
break;
default:
*result_char = 0;
return flare16x_error_make(FLARE16X_ERROR_UNKNOWN, FLARE16X_ERROR_SOURCE_OCR);
}
return flare16x_error_make(FLARE16X_ERROR_NONE, FLARE16X_ERROR_SOURCE_OCR);
}
// Attempts to detect a small string (with capacity length + 1) of length characters
flare16x_error flare16x_ocr_small_string(uint16_t offset_x, uint16_t offset_y, uint16_t pitch, uint16_t length,
uint16_t max_unknown, flare16x_canvas* canvas, char* result_string)
{
// Verify that the canvas and output char are not null pointers
if (canvas == NULL || result_string == NULL || canvas->pixels == NULL)
return flare16x_error_make(FLARE16X_ERROR_NULL, FLARE16X_ERROR_SOURCE_OCR);
// Check, that the width and height are valid
if (canvas->width == 0 || canvas->height == 0)
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_OCR);
// Also check that the offsets, width and height are correct
if (length == 0 || (FLARE16X_OCR_SMALL_WIDTH + pitch) * length + offset_x > canvas->width + pitch ||
offset_y + FLARE16X_OCR_SMALL_HEIGHT > canvas->height)
return flare16x_error_make(FLARE16X_ERROR_RANGE, FLARE16X_ERROR_SOURCE_OCR);
// Now, clear the output string
memset(result_string, 0, length + 1);
// Next, loop through the characters and end on the first error
int src_digit, dst_digit;
for (src_digit = 0, dst_digit = 0; src_digit < length; src_digit++)
{
flare16x_error error;
error = flare16x_ocr_small_char((FLARE16X_OCR_SMALL_WIDTH + pitch) * src_digit + offset_x, offset_y,
canvas, result_string + dst_digit);
switch (flare16x_error_reason(error))
{
case FLARE16X_ERROR_NONE:
// No error, move on to the next digit
dst_digit++;
break;
case FLARE16X_ERROR_UNKNOWN:
// Unknown digit, don't output it
// If the maximum number of unknown digits is reached, throw an error
if (max_unknown == 0)
return error;
// Otherwise, decrement the remaining counter of unknown values and continue
max_unknown--;
continue;
default:
return error;
}
}
// Fall through to success
return flare16x_error_make(FLARE16X_ERROR_NONE, FLARE16X_ERROR_SOURCE_OCR);
}