blob: f225039e6c58efd78880d3af6229d3a3ff7aa64f [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.
10 * For conditions of distribution and use, see copyright notice in png.h
11 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 * This file contains routines that an application calls directly to
13 * read a PNG file or stream.
14 */
Guy Schalnat0d580581995-07-20 02:43:20 -050015
Guy Schalnat0d580581995-07-20 02:43:20 -050016#include "png.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060017#if defined(PNG_READ_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050018#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060019
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050020
Andreas Dilger47a0c421997-05-16 02:46:07 -050021/* Create a PNG structure for reading, and allocate any memory needed. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050022png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -050023png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060024 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -050025{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050026
27#ifdef PNG_USER_MEM_SUPPORTED
28 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050029 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050030}
31
32/* Alternate create PNG structure for reading, and allocate any memory needed. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050033png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050034png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
35 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
36 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
37{
38#endif /* PNG_USER_MEM_SUPPORTED */
39
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -060040#ifdef PNG_SETJMP_SUPPORTED
41 volatile
42#endif
43 png_structp png_ptr;
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050044 int png_cleanup_needed = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060045
46#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060047#ifdef USE_FAR_KEYWORD
48 jmp_buf jmpbuf;
49#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060050#endif
51
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050052 int i;
53
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050054 png_debug(1, "in png_create_read_struct");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050055#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060056 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050057 malloc_fn, mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050058#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060059 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050060#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060061 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050062 return (NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050063
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -050064 /* Added at libpng-1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -050065#ifdef PNG_SET_USER_LIMITS_SUPPORTED
66 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
67 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
68#endif
69
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050070#ifdef PNG_SETJMP_SUPPORTED
71/* Applications that neglect to set up their own setjmp() and then
72 encounter a png_error() will longjmp here. Since the jmpbuf is
73 then meaningless we abort instead of returning. */
74#ifdef USE_FAR_KEYWORD
75 if (setjmp(jmpbuf))
76#else
77 if (setjmp(png_ptr->jmpbuf))
78#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050079 PNG_ABORT();
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050080#endif
81
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050082#ifdef PNG_USER_MEM_SUPPORTED
83 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060084#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050085
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060086 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnat6d764711995-12-19 03:22:19 -060087
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050088 if (user_png_ver)
Guy Schalnate5a37791996-06-05 15:50:50 -050089 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050090 i = 0;
91 do
92 {
93 if (user_png_ver[i] != png_libpng_ver[i])
94 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
95 } while (png_libpng_ver[i++]);
96 }
97 else
98 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
Glenn Randers-Pehrsond60c8862009-06-15 21:56:14 -050099
Guy Schalnate5a37791996-06-05 15:50:50 -0500100
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500101 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
102 {
103 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
104 * we must recompile any applications that use any older library version.
105 * For versions after libpng 1.0, we will be compatible, so we need
106 * only check the first digit.
107 */
108 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
109 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
110 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
111 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500112#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500113 char msg[80];
114 if (user_png_ver)
115 {
116 png_snprintf(msg, 80,
117 "Application was compiled with png.h from libpng-%.20s",
118 user_png_ver);
119 png_warning(png_ptr, msg);
120 }
121 png_snprintf(msg, 80,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500122 "Application is running with png.c from libpng-%.20s",
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500123 png_libpng_ver);
124 png_warning(png_ptr, msg);
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500125#endif
126#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500127 png_ptr->flags = 0;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500128#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500129 png_warning(png_ptr,
130 "Incompatible libpng version in application and library");
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500131
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500132 png_cleanup_needed = 1;
133 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500134 }
135
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500136 if (!png_cleanup_needed)
137 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500138 /* Initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500139 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500140 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500141 png_ptr->zbuf_size);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500142 if (png_ptr->zbuf == NULL)
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500143 png_cleanup_needed = 1;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500144 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600145 png_ptr->zstream.zalloc = png_zalloc;
146 png_ptr->zstream.zfree = png_zfree;
147 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500148
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500149 if (!png_cleanup_needed)
150 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500151 switch (inflateInit(&png_ptr->zstream))
152 {
153 case Z_OK: /* Do nothing */ break;
154 case Z_MEM_ERROR:
155 case Z_STREAM_ERROR: png_warning(png_ptr, "zlib memory error");
156 png_cleanup_needed = 1; break;
157 case Z_VERSION_ERROR: png_warning(png_ptr, "zlib version error");
158 png_cleanup_needed = 1; break;
159 default: png_warning(png_ptr, "Unknown zlib error");
160 png_cleanup_needed = 1;
161 }
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500162 }
163
164 if (png_cleanup_needed)
165 {
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500166 /* Clean up PNG structure and deallocate any memory. */
167 png_free(png_ptr, png_ptr->zbuf);
168 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500169#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500170 png_destroy_struct_2((png_voidp)png_ptr,
171 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500172#else
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500173 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500174#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500175 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500176 }
177
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600178 png_ptr->zstream.next_out = png_ptr->zbuf;
179 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500180
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500181 png_set_read_fn(png_ptr, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500182
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500183
Guy Schalnate5a37791996-06-05 15:50:50 -0500184 return (png_ptr);
185}
186
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500187
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500188#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600189/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -0600190 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600191 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500192 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600193 * via png_set_sig_bytes(), and we will only check the remaining bytes
194 * here. The application can then have access to the signature bytes we
195 * read if it is determined that this isn't a valid PNG file.
196 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500197void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600198png_read_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500199{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500200 if (png_ptr == NULL || info_ptr == NULL)
201 return;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500202 png_debug(1, "in png_read_info");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600203 /* If we haven't checked all of the PNG signature bytes, do so now. */
204 if (png_ptr->sig_bytes < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500205 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500206 png_size_t num_checked = png_ptr->sig_bytes,
207 num_to_check = 8 - num_checked;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600208
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500209#ifdef PNG_IO_STATE_SUPPORTED
210 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
211#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500212
Andreas Dilger47a0c421997-05-16 02:46:07 -0500213 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600214 png_ptr->sig_bytes = 8;
215
216 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
217 {
218 if (num_checked < 4 &&
219 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
220 png_error(png_ptr, "Not a PNG file");
221 else
222 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
223 }
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600224 if (num_checked < 3)
225 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500226 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500227
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500228 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500229 {
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600230#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500231 PNG_CONST PNG_IHDR;
232 PNG_CONST PNG_IDAT;
233 PNG_CONST PNG_IEND;
234 PNG_CONST PNG_PLTE;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600235#if defined(PNG_READ_bKGD_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500236 PNG_CONST PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600237#endif
238#if defined(PNG_READ_cHRM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500239 PNG_CONST PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600240#endif
241#if defined(PNG_READ_gAMA_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500242 PNG_CONST PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600243#endif
244#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500245 PNG_CONST PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600246#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600247#if defined(PNG_READ_iCCP_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500248 PNG_CONST PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600249#endif
250#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500251 PNG_CONST PNG_iTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600252#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600253#if defined(PNG_READ_oFFs_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500254 PNG_CONST PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600255#endif
256#if defined(PNG_READ_pCAL_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500257 PNG_CONST PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600258#endif
259#if defined(PNG_READ_pHYs_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500260 PNG_CONST PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600261#endif
262#if defined(PNG_READ_sBIT_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500263 PNG_CONST PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600264#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600265#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500266 PNG_CONST PNG_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600267#endif
268#if defined(PNG_READ_sPLT_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500269 PNG_CONST PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600270#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600271#if defined(PNG_READ_sRGB_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500272 PNG_CONST PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600273#endif
274#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500275 PNG_CONST PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600276#endif
277#if defined(PNG_READ_tIME_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500278 PNG_CONST PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600279#endif
280#if defined(PNG_READ_tRNS_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500281 PNG_CONST PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600282#endif
283#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500284 PNG_CONST PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600285#endif
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600286#endif /* PNG_USE_LOCAL_ARRAYS */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500287 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500288 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500289
290 /* This should be a binary subdivision search or a hash for
291 * matching the chunk name rather than a linear search.
292 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500293 if (!png_memcmp(chunk_name, png_IDAT, 4))
294 if (png_ptr->mode & PNG_AFTER_IDAT)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500295 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
296
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500297 if (!png_memcmp(chunk_name, png_IHDR, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600298 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500299 else if (!png_memcmp(chunk_name, png_IEND, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600300 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600301#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500302 else if (png_handle_as_unknown(png_ptr, chunk_name))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600303 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500304 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600305 png_ptr->mode |= PNG_HAVE_IDAT;
306 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500307 if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600308 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500309 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600310 {
311 if (!(png_ptr->mode & PNG_HAVE_IHDR))
312 png_error(png_ptr, "Missing IHDR before IDAT");
313 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
314 !(png_ptr->mode & PNG_HAVE_PLTE))
315 png_error(png_ptr, "Missing PLTE before IDAT");
316 break;
317 }
318 }
319#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500320 else if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600321 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500322 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500323 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500324 if (!(png_ptr->mode & PNG_HAVE_IHDR))
325 png_error(png_ptr, "Missing IHDR before IDAT");
326 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
327 !(png_ptr->mode & PNG_HAVE_PLTE))
328 png_error(png_ptr, "Missing PLTE before IDAT");
329
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500330 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500331 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500332 break;
333 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500334#if defined(PNG_READ_bKGD_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500335 else if (!png_memcmp(chunk_name, png_bKGD, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500336 png_handle_bKGD(png_ptr, info_ptr, length);
337#endif
338#if defined(PNG_READ_cHRM_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500339 else if (!png_memcmp(chunk_name, png_cHRM, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500340 png_handle_cHRM(png_ptr, info_ptr, length);
341#endif
342#if defined(PNG_READ_gAMA_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500343 else if (!png_memcmp(chunk_name, png_gAMA, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344 png_handle_gAMA(png_ptr, info_ptr, length);
345#endif
346#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500347 else if (!png_memcmp(chunk_name, png_hIST, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500348 png_handle_hIST(png_ptr, info_ptr, length);
349#endif
350#if defined(PNG_READ_oFFs_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500351 else if (!png_memcmp(chunk_name, png_oFFs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500352 png_handle_oFFs(png_ptr, info_ptr, length);
353#endif
354#if defined(PNG_READ_pCAL_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500355 else if (!png_memcmp(chunk_name, png_pCAL, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500356 png_handle_pCAL(png_ptr, info_ptr, length);
357#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600358#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500359 else if (!png_memcmp(chunk_name, png_sCAL, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600360 png_handle_sCAL(png_ptr, info_ptr, length);
361#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500362#if defined(PNG_READ_pHYs_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500363 else if (!png_memcmp(chunk_name, png_pHYs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500364 png_handle_pHYs(png_ptr, info_ptr, length);
365#endif
366#if defined(PNG_READ_sBIT_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500367 else if (!png_memcmp(chunk_name, png_sBIT, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500368 png_handle_sBIT(png_ptr, info_ptr, length);
369#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600370#if defined(PNG_READ_sRGB_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500371 else if (!png_memcmp(chunk_name, png_sRGB, 4))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600372 png_handle_sRGB(png_ptr, info_ptr, length);
373#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600374#if defined(PNG_READ_iCCP_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500375 else if (!png_memcmp(chunk_name, png_iCCP, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600376 png_handle_iCCP(png_ptr, info_ptr, length);
377#endif
378#if defined(PNG_READ_sPLT_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500379 else if (!png_memcmp(chunk_name, png_sPLT, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600380 png_handle_sPLT(png_ptr, info_ptr, length);
381#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500383 else if (!png_memcmp(chunk_name, png_tEXt, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500384 png_handle_tEXt(png_ptr, info_ptr, length);
385#endif
386#if defined(PNG_READ_tIME_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500387 else if (!png_memcmp(chunk_name, png_tIME, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500388 png_handle_tIME(png_ptr, info_ptr, length);
389#endif
390#if defined(PNG_READ_tRNS_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500391 else if (!png_memcmp(chunk_name, png_tRNS, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500392 png_handle_tRNS(png_ptr, info_ptr, length);
393#endif
394#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500395 else if (!png_memcmp(chunk_name, png_zTXt, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500396 png_handle_zTXt(png_ptr, info_ptr, length);
397#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600398#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500399 else if (!png_memcmp(chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600400 png_handle_iTXt(png_ptr, info_ptr, length);
401#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500402 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600403 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500404 }
405}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500406#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500407
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500408/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500409void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600410png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500411{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500412 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500413 if (png_ptr == NULL)
414 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500415 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500416 png_read_start_row(png_ptr);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600417 else
418 png_warning(png_ptr,
419 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600420 png_read_transform_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500421}
422
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500423#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600424/* Initialize palette, background, etc, after transformations
425 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500426 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600427 * If the user doesn't call this, we will do it ourselves.
428 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500429void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600430png_start_read_image(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500431{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500432 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500433 if (png_ptr == NULL)
434 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500435 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500436 png_read_start_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500437}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500438#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500439
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500440#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500441void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600442png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500443{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600444#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500445 PNG_CONST PNG_IDAT;
446 PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
447 0xff};
448 PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600449#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500450 int ret;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500451 if (png_ptr == NULL)
452 return;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500453 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500454 (unsigned long) png_ptr->row_number, png_ptr->pass);
Guy Schalnate5a37791996-06-05 15:50:50 -0500455 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500456 png_read_start_row(png_ptr);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500457 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
458 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500459 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500460#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
461 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500462 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500463#endif
464#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
465 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500466 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500467#endif
468#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
469 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500470 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500471#endif
472#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
473 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500474 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500475#endif
476#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
477 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500478 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500479#endif
480#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
481 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500482 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500483#endif
484#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
485 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500486 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500487#endif
488 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500489
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500490#if defined(PNG_READ_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500491 /* If interlaced and we do not need a new row, combine row and return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500492 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
493 {
494 switch (png_ptr->pass)
495 {
496 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600497 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500498 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500499 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500500 png_combine_row(png_ptr, dsp_row,
501 png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600502 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500503 return;
504 }
505 break;
506 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600507 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500508 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500509 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500510 png_combine_row(png_ptr, dsp_row,
511 png_pass_dsp_mask[png_ptr->pass]);
512 png_read_finish_row(png_ptr);
513 return;
514 }
515 break;
516 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600517 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500518 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500519 if (dsp_row != NULL && (png_ptr->row_number & 4))
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600520 png_combine_row(png_ptr, dsp_row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500521 png_pass_dsp_mask[png_ptr->pass]);
522 png_read_finish_row(png_ptr);
523 return;
524 }
525 break;
526 case 3:
527 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
528 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500529 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500530 png_combine_row(png_ptr, dsp_row,
531 png_pass_dsp_mask[png_ptr->pass]);
532 png_read_finish_row(png_ptr);
533 return;
534 }
535 break;
536 case 4:
537 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600538 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500539 if (dsp_row != NULL && (png_ptr->row_number & 2))
Guy Schalnat0d580581995-07-20 02:43:20 -0500540 png_combine_row(png_ptr, dsp_row,
541 png_pass_dsp_mask[png_ptr->pass]);
542 png_read_finish_row(png_ptr);
543 return;
544 }
545 break;
546 case 5:
547 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
548 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500549 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500550 png_combine_row(png_ptr, dsp_row,
551 png_pass_dsp_mask[png_ptr->pass]);
552 png_read_finish_row(png_ptr);
553 return;
554 }
555 break;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600556 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500557 if (!(png_ptr->row_number & 1))
558 {
559 png_read_finish_row(png_ptr);
560 return;
561 }
562 break;
563 }
564 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500565#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500566
Guy Schalnate5a37791996-06-05 15:50:50 -0500567 if (!(png_ptr->mode & PNG_HAVE_IDAT))
568 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500569
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600570 png_ptr->zstream.next_out = png_ptr->row_buf;
571 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500572 do
573 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600574 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500575 {
576 while (!png_ptr->idat_size)
577 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600578 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500579
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500580 png_ptr->idat_size = png_read_chunk_header(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600581 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
582 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500583 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600584 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
585 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500586 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600587 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500588 png_crc_read(png_ptr, png_ptr->zbuf,
589 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600590 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500591 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600592 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500593 if (ret == Z_STREAM_END)
594 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600595 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500596 png_ptr->idat_size)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500597 png_benign_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600598 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500599 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600600 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500601 }
602 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600603 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Guy Schalnate5a37791996-06-05 15:50:50 -0500604 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600605
606 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500607
Guy Schalnat0d580581995-07-20 02:43:20 -0500608 png_ptr->row_info.color_type = png_ptr->color_type;
609 png_ptr->row_info.width = png_ptr->iwidth;
610 png_ptr->row_info.channels = png_ptr->channels;
611 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
612 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500613 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
614 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500615
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500616 if (png_ptr->row_buf[0])
Guy Schalnate5a37791996-06-05 15:50:50 -0500617 png_read_filter_row(png_ptr, &(png_ptr->row_info),
618 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
619 (int)(png_ptr->row_buf[0]));
Guy Schalnat0d580581995-07-20 02:43:20 -0500620
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500621 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600622
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600623#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500624 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600625 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
626 {
627 /* Intrapixel differencing */
628 png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
629 }
630#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500631
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500632
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -0600633 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
Guy Schalnat0d580581995-07-20 02:43:20 -0500634 png_do_read_transformations(png_ptr);
635
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500636#if defined(PNG_READ_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500637 /* Blow up interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500638 if (png_ptr->interlaced &&
639 (png_ptr->transformations & PNG_INTERLACE))
640 {
641 if (png_ptr->pass < 6)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500642 /* Old interface (pre-1.0.9):
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500643 * png_do_read_interlace(&(png_ptr->row_info),
644 * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
645 */
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600646 png_do_read_interlace(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500647
Andreas Dilger47a0c421997-05-16 02:46:07 -0500648 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500649 png_combine_row(png_ptr, dsp_row,
650 png_pass_dsp_mask[png_ptr->pass]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500651 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500652 png_combine_row(png_ptr, row,
653 png_pass_mask[png_ptr->pass]);
654 }
655 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500656#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500657 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500658 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500659 png_combine_row(png_ptr, row, 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500660 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500661 png_combine_row(png_ptr, dsp_row, 0xff);
662 }
663 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600664
665 if (png_ptr->read_row_fn != NULL)
666 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500667}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500668#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500669
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500670#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500671/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600672 * and png_set_interlace_handling() has been called, the rows need to
673 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500674 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600675 * called, the rows contents must be initialized to the contents of the
676 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600677 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600678 * "row" holds the actual image, and pixels are placed in it
679 * as they arrive. If the image is displayed after each pass, it will
680 * appear to "sparkle" in. "display_row" can be used to display a
681 * "chunky" progressive image, with finer detail added as it becomes
682 * available. If you do not want this "chunky" display, you may pass
683 * NULL for display_row. If you do not want the sparkle display, and
684 * you have not called png_handle_alpha(), you may pass NULL for rows.
685 * If you have called png_handle_alpha(), and the image has either an
686 * alpha channel or a transparency chunk, you must provide a buffer for
687 * rows. In this case, you do not have to provide a display_row buffer
688 * also, but you may. If the image is not interlaced, or if you have
689 * not called png_set_interlace_handling(), the display_row buffer will
690 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500691 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600692 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600693 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600694
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500695void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600696png_read_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600697 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500698{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600699 png_uint_32 i;
700 png_bytepp rp;
701 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500702
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500703 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500704 if (png_ptr == NULL)
705 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600706 rp = row;
707 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500708 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500709 for (i = 0; i < num_rows; i++)
710 {
711 png_bytep rptr = *rp++;
712 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600713
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500714 png_read_row(png_ptr, rptr, dptr);
715 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500716 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500717 for (i = 0; i < num_rows; i++)
718 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500719 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500720 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500721 rp++;
722 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500723 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500724 for (i = 0; i < num_rows; i++)
725 {
726 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500727 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500728 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500729 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500730}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500731#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500732
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500733#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500734/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500735 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600736 * initialize the image to the current image that PNG will be overlaying.
737 * We set the num_rows again here, in case it was incorrectly set in
738 * png_read_start_row() by a call to png_read_update_info() or
739 * png_start_read_image() if png_set_interlace_handling() wasn't called
740 * prior to either of these functions like it should have been. You can
741 * only call this function once. If you desire to have an image for
742 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500743 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600744 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600745 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500746void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600747png_read_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500748{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500749 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500750 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600751 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500752
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500753 png_debug(1, "in png_read_image");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500754 if (png_ptr == NULL)
755 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500756
757#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -0500758 pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500759#else
760 if (png_ptr->interlaced)
761 png_error(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500762 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500763 pass = 1;
764#endif
765
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500766
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500767 image_height=png_ptr->height;
768 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
Guy Schalnate5a37791996-06-05 15:50:50 -0500769
Guy Schalnat0d580581995-07-20 02:43:20 -0500770 for (j = 0; j < pass; j++)
771 {
772 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500773 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500774 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500775 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500776 rp++;
777 }
778 }
779}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500780#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500781
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500782#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500783/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600784 * file, will verify the end is accurate, and will read any comments
785 * or time information at the end of the file, if info is not NULL.
786 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500787void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600788png_read_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500789{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500790 png_debug(1, "in png_read_end");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500791 if (png_ptr == NULL)
792 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500793 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500794
795 do
796 {
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600797#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500798 PNG_CONST PNG_IHDR;
799 PNG_CONST PNG_IDAT;
800 PNG_CONST PNG_IEND;
801 PNG_CONST PNG_PLTE;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600802#if defined(PNG_READ_bKGD_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500803 PNG_CONST PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600804#endif
805#if defined(PNG_READ_cHRM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500806 PNG_CONST PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600807#endif
808#if defined(PNG_READ_gAMA_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500809 PNG_CONST PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600810#endif
811#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500812 PNG_CONST PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600813#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600814#if defined(PNG_READ_iCCP_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500815 PNG_CONST PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600816#endif
817#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500818 PNG_CONST PNG_iTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600819#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600820#if defined(PNG_READ_oFFs_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500821 PNG_CONST PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600822#endif
823#if defined(PNG_READ_pCAL_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500824 PNG_CONST PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600825#endif
826#if defined(PNG_READ_pHYs_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500827 PNG_CONST PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600828#endif
829#if defined(PNG_READ_sBIT_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500830 PNG_CONST PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600831#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600832#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500833 PNG_CONST PNG_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600834#endif
835#if defined(PNG_READ_sPLT_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500836 PNG_CONST PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600837#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600838#if defined(PNG_READ_sRGB_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500839 PNG_CONST PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600840#endif
841#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500842 PNG_CONST PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600843#endif
844#if defined(PNG_READ_tIME_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500845 PNG_CONST PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600846#endif
847#if defined(PNG_READ_tRNS_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500848 PNG_CONST PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600849#endif
850#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500851 PNG_CONST PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600852#endif
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600853#endif /* PNG_USE_LOCAL_ARRAYS */
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500854 png_uint_32 length = png_read_chunk_header(png_ptr);
855 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500856
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500857 if (!png_memcmp(chunk_name, png_IHDR, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600858 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500859 else if (!png_memcmp(chunk_name, png_IEND, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600860 png_handle_IEND(png_ptr, info_ptr, length);
861#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500862 else if (png_handle_as_unknown(png_ptr, chunk_name))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600863 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500864 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600865 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500866 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500867 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600868 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600869 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500870 if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600871 png_ptr->mode |= PNG_HAVE_PLTE;
872 }
873#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500874 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Guy Schalnat0d580581995-07-20 02:43:20 -0500875 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500876 /* Zero length IDATs are legal after the last IDAT has been
877 * read, but not after other chunks have been read.
878 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500879 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500880 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500881 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500882 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500883 else if (!png_memcmp(chunk_name, png_PLTE, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500884 png_handle_PLTE(png_ptr, info_ptr, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500885#if defined(PNG_READ_bKGD_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500886 else if (!png_memcmp(chunk_name, png_bKGD, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500887 png_handle_bKGD(png_ptr, info_ptr, length);
888#endif
889#if defined(PNG_READ_cHRM_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500890 else if (!png_memcmp(chunk_name, png_cHRM, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500891 png_handle_cHRM(png_ptr, info_ptr, length);
892#endif
893#if defined(PNG_READ_gAMA_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500894 else if (!png_memcmp(chunk_name, png_gAMA, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500895 png_handle_gAMA(png_ptr, info_ptr, length);
896#endif
897#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500898 else if (!png_memcmp(chunk_name, png_hIST, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500899 png_handle_hIST(png_ptr, info_ptr, length);
900#endif
901#if defined(PNG_READ_oFFs_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500902 else if (!png_memcmp(chunk_name, png_oFFs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500903 png_handle_oFFs(png_ptr, info_ptr, length);
904#endif
905#if defined(PNG_READ_pCAL_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500906 else if (!png_memcmp(chunk_name, png_pCAL, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500907 png_handle_pCAL(png_ptr, info_ptr, length);
908#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600909#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500910 else if (!png_memcmp(chunk_name, png_sCAL, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600911 png_handle_sCAL(png_ptr, info_ptr, length);
912#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500913#if defined(PNG_READ_pHYs_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500914 else if (!png_memcmp(chunk_name, png_pHYs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500915 png_handle_pHYs(png_ptr, info_ptr, length);
916#endif
917#if defined(PNG_READ_sBIT_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500918 else if (!png_memcmp(chunk_name, png_sBIT, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500919 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500920#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600921#if defined(PNG_READ_sRGB_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500922 else if (!png_memcmp(chunk_name, png_sRGB, 4))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600923 png_handle_sRGB(png_ptr, info_ptr, length);
924#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600925#if defined(PNG_READ_iCCP_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500926 else if (!png_memcmp(chunk_name, png_iCCP, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600927 png_handle_iCCP(png_ptr, info_ptr, length);
928#endif
929#if defined(PNG_READ_sPLT_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500930 else if (!png_memcmp(chunk_name, png_sPLT, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600931 png_handle_sPLT(png_ptr, info_ptr, length);
932#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500933#if defined(PNG_READ_tEXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500934 else if (!png_memcmp(chunk_name, png_tEXt, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600935 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500936#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500937#if defined(PNG_READ_tIME_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500938 else if (!png_memcmp(chunk_name, png_tIME, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500939 png_handle_tIME(png_ptr, info_ptr, length);
940#endif
941#if defined(PNG_READ_tRNS_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500942 else if (!png_memcmp(chunk_name, png_tRNS, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500943 png_handle_tRNS(png_ptr, info_ptr, length);
944#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500945#if defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500946 else if (!png_memcmp(chunk_name, png_zTXt, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600947 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500948#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600949#if defined(PNG_READ_iTXt_SUPPORTED)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500950 else if (!png_memcmp(chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600951 png_handle_iTXt(png_ptr, info_ptr, length);
952#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500953 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600954 png_handle_unknown(png_ptr, info_ptr, length);
955 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500956}
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500957#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500958
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500959/* Free all memory used by the read */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500960void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500961png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600962 png_infopp end_info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500963{
964 png_structp png_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600965 png_infop info_ptr = NULL, end_info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500966#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500967 png_free_ptr free_fn = NULL;
968 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600969#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500970
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500971 png_debug(1, "in png_destroy_read_struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500972 if (png_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500973 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500974 if (png_ptr == NULL)
975 return;
976
977#ifdef PNG_USER_MEM_SUPPORTED
978 free_fn = png_ptr->free_fn;
979 mem_ptr = png_ptr->mem_ptr;
980#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500981
Andreas Dilger47a0c421997-05-16 02:46:07 -0500982 if (info_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500983 info_ptr = *info_ptr_ptr;
984
Andreas Dilger47a0c421997-05-16 02:46:07 -0500985 if (end_info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600986 end_info_ptr = *end_info_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500987
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600988 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500989
Andreas Dilger47a0c421997-05-16 02:46:07 -0500990 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500991 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600992#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600993 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600994#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500995
996#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500997 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
998 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500999#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001000 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001001#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001002 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001003 }
1004
Andreas Dilger47a0c421997-05-16 02:46:07 -05001005 if (end_info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001006 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001007#if defined(PNG_READ_TEXT_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001008 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001009#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001010#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001011 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
1012 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001013#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001014 png_destroy_struct((png_voidp)end_info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001015#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001016 *end_info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001017 }
1018
Andreas Dilger47a0c421997-05-16 02:46:07 -05001019 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001020 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001021#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001022 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1023 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001024#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001025 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001026#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001027 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001028 }
1029}
1030
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001031/* Free all memory used by the read (old method) */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001032void /* PRIVATE */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001033png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001034{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001035#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001036 jmp_buf tmp_jmp;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001037#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001038 png_error_ptr error_fn;
1039 png_error_ptr warning_fn;
1040 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001041#ifdef PNG_USER_MEM_SUPPORTED
1042 png_free_ptr free_fn;
1043#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001044
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001045 png_debug(1, "in png_read_destroy");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001046 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001047 png_info_destroy(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001048
Andreas Dilger47a0c421997-05-16 02:46:07 -05001049 if (end_info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001050 png_info_destroy(png_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001051
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001052 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -05001053 png_free(png_ptr, png_ptr->big_row_buf);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001054 png_free(png_ptr, png_ptr->prev_row);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001055 png_free(png_ptr, png_ptr->chunkdata);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001056#if defined(PNG_READ_DITHER_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001057 png_free(png_ptr, png_ptr->palette_lookup);
1058 png_free(png_ptr, png_ptr->dither_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001059#endif
1060#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001061 png_free(png_ptr, png_ptr->gamma_table);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001062#endif
1063#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001064 png_free(png_ptr, png_ptr->gamma_from_1);
1065 png_free(png_ptr, png_ptr->gamma_to_1);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001066#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001067#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001068 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001069 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001070 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001071#else
1072 if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
1073 png_zfree(png_ptr, png_ptr->palette);
1074 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
1075#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001076#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001077 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001078#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001079 if (png_ptr->free_me & PNG_FREE_TRNS)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001080 png_free(png_ptr, png_ptr->trans);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001081 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001082#else
1083 if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
1084 png_free(png_ptr, png_ptr->trans);
1085 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
1086#endif
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001087#endif
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001088#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001089#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001090 if (png_ptr->free_me & PNG_FREE_HIST)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001091 png_free(png_ptr, png_ptr->hist);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001092 png_ptr->free_me &= ~PNG_FREE_HIST;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001093#else
1094 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
1095 png_free(png_ptr, png_ptr->hist);
1096 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
1097#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001098#endif
1099#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001100 if (png_ptr->gamma_16_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001101 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001102 int i;
1103 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001104 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001105 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001106 png_free(png_ptr, png_ptr->gamma_16_table[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001107 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001108 png_free(png_ptr, png_ptr->gamma_16_table);
Guy Schalnat0d580581995-07-20 02:43:20 -05001109 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001110#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001111 if (png_ptr->gamma_16_from_1 != NULL)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001112 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001113 int i;
1114 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001115 for (i = 0; i < istop; i++)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001116 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001117 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001118 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001119 png_free(png_ptr, png_ptr->gamma_16_from_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001120 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001121 if (png_ptr->gamma_16_to_1 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001122 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001123 int i;
1124 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001125 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001126 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001127 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001128 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001129 png_free(png_ptr, png_ptr->gamma_16_to_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001130 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001131#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001132#endif
1133#if defined(PNG_TIME_RFC1123_SUPPORTED)
1134 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001135#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001136
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001137 inflateEnd(&png_ptr->zstream);
Guy Schalnat6d764711995-12-19 03:22:19 -06001138#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001139 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001140#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001141
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001142#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1143#ifdef PNG_TEXT_SUPPORTED
1144 png_free(png_ptr, png_ptr->current_text);
1145#endif /* PNG_TEXT_SUPPORTED */
1146#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1147
Guy Schalnate5a37791996-06-05 15:50:50 -05001148 /* Save the important info out of the png_struct, in case it is
1149 * being used again.
1150 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001151#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001152 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001153#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001154
1155 error_fn = png_ptr->error_fn;
1156 warning_fn = png_ptr->warning_fn;
1157 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001158#ifdef PNG_USER_MEM_SUPPORTED
1159 free_fn = png_ptr->free_fn;
1160#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001161
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001162 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001163
1164 png_ptr->error_fn = error_fn;
1165 png_ptr->warning_fn = warning_fn;
1166 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001167#ifdef PNG_USER_MEM_SUPPORTED
1168 png_ptr->free_fn = free_fn;
1169#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001170
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001171#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001172 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001173#endif
1174
Guy Schalnat0d580581995-07-20 02:43:20 -05001175}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001176
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001177void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001178png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1179{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001180 if (png_ptr == NULL)
1181 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001182 png_ptr->read_row_fn = read_row_fn;
1183}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001184
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001185
1186#ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001187#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001188void PNGAPI
1189png_read_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001190 int transforms,
1191 voidp params)
1192{
1193 int row;
1194
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001195 if (png_ptr == NULL)
1196 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001197#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001198 /* Invert the alpha channel from opacity to transparency
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001199 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001200 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1201 png_set_invert_alpha(png_ptr);
1202#endif
1203
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001204 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001205 * PNG file before the first IDAT (image data chunk).
1206 */
1207 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001208 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
1209 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001210
1211 /* -------------- image transformations start here ------------------- */
1212
1213#if defined(PNG_READ_16_TO_8_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001214 /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001215 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001216 if (transforms & PNG_TRANSFORM_STRIP_16)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001217 png_set_strip_16(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001218#endif
1219
1220#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001221 /* Strip alpha bytes from the input data without combining with
1222 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001223 */
1224 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001225 png_set_strip_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001226#endif
1227
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001228#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001229 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001230 * byte into separate bytes (useful for paletted and grayscale images).
1231 */
1232 if (transforms & PNG_TRANSFORM_PACKING)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001233 png_set_packing(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001234#endif
1235
1236#if defined(PNG_READ_PACKSWAP_SUPPORTED)
1237 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001238 * (not useful if you are using png_set_packing).
1239 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001240 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001241 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001242#endif
1243
1244#if defined(PNG_READ_EXPAND_SUPPORTED)
1245 /* Expand paletted colors into true RGB triplets
1246 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1247 * Expand paletted or RGB images with transparency to full alpha
1248 * channels so the data will be available as RGBA quartets.
1249 */
1250 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001251 if ((png_ptr->bit_depth < 8) ||
1252 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1253 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001254 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001255#endif
1256
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001257 /* We don't handle background color or gamma transformation or dithering.
1258 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001259
1260#if defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001261 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001262 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001263 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001264 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001265#endif
1266
1267#if defined(PNG_READ_SHIFT_SUPPORTED)
1268 /* If you want to shift the pixel values from the range [0,255] or
1269 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1270 * colors were originally in:
1271 */
1272 if ((transforms & PNG_TRANSFORM_SHIFT)
1273 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1274 {
1275 png_color_8p sig_bit;
1276
1277 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1278 png_set_shift(png_ptr, sig_bit);
1279 }
1280#endif
1281
1282#if defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001283 /* Flip the RGB pixels to BGR (or RGBA to BGRA)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001284 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001285 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001286 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001287#endif
1288
1289#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001290 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001291 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001292 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1293 png_set_swap_alpha(png_ptr);
1294#endif
1295
1296#if defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001297 /* Swap bytes of 16 bit files to least significant byte first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001298 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001299 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001300 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001301#endif
1302
1303 /* We don't handle adding filler bytes */
1304
1305 /* Optional call to gamma correct and add the background to the palette
1306 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001307 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001308 */
1309 png_read_update_info(png_ptr, info_ptr);
1310
1311 /* -------------- image transformations end here ------------------- */
1312
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001313#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001314 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001315#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001316 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001317 {
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001318#ifdef PNG_CALLOC_SUPPORTED
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001319 info_ptr->row_pointers = (png_bytepp)png_calloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001320 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001321#else
1322 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
1323 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -05001324 png_memset(info_ptr->row_pointers, 0, info_ptr->height
1325 * png_sizeof(png_bytep));
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001326#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001327#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001328 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001329#endif
1330 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001331 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001332 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001333 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001334
1335 png_read_image(png_ptr, info_ptr->row_pointers);
1336 info_ptr->valid |= PNG_INFO_IDAT;
1337
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001338 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001339 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001340
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001341 transforms = transforms; /* Quiet compiler warnings */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001342 params = params;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001343
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001344}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001345#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001346#endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001347#endif /* PNG_READ_SUPPORTED */