Debian Codegraph @

PNG/libpng/pngread.c

raw symbols SCIP
1
2 /* pngread.c - read a PNG file
3 *
4 * Last changed in libpng 1.2.11 June 7, 2006
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2006 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 *
10 * This file contains routines that an application calls directly to
11 * read a PNG file or stream.
12 */
13
14 #define PNG_INTERNAL
15 #include "png.h"
16
17 #if defined(PNG_READ_SUPPORTED)
18
19 /* Create a PNG structure for reading, and allocate any memory needed. */
20 png_structp PNGAPI
21 png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
22 png_error_ptr error_fn, png_error_ptr warn_fn)
23 {
24
25 #ifdef PNG_USER_MEM_SUPPORTED
26 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
27 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
28 }
29
30 /* Alternate create PNG structure for reading, and allocate any memory needed. */
31 png_structp PNGAPI
32 png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
33 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
34 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
35 {
36 #endif /* PNG_USER_MEM_SUPPORTED */
37
38 png_structp png_ptr;
39
40 #ifdef PNG_SETJMP_SUPPORTED
41 #ifdef USE_FAR_KEYWORD
42 jmp_buf jmpbuf;
43 #endif
44 #endif
45
46 int i;
47
48 png_debug(1, "in png_create_read_struct\n");
49 #ifdef PNG_USER_MEM_SUPPORTED
50 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
51 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
52 #else
53 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
54 #endif
55 if (png_ptr == NULL)
56 return (NULL);
57
58 #if !defined(PNG_1_0_X)
59 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
60 png_init_mmx_flags(png_ptr); /* 1.2.0 addition */
61 #endif
62 #endif /* PNG_1_0_X */
63
64 /* added at libpng-1.2.6 */
65 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
66 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
67 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
68 #endif
69
70 #ifdef PNG_SETJMP_SUPPORTED
71 #ifdef USE_FAR_KEYWORD
72 if (setjmp(jmpbuf))
73 #else
74 if (setjmp(png_ptr->jmpbuf))
75 #endif
76 {
77 png_free(png_ptr, png_ptr->zbuf);
78 png_ptr->zbuf=NULL;
79 #ifdef PNG_USER_MEM_SUPPORTED
80 png_destroy_struct_2((png_voidp)png_ptr,
81 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
82 #else
83 png_destroy_struct((png_voidp)png_ptr);
84 #endif
85 return (NULL);
86 }
87 #ifdef USE_FAR_KEYWORD
88 png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
89 #endif
90 #endif
91
92 #ifdef PNG_USER_MEM_SUPPORTED
93 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
94 #endif
95
96 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
97
98 i=0;
99 do
100 {
101 if(user_png_ver[i] != png_libpng_ver[i])
102 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
103 } while (png_libpng_ver[i++]);
104
105 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
106 {
107 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
108 * we must recompile any applications that use any older library version.
109 * For versions after libpng 1.0, we will be compatible, so we need
110 * only check the first digit.
111 */
112 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
113 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
114 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
115 {
116 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
117 char msg[80];
118 if (user_png_ver)
119 {
120 sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
121 user_png_ver);
122 png_warning(png_ptr, msg);
123 }
124 sprintf(msg, "Application is running with png.c from libpng-%.20s",
125 png_libpng_ver);
126 png_warning(png_ptr, msg);
127 #endif
128 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
129 png_ptr->flags=0;
130 #endif
131 png_error(png_ptr,
132 "Incompatible libpng version in application and library");
133 }
134 }
135
136 /* initialize zbuf - compression buffer */
137 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
138 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
139 (png_uint_32)png_ptr->zbuf_size);
140 png_ptr->zstream.zalloc = png_zalloc;
141 png_ptr->zstream.zfree = png_zfree;
142 png_ptr->zstream.opaque = (voidpf)png_ptr;
143
144 switch (inflateInit(&png_ptr->zstream))
145 {
146 case Z_OK: /* Do nothing */ break;
147 case Z_MEM_ERROR:
148 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
149 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
150 default: png_error(png_ptr, "Unknown zlib error");
151 }
152
153 png_ptr->zstream.next_out = png_ptr->zbuf;
154 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
155
156 png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
157
158 #ifdef PNG_SETJMP_SUPPORTED
159 /* Applications that neglect to set up their own setjmp() and then encounter
160 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
161 abort instead of returning. */
162 #ifdef USE_FAR_KEYWORD
163 if (setjmp(jmpbuf))
164 PNG_ABORT();
165 png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
166 #else
167 if (setjmp(png_ptr->jmpbuf))
168 PNG_ABORT();
169 #endif
170 #endif
171 return (png_ptr);
172 }
173
174 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
175 /* Initialize PNG structure for reading, and allocate any memory needed.
176 This interface is deprecated in favour of the png_create_read_struct(),
177 and it will disappear as of libpng-1.3.0. */
178 #undef png_read_init
179 void PNGAPI
180 png_read_init(png_structp png_ptr)
181 {
182 /* We only come here via pre-1.0.7-compiled applications */
183 png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
184 }
185
186 void PNGAPI
187 png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
188 png_size_t png_struct_size, png_size_t png_info_size)
189 {
190 /* We only come here via pre-1.0.12-compiled applications */
191 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
192 if(png_sizeof(png_struct) > png_struct_size ||
193 png_sizeof(png_info) > png_info_size)
194 {
195 char msg[80];
196 png_ptr->warning_fn=NULL;
197 if (user_png_ver)
198 {
199 sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
200 user_png_ver);
201 png_warning(png_ptr, msg);
202 }
203 sprintf(msg, "Application is running with png.c from libpng-%.20s",
204 png_libpng_ver);
205 png_warning(png_ptr, msg);
206 }
207 #endif
208 if(png_sizeof(png_struct) > png_struct_size)
209 {
210 png_ptr->error_fn=NULL;
211 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
212 png_ptr->flags=0;
213 #endif
214 png_error(png_ptr,
215 "The png struct allocated by the application for reading is too small.");
216 }
217 if(png_sizeof(png_info) > png_info_size)
218 {
219 png_ptr->error_fn=NULL;
220 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
221 png_ptr->flags=0;
222 #endif
223 png_error(png_ptr,
224 "The info struct allocated by application for reading is too small.");
225 }
226 png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
227 }
228 #endif /* PNG_1_0_X || PNG_1_2_X */
229
230 void PNGAPI
231 png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
232 png_size_t png_struct_size)
233 {
234 #ifdef PNG_SETJMP_SUPPORTED
235 jmp_buf tmp_jmp; /* to save current jump buffer */
236 #endif
237
238 int i=0;
239
240 png_structp png_ptr=*ptr_ptr;
241
242 do
243 {
244 if(user_png_ver[i] != png_libpng_ver[i])
245 {
246 #ifdef PNG_LEGACY_SUPPORTED
247 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
248 #else
249 png_ptr->warning_fn=NULL;
250 png_warning(png_ptr,
251 "Application uses deprecated png_read_init() and should be recompiled.");
252 break;
253 #endif
254 }
255 } while (png_libpng_ver[i++]);
256
257 png_debug(1, "in png_read_init_3\n");
258
259 #ifdef PNG_SETJMP_SUPPORTED
260 /* save jump buffer and error functions */
261 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
262 #endif
263
264 if(png_sizeof(png_struct) > png_struct_size)
265 {
266 png_destroy_struct(png_ptr);
267 *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
268 png_ptr = *ptr_ptr;
269 }
270
271 /* reset all variables to 0 */
272 png_memset(png_ptr, 0, png_sizeof (png_struct));
273
274 #ifdef PNG_SETJMP_SUPPORTED
275 /* restore jump buffer */
276 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
277 #endif
278
279 /* added at libpng-1.2.6 */
280 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
281 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
282 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
283 #endif
284
285 /* initialize zbuf - compression buffer */
286 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
287 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
288 (png_uint_32)png_ptr->zbuf_size);
289 png_ptr->zstream.zalloc = png_zalloc;
290 png_ptr->zstream.zfree = png_zfree;
291 png_ptr->zstream.opaque = (voidpf)png_ptr;
292
293 switch (inflateInit(&png_ptr->zstream))
294 {
295 case Z_OK: /* Do nothing */ break;
296 case Z_MEM_ERROR:
297 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
298 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
299 default: png_error(png_ptr, "Unknown zlib error");
300 }
301
302 png_ptr->zstream.next_out = png_ptr->zbuf;
303 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
304
305 png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
306 }
307
308 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
309 /* Read the information before the actual image data. This has been
310 * changed in v0.90 to allow reading a file that already has the magic
311 * bytes read from the stream. You can tell libpng how many bytes have
312 * been read from the beginning of the stream (up to the maximum of 8)
313 * via png_set_sig_bytes(), and we will only check the remaining bytes
314 * here. The application can then have access to the signature bytes we
315 * read if it is determined that this isn't a valid PNG file.
316 */
317 void PNGAPI
318 png_read_info(png_structp png_ptr, png_infop info_ptr)
319 {
320 png_debug(1, "in png_read_info\n");
321 /* If we haven't checked all of the PNG signature bytes, do so now. */
322 if (png_ptr->sig_bytes < 8)
323 {
324 png_size_t num_checked = png_ptr->sig_bytes,
325 num_to_check = 8 - num_checked;
326
327 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
328 png_ptr->sig_bytes = 8;
329
330 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
331 {
332 if (num_checked < 4 &&
333 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
334 png_error(png_ptr, "Not a PNG file");
335 else
336 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
337 }
338 if (num_checked < 3)
339 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
340 }
341
342 for(;;)
343 {
344 #ifdef PNG_USE_LOCAL_ARRAYS
345 PNG_IHDR;
346 PNG_IDAT;
347 PNG_IEND;
348 PNG_PLTE;
349 #if defined(PNG_READ_bKGD_SUPPORTED)
350 PNG_bKGD;
351 #endif
352 #if defined(PNG_READ_cHRM_SUPPORTED)
353 PNG_cHRM;
354 #endif
355 #if defined(PNG_READ_gAMA_SUPPORTED)
356 PNG_gAMA;
357 #endif
358 #if defined(PNG_READ_hIST_SUPPORTED)
359 PNG_hIST;
360 #endif
361 #if defined(PNG_READ_iCCP_SUPPORTED)
362 PNG_iCCP;
363 #endif
364 #if defined(PNG_READ_iTXt_SUPPORTED)
365 PNG_iTXt;
366 #endif
367 #if defined(PNG_READ_oFFs_SUPPORTED)
368 PNG_oFFs;
369 #endif
370 #if defined(PNG_READ_pCAL_SUPPORTED)
371 PNG_pCAL;
372 #endif
373 #if defined(PNG_READ_pHYs_SUPPORTED)
374 PNG_pHYs;
375 #endif
376 #if defined(PNG_READ_sBIT_SUPPORTED)
377 PNG_sBIT;
378 #endif
379 #if defined(PNG_READ_sCAL_SUPPORTED)
380 PNG_sCAL;
381 #endif
382 #if defined(PNG_READ_sPLT_SUPPORTED)
383 PNG_sPLT;
384 #endif
385 #if defined(PNG_READ_sRGB_SUPPORTED)
386 PNG_sRGB;
387 #endif
388 #if defined(PNG_READ_tEXt_SUPPORTED)
389 PNG_tEXt;
390 #endif
391 #if defined(PNG_READ_tIME_SUPPORTED)
392 PNG_tIME;
393 #endif
394 #if defined(PNG_READ_tRNS_SUPPORTED)
395 PNG_tRNS;
396 #endif
397 #if defined(PNG_READ_zTXt_SUPPORTED)
398 PNG_zTXt;
399 #endif
400 #endif /* PNG_USE_LOCAL_ARRAYS */
401 png_byte chunk_length[4];
402 png_uint_32 length;
403
404 png_read_data(png_ptr, chunk_length, 4);
405 length = png_get_uint_31(png_ptr,chunk_length);
406
407 png_reset_crc(png_ptr);
408 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
409
410 png_debug2(0, "Reading %s chunk, length=%lu.\n", png_ptr->chunk_name,
411 length);
412
413 /* This should be a binary subdivision search or a hash for
414 * matching the chunk name rather than a linear search.
415 */
416 if (!png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
417 if(png_ptr->mode & PNG_AFTER_IDAT)
418 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
419
420 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
421 png_handle_IHDR(png_ptr, info_ptr, length);
422 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
423 png_handle_IEND(png_ptr, info_ptr, length);
424 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
425 else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
426 {
427 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
428 png_ptr->mode |= PNG_HAVE_IDAT;
429 png_handle_unknown(png_ptr, info_ptr, length);
430 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
431 png_ptr->mode |= PNG_HAVE_PLTE;
432 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
433 {
434 if (!(png_ptr->mode & PNG_HAVE_IHDR))
435 png_error(png_ptr, "Missing IHDR before IDAT");
436 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
437 !(png_ptr->mode & PNG_HAVE_PLTE))
438 png_error(png_ptr, "Missing PLTE before IDAT");
439 break;
440 }
441 }
442 #endif
443 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
444 png_handle_PLTE(png_ptr, info_ptr, length);
445 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
446 {
447 if (!(png_ptr->mode & PNG_HAVE_IHDR))
448 png_error(png_ptr, "Missing IHDR before IDAT");
449 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
450 !(png_ptr->mode & PNG_HAVE_PLTE))
451 png_error(png_ptr, "Missing PLTE before IDAT");
452
453 png_ptr->idat_size = length;
454 png_ptr->mode |= PNG_HAVE_IDAT;
455 break;
456 }
457 #if defined(PNG_READ_bKGD_SUPPORTED)
458 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
459 png_handle_bKGD(png_ptr, info_ptr, length);
460 #endif
461 #if defined(PNG_READ_cHRM_SUPPORTED)
462 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
463 png_handle_cHRM(png_ptr, info_ptr, length);
464 #endif
465 #if defined(PNG_READ_gAMA_SUPPORTED)
466 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
467 png_handle_gAMA(png_ptr, info_ptr, length);
468 #endif
469 #if defined(PNG_READ_hIST_SUPPORTED)
470 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
471 png_handle_hIST(png_ptr, info_ptr, length);
472 #endif
473 #if defined(PNG_READ_oFFs_SUPPORTED)
474 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
475 png_handle_oFFs(png_ptr, info_ptr, length);
476 #endif
477 #if defined(PNG_READ_pCAL_SUPPORTED)
478 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
479 png_handle_pCAL(png_ptr, info_ptr, length);
480 #endif
481 #if defined(PNG_READ_sCAL_SUPPORTED)
482 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
483 png_handle_sCAL(png_ptr, info_ptr, length);
484 #endif
485 #if defined(PNG_READ_pHYs_SUPPORTED)
486 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
487 png_handle_pHYs(png_ptr, info_ptr, length);
488 #endif
489 #if defined(PNG_READ_sBIT_SUPPORTED)
490 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
491 png_handle_sBIT(png_ptr, info_ptr, length);
492 #endif
493 #if defined(PNG_READ_sRGB_SUPPORTED)
494 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
495 png_handle_sRGB(png_ptr, info_ptr, length);
496 #endif
497 #if defined(PNG_READ_iCCP_SUPPORTED)
498 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
499 png_handle_iCCP(png_ptr, info_ptr, length);
500 #endif
501 #if defined(PNG_READ_sPLT_SUPPORTED)
502 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
503 png_handle_sPLT(png_ptr, info_ptr, length);
504 #endif
505 #if defined(PNG_READ_tEXt_SUPPORTED)
506 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
507 png_handle_tEXt(png_ptr, info_ptr, length);
508 #endif
509 #if defined(PNG_READ_tIME_SUPPORTED)
510 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
511 png_handle_tIME(png_ptr, info_ptr, length);
512 #endif
513 #if defined(PNG_READ_tRNS_SUPPORTED)
514 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
515 png_handle_tRNS(png_ptr, info_ptr, length);
516 #endif
517 #if defined(PNG_READ_zTXt_SUPPORTED)
518 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
519 png_handle_zTXt(png_ptr, info_ptr, length);
520 #endif
521 #if defined(PNG_READ_iTXt_SUPPORTED)
522 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
523 png_handle_iTXt(png_ptr, info_ptr, length);
524 #endif
525 else
526 png_handle_unknown(png_ptr, info_ptr, length);
527 }
528 }
529 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
530
531 /* optional call to update the users info_ptr structure */
532 void PNGAPI
533 png_read_update_info(png_structp png_ptr, png_infop info_ptr)
534 {
535 png_debug(1, "in png_read_update_info\n");
536 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
537 png_read_start_row(png_ptr);
538 else
539 png_warning(png_ptr,
540 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
541 png_read_transform_info(png_ptr, info_ptr);
542 }
543
544 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
545 /* Initialize palette, background, etc, after transformations
546 * are set, but before any reading takes place. This allows
547 * the user to obtain a gamma-corrected palette, for example.
548 * If the user doesn't call this, we will do it ourselves.
549 */
550 void PNGAPI
551 png_start_read_image(png_structp png_ptr)
552 {
553 png_debug(1, "in png_start_read_image\n");
554 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
555 png_read_start_row(png_ptr);
556 }
557 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
558
559 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
560 void PNGAPI
561 png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
562 {
563 #ifdef PNG_USE_LOCAL_ARRAYS
564 PNG_IDAT;
565 const int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
566 const int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
567 #endif
568 int ret;
569 png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
570 png_ptr->row_number, png_ptr->pass);
571 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
572 png_read_start_row(png_ptr);
573 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
574 {
575 /* check for transforms that have been set but were defined out */
576 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
577 if (png_ptr->transformations & PNG_INVERT_MONO)
578 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
579 #endif
580 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
581 if (png_ptr->transformations & PNG_FILLER)
582 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
583 #endif
584 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
585 if (png_ptr->transformations & PNG_PACKSWAP)
586 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
587 #endif
588 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
589 if (png_ptr->transformations & PNG_PACK)
590 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
591 #endif
592 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
593 if (png_ptr->transformations & PNG_SHIFT)
594 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
595 #endif
596 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
597 if (png_ptr->transformations & PNG_BGR)
598 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
599 #endif
600 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
601 if (png_ptr->transformations & PNG_SWAP_BYTES)
602 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
603 #endif
604 }
605
606 #if defined(PNG_READ_INTERLACING_SUPPORTED)
607 /* if interlaced and we do not need a new row, combine row and return */
608 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
609 {
610 switch (png_ptr->pass)
611 {
612 case 0:
613 if (png_ptr->row_number & 0x07)
614 {
615 if (dsp_row != NULL)
616 png_combine_row(png_ptr, dsp_row,
617 png_pass_dsp_mask[png_ptr->pass]);
618 png_read_finish_row(png_ptr);
619 return;
620 }
621 break;
622 case 1:
623 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
624 {
625 if (dsp_row != NULL)
626 png_combine_row(png_ptr, dsp_row,
627 png_pass_dsp_mask[png_ptr->pass]);
628 png_read_finish_row(png_ptr);
629 return;
630 }
631 break;
632 case 2:
633 if ((png_ptr->row_number & 0x07) != 4)
634 {
635 if (dsp_row != NULL && (png_ptr->row_number & 4))
636 png_combine_row(png_ptr, dsp_row,
637 png_pass_dsp_mask[png_ptr->pass]);
638 png_read_finish_row(png_ptr);
639 return;
640 }
641 break;
642 case 3:
643 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
644 {
645 if (dsp_row != NULL)
646 png_combine_row(png_ptr, dsp_row,
647 png_pass_dsp_mask[png_ptr->pass]);
648 png_read_finish_row(png_ptr);
649 return;
650 }
651 break;
652 case 4:
653 if ((png_ptr->row_number & 3) != 2)
654 {
655 if (dsp_row != NULL && (png_ptr->row_number & 2))
656 png_combine_row(png_ptr, dsp_row,
657 png_pass_dsp_mask[png_ptr->pass]);
658 png_read_finish_row(png_ptr);
659 return;
660 }
661 break;
662 case 5:
663 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
664 {
665 if (dsp_row != NULL)
666 png_combine_row(png_ptr, dsp_row,
667 png_pass_dsp_mask[png_ptr->pass]);
668 png_read_finish_row(png_ptr);
669 return;
670 }
671 break;
672 case 6:
673 if (!(png_ptr->row_number & 1))
674 {
675 png_read_finish_row(png_ptr);
676 return;
677 }
678 break;
679 }
680 }
681 #endif
682
683 if (!(png_ptr->mode & PNG_HAVE_IDAT))
684 png_error(png_ptr, "Invalid attempt to read row data");
685
686 png_ptr->zstream.next_out = png_ptr->row_buf;
687 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
688 do
689 {
690 if (!(png_ptr->zstream.avail_in))
691 {
692 while (!png_ptr->idat_size)
693 {
694 png_byte chunk_length[4];
695
696 png_crc_finish(png_ptr, 0);
697
698 png_read_data(png_ptr, chunk_length, 4);
699 png_ptr->idat_size = png_get_uint_31(png_ptr,chunk_length);
700
701 png_reset_crc(png_ptr);
702 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
703 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
704 png_error(png_ptr, "Not enough image data");
705 }
706 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
707 png_ptr->zstream.next_in = png_ptr->zbuf;
708 if (png_ptr->zbuf_size > png_ptr->idat_size)
709 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
710 png_crc_read(png_ptr, png_ptr->zbuf,
711 (png_size_t)png_ptr->zstream.avail_in);
712 png_ptr->idat_size -= png_ptr->zstream.avail_in;
713 }
714 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
715 if (ret == Z_STREAM_END)
716 {
717 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
718 png_ptr->idat_size)
719 png_error(png_ptr, "Extra compressed data");
720 png_ptr->mode |= PNG_AFTER_IDAT;
721 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
722 break;
723 }
724 if (ret != Z_OK)
725 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
726 "Decompression error");
727
728 } while (png_ptr->zstream.avail_out);
729
730 png_ptr->row_info.color_type = png_ptr->color_type;
731 png_ptr->row_info.width = png_ptr->iwidth;
732 png_ptr->row_info.channels = png_ptr->channels;
733 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
734 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
735 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
736 png_ptr->row_info.width);
737
738 if(png_ptr->row_buf[0])
739 png_read_filter_row(png_ptr, &(png_ptr->row_info),
740 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
741 (int)(png_ptr->row_buf[0]));
742
743 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
744 png_ptr->rowbytes + 1);
745
746 #if defined(PNG_MNG_FEATURES_SUPPORTED)
747 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
748 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
749 {
750 /* Intrapixel differencing */
751 png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
752 }
753 #endif
754
755
756 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
757 png_do_read_transformations(png_ptr);
758
759 #if defined(PNG_READ_INTERLACING_SUPPORTED)
760 /* blow up interlaced rows to full size */
761 if (png_ptr->interlaced &&
762 (png_ptr->transformations & PNG_INTERLACE))
763 {
764 if (png_ptr->pass < 6)
765 /* old interface (pre-1.0.9):
766 png_do_read_interlace(&(png_ptr->row_info),
767 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
768 */
769 png_do_read_interlace(png_ptr);
770
771 if (dsp_row != NULL)
772 png_combine_row(png_ptr, dsp_row,
773 png_pass_dsp_mask[png_ptr->pass]);
774 if (row != NULL)
775 png_combine_row(png_ptr, row,
776 png_pass_mask[png_ptr->pass]);
777 }
778 else
779 #endif
780 {
781 if (row != NULL)
782 png_combine_row(png_ptr, row, 0xff);
783 if (dsp_row != NULL)
784 png_combine_row(png_ptr, dsp_row, 0xff);
785 }
786 png_read_finish_row(png_ptr);
787
788 if (png_ptr->read_row_fn != NULL)
789 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
790 }
791 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
792
793 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
794 /* Read one or more rows of image data. If the image is interlaced,
795 * and png_set_interlace_handling() has been called, the rows need to
796 * contain the contents of the rows from the previous pass. If the
797 * image has alpha or transparency, and png_handle_alpha()[*] has been
798 * called, the rows contents must be initialized to the contents of the
799 * screen.
800 *
801 * "row" holds the actual image, and pixels are placed in it
802 * as they arrive. If the image is displayed after each pass, it will
803 * appear to "sparkle" in. "display_row" can be used to display a
804 * "chunky" progressive image, with finer detail added as it becomes
805 * available. If you do not want this "chunky" display, you may pass
806 * NULL for display_row. If you do not want the sparkle display, and
807 * you have not called png_handle_alpha(), you may pass NULL for rows.
808 * If you have called png_handle_alpha(), and the image has either an
809 * alpha channel or a transparency chunk, you must provide a buffer for
810 * rows. In this case, you do not have to provide a display_row buffer
811 * also, but you may. If the image is not interlaced, or if you have
812 * not called png_set_interlace_handling(), the display_row buffer will
813 * be ignored, so pass NULL to it.
814 *
815 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
816 */
817
818 void PNGAPI
819 png_read_rows(png_structp png_ptr, png_bytepp row,
820 png_bytepp display_row, png_uint_32 num_rows)
821 {
822 png_uint_32 i;
823 png_bytepp rp;
824 png_bytepp dp;
825
826 png_debug(1, "in png_read_rows\n");
827 rp = row;
828 dp = display_row;
829 if (rp != NULL && dp != NULL)
830 for (i = 0; i < num_rows; i++)
831 {
832 png_bytep rptr = *rp++;
833 png_bytep dptr = *dp++;
834
835 png_read_row(png_ptr, rptr, dptr);
836 }
837 else if(rp != NULL)
838 for (i = 0; i < num_rows; i++)
839 {
840 png_bytep rptr = *rp;
841 png_read_row(png_ptr, rptr, png_bytep_NULL);
842 rp++;
843 }
844 else if(dp != NULL)
845 for (i = 0; i < num_rows; i++)
846 {
847 png_bytep dptr = *dp;
848 png_read_row(png_ptr, png_bytep_NULL, dptr);
849 dp++;
850 }
851 }
852 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
853
854 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
855 /* Read the entire image. If the image has an alpha channel or a tRNS
856 * chunk, and you have called png_handle_alpha()[*], you will need to
857 * initialize the image to the current image that PNG will be overlaying.
858 * We set the num_rows again here, in case it was incorrectly set in
859 * png_read_start_row() by a call to png_read_update_info() or
860 * png_start_read_image() if png_set_interlace_handling() wasn't called
861 * prior to either of these functions like it should have been. You can
862 * only call this function once. If you desire to have an image for
863 * each pass of a interlaced image, use png_read_rows() instead.
864 *
865 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
866 */
867 void PNGAPI
868 png_read_image(png_structp png_ptr, png_bytepp image)
869 {
870 png_uint_32 i,image_height;
871 int pass, j;
872 png_bytepp rp;
873
874 png_debug(1, "in png_read_image\n");
875
876 #ifdef PNG_READ_INTERLACING_SUPPORTED
877 pass = png_set_interlace_handling(png_ptr);
878 #else
879 if (png_ptr->interlaced)
880 png_error(png_ptr,
881 "Cannot read interlaced image -- interlace handler disabled.");
882 pass = 1;
883 #endif
884
885
886 image_height=png_ptr->height;
887 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
888
889 for (j = 0; j < pass; j++)
890 {
891 rp = image;
892 for (i = 0; i < image_height; i++)
893 {
894 png_read_row(png_ptr, *rp, png_bytep_NULL);
895 rp++;
896 }
897 }
898 }
899 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
900
901 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
902 /* Read the end of the PNG file. Will not read past the end of the
903 * file, will verify the end is accurate, and will read any comments
904 * or time information at the end of the file, if info is not NULL.
905 */
906 void PNGAPI
907 png_read_end(png_structp png_ptr, png_infop info_ptr)
908 {
909 png_byte chunk_length[4];
910 png_uint_32 length;
911
912 png_debug(1, "in png_read_end\n");
913 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
914
915 do
916 {
917 #ifdef PNG_USE_LOCAL_ARRAYS
918 PNG_IHDR;
919 PNG_IDAT;
920 PNG_IEND;
921 PNG_PLTE;
922 #if defined(PNG_READ_bKGD_SUPPORTED)
923 PNG_bKGD;
924 #endif
925 #if defined(PNG_READ_cHRM_SUPPORTED)
926 PNG_cHRM;
927 #endif
928 #if defined(PNG_READ_gAMA_SUPPORTED)
929 PNG_gAMA;
930 #endif
931 #if defined(PNG_READ_hIST_SUPPORTED)
932 PNG_hIST;
933 #endif
934 #if defined(PNG_READ_iCCP_SUPPORTED)
935 PNG_iCCP;
936 #endif
937 #if defined(PNG_READ_iTXt_SUPPORTED)
938 PNG_iTXt;
939 #endif
940 #if defined(PNG_READ_oFFs_SUPPORTED)
941 PNG_oFFs;
942 #endif
943 #if defined(PNG_READ_pCAL_SUPPORTED)
944 PNG_pCAL;
945 #endif
946 #if defined(PNG_READ_pHYs_SUPPORTED)
947 PNG_pHYs;
948 #endif
949 #if defined(PNG_READ_sBIT_SUPPORTED)
950 PNG_sBIT;
951 #endif
952 #if defined(PNG_READ_sCAL_SUPPORTED)
953 PNG_sCAL;
954 #endif
955 #if defined(PNG_READ_sPLT_SUPPORTED)
956 PNG_sPLT;
957 #endif
958 #if defined(PNG_READ_sRGB_SUPPORTED)
959 PNG_sRGB;
960 #endif
961 #if defined(PNG_READ_tEXt_SUPPORTED)
962 PNG_tEXt;
963 #endif
964 #if defined(PNG_READ_tIME_SUPPORTED)
965 PNG_tIME;
966 #endif
967 #if defined(PNG_READ_tRNS_SUPPORTED)
968 PNG_tRNS;
969 #endif
970 #if defined(PNG_READ_zTXt_SUPPORTED)
971 PNG_zTXt;
972 #endif
973 #endif /* PNG_USE_LOCAL_ARRAYS */
974
975 png_read_data(png_ptr, chunk_length, 4);
976 length = png_get_uint_31(png_ptr,chunk_length);
977
978 png_reset_crc(png_ptr);
979 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
980
981 png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
982
983 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
984 png_handle_IHDR(png_ptr, info_ptr, length);
985 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
986 png_handle_IEND(png_ptr, info_ptr, length);
987 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
988 else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
989 {
990 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
991 {
992 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
993 png_error(png_ptr, "Too many IDAT's found");
994 }
995 png_handle_unknown(png_ptr, info_ptr, length);
996 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
997 png_ptr->mode |= PNG_HAVE_PLTE;
998 }
999 #endif
1000 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
1001 {
1002 /* Zero length IDATs are legal after the last IDAT has been
1003 * read, but not after other chunks have been read.
1004 */
1005 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
1006 png_error(png_ptr, "Too many IDAT's found");
1007 png_crc_finish(png_ptr, length);
1008 }
1009 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
1010 png_handle_PLTE(png_ptr, info_ptr, length);
1011 #if defined(PNG_READ_bKGD_SUPPORTED)
1012 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
1013 png_handle_bKGD(png_ptr, info_ptr, length);
1014 #endif
1015 #if defined(PNG_READ_cHRM_SUPPORTED)
1016 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
1017 png_handle_cHRM(png_ptr, info_ptr, length);
1018 #endif
1019 #if defined(PNG_READ_gAMA_SUPPORTED)
1020 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
1021 png_handle_gAMA(png_ptr, info_ptr, length);
1022 #endif
1023 #if defined(PNG_READ_hIST_SUPPORTED)
1024 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
1025 png_handle_hIST(png_ptr, info_ptr, length);
1026 #endif
1027 #if defined(PNG_READ_oFFs_SUPPORTED)
1028 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
1029 png_handle_oFFs(png_ptr, info_ptr, length);
1030 #endif
1031 #if defined(PNG_READ_pCAL_SUPPORTED)
1032 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
1033 png_handle_pCAL(png_ptr, info_ptr, length);
1034 #endif
1035 #if defined(PNG_READ_sCAL_SUPPORTED)
1036 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
1037 png_handle_sCAL(png_ptr, info_ptr, length);
1038 #endif
1039 #if defined(PNG_READ_pHYs_SUPPORTED)
1040 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
1041 png_handle_pHYs(png_ptr, info_ptr, length);
1042 #endif
1043 #if defined(PNG_READ_sBIT_SUPPORTED)
1044 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
1045 png_handle_sBIT(png_ptr, info_ptr, length);
1046 #endif
1047 #if defined(PNG_READ_sRGB_SUPPORTED)
1048 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
1049 png_handle_sRGB(png_ptr, info_ptr, length);
1050 #endif
1051 #if defined(PNG_READ_iCCP_SUPPORTED)
1052 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
1053 png_handle_iCCP(png_ptr, info_ptr, length);
1054 #endif
1055 #if defined(PNG_READ_sPLT_SUPPORTED)
1056 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
1057 png_handle_sPLT(png_ptr, info_ptr, length);
1058 #endif
1059 #if defined(PNG_READ_tEXt_SUPPORTED)
1060 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
1061 png_handle_tEXt(png_ptr, info_ptr, length);
1062 #endif
1063 #if defined(PNG_READ_tIME_SUPPORTED)
1064 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
1065 png_handle_tIME(png_ptr, info_ptr, length);
1066 #endif
1067 #if defined(PNG_READ_tRNS_SUPPORTED)
1068 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
1069 png_handle_tRNS(png_ptr, info_ptr, length);
1070 #endif
1071 #if defined(PNG_READ_zTXt_SUPPORTED)
1072 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
1073 png_handle_zTXt(png_ptr, info_ptr, length);
1074 #endif
1075 #if defined(PNG_READ_iTXt_SUPPORTED)
1076 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
1077 png_handle_iTXt(png_ptr, info_ptr, length);
1078 #endif
1079 else
1080 png_handle_unknown(png_ptr, info_ptr, length);
1081 } while (!(png_ptr->mode & PNG_HAVE_IEND));
1082 }
1083 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
1084
1085 /* free all memory used by the read */
1086 void PNGAPI
1087 png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
1088 png_infopp end_info_ptr_ptr)
1089 {
1090 png_structp png_ptr = NULL;
1091 png_infop info_ptr = NULL, end_info_ptr = NULL;
1092 #ifdef PNG_USER_MEM_SUPPORTED
1093 png_free_ptr free_fn;
1094 png_voidp mem_ptr;
1095 #endif
1096
1097 png_debug(1, "in png_destroy_read_struct\n");
1098 if (png_ptr_ptr != NULL)
1099 png_ptr = *png_ptr_ptr;
1100
1101 if (info_ptr_ptr != NULL)
1102 info_ptr = *info_ptr_ptr;
1103
1104 if (end_info_ptr_ptr != NULL)
1105 end_info_ptr = *end_info_ptr_ptr;
1106
1107 #ifdef PNG_USER_MEM_SUPPORTED
1108 free_fn = png_ptr->free_fn;
1109 mem_ptr = png_ptr->mem_ptr;
1110 #endif
1111
1112 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
1113
1114 if (info_ptr != NULL)
1115 {
1116 #if defined(PNG_TEXT_SUPPORTED)
1117 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
1118 #endif
1119
1120 #ifdef PNG_USER_MEM_SUPPORTED
1121 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1122 (png_voidp)mem_ptr);
1123 #else
1124 png_destroy_struct((png_voidp)info_ptr);
1125 #endif
1126 *info_ptr_ptr = NULL;
1127 }
1128
1129 if (end_info_ptr != NULL)
1130 {
1131 #if defined(PNG_READ_TEXT_SUPPORTED)
1132 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
1133 #endif
1134 #ifdef PNG_USER_MEM_SUPPORTED
1135 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
1136 (png_voidp)mem_ptr);
1137 #else
1138 png_destroy_struct((png_voidp)end_info_ptr);
1139 #endif
1140 *end_info_ptr_ptr = NULL;
1141 }
1142
1143 if (png_ptr != NULL)
1144 {
1145 #ifdef PNG_USER_MEM_SUPPORTED
1146 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1147 (png_voidp)mem_ptr);
1148 #else
1149 png_destroy_struct((png_voidp)png_ptr);
1150 #endif
1151 *png_ptr_ptr = NULL;
1152 }
1153 }
1154
1155 /* free all memory used by the read (old method) */
1156 void /* PRIVATE */
1157 png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
1158 {
1159 #ifdef PNG_SETJMP_SUPPORTED
1160 jmp_buf tmp_jmp;
1161 #endif
1162 png_error_ptr error_fn;
1163 png_error_ptr warning_fn;
1164 png_voidp error_ptr;
1165 #ifdef PNG_USER_MEM_SUPPORTED
1166 png_free_ptr free_fn;
1167 #endif
1168
1169 png_debug(1, "in png_read_destroy\n");
1170 if (info_ptr != NULL)
1171 png_info_destroy(png_ptr, info_ptr);
1172
1173 if (end_info_ptr != NULL)
1174 png_info_destroy(png_ptr, end_info_ptr);
1175
1176 png_free(png_ptr, png_ptr->zbuf);
1177 png_free(png_ptr, png_ptr->big_row_buf);
1178 png_free(png_ptr, png_ptr->prev_row);
1179 #if defined(PNG_READ_DITHER_SUPPORTED)
1180 png_free(png_ptr, png_ptr->palette_lookup);
1181 png_free(png_ptr, png_ptr->dither_index);
1182 #endif
1183 #if defined(PNG_READ_GAMMA_SUPPORTED)
1184 png_free(png_ptr, png_ptr->gamma_table);
1185 #endif
1186 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1187 png_free(png_ptr, png_ptr->gamma_from_1);
1188 png_free(png_ptr, png_ptr->gamma_to_1);
1189 #endif
1190 #ifdef PNG_FREE_ME_SUPPORTED
1191 if (png_ptr->free_me & PNG_FREE_PLTE)
1192 png_zfree(png_ptr, png_ptr->palette);
1193 png_ptr->free_me &= ~PNG_FREE_PLTE;
1194 #else
1195 if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
1196 png_zfree(png_ptr, png_ptr->palette);
1197 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
1198 #endif
1199 #if defined(PNG_tRNS_SUPPORTED) || \
1200 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1201 #ifdef PNG_FREE_ME_SUPPORTED
1202 if (png_ptr->free_me & PNG_FREE_TRNS)
1203 png_free(png_ptr, png_ptr->trans);
1204 png_ptr->free_me &= ~PNG_FREE_TRNS;
1205 #else
1206 if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
1207 png_free(png_ptr, png_ptr->trans);
1208 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
1209 #endif
1210 #endif
1211 #if defined(PNG_READ_hIST_SUPPORTED)
1212 #ifdef PNG_FREE_ME_SUPPORTED
1213 if (png_ptr->free_me & PNG_FREE_HIST)
1214 png_free(png_ptr, png_ptr->hist);
1215 png_ptr->free_me &= ~PNG_FREE_HIST;
1216 #else
1217 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
1218 png_free(png_ptr, png_ptr->hist);
1219 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
1220 #endif
1221 #endif
1222 #if defined(PNG_READ_GAMMA_SUPPORTED)
1223 if (png_ptr->gamma_16_table != NULL)
1224 {
1225 int i;
1226 int istop = (1 << (8 - png_ptr->gamma_shift));
1227 for (i = 0; i < istop; i++)
1228 {
1229 png_free(png_ptr, png_ptr->gamma_16_table[i]);
1230 }
1231 png_free(png_ptr, png_ptr->gamma_16_table);
1232 }
1233 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1234 if (png_ptr->gamma_16_from_1 != NULL)
1235 {
1236 int i;
1237 int istop = (1 << (8 - png_ptr->gamma_shift));
1238 for (i = 0; i < istop; i++)
1239 {
1240 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
1241 }
1242 png_free(png_ptr, png_ptr->gamma_16_from_1);
1243 }
1244 if (png_ptr->gamma_16_to_1 != NULL)
1245 {
1246 int i;
1247 int istop = (1 << (8 - png_ptr->gamma_shift));
1248 for (i = 0; i < istop; i++)
1249 {
1250 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
1251 }
1252 png_free(png_ptr, png_ptr->gamma_16_to_1);
1253 }
1254 #endif
1255 #endif
1256 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1257 png_free(png_ptr, png_ptr->time_buffer);
1258 #endif
1259
1260 inflateEnd(&png_ptr->zstream);
1261 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1262 png_free(png_ptr, png_ptr->save_buffer);
1263 #endif
1264
1265 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1266 #ifdef PNG_TEXT_SUPPORTED
1267 png_free(png_ptr, png_ptr->current_text);
1268 #endif /* PNG_TEXT_SUPPORTED */
1269 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1270
1271 /* Save the important info out of the png_struct, in case it is
1272 * being used again.
1273 */
1274 #ifdef PNG_SETJMP_SUPPORTED
1275 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
1276 #endif
1277
1278 error_fn = png_ptr->error_fn;
1279 warning_fn = png_ptr->warning_fn;
1280 error_ptr = png_ptr->error_ptr;
1281 #ifdef PNG_USER_MEM_SUPPORTED
1282 free_fn = png_ptr->free_fn;
1283 #endif
1284
1285 png_memset(png_ptr, 0, png_sizeof (png_struct));
1286
1287 png_ptr->error_fn = error_fn;
1288 png_ptr->warning_fn = warning_fn;
1289 png_ptr->error_ptr = error_ptr;
1290 #ifdef PNG_USER_MEM_SUPPORTED
1291 png_ptr->free_fn = free_fn;
1292 #endif
1293
1294 #ifdef PNG_SETJMP_SUPPORTED
1295 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
1296 #endif
1297
1298 }
1299
1300 void PNGAPI
1301 png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1302 {
1303 png_ptr->read_row_fn = read_row_fn;
1304 }
1305
1306
1307 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
1308 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1309 void PNGAPI
1310 png_read_png(png_structp png_ptr, png_infop info_ptr,
1311 int transforms,
1312 voidp params)
1313 {
1314 int row;
1315
1316 #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1317 /* invert the alpha channel from opacity to transparency
1318 */
1319 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1320 png_set_invert_alpha(png_ptr);
1321 #endif
1322
1323 /* png_read_info() gives us all of the information from the
1324 * PNG file before the first IDAT (image data chunk).
1325 */
1326 png_read_info(png_ptr, info_ptr);
1327 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
1328 png_error(png_ptr,"Image is too high to process with png_read_png()");
1329
1330 /* -------------- image transformations start here ------------------- */
1331
1332 #if defined(PNG_READ_16_TO_8_SUPPORTED)
1333 /* tell libpng to strip 16 bit/color files down to 8 bits per color
1334 */
1335 if (transforms & PNG_TRANSFORM_STRIP_16)
1336 png_set_strip_16(png_ptr);
1337 #endif
1338
1339 #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1340 /* Strip alpha bytes from the input data without combining with
1341 * the background (not recommended).
1342 */
1343 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
1344 png_set_strip_alpha(png_ptr);
1345 #endif
1346
1347 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1348 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1349 * byte into separate bytes (useful for paletted and grayscale images).
1350 */
1351 if (transforms & PNG_TRANSFORM_PACKING)
1352 png_set_packing(png_ptr);
1353 #endif
1354
1355 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
1356 /* Change the order of packed pixels to least significant bit first
1357 * (not useful if you are using png_set_packing).
1358 */
1359 if (transforms & PNG_TRANSFORM_PACKSWAP)
1360 png_set_packswap(png_ptr);
1361 #endif
1362
1363 #if defined(PNG_READ_EXPAND_SUPPORTED)
1364 /* Expand paletted colors into true RGB triplets
1365 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1366 * Expand paletted or RGB images with transparency to full alpha
1367 * channels so the data will be available as RGBA quartets.
1368 */
1369 if (transforms & PNG_TRANSFORM_EXPAND)
1370 if ((png_ptr->bit_depth < 8) ||
1371 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1372 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1373 png_set_expand(png_ptr);
1374 #endif
1375
1376 /* We don't handle background color or gamma transformation or dithering.
1377 */
1378
1379 #if defined(PNG_READ_INVERT_SUPPORTED)
1380 /* invert monochrome files to have 0 as white and 1 as black
1381 */
1382 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1383 png_set_invert_mono(png_ptr);
1384 #endif
1385
1386 #if defined(PNG_READ_SHIFT_SUPPORTED)
1387 /* If you want to shift the pixel values from the range [0,255] or
1388 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1389 * colors were originally in:
1390 */
1391 if ((transforms & PNG_TRANSFORM_SHIFT)
1392 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1393 {
1394 png_color_8p sig_bit;
1395
1396 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1397 png_set_shift(png_ptr, sig_bit);
1398 }
1399 #endif
1400
1401 #if defined(PNG_READ_BGR_SUPPORTED)
1402 /* flip the RGB pixels to BGR (or RGBA to BGRA)
1403 */
1404 if (transforms & PNG_TRANSFORM_BGR)
1405 png_set_bgr(png_ptr);
1406 #endif
1407
1408 #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1409 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1410 */
1411 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1412 png_set_swap_alpha(png_ptr);
1413 #endif
1414
1415 #if defined(PNG_READ_SWAP_SUPPORTED)
1416 /* swap bytes of 16 bit files to least significant byte first
1417 */
1418 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1419 png_set_swap(png_ptr);
1420 #endif
1421
1422 /* We don't handle adding filler bytes */
1423
1424 /* Optional call to gamma correct and add the background to the palette
1425 * and update info structure. REQUIRED if you are expecting libpng to
1426 * update the palette for you (i.e., you selected such a transform above).
1427 */
1428 png_read_update_info(png_ptr, info_ptr);
1429
1430 /* -------------- image transformations end here ------------------- */
1431
1432 #ifdef PNG_FREE_ME_SUPPORTED
1433 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1434 #endif
1435 if(info_ptr->row_pointers == NULL)
1436 {
1437 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
1438 info_ptr->height * png_sizeof(png_bytep));
1439 #ifdef PNG_FREE_ME_SUPPORTED
1440 info_ptr->free_me |= PNG_FREE_ROWS;
1441 #endif
1442 for (row = 0; row < (int)info_ptr->height; row++)
1443 {
1444 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
1445 png_get_rowbytes(png_ptr, info_ptr));
1446 }
1447 }
1448
1449 png_read_image(png_ptr, info_ptr->row_pointers);
1450 info_ptr->valid |= PNG_INFO_IDAT;
1451
1452 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
1453 png_read_end(png_ptr, info_ptr);
1454
1455 if(transforms == 0 || params == NULL)
1456 /* quiet compiler warnings */ return;
1457
1458 }
1459 #endif /* PNG_INFO_IMAGE_SUPPORTED */
1460 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
1461 #endif /* PNG_READ_SUPPORTED */