2 /* pngread.c - read a PNG file
4 * Last changed in libpng 1.4.0 [January 3, 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 needed. */
36 png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
37 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
38 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
40 #endif /* PNG_USER_MEM_SUPPORTED */
42 #ifdef PNG_SETJMP_SUPPORTED
46 volatile int png_cleanup_needed = 0;
48 #ifdef PNG_SETJMP_SUPPORTED
49 #ifdef USE_FAR_KEYWORD
56 png_debug(1, "in png_create_read_struct");
58 #ifdef PNG_USER_MEM_SUPPORTED
59 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
62 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
67 /* Added at libpng-1.2.6 */
68 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
69 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
70 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
71 /* Added at libpng-1.4.0 */
72 png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
75 #ifdef PNG_SETJMP_SUPPORTED
76 /* Applications that neglect to set up their own setjmp() and then
77 encounter a png_error() will longjmp here. Since the jmpbuf is
78 then meaningless we abort instead of returning. */
79 #ifdef USE_FAR_KEYWORD
82 if (setjmp(png_jmpbuf(png_ptr))) /* Sets longjmp to match setjmp */
85 #ifdef USE_FAR_KEYWORD
86 png_memcpy(png_jmpbuf(png_ptr), jmpbuf, png_sizeof(jmp_buf));
88 #endif /* PNG_SETJMP_SUPPORTED */
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);
101 if (user_png_ver[i] != png_libpng_ver[i])
102 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
103 } while (png_libpng_ver[i++]);
106 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
109 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
111 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
112 * we must recompile any applications that use any older library version.
113 * For versions after libpng 1.0, we will be compatible, so we need
114 * only check the first digit.
116 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
117 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
118 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
120 #ifdef PNG_STDIO_SUPPORTED
124 png_snprintf(msg, 80,
125 "Application was compiled with png.h from libpng-%.20s",
127 png_warning(png_ptr, msg);
129 png_snprintf(msg, 80,
130 "Application is running with png.c from libpng-%.20s",
132 png_warning(png_ptr, msg);
134 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
138 "Incompatible libpng version in application and library");
140 png_cleanup_needed = 1;
144 if (!png_cleanup_needed)
146 /* Initialize zbuf - compression buffer */
147 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
148 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
150 if (png_ptr->zbuf == NULL)
151 png_cleanup_needed = 1;
153 png_ptr->zstream.zalloc = png_zalloc;
154 png_ptr->zstream.zfree = png_zfree;
155 png_ptr->zstream.opaque = (voidpf)png_ptr;
157 if (!png_cleanup_needed)
159 switch (inflateInit(&png_ptr->zstream))
161 case Z_OK: /* Do nothing */ break;
163 case Z_STREAM_ERROR: png_warning(png_ptr, "zlib memory error");
164 png_cleanup_needed = 1; break;
165 case Z_VERSION_ERROR: png_warning(png_ptr, "zlib version error");
166 png_cleanup_needed = 1; break;
167 default: png_warning(png_ptr, "Unknown zlib error");
168 png_cleanup_needed = 1;
172 if (png_cleanup_needed)
174 /* Clean up PNG structure and deallocate any memory. */
175 png_free(png_ptr, png_ptr->zbuf);
176 png_ptr->zbuf = NULL;
177 #ifdef PNG_USER_MEM_SUPPORTED
178 png_destroy_struct_2((png_voidp)png_ptr,
179 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
181 png_destroy_struct((png_voidp)png_ptr);
186 png_ptr->zstream.next_out = png_ptr->zbuf;
187 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
189 png_set_read_fn(png_ptr, NULL, NULL);
196 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
197 /* Read the information before the actual image data. This has been
198 * changed in v0.90 to allow reading a file that already has the magic
199 * bytes read from the stream. You can tell libpng how many bytes have
200 * been read from the beginning of the stream (up to the maximum of 8)
201 * via png_set_sig_bytes(), and we will only check the remaining bytes
202 * here. The application can then have access to the signature bytes we
203 * read if it is determined that this isn't a valid PNG file.
206 png_read_info(png_structp png_ptr, png_infop info_ptr)
208 png_debug(1, "in png_read_info");
210 if (png_ptr == NULL || info_ptr == NULL)
213 /* If we haven't checked all of the PNG signature bytes, do so now. */
214 if (png_ptr->sig_bytes < 8)
216 png_size_t num_checked = png_ptr->sig_bytes,
217 num_to_check = 8 - num_checked;
219 #ifdef PNG_IO_STATE_SUPPORTED
220 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
223 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
224 png_ptr->sig_bytes = 8;
226 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
228 if (num_checked < 4 &&
229 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
230 png_error(png_ptr, "Not a PNG file");
232 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
235 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
244 #ifdef PNG_READ_bKGD_SUPPORTED
247 #ifdef PNG_READ_cHRM_SUPPORTED
250 #ifdef PNG_READ_gAMA_SUPPORTED
253 #ifdef PNG_READ_hIST_SUPPORTED
256 #ifdef PNG_READ_iCCP_SUPPORTED
259 #ifdef PNG_READ_iTXt_SUPPORTED
262 #ifdef PNG_READ_oFFs_SUPPORTED
265 #ifdef PNG_READ_pCAL_SUPPORTED
268 #ifdef PNG_READ_pHYs_SUPPORTED
271 #ifdef PNG_READ_sBIT_SUPPORTED
274 #ifdef PNG_READ_sCAL_SUPPORTED
277 #ifdef PNG_READ_sPLT_SUPPORTED
280 #ifdef PNG_READ_sRGB_SUPPORTED
283 #ifdef PNG_READ_tEXt_SUPPORTED
286 #ifdef PNG_READ_tIME_SUPPORTED
289 #ifdef PNG_READ_tRNS_SUPPORTED
292 #ifdef PNG_READ_zTXt_SUPPORTED
295 png_uint_32 length = png_read_chunk_header(png_ptr);
296 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
298 /* This should be a binary subdivision search or a hash for
299 * matching the chunk name rather than a linear search.
301 if (!png_memcmp(chunk_name, png_IDAT, 4))
302 if (png_ptr->mode & PNG_AFTER_IDAT)
303 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
305 if (!png_memcmp(chunk_name, png_IHDR, 4))
306 png_handle_IHDR(png_ptr, info_ptr, length);
307 else if (!png_memcmp(chunk_name, png_IEND, 4))
308 png_handle_IEND(png_ptr, info_ptr, length);
309 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
310 else if (png_handle_as_unknown(png_ptr, chunk_name))
312 if (!png_memcmp(chunk_name, png_IDAT, 4))
313 png_ptr->mode |= PNG_HAVE_IDAT;
314 png_handle_unknown(png_ptr, info_ptr, length);
315 if (!png_memcmp(chunk_name, png_PLTE, 4))
316 png_ptr->mode |= PNG_HAVE_PLTE;
317 else if (!png_memcmp(chunk_name, png_IDAT, 4))
319 if (!(png_ptr->mode & PNG_HAVE_IHDR))
320 png_error(png_ptr, "Missing IHDR before IDAT");
321 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
322 !(png_ptr->mode & PNG_HAVE_PLTE))
323 png_error(png_ptr, "Missing PLTE before IDAT");
328 else if (!png_memcmp(chunk_name, png_PLTE, 4))
329 png_handle_PLTE(png_ptr, info_ptr, length);
330 else if (!png_memcmp(chunk_name, png_IDAT, 4))
332 if (!(png_ptr->mode & PNG_HAVE_IHDR))
333 png_error(png_ptr, "Missing IHDR before IDAT");
334 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
335 !(png_ptr->mode & PNG_HAVE_PLTE))
336 png_error(png_ptr, "Missing PLTE before IDAT");
338 png_ptr->idat_size = length;
339 png_ptr->mode |= PNG_HAVE_IDAT;
342 #ifdef PNG_READ_bKGD_SUPPORTED
343 else if (!png_memcmp(chunk_name, png_bKGD, 4))
344 png_handle_bKGD(png_ptr, info_ptr, length);
346 #ifdef PNG_READ_cHRM_SUPPORTED
347 else if (!png_memcmp(chunk_name, png_cHRM, 4))
348 png_handle_cHRM(png_ptr, info_ptr, length);
350 #ifdef PNG_READ_gAMA_SUPPORTED
351 else if (!png_memcmp(chunk_name, png_gAMA, 4))
352 png_handle_gAMA(png_ptr, info_ptr, length);
354 #ifdef PNG_READ_hIST_SUPPORTED
355 else if (!png_memcmp(chunk_name, png_hIST, 4))
356 png_handle_hIST(png_ptr, info_ptr, length);
358 #ifdef PNG_READ_oFFs_SUPPORTED
359 else if (!png_memcmp(chunk_name, png_oFFs, 4))
360 png_handle_oFFs(png_ptr, info_ptr, length);
362 #ifdef PNG_READ_pCAL_SUPPORTED
363 else if (!png_memcmp(chunk_name, png_pCAL, 4))
364 png_handle_pCAL(png_ptr, info_ptr, length);
366 #ifdef PNG_READ_sCAL_SUPPORTED
367 else if (!png_memcmp(chunk_name, png_sCAL, 4))
368 png_handle_sCAL(png_ptr, info_ptr, length);
370 #ifdef PNG_READ_pHYs_SUPPORTED
371 else if (!png_memcmp(chunk_name, png_pHYs, 4))
372 png_handle_pHYs(png_ptr, info_ptr, length);
374 #ifdef PNG_READ_sBIT_SUPPORTED
375 else if (!png_memcmp(chunk_name, png_sBIT, 4))
376 png_handle_sBIT(png_ptr, info_ptr, length);
378 #ifdef PNG_READ_sRGB_SUPPORTED
379 else if (!png_memcmp(chunk_name, png_sRGB, 4))
380 png_handle_sRGB(png_ptr, info_ptr, length);
382 #ifdef PNG_READ_iCCP_SUPPORTED
383 else if (!png_memcmp(chunk_name, png_iCCP, 4))
384 png_handle_iCCP(png_ptr, info_ptr, length);
386 #ifdef PNG_READ_sPLT_SUPPORTED
387 else if (!png_memcmp(chunk_name, png_sPLT, 4))
388 png_handle_sPLT(png_ptr, info_ptr, length);
390 #ifdef PNG_READ_tEXt_SUPPORTED
391 else if (!png_memcmp(chunk_name, png_tEXt, 4))
392 png_handle_tEXt(png_ptr, info_ptr, length);
394 #ifdef PNG_READ_tIME_SUPPORTED
395 else if (!png_memcmp(chunk_name, png_tIME, 4))
396 png_handle_tIME(png_ptr, info_ptr, length);
398 #ifdef PNG_READ_tRNS_SUPPORTED
399 else if (!png_memcmp(chunk_name, png_tRNS, 4))
400 png_handle_tRNS(png_ptr, info_ptr, length);
402 #ifdef PNG_READ_zTXt_SUPPORTED
403 else if (!png_memcmp(chunk_name, png_zTXt, 4))
404 png_handle_zTXt(png_ptr, info_ptr, length);
406 #ifdef PNG_READ_iTXt_SUPPORTED
407 else if (!png_memcmp(chunk_name, png_iTXt, 4))
408 png_handle_iTXt(png_ptr, info_ptr, length);
411 png_handle_unknown(png_ptr, info_ptr, length);
414 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
416 /* Optional call to update the users info_ptr structure */
418 png_read_update_info(png_structp png_ptr, png_infop info_ptr)
420 png_debug(1, "in png_read_update_info");
424 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
425 png_read_start_row(png_ptr);
428 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
430 png_read_transform_info(png_ptr, info_ptr);
433 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
434 /* Initialize palette, background, etc, after transformations
435 * are set, but before any reading takes place. This allows
436 * the user to obtain a gamma-corrected palette, for example.
437 * If the user doesn't call this, we will do it ourselves.
440 png_start_read_image(png_structp png_ptr)
442 png_debug(1, "in png_start_read_image");
446 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
447 png_read_start_row(png_ptr);
449 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
451 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
453 png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
456 PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
458 PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
464 png_debug2(1, "in png_read_row (row %lu, pass %d)",
465 (unsigned long) png_ptr->row_number, png_ptr->pass);
467 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
468 png_read_start_row(png_ptr);
469 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
471 /* Check for transforms that have been set but were defined out */
472 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
473 if (png_ptr->transformations & PNG_INVERT_MONO)
474 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
476 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
477 if (png_ptr->transformations & PNG_FILLER)
478 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
480 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
481 if (png_ptr->transformations & PNG_PACKSWAP)
482 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
484 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
485 if (png_ptr->transformations & PNG_PACK)
486 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
488 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
489 if (png_ptr->transformations & PNG_SHIFT)
490 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
492 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
493 if (png_ptr->transformations & PNG_BGR)
494 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
496 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
497 if (png_ptr->transformations & PNG_SWAP_BYTES)
498 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
502 #ifdef PNG_READ_INTERLACING_SUPPORTED
503 /* If interlaced and we do not need a new row, combine row and return */
504 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
506 switch (png_ptr->pass)
509 if (png_ptr->row_number & 0x07)
512 png_combine_row(png_ptr, dsp_row,
513 png_pass_dsp_mask[png_ptr->pass]);
514 png_read_finish_row(png_ptr);
519 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
522 png_combine_row(png_ptr, dsp_row,
523 png_pass_dsp_mask[png_ptr->pass]);
524 png_read_finish_row(png_ptr);
529 if ((png_ptr->row_number & 0x07) != 4)
531 if (dsp_row != NULL && (png_ptr->row_number & 4))
532 png_combine_row(png_ptr, dsp_row,
533 png_pass_dsp_mask[png_ptr->pass]);
534 png_read_finish_row(png_ptr);
539 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
542 png_combine_row(png_ptr, dsp_row,
543 png_pass_dsp_mask[png_ptr->pass]);
544 png_read_finish_row(png_ptr);
549 if ((png_ptr->row_number & 3) != 2)
551 if (dsp_row != NULL && (png_ptr->row_number & 2))
552 png_combine_row(png_ptr, dsp_row,
553 png_pass_dsp_mask[png_ptr->pass]);
554 png_read_finish_row(png_ptr);
559 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
562 png_combine_row(png_ptr, dsp_row,
563 png_pass_dsp_mask[png_ptr->pass]);
564 png_read_finish_row(png_ptr);
569 if (!(png_ptr->row_number & 1))
571 png_read_finish_row(png_ptr);
579 if (!(png_ptr->mode & PNG_HAVE_IDAT))
580 png_error(png_ptr, "Invalid attempt to read row data");
582 png_ptr->zstream.next_out = png_ptr->row_buf;
583 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
586 if (!(png_ptr->zstream.avail_in))
588 while (!png_ptr->idat_size)
590 png_crc_finish(png_ptr, 0);
592 png_ptr->idat_size = png_read_chunk_header(png_ptr);
593 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
594 png_error(png_ptr, "Not enough image data");
596 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
597 png_ptr->zstream.next_in = png_ptr->zbuf;
598 if (png_ptr->zbuf_size > png_ptr->idat_size)
599 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
600 png_crc_read(png_ptr, png_ptr->zbuf,
601 (png_size_t)png_ptr->zstream.avail_in);
602 png_ptr->idat_size -= png_ptr->zstream.avail_in;
604 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
605 if (ret == Z_STREAM_END)
607 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
609 png_benign_error(png_ptr, "Extra compressed data");
610 png_ptr->mode |= PNG_AFTER_IDAT;
611 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
615 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
616 "Decompression error");
618 } while (png_ptr->zstream.avail_out);
620 png_ptr->row_info.color_type = png_ptr->color_type;
621 png_ptr->row_info.width = png_ptr->iwidth;
622 png_ptr->row_info.channels = png_ptr->channels;
623 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
624 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
625 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
626 png_ptr->row_info.width);
628 if (png_ptr->row_buf[0])
629 png_read_filter_row(png_ptr, &(png_ptr->row_info),
630 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
631 (int)(png_ptr->row_buf[0]));
633 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
635 #ifdef PNG_MNG_FEATURES_SUPPORTED
636 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
637 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
639 /* Intrapixel differencing */
640 png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
645 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
646 png_do_read_transformations(png_ptr);
648 #ifdef PNG_READ_INTERLACING_SUPPORTED
649 /* Blow up interlaced rows to full size */
650 if (png_ptr->interlaced &&
651 (png_ptr->transformations & PNG_INTERLACE))
653 if (png_ptr->pass < 6)
654 /* Old interface (pre-1.0.9):
655 * png_do_read_interlace(&(png_ptr->row_info),
656 * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
658 png_do_read_interlace(png_ptr);
661 png_combine_row(png_ptr, dsp_row,
662 png_pass_dsp_mask[png_ptr->pass]);
664 png_combine_row(png_ptr, row,
665 png_pass_mask[png_ptr->pass]);
671 png_combine_row(png_ptr, row, 0xff);
673 png_combine_row(png_ptr, dsp_row, 0xff);
675 png_read_finish_row(png_ptr);
677 if (png_ptr->read_row_fn != NULL)
678 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
680 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
682 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
683 /* Read one or more rows of image data. If the image is interlaced,
684 * and png_set_interlace_handling() has been called, the rows need to
685 * contain the contents of the rows from the previous pass. If the
686 * image has alpha or transparency, and png_handle_alpha()[*] has been
687 * called, the rows contents must be initialized to the contents of the
690 * "row" holds the actual image, and pixels are placed in it
691 * as they arrive. If the image is displayed after each pass, it will
692 * appear to "sparkle" in. "display_row" can be used to display a
693 * "chunky" progressive image, with finer detail added as it becomes
694 * available. If you do not want this "chunky" display, you may pass
695 * NULL for display_row. If you do not want the sparkle display, and
696 * you have not called png_handle_alpha(), you may pass NULL for rows.
697 * If you have called png_handle_alpha(), and the image has either an
698 * alpha channel or a transparency chunk, you must provide a buffer for
699 * rows. In this case, you do not have to provide a display_row buffer
700 * also, but you may. If the image is not interlaced, or if you have
701 * not called png_set_interlace_handling(), the display_row buffer will
702 * be ignored, so pass NULL to it.
704 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
708 png_read_rows(png_structp png_ptr, png_bytepp row,
709 png_bytepp display_row, png_uint_32 num_rows)
715 png_debug(1, "in png_read_rows");
721 if (rp != NULL && dp != NULL)
722 for (i = 0; i < num_rows; i++)
724 png_bytep rptr = *rp++;
725 png_bytep dptr = *dp++;
727 png_read_row(png_ptr, rptr, dptr);
730 for (i = 0; i < num_rows; i++)
732 png_bytep rptr = *rp;
733 png_read_row(png_ptr, rptr, NULL);
737 for (i = 0; i < num_rows; i++)
739 png_bytep dptr = *dp;
740 png_read_row(png_ptr, NULL, dptr);
744 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
746 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
747 /* Read the entire image. If the image has an alpha channel or a tRNS
748 * chunk, and you have called png_handle_alpha()[*], you will need to
749 * initialize the image to the current image that PNG will be overlaying.
750 * We set the num_rows again here, in case it was incorrectly set in
751 * png_read_start_row() by a call to png_read_update_info() or
752 * png_start_read_image() if png_set_interlace_handling() wasn't called
753 * prior to either of these functions like it should have been. You can
754 * only call this function once. If you desire to have an image for
755 * each pass of a interlaced image, use png_read_rows() instead.
757 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
760 png_read_image(png_structp png_ptr, png_bytepp image)
762 png_uint_32 i, image_height;
766 png_debug(1, "in png_read_image");
771 #ifdef PNG_READ_INTERLACING_SUPPORTED
772 pass = png_set_interlace_handling(png_ptr);
774 if (png_ptr->interlaced)
776 "Cannot read interlaced image -- interlace handler disabled");
781 image_height=png_ptr->height;
782 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
784 for (j = 0; j < pass; j++)
787 for (i = 0; i < image_height; i++)
789 png_read_row(png_ptr, *rp, NULL);
794 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
796 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
797 /* Read the end of the PNG file. Will not read past the end of the
798 * file, will verify the end is accurate, and will read any comments
799 * or time information at the end of the file, if info is not NULL.
802 png_read_end(png_structp png_ptr, png_infop info_ptr)
804 png_debug(1, "in png_read_end");
808 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
816 #ifdef PNG_READ_bKGD_SUPPORTED
819 #ifdef PNG_READ_cHRM_SUPPORTED
822 #ifdef PNG_READ_gAMA_SUPPORTED
825 #ifdef PNG_READ_hIST_SUPPORTED
828 #ifdef PNG_READ_iCCP_SUPPORTED
831 #ifdef PNG_READ_iTXt_SUPPORTED
834 #ifdef PNG_READ_oFFs_SUPPORTED
837 #ifdef PNG_READ_pCAL_SUPPORTED
840 #ifdef PNG_READ_pHYs_SUPPORTED
843 #ifdef PNG_READ_sBIT_SUPPORTED
846 #ifdef PNG_READ_sCAL_SUPPORTED
849 #ifdef PNG_READ_sPLT_SUPPORTED
852 #ifdef PNG_READ_sRGB_SUPPORTED
855 #ifdef PNG_READ_tEXt_SUPPORTED
858 #ifdef PNG_READ_tIME_SUPPORTED
861 #ifdef PNG_READ_tRNS_SUPPORTED
864 #ifdef PNG_READ_zTXt_SUPPORTED
867 png_uint_32 length = png_read_chunk_header(png_ptr);
868 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
870 if (!png_memcmp(chunk_name, png_IHDR, 4))
871 png_handle_IHDR(png_ptr, info_ptr, length);
872 else if (!png_memcmp(chunk_name, png_IEND, 4))
873 png_handle_IEND(png_ptr, info_ptr, length);
874 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
875 else if (png_handle_as_unknown(png_ptr, chunk_name))
877 if (!png_memcmp(chunk_name, png_IDAT, 4))
879 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
880 png_benign_error(png_ptr, "Too many IDATs found");
882 png_handle_unknown(png_ptr, info_ptr, length);
883 if (!png_memcmp(chunk_name, png_PLTE, 4))
884 png_ptr->mode |= PNG_HAVE_PLTE;
887 else if (!png_memcmp(chunk_name, png_IDAT, 4))
889 /* Zero length IDATs are legal after the last IDAT has been
890 * read, but not after other chunks have been read.
892 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
893 png_benign_error(png_ptr, "Too many IDATs found");
894 png_crc_finish(png_ptr, length);
896 else if (!png_memcmp(chunk_name, png_PLTE, 4))
897 png_handle_PLTE(png_ptr, info_ptr, length);
898 #ifdef PNG_READ_bKGD_SUPPORTED
899 else if (!png_memcmp(chunk_name, png_bKGD, 4))
900 png_handle_bKGD(png_ptr, info_ptr, length);
902 #ifdef PNG_READ_cHRM_SUPPORTED
903 else if (!png_memcmp(chunk_name, png_cHRM, 4))
904 png_handle_cHRM(png_ptr, info_ptr, length);
906 #ifdef PNG_READ_gAMA_SUPPORTED
907 else if (!png_memcmp(chunk_name, png_gAMA, 4))
908 png_handle_gAMA(png_ptr, info_ptr, length);
910 #ifdef PNG_READ_hIST_SUPPORTED
911 else if (!png_memcmp(chunk_name, png_hIST, 4))
912 png_handle_hIST(png_ptr, info_ptr, length);
914 #ifdef PNG_READ_oFFs_SUPPORTED
915 else if (!png_memcmp(chunk_name, png_oFFs, 4))
916 png_handle_oFFs(png_ptr, info_ptr, length);
918 #ifdef PNG_READ_pCAL_SUPPORTED
919 else if (!png_memcmp(chunk_name, png_pCAL, 4))
920 png_handle_pCAL(png_ptr, info_ptr, length);
922 #ifdef PNG_READ_sCAL_SUPPORTED
923 else if (!png_memcmp(chunk_name, png_sCAL, 4))
924 png_handle_sCAL(png_ptr, info_ptr, length);
926 #ifdef PNG_READ_pHYs_SUPPORTED
927 else if (!png_memcmp(chunk_name, png_pHYs, 4))
928 png_handle_pHYs(png_ptr, info_ptr, length);
930 #ifdef PNG_READ_sBIT_SUPPORTED
931 else if (!png_memcmp(chunk_name, png_sBIT, 4))
932 png_handle_sBIT(png_ptr, info_ptr, length);
934 #ifdef PNG_READ_sRGB_SUPPORTED
935 else if (!png_memcmp(chunk_name, png_sRGB, 4))
936 png_handle_sRGB(png_ptr, info_ptr, length);
938 #ifdef PNG_READ_iCCP_SUPPORTED
939 else if (!png_memcmp(chunk_name, png_iCCP, 4))
940 png_handle_iCCP(png_ptr, info_ptr, length);
942 #ifdef PNG_READ_sPLT_SUPPORTED
943 else if (!png_memcmp(chunk_name, png_sPLT, 4))
944 png_handle_sPLT(png_ptr, info_ptr, length);
946 #ifdef PNG_READ_tEXt_SUPPORTED
947 else if (!png_memcmp(chunk_name, png_tEXt, 4))
948 png_handle_tEXt(png_ptr, info_ptr, length);
950 #ifdef PNG_READ_tIME_SUPPORTED
951 else if (!png_memcmp(chunk_name, png_tIME, 4))
952 png_handle_tIME(png_ptr, info_ptr, length);
954 #ifdef PNG_READ_tRNS_SUPPORTED
955 else if (!png_memcmp(chunk_name, png_tRNS, 4))
956 png_handle_tRNS(png_ptr, info_ptr, length);
958 #ifdef PNG_READ_zTXt_SUPPORTED
959 else if (!png_memcmp(chunk_name, png_zTXt, 4))
960 png_handle_zTXt(png_ptr, info_ptr, length);
962 #ifdef PNG_READ_iTXt_SUPPORTED
963 else if (!png_memcmp(chunk_name, png_iTXt, 4))
964 png_handle_iTXt(png_ptr, info_ptr, length);
967 png_handle_unknown(png_ptr, info_ptr, length);
968 } while (!(png_ptr->mode & PNG_HAVE_IEND));
970 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
972 /* Free all memory used by the read */
974 png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
975 png_infopp end_info_ptr_ptr)
977 png_structp png_ptr = NULL;
978 png_infop info_ptr = NULL, end_info_ptr = NULL;
979 #ifdef PNG_USER_MEM_SUPPORTED
980 png_free_ptr free_fn = NULL;
981 png_voidp mem_ptr = NULL;
984 png_debug(1, "in png_destroy_read_struct");
986 if (png_ptr_ptr != NULL)
987 png_ptr = *png_ptr_ptr;
991 #ifdef PNG_USER_MEM_SUPPORTED
992 free_fn = png_ptr->free_fn;
993 mem_ptr = png_ptr->mem_ptr;
996 if (info_ptr_ptr != NULL)
997 info_ptr = *info_ptr_ptr;
999 if (end_info_ptr_ptr != NULL)
1000 end_info_ptr = *end_info_ptr_ptr;
1002 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
1004 if (info_ptr != NULL)
1006 #ifdef PNG_TEXT_SUPPORTED
1007 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
1010 #ifdef PNG_USER_MEM_SUPPORTED
1011 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1012 (png_voidp)mem_ptr);
1014 png_destroy_struct((png_voidp)info_ptr);
1016 *info_ptr_ptr = NULL;
1019 if (end_info_ptr != NULL)
1021 #ifdef PNG_READ_TEXT_SUPPORTED
1022 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
1024 #ifdef PNG_USER_MEM_SUPPORTED
1025 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
1026 (png_voidp)mem_ptr);
1028 png_destroy_struct((png_voidp)end_info_ptr);
1030 *end_info_ptr_ptr = NULL;
1033 if (png_ptr != NULL)
1035 #ifdef PNG_USER_MEM_SUPPORTED
1036 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1037 (png_voidp)mem_ptr);
1039 png_destroy_struct((png_voidp)png_ptr);
1041 *png_ptr_ptr = NULL;
1045 /* Free all memory used by the read (old method) */
1047 png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
1049 #ifdef PNG_SETJMP_SUPPORTED
1052 png_error_ptr error_fn;
1053 png_error_ptr warning_fn;
1054 png_voidp error_ptr;
1055 #ifdef PNG_USER_MEM_SUPPORTED
1056 png_free_ptr free_fn;
1059 png_debug(1, "in png_read_destroy");
1061 if (info_ptr != NULL)
1062 png_info_destroy(png_ptr, info_ptr);
1064 if (end_info_ptr != NULL)
1065 png_info_destroy(png_ptr, end_info_ptr);
1067 png_free(png_ptr, png_ptr->zbuf);
1068 png_free(png_ptr, png_ptr->big_row_buf);
1069 png_free(png_ptr, png_ptr->prev_row);
1070 png_free(png_ptr, png_ptr->chunkdata);
1071 #ifdef PNG_READ_DITHER_SUPPORTED
1072 png_free(png_ptr, png_ptr->palette_lookup);
1073 png_free(png_ptr, png_ptr->dither_index);
1075 #ifdef PNG_READ_GAMMA_SUPPORTED
1076 png_free(png_ptr, png_ptr->gamma_table);
1078 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1079 png_free(png_ptr, png_ptr->gamma_from_1);
1080 png_free(png_ptr, png_ptr->gamma_to_1);
1082 if (png_ptr->free_me & PNG_FREE_PLTE)
1083 png_zfree(png_ptr, png_ptr->palette);
1084 png_ptr->free_me &= ~PNG_FREE_PLTE;
1085 #if defined(PNG_tRNS_SUPPORTED) || \
1086 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1087 if (png_ptr->free_me & PNG_FREE_TRNS)
1088 png_free(png_ptr, png_ptr->trans_alpha);
1089 png_ptr->free_me &= ~PNG_FREE_TRNS;
1091 #ifdef PNG_READ_hIST_SUPPORTED
1092 if (png_ptr->free_me & PNG_FREE_HIST)
1093 png_free(png_ptr, png_ptr->hist);
1094 png_ptr->free_me &= ~PNG_FREE_HIST;
1096 #ifdef PNG_READ_GAMMA_SUPPORTED
1097 if (png_ptr->gamma_16_table != NULL)
1100 int istop = (1 << (8 - png_ptr->gamma_shift));
1101 for (i = 0; i < istop; i++)
1103 png_free(png_ptr, png_ptr->gamma_16_table[i]);
1105 png_free(png_ptr, png_ptr->gamma_16_table);
1107 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1108 if (png_ptr->gamma_16_from_1 != NULL)
1111 int istop = (1 << (8 - png_ptr->gamma_shift));
1112 for (i = 0; i < istop; i++)
1114 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
1116 png_free(png_ptr, png_ptr->gamma_16_from_1);
1118 if (png_ptr->gamma_16_to_1 != NULL)
1121 int istop = (1 << (8 - png_ptr->gamma_shift));
1122 for (i = 0; i < istop; i++)
1124 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
1126 png_free(png_ptr, png_ptr->gamma_16_to_1);
1130 #ifdef PNG_TIME_RFC1123_SUPPORTED
1131 png_free(png_ptr, png_ptr->time_buffer);
1134 inflateEnd(&png_ptr->zstream);
1135 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1136 png_free(png_ptr, png_ptr->save_buffer);
1139 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1140 #ifdef PNG_TEXT_SUPPORTED
1141 png_free(png_ptr, png_ptr->current_text);
1142 #endif /* PNG_TEXT_SUPPORTED */
1143 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1145 /* Save the important info out of the png_struct, in case it is
1148 #ifdef PNG_SETJMP_SUPPORTED
1149 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
1152 error_fn = png_ptr->error_fn;
1153 warning_fn = png_ptr->warning_fn;
1154 error_ptr = png_ptr->error_ptr;
1155 #ifdef PNG_USER_MEM_SUPPORTED
1156 free_fn = png_ptr->free_fn;
1159 png_memset(png_ptr, 0, png_sizeof(png_struct));
1161 png_ptr->error_fn = error_fn;
1162 png_ptr->warning_fn = warning_fn;
1163 png_ptr->error_ptr = error_ptr;
1164 #ifdef PNG_USER_MEM_SUPPORTED
1165 png_ptr->free_fn = free_fn;
1168 #ifdef PNG_SETJMP_SUPPORTED
1169 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
1175 png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1177 if (png_ptr == NULL)
1179 png_ptr->read_row_fn = read_row_fn;
1183 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1184 #ifdef PNG_INFO_IMAGE_SUPPORTED
1186 png_read_png(png_structp png_ptr, png_infop info_ptr,
1192 if (png_ptr == NULL)
1195 /* png_read_info() gives us all of the information from the
1196 * PNG file before the first IDAT (image data chunk).
1198 png_read_info(png_ptr, info_ptr);
1199 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
1200 png_error(png_ptr, "Image is too high to process with png_read_png()");
1202 /* -------------- image transformations start here ------------------- */
1204 #ifdef PNG_READ_16_TO_8_SUPPORTED
1205 /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
1207 if (transforms & PNG_TRANSFORM_STRIP_16)
1208 png_set_strip_16(png_ptr);
1211 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1212 /* Strip alpha bytes from the input data without combining with
1213 * the background (not recommended).
1215 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
1216 png_set_strip_alpha(png_ptr);
1219 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1220 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1221 * byte into separate bytes (useful for paletted and grayscale images).
1223 if (transforms & PNG_TRANSFORM_PACKING)
1224 png_set_packing(png_ptr);
1227 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1228 /* Change the order of packed pixels to least significant bit first
1229 * (not useful if you are using png_set_packing).
1231 if (transforms & PNG_TRANSFORM_PACKSWAP)
1232 png_set_packswap(png_ptr);
1235 #ifdef PNG_READ_EXPAND_SUPPORTED
1236 /* Expand paletted colors into true RGB triplets
1237 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1238 * Expand paletted or RGB images with transparency to full alpha
1239 * channels so the data will be available as RGBA quartets.
1241 if (transforms & PNG_TRANSFORM_EXPAND)
1242 if ((png_ptr->bit_depth < 8) ||
1243 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1244 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1245 png_set_expand(png_ptr);
1248 /* We don't handle background color or gamma transformation or dithering.
1251 #ifdef PNG_READ_INVERT_SUPPORTED
1252 /* Invert monochrome files to have 0 as white and 1 as black
1254 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1255 png_set_invert_mono(png_ptr);
1258 #ifdef PNG_READ_SHIFT_SUPPORTED
1259 /* If you want to shift the pixel values from the range [0,255] or
1260 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1261 * colors were originally in:
1263 if ((transforms & PNG_TRANSFORM_SHIFT)
1264 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1266 png_color_8p sig_bit;
1268 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1269 png_set_shift(png_ptr, sig_bit);
1273 #ifdef PNG_READ_BGR_SUPPORTED
1274 /* Flip the RGB pixels to BGR (or RGBA to BGRA)
1276 if (transforms & PNG_TRANSFORM_BGR)
1277 png_set_bgr(png_ptr);
1280 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1281 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1283 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1284 png_set_swap_alpha(png_ptr);
1287 #ifdef PNG_READ_SWAP_SUPPORTED
1288 /* Swap bytes of 16 bit files to least significant byte first
1290 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1291 png_set_swap(png_ptr);
1294 /* Added at libpng-1.2.41 */
1295 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1296 /* Invert the alpha channel from opacity to transparency
1298 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1299 png_set_invert_alpha(png_ptr);
1302 /* Added at libpng-1.2.41 */
1303 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1304 /* Expand grayscale image to RGB
1306 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
1307 png_set_gray_to_rgb(png_ptr);
1310 /* We don't handle adding filler bytes */
1312 /* Optional call to gamma correct and add the background to the palette
1313 * and update info structure. REQUIRED if you are expecting libpng to
1314 * update the palette for you (i.e., you selected such a transform above).
1316 png_read_update_info(png_ptr, info_ptr);
1318 /* -------------- image transformations end here ------------------- */
1320 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1321 if (info_ptr->row_pointers == NULL)
1325 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
1326 info_ptr->height * png_sizeof(png_bytep));
1327 for (iptr=0; iptr<info_ptr->height; iptr++)
1328 info_ptr->row_pointers[iptr] = NULL;
1330 info_ptr->free_me |= PNG_FREE_ROWS;
1332 for (row = 0; row < (int)info_ptr->height; row++)
1333 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
1334 png_get_rowbytes(png_ptr, info_ptr));
1337 png_read_image(png_ptr, info_ptr->row_pointers);
1338 info_ptr->valid |= PNG_INFO_IDAT;
1340 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1341 png_read_end(png_ptr, info_ptr);
1343 transforms = transforms; /* Quiet compiler warnings */
1347 #endif /* PNG_INFO_IMAGE_SUPPORTED */
1348 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
1349 #endif /* PNG_READ_SUPPORTED */