Debian Codegraph @

src/png.c

raw symbols SCIP
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17 static void
18 image_png_read_buf(png_structp png_ptr, png_bytep data, png_size_t len)
19 {
20 image *im = (image *)png_get_io_ptr(png_ptr);
21
22 DEBUG_TRACE("PNG read_buf wants %ld bytes, %d in buffer\n", len, buffer_len(im->buf));
23
24 if (im->fh != NULL) {
25 if ( !_check_buf(im->fh, im->buf, len, MAX(len, BUFFER_SIZE)) ) {
26 goto eof;
27 }
28 }
29 else {
30 if (len > buffer_len(im->buf)) {
31 // read from SV into buffer
32 int sv_readlen = len - buffer_len(im->buf);
33
34 if (sv_readlen > sv_len(im->sv_data) - im->sv_offset)
35 goto eof;
36
37 DEBUG_TRACE(" Reading %d bytes of SV data @ %d\n", sv_readlen, im->sv_offset);
38 buffer_append(im->buf, SvPVX(im->sv_data) + im->sv_offset, sv_readlen);
39 im->sv_offset += sv_readlen;
40 }
41 }
42
43 memcpy(data, buffer_ptr(im->buf), len);
44 buffer_consume(im->buf, len);
45
46 goto ok;
47
48 eof:
49 png_error(png_ptr, "Not enough PNG data");
50
51 ok:
52 return;
53 }
54
55 static void
56 image_png_error(png_structp png_ptr, png_const_charp error_msg)
57 {
58 image *im = (image *)png_get_error_ptr(png_ptr);
59
60 warn("Image::Scale libpng error: %s (%s)\n", error_msg, SvPVX(im->path));
61
62 longjmp(png_jmpbuf(png_ptr), 1);
63 }
64
65 static void
66 image_png_warning(png_structp png_ptr, png_const_charp warning_msg)
67 {
68 image *im = (image *)png_get_error_ptr(png_ptr);
69
70 warn("Image::Scale libpng warning: %s (%s)\n", warning_msg, SvPVX(im->path));
71 }
72
73 int
74 image_png_read_header(image *im)
75 {
76 im->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)im, image_png_error, image_png_warning);
77 if ( !im->png_ptr )
78 croak("Image::Scale could not initialize libpng\n");
79
80 im->info_ptr = png_create_info_struct(im->png_ptr);
81 if ( !im->info_ptr ) {
82 png_destroy_read_struct(&im->png_ptr, (png_infopp)NULL, (png_infopp)NULL);
83 croak("Image::Scale could not initialize libpng\n");
84 }
85
86 if ( setjmp( png_jmpbuf(im->png_ptr) ) ) {
87 image_png_finish(im);
88 return 0;
89 }
90
91 png_set_read_fn(im->png_ptr, im, image_png_read_buf);
92
93 png_read_info(im->png_ptr, im->info_ptr);
94
95 im->width = png_get_image_width(im->png_ptr, im->info_ptr);
96 im->height = png_get_image_height(im->png_ptr, im->info_ptr);
97 im->channels = png_get_channels(im->png_ptr, im->info_ptr);
98 im->has_alpha = 1;
99
100 return 1;
101 }
102
103 static void
104 image_png_interlace_pass_gray(image *im, unsigned char *ptr, int start_y, int stride_y, int start_x, int stride_x)
105 {
106 int x, y;
107
108 for (y = 0; y < im->height; y++) {
109 png_read_row(im->png_ptr, ptr, NULL);
110 if (start_y == 0) {
111 start_y = stride_y;
112 for (x = start_x; x < im->width; x += stride_x) {
113 im->pixbuf[y * im->width + x] = COL_FULL(
114 ptr[x * 2], ptr[x * 2], ptr[x * 2], ptr[x * 2 + 1]
115 );
116 }
117 }
118 start_y--;
119 }
120 }
121
122 static void
123 image_png_interlace_pass(image *im, unsigned char *ptr, int start_y, int stride_y, int start_x, int stride_x)
124 {
125 int x, y;
126
127 for (y = 0; y < im->height; y++) {
128 png_read_row(im->png_ptr, ptr, NULL);
129 if (start_y == 0) {
130 start_y = stride_y;
131 for (x = start_x; x < im->width; x += stride_x) {
132 im->pixbuf[y * im->width + x] = COL_FULL(
133 ptr[x * 4], ptr[x * 4 + 1], ptr[x * 4 + 2], ptr[x * 4 + 3]
134 );
135 }
136 }
137 start_y--;
138 }
139 }
140
141 int
142 image_png_load(image *im)
143 {
144 int bit_depth, color_type, num_passes, x, y;
145 int ofs;
146 volatile unsigned char *ptr = NULL; // volatile = won't be rolled back if longjmp is called
147
148 if ( setjmp( png_jmpbuf(im->png_ptr) ) ) {
149 if (ptr != NULL)
150 Safefree(ptr);
151 image_png_finish(im);
152 return 0;
153 }
154
155 // If reusing the object a second time, we need to completely create a new png struct
156 if (im->used) {
157 DEBUG_TRACE("Recreating libpng objects\n");
158 image_png_finish(im);
159
160 if (im->fh != NULL) {
161 // reset file to begining of image
162 PerlIO_seek(im->fh, im->image_offset, SEEK_SET);
163 }
164 else {
165 // reset SV read
166 im->sv_offset = im->image_offset;
167 }
168
169 buffer_clear(im->buf);
170
171 image_png_read_header(im);
172 }
173
174 bit_depth = png_get_bit_depth(im->png_ptr, im->info_ptr);
175 color_type = png_get_color_type(im->png_ptr, im->info_ptr);
176
177 if (color_type == PNG_COLOR_TYPE_PALETTE) {
178 png_set_expand(im->png_ptr); // png_set_palette_to_rgb(im->png_ptr);
179 im->channels = 4;
180 }
181 else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
182 png_set_expand(im->png_ptr); // png_set_expand_gray_1_2_4_to_8(im->png_ptr);
183 else if (png_get_valid(im->png_ptr, im->info_ptr, PNG_INFO_tRNS))
184 png_set_expand(im->png_ptr); // png_set_tRNS_to_alpha(im->png_ptr);
185
186 if (bit_depth == 16)
187 png_set_strip_16(im->png_ptr);
188 else if (bit_depth < 8)
189 png_set_packing(im->png_ptr);
190
191 // Make non-alpha RGB/Palette 32-bit and Gray 16-bit for easier handling
192 if ( !(color_type & PNG_COLOR_MASK_ALPHA) ) {
193 png_set_add_alpha(im->png_ptr, 0xFF, PNG_FILLER_AFTER);
194 }
195
196 num_passes = png_set_interlace_handling(im->png_ptr);
197
198 DEBUG_TRACE("png bit_depth %d, color_type %d, channels %d, num_passes %d\n", bit_depth, color_type, im->channels, num_passes);
199
200 png_read_update_info(im->png_ptr, im->info_ptr);
201
202 image_alloc(im, im->width, im->height);
203
204 New(0, ptr, png_get_rowbytes(im->png_ptr, im->info_ptr), unsigned char);
205
206 ofs = 0;
207
208 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { // Grayscale (Alpha)
209 if (num_passes == 1) { // Non-interlaced
210 for (y = 0; y < im->height; y++) {
211 png_read_row(im->png_ptr, (unsigned char *)ptr, NULL);
212 for (x = 0; x < im->width; x++) {
213 im->pixbuf[ofs++] = COL_FULL(ptr[x * 2], ptr[x * 2], ptr[x * 2], ptr[x * 2 + 1]);
214 }
215 }
216 }
217 else if (num_passes == 7) { // Interlaced
218 image_png_interlace_pass_gray(im, (unsigned char *)ptr, 0, 8, 0, 8);
219 image_png_interlace_pass_gray(im, (unsigned char *)ptr, 0, 8, 4, 8);
220 image_png_interlace_pass_gray(im, (unsigned char *)ptr, 4, 8, 0, 4);
221 image_png_interlace_pass_gray(im, (unsigned char *)ptr, 0, 4, 2, 4);
222 image_png_interlace_pass_gray(im, (unsigned char *)ptr, 2, 4, 0, 2);
223 image_png_interlace_pass_gray(im, (unsigned char *)ptr, 0, 2, 1, 2);
224 image_png_interlace_pass_gray(im, (unsigned char *)ptr, 1, 2, 0, 1);
225 }
226 }
227 else { // RGB(A)
228 if (num_passes == 1) { // Non-interlaced
229 for (y = 0; y < im->height; y++) {
230 png_read_row(im->png_ptr, (unsigned char *)ptr, NULL);
231 for (x = 0; x < im->width; x++) {
232 im->pixbuf[ofs++] = COL_FULL(ptr[x * 4], ptr[x * 4 + 1], ptr[x * 4 + 2], ptr[x * 4 + 3]);
233 }
234 }
235 }
236 else if (num_passes == 7) { // Interlaced
237 // The first pass will return an image 1/8 as wide as the entire image
238 // (every 8th column starting in column 0)
239 // and 1/8 as high as the original (every 8th row starting in row 0)
240 image_png_interlace_pass(im, (unsigned char *)ptr, 0, 8, 0, 8);
241
242 // The second will be 1/8 as wide (starting in column 4)
243 // and 1/8 as high (also starting in row 0)
244 image_png_interlace_pass(im, (unsigned char *)ptr, 0, 8, 4, 8);
245
246 // The third pass will be 1/4 as wide (every 4th pixel starting in column 0)
247 // and 1/8 as high (every 8th row starting in row 4)
248 image_png_interlace_pass(im, (unsigned char *)ptr, 4, 8, 0, 4);
249
250 // The fourth pass will be 1/4 as wide and 1/4 as high
251 // (every 4th column starting in column 2, and every 4th row starting in row 0)
252 image_png_interlace_pass(im, (unsigned char *)ptr, 0, 4, 2, 4);
253
254 // The fifth pass will return an image 1/2 as wide,
255 // and 1/4 as high (starting at column 0 and row 2)
256 image_png_interlace_pass(im, (unsigned char *)ptr, 2, 4, 0, 2);
257
258 // The sixth pass will be 1/2 as wide and 1/2 as high as the original
259 // (starting in column 1 and row 0)
260 image_png_interlace_pass(im, (unsigned char *)ptr, 0, 2, 1, 2);
261
262 // The seventh pass will be as wide as the original, and 1/2 as high,
263 // containing all of the odd numbered scanlines.
264 image_png_interlace_pass(im, (unsigned char *)ptr, 1, 2, 0, 1);
265 }
266 else {
267 croak("Image::Scale unsupported PNG interlace type (%d passes)\n", num_passes);
268 }
269 }
270
271 Safefree(ptr);
272
273 // This is not required, so we can save some time by not reading post-image chunks
274 //png_read_end(im->png_ptr, im->info_ptr);
275
276 return 1;
277 }
278
279 static void
280 image_png_compress(image *im, png_structp png_ptr, png_infop info_ptr)
281 {
282 int i, x, y;
283 int color_space = PNG_COLOR_TYPE_RGB_ALPHA;
284 volatile unsigned char *ptr = NULL;
285
286 if (setjmp( png_jmpbuf(png_ptr) )) {
287 if (ptr != NULL)
288 Safefree(ptr);
289 return;
290 }
291
292 // Match output color space with input file
293 switch (im->channels) {
294 case 4:
295 case 3:
296 DEBUG_TRACE("PNG output color space set to RGBA\n");
297 color_space = PNG_COLOR_TYPE_RGB_ALPHA;
298 break;
299 case 2:
300 case 1:
301 DEBUG_TRACE("PNG output color space set to gray alpha\n");
302 color_space = PNG_COLOR_TYPE_GRAY_ALPHA;
303 break;
304 }
305
306 png_set_IHDR(png_ptr, info_ptr, im->target_width, im->target_height, 8, color_space,
307 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
308
309 png_write_info(png_ptr, info_ptr);
310
311 New(0, ptr, png_get_rowbytes(png_ptr, info_ptr), unsigned char);
312
313 i = 0;
314
315 if (color_space == PNG_COLOR_TYPE_GRAY_ALPHA) {
316 for (y = 0; y < im->target_height; y++) {
317 for (x = 0; x < im->target_width; x++) {
318 ptr[x * 2] = COL_BLUE(im->outbuf[i]);
319 ptr[x * 2 + 1] = COL_ALPHA(im->outbuf[i]);
320 i++;
321 }
322 png_write_row(png_ptr, (png_bytep)ptr);
323 }
324 }
325 else { // RGB
326 for (y = 0; y < im->target_height; y++) {
327 for (x = 0; x < im->target_width; x++) {
328 ptr[x * 4] = COL_RED(im->outbuf[i]);
329 ptr[x * 4 + 1] = COL_GREEN(im->outbuf[i]);
330 ptr[x * 4 + 2] = COL_BLUE(im->outbuf[i]);
331 ptr[x * 4 + 3] = COL_ALPHA(im->outbuf[i]);
332 i++;
333 }
334 png_write_row(png_ptr, (png_bytep)ptr);
335 }
336 }
337
338 Safefree(ptr);
339
340 png_write_end(png_ptr, info_ptr);
341 }
342
343 void
344 image_png_save(image *im, const char *path)
345 {
346 png_structp png_ptr;
347 png_infop info_ptr;
348 FILE *out;
349
350 if (im->outbuf == NULL)
351 croak("Image::Scale cannot write PNG with no output data\n");
352
353 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
354 if (!png_ptr) {
355 croak("Image::Scale could not initialize libpng\n");
356 }
357
358 info_ptr = png_create_info_struct(png_ptr);
359 if (!info_ptr) {
360 png_destroy_write_struct(&png_ptr, NULL);
361 croak("Image::Scale could not initialize libpng\n");
362 }
363
364 if ((out = fopen(path, "wb")) == NULL) {
365 png_destroy_write_struct(&png_ptr, &info_ptr);
366 croak("Image::Scale cannot open %s for writing\n", path);
367 }
368
369 png_init_io(png_ptr, out);
370
371 image_png_compress(im, png_ptr, info_ptr);
372
373 fclose(out);
374 png_destroy_write_struct(&png_ptr, &info_ptr);
375 }
376
377 static void
378 image_png_write_sv(png_structp png_ptr, png_bytep data, png_size_t len)
379 {
380 SV *sv_buf = (SV *)png_get_io_ptr(png_ptr);
381
382 // Copy buffer to SV
383 sv_catpvn(sv_buf, (char *)data, len);
384
385 DEBUG_TRACE("image_png_write_sv copied %ld bytes\n", len);
386 }
387
388 static void
389 image_png_flush_sv(png_structp png_ptr)
390 {
391 // Nothing
392 }
393
394 void
395 image_png_to_sv(image *im, SV *sv_buf)
396 {
397 png_structp png_ptr;
398 png_infop info_ptr;
399
400 if (im->outbuf == NULL)
401 croak("Image::Scale cannot write PNG with no output data\n");
402
403 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
404 if (!png_ptr) {
405 croak("Image::Scale could not initialize libpng\n");
406 }
407
408 info_ptr = png_create_info_struct(png_ptr);
409 if (!info_ptr) {
410 png_destroy_write_struct(&png_ptr, NULL);
411 croak("Image::Scale could not initialize libpng\n");
412 }
413
414 png_set_write_fn(png_ptr, sv_buf, image_png_write_sv, image_png_flush_sv);
415
416 image_png_compress(im, png_ptr, info_ptr);
417
418 png_destroy_write_struct(&png_ptr, &info_ptr);
419 }
420
421 void
422 image_png_finish(image *im)
423 {
424 if (im->png_ptr != NULL) {
425 png_destroy_read_struct(&im->png_ptr, &im->info_ptr, NULL);
426 im->png_ptr = NULL;
427 DEBUG_TRACE("libpng destroy\n");
428 }
429 }