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
|
/* quirc -- QR-code recognition library
* Copyright (C) 2010-2012 Daniel Beer <dlbeer@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jpeglib.h>
#include <png.h>
#include <quirc.h>
#include "dbgutil.h"
static const char *data_type_str(int dt)
{
switch (dt) {
case QUIRC_DATA_TYPE_NUMERIC: return "NUMERIC";
case QUIRC_DATA_TYPE_ALPHA: return "ALPHA";
case QUIRC_DATA_TYPE_BYTE: return "BYTE";
case QUIRC_DATA_TYPE_KANJI: return "KANJI";
}
return "unknown";
}
void dump_data(const struct quirc_data *data)
{
printf(" Version: %d\n", data->version);
printf(" ECC level: %c\n", "MLHQ"[data->ecc_level]);
printf(" Mask: %d\n", data->mask);
printf(" Data type: %d (%s)\n",
data->data_type, data_type_str(data->data_type));
printf(" Length: %d\n", data->payload_len);
printf(" Payload: %s\n", data->payload);
if (data->eci)
printf(" ECI: %d\n", data->eci);
}
void dump_cells(const struct quirc_code *code)
{
int u, v;
printf(" %d cells, corners:", code->size);
for (u = 0; u < 4; u++)
printf(" (%d,%d)", code->corners[u].x,
code->corners[u].y);
printf("\n");
for (v = 0; v < code->size; v++) {
printf(" ");
for (u = 0; u < code->size; u++) {
int p = v * code->size + u;
if (code->cell_bitmap[p >> 3] & (1 << (p & 7)))
printf("[]");
else
printf(" ");
}
printf("\n");
}
}
struct my_jpeg_error {
struct jpeg_error_mgr base;
jmp_buf env;
};
static void my_output_message(struct jpeg_common_struct *com)
{
struct my_jpeg_error *err = (struct my_jpeg_error *)com->err;
char buf[JMSG_LENGTH_MAX];
err->base.format_message(com, buf);
fprintf(stderr, "JPEG error: %s\n", buf);
}
static void my_error_exit(struct jpeg_common_struct *com)
{
struct my_jpeg_error *err = (struct my_jpeg_error *)com->err;
my_output_message(com);
longjmp(err->env, 0);
}
static struct jpeg_error_mgr *my_error_mgr(struct my_jpeg_error *err)
{
jpeg_std_error(&err->base);
err->base.error_exit = my_error_exit;
err->base.output_message = my_output_message;
return &err->base;
}
int load_jpeg(struct quirc *q, const char *filename)
{
FILE *infile = fopen(filename, "rb");
struct jpeg_decompress_struct dinfo;
struct my_jpeg_error err;
uint8_t *image;
int y;
if (!infile) {
perror("can't open input file");
return -1;
}
memset(&dinfo, 0, sizeof(dinfo));
dinfo.err = my_error_mgr(&err);
if (setjmp(err.env))
goto fail;
jpeg_create_decompress(&dinfo);
jpeg_stdio_src(&dinfo, infile);
jpeg_read_header(&dinfo, TRUE);
dinfo.output_components = 1;
dinfo.out_color_space = JCS_GRAYSCALE;
jpeg_start_decompress(&dinfo);
if (dinfo.output_components != 1) {
fprintf(stderr, "Unexpected number of output components: %d",
dinfo.output_components);
goto fail;
}
if (quirc_resize(q, dinfo.output_width, dinfo.output_height) < 0)
goto fail;
image = quirc_begin(q, NULL, NULL);
for (y = 0; y < dinfo.output_height; y++) {
JSAMPROW row_pointer = image + y * dinfo.output_width;
jpeg_read_scanlines(&dinfo, &row_pointer, 1);
}
jpeg_finish_decompress(&dinfo);
fclose(infile);
jpeg_destroy_decompress(&dinfo);
return 0;
fail:
fclose(infile);
jpeg_destroy_decompress(&dinfo);
return -1;
}
/* hacked from https://dev.w3.org/Amaya/libpng/example.c
*
* Check if a file is a PNG image using png_sig_cmp(). Returns 1 if the given
* file is a PNG and 0 otherwise.
*/
#define PNG_BYTES_TO_CHECK 4
int check_if_png(const char *filename)
{
int ret = 0;
FILE *infile = NULL;
unsigned char buf[PNG_BYTES_TO_CHECK];
/* Open the prospective PNG file. */
if ((infile = fopen(filename, "rb")) == NULL)
goto out;
/* Read in some of the signature bytes */
if (fread(buf, 1, PNG_BYTES_TO_CHECK, infile) != PNG_BYTES_TO_CHECK)
goto out;
/*
* Compare the first PNG_BYTES_TO_CHECK bytes of the signature.
* png_sig_cmp() returns zero if the image is a PNG and nonzero if it
* isn't a PNG.
*/
if (png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK) == 0)
ret = 1;
/* FALLTHROUGH */
out:
if (infile)
fclose(infile);
return (ret);
}
int load_png(struct quirc *q, const char *filename)
{
int width, height, rowbytes, interlace_type, number_passes = 1;
png_uint_32 trns;
png_byte color_type, bit_depth;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
FILE *infile = NULL;
uint8_t *image;
int ret = -1;
int pass;
if ((infile = fopen(filename, "rb")) == NULL)
goto out;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
goto out;
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
goto out;
if (setjmp(png_jmpbuf(png_ptr)))
goto out;
png_init_io(png_ptr, infile);
png_read_info(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
interlace_type = png_get_interlace_type(png_ptr, info_ptr);
// Read any color_type into 8bit depth, Grayscale format.
// See http://www.libpng.org/pub/png/libpng-manual.txt
// PNG_COLOR_TYPE_GRAY_ALPHA is always 8 or 16bit depth.
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
if ((trns = png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
png_set_tRNS_to_alpha(png_ptr);
if (bit_depth == 16)
#if PNG_LIBPNG_VER >= 10504
png_set_scale_16(png_ptr);
#else
png_set_strip_16(png_ptr);
#endif
if ((trns) || color_type & PNG_COLOR_MASK_ALPHA)
png_set_strip_alpha(png_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE ||
color_type == PNG_COLOR_TYPE_RGB ||
color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
png_set_rgb_to_gray_fixed(png_ptr, 1, -1, -1);
}
if (interlace_type != PNG_INTERLACE_NONE)
number_passes = png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
if (rowbytes != width) {
fprintf(stderr,
"load_png: expected rowbytes to be %u but got %u\n",
width, rowbytes);
goto out;
}
if (quirc_resize(q, width, height) < 0)
goto out;
image = quirc_begin(q, NULL, NULL);
for (pass = 0; pass < number_passes; pass++) {
int y;
for (y = 0; y < height; y++) {
png_bytep row_pointer = image + y * width;
png_read_rows(png_ptr, &row_pointer, NULL, 1);
}
}
png_read_end(png_ptr, info_ptr);
ret = 0;
/* FALLTHROUGH */
out:
/* cleanup */
if (png_ptr) {
if (info_ptr)
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
else
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
}
if (infile)
fclose(infile);
return (ret);
}
|