ZBar bar code reader . http://zbar.sourceforge.net/ ZBar is licensed under the GNU LGPL 2.1 to enable development of both open source and commercial projects.

Dependents:   GR-PEACH_Camera_in_barcode levkov_ov7670

LICENSE

The ZBar Bar Code Reader is Copyright (C) 2007-2009 Jeff Brown <spadix@users.sourceforge.net> The QR Code reader is Copyright (C) 1999-2009 Timothy B. Terriberry <tterribe@xiph.org>

You can redistribute this library and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This 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 General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

ISAAC is based on the public domain implementation by Robert J. Jenkins Jr., and is itself public domain.

Portions of the bit stream reader are copyright (C) The Xiph.Org Foundation 1994-2008, and are licensed under a BSD-style license.

The Reed-Solomon decoder is derived from an implementation (C) 1991-1995 Henry Minsky (hqm@ua.com, hqm@ai.mit.edu), and is licensed under the LGPL with permission.

Committer:
RyoheiHagimoto
Date:
Tue Apr 19 02:00:37 2016 +0000
Revision:
0:56c5742b9e2b
First revicion

Who changed what in which revision?

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