blob: b5e61e2d0a61e50cd06d560b82be3f6e82d12d63 [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-Pehrson13831bc2011-12-21 08:28:28 -06004 * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
Glenn Randers-Pehrson1531bd62012-01-01 14:45:04 -06005 * Copyright (c) 1998-2012 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{
John Bowler1d7f56a2011-12-21 20:14:23 -060029#ifndef PNG_USER_MEM_SUPPORTED
30 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
31 error_fn, warn_fn, NULL, NULL, NULL);
32#else
33 return png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
34 warn_fn, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050035}
36
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060037/* Alternate create PNG structure for reading, and allocate any memory
38 * needed.
39 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050040PNG_FUNCTION(png_structp,PNGAPI
41png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -060042 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050043 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050044{
John Bowler1d7f56a2011-12-21 20:14:23 -060045 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
46 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050047#endif /* PNG_USER_MEM_SUPPORTED */
48
John Bowler1d7f56a2011-12-21 20:14:23 -060049 if (png_ptr != NULL)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050050 {
John Bowler1d7f56a2011-12-21 20:14:23 -060051 int ok = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050052
John Bowler1d7f56a2011-12-21 20:14:23 -060053 /* TODO: why does this happen here on read, but in png_write_IHDR on
John Bowler5d567862011-12-24 09:12:00 -060054 * write? If it happened there then there would be no error handling case
55 * here and png_ptr could be a png_structrp.
John Bowler1d7f56a2011-12-21 20:14:23 -060056 */
57 png_ptr->zstream.zalloc = png_zalloc;
58 png_ptr->zstream.zfree = png_zfree;
59 png_ptr->zstream.opaque = png_ptr;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050060
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050061 switch (inflateInit(&png_ptr->zstream))
62 {
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050063 case Z_OK:
John Bowler1d7f56a2011-12-21 20:14:23 -060064 ok = 1;
65 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050066
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050067 case Z_MEM_ERROR:
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050068 png_warning(png_ptr, "zlib memory error");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050069 break;
70
71 case Z_STREAM_ERROR:
72 png_warning(png_ptr, "zlib stream error");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050073 break;
74
75 case Z_VERSION_ERROR:
76 png_warning(png_ptr, "zlib version error");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050077 break;
78
John Bowler1d7f56a2011-12-21 20:14:23 -060079 default:
80 png_warning(png_ptr, "Unknown zlib error");
81 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050082 }
John Bowler1d7f56a2011-12-21 20:14:23 -060083
84 if (ok)
85 {
86 png_ptr->zstream.next_out = png_ptr->zbuf;
87 png_ptr->zstream.avail_out = png_ptr->zbuf_size;
88
89 /* TODO: delay this, it can be done in png_init_io (if the app doesn't
90 * do it itself) avoiding setting the default function if it is not
91 * required.
92 */
93 png_set_read_fn(png_ptr, NULL, NULL);
94
95 return png_ptr;
96 }
97
98 /* Else something went wrong in the zlib initialization above; it would
99 * much simplify this code if the creation of the zlib stuff was to be
100 * delayed until it is needed.
101 */
102 png_destroy_read_struct(&png_ptr, NULL, NULL);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500103 }
104
John Bowler1d7f56a2011-12-21 20:14:23 -0600105 return NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500106}
107
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500108
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500109#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600110/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -0600111 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600112 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500113 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600114 * via png_set_sig_bytes(), and we will only check the remaining bytes
115 * here. The application can then have access to the signature bytes we
116 * read if it is determined that this isn't a valid PNG file.
117 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500118void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600119png_read_info(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500120{
Glenn Randers-Pehrsonc81bb8a2009-08-15 22:02:26 -0500121 png_debug(1, "in png_read_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500122
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500123 if (png_ptr == NULL || info_ptr == NULL)
124 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500125
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600126 /* Read and check the PNG file signature. */
127 png_read_sig(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500128
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500129 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500130 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500131 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500132 png_uint_32 chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133
134 /* This should be a binary subdivision search or a hash for
135 * matching the chunk name rather than a linear search.
136 */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500137 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600138 if (png_ptr->mode & PNG_AFTER_IDAT)
139 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500140
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500141 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600142 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500143
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500144 else if (chunk_name == png_IEND)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600145 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500146
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600147#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500148 else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
149 PNG_HANDLE_CHUNK_AS_DEFAULT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600150 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500151 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600152 png_ptr->mode |= PNG_HAVE_IDAT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500153
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600154 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500155
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500156 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600157 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500158
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500159 else if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600160 {
161 if (!(png_ptr->mode & PNG_HAVE_IHDR))
162 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500163
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600164 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600165 !(png_ptr->mode & PNG_HAVE_PLTE))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600166 png_error(png_ptr, "Missing PLTE before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500167
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600168 break;
169 }
170 }
171#endif
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500172 else if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600173 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500174
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500175 else if (chunk_name == png_IDAT)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500176 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500177 if (!(png_ptr->mode & PNG_HAVE_IHDR))
178 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500179
Guy Schalnate5a37791996-06-05 15:50:50 -0500180 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600181 !(png_ptr->mode & PNG_HAVE_PLTE))
Guy Schalnate5a37791996-06-05 15:50:50 -0500182 png_error(png_ptr, "Missing PLTE before IDAT");
183
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500184 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500185 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500186 break;
187 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500188
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500189#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500190 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500191 png_handle_bKGD(png_ptr, info_ptr, length);
192#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500193
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500194#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500195 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500196 png_handle_cHRM(png_ptr, info_ptr, length);
197#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500198
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500199#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500200 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500201 png_handle_gAMA(png_ptr, info_ptr, length);
202#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500203
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500204#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500205 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500206 png_handle_hIST(png_ptr, info_ptr, length);
207#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500208
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500209#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500210 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500211 png_handle_oFFs(png_ptr, info_ptr, length);
212#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500213
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500214#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500215 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500216 png_handle_pCAL(png_ptr, info_ptr, length);
217#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500218
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500219#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500220 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600221 png_handle_sCAL(png_ptr, info_ptr, length);
222#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500223
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500224#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500225 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500226 png_handle_pHYs(png_ptr, info_ptr, length);
227#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500228
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500229#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500230 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500231 png_handle_sBIT(png_ptr, info_ptr, length);
232#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500233
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500234#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500235 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600236 png_handle_sRGB(png_ptr, info_ptr, length);
237#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500238
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500239#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500240 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600241 png_handle_iCCP(png_ptr, info_ptr, length);
242#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500243
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500244#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500245 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600246 png_handle_sPLT(png_ptr, info_ptr, length);
247#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500248
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500249#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500250 else if (chunk_name == png_tEXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500251 png_handle_tEXt(png_ptr, info_ptr, length);
252#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500253
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500254#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500255 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500256 png_handle_tIME(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_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500260 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500261 png_handle_tRNS(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_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500265 else if (chunk_name == png_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500266 png_handle_zTXt(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_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500270 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600271 png_handle_iTXt(png_ptr, info_ptr, length);
272#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500273
Guy Schalnat0d580581995-07-20 02:43:20 -0500274 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600275 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500276 }
277}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500278#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500279
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500280/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500281void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600282png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500283{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500284 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500285
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500286 if (png_ptr == NULL)
287 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500288
Glenn Randers-Pehrson3dbfd302011-10-13 17:24:36 -0500289 png_read_start_row(png_ptr);
Glenn Randers-Pehrson4cfdb3c2009-11-26 11:49:37 -0600290
John Bowler4a12f4a2011-04-17 18:34:22 -0500291#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600292 png_read_transform_info(png_ptr, info_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -0500293#else
294 PNG_UNUSED(info_ptr)
295#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500296}
297
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500298#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600299/* Initialize palette, background, etc, after transformations
300 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500301 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600302 * If the user doesn't call this, we will do it ourselves.
303 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500304void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600305png_start_read_image(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500306{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500307 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500308
Glenn Randers-Pehrson3dbfd302011-10-13 17:24:36 -0500309 if (png_ptr != NULL)
310 png_read_start_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500311}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500312#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500313
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500314#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500315void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600316png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500317{
318 int ret;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500319
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500320 png_row_info row_info;
321
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500322 if (png_ptr == NULL)
323 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500324
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500325 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -0500326 (unsigned long)png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500327
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500328 /* png_read_start_row sets the information (in particular iwidth) for this
329 * interlace pass.
330 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500331 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500332 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500333
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500334 /* 1.5.6: row_info moved out of png_struct to a local here. */
335 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
336 row_info.color_type = png_ptr->color_type;
337 row_info.bit_depth = png_ptr->bit_depth;
338 row_info.channels = png_ptr->channels;
339 row_info.pixel_depth = png_ptr->pixel_depth;
340 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
341
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500342 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
343 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500344 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500345#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
346 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500347 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500348#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500349
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500350#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
351 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500352 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500353#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500354
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600355#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
356 !defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500357 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500358 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500359#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500360
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500361#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
362 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500363 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500364#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500365
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500366#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
367 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500368 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500369#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500370
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500371#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
372 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500373 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500374#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500375
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500376#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
377 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500378 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500379#endif
380 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500381
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500382#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500383 /* If interlaced and we do not need a new row, combine row and return.
384 * Notice that the pixels we have from previous rows have been transformed
385 * already; we can only combine like with like (transformed or
386 * untransformed) and, because of the libpng API for interlaced images, this
387 * means we must transform before de-interlacing.
388 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500389 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
390 {
391 switch (png_ptr->pass)
392 {
393 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600394 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500395 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500396 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500397 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600398 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500399 return;
400 }
401 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500402
Guy Schalnat0d580581995-07-20 02:43:20 -0500403 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600404 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500405 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500406 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500407 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500408
Guy Schalnat0d580581995-07-20 02:43:20 -0500409 png_read_finish_row(png_ptr);
410 return;
411 }
412 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500413
Guy Schalnat0d580581995-07-20 02:43:20 -0500414 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600415 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500416 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500417 if (dsp_row != NULL && (png_ptr->row_number & 4))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500418 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500419
Guy Schalnat0d580581995-07-20 02:43:20 -0500420 png_read_finish_row(png_ptr);
421 return;
422 }
423 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500424
Guy Schalnat0d580581995-07-20 02:43:20 -0500425 case 3:
426 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
427 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500428 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500429 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500430
Guy Schalnat0d580581995-07-20 02:43:20 -0500431 png_read_finish_row(png_ptr);
432 return;
433 }
434 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500435
Guy Schalnat0d580581995-07-20 02:43:20 -0500436 case 4:
437 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600438 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500439 if (dsp_row != NULL && (png_ptr->row_number & 2))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500440 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500441
Guy Schalnat0d580581995-07-20 02:43:20 -0500442 png_read_finish_row(png_ptr);
443 return;
444 }
445 break;
446 case 5:
447 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
448 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500449 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500450 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500451
Guy Schalnat0d580581995-07-20 02:43:20 -0500452 png_read_finish_row(png_ptr);
453 return;
454 }
455 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500456
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600457 default:
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600458 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500459 if (!(png_ptr->row_number & 1))
460 {
461 png_read_finish_row(png_ptr);
462 return;
463 }
464 break;
465 }
466 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500467#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500468
Guy Schalnate5a37791996-06-05 15:50:50 -0500469 if (!(png_ptr->mode & PNG_HAVE_IDAT))
470 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500471
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600472 png_ptr->zstream.next_out = png_ptr->row_buf;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600473 png_ptr->zstream.avail_out =
474 (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
475 png_ptr->iwidth) + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500476
Guy Schalnat0d580581995-07-20 02:43:20 -0500477 do
478 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600479 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500480 {
481 while (!png_ptr->idat_size)
482 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600483 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500484
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500485 png_ptr->idat_size = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500486 if (png_ptr->chunk_name != png_IDAT)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600487 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500488 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600489 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
490 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500491 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600492 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500493 png_crc_read(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600494 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600495 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500496 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500497
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600498 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500499
Guy Schalnat0d580581995-07-20 02:43:20 -0500500 if (ret == Z_STREAM_END)
501 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600502 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500503 png_ptr->idat_size)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500504 png_benign_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600505 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500506 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600507 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500508 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500509
Guy Schalnat0d580581995-07-20 02:43:20 -0500510 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600511 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600512 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600513
514 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500515
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500516 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
517 {
518 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
Mans Rullgardd3a94802011-11-03 00:47:55 -0500519 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500520 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
521 else
522 png_error(png_ptr, "bad adaptive filter value");
523 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500524
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500525 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
526 * 1.5.6, while the buffer really is this big in current versions of libpng
527 * it may not be in the future, so this was changed just to copy the
528 * interlaced count:
529 */
530 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600531
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500532#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500533 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600534 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600535 {
536 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500537 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600538 }
539#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500540
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500541
John Bowler4a12f4a2011-04-17 18:34:22 -0500542#ifdef PNG_READ_TRANSFORMS_SUPPORTED
John Bowler9b872f42011-02-12 09:00:16 -0600543 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500544 png_do_read_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500545#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500546
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500547 /* The transformed pixel depth should match the depth now in row_info. */
548 if (png_ptr->transformed_pixel_depth == 0)
549 {
550 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
551 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
552 png_error(png_ptr, "sequential row overflow");
553 }
554
555 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
556 png_error(png_ptr, "internal sequential row size calculation error");
557
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500558#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500559 /* Blow up interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500560 if (png_ptr->interlaced &&
561 (png_ptr->transformations & PNG_INTERLACE))
562 {
563 if (png_ptr->pass < 6)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500564 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
565 png_ptr->transformations);
Guy Schalnat0d580581995-07-20 02:43:20 -0500566
Andreas Dilger47a0c421997-05-16 02:46:07 -0500567 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500568 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500569
Andreas Dilger47a0c421997-05-16 02:46:07 -0500570 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500571 png_combine_row(png_ptr, row, 0/*row*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500572 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500573
Guy Schalnat0d580581995-07-20 02:43:20 -0500574 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500575#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500576 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500577 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500578 png_combine_row(png_ptr, row, -1/*ignored*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500579
Andreas Dilger47a0c421997-05-16 02:46:07 -0500580 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500581 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500582 }
583 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600584
585 if (png_ptr->read_row_fn != NULL)
586 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500587}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500588#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500589
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500590#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500591/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600592 * and png_set_interlace_handling() has been called, the rows need to
593 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500594 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600595 * called, the rows contents must be initialized to the contents of the
596 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600597 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600598 * "row" holds the actual image, and pixels are placed in it
599 * as they arrive. If the image is displayed after each pass, it will
600 * appear to "sparkle" in. "display_row" can be used to display a
601 * "chunky" progressive image, with finer detail added as it becomes
602 * available. If you do not want this "chunky" display, you may pass
603 * NULL for display_row. If you do not want the sparkle display, and
604 * you have not called png_handle_alpha(), you may pass NULL for rows.
605 * If you have called png_handle_alpha(), and the image has either an
606 * alpha channel or a transparency chunk, you must provide a buffer for
607 * rows. In this case, you do not have to provide a display_row buffer
608 * also, but you may. If the image is not interlaced, or if you have
609 * not called png_set_interlace_handling(), the display_row buffer will
610 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500611 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600612 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600613 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600614
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500615void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600616png_read_rows(png_structrp png_ptr, png_bytepp row,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600617 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500618{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600619 png_uint_32 i;
620 png_bytepp rp;
621 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500622
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500623 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500624
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500625 if (png_ptr == NULL)
626 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500627
Guy Schalnat0f716451995-11-28 11:22:13 -0600628 rp = row;
629 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500630 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500631 for (i = 0; i < num_rows; i++)
632 {
633 png_bytep rptr = *rp++;
634 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600635
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500636 png_read_row(png_ptr, rptr, dptr);
637 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500638
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500639 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500640 for (i = 0; i < num_rows; i++)
641 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500642 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500643 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500644 rp++;
645 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500646
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500647 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500648 for (i = 0; i < num_rows; i++)
649 {
650 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500651 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500652 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500653 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500654}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500655#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500656
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500657#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500658/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500659 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600660 * initialize the image to the current image that PNG will be overlaying.
661 * We set the num_rows again here, in case it was incorrectly set in
662 * png_read_start_row() by a call to png_read_update_info() or
663 * png_start_read_image() if png_set_interlace_handling() wasn't called
664 * prior to either of these functions like it should have been. You can
665 * only call this function once. If you desire to have an image for
666 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500667 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600668 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600669 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500670void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600671png_read_image(png_structrp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500672{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500673 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500674 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600675 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500676
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500677 png_debug(1, "in png_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500678
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500679 if (png_ptr == NULL)
680 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500681
682#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500683 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
684 {
685 pass = png_set_interlace_handling(png_ptr);
686 /* And make sure transforms are initialized. */
687 png_start_read_image(png_ptr);
688 }
689 else
690 {
Glenn Randers-Pehrsone15a96b2011-01-14 15:47:37 -0600691 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500692 {
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500693 /* Caller called png_start_read_image or png_read_update_info without
694 * first turning on the PNG_INTERLACE transform. We can fix this here,
695 * but the caller should do it!
696 */
697 png_warning(png_ptr, "Interlace handling should be turned on when "
698 "using png_read_image");
699 /* Make sure this is set correctly */
700 png_ptr->num_rows = png_ptr->height;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500701 }
702
703 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
704 * the above error case.
705 */
706 pass = png_set_interlace_handling(png_ptr);
707 }
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500708#else
709 if (png_ptr->interlaced)
710 png_error(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600711 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500712
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500713 pass = 1;
714#endif
715
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500716 image_height=png_ptr->height;
Guy Schalnate5a37791996-06-05 15:50:50 -0500717
Guy Schalnat0d580581995-07-20 02:43:20 -0500718 for (j = 0; j < pass; j++)
719 {
720 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500721 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500722 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500723 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500724 rp++;
725 }
726 }
727}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500728#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500729
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500730#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600732 * file, will verify the end is accurate, and will read any comments
733 * or time information at the end of the file, if info is not NULL.
734 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500735void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600736png_read_end(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500737{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500738 png_debug(1, "in png_read_end");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500739
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500740 if (png_ptr == NULL)
741 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500742
Andreas Dilger47a0c421997-05-16 02:46:07 -0500743 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500744
745 do
746 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500747 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500748 png_uint_32 chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500749
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500750 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600751 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500752
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500753 else if (chunk_name == png_IEND)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600754 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500755
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600756#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500757 else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
758 PNG_HANDLE_CHUNK_AS_DEFAULT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600759 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500760 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600761 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500762 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500763 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600764 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600765 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500766 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600767 png_ptr->mode |= PNG_HAVE_PLTE;
768 }
769#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500770
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500771 else if (chunk_name == png_IDAT)
Guy Schalnat0d580581995-07-20 02:43:20 -0500772 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500773 /* Zero length IDATs are legal after the last IDAT has been
774 * read, but not after other chunks have been read.
775 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500776 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500777 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500778
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500779 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500780 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500781 else if (chunk_name == png_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500782 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500783
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500784#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500785 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500786 png_handle_bKGD(png_ptr, info_ptr, length);
787#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500788
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500789#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500790 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500791 png_handle_cHRM(png_ptr, info_ptr, length);
792#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500793
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500794#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500795 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500796 png_handle_gAMA(png_ptr, info_ptr, length);
797#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500798
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500799#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500800 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500801 png_handle_hIST(png_ptr, info_ptr, length);
802#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500803
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500804#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500805 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500806 png_handle_oFFs(png_ptr, info_ptr, length);
807#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500808
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500809#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500810 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500811 png_handle_pCAL(png_ptr, info_ptr, length);
812#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500813
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500814#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500815 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600816 png_handle_sCAL(png_ptr, info_ptr, length);
817#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500818
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500819#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500820 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500821 png_handle_pHYs(png_ptr, info_ptr, length);
822#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500823
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500824#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500825 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500826 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500827#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500828
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500829#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500830 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600831 png_handle_sRGB(png_ptr, info_ptr, length);
832#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500833
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500834#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500835 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600836 png_handle_iCCP(png_ptr, info_ptr, length);
837#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500838
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500839#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500840 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600841 png_handle_sPLT(png_ptr, info_ptr, length);
842#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500843
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500844#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500845 else if (chunk_name == png_tEXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600846 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500847#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500848
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500849#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500850 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500851 png_handle_tIME(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_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500855 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500856 png_handle_tRNS(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_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500860 else if (chunk_name == png_zTXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600861 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500862#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500863
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500864#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500865 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600866 png_handle_iTXt(png_ptr, info_ptr, length);
867#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500868
Guy Schalnat0d580581995-07-20 02:43:20 -0500869 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600870 png_handle_unknown(png_ptr, info_ptr, length);
871 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500872}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500873#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500874
John Bowler1d7f56a2011-12-21 20:14:23 -0600875/* Free all memory used in the read struct */
876static void
John Bowler5d567862011-12-24 09:12:00 -0600877png_read_destroy(png_structrp png_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500878{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500879 png_debug(1, "in png_read_destroy");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500880
John Bowler07772cb2011-10-14 18:19:47 -0500881#ifdef PNG_READ_GAMMA_SUPPORTED
882 png_destroy_gamma_table(png_ptr);
883#endif
884
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600885 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -0500886 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgard1c422762011-10-17 16:52:19 -0500887 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrsonb3ff9682008-07-21 08:05:57 -0500888 png_free(png_ptr, png_ptr->chunkdata);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500889
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500890#ifdef PNG_READ_QUANTIZE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600891 png_free(png_ptr, png_ptr->palette_lookup);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500892 png_free(png_ptr, png_ptr->quantize_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500893#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500894
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600895 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600896 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600897 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500898
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600899#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500900 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600901 if (png_ptr->free_me & PNG_FREE_TRNS)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500902 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600903 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500904#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500905
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600906 inflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500907
Guy Schalnat6d764711995-12-19 03:22:19 -0600908#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600909 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -0600910#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500911
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500912#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
913#ifdef PNG_TEXT_SUPPORTED
914 png_free(png_ptr, png_ptr->current_text);
915#endif /* PNG_TEXT_SUPPORTED */
916#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
917
John Bowler40b26032011-12-22 08:09:15 -0600918#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
919 png_free(png_ptr, png_ptr->unknown_chunk.data);
920#endif
921
922#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
923 png_free(png_ptr, png_ptr->chunk_list);
924#endif
925
John Bowler1d7f56a2011-12-21 20:14:23 -0600926 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
927 * callbacks are still set at this point. They are required to complete the
928 * destruction of the png_struct itself.
Guy Schalnate5a37791996-06-05 15:50:50 -0500929 */
John Bowler1d7f56a2011-12-21 20:14:23 -0600930}
Guy Schalnate5a37791996-06-05 15:50:50 -0500931
John Bowler1d7f56a2011-12-21 20:14:23 -0600932/* Free all memory used by the read */
933void PNGAPI
934png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
935 png_infopp end_info_ptr_ptr)
936{
John Bowler5d567862011-12-24 09:12:00 -0600937 png_structrp png_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500938
John Bowler1d7f56a2011-12-21 20:14:23 -0600939 png_debug(1, "in png_destroy_read_struct");
Guy Schalnate5a37791996-06-05 15:50:50 -0500940
John Bowler1d7f56a2011-12-21 20:14:23 -0600941 if (png_ptr_ptr != NULL)
942 png_ptr = *png_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500943
John Bowler1d7f56a2011-12-21 20:14:23 -0600944 if (png_ptr == NULL)
945 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600946
John Bowler1d7f56a2011-12-21 20:14:23 -0600947 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
948 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
949 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
950 */
951 png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
952 png_destroy_info_struct(png_ptr, info_ptr_ptr);
953
954 *png_ptr_ptr = NULL;
955 png_read_destroy(png_ptr);
956 png_destroy_png_struct(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500957}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600958
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500959void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600960png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600961{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500962 if (png_ptr == NULL)
963 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500964
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600965 png_ptr->read_row_fn = read_row_fn;
966}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600967
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500968
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500969#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500970#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500971void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600972png_read_png(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600973 int transforms,
974 voidp params)
975{
976 int row;
977
John Bowler6a6d79f2011-02-12 08:56:40 -0600978 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500979 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600980
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500981 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600982 * PNG file before the first IDAT (image data chunk).
983 */
984 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500985 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
986 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600987
988 /* -------------- image transformations start here ------------------- */
989
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500990#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -0500991 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500992 */
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -0500993 if (transforms & PNG_TRANSFORM_SCALE_16)
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500994 {
Glenn Randers-Pehrson6da2f2d2011-06-17 23:07:16 -0500995 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
996 * did in earlier versions, while "scale_16" is now more accurate.
997 */
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500998 png_set_scale_16(png_ptr);
999 }
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001000#endif
1001
1002#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
John Bowler8d261262011-06-18 13:37:11 -05001003 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
1004 * latter by doing SCALE first. This is ok and allows apps not to check for
1005 * which is supported to get the right answer.
1006 */
1007 if (transforms & PNG_TRANSFORM_STRIP_16)
1008 png_set_strip_16(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001009#endif
1010
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001011#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001012 /* Strip alpha bytes from the input data without combining with
1013 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001014 */
1015 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001016 png_set_strip_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001017#endif
1018
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001019#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001020 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001021 * byte into separate bytes (useful for paletted and grayscale images).
1022 */
1023 if (transforms & PNG_TRANSFORM_PACKING)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001024 png_set_packing(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001025#endif
1026
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001027#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001028 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001029 * (not useful if you are using png_set_packing).
1030 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001031 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001032 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001033#endif
1034
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001035#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001036 /* Expand paletted colors into true RGB triplets
1037 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1038 * Expand paletted or RGB images with transparency to full alpha
1039 * channels so the data will be available as RGBA quartets.
1040 */
1041 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001042 if ((png_ptr->bit_depth < 8) ||
1043 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1044 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001045 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001046#endif
1047
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001048 /* We don't handle background color or gamma transformation or quantizing.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001049 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001050
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001051#ifdef PNG_READ_INVERT_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001052 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001053 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001054 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001055 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001056#endif
1057
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001058#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001059 /* If you want to shift the pixel values from the range [0,255] or
1060 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1061 * colors were originally in:
1062 */
1063 if ((transforms & PNG_TRANSFORM_SHIFT)
1064 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1065 {
1066 png_color_8p sig_bit;
1067
1068 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1069 png_set_shift(png_ptr, sig_bit);
1070 }
1071#endif
1072
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001073#ifdef PNG_READ_BGR_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001074 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001075 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001076 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001077#endif
1078
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001079#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001080 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001081 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001082 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001083#endif
1084
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001085#ifdef PNG_READ_SWAP_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -05001086 /* Swap bytes of 16-bit files to least significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001087 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001088 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001089#endif
1090
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001091/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001092#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001093 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001094 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001095 png_set_invert_alpha(png_ptr);
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001096#endif
1097
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001098/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001099#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001100 /* Expand grayscale image to RGB */
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001101 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001102 png_set_gray_to_rgb(png_ptr);
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001103#endif
1104
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001105/* Added at libpng-1.5.4 */
John Bowler96cec0e2011-05-08 22:48:12 -05001106#ifdef PNG_READ_EXPAND_16_SUPPORTED
1107 if (transforms & PNG_TRANSFORM_EXPAND_16)
1108 png_set_expand_16(png_ptr);
1109#endif
1110
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001111 /* We don't handle adding filler bytes */
1112
John Bowler6a6d79f2011-02-12 08:56:40 -06001113 /* We use png_read_image and rely on that for interlace handling, but we also
1114 * call png_read_update_info therefore must turn on interlace handling now:
1115 */
1116 (void)png_set_interlace_handling(png_ptr);
1117
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001118 /* Optional call to gamma correct and add the background to the palette
1119 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001120 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001121 */
1122 png_read_update_info(png_ptr, info_ptr);
1123
1124 /* -------------- image transformations end here ------------------- */
1125
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001126 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001127 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001128 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001129 png_uint_32 iptr;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001130
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001131 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001132 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001133 for (iptr=0; iptr<info_ptr->height; iptr++)
1134 info_ptr->row_pointers[iptr] = NULL;
1135
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001136 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001137
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001138 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001139 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001140 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001141 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001142
1143 png_read_image(png_ptr, info_ptr->row_pointers);
1144 info_ptr->valid |= PNG_INFO_IDAT;
1145
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001146 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001147 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001148
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001149 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1150 PNG_UNUSED(params)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001151
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001152}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001153#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001154#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
John Bowler7875d532011-11-07 22:33:49 -06001155
1156#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1157/* SIMPLIFIED READ
1158 *
1159 * This code currently relies on the sequential reader, though it could easily
1160 * be made to work with the progressive one.
1161 */
1162/* Do all the *safe* initialization - 'safe' means that png_error won't be
John Bowler18c5cfa2011-11-16 14:26:34 -06001163 * called, so setting up the jmp_buf is not required. This means that anything
1164 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1165 * instead so that control is returned safely back to this routine.
John Bowler7875d532011-11-07 22:33:49 -06001166 */
1167static int
1168png_image_read_init(png_imagep image)
1169{
1170 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
1171 png_safe_error, png_safe_warning);
1172
1173 if (png_ptr != NULL)
1174 {
1175 png_infop info_ptr = png_create_info_struct(png_ptr);
1176
1177 if (info_ptr != NULL)
1178 {
John Bowler4fa96a42011-11-16 16:39:16 -06001179 png_controlp control = png_voidcast(png_controlp,
1180 png_malloc_warn(png_ptr, sizeof *control));
John Bowler7875d532011-11-07 22:33:49 -06001181
1182 if (control != NULL)
1183 {
John Bowlerbaeb6d12011-11-26 18:21:02 -06001184 png_memset(control, 0, sizeof *control);
John Bowler7875d532011-11-07 22:33:49 -06001185
1186 control->png_ptr = png_ptr;
1187 control->info_ptr = info_ptr;
1188 control->for_write = 0;
1189
1190 image->opaque = control;
1191 return 1;
1192 }
1193
1194 /* Error clean up */
1195 png_destroy_info_struct(png_ptr, &info_ptr);
1196 }
1197
1198 png_destroy_read_struct(&png_ptr, NULL, NULL);
1199 }
1200
1201 return png_image_error(image, "png_image_read: out of memory");
1202}
1203
1204/* Utility to find the base format of a PNG file from a png_struct. */
1205static png_uint_32
John Bowler5d567862011-12-24 09:12:00 -06001206png_image_format(png_structrp png_ptr, png_inforp info_ptr)
John Bowler7875d532011-11-07 22:33:49 -06001207{
1208 png_uint_32 format = 0;
1209
1210 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1211 format |= PNG_FORMAT_FLAG_COLOR;
1212
1213 if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1214 format |= PNG_FORMAT_FLAG_ALPHA;
1215
1216 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
1217 format |= PNG_FORMAT_FLAG_ALPHA;
1218
1219 if (png_ptr->bit_depth == 16)
1220 format |= PNG_FORMAT_FLAG_LINEAR;
1221
1222 return format;
1223}
1224
1225/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1226 * header and fill in all the information. This is executed in a safe context,
1227 * unlike the init routine above.
1228 */
1229static int
1230png_image_read_header(png_voidp argument)
1231{
John Bowler4fa96a42011-11-16 16:39:16 -06001232 png_imagep image = png_voidcast(png_imagep, argument);
John Bowler5d567862011-12-24 09:12:00 -06001233 png_structrp png_ptr = image->opaque->png_ptr;
1234 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001235
1236 png_read_info(png_ptr, info_ptr);
1237
1238 /* Do this the fast way; just read directly out of png_struct. */
1239 image->width = png_ptr->width;
1240 image->height = png_ptr->height;
1241
1242 {
1243 png_uint_32 format = png_image_format(png_ptr, info_ptr);
1244
1245 image->format = format;
1246 image->flags = 0;
1247
1248 /* Now try to work out whether the color data does not match sRGB. */
1249 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 &&
1250 (info_ptr->valid & PNG_INFO_sRGB) == 0)
1251 {
1252 /* gamma is irrelevant because libpng does gamma correction, what
1253 * matters is if the cHRM chunk doesn't match or, in the absence of
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06001254 * cRHM, if the iCCP profile appears to have different end points.
John Bowler7875d532011-11-07 22:33:49 -06001255 */
1256 if (info_ptr->valid & PNG_INFO_cHRM)
1257 {
1258 /* TODO: this is a copy'n'paste from pngrutil.c, make a common
1259 * checking function. This checks for a 1% error.
1260 */
1261 /* The cHRM chunk is used in preference to iCCP */
1262 if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) ||
1263 PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) ||
1264 PNG_OUT_OF_RANGE(info_ptr->x_red, 64000, 1000) ||
1265 PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) ||
1266 PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) ||
1267 PNG_OUT_OF_RANGE(info_ptr->y_green, 60000, 1000) ||
1268 PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) ||
1269 PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000))
1270 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1271 }
1272
1273 else if (info_ptr->valid & PNG_INFO_iCCP)
1274 {
Glenn Randers-Pehrson6d8e0902011-11-11 18:41:59 -06001275# if 0
1276 /* TODO: IMPLEMENT THIS! Remember to remove iCCP from
1277 the chunks_to_ignore list */
John Bowler7875d532011-11-07 22:33:49 -06001278 /* Here if we just have an iCCP chunk. */
1279 if (!png_iCCP_is_sRGB(png_ptr, info_ptr))
1280# endif
1281 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1282 }
1283 }
1284 }
1285
1286 return 1;
1287}
1288
1289#ifdef PNG_STDIO_SUPPORTED
1290int PNGAPI
1291png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1292{
1293 if (image != NULL)
1294 {
1295 if (file != NULL)
1296 {
1297 if (png_image_read_init(image))
1298 {
1299 /* This is slightly evil, but png_init_io doesn't do anything other
1300 * than this and we haven't changed the standard IO functions so
1301 * this saves a 'safe' function.
1302 */
1303 image->opaque->png_ptr->io_ptr = file;
1304 return png_safe_execute(image, png_image_read_header, image);
1305 }
1306 }
1307
1308 else
1309 return png_image_error(image,
1310 "png_image_begin_read_from_stdio: invalid argument");
1311 }
1312
1313 return 0;
1314}
1315
1316int PNGAPI
1317png_image_begin_read_from_file(png_imagep image, const char *file_name)
1318{
1319 if (image != NULL)
1320 {
1321 if (file_name != NULL)
1322 {
1323 FILE *fp = fopen(file_name, "rb");
1324
1325 if (fp != NULL)
1326 {
1327 if (png_image_read_init(image))
1328 {
1329 image->opaque->png_ptr->io_ptr = fp;
1330 image->opaque->owned_file = 1;
1331 return png_safe_execute(image, png_image_read_header, image);
1332 }
1333
1334 /* Clean up: just the opened file. */
1335 (void)fclose(fp);
1336 }
1337
1338 else
1339 return png_image_error(image, strerror(errno));
1340 }
1341
1342 else
1343 return png_image_error(image,
1344 "png_image_begin_read_from_file: invalid argument");
1345 }
1346
1347 return 0;
1348}
1349#endif /* PNG_STDIO_SUPPORTED */
1350
1351static void PNGCBAPI
1352png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
1353{
1354 if (png_ptr != NULL)
1355 {
John Bowler4fa96a42011-11-16 16:39:16 -06001356 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001357 if (image != NULL)
1358 {
1359 png_controlp cp = image->opaque;
1360 if (cp != NULL)
1361 {
1362 png_const_bytep memory = cp->memory;
1363 png_size_t size = cp->size;
1364
1365 if (memory != NULL && size >= need)
1366 {
John Bowlerbaeb6d12011-11-26 18:21:02 -06001367 png_memcpy(out, memory, need);
John Bowler7875d532011-11-07 22:33:49 -06001368 cp->memory = memory + need;
1369 cp->size = size - need;
1370 return;
1371 }
1372
1373 png_error(png_ptr, "read beyond end of data");
1374 }
1375 }
1376
1377 png_error(png_ptr, "invalid memory read");
1378 }
1379}
1380
1381int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1382 png_const_voidp memory, png_size_t size)
1383{
1384 if (image != NULL)
1385 {
1386 if (memory != NULL && size > 0)
1387 {
1388 if (png_image_read_init(image))
1389 {
1390 /* Now set the IO functions to read from the memory buffer and
1391 * store it into io_ptr. Again do this in-place to avoid calling a
1392 * libpng function that requires error handling.
1393 */
John Bowler4fa96a42011-11-16 16:39:16 -06001394 image->opaque->memory = png_voidcast(png_const_bytep, memory);
John Bowler7875d532011-11-07 22:33:49 -06001395 image->opaque->size = size;
1396 image->opaque->png_ptr->io_ptr = image;
1397 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1398
1399 return png_safe_execute(image, png_image_read_header, image);
1400 }
1401 }
1402
1403 else
1404 return png_image_error(image,
1405 "png_image_begin_read_from_memory: invalid argument");
1406 }
1407
1408 return 0;
1409}
1410
1411/* Arguments to png_image_finish_read: */
1412typedef struct
1413{
1414 /* Arguments: */
1415 png_imagep image;
1416 png_voidp buffer;
1417 png_int_32 row_stride;
1418 png_colorp background;
1419 /* Local variables: */
1420 png_bytep local_row;
1421 png_bytep first_row;
1422 ptrdiff_t row_bytes; /* unsigned arithmetic step between rows */
1423} png_image_read_control;
1424
1425/* Just the row reading part of png_image_read. */
1426static int
1427png_image_read_composite(png_voidp argument)
1428{
John Bowler4fa96a42011-11-16 16:39:16 -06001429 png_image_read_control *display = png_voidcast(png_image_read_control*,
1430 argument);
John Bowler7875d532011-11-07 22:33:49 -06001431 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001432 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001433 png_byte interlace_type = png_ptr->interlaced;
John Bowler4f67e402011-12-28 08:43:37 -06001434 int passes;
John Bowler7875d532011-11-07 22:33:49 -06001435
1436 switch (interlace_type)
1437 {
1438 case PNG_INTERLACE_NONE:
1439 passes = 1;
1440 break;
1441
1442 case PNG_INTERLACE_ADAM7:
1443 passes = PNG_INTERLACE_ADAM7_PASSES;
1444 break;
1445
1446 default:
John Bowler4f67e402011-12-28 08:43:37 -06001447 passes = 0;
John Bowler7875d532011-11-07 22:33:49 -06001448 png_error(png_ptr, "unknown interlace type");
1449 }
1450
1451 {
1452 png_uint_32 height = image->height;
1453 png_uint_32 width = image->width;
1454 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
1455 int pass;
1456
1457 for (pass = 0; pass < passes; ++pass)
1458 {
1459 png_bytep row = display->first_row;
1460 unsigned int startx, stepx, stepy;
1461 png_uint_32 y;
1462
1463 if (interlace_type == PNG_INTERLACE_ADAM7)
1464 {
1465 /* The row may be empty for a short image: */
1466 if (PNG_PASS_COLS(width, pass) == 0)
1467 continue;
1468
1469 startx = PNG_PASS_START_COL(pass);
1470 stepx = PNG_PASS_COL_OFFSET(pass);
1471 y = PNG_PASS_START_ROW(pass);
John Bowler18c5cfa2011-11-16 14:26:34 -06001472 stepy = PNG_PASS_ROW_OFFSET(pass);
John Bowler7875d532011-11-07 22:33:49 -06001473 }
1474
1475 else
1476 {
1477 y = 0;
1478 startx = 0;
1479 stepx = stepy = 1;
1480 }
John Bowler18c5cfa2011-11-16 14:26:34 -06001481
1482 /* The following are invariants across all the rows: */
1483 startx *= channels;
1484 stepx *= channels;
John Bowler7875d532011-11-07 22:33:49 -06001485
1486 for (; y<height; y += stepy)
John Bowler7875d532011-11-07 22:33:49 -06001487 {
1488 png_bytep inrow = display->local_row;
John Bowler18c5cfa2011-11-16 14:26:34 -06001489 png_bytep outrow = row + startx;
1490 png_const_bytep end_row = row + width * channels;
John Bowler7875d532011-11-07 22:33:49 -06001491
1492 /* Read the row, which is packed: */
1493 png_read_row(png_ptr, inrow, NULL);
1494
1495 /* Now do the composition on each pixel in this row. */
John Bowler18c5cfa2011-11-16 14:26:34 -06001496 for (; outrow < end_row; outrow += stepx)
John Bowler7875d532011-11-07 22:33:49 -06001497 {
1498 png_byte alpha = inrow[channels];
1499
1500 if (alpha > 0) /* else no change to the output */
1501 {
1502 unsigned int c;
1503
1504 for (c=0; c<channels; ++c)
1505 {
1506 png_uint_32 component = inrow[c];
1507
1508 if (alpha < 255) /* else just use component */
1509 {
1510 /* This is PNG_OPTIMIZED_ALPHA, the component value
1511 * is a linear 8-bit value. Combine this with the
1512 * current outrow[c] value which is sRGB encoded.
1513 * Arithmetic here is 16-bits to preserve the output
1514 * values correctly.
1515 */
1516 component *= 257*255; /* =65535 */
1517 component += (255-alpha)*png_sRGB_table[outrow[c]];
1518
1519 /* So 'component' is scaled by 255*65535 and is
John Bowler18c5cfa2011-11-16 14:26:34 -06001520 * therefore appropriate for the sRGB to linear
Glenn Randers-Pehrson7455cbf2011-11-24 14:40:36 -06001521 * conversion table.
John Bowler7875d532011-11-07 22:33:49 -06001522 */
1523 component = PNG_sRGB_FROM_LINEAR(component);
1524 }
1525
1526 outrow[c] = (png_byte)component;
1527 }
1528 }
1529
1530 inrow += channels+1; /* components and alpha channel */
1531 }
1532
1533 row += display->row_bytes;
1534 }
1535 }
1536 }
1537
1538 return 1;
1539}
1540
John Bowler18c5cfa2011-11-16 14:26:34 -06001541/* The do_local_background case; called when all the following transforms are to
1542 * be done:
1543 *
1544 * PNG_RGB_TO_GRAY
1545 * PNG_COMPOSITE
1546 * PNG_GAMMA
1547 *
1548 * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and
1549 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
1550 * correction. The fix-up is to prevent the PNG_COMPOSITE operation happening
1551 * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles
1552 * the removal or pre-multiplication of the alpha channel.
1553 */
1554static int
1555png_image_read_background(png_voidp argument)
1556{
John Bowler4fa96a42011-11-16 16:39:16 -06001557 png_image_read_control *display = png_voidcast(png_image_read_control*,
1558 argument);
John Bowler18c5cfa2011-11-16 14:26:34 -06001559 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001560 png_structrp png_ptr = image->opaque->png_ptr;
1561 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler18c5cfa2011-11-16 14:26:34 -06001562 png_byte interlace_type = png_ptr->interlaced;
1563 png_uint_32 height = image->height;
1564 png_uint_32 width = image->width;
John Bowler4f67e402011-12-28 08:43:37 -06001565 int pass, passes;
John Bowler18c5cfa2011-11-16 14:26:34 -06001566
1567 /* Double check the convoluted logic below. We expect to get here with
1568 * libpng doing rgb to gray and gamma correction but background processing
1569 * left to the png_image_read_background function. The rows libpng produce
1570 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
1571 */
1572 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
1573 png_error(png_ptr, "lost rgb to gray");
1574
1575 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1576 png_error(png_ptr, "unexpected compose");
1577
1578 /* The palette code zaps PNG_GAMMA in place... */
1579 if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) == 0 &&
1580 (png_ptr->transformations & PNG_GAMMA) == 0)
1581 png_error(png_ptr, "lost gamma correction");
1582
1583 if (png_get_channels(png_ptr, info_ptr) != 2)
1584 png_error(png_ptr, "lost/gained channels");
1585
1586 switch (interlace_type)
1587 {
1588 case PNG_INTERLACE_NONE:
1589 passes = 1;
1590 break;
1591
1592 case PNG_INTERLACE_ADAM7:
1593 passes = PNG_INTERLACE_ADAM7_PASSES;
1594 break;
1595
1596 default:
John Bowler4f67e402011-12-28 08:43:37 -06001597 passes = 0;
John Bowler18c5cfa2011-11-16 14:26:34 -06001598 png_error(png_ptr, "unknown interlace type");
1599 }
1600
1601 switch (png_get_bit_depth(png_ptr, info_ptr))
1602 {
1603 default:
1604 png_error(png_ptr, "unexpected bit depth");
1605 break;
1606
1607 case 8:
1608 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
1609 * to be removed by composing on a backgroundi: either the row if
1610 * display->background is NULL or display->background.green if not.
1611 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
1612 */
1613 for (pass = 0; pass < passes; ++pass)
1614 {
1615 png_bytep row = display->first_row;
1616 unsigned int startx, stepx, stepy;
1617 png_uint_32 y;
1618
1619 if (interlace_type == PNG_INTERLACE_ADAM7)
1620 {
1621 /* The row may be empty for a short image: */
1622 if (PNG_PASS_COLS(width, pass) == 0)
1623 continue;
1624
1625 startx = PNG_PASS_START_COL(pass);
1626 stepx = PNG_PASS_COL_OFFSET(pass);
1627 y = PNG_PASS_START_ROW(pass);
1628 stepy = PNG_PASS_ROW_OFFSET(pass);
1629 }
1630
1631 else
1632 {
1633 y = 0;
1634 startx = 0;
1635 stepx = stepy = 1;
1636 }
1637
1638 if (display->background == NULL)
1639 {
1640 for (; y<height; y += stepy)
1641 {
1642 png_bytep inrow = display->local_row;
1643 png_bytep outrow = row + startx;
1644 png_const_bytep end_row = row + width;
1645
1646 /* Read the row, which is packed: */
1647 png_read_row(png_ptr, inrow, NULL);
1648
1649 /* Now do the composition on each pixel in this row. */
1650 for (; outrow < end_row; outrow += stepx)
1651 {
1652 png_byte alpha = inrow[1];
1653
1654 if (alpha > 0) /* else no change to the output */
1655 {
1656 png_uint_32 component = inrow[0];
1657
1658 if (alpha < 255) /* else just use component */
1659 {
1660 /* Since PNG_OPTIMIZED_ALPHA was not set it is
1661 * necessary to invert the sRGB transfer
1662 * function and multiply the alpha out.
1663 */
1664 component = png_sRGB_table[component] * alpha;
1665 component += png_sRGB_table[outrow[0]] *
1666 (255-alpha);
1667 component = PNG_sRGB_FROM_LINEAR(component);
1668 }
1669
1670 outrow[0] = (png_byte)component;
1671 }
1672
1673 inrow += 2; /* gray and alpha channel */
1674 }
1675
1676 row += display->row_bytes;
1677 }
1678 }
1679
1680 else /* constant background value */
1681 {
1682 png_byte background8 = display->background->green;
1683 png_uint_16 background = png_sRGB_table[background8];
1684
1685 for (; y<height; y += stepy)
1686 {
1687 png_bytep inrow = display->local_row;
1688 png_bytep outrow = row + startx;
1689 png_const_bytep end_row = row + width;
1690
1691 /* Read the row, which is packed: */
1692 png_read_row(png_ptr, inrow, NULL);
1693
1694 /* Now do the composition on each pixel in this row. */
1695 for (; outrow < end_row; outrow += stepx)
1696 {
1697 png_byte alpha = inrow[1];
1698
1699 if (alpha > 0) /* else use background */
1700 {
1701 png_uint_32 component = inrow[0];
1702
1703 if (alpha < 255) /* else just use component */
1704 {
1705 component = png_sRGB_table[component] * alpha;
1706 component += background * (255-alpha);
1707 component = PNG_sRGB_FROM_LINEAR(component);
1708 }
1709
1710 outrow[0] = (png_byte)component;
1711 }
1712
1713 else
1714 outrow[0] = background8;
1715
1716 inrow += 2; /* gray and alpha channel */
1717 }
1718
1719 row += display->row_bytes;
1720 }
1721 }
1722 }
1723 break;
1724
1725 case 16:
1726 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
1727 * still be done and, maybe, the alpha channel removed. This code also
1728 * handles the alpha-first option.
1729 */
1730 {
1731 unsigned int outchannels = png_get_channels(png_ptr, info_ptr);
1732 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
1733 int swap_alpha = 0;
1734
1735 if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST))
1736 swap_alpha = 1;
1737
1738 for (pass = 0; pass < passes; ++pass)
1739 {
1740 png_uint_16p row = (png_uint_16p)display->first_row;
1741 unsigned int startx, stepx, stepy; /* all in pixels */
1742 png_uint_32 y;
1743
1744 if (interlace_type == PNG_INTERLACE_ADAM7)
1745 {
1746 /* The row may be empty for a short image: */
1747 if (PNG_PASS_COLS(width, pass) == 0)
1748 continue;
1749
1750 startx = PNG_PASS_START_COL(pass);
1751 stepx = PNG_PASS_COL_OFFSET(pass);
1752 y = PNG_PASS_START_ROW(pass);
1753 stepy = PNG_PASS_ROW_OFFSET(pass);
1754 }
1755
1756 else
1757 {
1758 y = 0;
1759 startx = 0;
1760 stepx = stepy = 1;
1761 }
1762
1763 startx *= outchannels;
1764 stepx *= outchannels;
1765
1766 for (; y<height; y += stepy)
1767 {
1768 png_uint_16p inrow;
1769 png_uint_16p outrow = row + startx;
1770 png_uint_16p end_row = row + width * outchannels;
1771
1772 /* Read the row, which is packed: */
1773 png_read_row(png_ptr, display->local_row, NULL);
1774 inrow = (png_uint_16p)display->local_row;
1775
1776 /* Now do the pre-multiplication on each pixel in this row.
1777 */
1778 for (; outrow < end_row; outrow += stepx)
1779 {
1780 png_uint_32 component = inrow[0];
1781 png_uint_16 alpha = inrow[1];
1782
1783 if (alpha > 0) /* else 0 */
1784 {
1785 if (alpha < 65535) /* else just use component */
1786 {
1787 component *= alpha;
1788 component += 32767;
1789 component /= 65535;
1790 }
1791 }
1792
1793 else
1794 component = 0;
1795
1796 outrow[swap_alpha] = (png_uint_16)component;
1797 if (outchannels > 1)
1798 outrow[1 ^ swap_alpha] = alpha;
1799
1800 inrow += 2; /* components and alpha channel */
1801 }
1802
1803 row += display->row_bytes;
1804 }
1805 }
1806 }
1807 break;
1808 }
1809
1810 return 1;
1811}
1812
John Bowler7875d532011-11-07 22:33:49 -06001813/* The guts of png_image_finish_read as a png_safe_execute callback. */
1814static int
1815png_image_read_end(png_voidp argument)
1816{
John Bowler4fa96a42011-11-16 16:39:16 -06001817 png_image_read_control *display = png_voidcast(png_image_read_control*,
1818 argument);
John Bowler7875d532011-11-07 22:33:49 -06001819 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001820 png_structrp png_ptr = image->opaque->png_ptr;
1821 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001822
1823 png_uint_32 format = image->format;
1824 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
1825 int do_local_compose = 0;
John Bowler18c5cfa2011-11-16 14:26:34 -06001826 int do_local_background = 0; /* to avoid double gamma correction bug */
John Bowler7875d532011-11-07 22:33:49 -06001827 int passes = 0;
1828
1829 /* Add transforms to ensure the correct output format is produced then check
1830 * that the required implementation support is there. Always expand; always
1831 * need 8 bits minimum, no palette and expanded tRNS.
1832 */
1833 png_set_expand(png_ptr);
1834
1835 /* Now check the format to see if it was modified. */
1836 {
1837 png_uint_32 base_format = png_image_format(png_ptr, info_ptr);
1838 png_uint_32 change = format ^ base_format;
John Bowler3615d032011-11-08 10:38:09 -06001839 png_fixed_point output_gamma;
1840 int mode; /* alpha mode */
John Bowler7875d532011-11-07 22:33:49 -06001841
John Bowler18c5cfa2011-11-16 14:26:34 -06001842 /* Do this first so that we have a record if rgb to gray is happening. */
1843 if (change & PNG_FORMAT_FLAG_COLOR)
1844 {
1845 /* gray<->color transformation required. */
1846 if (format & PNG_FORMAT_FLAG_COLOR)
1847 png_set_gray_to_rgb(png_ptr);
1848
1849 else
1850 {
1851 /* libpng can't do both rgb to gray and
1852 * background/pre-multiplication if there is also significant gamma
1853 * correction, because both operations require linear colors and
1854 * the code only supports one transform doing the gamma correction.
1855 * Handle this by doing the pre-multiplication or background
1856 * operation in this code, if necessary.
1857 *
1858 * TODO: fix this by rewriting pngrtran.c (!)
1859 *
1860 * For the moment (given that fixing this in pngrtran.c is an
1861 * enormous change) 'do_local_background' is used to indicate that
1862 * the problem exists.
1863 */
1864 if (base_format & PNG_FORMAT_FLAG_ALPHA)
1865 do_local_background = 1/*maybe*/;
1866
1867 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
1868 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
1869 }
1870
1871 change &= ~PNG_FORMAT_FLAG_COLOR;
1872 }
1873
John Bowler7875d532011-11-07 22:33:49 -06001874 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
1875 */
1876 {
John Bowler3615d032011-11-08 10:38:09 -06001877 png_fixed_point input_gamma_default;
John Bowler7875d532011-11-07 22:33:49 -06001878
1879 if (base_format & PNG_FORMAT_FLAG_LINEAR)
1880 input_gamma_default = PNG_GAMMA_LINEAR;
1881 else
1882 input_gamma_default = PNG_DEFAULT_sRGB;
1883
John Bowler18c5cfa2011-11-16 14:26:34 -06001884 /* Call png_set_alpha_mode to set the default for the input gamma; the
1885 * output gamma is set by a second call below.
1886 */
1887 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
1888 }
1889
1890 if (linear)
1891 {
1892 /* If there *is* an alpha channel in the input it must be multiplied
1893 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
1894 */
1895 if (base_format & PNG_FORMAT_FLAG_ALPHA)
John Bowler3615d032011-11-08 10:38:09 -06001896 mode = PNG_ALPHA_STANDARD; /* associated alpha */
John Bowler7875d532011-11-07 22:33:49 -06001897
1898 else
John Bowler7875d532011-11-07 22:33:49 -06001899 mode = PNG_ALPHA_PNG;
John Bowler18c5cfa2011-11-16 14:26:34 -06001900
1901 output_gamma = PNG_GAMMA_LINEAR;
1902 }
1903
1904 else
1905 {
1906 mode = PNG_ALPHA_PNG;
1907 output_gamma = PNG_DEFAULT_sRGB;
1908 }
1909
1910 /* If 'do_local_background' is set check for the presence of gamma
1911 * correction; this is part of the work-round for the libpng bug
1912 * described above.
1913 *
1914 * TODO: fix libpng and remove this.
1915 */
1916 if (do_local_background)
1917 {
1918 png_fixed_point gtest;
1919
1920 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
1921 * gamma correction, the screen gamma hasn't been set on png_struct
1922 * yet; it's set below. png_struct::gamma, however, is set to the
1923 * final value.
1924 */
1925 if (png_muldiv(&gtest, output_gamma, png_ptr->gamma, PNG_FP_1) &&
1926 !png_gamma_significant(gtest))
1927 do_local_background = 0;
1928
1929 else if (mode == PNG_ALPHA_STANDARD)
1930 {
1931 do_local_background = 2/*required*/;
1932 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
John Bowler7875d532011-11-07 22:33:49 -06001933 }
1934
John Bowler18c5cfa2011-11-16 14:26:34 -06001935 /* else leave as 1 for the checks below */
John Bowler3615d032011-11-08 10:38:09 -06001936 }
John Bowler7875d532011-11-07 22:33:49 -06001937
John Bowler3615d032011-11-08 10:38:09 -06001938 /* If the bit-depth changes then handle that here. */
1939 if (change & PNG_FORMAT_FLAG_LINEAR)
1940 {
1941 if (linear /*16-bit output*/)
1942 png_set_expand_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001943
John Bowler3615d032011-11-08 10:38:09 -06001944 else /* 8-bit output */
1945 png_set_scale_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001946
John Bowler3615d032011-11-08 10:38:09 -06001947 change &= ~PNG_FORMAT_FLAG_LINEAR;
John Bowler7875d532011-11-07 22:33:49 -06001948 }
1949
1950 /* Now the background/alpha channel changes. */
1951 if (change & PNG_FORMAT_FLAG_ALPHA)
1952 {
1953 /* Removing an alpha channel requires composition for the 8-bit
1954 * formats; for the 16-bit it is already done, above, by the
1955 * pre-multiplication and the channel just needs to be stripped.
1956 */
1957 if (base_format & PNG_FORMAT_FLAG_ALPHA)
1958 {
John Bowler18c5cfa2011-11-16 14:26:34 -06001959 /* If RGB->gray is happening the alpha channel must be left and the
1960 * operation completed locally.
1961 *
1962 * TODO: fix libpng and remove this.
1963 */
1964 if (do_local_background)
1965 do_local_background = 2/*required*/;
1966
John Bowler3615d032011-11-08 10:38:09 -06001967 /* 16-bit output: just remove the channel */
John Bowler18c5cfa2011-11-16 14:26:34 -06001968 else if (linear) /* compose on black (well, pre-multiply) */
John Bowler7875d532011-11-07 22:33:49 -06001969 png_set_strip_alpha(png_ptr);
1970
John Bowler3615d032011-11-08 10:38:09 -06001971 /* 8-bit output: do an appropriate compose */
John Bowler7875d532011-11-07 22:33:49 -06001972 else if (display->background != NULL)
1973 {
1974 png_color_16 c;
1975
1976 c.index = 0; /*unused*/
1977 c.red = display->background->red;
1978 c.green = display->background->green;
1979 c.blue = display->background->blue;
1980 c.gray = display->background->green;
1981
1982 /* This is always an 8-bit sRGB value, using the 'green' channel
1983 * for gray is much better than calculating the luminance here;
1984 * we can get off-by-one errors in that calculation relative to
1985 * the app expectations and that will show up in transparent
1986 * pixels.
1987 */
1988 png_set_background_fixed(png_ptr, &c,
1989 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
1990 0/*gamma: not used*/);
1991 }
1992
1993 else /* compose on row: implemented below. */
1994 {
1995 do_local_compose = 1;
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06001996 /* This leaves the alpha channel in the output, so it has to be
John Bowler7875d532011-11-07 22:33:49 -06001997 * removed by the code below. Set the encoding to the 'OPTIMIZE'
1998 * one so the code only has to hack on the pixels that require
1999 * composition.
2000 */
John Bowler3615d032011-11-08 10:38:09 -06002001 mode = PNG_ALPHA_OPTIMIZED;
John Bowler7875d532011-11-07 22:33:49 -06002002 }
2003 }
2004
2005 else /* output needs an alpha channel */
2006 {
2007 /* This is tricky because it happens before the swap operation has
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06002008 * been accomplished; however, the swap does *not* swap the added
John Bowlere6fb6912011-11-08 21:35:16 -06002009 * alpha channel (weird API), so it must be added in the correct
2010 * place.
John Bowler7875d532011-11-07 22:33:49 -06002011 */
John Bowler3615d032011-11-08 10:38:09 -06002012 png_uint_32 filler; /* opaque filler */
John Bowlere6fb6912011-11-08 21:35:16 -06002013 int where;
John Bowler3615d032011-11-08 10:38:09 -06002014
2015 if (linear)
2016 filler = 65535;
2017
2018 else
2019 filler = 255;
2020
John Bowlere6fb6912011-11-08 21:35:16 -06002021# ifdef PNG_FORMAT_AFIRST_SUPPORTED
2022 if (format & PNG_FORMAT_FLAG_AFIRST)
2023 {
2024 where = PNG_FILLER_BEFORE;
2025 change &= ~PNG_FORMAT_FLAG_AFIRST;
2026 }
2027
2028 else
2029# endif
2030 where = PNG_FILLER_AFTER;
2031
2032 png_set_add_alpha(png_ptr, filler, where);
John Bowler7875d532011-11-07 22:33:49 -06002033 }
2034
John Bowlere6fb6912011-11-08 21:35:16 -06002035 /* This stops the (irrelevant) call to swap_alpha below. */
John Bowler7875d532011-11-07 22:33:49 -06002036 change &= ~PNG_FORMAT_FLAG_ALPHA;
2037 }
2038
John Bowler3615d032011-11-08 10:38:09 -06002039 /* Now set the alpha mode correctly; this is always done, even if there is
2040 * no alpha channel in either the input or the output because it correctly
2041 * sets the output gamma.
2042 */
2043 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
2044
John Bowler7875d532011-11-07 22:33:49 -06002045# ifdef PNG_FORMAT_BGR_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06002046 if (change & PNG_FORMAT_FLAG_BGR)
John Bowler7875d532011-11-07 22:33:49 -06002047 {
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06002048 /* Check only the output format; PNG is never BGR; don't do this if
John Bowler7875d532011-11-07 22:33:49 -06002049 * the output is gray, but fix up the 'format' value in that case.
2050 */
2051 if (format & PNG_FORMAT_FLAG_COLOR)
2052 png_set_bgr(png_ptr);
2053
2054 else
2055 format &= ~PNG_FORMAT_FLAG_BGR;
2056
2057 change &= ~PNG_FORMAT_FLAG_BGR;
2058 }
2059# endif
2060
2061# ifdef PNG_FORMAT_AFIRST_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06002062 if (change & PNG_FORMAT_FLAG_AFIRST)
John Bowler7875d532011-11-07 22:33:49 -06002063 {
2064 /* Only relevant if there is an alpha channel - it's particularly
2065 * important to handle this correctly because do_local_compose may
2066 * be set above and then libpng will keep the alpha channel for this
2067 * code to remove.
2068 */
2069 if (format & PNG_FORMAT_FLAG_ALPHA)
John Bowler18c5cfa2011-11-16 14:26:34 -06002070 {
2071 /* Disable this if doing a local background,
2072 * TODO: remove this when local background is no longer required.
2073 */
2074 if (do_local_background != 2)
2075 png_set_swap_alpha(png_ptr);
2076 }
John Bowler7875d532011-11-07 22:33:49 -06002077
2078 else
2079 format &= ~PNG_FORMAT_FLAG_AFIRST;
2080
2081 change &= ~PNG_FORMAT_FLAG_AFIRST;
2082 }
2083# endif
2084
2085 /* If the *output* is 16-bit then we need to check for a byte-swap on this
2086 * architecture.
2087 */
2088 if (linear)
2089 {
2090 PNG_CONST png_uint_16 le = 0x0001;
2091
2092 if (*(png_const_bytep)&le)
2093 png_set_swap(png_ptr);
2094 }
2095
2096 /* If change is not now 0 some transformation is missing - error out. */
2097 if (change)
2098 png_error(png_ptr, "png_read_image: unsupported transformation");
2099 }
2100
John Bowler89c2f842011-11-16 12:04:39 -06002101# ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2102 /* Prepare the reader to ignore all recognized chunks whose data will not
2103 * be used, i.e., all chunks recognized by libpng except for those
2104 * involved in basic image reading:
2105 *
2106 * IHDR, PLTE, IDAT, IEND
2107 *
2108 * Or image data handling:
2109 *
2110 * tRNS, bKGD, gAMA, cHRM, sRGB, iCCP and sBIT.
2111 *
2112 * This provides a small performance improvement and eliminates any
2113 * potential vulnerability to security problems in the unused chunks.
2114 *
2115 * TODO: make it so that this is an explicit list to process, not a list
2116 * to ignore?
2117 */
2118 {
2119 static PNG_CONST png_byte chunks_to_ignore[] = {
2120 104, 73, 83, 84, '\0', /* hIST */
2121 105, 84, 88, 116, '\0', /* iTXt */
2122 111, 70, 70, 115, '\0', /* oFFs */
2123 112, 67, 65, 76, '\0', /* pCAL */
2124 112, 72, 89, 115, '\0', /* pHYs */
2125 115, 67, 65, 76, '\0', /* sCAL */
2126 115, 80, 76, 84, '\0', /* sPLT */
2127 116, 69, 88, 116, '\0', /* tEXt */
2128 116, 73, 77, 69, '\0', /* tIME */
2129 122, 84, 88, 116, '\0' /* zTXt */
2130 };
2131
2132 /* Ignore unknown chunks */
2133 png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */,
2134 NULL, 0);
2135
2136 /* Ignore known but unused chunks */
2137 png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */,
2138 chunks_to_ignore, (sizeof chunks_to_ignore)/5);
2139 }
2140# endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */
2141
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06002142 /* Update the 'info' structure and make sure the result is as required; first
John Bowler7875d532011-11-07 22:33:49 -06002143 * make sure to turn on the interlace handling if it will be required
2144 * (because it can't be turned on *after* the call to png_read_update_info!)
John Bowler18c5cfa2011-11-16 14:26:34 -06002145 *
2146 * TODO: remove the do_local_background fixup below.
John Bowler7875d532011-11-07 22:33:49 -06002147 */
John Bowler18c5cfa2011-11-16 14:26:34 -06002148 if (!do_local_compose && do_local_background != 2)
John Bowler7875d532011-11-07 22:33:49 -06002149 passes = png_set_interlace_handling(png_ptr);
2150
2151 png_read_update_info(png_ptr, info_ptr);
2152
2153 {
2154 png_uint_32 info_format = 0;
2155
2156 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
2157 info_format |= PNG_FORMAT_FLAG_COLOR;
2158
2159 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
2160 {
John Bowler18c5cfa2011-11-16 14:26:34 -06002161 /* do_local_compose removes this channel below. */
John Bowler7875d532011-11-07 22:33:49 -06002162 if (!do_local_compose)
John Bowler18c5cfa2011-11-16 14:26:34 -06002163 {
2164 /* do_local_background does the same if required. */
2165 if (do_local_background != 2 ||
2166 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
2167 info_format |= PNG_FORMAT_FLAG_ALPHA;
2168 }
John Bowler7875d532011-11-07 22:33:49 -06002169 }
2170
2171 else if (do_local_compose) /* internal error */
2172 png_error(png_ptr, "png_image_read: alpha channel lost");
2173
2174 if (info_ptr->bit_depth == 16)
2175 info_format |= PNG_FORMAT_FLAG_LINEAR;
2176
2177# ifdef PNG_FORMAT_BGR_SUPPORTED
2178 if (png_ptr->transformations & PNG_BGR)
2179 info_format |= PNG_FORMAT_FLAG_BGR;
2180# endif
2181
2182# ifdef PNG_FORMAT_AFIRST_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06002183 if (png_ptr->transformations & PNG_SWAP_ALPHA ||
2184 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
2185 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
John Bowler7875d532011-11-07 22:33:49 -06002186 info_format |= PNG_FORMAT_FLAG_AFIRST;
2187# endif
2188
2189 /* This is actually an internal error. */
2190 if (info_format != format)
2191 png_error(png_ptr, "png_read_image: invalid transformations");
2192 }
2193
2194 /* Now read the rows. If do_local_compose is set then it is necessary to use
2195 * a local row buffer. The output will be GA, RGBA or BGRA and must be
2196 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
2197 * display acts as a flag.
2198 */
2199 {
John Bowler4fa96a42011-11-16 16:39:16 -06002200 png_bytep first_row = png_voidcast(png_bytep, display->buffer);
John Bowler7875d532011-11-07 22:33:49 -06002201 ptrdiff_t row_bytes = display->row_stride;
2202
2203 if (linear)
2204 row_bytes *= sizeof (png_uint_16);
2205
2206 /* The following expression is designed to work correctly whether it gives
2207 * a signed or an unsigned result.
2208 */
2209 if (row_bytes < 0)
2210 first_row += (image->height-1) * (-row_bytes);
2211
2212 display->first_row = first_row;
2213 display->row_bytes = row_bytes;
2214 }
2215
2216 if (do_local_compose)
2217 {
2218 int result;
John Bowler4fa96a42011-11-16 16:39:16 -06002219 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
2220 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler7875d532011-11-07 22:33:49 -06002221
2222 display->local_row = row;
2223 result = png_safe_execute(image, png_image_read_composite, display);
2224 display->local_row = NULL;
2225 png_free(png_ptr, row);
2226
2227 return result;
2228 }
2229
John Bowler18c5cfa2011-11-16 14:26:34 -06002230 else if (do_local_background == 2)
2231 {
2232 int result;
John Bowler4fa96a42011-11-16 16:39:16 -06002233 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
2234 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler18c5cfa2011-11-16 14:26:34 -06002235
2236 display->local_row = row;
2237 result = png_safe_execute(image, png_image_read_background, display);
2238 display->local_row = NULL;
2239 png_free(png_ptr, row);
2240
2241 return result;
2242 }
2243
John Bowler7875d532011-11-07 22:33:49 -06002244 else
2245 {
2246 png_alloc_size_t row_bytes = display->row_bytes;
2247
2248 while (--passes >= 0)
2249 {
2250 png_uint_32 y = image->height;
2251 png_bytep row = display->first_row;
2252
2253 while (y-- > 0)
2254 {
2255 png_read_row(png_ptr, row, NULL);
2256 row += row_bytes;
2257 }
2258 }
2259
2260 return 1;
2261 }
2262}
2263
2264int PNGAPI
2265png_image_finish_read(png_imagep image, png_colorp background, void *buffer,
2266 png_int_32 row_stride)
2267{
2268 if (image != NULL)
2269 {
2270 png_uint_32 check;
2271
John Bowler3706d732011-11-21 10:28:06 -06002272 if (row_stride == 0)
2273 row_stride = PNG_IMAGE_ROW_STRIDE(*image);
2274
John Bowler7875d532011-11-07 22:33:49 -06002275 if (row_stride < 0)
2276 check = -row_stride;
2277
2278 else
2279 check = row_stride;
2280
2281 if (buffer != NULL && check >= PNG_IMAGE_ROW_STRIDE(*image))
2282 {
2283 int result;
2284 png_image_read_control display;
2285
John Bowlerbaeb6d12011-11-26 18:21:02 -06002286 png_memset(&display, 0, sizeof display);
John Bowler7875d532011-11-07 22:33:49 -06002287 display.image = image;
2288 display.buffer = buffer;
2289 display.row_stride = row_stride;
2290 display.background = background;
2291 display.local_row = NULL;
2292 result = png_safe_execute(image, png_image_read_end, &display);
2293 png_image_free(image);
2294 return result;
2295 }
2296
2297 else
2298 return png_image_error(image,
2299 "png_image_finish_read: invalid argument");
2300 }
2301
2302 return 0;
2303}
2304
2305#endif /* PNG_SIMPLIFIED_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06002306#endif /* PNG_READ_SUPPORTED */