summaryrefslogtreecommitdiffstats
path: root/zbar/scanner.c
blob: 1db14f53a7359977088c2b67c883b9f1bd78e4b7 (plain)
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
/*------------------------------------------------------------------------
 *  Copyright 2007-2010 (c) Jeff Brown <spadix@users.sourceforge.net>
 *
 *  This file is part of the ZBar Bar Code Reader.
 *
 *  The ZBar Bar Code Reader 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 ZBar Bar Code Reader 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 ZBar Bar Code Reader; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *  Boston, MA  02110-1301  USA
 *
 *  http://sourceforge.net/projects/zbar
 *------------------------------------------------------------------------*/

#include "config.h"
#include <stddef.h>
#include <stdlib.h> /* malloc, free, abs */
#include <string.h> /* memset */

#include <zbar.h>
#include "svg.h"

#ifdef DEBUG_SCANNER
#define DEBUG_LEVEL (DEBUG_SCANNER)
#endif
#include "debug.h"

#ifndef ZBAR_FIXED
#define ZBAR_FIXED 5
#endif
#define ROUND (1 << (ZBAR_FIXED - 1))

/* FIXME add runtime config API for these */
#ifndef ZBAR_SCANNER_THRESH_MIN
#define ZBAR_SCANNER_THRESH_MIN 4
#endif

#ifndef ZBAR_SCANNER_THRESH_INIT_WEIGHT
#define ZBAR_SCANNER_THRESH_INIT_WEIGHT .44
#endif
#define THRESH_INIT                                                          \
    ((unsigned)((ZBAR_SCANNER_THRESH_INIT_WEIGHT * (1 << (ZBAR_FIXED + 1)) + \
		 1) /                                                        \
		2))

#ifndef ZBAR_SCANNER_THRESH_FADE
#define ZBAR_SCANNER_THRESH_FADE 8
#endif

#ifndef ZBAR_SCANNER_EWMA_WEIGHT
#define ZBAR_SCANNER_EWMA_WEIGHT .78
#endif
#define EWMA_WEIGHT \
    ((unsigned)((ZBAR_SCANNER_EWMA_WEIGHT * (1 << (ZBAR_FIXED + 1)) + 1) / 2))

/* scanner state */
struct zbar_scanner_s {
    zbar_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 */
};

zbar_scanner_t *zbar_scanner_create(zbar_decoder_t *dcode)
{
    zbar_scanner_t *scn = malloc(sizeof(zbar_scanner_t));
    scn->decoder	= dcode;
    scn->y1_min_thresh	= ZBAR_SCANNER_THRESH_MIN;
    zbar_scanner_reset(scn);
    return (scn);
}

void zbar_scanner_destroy(zbar_scanner_t *scn)
{
    free(scn);
}

zbar_symbol_type_t zbar_scanner_reset(zbar_scanner_t *scn)
{
    memset(&scn->x, 0, sizeof(zbar_scanner_t) - offsetof(zbar_scanner_t, x));
    scn->y1_thresh = scn->y1_min_thresh;
    if (scn->decoder)
	zbar_decoder_reset(scn->decoder);
    return (ZBAR_NONE);
}

unsigned zbar_scanner_get_width(const zbar_scanner_t *scn)
{
    return (scn->width);
}

unsigned zbar_scanner_get_edge(const zbar_scanner_t *scn, unsigned offset,
			       int prec)
{
    unsigned edge = scn->last_edge - offset - (1 << ZBAR_FIXED) - ROUND;
    prec	  = ZBAR_FIXED - prec;
    if (prec > 0)
	return (edge >> prec);
    else if (!prec)
	return (edge);
    else
	return (edge << -prec);
}

zbar_color_t zbar_scanner_get_color(const zbar_scanner_t *scn)
{
    return ((scn->y1_sign <= 0) ? ZBAR_SPACE : ZBAR_BAR);
}

