blob: 2e18f4fcb543f41d33628bda620d07f920cfad1e [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-Pehrson31aee0d2010-07-29 17:39:14 -05004 * Last changed in libpng 1.5.0 [July 29, 2010]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
15 */
Guy Schalnat0d580581995-07-20 02:43:20 -050016
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050017#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060018
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060019#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050020
Andreas Dilger47a0c421997-05-16 02:46:07 -050021/* Create a PNG structure for reading, and allocate any memory needed. */
Glenn Randers-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,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -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-Pehrsonf24daf22010-05-06 09:44:04 -050029 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050030}
31
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060032/* Alternate create PNG structure for reading, and allocate any memory
33 * needed.
34 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050035png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050036png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -060037 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
38 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050039{
40#endif /* PNG_USER_MEM_SUPPORTED */
41
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -060042#ifdef PNG_SETJMP_SUPPORTED
43 volatile
44#endif
45 png_structp png_ptr;
Glenn Randers-Pehrson62ca98e2009-12-20 15:14:57 -060046 volatile int png_cleanup_needed = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060047
48#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060049#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -060050 jmp_buf png_jmpbuf;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060051#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060052#endif
53
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050054 int i;
55
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050056 png_debug(1, "in png_create_read_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050057
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050058#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060059 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050060 malloc_fn, mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050061#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060062 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050063#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060064 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050065 return (NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050066
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -050067 /* Added at libpng-1.2.6 */
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060068#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -050069 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
70 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050071
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060072# ifdef PNG_USER_CHUNK_CACHE_MAX
73 /* Added at libpng-1.2.43 and 1.4.0 */
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -050074 png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060075# endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050076
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060077# ifdef PNG_SET_USER_CHUNK_MALLOC_MAX
78 /* Added at libpng-1.2.43 and 1.4.1 */
79 png_ptr->user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX;
80# endif
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -050081#endif
82
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050083#ifdef PNG_SETJMP_SUPPORTED
84/* Applications that neglect to set up their own setjmp() and then
85 encounter a png_error() will longjmp here. Since the jmpbuf is
86 then meaningless we abort instead of returning. */
87#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -060088 if (setjmp(png_jmpbuf))
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050089#else
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -060090 if (setjmp(png_jmpbuf(png_ptr))) /* Sets longjmp to match setjmp */
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050091#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050092 PNG_ABORT();
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -060093#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -060094 png_memcpy(png_jmpbuf(png_ptr), png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -060095#endif
Glenn Randers-Pehrsona2567be2009-10-19 20:31:31 -050096#endif /* PNG_SETJMP_SUPPORTED */
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050097
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050098#ifdef PNG_USER_MEM_SUPPORTED
99 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600100#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500101
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600102 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnat6d764711995-12-19 03:22:19 -0600103
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500104 if (user_png_ver)
Guy Schalnate5a37791996-06-05 15:50:50 -0500105 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500106 i = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500107
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500108 do
109 {
110 if (user_png_ver[i] != png_libpng_ver[i])
111 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
112 } while (png_libpng_ver[i++]);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500113 }
114
115 else
116 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
Glenn Randers-Pehrsond60c8862009-06-15 21:56:14 -0500117
Guy Schalnate5a37791996-06-05 15:50:50 -0500118
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500119 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
120 {
121 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
122 * we must recompile any applications that use any older library version.
123 * For versions after libpng 1.0, we will be compatible, so we need
124 * only check the first digit.
125 */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500126 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
127 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
128 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
129 {
Glenn Randers-Pehrson6a9e4802010-02-19 09:47:43 -0600130#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500131 char msg[80];
132 if (user_png_ver)
133 {
Glenn Randers-Pehrson92a3ef42010-03-31 21:50:21 -0500134 png_snprintf2(msg, 80,
135 "Application built with libpng-%.20s"
136 " but running with %.20s",
137 user_png_ver,
138 png_libpng_ver);
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600139 png_warning(png_ptr, msg);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500140 }
Glenn Randers-Pehrson92a3ef42010-03-31 21:50:21 -0500141#else
142 png_warning(png_ptr,
143 "Incompatible libpng version in application and library");
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500144#endif
145#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500146 png_ptr->flags = 0;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500147#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500148
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500149 png_cleanup_needed = 1;
150 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500151 }
152
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500153 if (!png_cleanup_needed)
154 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500155 /* Initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500156 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500157 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr, png_ptr->zbuf_size);
158
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500159 if (png_ptr->zbuf == NULL)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600160 png_cleanup_needed = 1;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500161 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500162
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600163 png_ptr->zstream.zalloc = png_zalloc;
164 png_ptr->zstream.zfree = png_zfree;
165 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500166
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500167 if (!png_cleanup_needed)
168 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500169 switch (inflateInit(&png_ptr->zstream))
170 {
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500171 case Z_OK:
172 break; /* Do nothing */
173
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500174 case Z_MEM_ERROR:
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500175 png_warning(png_ptr, "zlib memory error");
176 png_cleanup_needed = 1;
177 break;
178
179 case Z_STREAM_ERROR:
180 png_warning(png_ptr, "zlib stream error");
181 png_cleanup_needed = 1;
182 break;
183
184 case Z_VERSION_ERROR:
185 png_warning(png_ptr, "zlib version error");
186 png_cleanup_needed = 1;
187 break;
188
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500189 default: png_warning(png_ptr, "Unknown zlib error");
190 png_cleanup_needed = 1;
191 }
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500192 }
193
194 if (png_cleanup_needed)
195 {
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500196 /* Clean up PNG structure and deallocate any memory. */
197 png_free(png_ptr, png_ptr->zbuf);
198 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500199#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500200 png_destroy_struct_2((png_voidp)png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600201 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500202#else
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500203 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500204#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500205 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500206 }
207
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600208 png_ptr->zstream.next_out = png_ptr->zbuf;
209 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500210
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500211 png_set_read_fn(png_ptr, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500212
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500213
Guy Schalnate5a37791996-06-05 15:50:50 -0500214 return (png_ptr);
215}
216
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500217
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500218#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600219/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -0600220 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600221 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500222 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600223 * via png_set_sig_bytes(), and we will only check the remaining bytes
224 * here. The application can then have access to the signature bytes we
225 * read if it is determined that this isn't a valid PNG file.
226 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500227void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600228png_read_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500229{
Glenn Randers-Pehrsonc81bb8a2009-08-15 22:02:26 -0500230 png_debug(1, "in png_read_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500231
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500232 if (png_ptr == NULL || info_ptr == NULL)
233 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500234
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600235 /* If we haven't checked all of the PNG signature bytes, do so now. */
236 if (png_ptr->sig_bytes < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500237 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500238 png_size_t num_checked = png_ptr->sig_bytes,
239 num_to_check = 8 - num_checked;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600240
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500241#ifdef PNG_IO_STATE_SUPPORTED
242 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
243#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500244
Andreas Dilger47a0c421997-05-16 02:46:07 -0500245 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600246 png_ptr->sig_bytes = 8;
247
248 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
249 {
250 if (num_checked < 4 &&
251 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
252 png_error(png_ptr, "Not a PNG file");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500253
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600254 else
255 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
256 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500257
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600258 if (num_checked < 3)
259 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500260 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500261
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500262 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500263 {
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500264 PNG_IHDR;
265 PNG_IDAT;
266 PNG_IEND;
267 PNG_PLTE;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500268#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500269 PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600270#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500271#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500272 PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600273#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500274#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500275 PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600276#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500277#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500278 PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600279#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500280#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500281 PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600282#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500283#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500284 PNG_iTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600285#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500286#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500287 PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600288#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500289#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500290 PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600291#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500292#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500293 PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600294#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500295#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500296 PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600297#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500298#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500299 PNG_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600300#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500301#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500302 PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600303#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500304#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500305 PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600306#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500307#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500308 PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600309#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500310#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500311 PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600312#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500313#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500314 PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600315#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500316#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500317 PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600318#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500319 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500320 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500321
322 /* This should be a binary subdivision search or a hash for
323 * matching the chunk name rather than a linear search.
324 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500325 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600326 if (png_ptr->mode & PNG_AFTER_IDAT)
327 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500328
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500329 if (!png_memcmp(chunk_name, png_IHDR, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600330 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500331
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500332 else if (!png_memcmp(chunk_name, png_IEND, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600333 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500334
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600335#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500336 else if (png_handle_as_unknown(png_ptr, chunk_name))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600337 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500338 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600339 png_ptr->mode |= PNG_HAVE_IDAT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500340
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600341 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500342
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500343 if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600344 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500345
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500346 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600347 {
348 if (!(png_ptr->mode & PNG_HAVE_IHDR))
349 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500350
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600351 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600352 !(png_ptr->mode & PNG_HAVE_PLTE))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600353 png_error(png_ptr, "Missing PLTE before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500354
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600355 break;
356 }
357 }
358#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500359 else if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600360 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500361
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500362 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500363 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500364 if (!(png_ptr->mode & PNG_HAVE_IHDR))
365 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500366
Guy Schalnate5a37791996-06-05 15:50:50 -0500367 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600368 !(png_ptr->mode & PNG_HAVE_PLTE))
Guy Schalnate5a37791996-06-05 15:50:50 -0500369 png_error(png_ptr, "Missing PLTE before IDAT");
370
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500371 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500372 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500373 break;
374 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500375
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500376#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500377 else if (!png_memcmp(chunk_name, png_bKGD, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500378 png_handle_bKGD(png_ptr, info_ptr, length);
379#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500380
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500381#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500382 else if (!png_memcmp(chunk_name, png_cHRM, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383 png_handle_cHRM(png_ptr, info_ptr, length);
384#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500385
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500386#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500387 else if (!png_memcmp(chunk_name, png_gAMA, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500388 png_handle_gAMA(png_ptr, info_ptr, length);
389#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500390
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500391#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500392 else if (!png_memcmp(chunk_name, png_hIST, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500393 png_handle_hIST(png_ptr, info_ptr, length);
394#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500395
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500396#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500397 else if (!png_memcmp(chunk_name, png_oFFs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500398 png_handle_oFFs(png_ptr, info_ptr, length);
399#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500400
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500401#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500402 else if (!png_memcmp(chunk_name, png_pCAL, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500403 png_handle_pCAL(png_ptr, info_ptr, length);
404#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500405
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500406#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500407 else if (!png_memcmp(chunk_name, png_sCAL, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600408 png_handle_sCAL(png_ptr, info_ptr, length);
409#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500410
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500411#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500412 else if (!png_memcmp(chunk_name, png_pHYs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500413 png_handle_pHYs(png_ptr, info_ptr, length);
414#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500415
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500416#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500417 else if (!png_memcmp(chunk_name, png_sBIT, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500418 png_handle_sBIT(png_ptr, info_ptr, length);
419#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500420
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500421#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500422 else if (!png_memcmp(chunk_name, png_sRGB, 4))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600423 png_handle_sRGB(png_ptr, info_ptr, length);
424#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500425
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500426#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500427 else if (!png_memcmp(chunk_name, png_iCCP, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600428 png_handle_iCCP(png_ptr, info_ptr, length);
429#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500430
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500431#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500432 else if (!png_memcmp(chunk_name, png_sPLT, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600433 png_handle_sPLT(png_ptr, info_ptr, length);
434#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500435
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500436#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500437 else if (!png_memcmp(chunk_name, png_tEXt, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500438 png_handle_tEXt(png_ptr, info_ptr, length);
439#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500440
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500441#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500442 else if (!png_memcmp(chunk_name, png_tIME, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500443 png_handle_tIME(png_ptr, info_ptr, length);
444#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500445
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500446#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500447 else if (!png_memcmp(chunk_name, png_tRNS, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500448 png_handle_tRNS(png_ptr, info_ptr, length);
449#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500450
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500451#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500452 else if (!png_memcmp(chunk_name, png_zTXt, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453 png_handle_zTXt(png_ptr, info_ptr, length);
454#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500455
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500456#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500457 else if (!png_memcmp(chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600458 png_handle_iTXt(png_ptr, info_ptr, length);
459#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500460
Guy Schalnat0d580581995-07-20 02:43:20 -0500461 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600462 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500463 }
464}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500465#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500466
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500467/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500468void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600469png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500470{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500471 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500472
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500473 if (png_ptr == NULL)
474 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500475
Guy Schalnate5a37791996-06-05 15:50:50 -0500476 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500477 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500478
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600479 else
480 png_warning(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600481 "Ignoring extra png_read_update_info() call;"
482 " row buffer not reallocated");
Glenn Randers-Pehrson4cfdb3c2009-11-26 11:49:37 -0600483
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600484 png_read_transform_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500485}
486
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500487#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600488/* Initialize palette, background, etc, after transformations
489 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500490 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600491 * If the user doesn't call this, we will do it ourselves.
492 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500493void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600494png_start_read_image(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500495{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500496 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500497
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500498 if (png_ptr == NULL)
499 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500500
Guy Schalnate5a37791996-06-05 15:50:50 -0500501 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500502 png_read_start_row(png_ptr);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500503 else
504 png_warning(png_ptr,
505 "Ignoring extra png_start_read_image() call;"
506 " row buffer not reallocated");
Guy Schalnat0d580581995-07-20 02:43:20 -0500507}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500508#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500509
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500510#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500511void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600512png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500513{
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500514 PNG_IDAT;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500515 PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600516 0xff};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500517 PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
Guy Schalnat0d580581995-07-20 02:43:20 -0500518 int ret;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500519
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500520 if (png_ptr == NULL)
521 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500522
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500523 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600524 (unsigned long) png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500525
Guy Schalnate5a37791996-06-05 15:50:50 -0500526 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500527 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500528
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500529 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
530 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500531 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500532#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
533 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500534 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500535#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500536
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500537#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
538 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500539 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500540#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500541
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600542#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
543 !defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500544 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500545 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500546#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500547
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500548#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
549 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500550 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500551#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500552
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500553#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
554 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500555 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500556#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500557
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500558#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
559 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500560 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500561#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500562
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500563#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
564 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500565 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500566#endif
567 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500568
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500569#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500570 /* If interlaced and we do not need a new row, combine row and return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500571 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
572 {
573 switch (png_ptr->pass)
574 {
575 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600576 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500577 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500578 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500579 png_combine_row(png_ptr, dsp_row,
580 png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600581 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500582 return;
583 }
584 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500585
Guy Schalnat0d580581995-07-20 02:43:20 -0500586 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600587 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500588 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500589 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500590 png_combine_row(png_ptr, dsp_row,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500591 png_pass_dsp_mask[png_ptr->pass]);
592
Guy Schalnat0d580581995-07-20 02:43:20 -0500593 png_read_finish_row(png_ptr);
594 return;
595 }
596 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500597
Guy Schalnat0d580581995-07-20 02:43:20 -0500598 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600599 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500600 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500601 if (dsp_row != NULL && (png_ptr->row_number & 4))
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600602 png_combine_row(png_ptr, dsp_row,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500603 png_pass_dsp_mask[png_ptr->pass]);
604
Guy Schalnat0d580581995-07-20 02:43:20 -0500605 png_read_finish_row(png_ptr);
606 return;
607 }
608 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500609
Guy Schalnat0d580581995-07-20 02:43:20 -0500610 case 3:
611 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
612 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500613 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500614 png_combine_row(png_ptr, dsp_row,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500615 png_pass_dsp_mask[png_ptr->pass]);
616
Guy Schalnat0d580581995-07-20 02:43:20 -0500617 png_read_finish_row(png_ptr);
618 return;
619 }
620 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500621
Guy Schalnat0d580581995-07-20 02:43:20 -0500622 case 4:
623 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600624 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500625 if (dsp_row != NULL && (png_ptr->row_number & 2))
Guy Schalnat0d580581995-07-20 02:43:20 -0500626 png_combine_row(png_ptr, dsp_row,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500627 png_pass_dsp_mask[png_ptr->pass]);
628
Guy Schalnat0d580581995-07-20 02:43:20 -0500629 png_read_finish_row(png_ptr);
630 return;
631 }
632 break;
633 case 5:
634 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
635 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500636 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500637 png_combine_row(png_ptr, dsp_row,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500638 png_pass_dsp_mask[png_ptr->pass]);
639
Guy Schalnat0d580581995-07-20 02:43:20 -0500640 png_read_finish_row(png_ptr);
641 return;
642 }
643 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500644
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600645 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500646 if (!(png_ptr->row_number & 1))
647 {
648 png_read_finish_row(png_ptr);
649 return;
650 }
651 break;
652 }
653 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500654#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500655
Guy Schalnate5a37791996-06-05 15:50:50 -0500656 if (!(png_ptr->mode & PNG_HAVE_IDAT))
657 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500658
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600659 png_ptr->zstream.next_out = png_ptr->row_buf;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600660 png_ptr->zstream.avail_out =
661 (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
662 png_ptr->iwidth) + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500663
Guy Schalnat0d580581995-07-20 02:43:20 -0500664 do
665 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600666 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500667 {
668 while (!png_ptr->idat_size)
669 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600670 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500671
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500672 png_ptr->idat_size = png_read_chunk_header(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600673 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
674 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500675 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600676 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
677 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500678 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600679 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500680 png_crc_read(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600681 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600682 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500683 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500684
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600685 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500686
Guy Schalnat0d580581995-07-20 02:43:20 -0500687 if (ret == Z_STREAM_END)
688 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600689 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500690 png_ptr->idat_size)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500691 png_benign_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600692 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500693 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600694 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500695 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500696
Guy Schalnat0d580581995-07-20 02:43:20 -0500697 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600698 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600699 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600700
701 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500702
Guy Schalnat0d580581995-07-20 02:43:20 -0500703 png_ptr->row_info.color_type = png_ptr->color_type;
704 png_ptr->row_info.width = png_ptr->iwidth;
705 png_ptr->row_info.channels = png_ptr->channels;
706 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
707 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500708 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
709 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500710
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500711 if (png_ptr->row_buf[0])
Guy Schalnate5a37791996-06-05 15:50:50 -0500712 png_read_filter_row(png_ptr, &(png_ptr->row_info),
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600713 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
714 (int)(png_ptr->row_buf[0]));
Guy Schalnat0d580581995-07-20 02:43:20 -0500715
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500716 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600717
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500718#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500719 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600720 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600721 {
722 /* Intrapixel differencing */
723 png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
724 }
725#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500726
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500727
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -0600728 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
Guy Schalnat0d580581995-07-20 02:43:20 -0500729 png_do_read_transformations(png_ptr);
730
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500731#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500732 /* Blow up interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500733 if (png_ptr->interlaced &&
734 (png_ptr->transformations & PNG_INTERLACE))
735 {
736 if (png_ptr->pass < 6)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500737 /* Old interface (pre-1.0.9):
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500738 * png_do_read_interlace(&(png_ptr->row_info),
739 * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
740 */
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600741 png_do_read_interlace(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500742
Andreas Dilger47a0c421997-05-16 02:46:07 -0500743 if (dsp_row != NULL)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600744 png_combine_row(png_ptr, dsp_row, png_pass_dsp_mask[png_ptr->pass]);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500745
Andreas Dilger47a0c421997-05-16 02:46:07 -0500746 if (row != NULL)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600747 png_combine_row(png_ptr, row, png_pass_mask[png_ptr->pass]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500748 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500749
Guy Schalnat0d580581995-07-20 02:43:20 -0500750 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500751#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500752 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500753 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500754 png_combine_row(png_ptr, row, 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500755
Andreas Dilger47a0c421997-05-16 02:46:07 -0500756 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500757 png_combine_row(png_ptr, dsp_row, 0xff);
758 }
759 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600760
761 if (png_ptr->read_row_fn != NULL)
762 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500763}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500764#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500765
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500766#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500767/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600768 * and png_set_interlace_handling() has been called, the rows need to
769 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500770 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600771 * called, the rows contents must be initialized to the contents of the
772 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600773 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600774 * "row" holds the actual image, and pixels are placed in it
775 * as they arrive. If the image is displayed after each pass, it will
776 * appear to "sparkle" in. "display_row" can be used to display a
777 * "chunky" progressive image, with finer detail added as it becomes
778 * available. If you do not want this "chunky" display, you may pass
779 * NULL for display_row. If you do not want the sparkle display, and
780 * you have not called png_handle_alpha(), you may pass NULL for rows.
781 * If you have called png_handle_alpha(), and the image has either an
782 * alpha channel or a transparency chunk, you must provide a buffer for
783 * rows. In this case, you do not have to provide a display_row buffer
784 * also, but you may. If the image is not interlaced, or if you have
785 * not called png_set_interlace_handling(), the display_row buffer will
786 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500787 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600788 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600789 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600790
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500791void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600792png_read_rows(png_structp png_ptr, png_bytepp row,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600793 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500794{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600795 png_uint_32 i;
796 png_bytepp rp;
797 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500798
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500799 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500800
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500801 if (png_ptr == NULL)
802 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500803
Guy Schalnat0f716451995-11-28 11:22:13 -0600804 rp = row;
805 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500806 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500807 for (i = 0; i < num_rows; i++)
808 {
809 png_bytep rptr = *rp++;
810 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600811
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500812 png_read_row(png_ptr, rptr, dptr);
813 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500814
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500815 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500816 for (i = 0; i < num_rows; i++)
817 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500818 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500819 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500820 rp++;
821 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500822
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500823 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500824 for (i = 0; i < num_rows; i++)
825 {
826 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500827 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500828 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500829 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500830}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500831#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500832
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500833#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500834/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500835 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600836 * initialize the image to the current image that PNG will be overlaying.
837 * We set the num_rows again here, in case it was incorrectly set in
838 * png_read_start_row() by a call to png_read_update_info() or
839 * png_start_read_image() if png_set_interlace_handling() wasn't called
840 * prior to either of these functions like it should have been. You can
841 * only call this function once. If you desire to have an image for
842 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500843 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600844 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600845 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500846void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600847png_read_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500848{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500849 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500850 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600851 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500852
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500853 png_debug(1, "in png_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500854
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500855 if (png_ptr == NULL)
856 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500857
858#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500859 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
860 {
861 pass = png_set_interlace_handling(png_ptr);
862 /* And make sure transforms are initialized. */
863 png_start_read_image(png_ptr);
864 }
865 else
866 {
867 if (!(png_ptr->transformations & PNG_INTERLACE))
868 {
869 /* Caller called png_start_read_image or png_read_update_info without
870 * first turning on the PNG_INTERLACE transform. We can fix this here,
871 * but the caller should do it!
872 */
873 png_warning(png_ptr, "Interlace handling should be turned on when "
874 "using png_read_image");
875 /* Make sure this is set correctly */
876 png_ptr->num_rows = png_ptr->height;
877 }
878
879 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
880 * the above error case.
881 */
882 pass = png_set_interlace_handling(png_ptr);
883 }
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500884#else
885 if (png_ptr->interlaced)
886 png_error(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600887 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500888
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500889 pass = 1;
890#endif
891
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500892 image_height=png_ptr->height;
Guy Schalnate5a37791996-06-05 15:50:50 -0500893
Guy Schalnat0d580581995-07-20 02:43:20 -0500894 for (j = 0; j < pass; j++)
895 {
896 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500897 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500898 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500899 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500900 rp++;
901 }
902 }
903}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500904#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500905
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500906#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500907/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600908 * file, will verify the end is accurate, and will read any comments
909 * or time information at the end of the file, if info is not NULL.
910 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500911void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600912png_read_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500913{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500914 png_debug(1, "in png_read_end");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500915
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500916 if (png_ptr == NULL)
917 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500918
Andreas Dilger47a0c421997-05-16 02:46:07 -0500919 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500920
921 do
922 {
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500923 PNG_IHDR;
924 PNG_IDAT;
925 PNG_IEND;
926 PNG_PLTE;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500927#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500928 PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600929#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500930#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500931 PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600932#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500933#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500934 PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600935#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500936#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500937 PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600938#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500939#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500940 PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600941#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500942#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500943 PNG_iTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600944#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500945#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500946 PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600947#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500948#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500949 PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600950#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500951#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500952 PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600953#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500954#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500955 PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600956#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500957#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500958 PNG_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600959#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500960#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500961 PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600962#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500963#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500964 PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600965#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500966#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500967 PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600968#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500969#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500970 PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600971#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500972#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500973 PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600974#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500975#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500976 PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600977#endif
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500978 png_uint_32 length = png_read_chunk_header(png_ptr);
979 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500980
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500981 if (!png_memcmp(chunk_name, png_IHDR, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600982 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500983
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500984 else if (!png_memcmp(chunk_name, png_IEND, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600985 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500986
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600987#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500988 else if (png_handle_as_unknown(png_ptr, chunk_name))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600989 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500990 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600991 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500992 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500993 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600994 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600995 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500996 if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600997 png_ptr->mode |= PNG_HAVE_PLTE;
998 }
999#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001000
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001001 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Guy Schalnat0d580581995-07-20 02:43:20 -05001002 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001003 /* Zero length IDATs are legal after the last IDAT has been
1004 * read, but not after other chunks have been read.
1005 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001006 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001007 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001008
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001009 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -05001010 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001011 else if (!png_memcmp(chunk_name, png_PLTE, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001012 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001013
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001014#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001015 else if (!png_memcmp(chunk_name, png_bKGD, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001016 png_handle_bKGD(png_ptr, info_ptr, length);
1017#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001018
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001019#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001020 else if (!png_memcmp(chunk_name, png_cHRM, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001021 png_handle_cHRM(png_ptr, info_ptr, length);
1022#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001023
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001024#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001025 else if (!png_memcmp(chunk_name, png_gAMA, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001026 png_handle_gAMA(png_ptr, info_ptr, length);
1027#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001028
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001029#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001030 else if (!png_memcmp(chunk_name, png_hIST, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001031 png_handle_hIST(png_ptr, info_ptr, length);
1032#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001033
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001034#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001035 else if (!png_memcmp(chunk_name, png_oFFs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001036 png_handle_oFFs(png_ptr, info_ptr, length);
1037#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001038
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001039#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001040 else if (!png_memcmp(chunk_name, png_pCAL, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001041 png_handle_pCAL(png_ptr, info_ptr, length);
1042#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001043
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001044#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001045 else if (!png_memcmp(chunk_name, png_sCAL, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001046 png_handle_sCAL(png_ptr, info_ptr, length);
1047#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001048
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001049#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001050 else if (!png_memcmp(chunk_name, png_pHYs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001051 png_handle_pHYs(png_ptr, info_ptr, length);
1052#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001053
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001054#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001055 else if (!png_memcmp(chunk_name, png_sBIT, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001056 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001057#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001058
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001059#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001060 else if (!png_memcmp(chunk_name, png_sRGB, 4))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001061 png_handle_sRGB(png_ptr, info_ptr, length);
1062#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001063
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001064#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001065 else if (!png_memcmp(chunk_name, png_iCCP, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001066 png_handle_iCCP(png_ptr, info_ptr, length);
1067#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001068
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001069#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001070 else if (!png_memcmp(chunk_name, png_sPLT, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001071 png_handle_sPLT(png_ptr, info_ptr, length);
1072#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001073
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001074#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001075 else if (!png_memcmp(chunk_name, png_tEXt, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001076 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001077#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001078
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001079#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001080 else if (!png_memcmp(chunk_name, png_tIME, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001081 png_handle_tIME(png_ptr, info_ptr, length);
1082#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001083
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001084#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001085 else if (!png_memcmp(chunk_name, png_tRNS, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001086 png_handle_tRNS(png_ptr, info_ptr, length);
1087#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001088
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001089#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001090 else if (!png_memcmp(chunk_name, png_zTXt, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001091 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001092#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001093
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001094#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001095 else if (!png_memcmp(chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001096 png_handle_iTXt(png_ptr, info_ptr, length);
1097#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001098
Guy Schalnat0d580581995-07-20 02:43:20 -05001099 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001100 png_handle_unknown(png_ptr, info_ptr, length);
1101 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -05001102}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001103#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05001104
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001105/* Free all memory used by the read */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001106void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -05001107png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001108 png_infopp end_info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -05001109{
1110 png_structp png_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001111 png_infop info_ptr = NULL, end_info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001112#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001113 png_free_ptr free_fn = NULL;
1114 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001115#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001116
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001117 png_debug(1, "in png_destroy_read_struct");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05001118
Andreas Dilger47a0c421997-05-16 02:46:07 -05001119 if (png_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001120 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001121 if (png_ptr == NULL)
1122 return;
1123
1124#ifdef PNG_USER_MEM_SUPPORTED
1125 free_fn = png_ptr->free_fn;
1126 mem_ptr = png_ptr->mem_ptr;
1127#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001128
Andreas Dilger47a0c421997-05-16 02:46:07 -05001129 if (info_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001130 info_ptr = *info_ptr_ptr;
1131
Andreas Dilger47a0c421997-05-16 02:46:07 -05001132 if (end_info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001133 end_info_ptr = *end_info_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -05001134
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001135 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -05001136
Andreas Dilger47a0c421997-05-16 02:46:07 -05001137 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001138 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001139#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001140 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001141#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001142
1143#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001144 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1145 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001146#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001147 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001148#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001149 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001150 }
1151
Andreas Dilger47a0c421997-05-16 02:46:07 -05001152 if (end_info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001153 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001154#ifdef PNG_READ_TEXT_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001155 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001156#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001157#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001158 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001159 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001160#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001161 png_destroy_struct((png_voidp)end_info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001162#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001163 *end_info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001164 }
1165
Andreas Dilger47a0c421997-05-16 02:46:07 -05001166 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001167 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001168#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001169 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1170 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001171#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001172 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001173#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001174 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001175 }
1176}
1177
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001178/* Free all memory used by the read (old method) */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001179void /* PRIVATE */
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001180png_read_destroy(png_structp png_ptr, png_infop info_ptr,
1181 png_infop end_info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001182{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001183#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001184 jmp_buf tmp_jmp;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001185#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001186 png_error_ptr error_fn;
1187 png_error_ptr warning_fn;
1188 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001189#ifdef PNG_USER_MEM_SUPPORTED
1190 png_free_ptr free_fn;
1191#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001192
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001193 png_debug(1, "in png_read_destroy");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05001194
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001196 png_info_destroy(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001197
Andreas Dilger47a0c421997-05-16 02:46:07 -05001198 if (end_info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001199 png_info_destroy(png_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001200
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001201 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -05001202 png_free(png_ptr, png_ptr->big_row_buf);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001203 png_free(png_ptr, png_ptr->prev_row);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001204 png_free(png_ptr, png_ptr->chunkdata);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001205
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001206#ifdef PNG_READ_QUANTIZE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001207 png_free(png_ptr, png_ptr->palette_lookup);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001208 png_free(png_ptr, png_ptr->quantize_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001209#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001210
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001211#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001212 png_free(png_ptr, png_ptr->gamma_table);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001213#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001214
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001215#ifdef PNG_READ_BACKGROUND_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001216 png_free(png_ptr, png_ptr->gamma_from_1);
1217 png_free(png_ptr, png_ptr->gamma_to_1);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001218#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001219
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001220 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001221 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001222 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001223
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001224#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001225 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001226 if (png_ptr->free_me & PNG_FREE_TRNS)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001227 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001228 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001229#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001230
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001231#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001232 if (png_ptr->free_me & PNG_FREE_HIST)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001233 png_free(png_ptr, png_ptr->hist);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001234 png_ptr->free_me &= ~PNG_FREE_HIST;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001235#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001236
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001237#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001238 if (png_ptr->gamma_16_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001239 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001240 int i;
1241 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001242 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001243 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001244 png_free(png_ptr, png_ptr->gamma_16_table[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001245 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001246 png_free(png_ptr, png_ptr->gamma_16_table);
Guy Schalnat0d580581995-07-20 02:43:20 -05001247 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001248
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001249#ifdef PNG_READ_BACKGROUND_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001250 if (png_ptr->gamma_16_from_1 != NULL)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001251 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001252 int i;
1253 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001254 for (i = 0; i < istop; i++)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001255 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001256 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001257 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001258 png_free(png_ptr, png_ptr->gamma_16_from_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001259 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001260 if (png_ptr->gamma_16_to_1 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001261 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001262 int i;
1263 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001264 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001265 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001266 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001267 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001268 png_free(png_ptr, png_ptr->gamma_16_to_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001269 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001270#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001271#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001272
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001273#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001274 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001275#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001276
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001277 inflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001278
Guy Schalnat6d764711995-12-19 03:22:19 -06001279#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001280 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001281#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001282
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001283#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1284#ifdef PNG_TEXT_SUPPORTED
1285 png_free(png_ptr, png_ptr->current_text);
1286#endif /* PNG_TEXT_SUPPORTED */
1287#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1288
Guy Schalnate5a37791996-06-05 15:50:50 -05001289 /* Save the important info out of the png_struct, in case it is
1290 * being used again.
1291 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001292#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -06001293 png_memcpy(tmp_jmp, png_ptr->png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001294#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001295
1296 error_fn = png_ptr->error_fn;
1297 warning_fn = png_ptr->warning_fn;
1298 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001299#ifdef PNG_USER_MEM_SUPPORTED
1300 free_fn = png_ptr->free_fn;
1301#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001302
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001303 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001304
1305 png_ptr->error_fn = error_fn;
1306 png_ptr->warning_fn = warning_fn;
1307 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001308#ifdef PNG_USER_MEM_SUPPORTED
1309 png_ptr->free_fn = free_fn;
1310#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001311
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001312#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -06001313 png_memcpy(png_ptr->png_jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001314#endif
1315
Guy Schalnat0d580581995-07-20 02:43:20 -05001316}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001317
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001318void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001319png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1320{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001321 if (png_ptr == NULL)
1322 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001323
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001324 png_ptr->read_row_fn = read_row_fn;
1325}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001326
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001327
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001328#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001329#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001330void PNGAPI
1331png_read_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001332 int transforms,
1333 voidp params)
1334{
1335 int row;
1336
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001337 if (png_ptr == NULL)
1338 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001339
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001340 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001341 * PNG file before the first IDAT (image data chunk).
1342 */
1343 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001344 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
1345 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001346
1347 /* -------------- image transformations start here ------------------- */
1348
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001349#ifdef PNG_READ_16_TO_8_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001350 /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001351 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001352 if (transforms & PNG_TRANSFORM_STRIP_16)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001353 png_set_strip_16(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001354#endif
1355
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001356#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001357 /* Strip alpha bytes from the input data without combining with
1358 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001359 */
1360 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001361 png_set_strip_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001362#endif
1363
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001364#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001365 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001366 * byte into separate bytes (useful for paletted and grayscale images).
1367 */
1368 if (transforms & PNG_TRANSFORM_PACKING)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001369 png_set_packing(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001370#endif
1371
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001372#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001373 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001374 * (not useful if you are using png_set_packing).
1375 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001376 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001377 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001378#endif
1379
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001380#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001381 /* Expand paletted colors into true RGB triplets
1382 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1383 * Expand paletted or RGB images with transparency to full alpha
1384 * channels so the data will be available as RGBA quartets.
1385 */
1386 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001387 if ((png_ptr->bit_depth < 8) ||
1388 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1389 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001390 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001391#endif
1392
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001393 /* We don't handle background color or gamma transformation or quantizing.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001394 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001395
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001396#ifdef PNG_READ_INVERT_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001397 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001398 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001399 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001400 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001401#endif
1402
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001403#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001404 /* If you want to shift the pixel values from the range [0,255] or
1405 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1406 * colors were originally in:
1407 */
1408 if ((transforms & PNG_TRANSFORM_SHIFT)
1409 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1410 {
1411 png_color_8p sig_bit;
1412
1413 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1414 png_set_shift(png_ptr, sig_bit);
1415 }
1416#endif
1417
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001418#ifdef PNG_READ_BGR_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001419 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001420 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001421 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001422#endif
1423
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001424#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001425 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001426 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001427 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001428#endif
1429
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001430#ifdef PNG_READ_SWAP_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001431 /* Swap bytes of 16 bit files to least significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001432 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001433 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001434#endif
1435
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001436/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001437#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001438 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001439 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001440 png_set_invert_alpha(png_ptr);
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001441#endif
1442
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001443/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001444#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001445 /* Expand grayscale image to RGB */
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001446 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001447 png_set_gray_to_rgb(png_ptr);
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001448#endif
1449
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001450 /* We don't handle adding filler bytes */
1451
1452 /* Optional call to gamma correct and add the background to the palette
1453 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001454 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001455 */
1456 png_read_update_info(png_ptr, info_ptr);
1457
1458 /* -------------- image transformations end here ------------------- */
1459
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001460 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001461 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001462 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001463 png_uint_32 iptr;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001464
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001465 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001466 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001467 for (iptr=0; iptr<info_ptr->height; iptr++)
1468 info_ptr->row_pointers[iptr] = NULL;
1469
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001470 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001471
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001472 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001473 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001474 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001475 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001476
1477 png_read_image(png_ptr, info_ptr->row_pointers);
1478 info_ptr->valid |= PNG_INFO_IDAT;
1479
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001480 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001481 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001482
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001483 transforms = transforms; /* Quiet compiler warnings */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001484 params = params;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001485
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001486}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001487#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001488#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001489#endif /* PNG_READ_SUPPORTED */