-
Notifications
You must be signed in to change notification settings - Fork 44
/
airscan-png.c
264 lines (218 loc) · 6.73 KB
/
airscan-png.c
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
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* PNG image decoder
*/
#include "airscan.h"
#include <png.h>
#include <setjmp.h>
#include <string.h>
/* PNG image decoder
*/
typedef struct {
image_decoder decoder; /* Base class */
png_struct *png_ptr; /* Underlying libpng decoder */
png_info *info_ptr; /* libpng info struct */
const uint8_t *image_data; /* Remaining image data */
size_t image_size; /* Remaining image data */
char error[1024]; /* Error message buffer */
png_uint_32 width, height; /* Image size in pixels */
int bit_depth; /* 1/2/4/8/16 */
int color_type; /* PNG_COLOR_TYPE_XXX */
int interlace_type; /* PNG_INTERLACE_XXX */
unsigned int num_lines; /* Num of lines left to read */
} image_decoder_png;
/* Free PNG decoder
*/
static void
image_decoder_png_free (image_decoder *decoder)
{
image_decoder_png *png = (image_decoder_png*) decoder;
image_decoder_reset(decoder);
mem_free(png);
}
/* libpng error callback
*/
static void
image_decoder_png_error_fn (png_struct *png_ptr, const char *message)
{
image_decoder_png *png = png_get_error_ptr(png_ptr);
snprintf(png->error, sizeof(png->error), "PNG: %s", message);
}
/* libpng warning callback
*/
static void
image_decoder_png_warning_fn (png_struct *png_ptr, const char *message)
{
(void) png_ptr;
(void) message;
}
/* libpng malloc callback
*/
static void*
image_decoder_png_malloc_fn (png_struct *png_ptr, size_t size)
{
(void) png_ptr;
return mem_new(char, size);
}
/* libpng free callback
*/
static void
image_decoder_png_free_fn (png_struct *png_ptr, void *p)
{
(void) png_ptr;
mem_free(p);
}
/* libpng read callback
*/
static void
image_decoder_png_read_fn (png_struct *png_ptr, png_bytep data, size_t size)
{
image_decoder_png *png = png_get_io_ptr(png_ptr);
if (size > png->image_size) {
png_error(png_ptr, "unexpected EOF");
}
memcpy(data, png->image_data, size);
png->image_data += size;
png->image_size -= size;
}
/* Begin PNG decoding
*/
static error
image_decoder_png_begin (image_decoder *decoder, const void *data,
size_t size)
{
image_decoder_png *png = (image_decoder_png*) decoder;
/* Create libpng structures */
png->png_ptr = png_create_read_struct_2(PNG_LIBPNG_VER_STRING,
png, image_decoder_png_error_fn, image_decoder_png_warning_fn,
png, image_decoder_png_malloc_fn, image_decoder_png_free_fn);
if (png->png_ptr == NULL) {
return ERROR("PNG: png_create_read_struct_2() failed");
}
png->info_ptr = png_create_info_struct(png->png_ptr);
if (png->info_ptr == NULL) {
image_decoder_reset(decoder);
return ERROR("PNG: png_create_info_struct() failed");
}
/* Setup read function */
png_set_read_fn(png->png_ptr, png, image_decoder_png_read_fn);
png->image_data = data;
png->image_size = size;
/* Read image info */
if (setjmp(png_jmpbuf(png->png_ptr))) {
image_decoder_reset(decoder);
return ERROR(png->error);
}
png_read_info(png->png_ptr, png->info_ptr);
png_get_IHDR(png->png_ptr, png->info_ptr, &png->width, &png->height,
&png->bit_depth, &png->color_type, &png->interlace_type, NULL, NULL);
png->num_lines = png->height;
/* Reject interlaced images */
if (png->interlace_type != PNG_INTERLACE_NONE) {
image_decoder_reset(decoder);
return ERROR("PNG: interlaced images not supported");
}
/* Setup input transformations */
if (png->color_type == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png->png_ptr);
}
if (png->color_type == PNG_COLOR_TYPE_GRAY && png->bit_depth < 8) {
png_set_expand_gray_1_2_4_to_8(png->png_ptr);
png->bit_depth = 8;
}
if ((png->color_type & PNG_COLOR_MASK_ALPHA) != 0) {
png_set_strip_alpha(png->png_ptr);
}
return NULL;
}
/* Reset PNG decoder
*/
static void
image_decoder_png_reset (image_decoder *decoder)
{
image_decoder_png *png = (image_decoder_png*) decoder;
if (png->png_ptr != NULL) {
png_destroy_read_struct(&png->png_ptr, &png->info_ptr, NULL);
png->png_ptr = NULL;
png->info_ptr = NULL;
}
}
/* Get bytes count per pixel
*/
static int
image_decoder_png_get_bytes_per_pixel (image_decoder *decoder)
{
image_decoder_png *png = (image_decoder_png*) decoder;
int bit_depth = png->bit_depth;
if ((png->color_type & PNG_COLOR_MASK_COLOR) != 0) {
bit_depth *= 3;
}
return bit_depth / 3;
}
/* Get image parameters
*/
static void
image_decoder_png_get_params (image_decoder *decoder, SANE_Parameters *params)
{
image_decoder_png *png = (image_decoder_png*) decoder;
params->last_frame = SANE_TRUE;
params->pixels_per_line = png->width;
params->lines = png->height;
params->depth = png->bit_depth;
if ((png->color_type & PNG_COLOR_MASK_COLOR) != 0) {
params->format = SANE_FRAME_RGB;
params->bytes_per_line = params->pixels_per_line * 3;
} else {
params->format = SANE_FRAME_GRAY;
params->bytes_per_line = params->pixels_per_line;
}
}
/* Set clipping window
*/
static error
image_decoder_png_set_window (image_decoder *decoder, image_window *win)
{
image_decoder_png *png = (image_decoder_png*) decoder;
win->x_off = win->y_off = 0;
win->wid = png->width;
win->hei = png->height;
return NULL;
}
/* Read next line of image
*/
static error
image_decoder_png_read_line (image_decoder *decoder, void *buffer)
{
image_decoder_png *png = (image_decoder_png*) decoder;
if (!png->num_lines) {
return ERROR("PNG: end of file");
}
if (setjmp(png_jmpbuf(png->png_ptr))) {
image_decoder_reset(decoder);
return ERROR(png->error);
}
png_read_row(png->png_ptr, buffer, NULL);
png->num_lines --;
return NULL;
}
/* Create PNG image decoder
*/
image_decoder*
image_decoder_png_new (void)
{
image_decoder_png *png = mem_new(image_decoder_png, 1);
png->decoder.content_type = "image/png";
png->decoder.free = image_decoder_png_free;
png->decoder.begin = image_decoder_png_begin;
png->decoder.reset = image_decoder_png_reset;
png->decoder.get_bytes_per_pixel = image_decoder_png_get_bytes_per_pixel;
png->decoder.get_params = image_decoder_png_get_params;
png->decoder.set_window = image_decoder_png_set_window;
png->decoder.read_line = image_decoder_png_read_line;
return &png->decoder;
}
/* vim:ts=8:sw=4:et
*/