static inline unsigned calc_thresh(zbar_scanner_t *scn)
{
    /* threshold 1st to improve noise rejection */
    unsigned dx, thresh = scn->y1_thresh;
    unsigned long t;
    if ((thresh <= scn->y1_min_thresh) || !scn->width) {
	dbprintf(1, " tmin=%d", scn->y1_min_thresh);
	return (scn->y1_min_thresh);
    }
    /* slowly return threshold to min */
    dx = (scn->x << ZBAR_FIXED) - scn->last_edge;
    t  = thresh * dx;
    t /= scn->width;
    t /= ZBAR_SCANNER_THRESH_FADE;
    dbprintf(1, " thr=%d t=%ld x=%d last=%d.%d (%d)", thresh, t, scn->x,
	     scn->last_edge >> ZBAR_FIXED,
	     scn->last_edge & ((1 << ZBAR_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 zbar_symbol_type_t process_edge(zbar_scanner_t *scn, int y1)
{
    if (!scn->y1_sign)
	scn->last_edge = scn->cur_edge = (1 << ZBAR_FIXED) + ROUND;
    else if (!scn->last_edge)
	scn->last_edge = scn->cur_edge;

    scn->width = scn->cur_edge - scn->last_edge;
    dbprintf(1, " sgn=%d cur=%d.%d w=%d (%s)\n", scn->y1_sign,
	     scn->cur_edge >> ZBAR_FIXED,
	     scn->cur_edge & ((1 << ZBAR_FIXED) - 1), scn->width,
	     ((y1 > 0) ? "SPACE" : "BAR"));
    scn->last_edge = scn->cur_edge;

#if DEBUG_SVG > 1
    svg_path_moveto(SVG_ABS, scn->last_edge - (1 << ZBAR_FIXED) - ROUND, 0);
#endif

    /* pass to decoder */
    if (scn->decoder)
	return (zbar_decode_width(scn->decoder, scn->width));
    return (ZBAR_PARTIAL);
}

inline zbar_symbol_type_t zbar_scanner_flush(zbar_scanner_t *scn)
{
    unsigned x;
    if (!scn->y1_sign)
	return (ZBAR_NONE);

    x = (scn->x << ZBAR_FIXED) + ROUND;

    if (scn->cur_edge != x || scn->y1_sign > 0) {
	zbar_symbol_type_t edge = process_edge(scn, -scn->y1_sign);
	dbprintf(1, "flush0:");
	scn->cur_edge = x;
	scn->y1_sign  = -scn->y1_sign;
	return (edge);
    }

    scn->y1_sign = scn->width = 0;
    if (scn->decoder)
	return (zbar_decode_width(scn->decoder, 0));
    return (ZBAR_PARTIAL);
}

zbar_symbol_type_t zbar_scanner_new_scan(zbar_scanner_t *scn)
{
    zbar_symbol_type_t edge = ZBAR_NONE;
    while (scn->y1_sign) {
	zbar_symbol_type_t tmp = zbar_scanner_flush(scn);
	if (tmp < 0 || tmp > edge)
	    edge = tmp;
    }

    /* reset scanner and associated decoder */
    memset(&scn->x, 0, sizeof(zbar_scanner_t) - offsetof(zbar_scanner_t, x));
    scn->y1_thresh = scn->y1_min_thresh;
    if (scn->decoder)
	zbar_decoder_new_scan(scn->decoder);
    return (edge);
}

zbar_symbol_type_t zbar_scan_y(zbar_scanner_t *scn, int y)
{
    /* FIXME calc and clip to max y range... */
    /* retrieve short value history */
    register int x    = scn->x;
    register int y0_1 = scn->y0[(x - 1) & 3];
    register int y0_0 = y0_1;
    register int y0_2, y0_3, y1_1, y2_1, y2_2;
    zbar_symbol_type_t edge;
    if (x) {
	/* update weighted moving average */
	y0_0 += ((int)((y - y0_1) * EWMA_WEIGHT)) >> ZBAR_FIXED;
	scn->y0[x & 3] = y0_0;
    } else
	y0_0 = y0_1 = scn->y0[0] = scn->y0[1] = scn->y0[2] = scn->y0[3] = y;
    y0_2 = scn->y0[(x - 2) & 3];
    y0_3 = scn->y0[(x - 3) & 3];
    /* 1st differential @ x-1 */
    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 */
    y2_1 = y0_0 - (y0_1 * 2) + y0_2;
    y2_2 = y0_1 - (y0_2 * 2) + y0_3;

    dbprintf(1, "scan: x=%d y=%d y0=%d y1=%d y2=%d", x, y, y0_1, y1_1, y2_1);

    edge = ZBAR_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)
	    /* intensity change reversal - finalize previous edge */
	    edge = process_edge(scn, y1_1);

	if (y1_rev || (abs(scn->y1_sign) < abs(y1_1))) {
	    int d;
	    scn->y1_sign = y1_1;

	    /* adaptive thresholding */
	    /* start at multiple of new min/max */
	    scn->y1_thresh = (abs(y1_1) * THRESH_INIT + ROUND) >> ZBAR_FIXED;
	    dbprintf(1, "\tthr=%d", scn->y1_thresh);
	    if (scn->y1_thresh < scn->y1_min_thresh)
		scn->y1_thresh = scn->y1_min_thresh;

	    /* update current edge */
	    d		  = y2_1 - y2_2;
	    scn->cur_edge = 1 << ZBAR_FIXED;
	    if (!d)
		scn->cur_edge >>= 1;
	    else if (y2_1)
		/* interpolate zero crossing */
		scn->cur_edge -= ((y2_1 << ZBAR_FIXED) + 1) / d;
	    scn->cur_edge += x << ZBAR_FIXED;
	    dbprintf(1, "\n");
	}
    } else
	dbprintf(1, "\n");
    /* FIXME add fall-thru pass to decoder after heuristic "idle" period
       (eg, 6-8 * last width) */
    scn->x = x + 1;
    return (edge);
}

/* undocumented API for drawing cutesy debug graphics */
void zbar_scanner_get_state(const zbar_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];
    zbar_scanner_t *mut_scn;
    if (x)
	*x = scn->x - 1;
    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) */
    mut_scn = (zbar_scanner_t *)scn;
    if (y1_thresh)
	*y1_thresh = calc_thresh(mut_scn);
    dbprintf(1, "\n");
}