image.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 _IMAGE_H_
  24. #define _IMAGE_H_
  25. #include <config.h>
  26. #ifdef HAVE_INTTYPES_H
  27. # include <inttypes.h>
  28. #endif
  29. //#include <stdlib.h>
  30. //#include <assert.h>
  31. #include "error.h"
  32. #include "symbol.h"
  33. #include "refcnt.h"
  34. #include <zbar.h>
  35. #define PRIx32 "lx"
  36. /* adapted from v4l2 spec */
  37. #define fourcc(a, b, c, d) \
  38. ((uint32_t)(a) | ((uint32_t)(b) << 8) | \
  39. ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24))
  40. /* unpack size/location of component */
  41. #define RGB_SIZE(c) ((c) >> 5)
  42. #define RGB_OFFSET(c) ((c) & 0x1f)
  43. /* coarse image format categorization.
  44. * to limit conversion variations
  45. */
  46. typedef enum zbar_format_group_e {
  47. ZBAR_FMT_GRAY,
  48. ZBAR_FMT_YUV_PLANAR,
  49. ZBAR_FMT_YUV_PACKED,
  50. ZBAR_FMT_RGB_PACKED,
  51. ZBAR_FMT_YUV_NV,
  52. ZBAR_FMT_JPEG,
  53. /* enum size */
  54. ZBAR_FMT_NUM
  55. } zbar_format_group_t;
  56. struct zbar_image_s {
  57. uint32_t format; /* fourcc image format code */
  58. unsigned width, height; /* image size */
  59. const void *data; /* image sample data */
  60. unsigned long datalen; /* allocated/mapped size of data */
  61. void *userdata; /* user specified data associated w/image */
  62. /* cleanup handler */
  63. zbar_image_cleanup_handler_t *cleanup;
  64. refcnt_t refcnt; /* reference count */
  65. zbar_video_t *src; /* originator */
  66. int srcidx; /* index used by originator */
  67. zbar_image_t *next; /* internal image lists */
  68. unsigned seq; /* page/frame sequence number */
  69. zbar_symbol_set_t *syms; /* decoded result set */
  70. };
  71. /* description of an image format */
  72. typedef struct zbar_format_def_s {
  73. uint32_t format; /* fourcc */
  74. zbar_format_group_t group; /* coarse categorization */
  75. union {
  76. uint8_t gen[4]; /* raw bytes */
  77. struct {
  78. uint8_t bpp; /* bits per pixel */
  79. uint8_t red, green, blue; /* size/location a la RGB_BITS() */
  80. } rgb;
  81. struct {
  82. uint8_t xsub2, ysub2; /* chroma subsampling in each axis */
  83. uint8_t packorder; /* channel ordering flags
  84. * bit0: 0=UV, 1=VU
  85. * bit1: 0=Y/chroma, 1=chroma/Y
  86. */
  87. } yuv;
  88. uint32_t cmp; /* quick compare equivalent formats */
  89. } p;
  90. } zbar_format_def_t;
  91. extern int _zbar_best_format(uint32_t, uint32_t*, const uint32_t*);
  92. extern const zbar_format_def_t *_zbar_format_lookup(uint32_t);
  93. extern void _zbar_image_free(zbar_image_t*);
  94. #ifdef DEBUG_SVG
  95. extern int zbar_image_write_png(const zbar_image_t*, const char*);
  96. #else
  97. # define zbar_image_write_png(...)
  98. #endif
  99. static inline void _zbar_image_refcnt (zbar_image_t *img,
  100. int delta)
  101. {
  102. if(!_zbar_refcnt(&img->refcnt, delta) && delta <= 0) {
  103. if(img->cleanup)
  104. img->cleanup(img);
  105. if(!img->src)
  106. _zbar_image_free(img);
  107. }
  108. }
  109. static inline void _zbar_image_swap_symbols (zbar_image_t *a,
  110. zbar_image_t *b)
  111. {
  112. zbar_symbol_set_t *tmp = a->syms;
  113. a->syms = b->syms;
  114. b->syms = tmp;
  115. }
  116. #endif