blob: d96b98ae06282a38223281694d8535a81ce293fb [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-Pehrson55e85c22011-11-04 13:34:48 -05004 * Last changed in libpng 1.5.7 [(PENDING RELEASE)]
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"
John Bowler7875d532011-11-07 22:33:49 -060018#if defined PNG_SIMPLIFIED_READ_SUPPORTED && defined PNG_STDIO_SUPPORTED
19# include <errno.h>
20#endif
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060021
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060022#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050023
Andreas Dilger47a0c421997-05-16 02:46:07 -050024/* Create a PNG structure for reading, and allocate any memory needed. */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050025PNG_FUNCTION(png_structp,PNGAPI
26png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
27 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
Guy Schalnat0d580581995-07-20 02:43:20 -050028{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050029
30#ifdef PNG_USER_MEM_SUPPORTED
31 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050032 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050033}
34
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060035/* Alternate create PNG structure for reading, and allocate any memory
36 * needed.
37 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050038PNG_FUNCTION(png_structp,PNGAPI
39png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -060040 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050041 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050042{
43#endif /* PNG_USER_MEM_SUPPORTED */
44
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -060045#ifdef PNG_SETJMP_SUPPORTED
46 volatile
47#endif
48 png_structp png_ptr;
Glenn Randers-Pehrson62ca98e2009-12-20 15:14:57 -060049 volatile int png_cleanup_needed = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060050
51#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060052#ifdef USE_FAR_KEYWORD
John Bowlere6dc85b2011-04-27 14:47:15 -050053 jmp_buf tmp_jmpbuf;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060054#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060055#endif
56
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050057 png_debug(1, "in png_create_read_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050058
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050059#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060060 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050061 malloc_fn, mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050062#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060063 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050064#endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -060065 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050066 return (NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050067
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -050068 /* Added at libpng-1.2.6 */
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060069#ifdef PNG_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -050070 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
71 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050072
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060073# ifdef PNG_USER_CHUNK_CACHE_MAX
74 /* Added at libpng-1.2.43 and 1.4.0 */
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -050075 png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060076# endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050077
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060078# ifdef PNG_SET_USER_CHUNK_MALLOC_MAX
79 /* Added at libpng-1.2.43 and 1.4.1 */
80 png_ptr->user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX;
81# endif
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -050082#endif
83
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050084#ifdef PNG_SETJMP_SUPPORTED
85/* Applications that neglect to set up their own setjmp() and then
86 encounter a png_error() will longjmp here. Since the jmpbuf is
87 then meaningless we abort instead of returning. */
88#ifdef USE_FAR_KEYWORD
John Bowlere6dc85b2011-04-27 14:47:15 -050089 if (setjmp(tmp_jmpbuf))
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050090#else
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -060091 if (setjmp(png_jmpbuf(png_ptr))) /* Sets longjmp to match setjmp */
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050092#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050093 PNG_ABORT();
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -060094#ifdef USE_FAR_KEYWORD
John Bowlere6dc85b2011-04-27 14:47:15 -050095 png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -060096#endif
Glenn Randers-Pehrsona2567be2009-10-19 20:31:31 -050097#endif /* PNG_SETJMP_SUPPORTED */
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -050098
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050099#ifdef PNG_USER_MEM_SUPPORTED
100 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600101#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500102
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600103 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnat6d764711995-12-19 03:22:19 -0600104
John Bowler88b77cc2011-05-05 06:49:55 -0500105 /* Call the general version checker (shared with read and write code): */
106 if (!png_user_version_check(png_ptr, user_png_ver))
107 png_cleanup_needed = 1;
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500108
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500109 if (!png_cleanup_needed)
110 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500111 /* Initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500112 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500113 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr, png_ptr->zbuf_size);
114
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500115 if (png_ptr->zbuf == NULL)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600116 png_cleanup_needed = 1;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500117 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500118
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600119 png_ptr->zstream.zalloc = png_zalloc;
120 png_ptr->zstream.zfree = png_zfree;
121 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500122
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500123 if (!png_cleanup_needed)
124 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500125 switch (inflateInit(&png_ptr->zstream))
126 {
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500127 case Z_OK:
128 break; /* Do nothing */
129
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500130 case Z_MEM_ERROR:
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500131 png_warning(png_ptr, "zlib memory error");
132 png_cleanup_needed = 1;
133 break;
134
135 case Z_STREAM_ERROR:
136 png_warning(png_ptr, "zlib stream error");
137 png_cleanup_needed = 1;
138 break;
139
140 case Z_VERSION_ERROR:
141 png_warning(png_ptr, "zlib version error");
142 png_cleanup_needed = 1;
143 break;
144
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500145 default: png_warning(png_ptr, "Unknown zlib error");
146 png_cleanup_needed = 1;
147 }
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500148 }
149
150 if (png_cleanup_needed)
151 {
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500152 /* Clean up PNG structure and deallocate any memory. */
153 png_free(png_ptr, png_ptr->zbuf);
154 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500155#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500156 png_destroy_struct_2((png_voidp)png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600157 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500158#else
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500159 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500160#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500161 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500162 }
163
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600164 png_ptr->zstream.next_out = png_ptr->zbuf;
165 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500166
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500167 png_set_read_fn(png_ptr, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500168
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500169
Guy Schalnate5a37791996-06-05 15:50:50 -0500170 return (png_ptr);
171}
172
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500173
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500174#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600175/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -0600176 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600177 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500178 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600179 * via png_set_sig_bytes(), and we will only check the remaining bytes
180 * here. The application can then have access to the signature bytes we
181 * read if it is determined that this isn't a valid PNG file.
182 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500183void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600184png_read_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500185{
Glenn Randers-Pehrsonc81bb8a2009-08-15 22:02:26 -0500186 png_debug(1, "in png_read_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500187
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500188 if (png_ptr == NULL || info_ptr == NULL)
189 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500190
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600191 /* Read and check the PNG file signature. */
192 png_read_sig(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500193
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500194 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500195 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500196 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500197 png_uint_32 chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500198
199 /* This should be a binary subdivision search or a hash for
200 * matching the chunk name rather than a linear search.
201 */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500202 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600203 if (png_ptr->mode & PNG_AFTER_IDAT)
204 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500205
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500206 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600207 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500208
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500209 else if (chunk_name == png_IEND)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600210 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500211
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600212#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500213 else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
214 PNG_HANDLE_CHUNK_AS_DEFAULT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600215 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500216 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600217 png_ptr->mode |= PNG_HAVE_IDAT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500218
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600219 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500220
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500221 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600222 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500223
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500224 else if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600225 {
226 if (!(png_ptr->mode & PNG_HAVE_IHDR))
227 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500228
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600229 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600230 !(png_ptr->mode & PNG_HAVE_PLTE))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600231 png_error(png_ptr, "Missing PLTE before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500232
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600233 break;
234 }
235 }
236#endif
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500237 else if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600238 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500239
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500240 else if (chunk_name == png_IDAT)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500241 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500242 if (!(png_ptr->mode & PNG_HAVE_IHDR))
243 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500244
Guy Schalnate5a37791996-06-05 15:50:50 -0500245 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600246 !(png_ptr->mode & PNG_HAVE_PLTE))
Guy Schalnate5a37791996-06-05 15:50:50 -0500247 png_error(png_ptr, "Missing PLTE before IDAT");
248
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500249 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500250 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500251 break;
252 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500253
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500254#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500255 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500256 png_handle_bKGD(png_ptr, info_ptr, length);
257#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500258
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500259#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500260 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500261 png_handle_cHRM(png_ptr, info_ptr, length);
262#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500263
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500264#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500265 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500266 png_handle_gAMA(png_ptr, info_ptr, length);
267#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500268
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500269#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500270 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500271 png_handle_hIST(png_ptr, info_ptr, length);
272#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500273
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500274#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500275 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500276 png_handle_oFFs(png_ptr, info_ptr, length);
277#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500278
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500279#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500280 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500281 png_handle_pCAL(png_ptr, info_ptr, length);
282#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500283
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500284#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500285 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600286 png_handle_sCAL(png_ptr, info_ptr, length);
287#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500288
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500289#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500290 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500291 png_handle_pHYs(png_ptr, info_ptr, length);
292#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500293
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500294#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500295 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500296 png_handle_sBIT(png_ptr, info_ptr, length);
297#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500298
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500299#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500300 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600301 png_handle_sRGB(png_ptr, info_ptr, length);
302#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500303
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500304#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500305 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600306 png_handle_iCCP(png_ptr, info_ptr, length);
307#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500308
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500309#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500310 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600311 png_handle_sPLT(png_ptr, info_ptr, length);
312#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500313
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500314#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500315 else if (chunk_name == png_tEXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500316 png_handle_tEXt(png_ptr, info_ptr, length);
317#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500318
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500319#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500320 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500321 png_handle_tIME(png_ptr, info_ptr, length);
322#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500323
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500324#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500325 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500326 png_handle_tRNS(png_ptr, info_ptr, length);
327#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500328
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500329#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500330 else if (chunk_name == png_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500331 png_handle_zTXt(png_ptr, info_ptr, length);
332#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500333
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500334#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500335 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600336 png_handle_iTXt(png_ptr, info_ptr, length);
337#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500338
Guy Schalnat0d580581995-07-20 02:43:20 -0500339 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600340 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500341 }
342}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500343#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500344
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500345/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500346void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600347png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500348{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500349 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500350
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500351 if (png_ptr == NULL)
352 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500353
Glenn Randers-Pehrson3dbfd302011-10-13 17:24:36 -0500354 png_read_start_row(png_ptr);
Glenn Randers-Pehrson4cfdb3c2009-11-26 11:49:37 -0600355
John Bowler4a12f4a2011-04-17 18:34:22 -0500356#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600357 png_read_transform_info(png_ptr, info_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -0500358#else
359 PNG_UNUSED(info_ptr)
360#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500361}
362
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500363#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600364/* Initialize palette, background, etc, after transformations
365 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500366 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600367 * If the user doesn't call this, we will do it ourselves.
368 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500369void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600370png_start_read_image(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500371{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500372 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500373
Glenn Randers-Pehrson3dbfd302011-10-13 17:24:36 -0500374 if (png_ptr != NULL)
375 png_read_start_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500376}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500377#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500378
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500379#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500380void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600381png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500382{
383 int ret;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500384
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500385 png_row_info row_info;
386
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500387 if (png_ptr == NULL)
388 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500389
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500390 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -0500391 (unsigned long)png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500392
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500393 /* png_read_start_row sets the information (in particular iwidth) for this
394 * interlace pass.
395 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500396 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500397 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500398
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500399 /* 1.5.6: row_info moved out of png_struct to a local here. */
400 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
401 row_info.color_type = png_ptr->color_type;
402 row_info.bit_depth = png_ptr->bit_depth;
403 row_info.channels = png_ptr->channels;
404 row_info.pixel_depth = png_ptr->pixel_depth;
405 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
406
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500407 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
408 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500409 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500410#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
411 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500412 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500413#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500414
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500415#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
416 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500417 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500418#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500419
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600420#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
421 !defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500422 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500423 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500424#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500425
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500426#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
427 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500428 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500429#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500430
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500431#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
432 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500433 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500434#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500435
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500436#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
437 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500438 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500439#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500440
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500441#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
442 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500443 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500444#endif
445 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500446
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500447#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500448 /* If interlaced and we do not need a new row, combine row and return.
449 * Notice that the pixels we have from previous rows have been transformed
450 * already; we can only combine like with like (transformed or
451 * untransformed) and, because of the libpng API for interlaced images, this
452 * means we must transform before de-interlacing.
453 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500454 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
455 {
456 switch (png_ptr->pass)
457 {
458 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600459 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500460 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500461 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500462 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600463 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500464 return;
465 }
466 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500467
Guy Schalnat0d580581995-07-20 02:43:20 -0500468 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600469 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500470 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500471 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500472 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500473
Guy Schalnat0d580581995-07-20 02:43:20 -0500474 png_read_finish_row(png_ptr);
475 return;
476 }
477 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500478
Guy Schalnat0d580581995-07-20 02:43:20 -0500479 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600480 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500481 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500482 if (dsp_row != NULL && (png_ptr->row_number & 4))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500483 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500484
Guy Schalnat0d580581995-07-20 02:43:20 -0500485 png_read_finish_row(png_ptr);
486 return;
487 }
488 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500489
Guy Schalnat0d580581995-07-20 02:43:20 -0500490 case 3:
491 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
492 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500493 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500494 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500495
Guy Schalnat0d580581995-07-20 02:43:20 -0500496 png_read_finish_row(png_ptr);
497 return;
498 }
499 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500500
Guy Schalnat0d580581995-07-20 02:43:20 -0500501 case 4:
502 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600503 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500504 if (dsp_row != NULL && (png_ptr->row_number & 2))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500505 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500506
Guy Schalnat0d580581995-07-20 02:43:20 -0500507 png_read_finish_row(png_ptr);
508 return;
509 }
510 break;
511 case 5:
512 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
513 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500514 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500515 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500516
Guy Schalnat0d580581995-07-20 02:43:20 -0500517 png_read_finish_row(png_ptr);
518 return;
519 }
520 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500521
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600522 default:
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600523 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500524 if (!(png_ptr->row_number & 1))
525 {
526 png_read_finish_row(png_ptr);
527 return;
528 }
529 break;
530 }
531 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500532#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500533
Guy Schalnate5a37791996-06-05 15:50:50 -0500534 if (!(png_ptr->mode & PNG_HAVE_IDAT))
535 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500536
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600537 png_ptr->zstream.next_out = png_ptr->row_buf;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600538 png_ptr->zstream.avail_out =
539 (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
540 png_ptr->iwidth) + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500541
Guy Schalnat0d580581995-07-20 02:43:20 -0500542 do
543 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600544 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500545 {
546 while (!png_ptr->idat_size)
547 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600548 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500549
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500550 png_ptr->idat_size = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500551 if (png_ptr->chunk_name != png_IDAT)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600552 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500553 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600554 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
555 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500556 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600557 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500558 png_crc_read(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600559 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600560 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500561 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500562
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600563 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500564
Guy Schalnat0d580581995-07-20 02:43:20 -0500565 if (ret == Z_STREAM_END)
566 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600567 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500568 png_ptr->idat_size)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500569 png_benign_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600570 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500571 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600572 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500573 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500574
Guy Schalnat0d580581995-07-20 02:43:20 -0500575 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600576 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600577 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600578
579 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500580
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500581 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
582 {
583 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
Mans Rullgardd3a94802011-11-03 00:47:55 -0500584 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500585 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
586 else
587 png_error(png_ptr, "bad adaptive filter value");
588 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500589
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500590 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
591 * 1.5.6, while the buffer really is this big in current versions of libpng
592 * it may not be in the future, so this was changed just to copy the
593 * interlaced count:
594 */
595 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600596
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500597#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500598 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600599 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600600 {
601 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500602 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600603 }
604#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500605
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500606
John Bowler4a12f4a2011-04-17 18:34:22 -0500607#ifdef PNG_READ_TRANSFORMS_SUPPORTED
John Bowler9b872f42011-02-12 09:00:16 -0600608 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500609 png_do_read_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500610#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500611
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500612 /* The transformed pixel depth should match the depth now in row_info. */
613 if (png_ptr->transformed_pixel_depth == 0)
614 {
615 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
616 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
617 png_error(png_ptr, "sequential row overflow");
618 }
619
620 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
621 png_error(png_ptr, "internal sequential row size calculation error");
622
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500623#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500624 /* Blow up interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500625 if (png_ptr->interlaced &&
626 (png_ptr->transformations & PNG_INTERLACE))
627 {
628 if (png_ptr->pass < 6)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500629 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
630 png_ptr->transformations);
Guy Schalnat0d580581995-07-20 02:43:20 -0500631
Andreas Dilger47a0c421997-05-16 02:46:07 -0500632 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500633 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500634
Andreas Dilger47a0c421997-05-16 02:46:07 -0500635 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500636 png_combine_row(png_ptr, row, 0/*row*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500637 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500638
Guy Schalnat0d580581995-07-20 02:43:20 -0500639 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500640#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500641 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500642 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500643 png_combine_row(png_ptr, row, -1/*ignored*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500644
Andreas Dilger47a0c421997-05-16 02:46:07 -0500645 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500646 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500647 }
648 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600649
650 if (png_ptr->read_row_fn != NULL)
651 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500652}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500653#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500654
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500655#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500656/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600657 * and png_set_interlace_handling() has been called, the rows need to
658 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500659 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600660 * called, the rows contents must be initialized to the contents of the
661 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600662 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600663 * "row" holds the actual image, and pixels are placed in it
664 * as they arrive. If the image is displayed after each pass, it will
665 * appear to "sparkle" in. "display_row" can be used to display a
666 * "chunky" progressive image, with finer detail added as it becomes
667 * available. If you do not want this "chunky" display, you may pass
668 * NULL for display_row. If you do not want the sparkle display, and
669 * you have not called png_handle_alpha(), you may pass NULL for rows.
670 * If you have called png_handle_alpha(), and the image has either an
671 * alpha channel or a transparency chunk, you must provide a buffer for
672 * rows. In this case, you do not have to provide a display_row buffer
673 * also, but you may. If the image is not interlaced, or if you have
674 * not called png_set_interlace_handling(), the display_row buffer will
675 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500676 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600677 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600678 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600679
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500680void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600681png_read_rows(png_structp png_ptr, png_bytepp row,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600682 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500683{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600684 png_uint_32 i;
685 png_bytepp rp;
686 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500687
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500688 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500689
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500690 if (png_ptr == NULL)
691 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500692
Guy Schalnat0f716451995-11-28 11:22:13 -0600693 rp = row;
694 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500695 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500696 for (i = 0; i < num_rows; i++)
697 {
698 png_bytep rptr = *rp++;
699 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600700
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500701 png_read_row(png_ptr, rptr, dptr);
702 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500703
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500704 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500705 for (i = 0; i < num_rows; i++)
706 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500707 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500708 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500709 rp++;
710 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500711
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500712 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500713 for (i = 0; i < num_rows; i++)
714 {
715 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500716 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500717 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500718 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500719}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500720#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500721
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500722#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500723/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500724 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600725 * initialize the image to the current image that PNG will be overlaying.
726 * We set the num_rows again here, in case it was incorrectly set in
727 * png_read_start_row() by a call to png_read_update_info() or
728 * png_start_read_image() if png_set_interlace_handling() wasn't called
729 * prior to either of these functions like it should have been. You can
730 * only call this function once. If you desire to have an image for
731 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500732 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600733 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600734 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500735void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600736png_read_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500737{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500738 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500739 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600740 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500741
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500742 png_debug(1, "in png_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500743
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500744 if (png_ptr == NULL)
745 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500746
747#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500748 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
749 {
750 pass = png_set_interlace_handling(png_ptr);
751 /* And make sure transforms are initialized. */
752 png_start_read_image(png_ptr);
753 }
754 else
755 {
Glenn Randers-Pehrsone15a96b2011-01-14 15:47:37 -0600756 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500757 {
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500758 /* Caller called png_start_read_image or png_read_update_info without
759 * first turning on the PNG_INTERLACE transform. We can fix this here,
760 * but the caller should do it!
761 */
762 png_warning(png_ptr, "Interlace handling should be turned on when "
763 "using png_read_image");
764 /* Make sure this is set correctly */
765 png_ptr->num_rows = png_ptr->height;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500766 }
767
768 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
769 * the above error case.
770 */
771 pass = png_set_interlace_handling(png_ptr);
772 }
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500773#else
774 if (png_ptr->interlaced)
775 png_error(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600776 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500777
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500778 pass = 1;
779#endif
780
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500781 image_height=png_ptr->height;
Guy Schalnate5a37791996-06-05 15:50:50 -0500782
Guy Schalnat0d580581995-07-20 02:43:20 -0500783 for (j = 0; j < pass; j++)
784 {
785 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500786 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500787 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500788 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500789 rp++;
790 }
791 }
792}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500793#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500794
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500795#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500796/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600797 * file, will verify the end is accurate, and will read any comments
798 * or time information at the end of the file, if info is not NULL.
799 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500800void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600801png_read_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500802{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500803 png_debug(1, "in png_read_end");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500804
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500805 if (png_ptr == NULL)
806 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500807
Andreas Dilger47a0c421997-05-16 02:46:07 -0500808 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500809
810 do
811 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500812 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500813 png_uint_32 chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500814
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500815 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600816 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500817
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500818 else if (chunk_name == png_IEND)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600819 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500820
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600821#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500822 else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
823 PNG_HANDLE_CHUNK_AS_DEFAULT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600824 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500825 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600826 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500827 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500828 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600829 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600830 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500831 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600832 png_ptr->mode |= PNG_HAVE_PLTE;
833 }
834#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500835
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500836 else if (chunk_name == png_IDAT)
Guy Schalnat0d580581995-07-20 02:43:20 -0500837 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500838 /* Zero length IDATs are legal after the last IDAT has been
839 * read, but not after other chunks have been read.
840 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500841 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500842 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500843
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500844 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500845 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500846 else if (chunk_name == png_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500847 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500848
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500849#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500850 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500851 png_handle_bKGD(png_ptr, info_ptr, length);
852#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500853
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500854#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500855 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500856 png_handle_cHRM(png_ptr, info_ptr, length);
857#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500858
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500859#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500860 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500861 png_handle_gAMA(png_ptr, info_ptr, length);
862#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500863
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500864#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500865 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500866 png_handle_hIST(png_ptr, info_ptr, length);
867#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500868
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500869#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500870 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500871 png_handle_oFFs(png_ptr, info_ptr, length);
872#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500873
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500874#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500875 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500876 png_handle_pCAL(png_ptr, info_ptr, length);
877#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500878
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500879#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500880 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600881 png_handle_sCAL(png_ptr, info_ptr, length);
882#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500883
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500884#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500885 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500886 png_handle_pHYs(png_ptr, info_ptr, length);
887#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500888
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500889#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500890 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500891 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500892#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500893
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500894#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500895 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600896 png_handle_sRGB(png_ptr, info_ptr, length);
897#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500898
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500899#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500900 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600901 png_handle_iCCP(png_ptr, info_ptr, length);
902#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500903
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500904#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500905 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600906 png_handle_sPLT(png_ptr, info_ptr, length);
907#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500908
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500909#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500910 else if (chunk_name == png_tEXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600911 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500912#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500913
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500914#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500915 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500916 png_handle_tIME(png_ptr, info_ptr, length);
917#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500918
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500919#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500920 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500921 png_handle_tRNS(png_ptr, info_ptr, length);
922#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500923
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500924#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500925 else if (chunk_name == png_zTXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600926 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500927#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500928
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500929#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500930 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600931 png_handle_iTXt(png_ptr, info_ptr, length);
932#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500933
Guy Schalnat0d580581995-07-20 02:43:20 -0500934 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600935 png_handle_unknown(png_ptr, info_ptr, length);
936 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500937}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500938#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500939
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500940/* Free all memory used by the read */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500941void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500942png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600943 png_infopp end_info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500944{
945 png_structp png_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600946 png_infop info_ptr = NULL, end_info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500947#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500948 png_free_ptr free_fn = NULL;
949 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600950#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500951
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500952 png_debug(1, "in png_destroy_read_struct");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500953
Andreas Dilger47a0c421997-05-16 02:46:07 -0500954 if (png_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500955 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500956 if (png_ptr == NULL)
957 return;
958
959#ifdef PNG_USER_MEM_SUPPORTED
960 free_fn = png_ptr->free_fn;
961 mem_ptr = png_ptr->mem_ptr;
962#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500963
Andreas Dilger47a0c421997-05-16 02:46:07 -0500964 if (info_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500965 info_ptr = *info_ptr_ptr;
966
Andreas Dilger47a0c421997-05-16 02:46:07 -0500967 if (end_info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600968 end_info_ptr = *end_info_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500969
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600970 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500971
Andreas Dilger47a0c421997-05-16 02:46:07 -0500972 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500973 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500974#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600975 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600976#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500977
978#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500979 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
980 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500981#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600982 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500983#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500984 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500985 }
986
Andreas Dilger47a0c421997-05-16 02:46:07 -0500987 if (end_info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500988 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500989#ifdef PNG_READ_TEXT_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600990 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600991#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500992#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500993 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600994 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500995#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600996 png_destroy_struct((png_voidp)end_info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500997#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500998 *end_info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500999 }
1000
Andreas Dilger47a0c421997-05-16 02:46:07 -05001001 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001002 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001003#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001004 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1005 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001006#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001007 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001008#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001009 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001010 }
1011}
1012
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001013/* Free all memory used by the read (old method) */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001014void /* PRIVATE */
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001015png_read_destroy(png_structp png_ptr, png_infop info_ptr,
1016 png_infop end_info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001017{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001018#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001019 jmp_buf tmp_jmp;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001020#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001021 png_error_ptr error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -05001022#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001023 png_error_ptr warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -05001024#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001025 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001026#ifdef PNG_USER_MEM_SUPPORTED
1027 png_free_ptr free_fn;
1028#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001029
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001030 png_debug(1, "in png_read_destroy");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05001031
Andreas Dilger47a0c421997-05-16 02:46:07 -05001032 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001033 png_info_destroy(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001034
Andreas Dilger47a0c421997-05-16 02:46:07 -05001035 if (end_info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001036 png_info_destroy(png_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001037
John Bowler07772cb2011-10-14 18:19:47 -05001038#ifdef PNG_READ_GAMMA_SUPPORTED
1039 png_destroy_gamma_table(png_ptr);
1040#endif
1041
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001042 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -05001043 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgard1c422762011-10-17 16:52:19 -05001044 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -05001045 png_free(png_ptr, png_ptr->chunkdata);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001046
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001047#ifdef PNG_READ_QUANTIZE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001048 png_free(png_ptr, png_ptr->palette_lookup);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001049 png_free(png_ptr, png_ptr->quantize_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001050#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001051
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001052 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001053 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001054 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001055
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001056#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001057 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001058 if (png_ptr->free_me & PNG_FREE_TRNS)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001059 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001060 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001061#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001062
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001063#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001064 if (png_ptr->free_me & PNG_FREE_HIST)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001065 png_free(png_ptr, png_ptr->hist);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001066 png_ptr->free_me &= ~PNG_FREE_HIST;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001067#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001068
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001069 inflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001070
Guy Schalnat6d764711995-12-19 03:22:19 -06001071#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001072 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001073#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001074
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001075#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1076#ifdef PNG_TEXT_SUPPORTED
1077 png_free(png_ptr, png_ptr->current_text);
1078#endif /* PNG_TEXT_SUPPORTED */
1079#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1080
Guy Schalnate5a37791996-06-05 15:50:50 -05001081 /* Save the important info out of the png_struct, in case it is
1082 * being used again.
1083 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001084#ifdef PNG_SETJMP_SUPPORTED
John Bowlere6dc85b2011-04-27 14:47:15 -05001085 png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001086#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001087
1088 error_fn = png_ptr->error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -05001089#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001090 warning_fn = png_ptr->warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -05001091#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001092 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001093#ifdef PNG_USER_MEM_SUPPORTED
1094 free_fn = png_ptr->free_fn;
1095#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001096
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001097 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001098
1099 png_ptr->error_fn = error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -05001100#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001101 png_ptr->warning_fn = warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -05001102#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001103 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001104#ifdef PNG_USER_MEM_SUPPORTED
1105 png_ptr->free_fn = free_fn;
1106#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001107
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001108#ifdef PNG_SETJMP_SUPPORTED
John Bowlere6dc85b2011-04-27 14:47:15 -05001109 png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001110#endif
1111
Guy Schalnat0d580581995-07-20 02:43:20 -05001112}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001113
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001114void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001115png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1116{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001117 if (png_ptr == NULL)
1118 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001119
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001120 png_ptr->read_row_fn = read_row_fn;
1121}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001122
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001123
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001124#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001125#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001126void PNGAPI
1127png_read_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001128 int transforms,
1129 voidp params)
1130{
1131 int row;
1132
John Bowler6a6d79f2011-02-12 08:56:40 -06001133 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001134 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001135
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001136 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001137 * PNG file before the first IDAT (image data chunk).
1138 */
1139 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001140 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
1141 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001142
1143 /* -------------- image transformations start here ------------------- */
1144
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001145#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -05001146 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001147 */
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -05001148 if (transforms & PNG_TRANSFORM_SCALE_16)
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001149 {
Glenn Randers-Pehrson6da2f2d2011-06-17 23:07:16 -05001150 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
1151 * did in earlier versions, while "scale_16" is now more accurate.
1152 */
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001153 png_set_scale_16(png_ptr);
1154 }
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001155#endif
1156
1157#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
John Bowler8d261262011-06-18 13:37:11 -05001158 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
1159 * latter by doing SCALE first. This is ok and allows apps not to check for
1160 * which is supported to get the right answer.
1161 */
1162 if (transforms & PNG_TRANSFORM_STRIP_16)
1163 png_set_strip_16(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001164#endif
1165
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001166#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001167 /* Strip alpha bytes from the input data without combining with
1168 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001169 */
1170 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001171 png_set_strip_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001172#endif
1173
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001174#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001175 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001176 * byte into separate bytes (useful for paletted and grayscale images).
1177 */
1178 if (transforms & PNG_TRANSFORM_PACKING)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001179 png_set_packing(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001180#endif
1181
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001182#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001183 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001184 * (not useful if you are using png_set_packing).
1185 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001186 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001187 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001188#endif
1189
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001190#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001191 /* Expand paletted colors into true RGB triplets
1192 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1193 * Expand paletted or RGB images with transparency to full alpha
1194 * channels so the data will be available as RGBA quartets.
1195 */
1196 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001197 if ((png_ptr->bit_depth < 8) ||
1198 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1199 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001200 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001201#endif
1202
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001203 /* We don't handle background color or gamma transformation or quantizing.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001204 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001205
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001206#ifdef PNG_READ_INVERT_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001207 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001208 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001209 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001210 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001211#endif
1212
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001213#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001214 /* If you want to shift the pixel values from the range [0,255] or
1215 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1216 * colors were originally in:
1217 */
1218 if ((transforms & PNG_TRANSFORM_SHIFT)
1219 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1220 {
1221 png_color_8p sig_bit;
1222
1223 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1224 png_set_shift(png_ptr, sig_bit);
1225 }
1226#endif
1227
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001228#ifdef PNG_READ_BGR_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001229 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001230 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001231 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001232#endif
1233
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001234#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001235 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001236 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001237 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001238#endif
1239
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001240#ifdef PNG_READ_SWAP_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -05001241 /* Swap bytes of 16-bit files to least significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001242 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001243 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001244#endif
1245
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001246/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001247#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001248 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001249 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001250 png_set_invert_alpha(png_ptr);
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001251#endif
1252
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001253/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001254#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001255 /* Expand grayscale image to RGB */
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001256 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001257 png_set_gray_to_rgb(png_ptr);
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001258#endif
1259
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001260/* Added at libpng-1.5.4 */
John Bowler96cec0e2011-05-08 22:48:12 -05001261#ifdef PNG_READ_EXPAND_16_SUPPORTED
1262 if (transforms & PNG_TRANSFORM_EXPAND_16)
1263 png_set_expand_16(png_ptr);
1264#endif
1265
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001266 /* We don't handle adding filler bytes */
1267
John Bowler6a6d79f2011-02-12 08:56:40 -06001268 /* We use png_read_image and rely on that for interlace handling, but we also
1269 * call png_read_update_info therefore must turn on interlace handling now:
1270 */
1271 (void)png_set_interlace_handling(png_ptr);
1272
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001273 /* Optional call to gamma correct and add the background to the palette
1274 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001275 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001276 */
1277 png_read_update_info(png_ptr, info_ptr);
1278
1279 /* -------------- image transformations end here ------------------- */
1280
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001281 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001282 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001283 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001284 png_uint_32 iptr;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001285
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001286 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001287 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001288 for (iptr=0; iptr<info_ptr->height; iptr++)
1289 info_ptr->row_pointers[iptr] = NULL;
1290
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001291 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001292
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001293 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001294 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001295 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001296 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001297
1298 png_read_image(png_ptr, info_ptr->row_pointers);
1299 info_ptr->valid |= PNG_INFO_IDAT;
1300
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001301 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001302 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001303
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001304 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1305 PNG_UNUSED(params)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001306
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001307}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001308#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001309#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
John Bowler7875d532011-11-07 22:33:49 -06001310
1311#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1312/* SIMPLIFIED READ
1313 *
1314 * This code currently relies on the sequential reader, though it could easily
1315 * be made to work with the progressive one.
1316 */
1317/* Do all the *safe* initialization - 'safe' means that png_error won't be
1318 * called, so setting up the jmp_buf is not required.
1319 */
1320static int
1321png_image_read_init(png_imagep image)
1322{
1323 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
1324 png_safe_error, png_safe_warning);
1325
1326 if (png_ptr != NULL)
1327 {
1328 png_infop info_ptr = png_create_info_struct(png_ptr);
1329
1330 if (info_ptr != NULL)
1331 {
1332 png_controlp control = png_malloc_warn(png_ptr, sizeof *control);
1333
1334 if (control != NULL)
1335 {
1336 memset(control, 0, sizeof *control);
1337
1338 control->png_ptr = png_ptr;
1339 control->info_ptr = info_ptr;
1340 control->for_write = 0;
1341
1342 image->opaque = control;
1343 return 1;
1344 }
1345
1346 /* Error clean up */
1347 png_destroy_info_struct(png_ptr, &info_ptr);
1348 }
1349
1350 png_destroy_read_struct(&png_ptr, NULL, NULL);
1351 }
1352
1353 return png_image_error(image, "png_image_read: out of memory");
1354}
1355
1356/* Utility to find the base format of a PNG file from a png_struct. */
1357static png_uint_32
1358png_image_format(png_structp png_ptr, png_infop info_ptr)
1359{
1360 png_uint_32 format = 0;
1361
1362 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1363 format |= PNG_FORMAT_FLAG_COLOR;
1364
1365 if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1366 format |= PNG_FORMAT_FLAG_ALPHA;
1367
1368 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
1369 format |= PNG_FORMAT_FLAG_ALPHA;
1370
1371 if (png_ptr->bit_depth == 16)
1372 format |= PNG_FORMAT_FLAG_LINEAR;
1373
1374 return format;
1375}
1376
1377/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1378 * header and fill in all the information. This is executed in a safe context,
1379 * unlike the init routine above.
1380 */
1381static int
1382png_image_read_header(png_voidp argument)
1383{
1384 png_imagep image = argument;
1385 png_structp png_ptr = image->opaque->png_ptr;
1386 png_infop info_ptr = image->opaque->info_ptr;
1387
1388 png_read_info(png_ptr, info_ptr);
1389
1390 /* Do this the fast way; just read directly out of png_struct. */
1391 image->width = png_ptr->width;
1392 image->height = png_ptr->height;
1393
1394 {
1395 png_uint_32 format = png_image_format(png_ptr, info_ptr);
1396
1397 image->format = format;
1398 image->flags = 0;
1399
1400 /* Now try to work out whether the color data does not match sRGB. */
1401 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 &&
1402 (info_ptr->valid & PNG_INFO_sRGB) == 0)
1403 {
1404 /* gamma is irrelevant because libpng does gamma correction, what
1405 * matters is if the cHRM chunk doesn't match or, in the absence of
1406 * cRHM, if the iCCP profile looks to have different end points.
1407 */
1408 if (info_ptr->valid & PNG_INFO_cHRM)
1409 {
1410 /* TODO: this is a copy'n'paste from pngrutil.c, make a common
1411 * checking function. This checks for a 1% error.
1412 */
1413 /* The cHRM chunk is used in preference to iCCP */
1414 if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) ||
1415 PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) ||
1416 PNG_OUT_OF_RANGE(info_ptr->x_red, 64000, 1000) ||
1417 PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) ||
1418 PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) ||
1419 PNG_OUT_OF_RANGE(info_ptr->y_green, 60000, 1000) ||
1420 PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) ||
1421 PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000))
1422 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1423 }
1424
1425 else if (info_ptr->valid & PNG_INFO_iCCP)
1426 {
1427# if 0 /* TODO: IMPLEMENT THIS! */
1428 /* Here if we just have an iCCP chunk. */
1429 if (!png_iCCP_is_sRGB(png_ptr, info_ptr))
1430# endif
1431 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1432 }
1433 }
1434 }
1435
1436 return 1;
1437}
1438
1439#ifdef PNG_STDIO_SUPPORTED
1440int PNGAPI
1441png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1442{
1443 if (image != NULL)
1444 {
1445 if (file != NULL)
1446 {
1447 if (png_image_read_init(image))
1448 {
1449 /* This is slightly evil, but png_init_io doesn't do anything other
1450 * than this and we haven't changed the standard IO functions so
1451 * this saves a 'safe' function.
1452 */
1453 image->opaque->png_ptr->io_ptr = file;
1454 return png_safe_execute(image, png_image_read_header, image);
1455 }
1456 }
1457
1458 else
1459 return png_image_error(image,
1460 "png_image_begin_read_from_stdio: invalid argument");
1461 }
1462
1463 return 0;
1464}
1465
1466int PNGAPI
1467png_image_begin_read_from_file(png_imagep image, const char *file_name)
1468{
1469 if (image != NULL)
1470 {
1471 if (file_name != NULL)
1472 {
1473 FILE *fp = fopen(file_name, "rb");
1474
1475 if (fp != NULL)
1476 {
1477 if (png_image_read_init(image))
1478 {
1479 image->opaque->png_ptr->io_ptr = fp;
1480 image->opaque->owned_file = 1;
1481 return png_safe_execute(image, png_image_read_header, image);
1482 }
1483
1484 /* Clean up: just the opened file. */
1485 (void)fclose(fp);
1486 }
1487
1488 else
1489 return png_image_error(image, strerror(errno));
1490 }
1491
1492 else
1493 return png_image_error(image,
1494 "png_image_begin_read_from_file: invalid argument");
1495 }
1496
1497 return 0;
1498}
1499#endif /* PNG_STDIO_SUPPORTED */
1500
1501static void PNGCBAPI
1502png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
1503{
1504 if (png_ptr != NULL)
1505 {
1506 png_imagep image = png_ptr->io_ptr;
1507 if (image != NULL)
1508 {
1509 png_controlp cp = image->opaque;
1510 if (cp != NULL)
1511 {
1512 png_const_bytep memory = cp->memory;
1513 png_size_t size = cp->size;
1514
1515 if (memory != NULL && size >= need)
1516 {
1517 memcpy(out, memory, need);
1518 cp->memory = memory + need;
1519 cp->size = size - need;
1520 return;
1521 }
1522
1523 png_error(png_ptr, "read beyond end of data");
1524 }
1525 }
1526
1527 png_error(png_ptr, "invalid memory read");
1528 }
1529}
1530
1531int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1532 png_const_voidp memory, png_size_t size)
1533{
1534 if (image != NULL)
1535 {
1536 if (memory != NULL && size > 0)
1537 {
1538 if (png_image_read_init(image))
1539 {
1540 /* Now set the IO functions to read from the memory buffer and
1541 * store it into io_ptr. Again do this in-place to avoid calling a
1542 * libpng function that requires error handling.
1543 */
1544 image->opaque->memory = memory;
1545 image->opaque->size = size;
1546 image->opaque->png_ptr->io_ptr = image;
1547 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1548
1549 return png_safe_execute(image, png_image_read_header, image);
1550 }
1551 }
1552
1553 else
1554 return png_image_error(image,
1555 "png_image_begin_read_from_memory: invalid argument");
1556 }
1557
1558 return 0;
1559}
1560
1561/* Arguments to png_image_finish_read: */
1562typedef struct
1563{
1564 /* Arguments: */
1565 png_imagep image;
1566 png_voidp buffer;
1567 png_int_32 row_stride;
1568 png_colorp background;
1569 /* Local variables: */
1570 png_bytep local_row;
1571 png_bytep first_row;
1572 ptrdiff_t row_bytes; /* unsigned arithmetic step between rows */
1573} png_image_read_control;
1574
1575/* Just the row reading part of png_image_read. */
1576static int
1577png_image_read_composite(png_voidp argument)
1578{
1579 png_image_read_control *display = argument;
1580 png_imagep image = display->image;
1581 png_structp png_ptr = image->opaque->png_ptr;
1582 png_byte interlace_type = png_ptr->interlaced;
1583 int passes;
1584
1585 switch (interlace_type)
1586 {
1587 case PNG_INTERLACE_NONE:
1588 passes = 1;
1589 break;
1590
1591 case PNG_INTERLACE_ADAM7:
1592 passes = PNG_INTERLACE_ADAM7_PASSES;
1593 break;
1594
1595 default:
1596 png_error(png_ptr, "unknown interlace type");
1597 }
1598
1599 {
1600 png_uint_32 height = image->height;
1601 png_uint_32 width = image->width;
1602 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
1603 int pass;
1604
1605 for (pass = 0; pass < passes; ++pass)
1606 {
1607 png_bytep row = display->first_row;
1608 unsigned int startx, stepx, stepy;
1609 png_uint_32 y;
1610
1611 if (interlace_type == PNG_INTERLACE_ADAM7)
1612 {
1613 /* The row may be empty for a short image: */
1614 if (PNG_PASS_COLS(width, pass) == 0)
1615 continue;
1616
1617 startx = PNG_PASS_START_COL(pass);
1618 stepx = PNG_PASS_COL_OFFSET(pass);
1619 y = PNG_PASS_START_ROW(pass);
1620 stepy = PNG_PASS_COL_OFFSET(pass);
1621 }
1622
1623 else
1624 {
1625 y = 0;
1626 startx = 0;
1627 stepx = stepy = 1;
1628 }
1629
1630 for (; y<height; y += stepy)
1631 if (interlace_type == PNG_INTERLACE_NONE ||
1632 PNG_ROW_IN_INTERLACE_PASS(y, pass))
1633 {
1634 png_bytep inrow = display->local_row;
1635 png_bytep outrow = row;
1636 png_uint_32 x;
1637
1638 /* Read the row, which is packed: */
1639 png_read_row(png_ptr, inrow, NULL);
1640
1641 /* Now do the composition on each pixel in this row. */
1642 for (x = startx; x<width; x += stepx, outrow += stepx*channels)
1643 {
1644 png_byte alpha = inrow[channels];
1645
1646 if (alpha > 0) /* else no change to the output */
1647 {
1648 unsigned int c;
1649
1650 for (c=0; c<channels; ++c)
1651 {
1652 png_uint_32 component = inrow[c];
1653
1654 if (alpha < 255) /* else just use component */
1655 {
1656 /* This is PNG_OPTIMIZED_ALPHA, the component value
1657 * is a linear 8-bit value. Combine this with the
1658 * current outrow[c] value which is sRGB encoded.
1659 * Arithmetic here is 16-bits to preserve the output
1660 * values correctly.
1661 */
1662 component *= 257*255; /* =65535 */
1663 component += (255-alpha)*png_sRGB_table[outrow[c]];
1664
1665 /* So 'component' is scaled by 255*65535 and is
1666 * therefore appropriate for the above tables.
1667 */
1668 component = PNG_sRGB_FROM_LINEAR(component);
1669 }
1670
1671 outrow[c] = (png_byte)component;
1672 }
1673 }
1674
1675 inrow += channels+1; /* components and alpha channel */
1676 }
1677
1678 row += display->row_bytes;
1679 }
1680 }
1681 }
1682
1683 return 1;
1684}
1685
1686/* The guts of png_image_finish_read as a png_safe_execute callback. */
1687static int
1688png_image_read_end(png_voidp argument)
1689{
1690 png_image_read_control *display = argument;
1691 png_imagep image = display->image;
1692 png_structp png_ptr = image->opaque->png_ptr;
1693 png_infop info_ptr = image->opaque->info_ptr;
1694
1695 png_uint_32 format = image->format;
1696 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
1697 int do_local_compose = 0;
1698 int passes = 0;
1699
1700 /* Add transforms to ensure the correct output format is produced then check
1701 * that the required implementation support is there. Always expand; always
1702 * need 8 bits minimum, no palette and expanded tRNS.
1703 */
1704 png_set_expand(png_ptr);
1705
1706 /* Now check the format to see if it was modified. */
1707 {
1708 png_uint_32 base_format = png_image_format(png_ptr, info_ptr);
1709 png_uint_32 change = format ^ base_format;
1710
1711 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
1712 */
1713 {
1714 png_fixed_point input_gamma_default, output_gamma;
1715 int mode;
1716
1717 if (base_format & PNG_FORMAT_FLAG_LINEAR)
1718 input_gamma_default = PNG_GAMMA_LINEAR;
1719 else
1720 input_gamma_default = PNG_DEFAULT_sRGB;
1721
1722 if (linear)
1723 {
1724 mode = PNG_ALPHA_STANDARD;
1725 output_gamma = PNG_GAMMA_LINEAR;
1726 }
1727
1728 else
1729 {
1730 mode = PNG_ALPHA_PNG;
1731 output_gamma = PNG_DEFAULT_sRGB;
1732 }
1733
1734 /* Set the mode, the default gamma for the file and then, if it
1735 * doesn't match the default, the output gamma.
1736 */
1737 png_set_alpha_mode_fixed(png_ptr, mode, input_gamma_default);
1738 if (input_gamma_default != output_gamma)
1739 png_set_alpha_mode(png_ptr, mode, output_gamma);
1740
1741 /* If the bit-depth changes then handle that here. */
1742 if (change & PNG_FORMAT_FLAG_LINEAR)
1743 {
1744 if (linear /*16-bit output*/)
1745 png_set_expand_16(png_ptr);
1746
1747 else /* 8-bit output */
1748 png_set_scale_16(png_ptr);
1749
1750 change &= ~PNG_FORMAT_FLAG_LINEAR;
1751 }
1752 }
1753
1754 /* Now the background/alpha channel changes. */
1755 if (change & PNG_FORMAT_FLAG_ALPHA)
1756 {
1757 /* Removing an alpha channel requires composition for the 8-bit
1758 * formats; for the 16-bit it is already done, above, by the
1759 * pre-multiplication and the channel just needs to be stripped.
1760 */
1761 if (base_format & PNG_FORMAT_FLAG_ALPHA)
1762 {
1763 if (linear) /* compose on black (well, pre-multiply) */
1764 png_set_strip_alpha(png_ptr);
1765
1766 else if (display->background != NULL)
1767 {
1768 png_color_16 c;
1769
1770 c.index = 0; /*unused*/
1771 c.red = display->background->red;
1772 c.green = display->background->green;
1773 c.blue = display->background->blue;
1774 c.gray = display->background->green;
1775
1776 /* This is always an 8-bit sRGB value, using the 'green' channel
1777 * for gray is much better than calculating the luminance here;
1778 * we can get off-by-one errors in that calculation relative to
1779 * the app expectations and that will show up in transparent
1780 * pixels.
1781 */
1782 png_set_background_fixed(png_ptr, &c,
1783 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
1784 0/*gamma: not used*/);
1785 }
1786
1787 else /* compose on row: implemented below. */
1788 {
1789 do_local_compose = 1;
1790 /* This leaves the alpha channel in the output, it has to be
1791 * removed by the code below. Set the encoding to the 'OPTIMIZE'
1792 * one so the code only has to hack on the pixels that require
1793 * composition.
1794 */
1795 png_set_alpha_mode(png_ptr, PNG_ALPHA_OPTIMIZED,
1796 PNG_DEFAULT_sRGB);
1797 }
1798 }
1799
1800 else /* output needs an alpha channel */
1801 {
1802 /* This is tricky because it happens before the swap operation has
1803 * been accomplished, so always add the alpha channel after the
1804 * component channels.
1805 */
1806 png_set_add_alpha(png_ptr, 255/*opaque*/, PNG_FILLER_AFTER);
1807 }
1808
1809 change &= ~PNG_FORMAT_FLAG_ALPHA;
1810 }
1811
1812 if (change & PNG_FORMAT_FLAG_COLOR)
1813 {
1814 /* gray<->color transformation required. */
1815 if (format & PNG_FORMAT_FLAG_COLOR)
1816 png_set_gray_to_rgb(png_ptr);
1817
1818 else
1819 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
1820 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
1821
1822 change &= ~PNG_FORMAT_FLAG_COLOR;
1823 }
1824
1825# ifdef PNG_FORMAT_BGR_SUPPORTED
1826 if (format & PNG_FORMAT_FLAG_BGR)
1827 {
1828 /* Check only the output format; PNG is never BGR, don't do this if
1829 * the output is gray, but fix up the 'format' value in that case.
1830 */
1831 if (format & PNG_FORMAT_FLAG_COLOR)
1832 png_set_bgr(png_ptr);
1833
1834 else
1835 format &= ~PNG_FORMAT_FLAG_BGR;
1836
1837 change &= ~PNG_FORMAT_FLAG_BGR;
1838 }
1839# endif
1840
1841# ifdef PNG_FORMAT_AFIRST_SUPPORTED
1842 if (format & PNG_FORMAT_FLAG_AFIRST)
1843 {
1844 /* Only relevant if there is an alpha channel - it's particularly
1845 * important to handle this correctly because do_local_compose may
1846 * be set above and then libpng will keep the alpha channel for this
1847 * code to remove.
1848 */
1849 if (format & PNG_FORMAT_FLAG_ALPHA)
1850 png_set_swap_alpha(png_ptr);
1851
1852 else
1853 format &= ~PNG_FORMAT_FLAG_AFIRST;
1854
1855 change &= ~PNG_FORMAT_FLAG_AFIRST;
1856 }
1857# endif
1858
1859 /* If the *output* is 16-bit then we need to check for a byte-swap on this
1860 * architecture.
1861 */
1862 if (linear)
1863 {
1864 PNG_CONST png_uint_16 le = 0x0001;
1865
1866 if (*(png_const_bytep)&le)
1867 png_set_swap(png_ptr);
1868 }
1869
1870 /* If change is not now 0 some transformation is missing - error out. */
1871 if (change)
1872 png_error(png_ptr, "png_read_image: unsupported transformation");
1873 }
1874
1875 /* Update the 'info' structure and make sure the result is as required, first
1876 * make sure to turn on the interlace handling if it will be required
1877 * (because it can't be turned on *after* the call to png_read_update_info!)
1878 */
1879 if (!do_local_compose)
1880 passes = png_set_interlace_handling(png_ptr);
1881
1882 png_read_update_info(png_ptr, info_ptr);
1883
1884 {
1885 png_uint_32 info_format = 0;
1886
1887 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
1888 info_format |= PNG_FORMAT_FLAG_COLOR;
1889
1890 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1891 {
1892 /* This channel may be removed below. */
1893 if (!do_local_compose)
1894 info_format |= PNG_FORMAT_FLAG_ALPHA;
1895 }
1896
1897 else if (do_local_compose) /* internal error */
1898 png_error(png_ptr, "png_image_read: alpha channel lost");
1899
1900 if (info_ptr->bit_depth == 16)
1901 info_format |= PNG_FORMAT_FLAG_LINEAR;
1902
1903# ifdef PNG_FORMAT_BGR_SUPPORTED
1904 if (png_ptr->transformations & PNG_BGR)
1905 info_format |= PNG_FORMAT_FLAG_BGR;
1906# endif
1907
1908# ifdef PNG_FORMAT_AFIRST_SUPPORTED
1909 if (png_ptr->transformations & PNG_SWAP_ALPHA)
1910 info_format |= PNG_FORMAT_FLAG_AFIRST;
1911# endif
1912
1913 /* This is actually an internal error. */
1914 if (info_format != format)
1915 png_error(png_ptr, "png_read_image: invalid transformations");
1916 }
1917
1918 /* Now read the rows. If do_local_compose is set then it is necessary to use
1919 * a local row buffer. The output will be GA, RGBA or BGRA and must be
1920 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
1921 * display acts as a flag.
1922 */
1923 {
1924 png_bytep first_row = display->buffer;
1925 ptrdiff_t row_bytes = display->row_stride;
1926
1927 if (linear)
1928 row_bytes *= sizeof (png_uint_16);
1929
1930 /* The following expression is designed to work correctly whether it gives
1931 * a signed or an unsigned result.
1932 */
1933 if (row_bytes < 0)
1934 first_row += (image->height-1) * (-row_bytes);
1935
1936 display->first_row = first_row;
1937 display->row_bytes = row_bytes;
1938 }
1939
1940 if (do_local_compose)
1941 {
1942 int result;
1943 png_bytep row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
1944
1945 display->local_row = row;
1946 result = png_safe_execute(image, png_image_read_composite, display);
1947 display->local_row = NULL;
1948 png_free(png_ptr, row);
1949
1950 return result;
1951 }
1952
1953 else
1954 {
1955 png_alloc_size_t row_bytes = display->row_bytes;
1956
1957 while (--passes >= 0)
1958 {
1959 png_uint_32 y = image->height;
1960 png_bytep row = display->first_row;
1961
1962 while (y-- > 0)
1963 {
1964 png_read_row(png_ptr, row, NULL);
1965 row += row_bytes;
1966 }
1967 }
1968
1969 return 1;
1970 }
1971}
1972
1973int PNGAPI
1974png_image_finish_read(png_imagep image, png_colorp background, void *buffer,
1975 png_int_32 row_stride)
1976{
1977 if (image != NULL)
1978 {
1979 png_uint_32 check;
1980
1981 if (row_stride < 0)
1982 check = -row_stride;
1983
1984 else
1985 check = row_stride;
1986
1987 if (buffer != NULL && check >= PNG_IMAGE_ROW_STRIDE(*image))
1988 {
1989 int result;
1990 png_image_read_control display;
1991
1992 memset(&display, 0, sizeof display);
1993 display.image = image;
1994 display.buffer = buffer;
1995 display.row_stride = row_stride;
1996 display.background = background;
1997 display.local_row = NULL;
1998 result = png_safe_execute(image, png_image_read_end, &display);
1999 png_image_free(image);
2000 return result;
2001 }
2002
2003 else
2004 return png_image_error(image,
2005 "png_image_finish_read: invalid argument");
2006 }
2007
2008 return 0;
2009}
2010
2011#endif /* PNG_SIMPLIFIED_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06002012#endif /* PNG_READ_SUPPORTED */