2 /* pngread.c - read a PNG file
4 * libpng 1.2.8 - December 3, 2004
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2004 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.)
10 * This file contains routines that an application calls directly to
11 * read a PNG file or stream.
17 /* Create a PNG structure for reading, and allocate any memory needed. */
19 png_create_read_struct(png_const_charp user_png_ver
, png_voidp error_ptr
,
20 png_error_ptr error_fn
, png_error_ptr warn_fn
)
23 #ifdef PNG_USER_MEM_SUPPORTED
24 return (png_create_read_struct_2(user_png_ver
, error_ptr
, error_fn
,
25 warn_fn
, png_voidp_NULL
, png_malloc_ptr_NULL
, png_free_ptr_NULL
));
28 /* Alternate create PNG structure for reading, and allocate any memory needed. */
30 png_create_read_struct_2(png_const_charp user_png_ver
, png_voidp error_ptr
,
31 png_error_ptr error_fn
, png_error_ptr warn_fn
, png_voidp mem_ptr
,
32 png_malloc_ptr malloc_fn
, png_free_ptr free_fn
)
34 #endif /* PNG_USER_MEM_SUPPORTED */
38 #ifdef PNG_SETJMP_SUPPORTED
39 #ifdef USE_FAR_KEYWORD
46 png_debug(1, "in png_create_read_struct\n");
47 #ifdef PNG_USER_MEM_SUPPORTED
48 png_ptr
= (png_structp
)png_create_struct_2(PNG_STRUCT_PNG
,
49 (png_malloc_ptr
)malloc_fn
, (png_voidp
)mem_ptr
);
51 png_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
56 #if !defined(PNG_1_0_X)
57 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
58 png_init_mmx_flags(png_ptr
); /* 1.2.0 addition */
60 #endif /* PNG_1_0_X */
62 /* added at libpng-1.2.6 */
63 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
64 png_ptr
->user_width_max
=PNG_USER_WIDTH_MAX
;
65 png_ptr
->user_height_max
=PNG_USER_HEIGHT_MAX
;
68 #ifdef PNG_SETJMP_SUPPORTED
69 #ifdef USE_FAR_KEYWORD
72 if (setjmp(png_ptr
->jmpbuf
))
75 png_free(png_ptr
, png_ptr
->zbuf
);
77 #ifdef PNG_USER_MEM_SUPPORTED
78 png_destroy_struct_2((png_voidp
)png_ptr
,
79 (png_free_ptr
)free_fn
, (png_voidp
)mem_ptr
);
81 png_destroy_struct((png_voidp
)png_ptr
);
85 #ifdef USE_FAR_KEYWORD
86 png_memcpy(png_ptr
->jmpbuf
,jmpbuf
,png_sizeof(jmp_buf));
90 #ifdef PNG_USER_MEM_SUPPORTED
91 png_set_mem_fn(png_ptr
, mem_ptr
, malloc_fn
, free_fn
);
94 png_set_error_fn(png_ptr
, error_ptr
, error_fn
, warn_fn
);
99 if(user_png_ver
[i
] != png_libpng_ver
[i
])
100 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
101 } while (png_libpng_ver
[i
++]);
103 if (png_ptr
->flags
& PNG_FLAG_LIBRARY_MISMATCH
)
105 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
106 * we must recompile any applications that use any older library version.
107 * For versions after libpng 1.0, we will be compatible, so we need
108 * only check the first digit.
110 if (user_png_ver
== NULL
|| user_png_ver
[0] != png_libpng_ver
[0] ||
111 (user_png_ver
[0] == '1' && user_png_ver
[2] != png_libpng_ver
[2]) ||
112 (user_png_ver
[0] == '0' && user_png_ver
[2] < '9'))
114 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
118 sprintf(msg
, "Application was compiled with png.h from libpng-%.20s",
120 png_warning(png_ptr
, msg
);
122 sprintf(msg
, "Application is running with png.c from libpng-%.20s",
124 png_warning(png_ptr
, msg
);
126 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
130 "Incompatible libpng version in application and library");
134 /* initialize zbuf - compression buffer */
135 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
136 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
137 (png_uint_32
)png_ptr
->zbuf_size
);
138 png_ptr
->zstream
.zalloc
= png_zalloc
;
139 png_ptr
->zstream
.zfree
= png_zfree
;
140 png_ptr
->zstream
.opaque
= (voidpf
)png_ptr
;
142 switch (inflateInit(&png_ptr
->zstream
))
144 case Z_OK
: /* Do nothing */ break;
146 case Z_STREAM_ERROR
: png_error(png_ptr
, "zlib memory error"); break;
147 case Z_VERSION_ERROR
: png_error(png_ptr
, "zlib version error"); break;
148 default: png_error(png_ptr
, "Unknown zlib error");
151 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
152 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
154 png_set_read_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
);
156 #ifdef PNG_SETJMP_SUPPORTED
157 /* Applications that neglect to set up their own setjmp() and then encounter
158 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
159 abort instead of returning. */
160 #ifdef USE_FAR_KEYWORD
163 png_memcpy(png_ptr
->jmpbuf
,jmpbuf
,png_sizeof(jmp_buf));
165 if (setjmp(png_ptr
->jmpbuf
))
172 /* Initialize PNG structure for reading, and allocate any memory needed.
173 This interface is deprecated in favour of the png_create_read_struct(),
174 and it will eventually disappear. */
175 #if defined(PNG_1_0_X) || defined (PNG_1_2_X)
178 png_read_init(png_structp png_ptr
)
180 /* We only come here via pre-1.0.7-compiled applications */
181 png_read_init_2(png_ptr
, "1.0.6 or earlier", 0, 0);
186 png_read_init_2(png_structp png_ptr
, png_const_charp user_png_ver
,
187 png_size_t png_struct_size
, png_size_t png_info_size
)
189 /* We only come here via pre-1.0.12-compiled applications */
190 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
191 if(png_sizeof(png_struct
) > png_struct_size
||
192 png_sizeof(png_info
) > png_info_size
)
195 png_ptr
->warning_fn
=NULL
;
198 sprintf(msg
, "Application was compiled with png.h from libpng-%.20s",
200 png_warning(png_ptr
, msg
);
202 sprintf(msg
, "Application is running with png.c from libpng-%.20s",
204 png_warning(png_ptr
, msg
);
207 if(png_sizeof(png_struct
) > png_struct_size
)
209 png_ptr
->error_fn
=NULL
;
210 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
214 "The png struct allocated by the application for reading is too small.");
216 if(png_sizeof(png_info
) > png_info_size
)
218 png_ptr
->error_fn
=NULL
;
219 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
223 "The info struct allocated by application for reading is too small.");
225 png_read_init_3(&png_ptr
, user_png_ver
, png_struct_size
);
229 png_read_init_3(png_structpp ptr_ptr
, png_const_charp user_png_ver
,
230 png_size_t png_struct_size
)
232 #ifdef PNG_SETJMP_SUPPORTED
233 jmp_buf tmp_jmp
; /* to save current jump buffer */
238 png_structp png_ptr
=*ptr_ptr
;
242 if(user_png_ver
[i
] != png_libpng_ver
[i
])
244 #ifdef PNG_LEGACY_SUPPORTED
245 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
247 png_ptr
->warning_fn
=NULL
;
249 "Application uses deprecated png_read_init() and should be recompiled.");
253 } while (png_libpng_ver
[i
++]);
255 png_debug(1, "in png_read_init_3\n");
257 #ifdef PNG_SETJMP_SUPPORTED
258 /* save jump buffer and error functions */
259 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, png_sizeof (jmp_buf));
262 if(png_sizeof(png_struct
) > png_struct_size
)
264 png_destroy_struct(png_ptr
);
265 *ptr_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
269 /* reset all variables to 0 */
270 png_memset(png_ptr
, 0, png_sizeof (png_struct
));
272 #ifdef PNG_SETJMP_SUPPORTED
273 /* restore jump buffer */
274 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, png_sizeof (jmp_buf));
277 /* added at libpng-1.2.6 */
278 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
279 png_ptr
->user_width_max
=PNG_USER_WIDTH_MAX
;
280 png_ptr
->user_height_max
=PNG_USER_HEIGHT_MAX
;
283 /* initialize zbuf - compression buffer */
284 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
285 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
286 (png_uint_32
)png_ptr
->zbuf_size
);
287 png_ptr
->zstream
.zalloc
= png_zalloc
;
288 png_ptr
->zstream
.zfree
= png_zfree
;
289 png_ptr
->zstream
.opaque
= (voidpf
)png_ptr
;
291 switch (inflateInit(&png_ptr
->zstream
))
293 case Z_OK
: /* Do nothing */ break;
295 case Z_STREAM_ERROR
: png_error(png_ptr
, "zlib memory"); break;
296 case Z_VERSION_ERROR
: png_error(png_ptr
, "zlib version"); break;
297 default: png_error(png_ptr
, "Unknown zlib error");
300 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
301 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
303 png_set_read_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
);
306 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
307 /* Read the information before the actual image data. This has been
308 * changed in v0.90 to allow reading a file that already has the magic
309 * bytes read from the stream. You can tell libpng how many bytes have
310 * been read from the beginning of the stream (up to the maximum of 8)
311 * via png_set_sig_bytes(), and we will only check the remaining bytes
312 * here. The application can then have access to the signature bytes we
313 * read if it is determined that this isn't a valid PNG file.
316 png_read_info(png_structp png_ptr
, png_infop info_ptr
)
318 png_debug(1, "in png_read_info\n");
319 /* If we haven't checked all of the PNG signature bytes, do so now. */
320 if (png_ptr
->sig_bytes
< 8)
322 png_size_t num_checked
= png_ptr
->sig_bytes
,
323 num_to_check
= 8 - num_checked
;
325 png_read_data(png_ptr
, &(info_ptr
->signature
[num_checked
]), num_to_check
);
326 png_ptr
->sig_bytes
= 8;
328 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
330 if (num_checked
< 4 &&
331 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
332 png_error(png_ptr
, "Not a PNG file");
334 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
337 png_ptr
->mode
|= PNG_HAVE_PNG_SIGNATURE
;
342 #ifdef PNG_USE_LOCAL_ARRAYS
347 #if defined(PNG_READ_bKGD_SUPPORTED)
350 #if defined(PNG_READ_cHRM_SUPPORTED)
353 #if defined(PNG_READ_gAMA_SUPPORTED)
356 #if defined(PNG_READ_hIST_SUPPORTED)
359 #if defined(PNG_READ_iCCP_SUPPORTED)
362 #if defined(PNG_READ_iTXt_SUPPORTED)
365 #if defined(PNG_READ_oFFs_SUPPORTED)
368 #if defined(PNG_READ_pCAL_SUPPORTED)
371 #if defined(PNG_READ_pHYs_SUPPORTED)
374 #if defined(PNG_READ_sBIT_SUPPORTED)
377 #if defined(PNG_READ_sCAL_SUPPORTED)
380 #if defined(PNG_READ_sPLT_SUPPORTED)
383 #if defined(PNG_READ_sRGB_SUPPORTED)
386 #if defined(PNG_READ_tEXt_SUPPORTED)
389 #if defined(PNG_READ_tIME_SUPPORTED)
392 #if defined(PNG_READ_tRNS_SUPPORTED)
395 #if defined(PNG_READ_zTXt_SUPPORTED)
398 #endif /* PNG_USE_LOCAL_ARRAYS */
399 png_byte chunk_length
[4];
402 png_read_data(png_ptr
, chunk_length
, 4);
403 length
= png_get_uint_31(png_ptr
,chunk_length
);
405 png_reset_crc(png_ptr
);
406 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
408 png_debug2(0, "Reading %s chunk, length=%lu.\n", png_ptr
->chunk_name
,
411 /* This should be a binary subdivision search or a hash for
412 * matching the chunk name rather than a linear search.
414 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
415 png_handle_IHDR(png_ptr
, info_ptr
, length
);
416 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
417 png_handle_IEND(png_ptr
, info_ptr
, length
);
418 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
419 else if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
))
421 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
422 png_ptr
->mode
|= PNG_HAVE_IDAT
;
423 png_handle_unknown(png_ptr
, info_ptr
, length
);
424 if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
425 png_ptr
->mode
|= PNG_HAVE_PLTE
;
426 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
428 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
429 png_error(png_ptr
, "Missing IHDR before IDAT");
430 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
431 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
432 png_error(png_ptr
, "Missing PLTE before IDAT");
437 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
438 png_handle_PLTE(png_ptr
, info_ptr
, length
);
439 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
441 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
442 png_error(png_ptr
, "Missing IHDR before IDAT");
443 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
444 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
445 png_error(png_ptr
, "Missing PLTE before IDAT");
447 png_ptr
->idat_size
= length
;
448 png_ptr
->mode
|= PNG_HAVE_IDAT
;
451 #if defined(PNG_READ_bKGD_SUPPORTED)
452 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
453 png_handle_bKGD(png_ptr
, info_ptr
, length
);
455 #if defined(PNG_READ_cHRM_SUPPORTED)
456 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
457 png_handle_cHRM(png_ptr
, info_ptr
, length
);
459 #if defined(PNG_READ_gAMA_SUPPORTED)
460 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
461 png_handle_gAMA(png_ptr
, info_ptr
, length
);
463 #if defined(PNG_READ_hIST_SUPPORTED)
464 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
465 png_handle_hIST(png_ptr
, info_ptr
, length
);
467 #if defined(PNG_READ_oFFs_SUPPORTED)
468 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
469 png_handle_oFFs(png_ptr
, info_ptr
, length
);
471 #if defined(PNG_READ_pCAL_SUPPORTED)
472 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
473 png_handle_pCAL(png_ptr
, info_ptr
, length
);
475 #if defined(PNG_READ_sCAL_SUPPORTED)
476 else if (!png_memcmp(png_ptr
->chunk_name
, png_sCAL
, 4))
477 png_handle_sCAL(png_ptr
, info_ptr
, length
);
479 #if defined(PNG_READ_pHYs_SUPPORTED)
480 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
481 png_handle_pHYs(png_ptr
, info_ptr
, length
);
483 #if defined(PNG_READ_sBIT_SUPPORTED)
484 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
485 png_handle_sBIT(png_ptr
, info_ptr
, length
);
487 #if defined(PNG_READ_sRGB_SUPPORTED)
488 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
489 png_handle_sRGB(png_ptr
, info_ptr
, length
);
491 #if defined(PNG_READ_iCCP_SUPPORTED)
492 else if (!png_memcmp(png_ptr
->chunk_name
, png_iCCP
, 4))
493 png_handle_iCCP(png_ptr
, info_ptr
, length
);
495 #if defined(PNG_READ_sPLT_SUPPORTED)
496 else if (!png_memcmp(png_ptr
->chunk_name
, png_sPLT
, 4))
497 png_handle_sPLT(png_ptr
, info_ptr
, length
);
499 #if defined(PNG_READ_tEXt_SUPPORTED)
500 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
501 png_handle_tEXt(png_ptr
, info_ptr
, length
);
503 #if defined(PNG_READ_tIME_SUPPORTED)
504 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
505 png_handle_tIME(png_ptr
, info_ptr
, length
);
507 #if defined(PNG_READ_tRNS_SUPPORTED)
508 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
509 png_handle_tRNS(png_ptr
, info_ptr
, length
);
511 #if defined(PNG_READ_zTXt_SUPPORTED)
512 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
513 png_handle_zTXt(png_ptr
, info_ptr
, length
);
515 #if defined(PNG_READ_iTXt_SUPPORTED)
516 else if (!png_memcmp(png_ptr
->chunk_name
, png_iTXt
, 4))
517 png_handle_iTXt(png_ptr
, info_ptr
, length
);
520 png_handle_unknown(png_ptr
, info_ptr
, length
);
523 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
525 /* optional call to update the users info_ptr structure */
527 png_read_update_info(png_structp png_ptr
, png_infop info_ptr
)
529 png_debug(1, "in png_read_update_info\n");
530 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
531 png_read_start_row(png_ptr
);
534 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
535 png_read_transform_info(png_ptr
, info_ptr
);
538 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
539 /* Initialize palette, background, etc, after transformations
540 * are set, but before any reading takes place. This allows
541 * the user to obtain a gamma-corrected palette, for example.
542 * If the user doesn't call this, we will do it ourselves.
545 png_start_read_image(png_structp png_ptr
)
547 png_debug(1, "in png_start_read_image\n");
548 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
549 png_read_start_row(png_ptr
);
551 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
553 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
555 png_read_row(png_structp png_ptr
, png_bytep row
, png_bytep dsp_row
)
557 #ifdef PNG_USE_LOCAL_ARRAYS
559 const int png_pass_dsp_mask
[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
560 const int png_pass_mask
[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
563 png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
564 png_ptr
->row_number
, png_ptr
->pass
);
565 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
566 png_read_start_row(png_ptr
);
567 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
569 /* check for transforms that have been set but were defined out */
570 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
571 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
572 png_warning(png_ptr
, "PNG_READ_INVERT_SUPPORTED is not defined.");
574 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
575 if (png_ptr
->transformations
& PNG_FILLER
)
576 png_warning(png_ptr
, "PNG_READ_FILLER_SUPPORTED is not defined.");
578 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
579 if (png_ptr
->transformations
& PNG_PACKSWAP
)
580 png_warning(png_ptr
, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
582 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
583 if (png_ptr
->transformations
& PNG_PACK
)
584 png_warning(png_ptr
, "PNG_READ_PACK_SUPPORTED is not defined.");
586 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
587 if (png_ptr
->transformations
& PNG_SHIFT
)
588 png_warning(png_ptr
, "PNG_READ_SHIFT_SUPPORTED is not defined.");
590 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
591 if (png_ptr
->transformations
& PNG_BGR
)
592 png_warning(png_ptr
, "PNG_READ_BGR_SUPPORTED is not defined.");
594 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
595 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
596 png_warning(png_ptr
, "PNG_READ_SWAP_SUPPORTED is not defined.");
600 #if defined(PNG_READ_INTERLACING_SUPPORTED)
601 /* if interlaced and we do not need a new row, combine row and return */
602 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
604 switch (png_ptr
->pass
)
607 if (png_ptr
->row_number
& 0x07)
610 png_combine_row(png_ptr
, dsp_row
,
611 png_pass_dsp_mask
[png_ptr
->pass
]);
612 png_read_finish_row(png_ptr
);
617 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
620 png_combine_row(png_ptr
, dsp_row
,
621 png_pass_dsp_mask
[png_ptr
->pass
]);
622 png_read_finish_row(png_ptr
);
627 if ((png_ptr
->row_number
& 0x07) != 4)
629 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 4))
630 png_combine_row(png_ptr
, dsp_row
,
631 png_pass_dsp_mask
[png_ptr
->pass
]);
632 png_read_finish_row(png_ptr
);
637 if ((png_ptr
->row_number
& 3) || png_ptr
->width
< 3)
640 png_combine_row(png_ptr
, dsp_row
,
641 png_pass_dsp_mask
[png_ptr
->pass
]);
642 png_read_finish_row(png_ptr
);
647 if ((png_ptr
->row_number
& 3) != 2)
649 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 2))
650 png_combine_row(png_ptr
, dsp_row
,
651 png_pass_dsp_mask
[png_ptr
->pass
]);
652 png_read_finish_row(png_ptr
);
657 if ((png_ptr
->row_number
& 1) || png_ptr
->width
< 2)
660 png_combine_row(png_ptr
, dsp_row
,
661 png_pass_dsp_mask
[png_ptr
->pass
]);
662 png_read_finish_row(png_ptr
);
667 if (!(png_ptr
->row_number
& 1))
669 png_read_finish_row(png_ptr
);
677 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
))
678 png_error(png_ptr
, "Invalid attempt to read row data");
680 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
681 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->irowbytes
;
684 if (!(png_ptr
->zstream
.avail_in
))
686 while (!png_ptr
->idat_size
)
688 png_byte chunk_length
[4];
690 png_crc_finish(png_ptr
, 0);
692 png_read_data(png_ptr
, chunk_length
, 4);
693 png_ptr
->idat_size
= png_get_uint_31(png_ptr
,chunk_length
);
695 png_reset_crc(png_ptr
);
696 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
697 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
698 png_error(png_ptr
, "Not enough image data");
700 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->zbuf_size
;
701 png_ptr
->zstream
.next_in
= png_ptr
->zbuf
;
702 if (png_ptr
->zbuf_size
> png_ptr
->idat_size
)
703 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->idat_size
;
704 png_crc_read(png_ptr
, png_ptr
->zbuf
,
705 (png_size_t
)png_ptr
->zstream
.avail_in
);
706 png_ptr
->idat_size
-= png_ptr
->zstream
.avail_in
;
708 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
709 if (ret
== Z_STREAM_END
)
711 if (png_ptr
->zstream
.avail_out
|| png_ptr
->zstream
.avail_in
||
713 png_error(png_ptr
, "Extra compressed data");
714 png_ptr
->mode
|= PNG_AFTER_IDAT
;
715 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
719 png_error(png_ptr
, png_ptr
->zstream
.msg
? png_ptr
->zstream
.msg
:
720 "Decompression error");
722 } while (png_ptr
->zstream
.avail_out
);
724 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
725 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
726 png_ptr
->row_info
.channels
= png_ptr
->channels
;
727 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
728 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
729 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
730 png_ptr
->row_info
.width
);
732 if(png_ptr
->row_buf
[0])
733 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
734 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
735 (int)(png_ptr
->row_buf
[0]));
737 png_memcpy_check(png_ptr
, png_ptr
->prev_row
, png_ptr
->row_buf
,
738 png_ptr
->rowbytes
+ 1);
740 #if defined(PNG_MNG_FEATURES_SUPPORTED)
741 if((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
742 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
744 /* Intrapixel differencing */
745 png_do_read_intrapixel(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
750 if (png_ptr
->transformations
|| (png_ptr
->flags
&PNG_FLAG_STRIP_ALPHA
))
751 png_do_read_transformations(png_ptr
);
753 #if defined(PNG_READ_INTERLACING_SUPPORTED)
754 /* blow up interlaced rows to full size */
755 if (png_ptr
->interlaced
&&
756 (png_ptr
->transformations
& PNG_INTERLACE
))
758 if (png_ptr
->pass
< 6)
759 /* old interface (pre-1.0.9):
760 png_do_read_interlace(&(png_ptr->row_info),
761 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
763 png_do_read_interlace(png_ptr
);
766 png_combine_row(png_ptr
, dsp_row
,
767 png_pass_dsp_mask
[png_ptr
->pass
]);
769 png_combine_row(png_ptr
, row
,
770 png_pass_mask
[png_ptr
->pass
]);
776 png_combine_row(png_ptr
, row
, 0xff);
778 png_combine_row(png_ptr
, dsp_row
, 0xff);
780 png_read_finish_row(png_ptr
);
782 if (png_ptr
->read_row_fn
!= NULL
)
783 (*(png_ptr
->read_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
785 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
787 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
788 /* Read one or more rows of image data. If the image is interlaced,
789 * and png_set_interlace_handling() has been called, the rows need to
790 * contain the contents of the rows from the previous pass. If the
791 * image has alpha or transparency, and png_handle_alpha()[*] has been
792 * called, the rows contents must be initialized to the contents of the
795 * "row" holds the actual image, and pixels are placed in it
796 * as they arrive. If the image is displayed after each pass, it will
797 * appear to "sparkle" in. "display_row" can be used to display a
798 * "chunky" progressive image, with finer detail added as it becomes
799 * available. If you do not want this "chunky" display, you may pass
800 * NULL for display_row. If you do not want the sparkle display, and
801 * you have not called png_handle_alpha(), you may pass NULL for rows.
802 * If you have called png_handle_alpha(), and the image has either an
803 * alpha channel or a transparency chunk, you must provide a buffer for
804 * rows. In this case, you do not have to provide a display_row buffer
805 * also, but you may. If the image is not interlaced, or if you have
806 * not called png_set_interlace_handling(), the display_row buffer will
807 * be ignored, so pass NULL to it.
809 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.2.8
813 png_read_rows(png_structp png_ptr
, png_bytepp row
,
814 png_bytepp display_row
, png_uint_32 num_rows
)
820 png_debug(1, "in png_read_rows\n");
823 if (rp
!= NULL
&& dp
!= NULL
)
824 for (i
= 0; i
< num_rows
; i
++)
826 png_bytep rptr
= *rp
++;
827 png_bytep dptr
= *dp
++;
829 png_read_row(png_ptr
, rptr
, dptr
);
832 for (i
= 0; i
< num_rows
; i
++)
834 png_bytep rptr
= *rp
;
835 png_read_row(png_ptr
, rptr
, png_bytep_NULL
);
839 for (i
= 0; i
< num_rows
; i
++)
841 png_bytep dptr
= *dp
;
842 png_read_row(png_ptr
, png_bytep_NULL
, dptr
);
846 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
848 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
849 /* Read the entire image. If the image has an alpha channel or a tRNS
850 * chunk, and you have called png_handle_alpha()[*], you will need to
851 * initialize the image to the current image that PNG will be overlaying.
852 * We set the num_rows again here, in case it was incorrectly set in
853 * png_read_start_row() by a call to png_read_update_info() or
854 * png_start_read_image() if png_set_interlace_handling() wasn't called
855 * prior to either of these functions like it should have been. You can
856 * only call this function once. If you desire to have an image for
857 * each pass of a interlaced image, use png_read_rows() instead.
859 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.2.8
862 png_read_image(png_structp png_ptr
, png_bytepp image
)
864 png_uint_32 i
,image_height
;
868 png_debug(1, "in png_read_image\n");
870 #ifdef PNG_READ_INTERLACING_SUPPORTED
871 pass
= png_set_interlace_handling(png_ptr
);
873 if (png_ptr
->interlaced
)
875 "Cannot read interlaced image -- interlace handler disabled.");
880 image_height
=png_ptr
->height
;
881 png_ptr
->num_rows
= image_height
; /* Make sure this is set correctly */
883 for (j
= 0; j
< pass
; j
++)
886 for (i
= 0; i
< image_height
; i
++)
888 png_read_row(png_ptr
, *rp
, png_bytep_NULL
);
893 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
895 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
896 /* Read the end of the PNG file. Will not read past the end of the
897 * file, will verify the end is accurate, and will read any comments
898 * or time information at the end of the file, if info is not NULL.
901 png_read_end(png_structp png_ptr
, png_infop info_ptr
)
903 png_byte chunk_length
[4];
906 png_debug(1, "in png_read_end\n");
907 png_crc_finish(png_ptr
, 0); /* Finish off CRC from last IDAT chunk */
911 #ifdef PNG_USE_LOCAL_ARRAYS
916 #if defined(PNG_READ_bKGD_SUPPORTED)
919 #if defined(PNG_READ_cHRM_SUPPORTED)
922 #if defined(PNG_READ_gAMA_SUPPORTED)
925 #if defined(PNG_READ_hIST_SUPPORTED)
928 #if defined(PNG_READ_iCCP_SUPPORTED)
931 #if defined(PNG_READ_iTXt_SUPPORTED)
934 #if defined(PNG_READ_oFFs_SUPPORTED)
937 #if defined(PNG_READ_pCAL_SUPPORTED)
940 #if defined(PNG_READ_pHYs_SUPPORTED)
943 #if defined(PNG_READ_sBIT_SUPPORTED)
946 #if defined(PNG_READ_sCAL_SUPPORTED)
949 #if defined(PNG_READ_sPLT_SUPPORTED)
952 #if defined(PNG_READ_sRGB_SUPPORTED)
955 #if defined(PNG_READ_tEXt_SUPPORTED)
958 #if defined(PNG_READ_tIME_SUPPORTED)
961 #if defined(PNG_READ_tRNS_SUPPORTED)
964 #if defined(PNG_READ_zTXt_SUPPORTED)
967 #endif /* PNG_USE_LOCAL_ARRAYS */
969 png_read_data(png_ptr
, chunk_length
, 4);
970 length
= png_get_uint_31(png_ptr
,chunk_length
);
972 png_reset_crc(png_ptr
);
973 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
975 png_debug1(0, "Reading %s chunk.\n", png_ptr
->chunk_name
);
977 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
978 png_handle_IHDR(png_ptr
, info_ptr
, length
);
979 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
980 png_handle_IEND(png_ptr
, info_ptr
, length
);
981 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
982 else if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
))
984 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
986 if (length
> 0 || png_ptr
->mode
& PNG_AFTER_IDAT
)
987 png_error(png_ptr
, "Too many IDAT's found");
990 png_ptr
->mode
|= PNG_AFTER_IDAT
;
991 png_handle_unknown(png_ptr
, info_ptr
, length
);
992 if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
993 png_ptr
->mode
|= PNG_HAVE_PLTE
;
996 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
998 /* Zero length IDATs are legal after the last IDAT has been
999 * read, but not after other chunks have been read.
1001 if (length
> 0 || png_ptr
->mode
& PNG_AFTER_IDAT
)
1002 png_error(png_ptr
, "Too many IDAT's found");
1003 png_crc_finish(png_ptr
, length
);
1005 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
1006 png_handle_PLTE(png_ptr
, info_ptr
, length
);
1007 #if defined(PNG_READ_bKGD_SUPPORTED)
1008 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
1009 png_handle_bKGD(png_ptr
, info_ptr
, length
);
1011 #if defined(PNG_READ_cHRM_SUPPORTED)
1012 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
1013 png_handle_cHRM(png_ptr
, info_ptr
, length
);
1015 #if defined(PNG_READ_gAMA_SUPPORTED)
1016 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
1017 png_handle_gAMA(png_ptr
, info_ptr
, length
);
1019 #if defined(PNG_READ_hIST_SUPPORTED)
1020 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
1021 png_handle_hIST(png_ptr
, info_ptr
, length
);
1023 #if defined(PNG_READ_oFFs_SUPPORTED)
1024 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
1025 png_handle_oFFs(png_ptr
, info_ptr
, length
);
1027 #if defined(PNG_READ_pCAL_SUPPORTED)
1028 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
1029 png_handle_pCAL(png_ptr
, info_ptr
, length
);
1031 #if defined(PNG_READ_sCAL_SUPPORTED)
1032 else if (!png_memcmp(png_ptr
->chunk_name
, png_sCAL
, 4))
1033 png_handle_sCAL(png_ptr
, info_ptr
, length
);
1035 #if defined(PNG_READ_pHYs_SUPPORTED)
1036 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
1037 png_handle_pHYs(png_ptr
, info_ptr
, length
);
1039 #if defined(PNG_READ_sBIT_SUPPORTED)
1040 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
1041 png_handle_sBIT(png_ptr
, info_ptr
, length
);
1043 #if defined(PNG_READ_sRGB_SUPPORTED)
1044 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
1045 png_handle_sRGB(png_ptr
, info_ptr
, length
);
1047 #if defined(PNG_READ_iCCP_SUPPORTED)
1048 else if (!png_memcmp(png_ptr
->chunk_name
, png_iCCP
, 4))
1049 png_handle_iCCP(png_ptr
, info_ptr
, length
);
1051 #if defined(PNG_READ_sPLT_SUPPORTED)
1052 else if (!png_memcmp(png_ptr
->chunk_name
, png_sPLT
, 4))
1053 png_handle_sPLT(png_ptr
, info_ptr
, length
);
1055 #if defined(PNG_READ_tEXt_SUPPORTED)
1056 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
1057 png_handle_tEXt(png_ptr
, info_ptr
, length
);
1059 #if defined(PNG_READ_tIME_SUPPORTED)
1060 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
1061 png_handle_tIME(png_ptr
, info_ptr
, length
);
1063 #if defined(PNG_READ_tRNS_SUPPORTED)
1064 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
1065 png_handle_tRNS(png_ptr
, info_ptr
, length
);
1067 #if defined(PNG_READ_zTXt_SUPPORTED)
1068 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
1069 png_handle_zTXt(png_ptr
, info_ptr
, length
);
1071 #if defined(PNG_READ_iTXt_SUPPORTED)
1072 else if (!png_memcmp(png_ptr
->chunk_name
, png_iTXt
, 4))
1073 png_handle_iTXt(png_ptr
, info_ptr
, length
);
1076 png_handle_unknown(png_ptr
, info_ptr
, length
);
1077 } while (!(png_ptr
->mode
& PNG_HAVE_IEND
));
1079 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
1081 /* free all memory used by the read */
1083 png_destroy_read_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
,
1084 png_infopp end_info_ptr_ptr
)
1086 png_structp png_ptr
= NULL
;
1087 png_infop info_ptr
= NULL
, end_info_ptr
= NULL
;
1088 #ifdef PNG_USER_MEM_SUPPORTED
1089 png_free_ptr free_fn
;
1093 png_debug(1, "in png_destroy_read_struct\n");
1094 if (png_ptr_ptr
!= NULL
)
1095 png_ptr
= *png_ptr_ptr
;
1097 if (info_ptr_ptr
!= NULL
)
1098 info_ptr
= *info_ptr_ptr
;
1100 if (end_info_ptr_ptr
!= NULL
)
1101 end_info_ptr
= *end_info_ptr_ptr
;
1103 #ifdef PNG_USER_MEM_SUPPORTED
1104 free_fn
= png_ptr
->free_fn
;
1105 mem_ptr
= png_ptr
->mem_ptr
;
1108 png_read_destroy(png_ptr
, info_ptr
, end_info_ptr
);
1110 if (info_ptr
!= NULL
)
1112 #if defined(PNG_TEXT_SUPPORTED)
1113 png_free_data(png_ptr
, info_ptr
, PNG_FREE_TEXT
, -1);
1116 #ifdef PNG_USER_MEM_SUPPORTED
1117 png_destroy_struct_2((png_voidp
)info_ptr
, (png_free_ptr
)free_fn
,
1118 (png_voidp
)mem_ptr
);
1120 png_destroy_struct((png_voidp
)info_ptr
);
1122 *info_ptr_ptr
= NULL
;
1125 if (end_info_ptr
!= NULL
)
1127 #if defined(PNG_READ_TEXT_SUPPORTED)
1128 png_free_data(png_ptr
, end_info_ptr
, PNG_FREE_TEXT
, -1);
1130 #ifdef PNG_USER_MEM_SUPPORTED
1131 png_destroy_struct_2((png_voidp
)end_info_ptr
, (png_free_ptr
)free_fn
,
1132 (png_voidp
)mem_ptr
);
1134 png_destroy_struct((png_voidp
)end_info_ptr
);
1136 *end_info_ptr_ptr
= NULL
;
1139 if (png_ptr
!= NULL
)
1141 #ifdef PNG_USER_MEM_SUPPORTED
1142 png_destroy_struct_2((png_voidp
)png_ptr
, (png_free_ptr
)free_fn
,
1143 (png_voidp
)mem_ptr
);
1145 png_destroy_struct((png_voidp
)png_ptr
);
1147 *png_ptr_ptr
= NULL
;
1151 /* free all memory used by the read (old method) */
1153 png_read_destroy(png_structp png_ptr
, png_infop info_ptr
, png_infop end_info_ptr
)
1155 #ifdef PNG_SETJMP_SUPPORTED
1158 png_error_ptr error_fn
;
1159 png_error_ptr warning_fn
;
1160 png_voidp error_ptr
;
1161 #ifdef PNG_USER_MEM_SUPPORTED
1162 png_free_ptr free_fn
;
1165 png_debug(1, "in png_read_destroy\n");
1166 if (info_ptr
!= NULL
)
1167 png_info_destroy(png_ptr
, info_ptr
);
1169 if (end_info_ptr
!= NULL
)
1170 png_info_destroy(png_ptr
, end_info_ptr
);
1172 png_free(png_ptr
, png_ptr
->zbuf
);
1173 png_free(png_ptr
, png_ptr
->big_row_buf
);
1174 png_free(png_ptr
, png_ptr
->prev_row
);
1175 #if defined(PNG_READ_DITHER_SUPPORTED)
1176 png_free(png_ptr
, png_ptr
->palette_lookup
);
1177 png_free(png_ptr
, png_ptr
->dither_index
);
1179 #if defined(PNG_READ_GAMMA_SUPPORTED)
1180 png_free(png_ptr
, png_ptr
->gamma_table
);
1182 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1183 png_free(png_ptr
, png_ptr
->gamma_from_1
);
1184 png_free(png_ptr
, png_ptr
->gamma_to_1
);
1186 #ifdef PNG_FREE_ME_SUPPORTED
1187 if (png_ptr
->free_me
& PNG_FREE_PLTE
)
1188 png_zfree(png_ptr
, png_ptr
->palette
);
1189 png_ptr
->free_me
&= ~PNG_FREE_PLTE
;
1191 if (png_ptr
->flags
& PNG_FLAG_FREE_PLTE
)
1192 png_zfree(png_ptr
, png_ptr
->palette
);
1193 png_ptr
->flags
&= ~PNG_FLAG_FREE_PLTE
;
1195 #if defined(PNG_tRNS_SUPPORTED) || \
1196 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1197 #ifdef PNG_FREE_ME_SUPPORTED
1198 if (png_ptr
->free_me
& PNG_FREE_TRNS
)
1199 png_free(png_ptr
, png_ptr
->trans
);
1200 png_ptr
->free_me
&= ~PNG_FREE_TRNS
;
1202 if (png_ptr
->flags
& PNG_FLAG_FREE_TRNS
)
1203 png_free(png_ptr
, png_ptr
->trans
);
1204 png_ptr
->flags
&= ~PNG_FLAG_FREE_TRNS
;
1207 #if defined(PNG_READ_hIST_SUPPORTED)
1208 #ifdef PNG_FREE_ME_SUPPORTED
1209 if (png_ptr
->free_me
& PNG_FREE_HIST
)
1210 png_free(png_ptr
, png_ptr
->hist
);
1211 png_ptr
->free_me
&= ~PNG_FREE_HIST
;
1213 if (png_ptr
->flags
& PNG_FLAG_FREE_HIST
)
1214 png_free(png_ptr
, png_ptr
->hist
);
1215 png_ptr
->flags
&= ~PNG_FLAG_FREE_HIST
;
1218 #if defined(PNG_READ_GAMMA_SUPPORTED)
1219 if (png_ptr
->gamma_16_table
!= NULL
)
1222 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1223 for (i
= 0; i
< istop
; i
++)
1225 png_free(png_ptr
, png_ptr
->gamma_16_table
[i
]);
1227 png_free(png_ptr
, png_ptr
->gamma_16_table
);
1229 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1230 if (png_ptr
->gamma_16_from_1
!= NULL
)
1233 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1234 for (i
= 0; i
< istop
; i
++)
1236 png_free(png_ptr
, png_ptr
->gamma_16_from_1
[i
]);
1238 png_free(png_ptr
, png_ptr
->gamma_16_from_1
);
1240 if (png_ptr
->gamma_16_to_1
!= NULL
)
1243 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1244 for (i
= 0; i
< istop
; i
++)
1246 png_free(png_ptr
, png_ptr
->gamma_16_to_1
[i
]);
1248 png_free(png_ptr
, png_ptr
->gamma_16_to_1
);
1252 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1253 png_free(png_ptr
, png_ptr
->time_buffer
);
1256 inflateEnd(&png_ptr
->zstream
);
1257 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1258 png_free(png_ptr
, png_ptr
->save_buffer
);
1261 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1262 #ifdef PNG_TEXT_SUPPORTED
1263 png_free(png_ptr
, png_ptr
->current_text
);
1264 #endif /* PNG_TEXT_SUPPORTED */
1265 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1267 /* Save the important info out of the png_struct, in case it is
1270 #ifdef PNG_SETJMP_SUPPORTED
1271 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, png_sizeof (jmp_buf));
1274 error_fn
= png_ptr
->error_fn
;
1275 warning_fn
= png_ptr
->warning_fn
;
1276 error_ptr
= png_ptr
->error_ptr
;
1277 #ifdef PNG_USER_MEM_SUPPORTED
1278 free_fn
= png_ptr
->free_fn
;
1281 png_memset(png_ptr
, 0, png_sizeof (png_struct
));
1283 png_ptr
->error_fn
= error_fn
;
1284 png_ptr
->warning_fn
= warning_fn
;
1285 png_ptr
->error_ptr
= error_ptr
;
1286 #ifdef PNG_USER_MEM_SUPPORTED
1287 png_ptr
->free_fn
= free_fn
;
1290 #ifdef PNG_SETJMP_SUPPORTED
1291 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, png_sizeof (jmp_buf));
1297 png_set_read_status_fn(png_structp png_ptr
, png_read_status_ptr read_row_fn
)
1299 png_ptr
->read_row_fn
= read_row_fn
;
1303 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
1304 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1306 png_read_png(png_structp png_ptr
, png_infop info_ptr
,
1312 #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1313 /* invert the alpha channel from opacity to transparency
1315 if (transforms
& PNG_TRANSFORM_INVERT_ALPHA
)
1316 png_set_invert_alpha(png_ptr
);
1319 /* png_read_info() gives us all of the information from the
1320 * PNG file before the first IDAT (image data chunk).
1322 png_read_info(png_ptr
, info_ptr
);
1323 if (info_ptr
->height
> PNG_UINT_32_MAX
/png_sizeof(png_bytep
))
1324 png_error(png_ptr
,"Image is too high to process with png_read_png()");
1326 /* -------------- image transformations start here ------------------- */
1328 #if defined(PNG_READ_16_TO_8_SUPPORTED)
1329 /* tell libpng to strip 16 bit/color files down to 8 bits per color
1331 if (transforms
& PNG_TRANSFORM_STRIP_16
)
1332 png_set_strip_16(png_ptr
);
1335 #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1336 /* Strip alpha bytes from the input data without combining with
1337 * the background (not recommended).
1339 if (transforms
& PNG_TRANSFORM_STRIP_ALPHA
)
1340 png_set_strip_alpha(png_ptr
);
1343 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1344 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1345 * byte into separate bytes (useful for paletted and grayscale images).
1347 if (transforms
& PNG_TRANSFORM_PACKING
)
1348 png_set_packing(png_ptr
);
1351 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
1352 /* Change the order of packed pixels to least significant bit first
1353 * (not useful if you are using png_set_packing).
1355 if (transforms
& PNG_TRANSFORM_PACKSWAP
)
1356 png_set_packswap(png_ptr
);
1359 #if defined(PNG_READ_EXPAND_SUPPORTED)
1360 /* Expand paletted colors into true RGB triplets
1361 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1362 * Expand paletted or RGB images with transparency to full alpha
1363 * channels so the data will be available as RGBA quartets.
1365 if (transforms
& PNG_TRANSFORM_EXPAND
)
1366 if ((png_ptr
->bit_depth
< 8) ||
1367 (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
) ||
1368 (png_get_valid(png_ptr
, info_ptr
, PNG_INFO_tRNS
)))
1369 png_set_expand(png_ptr
);
1372 /* We don't handle background color or gamma transformation or dithering.
1375 #if defined(PNG_READ_INVERT_SUPPORTED)
1376 /* invert monochrome files to have 0 as white and 1 as black
1378 if (transforms
& PNG_TRANSFORM_INVERT_MONO
)
1379 png_set_invert_mono(png_ptr
);
1382 #if defined(PNG_READ_SHIFT_SUPPORTED)
1383 /* If you want to shift the pixel values from the range [0,255] or
1384 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1385 * colors were originally in:
1387 if ((transforms
& PNG_TRANSFORM_SHIFT
)
1388 && png_get_valid(png_ptr
, info_ptr
, PNG_INFO_sBIT
))
1390 png_color_8p sig_bit
;
1392 png_get_sBIT(png_ptr
, info_ptr
, &sig_bit
);
1393 png_set_shift(png_ptr
, sig_bit
);
1397 #if defined(PNG_READ_BGR_SUPPORTED)
1398 /* flip the RGB pixels to BGR (or RGBA to BGRA)
1400 if (transforms
& PNG_TRANSFORM_BGR
)
1401 png_set_bgr(png_ptr
);
1404 #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1405 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1407 if (transforms
& PNG_TRANSFORM_SWAP_ALPHA
)
1408 png_set_swap_alpha(png_ptr
);
1411 #if defined(PNG_READ_SWAP_SUPPORTED)
1412 /* swap bytes of 16 bit files to least significant byte first
1414 if (transforms
& PNG_TRANSFORM_SWAP_ENDIAN
)
1415 png_set_swap(png_ptr
);
1418 /* We don't handle adding filler bytes */
1420 /* Optional call to gamma correct and add the background to the palette
1421 * and update info structure. REQUIRED if you are expecting libpng to
1422 * update the palette for you (i.e., you selected such a transform above).
1424 png_read_update_info(png_ptr
, info_ptr
);
1426 /* -------------- image transformations end here ------------------- */
1428 #ifdef PNG_FREE_ME_SUPPORTED
1429 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ROWS
, 0);
1431 if(info_ptr
->row_pointers
== NULL
)
1433 info_ptr
->row_pointers
= (png_bytepp
)png_malloc(png_ptr
,
1434 info_ptr
->height
* png_sizeof(png_bytep
));
1435 #ifdef PNG_FREE_ME_SUPPORTED
1436 info_ptr
->free_me
|= PNG_FREE_ROWS
;
1438 for (row
= 0; row
< (int)info_ptr
->height
; row
++)
1440 info_ptr
->row_pointers
[row
] = (png_bytep
)png_malloc(png_ptr
,
1441 png_get_rowbytes(png_ptr
, info_ptr
));
1445 png_read_image(png_ptr
, info_ptr
->row_pointers
);
1446 info_ptr
->valid
|= PNG_INFO_IDAT
;
1448 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
1449 png_read_end(png_ptr
, info_ptr
);
1451 if(transforms
== 0 || params
== NULL
)
1452 /* quiet compiler warnings */ return;
1456 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */