blob: 7ff4d08a48c59065ee6e90490059148aa8d91d88 [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 */
John Bowler5bc90382012-01-23 22:43:22 -06001162/* Arguments to png_image_finish_read: */
1163
1164/* Encoding of PNG data (used by the color-map code) */
1165# define E_NOTSET 0 /* File encoding not yet known */
1166# define E_sRGB 1 /* 8-bit encoded to sRGB gamma */
1167# define E_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1168# define E_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1169# define E_LINEAR8 4 /* 8-bit linear: only from a file value */
1170
1171/* Color-map processing: after libpng has run on the PNG image further
1172 * processing may be needed to conver the data to color-map indicies.
1173 */
1174#define PNG_CMAP_NONE 0
1175#define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1176#define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1177#define PNG_CMAP_RGB 3 /* Process RGB data */
1178#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1179
1180/* The following document where the background is for each processing case. */
1181#define PNG_CMAP_NONE_BACKGROUND 256
1182#define PNG_CMAP_GA_BACKGROUND 231
1183#define PNG_CMAP_TRANS_BACKGROUND 254
1184#define PNG_CMAP_RGB_BACKGROUND 256
1185#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1186
1187typedef struct
1188{
1189 /* Arguments: */
1190 png_imagep image;
1191 png_voidp buffer;
1192 png_int_32 row_stride;
1193 png_voidp colormap;
1194 png_colorp background;
1195 /* Local variables: */
1196 png_bytep local_row;
1197 png_bytep first_row;
1198 ptrdiff_t row_bytes; /* step between rows */
1199 int file_encoding; /* E_ values above */
1200 png_fixed_point gamma_to_linear; /* For E_FILE, reciprocal of gamma */
1201 int colormap_processing; /* PNG_CMAP_ values above */
1202} png_image_read_control;
1203
John Bowler7875d532011-11-07 22:33:49 -06001204/* Do all the *safe* initialization - 'safe' means that png_error won't be
John Bowler18c5cfa2011-11-16 14:26:34 -06001205 * called, so setting up the jmp_buf is not required. This means that anything
1206 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1207 * instead so that control is returned safely back to this routine.
John Bowler7875d532011-11-07 22:33:49 -06001208 */
1209static int
1210png_image_read_init(png_imagep image)
1211{
John Bowler5bc90382012-01-23 22:43:22 -06001212 if (image->opaque == NULL)
1213 {
1214 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
John Bowler7875d532011-11-07 22:33:49 -06001215 png_safe_error, png_safe_warning);
1216
John Bowler5bc90382012-01-23 22:43:22 -06001217 /* And set the rest of the structure to NULL to ensure that the various
1218 * fields are consistent.
1219 */
1220 memset(image, 0, sizeof *image);
1221 image->version = PNG_IMAGE_VERSION;
John Bowler7875d532011-11-07 22:33:49 -06001222
John Bowler5bc90382012-01-23 22:43:22 -06001223 if (png_ptr != NULL)
John Bowler7875d532011-11-07 22:33:49 -06001224 {
John Bowler5bc90382012-01-23 22:43:22 -06001225 png_infop info_ptr = png_create_info_struct(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001226
John Bowler5bc90382012-01-23 22:43:22 -06001227 if (info_ptr != NULL)
John Bowler7875d532011-11-07 22:33:49 -06001228 {
John Bowler5bc90382012-01-23 22:43:22 -06001229 png_controlp control = png_voidcast(png_controlp,
1230 png_malloc_warn(png_ptr, sizeof *control));
John Bowler7875d532011-11-07 22:33:49 -06001231
John Bowler5bc90382012-01-23 22:43:22 -06001232 if (control != NULL)
1233 {
1234 png_memset(control, 0, sizeof *control);
John Bowler7875d532011-11-07 22:33:49 -06001235
John Bowler5bc90382012-01-23 22:43:22 -06001236 control->png_ptr = png_ptr;
1237 control->info_ptr = info_ptr;
1238 control->for_write = 0;
1239
1240 image->opaque = control;
1241 return 1;
1242 }
1243
1244 /* Error clean up */
1245 png_destroy_info_struct(png_ptr, &info_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001246 }
1247
John Bowler5bc90382012-01-23 22:43:22 -06001248 png_destroy_read_struct(&png_ptr, NULL, NULL);
John Bowler7875d532011-11-07 22:33:49 -06001249 }
1250
John Bowler5bc90382012-01-23 22:43:22 -06001251 return png_image_error(image, "png_image_read: out of memory");
John Bowler7875d532011-11-07 22:33:49 -06001252 }
1253
John Bowler5bc90382012-01-23 22:43:22 -06001254 return png_image_error(image, "png_image_read: opaque pointer not NULL");
John Bowler7875d532011-11-07 22:33:49 -06001255}
1256
1257/* Utility to find the base format of a PNG file from a png_struct. */
1258static png_uint_32
John Bowler5bc90382012-01-23 22:43:22 -06001259png_image_format(png_structrp png_ptr)
John Bowler7875d532011-11-07 22:33:49 -06001260{
1261 png_uint_32 format = 0;
1262
1263 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1264 format |= PNG_FORMAT_FLAG_COLOR;
1265
1266 if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1267 format |= PNG_FORMAT_FLAG_ALPHA;
1268
John Bowler5bc90382012-01-23 22:43:22 -06001269 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1270 * sets the png_struct fields; that's all we are interested in here. The
1271 * precise interaction with an app call to png_set_tRNS and PNG file reading
1272 * is unclear.
1273 */
1274 else if (png_ptr->num_trans > 0)
John Bowler7875d532011-11-07 22:33:49 -06001275 format |= PNG_FORMAT_FLAG_ALPHA;
1276
1277 if (png_ptr->bit_depth == 16)
1278 format |= PNG_FORMAT_FLAG_LINEAR;
1279
John Bowler5bc90382012-01-23 22:43:22 -06001280 if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE)
1281 format |= PNG_FORMAT_FLAG_COLORMAP;
1282
John Bowler7875d532011-11-07 22:33:49 -06001283 return format;
1284}
1285
John Bowler5bc90382012-01-23 22:43:22 -06001286/* Is the given gamma significantly different from sRGB? The test is the same
1287 * one used in pngrtran.c when deciding whether to do gamma correction. The
1288 * arithmetic optimizes the division by using the fact that the inverse of the
1289 * file sRGB gamma is 2.2
1290 */
1291static int
1292png_gamma_not_sRGB(png_fixed_point g)
1293{
1294 if (g < PNG_FP_1)
1295 {
1296 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
1297 if (g == 0)
1298 return 0;
1299
1300 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1301 }
1302
1303 return 1;
1304}
1305
John Bowler7875d532011-11-07 22:33:49 -06001306/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1307 * header and fill in all the information. This is executed in a safe context,
1308 * unlike the init routine above.
1309 */
1310static int
1311png_image_read_header(png_voidp argument)
1312{
John Bowler4fa96a42011-11-16 16:39:16 -06001313 png_imagep image = png_voidcast(png_imagep, argument);
John Bowler5d567862011-12-24 09:12:00 -06001314 png_structrp png_ptr = image->opaque->png_ptr;
1315 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001316
1317 png_read_info(png_ptr, info_ptr);
1318
1319 /* Do this the fast way; just read directly out of png_struct. */
1320 image->width = png_ptr->width;
1321 image->height = png_ptr->height;
1322
1323 {
John Bowler5bc90382012-01-23 22:43:22 -06001324 png_uint_32 format = png_image_format(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001325
1326 image->format = format;
John Bowler04336ba2012-01-16 07:48:36 -06001327
John Bowler7875d532011-11-07 22:33:49 -06001328 /* Now try to work out whether the color data does not match sRGB. */
1329 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 &&
1330 (info_ptr->valid & PNG_INFO_sRGB) == 0)
1331 {
1332 /* gamma is irrelevant because libpng does gamma correction, what
1333 * matters is if the cHRM chunk doesn't match or, in the absence of
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06001334 * cRHM, if the iCCP profile appears to have different end points.
John Bowler7875d532011-11-07 22:33:49 -06001335 */
1336 if (info_ptr->valid & PNG_INFO_cHRM)
1337 {
1338 /* TODO: this is a copy'n'paste from pngrutil.c, make a common
1339 * checking function. This checks for a 1% error.
1340 */
1341 /* The cHRM chunk is used in preference to iCCP */
1342 if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) ||
1343 PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) ||
1344 PNG_OUT_OF_RANGE(info_ptr->x_red, 64000, 1000) ||
1345 PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) ||
1346 PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) ||
1347 PNG_OUT_OF_RANGE(info_ptr->y_green, 60000, 1000) ||
1348 PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) ||
1349 PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000))
1350 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1351 }
1352
1353 else if (info_ptr->valid & PNG_INFO_iCCP)
1354 {
Glenn Randers-Pehrson6d8e0902011-11-11 18:41:59 -06001355# if 0
John Bowler04336ba2012-01-16 07:48:36 -06001356 /* TODO: IMPLEMENT THIS! */
John Bowler7875d532011-11-07 22:33:49 -06001357 /* Here if we just have an iCCP chunk. */
1358 if (!png_iCCP_is_sRGB(png_ptr, info_ptr))
1359# endif
1360 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1361 }
1362 }
1363 }
1364
John Bowler5bc90382012-01-23 22:43:22 -06001365 /* We need the maximum number of entries regardless of the format the
1366 * application sets here.
1367 */
1368 {
1369 png_uint_32 cmap_entries;
1370
1371 switch (png_ptr->color_type)
1372 {
1373 case PNG_COLOR_TYPE_GRAY:
1374 cmap_entries = 1U << png_ptr->bit_depth;
1375 break;
1376
1377 case PNG_COLOR_TYPE_PALETTE:
1378 cmap_entries = png_ptr->num_palette;
1379 break;
1380
1381 default:
1382 cmap_entries = 256;
1383 break;
1384 }
1385
1386 if (cmap_entries > 256)
1387 cmap_entries = 256;
1388
1389 image->colormap_entries = cmap_entries;
1390 }
1391
John Bowler7875d532011-11-07 22:33:49 -06001392 return 1;
1393}
1394
1395#ifdef PNG_STDIO_SUPPORTED
1396int PNGAPI
1397png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1398{
John Bowler5bc90382012-01-23 22:43:22 -06001399 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001400 {
1401 if (file != NULL)
1402 {
1403 if (png_image_read_init(image))
1404 {
1405 /* This is slightly evil, but png_init_io doesn't do anything other
1406 * than this and we haven't changed the standard IO functions so
1407 * this saves a 'safe' function.
1408 */
1409 image->opaque->png_ptr->io_ptr = file;
1410 return png_safe_execute(image, png_image_read_header, image);
1411 }
1412 }
1413
1414 else
1415 return png_image_error(image,
1416 "png_image_begin_read_from_stdio: invalid argument");
1417 }
1418
1419 return 0;
1420}
1421
1422int PNGAPI
1423png_image_begin_read_from_file(png_imagep image, const char *file_name)
1424{
John Bowler5bc90382012-01-23 22:43:22 -06001425 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001426 {
1427 if (file_name != NULL)
1428 {
1429 FILE *fp = fopen(file_name, "rb");
1430
1431 if (fp != NULL)
1432 {
1433 if (png_image_read_init(image))
1434 {
1435 image->opaque->png_ptr->io_ptr = fp;
1436 image->opaque->owned_file = 1;
1437 return png_safe_execute(image, png_image_read_header, image);
1438 }
1439
1440 /* Clean up: just the opened file. */
1441 (void)fclose(fp);
1442 }
1443
1444 else
1445 return png_image_error(image, strerror(errno));
1446 }
1447
1448 else
1449 return png_image_error(image,
1450 "png_image_begin_read_from_file: invalid argument");
1451 }
1452
1453 return 0;
1454}
1455#endif /* PNG_STDIO_SUPPORTED */
1456
1457static void PNGCBAPI
1458png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
1459{
1460 if (png_ptr != NULL)
1461 {
John Bowler4fa96a42011-11-16 16:39:16 -06001462 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001463 if (image != NULL)
1464 {
1465 png_controlp cp = image->opaque;
1466 if (cp != NULL)
1467 {
1468 png_const_bytep memory = cp->memory;
1469 png_size_t size = cp->size;
1470
1471 if (memory != NULL && size >= need)
1472 {
John Bowlerbaeb6d12011-11-26 18:21:02 -06001473 png_memcpy(out, memory, need);
John Bowler7875d532011-11-07 22:33:49 -06001474 cp->memory = memory + need;
1475 cp->size = size - need;
1476 return;
1477 }
1478
1479 png_error(png_ptr, "read beyond end of data");
1480 }
1481 }
1482
1483 png_error(png_ptr, "invalid memory read");
1484 }
1485}
1486
1487int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1488 png_const_voidp memory, png_size_t size)
1489{
John Bowler5bc90382012-01-23 22:43:22 -06001490 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001491 {
1492 if (memory != NULL && size > 0)
1493 {
1494 if (png_image_read_init(image))
1495 {
1496 /* Now set the IO functions to read from the memory buffer and
1497 * store it into io_ptr. Again do this in-place to avoid calling a
1498 * libpng function that requires error handling.
1499 */
John Bowler4fa96a42011-11-16 16:39:16 -06001500 image->opaque->memory = png_voidcast(png_const_bytep, memory);
John Bowler7875d532011-11-07 22:33:49 -06001501 image->opaque->size = size;
1502 image->opaque->png_ptr->io_ptr = image;
1503 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1504
1505 return png_safe_execute(image, png_image_read_header, image);
1506 }
1507 }
1508
1509 else
1510 return png_image_error(image,
1511 "png_image_begin_read_from_memory: invalid argument");
1512 }
1513
1514 return 0;
1515}
1516
John Bowler5bc90382012-01-23 22:43:22 -06001517/* Utility function to skip chunks that are not used by the simplified image
1518 * read functions and an appropriate macro to call it.
1519 */
1520#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1521static void
1522png_image_skip_unused_chunks(png_structrp png_ptr)
John Bowler04336ba2012-01-16 07:48:36 -06001523{
John Bowler5bc90382012-01-23 22:43:22 -06001524 /* Prepare the reader to ignore all recognized chunks whose data will not
1525 * be used, i.e., all chunks recognized by libpng except for those
1526 * involved in basic image reading:
1527 *
1528 * IHDR, PLTE, IDAT, IEND
1529 *
1530 * Or image data handling:
1531 *
1532 * tRNS, bKGD, gAMA, cHRM, sRGB, iCCP and sBIT.
1533 *
1534 * This provides a small performance improvement and eliminates any
1535 * potential vulnerability to security problems in the unused chunks.
1536 *
1537 * TODO: make it so that this is an explicit list to process, not a list
1538 * to ignore?
1539 */
John Bowler04336ba2012-01-16 07:48:36 -06001540 {
John Bowler5bc90382012-01-23 22:43:22 -06001541 static PNG_CONST png_byte chunks_to_ignore[] = {
1542 104, 73, 83, 84, '\0', /* hIST */
1543 105, 84, 88, 116, '\0', /* iTXt */
1544 111, 70, 70, 115, '\0', /* oFFs */
1545 112, 67, 65, 76, '\0', /* pCAL */
1546 112, 72, 89, 115, '\0', /* pHYs */
1547 115, 67, 65, 76, '\0', /* sCAL */
1548 115, 80, 76, 84, '\0', /* sPLT */
1549 116, 69, 88, 116, '\0', /* tEXt */
1550 116, 73, 77, 69, '\0', /* tIME */
1551 122, 84, 88, 116, '\0' /* zTXt */
1552 };
1553
1554 /* Ignore unknown chunks */
1555 png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */,
1556 NULL, 0);
1557
1558 /* Ignore known but unused chunks */
1559 png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */,
1560 chunks_to_ignore, (sizeof chunks_to_ignore)/5);
1561 }
1562}
1563
1564# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1565#else
1566# define PNG_SKIP_CHUNKS(p) ((void)0)
1567#endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */
1568
1569/* The following macro gives the exact rounded answer for all values in the
1570 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1571 * the correct numbers 0..5
1572 */
1573#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1574
1575/* Utility functions to make particular color-maps */
1576static void
1577set_file_encoding(png_image_read_control *display)
1578{
1579 png_fixed_point g = display->image->opaque->png_ptr->gamma;
1580 if (png_gamma_significant(g))
1581 {
1582 if (png_gamma_not_sRGB(g))
John Bowler04336ba2012-01-16 07:48:36 -06001583 {
John Bowler5bc90382012-01-23 22:43:22 -06001584 display->file_encoding = E_FILE;
1585 display->gamma_to_linear = png_reciprocal(g);
John Bowler04336ba2012-01-16 07:48:36 -06001586 }
1587
1588 else
John Bowler5bc90382012-01-23 22:43:22 -06001589 display->file_encoding = E_sRGB;
John Bowler04336ba2012-01-16 07:48:36 -06001590 }
1591
John Bowler5bc90382012-01-23 22:43:22 -06001592 else
1593 display->file_encoding = E_LINEAR8;
John Bowler04336ba2012-01-16 07:48:36 -06001594}
1595
John Bowler5bc90382012-01-23 22:43:22 -06001596static unsigned int
1597decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
John Bowler7875d532011-11-07 22:33:49 -06001598{
John Bowler5bc90382012-01-23 22:43:22 -06001599 if (encoding == E_FILE) /* double check */
1600 encoding = display->file_encoding;
John Bowler7875d532011-11-07 22:33:49 -06001601
John Bowler5bc90382012-01-23 22:43:22 -06001602 if (encoding == E_NOTSET) /* must be the file encoding */
1603 {
1604 set_file_encoding(display);
1605 encoding = display->file_encoding;
1606 }
1607
1608 switch (encoding)
1609 {
1610 case E_FILE:
1611 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
1612 break;
1613
1614 case E_sRGB:
1615 value = png_sRGB_table[value];
1616 break;
1617
1618 case E_LINEAR:
1619 break;
1620
1621 case E_LINEAR8:
1622 value *= 257;
1623 break;
1624
1625 default:
1626 png_error(display->image->opaque->png_ptr,
1627 "unexpected encoding (internal error)");
1628 break;
1629 }
1630
1631 return value;
1632}
1633
1634static png_uint_32
1635png_colormap_compose(png_image_read_control *display,
1636 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
1637 png_uint_32 background, int encoding)
1638{
1639 /* The file value is composed on the background, the background has the given
1640 * encoding and so does the result, the file is encoded with E_FILE and the
1641 * file and alpha are 8-bit values. The (output) encoding will always be
1642 * E_LINEAR or E_sRGB.
1643 */
1644 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
1645 png_uint_32 b = decode_gamma(display, background, encoding);
1646
1647 /* The alpha is always an 8-bit value (it comes from the palette), the value
1648 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1649 */
1650 f = f * alpha + b * (255-alpha);
1651
1652 if (encoding == E_LINEAR)
1653 {
1654 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1655 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1656 */
1657 f *= 257; /* Now scaled by 65535 */
1658 f += f >> 16;
1659 f = (f+32768) >> 16;
1660 }
1661
1662 else /* E_sRGB */
1663 f = PNG_sRGB_FROM_LINEAR(f);
1664
1665 return f;
1666}
1667
1668/* NOTE: E_LINEAR values to this routine must be 16-bit, but E_FILE values must
1669 * be 8-bit.
1670 */
1671static void
1672png_create_colormap_entry(png_image_read_control *display,
1673 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
1674 png_uint_32 alpha, int encoding)
1675{
1676 png_imagep image = display->image;
1677 const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ?
1678 E_LINEAR : E_sRGB;
1679 const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
1680 (red != green || green != blue);
1681
1682 if (ip > 255)
1683 png_error(image->opaque->png_ptr, "color-map index out of range");
1684
1685 /* Update the cache with whether the file gamma is significantly different
1686 * from sRGB.
1687 */
1688 if (encoding == E_FILE)
1689 {
1690 if (display->file_encoding == E_NOTSET)
1691 set_file_encoding(display);
1692
1693 /* Note that the cached value may be E_FILE too, but if it is then the
1694 * gamma_to_linear member has been set.
1695 */
1696 encoding = display->file_encoding;
1697 }
1698
1699 if (encoding == E_FILE)
1700 {
1701 png_fixed_point g = display->gamma_to_linear;
1702
1703 red = png_gamma_16bit_correct(red*257, g);
1704 green = png_gamma_16bit_correct(green*257, g);
1705 blue = png_gamma_16bit_correct(blue*257, g);
1706
1707 if (convert_to_Y || output_encoding == E_LINEAR)
1708 {
1709 alpha *= 257;
1710 encoding = E_LINEAR;
1711 }
1712
1713 else
1714 {
1715 red = PNG_sRGB_FROM_LINEAR(red * 255);
1716 green = PNG_sRGB_FROM_LINEAR(green * 255);
1717 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1718 encoding = E_sRGB;
1719 }
1720 }
1721
1722 else if (encoding == E_LINEAR8)
1723 {
1724 /* This encoding occurs quite frequently in test cases because PngSuite
1725 * includes a gAMA 1.0 chunk with most images.
1726 */
1727 red *= 257;
1728 green *= 257;
1729 blue *= 257;
1730 alpha *= 257;
1731 encoding = E_LINEAR;
1732 }
1733
1734 else if (encoding == E_sRGB && (convert_to_Y || output_encoding == E_LINEAR))
1735 {
1736 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1737 * linear.
1738 */
1739 red = png_sRGB_table[red];
1740 green = png_sRGB_table[green];
1741 blue = png_sRGB_table[blue];
1742 alpha *= 257;
1743 encoding = E_LINEAR;
1744 }
1745
1746 /* This is set if the color isn't gray but the output is. */
1747 if (encoding == E_LINEAR)
1748 {
1749 if (convert_to_Y)
1750 {
1751 /* NOTE: these values are copied from png_do_rgb_to_gray */
1752 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
1753 (png_uint_32)2366 * blue;
1754
1755 if (output_encoding == E_LINEAR)
1756 y = (y + 16384) >> 15;
1757
1758 else
1759 {
1760 /* y is scaled by 32768, we need it scaled by 255: */
1761 y = (y + 128) >> 8;
1762 y *= 255;
1763 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
1764 encoding = E_sRGB;
1765 }
1766
1767 blue = red = green = y;
1768 }
1769
1770 else if (output_encoding == E_sRGB)
1771 {
1772 red = PNG_sRGB_FROM_LINEAR(red * 255);
1773 green = PNG_sRGB_FROM_LINEAR(green * 255);
1774 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1775 alpha = PNG_DIV257(alpha);
1776 encoding = E_sRGB;
1777 }
1778 }
1779
1780 if (encoding != output_encoding)
1781 png_error(image->opaque->png_ptr, "bad encoding (internal error)");
1782
1783 /* Store the value. */
1784 {
1785# ifdef PNG_FORMAT_BGR_SUPPORTED
1786 const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1787 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
1788# else
1789# define afirst 0
1790# endif
1791# ifdef PNG_FORMAT_BGR_SUPPORTED
1792 const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
1793# else
1794# define bgr 0
1795# endif
1796
1797 if (output_encoding == E_LINEAR)
1798 {
1799 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
1800
1801 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1802
1803 /* The linear 16-bit values must be pre-multiplied by the alpha channel
1804 * value, if less than 65535 (this is, effectively, composite on black
1805 * if the alpha channel is removed.)
1806 */
1807 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1808 {
1809 case 4:
1810 entry[afirst ? 0 : 3] = (png_uint_16)alpha;
1811 /* FALL THROUGH */
1812
1813 case 3:
1814 if (alpha < 65535)
1815 {
1816 if (alpha > 0)
1817 {
1818 blue = (blue * alpha + 32767U)/65535U;
1819 green = (green * alpha + 32767U)/65535U;
1820 red = (red * alpha + 32767U)/65535U;
1821 }
1822
1823 else
1824 red = green = blue = 0;
1825 }
1826 entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
1827 entry[afirst + 1] = (png_uint_16)green;
1828 entry[afirst + bgr] = (png_uint_16)red;
1829 break;
1830
1831 case 2:
1832 entry[1 ^ afirst] = (png_uint_16)alpha;
1833 /* FALL THROUGH */
1834
1835 case 1:
1836 if (alpha < 65535)
1837 {
1838 if (alpha > 0)
1839 green = (green * alpha + 32767U)/65535U;
1840
1841 else
1842 green = 0;
1843 }
1844 entry[afirst] = (png_uint_16)green;
1845 break;
1846
1847 default:
1848 break;
1849 }
1850 }
1851
1852 else /* output encoding is E_sRGB */
1853 {
1854 png_bytep entry = png_voidcast(png_bytep, display->colormap);
1855
1856 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1857
1858 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1859 {
1860 case 4:
1861 entry[afirst ? 0 : 3] = (png_byte)alpha;
1862 case 3:
1863 entry[afirst + (2 ^ bgr)] = (png_byte)blue;
1864 entry[afirst + 1] = (png_byte)green;
1865 entry[afirst + bgr] = (png_byte)red;
1866 break;
1867
1868 case 2:
1869 entry[1 ^ afirst] = (png_byte)alpha;
1870 case 1:
1871 entry[afirst] = (png_byte)green;
1872 break;
1873
1874 default:
1875 break;
1876 }
1877 }
1878
1879# ifdef afirst
1880# undef afirst
1881# endif
1882# ifdef bgr
1883# undef bgr
1884# endif
1885 }
1886}
1887
John Bowler7875d532011-11-07 22:33:49 -06001888static int
John Bowler5bc90382012-01-23 22:43:22 -06001889make_gray_colormap(png_image_read_control *display)
1890{
1891 unsigned int i;
1892
1893 for (i=0; i<256; ++i)
1894 png_create_colormap_entry(display, i, i, i, i, 255, E_sRGB);
1895
1896 return i;
1897}
1898#define PNG_GRAY_COLORMAP_ENTRIES 256
1899
1900static int
1901make_ga_colormap(png_image_read_control *display)
1902{
1903 unsigned int i, a;
1904
1905 /* Alpha is retained, the output will be a color-map with entries
1906 * selected by six levels of alpha. One transparent entry, 6 gray
1907 * levels for all the intermediate alpha values, leaving 230 entries
1908 * for the opaque grays. The color-map entries are the six values
1909 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
1910 * relevant entry.
1911 *
1912 * if (alpha > 229) // opaque
1913 * {
1914 * // The 231 entries are selected to make the math below work:
1915 * base = 0;
1916 * entry = (231 * gray + 128) >> 8;
1917 * }
1918 * else if (alpha < 26) // transparent
1919 * {
1920 * base = 231;
1921 * entry = 0;
1922 * }
1923 * else // partially opaque
1924 * {
1925 * base = 226 + 6 * PNG_DIV51(alpha);
1926 * entry = PNG_DIV51(gray);
1927 * }
1928 */
1929 i = 0;
1930 while (i < 231)
1931 {
1932 unsigned int gray = (i * 256 + 115) / 231;
1933 png_create_colormap_entry(display, i++, gray, gray, gray, 255, E_sRGB);
1934 }
1935
1936 /* 255 is used here for the component values for consistency with the code
1937 * that undoes premultiplication in pngwrite.c.
1938 */
1939 png_create_colormap_entry(display, i++, 255, 255, 255, 0, E_sRGB);
1940
1941 for (a=1; a<5; ++a)
1942 {
1943 unsigned int g;
1944
1945 for (g=0; g<6; ++g)
1946 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
1947 E_sRGB);
1948 }
1949
1950 return i;
1951}
1952
1953#define PNG_GA_COLORMAP_ENTRIES 256
1954
1955static int
1956make_rgb_colormap(png_image_read_control *display)
1957{
1958 unsigned int i, r;
1959
1960 /* Build a 6x6x6 opaque RGB cube */
1961 for (i=r=0; r<6; ++r)
1962 {
1963 unsigned int g;
1964
1965 for (g=0; g<6; ++g)
1966 {
1967 unsigned int b;
1968
1969 for (b=0; b<6; ++b)
1970 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
1971 E_sRGB);
1972 }
1973 }
1974
1975 return i;
1976}
1977
1978#define PNG_RGB_COLORMAP_ENTRIES 216
1979
1980/* Return a palette index to the above palette given three 8-bit sRGB values. */
1981#define PNG_RGB_INDEX(r,g,b) \
1982 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
1983
1984static int
1985png_image_read_colormap(png_voidp argument)
1986{
1987 png_image_read_control *display =
1988 png_voidcast(png_image_read_control*, argument);
1989 const png_imagep image = display->image;
1990
1991 const png_structrp png_ptr = image->opaque->png_ptr;
1992 const png_uint_32 output_format = image->format;
1993 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ?
1994 E_LINEAR : E_sRGB;
1995
1996 unsigned int cmap_entries;
1997 unsigned int output_processing; /* Output processing option */
1998 unsigned int data_encoding = E_NOTSET; /* Encoding libpng must produce */
1999
2000 /* Background information; the background color and the index of this color
2001 * in the color-map if it exists (else 256).
2002 */
2003 unsigned int background_index = 256;
2004 png_uint_32 back_r, back_g, back_b;
2005
2006 /* Flags to accumulate things that need to be done to the input. */
2007 int expand_tRNS = 0;
2008
2009 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
2010 * very difficult to do, the results look awful, and it is difficult to see
2011 * what possible use it is because the application can't control the
2012 * color-map.
2013 */
2014 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
2015 png_ptr->num_trans > 0) /* alpha in input */ &&
2016 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
2017 {
2018 if (output_encoding == E_LINEAR) /* compose on black */
2019 back_b = back_g = back_r = 0;
2020
2021 else if (display->background == NULL /* no way to remove it */)
2022 png_error(png_ptr,
2023 "a background color must be supplied to remove alpha/transparency");
2024
2025 /* Get a copy of the background color (this avoids repeating the checks
2026 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
2027 * output format.
2028 */
2029 else
2030 {
2031 back_g = display->background->green;
2032 if (output_format & PNG_FORMAT_FLAG_COLOR)
2033 {
2034 back_r = display->background->red;
2035 back_b = display->background->blue;
2036 }
2037 else
2038 back_b = back_r = back_g;
2039 }
2040 }
2041
2042 else if (output_encoding == E_LINEAR)
2043 back_b = back_r = back_g = 65535;
2044
2045 else
2046 back_b = back_r = back_g = 255;
2047
2048 /* Decide what to do based on the PNG color type of the input data. The
2049 * utility function png_create_colormap_entry deals with most aspects of the
2050 * output transformations; this code works out how to produce bytes of
2051 * color-map entries from the original format.
2052 */
2053 switch (png_ptr->color_type)
2054 {
2055 case PNG_COLOR_TYPE_GRAY:
2056 if (png_ptr->bit_depth <= 8)
2057 {
2058 /* There at most 256 colors in the output, regardless of
2059 * transparency.
2060 */
2061 unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
2062
2063 cmap_entries = 1U << png_ptr->bit_depth;
2064 if (cmap_entries > image->colormap_entries)
2065 png_error(png_ptr, "gray[8] color-map: too few entries");
2066
2067 step = 255 / (cmap_entries - 1);
2068 output_processing = PNG_CMAP_NONE;
2069
2070 /* If there is a tRNS chunk then this either selects a transparent
2071 * value or, if the output has no alpha, the background color.
2072 */
2073 if (png_ptr->num_trans > 0)
2074 {
2075 trans = png_ptr->trans_color.gray;
2076
2077 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
2078 back_alpha = output_encoding == E_LINEAR ? 65535 : 255;
2079 }
2080
2081 /* png_create_colormap_entry just takes an RGBA and writes the
2082 * corresponding color-map entry using the format from 'image',
2083 * including the required convertion to sRGB or linear as
2084 * appropriate. The input values are always either sRGB (if the
2085 * gamma correction flag is 0) or 0..255 scaled file encoded values
2086 * (if the function must gamma correct them).
2087 */
2088 for (i=val=0; i<cmap_entries; ++i, val += step)
2089 {
2090 /* 'i' is a file value. While this will result in duplicated
2091 * entries for 8-bit non-sRGB encoded files it is necessary to
2092 * have non-gamma corrected values to do tRNS handling.
2093 */
2094 if (i != trans)
2095 png_create_colormap_entry(display, i, val, val, val, 255,
2096 E_FILE/*8-bit with file gamma*/);
2097
2098 /* Else this entry is transparent. The colors don't matter if
2099 * there is an alpha channel (back_alpha == 0), but it does no
2100 * harm to pass them in; the values are not set above so this
2101 * passes in white.
2102 *
2103 * NOTE: this preserves the full precision of the application
2104 * supplied background color when it is used.
2105 */
2106 else
2107 png_create_colormap_entry(display, i, back_r, back_g, back_b,
2108 back_alpha, output_encoding);
2109 }
2110
2111 /* We need libpng to preserve the original encoding. */
2112 data_encoding = E_FILE;
2113
2114 /* The rows from libpng, while technically gray values, are now also
2115 * color-map indicies; however, they may need to be expanded to 1
2116 * byte per pixel. This is what png_set_packing does (i.e., it
2117 * unpacks the bit values into bytes.)
2118 */
2119 if (png_ptr->bit_depth < 8)
2120 png_set_packing(png_ptr);
2121 }
2122
2123 else /* bit depth is 16 */
2124 {
2125 /* The 16-bit input values can be converted directly to 8-bit gamma
2126 * encoded values; however, if a tRNS chunk is present 257 color-map
2127 * entries are required. This means that the extra entry requires
2128 * special processing; add an alpha channel, sacrifice gray level
2129 * 254 and convert transparent (alpha==0) entries to that.
2130 *
2131 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2132 * same time to minimize quality loss. If a tRNS chunk is present
2133 * this means libpng must handle it too; otherwise it is impossible
2134 * to do the exact match on the 16-bit value.
2135 *
2136 * If the output has no alpha channel *and* the background color is
2137 * gray then it is possible to let libpng handle the substitution by
2138 * ensuring that the corresponding gray level matches the background
2139 * color exactly.
2140 */
2141 data_encoding = E_sRGB;
2142
2143 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2144 png_error(png_ptr, "gray[16] color-map: too few entries");
2145
2146 cmap_entries = make_gray_colormap(display);
2147
2148 if (png_ptr->num_trans > 0)
2149 {
2150 unsigned int back_alpha;
2151
2152 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2153 back_alpha = 0;
2154
2155 else
2156 {
2157 if (back_r == back_g && back_g == back_b)
2158 {
2159 /* Background is gray; no special processing will be
2160 * required.
2161 */
2162 png_color_16 c;
2163 png_uint_32 gray = back_g;
2164
2165 if (output_encoding == E_LINEAR)
2166 {
2167 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2168
2169 /* And make sure the corresponding palette entry
2170 * matches.
2171 */
2172 png_create_colormap_entry(display, gray, back_g, back_g,
2173 back_g, 65535, E_LINEAR);
2174 }
2175
2176 /* The background passed to libpng, however, must be the
2177 * sRGB value.
2178 */
2179 c.index = 0; /*unused*/
2180 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2181
2182 /* NOTE: does this work without expanding tRNS to alpha?
2183 * It should be the color->gray case below apparently
2184 * doesn't.
2185 */
2186 png_set_background_fixed(png_ptr, &c,
2187 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2188 0/*gamma: not used*/);
2189
2190 output_processing = PNG_CMAP_NONE;
2191 break;
2192 }
2193
2194 back_alpha = output_encoding == E_LINEAR ? 65535 : 255;
2195 }
2196
2197 /* output_processing means that the libpng-processed row will be
2198 * 8-bit GA and it has to be processing to single byte color-map
2199 * values. Entry 254 is replaced by either a completely
2200 * transparent entry or by the background color at full
2201 * precision (and the background color is not a simple gray leve
2202 * in this case.)
2203 */
2204 expand_tRNS = 1;
2205 output_processing = PNG_CMAP_TRANS;
2206 background_index = 254;
2207
2208 /* And set (overwrite) color-map entry 254 to the actual
2209 * background color at full precision.
2210 */
2211 png_create_colormap_entry(display, 254, back_r, back_g, back_b,
2212 back_alpha, output_encoding);
2213 }
2214
2215 else
2216 output_processing = PNG_CMAP_NONE;
2217 }
2218 break;
2219
2220 case PNG_COLOR_TYPE_GRAY_ALPHA:
2221 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2222 * of 65536 combinations. If, however, the alpha channel is to be
2223 * removed there are only 256 possibilities if the background is gray.
2224 * (Otherwise there is a subset of the 65536 possibilities defined by
2225 * the triangle between black, white and the background color.)
2226 *
2227 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2228 * worry about tRNS matching - tRNS is ignored if there is an alpha
2229 * channel.
2230 */
2231 data_encoding = E_sRGB;
2232
2233 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2234 {
2235 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2236 png_error(png_ptr, "gray+alpha color-map: too few entries");
2237
2238 cmap_entries = make_ga_colormap(display);
2239
2240 background_index = PNG_CMAP_GA_BACKGROUND;
2241 output_processing = PNG_CMAP_GA;
2242 }
2243
2244 else /* alpha is removed */
2245 {
2246 /* Alpha must be removed as the PNG data is processed when the
2247 * background is a color because the G and A channels are
2248 * independent and the vector addition (non-parallel vectors) is a
2249 * 2-D problem.
2250 *
2251 * This can be reduced to the same algorithm as above by making a
2252 * colormap containing gray levels (for the opaque grays), a
2253 * background entry (for a transparent pixel) and a set of four six
2254 * level color values, one set for each intermediate alpha value.
2255 * See the comments in make_ga_colormap for how this works in the
2256 * per-pixel processing.
2257 *
2258 * If the background is gray, however, we only need a 256 entry gray
2259 * level color map. It is sufficient to make the entry generated
2260 * for the background color be exactly the color specified.
2261 */
2262 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2263 (back_r == back_g && back_g == back_b))
2264 {
2265 /* Background is gray; no special processing will be required. */
2266 png_color_16 c;
2267 png_uint_32 gray = back_g;
2268
2269 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2270 png_error(png_ptr, "gray-alpha color-map: too few entries");
2271
2272 cmap_entries = make_gray_colormap(display);
2273
2274 if (output_encoding == E_LINEAR)
2275 {
2276 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2277
2278 /* And make sure the corresponding palette entry matches. */
2279 png_create_colormap_entry(display, gray, back_g, back_g,
2280 back_g, 65535, E_LINEAR);
2281 }
2282
2283 /* The background passed to libpng, however, must be the sRGB
2284 * value.
2285 */
2286 c.index = 0; /*unused*/
2287 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2288
2289 png_set_background_fixed(png_ptr, &c,
2290 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2291 0/*gamma: not used*/);
2292
2293 output_processing = PNG_CMAP_NONE;
2294 }
2295
2296 else
2297 {
2298 png_uint_32 i, a;
2299
2300 /* This is the same as png_make_ga_colormap, above, except that
2301 * the entries are all opaque.
2302 */
2303 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2304 png_error(png_ptr, "ga-alpha color-map: too few entries");
2305
2306 i = 0;
2307 while (i < 231)
2308 {
2309 png_uint_32 gray = (i * 256 + 115) / 231;
2310 png_create_colormap_entry(display, i++, gray, gray, gray,
2311 255, E_sRGB);
2312 }
2313
2314 /* NOTE: this preserves the full precision of the application
2315 * background color.
2316 */
2317 background_index = i;
2318 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
2319 output_encoding == E_LINEAR ? 65535U : 255U, output_encoding);
2320
2321 /* For non-opaque input composite on the sRGB background - this
2322 * requires inverting the encoding for each component. The input
2323 * is still converted to the sRGB encoding because this is a
2324 * reasonable approximate to the logarithmic curve of human
2325 * visual sensitivity, at least over the narrow range which PNG
2326 * represents. Consequently 'G' is always sRGB encoded, while
2327 * 'A' is linear. We need the linear background colors.
2328 */
2329 if (output_encoding == E_sRGB) /* else already linear */
2330 {
2331 /* This may produce a value not exactly matching the
2332 * background, but that's ok because these numbers are only
2333 * used when alpha != 0
2334 */
2335 back_r = png_sRGB_table[back_r];
2336 back_g = png_sRGB_table[back_g];
2337 back_b = png_sRGB_table[back_b];
2338 }
2339
2340 for (a=1; a<5; ++a)
2341 {
2342 unsigned int g;
2343
2344 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2345 * by an 8-bit alpha value (0..255).
2346 */
2347 png_uint_32 alpha = 51 * a;
2348 png_uint_32 back_rx = (255-alpha) * back_r;
2349 png_uint_32 back_gx = (255-alpha) * back_g;
2350 png_uint_32 back_bx = (255-alpha) * back_b;
2351
2352 for (g=0; g<6; ++g)
2353 {
2354 png_uint_32 gray = png_sRGB_table[g*51] * alpha;
2355
2356 png_create_colormap_entry(display, i++,
2357 PNG_sRGB_FROM_LINEAR(gray + back_rx),
2358 PNG_sRGB_FROM_LINEAR(gray + back_gx),
2359 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, E_sRGB);
2360 }
2361 }
2362
2363 cmap_entries = i;
2364 output_processing = PNG_CMAP_GA;
2365 }
2366 }
2367 break;
2368
2369 case PNG_COLOR_TYPE_RGB:
2370 case PNG_COLOR_TYPE_RGB_ALPHA:
2371 /* Exclude the case where the output is gray; we can always handle this
2372 * with the cases above.
2373 */
2374 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
2375 {
2376 /* The color-map will be grayscale, so we may as well convert the
2377 * input RGB values to a simple grayscale and use the grayscale
2378 * code above.
2379 *
2380 * NOTE: calling this apparently damages the recognition of the
2381 * transparent color in background color handling; call
2382 * png_set_tRNS_to_alpha before png_set_background_fixed.
2383 */
2384 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2385 -1);
2386 data_encoding = E_sRGB;
2387
2388 /* The output will now be one or two 8-bit gray or gray+alpha
2389 * channels. The more complex case arises when the input has alpha.
2390 */
2391 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2392 png_ptr->num_trans > 0) &&
2393 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2394 {
2395 /* Both input and output have an alpha channel, so no background
2396 * processing is required; just map the GA bytes to the right
2397 * color-map entry.
2398 */
2399 expand_tRNS = 1;
2400
2401 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2402 png_error(png_ptr, "rgb[ga] color-map: too few entries");
2403
2404 cmap_entries = make_ga_colormap(display);
2405 background_index = PNG_CMAP_GA_BACKGROUND;
2406 output_processing = PNG_CMAP_GA;
2407 }
2408
2409 else
2410 {
2411 /* Either the input or the output has no alpha channel, so there
2412 * will be no non-opaque pixels in the color-map; it will just be
2413 * grayscale.
2414 */
2415 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2416 png_error(png_ptr, "rgb[gray] color-map: too few entries");
2417
2418 cmap_entries = make_gray_colormap(display);
2419
2420 /* But if the input has alpha or transparency it must be removed
2421 */
2422 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2423 png_ptr->num_trans > 0)
2424 {
2425 png_color_16 c;
2426 png_uint_32 gray = back_g;
2427
2428 /* We need to ensure that the application background exists in
2429 * the colormap and that completely transparent pixels map to
2430 * it. Achieve this simply by ensuring that the entry
2431 * selected for the background really is the background color.
2432 */
2433 if (output_encoding == E_LINEAR)
2434 {
2435 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2436
2437 /* And make sure the corresponding palette entry matches.
2438 */
2439 png_create_colormap_entry(display, gray, back_g, back_g,
2440 back_g, 65535, E_LINEAR);
2441 }
2442
2443 /* The background passed to libpng, however, must be the sRGB
2444 * value.
2445 */
2446 c.index = 0; /*unused*/
2447 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2448
2449 /* NOTE: the following is apparently a bug in libpng. Without
2450 * it the transparent color recognition in
2451 * png_set_background_fixed seems to go wrong.
2452 */
2453 expand_tRNS = 1;
2454 png_set_background_fixed(png_ptr, &c,
2455 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2456 0/*gamma: not used*/);
2457 }
2458
2459 output_processing = PNG_CMAP_NONE;
2460 }
2461 }
2462
2463 else /* output is color */
2464 {
2465 /* We could use png_quantize here so long as there is no transparent
2466 * color or alpha; png_quantize ignores alpha. Easier overall just
2467 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2468 * Consequently we always want libpng to produce sRGB data.
2469 */
2470 data_encoding = E_sRGB;
2471
2472 /* Is there any transparency or alpha? */
2473 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2474 png_ptr->num_trans > 0)
2475 {
2476 /* Is there alpha in the output too? If so all four channels are
2477 * processed into a special RGB cube with alpha support.
2478 */
2479 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2480 {
2481 png_uint_32 r;
2482
2483 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2484 png_error(png_ptr, "rgb+alpha color-map: too few entries");
2485
2486 cmap_entries = make_rgb_colormap(display);
2487
2488 /* Add a transparent entry. */
2489 png_create_colormap_entry(display, cmap_entries, 255, 255,
2490 255, 0, E_sRGB);
2491
2492 /* This is stored as the background index for the processing
2493 * algorithm.
2494 */
2495 background_index = cmap_entries++;
2496
2497 /* Add 27 r,g,b entries each with alpha 0.5. */
2498 for (r=0; r<256; r = (r << 1) | 0x7f)
2499 {
2500 png_uint_32 g;
2501
2502 for (g=0; g<256; g = (g << 1) | 0x7f)
2503 {
2504 png_uint_32 b;
2505
2506 /* This generates components with the values 0, 127 and
2507 * 255
2508 */
2509 for (b=0; b<256; b = (b << 1) | 0x7f)
2510 png_create_colormap_entry(display, cmap_entries++,
2511 r, g, b, 128, E_sRGB);
2512 }
2513 }
2514
2515 expand_tRNS = 1;
2516 output_processing = PNG_CMAP_RGB_ALPHA;
2517 }
2518
2519 else
2520 {
2521 /* Alpha/transparency must be removed. The background must
2522 * exist in the color map (achieved by setting adding it after
2523 * the 666 color-map). If the standard processing code will
2524 * pick up this entry automatically that's all that is
2525 * required; libpng can be called to do the background
2526 * processing.
2527 */
2528 unsigned int sample_size =
2529 PNG_IMAGE_SAMPLE_SIZE(output_format);
2530 png_uint_32 r, g, b; /* sRGB background */
2531
2532 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2533 png_error(png_ptr, "rgb-alpha color-map: too few entries");
2534
2535 cmap_entries = make_rgb_colormap(display);
2536
2537 png_create_colormap_entry(display, cmap_entries, back_r,
2538 back_g, back_b, 0/*unused*/, output_encoding);
2539
2540 if (output_encoding == E_LINEAR)
2541 {
2542 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2543 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2544 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2545 }
2546
2547 else
2548 {
2549 r = back_r;
2550 g = back_g;
2551 b = back_g;
2552 }
2553
2554 /* Compare the newly-created color-map entry with the one the
2555 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2556 * match, add the new one and set this as the background index.
2557 */
2558 if (memcmp((png_const_bytep)display->colormap +
2559 sample_size * cmap_entries,
2560 (png_const_bytep)display->colormap +
2561 sample_size * PNG_RGB_INDEX(r,g,b),
2562 sample_size) != 0)
2563 {
2564 /* The background color must be added. */
2565 background_index = cmap_entries++;
2566
2567 /* Add 27 r,g,b entries each with created by composing with
2568 * the background at alpha 0.5.
2569 */
2570 for (r=0; r<256; r = (r << 1) | 0x7f)
2571 {
2572 for (g=0; g<256; g = (g << 1) | 0x7f)
2573 {
2574 /* This generates components with the values 0, 127
2575 * and 255
2576 */
2577 for (b=0; b<256; b = (b << 1) | 0x7f)
2578 png_create_colormap_entry(display, cmap_entries++,
2579 png_colormap_compose(display, r, E_sRGB, 128,
2580 back_r, output_encoding),
2581 png_colormap_compose(display, g, E_sRGB, 128,
2582 back_g, output_encoding),
2583 png_colormap_compose(display, b, E_sRGB, 128,
2584 back_b, output_encoding),
2585 0/*unused*/, output_encoding);
2586 }
2587 }
2588
2589 expand_tRNS = 1;
2590 output_processing = PNG_CMAP_RGB_ALPHA;
2591 }
2592
2593 else /* background color is in the standard color-map */
2594 {
2595 png_color_16 c;
2596
2597 c.index = 0; /*unused*/
2598 c.red = (png_uint_16)back_r;
2599 c.gray = c.green = (png_uint_16)back_g;
2600 c.blue = (png_uint_16)back_b;
2601
2602 png_set_background_fixed(png_ptr, &c,
2603 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2604 0/*gamma: not used*/);
2605
2606 output_processing = PNG_CMAP_RGB;
2607 }
2608 }
2609 }
2610
2611 else /* no alpha or transparency in the input */
2612 {
2613 /* Alpha in the output is irrelevant, simply map the opaque input
2614 * pixels to the 6x6x6 color-map.
2615 */
2616 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2617 png_error(png_ptr, "rgb color-map: too few entries");
2618
2619 cmap_entries = make_rgb_colormap(display);
2620 output_processing = PNG_CMAP_RGB;
2621 }
2622 }
2623 break;
2624
2625 case PNG_COLOR_TYPE_PALETTE:
2626 /* It's already got a color-map. It may be necessary to eliminate the
2627 * tRNS entries though.
2628 */
2629 {
2630 unsigned int num_trans = png_ptr->num_trans;
2631 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2632 png_const_colorp colormap = png_ptr->palette;
2633 const int do_background = trans != NULL &&
2634 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2635 unsigned int i;
2636
2637 /* Just in case: */
2638 if (trans == NULL)
2639 num_trans = 0;
2640
2641 output_processing = PNG_CMAP_NONE;
2642 data_encoding = E_FILE; /* Don't change from color-map indicies */
2643 cmap_entries = png_ptr->num_palette;
2644 if (cmap_entries > 256)
2645 cmap_entries = 256;
2646
2647 if (cmap_entries > image->colormap_entries)
2648 png_error(png_ptr, "palette color-map: too few entries");
2649
2650 for (i=0; i < cmap_entries; ++i)
2651 {
2652 if (do_background && i < num_trans && trans[i] < 255)
2653 {
2654 if (trans[i] == 0)
2655 png_create_colormap_entry(display, i, back_r, back_g,
2656 back_b, 0, output_encoding);
2657
2658 else
2659 {
2660 /* Must compose the PNG file color in the color-map entry
2661 * on the sRGB color in 'back'.
2662 */
2663 png_create_colormap_entry(display, i,
2664 png_colormap_compose(display, colormap[i].red, E_FILE,
2665 trans[i], back_r, output_encoding),
2666 png_colormap_compose(display, colormap[i].green, E_FILE,
2667 trans[i], back_g, output_encoding),
2668 png_colormap_compose(display, colormap[i].blue, E_FILE,
2669 trans[i], back_b, output_encoding),
2670 output_encoding == E_LINEAR ? trans[i] * 257U :
2671 trans[i],
2672 output_encoding);
2673 }
2674 }
2675
2676 else
2677 png_create_colormap_entry(display, i, colormap[i].red,
2678 colormap[i].green, colormap[i].blue,
2679 i < num_trans ? trans[i] : 255U, E_FILE/*8-bit*/);
2680 }
2681
2682 /* The PNG data may have indicies packed in fewer than 8 bits, it
2683 * must be expanded if so.
2684 */
2685 if (png_ptr->bit_depth < 8)
2686 png_set_packing(png_ptr);
2687 }
2688 break;
2689
2690 default:
2691 png_error(png_ptr, "invalid PNG color type");
2692 /*NOT REACHED*/
2693 break;
2694 }
2695
2696 /* Now deal with the output processing */
2697 if (expand_tRNS && png_ptr->num_trans > 0 &&
2698 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
2699 png_set_tRNS_to_alpha(png_ptr);
2700
2701 switch (data_encoding)
2702 {
2703 default:
2704 png_error(png_ptr, "bad data option (internal error)");
2705 break;
2706
2707 case E_FILE:
2708 /* Make no changes */
2709 break;
2710
2711 case E_sRGB:
2712 /* Change to 8-bit sRGB */
2713 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
2714 if (png_ptr->bit_depth > 8)
2715 png_set_scale_16(png_ptr);
2716 break;
2717 }
2718
2719 if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2720 png_error(png_ptr, "color map overflow (BAD internal error)");
2721
2722 image->colormap_entries = cmap_entries;
2723
2724 /* Double check using the recorded background index */
2725 switch (output_processing)
2726 {
2727 case PNG_CMAP_NONE:
2728 if (background_index != PNG_CMAP_NONE_BACKGROUND)
2729 goto bad_background;
2730 break;
2731
2732 case PNG_CMAP_GA:
2733 if (background_index != PNG_CMAP_GA_BACKGROUND)
2734 goto bad_background;
2735 break;
2736
2737 case PNG_CMAP_TRANS:
2738 if (background_index >= cmap_entries ||
2739 background_index != PNG_CMAP_TRANS_BACKGROUND)
2740 goto bad_background;
2741 break;
2742
2743 case PNG_CMAP_RGB:
2744 if (background_index != PNG_CMAP_RGB_BACKGROUND)
2745 goto bad_background;
2746 break;
2747
2748 case PNG_CMAP_RGB_ALPHA:
2749 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
2750 goto bad_background;
2751 break;
2752
2753 default:
2754 png_error(png_ptr, "bad processing option (internal error)");
2755
2756 bad_background:
2757 png_error(png_ptr, "bad background index (internal error)");
2758 }
2759
2760 display->colormap_processing = output_processing;
2761
2762 return 1/*ok*/;
2763}
2764
2765/* The final part of the color-map read called from png_image_finish_read. */
2766static int
2767png_image_read_and_map(png_voidp argument)
John Bowler7875d532011-11-07 22:33:49 -06002768{
John Bowler4fa96a42011-11-16 16:39:16 -06002769 png_image_read_control *display = png_voidcast(png_image_read_control*,
2770 argument);
John Bowler7875d532011-11-07 22:33:49 -06002771 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06002772 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler4f67e402011-12-28 08:43:37 -06002773 int passes;
John Bowler7875d532011-11-07 22:33:49 -06002774
John Bowler5bc90382012-01-23 22:43:22 -06002775 /* Called when the libpng data must be transformed into the color-mapped
2776 * form. There is a local row buffer in display->local and this routine must
2777 * do the interlace handling.
2778 */
2779 switch (png_ptr->interlaced)
John Bowler7875d532011-11-07 22:33:49 -06002780 {
2781 case PNG_INTERLACE_NONE:
2782 passes = 1;
2783 break;
2784
2785 case PNG_INTERLACE_ADAM7:
2786 passes = PNG_INTERLACE_ADAM7_PASSES;
2787 break;
2788
2789 default:
John Bowler4f67e402011-12-28 08:43:37 -06002790 passes = 0;
John Bowler7875d532011-11-07 22:33:49 -06002791 png_error(png_ptr, "unknown interlace type");
2792 }
2793
2794 {
John Bowler5bc90382012-01-23 22:43:22 -06002795 png_uint_32 height = image->height;
2796 png_uint_32 width = image->width;
2797 int proc = display->colormap_processing;
2798 png_bytep first_row = display->first_row;
2799 ptrdiff_t step_row = display->row_bytes;
John Bowler7875d532011-11-07 22:33:49 -06002800 int pass;
2801
2802 for (pass = 0; pass < passes; ++pass)
2803 {
John Bowler7875d532011-11-07 22:33:49 -06002804 unsigned int startx, stepx, stepy;
2805 png_uint_32 y;
2806
John Bowler5bc90382012-01-23 22:43:22 -06002807 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
John Bowler7875d532011-11-07 22:33:49 -06002808 {
2809 /* The row may be empty for a short image: */
2810 if (PNG_PASS_COLS(width, pass) == 0)
2811 continue;
2812
2813 startx = PNG_PASS_START_COL(pass);
2814 stepx = PNG_PASS_COL_OFFSET(pass);
2815 y = PNG_PASS_START_ROW(pass);
John Bowler18c5cfa2011-11-16 14:26:34 -06002816 stepy = PNG_PASS_ROW_OFFSET(pass);
John Bowler7875d532011-11-07 22:33:49 -06002817 }
2818
2819 else
2820 {
2821 y = 0;
2822 startx = 0;
2823 stepx = stepy = 1;
2824 }
John Bowler18c5cfa2011-11-16 14:26:34 -06002825
John Bowler7875d532011-11-07 22:33:49 -06002826 for (; y<height; y += stepy)
John Bowler5bc90382012-01-23 22:43:22 -06002827 {
2828 png_bytep inrow = display->local_row;
2829 png_bytep outrow = first_row + y * step_row;
2830 png_const_bytep end_row = outrow + width;
2831
2832 /* Read read the libpng data into the temporary buffer. */
2833 png_read_row(png_ptr, inrow, NULL);
2834
2835 /* Now process the row according to the processing option, note
2836 * that the caller verifies that the format of the libpng output
2837 * data is as required.
2838 */
2839 outrow += startx;
2840 switch (proc)
John Bowler7875d532011-11-07 22:33:49 -06002841 {
John Bowler5bc90382012-01-23 22:43:22 -06002842 case PNG_CMAP_GA:
2843 for (; outrow < end_row; outrow += stepx)
John Bowler7875d532011-11-07 22:33:49 -06002844 {
John Bowler5bc90382012-01-23 22:43:22 -06002845 /* The data is always in the PNG order */
2846 unsigned int gray = *inrow++;
2847 unsigned int alpha = *inrow++;
2848 unsigned int entry;
John Bowler7875d532011-11-07 22:33:49 -06002849
John Bowler5bc90382012-01-23 22:43:22 -06002850 /* NOTE: this code is copied as a comment in
2851 * make_ga_colormap above. Please update the
2852 * comment if you change this code!
2853 */
2854 if (alpha > 229) /* opaque */
John Bowler7875d532011-11-07 22:33:49 -06002855 {
John Bowler5bc90382012-01-23 22:43:22 -06002856 entry = (231 * gray + 128) >> 8;
John Bowler7875d532011-11-07 22:33:49 -06002857 }
John Bowler5bc90382012-01-23 22:43:22 -06002858 else if (alpha < 26) /* transparent */
2859 {
2860 entry = 231;
2861 }
2862 else /* partially opaque */
2863 {
2864 entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
2865 }
John Bowler7875d532011-11-07 22:33:49 -06002866
John Bowler5bc90382012-01-23 22:43:22 -06002867 *outrow = (png_byte)entry;
2868 }
2869 break;
2870
2871 case PNG_CMAP_TRANS:
2872 for (; outrow < end_row; outrow += stepx)
2873 {
2874 png_byte gray = *inrow++;
2875 png_byte alpha = *inrow++;
2876
2877 if (alpha == 0)
2878 *outrow = PNG_CMAP_TRANS_BACKGROUND;
2879
2880 else if (gray != PNG_CMAP_TRANS_BACKGROUND)
2881 *outrow = gray;
2882
2883 else
2884 *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
2885 }
2886 break;
2887
2888 case PNG_CMAP_RGB:
2889 for (; outrow < end_row; outrow += stepx)
2890 {
2891 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
2892 inrow += 3;
2893 }
2894 break;
2895
2896 case PNG_CMAP_RGB_ALPHA:
2897 for (; outrow < end_row; outrow += stepx)
2898 {
2899 unsigned int alpha = inrow[3];
2900
2901 /* Because the alpha entries only hold alpha==0.5 values
2902 * split the processing at alpha==0.25 (64) and 0.75
2903 * (196).
2904 */
2905
2906 if (alpha >= 196)
2907 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
2908 inrow[2]);
2909
2910 else if (alpha < 64)
2911 *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
2912
2913 else
2914 {
2915 /* Likewise there are three entries for each of r, g
2916 * and b. We could select the entry by popcount on
2917 * the top two bits on those architectures that
2918 * support it, this is what the code below does,
2919 * crudely.
2920 */
2921 unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
2922
2923 /* Here are how the values map:
2924 *
2925 * 0x00 .. 0x3f -> 0
2926 * 0x40 .. 0xbf -> 1
2927 * 0xc0 .. 0xff -> 2
2928 *
2929 * So, as above with the explicit alpha checks, the
2930 * breakpoints are at 64 and 196.
2931 */
2932 if (inrow[0] & 0x80) back_i += 9; /* red */
2933 if (inrow[0] & 0x40) back_i += 9;
2934 if (inrow[0] & 0x80) back_i += 3; /* green */
2935 if (inrow[0] & 0x40) back_i += 3;
2936 if (inrow[0] & 0x80) back_i += 1; /* blue */
2937 if (inrow[0] & 0x40) back_i += 1;
2938
2939 *outrow = (png_byte)back_i;
2940 }
2941
2942 inrow += 4;
2943 }
2944 break;
2945
2946 default:
2947 break;
2948 }
2949 }
2950 }
2951 }
2952
2953 return 1;
2954}
2955
2956static int
2957png_image_read_colormapped(png_voidp argument)
2958{
2959 png_image_read_control *display = png_voidcast(png_image_read_control*,
2960 argument);
2961 png_imagep image = display->image;
2962 png_controlp control = image->opaque;
2963 png_structrp png_ptr = control->png_ptr;
2964 png_inforp info_ptr = control->info_ptr;
2965
2966 int passes = 0; /* As a flag */
2967
2968 PNG_SKIP_CHUNKS(png_ptr);
2969
2970 /* Update the 'info' structure and make sure the result is as required; first
2971 * make sure to turn on the interlace handling if it will be required
2972 * (because it can't be turned on *after* the call to png_read_update_info!)
2973 */
2974 if (display->colormap_processing == PNG_CMAP_NONE)
2975 passes = png_set_interlace_handling(png_ptr);
2976
2977 png_read_update_info(png_ptr, info_ptr);
2978
2979 /* The expected output can be deduced from the colormap_processing option. */
2980 switch (display->colormap_processing)
2981 {
2982 case PNG_CMAP_NONE:
2983 /* Output must be one channel and one byte per pixel, the output
2984 * encoding can be anything.
2985 */
2986 if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
2987 info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
2988 info_ptr->bit_depth == 8)
2989 break;
2990
2991 goto bad_output;
2992
2993 case PNG_CMAP_TRANS:
2994 case PNG_CMAP_GA:
2995 /* Output must be two channels and the 'G' one must be sRGB, the latter
2996 * can be checked with an exact number because it should have been set
2997 * to this number above!
2998 */
2999 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
3000 info_ptr->bit_depth == 8 &&
3001 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3002 image->colormap_entries == 256)
3003 break;
3004
3005 goto bad_output;
3006
3007 case PNG_CMAP_RGB:
3008 /* Output must be 8-bit sRGB encoded RGB */
3009 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
3010 info_ptr->bit_depth == 8 &&
3011 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3012 image->colormap_entries == 216)
3013 break;
3014
3015 goto bad_output;
3016
3017 case PNG_CMAP_RGB_ALPHA:
3018 /* Output must be 8-bit sRGB encoded RGBA */
3019 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
3020 info_ptr->bit_depth == 8 &&
3021 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3022 image->colormap_entries == 244 /* 216 + 1 + 27 */)
3023 break;
3024
3025 /* goto bad_output; */
3026 /* FALL THROUGH */
3027
3028 default:
3029 bad_output:
3030 png_error(png_ptr, "bad color-map processing (internal error)");
3031 }
3032
3033 /* Now read the rows. Do this here if it is possible to read directly into
3034 * the output buffer, otherwise allocate a local row buffer of the maximum
3035 * size libpng requires and call the relevant processing routine safely.
3036 */
3037 {
3038 png_bytep first_row = png_voidcast(png_bytep, display->buffer);
3039 ptrdiff_t row_bytes = display->row_stride;
3040
3041 /* The following expression is designed to work correctly whether it gives
3042 * a signed or an unsigned result.
3043 */
3044 if (row_bytes < 0)
3045 first_row += (image->height-1) * (-row_bytes);
3046
3047 display->first_row = first_row;
3048 display->row_bytes = row_bytes;
3049 }
3050
3051 if (passes == 0)
3052 {
3053 int result;
3054 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
3055 png_get_rowbytes(png_ptr, info_ptr)));
3056
3057 display->local_row = row;
3058 result = png_safe_execute(image, png_image_read_and_map, display);
3059 display->local_row = NULL;
3060 png_free(png_ptr, row);
3061
3062 return result;
3063 }
3064
3065 else
3066 {
3067 png_alloc_size_t row_bytes = display->row_bytes;
3068
3069 while (--passes >= 0)
3070 {
3071 png_uint_32 y = image->height;
3072 png_bytep row = display->first_row;
3073
3074 while (y-- > 0)
3075 {
3076 png_read_row(png_ptr, row, NULL);
3077 row += row_bytes;
3078 }
3079 }
3080
3081 return 1;
3082 }
3083}
3084
3085/* Just the row reading part of png_image_read. */
3086static int
3087png_image_read_composite(png_voidp argument)
3088{
3089 png_image_read_control *display = png_voidcast(png_image_read_control*,
3090 argument);
3091 png_imagep image = display->image;
3092 png_structrp png_ptr = image->opaque->png_ptr;
3093 int passes;
3094
3095 switch (png_ptr->interlaced)
3096 {
3097 case PNG_INTERLACE_NONE:
3098 passes = 1;
3099 break;
3100
3101 case PNG_INTERLACE_ADAM7:
3102 passes = PNG_INTERLACE_ADAM7_PASSES;
3103 break;
3104
3105 default:
3106 passes = 0;
3107 png_error(png_ptr, "unknown interlace type");
3108 }
3109
3110 {
3111 png_uint_32 height = image->height;
3112 png_uint_32 width = image->width;
3113 png_bytep first_row = display->first_row;
3114 ptrdiff_t step_row = display->row_bytes;
3115 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
3116 int pass;
3117
3118 for (pass = 0; pass < passes; ++pass)
3119 {
3120 unsigned int startx, stepx, stepy;
3121 png_uint_32 y;
3122
3123 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3124 {
3125 /* The row may be empty for a short image: */
3126 if (PNG_PASS_COLS(width, pass) == 0)
3127 continue;
3128
3129 startx = PNG_PASS_START_COL(pass) * channels;
3130 stepx = PNG_PASS_COL_OFFSET(pass) * channels;
3131 y = PNG_PASS_START_ROW(pass);
3132 stepy = PNG_PASS_ROW_OFFSET(pass);
3133 }
3134
3135 else
3136 {
3137 y = 0;
3138 startx = 0;
3139 stepx = channels;
3140 stepy = 1;
3141 }
3142
3143 for (; y<height; y += stepy)
3144 {
3145 png_bytep inrow = display->local_row;
3146 png_bytep outrow = first_row + y * step_row;
3147 png_const_bytep end_row = outrow + width * channels;
3148
3149 /* Read the row, which is packed: */
3150 png_read_row(png_ptr, inrow, NULL);
3151
3152 /* Now do the composition on each pixel in this row. */
3153 outrow += startx;
3154 for (; outrow < end_row; outrow += stepx)
3155 {
3156 png_byte alpha = inrow[channels];
3157
3158 if (alpha > 0) /* else no change to the output */
3159 {
3160 unsigned int c;
3161
3162 for (c=0; c<channels; ++c)
3163 {
3164 png_uint_32 component = inrow[c];
3165
3166 if (alpha < 255) /* else just use component */
3167 {
3168 /* This is PNG_OPTIMIZED_ALPHA, the component value
3169 * is a linear 8-bit value. Combine this with the
3170 * current outrow[c] value which is sRGB encoded.
3171 * Arithmetic here is 16-bits to preserve the output
3172 * values correctly.
3173 */
3174 component *= 257*255; /* =65535 */
3175 component += (255-alpha)*png_sRGB_table[outrow[c]];
3176
3177 /* So 'component' is scaled by 255*65535 and is
3178 * therefore appropriate for the sRGB to linear
3179 * conversion table.
3180 */
3181 component = PNG_sRGB_FROM_LINEAR(component);
3182 }
3183
3184 outrow[c] = (png_byte)component;
3185 }
John Bowler7875d532011-11-07 22:33:49 -06003186 }
3187
John Bowler5bc90382012-01-23 22:43:22 -06003188 inrow += channels+1; /* components and alpha channel */
John Bowler7875d532011-11-07 22:33:49 -06003189 }
John Bowler5bc90382012-01-23 22:43:22 -06003190 }
John Bowler7875d532011-11-07 22:33:49 -06003191 }
3192 }
3193
3194 return 1;
3195}
3196
John Bowler18c5cfa2011-11-16 14:26:34 -06003197/* The do_local_background case; called when all the following transforms are to
3198 * be done:
3199 *
3200 * PNG_RGB_TO_GRAY
3201 * PNG_COMPOSITE
3202 * PNG_GAMMA
3203 *
3204 * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and
3205 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
3206 * correction. The fix-up is to prevent the PNG_COMPOSITE operation happening
3207 * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles
3208 * the removal or pre-multiplication of the alpha channel.
3209 */
3210static int
3211png_image_read_background(png_voidp argument)
3212{
John Bowler4fa96a42011-11-16 16:39:16 -06003213 png_image_read_control *display = png_voidcast(png_image_read_control*,
3214 argument);
John Bowler18c5cfa2011-11-16 14:26:34 -06003215 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06003216 png_structrp png_ptr = image->opaque->png_ptr;
3217 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler18c5cfa2011-11-16 14:26:34 -06003218 png_uint_32 height = image->height;
3219 png_uint_32 width = image->width;
John Bowler4f67e402011-12-28 08:43:37 -06003220 int pass, passes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003221
3222 /* Double check the convoluted logic below. We expect to get here with
3223 * libpng doing rgb to gray and gamma correction but background processing
3224 * left to the png_image_read_background function. The rows libpng produce
3225 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
3226 */
3227 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
3228 png_error(png_ptr, "lost rgb to gray");
3229
3230 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
3231 png_error(png_ptr, "unexpected compose");
3232
3233 /* The palette code zaps PNG_GAMMA in place... */
3234 if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) == 0 &&
3235 (png_ptr->transformations & PNG_GAMMA) == 0)
3236 png_error(png_ptr, "lost gamma correction");
3237
3238 if (png_get_channels(png_ptr, info_ptr) != 2)
3239 png_error(png_ptr, "lost/gained channels");
3240
John Bowler5bc90382012-01-23 22:43:22 -06003241 switch (png_ptr->interlaced)
John Bowler18c5cfa2011-11-16 14:26:34 -06003242 {
3243 case PNG_INTERLACE_NONE:
3244 passes = 1;
3245 break;
3246
3247 case PNG_INTERLACE_ADAM7:
3248 passes = PNG_INTERLACE_ADAM7_PASSES;
3249 break;
3250
3251 default:
John Bowler4f67e402011-12-28 08:43:37 -06003252 passes = 0;
John Bowler18c5cfa2011-11-16 14:26:34 -06003253 png_error(png_ptr, "unknown interlace type");
3254 }
3255
3256 switch (png_get_bit_depth(png_ptr, info_ptr))
3257 {
3258 default:
3259 png_error(png_ptr, "unexpected bit depth");
3260 break;
3261
3262 case 8:
3263 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
3264 * to be removed by composing on a backgroundi: either the row if
John Bowler5bc90382012-01-23 22:43:22 -06003265 * display->background is NULL or display->background->green if not.
John Bowler18c5cfa2011-11-16 14:26:34 -06003266 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3267 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003268 {
John Bowler5bc90382012-01-23 22:43:22 -06003269 png_bytep first_row = display->first_row;
3270 ptrdiff_t step_row = display->row_bytes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003271
John Bowler5bc90382012-01-23 22:43:22 -06003272 for (pass = 0; pass < passes; ++pass)
John Bowler18c5cfa2011-11-16 14:26:34 -06003273 {
John Bowler5bc90382012-01-23 22:43:22 -06003274 png_bytep row = display->first_row;
3275 unsigned int startx, stepx, stepy;
3276 png_uint_32 y;
John Bowler18c5cfa2011-11-16 14:26:34 -06003277
John Bowler5bc90382012-01-23 22:43:22 -06003278 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3279 {
3280 /* The row may be empty for a short image: */
3281 if (PNG_PASS_COLS(width, pass) == 0)
3282 continue;
John Bowler18c5cfa2011-11-16 14:26:34 -06003283
John Bowler5bc90382012-01-23 22:43:22 -06003284 startx = PNG_PASS_START_COL(pass);
3285 stepx = PNG_PASS_COL_OFFSET(pass);
3286 y = PNG_PASS_START_ROW(pass);
3287 stepy = PNG_PASS_ROW_OFFSET(pass);
3288 }
3289
3290 else
3291 {
3292 y = 0;
3293 startx = 0;
3294 stepx = stepy = 1;
3295 }
3296
3297 if (display->background == NULL)
3298 {
3299 for (; y<height; y += stepy)
John Bowler18c5cfa2011-11-16 14:26:34 -06003300 {
3301 png_bytep inrow = display->local_row;
John Bowler5bc90382012-01-23 22:43:22 -06003302 png_bytep outrow = first_row + y * step_row;
3303 png_const_bytep end_row = outrow + width;
John Bowler18c5cfa2011-11-16 14:26:34 -06003304
3305 /* Read the row, which is packed: */
3306 png_read_row(png_ptr, inrow, NULL);
3307
3308 /* Now do the composition on each pixel in this row. */
John Bowler5bc90382012-01-23 22:43:22 -06003309 outrow += startx;
John Bowler18c5cfa2011-11-16 14:26:34 -06003310 for (; outrow < end_row; outrow += stepx)
3311 {
3312 png_byte alpha = inrow[1];
3313
3314 if (alpha > 0) /* else no change to the output */
3315 {
3316 png_uint_32 component = inrow[0];
3317
3318 if (alpha < 255) /* else just use component */
3319 {
3320 /* Since PNG_OPTIMIZED_ALPHA was not set it is
3321 * necessary to invert the sRGB transfer
3322 * function and multiply the alpha out.
3323 */
3324 component = png_sRGB_table[component] * alpha;
3325 component += png_sRGB_table[outrow[0]] *
3326 (255-alpha);
3327 component = PNG_sRGB_FROM_LINEAR(component);
3328 }
3329
3330 outrow[0] = (png_byte)component;
3331 }
3332
3333 inrow += 2; /* gray and alpha channel */
3334 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003335 }
John Bowler5bc90382012-01-23 22:43:22 -06003336 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003337
John Bowler5bc90382012-01-23 22:43:22 -06003338 else /* constant background value */
3339 {
3340 png_byte background8 = display->background->green;
3341 png_uint_16 background = png_sRGB_table[background8];
John Bowler18c5cfa2011-11-16 14:26:34 -06003342
John Bowler5bc90382012-01-23 22:43:22 -06003343 for (; y<height; y += stepy)
John Bowler18c5cfa2011-11-16 14:26:34 -06003344 {
3345 png_bytep inrow = display->local_row;
John Bowler5bc90382012-01-23 22:43:22 -06003346 png_bytep outrow = first_row + y * step_row;
3347 png_const_bytep end_row = outrow + width;
John Bowler18c5cfa2011-11-16 14:26:34 -06003348
3349 /* Read the row, which is packed: */
3350 png_read_row(png_ptr, inrow, NULL);
3351
3352 /* Now do the composition on each pixel in this row. */
John Bowler5bc90382012-01-23 22:43:22 -06003353 outrow += startx;
John Bowler18c5cfa2011-11-16 14:26:34 -06003354 for (; outrow < end_row; outrow += stepx)
3355 {
3356 png_byte alpha = inrow[1];
3357
3358 if (alpha > 0) /* else use background */
3359 {
3360 png_uint_32 component = inrow[0];
3361
3362 if (alpha < 255) /* else just use component */
3363 {
3364 component = png_sRGB_table[component] * alpha;
3365 component += background * (255-alpha);
3366 component = PNG_sRGB_FROM_LINEAR(component);
3367 }
3368
3369 outrow[0] = (png_byte)component;
3370 }
3371
3372 else
3373 outrow[0] = background8;
3374
3375 inrow += 2; /* gray and alpha channel */
3376 }
3377
3378 row += display->row_bytes;
3379 }
John Bowler5bc90382012-01-23 22:43:22 -06003380 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003381 }
3382 }
3383 break;
3384
3385 case 16:
3386 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3387 * still be done and, maybe, the alpha channel removed. This code also
3388 * handles the alpha-first option.
3389 */
3390 {
John Bowler5bc90382012-01-23 22:43:22 -06003391 png_bytep first_row = display->first_row;
3392 ptrdiff_t step_row = display->row_bytes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003393 unsigned int outchannels = png_get_channels(png_ptr, info_ptr);
3394 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
3395 int swap_alpha = 0;
3396
3397 if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST))
3398 swap_alpha = 1;
3399
3400 for (pass = 0; pass < passes; ++pass)
3401 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003402 unsigned int startx, stepx, stepy; /* all in pixels */
3403 png_uint_32 y;
3404
John Bowler5bc90382012-01-23 22:43:22 -06003405 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
John Bowler18c5cfa2011-11-16 14:26:34 -06003406 {
3407 /* The row may be empty for a short image: */
3408 if (PNG_PASS_COLS(width, pass) == 0)
3409 continue;
3410
John Bowler5bc90382012-01-23 22:43:22 -06003411 startx = PNG_PASS_START_COL(pass) * outchannels;
3412 stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
John Bowler18c5cfa2011-11-16 14:26:34 -06003413 y = PNG_PASS_START_ROW(pass);
3414 stepy = PNG_PASS_ROW_OFFSET(pass);
3415 }
3416
3417 else
3418 {
3419 y = 0;
3420 startx = 0;
John Bowler5bc90382012-01-23 22:43:22 -06003421 stepx = outchannels;
3422 stepy = 1;
John Bowler18c5cfa2011-11-16 14:26:34 -06003423 }
3424
John Bowler18c5cfa2011-11-16 14:26:34 -06003425 for (; y<height; y += stepy)
John Bowler5bc90382012-01-23 22:43:22 -06003426 {
3427 png_const_uint_16p inrow;
3428 png_uint_16p outrow = (png_uint_16p)(first_row + y*step_row);
3429 png_uint_16p end_row = outrow + width * outchannels;
3430
3431 /* Read the row, which is packed: */
3432 png_read_row(png_ptr, display->local_row, NULL);
3433 inrow = (png_const_uint_16p)display->local_row;
3434
3435 /* Now do the pre-multiplication on each pixel in this row.
3436 */
3437 outrow += startx;
3438 for (; outrow < end_row; outrow += stepx)
John Bowler18c5cfa2011-11-16 14:26:34 -06003439 {
John Bowler5bc90382012-01-23 22:43:22 -06003440 png_uint_32 component = inrow[0];
3441 png_uint_16 alpha = inrow[1];
John Bowler18c5cfa2011-11-16 14:26:34 -06003442
John Bowler5bc90382012-01-23 22:43:22 -06003443 if (alpha > 0) /* else 0 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003444 {
John Bowler5bc90382012-01-23 22:43:22 -06003445 if (alpha < 65535) /* else just use component */
John Bowler18c5cfa2011-11-16 14:26:34 -06003446 {
John Bowler5bc90382012-01-23 22:43:22 -06003447 component *= alpha;
3448 component += 32767;
3449 component /= 65535;
John Bowler18c5cfa2011-11-16 14:26:34 -06003450 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003451 }
3452
John Bowler5bc90382012-01-23 22:43:22 -06003453 else
3454 component = 0;
3455
3456 outrow[swap_alpha] = (png_uint_16)component;
3457 if (outchannels > 1)
3458 outrow[1 ^ swap_alpha] = alpha;
3459
3460 inrow += 2; /* components and alpha channel */
John Bowler18c5cfa2011-11-16 14:26:34 -06003461 }
John Bowler5bc90382012-01-23 22:43:22 -06003462 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003463 }
3464 }
3465 break;
3466 }
3467
3468 return 1;
3469}
3470
John Bowler7875d532011-11-07 22:33:49 -06003471/* The guts of png_image_finish_read as a png_safe_execute callback. */
3472static int
John Bowler5bc90382012-01-23 22:43:22 -06003473png_image_read_direct(png_voidp argument)
John Bowler7875d532011-11-07 22:33:49 -06003474{
John Bowler4fa96a42011-11-16 16:39:16 -06003475 png_image_read_control *display = png_voidcast(png_image_read_control*,
3476 argument);
John Bowler7875d532011-11-07 22:33:49 -06003477 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06003478 png_structrp png_ptr = image->opaque->png_ptr;
3479 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06003480
3481 png_uint_32 format = image->format;
3482 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
3483 int do_local_compose = 0;
John Bowler18c5cfa2011-11-16 14:26:34 -06003484 int do_local_background = 0; /* to avoid double gamma correction bug */
John Bowler7875d532011-11-07 22:33:49 -06003485 int passes = 0;
3486
3487 /* Add transforms to ensure the correct output format is produced then check
3488 * that the required implementation support is there. Always expand; always
3489 * need 8 bits minimum, no palette and expanded tRNS.
3490 */
3491 png_set_expand(png_ptr);
3492
3493 /* Now check the format to see if it was modified. */
3494 {
John Bowler5bc90382012-01-23 22:43:22 -06003495 png_uint_32 base_format = png_image_format(png_ptr) &
3496 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
John Bowler7875d532011-11-07 22:33:49 -06003497 png_uint_32 change = format ^ base_format;
John Bowler3615d032011-11-08 10:38:09 -06003498 png_fixed_point output_gamma;
3499 int mode; /* alpha mode */
John Bowler7875d532011-11-07 22:33:49 -06003500
John Bowler18c5cfa2011-11-16 14:26:34 -06003501 /* Do this first so that we have a record if rgb to gray is happening. */
3502 if (change & PNG_FORMAT_FLAG_COLOR)
3503 {
3504 /* gray<->color transformation required. */
3505 if (format & PNG_FORMAT_FLAG_COLOR)
3506 png_set_gray_to_rgb(png_ptr);
3507
3508 else
3509 {
3510 /* libpng can't do both rgb to gray and
3511 * background/pre-multiplication if there is also significant gamma
3512 * correction, because both operations require linear colors and
3513 * the code only supports one transform doing the gamma correction.
3514 * Handle this by doing the pre-multiplication or background
3515 * operation in this code, if necessary.
3516 *
3517 * TODO: fix this by rewriting pngrtran.c (!)
3518 *
3519 * For the moment (given that fixing this in pngrtran.c is an
3520 * enormous change) 'do_local_background' is used to indicate that
3521 * the problem exists.
3522 */
3523 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3524 do_local_background = 1/*maybe*/;
3525
3526 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
3527 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
3528 }
3529
3530 change &= ~PNG_FORMAT_FLAG_COLOR;
3531 }
3532
John Bowler7875d532011-11-07 22:33:49 -06003533 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3534 */
3535 {
John Bowler3615d032011-11-08 10:38:09 -06003536 png_fixed_point input_gamma_default;
John Bowler7875d532011-11-07 22:33:49 -06003537
3538 if (base_format & PNG_FORMAT_FLAG_LINEAR)
3539 input_gamma_default = PNG_GAMMA_LINEAR;
3540 else
3541 input_gamma_default = PNG_DEFAULT_sRGB;
3542
John Bowler18c5cfa2011-11-16 14:26:34 -06003543 /* Call png_set_alpha_mode to set the default for the input gamma; the
3544 * output gamma is set by a second call below.
3545 */
3546 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
3547 }
3548
3549 if (linear)
3550 {
3551 /* If there *is* an alpha channel in the input it must be multiplied
3552 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3553 */
3554 if (base_format & PNG_FORMAT_FLAG_ALPHA)
John Bowler3615d032011-11-08 10:38:09 -06003555 mode = PNG_ALPHA_STANDARD; /* associated alpha */
John Bowler7875d532011-11-07 22:33:49 -06003556
3557 else
John Bowler7875d532011-11-07 22:33:49 -06003558 mode = PNG_ALPHA_PNG;
John Bowler18c5cfa2011-11-16 14:26:34 -06003559
3560 output_gamma = PNG_GAMMA_LINEAR;
3561 }
3562
3563 else
3564 {
3565 mode = PNG_ALPHA_PNG;
3566 output_gamma = PNG_DEFAULT_sRGB;
3567 }
3568
3569 /* If 'do_local_background' is set check for the presence of gamma
3570 * correction; this is part of the work-round for the libpng bug
3571 * described above.
3572 *
3573 * TODO: fix libpng and remove this.
3574 */
3575 if (do_local_background)
3576 {
3577 png_fixed_point gtest;
3578
3579 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3580 * gamma correction, the screen gamma hasn't been set on png_struct
3581 * yet; it's set below. png_struct::gamma, however, is set to the
3582 * final value.
3583 */
3584 if (png_muldiv(&gtest, output_gamma, png_ptr->gamma, PNG_FP_1) &&
3585 !png_gamma_significant(gtest))
3586 do_local_background = 0;
3587
3588 else if (mode == PNG_ALPHA_STANDARD)
3589 {
3590 do_local_background = 2/*required*/;
3591 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
John Bowler7875d532011-11-07 22:33:49 -06003592 }
3593
John Bowler18c5cfa2011-11-16 14:26:34 -06003594 /* else leave as 1 for the checks below */
John Bowler3615d032011-11-08 10:38:09 -06003595 }
John Bowler7875d532011-11-07 22:33:49 -06003596
John Bowler3615d032011-11-08 10:38:09 -06003597 /* If the bit-depth changes then handle that here. */
3598 if (change & PNG_FORMAT_FLAG_LINEAR)
3599 {
3600 if (linear /*16-bit output*/)
3601 png_set_expand_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06003602
John Bowler3615d032011-11-08 10:38:09 -06003603 else /* 8-bit output */
3604 png_set_scale_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06003605
John Bowler3615d032011-11-08 10:38:09 -06003606 change &= ~PNG_FORMAT_FLAG_LINEAR;
John Bowler7875d532011-11-07 22:33:49 -06003607 }
3608
3609 /* Now the background/alpha channel changes. */
3610 if (change & PNG_FORMAT_FLAG_ALPHA)
3611 {
3612 /* Removing an alpha channel requires composition for the 8-bit
3613 * formats; for the 16-bit it is already done, above, by the
3614 * pre-multiplication and the channel just needs to be stripped.
3615 */
3616 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3617 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003618 /* If RGB->gray is happening the alpha channel must be left and the
3619 * operation completed locally.
3620 *
3621 * TODO: fix libpng and remove this.
3622 */
3623 if (do_local_background)
3624 do_local_background = 2/*required*/;
3625
John Bowler3615d032011-11-08 10:38:09 -06003626 /* 16-bit output: just remove the channel */
John Bowler18c5cfa2011-11-16 14:26:34 -06003627 else if (linear) /* compose on black (well, pre-multiply) */
John Bowler7875d532011-11-07 22:33:49 -06003628 png_set_strip_alpha(png_ptr);
3629
John Bowler3615d032011-11-08 10:38:09 -06003630 /* 8-bit output: do an appropriate compose */
John Bowler7875d532011-11-07 22:33:49 -06003631 else if (display->background != NULL)
3632 {
3633 png_color_16 c;
3634
3635 c.index = 0; /*unused*/
3636 c.red = display->background->red;
3637 c.green = display->background->green;
3638 c.blue = display->background->blue;
3639 c.gray = display->background->green;
3640
3641 /* This is always an 8-bit sRGB value, using the 'green' channel
3642 * for gray is much better than calculating the luminance here;
3643 * we can get off-by-one errors in that calculation relative to
3644 * the app expectations and that will show up in transparent
3645 * pixels.
3646 */
3647 png_set_background_fixed(png_ptr, &c,
3648 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
3649 0/*gamma: not used*/);
3650 }
3651
3652 else /* compose on row: implemented below. */
3653 {
3654 do_local_compose = 1;
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003655 /* This leaves the alpha channel in the output, so it has to be
John Bowler7875d532011-11-07 22:33:49 -06003656 * removed by the code below. Set the encoding to the 'OPTIMIZE'
3657 * one so the code only has to hack on the pixels that require
3658 * composition.
3659 */
John Bowler3615d032011-11-08 10:38:09 -06003660 mode = PNG_ALPHA_OPTIMIZED;
John Bowler7875d532011-11-07 22:33:49 -06003661 }
3662 }
3663
3664 else /* output needs an alpha channel */
3665 {
3666 /* This is tricky because it happens before the swap operation has
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003667 * been accomplished; however, the swap does *not* swap the added
John Bowlere6fb6912011-11-08 21:35:16 -06003668 * alpha channel (weird API), so it must be added in the correct
3669 * place.
John Bowler7875d532011-11-07 22:33:49 -06003670 */
John Bowler3615d032011-11-08 10:38:09 -06003671 png_uint_32 filler; /* opaque filler */
John Bowlere6fb6912011-11-08 21:35:16 -06003672 int where;
John Bowler3615d032011-11-08 10:38:09 -06003673
3674 if (linear)
3675 filler = 65535;
3676
3677 else
3678 filler = 255;
3679
John Bowlere6fb6912011-11-08 21:35:16 -06003680# ifdef PNG_FORMAT_AFIRST_SUPPORTED
3681 if (format & PNG_FORMAT_FLAG_AFIRST)
3682 {
3683 where = PNG_FILLER_BEFORE;
3684 change &= ~PNG_FORMAT_FLAG_AFIRST;
3685 }
3686
3687 else
3688# endif
3689 where = PNG_FILLER_AFTER;
3690
3691 png_set_add_alpha(png_ptr, filler, where);
John Bowler7875d532011-11-07 22:33:49 -06003692 }
3693
John Bowlere6fb6912011-11-08 21:35:16 -06003694 /* This stops the (irrelevant) call to swap_alpha below. */
John Bowler7875d532011-11-07 22:33:49 -06003695 change &= ~PNG_FORMAT_FLAG_ALPHA;
3696 }
3697
John Bowler3615d032011-11-08 10:38:09 -06003698 /* Now set the alpha mode correctly; this is always done, even if there is
3699 * no alpha channel in either the input or the output because it correctly
3700 * sets the output gamma.
3701 */
3702 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
3703
John Bowler7875d532011-11-07 22:33:49 -06003704# ifdef PNG_FORMAT_BGR_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06003705 if (change & PNG_FORMAT_FLAG_BGR)
John Bowler7875d532011-11-07 22:33:49 -06003706 {
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003707 /* Check only the output format; PNG is never BGR; don't do this if
John Bowler7875d532011-11-07 22:33:49 -06003708 * the output is gray, but fix up the 'format' value in that case.
3709 */
3710 if (format & PNG_FORMAT_FLAG_COLOR)
3711 png_set_bgr(png_ptr);
3712
3713 else
3714 format &= ~PNG_FORMAT_FLAG_BGR;
3715
3716 change &= ~PNG_FORMAT_FLAG_BGR;
3717 }
3718# endif
3719
3720# ifdef PNG_FORMAT_AFIRST_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06003721 if (change & PNG_FORMAT_FLAG_AFIRST)
John Bowler7875d532011-11-07 22:33:49 -06003722 {
3723 /* Only relevant if there is an alpha channel - it's particularly
3724 * important to handle this correctly because do_local_compose may
3725 * be set above and then libpng will keep the alpha channel for this
3726 * code to remove.
3727 */
3728 if (format & PNG_FORMAT_FLAG_ALPHA)
John Bowler18c5cfa2011-11-16 14:26:34 -06003729 {
3730 /* Disable this if doing a local background,
3731 * TODO: remove this when local background is no longer required.
3732 */
3733 if (do_local_background != 2)
3734 png_set_swap_alpha(png_ptr);
3735 }
John Bowler7875d532011-11-07 22:33:49 -06003736
3737 else
3738 format &= ~PNG_FORMAT_FLAG_AFIRST;
3739
3740 change &= ~PNG_FORMAT_FLAG_AFIRST;
3741 }
3742# endif
3743
3744 /* If the *output* is 16-bit then we need to check for a byte-swap on this
3745 * architecture.
3746 */
3747 if (linear)
3748 {
3749 PNG_CONST png_uint_16 le = 0x0001;
3750
3751 if (*(png_const_bytep)&le)
3752 png_set_swap(png_ptr);
3753 }
3754
3755 /* If change is not now 0 some transformation is missing - error out. */
3756 if (change)
3757 png_error(png_ptr, "png_read_image: unsupported transformation");
3758 }
3759
John Bowler5bc90382012-01-23 22:43:22 -06003760 PNG_SKIP_CHUNKS(png_ptr);
John Bowler89c2f842011-11-16 12:04:39 -06003761
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003762 /* Update the 'info' structure and make sure the result is as required; first
John Bowler7875d532011-11-07 22:33:49 -06003763 * make sure to turn on the interlace handling if it will be required
3764 * (because it can't be turned on *after* the call to png_read_update_info!)
John Bowler18c5cfa2011-11-16 14:26:34 -06003765 *
3766 * TODO: remove the do_local_background fixup below.
John Bowler7875d532011-11-07 22:33:49 -06003767 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003768 if (!do_local_compose && do_local_background != 2)
John Bowler7875d532011-11-07 22:33:49 -06003769 passes = png_set_interlace_handling(png_ptr);
3770
3771 png_read_update_info(png_ptr, info_ptr);
3772
3773 {
3774 png_uint_32 info_format = 0;
3775
3776 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
3777 info_format |= PNG_FORMAT_FLAG_COLOR;
3778
3779 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
3780 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003781 /* do_local_compose removes this channel below. */
John Bowler7875d532011-11-07 22:33:49 -06003782 if (!do_local_compose)
John Bowler18c5cfa2011-11-16 14:26:34 -06003783 {
3784 /* do_local_background does the same if required. */
3785 if (do_local_background != 2 ||
3786 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
3787 info_format |= PNG_FORMAT_FLAG_ALPHA;
3788 }
John Bowler7875d532011-11-07 22:33:49 -06003789 }
3790
3791 else if (do_local_compose) /* internal error */
3792 png_error(png_ptr, "png_image_read: alpha channel lost");
3793
3794 if (info_ptr->bit_depth == 16)
3795 info_format |= PNG_FORMAT_FLAG_LINEAR;
3796
3797# ifdef PNG_FORMAT_BGR_SUPPORTED
3798 if (png_ptr->transformations & PNG_BGR)
3799 info_format |= PNG_FORMAT_FLAG_BGR;
3800# endif
3801
3802# ifdef PNG_FORMAT_AFIRST_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06003803 if (png_ptr->transformations & PNG_SWAP_ALPHA ||
3804 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
3805 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
John Bowler7875d532011-11-07 22:33:49 -06003806 info_format |= PNG_FORMAT_FLAG_AFIRST;
3807# endif
3808
3809 /* This is actually an internal error. */
3810 if (info_format != format)
3811 png_error(png_ptr, "png_read_image: invalid transformations");
3812 }
3813
3814 /* Now read the rows. If do_local_compose is set then it is necessary to use
3815 * a local row buffer. The output will be GA, RGBA or BGRA and must be
3816 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
3817 * display acts as a flag.
3818 */
3819 {
John Bowler4fa96a42011-11-16 16:39:16 -06003820 png_bytep first_row = png_voidcast(png_bytep, display->buffer);
John Bowler7875d532011-11-07 22:33:49 -06003821 ptrdiff_t row_bytes = display->row_stride;
3822
3823 if (linear)
John Bowler5bc90382012-01-23 22:43:22 -06003824 row_bytes *= 2;
John Bowler7875d532011-11-07 22:33:49 -06003825
3826 /* The following expression is designed to work correctly whether it gives
3827 * a signed or an unsigned result.
3828 */
3829 if (row_bytes < 0)
3830 first_row += (image->height-1) * (-row_bytes);
3831
3832 display->first_row = first_row;
3833 display->row_bytes = row_bytes;
3834 }
3835
3836 if (do_local_compose)
3837 {
3838 int result;
John Bowler4fa96a42011-11-16 16:39:16 -06003839 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
3840 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler7875d532011-11-07 22:33:49 -06003841
3842 display->local_row = row;
3843 result = png_safe_execute(image, png_image_read_composite, display);
3844 display->local_row = NULL;
3845 png_free(png_ptr, row);
3846
3847 return result;
3848 }
3849
John Bowler18c5cfa2011-11-16 14:26:34 -06003850 else if (do_local_background == 2)
3851 {
3852 int result;
John Bowler4fa96a42011-11-16 16:39:16 -06003853 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
3854 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler18c5cfa2011-11-16 14:26:34 -06003855
3856 display->local_row = row;
3857 result = png_safe_execute(image, png_image_read_background, display);
3858 display->local_row = NULL;
3859 png_free(png_ptr, row);
3860
3861 return result;
3862 }
3863
John Bowler7875d532011-11-07 22:33:49 -06003864 else
3865 {
3866 png_alloc_size_t row_bytes = display->row_bytes;
3867
3868 while (--passes >= 0)
3869 {
3870 png_uint_32 y = image->height;
3871 png_bytep row = display->first_row;
3872
3873 while (y-- > 0)
3874 {
3875 png_read_row(png_ptr, row, NULL);
3876 row += row_bytes;
3877 }
3878 }
3879
3880 return 1;
3881 }
3882}
3883
3884int PNGAPI
3885png_image_finish_read(png_imagep image, png_colorp background, void *buffer,
John Bowler5bc90382012-01-23 22:43:22 -06003886 png_int_32 row_stride, void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06003887{
John Bowler5bc90382012-01-23 22:43:22 -06003888 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06003889 {
3890 png_uint_32 check;
3891
John Bowler3706d732011-11-21 10:28:06 -06003892 if (row_stride == 0)
3893 row_stride = PNG_IMAGE_ROW_STRIDE(*image);
3894
John Bowler7875d532011-11-07 22:33:49 -06003895 if (row_stride < 0)
3896 check = -row_stride;
3897
3898 else
3899 check = row_stride;
3900
John Bowler5bc90382012-01-23 22:43:22 -06003901 if (image->opaque != NULL && buffer != NULL &&
3902 check >= PNG_IMAGE_ROW_STRIDE(*image))
John Bowler7875d532011-11-07 22:33:49 -06003903 {
John Bowler5bc90382012-01-23 22:43:22 -06003904 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
3905 (image->colormap_entries > 0 && colormap != NULL))
3906 {
3907 int result;
3908 png_image_read_control display;
John Bowler7875d532011-11-07 22:33:49 -06003909
John Bowler5bc90382012-01-23 22:43:22 -06003910 png_memset(&display, 0, sizeof display);
3911 display.image = image;
3912 display.buffer = buffer;
3913 display.row_stride = row_stride;
3914 display.colormap = colormap;
3915 display.background = background;
3916 display.local_row = NULL;
3917
3918 /* Choose the correct 'end' routine; for the color-map case all the
3919 * setup has already been done.
3920 */
3921 if (image->format & PNG_FORMAT_FLAG_COLORMAP)
3922 result =
3923 png_safe_execute(image, png_image_read_colormap, &display) &&
3924 png_safe_execute(image, png_image_read_colormapped, &display);
3925
3926 else
3927 result =
3928 png_safe_execute(image, png_image_read_direct, &display);
3929
3930 png_image_free(image);
3931 return result;
3932 }
3933
3934 else
3935 return png_image_error(image,
3936 "png_image_finish_read[color-map]: no color-map");
John Bowler7875d532011-11-07 22:33:49 -06003937 }
3938
3939 else
3940 return png_image_error(image,
3941 "png_image_finish_read: invalid argument");
3942 }
3943
3944 return 0;
3945}
3946
3947#endif /* PNG_SIMPLIFIED_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06003948#endif /* PNG_READ_SUPPORTED */