2 /* pngread.c - read a PNG file
4 * Last changed in libpng 1.2.51 [February 6, 2014]
5 * Copyright (c) 1998-2014 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
13 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
18 #define PNG_NO_PEDANTIC_WARNINGS
20 #ifdef PNG_READ_SUPPORTED
22 /* Create a PNG structure for reading, and allocate any memory needed. */
24 png_create_read_struct(png_const_charp user_png_ver
, png_voidp error_ptr
,
25 png_error_ptr error_fn
, png_error_ptr warn_fn
)
28 #ifdef PNG_USER_MEM_SUPPORTED
29 return (png_create_read_struct_2(user_png_ver
, error_ptr
, error_fn
,
30 warn_fn
, png_voidp_NULL
, png_malloc_ptr_NULL
, png_free_ptr_NULL
));
33 /* Alternate create PNG structure for reading, and allocate any memory
37 png_create_read_struct_2(png_const_charp user_png_ver
, png_voidp error_ptr
,
38 png_error_ptr error_fn
, png_error_ptr warn_fn
, png_voidp mem_ptr
,
39 png_malloc_ptr malloc_fn
, png_free_ptr free_fn
)
41 #endif /* PNG_USER_MEM_SUPPORTED */
43 #ifdef PNG_SETJMP_SUPPORTED
48 #ifdef PNG_SETJMP_SUPPORTED
49 #ifdef USE_FAR_KEYWORD
56 png_debug(1, "in png_create_read_struct");
58 #ifdef PNG_USER_MEM_SUPPORTED
59 png_ptr
= (png_structp
)png_create_struct_2(PNG_STRUCT_PNG
,
60 (png_malloc_ptr
)malloc_fn
, (png_voidp
)mem_ptr
);
62 png_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
67 /* Added at libpng-1.2.6 */
68 #ifdef PNG_USER_LIMITS_SUPPORTED
69 png_ptr
->user_width_max
= PNG_USER_WIDTH_MAX
;
70 png_ptr
->user_height_max
= PNG_USER_HEIGHT_MAX
;
71 /* Added at libpng-1.2.43 and 1.4.0 */
72 png_ptr
->user_chunk_cache_max
= PNG_USER_CHUNK_CACHE_MAX
;
75 #ifdef PNG_SETJMP_SUPPORTED
76 #ifdef USE_FAR_KEYWORD
79 if (setjmp(png_ptr
->jmpbuf
))
82 png_free(png_ptr
, png_ptr
->zbuf
);
84 #ifdef PNG_USER_MEM_SUPPORTED
85 png_destroy_struct_2((png_voidp
)png_ptr
,
86 (png_free_ptr
)free_fn
, (png_voidp
)mem_ptr
);
88 png_destroy_struct((png_voidp
)png_ptr
);
92 #ifdef USE_FAR_KEYWORD
93 png_memcpy(png_ptr
->jmpbuf
, jmpbuf
, png_sizeof(jmp_buf));
95 #endif /* PNG_SETJMP_SUPPORTED */
97 #ifdef PNG_USER_MEM_SUPPORTED
98 png_set_mem_fn(png_ptr
, mem_ptr
, malloc_fn
, free_fn
);
101 png_set_error_fn(png_ptr
, error_ptr
, error_fn
, warn_fn
);
108 if (user_png_ver
[i
] != png_libpng_ver
[i
])
109 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
110 } while (png_libpng_ver
[i
++]);
113 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
116 if (png_ptr
->flags
& PNG_FLAG_LIBRARY_MISMATCH
)
118 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
119 * we must recompile any applications that use any older library version.
120 * For versions after libpng 1.0, we will be compatible, so we need
121 * only check the first digit.
123 if (user_png_ver
== NULL
|| user_png_ver
[0] != png_libpng_ver
[0] ||
124 (user_png_ver
[0] == '1' && user_png_ver
[2] != png_libpng_ver
[2]) ||
125 (user_png_ver
[0] == '0' && user_png_ver
[2] < '9'))
127 #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
131 png_snprintf(msg
, 80,
132 "Application was compiled with png.h from libpng-%.20s",
134 png_warning(png_ptr
, msg
);
136 png_snprintf(msg
, 80,
137 "Application is running with png.c from libpng-%.20s",
139 png_warning(png_ptr
, msg
);
141 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
145 "Incompatible libpng version in application and library");
149 /* Initialize zbuf - compression buffer */
150 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
151 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
152 (png_uint_32
)png_ptr
->zbuf_size
);
153 png_ptr
->zstream
.zalloc
= png_zalloc
;
154 png_ptr
->zstream
.zfree
= png_zfree
;
155 png_ptr
->zstream
.opaque
= (voidpf
)png_ptr
;
157 switch (inflateInit(&png_ptr
->zstream
))
159 case Z_OK
: /* Do nothing */ break;
161 case Z_STREAM_ERROR
: png_error(png_ptr
, "zlib memory error");
163 case Z_VERSION_ERROR
: png_error(png_ptr
, "zlib version error");
165 default: png_error(png_ptr
, "Unknown zlib error");
169 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
170 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
172 png_set_read_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
);
174 #ifdef PNG_SETJMP_SUPPORTED
175 /* Applications that neglect to set up their own setjmp() and then
176 encounter a png_error() will longjmp here. Since the jmpbuf is
177 then meaningless we abort instead of returning. */
178 #ifdef USE_FAR_KEYWORD
181 png_memcpy(png_ptr
->jmpbuf
, jmpbuf
, png_sizeof(jmp_buf));
183 if (setjmp(png_ptr
->jmpbuf
))
186 #endif /* PNG_SETJMP_SUPPORTED */
191 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
192 /* Initialize PNG structure for reading, and allocate any memory needed.
193 * This interface is deprecated in favour of the png_create_read_struct(),
194 * and it will disappear as of libpng-1.3.0.
198 png_read_init(png_structp png_ptr
)
200 /* We only come here via pre-1.0.7-compiled applications */
201 png_read_init_2(png_ptr
, "1.0.6 or earlier", 0, 0);
205 png_read_init_2(png_structp png_ptr
, png_const_charp user_png_ver
,
206 png_size_t png_struct_size
, png_size_t png_info_size
)
208 /* We only come here via pre-1.0.12-compiled applications */
211 #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
212 if (png_sizeof(png_struct
) > png_struct_size
||
213 png_sizeof(png_info
) > png_info_size
)
216 png_ptr
->warning_fn
= NULL
;
219 png_snprintf(msg
, 80,
220 "Application was compiled with png.h from libpng-%.20s",
222 png_warning(png_ptr
, msg
);
224 png_snprintf(msg
, 80,
225 "Application is running with png.c from libpng-%.20s",
227 png_warning(png_ptr
, msg
);
230 if (png_sizeof(png_struct
) > png_struct_size
)
232 png_ptr
->error_fn
= NULL
;
233 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
237 "The png struct allocated by the application for reading is"
240 if (png_sizeof(png_info
) > png_info_size
)
242 png_ptr
->error_fn
= NULL
;
243 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
247 "The info struct allocated by application for reading is"
250 png_read_init_3(&png_ptr
, user_png_ver
, png_struct_size
);
252 #endif /* PNG_1_0_X || PNG_1_2_X */
255 png_read_init_3(png_structpp ptr_ptr
, png_const_charp user_png_ver
,
256 png_size_t png_struct_size
)
258 #ifdef PNG_SETJMP_SUPPORTED
259 jmp_buf tmp_jmp
; /* to save current jump buffer */
264 png_structp png_ptr
=*ptr_ptr
;
271 if (user_png_ver
[i
] != png_libpng_ver
[i
])
273 #ifdef PNG_LEGACY_SUPPORTED
274 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
276 png_ptr
->warning_fn
= NULL
;
278 "Application uses deprecated png_read_init() and should be"
283 } while (png_libpng_ver
[i
++]);
285 png_debug(1, "in png_read_init_3");
287 #ifdef PNG_SETJMP_SUPPORTED
288 /* Save jump buffer and error functions */
289 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, png_sizeof(jmp_buf));
292 if (png_sizeof(png_struct
) > png_struct_size
)
294 png_destroy_struct(png_ptr
);
295 *ptr_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
299 /* Reset all variables to 0 */
300 png_memset(png_ptr
, 0, png_sizeof(png_struct
));
302 #ifdef PNG_SETJMP_SUPPORTED
303 /* Restore jump buffer */
304 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, png_sizeof(jmp_buf));
307 /* Added at libpng-1.2.6 */
308 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
309 png_ptr
->user_width_max
= PNG_USER_WIDTH_MAX
;
310 png_ptr
->user_height_max
= PNG_USER_HEIGHT_MAX
;
313 /* Initialize zbuf - compression buffer */
314 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
315 png_ptr
->zstream
.zalloc
= png_zalloc
;
316 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
317 (png_uint_32
)png_ptr
->zbuf_size
);
318 png_ptr
->zstream
.zalloc
= png_zalloc
;
319 png_ptr
->zstream
.zfree
= png_zfree
;
320 png_ptr
->zstream
.opaque
= (voidpf
)png_ptr
;
322 switch (inflateInit(&png_ptr
->zstream
))
324 case Z_OK
: /* Do nothing */ break;
325 case Z_STREAM_ERROR
: png_error(png_ptr
, "zlib memory error"); break;
326 case Z_VERSION_ERROR
: png_error(png_ptr
, "zlib version error");
328 default: png_error(png_ptr
, "Unknown zlib error");
331 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
332 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
334 png_set_read_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
);
337 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
338 /* Read the information before the actual image data. This has been
339 * changed in v0.90 to allow reading a file that already has the magic
340 * bytes read from the stream. You can tell libpng how many bytes have
341 * been read from the beginning of the stream (up to the maximum of 8)
342 * via png_set_sig_bytes(), and we will only check the remaining bytes
343 * here. The application can then have access to the signature bytes we
344 * read if it is determined that this isn't a valid PNG file.
347 png_read_info(png_structp png_ptr
, png_infop info_ptr
)
349 png_debug(1, "in png_read_info");
351 if (png_ptr
== NULL
|| info_ptr
== NULL
)
354 /* If we haven't checked all of the PNG signature bytes, do so now. */
355 if (png_ptr
->sig_bytes
< 8)
357 png_size_t num_checked
= png_ptr
->sig_bytes
,
358 num_to_check
= 8 - num_checked
;
360 png_read_data(png_ptr
, &(info_ptr
->signature
[num_checked
]), num_to_check
);
361 png_ptr
->sig_bytes
= 8;
363 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
365 if (num_checked
< 4 &&
366 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
367 png_error(png_ptr
, "Not a PNG file");
369 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
372 png_ptr
->mode
|= PNG_HAVE_PNG_SIGNATURE
;
377 #ifdef PNG_USE_LOCAL_ARRAYS
382 #ifdef PNG_READ_bKGD_SUPPORTED
385 #ifdef PNG_READ_cHRM_SUPPORTED
388 #ifdef PNG_READ_gAMA_SUPPORTED
391 #ifdef PNG_READ_hIST_SUPPORTED
394 #ifdef PNG_READ_iCCP_SUPPORTED
397 #ifdef PNG_READ_iTXt_SUPPORTED
400 #ifdef PNG_READ_oFFs_SUPPORTED
403 #ifdef PNG_READ_pCAL_SUPPORTED
406 #ifdef PNG_READ_pHYs_SUPPORTED
409 #ifdef PNG_READ_sBIT_SUPPORTED
412 #ifdef PNG_READ_sCAL_SUPPORTED
415 #ifdef PNG_READ_sPLT_SUPPORTED
418 #ifdef PNG_READ_sRGB_SUPPORTED
421 #ifdef PNG_READ_tEXt_SUPPORTED
424 #ifdef PNG_READ_tIME_SUPPORTED
427 #ifdef PNG_READ_tRNS_SUPPORTED
430 #ifdef PNG_READ_zTXt_SUPPORTED
433 #endif /* PNG_USE_LOCAL_ARRAYS */
434 png_uint_32 length
= png_read_chunk_header(png_ptr
);
435 PNG_CONST png_bytep chunk_name
= png_ptr
->chunk_name
;
437 /* This should be a binary subdivision search or a hash for
438 * matching the chunk name rather than a linear search.
440 if (!png_memcmp(chunk_name
, png_IDAT
, 4))
441 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
442 png_ptr
->mode
|= PNG_HAVE_CHUNK_AFTER_IDAT
;
444 if (!png_memcmp(chunk_name
, png_IHDR
, 4))
445 png_handle_IHDR(png_ptr
, info_ptr
, length
);
446 else if (!png_memcmp(chunk_name
, png_IEND
, 4))
447 png_handle_IEND(png_ptr
, info_ptr
, length
);
448 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
449 else if (png_handle_as_unknown(png_ptr
, chunk_name
))
451 if (!png_memcmp(chunk_name
, png_IDAT
, 4))
452 png_ptr
->mode
|= PNG_HAVE_IDAT
;
453 png_handle_unknown(png_ptr
, info_ptr
, length
);
454 if (!png_memcmp(chunk_name
, png_PLTE
, 4))
455 png_ptr
->mode
|= PNG_HAVE_PLTE
;
456 else if (!png_memcmp(chunk_name
, png_IDAT
, 4))
458 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
459 png_error(png_ptr
, "Missing IHDR before IDAT");
460 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
461 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
462 png_error(png_ptr
, "Missing PLTE before IDAT");
467 else if (!png_memcmp(chunk_name
, png_PLTE
, 4))
468 png_handle_PLTE(png_ptr
, info_ptr
, length
);
469 else if (!png_memcmp(chunk_name
, png_IDAT
, 4))
471 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
472 png_error(png_ptr
, "Missing IHDR before IDAT");
473 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
474 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
475 png_error(png_ptr
, "Missing PLTE before IDAT");
477 png_ptr
->idat_size
= length
;
478 png_ptr
->mode
|= PNG_HAVE_IDAT
;
481 #ifdef PNG_READ_bKGD_SUPPORTED
482 else if (!png_memcmp(chunk_name
, png_bKGD
, 4))
483 png_handle_bKGD(png_ptr
, info_ptr
, length
);
485 #ifdef PNG_READ_cHRM_SUPPORTED
486 else if (!png_memcmp(chunk_name
, png_cHRM
, 4))
487 png_handle_cHRM(png_ptr
, info_ptr
, length
);
489 #ifdef PNG_READ_gAMA_SUPPORTED
490 else if (!png_memcmp(chunk_name
, png_gAMA
, 4))
491 png_handle_gAMA(png_ptr
, info_ptr
, length
);
493 #ifdef PNG_READ_hIST_SUPPORTED
494 else if (!png_memcmp(chunk_name
, png_hIST
, 4))
495 png_handle_hIST(png_ptr
, info_ptr
, length
);
497 #ifdef PNG_READ_oFFs_SUPPORTED
498 else if (!png_memcmp(chunk_name
, png_oFFs
, 4))
499 png_handle_oFFs(png_ptr
, info_ptr
, length
);
501 #ifdef PNG_READ_pCAL_SUPPORTED
502 else if (!png_memcmp(chunk_name
, png_pCAL
, 4))
503 png_handle_pCAL(png_ptr
, info_ptr
, length
);
505 #ifdef PNG_READ_sCAL_SUPPORTED
506 else if (!png_memcmp(chunk_name
, png_sCAL
, 4))
507 png_handle_sCAL(png_ptr
, info_ptr
, length
);
509 #ifdef PNG_READ_pHYs_SUPPORTED
510 else if (!png_memcmp(chunk_name
, png_pHYs
, 4))
511 png_handle_pHYs(png_ptr
, info_ptr
, length
);
513 #ifdef PNG_READ_sBIT_SUPPORTED
514 else if (!png_memcmp(chunk_name
, png_sBIT
, 4))
515 png_handle_sBIT(png_ptr
, info_ptr
, length
);
517 #ifdef PNG_READ_sRGB_SUPPORTED
518 else if (!png_memcmp(chunk_name
, png_sRGB
, 4))
519 png_handle_sRGB(png_ptr
, info_ptr
, length
);
521 #ifdef PNG_READ_iCCP_SUPPORTED
522 else if (!png_memcmp(chunk_name
, png_iCCP
, 4))
523 png_handle_iCCP(png_ptr
, info_ptr
, length
);
525 #ifdef PNG_READ_sPLT_SUPPORTED
526 else if (!png_memcmp(chunk_name
, png_sPLT
, 4))
527 png_handle_sPLT(png_ptr
, info_ptr
, length
);
529 #ifdef PNG_READ_tEXt_SUPPORTED
530 else if (!png_memcmp(chunk_name
, png_tEXt
, 4))
531 png_handle_tEXt(png_ptr
, info_ptr
, length
);
533 #ifdef PNG_READ_tIME_SUPPORTED
534 else if (!png_memcmp(chunk_name
, png_tIME
, 4))
535 png_handle_tIME(png_ptr
, info_ptr
, length
);
537 #ifdef PNG_READ_tRNS_SUPPORTED
538 else if (!png_memcmp(chunk_name
, png_tRNS
, 4))
539 png_handle_tRNS(png_ptr
, info_ptr
, length
);
541 #ifdef PNG_READ_zTXt_SUPPORTED
542 else if (!png_memcmp(chunk_name
, png_zTXt
, 4))
543 png_handle_zTXt(png_ptr
, info_ptr
, length
);
545 #ifdef PNG_READ_iTXt_SUPPORTED
546 else if (!png_memcmp(chunk_name
, png_iTXt
, 4))
547 png_handle_iTXt(png_ptr
, info_ptr
, length
);
550 png_handle_unknown(png_ptr
, info_ptr
, length
);
553 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
555 /* Optional call to update the users info_ptr structure */
557 png_read_update_info(png_structp png_ptr
, png_infop info_ptr
)
559 png_debug(1, "in png_read_update_info");
563 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
564 png_read_start_row(png_ptr
);
567 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
569 png_read_transform_info(png_ptr
, info_ptr
);
572 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
573 /* Initialize palette, background, etc, after transformations
574 * are set, but before any reading takes place. This allows
575 * the user to obtain a gamma-corrected palette, for example.
576 * If the user doesn't call this, we will do it ourselves.
579 png_start_read_image(png_structp png_ptr
)
581 png_debug(1, "in png_start_read_image");
585 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
586 png_read_start_row(png_ptr
);
588 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
590 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
592 png_read_row(png_structp png_ptr
, png_bytep row
, png_bytep dsp_row
)
595 PNG_CONST
int png_pass_dsp_mask
[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
597 PNG_CONST
int png_pass_mask
[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
603 png_debug2(1, "in png_read_row (row %lu, pass %d)",
604 png_ptr
->row_number
, png_ptr
->pass
);
606 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
607 png_read_start_row(png_ptr
);
608 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
610 /* Check for transforms that have been set but were defined out */
611 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
612 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
613 png_warning(png_ptr
, "PNG_READ_INVERT_SUPPORTED is not defined.");
615 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
616 if (png_ptr
->transformations
& PNG_FILLER
)
617 png_warning(png_ptr
, "PNG_READ_FILLER_SUPPORTED is not defined.");
619 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
620 !defined(PNG_READ_PACKSWAP_SUPPORTED)
621 if (png_ptr
->transformations
& PNG_PACKSWAP
)
622 png_warning(png_ptr
, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
624 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
625 if (png_ptr
->transformations
& PNG_PACK
)
626 png_warning(png_ptr
, "PNG_READ_PACK_SUPPORTED is not defined.");
628 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
629 if (png_ptr
->transformations
& PNG_SHIFT
)
630 png_warning(png_ptr
, "PNG_READ_SHIFT_SUPPORTED is not defined.");
632 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
633 if (png_ptr
->transformations
& PNG_BGR
)
634 png_warning(png_ptr
, "PNG_READ_BGR_SUPPORTED is not defined.");
636 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
637 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
638 png_warning(png_ptr
, "PNG_READ_SWAP_SUPPORTED is not defined.");
642 #ifdef PNG_READ_INTERLACING_SUPPORTED
643 /* If interlaced and we do not need a new row, combine row and return */
644 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
646 switch (png_ptr
->pass
)
649 if (png_ptr
->row_number
& 0x07)
652 png_combine_row(png_ptr
, dsp_row
,
653 png_pass_dsp_mask
[png_ptr
->pass
]);
654 png_read_finish_row(png_ptr
);
659 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
662 png_combine_row(png_ptr
, dsp_row
,
663 png_pass_dsp_mask
[png_ptr
->pass
]);
664 png_read_finish_row(png_ptr
);
669 if ((png_ptr
->row_number
& 0x07) != 4)
671 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 4))
672 png_combine_row(png_ptr
, dsp_row
,
673 png_pass_dsp_mask
[png_ptr
->pass
]);
674 png_read_finish_row(png_ptr
);
679 if ((png_ptr
->row_number
& 3) || png_ptr
->width
< 3)
682 png_combine_row(png_ptr
, dsp_row
,
683 png_pass_dsp_mask
[png_ptr
->pass
]);
684 png_read_finish_row(png_ptr
);
689 if ((png_ptr
->row_number
& 3) != 2)
691 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 2))
692 png_combine_row(png_ptr
, dsp_row
,
693 png_pass_dsp_mask
[png_ptr
->pass
]);
694 png_read_finish_row(png_ptr
);
699 if ((png_ptr
->row_number
& 1) || png_ptr
->width
< 2)
702 png_combine_row(png_ptr
, dsp_row
,
703 png_pass_dsp_mask
[png_ptr
->pass
]);
704 png_read_finish_row(png_ptr
);
709 if (!(png_ptr
->row_number
& 1))
711 png_read_finish_row(png_ptr
);
719 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
))
720 png_error(png_ptr
, "Invalid attempt to read row data");
722 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
723 png_ptr
->zstream
.avail_out
=
724 (uInt
)(PNG_ROWBYTES(png_ptr
->pixel_depth
,
725 png_ptr
->iwidth
) + 1);
728 if (!(png_ptr
->zstream
.avail_in
))
730 while (!png_ptr
->idat_size
)
732 png_crc_finish(png_ptr
, 0);
734 png_ptr
->idat_size
= png_read_chunk_header(png_ptr
);
735 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
736 png_error(png_ptr
, "Not enough image data");
738 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->zbuf_size
;
739 png_ptr
->zstream
.next_in
= png_ptr
->zbuf
;
740 if (png_ptr
->zbuf_size
> png_ptr
->idat_size
)
741 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->idat_size
;
742 png_crc_read(png_ptr
, png_ptr
->zbuf
,
743 (png_size_t
)png_ptr
->zstream
.avail_in
);
744 png_ptr
->idat_size
-= png_ptr
->zstream
.avail_in
;
746 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
747 if (ret
== Z_STREAM_END
)
749 if (png_ptr
->zstream
.avail_out
|| png_ptr
->zstream
.avail_in
||
751 png_error(png_ptr
, "Extra compressed data");
752 png_ptr
->mode
|= PNG_AFTER_IDAT
;
753 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
757 png_error(png_ptr
, png_ptr
->zstream
.msg
? png_ptr
->zstream
.msg
:
758 "Decompression error");
760 } while (png_ptr
->zstream
.avail_out
);
762 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
763 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
764 png_ptr
->row_info
.channels
= png_ptr
->channels
;
765 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
766 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
767 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
768 png_ptr
->row_info
.width
);
770 if (png_ptr
->row_buf
[0])
771 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
772 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
773 (int)(png_ptr
->row_buf
[0]));
775 png_memcpy_check(png_ptr
, png_ptr
->prev_row
, png_ptr
->row_buf
,
776 png_ptr
->rowbytes
+ 1);
778 #ifdef PNG_MNG_FEATURES_SUPPORTED
779 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
780 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
782 /* Intrapixel differencing */
783 png_do_read_intrapixel(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
788 if (png_ptr
->transformations
|| (png_ptr
->flags
&PNG_FLAG_STRIP_ALPHA
))
789 png_do_read_transformations(png_ptr
);
791 #ifdef PNG_READ_INTERLACING_SUPPORTED
792 /* Blow up interlaced rows to full size */
793 if (png_ptr
->interlaced
&&
794 (png_ptr
->transformations
& PNG_INTERLACE
))
796 if (png_ptr
->pass
< 6)
797 /* Old interface (pre-1.0.9):
798 * png_do_read_interlace(&(png_ptr->row_info),
799 * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
801 png_do_read_interlace(png_ptr
);
804 png_combine_row(png_ptr
, dsp_row
,
805 png_pass_dsp_mask
[png_ptr
->pass
]);
807 png_combine_row(png_ptr
, row
,
808 png_pass_mask
[png_ptr
->pass
]);
814 png_combine_row(png_ptr
, row
, 0xff);
816 png_combine_row(png_ptr
, dsp_row
, 0xff);
818 png_read_finish_row(png_ptr
);
820 if (png_ptr
->read_row_fn
!= NULL
)
821 (*(png_ptr
->read_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
823 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
825 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
826 /* Read one or more rows of image data. If the image is interlaced,
827 * and png_set_interlace_handling() has been called, the rows need to
828 * contain the contents of the rows from the previous pass. If the
829 * image has alpha or transparency, and png_handle_alpha()[*] has been
830 * called, the rows contents must be initialized to the contents of the
833 * "row" holds the actual image, and pixels are placed in it
834 * as they arrive. If the image is displayed after each pass, it will
835 * appear to "sparkle" in. "display_row" can be used to display a
836 * "chunky" progressive image, with finer detail added as it becomes
837 * available. If you do not want this "chunky" display, you may pass
838 * NULL for display_row. If you do not want the sparkle display, and
839 * you have not called png_handle_alpha(), you may pass NULL for rows.
840 * If you have called png_handle_alpha(), and the image has either an
841 * alpha channel or a transparency chunk, you must provide a buffer for
842 * rows. In this case, you do not have to provide a display_row buffer
843 * also, but you may. If the image is not interlaced, or if you have
844 * not called png_set_interlace_handling(), the display_row buffer will
845 * be ignored, so pass NULL to it.
847 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
851 png_read_rows(png_structp png_ptr
, png_bytepp row
,
852 png_bytepp display_row
, png_uint_32 num_rows
)
858 png_debug(1, "in png_read_rows");
864 if (rp
!= NULL
&& dp
!= NULL
)
865 for (i
= 0; i
< num_rows
; i
++)
867 png_bytep rptr
= *rp
++;
868 png_bytep dptr
= *dp
++;
870 png_read_row(png_ptr
, rptr
, dptr
);
873 for (i
= 0; i
< num_rows
; i
++)
875 png_bytep rptr
= *rp
;
876 png_read_row(png_ptr
, rptr
, png_bytep_NULL
);
880 for (i
= 0; i
< num_rows
; i
++)
882 png_bytep dptr
= *dp
;
883 png_read_row(png_ptr
, png_bytep_NULL
, dptr
);
887 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
889 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
890 /* Read the entire image. If the image has an alpha channel or a tRNS
891 * chunk, and you have called png_handle_alpha()[*], you will need to
892 * initialize the image to the current image that PNG will be overlaying.
893 * We set the num_rows again here, in case it was incorrectly set in
894 * png_read_start_row() by a call to png_read_update_info() or
895 * png_start_read_image() if png_set_interlace_handling() wasn't called
896 * prior to either of these functions like it should have been. You can
897 * only call this function once. If you desire to have an image for
898 * each pass of a interlaced image, use png_read_rows() instead.
900 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
903 png_read_image(png_structp png_ptr
, png_bytepp image
)
905 png_uint_32 i
, image_height
;
909 png_debug(1, "in png_read_image");
914 #ifdef PNG_READ_INTERLACING_SUPPORTED
915 pass
= png_set_interlace_handling(png_ptr
);
917 if (png_ptr
->interlaced
)
919 "Cannot read interlaced image -- interlace handler disabled.");
924 image_height
=png_ptr
->height
;
925 png_ptr
->num_rows
= image_height
; /* Make sure this is set correctly */
927 for (j
= 0; j
< pass
; j
++)
930 for (i
= 0; i
< image_height
; i
++)
932 png_read_row(png_ptr
, *rp
, png_bytep_NULL
);
937 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
939 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
940 /* Read the end of the PNG file. Will not read past the end of the
941 * file, will verify the end is accurate, and will read any comments
942 * or time information at the end of the file, if info is not NULL.
945 png_read_end(png_structp png_ptr
, png_infop info_ptr
)
947 png_debug(1, "in png_read_end");
951 png_crc_finish(png_ptr
, 0); /* Finish off CRC from last IDAT chunk */
955 #ifdef PNG_USE_LOCAL_ARRAYS
960 #ifdef PNG_READ_bKGD_SUPPORTED
963 #ifdef PNG_READ_cHRM_SUPPORTED
966 #ifdef PNG_READ_gAMA_SUPPORTED
969 #ifdef PNG_READ_hIST_SUPPORTED
972 #ifdef PNG_READ_iCCP_SUPPORTED
975 #ifdef PNG_READ_iTXt_SUPPORTED
978 #ifdef PNG_READ_oFFs_SUPPORTED
981 #ifdef PNG_READ_pCAL_SUPPORTED
984 #ifdef PNG_READ_pHYs_SUPPORTED
987 #ifdef PNG_READ_sBIT_SUPPORTED
990 #ifdef PNG_READ_sCAL_SUPPORTED
993 #ifdef PNG_READ_sPLT_SUPPORTED
996 #ifdef PNG_READ_sRGB_SUPPORTED
999 #ifdef PNG_READ_tEXt_SUPPORTED
1002 #ifdef PNG_READ_tIME_SUPPORTED
1005 #ifdef PNG_READ_tRNS_SUPPORTED
1008 #ifdef PNG_READ_zTXt_SUPPORTED
1011 #endif /* PNG_USE_LOCAL_ARRAYS */
1012 png_uint_32 length
= png_read_chunk_header(png_ptr
);
1013 PNG_CONST png_bytep chunk_name
= png_ptr
->chunk_name
;
1015 if (!png_memcmp(chunk_name
, png_IHDR
, 4))
1016 png_handle_IHDR(png_ptr
, info_ptr
, length
);
1017 else if (!png_memcmp(chunk_name
, png_IEND
, 4))
1018 png_handle_IEND(png_ptr
, info_ptr
, length
);
1019 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1020 else if (png_handle_as_unknown(png_ptr
, chunk_name
))
1022 if (!png_memcmp(chunk_name
, png_IDAT
, 4))
1024 if ((length
> 0) || (png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
))
1025 png_error(png_ptr
, "Too many IDAT's found");
1027 png_handle_unknown(png_ptr
, info_ptr
, length
);
1028 if (!png_memcmp(chunk_name
, png_PLTE
, 4))
1029 png_ptr
->mode
|= PNG_HAVE_PLTE
;
1032 else if (!png_memcmp(chunk_name
, png_IDAT
, 4))
1034 /* Zero length IDATs are legal after the last IDAT has been
1035 * read, but not after other chunks have been read.
1037 if ((length
> 0) || (png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
))
1038 png_error(png_ptr
, "Too many IDAT's found");
1039 png_crc_finish(png_ptr
, length
);
1041 else if (!png_memcmp(chunk_name
, png_PLTE
, 4))
1042 png_handle_PLTE(png_ptr
, info_ptr
, length
);
1043 #ifdef PNG_READ_bKGD_SUPPORTED
1044 else if (!png_memcmp(chunk_name
, png_bKGD
, 4))
1045 png_handle_bKGD(png_ptr
, info_ptr
, length
);
1047 #ifdef PNG_READ_cHRM_SUPPORTED
1048 else if (!png_memcmp(chunk_name
, png_cHRM
, 4))
1049 png_handle_cHRM(png_ptr
, info_ptr
, length
);
1051 #ifdef PNG_READ_gAMA_SUPPORTED
1052 else if (!png_memcmp(chunk_name
, png_gAMA
, 4))
1053 png_handle_gAMA(png_ptr
, info_ptr
, length
);
1055 #ifdef PNG_READ_hIST_SUPPORTED
1056 else if (!png_memcmp(chunk_name
, png_hIST
, 4))
1057 png_handle_hIST(png_ptr
, info_ptr
, length
);
1059 #ifdef PNG_READ_oFFs_SUPPORTED
1060 else if (!png_memcmp(chunk_name
, png_oFFs
, 4))
1061 png_handle_oFFs(png_ptr
, info_ptr
, length
);
1063 #ifdef PNG_READ_pCAL_SUPPORTED
1064 else if (!png_memcmp(chunk_name
, png_pCAL
, 4))
1065 png_handle_pCAL(png_ptr
, info_ptr
, length
);
1067 #ifdef PNG_READ_sCAL_SUPPORTED
1068 else if (!png_memcmp(chunk_name
, png_sCAL
, 4))
1069 png_handle_sCAL(png_ptr
, info_ptr
, length
);
1071 #ifdef PNG_READ_pHYs_SUPPORTED
1072 else if (!png_memcmp(chunk_name
, png_pHYs
, 4))
1073 png_handle_pHYs(png_ptr
, info_ptr
, length
);
1075 #ifdef PNG_READ_sBIT_SUPPORTED
1076 else if (!png_memcmp(chunk_name
, png_sBIT
, 4))
1077 png_handle_sBIT(png_ptr
, info_ptr
, length
);
1079 #ifdef PNG_READ_sRGB_SUPPORTED
1080 else if (!png_memcmp(chunk_name
, png_sRGB
, 4))
1081 png_handle_sRGB(png_ptr
, info_ptr
, length
);
1083 #ifdef PNG_READ_iCCP_SUPPORTED
1084 else if (!png_memcmp(chunk_name
, png_iCCP
, 4))
1085 png_handle_iCCP(png_ptr
, info_ptr
, length
);
1087 #ifdef PNG_READ_sPLT_SUPPORTED
1088 else if (!png_memcmp(chunk_name
, png_sPLT
, 4))
1089 png_handle_sPLT(png_ptr
, info_ptr
, length
);
1091 #ifdef PNG_READ_tEXt_SUPPORTED
1092 else if (!png_memcmp(chunk_name
, png_tEXt
, 4))
1093 png_handle_tEXt(png_ptr
, info_ptr
, length
);
1095 #ifdef PNG_READ_tIME_SUPPORTED
1096 else if (!png_memcmp(chunk_name
, png_tIME
, 4))
1097 png_handle_tIME(png_ptr
, info_ptr
, length
);
1099 #ifdef PNG_READ_tRNS_SUPPORTED
1100 else if (!png_memcmp(chunk_name
, png_tRNS
, 4))
1101 png_handle_tRNS(png_ptr
, info_ptr
, length
);
1103 #ifdef PNG_READ_zTXt_SUPPORTED
1104 else if (!png_memcmp(chunk_name
, png_zTXt
, 4))
1105 png_handle_zTXt(png_ptr
, info_ptr
, length
);
1107 #ifdef PNG_READ_iTXt_SUPPORTED
1108 else if (!png_memcmp(chunk_name
, png_iTXt
, 4))
1109 png_handle_iTXt(png_ptr
, info_ptr
, length
);
1112 png_handle_unknown(png_ptr
, info_ptr
, length
);
1113 } while (!(png_ptr
->mode
& PNG_HAVE_IEND
));
1115 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
1117 /* Free all memory used by the read */
1119 png_destroy_read_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
,
1120 png_infopp end_info_ptr_ptr
)
1122 png_structp png_ptr
= NULL
;
1123 png_infop info_ptr
= NULL
, end_info_ptr
= NULL
;
1124 #ifdef PNG_USER_MEM_SUPPORTED
1125 png_free_ptr free_fn
= NULL
;
1126 png_voidp mem_ptr
= NULL
;
1129 png_debug(1, "in png_destroy_read_struct");
1131 if (png_ptr_ptr
!= NULL
)
1132 png_ptr
= *png_ptr_ptr
;
1133 if (png_ptr
== NULL
)
1136 #ifdef PNG_USER_MEM_SUPPORTED
1137 free_fn
= png_ptr
->free_fn
;
1138 mem_ptr
= png_ptr
->mem_ptr
;
1141 if (info_ptr_ptr
!= NULL
)
1142 info_ptr
= *info_ptr_ptr
;
1144 if (end_info_ptr_ptr
!= NULL
)
1145 end_info_ptr
= *end_info_ptr_ptr
;
1147 png_read_destroy(png_ptr
, info_ptr
, end_info_ptr
);
1149 if (info_ptr
!= NULL
)
1151 #ifdef PNG_TEXT_SUPPORTED
1152 png_free_data(png_ptr
, info_ptr
, PNG_FREE_TEXT
, -1);
1155 #ifdef PNG_USER_MEM_SUPPORTED
1156 png_destroy_struct_2((png_voidp
)info_ptr
, (png_free_ptr
)free_fn
,
1157 (png_voidp
)mem_ptr
);
1159 png_destroy_struct((png_voidp
)info_ptr
);
1161 *info_ptr_ptr
= NULL
;
1164 if (end_info_ptr
!= NULL
)
1166 #ifdef PNG_READ_TEXT_SUPPORTED
1167 png_free_data(png_ptr
, end_info_ptr
, PNG_FREE_TEXT
, -1);
1169 #ifdef PNG_USER_MEM_SUPPORTED
1170 png_destroy_struct_2((png_voidp
)end_info_ptr
, (png_free_ptr
)free_fn
,
1171 (png_voidp
)mem_ptr
);
1173 png_destroy_struct((png_voidp
)end_info_ptr
);
1175 *end_info_ptr_ptr
= NULL
;
1178 if (png_ptr
!= NULL
)
1180 #ifdef PNG_USER_MEM_SUPPORTED
1181 png_destroy_struct_2((png_voidp
)png_ptr
, (png_free_ptr
)free_fn
,
1182 (png_voidp
)mem_ptr
);
1184 png_destroy_struct((png_voidp
)png_ptr
);
1186 *png_ptr_ptr
= NULL
;
1190 /* Free all memory used by the read (old method) */
1192 png_read_destroy(png_structp png_ptr
, png_infop info_ptr
,
1193 png_infop end_info_ptr
)
1195 #ifdef PNG_SETJMP_SUPPORTED
1198 png_error_ptr error_fn
;
1199 png_error_ptr warning_fn
;
1200 png_voidp error_ptr
;
1201 #ifdef PNG_USER_MEM_SUPPORTED
1202 png_free_ptr free_fn
;
1205 png_debug(1, "in png_read_destroy");
1207 if (info_ptr
!= NULL
)
1208 png_info_destroy(png_ptr
, info_ptr
);
1210 if (end_info_ptr
!= NULL
)
1211 png_info_destroy(png_ptr
, end_info_ptr
);
1213 png_free(png_ptr
, png_ptr
->zbuf
);
1214 png_free(png_ptr
, png_ptr
->big_row_buf
);
1215 png_free(png_ptr
, png_ptr
->prev_row
);
1216 png_free(png_ptr
, png_ptr
->chunkdata
);
1217 #ifdef PNG_READ_DITHER_SUPPORTED
1218 png_free(png_ptr
, png_ptr
->palette_lookup
);
1219 png_free(png_ptr
, png_ptr
->dither_index
);
1221 #ifdef PNG_READ_GAMMA_SUPPORTED
1222 png_free(png_ptr
, png_ptr
->gamma_table
);
1224 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1225 png_free(png_ptr
, png_ptr
->gamma_from_1
);
1226 png_free(png_ptr
, png_ptr
->gamma_to_1
);
1228 #ifdef PNG_FREE_ME_SUPPORTED
1229 if (png_ptr
->free_me
& PNG_FREE_PLTE
)
1230 png_zfree(png_ptr
, png_ptr
->palette
);
1231 png_ptr
->free_me
&= ~PNG_FREE_PLTE
;
1233 if (png_ptr
->flags
& PNG_FLAG_FREE_PLTE
)
1234 png_zfree(png_ptr
, png_ptr
->palette
);
1235 png_ptr
->flags
&= ~PNG_FLAG_FREE_PLTE
;
1237 #if defined(PNG_tRNS_SUPPORTED) || \
1238 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1239 #ifdef PNG_FREE_ME_SUPPORTED
1240 if (png_ptr
->free_me
& PNG_FREE_TRNS
)
1241 png_free(png_ptr
, png_ptr
->trans
);
1242 png_ptr
->free_me
&= ~PNG_FREE_TRNS
;
1244 if (png_ptr
->flags
& PNG_FLAG_FREE_TRNS
)
1245 png_free(png_ptr
, png_ptr
->trans
);
1246 png_ptr
->flags
&= ~PNG_FLAG_FREE_TRNS
;
1249 #ifdef PNG_READ_hIST_SUPPORTED
1250 #ifdef PNG_FREE_ME_SUPPORTED
1251 if (png_ptr
->free_me
& PNG_FREE_HIST
)
1252 png_free(png_ptr
, png_ptr
->hist
);
1253 png_ptr
->free_me
&= ~PNG_FREE_HIST
;
1255 if (png_ptr
->flags
& PNG_FLAG_FREE_HIST
)
1256 png_free(png_ptr
, png_ptr
->hist
);
1257 png_ptr
->flags
&= ~PNG_FLAG_FREE_HIST
;
1260 #ifdef PNG_READ_GAMMA_SUPPORTED
1261 if (png_ptr
->gamma_16_table
!= NULL
)
1264 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1265 for (i
= 0; i
< istop
; i
++)
1267 png_free(png_ptr
, png_ptr
->gamma_16_table
[i
]);
1269 png_free(png_ptr
, png_ptr
->gamma_16_table
);
1271 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1272 if (png_ptr
->gamma_16_from_1
!= NULL
)
1275 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1276 for (i
= 0; i
< istop
; i
++)
1278 png_free(png_ptr
, png_ptr
->gamma_16_from_1
[i
]);
1280 png_free(png_ptr
, png_ptr
->gamma_16_from_1
);
1282 if (png_ptr
->gamma_16_to_1
!= NULL
)
1285 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1286 for (i
= 0; i
< istop
; i
++)
1288 png_free(png_ptr
, png_ptr
->gamma_16_to_1
[i
]);
1290 png_free(png_ptr
, png_ptr
->gamma_16_to_1
);
1294 #ifdef PNG_TIME_RFC1123_SUPPORTED
1295 png_free(png_ptr
, png_ptr
->time_buffer
);
1298 inflateEnd(&png_ptr
->zstream
);
1299 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1300 png_free(png_ptr
, png_ptr
->save_buffer
);
1303 /* Save the important info out of the png_struct, in case it is
1306 #ifdef PNG_SETJMP_SUPPORTED
1307 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, png_sizeof(jmp_buf));
1310 error_fn
= png_ptr
->error_fn
;
1311 warning_fn
= png_ptr
->warning_fn
;
1312 error_ptr
= png_ptr
->error_ptr
;
1313 #ifdef PNG_USER_MEM_SUPPORTED
1314 free_fn
= png_ptr
->free_fn
;
1317 png_memset(png_ptr
, 0, png_sizeof(png_struct
));
1319 png_ptr
->error_fn
= error_fn
;
1320 png_ptr
->warning_fn
= warning_fn
;
1321 png_ptr
->error_ptr
= error_ptr
;
1322 #ifdef PNG_USER_MEM_SUPPORTED
1323 png_ptr
->free_fn
= free_fn
;
1326 #ifdef PNG_SETJMP_SUPPORTED
1327 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, png_sizeof(jmp_buf));
1333 png_set_read_status_fn(png_structp png_ptr
, png_read_status_ptr read_row_fn
)
1335 if (png_ptr
== NULL
)
1337 png_ptr
->read_row_fn
= read_row_fn
;
1341 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1342 #ifdef PNG_INFO_IMAGE_SUPPORTED
1344 png_read_png(png_structp png_ptr
, png_infop info_ptr
,
1350 if (png_ptr
== NULL
)
1352 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1353 /* Invert the alpha channel from opacity to transparency
1355 if (transforms
& PNG_TRANSFORM_INVERT_ALPHA
)
1356 png_set_invert_alpha(png_ptr
);
1359 /* png_read_info() gives us all of the information from the
1360 * PNG file before the first IDAT (image data chunk).
1362 png_read_info(png_ptr
, info_ptr
);
1363 if (info_ptr
->height
> PNG_UINT_32_MAX
/png_sizeof(png_bytep
))
1364 png_error(png_ptr
, "Image is too high to process with png_read_png()");
1366 /* -------------- image transformations start here ------------------- */
1368 #ifdef PNG_READ_16_TO_8_SUPPORTED
1369 /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
1371 if (transforms
& PNG_TRANSFORM_STRIP_16
)
1372 png_set_strip_16(png_ptr
);
1375 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1376 /* Strip alpha bytes from the input data without combining with
1377 * the background (not recommended).
1379 if (transforms
& PNG_TRANSFORM_STRIP_ALPHA
)
1380 png_set_strip_alpha(png_ptr
);
1383 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1384 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1385 * byte into separate bytes (useful for paletted and grayscale images).
1387 if (transforms
& PNG_TRANSFORM_PACKING
)
1388 png_set_packing(png_ptr
);
1391 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1392 /* Change the order of packed pixels to least significant bit first
1393 * (not useful if you are using png_set_packing).
1395 if (transforms
& PNG_TRANSFORM_PACKSWAP
)
1396 png_set_packswap(png_ptr
);
1399 #ifdef PNG_READ_EXPAND_SUPPORTED
1400 /* Expand paletted colors into true RGB triplets
1401 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1402 * Expand paletted or RGB images with transparency to full alpha
1403 * channels so the data will be available as RGBA quartets.
1405 if (transforms
& PNG_TRANSFORM_EXPAND
)
1406 if ((png_ptr
->bit_depth
< 8) ||
1407 (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
) ||
1408 (info_ptr
->valid
& PNG_INFO_tRNS
))
1409 png_set_expand(png_ptr
);
1412 /* We don't handle background color or gamma transformation or dithering.
1415 #ifdef PNG_READ_INVERT_SUPPORTED
1416 /* Invert monochrome files to have 0 as white and 1 as black
1418 if (transforms
& PNG_TRANSFORM_INVERT_MONO
)
1419 png_set_invert_mono(png_ptr
);
1422 #ifdef PNG_READ_SHIFT_SUPPORTED
1423 /* If you want to shift the pixel values from the range [0,255] or
1424 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1425 * colors were originally in:
1427 if ((transforms
& PNG_TRANSFORM_SHIFT
) && (info_ptr
->valid
& PNG_INFO_sBIT
))
1428 png_set_shift(png_ptr
, &info_ptr
->sig_bit
);
1431 #ifdef PNG_READ_BGR_SUPPORTED
1432 /* Flip the RGB pixels to BGR (or RGBA to BGRA)
1434 if (transforms
& PNG_TRANSFORM_BGR
)
1435 png_set_bgr(png_ptr
);
1438 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1439 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1441 if (transforms
& PNG_TRANSFORM_SWAP_ALPHA
)
1442 png_set_swap_alpha(png_ptr
);
1445 #ifdef PNG_READ_SWAP_SUPPORTED
1446 /* Swap bytes of 16 bit files to least significant byte first
1448 if (transforms
& PNG_TRANSFORM_SWAP_ENDIAN
)
1449 png_set_swap(png_ptr
);
1452 /* Added at libpng-1.2.41 */
1453 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1454 /* Invert the alpha channel from opacity to transparency
1456 if (transforms
& PNG_TRANSFORM_INVERT_ALPHA
)
1457 png_set_invert_alpha(png_ptr
);
1460 /* Added at libpng-1.2.41 */
1461 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1462 /* Expand grayscale image to RGB
1464 if (transforms
& PNG_TRANSFORM_GRAY_TO_RGB
)
1465 png_set_gray_to_rgb(png_ptr
);
1468 /* We don't handle adding filler bytes */
1470 /* Optional call to gamma correct and add the background to the palette
1471 * and update info structure. REQUIRED if you are expecting libpng to
1472 * update the palette for you (i.e., you selected such a transform above).
1474 png_read_update_info(png_ptr
, info_ptr
);
1476 /* -------------- image transformations end here ------------------- */
1478 #ifdef PNG_FREE_ME_SUPPORTED
1479 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ROWS
, 0);
1481 if (info_ptr
->row_pointers
== NULL
)
1483 info_ptr
->row_pointers
= (png_bytepp
)png_malloc(png_ptr
,
1484 info_ptr
->height
* png_sizeof(png_bytep
));
1485 png_memset(info_ptr
->row_pointers
, 0, info_ptr
->height
1486 * png_sizeof(png_bytep
));
1488 #ifdef PNG_FREE_ME_SUPPORTED
1489 info_ptr
->free_me
|= PNG_FREE_ROWS
;
1492 for (row
= 0; row
< (int)info_ptr
->height
; row
++)
1493 info_ptr
->row_pointers
[row
] = (png_bytep
)png_malloc(png_ptr
,
1494 png_get_rowbytes(png_ptr
, info_ptr
));
1497 png_read_image(png_ptr
, info_ptr
->row_pointers
);
1498 info_ptr
->valid
|= PNG_INFO_IDAT
;
1500 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1501 png_read_end(png_ptr
, info_ptr
);
1503 PNG_UNUSED(transforms
) /* Quiet compiler warnings */
1507 #endif /* PNG_INFO_IMAGE_SUPPORTED */
1508 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
1509 #endif /* PNG_READ_SUPPORTED */