1 /*------------------------------------------------------------------------
2  *  Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
3  *
4  *  This file is part of the ZBar Bar Code Reader.
5  *
6  *  The ZBar Bar Code Reader is free software; you can redistribute it
7  *  and/or modify it under the terms of the GNU Lesser Public License as
8  *  published by the Free Software Foundation; either version 2.1 of
9  *  the License, or (at your option) any later version.
10  *
11  *  The ZBar Bar Code Reader is distributed in the hope that it will be
12  *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Lesser Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser Public License
17  *  along with the ZBar Bar Code Reader; if not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  *  Boston, MA  02110-1301  USA
20  *
21  *  http://sourceforge.net/projects/zbar
22  *------------------------------------------------------------------------*/
23 #ifndef _DECODER_H_
24 #define _DECODER_H_
25 
26 #include <stdlib.h>     /* realloc */
27 
28 #include <zbar.h>
29 
30 #define NUM_CFGS (ZBAR_CFG_MAX_LEN - ZBAR_CFG_MIN_LEN + 1)
31 
32 #ifdef ENABLE_EAN
33 # include "decoder/ean.h"
34 #endif
35 #ifdef ENABLE_I25
36 # include "decoder/i25.h"
37 #endif
38 #ifdef ENABLE_CODE39
39 # include "decoder/code39.h"
40 #endif
41 #ifdef ENABLE_CODE128
42 # include "decoder/code128.h"
43 #endif
44 #ifdef ENABLE_PDF417
45 # include "decoder/pdf417.h"
46 #endif
47 #ifdef ENABLE_QRCODE
48 # include "decoder/qr_finder.h"
49 #endif
50 
51 /* size of bar width history (implementation assumes power of two) */
52 #ifndef DECODE_WINDOW
53 # define DECODE_WINDOW  16
54 #endif
55 
56 /* initial data buffer allocation */
57 #ifndef BUFFER_MIN
58 # define BUFFER_MIN   0x20
59 #endif
60 
61 /* maximum data buffer allocation
62  * (longer symbols are rejected)
63  */
64 #ifndef BUFFER_MAX
65 # define BUFFER_MAX  0x100
66 #endif
67 
68 /* buffer allocation increment */
69 #ifndef BUFFER_INCR
70 # define BUFFER_INCR  0x10
71 #endif
72 
73 #define CFG(dcode, cfg) ((dcode).configs[(cfg) - ZBAR_CFG_MIN_LEN])
74 #define TEST_CFG(config, cfg) (((config) >> (cfg)) & 1)
75 
76 /* symbology independent decoder state */
77 struct zbar_decoder_s {
78     unsigned char idx;                  /* current width index */
79     unsigned w[DECODE_WINDOW];          /* window of last N bar widths */
80     zbar_symbol_type_t type;            /* type of last decoded data */
81     zbar_symbol_type_t lock;            /* buffer lock */
82 
83     /* everything above here is automatically reset */
84     unsigned buf_alloc;                 /* dynamic buffer allocation */
85     unsigned buflen;                    /* binary data length */
86     unsigned char *buf;                 /* decoded characters */
87     void *userdata;                     /* application data */
88     zbar_decoder_handler_t *handler;    /* application callback */
89 
90     /* symbology specific state */
91 #ifdef ENABLE_EAN
92     ean_decoder_t ean;                  /* EAN/UPC parallel decode attempts */
93 #endif
94 #ifdef ENABLE_I25
95     i25_decoder_t i25;                  /* Interleaved 2 of 5 decode state */
96 #endif
97 #ifdef ENABLE_CODE39
98     code39_decoder_t code39;            /* Code 39 decode state */
99 #endif
100 #ifdef ENABLE_CODE128
101     code128_decoder_t code128;          /* Code 128 decode state */
102 #endif
103 #ifdef ENABLE_PDF417
104     pdf417_decoder_t pdf417;            /* PDF417 decode state */
105 #endif
106 #ifdef ENABLE_QRCODE
107     qr_finder_t qrf;                    /* QR Code finder state */
108 #endif
109 };
110 
111 /* return current element color */
get_color(const zbar_decoder_t * dcode)112 static inline char get_color (const zbar_decoder_t *dcode)
113 {
114     return(dcode->idx & 1);
115 }
116 
117 /* retrieve i-th previous element width */
get_width(const zbar_decoder_t * dcode,unsigned char offset)118 static inline unsigned get_width (const zbar_decoder_t *dcode,
119                                   unsigned char offset)
120 {
121     return(dcode->w[(dcode->idx - offset) & (DECODE_WINDOW - 1)]);
122 }
123 
124 /* retrieve bar+space pair width starting at offset i */
pair_width(const zbar_decoder_t * dcode,unsigned char offset)125 static inline unsigned pair_width (const zbar_decoder_t *dcode,
126                                    unsigned char offset)
127 {
128     return(get_width(dcode, offset) + get_width(dcode, offset + 1));
129 }
130 
131 /* calculate total character width "s"
132  *   - start of character identified by context sensitive offset
133  *     (<= DECODE_WINDOW - n)
134  *   - size of character is n elements
135  */
calc_s(const zbar_decoder_t * dcode,unsigned char offset,unsigned char n)136 static inline unsigned calc_s (const zbar_decoder_t *dcode,
137                                unsigned char offset,
138                                unsigned char n)
139 {
140     /* FIXME check that this gets unrolled for constant n */
141     unsigned s = 0;
142     while(n--)
143         s += get_width(dcode, offset++);
144     return(s);
145 }
146 
147 /* fixed character width decode assist
148  * bar+space width are compared as a fraction of the reference dimension "x"
149  *   - +/- 1/2 x tolerance
150  *   - measured total character width (s) compared to symbology baseline (n)
151  *     (n = 7 for EAN/UPC, 11 for Code 128)
152  *   - bar+space *pair width* "e" is used to factor out bad "exposures"
153  *     ("blooming" or "swelling" of dark or light areas)
154  *     => using like-edge measurements avoids these issues
155  *   - n should be > 3
156  */
decode_e(unsigned e,unsigned s,unsigned n)157 static inline int decode_e (unsigned e,
158                             unsigned s,
159                             unsigned n)
160 {
161     /* result is encoded number of units - 2
162      * (for use as zero based index)
163      * or -1 if invalid
164      */
165     unsigned char E = ((e * n * 2 + 1) / s - 3) / 2;
166     return((E >= n - 3) ? -1 : E);
167 }
168 
169 /* acquire shared state lock */
get_lock(zbar_decoder_t * dcode,zbar_symbol_type_t req)170 static inline char get_lock (zbar_decoder_t *dcode,
171                              zbar_symbol_type_t req)
172 {
173     if(dcode->lock)
174         return(1);
175     dcode->lock = req;
176     return(0);
177 }
178 
179 /* ensure output buffer has sufficient allocation for request */
size_buf(zbar_decoder_t * dcode,unsigned len)180 static inline char size_buf (zbar_decoder_t *dcode,
181                              unsigned len)
182 {
183     if(len < dcode->buf_alloc)
184         /* FIXME size reduction heuristic? */
185         return(0);
186     if(len > BUFFER_MAX)
187         return(1);
188     if(len < dcode->buf_alloc + BUFFER_INCR) {
189         len = dcode->buf_alloc + BUFFER_INCR;
190         if(len > BUFFER_MAX)
191             len = BUFFER_MAX;
192     }
193     unsigned char *buf = realloc(dcode->buf, len);
194     if(!buf)
195         return(1);
196     dcode->buf = buf;
197     dcode->buf_alloc = len;
198     return(0);
199 }
200 
201 extern const char *_zbar_decoder_buf_dump (unsigned char *buf,
202                                             unsigned int buflen);
203 
204 #endif
205