summaryrefslogtreecommitdiffstats
path: root/zbar/image.c
blob: 82aeeb8f400481396adecc2420f0432bb5bb2987 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*------------------------------------------------------------------------
 *  Copyright 2007-2010 (c) Jeff Brown <spadix@users.sourceforge.net>
 *
 *  This file is part of the ZBar Bar Code Reader.
 *
 *  The ZBar Bar Code Reader is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Lesser Public License as
 *  published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *
 *  The ZBar Bar Code Reader 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 Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser Public License
 *  along with the ZBar Bar Code Reader; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *  Boston, MA  02110-1301  USA
 *
 *  http://sourceforge.net/projects/zbar
 *------------------------------------------------------------------------*/

#include "config.h"
#include <stdio.h>
#include <stdlib.h>

#include "error.h"
#include "image.h"
#include "refcnt.h"

zbar_image_t *zbar_image_create()
{
    zbar_image_t *img = calloc(1, sizeof(zbar_image_t));
    _zbar_refcnt_init();
    _zbar_image_refcnt(img, 1);
    img->srcidx = -1;
    return (img);
}

void _zbar_image_free(zbar_image_t *img)
{
    if (img->syms) {
	zbar_symbol_set_ref(img->syms, -1);
	img->syms = NULL;
    }
    free(img);
}

void zbar_image_destroy(zbar_image_t *img)
{
    _zbar_image_refcnt(img, -1);
}

void zbar_image_ref(zbar_image_t *img, int refs)
{
    _zbar_image_refcnt(img, refs);
}

unsigned long zbar_image_get_format(const zbar_image_t *img)
{
    return (img->format);
}

unsigned zbar_image_get_sequence(const zbar_image_t *img)
{
    return (img->seq);
}

unsigned zbar_image_get_width(const zbar_image_t *img)
{
    return (img->width);
}

unsigned zbar_image_get_height(const zbar_image_t *img)
{
    return (img->height);
}

void zbar_image_get_size(const zbar_image_t *img, unsigned *w, unsigned *h)
{
    if (w)
	*w = img->width;
    if (h)
	*h = img->height;
}

void zbar_image_get_crop(const zbar_image_t *img, unsigned *x, unsigned *y,
			 unsigned *w, unsigned *h)
{
    if (x)
	*x = img->crop_x;
    if (y)
	*y = img->crop_y;
    if (w)
	*w = img->crop_w;
    if (h)
	*h = img->crop_h;
}

const void *zbar_image_get_data(const zbar_image_t *img)
{
    return (img->data);
}

unsigned long zbar_image_get_data_length(const zbar_image_t *img)
{
    return (img->datalen);
}

void zbar_image_set_format(zbar_image_t *img, unsigned long fmt)
{
    img->format = fmt;
}

void zbar_image_set_sequence(zbar_image_t *img, unsigned seq)
{
    img->seq = seq;
}

void zbar_image_set_size(zbar_image_t *img, unsigned w, unsigned h)
{
    img->crop_x = img->crop_y = 0;
    img->width = img->crop_w = w;
    img->height = img->crop_h = h;
}

void zbar_image_set_crop(zbar_image_t *img, unsigned x, unsigned y, unsigned w,
			 unsigned h)
{
    unsigned img_h;
    unsigned img_w = img->width;
    if (x > img_w)
	x = img_w;
    if (x + w > img_w)
	w = img_w - x;
    img->crop_x = x;
    img->crop_w = w;

    img_h = img->height;
    if (y > img_h)
	y = img_h;
    if (y + h > img_h)
	h = img_h - y;
    img->crop_y = y;
    img->crop_h = h;
}

inline void zbar_image_free_data(zbar_image_t *img)
{
    if (!img)
	return;
    if (img->src) {
	zbar_image_t *newimg;
	/* replace video image w/new copy */
	assert(img->refcnt); /* FIXME needs lock */
	newimg = zbar_image_create();
	memcpy(newimg, img, sizeof(zbar_image_t));
	/* recycle video image */
	newimg->cleanup(newimg);
	/* detach old image from src */
	img->cleanup = NULL;
	img->src     = NULL;
	img->srcidx  = -1;
    } else if (img->cleanup && img->data) {
	if (img->cleanup != zbar_image_free_data) {
	    /* using function address to detect this case is a bad idea;
             * windows link libraries add an extra layer of indirection...
             * this works around that problem (bug #2796277)
             */
	    zbar_image_cleanup_handler_t *cleanup = img->cleanup;
	    img->cleanup			  = zbar_image_free_data;
	    cleanup(img);
	} else
	    free((void *)img->data);
    }
    img->data = NULL;
}

