decoder.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include <zbar_config.h>
  26. //#include <stdlib.h> /* realloc */
  27. #include <zbar.h>
  28. #define NUM_CFGS (ZBAR_CFG_MAX_LEN - ZBAR_CFG_MIN_LEN + 1)
  29. #ifdef ENABLE_EAN
  30. # include "ean.h"
  31. #endif
  32. #ifdef ENABLE_I25
  33. # include "i25.h"
  34. #endif
  35. #ifdef ENABLE_CODE39
  36. # include "code39.h"
  37. #endif
  38. #ifdef ENABLE_CODE128
  39. # include "code128.h"
  40. #endif
  41. #ifdef ENABLE_PDF417
  42. # include "pdf417.h"
  43. #endif
  44. #ifdef ENABLE_QRCODE
  45. # include "qr_finder.h"
  46. #endif
  47. /* size of bar width history (implementation assumes power of two) */
  48. #ifndef DECODE_WINDOW
  49. # define DECODE_WINDOW 16
  50. #endif
  51. /* initial data buffer allocation */
  52. #ifndef BUFFER_MIN
  53. # define BUFFER_MIN 0x20
  54. #endif
  55. /* maximum data buffer allocation
  56. * (longer symbols are rejected)
  57. */
  58. #ifndef BUFFER_MAX
  59. # define BUFFER_MAX 0x100
  60. #endif
  61. /* buffer allocation increment */
  62. #ifndef BUFFER_INCR
  63. # define BUFFER_INCR 0x10
  64. #endif
  65. #define CFG(dcode, cfg) ((dcode).configs[(cfg) - ZBAR_CFG_MIN_LEN])
  66. #define TEST_CFG(config, cfg) (((config) >> (cfg)) & 1)
  67. /* symbology independent decoder state */
  68. struct zbar_decoder_s {
  69. unsigned char idx; /* current width index */
  70. unsigned w[DECODE_WINDOW]; /* window of last N bar widths */
  71. zbar_symbol_type_t type; /* type of last decoded data */
  72. zbar_symbol_type_t lock; /* buffer lock */
  73. /* everything above here is automatically reset */
  74. unsigned buf_alloc; /* dynamic buffer allocation */
  75. unsigned buflen; /* binary data length */
  76. unsigned char *buf; /* decoded characters */
  77. void *userdata; /* application data */
  78. zbar_decoder_handler_t *handler; /* application callback */
  79. /* symbology specific state */
  80. #ifdef ENABLE_EAN
  81. ean_decoder_t ean; /* EAN/UPC parallel decode attempts */
  82. #endif
  83. #ifdef ENABLE_I25
  84. i25_decoder_t i25; /* Interleaved 2 of 5 decode state */
  85. #endif
  86. #ifdef ENABLE_CODE39
  87. code39_decoder_t code39; /* Code 39 decode state */
  88. #endif
  89. #ifdef ENABLE_CODE128
  90. code128_decoder_t code128; /* Code 128 decode state */
  91. #endif
  92. #ifdef ENABLE_PDF417
  93. pdf417_decoder_t pdf417; /* PDF417 decode state */
  94. #endif
  95. #ifdef ENABLE_QRCODE
  96. qr_finder_t qrf; /* QR Code finder state */
  97. #endif
  98. };
  99. /* return current element color */
  100. static inline char get_color (const zbar_decoder_t *dcode)
  101. {
  102. return(dcode->idx & 1);
  103. }
  104. /* retrieve i-th previous element width */
  105. static inline unsigned get_width (const zbar_decoder_t *dcode,
  106. unsigned char offset)
  107. {
  108. return(dcode->w[(dcode->idx - offset) & (DECODE_WINDOW - 1)]);
  109. }
  110. /* retrieve bar+space pair width starting at offset i */
  111. static inline unsigned pair_width (const zbar_decoder_t *dcode,
  112. unsigned char offset)
  113. {
  114. return(get_width(dcode, offset) + get_width(dcode, offset + 1));
  115. }
  116. /* calculate total character width "s"
  117. * - start of character identified by context sensitive offset
  118. * (<= DECODE_WINDOW - n)
  119. * - size of character is n elements
  120. */
  121. static inline unsigned calc_s (const zbar_decoder_t *dcode,
  122. unsigned char offset,
  123. unsigned char n)
  124. {
  125. /* FIXME check that this gets unrolled for constant n */
  126. unsigned s = 0;
  127. while(n--)
  128. s += get_width(dcode, offset++);
  129. return(s);
  130. }
  131. /* fixed character width decode assist
  132. * bar+space width are compared as a fraction of the reference dimension "x"
  133. * - +/- 1/2 x tolerance
  134. * - measured total character width (s) compared to symbology baseline (n)
  135. * (n = 7 for EAN/UPC, 11 for Code 128)
  136. * - bar+space *pair width* "e" is used to factor out bad "exposures"
  137. * ("blooming" or "swelling" of dark or light areas)
  138. * => using like-edge measurements avoids these issues
  139. * - n should be > 3
  140. */
  141. static inline int decode_e (unsigned e,
  142. unsigned s,
  143. unsigned n)
  144. {
  145. /* result is encoded number of units - 2
  146. * (for use as zero based index)
  147. * or -1 if invalid
  148. */
  149. unsigned char E = ((e * n * 2 + 1) / s - 3) / 2;
  150. return((E >= n - 3) ? -1 : E);
  151. }
  152. /* acquire shared state lock */
  153. static inline char get_lock (zbar_decoder_t *dcode,
  154. zbar_symbol_type_t req)
  155. {
  156. if(dcode->lock)
  157. return(1);
  158. dcode->lock = req;
  159. return(0);
  160. }
  161. /* ensure output buffer has sufficient allocation for request */
  162. static inline char size_buf (zbar_decoder_t *dcode,
  163. unsigned len)
  164. {
  165. unsigned char *buf;
  166. if(len < dcode->buf_alloc)
  167. /* FIXME size reduction heuristic? */
  168. return(0);
  169. if(len > BUFFER_MAX)
  170. return(1);
  171. if(len < dcode->buf_alloc + BUFFER_INCR) {
  172. len = dcode->buf_alloc + BUFFER_INCR;
  173. if(len > BUFFER_MAX)
  174. len = BUFFER_MAX;
  175. }
  176. buf = realloc(dcode->buf, len);
  177. if(!buf)
  178. return(1);
  179. dcode->buf = buf;
  180. dcode->buf_alloc = len;
  181. return(0);
  182. }
  183. extern const char *_zbar_decoder_buf_dump (unsigned char *buf,
  184. unsigned int buflen);
  185. #endif