blob: b550be69f30dd607b1166049f4100713b7188d32 [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-Pehrson668af4e2009-06-24 06:35:59 -05004 * Last changed in libpng 1.4.0 [June 24, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 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-Pehrson3e61d792009-06-24 09:31:28 -05009 * This code is released under the zlib-libpng license.
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050010 * For conditions of distribution and use, see copyright notice, disclaimer,
11 * 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
Guy Schalnat0d580581995-07-20 02:43:20 -050017#include "png.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060018#if defined(PNG_READ_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050019#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060020
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050021
Andreas Dilger47a0c421997-05-16 02:46:07 -050022/* Create a PNG structure for reading, and allocate any memory needed. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050023png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -050024png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060025 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -050026{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050027
28#ifdef PNG_USER_MEM_SUPPORTED
29 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050030 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050031}
32
33/* Alternate create PNG structure for reading, and allocate any memory needed. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050034png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050035png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
36 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
37 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
38{
39#endif /* PNG_USER_MEM_SUPPORTED */
40
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -060041#ifdef PNG_SETJMP_SUPPORTED
42 volatile
43#endif
44 png_structp png_ptr;
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050045 int png_cleanup_needed = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060046
47#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060048#ifdef USE_FAR_KEYWORD
49 jmp_buf jmpbuf;
50#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060051#endif
52
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050053 int i;
54
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050055 png_debug(1, "in png_create_read_struct");
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-Pehrsonbeb572e2006-08-19 13:59:24 -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-Pehrson272489d2004-08-04 06:34:52 -050066#ifdef PNG_SET_USER_LIMITS_SUPPORTED
67 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
68 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
69#endif
70
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050071#ifdef PNG_SETJMP_SUPPORTED
72/* Applications that neglect to set up their own setjmp() and then
73 encounter a png_error() will longjmp here. Since the jmpbuf is
74 then meaningless we abort instead of returning. */
75#ifdef USE_FAR_KEYWORD
76 if (setjmp(jmpbuf))
77#else
78 if (setjmp(png_ptr->jmpbuf))
79#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050080 PNG_ABORT();
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050081#endif
82
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050083#ifdef PNG_USER_MEM_SUPPORTED
84 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060085#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050086
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060087 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnat6d764711995-12-19 03:22:19 -060088
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050089 if (user_png_ver)
Guy Schalnate5a37791996-06-05 15:50:50 -050090 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050091 i = 0;
92 do
93 {
94 if (user_png_ver[i] != png_libpng_ver[i])
95 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
96 } while (png_libpng_ver[i++]);
97 }
98 else
99 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
Glenn Randers-Pehrsond60c8862009-06-15 21:56:14 -0500100
Guy Schalnate5a37791996-06-05 15:50:50 -0500101
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500102 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
103 {
104 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
105 * we must recompile any applications that use any older library version.
106 * For versions after libpng 1.0, we will be compatible, so we need
107 * only check the first digit.
108 */
109 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
110 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
111 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
112 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500113#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500114 char msg[80];
115 if (user_png_ver)
116 {
117 png_snprintf(msg, 80,
118 "Application was compiled with png.h from libpng-%.20s",
119 user_png_ver);
120 png_warning(png_ptr, msg);
121 }
122 png_snprintf(msg, 80,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500123 "Application is running with png.c from libpng-%.20s",
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500124 png_libpng_ver);
125 png_warning(png_ptr, msg);
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500126#endif
127#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500128 png_ptr->flags = 0;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500129#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500130 png_warning(png_ptr,
131 "Incompatible libpng version in application and library");
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500132
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500133 png_cleanup_needed = 1;
134 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500135 }
136
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500137 if (!png_cleanup_needed)
138 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500139 /* Initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500140 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500141 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500142 png_ptr->zbuf_size);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500143 if (png_ptr->zbuf == NULL)
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500144 png_cleanup_needed = 1;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500145 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600146 png_ptr->zstream.zalloc = png_zalloc;
147 png_ptr->zstream.zfree = png_zfree;
148 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500149
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500150 if (!png_cleanup_needed)
151 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500152 switch (inflateInit(&png_ptr->zstream))
153 {
154 case Z_OK: /* Do nothing */ break;
155 case Z_MEM_ERROR:
156 case Z_STREAM_ERROR: png_warning(png_ptr, "zlib memory error");
157 png_cleanup_needed = 1; break;
158 case Z_VERSION_ERROR: png_warning(png_ptr, "zlib version error");
159 png_cleanup_needed = 1; break;
160 default: png_warning(png_ptr, "Unknown zlib error");
161 png_cleanup_needed = 1;
162 }
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500163 }
164
165 if (png_cleanup_needed)
166 {
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500167 /* Clean up PNG structure and deallocate any memory. */
168 png_free(png_ptr, png_ptr->zbuf);
169 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500170#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500171 png_destroy_struct_2((png_voidp)png_ptr,
172 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500173#else
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500174 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500175#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500176 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500177 }
178
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600179 png_ptr->zstream.next_out = png_ptr->zbuf;
180 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500181
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500182 png_set_read_fn(png_ptr, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500183
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500184
Guy Schalnate5a37791996-06-05 15:50:50 -0500185 return (png_ptr);
186}
187
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500188
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500189#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600190/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -0600191 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600192 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500193 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600194 * via png_set_sig_bytes(), and we will only check the remaining bytes
195 * here. The application can then have access to the signature bytes we
196 * read if it is determined that this isn't a valid PNG file.
197 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500198void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600199png_read_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500200{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500201 if (png_ptr == NULL || info_ptr == NULL)
202 return;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500203 png_debug(1, "in png_read_info");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600204 /* If we haven't checked all of the PNG signature bytes, do so now. */
205 if (png_ptr->sig_bytes < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500206 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500207 png_size_t num_checked = png_ptr->sig_bytes,
208 num_to_check = 8 - num_checked;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600209
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500210#ifdef PNG_IO_STATE_SUPPORTED
211 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
212#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500213
Andreas Dilger47a0c421997-05-16 02:46:07 -0500214 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600215 png_ptr->sig_bytes = 8;
216
217 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
218 {
219 if (num_checked < 4 &&
220 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
221 png_error(png_ptr, "Not a PNG file");
222 else
223 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
224 }
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600225 if (num_checked < 3)
226 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500227 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500228
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500229 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500230 {
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600231#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500232 PNG_CONST PNG_IHDR;
233 PNG_CONST PNG_IDAT;
234 PNG_CONST PNG_IEND;
235 PNG_CONST PNG_PLTE;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600236#if defined(PNG_READ_bKGD_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500237 PNG_CONST PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600238#endif
239#if defined(PNG_READ_cHRM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500240 PNG_CONST PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600241#endif
242#if defined(PNG_READ_gAMA_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500243 PNG_CONST PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600244#endif
245#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500246 PNG_CONST PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600247#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600248#if defined(PNG_READ_iCCP_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500249 PNG_CONST PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600250#endif
251#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500252 PNG_CONST PNG_iTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600253#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600254#if defined(PNG_READ_oFFs_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500255 PNG_CONST PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600256#endif
257#if defined(PNG_READ_pCAL_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500258 PNG_CONST PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600259#endif
260#if defined(PNG_READ_pHYs_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500261 PNG_CONST PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600262#endif
263#if defined(PNG_READ_sBIT_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500264 PNG_CONST PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600265#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600266#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500267 PNG_CONST PNG_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600268#endif
269#if defined(PNG_READ_sPLT_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500270 PNG_CONST PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600271#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600272#if defined(PNG_READ_sRGB_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500273 PNG_CONST PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600274#endif
275#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500276 PNG_CONST PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600277#endif
278#if defined(PNG_READ_tIME_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500279 PNG_CONST PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600280#endif
281#if defined(PNG_READ_tRNS_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500282 PNG_CONST PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600283#endif
284#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500285 PNG_CONST PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600286#endif
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600287#endif /* PNG_USE_LOCAL_ARRAYS */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500288 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500289 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500290
291 /* This should be a binary subdivision search or a hash for
292 * matching the chunk name rather than a linear search.
293 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500294 if (!png_memcmp(chunk_name, png_IDAT, 4))
295 if (png_ptr->mode & PNG_AFTER_IDAT)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500296 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
297
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500298 if (!png_memcmp(chunk_name, png_IHDR, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600299 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500300 else if (!png_memcmp(chunk_name, png_IEND, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600301 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600302#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500303 else if (png_handle_as_unknown(png_ptr, chunk_name))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600304 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500305 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600306 png_ptr->mode |= PNG_HAVE_IDAT;
307 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500308 if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600309 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500310 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600311 {
312 if (!(png_ptr->mode & PNG_HAVE_IHDR))
313 png_error(png_ptr, "Missing IHDR before IDAT");
314 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
315 !(png_ptr->mode & PNG_HAVE_PLTE))
316 png_error(png_ptr, "Missing PLTE before IDAT");
317 break;
318 }
319 }
320#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500321 else if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600322 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500323 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500324 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500325 if (!(png_ptr->mode & PNG_HAVE_IHDR))
326 png_error(png_ptr, "Missing IHDR before IDAT");
327 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
328 !(png_ptr->mode & PNG_HAVE_PLTE))
329 png_error(png_ptr, "Missing PLTE before IDAT");
330
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500331 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500332 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500333 break;
334 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500335#if defined(PNG_READ_bKGD_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500336 else if (!png_memcmp(chunk_name, png_bKGD, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337 png_handle_bKGD(png_ptr, info_ptr, length);
338#endif
339#if defined(PNG_READ_cHRM_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500340 else if (!png_memcmp(chunk_name, png_cHRM, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500341 png_handle_cHRM(png_ptr, info_ptr, length);
342#endif
343#if defined(PNG_READ_gAMA_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500344 else if (!png_memcmp(chunk_name, png_gAMA, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500345 png_handle_gAMA(png_ptr, info_ptr, length);
346#endif
347#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500348 else if (!png_memcmp(chunk_name, png_hIST, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500349 png_handle_hIST(png_ptr, info_ptr, length);
350#endif
351#if defined(PNG_READ_oFFs_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500352 else if (!png_memcmp(chunk_name, png_oFFs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500353 png_handle_oFFs(png_ptr, info_ptr, length);
354#endif
355#if defined(PNG_READ_pCAL_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500356 else if (!png_memcmp(chunk_name, png_pCAL, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500357 png_handle_pCAL(png_ptr, info_ptr, length);
358#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600359#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500360 else if (!png_memcmp(chunk_name, png_sCAL, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600361 png_handle_sCAL(png_ptr, info_ptr, length);
362#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363#if defined(PNG_READ_pHYs_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500364 else if (!png_memcmp(chunk_name, png_pHYs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500365 png_handle_pHYs(png_ptr, info_ptr, length);
366#endif
367#if defined(PNG_READ_sBIT_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500368 else if (!png_memcmp(chunk_name, png_sBIT, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369 png_handle_sBIT(png_ptr, info_ptr, length);
370#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600371#if defined(PNG_READ_sRGB_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500372 else if (!png_memcmp(chunk_name, png_sRGB, 4))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600373 png_handle_sRGB(png_ptr, info_ptr, length);
374#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600375#if defined(PNG_READ_iCCP_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500376 else if (!png_memcmp(chunk_name, png_iCCP, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600377 png_handle_iCCP(png_ptr, info_ptr, length);
378#endif
379#if defined(PNG_READ_sPLT_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500380 else if (!png_memcmp(chunk_name, png_sPLT, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600381 png_handle_sPLT(png_ptr, info_ptr, length);
382#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500384 else if (!png_memcmp(chunk_name, png_tEXt, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385 png_handle_tEXt(png_ptr, info_ptr, length);
386#endif
387#if defined(PNG_READ_tIME_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500388 else if (!png_memcmp(chunk_name, png_tIME, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500389 png_handle_tIME(png_ptr, info_ptr, length);
390#endif
391#if defined(PNG_READ_tRNS_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500392 else if (!png_memcmp(chunk_name, png_tRNS, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500393 png_handle_tRNS(png_ptr, info_ptr, length);
394#endif
395#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500396 else if (!png_memcmp(chunk_name, png_zTXt, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 png_handle_zTXt(png_ptr, info_ptr, length);
398#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600399#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500400 else if (!png_memcmp(chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600401 png_handle_iTXt(png_ptr, info_ptr, length);
402#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500403 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600404 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500405 }
406}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500407#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500408
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500409/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500410void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600411png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500412{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500413 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500414 if (png_ptr == NULL)
415 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500416 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500417 png_read_start_row(png_ptr);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600418 else
419 png_warning(png_ptr,
420 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600421 png_read_transform_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500422}
423
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500424#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600425/* Initialize palette, background, etc, after transformations
426 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500427 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600428 * If the user doesn't call this, we will do it ourselves.
429 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500430void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600431png_start_read_image(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500432{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500433 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500434 if (png_ptr == NULL)
435 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500436 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500437 png_read_start_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500438}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500439#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500440
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500441#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500442void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600443png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500444{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600445#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500446 PNG_CONST PNG_IDAT;
447 PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
448 0xff};
449 PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600450#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500451 int ret;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500452 if (png_ptr == NULL)
453 return;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500454 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500455 (unsigned long) png_ptr->row_number, png_ptr->pass);
Guy Schalnate5a37791996-06-05 15:50:50 -0500456 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500457 png_read_start_row(png_ptr);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500458 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
459 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500460 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500461#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
462 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500463 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500464#endif
465#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
466 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500467 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500468#endif
469#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
470 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500471 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500472#endif
473#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
474 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500475 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500476#endif
477#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
478 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500479 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500480#endif
481#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
482 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500483 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500484#endif
485#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
486 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500487 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500488#endif
489 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500490
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500491#if defined(PNG_READ_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500492 /* If interlaced and we do not need a new row, combine row and return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500493 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
494 {
495 switch (png_ptr->pass)
496 {
497 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600498 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500499 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500500 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500501 png_combine_row(png_ptr, dsp_row,
502 png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600503 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500504 return;
505 }
506 break;
507 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600508 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500509 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500510 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500511 png_combine_row(png_ptr, dsp_row,
512 png_pass_dsp_mask[png_ptr->pass]);
513 png_read_finish_row(png_ptr);
514 return;
515 }
516 break;
517 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600518 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500519 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500520 if (dsp_row != NULL && (png_ptr->row_number & 4))
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600521 png_combine_row(png_ptr, dsp_row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500522 png_pass_dsp_mask[png_ptr->pass]);
523 png_read_finish_row(png_ptr);
524 return;
525 }
526 break;
527 case 3:
528 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
529 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500530 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500531 png_combine_row(png_ptr, dsp_row,
532 png_pass_dsp_mask[png_ptr->pass]);
533 png_read_finish_row(png_ptr);
534 return;
535 }
536 break;
537 case 4:
538 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600539 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500540 if (dsp_row != NULL && (png_ptr->row_number & 2))
Guy Schalnat0d580581995-07-20 02:43:20 -0500541 png_combine_row(png_ptr, dsp_row,
542 png_pass_dsp_mask[png_ptr->pass]);
543 png_read_finish_row(png_ptr);
544 return;
545 }
546 break;
547 case 5:
548 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
549 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500550 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500551 png_combine_row(png_ptr, dsp_row,
552 png_pass_dsp_mask[png_ptr->pass]);
553 png_read_finish_row(png_ptr);
554 return;
555 }
556 break;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600557 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500558 if (!(png_ptr->row_number & 1))
559 {
560 png_read_finish_row(png_ptr);
561 return;
562 }
563 break;
564 }
565 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500566#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500567
Guy Schalnate5a37791996-06-05 15:50:50 -0500568 if (!(png_ptr->mode & PNG_HAVE_IDAT))
569 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500570
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600571 png_ptr->zstream.next_out = png_ptr->row_buf;
572 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500573 do
574 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600575 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500576 {
577 while (!png_ptr->idat_size)
578 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600579 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500580
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500581 png_ptr->idat_size = png_read_chunk_header(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600582 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
583 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500584 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600585 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
586 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500587 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600588 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500589 png_crc_read(png_ptr, png_ptr->zbuf,
590 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600591 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500592 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600593 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500594 if (ret == Z_STREAM_END)
595 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600596 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500597 png_ptr->idat_size)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500598 png_benign_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600599 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500600 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600601 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500602 }
603 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600604 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Guy Schalnate5a37791996-06-05 15:50:50 -0500605 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600606
607 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500608
Guy Schalnat0d580581995-07-20 02:43:20 -0500609 png_ptr->row_info.color_type = png_ptr->color_type;
610 png_ptr->row_info.width = png_ptr->iwidth;
611 png_ptr->row_info.channels = png_ptr->channels;
612 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
613 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500614 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
615 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500616
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500617 if (png_ptr->row_buf[0])
Guy Schalnate5a37791996-06-05 15:50:50 -0500618 png_read_filter_row(png_ptr, &(png_ptr->row_info),
619 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
620 (int)(png_ptr->row_buf[0]));
Guy Schalnat0d580581995-07-20 02:43:20 -0500621
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500622 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600623
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600624#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500625 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600626 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
627 {
628 /* Intrapixel differencing */
629 png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
630 }
631#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500632
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500633
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -0600634 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
Guy Schalnat0d580581995-07-20 02:43:20 -0500635 png_do_read_transformations(png_ptr);
636
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500637#if defined(PNG_READ_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500638 /* Blow up interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500639 if (png_ptr->interlaced &&
640 (png_ptr->transformations & PNG_INTERLACE))
641 {
642 if (png_ptr->pass < 6)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500643 /* Old interface (pre-1.0.9):
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500644 * png_do_read_interlace(&(png_ptr->row_info),
645 * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
646 */
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600647 png_do_read_interlace(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500648
Andreas Dilger47a0c421997-05-16 02:46:07 -0500649 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500650 png_combine_row(png_ptr, dsp_row,
651 png_pass_dsp_mask[png_ptr->pass]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500652 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500653 png_combine_row(png_ptr, row,
654 png_pass_mask[png_ptr->pass]);
655 }
656 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500657#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500658 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500659 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500660 png_combine_row(png_ptr, row, 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500661 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500662 png_combine_row(png_ptr, dsp_row, 0xff);
663 }
664 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600665
666 if (png_ptr->read_row_fn != NULL)
667 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500668}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500669#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500670
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500671#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500672/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600673 * and png_set_interlace_handling() has been called, the rows need to
674 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500675 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600676 * called, the rows contents must be initialized to the contents of the
677 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600678 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600679 * "row" holds the actual image, and pixels are placed in it
680 * as they arrive. If the image is displayed after each pass, it will
681 * appear to "sparkle" in. "display_row" can be used to display a
682 * "chunky" progressive image, with finer detail added as it becomes
683 * available. If you do not want this "chunky" display, you may pass
684 * NULL for display_row. If you do not want the sparkle display, and
685 * you have not called png_handle_alpha(), you may pass NULL for rows.
686 * If you have called png_handle_alpha(), and the image has either an
687 * alpha channel or a transparency chunk, you must provide a buffer for
688 * rows. In this case, you do not have to provide a display_row buffer
689 * also, but you may. If the image is not interlaced, or if you have
690 * not called png_set_interlace_handling(), the display_row buffer will
691 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500692 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600693 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600694 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600695
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500696void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600697png_read_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600698 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500699{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600700 png_uint_32 i;
701 png_bytepp rp;
702 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500703
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500704 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500705 if (png_ptr == NULL)
706 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600707 rp = row;
708 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500709 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500710 for (i = 0; i < num_rows; i++)
711 {
712 png_bytep rptr = *rp++;
713 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600714
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500715 png_read_row(png_ptr, rptr, dptr);
716 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500717 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500718 for (i = 0; i < num_rows; i++)
719 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500720 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500721 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500722 rp++;
723 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500724 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500725 for (i = 0; i < num_rows; i++)
726 {
727 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500728 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500729 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500730 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500731}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500732#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500733
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500734#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500735/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500736 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600737 * initialize the image to the current image that PNG will be overlaying.
738 * We set the num_rows again here, in case it was incorrectly set in
739 * png_read_start_row() by a call to png_read_update_info() or
740 * png_start_read_image() if png_set_interlace_handling() wasn't called
741 * prior to either of these functions like it should have been. You can
742 * only call this function once. If you desire to have an image for
743 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500744 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600745 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600746 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500747void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600748png_read_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500749{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500750 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500751 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600752 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500753
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500754 png_debug(1, "in png_read_image");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500755 if (png_ptr == NULL)
756 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500757
758#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -0500759 pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500760#else
761 if (png_ptr->interlaced)
762 png_error(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500763 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500764 pass = 1;
765#endif
766
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500767
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500768 image_height=png_ptr->height;
769 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
Guy Schalnate5a37791996-06-05 15:50:50 -0500770
Guy Schalnat0d580581995-07-20 02:43:20 -0500771 for (j = 0; j < pass; j++)
772 {
773 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500774 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500775 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500776 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500777 rp++;
778 }
779 }
780}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500781#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500782
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500783#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500784/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600785 * file, will verify the end is accurate, and will read any comments
786 * or time information at the end of the file, if info is not NULL.
787 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500788void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600789png_read_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500790{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500791 png_debug(1, "in png_read_end");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500792 if (png_ptr == NULL)
793 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500794 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500795
796 do
797 {
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600798#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500799 PNG_CONST PNG_IHDR;
800 PNG_CONST PNG_IDAT;
801 PNG_CONST PNG_IEND;
802 PNG_CONST PNG_PLTE;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600803#if defined(PNG_READ_bKGD_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500804 PNG_CONST PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600805#endif
806#if defined(PNG_READ_cHRM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500807 PNG_CONST PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600808#endif
809#if defined(PNG_READ_gAMA_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500810 PNG_CONST PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600811#endif
812#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500813 PNG_CONST PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600814#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600815#if defined(PNG_READ_iCCP_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500816 PNG_CONST PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600817#endif
818#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500819 PNG_CONST PNG_iTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600820#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600821#if defined(PNG_READ_oFFs_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500822 PNG_CONST PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600823#endif
824#if defined(PNG_READ_pCAL_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500825 PNG_CONST PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600826#endif
827#if defined(PNG_READ_pHYs_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500828 PNG_CONST PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600829#endif
830#if defined(PNG_READ_sBIT_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500831 PNG_CONST PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600832#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600833#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500834 PNG_CONST PNG_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600835#endif
836#if defined(PNG_READ_sPLT_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500837 PNG_CONST PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600838#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600839#if defined(PNG_READ_sRGB_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500840 PNG_CONST PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600841#endif
842#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500843 PNG_CONST PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600844#endif
845#if defined(PNG_READ_tIME_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500846 PNG_CONST PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600847#endif
848#if defined(PNG_READ_tRNS_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500849 PNG_CONST PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600850#endif
851#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500852 PNG_CONST PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600853#endif
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600854#endif /* PNG_USE_LOCAL_ARRAYS */
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500855 png_uint_32 length = png_read_chunk_header(png_ptr);
856 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500857
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500858 if (!png_memcmp(chunk_name, png_IHDR, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600859 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500860 else if (!png_memcmp(chunk_name, png_IEND, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600861 png_handle_IEND(png_ptr, info_ptr, length);
862#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500863 else if (png_handle_as_unknown(png_ptr, chunk_name))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600864 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500865 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600866 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500867 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500868 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600869 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600870 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500871 if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600872 png_ptr->mode |= PNG_HAVE_PLTE;
873 }
874#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500875 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Guy Schalnat0d580581995-07-20 02:43:20 -0500876 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500877 /* Zero length IDATs are legal after the last IDAT has been
878 * read, but not after other chunks have been read.
879 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500880 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500881 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500882 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500883 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500884 else if (!png_memcmp(chunk_name, png_PLTE, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500885 png_handle_PLTE(png_ptr, info_ptr, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500886#if defined(PNG_READ_bKGD_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500887 else if (!png_memcmp(chunk_name, png_bKGD, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888 png_handle_bKGD(png_ptr, info_ptr, length);
889#endif
890#if defined(PNG_READ_cHRM_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500891 else if (!png_memcmp(chunk_name, png_cHRM, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500892 png_handle_cHRM(png_ptr, info_ptr, length);
893#endif
894#if defined(PNG_READ_gAMA_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500895 else if (!png_memcmp(chunk_name, png_gAMA, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896 png_handle_gAMA(png_ptr, info_ptr, length);
897#endif
898#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500899 else if (!png_memcmp(chunk_name, png_hIST, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500900 png_handle_hIST(png_ptr, info_ptr, length);
901#endif
902#if defined(PNG_READ_oFFs_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500903 else if (!png_memcmp(chunk_name, png_oFFs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500904 png_handle_oFFs(png_ptr, info_ptr, length);
905#endif
906#if defined(PNG_READ_pCAL_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500907 else if (!png_memcmp(chunk_name, png_pCAL, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500908 png_handle_pCAL(png_ptr, info_ptr, length);
909#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600910#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500911 else if (!png_memcmp(chunk_name, png_sCAL, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600912 png_handle_sCAL(png_ptr, info_ptr, length);
913#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500914#if defined(PNG_READ_pHYs_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500915 else if (!png_memcmp(chunk_name, png_pHYs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500916 png_handle_pHYs(png_ptr, info_ptr, length);
917#endif
918#if defined(PNG_READ_sBIT_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500919 else if (!png_memcmp(chunk_name, png_sBIT, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500920 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500921#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600922#if defined(PNG_READ_sRGB_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500923 else if (!png_memcmp(chunk_name, png_sRGB, 4))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600924 png_handle_sRGB(png_ptr, info_ptr, length);
925#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600926#if defined(PNG_READ_iCCP_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500927 else if (!png_memcmp(chunk_name, png_iCCP, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600928 png_handle_iCCP(png_ptr, info_ptr, length);
929#endif
930#if defined(PNG_READ_sPLT_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500931 else if (!png_memcmp(chunk_name, png_sPLT, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600932 png_handle_sPLT(png_ptr, info_ptr, length);
933#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500934#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500935 else if (!png_memcmp(chunk_name, png_tEXt, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600936 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500937#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500938#if defined(PNG_READ_tIME_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500939 else if (!png_memcmp(chunk_name, png_tIME, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500940 png_handle_tIME(png_ptr, info_ptr, length);
941#endif
942#if defined(PNG_READ_tRNS_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500943 else if (!png_memcmp(chunk_name, png_tRNS, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500944 png_handle_tRNS(png_ptr, info_ptr, length);
945#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500946#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500947 else if (!png_memcmp(chunk_name, png_zTXt, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600948 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500949#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600950#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500951 else if (!png_memcmp(chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600952 png_handle_iTXt(png_ptr, info_ptr, length);
953#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500954 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600955 png_handle_unknown(png_ptr, info_ptr, length);
956 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500957}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500958#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500959
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500960/* Free all memory used by the read */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500961void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500962png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600963 png_infopp end_info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500964{
965 png_structp png_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600966 png_infop info_ptr = NULL, end_info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500967#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500968 png_free_ptr free_fn = NULL;
969 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600970#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500971
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500972 png_debug(1, "in png_destroy_read_struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500973 if (png_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500974 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500975 if (png_ptr == NULL)
976 return;
977
978#ifdef PNG_USER_MEM_SUPPORTED
979 free_fn = png_ptr->free_fn;
980 mem_ptr = png_ptr->mem_ptr;
981#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500982
Andreas Dilger47a0c421997-05-16 02:46:07 -0500983 if (info_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500984 info_ptr = *info_ptr_ptr;
985
Andreas Dilger47a0c421997-05-16 02:46:07 -0500986 if (end_info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600987 end_info_ptr = *end_info_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500988
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600989 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500990
Andreas Dilger47a0c421997-05-16 02:46:07 -0500991 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500992 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600993#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600994 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600995#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500996
997#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500998 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
999 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001000#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001001 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001002#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001003 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001004 }
1005
Andreas Dilger47a0c421997-05-16 02:46:07 -05001006 if (end_info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001007 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001008#if defined(PNG_READ_TEXT_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001009 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001010#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001011#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001012 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
1013 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001014#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001015 png_destroy_struct((png_voidp)end_info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001016#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001017 *end_info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001018 }
1019
Andreas Dilger47a0c421997-05-16 02:46:07 -05001020 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001021 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001022#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001023 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1024 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001025#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001026 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001027#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001028 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001029 }
1030}
1031
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001032/* Free all memory used by the read (old method) */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001033void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001034png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001035{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001036#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001037 jmp_buf tmp_jmp;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001038#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001039 png_error_ptr error_fn;
1040 png_error_ptr warning_fn;
1041 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001042#ifdef PNG_USER_MEM_SUPPORTED
1043 png_free_ptr free_fn;
1044#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001045
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001046 png_debug(1, "in png_read_destroy");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001047 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001048 png_info_destroy(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001049
Andreas Dilger47a0c421997-05-16 02:46:07 -05001050 if (end_info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001051 png_info_destroy(png_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001052
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001053 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -05001054 png_free(png_ptr, png_ptr->big_row_buf);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001055 png_free(png_ptr, png_ptr->prev_row);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001056 png_free(png_ptr, png_ptr->chunkdata);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001057#if defined(PNG_READ_DITHER_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001058 png_free(png_ptr, png_ptr->palette_lookup);
1059 png_free(png_ptr, png_ptr->dither_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001060#endif
1061#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001062 png_free(png_ptr, png_ptr->gamma_table);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001063#endif
1064#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001065 png_free(png_ptr, png_ptr->gamma_from_1);
1066 png_free(png_ptr, png_ptr->gamma_to_1);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001067#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001068#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001069 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001070 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001071 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001072#else
1073 if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
1074 png_zfree(png_ptr, png_ptr->palette);
1075 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
1076#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001077#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001078 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001079#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001080 if (png_ptr->free_me & PNG_FREE_TRNS)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001081 png_free(png_ptr, png_ptr->trans);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001082 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001083#else
1084 if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
1085 png_free(png_ptr, png_ptr->trans);
1086 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
1087#endif
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001088#endif
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001089#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001090#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001091 if (png_ptr->free_me & PNG_FREE_HIST)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001092 png_free(png_ptr, png_ptr->hist);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001093 png_ptr->free_me &= ~PNG_FREE_HIST;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001094#else
1095 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
1096 png_free(png_ptr, png_ptr->hist);
1097 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
1098#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001099#endif
1100#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001101 if (png_ptr->gamma_16_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001102 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001103 int i;
1104 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001105 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001106 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001107 png_free(png_ptr, png_ptr->gamma_16_table[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001108 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001109 png_free(png_ptr, png_ptr->gamma_16_table);
Guy Schalnat0d580581995-07-20 02:43:20 -05001110 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001111#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001112 if (png_ptr->gamma_16_from_1 != NULL)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001113 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001114 int i;
1115 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001116 for (i = 0; i < istop; i++)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001117 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001118 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001119 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001120 png_free(png_ptr, png_ptr->gamma_16_from_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001121 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001122 if (png_ptr->gamma_16_to_1 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001123 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001124 int i;
1125 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001126 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001127 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001128 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001129 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001130 png_free(png_ptr, png_ptr->gamma_16_to_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001131 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001132#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001133#endif
1134#if defined(PNG_TIME_RFC1123_SUPPORTED)
1135 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001136#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001137
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001138 inflateEnd(&png_ptr->zstream);
Guy Schalnat6d764711995-12-19 03:22:19 -06001139#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001140 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001141#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001142
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001143#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1144#ifdef PNG_TEXT_SUPPORTED
1145 png_free(png_ptr, png_ptr->current_text);
1146#endif /* PNG_TEXT_SUPPORTED */
1147#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1148
Guy Schalnate5a37791996-06-05 15:50:50 -05001149 /* Save the important info out of the png_struct, in case it is
1150 * being used again.
1151 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001152#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001153 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001154#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001155
1156 error_fn = png_ptr->error_fn;
1157 warning_fn = png_ptr->warning_fn;
1158 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001159#ifdef PNG_USER_MEM_SUPPORTED
1160 free_fn = png_ptr->free_fn;
1161#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001162
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001163 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001164
1165 png_ptr->error_fn = error_fn;
1166 png_ptr->warning_fn = warning_fn;
1167 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001168#ifdef PNG_USER_MEM_SUPPORTED
1169 png_ptr->free_fn = free_fn;
1170#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001171
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001172#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001173 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001174#endif
1175
Guy Schalnat0d580581995-07-20 02:43:20 -05001176}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001177
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001178void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001179png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1180{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001181 if (png_ptr == NULL)
1182 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001183 png_ptr->read_row_fn = read_row_fn;
1184}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001185
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001186
1187#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001188#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001189void PNGAPI
1190png_read_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001191 int transforms,
1192 voidp params)
1193{
1194 int row;
1195
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001196 if (png_ptr == NULL)
1197 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001198#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001199 /* Invert the alpha channel from opacity to transparency
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001200 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001201 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1202 png_set_invert_alpha(png_ptr);
1203#endif
1204
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001205 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001206 * PNG file before the first IDAT (image data chunk).
1207 */
1208 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001209 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
1210 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001211
1212 /* -------------- image transformations start here ------------------- */
1213
1214#if defined(PNG_READ_16_TO_8_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001215 /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001216 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001217 if (transforms & PNG_TRANSFORM_STRIP_16)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001218 png_set_strip_16(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001219#endif
1220
1221#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001222 /* Strip alpha bytes from the input data without combining with
1223 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001224 */
1225 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001226 png_set_strip_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001227#endif
1228
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001229#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001230 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001231 * byte into separate bytes (useful for paletted and grayscale images).
1232 */
1233 if (transforms & PNG_TRANSFORM_PACKING)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001234 png_set_packing(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001235#endif
1236
1237#if defined(PNG_READ_PACKSWAP_SUPPORTED)
1238 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001239 * (not useful if you are using png_set_packing).
1240 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001241 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001242 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001243#endif
1244
1245#if defined(PNG_READ_EXPAND_SUPPORTED)
1246 /* Expand paletted colors into true RGB triplets
1247 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1248 * Expand paletted or RGB images with transparency to full alpha
1249 * channels so the data will be available as RGBA quartets.
1250 */
1251 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001252 if ((png_ptr->bit_depth < 8) ||
1253 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1254 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001255 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001256#endif
1257
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001258 /* We don't handle background color or gamma transformation or dithering.
1259 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001260
1261#if defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001262 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001263 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001264 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001265 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001266#endif
1267
1268#if defined(PNG_READ_SHIFT_SUPPORTED)
1269 /* If you want to shift the pixel values from the range [0,255] or
1270 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1271 * colors were originally in:
1272 */
1273 if ((transforms & PNG_TRANSFORM_SHIFT)
1274 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1275 {
1276 png_color_8p sig_bit;
1277
1278 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1279 png_set_shift(png_ptr, sig_bit);
1280 }
1281#endif
1282
1283#if defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001284 /* Flip the RGB pixels to BGR (or RGBA to BGRA)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001285 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001286 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001287 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001288#endif
1289
1290#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001291 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001292 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001293 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1294 png_set_swap_alpha(png_ptr);
1295#endif
1296
1297#if defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001298 /* Swap bytes of 16 bit files to least significant byte first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001299 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001300 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001301 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001302#endif
1303
1304 /* We don't handle adding filler bytes */
1305
1306 /* Optional call to gamma correct and add the background to the palette
1307 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001308 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001309 */
1310 png_read_update_info(png_ptr, info_ptr);
1311
1312 /* -------------- image transformations end here ------------------- */
1313
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001314#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001315 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001316#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001317 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001318 {
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001319#ifdef PNG_CALLOC_SUPPORTED
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001320 info_ptr->row_pointers = (png_bytepp)png_calloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001321 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001322#else
1323 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
1324 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -05001325 png_memset(info_ptr->row_pointers, 0, info_ptr->height
1326 * png_sizeof(png_bytep));
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001327#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001328#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001329 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001330#endif
1331 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001332 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001333 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001334 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001335
1336 png_read_image(png_ptr, info_ptr->row_pointers);
1337 info_ptr->valid |= PNG_INFO_IDAT;
1338
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001339 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001340 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001341
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001342 transforms = transforms; /* Quiet compiler warnings */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001343 params = params;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001344
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001345}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001346#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001347#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001348#endif /* PNG_READ_SUPPORTED */