void zbar_image_set_data(zbar_image_t *img, const void *data, unsigned long len,
			 zbar_image_cleanup_handler_t *cleanup)
{
    zbar_image_free_data(img);
    img->data	 = data;
    img->datalen = len;
    img->cleanup = cleanup;
}

void zbar_image_set_userdata(zbar_image_t *img, void *userdata)
{
    img->userdata = userdata;
}

void *zbar_image_get_userdata(const zbar_image_t *img)
{
    return (img->userdata);
}

zbar_image_t *zbar_image_copy(const zbar_image_t *src)
{
    return _zbar_image_copy(src, 0);
}

const zbar_symbol_set_t *zbar_image_get_symbols(const zbar_image_t *img)
{
    return (img->syms);
}

void zbar_image_set_symbols(zbar_image_t *img, const zbar_symbol_set_t *syms)
{
    if (syms)
	zbar_symbol_set_ref(syms, 1);
    if (img->syms)
	zbar_symbol_set_ref(img->syms, -1);
    img->syms = (zbar_symbol_set_t *)syms;
}

const zbar_symbol_t *zbar_image_first_symbol(const zbar_image_t *img)
{
    return ((img->syms) ? img->syms->head : NULL);
}

typedef struct zimg_hdr_s {
    uint32_t magic, format;
    uint16_t width, height;
    uint32_t size;
} zimg_hdr_t;

int zbar_image_write(const zbar_image_t *img, const char *filebase)
{
    int len	   = strlen(filebase) + 16;
    char *filename = malloc(len);
    int n = 0, rc = 0;
    FILE *f;
    zimg_hdr_t hdr;
    strcpy(filename, filebase);
    if ((img->format & 0xff) >= ' ')
	n = snprintf(filename, len, "%s.%.4s.zimg", filebase,
		     (char *)&img->format);
    else
	n = snprintf(filename, len, "%s.%08" PRIx32 ".zimg", filebase,
		     img->format);
    assert(n < len - 1);
    filename[len - 1] = '\0';

    zprintf(1, "dumping %.4s(%08" PRIx32 ") image to %s\n",
	    (char *)&img->format, img->format, filename);

    f = fopen(filename, "w");
    if (!f) {
#ifdef HAVE_ERRNO_H
	rc = errno;
	zprintf(1, "ERROR opening %s: %s\n", filename, strerror(rc));
#else
	rc = 1;
#endif
	goto error;
    }

    hdr.magic  = 0x676d697a;
    hdr.format = img->format;
    hdr.width  = img->width;
    hdr.height = img->height;
    hdr.size   = img->datalen;

    if (fwrite(&hdr, sizeof(hdr), 1, f) != 1 ||
	fwrite(img->data, 1, img->datalen, f) != img->datalen) {
#ifdef HAVE_ERRNO_H
	rc = errno;
	zprintf(1, "ERROR writing %s: %s\n", filename, strerror(rc));
#else
	rc = 1;
#endif
	fclose(f);
	goto error;
    }

    rc = fclose(f);

error:
    free(filename);
    return (rc);
}

#ifdef DEBUG_SVG
#include <png.h>

int zbar_image_write_png(const zbar_image_t *img, const char *filename)
{
    int rc		 = -1;
    FILE *file		 = NULL;
    png_struct *png	 = NULL;
    png_info *info	 = NULL;
    const uint8_t **rows = NULL;

    rows = malloc(img->height * sizeof(*rows));
    if (!rows)
	goto done;

    rows[0] = img->data;
    int y;
    for (y = 1; y < img->height; y++)
	rows[y] = rows[y - 1] + img->width;

    file = fopen(filename, "wb");
    if (!file)
	goto done;

    png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png)
	goto done;

    info = png_create_info_struct(png);
    if (!info)
	goto done;

    if (setjmp(png_jmpbuf(png)))
	goto done;

    png_init_io(png, file);
    png_set_compression_level(png, 9);
    png_set_IHDR(png, info, img->width, img->height, 8, PNG_COLOR_TYPE_GRAY,
		 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
		 PNG_FILTER_TYPE_DEFAULT);

    png_set_rows(png, info, (void *)rows);
    png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);

    png_write_end(png, info);
    rc = 0;

done:
    if (png)
	png_destroy_write_struct(&png, &info);
    if (rows)
	free(rows);
    if (file)
	fclose(file);
    return (rc);
}

#endif