blob: 0f598fa15b559a388fe6ed25bc54855a50da5dcb [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngread.c - read a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004 * Last changed in libpng 1.5.6 [(PENDING RELEASE)]
Glenn Randers-Pehrson64b863c2011-01-04 09:57:06 -06005 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
15 */
Guy Schalnat0d580581995-07-20 02:43:20 -050016
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050017#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060018
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060019#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050020
Andreas Dilger47a0c421997-05-16 02:46:07 -050021/* Create a PNG structure for reading, and allocate any memory needed. */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050022PNG_FUNCTION(png_structp,PNGAPI
23png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
24 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
Guy Schalnat0d580581995-07-20 02:43:20 -050025{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050026
27#ifdef PNG_USER_MEM_SUPPORTED
28 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050029 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050030}
31
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060032/* Alternate create PNG structure for reading, and allocate any memory
33 * needed.
34 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050035PNG_FUNCTION(png_structp,PNGAPI
36png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -060037 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050038 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050039{
40#endif /* PNG_USER_MEM_SUPPORTED */
41
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -060042#ifdef PNG_SETJMP_SUPPORTED
43 volatile
44#endif
45 png_structp png_ptr;
Glenn Randers-Pehrson62ca98e2009-12-20 15:14:57 -060046 volatile int png_cleanup_needed = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060047
48#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060049#ifdef USE_FAR_KEYWORD
John Bowlere6dc85b2011-04-27 14:47:15 -050050 jmp_buf tmp_jmpbuf;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060051#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060052#endif
53
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050054 png_debug(1, "in png_create_read_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050055
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050056#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060057 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050058 malloc_fn, mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050059#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060060 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050061#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060062 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050063 return (NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050064
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -050065 /* Added at libpng-1.2.6 */
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060066#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -050067 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
68 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050069
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060070# ifdef PNG_USER_CHUNK_CACHE_MAX
71 /* Added at libpng-1.2.43 and 1.4.0 */
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -050072 png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060073# endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050074
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060075# ifdef PNG_SET_USER_CHUNK_MALLOC_MAX
76 /* Added at libpng-1.2.43 and 1.4.1 */
77 png_ptr->user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX;
78# endif
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -050079#endif
80
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050081#ifdef PNG_SETJMP_SUPPORTED
82/* Applications that neglect to set up their own setjmp() and then
83 encounter a png_error() will longjmp here. Since the jmpbuf is
84 then meaningless we abort instead of returning. */
85#ifdef USE_FAR_KEYWORD
John Bowlere6dc85b2011-04-27 14:47:15 -050086 if (setjmp(tmp_jmpbuf))
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050087#else
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -060088 if (setjmp(png_jmpbuf(png_ptr))) /* Sets longjmp to match setjmp */
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050089#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050090 PNG_ABORT();
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -060091#ifdef USE_FAR_KEYWORD
John Bowlere6dc85b2011-04-27 14:47:15 -050092 png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -060093#endif
Glenn Randers-Pehrsona2567be2009-10-19 20:31:31 -050094#endif /* PNG_SETJMP_SUPPORTED */
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050095
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050096#ifdef PNG_USER_MEM_SUPPORTED
97 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060098#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050099
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600100 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnat6d764711995-12-19 03:22:19 -0600101
John Bowler88b77cc2011-05-05 06:49:55 -0500102 /* Call the general version checker (shared with read and write code): */
103 if (!png_user_version_check(png_ptr, user_png_ver))
104 png_cleanup_needed = 1;
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500105
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500106 if (!png_cleanup_needed)
107 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500108 /* Initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500109 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500110 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr, png_ptr->zbuf_size);
111
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500112 if (png_ptr->zbuf == NULL)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600113 png_cleanup_needed = 1;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500114 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500115
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600116 png_ptr->zstream.zalloc = png_zalloc;
117 png_ptr->zstream.zfree = png_zfree;
118 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500119
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500120 if (!png_cleanup_needed)
121 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500122 switch (inflateInit(&png_ptr->zstream))
123 {
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500124 case Z_OK:
125 break; /* Do nothing */
126
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500127 case Z_MEM_ERROR:
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500128 png_warning(png_ptr, "zlib memory error");
129 png_cleanup_needed = 1;
130 break;
131
132 case Z_STREAM_ERROR:
133 png_warning(png_ptr, "zlib stream error");
134 png_cleanup_needed = 1;
135 break;
136
137 case Z_VERSION_ERROR:
138 png_warning(png_ptr, "zlib version error");
139 png_cleanup_needed = 1;
140 break;
141
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500142 default: png_warning(png_ptr, "Unknown zlib error");
143 png_cleanup_needed = 1;
144 }
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500145 }
146
147 if (png_cleanup_needed)
148 {
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500149 /* Clean up PNG structure and deallocate any memory. */
150 png_free(png_ptr, png_ptr->zbuf);
151 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500152#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500153 png_destroy_struct_2((png_voidp)png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600154 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500155#else
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500156 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500157#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500158 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500159 }
160
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600161 png_ptr->zstream.next_out = png_ptr->zbuf;
162 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500163
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500164 png_set_read_fn(png_ptr, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500165
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500166
Guy Schalnate5a37791996-06-05 15:50:50 -0500167 return (png_ptr);
168}
169
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500170
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500171#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600172/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -0600173 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600174 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500175 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600176 * via png_set_sig_bytes(), and we will only check the remaining bytes
177 * here. The application can then have access to the signature bytes we
178 * read if it is determined that this isn't a valid PNG file.
179 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500180void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600181png_read_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500182{
Glenn Randers-Pehrsonc81bb8a2009-08-15 22:02:26 -0500183 png_debug(1, "in png_read_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500184
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500185 if (png_ptr == NULL || info_ptr == NULL)
186 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500187
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600188 /* Read and check the PNG file signature. */
189 png_read_sig(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500190
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500191 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500192 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500193 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500194 png_uint_32 chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500195
196 /* This should be a binary subdivision search or a hash for
197 * matching the chunk name rather than a linear search.
198 */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500199 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600200 if (png_ptr->mode & PNG_AFTER_IDAT)
201 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500202
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500203 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600204 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500205
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500206 else if (chunk_name == png_IEND)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600207 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500208
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600209#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500210 else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
211 PNG_HANDLE_CHUNK_AS_DEFAULT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600212 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500213 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600214 png_ptr->mode |= PNG_HAVE_IDAT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500215
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600216 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500217
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500218 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600219 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500220
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500221 else if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600222 {
223 if (!(png_ptr->mode & PNG_HAVE_IHDR))
224 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500225
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600226 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600227 !(png_ptr->mode & PNG_HAVE_PLTE))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600228 png_error(png_ptr, "Missing PLTE before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500229
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600230 break;
231 }
232 }
233#endif
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500234 else if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600235 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500236
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500237 else if (chunk_name == png_IDAT)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500238 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500239 if (!(png_ptr->mode & PNG_HAVE_IHDR))
240 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500241
Guy Schalnate5a37791996-06-05 15:50:50 -0500242 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600243 !(png_ptr->mode & PNG_HAVE_PLTE))
Guy Schalnate5a37791996-06-05 15:50:50 -0500244 png_error(png_ptr, "Missing PLTE before IDAT");
245
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500246 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500247 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500248 break;
249 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500250
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500251#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500252 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500253 png_handle_bKGD(png_ptr, info_ptr, length);
254#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500255
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500256#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500257 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258 png_handle_cHRM(png_ptr, info_ptr, length);
259#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500260
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500261#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500262 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500263 png_handle_gAMA(png_ptr, info_ptr, length);
264#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500265
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500266#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500267 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500268 png_handle_hIST(png_ptr, info_ptr, length);
269#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500270
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500271#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500272 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500273 png_handle_oFFs(png_ptr, info_ptr, length);
274#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500275
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500276#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500277 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500278 png_handle_pCAL(png_ptr, info_ptr, length);
279#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500280
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500281#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500282 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600283 png_handle_sCAL(png_ptr, info_ptr, length);
284#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500285
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500286#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500287 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500288 png_handle_pHYs(png_ptr, info_ptr, length);
289#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500290
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500291#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500292 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500293 png_handle_sBIT(png_ptr, info_ptr, length);
294#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500295
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500296#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500297 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600298 png_handle_sRGB(png_ptr, info_ptr, length);
299#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500300
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500301#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500302 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600303 png_handle_iCCP(png_ptr, info_ptr, length);
304#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500305
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500306#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500307 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600308 png_handle_sPLT(png_ptr, info_ptr, length);
309#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500310
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500311#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500312 else if (chunk_name == png_tEXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500313 png_handle_tEXt(png_ptr, info_ptr, length);
314#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500315
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500316#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500317 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318 png_handle_tIME(png_ptr, info_ptr, length);
319#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500320
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500321#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500322 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500323 png_handle_tRNS(png_ptr, info_ptr, length);
324#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500325
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500326#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500327 else if (chunk_name == png_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500328 png_handle_zTXt(png_ptr, info_ptr, length);
329#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500330
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500331#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500332 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600333 png_handle_iTXt(png_ptr, info_ptr, length);
334#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500335
Guy Schalnat0d580581995-07-20 02:43:20 -0500336 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600337 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500338 }
339}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500340#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500341
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500342/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500343void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600344png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500345{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500346 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500347
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500348 if (png_ptr == NULL)
349 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500350
Glenn Randers-Pehrson3dbfd302011-10-13 17:24:36 -0500351 png_read_start_row(png_ptr);
Glenn Randers-Pehrson4cfdb3c2009-11-26 11:49:37 -0600352
John Bowler4a12f4a2011-04-17 18:34:22 -0500353#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600354 png_read_transform_info(png_ptr, info_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -0500355#else
356 PNG_UNUSED(info_ptr)
357#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500358}
359
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500360#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600361/* Initialize palette, background, etc, after transformations
362 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500363 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600364 * If the user doesn't call this, we will do it ourselves.
365 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500366void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600367png_start_read_image(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500368{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500369 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500370
Glenn Randers-Pehrson3dbfd302011-10-13 17:24:36 -0500371 if (png_ptr != NULL)
372 png_read_start_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500373}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500374#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500375
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500376#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500377void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600378png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500379{
380 int ret;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500381
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500382 png_row_info row_info;
383
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500384 if (png_ptr == NULL)
385 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500386
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500387 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -0500388 (unsigned long)png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500389
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500390 /* png_read_start_row sets the information (in particular iwidth) for this
391 * interlace pass.
392 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500393 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500394 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500395
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500396 /* 1.5.6: row_info moved out of png_struct to a local here. */
397 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
398 row_info.color_type = png_ptr->color_type;
399 row_info.bit_depth = png_ptr->bit_depth;
400 row_info.channels = png_ptr->channels;
401 row_info.pixel_depth = png_ptr->pixel_depth;
402 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
403
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500404 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
405 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500406 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500407#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
408 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500409 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500410#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500411
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500412#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
413 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500414 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500415#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500416
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600417#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
418 !defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500419 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500420 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500421#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500422
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500423#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
424 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500425 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500426#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500427
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500428#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
429 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500430 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500431#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500432
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500433#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
434 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500435 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500436#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500437
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500438#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
439 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500440 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500441#endif
442 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500443
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500444#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500445 /* If interlaced and we do not need a new row, combine row and return.
446 * Notice that the pixels we have from previous rows have been transformed
447 * already; we can only combine like with like (transformed or
448 * untransformed) and, because of the libpng API for interlaced images, this
449 * means we must transform before de-interlacing.
450 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500451 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
452 {
453 switch (png_ptr->pass)
454 {
455 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600456 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500457 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500458 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500459 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600460 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500461 return;
462 }
463 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500464
Guy Schalnat0d580581995-07-20 02:43:20 -0500465 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600466 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500467 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500468 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500469 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500470
Guy Schalnat0d580581995-07-20 02:43:20 -0500471 png_read_finish_row(png_ptr);
472 return;
473 }
474 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500475
Guy Schalnat0d580581995-07-20 02:43:20 -0500476 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600477 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500478 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500479 if (dsp_row != NULL && (png_ptr->row_number & 4))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500480 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500481
Guy Schalnat0d580581995-07-20 02:43:20 -0500482 png_read_finish_row(png_ptr);
483 return;
484 }
485 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500486
Guy Schalnat0d580581995-07-20 02:43:20 -0500487 case 3:
488 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
489 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500490 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500491 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500492
Guy Schalnat0d580581995-07-20 02:43:20 -0500493 png_read_finish_row(png_ptr);
494 return;
495 }
496 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500497
Guy Schalnat0d580581995-07-20 02:43:20 -0500498 case 4:
499 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600500 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500501 if (dsp_row != NULL && (png_ptr->row_number & 2))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500502 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500503
Guy Schalnat0d580581995-07-20 02:43:20 -0500504 png_read_finish_row(png_ptr);
505 return;
506 }
507 break;
508 case 5:
509 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
510 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500511 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500512 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500513
Guy Schalnat0d580581995-07-20 02:43:20 -0500514 png_read_finish_row(png_ptr);
515 return;
516 }
517 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500518
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600519 default:
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600520 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500521 if (!(png_ptr->row_number & 1))
522 {
523 png_read_finish_row(png_ptr);
524 return;
525 }
526 break;
527 }
528 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500529#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500530
Guy Schalnate5a37791996-06-05 15:50:50 -0500531 if (!(png_ptr->mode & PNG_HAVE_IDAT))
532 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500533
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600534 png_ptr->zstream.next_out = png_ptr->row_buf;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600535 png_ptr->zstream.avail_out =
536 (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
537 png_ptr->iwidth) + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500538
Guy Schalnat0d580581995-07-20 02:43:20 -0500539 do
540 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600541 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500542 {
543 while (!png_ptr->idat_size)
544 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600545 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500546
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500547 png_ptr->idat_size = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500548 if (png_ptr->chunk_name != png_IDAT)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600549 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500550 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600551 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
552 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500553 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600554 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555 png_crc_read(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600556 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600557 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500558 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500559
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600560 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500561
Guy Schalnat0d580581995-07-20 02:43:20 -0500562 if (ret == Z_STREAM_END)
563 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600564 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500565 png_ptr->idat_size)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500566 png_benign_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600567 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500568 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600569 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500570 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500571
Guy Schalnat0d580581995-07-20 02:43:20 -0500572 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600573 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600574 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600575
576 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500577
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500578 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
579 {
580 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
581 png_read_filter_row(&row_info, png_ptr->row_buf + 1,
582 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
583 else
584 png_error(png_ptr, "bad adaptive filter value");
585 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500586
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500587 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
588 * 1.5.6, while the buffer really is this big in current versions of libpng
589 * it may not be in the future, so this was changed just to copy the
590 * interlaced count:
591 */
592 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600593
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500594#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500595 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600596 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600597 {
598 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500599 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600600 }
601#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500602
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500603
John Bowler4a12f4a2011-04-17 18:34:22 -0500604#ifdef PNG_READ_TRANSFORMS_SUPPORTED
John Bowler9b872f42011-02-12 09:00:16 -0600605 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500606 png_do_read_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500607#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500608
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500609 /* The transformed pixel depth should match the depth now in row_info. */
610 if (png_ptr->transformed_pixel_depth == 0)
611 {
612 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
613 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
614 png_error(png_ptr, "sequential row overflow");
615 }
616
617 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
618 png_error(png_ptr, "internal sequential row size calculation error");
619
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500620#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500621 /* Blow up interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500622 if (png_ptr->interlaced &&
623 (png_ptr->transformations & PNG_INTERLACE))
624 {
625 if (png_ptr->pass < 6)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500626 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
627 png_ptr->transformations);
Guy Schalnat0d580581995-07-20 02:43:20 -0500628
Andreas Dilger47a0c421997-05-16 02:46:07 -0500629 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500630 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500631
Andreas Dilger47a0c421997-05-16 02:46:07 -0500632 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500633 png_combine_row(png_ptr, row, 0/*row*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500634 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500635
Guy Schalnat0d580581995-07-20 02:43:20 -0500636 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500637#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500638 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500639 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500640 png_combine_row(png_ptr, row, -1/*ignored*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500641
Andreas Dilger47a0c421997-05-16 02:46:07 -0500642 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500643 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500644 }
645 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600646
647 if (png_ptr->read_row_fn != NULL)
648 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500649}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500650#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500651
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500652#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500653/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600654 * and png_set_interlace_handling() has been called, the rows need to
655 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500656 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600657 * called, the rows contents must be initialized to the contents of the
658 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600659 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600660 * "row" holds the actual image, and pixels are placed in it
661 * as they arrive. If the image is displayed after each pass, it will
662 * appear to "sparkle" in. "display_row" can be used to display a
663 * "chunky" progressive image, with finer detail added as it becomes
664 * available. If you do not want this "chunky" display, you may pass
665 * NULL for display_row. If you do not want the sparkle display, and
666 * you have not called png_handle_alpha(), you may pass NULL for rows.
667 * If you have called png_handle_alpha(), and the image has either an
668 * alpha channel or a transparency chunk, you must provide a buffer for
669 * rows. In this case, you do not have to provide a display_row buffer
670 * also, but you may. If the image is not interlaced, or if you have
671 * not called png_set_interlace_handling(), the display_row buffer will
672 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500673 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600674 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600675 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600676
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500677void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600678png_read_rows(png_structp png_ptr, png_bytepp row,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600679 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500680{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600681 png_uint_32 i;
682 png_bytepp rp;
683 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500684
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500685 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500686
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500687 if (png_ptr == NULL)
688 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500689
Guy Schalnat0f716451995-11-28 11:22:13 -0600690 rp = row;
691 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500692 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500693 for (i = 0; i < num_rows; i++)
694 {
695 png_bytep rptr = *rp++;
696 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600697
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500698 png_read_row(png_ptr, rptr, dptr);
699 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500700
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500701 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500702 for (i = 0; i < num_rows; i++)
703 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500704 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500705 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500706 rp++;
707 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500708
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500709 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500710 for (i = 0; i < num_rows; i++)
711 {
712 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500713 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500714 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500715 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500716}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500717#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500718
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500719#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500720/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500721 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600722 * initialize the image to the current image that PNG will be overlaying.
723 * We set the num_rows again here, in case it was incorrectly set in
724 * png_read_start_row() by a call to png_read_update_info() or
725 * png_start_read_image() if png_set_interlace_handling() wasn't called
726 * prior to either of these functions like it should have been. You can
727 * only call this function once. If you desire to have an image for
728 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500729 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600730 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600731 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500732void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600733png_read_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500734{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500735 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500736 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600737 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500738
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500739 png_debug(1, "in png_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500740
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500741 if (png_ptr == NULL)
742 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500743
744#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500745 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
746 {
747 pass = png_set_interlace_handling(png_ptr);
748 /* And make sure transforms are initialized. */
749 png_start_read_image(png_ptr);
750 }
751 else
752 {
Glenn Randers-Pehrsone15a96b2011-01-14 15:47:37 -0600753 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500754 {
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500755 /* Caller called png_start_read_image or png_read_update_info without
756 * first turning on the PNG_INTERLACE transform. We can fix this here,
757 * but the caller should do it!
758 */
759 png_warning(png_ptr, "Interlace handling should be turned on when "
760 "using png_read_image");
761 /* Make sure this is set correctly */
762 png_ptr->num_rows = png_ptr->height;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500763 }
764
765 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
766 * the above error case.
767 */
768 pass = png_set_interlace_handling(png_ptr);
769 }
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500770#else
771 if (png_ptr->interlaced)
772 png_error(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600773 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500774
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500775 pass = 1;
776#endif
777
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500778 image_height=png_ptr->height;
Guy Schalnate5a37791996-06-05 15:50:50 -0500779
Guy Schalnat0d580581995-07-20 02:43:20 -0500780 for (j = 0; j < pass; j++)
781 {
782 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500783 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500784 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500785 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500786 rp++;
787 }
788 }
789}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500790#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500791
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500792#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500793/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600794 * file, will verify the end is accurate, and will read any comments
795 * or time information at the end of the file, if info is not NULL.
796 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500797void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600798png_read_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500799{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500800 png_debug(1, "in png_read_end");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500801
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500802 if (png_ptr == NULL)
803 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500804
Andreas Dilger47a0c421997-05-16 02:46:07 -0500805 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500806
807 do
808 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500809 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500810 png_uint_32 chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500811
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500812 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600813 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500814
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500815 else if (chunk_name == png_IEND)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600816 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500817
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600818#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500819 else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
820 PNG_HANDLE_CHUNK_AS_DEFAULT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600821 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500822 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600823 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500824 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500825 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600826 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600827 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500828 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600829 png_ptr->mode |= PNG_HAVE_PLTE;
830 }
831#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500832
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500833 else if (chunk_name == png_IDAT)
Guy Schalnat0d580581995-07-20 02:43:20 -0500834 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500835 /* Zero length IDATs are legal after the last IDAT has been
836 * read, but not after other chunks have been read.
837 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500838 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500839 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500840
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500841 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500842 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500843 else if (chunk_name == png_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500844 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500845
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500846#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500847 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500848 png_handle_bKGD(png_ptr, info_ptr, length);
849#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500850
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500851#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500852 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500853 png_handle_cHRM(png_ptr, info_ptr, length);
854#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500855
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500856#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500857 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500858 png_handle_gAMA(png_ptr, info_ptr, length);
859#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500860
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500861#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500862 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500863 png_handle_hIST(png_ptr, info_ptr, length);
864#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500865
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500866#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500867 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500868 png_handle_oFFs(png_ptr, info_ptr, length);
869#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500870
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500871#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500872 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500873 png_handle_pCAL(png_ptr, info_ptr, length);
874#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500875
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500876#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500877 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600878 png_handle_sCAL(png_ptr, info_ptr, length);
879#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500880
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500881#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500882 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500883 png_handle_pHYs(png_ptr, info_ptr, length);
884#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500885
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500886#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500887 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500889#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500890
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500891#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500892 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600893 png_handle_sRGB(png_ptr, info_ptr, length);
894#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500895
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500896#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500897 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600898 png_handle_iCCP(png_ptr, info_ptr, length);
899#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500900
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500901#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500902 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600903 png_handle_sPLT(png_ptr, info_ptr, length);
904#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500905
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500906#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500907 else if (chunk_name == png_tEXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600908 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500909#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500910
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500911#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500912 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500913 png_handle_tIME(png_ptr, info_ptr, length);
914#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500915
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500916#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500917 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500918 png_handle_tRNS(png_ptr, info_ptr, length);
919#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500920
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500921#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500922 else if (chunk_name == png_zTXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600923 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500924#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500925
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500926#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500927 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600928 png_handle_iTXt(png_ptr, info_ptr, length);
929#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500930
Guy Schalnat0d580581995-07-20 02:43:20 -0500931 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600932 png_handle_unknown(png_ptr, info_ptr, length);
933 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500934}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500935#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500936
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500937/* Free all memory used by the read */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500938void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500939png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600940 png_infopp end_info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500941{
942 png_structp png_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600943 png_infop info_ptr = NULL, end_info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500944#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500945 png_free_ptr free_fn = NULL;
946 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600947#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500948
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500949 png_debug(1, "in png_destroy_read_struct");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500950
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951 if (png_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500952 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500953 if (png_ptr == NULL)
954 return;
955
956#ifdef PNG_USER_MEM_SUPPORTED
957 free_fn = png_ptr->free_fn;
958 mem_ptr = png_ptr->mem_ptr;
959#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500960
Andreas Dilger47a0c421997-05-16 02:46:07 -0500961 if (info_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500962 info_ptr = *info_ptr_ptr;
963
Andreas Dilger47a0c421997-05-16 02:46:07 -0500964 if (end_info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600965 end_info_ptr = *end_info_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500966
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600967 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500968
Andreas Dilger47a0c421997-05-16 02:46:07 -0500969 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500970 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500971#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600972 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600973#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500974
975#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500976 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
977 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500978#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600979 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500980#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500981 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500982 }
983
Andreas Dilger47a0c421997-05-16 02:46:07 -0500984 if (end_info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500985 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500986#ifdef PNG_READ_TEXT_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600987 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600988#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500989#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500990 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600991 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500992#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600993 png_destroy_struct((png_voidp)end_info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500994#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500995 *end_info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500996 }
997
Andreas Dilger47a0c421997-05-16 02:46:07 -0500998 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500999 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001000#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001001 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1002 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001003#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001004 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001005#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001006 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001007 }
1008}
1009
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001010/* Free all memory used by the read (old method) */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001011void /* PRIVATE */
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001012png_read_destroy(png_structp png_ptr, png_infop info_ptr,
1013 png_infop end_info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001014{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001015#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001016 jmp_buf tmp_jmp;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001017#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001018 png_error_ptr error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -05001019#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001020 png_error_ptr warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -05001021#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001022 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001023#ifdef PNG_USER_MEM_SUPPORTED
1024 png_free_ptr free_fn;
1025#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001026
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001027 png_debug(1, "in png_read_destroy");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05001028
Andreas Dilger47a0c421997-05-16 02:46:07 -05001029 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001030 png_info_destroy(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001031
Andreas Dilger47a0c421997-05-16 02:46:07 -05001032 if (end_info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001033 png_info_destroy(png_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001034
John Bowler07772cb2011-10-14 18:19:47 -05001035#ifdef PNG_READ_GAMMA_SUPPORTED
1036 png_destroy_gamma_table(png_ptr);
1037#endif
1038
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001039 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -05001040 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgard1c422762011-10-17 16:52:19 -05001041 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001042 png_free(png_ptr, png_ptr->chunkdata);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001043
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001044#ifdef PNG_READ_QUANTIZE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001045 png_free(png_ptr, png_ptr->palette_lookup);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001046 png_free(png_ptr, png_ptr->quantize_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001047#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001048
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001049 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001050 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001051 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001052
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001053#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001054 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001055 if (png_ptr->free_me & PNG_FREE_TRNS)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001056 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001057 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001058#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001059
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001060#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001061 if (png_ptr->free_me & PNG_FREE_HIST)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001062 png_free(png_ptr, png_ptr->hist);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001063 png_ptr->free_me &= ~PNG_FREE_HIST;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001064#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001065
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001066 inflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001067
Guy Schalnat6d764711995-12-19 03:22:19 -06001068#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001069 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001070#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001071
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001072#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1073#ifdef PNG_TEXT_SUPPORTED
1074 png_free(png_ptr, png_ptr->current_text);
1075#endif /* PNG_TEXT_SUPPORTED */
1076#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1077
Guy Schalnate5a37791996-06-05 15:50:50 -05001078 /* Save the important info out of the png_struct, in case it is
1079 * being used again.
1080 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001081#ifdef PNG_SETJMP_SUPPORTED
John Bowlere6dc85b2011-04-27 14:47:15 -05001082 png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001083#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001084
1085 error_fn = png_ptr->error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -05001086#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001087 warning_fn = png_ptr->warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -05001088#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001089 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001090#ifdef PNG_USER_MEM_SUPPORTED
1091 free_fn = png_ptr->free_fn;
1092#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001093
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001094 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001095
1096 png_ptr->error_fn = error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -05001097#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001098 png_ptr->warning_fn = warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -05001099#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001100 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001101#ifdef PNG_USER_MEM_SUPPORTED
1102 png_ptr->free_fn = free_fn;
1103#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001104
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001105#ifdef PNG_SETJMP_SUPPORTED
John Bowlere6dc85b2011-04-27 14:47:15 -05001106 png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001107#endif
1108
Guy Schalnat0d580581995-07-20 02:43:20 -05001109}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001110
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001111void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001112png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1113{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001114 if (png_ptr == NULL)
1115 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001116
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001117 png_ptr->read_row_fn = read_row_fn;
1118}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001119
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001120
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001121#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001122#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001123void PNGAPI
1124png_read_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001125 int transforms,
1126 voidp params)
1127{
1128 int row;
1129
John Bowler6a6d79f2011-02-12 08:56:40 -06001130 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001131 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001132
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001133 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001134 * PNG file before the first IDAT (image data chunk).
1135 */
1136 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001137 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
1138 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001139
1140 /* -------------- image transformations start here ------------------- */
1141
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001142#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -05001143 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001144 */
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -05001145 if (transforms & PNG_TRANSFORM_SCALE_16)
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001146 {
Glenn Randers-Pehrson6da2f2d2011-06-17 23:07:16 -05001147 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
1148 * did in earlier versions, while "scale_16" is now more accurate.
1149 */
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001150 png_set_scale_16(png_ptr);
1151 }
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001152#endif
1153
1154#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
John Bowler8d261262011-06-18 13:37:11 -05001155 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
1156 * latter by doing SCALE first. This is ok and allows apps not to check for
1157 * which is supported to get the right answer.
1158 */
1159 if (transforms & PNG_TRANSFORM_STRIP_16)
1160 png_set_strip_16(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001161#endif
1162
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001163#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001164 /* Strip alpha bytes from the input data without combining with
1165 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001166 */
1167 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001168 png_set_strip_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001169#endif
1170
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001171#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001172 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001173 * byte into separate bytes (useful for paletted and grayscale images).
1174 */
1175 if (transforms & PNG_TRANSFORM_PACKING)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001176 png_set_packing(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001177#endif
1178
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001179#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001180 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001181 * (not useful if you are using png_set_packing).
1182 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001183 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001184 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001185#endif
1186
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001187#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001188 /* Expand paletted colors into true RGB triplets
1189 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1190 * Expand paletted or RGB images with transparency to full alpha
1191 * channels so the data will be available as RGBA quartets.
1192 */
1193 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001194 if ((png_ptr->bit_depth < 8) ||
1195 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1196 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001197 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001198#endif
1199
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001200 /* We don't handle background color or gamma transformation or quantizing.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001201 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001202
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001203#ifdef PNG_READ_INVERT_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001204 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001205 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001206 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001207 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001208#endif
1209
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001210#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001211 /* If you want to shift the pixel values from the range [0,255] or
1212 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1213 * colors were originally in:
1214 */
1215 if ((transforms & PNG_TRANSFORM_SHIFT)
1216 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1217 {
1218 png_color_8p sig_bit;
1219
1220 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1221 png_set_shift(png_ptr, sig_bit);
1222 }
1223#endif
1224
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001225#ifdef PNG_READ_BGR_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001226 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001227 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001228 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001229#endif
1230
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001231#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001232 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001233 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001234 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001235#endif
1236
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001237#ifdef PNG_READ_SWAP_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -05001238 /* Swap bytes of 16-bit files to least significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001239 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001240 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001241#endif
1242
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001243/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001244#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001245 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001246 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001247 png_set_invert_alpha(png_ptr);
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001248#endif
1249
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001250/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001251#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001252 /* Expand grayscale image to RGB */
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001253 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001254 png_set_gray_to_rgb(png_ptr);
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001255#endif
1256
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001257/* Added at libpng-1.5.4 */
John Bowler96cec0e2011-05-08 22:48:12 -05001258#ifdef PNG_READ_EXPAND_16_SUPPORTED
1259 if (transforms & PNG_TRANSFORM_EXPAND_16)
1260 png_set_expand_16(png_ptr);
1261#endif
1262
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001263 /* We don't handle adding filler bytes */
1264
John Bowler6a6d79f2011-02-12 08:56:40 -06001265 /* We use png_read_image and rely on that for interlace handling, but we also
1266 * call png_read_update_info therefore must turn on interlace handling now:
1267 */
1268 (void)png_set_interlace_handling(png_ptr);
1269
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001270 /* Optional call to gamma correct and add the background to the palette
1271 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001272 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001273 */
1274 png_read_update_info(png_ptr, info_ptr);
1275
1276 /* -------------- image transformations end here ------------------- */
1277
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001278 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001279 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001280 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001281 png_uint_32 iptr;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001282
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001283 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001284 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001285 for (iptr=0; iptr<info_ptr->height; iptr++)
1286 info_ptr->row_pointers[iptr] = NULL;
1287
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001288 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001289
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001290 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001291 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001292 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001293 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001294
1295 png_read_image(png_ptr, info_ptr->row_pointers);
1296 info_ptr->valid |= PNG_INFO_IDAT;
1297
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001298 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001299 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001300
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001301 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1302 PNG_UNUSED(params)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001303
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001304}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001305#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001306#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001307#endif /* PNG_READ_SUPPORTED */