2 /* pngread.c - read a PNG file
4 * Last changed in libpng 1.4.1 [February 25, 2010]
5 * Copyright (c) 1998-2010 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.
17 #define PNG_NO_PEDANTIC_WARNINGS
19 #ifdef PNG_READ_SUPPORTED
23 /* Create a PNG structure for reading, and allocate any memory needed. */
25 png_create_read_struct(png_const_charp user_png_ver
, png_voidp error_ptr
,
26 png_error_ptr error_fn
, png_error_ptr warn_fn
)
29 #ifdef PNG_USER_MEM_SUPPORTED
30 return (png_create_read_struct_2(user_png_ver
, error_ptr
, error_fn
,
31 warn_fn
, NULL
, NULL
, NULL
));
34 /* Alternate create PNG structure for reading, and allocate any memory
38 png_create_read_struct_2(png_const_charp user_png_ver
, png_voidp error_ptr
,
39 png_error_ptr error_fn
, png_error_ptr warn_fn
, png_voidp mem_ptr
,
40 png_malloc_ptr malloc_fn
, png_free_ptr free_fn
)
42 #endif /* PNG_USER_MEM_SUPPORTED */
44 #ifdef PNG_SETJMP_SUPPORTED
48 volatile int png_cleanup_needed
= 0;
50 #ifdef PNG_SETJMP_SUPPORTED
51 #ifdef USE_FAR_KEYWORD
58 png_debug(1, "in png_create_read_struct");
60 #ifdef PNG_USER_MEM_SUPPORTED
61 png_ptr
= (png_structp
)png_create_struct_2(PNG_STRUCT_PNG
,
64 png_ptr
= (png_structp
)png_create_struct(PNG_STRUCT_PNG
);
69 /* Added at libpng-1.2.6 */
70 #ifdef PNG_USER_LIMITS_SUPPORTED
71 png_ptr
->user_width_max
= PNG_USER_WIDTH_MAX
;
72 png_ptr
->user_height_max
= PNG_USER_HEIGHT_MAX
;
73 # ifdef PNG_USER_CHUNK_CACHE_MAX
74 /* Added at libpng-1.2.43 and 1.4.0 */
75 png_ptr
->user_chunk_cache_max
= PNG_USER_CHUNK_CACHE_MAX
;
77 # ifdef PNG_SET_USER_CHUNK_MALLOC_MAX
78 /* Added at libpng-1.2.43 and 1.4.1 */
79 png_ptr
->user_chunk_malloc_max
= PNG_USER_CHUNK_MALLOC_MAX
;
83 #ifdef PNG_SETJMP_SUPPORTED
84 /* Applications that neglect to set up their own setjmp() and then
85 encounter a png_error() will longjmp here. Since the jmpbuf is
86 then meaningless we abort instead of returning. */
87 #ifdef USE_FAR_KEYWORD
90 if (setjmp(png_jmpbuf(png_ptr
))) /* Sets longjmp to match setjmp */
93 #ifdef USE_FAR_KEYWORD
94 png_memcpy(png_jmpbuf(png_ptr
), jmpbuf
, png_sizeof(jmp_buf));
96 #endif /* PNG_SETJMP_SUPPORTED */
98 #ifdef PNG_USER_MEM_SUPPORTED
99 png_set_mem_fn(png_ptr
, mem_ptr
, malloc_fn
, free_fn
);
102 png_set_error_fn(png_ptr
, error_ptr
, error_fn
, warn_fn
);
109 if (user_png_ver
[i
] != png_libpng_ver
[i
])
110 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
111 } while (png_libpng_ver
[i
++]);
114 png_ptr
->flags
|= PNG_FLAG_LIBRARY_MISMATCH
;
117 if (png_ptr
->flags
& PNG_FLAG_LIBRARY_MISMATCH
)
119 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
120 * we must recompile any applications that use any older library version.
121 * For versions after libpng 1.0, we will be compatible, so we need
122 * only check the first digit.
124 if (user_png_ver
== NULL
|| user_png_ver
[0] != png_libpng_ver
[0] ||
125 (user_png_ver
[0] == '1' && user_png_ver
[2] != png_libpng_ver
[2]) ||
126 (user_png_ver
[0] == '0' && user_png_ver
[2] < '9'))
128 #ifdef PNG_STDIO_SUPPORTED
132 png_snprintf(msg
, 80,
133 "Application was compiled with png.h from libpng-%.20s",
135 png_warning(png_ptr
, msg
);
137 png_snprintf(msg
, 80,
138 "Application is running with png.c from libpng-%.20s",
140 png_warning(png_ptr
, msg
);
142 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
146 "Incompatible libpng version in application and library");
148 png_cleanup_needed
= 1;
152 if (!png_cleanup_needed
)
154 /* Initialize zbuf - compression buffer */
155 png_ptr
->zbuf_size
= PNG_ZBUF_SIZE
;
156 png_ptr
->zbuf
= (png_bytep
)png_malloc_warn(png_ptr
,
158 if (png_ptr
->zbuf
== NULL
)
159 png_cleanup_needed
= 1;
161 png_ptr
->zstream
.zalloc
= png_zalloc
;
162 png_ptr
->zstream
.zfree
= png_zfree
;
163 png_ptr
->zstream
.opaque
= (voidpf
)png_ptr
;
165 if (!png_cleanup_needed
)
167 switch (inflateInit(&png_ptr
->zstream
))
169 case Z_OK
: /* Do nothing */ break;
171 case Z_STREAM_ERROR
: png_warning(png_ptr
, "zlib memory error");
172 png_cleanup_needed
= 1; break;
173 case Z_VERSION_ERROR
: png_warning(png_ptr
, "zlib version error");
174 png_cleanup_needed
= 1; break;
175 default: png_warning(png_ptr
, "Unknown zlib error");
176 png_cleanup_needed
= 1;
180 if (png_cleanup_needed
)
182 /* Clean up PNG structure and deallocate any memory. */
183 png_free(png_ptr
, png_ptr
->zbuf
);
184 png_ptr
->zbuf
= NULL
;
185 #ifdef PNG_USER_MEM_SUPPORTED
186 png_destroy_struct_2((png_voidp
)png_ptr
,
187 (png_free_ptr
)free_fn
, (png_voidp
)mem_ptr
);
189 png_destroy_struct((png_voidp
)png_ptr
);
194 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
195 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
197 png_set_read_fn(png_ptr
, NULL
, NULL
);
204 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
205 /* Read the information before the actual image data. This has been
206 * changed in v0.90 to allow reading a file that already has the magic
207 * bytes read from the stream. You can tell libpng how many bytes have
208 * been read from the beginning of the stream (up to the maximum of 8)
209 * via png_set_sig_bytes(), and we will only check the remaining bytes
210 * here. The application can then have access to the signature bytes we
211 * read if it is determined that this isn't a valid PNG file.
214 png_read_info(png_structp png_ptr
, png_infop info_ptr
)
216 png_debug(1, "in png_read_info");
218 if (png_ptr
== NULL
|| info_ptr
== NULL
)
221 /* If we haven't checked all of the PNG signature bytes, do so now. */
222 if (png_ptr
->sig_bytes
< 8)
224 png_size_t num_checked
= png_ptr
->sig_bytes
,
225 num_to_check
= 8 - num_checked
;
227 #ifdef PNG_IO_STATE_SUPPORTED
228 png_ptr
->io_state
= PNG_IO_READING
| PNG_IO_SIGNATURE
;
231 png_read_data(png_ptr
, &(info_ptr
->signature
[num_checked
]), num_to_check
);
232 png_ptr
->sig_bytes
= 8;
234 if (png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
))
236 if (num_checked
< 4 &&
237 png_sig_cmp(info_ptr
->signature
, num_checked
, num_to_check
- 4))
238 png_error(png_ptr
, "Not a PNG file");
240 png_error(png_ptr
, "PNG file corrupted by ASCII conversion");
243 png_ptr
->mode
|= PNG_HAVE_PNG_SIGNATURE
;
252 #ifdef PNG_READ_bKGD_SUPPORTED
255 #ifdef PNG_READ_cHRM_SUPPORTED
258 #ifdef PNG_READ_gAMA_SUPPORTED
261 #ifdef PNG_READ_hIST_SUPPORTED
264 #ifdef PNG_READ_iCCP_SUPPORTED
267 #ifdef PNG_READ_iTXt_SUPPORTED
270 #ifdef PNG_READ_oFFs_SUPPORTED
273 #ifdef PNG_READ_pCAL_SUPPORTED
276 #ifdef PNG_READ_pHYs_SUPPORTED
279 #ifdef PNG_READ_sBIT_SUPPORTED
282 #ifdef PNG_READ_sCAL_SUPPORTED
285 #ifdef PNG_READ_sPLT_SUPPORTED
288 #ifdef PNG_READ_sRGB_SUPPORTED
291 #ifdef PNG_READ_tEXt_SUPPORTED
294 #ifdef PNG_READ_tIME_SUPPORTED
297 #ifdef PNG_READ_tRNS_SUPPORTED
300 #ifdef PNG_READ_zTXt_SUPPORTED
303 png_uint_32 length
= png_read_chunk_header(png_ptr
);
304 PNG_CONST png_bytep chunk_name
= png_ptr
->chunk_name
;
306 /* This should be a binary subdivision search or a hash for
307 * matching the chunk name rather than a linear search.
309 if (!png_memcmp(chunk_name
, png_IDAT
, 4))
310 if (png_ptr
->mode
& PNG_AFTER_IDAT
)
311 png_ptr
->mode
|= PNG_HAVE_CHUNK_AFTER_IDAT
;
313 if (!png_memcmp(chunk_name
, png_IHDR
, 4))
314 png_handle_IHDR(png_ptr
, info_ptr
, length
);
315 else if (!png_memcmp(chunk_name
, png_IEND
, 4))
316 png_handle_IEND(png_ptr
, info_ptr
, length
);
317 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
318 else if (png_handle_as_unknown(png_ptr
, chunk_name
))
320 if (!png_memcmp(chunk_name
, png_IDAT
, 4))
321 png_ptr
->mode
|= PNG_HAVE_IDAT
;
322 png_handle_unknown(png_ptr
, info_ptr
, length
);
323 if (!png_memcmp(chunk_name
, png_PLTE
, 4))
324 png_ptr
->mode
|= PNG_HAVE_PLTE
;
325 else if (!png_memcmp(chunk_name
, png_IDAT
, 4))
327 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
328 png_error(png_ptr
, "Missing IHDR before IDAT");
329 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
330 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
331 png_error(png_ptr
, "Missing PLTE before IDAT");
336 else if (!png_memcmp(chunk_name
, png_PLTE
, 4))
337 png_handle_PLTE(png_ptr
, info_ptr
, length
);
338 else if (!png_memcmp(chunk_name
, png_IDAT
, 4))
340 if (!(png_ptr
->mode
& PNG_HAVE_IHDR
))
341 png_error(png_ptr
, "Missing IHDR before IDAT");
342 else if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
343 !(png_ptr
->mode
& PNG_HAVE_PLTE
))
344 png_error(png_ptr
, "Missing PLTE before IDAT");
346 png_ptr
->idat_size
= length
;
347 png_ptr
->mode
|= PNG_HAVE_IDAT
;
350 #ifdef PNG_READ_bKGD_SUPPORTED
351 else if (!png_memcmp(chunk_name
, png_bKGD
, 4))
352 png_handle_bKGD(png_ptr
, info_ptr
, length
);
354 #ifdef PNG_READ_cHRM_SUPPORTED
355 else if (!png_memcmp(chunk_name
, png_cHRM
, 4))
356 png_handle_cHRM(png_ptr
, info_ptr
, length
);
358 #ifdef PNG_READ_gAMA_SUPPORTED
359 else if (!png_memcmp(chunk_name
, png_gAMA
, 4))
360 png_handle_gAMA(png_ptr
, info_ptr
, length
);
362 #ifdef PNG_READ_hIST_SUPPORTED
363 else if (!png_memcmp(chunk_name
, png_hIST
, 4))
364 png_handle_hIST(png_ptr
, info_ptr
, length
);
366 #ifdef PNG_READ_oFFs_SUPPORTED
367 else if (!png_memcmp(chunk_name
, png_oFFs
, 4))
368 png_handle_oFFs(png_ptr
, info_ptr
, length
);
370 #ifdef PNG_READ_pCAL_SUPPORTED
371 else if (!png_memcmp(chunk_name
, png_pCAL
, 4))
372 png_handle_pCAL(png_ptr
, info_ptr
, length
);
374 #ifdef PNG_READ_sCAL_SUPPORTED
375 else if (!png_memcmp(chunk_name
, png_sCAL
, 4))
376 png_handle_sCAL(png_ptr
, info_ptr
, length
);
378 #ifdef PNG_READ_pHYs_SUPPORTED
379 else if (!png_memcmp(chunk_name
, png_pHYs
, 4))
380 png_handle_pHYs(png_ptr
, info_ptr
, length
);
382 #ifdef PNG_READ_sBIT_SUPPORTED
383 else if (!png_memcmp(chunk_name
, png_sBIT
, 4))
384 png_handle_sBIT(png_ptr
, info_ptr
, length
);
386 #ifdef PNG_READ_sRGB_SUPPORTED
387 else if (!png_memcmp(chunk_name
, png_sRGB
, 4))
388 png_handle_sRGB(png_ptr
, info_ptr
, length
);
390 #ifdef PNG_READ_iCCP_SUPPORTED
391 else if (!png_memcmp(chunk_name
, png_iCCP
, 4))
392 png_handle_iCCP(png_ptr
, info_ptr
, length
);
394 #ifdef PNG_READ_sPLT_SUPPORTED
395 else if (!png_memcmp(chunk_name
, png_sPLT
, 4))
396 png_handle_sPLT(png_ptr
, info_ptr
, length
);
398 #ifdef PNG_READ_tEXt_SUPPORTED
399 else if (!png_memcmp(chunk_name
, png_tEXt
, 4))
400 png_handle_tEXt(png_ptr
, info_ptr
, length
);
402 #ifdef PNG_READ_tIME_SUPPORTED
403 else if (!png_memcmp(chunk_name
, png_tIME
, 4))
404 png_handle_tIME(png_ptr
, info_ptr
, length
);
406 #ifdef PNG_READ_tRNS_SUPPORTED
407 else if (!png_memcmp(chunk_name
, png_tRNS
, 4))
408 png_handle_tRNS(png_ptr
, info_ptr
, length
);
410 #ifdef PNG_READ_zTXt_SUPPORTED
411 else if (!png_memcmp(chunk_name
, png_zTXt
, 4))
412 png_handle_zTXt(png_ptr
, info_ptr
, length
);
414 #ifdef PNG_READ_iTXt_SUPPORTED
415 else if (!png_memcmp(chunk_name
, png_iTXt
, 4))
416 png_handle_iTXt(png_ptr
, info_ptr
, length
);
419 png_handle_unknown(png_ptr
, info_ptr
, length
);
422 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
424 /* Optional call to update the users info_ptr structure */
426 png_read_update_info(png_structp png_ptr
, png_infop info_ptr
)
428 png_debug(1, "in png_read_update_info");
432 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
433 png_read_start_row(png_ptr
);
436 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
438 png_read_transform_info(png_ptr
, info_ptr
);
441 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
442 /* Initialize palette, background, etc, after transformations
443 * are set, but before any reading takes place. This allows
444 * the user to obtain a gamma-corrected palette, for example.
445 * If the user doesn't call this, we will do it ourselves.
448 png_start_read_image(png_structp png_ptr
)
450 png_debug(1, "in png_start_read_image");
454 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
455 png_read_start_row(png_ptr
);
457 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
459 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
461 png_read_row(png_structp png_ptr
, png_bytep row
, png_bytep dsp_row
)
464 PNG_CONST
int png_pass_dsp_mask
[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
466 PNG_CONST
int png_pass_mask
[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
472 png_debug2(1, "in png_read_row (row %lu, pass %d)",
473 (unsigned long) png_ptr
->row_number
, png_ptr
->pass
);
475 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
476 png_read_start_row(png_ptr
);
477 if (png_ptr
->row_number
== 0 && png_ptr
->pass
== 0)
479 /* Check for transforms that have been set but were defined out */
480 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
481 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
482 png_warning(png_ptr
, "PNG_READ_INVERT_SUPPORTED is not defined");
484 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
485 if (png_ptr
->transformations
& PNG_FILLER
)
486 png_warning(png_ptr
, "PNG_READ_FILLER_SUPPORTED is not defined");
488 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
489 !defined(PNG_READ_PACKSWAP_SUPPORTED)
490 if (png_ptr
->transformations
& PNG_PACKSWAP
)
491 png_warning(png_ptr
, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
493 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
494 if (png_ptr
->transformations
& PNG_PACK
)
495 png_warning(png_ptr
, "PNG_READ_PACK_SUPPORTED is not defined");
497 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
498 if (png_ptr
->transformations
& PNG_SHIFT
)
499 png_warning(png_ptr
, "PNG_READ_SHIFT_SUPPORTED is not defined");
501 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
502 if (png_ptr
->transformations
& PNG_BGR
)
503 png_warning(png_ptr
, "PNG_READ_BGR_SUPPORTED is not defined");
505 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
506 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
507 png_warning(png_ptr
, "PNG_READ_SWAP_SUPPORTED is not defined");
511 #ifdef PNG_READ_INTERLACING_SUPPORTED
512 /* If interlaced and we do not need a new row, combine row and return */
513 if (png_ptr
->interlaced
&& (png_ptr
->transformations
& PNG_INTERLACE
))
515 switch (png_ptr
->pass
)
518 if (png_ptr
->row_number
& 0x07)
521 png_combine_row(png_ptr
, dsp_row
,
522 png_pass_dsp_mask
[png_ptr
->pass
]);
523 png_read_finish_row(png_ptr
);
528 if ((png_ptr
->row_number
& 0x07) || png_ptr
->width
< 5)
531 png_combine_row(png_ptr
, dsp_row
,
532 png_pass_dsp_mask
[png_ptr
->pass
]);
533 png_read_finish_row(png_ptr
);
538 if ((png_ptr
->row_number
& 0x07) != 4)
540 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 4))
541 png_combine_row(png_ptr
, dsp_row
,
542 png_pass_dsp_mask
[png_ptr
->pass
]);
543 png_read_finish_row(png_ptr
);
548 if ((png_ptr
->row_number
& 3) || png_ptr
->width
< 3)
551 png_combine_row(png_ptr
, dsp_row
,
552 png_pass_dsp_mask
[png_ptr
->pass
]);
553 png_read_finish_row(png_ptr
);
558 if ((png_ptr
->row_number
& 3) != 2)
560 if (dsp_row
!= NULL
&& (png_ptr
->row_number
& 2))
561 png_combine_row(png_ptr
, dsp_row
,
562 png_pass_dsp_mask
[png_ptr
->pass
]);
563 png_read_finish_row(png_ptr
);
568 if ((png_ptr
->row_number
& 1) || png_ptr
->width
< 2)
571 png_combine_row(png_ptr
, dsp_row
,
572 png_pass_dsp_mask
[png_ptr
->pass
]);
573 png_read_finish_row(png_ptr
);
578 if (!(png_ptr
->row_number
& 1))
580 png_read_finish_row(png_ptr
);
588 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
))
589 png_error(png_ptr
, "Invalid attempt to read row data");
591 png_ptr
->zstream
.next_out
= png_ptr
->row_buf
;
592 png_ptr
->zstream
.avail_out
=
593 (uInt
)(PNG_ROWBYTES(png_ptr
->pixel_depth
,
594 png_ptr
->iwidth
) + 1);
597 if (!(png_ptr
->zstream
.avail_in
))
599 while (!png_ptr
->idat_size
)
601 png_crc_finish(png_ptr
, 0);
603 png_ptr
->idat_size
= png_read_chunk_header(png_ptr
);
604 if (png_memcmp(png_ptr
->chunk_name
, png_IDAT
, 4))
605 png_error(png_ptr
, "Not enough image data");
607 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->zbuf_size
;
608 png_ptr
->zstream
.next_in
= png_ptr
->zbuf
;
609 if (png_ptr
->zbuf_size
> png_ptr
->idat_size
)
610 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->idat_size
;
611 png_crc_read(png_ptr
, png_ptr
->zbuf
,
612 (png_size_t
)png_ptr
->zstream
.avail_in
);
613 png_ptr
->idat_size
-= png_ptr
->zstream
.avail_in
;
615 ret
= inflate(&png_ptr
->zstream
, Z_PARTIAL_FLUSH
);
616 if (ret
== Z_STREAM_END
)
618 if (png_ptr
->zstream
.avail_out
|| png_ptr
->zstream
.avail_in
||
620 png_benign_error(png_ptr
, "Extra compressed data");
621 png_ptr
->mode
|= PNG_AFTER_IDAT
;
622 png_ptr
->flags
|= PNG_FLAG_ZLIB_FINISHED
;
626 png_error(png_ptr
, png_ptr
->zstream
.msg
? png_ptr
->zstream
.msg
:
627 "Decompression error");
629 } while (png_ptr
->zstream
.avail_out
);
631 png_ptr
->row_info
.color_type
= png_ptr
->color_type
;
632 png_ptr
->row_info
.width
= png_ptr
->iwidth
;
633 png_ptr
->row_info
.channels
= png_ptr
->channels
;
634 png_ptr
->row_info
.bit_depth
= png_ptr
->bit_depth
;
635 png_ptr
->row_info
.pixel_depth
= png_ptr
->pixel_depth
;
636 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
637 png_ptr
->row_info
.width
);
639 if (png_ptr
->row_buf
[0])
640 png_read_filter_row(png_ptr
, &(png_ptr
->row_info
),
641 png_ptr
->row_buf
+ 1, png_ptr
->prev_row
+ 1,
642 (int)(png_ptr
->row_buf
[0]));
644 png_memcpy(png_ptr
->prev_row
, png_ptr
->row_buf
, png_ptr
->rowbytes
+ 1);
646 #ifdef PNG_MNG_FEATURES_SUPPORTED
647 if ((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
648 (png_ptr
->filter_type
== PNG_INTRAPIXEL_DIFFERENCING
))
650 /* Intrapixel differencing */
651 png_do_read_intrapixel(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
656 if (png_ptr
->transformations
|| (png_ptr
->flags
&PNG_FLAG_STRIP_ALPHA
))
657 png_do_read_transformations(png_ptr
);
659 #ifdef PNG_READ_INTERLACING_SUPPORTED
660 /* Blow up interlaced rows to full size */
661 if (png_ptr
->interlaced
&&
662 (png_ptr
->transformations
& PNG_INTERLACE
))
664 if (png_ptr
->pass
< 6)
665 /* Old interface (pre-1.0.9):
666 * png_do_read_interlace(&(png_ptr->row_info),
667 * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
669 png_do_read_interlace(png_ptr
);
672 png_combine_row(png_ptr
, dsp_row
,
673 png_pass_dsp_mask
[png_ptr
->pass
]);
675 png_combine_row(png_ptr
, row
,
676 png_pass_mask
[png_ptr
->pass
]);
682 png_combine_row(png_ptr
, row
, 0xff);
684 png_combine_row(png_ptr
, dsp_row
, 0xff);
686 png_read_finish_row(png_ptr
);
688 if (png_ptr
->read_row_fn
!= NULL
)
689 (*(png_ptr
->read_row_fn
))(png_ptr
, png_ptr
->row_number
, png_ptr
->pass
);
691 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
693 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
694 /* Read one or more rows of image data. If the image is interlaced,
695 * and png_set_interlace_handling() has been called, the rows need to
696 * contain the contents of the rows from the previous pass. If the
697 * image has alpha or transparency, and png_handle_alpha()[*] has been
698 * called, the rows contents must be initialized to the contents of the
701 * "row" holds the actual image, and pixels are placed in it
702 * as they arrive. If the image is displayed after each pass, it will
703 * appear to "sparkle" in. "display_row" can be used to display a
704 * "chunky" progressive image, with finer detail added as it becomes
705 * available. If you do not want this "chunky" display, you may pass
706 * NULL for display_row. If you do not want the sparkle display, and
707 * you have not called png_handle_alpha(), you may pass NULL for rows.
708 * If you have called png_handle_alpha(), and the image has either an
709 * alpha channel or a transparency chunk, you must provide a buffer for
710 * rows. In this case, you do not have to provide a display_row buffer
711 * also, but you may. If the image is not interlaced, or if you have
712 * not called png_set_interlace_handling(), the display_row buffer will
713 * be ignored, so pass NULL to it.
715 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
719 png_read_rows(png_structp png_ptr
, png_bytepp row
,
720 png_bytepp display_row
, png_uint_32 num_rows
)
726 png_debug(1, "in png_read_rows");
732 if (rp
!= NULL
&& dp
!= NULL
)
733 for (i
= 0; i
< num_rows
; i
++)
735 png_bytep rptr
= *rp
++;
736 png_bytep dptr
= *dp
++;
738 png_read_row(png_ptr
, rptr
, dptr
);
741 for (i
= 0; i
< num_rows
; i
++)
743 png_bytep rptr
= *rp
;
744 png_read_row(png_ptr
, rptr
, NULL
);
748 for (i
= 0; i
< num_rows
; i
++)
750 png_bytep dptr
= *dp
;
751 png_read_row(png_ptr
, NULL
, dptr
);
755 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
757 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
758 /* Read the entire image. If the image has an alpha channel or a tRNS
759 * chunk, and you have called png_handle_alpha()[*], you will need to
760 * initialize the image to the current image that PNG will be overlaying.
761 * We set the num_rows again here, in case it was incorrectly set in
762 * png_read_start_row() by a call to png_read_update_info() or
763 * png_start_read_image() if png_set_interlace_handling() wasn't called
764 * prior to either of these functions like it should have been. You can
765 * only call this function once. If you desire to have an image for
766 * each pass of a interlaced image, use png_read_rows() instead.
768 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
771 png_read_image(png_structp png_ptr
, png_bytepp image
)
773 png_uint_32 i
, image_height
;
777 png_debug(1, "in png_read_image");
782 #ifdef PNG_READ_INTERLACING_SUPPORTED
783 pass
= png_set_interlace_handling(png_ptr
);
785 if (png_ptr
->interlaced
)
787 "Cannot read interlaced image -- interlace handler disabled");
792 image_height
=png_ptr
->height
;
793 png_ptr
->num_rows
= image_height
; /* Make sure this is set correctly */
795 for (j
= 0; j
< pass
; j
++)
798 for (i
= 0; i
< image_height
; i
++)
800 png_read_row(png_ptr
, *rp
, NULL
);
805 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
807 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
808 /* Read the end of the PNG file. Will not read past the end of the
809 * file, will verify the end is accurate, and will read any comments
810 * or time information at the end of the file, if info is not NULL.
813 png_read_end(png_structp png_ptr
, png_infop info_ptr
)
815 png_debug(1, "in png_read_end");
819 png_crc_finish(png_ptr
, 0); /* Finish off CRC from last IDAT chunk */
827 #ifdef PNG_READ_bKGD_SUPPORTED
830 #ifdef PNG_READ_cHRM_SUPPORTED
833 #ifdef PNG_READ_gAMA_SUPPORTED
836 #ifdef PNG_READ_hIST_SUPPORTED
839 #ifdef PNG_READ_iCCP_SUPPORTED
842 #ifdef PNG_READ_iTXt_SUPPORTED
845 #ifdef PNG_READ_oFFs_SUPPORTED
848 #ifdef PNG_READ_pCAL_SUPPORTED
851 #ifdef PNG_READ_pHYs_SUPPORTED
854 #ifdef PNG_READ_sBIT_SUPPORTED
857 #ifdef PNG_READ_sCAL_SUPPORTED
860 #ifdef PNG_READ_sPLT_SUPPORTED
863 #ifdef PNG_READ_sRGB_SUPPORTED
866 #ifdef PNG_READ_tEXt_SUPPORTED
869 #ifdef PNG_READ_tIME_SUPPORTED
872 #ifdef PNG_READ_tRNS_SUPPORTED
875 #ifdef PNG_READ_zTXt_SUPPORTED
878 png_uint_32 length
= png_read_chunk_header(png_ptr
);
879 PNG_CONST png_bytep chunk_name
= png_ptr
->chunk_name
;
881 if (!png_memcmp(chunk_name
, png_IHDR
, 4))
882 png_handle_IHDR(png_ptr
, info_ptr
, length
);
883 else if (!png_memcmp(chunk_name
, png_IEND
, 4))
884 png_handle_IEND(png_ptr
, info_ptr
, length
);
885 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
886 else if (png_handle_as_unknown(png_ptr
, chunk_name
))
888 if (!png_memcmp(chunk_name
, png_IDAT
, 4))
890 if ((length
> 0) || (png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
))
891 png_benign_error(png_ptr
, "Too many IDATs found");
893 png_handle_unknown(png_ptr
, info_ptr
, length
);
894 if (!png_memcmp(chunk_name
, png_PLTE
, 4))
895 png_ptr
->mode
|= PNG_HAVE_PLTE
;
898 else if (!png_memcmp(chunk_name
, png_IDAT
, 4))
900 /* Zero length IDATs are legal after the last IDAT has been
901 * read, but not after other chunks have been read.
903 if ((length
> 0) || (png_ptr
->mode
& PNG_HAVE_CHUNK_AFTER_IDAT
))
904 png_benign_error(png_ptr
, "Too many IDATs found");
905 png_crc_finish(png_ptr
, length
);
907 else if (!png_memcmp(chunk_name
, png_PLTE
, 4))
908 png_handle_PLTE(png_ptr
, info_ptr
, length
);
909 #ifdef PNG_READ_bKGD_SUPPORTED
910 else if (!png_memcmp(chunk_name
, png_bKGD
, 4))
911 png_handle_bKGD(png_ptr
, info_ptr
, length
);
913 #ifdef PNG_READ_cHRM_SUPPORTED
914 else if (!png_memcmp(chunk_name
, png_cHRM
, 4))
915 png_handle_cHRM(png_ptr
, info_ptr
, length
);
917 #ifdef PNG_READ_gAMA_SUPPORTED
918 else if (!png_memcmp(chunk_name
, png_gAMA
, 4))
919 png_handle_gAMA(png_ptr
, info_ptr
, length
);
921 #ifdef PNG_READ_hIST_SUPPORTED
922 else if (!png_memcmp(chunk_name
, png_hIST
, 4))
923 png_handle_hIST(png_ptr
, info_ptr
, length
);
925 #ifdef PNG_READ_oFFs_SUPPORTED
926 else if (!png_memcmp(chunk_name
, png_oFFs
, 4))
927 png_handle_oFFs(png_ptr
, info_ptr
, length
);
929 #ifdef PNG_READ_pCAL_SUPPORTED
930 else if (!png_memcmp(chunk_name
, png_pCAL
, 4))
931 png_handle_pCAL(png_ptr
, info_ptr
, length
);
933 #ifdef PNG_READ_sCAL_SUPPORTED
934 else if (!png_memcmp(chunk_name
, png_sCAL
, 4))
935 png_handle_sCAL(png_ptr
, info_ptr
, length
);
937 #ifdef PNG_READ_pHYs_SUPPORTED
938 else if (!png_memcmp(chunk_name
, png_pHYs
, 4))
939 png_handle_pHYs(png_ptr
, info_ptr
, length
);
941 #ifdef PNG_READ_sBIT_SUPPORTED
942 else if (!png_memcmp(chunk_name
, png_sBIT
, 4))
943 png_handle_sBIT(png_ptr
, info_ptr
, length
);
945 #ifdef PNG_READ_sRGB_SUPPORTED
946 else if (!png_memcmp(chunk_name
, png_sRGB
, 4))
947 png_handle_sRGB(png_ptr
, info_ptr
, length
);
949 #ifdef PNG_READ_iCCP_SUPPORTED
950 else if (!png_memcmp(chunk_name
, png_iCCP
, 4))
951 png_handle_iCCP(png_ptr
, info_ptr
, length
);
953 #ifdef PNG_READ_sPLT_SUPPORTED
954 else if (!png_memcmp(chunk_name
, png_sPLT
, 4))
955 png_handle_sPLT(png_ptr
, info_ptr
, length
);
957 #ifdef PNG_READ_tEXt_SUPPORTED
958 else if (!png_memcmp(chunk_name
, png_tEXt
, 4))
959 png_handle_tEXt(png_ptr
, info_ptr
, length
);
961 #ifdef PNG_READ_tIME_SUPPORTED
962 else if (!png_memcmp(chunk_name
, png_tIME
, 4))
963 png_handle_tIME(png_ptr
, info_ptr
, length
);
965 #ifdef PNG_READ_tRNS_SUPPORTED
966 else if (!png_memcmp(chunk_name
, png_tRNS
, 4))
967 png_handle_tRNS(png_ptr
, info_ptr
, length
);
969 #ifdef PNG_READ_zTXt_SUPPORTED
970 else if (!png_memcmp(chunk_name
, png_zTXt
, 4))
971 png_handle_zTXt(png_ptr
, info_ptr
, length
);
973 #ifdef PNG_READ_iTXt_SUPPORTED
974 else if (!png_memcmp(chunk_name
, png_iTXt
, 4))
975 png_handle_iTXt(png_ptr
, info_ptr
, length
);
978 png_handle_unknown(png_ptr
, info_ptr
, length
);
979 } while (!(png_ptr
->mode
& PNG_HAVE_IEND
));
981 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
983 /* Free all memory used by the read */
985 png_destroy_read_struct(png_structpp png_ptr_ptr
, png_infopp info_ptr_ptr
,
986 png_infopp end_info_ptr_ptr
)
988 png_structp png_ptr
= NULL
;
989 png_infop info_ptr
= NULL
, end_info_ptr
= NULL
;
990 #ifdef PNG_USER_MEM_SUPPORTED
991 png_free_ptr free_fn
= NULL
;
992 png_voidp mem_ptr
= NULL
;
995 png_debug(1, "in png_destroy_read_struct");
997 if (png_ptr_ptr
!= NULL
)
998 png_ptr
= *png_ptr_ptr
;
1002 #ifdef PNG_USER_MEM_SUPPORTED
1003 free_fn
= png_ptr
->free_fn
;
1004 mem_ptr
= png_ptr
->mem_ptr
;
1007 if (info_ptr_ptr
!= NULL
)
1008 info_ptr
= *info_ptr_ptr
;
1010 if (end_info_ptr_ptr
!= NULL
)
1011 end_info_ptr
= *end_info_ptr_ptr
;
1013 png_read_destroy(png_ptr
, info_ptr
, end_info_ptr
);
1015 if (info_ptr
!= NULL
)
1017 #ifdef PNG_TEXT_SUPPORTED
1018 png_free_data(png_ptr
, info_ptr
, PNG_FREE_TEXT
, -1);
1021 #ifdef PNG_USER_MEM_SUPPORTED
1022 png_destroy_struct_2((png_voidp
)info_ptr
, (png_free_ptr
)free_fn
,
1023 (png_voidp
)mem_ptr
);
1025 png_destroy_struct((png_voidp
)info_ptr
);
1027 *info_ptr_ptr
= NULL
;
1030 if (end_info_ptr
!= NULL
)
1032 #ifdef PNG_READ_TEXT_SUPPORTED
1033 png_free_data(png_ptr
, end_info_ptr
, PNG_FREE_TEXT
, -1);
1035 #ifdef PNG_USER_MEM_SUPPORTED
1036 png_destroy_struct_2((png_voidp
)end_info_ptr
, (png_free_ptr
)free_fn
,
1037 (png_voidp
)mem_ptr
);
1039 png_destroy_struct((png_voidp
)end_info_ptr
);
1041 *end_info_ptr_ptr
= NULL
;
1044 if (png_ptr
!= NULL
)
1046 #ifdef PNG_USER_MEM_SUPPORTED
1047 png_destroy_struct_2((png_voidp
)png_ptr
, (png_free_ptr
)free_fn
,
1048 (png_voidp
)mem_ptr
);
1050 png_destroy_struct((png_voidp
)png_ptr
);
1052 *png_ptr_ptr
= NULL
;
1056 /* Free all memory used by the read (old method) */
1058 png_read_destroy(png_structp png_ptr
, png_infop info_ptr
,
1059 png_infop end_info_ptr
)
1061 #ifdef PNG_SETJMP_SUPPORTED
1064 png_error_ptr error_fn
;
1065 png_error_ptr warning_fn
;
1066 png_voidp error_ptr
;
1067 #ifdef PNG_USER_MEM_SUPPORTED
1068 png_free_ptr free_fn
;
1071 png_debug(1, "in png_read_destroy");
1073 if (info_ptr
!= NULL
)
1074 png_info_destroy(png_ptr
, info_ptr
);
1076 if (end_info_ptr
!= NULL
)
1077 png_info_destroy(png_ptr
, end_info_ptr
);
1079 png_free(png_ptr
, png_ptr
->zbuf
);
1080 png_free(png_ptr
, png_ptr
->big_row_buf
);
1081 png_free(png_ptr
, png_ptr
->prev_row
);
1082 png_free(png_ptr
, png_ptr
->chunkdata
);
1083 #ifdef PNG_READ_QUANTIZE_SUPPORTED
1084 png_free(png_ptr
, png_ptr
->palette_lookup
);
1085 png_free(png_ptr
, png_ptr
->quantize_index
);
1087 #ifdef PNG_READ_GAMMA_SUPPORTED
1088 png_free(png_ptr
, png_ptr
->gamma_table
);
1090 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1091 png_free(png_ptr
, png_ptr
->gamma_from_1
);
1092 png_free(png_ptr
, png_ptr
->gamma_to_1
);
1094 if (png_ptr
->free_me
& PNG_FREE_PLTE
)
1095 png_zfree(png_ptr
, png_ptr
->palette
);
1096 png_ptr
->free_me
&= ~PNG_FREE_PLTE
;
1097 #if defined(PNG_tRNS_SUPPORTED) || \
1098 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1099 if (png_ptr
->free_me
& PNG_FREE_TRNS
)
1100 png_free(png_ptr
, png_ptr
->trans_alpha
);
1101 png_ptr
->free_me
&= ~PNG_FREE_TRNS
;
1103 #ifdef PNG_READ_hIST_SUPPORTED
1104 if (png_ptr
->free_me
& PNG_FREE_HIST
)
1105 png_free(png_ptr
, png_ptr
->hist
);
1106 png_ptr
->free_me
&= ~PNG_FREE_HIST
;
1108 #ifdef PNG_READ_GAMMA_SUPPORTED
1109 if (png_ptr
->gamma_16_table
!= NULL
)
1112 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1113 for (i
= 0; i
< istop
; i
++)
1115 png_free(png_ptr
, png_ptr
->gamma_16_table
[i
]);
1117 png_free(png_ptr
, png_ptr
->gamma_16_table
);
1119 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1120 if (png_ptr
->gamma_16_from_1
!= NULL
)
1123 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1124 for (i
= 0; i
< istop
; i
++)
1126 png_free(png_ptr
, png_ptr
->gamma_16_from_1
[i
]);
1128 png_free(png_ptr
, png_ptr
->gamma_16_from_1
);
1130 if (png_ptr
->gamma_16_to_1
!= NULL
)
1133 int istop
= (1 << (8 - png_ptr
->gamma_shift
));
1134 for (i
= 0; i
< istop
; i
++)
1136 png_free(png_ptr
, png_ptr
->gamma_16_to_1
[i
]);
1138 png_free(png_ptr
, png_ptr
->gamma_16_to_1
);
1142 #ifdef PNG_TIME_RFC1123_SUPPORTED
1143 png_free(png_ptr
, png_ptr
->time_buffer
);
1146 inflateEnd(&png_ptr
->zstream
);
1147 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1148 png_free(png_ptr
, png_ptr
->save_buffer
);
1151 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1152 #ifdef PNG_TEXT_SUPPORTED
1153 png_free(png_ptr
, png_ptr
->current_text
);
1154 #endif /* PNG_TEXT_SUPPORTED */
1155 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1157 /* Save the important info out of the png_struct, in case it is
1160 #ifdef PNG_SETJMP_SUPPORTED
1161 png_memcpy(tmp_jmp
, png_ptr
->jmpbuf
, png_sizeof(jmp_buf));
1164 error_fn
= png_ptr
->error_fn
;
1165 warning_fn
= png_ptr
->warning_fn
;
1166 error_ptr
= png_ptr
->error_ptr
;
1167 #ifdef PNG_USER_MEM_SUPPORTED
1168 free_fn
= png_ptr
->free_fn
;
1171 png_memset(png_ptr
, 0, png_sizeof(png_struct
));
1173 png_ptr
->error_fn
= error_fn
;
1174 png_ptr
->warning_fn
= warning_fn
;
1175 png_ptr
->error_ptr
= error_ptr
;
1176 #ifdef PNG_USER_MEM_SUPPORTED
1177 png_ptr
->free_fn
= free_fn
;
1180 #ifdef PNG_SETJMP_SUPPORTED
1181 png_memcpy(png_ptr
->jmpbuf
, tmp_jmp
, png_sizeof(jmp_buf));
1187 png_set_read_status_fn(png_structp png_ptr
, png_read_status_ptr read_row_fn
)
1189 if (png_ptr
== NULL
)
1191 png_ptr
->read_row_fn
= read_row_fn
;
1195 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1196 #ifdef PNG_INFO_IMAGE_SUPPORTED
1198 png_read_png(png_structp png_ptr
, png_infop info_ptr
,
1204 if (png_ptr
== NULL
)
1207 /* png_read_info() gives us all of the information from the
1208 * PNG file before the first IDAT (image data chunk).
1210 png_read_info(png_ptr
, info_ptr
);
1211 if (info_ptr
->height
> PNG_UINT_32_MAX
/png_sizeof(png_bytep
))
1212 png_error(png_ptr
, "Image is too high to process with png_read_png()");
1214 /* -------------- image transformations start here ------------------- */
1216 #ifdef PNG_READ_16_TO_8_SUPPORTED
1217 /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
1219 if (transforms
& PNG_TRANSFORM_STRIP_16
)
1220 png_set_strip_16(png_ptr
);
1223 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1224 /* Strip alpha bytes from the input data without combining with
1225 * the background (not recommended).
1227 if (transforms
& PNG_TRANSFORM_STRIP_ALPHA
)
1228 png_set_strip_alpha(png_ptr
);
1231 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1232 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1233 * byte into separate bytes (useful for paletted and grayscale images).
1235 if (transforms
& PNG_TRANSFORM_PACKING
)
1236 png_set_packing(png_ptr
);
1239 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1240 /* Change the order of packed pixels to least significant bit first
1241 * (not useful if you are using png_set_packing).
1243 if (transforms
& PNG_TRANSFORM_PACKSWAP
)
1244 png_set_packswap(png_ptr
);
1247 #ifdef PNG_READ_EXPAND_SUPPORTED
1248 /* Expand paletted colors into true RGB triplets
1249 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1250 * Expand paletted or RGB images with transparency to full alpha
1251 * channels so the data will be available as RGBA quartets.
1253 if (transforms
& PNG_TRANSFORM_EXPAND
)
1254 if ((png_ptr
->bit_depth
< 8) ||
1255 (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
) ||
1256 (png_get_valid(png_ptr
, info_ptr
, PNG_INFO_tRNS
)))
1257 png_set_expand(png_ptr
);
1260 /* We don't handle background color or gamma transformation or quantizing.
1263 #ifdef PNG_READ_INVERT_SUPPORTED
1264 /* Invert monochrome files to have 0 as white and 1 as black
1266 if (transforms
& PNG_TRANSFORM_INVERT_MONO
)
1267 png_set_invert_mono(png_ptr
);
1270 #ifdef PNG_READ_SHIFT_SUPPORTED
1271 /* If you want to shift the pixel values from the range [0,255] or
1272 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1273 * colors were originally in:
1275 if ((transforms
& PNG_TRANSFORM_SHIFT
)
1276 && png_get_valid(png_ptr
, info_ptr
, PNG_INFO_sBIT
))
1278 png_color_8p sig_bit
;
1280 png_get_sBIT(png_ptr
, info_ptr
, &sig_bit
);
1281 png_set_shift(png_ptr
, sig_bit
);
1285 #ifdef PNG_READ_BGR_SUPPORTED
1286 /* Flip the RGB pixels to BGR (or RGBA to BGRA)
1288 if (transforms
& PNG_TRANSFORM_BGR
)
1289 png_set_bgr(png_ptr
);
1292 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1293 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1295 if (transforms
& PNG_TRANSFORM_SWAP_ALPHA
)
1296 png_set_swap_alpha(png_ptr
);
1299 #ifdef PNG_READ_SWAP_SUPPORTED
1300 /* Swap bytes of 16 bit files to least significant byte first
1302 if (transforms
& PNG_TRANSFORM_SWAP_ENDIAN
)
1303 png_set_swap(png_ptr
);
1306 /* Added at libpng-1.2.41 */
1307 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1308 /* Invert the alpha channel from opacity to transparency
1310 if (transforms
& PNG_TRANSFORM_INVERT_ALPHA
)
1311 png_set_invert_alpha(png_ptr
);
1314 /* Added at libpng-1.2.41 */
1315 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1316 /* Expand grayscale image to RGB
1318 if (transforms
& PNG_TRANSFORM_GRAY_TO_RGB
)
1319 png_set_gray_to_rgb(png_ptr
);
1322 /* We don't handle adding filler bytes */
1324 /* Optional call to gamma correct and add the background to the palette
1325 * and update info structure. REQUIRED if you are expecting libpng to
1326 * update the palette for you (i.e., you selected such a transform above).
1328 png_read_update_info(png_ptr
, info_ptr
);
1330 /* -------------- image transformations end here ------------------- */
1332 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ROWS
, 0);
1333 if (info_ptr
->row_pointers
== NULL
)
1337 info_ptr
->row_pointers
= (png_bytepp
)png_malloc(png_ptr
,
1338 info_ptr
->height
* png_sizeof(png_bytep
));
1339 for (iptr
=0; iptr
<info_ptr
->height
; iptr
++)
1340 info_ptr
->row_pointers
[iptr
] = NULL
;
1342 info_ptr
->free_me
|= PNG_FREE_ROWS
;
1344 for (row
= 0; row
< (int)info_ptr
->height
; row
++)
1345 info_ptr
->row_pointers
[row
] = (png_bytep
)png_malloc(png_ptr
,
1346 png_get_rowbytes(png_ptr
, info_ptr
));
1349 png_read_image(png_ptr
, info_ptr
->row_pointers
);
1350 info_ptr
->valid
|= PNG_INFO_IDAT
;
1352 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1353 png_read_end(png_ptr
, info_ptr
);
1355 transforms
= transforms
; /* Quiet compiler warnings */
1359 #endif /* PNG_INFO_IMAGE_SUPPORTED */
1360 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
1361 #endif /* PNG_READ_SUPPORTED */