blob: f6b956e75272edcb2804b4efcdf873800efaebc9 [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-Pehrson3cd7cff2010-04-16 19:27:08 -05004 * Last changed in libpng 1.5.0 [April 17, 2010]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
15 */
Guy Schalnat0d580581995-07-20 02:43:20 -050016
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050017#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060018
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060019#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050020
Andreas Dilger47a0c421997-05-16 02:46:07 -050021/* Create a PNG structure for reading, and allocate any memory needed. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050022png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -050023png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -060024 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -050025{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050026
27#ifdef PNG_USER_MEM_SUPPORTED
28 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050029 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050030}
31
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060032/* Alternate create PNG structure for reading, and allocate any memory
33 * needed.
34 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050035png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050036png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -060037 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
38 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050039{
40#endif /* PNG_USER_MEM_SUPPORTED */
41
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -060042#ifdef PNG_SETJMP_SUPPORTED
43 volatile
44#endif
45 png_structp png_ptr;
Glenn Randers-Pehrson62ca98e2009-12-20 15:14:57 -060046 volatile int png_cleanup_needed = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060047
48#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060049#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -060050 jmp_buf png_jmpbuf;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060051#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060052#endif
53
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050054 int i;
55
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050056 png_debug(1, "in png_create_read_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050057
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050058#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060059 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -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-Pehrsone3f3c4e2010-02-07 18:08:50 -060071# ifdef PNG_USER_CHUNK_CACHE_MAX
72 /* Added at libpng-1.2.43 and 1.4.0 */
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -050073 png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060074# endif
75# ifdef PNG_SET_USER_CHUNK_MALLOC_MAX
76 /* Added at libpng-1.2.43 and 1.4.1 */
77 png_ptr->user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX;
78# endif
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -050079#endif
80
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050081#ifdef PNG_SETJMP_SUPPORTED
82/* Applications that neglect to set up their own setjmp() and then
83 encounter a png_error() will longjmp here. Since the jmpbuf is
84 then meaningless we abort instead of returning. */
85#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -060086 if (setjmp(png_jmpbuf))
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050087#else
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -060088 if (setjmp(png_jmpbuf(png_ptr))) /* Sets longjmp to match setjmp */
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050089#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050090 PNG_ABORT();
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -060091#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -060092 png_memcpy(png_jmpbuf(png_ptr), png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -060093#endif
Glenn Randers-Pehrsona2567be2009-10-19 20:31:31 -050094#endif /* PNG_SETJMP_SUPPORTED */
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050095
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050096#ifdef PNG_USER_MEM_SUPPORTED
97 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060098#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050099
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600100 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnat6d764711995-12-19 03:22:19 -0600101
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500102 if (user_png_ver)
Guy Schalnate5a37791996-06-05 15:50:50 -0500103 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500104 i = 0;
105 do
106 {
107 if (user_png_ver[i] != png_libpng_ver[i])
108 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
109 } while (png_libpng_ver[i++]);
110 }
111 else
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600112 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
Glenn Randers-Pehrsond60c8862009-06-15 21:56:14 -0500113
Guy Schalnate5a37791996-06-05 15:50:50 -0500114
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500115 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
116 {
117 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
118 * we must recompile any applications that use any older library version.
119 * For versions after libpng 1.0, we will be compatible, so we need
120 * only check the first digit.
121 */
122 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
123 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
124 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
125 {
Glenn Randers-Pehrson6a9e4802010-02-19 09:47:43 -0600126#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500127 char msg[80];
128 if (user_png_ver)
129 {
Glenn Randers-Pehrson92a3ef42010-03-31 21:50:21 -0500130 png_snprintf2(msg, 80,
131 "Application built with libpng-%.20s"
132 " but running with %.20s",
133 user_png_ver,
134 png_libpng_ver);
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600135 png_warning(png_ptr, msg);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500136 }
Glenn Randers-Pehrson92a3ef42010-03-31 21:50:21 -0500137#else
138 png_warning(png_ptr,
139 "Incompatible libpng version in application and library");
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500140#endif
141#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500142 png_ptr->flags = 0;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500143#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500144
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500145 png_cleanup_needed = 1;
146 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500147 }
148
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500149 if (!png_cleanup_needed)
150 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500151 /* Initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500152 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500153 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600154 png_ptr->zbuf_size);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500155 if (png_ptr->zbuf == NULL)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600156 png_cleanup_needed = 1;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500157 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600158 png_ptr->zstream.zalloc = png_zalloc;
159 png_ptr->zstream.zfree = png_zfree;
160 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500161
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500162 if (!png_cleanup_needed)
163 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500164 switch (inflateInit(&png_ptr->zstream))
165 {
166 case Z_OK: /* Do nothing */ break;
167 case Z_MEM_ERROR:
168 case Z_STREAM_ERROR: png_warning(png_ptr, "zlib memory error");
169 png_cleanup_needed = 1; break;
170 case Z_VERSION_ERROR: png_warning(png_ptr, "zlib version error");
171 png_cleanup_needed = 1; break;
172 default: png_warning(png_ptr, "Unknown zlib error");
173 png_cleanup_needed = 1;
174 }
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500175 }
176
177 if (png_cleanup_needed)
178 {
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500179 /* Clean up PNG structure and deallocate any memory. */
180 png_free(png_ptr, png_ptr->zbuf);
181 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500182#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500183 png_destroy_struct_2((png_voidp)png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600184 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500185#else
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500186 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500187#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500188 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500189 }
190
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600191 png_ptr->zstream.next_out = png_ptr->zbuf;
192 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500193
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500194 png_set_read_fn(png_ptr, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500195
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500196
Guy Schalnate5a37791996-06-05 15:50:50 -0500197 return (png_ptr);
198}
199
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500200
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500201#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600202/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -0600203 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600204 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500205 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600206 * via png_set_sig_bytes(), and we will only check the remaining bytes
207 * here. The application can then have access to the signature bytes we
208 * read if it is determined that this isn't a valid PNG file.
209 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500210void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600211png_read_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500212{
Glenn Randers-Pehrsonc81bb8a2009-08-15 22:02:26 -0500213 png_debug(1, "in png_read_info");
214
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500215 if (png_ptr == NULL || info_ptr == NULL)
216 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500217
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600218 /* If we haven't checked all of the PNG signature bytes, do so now. */
219 if (png_ptr->sig_bytes < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500220 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500221 png_size_t num_checked = png_ptr->sig_bytes,
222 num_to_check = 8 - num_checked;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600223
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500224#ifdef PNG_IO_STATE_SUPPORTED
225 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
226#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500227
Andreas Dilger47a0c421997-05-16 02:46:07 -0500228 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600229 png_ptr->sig_bytes = 8;
230
231 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
232 {
233 if (num_checked < 4 &&
234 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
235 png_error(png_ptr, "Not a PNG file");
236 else
237 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
238 }
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600239 if (num_checked < 3)
240 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500241 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500242
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500243 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500244 {
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500245 PNG_IHDR;
246 PNG_IDAT;
247 PNG_IEND;
248 PNG_PLTE;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500249#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500250 PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600251#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500252#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500253 PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600254#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500255#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500256 PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600257#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500258#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500259 PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600260#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500261#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500262 PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600263#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500264#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500265 PNG_iTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600266#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500267#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500268 PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600269#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500270#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500271 PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600272#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500273#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500274 PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600275#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500276#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500277 PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600278#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500279#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500280 PNG_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600281#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500282#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500283 PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600284#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500285#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500286 PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600287#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500288#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500289 PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600290#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500291#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500292 PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600293#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500294#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500295 PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600296#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500297#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500298 PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600299#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500300 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500301 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500302
303 /* This should be a binary subdivision search or a hash for
304 * matching the chunk name rather than a linear search.
305 */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500306 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600307 if (png_ptr->mode & PNG_AFTER_IDAT)
308 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500309
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500310 if (!png_memcmp(chunk_name, png_IHDR, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600311 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500312 else if (!png_memcmp(chunk_name, png_IEND, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600313 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600314#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500315 else if (png_handle_as_unknown(png_ptr, chunk_name))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600316 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500317 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600318 png_ptr->mode |= PNG_HAVE_IDAT;
319 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500320 if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600321 png_ptr->mode |= PNG_HAVE_PLTE;
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");
326 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600327 !(png_ptr->mode & PNG_HAVE_PLTE))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600328 png_error(png_ptr, "Missing PLTE before IDAT");
329 break;
330 }
331 }
332#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500333 else if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600334 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500335 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500336 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500337 if (!(png_ptr->mode & PNG_HAVE_IHDR))
338 png_error(png_ptr, "Missing IHDR before IDAT");
339 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600340 !(png_ptr->mode & PNG_HAVE_PLTE))
Guy Schalnate5a37791996-06-05 15:50:50 -0500341 png_error(png_ptr, "Missing PLTE before IDAT");
342
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500343 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500344 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500345 break;
346 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500347#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500348 else if (!png_memcmp(chunk_name, png_bKGD, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500349 png_handle_bKGD(png_ptr, info_ptr, length);
350#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500351#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500352 else if (!png_memcmp(chunk_name, png_cHRM, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500353 png_handle_cHRM(png_ptr, info_ptr, length);
354#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500355#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500356 else if (!png_memcmp(chunk_name, png_gAMA, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500357 png_handle_gAMA(png_ptr, info_ptr, length);
358#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500359#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500360 else if (!png_memcmp(chunk_name, png_hIST, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500361 png_handle_hIST(png_ptr, info_ptr, length);
362#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500363#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500364 else if (!png_memcmp(chunk_name, png_oFFs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500365 png_handle_oFFs(png_ptr, info_ptr, length);
366#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500367#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500368 else if (!png_memcmp(chunk_name, png_pCAL, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369 png_handle_pCAL(png_ptr, info_ptr, length);
370#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500371#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500372 else if (!png_memcmp(chunk_name, png_sCAL, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600373 png_handle_sCAL(png_ptr, info_ptr, length);
374#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500375#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500376 else if (!png_memcmp(chunk_name, png_pHYs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500377 png_handle_pHYs(png_ptr, info_ptr, length);
378#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500379#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500380 else if (!png_memcmp(chunk_name, png_sBIT, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500381 png_handle_sBIT(png_ptr, info_ptr, length);
382#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500383#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500384 else if (!png_memcmp(chunk_name, png_sRGB, 4))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600385 png_handle_sRGB(png_ptr, info_ptr, length);
386#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500387#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500388 else if (!png_memcmp(chunk_name, png_iCCP, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600389 png_handle_iCCP(png_ptr, info_ptr, length);
390#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500391#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500392 else if (!png_memcmp(chunk_name, png_sPLT, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600393 png_handle_sPLT(png_ptr, info_ptr, length);
394#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500395#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500396 else if (!png_memcmp(chunk_name, png_tEXt, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 png_handle_tEXt(png_ptr, info_ptr, length);
398#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500399#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500400 else if (!png_memcmp(chunk_name, png_tIME, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500401 png_handle_tIME(png_ptr, info_ptr, length);
402#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500403#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500404 else if (!png_memcmp(chunk_name, png_tRNS, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500405 png_handle_tRNS(png_ptr, info_ptr, length);
406#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500407#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500408 else if (!png_memcmp(chunk_name, png_zTXt, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500409 png_handle_zTXt(png_ptr, info_ptr, length);
410#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500411#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500412 else if (!png_memcmp(chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600413 png_handle_iTXt(png_ptr, info_ptr, length);
414#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500415 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600416 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500417 }
418}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500419#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500420
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500421/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500422void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600423png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500424{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500425 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500426
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500427 if (png_ptr == NULL)
428 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500429 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500430 png_read_start_row(png_ptr);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600431 else
432 png_warning(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600433 "Ignoring extra png_read_update_info() call;"
434 " row buffer not reallocated");
Glenn Randers-Pehrson4cfdb3c2009-11-26 11:49:37 -0600435
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600436 png_read_transform_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500437}
438
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500439#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600440/* Initialize palette, background, etc, after transformations
441 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500442 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600443 * If the user doesn't call this, we will do it ourselves.
444 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500445void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600446png_start_read_image(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500447{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500448 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500449
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500450 if (png_ptr == NULL)
451 return;
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);
Guy Schalnat0d580581995-07-20 02:43:20 -0500454}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500455#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500456
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500457#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500458void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600459png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500460{
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500461 PNG_IDAT;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500462 PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600463 0xff};
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500464 PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
Guy Schalnat0d580581995-07-20 02:43:20 -0500465 int ret;
Glenn Randers-Pehrsonc81bb8a2009-08-15 22:02:26 -0500466
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500467 if (png_ptr == NULL)
468 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500469
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500470 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600471 (unsigned long) png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500472
Guy Schalnate5a37791996-06-05 15:50:50 -0500473 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500474 png_read_start_row(png_ptr);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500475 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
476 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500477 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500478#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
479 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500480 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500481#endif
482#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
483 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500484 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500485#endif
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600486#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
487 !defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500488 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500489 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500490#endif
491#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
492 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500493 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500494#endif
495#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
496 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500497 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500498#endif
499#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
500 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500501 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500502#endif
503#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
504 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500505 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500506#endif
507 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500508
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500509#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500510 /* If interlaced and we do not need a new row, combine row and return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500511 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
512 {
513 switch (png_ptr->pass)
514 {
515 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600516 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500517 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500518 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500519 png_combine_row(png_ptr, dsp_row,
520 png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600521 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500522 return;
523 }
524 break;
525 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600526 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500527 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500528 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500529 png_combine_row(png_ptr, dsp_row,
530 png_pass_dsp_mask[png_ptr->pass]);
531 png_read_finish_row(png_ptr);
532 return;
533 }
534 break;
535 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600536 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500537 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500538 if (dsp_row != NULL && (png_ptr->row_number & 4))
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600539 png_combine_row(png_ptr, dsp_row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500540 png_pass_dsp_mask[png_ptr->pass]);
541 png_read_finish_row(png_ptr);
542 return;
543 }
544 break;
545 case 3:
546 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
547 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500548 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500549 png_combine_row(png_ptr, dsp_row,
550 png_pass_dsp_mask[png_ptr->pass]);
551 png_read_finish_row(png_ptr);
552 return;
553 }
554 break;
555 case 4:
556 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600557 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500558 if (dsp_row != NULL && (png_ptr->row_number & 2))
Guy Schalnat0d580581995-07-20 02:43:20 -0500559 png_combine_row(png_ptr, dsp_row,
560 png_pass_dsp_mask[png_ptr->pass]);
561 png_read_finish_row(png_ptr);
562 return;
563 }
564 break;
565 case 5:
566 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
567 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500568 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500569 png_combine_row(png_ptr, dsp_row,
570 png_pass_dsp_mask[png_ptr->pass]);
571 png_read_finish_row(png_ptr);
572 return;
573 }
574 break;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600575 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500576 if (!(png_ptr->row_number & 1))
577 {
578 png_read_finish_row(png_ptr);
579 return;
580 }
581 break;
582 }
583 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500584#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500585
Guy Schalnate5a37791996-06-05 15:50:50 -0500586 if (!(png_ptr->mode & PNG_HAVE_IDAT))
587 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500588
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600589 png_ptr->zstream.next_out = png_ptr->row_buf;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600590 png_ptr->zstream.avail_out =
591 (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
592 png_ptr->iwidth) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500593 do
594 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600595 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500596 {
597 while (!png_ptr->idat_size)
598 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600599 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500600
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500601 png_ptr->idat_size = png_read_chunk_header(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600602 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
603 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500604 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600605 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
606 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500607 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600608 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500609 png_crc_read(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600610 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600611 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500612 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600613 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500614 if (ret == Z_STREAM_END)
615 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600616 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500617 png_ptr->idat_size)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500618 png_benign_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600619 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500620 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600621 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500622 }
623 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600624 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600625 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600626
627 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500628
Guy Schalnat0d580581995-07-20 02:43:20 -0500629 png_ptr->row_info.color_type = png_ptr->color_type;
630 png_ptr->row_info.width = png_ptr->iwidth;
631 png_ptr->row_info.channels = png_ptr->channels;
632 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
633 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500634 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
635 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500636
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500637 if (png_ptr->row_buf[0])
Guy Schalnate5a37791996-06-05 15:50:50 -0500638 png_read_filter_row(png_ptr, &(png_ptr->row_info),
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600639 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
640 (int)(png_ptr->row_buf[0]));
Guy Schalnat0d580581995-07-20 02:43:20 -0500641
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500642 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600643
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500644#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500645 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600646 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600647 {
648 /* Intrapixel differencing */
649 png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
650 }
651#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500652
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500653
Glenn Randers-Pehrson73b029f2004-11-26 17:28:09 -0600654 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
Guy Schalnat0d580581995-07-20 02:43:20 -0500655 png_do_read_transformations(png_ptr);
656
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500657#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500658 /* Blow up interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500659 if (png_ptr->interlaced &&
660 (png_ptr->transformations & PNG_INTERLACE))
661 {
662 if (png_ptr->pass < 6)
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500663 /* Old interface (pre-1.0.9):
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500664 * png_do_read_interlace(&(png_ptr->row_info),
665 * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
666 */
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -0600667 png_do_read_interlace(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500668
Andreas Dilger47a0c421997-05-16 02:46:07 -0500669 if (dsp_row != NULL)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600670 png_combine_row(png_ptr, dsp_row, png_pass_dsp_mask[png_ptr->pass]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500671 if (row != NULL)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600672 png_combine_row(png_ptr, row, png_pass_mask[png_ptr->pass]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500673 }
674 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500675#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500676 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500677 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500678 png_combine_row(png_ptr, row, 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500679 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500680 png_combine_row(png_ptr, dsp_row, 0xff);
681 }
682 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600683
684 if (png_ptr->read_row_fn != NULL)
685 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500686}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500687#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500688
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500689#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500690/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600691 * and png_set_interlace_handling() has been called, the rows need to
692 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500693 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600694 * called, the rows contents must be initialized to the contents of the
695 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600696 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600697 * "row" holds the actual image, and pixels are placed in it
698 * as they arrive. If the image is displayed after each pass, it will
699 * appear to "sparkle" in. "display_row" can be used to display a
700 * "chunky" progressive image, with finer detail added as it becomes
701 * available. If you do not want this "chunky" display, you may pass
702 * NULL for display_row. If you do not want the sparkle display, and
703 * you have not called png_handle_alpha(), you may pass NULL for rows.
704 * If you have called png_handle_alpha(), and the image has either an
705 * alpha channel or a transparency chunk, you must provide a buffer for
706 * rows. In this case, you do not have to provide a display_row buffer
707 * also, but you may. If the image is not interlaced, or if you have
708 * not called png_set_interlace_handling(), the display_row buffer will
709 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500710 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600711 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600712 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600713
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500714void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600715png_read_rows(png_structp png_ptr, png_bytepp row,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600716 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500717{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600718 png_uint_32 i;
719 png_bytepp rp;
720 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500721
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500722 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500723
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500724 if (png_ptr == NULL)
725 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600726 rp = row;
727 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500728 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500729 for (i = 0; i < num_rows; i++)
730 {
731 png_bytep rptr = *rp++;
732 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600733
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500734 png_read_row(png_ptr, rptr, dptr);
735 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500736 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500737 for (i = 0; i < num_rows; i++)
738 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500739 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500740 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500741 rp++;
742 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500743 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500744 for (i = 0; i < num_rows; i++)
745 {
746 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500747 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500748 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500749 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500750}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500751#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500752
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500753#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500754/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500755 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600756 * initialize the image to the current image that PNG will be overlaying.
757 * We set the num_rows again here, in case it was incorrectly set in
758 * png_read_start_row() by a call to png_read_update_info() or
759 * png_start_read_image() if png_set_interlace_handling() wasn't called
760 * prior to either of these functions like it should have been. You can
761 * only call this function once. If you desire to have an image for
762 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500763 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600764 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600765 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500766void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600767png_read_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500768{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500769 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500770 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600771 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500772
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500773 png_debug(1, "in png_read_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500774
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500775 if (png_ptr == NULL)
776 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500777
778#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -0500779 pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500780#else
781 if (png_ptr->interlaced)
782 png_error(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600783 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500784 pass = 1;
785#endif
786
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500787
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500788 image_height=png_ptr->height;
789 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
Guy Schalnate5a37791996-06-05 15:50:50 -0500790
Guy Schalnat0d580581995-07-20 02:43:20 -0500791 for (j = 0; j < pass; j++)
792 {
793 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500794 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500795 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500796 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500797 rp++;
798 }
799 }
800}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500801#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500802
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500803#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500804/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600805 * file, will verify the end is accurate, and will read any comments
806 * or time information at the end of the file, if info is not NULL.
807 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500808void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600809png_read_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500810{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500811 png_debug(1, "in png_read_end");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500812
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500813 if (png_ptr == NULL)
814 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500815 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500816
817 do
818 {
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500819 PNG_IHDR;
820 PNG_IDAT;
821 PNG_IEND;
822 PNG_PLTE;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500823#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500824 PNG_bKGD;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600825#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500826#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500827 PNG_cHRM;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600828#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500829#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500830 PNG_gAMA;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600831#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500832#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500833 PNG_hIST;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600834#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500835#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500836 PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600837#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500838#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500839 PNG_iTXt;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600840#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500841#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500842 PNG_oFFs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600843#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500844#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500845 PNG_pCAL;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600846#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500847#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500848 PNG_pHYs;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600849#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500850#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500851 PNG_sBIT;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600852#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500853#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500854 PNG_sCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600855#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500856#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500857 PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600858#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500859#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500860 PNG_sRGB;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600861#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500862#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500863 PNG_tEXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600864#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500865#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500866 PNG_tIME;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600867#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500868#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500869 PNG_tRNS;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600870#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500871#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrson9bf60832009-09-20 13:37:50 -0500872 PNG_zTXt;
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600873#endif
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500874 png_uint_32 length = png_read_chunk_header(png_ptr);
875 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500876
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500877 if (!png_memcmp(chunk_name, png_IHDR, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600878 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500879 else if (!png_memcmp(chunk_name, png_IEND, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600880 png_handle_IEND(png_ptr, info_ptr, length);
881#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500882 else if (png_handle_as_unknown(png_ptr, chunk_name))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600883 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500884 if (!png_memcmp(chunk_name, png_IDAT, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600885 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500886 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500887 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600888 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600889 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500890 if (!png_memcmp(chunk_name, png_PLTE, 4))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600891 png_ptr->mode |= PNG_HAVE_PLTE;
892 }
893#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500894 else if (!png_memcmp(chunk_name, png_IDAT, 4))
Guy Schalnat0d580581995-07-20 02:43:20 -0500895 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896 /* Zero length IDATs are legal after the last IDAT has been
897 * read, but not after other chunks have been read.
898 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500899 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500900 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500901 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500902 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500903 else if (!png_memcmp(chunk_name, png_PLTE, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500904 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500905#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500906 else if (!png_memcmp(chunk_name, png_bKGD, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500907 png_handle_bKGD(png_ptr, info_ptr, length);
908#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500909#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500910 else if (!png_memcmp(chunk_name, png_cHRM, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500911 png_handle_cHRM(png_ptr, info_ptr, length);
912#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500913#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500914 else if (!png_memcmp(chunk_name, png_gAMA, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500915 png_handle_gAMA(png_ptr, info_ptr, length);
916#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500917#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500918 else if (!png_memcmp(chunk_name, png_hIST, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500919 png_handle_hIST(png_ptr, info_ptr, length);
920#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500921#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500922 else if (!png_memcmp(chunk_name, png_oFFs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500923 png_handle_oFFs(png_ptr, info_ptr, length);
924#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500925#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500926 else if (!png_memcmp(chunk_name, png_pCAL, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500927 png_handle_pCAL(png_ptr, info_ptr, length);
928#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500929#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500930 else if (!png_memcmp(chunk_name, png_sCAL, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600931 png_handle_sCAL(png_ptr, info_ptr, length);
932#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500933#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500934 else if (!png_memcmp(chunk_name, png_pHYs, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500935 png_handle_pHYs(png_ptr, info_ptr, length);
936#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500937#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500938 else if (!png_memcmp(chunk_name, png_sBIT, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500939 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500940#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500941#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500942 else if (!png_memcmp(chunk_name, png_sRGB, 4))
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600943 png_handle_sRGB(png_ptr, info_ptr, length);
944#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500945#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500946 else if (!png_memcmp(chunk_name, png_iCCP, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600947 png_handle_iCCP(png_ptr, info_ptr, length);
948#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500949#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500950 else if (!png_memcmp(chunk_name, png_sPLT, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600951 png_handle_sPLT(png_ptr, info_ptr, length);
952#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500953#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500954 else if (!png_memcmp(chunk_name, png_tEXt, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600955 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500956#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500957#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500958 else if (!png_memcmp(chunk_name, png_tIME, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500959 png_handle_tIME(png_ptr, info_ptr, length);
960#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500961#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500962 else if (!png_memcmp(chunk_name, png_tRNS, 4))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500963 png_handle_tRNS(png_ptr, info_ptr, length);
964#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500965#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500966 else if (!png_memcmp(chunk_name, png_zTXt, 4))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600967 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500968#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500969#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500970 else if (!png_memcmp(chunk_name, png_iTXt, 4))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600971 png_handle_iTXt(png_ptr, info_ptr, length);
972#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500973 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600974 png_handle_unknown(png_ptr, info_ptr, length);
975 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500976}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500977#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500978
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500979/* Free all memory used by the read */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500980void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500981png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600982 png_infopp end_info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500983{
984 png_structp png_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600985 png_infop info_ptr = NULL, end_info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500986#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500987 png_free_ptr free_fn = NULL;
988 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600989#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500990
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500991 png_debug(1, "in png_destroy_read_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500992
Andreas Dilger47a0c421997-05-16 02:46:07 -0500993 if (png_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500994 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500995 if (png_ptr == NULL)
996 return;
997
998#ifdef PNG_USER_MEM_SUPPORTED
999 free_fn = png_ptr->free_fn;
1000 mem_ptr = png_ptr->mem_ptr;
1001#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001002
Andreas Dilger47a0c421997-05-16 02:46:07 -05001003 if (info_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001004 info_ptr = *info_ptr_ptr;
1005
Andreas Dilger47a0c421997-05-16 02:46:07 -05001006 if (end_info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001007 end_info_ptr = *end_info_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -05001008
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001009 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -05001010
Andreas Dilger47a0c421997-05-16 02:46:07 -05001011 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001012 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001013#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001014 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001015#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001016
1017#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001018 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1019 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001020#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001021 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001022#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001023 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001024 }
1025
Andreas Dilger47a0c421997-05-16 02:46:07 -05001026 if (end_info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001027 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001028#ifdef PNG_READ_TEXT_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001029 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001030#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001031#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001032 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001033 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001034#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001035 png_destroy_struct((png_voidp)end_info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001036#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001037 *end_info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001038 }
1039
Andreas Dilger47a0c421997-05-16 02:46:07 -05001040 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001041 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001042#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001043 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1044 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001045#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001046 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001047#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001048 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001049 }
1050}
1051
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001052/* Free all memory used by the read (old method) */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001053void /* PRIVATE */
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001054png_read_destroy(png_structp png_ptr, png_infop info_ptr,
1055 png_infop end_info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001056{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001057#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001058 jmp_buf tmp_jmp;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001059#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001060 png_error_ptr error_fn;
1061 png_error_ptr warning_fn;
1062 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001063#ifdef PNG_USER_MEM_SUPPORTED
1064 png_free_ptr free_fn;
1065#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001066
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001067 png_debug(1, "in png_read_destroy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001068
Andreas Dilger47a0c421997-05-16 02:46:07 -05001069 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001070 png_info_destroy(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001071
Andreas Dilger47a0c421997-05-16 02:46:07 -05001072 if (end_info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001073 png_info_destroy(png_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001074
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001075 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -05001076 png_free(png_ptr, png_ptr->big_row_buf);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001077 png_free(png_ptr, png_ptr->prev_row);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001078 png_free(png_ptr, png_ptr->chunkdata);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001079#ifdef PNG_READ_QUANTIZE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001080 png_free(png_ptr, png_ptr->palette_lookup);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001081 png_free(png_ptr, png_ptr->quantize_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001082#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001083#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001084 png_free(png_ptr, png_ptr->gamma_table);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001085#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001086#ifdef PNG_READ_BACKGROUND_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001087 png_free(png_ptr, png_ptr->gamma_from_1);
1088 png_free(png_ptr, png_ptr->gamma_to_1);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001089#endif
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001090 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001091 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001092 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001093#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001094 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001095 if (png_ptr->free_me & PNG_FREE_TRNS)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001096 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001097 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001098#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001099#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001100 if (png_ptr->free_me & PNG_FREE_HIST)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001101 png_free(png_ptr, png_ptr->hist);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001102 png_ptr->free_me &= ~PNG_FREE_HIST;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001103#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001104#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001105 if (png_ptr->gamma_16_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001106 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001107 int i;
1108 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001109 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001110 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001111 png_free(png_ptr, png_ptr->gamma_16_table[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001112 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001113 png_free(png_ptr, png_ptr->gamma_16_table);
Guy Schalnat0d580581995-07-20 02:43:20 -05001114 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001115#ifdef PNG_READ_BACKGROUND_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001116 if (png_ptr->gamma_16_from_1 != NULL)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001117 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001118 int i;
1119 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001120 for (i = 0; i < istop; i++)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001121 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001122 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001123 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001124 png_free(png_ptr, png_ptr->gamma_16_from_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001125 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001126 if (png_ptr->gamma_16_to_1 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001127 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001128 int i;
1129 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001130 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001131 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001132 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001133 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001134 png_free(png_ptr, png_ptr->gamma_16_to_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001135 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001136#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001137#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001138#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001139 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001140#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001141
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001142 inflateEnd(&png_ptr->zstream);
Guy Schalnat6d764711995-12-19 03:22:19 -06001143#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001144 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001145#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001146
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001147#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1148#ifdef PNG_TEXT_SUPPORTED
1149 png_free(png_ptr, png_ptr->current_text);
1150#endif /* PNG_TEXT_SUPPORTED */
1151#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1152
Guy Schalnate5a37791996-06-05 15:50:50 -05001153 /* Save the important info out of the png_struct, in case it is
1154 * being used again.
1155 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001156#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -06001157 png_memcpy(tmp_jmp, png_ptr->png_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001158#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001159
1160 error_fn = png_ptr->error_fn;
1161 warning_fn = png_ptr->warning_fn;
1162 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001163#ifdef PNG_USER_MEM_SUPPORTED
1164 free_fn = png_ptr->free_fn;
1165#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001166
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001167 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001168
1169 png_ptr->error_fn = error_fn;
1170 png_ptr->warning_fn = warning_fn;
1171 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001172#ifdef PNG_USER_MEM_SUPPORTED
1173 png_ptr->free_fn = free_fn;
1174#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001175
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001176#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson95ca51b2010-02-19 14:09:09 -06001177 png_memcpy(png_ptr->png_jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001178#endif
1179
Guy Schalnat0d580581995-07-20 02:43:20 -05001180}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001181
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001182void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001183png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1184{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001185 if (png_ptr == NULL)
1186 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001187 png_ptr->read_row_fn = read_row_fn;
1188}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001189
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001190
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001191#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001192#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001193void PNGAPI
1194png_read_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001195 int transforms,
1196 voidp params)
1197{
1198 int row;
1199
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001200 if (png_ptr == NULL)
1201 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001202
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001203 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001204 * PNG file before the first IDAT (image data chunk).
1205 */
1206 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001207 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
1208 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001209
1210 /* -------------- image transformations start here ------------------- */
1211
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001212#ifdef PNG_READ_16_TO_8_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001213 /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001214 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001215 if (transforms & PNG_TRANSFORM_STRIP_16)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001216 png_set_strip_16(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001217#endif
1218
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001219#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001220 /* Strip alpha bytes from the input data without combining with
1221 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001222 */
1223 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001224 png_set_strip_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001225#endif
1226
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001227#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001228 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001229 * byte into separate bytes (useful for paletted and grayscale images).
1230 */
1231 if (transforms & PNG_TRANSFORM_PACKING)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001232 png_set_packing(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001233#endif
1234
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001235#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001236 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001237 * (not useful if you are using png_set_packing).
1238 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001239 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001240 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001241#endif
1242
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001243#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001244 /* Expand paletted colors into true RGB triplets
1245 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1246 * Expand paletted or RGB images with transparency to full alpha
1247 * channels so the data will be available as RGBA quartets.
1248 */
1249 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001250 if ((png_ptr->bit_depth < 8) ||
1251 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1252 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001253 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001254#endif
1255
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001256 /* We don't handle background color or gamma transformation or quantizing.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001257 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001258
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001259#ifdef PNG_READ_INVERT_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001260 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001261 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001262 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001263 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001264#endif
1265
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001266#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001267 /* If you want to shift the pixel values from the range [0,255] or
1268 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1269 * colors were originally in:
1270 */
1271 if ((transforms & PNG_TRANSFORM_SHIFT)
1272 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1273 {
1274 png_color_8p sig_bit;
1275
1276 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1277 png_set_shift(png_ptr, sig_bit);
1278 }
1279#endif
1280
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001281#ifdef PNG_READ_BGR_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001282 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001283 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001284 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001285#endif
1286
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001287#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001288 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001289 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001290 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001291#endif
1292
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001293#ifdef PNG_READ_SWAP_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001294 /* Swap bytes of 16 bit files to least significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001295 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001296 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001297#endif
1298
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001299/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001300#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001301 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001302 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001303 png_set_invert_alpha(png_ptr);
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001304#endif
1305
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001306/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001307#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001308 /* Expand grayscale image to RGB */
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001309 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001310 png_set_gray_to_rgb(png_ptr);
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001311#endif
1312
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001313 /* We don't handle adding filler bytes */
1314
1315 /* Optional call to gamma correct and add the background to the palette
1316 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001317 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001318 */
1319 png_read_update_info(png_ptr, info_ptr);
1320
1321 /* -------------- image transformations end here ------------------- */
1322
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001323 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001324 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001325 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001326 png_uint_32 iptr;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001327
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001328 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001329 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001330 for (iptr=0; iptr<info_ptr->height; iptr++)
1331 info_ptr->row_pointers[iptr] = NULL;
1332
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001333 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001334
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001335 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001336 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001337 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001338 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001339
1340 png_read_image(png_ptr, info_ptr->row_pointers);
1341 info_ptr->valid |= PNG_INFO_IDAT;
1342
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001343 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001344 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001345
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001346 transforms = transforms; /* Quiet compiler warnings */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001347 params = params;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001348
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001349}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001350#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001351#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001352#endif /* PNG_READ_SUPPORTED */