2 /* pngread.c - read a PNG file
4 * libpng 1.2.7 - September 12, 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. */
177 png_read_init(png_structp png_ptr
)
179 /* We only come here via pre-1.0.7-compiled applications */
180 png_read_init_2(png_ptr
, "1.0.6 or earlier", 0, 0);
184 png_read_init_2(png_structp png_ptr
, png_const_charp user_png_ver
,
185 png_size_t png_struct_size
, png_size_t png_info_size
)
187 /* We only come here via pre-1.0.12-compiled applications */
188 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
189 if(png_sizeof(png_struct
) > png_struct_size
||
190 png_sizeof(png_info
) > png_info_size
)
193 png_ptr
->warning_fn
=NULL
;
196 sprintf(msg
, "Application was compiled with png.h from libpng-%.20s",
198 png_warning(png_ptr
, msg
);
200 sprintf(msg
, "Application is running with png.c from libpng-%.20s",
202 png_warning(png_ptr
, msg
);
205 if(png_sizeof(png_struct
) > png_struct_size
)
207 png_ptr
->error_fn
=NULL
;
208 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
212 "The png struct allocated by the application for reading is too small.");
214 if(png_sizeof(png_info
) > png_info_size
)
216 png_ptr
->error_fn
=NULL
;
217 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
221 "The info struct allocated by application for reading is too small.");
223 png_read_init_3(&png_ptr
, user_png_ver
, png_struct_size
);
227 png_read_init_3(png_structpp ptr_ptr
, png_const_charp user_png_ver
,
228 png_size_t png_struct_size
)
230 #ifdef PNG_SETJMP_SUPPORTED
231 jmp_buf tmp_jmp
; /* to save current jump buffer */
236 png_structp png_ptr
=*ptr_ptr
;
240 if(user_png_ver
[i
] != png_libpng_ver
[i
])
242 #ifdef PNG_LEGACY_SUPPORTED
243 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
245 png_ptr
->warning_fn
=NULL
;
247 "Application uses deprecated png_read_init() and should be recompiled.");
251 } while (png_libpng_ver
[i
++]);
253 png_debug(1, "in png_read_init_3\n");
255 #ifdef PNG_SETJMP_SUPPORTED
256 /* save jump buffer and error functions */
257 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, png_sizeof (jmp_buf));
260 if(png_sizeof(png_struct
) > png_struct_size
)
262 png_destroy_struct(png_ptr
);
263 *ptr_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
267 /* reset all variables to 0 */
268 png_memset(png_ptr
, 0, png_sizeof (png_struct
));
270 #ifdef PNG_SETJMP_SUPPORTED
271 /* restore jump buffer */
272 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, png_sizeof (jmp_buf));
275 /* added at libpng-1.2.6 */
276 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
277 png_ptr
->user_width_max
=PNG_USER_WIDTH_MAX
;
278 png_ptr
->user_height_max
=PNG_USER_HEIGHT_MAX
;
281 /* initialize zbuf - compression buffer */
282 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
283 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
284 (png_uint_32
)png_ptr
->zbuf_size
);
285 png_ptr
->zstream
.zalloc
= png_zalloc
;
286 png_ptr
->zstream
.zfree
= png_zfree
;
287 png_ptr
->zstream
.opaque
= (voidpf
)png_ptr
;
289 switch (inflateInit(&png_ptr
->zstream
))
291 case Z_OK
: /* Do nothing */ break;
293 case Z_STREAM_ERROR
: png_error(png_ptr
, "zlib memory"); break;
294 case Z_VERSION_ERROR
: png_error(png_ptr
, "zlib version"); break;
295 default: png_error(png_ptr
, "Unknown zlib error");
298 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
299 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
301 png_set_read_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
);
304 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
305 /* Read the information before the actual image data. This has been
306 * changed in v0.90 to allow reading a file that already has the magic
307 * bytes read from the stream. You can tell libpng how many bytes have
308 * been read from the beginning of the stream (up to the maximum of 8)
309 * via png_set_sig_bytes(), and we will only check the remaining bytes
310 * here. The application can then have access to the signature bytes we
311 * read if it is determined that this isn't a valid PNG file.
314 png_read_info(png_structp png_ptr
, png_infop info_ptr
)
316 png_debug(1, "in png_read_info\n");
317 /* If we haven't checked all of the PNG signature bytes, do so now. */
318 if (png_ptr
->sig_bytes
< 8)
320 png_size_t num_checked
= png_ptr
->sig_bytes
,
321 num_to_check
= 8 - num_checked
;
323 png_read_data(png_ptr
, &(info_ptr
->signature
[num_checked
]), num_to_check
);
324 png_ptr
->sig_bytes
= 8;
326 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
328 if (num_checked
< 4 &&
329 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
330 png_error(png_ptr
, "Not a PNG file");
332 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
335 png_ptr
->mode
|= PNG_HAVE_PNG_SIGNATURE
;
340 #ifdef PNG_USE_LOCAL_ARRAYS
345 #if defined(PNG_READ_bKGD_SUPPORTED)
348 #if defined(PNG_READ_cHRM_SUPPORTED)
351 #if defined(PNG_READ_gAMA_SUPPORTED)
354 #if defined(PNG_READ_hIST_SUPPORTED)
357 #if defined(PNG_READ_iCCP_SUPPORTED)
360 #if defined(PNG_READ_iTXt_SUPPORTED)
363 #if defined(PNG_READ_oFFs_SUPPORTED)
366 #if defined(PNG_READ_pCAL_SUPPORTED)
369 #if defined(PNG_READ_pHYs_SUPPORTED)
372 #if defined(PNG_READ_sBIT_SUPPORTED)
375 #if defined(PNG_READ_sCAL_SUPPORTED)
378 #if defined(PNG_READ_sPLT_SUPPORTED)
381 #if defined(PNG_READ_sRGB_SUPPORTED)
384 #if defined(PNG_READ_tEXt_SUPPORTED)
387 #if defined(PNG_READ_tIME_SUPPORTED)
390 #if defined(PNG_READ_tRNS_SUPPORTED)
393 #if defined(PNG_READ_zTXt_SUPPORTED)
396 #endif /* PNG_GLOBAL_ARRAYS */
397 png_byte chunk_length
[4];
400 png_read_data(png_ptr
, chunk_length
, 4);
401 length
= png_get_uint_31(png_ptr
,chunk_length
);
403 png_reset_crc(png_ptr
);
404 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
406 png_debug2(0, "Reading %s chunk, length=%lu.\n", png_ptr
->chunk_name
,
409 /* This should be a binary subdivision search or a hash for
410 * matching the chunk name rather than a linear search.
412 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
413 png_handle_IHDR(png_ptr
, info_ptr
, length
);
414 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
415 png_handle_IEND(png_ptr
, info_ptr
, length
);
416 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
417 else if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
))
419 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
420 png_ptr
->mode
|= PNG_HAVE_IDAT
;
421 png_handle_unknown(png_ptr
, info_ptr
, length
);
422 if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
423 png_ptr
->mode
|= PNG_HAVE_PLTE
;
424 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
426 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
427 png_error(png_ptr
, "Missing IHDR before IDAT");
428 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
429 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
430 png_error(png_ptr
, "Missing PLTE before IDAT");
435 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
436 png_handle_PLTE(png_ptr
, info_ptr
, length
);
437 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
439 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
440 png_error(png_ptr
, "Missing IHDR before IDAT");
441 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
442 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
443 png_error(png_ptr
, "Missing PLTE before IDAT");
445 png_ptr
->idat_size
= length
;
446 png_ptr
->mode
|= PNG_HAVE_IDAT
;
449 #if defined(PNG_READ_bKGD_SUPPORTED)
450 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
451 png_handle_bKGD(png_ptr
, info_ptr
, length
);
453 #if defined(PNG_READ_cHRM_SUPPORTED)
454 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
455 png_handle_cHRM(png_ptr
, info_ptr
, length
);
457 #if defined(PNG_READ_gAMA_SUPPORTED)
458 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
459 png_handle_gAMA(png_ptr
, info_ptr
, length
);
461 #if defined(PNG_READ_hIST_SUPPORTED)
462 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
463 png_handle_hIST(png_ptr
, info_ptr
, length
);
465 #if defined(PNG_READ_oFFs_SUPPORTED)
466 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
467 png_handle_oFFs(png_ptr
, info_ptr
, length
);
469 #if defined(PNG_READ_pCAL_SUPPORTED)
470 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
471 png_handle_pCAL(png_ptr
, info_ptr
, length
);
473 #if defined(PNG_READ_sCAL_SUPPORTED)
474 else if (!png_memcmp(png_ptr
->chunk_name
, png_sCAL
, 4))
475 png_handle_sCAL(png_ptr
, info_ptr
, length
);
477 #if defined(PNG_READ_pHYs_SUPPORTED)
478 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
479 png_handle_pHYs(png_ptr
, info_ptr
, length
);
481 #if defined(PNG_READ_sBIT_SUPPORTED)
482 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
483 png_handle_sBIT(png_ptr
, info_ptr
, length
);
485 #if defined(PNG_READ_sRGB_SUPPORTED)
486 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
487 png_handle_sRGB(png_ptr
, info_ptr
, length
);
489 #if defined(PNG_READ_iCCP_SUPPORTED)
490 else if (!png_memcmp(png_ptr
->chunk_name
, png_iCCP
, 4))
491 png_handle_iCCP(png_ptr
, info_ptr
, length
);
493 #if defined(PNG_READ_sPLT_SUPPORTED)
494 else if (!png_memcmp(png_ptr
->chunk_name
, png_sPLT
, 4))
495 png_handle_sPLT(png_ptr
, info_ptr
, length
);
497 #if defined(PNG_READ_tEXt_SUPPORTED)
498 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
499 png_handle_tEXt(png_ptr
, info_ptr
, length
);
501 #if defined(PNG_READ_tIME_SUPPORTED)
502 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
503 png_handle_tIME(png_ptr
, info_ptr
, length
);
505 #if defined(PNG_READ_tRNS_SUPPORTED)
506 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
507 png_handle_tRNS(png_ptr
, info_ptr
, length
);
509 #if defined(PNG_READ_zTXt_SUPPORTED)
510 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
511 png_handle_zTXt(png_ptr
, info_ptr
, length
);
513 #if defined(PNG_READ_iTXt_SUPPORTED)
514 else if (!png_memcmp(png_ptr
->chunk_name
, png_iTXt
, 4))
515 png_handle_iTXt(png_ptr
, info_ptr
, length
);
518 png_handle_unknown(png_ptr
, info_ptr
, length
);
521 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
523 /* optional call to update the users info_ptr structure */
525 png_read_update_info(png_structp png_ptr
, png_infop info_ptr
)
527 png_debug(1, "in png_read_update_info\n");
528 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
529 png_read_start_row(png_ptr
);
532 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
533 png_read_transform_info(png_ptr
, info_ptr
);
536 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
537 /* Initialize palette, background, etc, after transformations
538 * are set, but before any reading takes place. This allows
539 * the user to obtain a gamma-corrected palette, for example.
540 * If the user doesn't call this, we will do it ourselves.
543 png_start_read_image(png_structp png_ptr
)
545 png_debug(1, "in png_start_read_image\n");
546 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
547 png_read_start_row(png_ptr
);
549 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
551 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
553 png_read_row(png_structp png_ptr
, png_bytep row
, png_bytep dsp_row
)
555 #ifdef PNG_USE_LOCAL_ARRAYS
557 const int png_pass_dsp_mask
[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
558 const int png_pass_mask
[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
561 png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
562 png_ptr
->row_number
, png_ptr
->pass
);
563 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
564 png_read_start_row(png_ptr
);
565 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
567 /* check for transforms that have been set but were defined out */
568 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
569 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
570 png_warning(png_ptr
, "PNG_READ_INVERT_SUPPORTED is not defined.");
572 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
573 if (png_ptr
->transformations
& PNG_FILLER
)
574 png_warning(png_ptr
, "PNG_READ_FILLER_SUPPORTED is not defined.");
576 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
577 if (png_ptr
->transformations
& PNG_PACKSWAP
)
578 png_warning(png_ptr
, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
580 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
581 if (png_ptr
->transformations
& PNG_PACK
)
582 png_warning(png_ptr
, "PNG_READ_PACK_SUPPORTED is not defined.");
584 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
585 if (png_ptr
->transformations
& PNG_SHIFT
)
586 png_warning(png_ptr
, "PNG_READ_SHIFT_SUPPORTED is not defined.");
588 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
589 if (png_ptr
->transformations
& PNG_BGR
)
590 png_warning(png_ptr
, "PNG_READ_BGR_SUPPORTED is not defined.");
592 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
593 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
594 png_warning(png_ptr
, "PNG_READ_SWAP_SUPPORTED is not defined.");
598 #if defined(PNG_READ_INTERLACING_SUPPORTED)
599 /* if interlaced and we do not need a new row, combine row and return */
600 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
602 switch (png_ptr
->pass
)
605 if (png_ptr
->row_number
& 0x07)
608 png_combine_row(png_ptr
, dsp_row
,
609 png_pass_dsp_mask
[png_ptr
->pass
]);
610 png_read_finish_row(png_ptr
);
615 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
618 png_combine_row(png_ptr
, dsp_row
,
619 png_pass_dsp_mask
[png_ptr
->pass
]);
620 png_read_finish_row(png_ptr
);
625 if ((png_ptr
->row_number
& 0x07) != 4)
627 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 4))
628 png_combine_row(png_ptr
, dsp_row
,
629 png_pass_dsp_mask
[png_ptr
->pass
]);
630 png_read_finish_row(png_ptr
);
635 if ((png_ptr
->row_number
& 3) || png_ptr
->width
< 3)
638 png_combine_row(png_ptr
, dsp_row
,
639 png_pass_dsp_mask
[png_ptr
->pass
]);
640 png_read_finish_row(png_ptr
);
645 if ((png_ptr
->row_number
& 3) != 2)
647 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 2))
648 png_combine_row(png_ptr
, dsp_row
,
649 png_pass_dsp_mask
[png_ptr
->pass
]);
650 png_read_finish_row(png_ptr
);
655 if ((png_ptr
->row_number
& 1) || png_ptr
->width
< 2)
658 png_combine_row(png_ptr
, dsp_row
,
659 png_pass_dsp_mask
[png_ptr
->pass
]);
660 png_read_finish_row(png_ptr
);
665 if (!(png_ptr
->row_number
& 1))
667 png_read_finish_row(png_ptr
);
675 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
))
676 png_error(png_ptr
, "Invalid attempt to read row data");
678 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
679 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->irowbytes
;
682 if (!(png_ptr
->zstream
.avail_in
))
684 while (!png_ptr
->idat_size
)
686 png_byte chunk_length
[4];
688 png_crc_finish(png_ptr
, 0);
690 png_read_data(png_ptr
, chunk_length
, 4);
691 png_ptr
->idat_size
= png_get_uint_31(png_ptr
,chunk_length
);
693 png_reset_crc(png_ptr
);
694 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
695 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
696 png_error(png_ptr
, "Not enough image data");
698 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->zbuf_size
;
699 png_ptr
->zstream
.next_in
= png_ptr
->zbuf
;
700 if (png_ptr
->zbuf_size
> png_ptr
->idat_size
)
701 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->idat_size
;
702 png_crc_read(png_ptr
, png_ptr
->zbuf
,
703 (png_size_t
)png_ptr
->zstream
.avail_in
);
704 png_ptr
->idat_size
-= png_ptr
->zstream
.avail_in
;
706 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
707 if (ret
== Z_STREAM_END
)
709 if (png_ptr
->zstream
.avail_out
|| png_ptr
->zstream
.avail_in
||
711 png_error(png_ptr
, "Extra compressed data");
712 png_ptr
->mode
|= PNG_AFTER_IDAT
;
713 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
717 png_error(png_ptr
, png_ptr
->zstream
.msg
? png_ptr
->zstream
.msg
:
718 "Decompression error");
720 } while (png_ptr
->zstream
.avail_out
);
722 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
723 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
724 png_ptr
->row_info
.channels
= png_ptr
->channels
;
725 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
726 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
727 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
728 png_ptr
->row_info
.width
);
730 if(png_ptr
->row_buf
[0])
731 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
732 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
733 (int)(png_ptr
->row_buf
[0]));
735 png_memcpy_check(png_ptr
, png_ptr
->prev_row
, png_ptr
->row_buf
,
736 png_ptr
->rowbytes
+ 1);
738 #if defined(PNG_MNG_FEATURES_SUPPORTED)
739 if((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
740 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
742 /* Intrapixel differencing */
743 png_do_read_intrapixel(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
747 if (png_ptr
->transformations
)
748 png_do_read_transformations(png_ptr
);
750 #if defined(PNG_READ_INTERLACING_SUPPORTED)
751 /* blow up interlaced rows to full size */
752 if (png_ptr
->interlaced
&&
753 (png_ptr
->transformations
& PNG_INTERLACE
))
755 if (png_ptr
->pass
< 6)
756 /* old interface (pre-1.0.9):
757 png_do_read_interlace(&(png_ptr->row_info),
758 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
760 png_do_read_interlace(png_ptr
);
763 png_combine_row(png_ptr
, dsp_row
,
764 png_pass_dsp_mask
[png_ptr
->pass
]);
766 png_combine_row(png_ptr
, row
,
767 png_pass_mask
[png_ptr
->pass
]);
773 png_combine_row(png_ptr
, row
, 0xff);
775 png_combine_row(png_ptr
, dsp_row
, 0xff);
777 png_read_finish_row(png_ptr
);
779 if (png_ptr
->read_row_fn
!= NULL
)
780 (*(png_ptr
->read_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
782 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
784 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
785 /* Read one or more rows of image data. If the image is interlaced,
786 * and png_set_interlace_handling() has been called, the rows need to
787 * contain the contents of the rows from the previous pass. If the
788 * image has alpha or transparency, and png_handle_alpha()[*] has been
789 * called, the rows contents must be initialized to the contents of the
792 * "row" holds the actual image, and pixels are placed in it
793 * as they arrive. If the image is displayed after each pass, it will
794 * appear to "sparkle" in. "display_row" can be used to display a
795 * "chunky" progressive image, with finer detail added as it becomes
796 * available. If you do not want this "chunky" display, you may pass
797 * NULL for display_row. If you do not want the sparkle display, and
798 * you have not called png_handle_alpha(), you may pass NULL for rows.
799 * If you have called png_handle_alpha(), and the image has either an
800 * alpha channel or a transparency chunk, you must provide a buffer for
801 * rows. In this case, you do not have to provide a display_row buffer
802 * also, but you may. If the image is not interlaced, or if you have
803 * not called png_set_interlace_handling(), the display_row buffer will
804 * be ignored, so pass NULL to it.
806 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.2.7
810 png_read_rows(png_structp png_ptr
, png_bytepp row
,
811 png_bytepp display_row
, png_uint_32 num_rows
)
817 png_debug(1, "in png_read_rows\n");
820 if (rp
!= NULL
&& dp
!= NULL
)
821 for (i
= 0; i
< num_rows
; i
++)
823 png_bytep rptr
= *rp
++;
824 png_bytep dptr
= *dp
++;
826 png_read_row(png_ptr
, rptr
, dptr
);
829 for (i
= 0; i
< num_rows
; i
++)
831 png_bytep rptr
= *rp
;
832 png_read_row(png_ptr
, rptr
, png_bytep_NULL
);
836 for (i
= 0; i
< num_rows
; i
++)
838 png_bytep dptr
= *dp
;
839 png_read_row(png_ptr
, png_bytep_NULL
, dptr
);
843 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
845 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
846 /* Read the entire image. If the image has an alpha channel or a tRNS
847 * chunk, and you have called png_handle_alpha()[*], you will need to
848 * initialize the image to the current image that PNG will be overlaying.
849 * We set the num_rows again here, in case it was incorrectly set in
850 * png_read_start_row() by a call to png_read_update_info() or
851 * png_start_read_image() if png_set_interlace_handling() wasn't called
852 * prior to either of these functions like it should have been. You can
853 * only call this function once. If you desire to have an image for
854 * each pass of a interlaced image, use png_read_rows() instead.
856 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.2.7
859 png_read_image(png_structp png_ptr
, png_bytepp image
)
861 png_uint_32 i
,image_height
;
865 png_debug(1, "in png_read_image\n");
867 #ifdef PNG_READ_INTERLACING_SUPPORTED
868 pass
= png_set_interlace_handling(png_ptr
);
870 if (png_ptr
->interlaced
)
872 "Cannot read interlaced image -- interlace handler disabled.");
877 image_height
=png_ptr
->height
;
878 png_ptr
->num_rows
= image_height
; /* Make sure this is set correctly */
880 for (j
= 0; j
< pass
; j
++)
883 for (i
= 0; i
< image_height
; i
++)
885 png_read_row(png_ptr
, *rp
, png_bytep_NULL
);
890 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
892 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
893 /* Read the end of the PNG file. Will not read past the end of the
894 * file, will verify the end is accurate, and will read any comments
895 * or time information at the end of the file, if info is not NULL.
898 png_read_end(png_structp png_ptr
, png_infop info_ptr
)
900 png_byte chunk_length
[4];
903 png_debug(1, "in png_read_end\n");
904 png_crc_finish(png_ptr
, 0); /* Finish off CRC from last IDAT chunk */
908 #ifdef PNG_USE_LOCAL_ARRAYS
913 #if defined(PNG_READ_bKGD_SUPPORTED)
916 #if defined(PNG_READ_cHRM_SUPPORTED)
919 #if defined(PNG_READ_gAMA_SUPPORTED)
922 #if defined(PNG_READ_hIST_SUPPORTED)
925 #if defined(PNG_READ_iCCP_SUPPORTED)
928 #if defined(PNG_READ_iTXt_SUPPORTED)
931 #if defined(PNG_READ_oFFs_SUPPORTED)
934 #if defined(PNG_READ_pCAL_SUPPORTED)
937 #if defined(PNG_READ_pHYs_SUPPORTED)
940 #if defined(PNG_READ_sBIT_SUPPORTED)
943 #if defined(PNG_READ_sCAL_SUPPORTED)
946 #if defined(PNG_READ_sPLT_SUPPORTED)
949 #if defined(PNG_READ_sRGB_SUPPORTED)
952 #if defined(PNG_READ_tEXt_SUPPORTED)
955 #if defined(PNG_READ_tIME_SUPPORTED)
958 #if defined(PNG_READ_tRNS_SUPPORTED)
961 #if defined(PNG_READ_zTXt_SUPPORTED)
964 #endif /* PNG_GLOBAL_ARRAYS */
966 png_read_data(png_ptr
, chunk_length
, 4);
967 length
= png_get_uint_31(png_ptr
,chunk_length
);
969 png_reset_crc(png_ptr
);
970 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
972 png_debug1(0, "Reading %s chunk.\n", png_ptr
->chunk_name
);
974 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
975 png_handle_IHDR(png_ptr
, info_ptr
, length
);
976 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
977 png_handle_IEND(png_ptr
, info_ptr
, length
);
978 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
979 else if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
))
981 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
983 if (length
> 0 || png_ptr
->mode
& PNG_AFTER_IDAT
)
984 png_error(png_ptr
, "Too many IDAT's found");
987 png_ptr
->mode
|= PNG_AFTER_IDAT
;
988 png_handle_unknown(png_ptr
, info_ptr
, length
);
989 if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
990 png_ptr
->mode
|= PNG_HAVE_PLTE
;
993 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
995 /* Zero length IDATs are legal after the last IDAT has been
996 * read, but not after other chunks have been read.
998 if (length
> 0 || png_ptr
->mode
& PNG_AFTER_IDAT
)
999 png_error(png_ptr
, "Too many IDAT's found");
1000 png_crc_finish(png_ptr
, length
);
1002 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
1003 png_handle_PLTE(png_ptr
, info_ptr
, length
);
1004 #if defined(PNG_READ_bKGD_SUPPORTED)
1005 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
1006 png_handle_bKGD(png_ptr
, info_ptr
, length
);
1008 #if defined(PNG_READ_cHRM_SUPPORTED)
1009 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
1010 png_handle_cHRM(png_ptr
, info_ptr
, length
);
1012 #if defined(PNG_READ_gAMA_SUPPORTED)
1013 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
1014 png_handle_gAMA(png_ptr
, info_ptr
, length
);
1016 #if defined(PNG_READ_hIST_SUPPORTED)
1017 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
1018 png_handle_hIST(png_ptr
, info_ptr
, length
);
1020 #if defined(PNG_READ_oFFs_SUPPORTED)
1021 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
1022 png_handle_oFFs(png_ptr
, info_ptr
, length
);
1024 #if defined(PNG_READ_pCAL_SUPPORTED)
1025 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
1026 png_handle_pCAL(png_ptr
, info_ptr
, length
);
1028 #if defined(PNG_READ_sCAL_SUPPORTED)
1029 else if (!png_memcmp(png_ptr
->chunk_name
, png_sCAL
, 4))
1030 png_handle_sCAL(png_ptr
, info_ptr
, length
);
1032 #if defined(PNG_READ_pHYs_SUPPORTED)
1033 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
1034 png_handle_pHYs(png_ptr
, info_ptr
, length
);
1036 #if defined(PNG_READ_sBIT_SUPPORTED)
1037 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
1038 png_handle_sBIT(png_ptr
, info_ptr
, length
);
1040 #if defined(PNG_READ_sRGB_SUPPORTED)
1041 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
1042 png_handle_sRGB(png_ptr
, info_ptr
, length
);
1044 #if defined(PNG_READ_iCCP_SUPPORTED)
1045 else if (!png_memcmp(png_ptr
->chunk_name
, png_iCCP
, 4))
1046 png_handle_iCCP(png_ptr
, info_ptr
, length
);
1048 #if defined(PNG_READ_sPLT_SUPPORTED)
1049 else if (!png_memcmp(png_ptr
->chunk_name
, png_sPLT
, 4))
1050 png_handle_sPLT(png_ptr
, info_ptr
, length
);
1052 #if defined(PNG_READ_tEXt_SUPPORTED)
1053 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
1054 png_handle_tEXt(png_ptr
, info_ptr
, length
);
1056 #if defined(PNG_READ_tIME_SUPPORTED)
1057 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
1058 png_handle_tIME(png_ptr
, info_ptr
, length
);
1060 #if defined(PNG_READ_tRNS_SUPPORTED)
1061 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
1062 png_handle_tRNS(png_ptr
, info_ptr
, length
);
1064 #if defined(PNG_READ_zTXt_SUPPORTED)
1065 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
1066 png_handle_zTXt(png_ptr
, info_ptr
, length
);
1068 #if defined(PNG_READ_iTXt_SUPPORTED)
1069 else if (!png_memcmp(png_ptr
->chunk_name
, png_iTXt
, 4))
1070 png_handle_iTXt(png_ptr
, info_ptr
, length
);
1073 png_handle_unknown(png_ptr
, info_ptr
, length
);
1074 } while (!(png_ptr
->mode
& PNG_HAVE_IEND
));
1076 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
1078 /* free all memory used by the read */
1080 png_destroy_read_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
,
1081 png_infopp end_info_ptr_ptr
)
1083 png_structp png_ptr
= NULL
;
1084 png_infop info_ptr
= NULL
, end_info_ptr
= NULL
;
1085 #ifdef PNG_USER_MEM_SUPPORTED
1086 png_free_ptr free_fn
;
1090 png_debug(1, "in png_destroy_read_struct\n");
1091 if (png_ptr_ptr
!= NULL
)
1092 png_ptr
= *png_ptr_ptr
;
1094 if (info_ptr_ptr
!= NULL
)
1095 info_ptr
= *info_ptr_ptr
;
1097 if (end_info_ptr_ptr
!= NULL
)
1098 end_info_ptr
= *end_info_ptr_ptr
;
1100 #ifdef PNG_USER_MEM_SUPPORTED
1101 free_fn
= png_ptr
->free_fn
;
1102 mem_ptr
= png_ptr
->mem_ptr
;
1105 png_read_destroy(png_ptr
, info_ptr
, end_info_ptr
);
1107 if (info_ptr
!= NULL
)
1109 #if defined(PNG_TEXT_SUPPORTED)
1110 png_free_data(png_ptr
, info_ptr
, PNG_FREE_TEXT
, -1);
1113 #ifdef PNG_USER_MEM_SUPPORTED
1114 png_destroy_struct_2((png_voidp
)info_ptr
, (png_free_ptr
)free_fn
,
1115 (png_voidp
)mem_ptr
);
1117 png_destroy_struct((png_voidp
)info_ptr
);
1119 *info_ptr_ptr
= NULL
;
1122 if (end_info_ptr
!= NULL
)
1124 #if defined(PNG_READ_TEXT_SUPPORTED)
1125 png_free_data(png_ptr
, end_info_ptr
, PNG_FREE_TEXT
, -1);
1127 #ifdef PNG_USER_MEM_SUPPORTED
1128 png_destroy_struct_2((png_voidp
)end_info_ptr
, (png_free_ptr
)free_fn
,
1129 (png_voidp
)mem_ptr
);
1131 png_destroy_struct((png_voidp
)end_info_ptr
);
1133 *end_info_ptr_ptr
= NULL
;
1136 if (png_ptr
!= NULL
)
1138 #ifdef PNG_USER_MEM_SUPPORTED
1139 png_destroy_struct_2((png_voidp
)png_ptr
, (png_free_ptr
)free_fn
,
1140 (png_voidp
)mem_ptr
);
1142 png_destroy_struct((png_voidp
)png_ptr
);
1144 *png_ptr_ptr
= NULL
;
1148 /* free all memory used by the read (old method) */
1150 png_read_destroy(png_structp png_ptr
, png_infop info_ptr
, png_infop end_info_ptr
)
1152 #ifdef PNG_SETJMP_SUPPORTED
1155 png_error_ptr error_fn
;
1156 png_error_ptr warning_fn
;
1157 png_voidp error_ptr
;
1158 #ifdef PNG_USER_MEM_SUPPORTED
1159 png_free_ptr free_fn
;
1162 png_debug(1, "in png_read_destroy\n");
1163 if (info_ptr
!= NULL
)
1164 png_info_destroy(png_ptr
, info_ptr
);
1166 if (end_info_ptr
!= NULL
)
1167 png_info_destroy(png_ptr
, end_info_ptr
);
1169 png_free(png_ptr
, png_ptr
->zbuf
);
1170 png_free(png_ptr
, png_ptr
->big_row_buf
);
1171 png_free(png_ptr
, png_ptr
->prev_row
);
1172 #if defined(PNG_READ_DITHER_SUPPORTED)
1173 png_free(png_ptr
, png_ptr
->palette_lookup
);
1174 png_free(png_ptr
, png_ptr
->dither_index
);
1176 #if defined(PNG_READ_GAMMA_SUPPORTED)
1177 png_free(png_ptr
, png_ptr
->gamma_table
);
1179 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1180 png_free(png_ptr
, png_ptr
->gamma_from_1
);
1181 png_free(png_ptr
, png_ptr
->gamma_to_1
);
1183 #ifdef PNG_FREE_ME_SUPPORTED
1184 if (png_ptr
->free_me
& PNG_FREE_PLTE
)
1185 png_zfree(png_ptr
, png_ptr
->palette
);
1186 png_ptr
->free_me
&= ~PNG_FREE_PLTE
;
1188 if (png_ptr
->flags
& PNG_FLAG_FREE_PLTE
)
1189 png_zfree(png_ptr
, png_ptr
->palette
);
1190 png_ptr
->flags
&= ~PNG_FLAG_FREE_PLTE
;
1192 #if defined(PNG_tRNS_SUPPORTED) || \
1193 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1194 #ifdef PNG_FREE_ME_SUPPORTED
1195 if (png_ptr
->free_me
& PNG_FREE_TRNS
)
1196 png_free(png_ptr
, png_ptr
->trans
);
1197 png_ptr
->free_me
&= ~PNG_FREE_TRNS
;
1199 if (png_ptr
->flags
& PNG_FLAG_FREE_TRNS
)
1200 png_free(png_ptr
, png_ptr
->trans
);
1201 png_ptr
->flags
&= ~PNG_FLAG_FREE_TRNS
;
1204 #if defined(PNG_READ_hIST_SUPPORTED)
1205 #ifdef PNG_FREE_ME_SUPPORTED
1206 if (png_ptr
->free_me
& PNG_FREE_HIST
)
1207 png_free(png_ptr
, png_ptr
->hist
);
1208 png_ptr
->free_me
&= ~PNG_FREE_HIST
;
1210 if (png_ptr
->flags
& PNG_FLAG_FREE_HIST
)
1211 png_free(png_ptr
, png_ptr
->hist
);
1212 png_ptr
->flags
&= ~PNG_FLAG_FREE_HIST
;
1215 #if defined(PNG_READ_GAMMA_SUPPORTED)
1216 if (png_ptr
->gamma_16_table
!= NULL
)
1219 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1220 for (i
= 0; i
< istop
; i
++)
1222 png_free(png_ptr
, png_ptr
->gamma_16_table
[i
]);
1224 png_free(png_ptr
, png_ptr
->gamma_16_table
);
1226 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1227 if (png_ptr
->gamma_16_from_1
!= NULL
)
1230 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1231 for (i
= 0; i
< istop
; i
++)
1233 png_free(png_ptr
, png_ptr
->gamma_16_from_1
[i
]);
1235 png_free(png_ptr
, png_ptr
->gamma_16_from_1
);
1237 if (png_ptr
->gamma_16_to_1
!= NULL
)
1240 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1241 for (i
= 0; i
< istop
; i
++)
1243 png_free(png_ptr
, png_ptr
->gamma_16_to_1
[i
]);
1245 png_free(png_ptr
, png_ptr
->gamma_16_to_1
);
1249 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1250 png_free(png_ptr
, png_ptr
->time_buffer
);
1253 inflateEnd(&png_ptr
->zstream
);
1254 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1255 png_free(png_ptr
, png_ptr
->save_buffer
);
1258 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1259 #ifdef PNG_TEXT_SUPPORTED
1260 png_free(png_ptr
, png_ptr
->current_text
);
1261 #endif /* PNG_TEXT_SUPPORTED */
1262 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1264 /* Save the important info out of the png_struct, in case it is
1267 #ifdef PNG_SETJMP_SUPPORTED
1268 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, png_sizeof (jmp_buf));
1271 error_fn
= png_ptr
->error_fn
;
1272 warning_fn
= png_ptr
->warning_fn
;
1273 error_ptr
= png_ptr
->error_ptr
;
1274 #ifdef PNG_USER_MEM_SUPPORTED
1275 free_fn
= png_ptr
->free_fn
;
1278 png_memset(png_ptr
, 0, png_sizeof (png_struct
));
1280 png_ptr
->error_fn
= error_fn
;
1281 png_ptr
->warning_fn
= warning_fn
;
1282 png_ptr
->error_ptr
= error_ptr
;
1283 #ifdef PNG_USER_MEM_SUPPORTED
1284 png_ptr
->free_fn
= free_fn
;
1287 #ifdef PNG_SETJMP_SUPPORTED
1288 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, png_sizeof (jmp_buf));
1294 png_set_read_status_fn(png_structp png_ptr
, png_read_status_ptr read_row_fn
)
1296 png_ptr
->read_row_fn
= read_row_fn
;
1300 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
1301 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1303 png_read_png(png_structp png_ptr
, png_infop info_ptr
,
1309 #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1310 /* invert the alpha channel from opacity to transparency
1312 if (transforms
& PNG_TRANSFORM_INVERT_ALPHA
)
1313 png_set_invert_alpha(png_ptr
);
1316 /* png_read_info() gives us all of the information from the
1317 * PNG file before the first IDAT (image data chunk).
1319 png_read_info(png_ptr
, info_ptr
);
1320 if (info_ptr
->height
> PNG_UINT_32_MAX
/png_sizeof(png_bytep
))
1321 png_error(png_ptr
,"Image is too high to process with png_read_png()");
1323 /* -------------- image transformations start here ------------------- */
1325 #if defined(PNG_READ_16_TO_8_SUPPORTED)
1326 /* tell libpng to strip 16 bit/color files down to 8 bits per color
1328 if (transforms
& PNG_TRANSFORM_STRIP_16
)
1329 png_set_strip_16(png_ptr
);
1332 #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1333 /* Strip alpha bytes from the input data without combining with
1334 * the background (not recommended).
1336 if (transforms
& PNG_TRANSFORM_STRIP_ALPHA
)
1337 png_set_strip_alpha(png_ptr
);
1340 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1341 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1342 * byte into separate bytes (useful for paletted and grayscale images).
1344 if (transforms
& PNG_TRANSFORM_PACKING
)
1345 png_set_packing(png_ptr
);
1348 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
1349 /* Change the order of packed pixels to least significant bit first
1350 * (not useful if you are using png_set_packing).
1352 if (transforms
& PNG_TRANSFORM_PACKSWAP
)
1353 png_set_packswap(png_ptr
);
1356 #if defined(PNG_READ_EXPAND_SUPPORTED)
1357 /* Expand paletted colors into true RGB triplets
1358 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1359 * Expand paletted or RGB images with transparency to full alpha
1360 * channels so the data will be available as RGBA quartets.
1362 if (transforms
& PNG_TRANSFORM_EXPAND
)
1363 if ((png_ptr
->bit_depth
< 8) ||
1364 (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
) ||
1365 (png_get_valid(png_ptr
, info_ptr
, PNG_INFO_tRNS
)))
1366 png_set_expand(png_ptr
);
1369 /* We don't handle background color or gamma transformation or dithering.
1372 #if defined(PNG_READ_INVERT_SUPPORTED)
1373 /* invert monochrome files to have 0 as white and 1 as black
1375 if (transforms
& PNG_TRANSFORM_INVERT_MONO
)
1376 png_set_invert_mono(png_ptr
);
1379 #if defined(PNG_READ_SHIFT_SUPPORTED)
1380 /* If you want to shift the pixel values from the range [0,255] or
1381 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1382 * colors were originally in:
1384 if ((transforms
& PNG_TRANSFORM_SHIFT
)
1385 && png_get_valid(png_ptr
, info_ptr
, PNG_INFO_sBIT
))
1387 png_color_8p sig_bit
;
1389 png_get_sBIT(png_ptr
, info_ptr
, &sig_bit
);
1390 png_set_shift(png_ptr
, sig_bit
);
1394 #if defined(PNG_READ_BGR_SUPPORTED)
1395 /* flip the RGB pixels to BGR (or RGBA to BGRA)
1397 if (transforms
& PNG_TRANSFORM_BGR
)
1398 png_set_bgr(png_ptr
);
1401 #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1402 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1404 if (transforms
& PNG_TRANSFORM_SWAP_ALPHA
)
1405 png_set_swap_alpha(png_ptr
);
1408 #if defined(PNG_READ_SWAP_SUPPORTED)
1409 /* swap bytes of 16 bit files to least significant byte first
1411 if (transforms
& PNG_TRANSFORM_SWAP_ENDIAN
)
1412 png_set_swap(png_ptr
);
1415 /* We don't handle adding filler bytes */
1417 /* Optional call to gamma correct and add the background to the palette
1418 * and update info structure. REQUIRED if you are expecting libpng to
1419 * update the palette for you (i.e., you selected such a transform above).
1421 png_read_update_info(png_ptr
, info_ptr
);
1423 /* -------------- image transformations end here ------------------- */
1425 #ifdef PNG_FREE_ME_SUPPORTED
1426 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ROWS
, 0);
1428 if(info_ptr
->row_pointers
== NULL
)
1430 info_ptr
->row_pointers
= (png_bytepp
)png_malloc(png_ptr
,
1431 info_ptr
->height
* png_sizeof(png_bytep
));
1432 #ifdef PNG_FREE_ME_SUPPORTED
1433 info_ptr
->free_me
|= PNG_FREE_ROWS
;
1435 for (row
= 0; row
< (int)info_ptr
->height
; row
++)
1437 info_ptr
->row_pointers
[row
] = (png_bytep
)png_malloc(png_ptr
,
1438 png_get_rowbytes(png_ptr
, info_ptr
));
1442 png_read_image(png_ptr
, info_ptr
->row_pointers
);
1443 info_ptr
->valid
|= PNG_INFO_IDAT
;
1445 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
1446 png_read_end(png_ptr
, info_ptr
);
1448 if(transforms
== 0 || params
== NULL
)
1449 /* quiet compiler warnings */ return;
1453 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */