blob: 056bcbdac16fa1a70d34da5e0c8b9a66c9325bd1 [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-Pehrsonf5ea1b72011-01-06 06:42:51 -06004 * Last changed in libpng 1.5.0 [January 6, 2011]
Glenn Randers-Pehrson64b863c2011-01-04 09:57:06 -06005 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
15 */
Guy Schalnat0d580581995-07-20 02:43:20 -050016
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050017#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060018
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060019#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050020
Andreas Dilger47a0c421997-05-16 02:46:07 -050021/* Create a PNG structure for reading, and allocate any memory needed. */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050022PNG_FUNCTION(png_structp,PNGAPI
23png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
24 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
Guy Schalnat0d580581995-07-20 02:43:20 -050025{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050026
27#ifdef PNG_USER_MEM_SUPPORTED
28 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050029 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050030}
31
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060032/* Alternate create PNG structure for reading, and allocate any memory
33 * needed.
34 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050035PNG_FUNCTION(png_structp,PNGAPI
36png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -060037 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050038 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050039{
40#endif /* PNG_USER_MEM_SUPPORTED */
41
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -060042#ifdef PNG_SETJMP_SUPPORTED
43 volatile
44#endif
45 png_structp png_ptr;
Glenn Randers-Pehrson62ca98e2009-12-20 15:14:57 -060046 volatile int png_cleanup_needed = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060047
48#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060049#ifdef USE_FAR_KEYWORD
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
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600235 /* Read and check the PNG file signature. */
236 png_read_sig(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500237
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500238 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500239 {
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500240 PNG_IHDR;
241 PNG_IDAT;
242 PNG_IEND;
243 PNG_PLTE;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500244#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500245 PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600246#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500247#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500248 PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600249#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500250#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500251 PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600252#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500253#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500254 PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600255#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500256#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500257 PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600258#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500259#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500260 PNG_iTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600261#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500262#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500263 PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600264#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500265#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500266 PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600267#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500268#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500269 PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600270#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500271#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500272 PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600273#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500274#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500275 PNG_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600276#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500277#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500278 PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600279#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500280#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500281 PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600282#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500283#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500284 PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600285#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500286#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500287 PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600288#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500289#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500290 PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600291#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500292#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500293 PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600294#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500295 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500296 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297
298 /* This should be a binary subdivision search or a hash for
299 * matching the chunk name rather than a linear search.
300 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500301 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600302 if (png_ptr->mode & PNG_AFTER_IDAT)
303 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500304
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500305 if (!png_memcmp(chunk_name, png_IHDR, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600306 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500307
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500308 else if (!png_memcmp(chunk_name, png_IEND, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600309 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500310
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600311#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500312 else if (png_handle_as_unknown(png_ptr, chunk_name))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600313 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500314 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600315 png_ptr->mode |= PNG_HAVE_IDAT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500316
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600317 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500318
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500319 if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600320 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500321
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500322 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600323 {
324 if (!(png_ptr->mode & PNG_HAVE_IHDR))
325 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500326
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600327 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600328 !(png_ptr->mode & PNG_HAVE_PLTE))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600329 png_error(png_ptr, "Missing PLTE before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500330
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600331 break;
332 }
333 }
334#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500335 else if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600336 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500337
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500338 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500339 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500340 if (!(png_ptr->mode & PNG_HAVE_IHDR))
341 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500342
Guy Schalnate5a37791996-06-05 15:50:50 -0500343 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600344 !(png_ptr->mode & PNG_HAVE_PLTE))
Guy Schalnate5a37791996-06-05 15:50:50 -0500345 png_error(png_ptr, "Missing PLTE before IDAT");
346
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500347 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500348 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500349 break;
350 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500351
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500352#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500353 else if (!png_memcmp(chunk_name, png_bKGD, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500354 png_handle_bKGD(png_ptr, info_ptr, length);
355#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500356
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500357#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500358 else if (!png_memcmp(chunk_name, png_cHRM, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500359 png_handle_cHRM(png_ptr, info_ptr, length);
360#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500361
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500362#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500363 else if (!png_memcmp(chunk_name, png_gAMA, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500364 png_handle_gAMA(png_ptr, info_ptr, length);
365#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500366
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500367#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500368 else if (!png_memcmp(chunk_name, png_hIST, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369 png_handle_hIST(png_ptr, info_ptr, length);
370#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500371
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500372#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500373 else if (!png_memcmp(chunk_name, png_oFFs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500374 png_handle_oFFs(png_ptr, info_ptr, length);
375#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500376
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500377#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500378 else if (!png_memcmp(chunk_name, png_pCAL, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500379 png_handle_pCAL(png_ptr, info_ptr, length);
380#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500381
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500382#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500383 else if (!png_memcmp(chunk_name, png_sCAL, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600384 png_handle_sCAL(png_ptr, info_ptr, length);
385#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500386
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500387#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500388 else if (!png_memcmp(chunk_name, png_pHYs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500389 png_handle_pHYs(png_ptr, info_ptr, length);
390#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500391
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500392#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500393 else if (!png_memcmp(chunk_name, png_sBIT, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500394 png_handle_sBIT(png_ptr, info_ptr, length);
395#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500396
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500397#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500398 else if (!png_memcmp(chunk_name, png_sRGB, 4))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600399 png_handle_sRGB(png_ptr, info_ptr, length);
400#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500401
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500402#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500403 else if (!png_memcmp(chunk_name, png_iCCP, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600404 png_handle_iCCP(png_ptr, info_ptr, length);
405#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500406
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500407#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500408 else if (!png_memcmp(chunk_name, png_sPLT, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600409 png_handle_sPLT(png_ptr, info_ptr, length);
410#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500411
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500412#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500413 else if (!png_memcmp(chunk_name, png_tEXt, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500414 png_handle_tEXt(png_ptr, info_ptr, length);
415#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500416
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500417#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500418 else if (!png_memcmp(chunk_name, png_tIME, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500419 png_handle_tIME(png_ptr, info_ptr, length);
420#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500421
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500422#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500423 else if (!png_memcmp(chunk_name, png_tRNS, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500424 png_handle_tRNS(png_ptr, info_ptr, length);
425#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500426
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500427#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500428 else if (!png_memcmp(chunk_name, png_zTXt, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500429 png_handle_zTXt(png_ptr, info_ptr, length);
430#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500431
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500432#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500433 else if (!png_memcmp(chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600434 png_handle_iTXt(png_ptr, info_ptr, length);
435#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500436
Guy Schalnat0d580581995-07-20 02:43:20 -0500437 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600438 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500439 }
440}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500441#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500442
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500443/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500444void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600445png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500446{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500447 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500448
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500449 if (png_ptr == NULL)
450 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500451
Guy Schalnate5a37791996-06-05 15:50:50 -0500452 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500453 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500454
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600455 else
456 png_warning(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600457 "Ignoring extra png_read_update_info() call;"
458 " row buffer not reallocated");
Glenn Randers-Pehrson4cfdb3c2009-11-26 11:49:37 -0600459
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600460 png_read_transform_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500461}
462
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500463#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600464/* Initialize palette, background, etc, after transformations
465 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500466 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600467 * If the user doesn't call this, we will do it ourselves.
468 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500469void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600470png_start_read_image(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500471{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500472 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500473
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500474 if (png_ptr == NULL)
475 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500476
Guy Schalnate5a37791996-06-05 15:50:50 -0500477 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500478 png_read_start_row(png_ptr);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500479 else
480 png_warning(png_ptr,
481 "Ignoring extra png_start_read_image() call;"
482 " row buffer not reallocated");
Guy Schalnat0d580581995-07-20 02:43:20 -0500483}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500484#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500485
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500486#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500487void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600488png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500489{
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500490 PNG_IDAT;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500491 PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600492 0xff};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500493 PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
Guy Schalnat0d580581995-07-20 02:43:20 -0500494 int ret;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500495
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500496 if (png_ptr == NULL)
497 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500498
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500499 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -0500500 (unsigned long)png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500501
Guy Schalnate5a37791996-06-05 15:50:50 -0500502 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500503 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500504
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500505 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
506 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500507 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500508#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
509 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500510 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500511#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500512
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500513#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
514 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500515 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500516#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500517
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600518#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
519 !defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500520 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500521 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500522#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500523
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500524#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
525 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500526 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500527#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500528
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500529#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
530 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500531 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500532#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500533
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500534#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
535 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500536 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500537#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500538
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500539#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
540 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500541 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500542#endif
543 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500544
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500545#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500546 /* If interlaced and we do not need a new row, combine row and return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500547 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
548 {
549 switch (png_ptr->pass)
550 {
551 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600552 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500553 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500554 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500555 png_combine_row(png_ptr, dsp_row,
556 png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600557 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500558 return;
559 }
560 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500561
Guy Schalnat0d580581995-07-20 02:43:20 -0500562 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600563 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500564 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500565 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500566 png_combine_row(png_ptr, dsp_row,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500567 png_pass_dsp_mask[png_ptr->pass]);
568
Guy Schalnat0d580581995-07-20 02:43:20 -0500569 png_read_finish_row(png_ptr);
570 return;
571 }
572 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500573
Guy Schalnat0d580581995-07-20 02:43:20 -0500574 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600575 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500576 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500577 if (dsp_row != NULL && (png_ptr->row_number & 4))
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600578 png_combine_row(png_ptr, dsp_row,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500579 png_pass_dsp_mask[png_ptr->pass]);
580
Guy Schalnat0d580581995-07-20 02:43:20 -0500581 png_read_finish_row(png_ptr);
582 return;
583 }
584 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500585
Guy Schalnat0d580581995-07-20 02:43:20 -0500586 case 3:
587 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
588 {
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 4:
599 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600600 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500601 if (dsp_row != NULL && (png_ptr->row_number & 2))
Guy Schalnat0d580581995-07-20 02:43:20 -0500602 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;
609 case 5:
610 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
611 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500612 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500613 png_combine_row(png_ptr, dsp_row,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500614 png_pass_dsp_mask[png_ptr->pass]);
615
Guy Schalnat0d580581995-07-20 02:43:20 -0500616 png_read_finish_row(png_ptr);
617 return;
618 }
619 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500620
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600621 default:
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600622 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500623 if (!(png_ptr->row_number & 1))
624 {
625 png_read_finish_row(png_ptr);
626 return;
627 }
628 break;
629 }
630 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500631#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500632
Guy Schalnate5a37791996-06-05 15:50:50 -0500633 if (!(png_ptr->mode & PNG_HAVE_IDAT))
634 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500635
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600636 png_ptr->zstream.next_out = png_ptr->row_buf;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600637 png_ptr->zstream.avail_out =
638 (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
639 png_ptr->iwidth) + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500640
Guy Schalnat0d580581995-07-20 02:43:20 -0500641 do
642 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600643 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500644 {
645 while (!png_ptr->idat_size)
646 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600647 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500648
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500649 png_ptr->idat_size = png_read_chunk_header(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600650 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
651 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500652 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600653 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
654 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500655 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600656 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500657 png_crc_read(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600658 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600659 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500660 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500661
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600662 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500663
Guy Schalnat0d580581995-07-20 02:43:20 -0500664 if (ret == Z_STREAM_END)
665 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600666 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500667 png_ptr->idat_size)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500668 png_benign_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600669 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500670 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600671 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500672 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500673
Guy Schalnat0d580581995-07-20 02:43:20 -0500674 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600675 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600676 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600677
678 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500679
Guy Schalnat0d580581995-07-20 02:43:20 -0500680 png_ptr->row_info.color_type = png_ptr->color_type;
681 png_ptr->row_info.width = png_ptr->iwidth;
682 png_ptr->row_info.channels = png_ptr->channels;
683 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
684 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500685 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
686 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500687
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500688 if (png_ptr->row_buf[0])
Guy Schalnate5a37791996-06-05 15:50:50 -0500689 png_read_filter_row(png_ptr, &(png_ptr->row_info),
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600690 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
691 (int)(png_ptr->row_buf[0]));
Guy Schalnat0d580581995-07-20 02:43:20 -0500692
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500693 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600694
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500695#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500696 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600697 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600698 {
699 /* Intrapixel differencing */
700 png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
701 }
702#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500703
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500704
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -0600705 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
Guy Schalnat0d580581995-07-20 02:43:20 -0500706 png_do_read_transformations(png_ptr);
707
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500708#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500709 /* Blow up interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500710 if (png_ptr->interlaced &&
711 (png_ptr->transformations & PNG_INTERLACE))
712 {
713 if (png_ptr->pass < 6)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500714 /* Old interface (pre-1.0.9):
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500715 * png_do_read_interlace(&(png_ptr->row_info),
716 * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
717 */
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600718 png_do_read_interlace(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500719
Andreas Dilger47a0c421997-05-16 02:46:07 -0500720 if (dsp_row != NULL)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600721 png_combine_row(png_ptr, dsp_row, png_pass_dsp_mask[png_ptr->pass]);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500722
Andreas Dilger47a0c421997-05-16 02:46:07 -0500723 if (row != NULL)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600724 png_combine_row(png_ptr, row, png_pass_mask[png_ptr->pass]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500725 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500726
Guy Schalnat0d580581995-07-20 02:43:20 -0500727 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500728#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500729 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500730 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500731 png_combine_row(png_ptr, row, 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500732
Andreas Dilger47a0c421997-05-16 02:46:07 -0500733 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500734 png_combine_row(png_ptr, dsp_row, 0xff);
735 }
736 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600737
738 if (png_ptr->read_row_fn != NULL)
739 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500740}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500741#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500742
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500743#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500744/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600745 * and png_set_interlace_handling() has been called, the rows need to
746 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500747 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600748 * called, the rows contents must be initialized to the contents of the
749 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600750 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600751 * "row" holds the actual image, and pixels are placed in it
752 * as they arrive. If the image is displayed after each pass, it will
753 * appear to "sparkle" in. "display_row" can be used to display a
754 * "chunky" progressive image, with finer detail added as it becomes
755 * available. If you do not want this "chunky" display, you may pass
756 * NULL for display_row. If you do not want the sparkle display, and
757 * you have not called png_handle_alpha(), you may pass NULL for rows.
758 * If you have called png_handle_alpha(), and the image has either an
759 * alpha channel or a transparency chunk, you must provide a buffer for
760 * rows. In this case, you do not have to provide a display_row buffer
761 * also, but you may. If the image is not interlaced, or if you have
762 * not called png_set_interlace_handling(), the display_row buffer will
763 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500764 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600765 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600766 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600767
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500768void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600769png_read_rows(png_structp png_ptr, png_bytepp row,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600770 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500771{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600772 png_uint_32 i;
773 png_bytepp rp;
774 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500775
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500776 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500777
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500778 if (png_ptr == NULL)
779 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500780
Guy Schalnat0f716451995-11-28 11:22:13 -0600781 rp = row;
782 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500783 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500784 for (i = 0; i < num_rows; i++)
785 {
786 png_bytep rptr = *rp++;
787 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600788
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500789 png_read_row(png_ptr, rptr, dptr);
790 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500791
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500792 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500793 for (i = 0; i < num_rows; i++)
794 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500795 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500796 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500797 rp++;
798 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500799
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500800 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500801 for (i = 0; i < num_rows; i++)
802 {
803 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500804 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500805 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500806 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500807}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500808#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500809
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500810#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500811/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500812 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600813 * initialize the image to the current image that PNG will be overlaying.
814 * We set the num_rows again here, in case it was incorrectly set in
815 * png_read_start_row() by a call to png_read_update_info() or
816 * png_start_read_image() if png_set_interlace_handling() wasn't called
817 * prior to either of these functions like it should have been. You can
818 * only call this function once. If you desire to have an image for
819 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500820 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600821 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600822 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500823void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600824png_read_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500825{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500826 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500827 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600828 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500829
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500830 png_debug(1, "in png_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500831
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500832 if (png_ptr == NULL)
833 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500834
835#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500836 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
837 {
838 pass = png_set_interlace_handling(png_ptr);
839 /* And make sure transforms are initialized. */
840 png_start_read_image(png_ptr);
841 }
842 else
843 {
Glenn Randers-Pehrsone15a96b2011-01-14 15:47:37 -0600844 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500845 {
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500846 /* Caller called png_start_read_image or png_read_update_info without
847 * first turning on the PNG_INTERLACE transform. We can fix this here,
848 * but the caller should do it!
849 */
850 png_warning(png_ptr, "Interlace handling should be turned on when "
851 "using png_read_image");
852 /* Make sure this is set correctly */
853 png_ptr->num_rows = png_ptr->height;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500854 }
855
856 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
857 * the above error case.
858 */
859 pass = png_set_interlace_handling(png_ptr);
860 }
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500861#else
862 if (png_ptr->interlaced)
863 png_error(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600864 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500865
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500866 pass = 1;
867#endif
868
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500869 image_height=png_ptr->height;
Guy Schalnate5a37791996-06-05 15:50:50 -0500870
Guy Schalnat0d580581995-07-20 02:43:20 -0500871 for (j = 0; j < pass; j++)
872 {
873 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500874 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500875 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500876 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500877 rp++;
878 }
879 }
880}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500881#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500882
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500883#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500884/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600885 * file, will verify the end is accurate, and will read any comments
886 * or time information at the end of the file, if info is not NULL.
887 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500888void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600889png_read_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500890{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500891 png_debug(1, "in png_read_end");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500892
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500893 if (png_ptr == NULL)
894 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500895
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500897
898 do
899 {
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500900 PNG_IHDR;
901 PNG_IDAT;
902 PNG_IEND;
903 PNG_PLTE;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500904#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500905 PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600906#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500907#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500908 PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600909#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500910#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500911 PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600912#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500913#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500914 PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600915#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500916#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500917 PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600918#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500919#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500920 PNG_iTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600921#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500922#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500923 PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600924#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500925#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500926 PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600927#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500928#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500929 PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600930#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500931#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500932 PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600933#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500934#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500935 PNG_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600936#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500937#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500938 PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600939#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500940#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500941 PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600942#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500943#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500944 PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600945#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500946#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500947 PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600948#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500949#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500950 PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600951#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500952#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500953 PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600954#endif
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500955 png_uint_32 length = png_read_chunk_header(png_ptr);
956 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500957
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500958 if (!png_memcmp(chunk_name, png_IHDR, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600959 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500960
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500961 else if (!png_memcmp(chunk_name, png_IEND, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600962 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500963
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600964#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500965 else if (png_handle_as_unknown(png_ptr, chunk_name))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600966 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500967 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600968 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500969 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500970 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600971 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600972 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500973 if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600974 png_ptr->mode |= PNG_HAVE_PLTE;
975 }
976#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500977
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500978 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Guy Schalnat0d580581995-07-20 02:43:20 -0500979 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500980 /* Zero length IDATs are legal after the last IDAT has been
981 * read, but not after other chunks have been read.
982 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500983 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500984 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500985
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500986 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500987 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500988 else if (!png_memcmp(chunk_name, png_PLTE, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500989 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500990
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500991#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500992 else if (!png_memcmp(chunk_name, png_bKGD, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500993 png_handle_bKGD(png_ptr, info_ptr, length);
994#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500995
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500996#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500997 else if (!png_memcmp(chunk_name, png_cHRM, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500998 png_handle_cHRM(png_ptr, info_ptr, length);
999#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001000
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001001#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001002 else if (!png_memcmp(chunk_name, png_gAMA, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001003 png_handle_gAMA(png_ptr, info_ptr, length);
1004#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001005
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001006#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001007 else if (!png_memcmp(chunk_name, png_hIST, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001008 png_handle_hIST(png_ptr, info_ptr, length);
1009#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001010
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001011#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001012 else if (!png_memcmp(chunk_name, png_oFFs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001013 png_handle_oFFs(png_ptr, info_ptr, length);
1014#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001015
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001016#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001017 else if (!png_memcmp(chunk_name, png_pCAL, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001018 png_handle_pCAL(png_ptr, info_ptr, length);
1019#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001020
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001021#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001022 else if (!png_memcmp(chunk_name, png_sCAL, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001023 png_handle_sCAL(png_ptr, info_ptr, length);
1024#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001025
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001026#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001027 else if (!png_memcmp(chunk_name, png_pHYs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001028 png_handle_pHYs(png_ptr, info_ptr, length);
1029#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001030
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001031#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001032 else if (!png_memcmp(chunk_name, png_sBIT, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001033 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001034#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001035
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001036#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001037 else if (!png_memcmp(chunk_name, png_sRGB, 4))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001038 png_handle_sRGB(png_ptr, info_ptr, length);
1039#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001040
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001041#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001042 else if (!png_memcmp(chunk_name, png_iCCP, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001043 png_handle_iCCP(png_ptr, info_ptr, length);
1044#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001045
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001046#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001047 else if (!png_memcmp(chunk_name, png_sPLT, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001048 png_handle_sPLT(png_ptr, info_ptr, length);
1049#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001050
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001051#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001052 else if (!png_memcmp(chunk_name, png_tEXt, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001053 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001054#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001055
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001056#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001057 else if (!png_memcmp(chunk_name, png_tIME, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001058 png_handle_tIME(png_ptr, info_ptr, length);
1059#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001060
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001061#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001062 else if (!png_memcmp(chunk_name, png_tRNS, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001063 png_handle_tRNS(png_ptr, info_ptr, length);
1064#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001065
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001066#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001067 else if (!png_memcmp(chunk_name, png_zTXt, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001068 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001069#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001070
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001071#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001072 else if (!png_memcmp(chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001073 png_handle_iTXt(png_ptr, info_ptr, length);
1074#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001075
Guy Schalnat0d580581995-07-20 02:43:20 -05001076 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001077 png_handle_unknown(png_ptr, info_ptr, length);
1078 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -05001079}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001080#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05001081
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001082/* Free all memory used by the read */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001083void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -05001084png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001085 png_infopp end_info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -05001086{
1087 png_structp png_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001088 png_infop info_ptr = NULL, end_info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001089#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001090 png_free_ptr free_fn = NULL;
1091 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001092#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001093
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001094 png_debug(1, "in png_destroy_read_struct");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05001095
Andreas Dilger47a0c421997-05-16 02:46:07 -05001096 if (png_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001097 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001098 if (png_ptr == NULL)
1099 return;
1100
1101#ifdef PNG_USER_MEM_SUPPORTED
1102 free_fn = png_ptr->free_fn;
1103 mem_ptr = png_ptr->mem_ptr;
1104#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001105
Andreas Dilger47a0c421997-05-16 02:46:07 -05001106 if (info_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001107 info_ptr = *info_ptr_ptr;
1108
Andreas Dilger47a0c421997-05-16 02:46:07 -05001109 if (end_info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001110 end_info_ptr = *end_info_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -05001111
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001112 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -05001113
Andreas Dilger47a0c421997-05-16 02:46:07 -05001114 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001115 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001116#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001117 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001118#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001119
1120#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001121 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1122 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001123#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001124 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001125#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001126 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001127 }
1128
Andreas Dilger47a0c421997-05-16 02:46:07 -05001129 if (end_info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001130 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001131#ifdef PNG_READ_TEXT_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001132 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001133#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001134#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001135 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001136 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001137#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001138 png_destroy_struct((png_voidp)end_info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001139#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001140 *end_info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001141 }
1142
Andreas Dilger47a0c421997-05-16 02:46:07 -05001143 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001144 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001145#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001146 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1147 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001148#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001149 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001150#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001151 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001152 }
1153}
1154
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001155/* Free all memory used by the read (old method) */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001156void /* PRIVATE */
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001157png_read_destroy(png_structp png_ptr, png_infop info_ptr,
1158 png_infop end_info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001159{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001160#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001161 jmp_buf tmp_jmp;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001162#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001163 png_error_ptr error_fn;
1164 png_error_ptr warning_fn;
1165 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001166#ifdef PNG_USER_MEM_SUPPORTED
1167 png_free_ptr free_fn;
1168#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001169
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001170 png_debug(1, "in png_read_destroy");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05001171
Andreas Dilger47a0c421997-05-16 02:46:07 -05001172 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001173 png_info_destroy(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001174
Andreas Dilger47a0c421997-05-16 02:46:07 -05001175 if (end_info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001176 png_info_destroy(png_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001177
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001178 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -05001179 png_free(png_ptr, png_ptr->big_row_buf);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001180 png_free(png_ptr, png_ptr->prev_row);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001181 png_free(png_ptr, png_ptr->chunkdata);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001182
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001183#ifdef PNG_READ_QUANTIZE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001184 png_free(png_ptr, png_ptr->palette_lookup);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001185 png_free(png_ptr, png_ptr->quantize_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001186#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001187
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001188#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001189 png_free(png_ptr, png_ptr->gamma_table);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001190#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001191
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001192#ifdef PNG_READ_BACKGROUND_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001193 png_free(png_ptr, png_ptr->gamma_from_1);
1194 png_free(png_ptr, png_ptr->gamma_to_1);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001195#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001196
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001197 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001198 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001199 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001200
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001201#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001202 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001203 if (png_ptr->free_me & PNG_FREE_TRNS)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001204 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001205 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001206#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001207
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001208#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001209 if (png_ptr->free_me & PNG_FREE_HIST)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001210 png_free(png_ptr, png_ptr->hist);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001211 png_ptr->free_me &= ~PNG_FREE_HIST;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001212#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001213
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001214#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001215 if (png_ptr->gamma_16_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001216 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001217 int i;
1218 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001219 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001220 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001221 png_free(png_ptr, png_ptr->gamma_16_table[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001222 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001223 png_free(png_ptr, png_ptr->gamma_16_table);
Guy Schalnat0d580581995-07-20 02:43:20 -05001224 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001225
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001226#ifdef PNG_READ_BACKGROUND_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001227 if (png_ptr->gamma_16_from_1 != NULL)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001228 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001229 int i;
1230 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001231 for (i = 0; i < istop; i++)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001232 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001233 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001234 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001235 png_free(png_ptr, png_ptr->gamma_16_from_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001236 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237 if (png_ptr->gamma_16_to_1 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001238 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001239 int i;
1240 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001241 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001242 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001243 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001244 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001245 png_free(png_ptr, png_ptr->gamma_16_to_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001246 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001247#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001248#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001249
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001250#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001251 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001252#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001253
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001254 inflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001255
Guy Schalnat6d764711995-12-19 03:22:19 -06001256#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001257 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001258#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001259
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001260#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1261#ifdef PNG_TEXT_SUPPORTED
1262 png_free(png_ptr, png_ptr->current_text);
1263#endif /* PNG_TEXT_SUPPORTED */
1264#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1265
Guy Schalnate5a37791996-06-05 15:50:50 -05001266 /* Save the important info out of the png_struct, in case it is
1267 * being used again.
1268 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001269#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -06001270 png_memcpy(tmp_jmp, png_ptr->png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001271#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001272
1273 error_fn = png_ptr->error_fn;
1274 warning_fn = png_ptr->warning_fn;
1275 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001276#ifdef PNG_USER_MEM_SUPPORTED
1277 free_fn = png_ptr->free_fn;
1278#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001279
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001280 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001281
1282 png_ptr->error_fn = error_fn;
1283 png_ptr->warning_fn = warning_fn;
1284 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001285#ifdef PNG_USER_MEM_SUPPORTED
1286 png_ptr->free_fn = free_fn;
1287#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001288
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001289#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -06001290 png_memcpy(png_ptr->png_jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001291#endif
1292
Guy Schalnat0d580581995-07-20 02:43:20 -05001293}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001294
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001295void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001296png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1297{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001298 if (png_ptr == NULL)
1299 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001300
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001301 png_ptr->read_row_fn = read_row_fn;
1302}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001303
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001304
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001305#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001306#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001307void PNGAPI
1308png_read_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001309 int transforms,
1310 voidp params)
1311{
1312 int row;
1313
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001314 if (png_ptr == NULL)
1315 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001316
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001317 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001318 * PNG file before the first IDAT (image data chunk).
1319 */
1320 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001321 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
1322 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001323
1324 /* -------------- image transformations start here ------------------- */
1325
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001326#ifdef PNG_READ_16_TO_8_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001327 /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001328 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001329 if (transforms & PNG_TRANSFORM_STRIP_16)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001330 png_set_strip_16(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001331#endif
1332
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001333#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001334 /* Strip alpha bytes from the input data without combining with
1335 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001336 */
1337 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001338 png_set_strip_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001339#endif
1340
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001341#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001342 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001343 * byte into separate bytes (useful for paletted and grayscale images).
1344 */
1345 if (transforms & PNG_TRANSFORM_PACKING)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001346 png_set_packing(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001347#endif
1348
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001349#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001350 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001351 * (not useful if you are using png_set_packing).
1352 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001353 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001354 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001355#endif
1356
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001357#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001358 /* Expand paletted colors into true RGB triplets
1359 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1360 * Expand paletted or RGB images with transparency to full alpha
1361 * channels so the data will be available as RGBA quartets.
1362 */
1363 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001364 if ((png_ptr->bit_depth < 8) ||
1365 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1366 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001367 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001368#endif
1369
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001370 /* We don't handle background color or gamma transformation or quantizing.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001371 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001372
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001373#ifdef PNG_READ_INVERT_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001374 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001375 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001376 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001377 png_set_invert_mono(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_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001381 /* If you want to shift the pixel values from the range [0,255] or
1382 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1383 * colors were originally in:
1384 */
1385 if ((transforms & PNG_TRANSFORM_SHIFT)
1386 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1387 {
1388 png_color_8p sig_bit;
1389
1390 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1391 png_set_shift(png_ptr, sig_bit);
1392 }
1393#endif
1394
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001395#ifdef PNG_READ_BGR_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001396 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001397 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001398 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001399#endif
1400
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001401#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001402 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001403 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001404 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001405#endif
1406
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001407#ifdef PNG_READ_SWAP_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001408 /* Swap bytes of 16 bit files to least significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001409 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001410 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001411#endif
1412
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001413/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001414#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001415 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001416 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001417 png_set_invert_alpha(png_ptr);
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001418#endif
1419
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001420/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001421#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001422 /* Expand grayscale image to RGB */
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001423 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001424 png_set_gray_to_rgb(png_ptr);
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001425#endif
1426
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001427 /* We don't handle adding filler bytes */
1428
1429 /* Optional call to gamma correct and add the background to the palette
1430 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001431 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001432 */
1433 png_read_update_info(png_ptr, info_ptr);
1434
1435 /* -------------- image transformations end here ------------------- */
1436
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001437 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001438 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001439 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001440 png_uint_32 iptr;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001441
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001442 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001443 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001444 for (iptr=0; iptr<info_ptr->height; iptr++)
1445 info_ptr->row_pointers[iptr] = NULL;
1446
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001447 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001448
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001449 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001450 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001451 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001452 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001453
1454 png_read_image(png_ptr, info_ptr->row_pointers);
1455 info_ptr->valid |= PNG_INFO_IDAT;
1456
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001457 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001458 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001459
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001460 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1461 PNG_UNUSED(params)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001462
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001463}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001464#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001465#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001466#endif /* PNG_READ_SUPPORTED */