1 /*------------------------------------------------------------------------
2 * Copyright 2007-2010 (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
26 #include <config.h>
27 #ifdef HAVE_INTTYPES_H
28 # include <inttypes.h>
29 #endif
30 #include <stdlib.h>
31 #include <assert.h>
32
33 #include <zbar.h>
34 #include "error.h"
35 #include "symbol.h"
36 #include "refcnt.h"
37
38 #define fourcc zbar_fourcc
39
40 /* unpack size/location of component */
41 #define RGB_SIZE(c) ((c) >> 5)
42 #define RGB_OFFSET(c) ((c) & 0x1f)
43
44 /* coarse image format categorization.
45 * to limit conversion variations
46 */
47 typedef enum zbar_format_group_e {
48 ZBAR_FMT_GRAY,
49 ZBAR_FMT_YUV_PLANAR,
50 ZBAR_FMT_YUV_PACKED,
51 ZBAR_FMT_RGB_PACKED,
52 ZBAR_FMT_YUV_NV,
53 ZBAR_FMT_JPEG,
54
55 /* enum size */
56 ZBAR_FMT_NUM
57 } zbar_format_group_t;
58
59
60 struct zbar_image_s {
61 uint32_t format; /* fourcc image format code */
62 unsigned width, height; /* image size */
63 const void *data; /* image sample data */
64 unsigned long datalen; /* allocated/mapped size of data */
65 unsigned crop_x, crop_y; /* crop rectangle */
66 unsigned crop_w, crop_h;
67 void *userdata; /* user specified data associated w/image */
68
69 /* cleanup handler */
70 zbar_image_cleanup_handler_t *cleanup;
71 refcnt_t refcnt; /* reference count */
72 zbar_video_t *src; /* originator */
73 int srcidx; /* index used by originator */
74 zbar_image_t *next; /* internal image lists */
75
76 unsigned seq; /* page/frame sequence number */
77 zbar_symbol_set_t *syms; /* decoded result set */
78 };
79
80 /* description of an image format */
81 typedef struct zbar_format_def_s {
82 uint32_t format; /* fourcc */
83 zbar_format_group_t group; /* coarse categorization */
84 union {
85 uint8_t gen[4]; /* raw bytes */
86 struct {
87 uint8_t bpp; /* bits per pixel */
88 uint8_t red, green, blue; /* size/location a la RGB_BITS() */
89 } rgb;
90 struct {
91 uint8_t xsub2, ysub2; /* chroma subsampling in each axis */
92 uint8_t packorder; /* channel ordering flags
93 * bit0: 0=UV, 1=VU
94 * bit1: 0=Y/chroma, 1=chroma/Y
95 */
96 } yuv;
97 uint32_t cmp; /* quick compare equivalent formats */
98 } p;
99 } zbar_format_def_t;
100
101
102 extern int _zbar_best_format(uint32_t, uint32_t*, const uint32_t*);
103 extern const zbar_format_def_t *_zbar_format_lookup(uint32_t);
104 extern void _zbar_image_free(zbar_image_t*);
105
106 #ifdef DEBUG_SVG
107 extern int zbar_image_write_png(const zbar_image_t*, const char*);
108 #else
109 # define zbar_image_write_png(...)
110 #endif
111
_zbar_image_refcnt(zbar_image_t * img,int delta)112 static inline void _zbar_image_refcnt (zbar_image_t *img,
113 int delta)
114 {
115 if(!_zbar_refcnt(&img->refcnt, delta) && delta <= 0) {
116 if(img->cleanup)
117 img->cleanup(img);
118 if(!img->src)
119 _zbar_image_free(img);
120 }
121 }
122
_zbar_image_swap_symbols(zbar_image_t * a,zbar_image_t * b)123 static inline void _zbar_image_swap_symbols (zbar_image_t *a,
124 zbar_image_t *b)
125 {
126 zbar_symbol_set_t *tmp = a->syms;
127 a->syms = b->syms;
128 b->syms = tmp;
129 }
130
_zbar_image_copy_size(zbar_image_t * dst,const zbar_image_t * src)131 static inline void _zbar_image_copy_size (zbar_image_t *dst,
132 const zbar_image_t *src)
133 {
134 dst->width = src->width;
135 dst->height = src->height;
136 dst->crop_x = src->crop_x;
137 dst->crop_y = src->crop_y;
138 dst->crop_w = src->crop_w;
139 dst->crop_h = src->crop_h;
140 }
141
_zbar_image_copy(const zbar_image_t * src,int inverted)142 static inline zbar_image_t *_zbar_image_copy (const zbar_image_t *src,
143 int inverted)
144 {
145 zbar_image_t *dst;
146
147 if (inverted && (src->format != fourcc('Y','8','0','0'))
148 && (src->format != fourcc('G','R','E','Y')) )
149 return NULL;
150
151 dst = zbar_image_create();
152 dst->format = src->format;
153 _zbar_image_copy_size(dst, src);
154 dst->datalen = src->datalen;
155 dst->data = malloc(src->datalen);
156 assert(dst->data);
157
158 if (!inverted) {
159 memcpy((void*)dst->data, src->data, src->datalen);
160 } else {
161 int i, len = src->datalen;
162 long *sp = (void *)src->data, *dp = (void *)dst->data;
163 char *spc, *dpc;
164
165 /* Do it word per word, in order to speedup */
166 for (i = 0; i < len; i+= sizeof(long))
167 *dp++ = ~(*sp++);
168
169 /* Deal with non-aligned remains, if any */
170 len -= i;
171 spc = (char *)sp;
172 dpc = (char *)dp;
173 for (i = 0; i < len; i++)
174 *dpc++ = ~(*spc++);
175 }
176 dst->cleanup = zbar_image_free_data;
177 return(dst);
178 }
179
180 #endif
181