-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner.c
266 lines (236 loc) · 8.79 KB
/
scanner.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
/*------------------------------------------------------------------------
* Copyright 2007-2008 (c) Jeff Brown <spadix@users.sourceforge.net>
*
* This file is part of the Zebra Barcode Library.
*
* The Zebra Barcode Library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* The Zebra Barcode Library 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 Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with the Zebra Barcode Library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* http://sourceforge.net/projects/zebra
*------------------------------------------------------------------------*/
#include "config.h"
#include <stdlib.h> /* malloc, free, abs */
#include <string.h> /* memset */
#include "zebra.h"
#ifdef DEBUG_SCANNER
# define DEBUG_LEVEL (DEBUG_SCANNER)
#endif
#include "debug.h"
#ifndef ZEBRA_FIXED
# define ZEBRA_FIXED 5
#endif
#define ROUND (1 << (ZEBRA_FIXED - 1))
/* FIXME add runtime config API for these */
#ifndef ZEBRA_SCANNER_THRESH_MIN
# define ZEBRA_SCANNER_THRESH_MIN 4
#endif
#ifndef ZEBRA_SCANNER_THRESH_INIT_WEIGHT
# define ZEBRA_SCANNER_THRESH_INIT_WEIGHT .44
#endif
#define THRESH_INIT ((unsigned)((ZEBRA_SCANNER_THRESH_INIT_WEIGHT \
* (1 << (ZEBRA_FIXED + 1)) + 1) / 2))
#ifndef ZEBRA_SCANNER_THRESH_FADE
# define ZEBRA_SCANNER_THRESH_FADE 8
#endif
#ifndef ZEBRA_SCANNER_EWMA_WEIGHT
# define ZEBRA_SCANNER_EWMA_WEIGHT .78
#endif
#define EWMA_WEIGHT ((unsigned)((ZEBRA_SCANNER_EWMA_WEIGHT \
* (1 << (ZEBRA_FIXED + 1)) + 1) / 2))
/* scanner state */
struct zebra_scanner_s {
zebra_decoder_t *decoder; /* associated bar width decoder */
unsigned y1_min_thresh; /* minimum threshold */
unsigned x; /* relative scan position of next sample */
int y0[4]; /* short circular buffer of average intensities */
int y1_sign; /* slope at last crossing */
unsigned y1_thresh; /* current slope threshold */
unsigned cur_edge; /* interpolated position of tracking edge */
unsigned last_edge; /* interpolated position of last located edge */
unsigned width; /* last element width */
};
zebra_scanner_t *zebra_scanner_create (zebra_decoder_t *dcode)
{
zebra_scanner_t *scn = malloc(sizeof(zebra_scanner_t));
scn->decoder = dcode;
scn->y1_min_thresh = ZEBRA_SCANNER_THRESH_MIN;
zebra_scanner_reset(scn);
return(scn);
}
void zebra_scanner_destroy (zebra_scanner_t *scn)
{
free(scn);
}
zebra_symbol_type_t zebra_scanner_reset (zebra_scanner_t *scn)
{
memset(&scn->x, 0, sizeof(zebra_scanner_t) + (void*)scn - (void*)&scn->x);
scn->y1_thresh = scn->y1_min_thresh;
if(scn->decoder)
zebra_decoder_reset(scn->decoder);
return(ZEBRA_NONE);
}
unsigned zebra_scanner_get_width (const zebra_scanner_t *scn)
{
return(scn->width);
}
zebra_color_t zebra_scanner_get_color (const zebra_scanner_t *scn)
{
return((scn->y1_sign <= 0) ? ZEBRA_SPACE : ZEBRA_BAR);
}
static inline unsigned calc_thresh (zebra_scanner_t *scn)
{
/* threshold 1st to improve noise rejection */
unsigned thresh = scn->y1_thresh;
if((thresh <= scn->y1_min_thresh) || !scn->width) {
dprintf(1, " tmin=%d", scn->y1_min_thresh);
return(scn->y1_min_thresh);
}
/* slowly return threshold to min */
unsigned dx = (scn->x << ZEBRA_FIXED) - scn->last_edge;
unsigned long t = thresh * dx;
t /= scn->width;
t /= ZEBRA_SCANNER_THRESH_FADE;
dprintf(1, " thr=%d t=%ld x=%d last=%d.%d (%d)",
thresh, t, scn->x, scn->last_edge >> ZEBRA_FIXED,
scn->last_edge & ((1 << ZEBRA_FIXED) - 1), dx);
if(thresh > t) {
thresh -= t;
if(thresh > scn->y1_min_thresh)
return(thresh);
}
scn->y1_thresh = scn->y1_min_thresh;
return(scn->y1_min_thresh);
}
static inline zebra_symbol_type_t process_edge (zebra_scanner_t *scn,
int y1)
{
scn->width = scn->cur_edge - scn->last_edge;
dprintf(1, " sgn=%d cur=%d.%d w=%d (%s)\n",
scn->y1_sign, scn->cur_edge >> ZEBRA_FIXED,
scn->cur_edge & ((1 << ZEBRA_FIXED) - 1), scn->width,
((y1 > 0) ? "SPACE" : "BAR"));
scn->last_edge = scn->cur_edge;
/* pass to decoder */
if(scn->width || (y1 > 0)) {
if(scn->decoder)
return(zebra_decode_width(scn->decoder, scn->width));
return(ZEBRA_PARTIAL);
}
/* skip initial transition */
return(ZEBRA_NONE);
}
zebra_symbol_type_t zebra_scanner_new_scan (zebra_scanner_t *scn)
{
/* finalize outstanding edge */
zebra_symbol_type_t edge = process_edge(scn, 0);
/* reset color to SPACE
* (actually just resets everything)
*/
memset(&scn->x, 0, sizeof(zebra_scanner_t) + (void*)scn - (void*)&scn->x);
scn->y1_thresh = scn->y1_min_thresh;
if(scn->decoder)
zebra_decoder_new_scan(scn->decoder);
return(edge);
}
zebra_symbol_type_t zebra_scan_y (zebra_scanner_t *scn,
int y)
{
/* FIXME calc and clip to max y range... */
/* retrieve short value history */
register int y0_1 = scn->y0[(scn->x - 1) & 3];
register int y0_0 = y0_1;
if(scn->x) {
/* update weighted moving average */
y0_0 += ((int)((y - y0_1) * EWMA_WEIGHT)) >> ZEBRA_FIXED;
scn->y0[scn->x & 3] = y0_0;
}
else
y0_0 = y0_1 = scn->y0[0] = scn->y0[1] = scn->y0[2] = scn->y0[3] = y;
register int y0_2 = scn->y0[(scn->x - 2) & 3];
register int y0_3 = scn->y0[(scn->x - 3) & 3];
/* 1st differential @ x-1 */
register int y1_1 = y0_1 - y0_2;
{
register int y1_2 = y0_2 - y0_3;
if((abs(y1_1) < abs(y1_2)) &&
((y1_1 >= 0) == (y1_2 >= 0)))
y1_1 = y1_2;
}
/* 2nd differentials @ x-1 & x-2 */
register int y2_1 = y0_0 - (y0_1 * 2) + y0_2;
register int y2_2 = y0_1 - (y0_2 * 2) + y0_3;
dprintf(1, "scan: y=%d y0=%d y1=%d y2=%d", y, y0_1, y1_1, y2_1);
zebra_symbol_type_t edge = ZEBRA_NONE;
/* 2nd zero-crossing is 1st local min/max - could be edge */
if((!y2_1 ||
((y2_1 > 0) ? y2_2 < 0 : y2_2 > 0)) &&
(calc_thresh(scn) <= abs(y1_1)))
{
/* check for 1st sign change */
char y1_rev = (scn->y1_sign > 0) ? y1_1 < 0 : y1_1 > 0;
if(y1_rev || !scn->y1_sign)
/* intensity change reversal - finalize previous edge */
edge = process_edge(scn, y1_1);
else
dprintf(1, "\n");
if(y1_rev || (abs(scn->y1_sign) < abs(y1_1))) {
scn->y1_sign = y1_1;
/* adaptive thresholding */
/* start at multiple of new min/max */
scn->y1_thresh = (abs(y1_1) * THRESH_INIT + ROUND) >> ZEBRA_FIXED;
dprintf(1, " thr=%d", scn->y1_thresh);
if(scn->y1_thresh < scn->y1_min_thresh)
scn->y1_thresh = scn->y1_min_thresh;
/* update current edge */
int d = y2_1 - y2_2;
scn->cur_edge = 1 << ZEBRA_FIXED;
if(!d)
scn->cur_edge >>= 1;
else if(y2_1)
/* interpolate zero crossing */
scn->cur_edge -= ((y2_1 << ZEBRA_FIXED) + 1) / d;
scn->cur_edge += scn->x << ZEBRA_FIXED;
}
}
else
dprintf(1, "\n");
/* FIXME add fall-thru pass to decoder after heuristic "idle" period
(eg, 6-8 * last width) */
scn->x++;
return(edge);
}
/* undocumented API for drawing cutesy debug graphics */
void zebra_scanner_get_state (const zebra_scanner_t *scn,
unsigned *x,
unsigned *cur_edge,
unsigned *last_edge,
int *y0,
int *y1,
int *y2,
int *y1_thresh)
{
register int y0_0 = scn->y0[(scn->x - 1) & 3];
register int y0_1 = scn->y0[(scn->x - 2) & 3];
register int y0_2 = scn->y0[(scn->x - 3) & 3];
if(x) *x = scn->x - 1;
if(cur_edge) *cur_edge = scn->cur_edge;
if(last_edge) *last_edge = scn->last_edge;
if(y0) *y0 = y0_1;
if(y1) *y1 = y0_1 - y0_2;
if(y2) *y2 = y0_0 - (y0_1 * 2) + y0_2;
/* NB not quite accurate (uses updated x) */
zebra_scanner_t *mut_scn = (zebra_scanner_t*)scn;
if(y1_thresh) *y1_thresh = calc_thresh(mut_scn);
}