2 /* pngread.c - read a PNG file
4 * libpng 1.2.5rc3 - September 18, 2002
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2002 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 #ifdef PNG_SETJMP_SUPPORTED
63 #ifdef USE_FAR_KEYWORD
66 if (setjmp(png_ptr
->jmpbuf
))
69 png_free(png_ptr
, png_ptr
->zbuf
);
71 #ifdef PNG_USER_MEM_SUPPORTED
72 png_destroy_struct_2((png_voidp
)png_ptr
,
73 (png_free_ptr
)free_fn
, (png_voidp
)mem_ptr
);
75 png_destroy_struct((png_voidp
)png_ptr
);
79 #ifdef USE_FAR_KEYWORD
80 png_memcpy(png_ptr
->jmpbuf
,jmpbuf
,sizeof(jmp_buf));
84 #ifdef PNG_USER_MEM_SUPPORTED
85 png_set_mem_fn(png_ptr
, mem_ptr
, malloc_fn
, free_fn
);
88 png_set_error_fn(png_ptr
, error_ptr
, error_fn
, warn_fn
);
93 if(user_png_ver
[i
] != png_libpng_ver
[i
])
94 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
95 } while (png_libpng_ver
[i
++]);
97 if (png_ptr
->flags
& PNG_FLAG_LIBRARY_MISMATCH
)
99 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
100 * we must recompile any applications that use any older library version.
101 * For versions after libpng 1.0, we will be compatible, so we need
102 * only check the first digit.
104 if (user_png_ver
== NULL
|| user_png_ver
[0] != png_libpng_ver
[0] ||
105 (user_png_ver
[0] == '1' && user_png_ver
[2] != png_libpng_ver
[2]) ||
106 (user_png_ver
[0] == '0' && user_png_ver
[2] < '9'))
108 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
112 sprintf(msg
, "Application was compiled with png.h from libpng-%.20s",
114 png_warning(png_ptr
, msg
);
116 sprintf(msg
, "Application is running with png.c from libpng-%.20s",
118 png_warning(png_ptr
, msg
);
120 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
124 "Incompatible libpng version in application and library");
128 /* initialize zbuf - compression buffer */
129 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
130 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
131 (png_uint_32
)png_ptr
->zbuf_size
);
132 png_ptr
->zstream
.zalloc
= png_zalloc
;
133 png_ptr
->zstream
.zfree
= png_zfree
;
134 png_ptr
->zstream
.opaque
= (voidpf
)png_ptr
;
136 switch (inflateInit(&png_ptr
->zstream
))
138 case Z_OK
: /* Do nothing */ break;
140 case Z_STREAM_ERROR
: png_error(png_ptr
, "zlib memory error"); break;
141 case Z_VERSION_ERROR
: png_error(png_ptr
, "zlib version error"); break;
142 default: png_error(png_ptr
, "Unknown zlib error");
145 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
146 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
148 png_set_read_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
);
150 #ifdef PNG_SETJMP_SUPPORTED
151 /* Applications that neglect to set up their own setjmp() and then encounter
152 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
153 abort instead of returning. */
154 #ifdef USE_FAR_KEYWORD
157 png_memcpy(png_ptr
->jmpbuf
,jmpbuf
,sizeof(jmp_buf));
159 if (setjmp(png_ptr
->jmpbuf
))
166 /* Initialize PNG structure for reading, and allocate any memory needed.
167 This interface is deprecated in favour of the png_create_read_struct(),
168 and it will eventually disappear. */
171 png_read_init(png_structp png_ptr
)
173 /* We only come here via pre-1.0.7-compiled applications */
174 png_read_init_2(png_ptr
, "1.0.6 or earlier", 0, 0);
178 png_read_init_2(png_structp png_ptr
, png_const_charp user_png_ver
,
179 png_size_t png_struct_size
, png_size_t png_info_size
)
181 /* We only come here via pre-1.0.12-compiled applications */
182 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
183 if(sizeof(png_struct
) > png_struct_size
|| sizeof(png_info
) > png_info_size
)
186 png_ptr
->warning_fn
=NULL
;
189 sprintf(msg
, "Application was compiled with png.h from libpng-%.20s",
191 png_warning(png_ptr
, msg
);
193 sprintf(msg
, "Application is running with png.c from libpng-%.20s",
195 png_warning(png_ptr
, msg
);
198 if(sizeof(png_struct
) > png_struct_size
)
200 png_ptr
->error_fn
=NULL
;
201 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
205 "The png struct allocated by the application for reading is too small.");
207 if(sizeof(png_info
) > png_info_size
)
209 png_ptr
->error_fn
=NULL
;
210 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
214 "The info struct allocated by application for reading is too small.");
216 png_read_init_3(&png_ptr
, user_png_ver
, png_struct_size
);
220 png_read_init_3(png_structpp ptr_ptr
, png_const_charp user_png_ver
,
221 png_size_t png_struct_size
)
223 #ifdef PNG_SETJMP_SUPPORTED
224 jmp_buf tmp_jmp
; /* to save current jump buffer */
229 png_structp png_ptr
=*ptr_ptr
;
233 if(user_png_ver
[i
] != png_libpng_ver
[i
])
235 #ifdef PNG_LEGACY_SUPPORTED
236 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
238 png_ptr
->warning_fn
=NULL
;
240 "Application uses deprecated png_read_init() and should be recompiled.");
244 } while (png_libpng_ver
[i
++]);
246 png_debug(1, "in png_read_init_3\n");
248 #ifdef PNG_SETJMP_SUPPORTED
249 /* save jump buffer and error functions */
250 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, sizeof (jmp_buf));
253 if(sizeof(png_struct
) > png_struct_size
)
255 png_destroy_struct(png_ptr
);
256 *ptr_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
260 /* reset all variables to 0 */
261 png_memset(png_ptr
, 0, sizeof (png_struct
));
263 #ifdef PNG_SETJMP_SUPPORTED
264 /* restore jump buffer */
265 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, sizeof (jmp_buf));
268 /* initialize zbuf - compression buffer */
269 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
270 png_ptr
->zbuf
= (png_bytep
)png_malloc(png_ptr
,
271 (png_uint_32
)png_ptr
->zbuf_size
);
272 png_ptr
->zstream
.zalloc
= png_zalloc
;
273 png_ptr
->zstream
.zfree
= png_zfree
;
274 png_ptr
->zstream
.opaque
= (voidpf
)png_ptr
;
276 switch (inflateInit(&png_ptr
->zstream
))
278 case Z_OK
: /* Do nothing */ break;
280 case Z_STREAM_ERROR
: png_error(png_ptr
, "zlib memory"); break;
281 case Z_VERSION_ERROR
: png_error(png_ptr
, "zlib version"); break;
282 default: png_error(png_ptr
, "Unknown zlib error");
285 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
286 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
288 png_set_read_fn(png_ptr
, png_voidp_NULL
, png_rw_ptr_NULL
);
291 /* Read the information before the actual image data. This has been
292 * changed in v0.90 to allow reading a file that already has the magic
293 * bytes read from the stream. You can tell libpng how many bytes have
294 * been read from the beginning of the stream (up to the maximum of 8)
295 * via png_set_sig_bytes(), and we will only check the remaining bytes
296 * here. The application can then have access to the signature bytes we
297 * read if it is determined that this isn't a valid PNG file.
300 png_read_info(png_structp png_ptr
, png_infop info_ptr
)
302 png_debug(1, "in png_read_info\n");
303 /* If we haven't checked all of the PNG signature bytes, do so now. */
304 if (png_ptr
->sig_bytes
< 8)
306 png_size_t num_checked
= png_ptr
->sig_bytes
,
307 num_to_check
= 8 - num_checked
;
309 png_read_data(png_ptr
, &(info_ptr
->signature
[num_checked
]), num_to_check
);
310 png_ptr
->sig_bytes
= 8;
312 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
314 if (num_checked
< 4 &&
315 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
316 png_error(png_ptr
, "Not a PNG file");
318 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
321 png_ptr
->mode
|= PNG_HAVE_PNG_SIGNATURE
;
326 #ifdef PNG_USE_LOCAL_ARRAYS
331 #if defined(PNG_READ_bKGD_SUPPORTED)
334 #if defined(PNG_READ_cHRM_SUPPORTED)
337 #if defined(PNG_READ_gAMA_SUPPORTED)
340 #if defined(PNG_READ_hIST_SUPPORTED)
343 #if defined(PNG_READ_iCCP_SUPPORTED)
346 #if defined(PNG_READ_iTXt_SUPPORTED)
349 #if defined(PNG_READ_oFFs_SUPPORTED)
352 #if defined(PNG_READ_pCAL_SUPPORTED)
355 #if defined(PNG_READ_pHYs_SUPPORTED)
358 #if defined(PNG_READ_sBIT_SUPPORTED)
361 #if defined(PNG_READ_sCAL_SUPPORTED)
364 #if defined(PNG_READ_sPLT_SUPPORTED)
367 #if defined(PNG_READ_sRGB_SUPPORTED)
370 #if defined(PNG_READ_tEXt_SUPPORTED)
373 #if defined(PNG_READ_tIME_SUPPORTED)
376 #if defined(PNG_READ_tRNS_SUPPORTED)
379 #if defined(PNG_READ_zTXt_SUPPORTED)
382 #endif /* PNG_GLOBAL_ARRAYS */
383 png_byte chunk_length
[4];
386 png_read_data(png_ptr
, chunk_length
, 4);
387 length
= png_get_uint_32(chunk_length
);
389 png_reset_crc(png_ptr
);
390 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
392 png_debug2(0, "Reading %s chunk, length=%lu.\n", png_ptr
->chunk_name
,
395 if (length
> PNG_MAX_UINT
)
396 png_error(png_ptr
, "Invalid chunk length.");
398 /* This should be a binary subdivision search or a hash for
399 * matching the chunk name rather than a linear search.
401 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
402 png_handle_IHDR(png_ptr
, info_ptr
, length
);
403 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
404 png_handle_IEND(png_ptr
, info_ptr
, length
);
405 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
406 else if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
))
408 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
409 png_ptr
->mode
|= PNG_HAVE_IDAT
;
410 png_handle_unknown(png_ptr
, info_ptr
, length
);
411 if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
412 png_ptr
->mode
|= PNG_HAVE_PLTE
;
413 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
415 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
416 png_error(png_ptr
, "Missing IHDR before IDAT");
417 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
418 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
419 png_error(png_ptr
, "Missing PLTE before IDAT");
424 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
425 png_handle_PLTE(png_ptr
, info_ptr
, length
);
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");
434 png_ptr
->idat_size
= length
;
435 png_ptr
->mode
|= PNG_HAVE_IDAT
;
438 #if defined(PNG_READ_bKGD_SUPPORTED)
439 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
440 png_handle_bKGD(png_ptr
, info_ptr
, length
);
442 #if defined(PNG_READ_cHRM_SUPPORTED)
443 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
444 png_handle_cHRM(png_ptr
, info_ptr
, length
);
446 #if defined(PNG_READ_gAMA_SUPPORTED)
447 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
448 png_handle_gAMA(png_ptr
, info_ptr
, length
);
450 #if defined(PNG_READ_hIST_SUPPORTED)
451 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
452 png_handle_hIST(png_ptr
, info_ptr
, length
);
454 #if defined(PNG_READ_oFFs_SUPPORTED)
455 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
456 png_handle_oFFs(png_ptr
, info_ptr
, length
);
458 #if defined(PNG_READ_pCAL_SUPPORTED)
459 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
460 png_handle_pCAL(png_ptr
, info_ptr
, length
);
462 #if defined(PNG_READ_sCAL_SUPPORTED)
463 else if (!png_memcmp(png_ptr
->chunk_name
, png_sCAL
, 4))
464 png_handle_sCAL(png_ptr
, info_ptr
, length
);
466 #if defined(PNG_READ_pHYs_SUPPORTED)
467 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
468 png_handle_pHYs(png_ptr
, info_ptr
, length
);
470 #if defined(PNG_READ_sBIT_SUPPORTED)
471 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
472 png_handle_sBIT(png_ptr
, info_ptr
, length
);
474 #if defined(PNG_READ_sRGB_SUPPORTED)
475 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
476 png_handle_sRGB(png_ptr
, info_ptr
, length
);
478 #if defined(PNG_READ_iCCP_SUPPORTED)
479 else if (!png_memcmp(png_ptr
->chunk_name
, png_iCCP
, 4))
480 png_handle_iCCP(png_ptr
, info_ptr
, length
);
482 #if defined(PNG_READ_sPLT_SUPPORTED)
483 else if (!png_memcmp(png_ptr
->chunk_name
, png_sPLT
, 4))
484 png_handle_sPLT(png_ptr
, info_ptr
, length
);
486 #if defined(PNG_READ_tEXt_SUPPORTED)
487 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
488 png_handle_tEXt(png_ptr
, info_ptr
, length
);
490 #if defined(PNG_READ_tIME_SUPPORTED)
491 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
492 png_handle_tIME(png_ptr
, info_ptr
, length
);
494 #if defined(PNG_READ_tRNS_SUPPORTED)
495 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
496 png_handle_tRNS(png_ptr
, info_ptr
, length
);
498 #if defined(PNG_READ_zTXt_SUPPORTED)
499 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
500 png_handle_zTXt(png_ptr
, info_ptr
, length
);
502 #if defined(PNG_READ_iTXt_SUPPORTED)
503 else if (!png_memcmp(png_ptr
->chunk_name
, png_iTXt
, 4))
504 png_handle_iTXt(png_ptr
, info_ptr
, length
);
507 png_handle_unknown(png_ptr
, info_ptr
, length
);
511 /* optional call to update the users info_ptr structure */
513 png_read_update_info(png_structp png_ptr
, png_infop info_ptr
)
515 png_debug(1, "in png_read_update_info\n");
516 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
517 png_read_start_row(png_ptr
);
520 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
521 png_read_transform_info(png_ptr
, info_ptr
);
524 /* Initialize palette, background, etc, after transformations
525 * are set, but before any reading takes place. This allows
526 * the user to obtain a gamma-corrected palette, for example.
527 * If the user doesn't call this, we will do it ourselves.
530 png_start_read_image(png_structp png_ptr
)
532 png_debug(1, "in png_start_read_image\n");
533 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
534 png_read_start_row(png_ptr
);
538 png_read_row(png_structp png_ptr
, png_bytep row
, png_bytep dsp_row
)
540 #ifdef PNG_USE_LOCAL_ARRAYS
542 const int png_pass_dsp_mask
[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
543 const int png_pass_mask
[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
546 png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
547 png_ptr
->row_number
, png_ptr
->pass
);
548 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
549 png_read_start_row(png_ptr
);
550 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
552 /* check for transforms that have been set but were defined out */
553 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
554 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
555 png_warning(png_ptr
, "PNG_READ_INVERT_SUPPORTED is not defined.");
557 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
558 if (png_ptr
->transformations
& PNG_FILLER
)
559 png_warning(png_ptr
, "PNG_READ_FILLER_SUPPORTED is not defined.");
561 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
562 if (png_ptr
->transformations
& PNG_PACKSWAP
)
563 png_warning(png_ptr
, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
565 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
566 if (png_ptr
->transformations
& PNG_PACK
)
567 png_warning(png_ptr
, "PNG_READ_PACK_SUPPORTED is not defined.");
569 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
570 if (png_ptr
->transformations
& PNG_SHIFT
)
571 png_warning(png_ptr
, "PNG_READ_SHIFT_SUPPORTED is not defined.");
573 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
574 if (png_ptr
->transformations
& PNG_BGR
)
575 png_warning(png_ptr
, "PNG_READ_BGR_SUPPORTED is not defined.");
577 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
578 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
579 png_warning(png_ptr
, "PNG_READ_SWAP_SUPPORTED is not defined.");
583 #if defined(PNG_READ_INTERLACING_SUPPORTED)
584 /* if interlaced and we do not need a new row, combine row and return */
585 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
587 switch (png_ptr
->pass
)
590 if (png_ptr
->row_number
& 0x07)
593 png_combine_row(png_ptr
, dsp_row
,
594 png_pass_dsp_mask
[png_ptr
->pass
]);
595 png_read_finish_row(png_ptr
);
600 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
603 png_combine_row(png_ptr
, dsp_row
,
604 png_pass_dsp_mask
[png_ptr
->pass
]);
605 png_read_finish_row(png_ptr
);
610 if ((png_ptr
->row_number
& 0x07) != 4)
612 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 4))
613 png_combine_row(png_ptr
, dsp_row
,
614 png_pass_dsp_mask
[png_ptr
->pass
]);
615 png_read_finish_row(png_ptr
);
620 if ((png_ptr
->row_number
& 3) || png_ptr
->width
< 3)
623 png_combine_row(png_ptr
, dsp_row
,
624 png_pass_dsp_mask
[png_ptr
->pass
]);
625 png_read_finish_row(png_ptr
);
630 if ((png_ptr
->row_number
& 3) != 2)
632 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 2))
633 png_combine_row(png_ptr
, dsp_row
,
634 png_pass_dsp_mask
[png_ptr
->pass
]);
635 png_read_finish_row(png_ptr
);
640 if ((png_ptr
->row_number
& 1) || png_ptr
->width
< 2)
643 png_combine_row(png_ptr
, dsp_row
,
644 png_pass_dsp_mask
[png_ptr
->pass
]);
645 png_read_finish_row(png_ptr
);
650 if (!(png_ptr
->row_number
& 1))
652 png_read_finish_row(png_ptr
);
660 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
))
661 png_error(png_ptr
, "Invalid attempt to read row data");
663 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
664 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->irowbytes
;
667 if (!(png_ptr
->zstream
.avail_in
))
669 while (!png_ptr
->idat_size
)
671 png_byte chunk_length
[4];
673 png_crc_finish(png_ptr
, 0);
675 png_read_data(png_ptr
, chunk_length
, 4);
676 png_ptr
->idat_size
= png_get_uint_32(chunk_length
);
678 if (png_ptr
->idat_size
> PNG_MAX_UINT
)
679 png_error(png_ptr
, "Invalid chunk length.");
681 png_reset_crc(png_ptr
);
682 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
683 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
684 png_error(png_ptr
, "Not enough image data");
686 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->zbuf_size
;
687 png_ptr
->zstream
.next_in
= png_ptr
->zbuf
;
688 if (png_ptr
->zbuf_size
> png_ptr
->idat_size
)
689 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->idat_size
;
690 png_crc_read(png_ptr
, png_ptr
->zbuf
,
691 (png_size_t
)png_ptr
->zstream
.avail_in
);
692 png_ptr
->idat_size
-= png_ptr
->zstream
.avail_in
;
694 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
695 if (ret
== Z_STREAM_END
)
697 if (png_ptr
->zstream
.avail_out
|| png_ptr
->zstream
.avail_in
||
699 png_error(png_ptr
, "Extra compressed data");
700 png_ptr
->mode
|= PNG_AFTER_IDAT
;
701 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
705 png_error(png_ptr
, png_ptr
->zstream
.msg
? png_ptr
->zstream
.msg
:
706 "Decompression error");
708 } while (png_ptr
->zstream
.avail_out
);
710 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
711 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
712 png_ptr
->row_info
.channels
= png_ptr
->channels
;
713 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
714 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
715 png_ptr
->row_info
.rowbytes
= ((png_ptr
->row_info
.width
*
716 (png_uint_32
)png_ptr
->row_info
.pixel_depth
+ 7) >> 3);
718 if(png_ptr
->row_buf
[0])
719 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
720 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
721 (int)(png_ptr
->row_buf
[0]));
723 png_memcpy_check(png_ptr
, png_ptr
->prev_row
, png_ptr
->row_buf
,
724 png_ptr
->rowbytes
+ 1);
726 #if defined(PNG_MNG_FEATURES_SUPPORTED)
727 if((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
728 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
730 /* Intrapixel differencing */
731 png_do_read_intrapixel(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
735 if (png_ptr
->transformations
)
736 png_do_read_transformations(png_ptr
);
738 #if defined(PNG_READ_INTERLACING_SUPPORTED)
739 /* blow up interlaced rows to full size */
740 if (png_ptr
->interlaced
&&
741 (png_ptr
->transformations
& PNG_INTERLACE
))
743 if (png_ptr
->pass
< 6)
744 /* old interface (pre-1.0.9):
745 png_do_read_interlace(&(png_ptr->row_info),
746 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
748 png_do_read_interlace(png_ptr
);
751 png_combine_row(png_ptr
, dsp_row
,
752 png_pass_dsp_mask
[png_ptr
->pass
]);
754 png_combine_row(png_ptr
, row
,
755 png_pass_mask
[png_ptr
->pass
]);
761 png_combine_row(png_ptr
, row
, 0xff);
763 png_combine_row(png_ptr
, dsp_row
, 0xff);
765 png_read_finish_row(png_ptr
);
767 if (png_ptr
->read_row_fn
!= NULL
)
768 (*(png_ptr
->read_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
771 /* Read one or more rows of image data. If the image is interlaced,
772 * and png_set_interlace_handling() has been called, the rows need to
773 * contain the contents of the rows from the previous pass. If the
774 * image has alpha or transparency, and png_handle_alpha()[*] has been
775 * called, the rows contents must be initialized to the contents of the
778 * "row" holds the actual image, and pixels are placed in it
779 * as they arrive. If the image is displayed after each pass, it will
780 * appear to "sparkle" in. "display_row" can be used to display a
781 * "chunky" progressive image, with finer detail added as it becomes
782 * available. If you do not want this "chunky" display, you may pass
783 * NULL for display_row. If you do not want the sparkle display, and
784 * you have not called png_handle_alpha(), you may pass NULL for rows.
785 * If you have called png_handle_alpha(), and the image has either an
786 * alpha channel or a transparency chunk, you must provide a buffer for
787 * rows. In this case, you do not have to provide a display_row buffer
788 * also, but you may. If the image is not interlaced, or if you have
789 * not called png_set_interlace_handling(), the display_row buffer will
790 * be ignored, so pass NULL to it.
792 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.2.5rc3
796 png_read_rows(png_structp png_ptr
, png_bytepp row
,
797 png_bytepp display_row
, png_uint_32 num_rows
)
803 png_debug(1, "in png_read_rows\n");
806 if (rp
!= NULL
&& dp
!= NULL
)
807 for (i
= 0; i
< num_rows
; i
++)
809 png_bytep rptr
= *rp
++;
810 png_bytep dptr
= *dp
++;
812 png_read_row(png_ptr
, rptr
, dptr
);
815 for (i
= 0; i
< num_rows
; i
++)
817 png_bytep rptr
= *rp
;
818 png_read_row(png_ptr
, rptr
, png_bytep_NULL
);
822 for (i
= 0; i
< num_rows
; i
++)
824 png_bytep dptr
= *dp
;
825 png_read_row(png_ptr
, png_bytep_NULL
, dptr
);
830 /* Read the entire image. If the image has an alpha channel or a tRNS
831 * chunk, and you have called png_handle_alpha()[*], you will need to
832 * initialize the image to the current image that PNG will be overlaying.
833 * We set the num_rows again here, in case it was incorrectly set in
834 * png_read_start_row() by a call to png_read_update_info() or
835 * png_start_read_image() if png_set_interlace_handling() wasn't called
836 * prior to either of these functions like it should have been. You can
837 * only call this function once. If you desire to have an image for
838 * each pass of a interlaced image, use png_read_rows() instead.
840 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.2.5rc3
843 png_read_image(png_structp png_ptr
, png_bytepp image
)
845 png_uint_32 i
,image_height
;
849 png_debug(1, "in png_read_image\n");
851 #ifdef PNG_READ_INTERLACING_SUPPORTED
852 pass
= png_set_interlace_handling(png_ptr
);
854 if (png_ptr
->interlaced
)
856 "Cannot read interlaced image -- interlace handler disabled.");
861 image_height
=png_ptr
->height
;
862 png_ptr
->num_rows
= image_height
; /* Make sure this is set correctly */
864 for (j
= 0; j
< pass
; j
++)
867 for (i
= 0; i
< image_height
; i
++)
869 png_read_row(png_ptr
, *rp
, png_bytep_NULL
);
875 /* Read the end of the PNG file. Will not read past the end of the
876 * file, will verify the end is accurate, and will read any comments
877 * or time information at the end of the file, if info is not NULL.
880 png_read_end(png_structp png_ptr
, png_infop info_ptr
)
882 png_byte chunk_length
[4];
885 png_debug(1, "in png_read_end\n");
886 png_crc_finish(png_ptr
, 0); /* Finish off CRC from last IDAT chunk */
890 #ifdef PNG_USE_LOCAL_ARRAYS
895 #if defined(PNG_READ_bKGD_SUPPORTED)
898 #if defined(PNG_READ_cHRM_SUPPORTED)
901 #if defined(PNG_READ_gAMA_SUPPORTED)
904 #if defined(PNG_READ_hIST_SUPPORTED)
907 #if defined(PNG_READ_iCCP_SUPPORTED)
910 #if defined(PNG_READ_iTXt_SUPPORTED)
913 #if defined(PNG_READ_oFFs_SUPPORTED)
916 #if defined(PNG_READ_pCAL_SUPPORTED)
919 #if defined(PNG_READ_pHYs_SUPPORTED)
922 #if defined(PNG_READ_sBIT_SUPPORTED)
925 #if defined(PNG_READ_sCAL_SUPPORTED)
928 #if defined(PNG_READ_sPLT_SUPPORTED)
931 #if defined(PNG_READ_sRGB_SUPPORTED)
934 #if defined(PNG_READ_tEXt_SUPPORTED)
937 #if defined(PNG_READ_tIME_SUPPORTED)
940 #if defined(PNG_READ_tRNS_SUPPORTED)
943 #if defined(PNG_READ_zTXt_SUPPORTED)
946 #endif /* PNG_GLOBAL_ARRAYS */
948 png_read_data(png_ptr
, chunk_length
, 4);
949 length
= png_get_uint_32(chunk_length
);
951 png_reset_crc(png_ptr
);
952 png_crc_read(png_ptr
, png_ptr
->chunk_name
, 4);
954 png_debug1(0, "Reading %s chunk.\n", png_ptr
->chunk_name
);
956 if (length
> PNG_MAX_UINT
)
957 png_error(png_ptr
, "Invalid chunk length.");
959 if (!png_memcmp(png_ptr
->chunk_name
, png_IHDR
, 4))
960 png_handle_IHDR(png_ptr
, info_ptr
, length
);
961 else if (!png_memcmp(png_ptr
->chunk_name
, png_IEND
, 4))
962 png_handle_IEND(png_ptr
, info_ptr
, length
);
963 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
964 else if (png_handle_as_unknown(png_ptr
, png_ptr
->chunk_name
))
966 if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
968 if (length
> 0 || png_ptr
->mode
& PNG_AFTER_IDAT
)
969 png_error(png_ptr
, "Too many IDAT's found");
972 png_ptr
->mode
|= PNG_AFTER_IDAT
;
973 png_handle_unknown(png_ptr
, info_ptr
, length
);
974 if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
975 png_ptr
->mode
|= PNG_HAVE_PLTE
;
978 else if (!png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
980 /* Zero length IDATs are legal after the last IDAT has been
981 * read, but not after other chunks have been read.
983 if (length
> 0 || png_ptr
->mode
& PNG_AFTER_IDAT
)
984 png_error(png_ptr
, "Too many IDAT's found");
985 png_crc_finish(png_ptr
, length
);
987 else if (!png_memcmp(png_ptr
->chunk_name
, png_PLTE
, 4))
988 png_handle_PLTE(png_ptr
, info_ptr
, length
);
989 #if defined(PNG_READ_bKGD_SUPPORTED)
990 else if (!png_memcmp(png_ptr
->chunk_name
, png_bKGD
, 4))
991 png_handle_bKGD(png_ptr
, info_ptr
, length
);
993 #if defined(PNG_READ_cHRM_SUPPORTED)
994 else if (!png_memcmp(png_ptr
->chunk_name
, png_cHRM
, 4))
995 png_handle_cHRM(png_ptr
, info_ptr
, length
);
997 #if defined(PNG_READ_gAMA_SUPPORTED)
998 else if (!png_memcmp(png_ptr
->chunk_name
, png_gAMA
, 4))
999 png_handle_gAMA(png_ptr
, info_ptr
, length
);
1001 #if defined(PNG_READ_hIST_SUPPORTED)
1002 else if (!png_memcmp(png_ptr
->chunk_name
, png_hIST
, 4))
1003 png_handle_hIST(png_ptr
, info_ptr
, length
);
1005 #if defined(PNG_READ_oFFs_SUPPORTED)
1006 else if (!png_memcmp(png_ptr
->chunk_name
, png_oFFs
, 4))
1007 png_handle_oFFs(png_ptr
, info_ptr
, length
);
1009 #if defined(PNG_READ_pCAL_SUPPORTED)
1010 else if (!png_memcmp(png_ptr
->chunk_name
, png_pCAL
, 4))
1011 png_handle_pCAL(png_ptr
, info_ptr
, length
);
1013 #if defined(PNG_READ_sCAL_SUPPORTED)
1014 else if (!png_memcmp(png_ptr
->chunk_name
, png_sCAL
, 4))
1015 png_handle_sCAL(png_ptr
, info_ptr
, length
);
1017 #if defined(PNG_READ_pHYs_SUPPORTED)
1018 else if (!png_memcmp(png_ptr
->chunk_name
, png_pHYs
, 4))
1019 png_handle_pHYs(png_ptr
, info_ptr
, length
);
1021 #if defined(PNG_READ_sBIT_SUPPORTED)
1022 else if (!png_memcmp(png_ptr
->chunk_name
, png_sBIT
, 4))
1023 png_handle_sBIT(png_ptr
, info_ptr
, length
);
1025 #if defined(PNG_READ_sRGB_SUPPORTED)
1026 else if (!png_memcmp(png_ptr
->chunk_name
, png_sRGB
, 4))
1027 png_handle_sRGB(png_ptr
, info_ptr
, length
);
1029 #if defined(PNG_READ_iCCP_SUPPORTED)
1030 else if (!png_memcmp(png_ptr
->chunk_name
, png_iCCP
, 4))
1031 png_handle_iCCP(png_ptr
, info_ptr
, length
);
1033 #if defined(PNG_READ_sPLT_SUPPORTED)
1034 else if (!png_memcmp(png_ptr
->chunk_name
, png_sPLT
, 4))
1035 png_handle_sPLT(png_ptr
, info_ptr
, length
);
1037 #if defined(PNG_READ_tEXt_SUPPORTED)
1038 else if (!png_memcmp(png_ptr
->chunk_name
, png_tEXt
, 4))
1039 png_handle_tEXt(png_ptr
, info_ptr
, length
);
1041 #if defined(PNG_READ_tIME_SUPPORTED)
1042 else if (!png_memcmp(png_ptr
->chunk_name
, png_tIME
, 4))
1043 png_handle_tIME(png_ptr
, info_ptr
, length
);
1045 #if defined(PNG_READ_tRNS_SUPPORTED)
1046 else if (!png_memcmp(png_ptr
->chunk_name
, png_tRNS
, 4))
1047 png_handle_tRNS(png_ptr
, info_ptr
, length
);
1049 #if defined(PNG_READ_zTXt_SUPPORTED)
1050 else if (!png_memcmp(png_ptr
->chunk_name
, png_zTXt
, 4))
1051 png_handle_zTXt(png_ptr
, info_ptr
, length
);
1053 #if defined(PNG_READ_iTXt_SUPPORTED)
1054 else if (!png_memcmp(png_ptr
->chunk_name
, png_iTXt
, 4))
1055 png_handle_iTXt(png_ptr
, info_ptr
, length
);
1058 png_handle_unknown(png_ptr
, info_ptr
, length
);
1059 } while (!(png_ptr
->mode
& PNG_HAVE_IEND
));
1062 /* free all memory used by the read */
1064 png_destroy_read_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
,
1065 png_infopp end_info_ptr_ptr
)
1067 png_structp png_ptr
= NULL
;
1068 png_infop info_ptr
= NULL
, end_info_ptr
= NULL
;
1069 #ifdef PNG_USER_MEM_SUPPORTED
1070 png_free_ptr free_fn
= NULL
;
1071 png_voidp mem_ptr
= NULL
;
1074 png_debug(1, "in png_destroy_read_struct\n");
1075 if (png_ptr_ptr
!= NULL
)
1076 png_ptr
= *png_ptr_ptr
;
1078 if (info_ptr_ptr
!= NULL
)
1079 info_ptr
= *info_ptr_ptr
;
1081 if (end_info_ptr_ptr
!= NULL
)
1082 end_info_ptr
= *end_info_ptr_ptr
;
1084 #ifdef PNG_USER_MEM_SUPPORTED
1085 free_fn
= png_ptr
->free_fn
;
1086 mem_ptr
= png_ptr
->mem_ptr
;
1089 png_read_destroy(png_ptr
, info_ptr
, end_info_ptr
);
1091 if (info_ptr
!= NULL
)
1093 #if defined(PNG_TEXT_SUPPORTED)
1094 png_free_data(png_ptr
, info_ptr
, PNG_FREE_TEXT
, -1);
1097 #ifdef PNG_USER_MEM_SUPPORTED
1098 png_destroy_struct_2((png_voidp
)info_ptr
, (png_free_ptr
)free_fn
,
1099 (png_voidp
)mem_ptr
);
1101 png_destroy_struct((png_voidp
)info_ptr
);
1103 *info_ptr_ptr
= NULL
;
1106 if (end_info_ptr
!= NULL
)
1108 #if defined(PNG_READ_TEXT_SUPPORTED)
1109 png_free_data(png_ptr
, end_info_ptr
, PNG_FREE_TEXT
, -1);
1111 #ifdef PNG_USER_MEM_SUPPORTED
1112 png_destroy_struct_2((png_voidp
)end_info_ptr
, (png_free_ptr
)free_fn
,
1113 (png_voidp
)mem_ptr
);
1115 png_destroy_struct((png_voidp
)end_info_ptr
);
1117 *end_info_ptr_ptr
= NULL
;
1120 if (png_ptr
!= NULL
)
1122 #ifdef PNG_USER_MEM_SUPPORTED
1123 png_destroy_struct_2((png_voidp
)png_ptr
, (png_free_ptr
)free_fn
,
1124 (png_voidp
)mem_ptr
);
1126 png_destroy_struct((png_voidp
)png_ptr
);
1128 *png_ptr_ptr
= NULL
;
1132 /* free all memory used by the read (old method) */
1134 png_read_destroy(png_structp png_ptr
, png_infop info_ptr
, png_infop end_info_ptr
)
1136 #ifdef PNG_SETJMP_SUPPORTED
1139 png_error_ptr error_fn
;
1140 png_error_ptr warning_fn
;
1141 png_voidp error_ptr
;
1142 #ifdef PNG_USER_MEM_SUPPORTED
1143 png_free_ptr free_fn
;
1146 png_debug(1, "in png_read_destroy\n");
1147 if (info_ptr
!= NULL
)
1148 png_info_destroy(png_ptr
, info_ptr
);
1150 if (end_info_ptr
!= NULL
)
1151 png_info_destroy(png_ptr
, end_info_ptr
);
1153 png_free(png_ptr
, png_ptr
->zbuf
);
1154 png_free(png_ptr
, png_ptr
->big_row_buf
);
1155 png_free(png_ptr
, png_ptr
->prev_row
);
1156 #if defined(PNG_READ_DITHER_SUPPORTED)
1157 png_free(png_ptr
, png_ptr
->palette_lookup
);
1158 png_free(png_ptr
, png_ptr
->dither_index
);
1160 #if defined(PNG_READ_GAMMA_SUPPORTED)
1161 png_free(png_ptr
, png_ptr
->gamma_table
);
1163 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1164 png_free(png_ptr
, png_ptr
->gamma_from_1
);
1165 png_free(png_ptr
, png_ptr
->gamma_to_1
);
1167 #ifdef PNG_FREE_ME_SUPPORTED
1168 if (png_ptr
->free_me
& PNG_FREE_PLTE
)
1169 png_zfree(png_ptr
, png_ptr
->palette
);
1170 png_ptr
->free_me
&= ~PNG_FREE_PLTE
;
1172 if (png_ptr
->flags
& PNG_FLAG_FREE_PLTE
)
1173 png_zfree(png_ptr
, png_ptr
->palette
);
1174 png_ptr
->flags
&= ~PNG_FLAG_FREE_PLTE
;
1176 #if defined(PNG_tRNS_SUPPORTED) || \
1177 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1178 #ifdef PNG_FREE_ME_SUPPORTED
1179 if (png_ptr
->free_me
& PNG_FREE_TRNS
)
1180 png_free(png_ptr
, png_ptr
->trans
);
1181 png_ptr
->free_me
&= ~PNG_FREE_TRNS
;
1183 if (png_ptr
->flags
& PNG_FLAG_FREE_TRNS
)
1184 png_free(png_ptr
, png_ptr
->trans
);
1185 png_ptr
->flags
&= ~PNG_FLAG_FREE_TRNS
;
1188 #if defined(PNG_READ_hIST_SUPPORTED)
1189 #ifdef PNG_FREE_ME_SUPPORTED
1190 if (png_ptr
->free_me
& PNG_FREE_HIST
)
1191 png_free(png_ptr
, png_ptr
->hist
);
1192 png_ptr
->free_me
&= ~PNG_FREE_HIST
;
1194 if (png_ptr
->flags
& PNG_FLAG_FREE_HIST
)
1195 png_free(png_ptr
, png_ptr
->hist
);
1196 png_ptr
->flags
&= ~PNG_FLAG_FREE_HIST
;
1199 #if defined(PNG_READ_GAMMA_SUPPORTED)
1200 if (png_ptr
->gamma_16_table
!= NULL
)
1203 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1204 for (i
= 0; i
< istop
; i
++)
1206 png_free(png_ptr
, png_ptr
->gamma_16_table
[i
]);
1208 png_free(png_ptr
, png_ptr
->gamma_16_table
);
1210 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1211 if (png_ptr
->gamma_16_from_1
!= NULL
)
1214 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1215 for (i
= 0; i
< istop
; i
++)
1217 png_free(png_ptr
, png_ptr
->gamma_16_from_1
[i
]);
1219 png_free(png_ptr
, png_ptr
->gamma_16_from_1
);
1221 if (png_ptr
->gamma_16_to_1
!= NULL
)
1224 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1225 for (i
= 0; i
< istop
; i
++)
1227 png_free(png_ptr
, png_ptr
->gamma_16_to_1
[i
]);
1229 png_free(png_ptr
, png_ptr
->gamma_16_to_1
);
1233 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1234 png_free(png_ptr
, png_ptr
->time_buffer
);
1237 inflateEnd(&png_ptr
->zstream
);
1238 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1239 png_free(png_ptr
, png_ptr
->save_buffer
);
1242 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1243 #ifdef PNG_TEXT_SUPPORTED
1244 png_free(png_ptr
, png_ptr
->current_text
);
1245 #endif /* PNG_TEXT_SUPPORTED */
1246 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1248 /* Save the important info out of the png_struct, in case it is
1251 #ifdef PNG_SETJMP_SUPPORTED
1252 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, sizeof (jmp_buf));
1255 error_fn
= png_ptr
->error_fn
;
1256 warning_fn
= png_ptr
->warning_fn
;
1257 error_ptr
= png_ptr
->error_ptr
;
1258 #ifdef PNG_USER_MEM_SUPPORTED
1259 free_fn
= png_ptr
->free_fn
;
1262 png_memset(png_ptr
, 0, sizeof (png_struct
));
1264 png_ptr
->error_fn
= error_fn
;
1265 png_ptr
->warning_fn
= warning_fn
;
1266 png_ptr
->error_ptr
= error_ptr
;
1267 #ifdef PNG_USER_MEM_SUPPORTED
1268 png_ptr
->free_fn
= free_fn
;
1271 #ifdef PNG_SETJMP_SUPPORTED
1272 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, sizeof (jmp_buf));
1278 png_set_read_status_fn(png_structp png_ptr
, png_read_status_ptr read_row_fn
)
1280 png_ptr
->read_row_fn
= read_row_fn
;
1283 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1285 png_read_png(png_structp png_ptr
, png_infop info_ptr
,
1291 #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1292 /* invert the alpha channel from opacity to transparency */
1293 if (transforms
& PNG_TRANSFORM_INVERT_ALPHA
)
1294 png_set_invert_alpha(png_ptr
);
1297 /* The call to png_read_info() gives us all of the information from the
1298 * PNG file before the first IDAT (image data chunk).
1300 png_read_info(png_ptr
, info_ptr
);
1302 /* -------------- image transformations start here ------------------- */
1304 #if defined(PNG_READ_16_TO_8_SUPPORTED)
1305 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
1306 if (transforms
& PNG_TRANSFORM_STRIP_16
)
1307 png_set_strip_16(png_ptr
);
1310 #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1311 /* Strip alpha bytes from the input data without combining with the
1312 * background (not recommended).
1314 if (transforms
& PNG_TRANSFORM_STRIP_ALPHA
)
1315 png_set_strip_alpha(png_ptr
);
1318 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1319 /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
1320 * byte into separate bytes (useful for paletted and grayscale images).
1322 if (transforms
& PNG_TRANSFORM_PACKING
)
1323 png_set_packing(png_ptr
);
1326 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
1327 /* Change the order of packed pixels to least significant bit first
1328 * (not useful if you are using png_set_packing). */
1329 if (transforms
& PNG_TRANSFORM_PACKSWAP
)
1330 png_set_packswap(png_ptr
);
1333 #if defined(PNG_READ_EXPAND_SUPPORTED)
1334 /* Expand paletted colors into true RGB triplets
1335 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1336 * Expand paletted or RGB images with transparency to full alpha
1337 * channels so the data will be available as RGBA quartets.
1339 if (transforms
& PNG_TRANSFORM_EXPAND
)
1340 if ((png_ptr
->bit_depth
< 8) ||
1341 (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
) ||
1342 (png_get_valid(png_ptr
, info_ptr
, PNG_INFO_tRNS
)))
1343 png_set_expand(png_ptr
);
1346 /* We don't handle background color or gamma transformation or dithering. */
1348 #if defined(PNG_READ_INVERT_SUPPORTED)
1349 /* invert monochrome files to have 0 as white and 1 as black */
1350 if (transforms
& PNG_TRANSFORM_INVERT_MONO
)
1351 png_set_invert_mono(png_ptr
);
1354 #if defined(PNG_READ_SHIFT_SUPPORTED)
1355 /* If you want to shift the pixel values from the range [0,255] or
1356 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1357 * colors were originally in:
1359 if ((transforms
& PNG_TRANSFORM_SHIFT
)
1360 && png_get_valid(png_ptr
, info_ptr
, PNG_INFO_sBIT
))
1362 png_color_8p sig_bit
;
1364 png_get_sBIT(png_ptr
, info_ptr
, &sig_bit
);
1365 png_set_shift(png_ptr
, sig_bit
);
1369 #if defined(PNG_READ_BGR_SUPPORTED)
1370 /* flip the RGB pixels to BGR (or RGBA to BGRA) */
1371 if (transforms
& PNG_TRANSFORM_BGR
)
1372 png_set_bgr(png_ptr
);
1375 #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1376 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
1377 if (transforms
& PNG_TRANSFORM_SWAP_ALPHA
)
1378 png_set_swap_alpha(png_ptr
);
1381 #if defined(PNG_READ_SWAP_SUPPORTED)
1382 /* swap bytes of 16 bit files to least significant byte first */
1383 if (transforms
& PNG_TRANSFORM_SWAP_ENDIAN
)
1384 png_set_swap(png_ptr
);
1387 /* We don't handle adding filler bytes */
1389 /* Optional call to gamma correct and add the background to the palette
1390 * and update info structure. REQUIRED if you are expecting libpng to
1391 * update the palette for you (i.e., you selected such a transform above).
1393 png_read_update_info(png_ptr
, info_ptr
);
1395 /* -------------- image transformations end here ------------------- */
1397 #ifdef PNG_FREE_ME_SUPPORTED
1398 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ROWS
, 0);
1400 if(info_ptr
->row_pointers
== NULL
)
1402 info_ptr
->row_pointers
= (png_bytepp
)png_malloc(png_ptr
,
1403 info_ptr
->height
* sizeof(png_bytep
));
1404 #ifdef PNG_FREE_ME_SUPPORTED
1405 info_ptr
->free_me
|= PNG_FREE_ROWS
;
1407 for (row
= 0; row
< (int)info_ptr
->height
; row
++)
1409 info_ptr
->row_pointers
[row
] = (png_bytep
)png_malloc(png_ptr
,
1410 png_get_rowbytes(png_ptr
, info_ptr
));
1414 png_read_image(png_ptr
, info_ptr
->row_pointers
);
1415 info_ptr
->valid
|= PNG_INFO_IDAT
;
1417 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
1418 png_read_end(png_ptr
, info_ptr
);
1420 if(transforms
== 0 || params
== NULL
)
1421 /* quiet compiler warnings */ return;