blob: 4da7224ed9d3613eca364894870e5290744a584d [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-Pehrsonc81bb8a2009-08-15 22:02:26 -05004 * Last changed in libpng 1.4.0 [August 16, 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-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
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-Pehrsonda009802009-08-15 13:25:47 -050056
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050057#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060058 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050059 malloc_fn, mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050060#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060061 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050062#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060063 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050064 return (NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050065
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -050066 /* Added at libpng-1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -050067#ifdef PNG_SET_USER_LIMITS_SUPPORTED
68 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
69 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
70#endif
71
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050072#ifdef PNG_SETJMP_SUPPORTED
73/* Applications that neglect to set up their own setjmp() and then
74 encounter a png_error() will longjmp here. Since the jmpbuf is
75 then meaningless we abort instead of returning. */
76#ifdef USE_FAR_KEYWORD
77 if (setjmp(jmpbuf))
78#else
79 if (setjmp(png_ptr->jmpbuf))
80#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050081 PNG_ABORT();
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050082#endif
83
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050084#ifdef PNG_USER_MEM_SUPPORTED
85 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060086#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050087
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060088 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnat6d764711995-12-19 03:22:19 -060089
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050090 if (user_png_ver)
Guy Schalnate5a37791996-06-05 15:50:50 -050091 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050092 i = 0;
93 do
94 {
95 if (user_png_ver[i] != png_libpng_ver[i])
96 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
97 } while (png_libpng_ver[i++]);
98 }
99 else
100 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
Glenn Randers-Pehrsond60c8862009-06-15 21:56:14 -0500101
Guy Schalnate5a37791996-06-05 15:50:50 -0500102
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500103 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
104 {
105 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
106 * we must recompile any applications that use any older library version.
107 * For versions after libpng 1.0, we will be compatible, so we need
108 * only check the first digit.
109 */
110 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
111 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
112 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
113 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500114#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500115 char msg[80];
116 if (user_png_ver)
117 {
118 png_snprintf(msg, 80,
119 "Application was compiled with png.h from libpng-%.20s",
120 user_png_ver);
121 png_warning(png_ptr, msg);
122 }
123 png_snprintf(msg, 80,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500124 "Application is running with png.c from libpng-%.20s",
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500125 png_libpng_ver);
126 png_warning(png_ptr, msg);
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500127#endif
128#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500129 png_ptr->flags = 0;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500130#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500131 png_warning(png_ptr,
132 "Incompatible libpng version in application and library");
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500133
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500134 png_cleanup_needed = 1;
135 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500136 }
137
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500138 if (!png_cleanup_needed)
139 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500140 /* Initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500141 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500142 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500143 png_ptr->zbuf_size);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500144 if (png_ptr->zbuf == NULL)
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500145 png_cleanup_needed = 1;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500146 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600147 png_ptr->zstream.zalloc = png_zalloc;
148 png_ptr->zstream.zfree = png_zfree;
149 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500150
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500151 if (!png_cleanup_needed)
152 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500153 switch (inflateInit(&png_ptr->zstream))
154 {
155 case Z_OK: /* Do nothing */ break;
156 case Z_MEM_ERROR:
157 case Z_STREAM_ERROR: png_warning(png_ptr, "zlib memory error");
158 png_cleanup_needed = 1; break;
159 case Z_VERSION_ERROR: png_warning(png_ptr, "zlib version error");
160 png_cleanup_needed = 1; break;
161 default: png_warning(png_ptr, "Unknown zlib error");
162 png_cleanup_needed = 1;
163 }
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500164 }
165
166 if (png_cleanup_needed)
167 {
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500168 /* Clean up PNG structure and deallocate any memory. */
169 png_free(png_ptr, png_ptr->zbuf);
170 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500171#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500172 png_destroy_struct_2((png_voidp)png_ptr,
173 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500174#else
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500175 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500176#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500177 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500178 }
179
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600180 png_ptr->zstream.next_out = png_ptr->zbuf;
181 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500182
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500183 png_set_read_fn(png_ptr, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500184
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500185
Guy Schalnate5a37791996-06-05 15:50:50 -0500186 return (png_ptr);
187}
188
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500189
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500190#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600191/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -0600192 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600193 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500194 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600195 * via png_set_sig_bytes(), and we will only check the remaining bytes
196 * here. The application can then have access to the signature bytes we
197 * read if it is determined that this isn't a valid PNG file.
198 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500199void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600200png_read_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500201{
Glenn Randers-Pehrsonc81bb8a2009-08-15 22:02:26 -0500202 png_debug(1, "in png_read_info");
203
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500204 if (png_ptr == NULL || info_ptr == NULL)
205 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500206
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600207 /* If we haven't checked all of the PNG signature bytes, do so now. */
208 if (png_ptr->sig_bytes < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500209 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500210 png_size_t num_checked = png_ptr->sig_bytes,
211 num_to_check = 8 - num_checked;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600212
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500213#ifdef PNG_IO_STATE_SUPPORTED
214 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
215#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500216
Andreas Dilger47a0c421997-05-16 02:46:07 -0500217 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600218 png_ptr->sig_bytes = 8;
219
220 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
221 {
222 if (num_checked < 4 &&
223 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
224 png_error(png_ptr, "Not a PNG file");
225 else
226 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
227 }
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600228 if (num_checked < 3)
229 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500230 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500231
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500232 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500233 {
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600234#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500235 PNG_CONST PNG_IHDR;
236 PNG_CONST PNG_IDAT;
237 PNG_CONST PNG_IEND;
238 PNG_CONST PNG_PLTE;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600239#if defined(PNG_READ_bKGD_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500240 PNG_CONST PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600241#endif
242#if defined(PNG_READ_cHRM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500243 PNG_CONST PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600244#endif
245#if defined(PNG_READ_gAMA_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500246 PNG_CONST PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600247#endif
248#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500249 PNG_CONST PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600250#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600251#if defined(PNG_READ_iCCP_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500252 PNG_CONST PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600253#endif
254#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500255 PNG_CONST PNG_iTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600256#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600257#if defined(PNG_READ_oFFs_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500258 PNG_CONST PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600259#endif
260#if defined(PNG_READ_pCAL_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500261 PNG_CONST PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600262#endif
263#if defined(PNG_READ_pHYs_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500264 PNG_CONST PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600265#endif
266#if defined(PNG_READ_sBIT_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500267 PNG_CONST PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600268#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600269#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500270 PNG_CONST PNG_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600271#endif
272#if defined(PNG_READ_sPLT_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500273 PNG_CONST PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600274#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600275#if defined(PNG_READ_sRGB_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500276 PNG_CONST PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600277#endif
278#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500279 PNG_CONST PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600280#endif
281#if defined(PNG_READ_tIME_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500282 PNG_CONST PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600283#endif
284#if defined(PNG_READ_tRNS_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500285 PNG_CONST PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600286#endif
287#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500288 PNG_CONST PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600289#endif
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600290#endif /* PNG_USE_LOCAL_ARRAYS */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500291 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500292 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500293
294 /* This should be a binary subdivision search or a hash for
295 * matching the chunk name rather than a linear search.
296 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500297 if (!png_memcmp(chunk_name, png_IDAT, 4))
298 if (png_ptr->mode & PNG_AFTER_IDAT)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500299 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
300
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500301 if (!png_memcmp(chunk_name, png_IHDR, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600302 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500303 else if (!png_memcmp(chunk_name, png_IEND, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600304 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600305#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500306 else if (png_handle_as_unknown(png_ptr, chunk_name))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600307 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500308 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600309 png_ptr->mode |= PNG_HAVE_IDAT;
310 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500311 if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600312 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500313 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600314 {
315 if (!(png_ptr->mode & PNG_HAVE_IHDR))
316 png_error(png_ptr, "Missing IHDR before IDAT");
317 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
318 !(png_ptr->mode & PNG_HAVE_PLTE))
319 png_error(png_ptr, "Missing PLTE before IDAT");
320 break;
321 }
322 }
323#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500324 else if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600325 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500326 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500327 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500328 if (!(png_ptr->mode & PNG_HAVE_IHDR))
329 png_error(png_ptr, "Missing IHDR before IDAT");
330 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
331 !(png_ptr->mode & PNG_HAVE_PLTE))
332 png_error(png_ptr, "Missing PLTE before IDAT");
333
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500334 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500335 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500336 break;
337 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500338#if defined(PNG_READ_bKGD_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500339 else if (!png_memcmp(chunk_name, png_bKGD, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500340 png_handle_bKGD(png_ptr, info_ptr, length);
341#endif
342#if defined(PNG_READ_cHRM_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500343 else if (!png_memcmp(chunk_name, png_cHRM, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344 png_handle_cHRM(png_ptr, info_ptr, length);
345#endif
346#if defined(PNG_READ_gAMA_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500347 else if (!png_memcmp(chunk_name, png_gAMA, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500348 png_handle_gAMA(png_ptr, info_ptr, length);
349#endif
350#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500351 else if (!png_memcmp(chunk_name, png_hIST, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500352 png_handle_hIST(png_ptr, info_ptr, length);
353#endif
354#if defined(PNG_READ_oFFs_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500355 else if (!png_memcmp(chunk_name, png_oFFs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500356 png_handle_oFFs(png_ptr, info_ptr, length);
357#endif
358#if defined(PNG_READ_pCAL_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500359 else if (!png_memcmp(chunk_name, png_pCAL, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500360 png_handle_pCAL(png_ptr, info_ptr, length);
361#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600362#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500363 else if (!png_memcmp(chunk_name, png_sCAL, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600364 png_handle_sCAL(png_ptr, info_ptr, length);
365#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500366#if defined(PNG_READ_pHYs_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500367 else if (!png_memcmp(chunk_name, png_pHYs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500368 png_handle_pHYs(png_ptr, info_ptr, length);
369#endif
370#if defined(PNG_READ_sBIT_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500371 else if (!png_memcmp(chunk_name, png_sBIT, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500372 png_handle_sBIT(png_ptr, info_ptr, length);
373#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600374#if defined(PNG_READ_sRGB_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500375 else if (!png_memcmp(chunk_name, png_sRGB, 4))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600376 png_handle_sRGB(png_ptr, info_ptr, length);
377#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600378#if defined(PNG_READ_iCCP_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500379 else if (!png_memcmp(chunk_name, png_iCCP, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600380 png_handle_iCCP(png_ptr, info_ptr, length);
381#endif
382#if defined(PNG_READ_sPLT_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500383 else if (!png_memcmp(chunk_name, png_sPLT, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600384 png_handle_sPLT(png_ptr, info_ptr, length);
385#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500386#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500387 else if (!png_memcmp(chunk_name, png_tEXt, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500388 png_handle_tEXt(png_ptr, info_ptr, length);
389#endif
390#if defined(PNG_READ_tIME_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500391 else if (!png_memcmp(chunk_name, png_tIME, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500392 png_handle_tIME(png_ptr, info_ptr, length);
393#endif
394#if defined(PNG_READ_tRNS_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500395 else if (!png_memcmp(chunk_name, png_tRNS, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500396 png_handle_tRNS(png_ptr, info_ptr, length);
397#endif
398#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500399 else if (!png_memcmp(chunk_name, png_zTXt, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500400 png_handle_zTXt(png_ptr, info_ptr, length);
401#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600402#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500403 else if (!png_memcmp(chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600404 png_handle_iTXt(png_ptr, info_ptr, length);
405#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500406 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600407 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500408 }
409}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500410#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500411
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500412/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500413void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600414png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500415{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500416 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500417
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500418 if (png_ptr == NULL)
419 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500420 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500421 png_read_start_row(png_ptr);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600422 else
423 png_warning(png_ptr,
424 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600425 png_read_transform_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500426}
427
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500428#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600429/* Initialize palette, background, etc, after transformations
430 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500431 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600432 * If the user doesn't call this, we will do it ourselves.
433 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500434void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600435png_start_read_image(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500436{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500437 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500438
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500439 if (png_ptr == NULL)
440 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500441 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500442 png_read_start_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500443}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500444#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500445
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500446#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500447void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600448png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500449{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600450#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500451 PNG_CONST PNG_IDAT;
452 PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
453 0xff};
454 PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600455#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500456 int ret;
Glenn Randers-Pehrsonc81bb8a2009-08-15 22:02:26 -0500457
458 png_debug2(1, "in png_read_row (row %lu, pass %d)",
459
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500460 if (png_ptr == NULL)
461 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500462
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500463 (unsigned long) png_ptr->row_number, png_ptr->pass);
Guy Schalnate5a37791996-06-05 15:50:50 -0500464 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500465 png_read_start_row(png_ptr);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500466 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
467 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500468 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500469#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
470 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500471 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500472#endif
473#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
474 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500475 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500476#endif
477#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
478 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500479 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500480#endif
481#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
482 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500483 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500484#endif
485#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
486 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500487 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500488#endif
489#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
490 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500491 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500492#endif
493#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
494 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500495 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500496#endif
497 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500498
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500499#if defined(PNG_READ_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500500 /* If interlaced and we do not need a new row, combine row and return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500501 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
502 {
503 switch (png_ptr->pass)
504 {
505 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600506 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500507 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500508 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500509 png_combine_row(png_ptr, dsp_row,
510 png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600511 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500512 return;
513 }
514 break;
515 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600516 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500517 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500518 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500519 png_combine_row(png_ptr, dsp_row,
520 png_pass_dsp_mask[png_ptr->pass]);
521 png_read_finish_row(png_ptr);
522 return;
523 }
524 break;
525 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600526 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500527 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500528 if (dsp_row != NULL && (png_ptr->row_number & 4))
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600529 png_combine_row(png_ptr, dsp_row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500530 png_pass_dsp_mask[png_ptr->pass]);
531 png_read_finish_row(png_ptr);
532 return;
533 }
534 break;
535 case 3:
536 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
537 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500538 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500539 png_combine_row(png_ptr, dsp_row,
540 png_pass_dsp_mask[png_ptr->pass]);
541 png_read_finish_row(png_ptr);
542 return;
543 }
544 break;
545 case 4:
546 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600547 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500548 if (dsp_row != NULL && (png_ptr->row_number & 2))
Guy Schalnat0d580581995-07-20 02:43:20 -0500549 png_combine_row(png_ptr, dsp_row,
550 png_pass_dsp_mask[png_ptr->pass]);
551 png_read_finish_row(png_ptr);
552 return;
553 }
554 break;
555 case 5:
556 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
557 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500558 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500559 png_combine_row(png_ptr, dsp_row,
560 png_pass_dsp_mask[png_ptr->pass]);
561 png_read_finish_row(png_ptr);
562 return;
563 }
564 break;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600565 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500566 if (!(png_ptr->row_number & 1))
567 {
568 png_read_finish_row(png_ptr);
569 return;
570 }
571 break;
572 }
573 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500574#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500575
Guy Schalnate5a37791996-06-05 15:50:50 -0500576 if (!(png_ptr->mode & PNG_HAVE_IDAT))
577 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500578
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600579 png_ptr->zstream.next_out = png_ptr->row_buf;
580 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500581 do
582 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600583 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500584 {
585 while (!png_ptr->idat_size)
586 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600587 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500588
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500589 png_ptr->idat_size = png_read_chunk_header(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600590 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
591 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500592 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600593 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
594 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500595 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600596 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500597 png_crc_read(png_ptr, png_ptr->zbuf,
598 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600599 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500600 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600601 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500602 if (ret == Z_STREAM_END)
603 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600604 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500605 png_ptr->idat_size)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500606 png_benign_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600607 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500608 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600609 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500610 }
611 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600612 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Guy Schalnate5a37791996-06-05 15:50:50 -0500613 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600614
615 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500616
Guy Schalnat0d580581995-07-20 02:43:20 -0500617 png_ptr->row_info.color_type = png_ptr->color_type;
618 png_ptr->row_info.width = png_ptr->iwidth;
619 png_ptr->row_info.channels = png_ptr->channels;
620 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
621 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500622 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
623 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500624
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500625 if (png_ptr->row_buf[0])
Guy Schalnate5a37791996-06-05 15:50:50 -0500626 png_read_filter_row(png_ptr, &(png_ptr->row_info),
627 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
628 (int)(png_ptr->row_buf[0]));
Guy Schalnat0d580581995-07-20 02:43:20 -0500629
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500630 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600631
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600632#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500633 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600634 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
635 {
636 /* Intrapixel differencing */
637 png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
638 }
639#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500640
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500641
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -0600642 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
Guy Schalnat0d580581995-07-20 02:43:20 -0500643 png_do_read_transformations(png_ptr);
644
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500645#if defined(PNG_READ_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500646 /* Blow up interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500647 if (png_ptr->interlaced &&
648 (png_ptr->transformations & PNG_INTERLACE))
649 {
650 if (png_ptr->pass < 6)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500651 /* Old interface (pre-1.0.9):
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500652 * png_do_read_interlace(&(png_ptr->row_info),
653 * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
654 */
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600655 png_do_read_interlace(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500656
Andreas Dilger47a0c421997-05-16 02:46:07 -0500657 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500658 png_combine_row(png_ptr, dsp_row,
659 png_pass_dsp_mask[png_ptr->pass]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500660 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500661 png_combine_row(png_ptr, row,
662 png_pass_mask[png_ptr->pass]);
663 }
664 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500665#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500666 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500667 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500668 png_combine_row(png_ptr, row, 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500669 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500670 png_combine_row(png_ptr, dsp_row, 0xff);
671 }
672 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600673
674 if (png_ptr->read_row_fn != NULL)
675 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500676}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500677#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500678
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500679#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500680/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600681 * and png_set_interlace_handling() has been called, the rows need to
682 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500683 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600684 * called, the rows contents must be initialized to the contents of the
685 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600686 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600687 * "row" holds the actual image, and pixels are placed in it
688 * as they arrive. If the image is displayed after each pass, it will
689 * appear to "sparkle" in. "display_row" can be used to display a
690 * "chunky" progressive image, with finer detail added as it becomes
691 * available. If you do not want this "chunky" display, you may pass
692 * NULL for display_row. If you do not want the sparkle display, and
693 * you have not called png_handle_alpha(), you may pass NULL for rows.
694 * If you have called png_handle_alpha(), and the image has either an
695 * alpha channel or a transparency chunk, you must provide a buffer for
696 * rows. In this case, you do not have to provide a display_row buffer
697 * also, but you may. If the image is not interlaced, or if you have
698 * not called png_set_interlace_handling(), the display_row buffer will
699 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500700 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600701 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600702 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600703
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500704void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600705png_read_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600706 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500707{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600708 png_uint_32 i;
709 png_bytepp rp;
710 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500711
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500712 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500713
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500714 if (png_ptr == NULL)
715 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600716 rp = row;
717 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500718 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500719 for (i = 0; i < num_rows; i++)
720 {
721 png_bytep rptr = *rp++;
722 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600723
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500724 png_read_row(png_ptr, rptr, dptr);
725 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500726 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500727 for (i = 0; i < num_rows; i++)
728 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500729 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500730 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500731 rp++;
732 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500733 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500734 for (i = 0; i < num_rows; i++)
735 {
736 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500737 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500738 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500739 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500740}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500741#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500742
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500743#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500744/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500745 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600746 * initialize the image to the current image that PNG will be overlaying.
747 * We set the num_rows again here, in case it was incorrectly set in
748 * png_read_start_row() by a call to png_read_update_info() or
749 * png_start_read_image() if png_set_interlace_handling() wasn't called
750 * prior to either of these functions like it should have been. You can
751 * only call this function once. If you desire to have an image for
752 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500753 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600754 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600755 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500756void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600757png_read_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500758{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500759 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500760 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600761 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500762
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500763 png_debug(1, "in png_read_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500764
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500765 if (png_ptr == NULL)
766 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500767
768#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -0500769 pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500770#else
771 if (png_ptr->interlaced)
772 png_error(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500773 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500774 pass = 1;
775#endif
776
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500777
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500778 image_height=png_ptr->height;
779 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
Guy Schalnate5a37791996-06-05 15:50:50 -0500780
Guy Schalnat0d580581995-07-20 02:43:20 -0500781 for (j = 0; j < pass; j++)
782 {
783 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500784 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500785 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500786 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500787 rp++;
788 }
789 }
790}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500791#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500792
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500793#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500794/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600795 * file, will verify the end is accurate, and will read any comments
796 * or time information at the end of the file, if info is not NULL.
797 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500798void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600799png_read_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500800{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500801 png_debug(1, "in png_read_end");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500802
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500803 if (png_ptr == NULL)
804 return;
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-Pehrson074af5e1999-11-28 23:32:18 -0600809#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500810 PNG_CONST PNG_IHDR;
811 PNG_CONST PNG_IDAT;
812 PNG_CONST PNG_IEND;
813 PNG_CONST PNG_PLTE;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600814#if defined(PNG_READ_bKGD_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500815 PNG_CONST PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600816#endif
817#if defined(PNG_READ_cHRM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500818 PNG_CONST PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600819#endif
820#if defined(PNG_READ_gAMA_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500821 PNG_CONST PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600822#endif
823#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500824 PNG_CONST PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600825#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600826#if defined(PNG_READ_iCCP_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500827 PNG_CONST PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600828#endif
829#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500830 PNG_CONST PNG_iTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600831#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600832#if defined(PNG_READ_oFFs_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500833 PNG_CONST PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600834#endif
835#if defined(PNG_READ_pCAL_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500836 PNG_CONST PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600837#endif
838#if defined(PNG_READ_pHYs_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500839 PNG_CONST PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600840#endif
841#if defined(PNG_READ_sBIT_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500842 PNG_CONST PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600843#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600844#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500845 PNG_CONST PNG_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600846#endif
847#if defined(PNG_READ_sPLT_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500848 PNG_CONST PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600849#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600850#if defined(PNG_READ_sRGB_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500851 PNG_CONST PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600852#endif
853#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500854 PNG_CONST PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600855#endif
856#if defined(PNG_READ_tIME_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500857 PNG_CONST PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600858#endif
859#if defined(PNG_READ_tRNS_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500860 PNG_CONST PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600861#endif
862#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500863 PNG_CONST PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600864#endif
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600865#endif /* PNG_USE_LOCAL_ARRAYS */
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500866 png_uint_32 length = png_read_chunk_header(png_ptr);
867 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500868
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500869 if (!png_memcmp(chunk_name, png_IHDR, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600870 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500871 else if (!png_memcmp(chunk_name, png_IEND, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600872 png_handle_IEND(png_ptr, info_ptr, length);
873#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500874 else if (png_handle_as_unknown(png_ptr, chunk_name))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600875 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500876 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600877 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500878 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500879 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600880 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600881 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500882 if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600883 png_ptr->mode |= PNG_HAVE_PLTE;
884 }
885#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500886 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Guy Schalnat0d580581995-07-20 02:43:20 -0500887 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888 /* Zero length IDATs are legal after the last IDAT has been
889 * read, but not after other chunks have been read.
890 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500891 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500892 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500893 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500894 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500895 else if (!png_memcmp(chunk_name, png_PLTE, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896 png_handle_PLTE(png_ptr, info_ptr, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500897#if defined(PNG_READ_bKGD_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500898 else if (!png_memcmp(chunk_name, png_bKGD, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500899 png_handle_bKGD(png_ptr, info_ptr, length);
900#endif
901#if defined(PNG_READ_cHRM_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500902 else if (!png_memcmp(chunk_name, png_cHRM, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500903 png_handle_cHRM(png_ptr, info_ptr, length);
904#endif
905#if defined(PNG_READ_gAMA_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500906 else if (!png_memcmp(chunk_name, png_gAMA, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500907 png_handle_gAMA(png_ptr, info_ptr, length);
908#endif
909#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500910 else if (!png_memcmp(chunk_name, png_hIST, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500911 png_handle_hIST(png_ptr, info_ptr, length);
912#endif
913#if defined(PNG_READ_oFFs_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500914 else if (!png_memcmp(chunk_name, png_oFFs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500915 png_handle_oFFs(png_ptr, info_ptr, length);
916#endif
917#if defined(PNG_READ_pCAL_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500918 else if (!png_memcmp(chunk_name, png_pCAL, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500919 png_handle_pCAL(png_ptr, info_ptr, length);
920#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600921#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500922 else if (!png_memcmp(chunk_name, png_sCAL, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600923 png_handle_sCAL(png_ptr, info_ptr, length);
924#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500925#if defined(PNG_READ_pHYs_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500926 else if (!png_memcmp(chunk_name, png_pHYs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500927 png_handle_pHYs(png_ptr, info_ptr, length);
928#endif
929#if defined(PNG_READ_sBIT_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500930 else if (!png_memcmp(chunk_name, png_sBIT, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500931 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500932#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600933#if defined(PNG_READ_sRGB_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500934 else if (!png_memcmp(chunk_name, png_sRGB, 4))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600935 png_handle_sRGB(png_ptr, info_ptr, length);
936#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600937#if defined(PNG_READ_iCCP_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500938 else if (!png_memcmp(chunk_name, png_iCCP, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600939 png_handle_iCCP(png_ptr, info_ptr, length);
940#endif
941#if defined(PNG_READ_sPLT_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500942 else if (!png_memcmp(chunk_name, png_sPLT, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600943 png_handle_sPLT(png_ptr, info_ptr, length);
944#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500945#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500946 else if (!png_memcmp(chunk_name, png_tEXt, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600947 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500948#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500949#if defined(PNG_READ_tIME_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500950 else if (!png_memcmp(chunk_name, png_tIME, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951 png_handle_tIME(png_ptr, info_ptr, length);
952#endif
953#if defined(PNG_READ_tRNS_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500954 else if (!png_memcmp(chunk_name, png_tRNS, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500955 png_handle_tRNS(png_ptr, info_ptr, length);
956#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500957#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500958 else if (!png_memcmp(chunk_name, png_zTXt, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600959 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500960#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600961#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500962 else if (!png_memcmp(chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600963 png_handle_iTXt(png_ptr, info_ptr, length);
964#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500965 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600966 png_handle_unknown(png_ptr, info_ptr, length);
967 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500968}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500969#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500970
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500971/* Free all memory used by the read */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500972void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500973png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600974 png_infopp end_info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500975{
976 png_structp png_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600977 png_infop info_ptr = NULL, end_info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500978#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500979 png_free_ptr free_fn = NULL;
980 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600981#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500982
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500983 png_debug(1, "in png_destroy_read_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500984
Andreas Dilger47a0c421997-05-16 02:46:07 -0500985 if (png_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500986 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500987 if (png_ptr == NULL)
988 return;
989
990#ifdef PNG_USER_MEM_SUPPORTED
991 free_fn = png_ptr->free_fn;
992 mem_ptr = png_ptr->mem_ptr;
993#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500994
Andreas Dilger47a0c421997-05-16 02:46:07 -0500995 if (info_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500996 info_ptr = *info_ptr_ptr;
997
Andreas Dilger47a0c421997-05-16 02:46:07 -0500998 if (end_info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600999 end_info_ptr = *end_info_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -05001000
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001001 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -05001002
Andreas Dilger47a0c421997-05-16 02:46:07 -05001003 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001004 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001005#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001006 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001007#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001008
1009#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001010 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1011 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001012#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001013 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001014#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001015 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001016 }
1017
Andreas Dilger47a0c421997-05-16 02:46:07 -05001018 if (end_info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001019 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001020#if defined(PNG_READ_TEXT_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001021 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001022#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001023#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001024 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
1025 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001026#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001027 png_destroy_struct((png_voidp)end_info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001028#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001029 *end_info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001030 }
1031
Andreas Dilger47a0c421997-05-16 02:46:07 -05001032 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001033 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001034#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001035 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1036 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001037#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001038 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001039#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001040 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001041 }
1042}
1043
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001044/* Free all memory used by the read (old method) */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001045void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001046png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001047{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001048#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001049 jmp_buf tmp_jmp;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001050#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001051 png_error_ptr error_fn;
1052 png_error_ptr warning_fn;
1053 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001054#ifdef PNG_USER_MEM_SUPPORTED
1055 png_free_ptr free_fn;
1056#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001057
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001058 png_debug(1, "in png_read_destroy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001059
Andreas Dilger47a0c421997-05-16 02:46:07 -05001060 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001061 png_info_destroy(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001062
Andreas Dilger47a0c421997-05-16 02:46:07 -05001063 if (end_info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001064 png_info_destroy(png_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001065
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001066 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -05001067 png_free(png_ptr, png_ptr->big_row_buf);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001068 png_free(png_ptr, png_ptr->prev_row);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001069 png_free(png_ptr, png_ptr->chunkdata);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001070#if defined(PNG_READ_DITHER_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001071 png_free(png_ptr, png_ptr->palette_lookup);
1072 png_free(png_ptr, png_ptr->dither_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001073#endif
1074#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001075 png_free(png_ptr, png_ptr->gamma_table);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001076#endif
1077#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001078 png_free(png_ptr, png_ptr->gamma_from_1);
1079 png_free(png_ptr, png_ptr->gamma_to_1);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001080#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001081#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001082 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001083 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001084 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001085#else
1086 if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
1087 png_zfree(png_ptr, png_ptr->palette);
1088 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
1089#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001090#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001091 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001092#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001093 if (png_ptr->free_me & PNG_FREE_TRNS)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001094 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001095 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001096#else
1097 if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001098 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001099 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
1100#endif
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001101#endif
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001102#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001103#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001104 if (png_ptr->free_me & PNG_FREE_HIST)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001105 png_free(png_ptr, png_ptr->hist);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001106 png_ptr->free_me &= ~PNG_FREE_HIST;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001107#else
1108 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
1109 png_free(png_ptr, png_ptr->hist);
1110 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
1111#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001112#endif
1113#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001114 if (png_ptr->gamma_16_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001115 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001116 int i;
1117 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001118 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001119 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001120 png_free(png_ptr, png_ptr->gamma_16_table[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001121 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001122 png_free(png_ptr, png_ptr->gamma_16_table);
Guy Schalnat0d580581995-07-20 02:43:20 -05001123 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001124#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001125 if (png_ptr->gamma_16_from_1 != NULL)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001126 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001127 int i;
1128 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001129 for (i = 0; i < istop; i++)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001130 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001131 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001132 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001133 png_free(png_ptr, png_ptr->gamma_16_from_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001134 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001135 if (png_ptr->gamma_16_to_1 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001136 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001137 int i;
1138 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001139 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001140 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001141 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001142 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001143 png_free(png_ptr, png_ptr->gamma_16_to_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001144 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001145#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001146#endif
1147#if defined(PNG_TIME_RFC1123_SUPPORTED)
1148 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001149#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001150
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001151 inflateEnd(&png_ptr->zstream);
Guy Schalnat6d764711995-12-19 03:22:19 -06001152#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001153 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001154#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001155
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001156#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1157#ifdef PNG_TEXT_SUPPORTED
1158 png_free(png_ptr, png_ptr->current_text);
1159#endif /* PNG_TEXT_SUPPORTED */
1160#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1161
Guy Schalnate5a37791996-06-05 15:50:50 -05001162 /* Save the important info out of the png_struct, in case it is
1163 * being used again.
1164 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001165#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001166 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001167#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001168
1169 error_fn = png_ptr->error_fn;
1170 warning_fn = png_ptr->warning_fn;
1171 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001172#ifdef PNG_USER_MEM_SUPPORTED
1173 free_fn = png_ptr->free_fn;
1174#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001175
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001176 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001177
1178 png_ptr->error_fn = error_fn;
1179 png_ptr->warning_fn = warning_fn;
1180 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001181#ifdef PNG_USER_MEM_SUPPORTED
1182 png_ptr->free_fn = free_fn;
1183#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001184
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001185#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001186 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001187#endif
1188
Guy Schalnat0d580581995-07-20 02:43:20 -05001189}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001190
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001191void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001192png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1193{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001194 if (png_ptr == NULL)
1195 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001196 png_ptr->read_row_fn = read_row_fn;
1197}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001198
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001199
1200#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001201#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001202void PNGAPI
1203png_read_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001204 int transforms,
1205 voidp params)
1206{
1207 int row;
1208
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001209 if (png_ptr == NULL)
1210 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001211
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001212 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001213 * PNG file before the first IDAT (image data chunk).
1214 */
1215 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001216 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
1217 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001218
1219 /* -------------- image transformations start here ------------------- */
1220
1221#if defined(PNG_READ_16_TO_8_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001222 /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001223 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001224 if (transforms & PNG_TRANSFORM_STRIP_16)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001225 png_set_strip_16(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001226#endif
1227
1228#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001229 /* Strip alpha bytes from the input data without combining with
1230 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001231 */
1232 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001233 png_set_strip_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001234#endif
1235
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001236#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001237 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001238 * byte into separate bytes (useful for paletted and grayscale images).
1239 */
1240 if (transforms & PNG_TRANSFORM_PACKING)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001241 png_set_packing(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001242#endif
1243
1244#if defined(PNG_READ_PACKSWAP_SUPPORTED)
1245 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001246 * (not useful if you are using png_set_packing).
1247 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001248 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001249 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001250#endif
1251
1252#if defined(PNG_READ_EXPAND_SUPPORTED)
1253 /* Expand paletted colors into true RGB triplets
1254 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1255 * Expand paletted or RGB images with transparency to full alpha
1256 * channels so the data will be available as RGBA quartets.
1257 */
1258 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001259 if ((png_ptr->bit_depth < 8) ||
1260 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1261 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001262 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001263#endif
1264
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001265 /* We don't handle background color or gamma transformation or dithering.
1266 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001267
1268#if defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001269 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001270 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001271 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001272 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001273#endif
1274
1275#if defined(PNG_READ_SHIFT_SUPPORTED)
1276 /* If you want to shift the pixel values from the range [0,255] or
1277 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1278 * colors were originally in:
1279 */
1280 if ((transforms & PNG_TRANSFORM_SHIFT)
1281 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1282 {
1283 png_color_8p sig_bit;
1284
1285 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1286 png_set_shift(png_ptr, sig_bit);
1287 }
1288#endif
1289
1290#if defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001291 /* Flip the RGB pixels to BGR (or RGBA to BGRA)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001292 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001293 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001294 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001295#endif
1296
1297#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001298 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001299 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001300 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1301 png_set_swap_alpha(png_ptr);
1302#endif
1303
1304#if defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001305 /* Swap bytes of 16 bit files to least significant byte first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001306 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001307 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001308 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001309#endif
1310
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001311#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1312 /* Invert the alpha channel from opacity to transparency
1313 */
1314 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1315 png_set_invert_alpha(png_ptr);
1316#endif
1317
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001318#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
1319 /* Expand grayscale image to RGB
1320 */
1321 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
Glenn Randers-Pehrson8cb69f02009-07-19 19:24:58 -05001322 png_set_gray_to_rgb(png_ptr);
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001323#endif
1324
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001325 /* We don't handle adding filler bytes */
1326
1327 /* Optional call to gamma correct and add the background to the palette
1328 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001329 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001330 */
1331 png_read_update_info(png_ptr, info_ptr);
1332
1333 /* -------------- image transformations end here ------------------- */
1334
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001335#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001336 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001337#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001338 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001339 {
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001340#ifdef PNG_CALLOC_SUPPORTED
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001341 info_ptr->row_pointers = (png_bytepp)png_calloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001342 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001343#else
1344 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
1345 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -05001346 png_memset(info_ptr->row_pointers, 0, info_ptr->height
1347 * png_sizeof(png_bytep));
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001348#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001349#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001350 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001351#endif
1352 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001353 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001354 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001355 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001356
1357 png_read_image(png_ptr, info_ptr->row_pointers);
1358 info_ptr->valid |= PNG_INFO_IDAT;
1359
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001360 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001361 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001362
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001363 transforms = transforms; /* Quiet compiler warnings */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001364 params = params;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001365
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001366}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001367#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001368#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001369#endif /* PNG_READ_SUPPORTED */