blob: 3996041218ad7480c086ff0e74425765683cf09a [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 Bowlerb5d00512012-03-09 09:15:18 -060051 png_ptr->mode = PNG_IS_READ_STRUCT;
52
John Bowleraa816c42012-03-16 07:39:49 -050053 /* Added in libpng-1.6.0; this can be used to detect a read structure if
John Bowlerb5d00512012-03-09 09:15:18 -060054 * required (it will be zero in a write structure.)
55 */
56# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
57 png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE;
58# endif
John Bowleraa816c42012-03-16 07:39:49 -050059
60# ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
61 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
John Bowler2bc76ff2012-03-18 22:37:25 -050062
63 /* In stable builds only warn if an application error can be completely
64 * handled.
65 */
John Bowler921648a2012-03-28 23:36:12 -050066# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
John Bowler2bc76ff2012-03-18 22:37:25 -050067 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
68# endif
John Bowleraa816c42012-03-16 07:39:49 -050069# endif
70
John Bowler42a2b552012-03-05 20:57:40 -060071 /* TODO: delay this, it can be done in png_init_io (if the app doesn't
72 * do it itself) avoiding setting the default function if it is not
73 * required.
John Bowler1d7f56a2011-12-21 20:14:23 -060074 */
John Bowler42a2b552012-03-05 20:57:40 -060075 png_set_read_fn(png_ptr, NULL, NULL);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050076 }
77
John Bowler42a2b552012-03-05 20:57:40 -060078 return png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -050079}
80
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050081
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050082#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060083/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -060084 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060085 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -050086 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060087 * via png_set_sig_bytes(), and we will only check the remaining bytes
88 * here. The application can then have access to the signature bytes we
89 * read if it is determined that this isn't a valid PNG file.
90 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050091void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -060092png_read_info(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050093{
Glenn Randers-Pehrsonc81bb8a2009-08-15 22:02:26 -050094 png_debug(1, "in png_read_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -050095
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050096 if (png_ptr == NULL || info_ptr == NULL)
97 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -050098
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -060099 /* Read and check the PNG file signature. */
100 png_read_sig(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500101
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500102 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500103 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500104 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500105 png_uint_32 chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500106
107 /* This should be a binary subdivision search or a hash for
108 * matching the chunk name rather than a linear search.
109 */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500110 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600111 if (png_ptr->mode & PNG_AFTER_IDAT)
112 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500113
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500114 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600115 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500116
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500117 else if (chunk_name == png_IEND)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600118 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500119
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600120#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500121 else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
122 PNG_HANDLE_CHUNK_AS_DEFAULT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600123 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500124 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600125 png_ptr->mode |= PNG_HAVE_IDAT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500126
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600127 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500128
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500129 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600130 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500131
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500132 else if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600133 {
134 if (!(png_ptr->mode & PNG_HAVE_IHDR))
135 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500136
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600137 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600138 !(png_ptr->mode & PNG_HAVE_PLTE))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600139 png_error(png_ptr, "Missing PLTE before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500140
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600141 break;
142 }
143 }
144#endif
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500145 else if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600146 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500147
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500148 else if (chunk_name == png_IDAT)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500149 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500150 if (!(png_ptr->mode & PNG_HAVE_IHDR))
151 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500152
Guy Schalnate5a37791996-06-05 15:50:50 -0500153 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600154 !(png_ptr->mode & PNG_HAVE_PLTE))
Guy Schalnate5a37791996-06-05 15:50:50 -0500155 png_error(png_ptr, "Missing PLTE before IDAT");
156
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500157 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500158 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500159 break;
160 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500161
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500162#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500163 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500164 png_handle_bKGD(png_ptr, info_ptr, length);
165#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500166
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500167#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500168 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500169 png_handle_cHRM(png_ptr, info_ptr, length);
170#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500171
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500172#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500173 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500174 png_handle_gAMA(png_ptr, info_ptr, length);
175#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500176
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500177#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500178 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500179 png_handle_hIST(png_ptr, info_ptr, length);
180#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500181
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500182#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500183 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500184 png_handle_oFFs(png_ptr, info_ptr, length);
185#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500186
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500187#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500188 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500189 png_handle_pCAL(png_ptr, info_ptr, length);
190#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500191
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500192#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500193 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600194 png_handle_sCAL(png_ptr, info_ptr, length);
195#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500196
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500197#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500198 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500199 png_handle_pHYs(png_ptr, info_ptr, length);
200#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500201
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500202#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500203 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500204 png_handle_sBIT(png_ptr, info_ptr, length);
205#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500206
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500207#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500208 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600209 png_handle_sRGB(png_ptr, info_ptr, length);
210#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500211
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500212#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500213 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600214 png_handle_iCCP(png_ptr, info_ptr, length);
215#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500216
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500217#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500218 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600219 png_handle_sPLT(png_ptr, info_ptr, length);
220#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500221
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500222#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500223 else if (chunk_name == png_tEXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500224 png_handle_tEXt(png_ptr, info_ptr, length);
225#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500226
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500227#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500228 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 png_handle_tIME(png_ptr, info_ptr, length);
230#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500231
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500232#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500233 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500234 png_handle_tRNS(png_ptr, info_ptr, length);
235#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500236
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500237#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500238 else if (chunk_name == png_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500239 png_handle_zTXt(png_ptr, info_ptr, length);
240#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500241
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500242#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500243 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600244 png_handle_iTXt(png_ptr, info_ptr, length);
245#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500246
Guy Schalnat0d580581995-07-20 02:43:20 -0500247 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600248 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500249 }
250}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500251#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500252
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500253/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500254void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600255png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500256{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500257 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500258
John Bowler66efa242012-03-18 21:10:29 -0500259 if (png_ptr != NULL)
260 {
261 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
262 {
263 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500264
John Bowler66efa242012-03-18 21:10:29 -0500265# ifdef PNG_READ_TRANSFORMS_SUPPORTED
266 png_read_transform_info(png_ptr, info_ptr);
267# else
268 PNG_UNUSED(info_ptr)
269# endif
270 }
Glenn Randers-Pehrson4cfdb3c2009-11-26 11:49:37 -0600271
John Bowler66efa242012-03-18 21:10:29 -0500272 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
273 else
John Bowler2bc76ff2012-03-18 22:37:25 -0500274 png_app_error(png_ptr,
John Bowler66efa242012-03-18 21:10:29 -0500275 "png_read_update_info/png_start_read_image: duplicate call");
276 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500277}
278
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500279#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600280/* Initialize palette, background, etc, after transformations
281 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500282 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600283 * If the user doesn't call this, we will do it ourselves.
284 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500285void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600286png_start_read_image(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500287{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500288 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500289
Glenn Randers-Pehrson3dbfd302011-10-13 17:24:36 -0500290 if (png_ptr != NULL)
John Bowler66efa242012-03-18 21:10:29 -0500291 {
292 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
293 png_read_start_row(png_ptr);
294
295 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
296 else
John Bowler2bc76ff2012-03-18 22:37:25 -0500297 png_app_error(png_ptr,
John Bowler66efa242012-03-18 21:10:29 -0500298 "png_start_read_image/png_read_update_info: duplicate call");
299 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500300}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500301#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500302
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500303#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500304void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600305png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500306{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500307 png_row_info row_info;
308
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500309 if (png_ptr == NULL)
310 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500311
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500312 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -0500313 (unsigned long)png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500314
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500315 /* png_read_start_row sets the information (in particular iwidth) for this
316 * interlace pass.
317 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500318 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500319 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500320
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500321 /* 1.5.6: row_info moved out of png_struct to a local here. */
322 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
323 row_info.color_type = png_ptr->color_type;
324 row_info.bit_depth = png_ptr->bit_depth;
325 row_info.channels = png_ptr->channels;
326 row_info.pixel_depth = png_ptr->pixel_depth;
327 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
328
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500329 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
330 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500331 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500332#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
333 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500334 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500335#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500336
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500337#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
338 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500339 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500340#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500341
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600342#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
343 !defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500344 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500345 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500346#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500347
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500348#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
349 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500350 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500351#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500352
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500353#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
354 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500355 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500356#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500357
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500358#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
359 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500360 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500361#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500362
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500363#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
364 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500365 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500366#endif
367 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500368
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500369#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500370 /* If interlaced and we do not need a new row, combine row and return.
371 * Notice that the pixels we have from previous rows have been transformed
372 * already; we can only combine like with like (transformed or
373 * untransformed) and, because of the libpng API for interlaced images, this
374 * means we must transform before de-interlacing.
375 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500376 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
377 {
378 switch (png_ptr->pass)
379 {
380 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600381 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500382 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500384 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600385 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500386 return;
387 }
388 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500389
Guy Schalnat0d580581995-07-20 02:43:20 -0500390 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600391 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500392 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500393 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500394 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500395
Guy Schalnat0d580581995-07-20 02:43:20 -0500396 png_read_finish_row(png_ptr);
397 return;
398 }
399 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500400
Guy Schalnat0d580581995-07-20 02:43:20 -0500401 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600402 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500403 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500404 if (dsp_row != NULL && (png_ptr->row_number & 4))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500405 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500406
Guy Schalnat0d580581995-07-20 02:43:20 -0500407 png_read_finish_row(png_ptr);
408 return;
409 }
410 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500411
Guy Schalnat0d580581995-07-20 02:43:20 -0500412 case 3:
413 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
414 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500415 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500416 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500417
Guy Schalnat0d580581995-07-20 02:43:20 -0500418 png_read_finish_row(png_ptr);
419 return;
420 }
421 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500422
Guy Schalnat0d580581995-07-20 02:43:20 -0500423 case 4:
424 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600425 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500426 if (dsp_row != NULL && (png_ptr->row_number & 2))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500427 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500428
Guy Schalnat0d580581995-07-20 02:43:20 -0500429 png_read_finish_row(png_ptr);
430 return;
431 }
432 break;
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -0600433
Guy Schalnat0d580581995-07-20 02:43:20 -0500434 case 5:
435 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
436 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500437 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500438 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500439
Guy Schalnat0d580581995-07-20 02:43:20 -0500440 png_read_finish_row(png_ptr);
441 return;
442 }
443 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500444
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600445 default:
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600446 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500447 if (!(png_ptr->row_number & 1))
448 {
449 png_read_finish_row(png_ptr);
450 return;
451 }
452 break;
453 }
454 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500455#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500456
Guy Schalnate5a37791996-06-05 15:50:50 -0500457 if (!(png_ptr->mode & PNG_HAVE_IDAT))
458 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500459
John Bowlerb5d00512012-03-09 09:15:18 -0600460 /* Fill the row with IDAT data: */
461 png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500462
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500463 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
464 {
465 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
Mans Rullgardd3a94802011-11-03 00:47:55 -0500466 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500467 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
468 else
469 png_error(png_ptr, "bad adaptive filter value");
470 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500471
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500472 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
473 * 1.5.6, while the buffer really is this big in current versions of libpng
474 * it may not be in the future, so this was changed just to copy the
475 * interlaced count:
476 */
477 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600478
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500479#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500480 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600481 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600482 {
483 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500484 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600485 }
486#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500487
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500488
John Bowler4a12f4a2011-04-17 18:34:22 -0500489#ifdef PNG_READ_TRANSFORMS_SUPPORTED
John Bowler9b872f42011-02-12 09:00:16 -0600490 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500491 png_do_read_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500492#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500493
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500494 /* The transformed pixel depth should match the depth now in row_info. */
495 if (png_ptr->transformed_pixel_depth == 0)
496 {
497 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
498 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
499 png_error(png_ptr, "sequential row overflow");
500 }
501
502 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
503 png_error(png_ptr, "internal sequential row size calculation error");
504
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500505#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500506 /* Blow up interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500507 if (png_ptr->interlaced &&
508 (png_ptr->transformations & PNG_INTERLACE))
509 {
510 if (png_ptr->pass < 6)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500511 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
512 png_ptr->transformations);
Guy Schalnat0d580581995-07-20 02:43:20 -0500513
Andreas Dilger47a0c421997-05-16 02:46:07 -0500514 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500515 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500516
Andreas Dilger47a0c421997-05-16 02:46:07 -0500517 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500518 png_combine_row(png_ptr, row, 0/*row*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500519 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500520
Guy Schalnat0d580581995-07-20 02:43:20 -0500521 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500522#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500523 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500524 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500525 png_combine_row(png_ptr, row, -1/*ignored*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500526
Andreas Dilger47a0c421997-05-16 02:46:07 -0500527 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500528 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500529 }
530 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600531
532 if (png_ptr->read_row_fn != NULL)
533 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson67dba432012-02-16 20:15:13 -0600534
Guy Schalnat0d580581995-07-20 02:43:20 -0500535}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500536#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500537
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500538#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500539/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600540 * and png_set_interlace_handling() has been called, the rows need to
541 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500542 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600543 * called, the rows contents must be initialized to the contents of the
544 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600545 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600546 * "row" holds the actual image, and pixels are placed in it
547 * as they arrive. If the image is displayed after each pass, it will
548 * appear to "sparkle" in. "display_row" can be used to display a
549 * "chunky" progressive image, with finer detail added as it becomes
550 * available. If you do not want this "chunky" display, you may pass
551 * NULL for display_row. If you do not want the sparkle display, and
552 * you have not called png_handle_alpha(), you may pass NULL for rows.
553 * If you have called png_handle_alpha(), and the image has either an
554 * alpha channel or a transparency chunk, you must provide a buffer for
555 * rows. In this case, you do not have to provide a display_row buffer
556 * also, but you may. If the image is not interlaced, or if you have
557 * not called png_set_interlace_handling(), the display_row buffer will
558 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500559 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600560 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600561 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600562
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500563void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600564png_read_rows(png_structrp png_ptr, png_bytepp row,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600565 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500566{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600567 png_uint_32 i;
568 png_bytepp rp;
569 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500570
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500571 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500572
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500573 if (png_ptr == NULL)
574 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500575
Guy Schalnat0f716451995-11-28 11:22:13 -0600576 rp = row;
577 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500578 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500579 for (i = 0; i < num_rows; i++)
580 {
581 png_bytep rptr = *rp++;
582 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600583
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500584 png_read_row(png_ptr, rptr, dptr);
585 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500586
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500587 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500588 for (i = 0; i < num_rows; i++)
589 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500590 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500591 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500592 rp++;
593 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500594
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500595 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500596 for (i = 0; i < num_rows; i++)
597 {
598 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500599 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500600 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500601 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500602}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500603#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500604
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500605#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500606/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500607 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600608 * initialize the image to the current image that PNG will be overlaying.
609 * We set the num_rows again here, in case it was incorrectly set in
610 * png_read_start_row() by a call to png_read_update_info() or
611 * png_start_read_image() if png_set_interlace_handling() wasn't called
612 * prior to either of these functions like it should have been. You can
613 * only call this function once. If you desire to have an image for
614 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500615 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600616 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600617 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500618void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600619png_read_image(png_structrp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500620{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500621 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500622 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600623 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500624
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500625 png_debug(1, "in png_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500626
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500627 if (png_ptr == NULL)
628 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500629
630#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500631 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
632 {
633 pass = png_set_interlace_handling(png_ptr);
634 /* And make sure transforms are initialized. */
635 png_start_read_image(png_ptr);
636 }
637 else
638 {
Glenn Randers-Pehrsone15a96b2011-01-14 15:47:37 -0600639 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500640 {
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500641 /* Caller called png_start_read_image or png_read_update_info without
642 * first turning on the PNG_INTERLACE transform. We can fix this here,
643 * but the caller should do it!
644 */
645 png_warning(png_ptr, "Interlace handling should be turned on when "
646 "using png_read_image");
647 /* Make sure this is set correctly */
648 png_ptr->num_rows = png_ptr->height;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500649 }
650
651 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
652 * the above error case.
653 */
654 pass = png_set_interlace_handling(png_ptr);
655 }
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500656#else
657 if (png_ptr->interlaced)
658 png_error(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600659 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500660
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500661 pass = 1;
662#endif
663
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500664 image_height=png_ptr->height;
Guy Schalnate5a37791996-06-05 15:50:50 -0500665
Guy Schalnat0d580581995-07-20 02:43:20 -0500666 for (j = 0; j < pass; j++)
667 {
668 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500669 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500670 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500671 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500672 rp++;
673 }
674 }
675}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500676#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500677
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500678#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500679/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600680 * file, will verify the end is accurate, and will read any comments
681 * or time information at the end of the file, if info is not NULL.
682 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500683void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600684png_read_end(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500685{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500686 png_debug(1, "in png_read_end");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500687
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500688 if (png_ptr == NULL)
689 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500690
John Bowlerb5d00512012-03-09 09:15:18 -0600691 /* If png_read_end is called in the middle of reading the rows there may
692 * still be pending IDAT data and an owned zstream. Deal with this here.
693 */
694 png_read_finish_IDAT(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500695
John Bowler6f237b62012-03-02 13:13:15 -0600696#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
697 /* Report invalid palette index; added at libng-1.5.10 */
698 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
699 png_ptr->num_palette_max > png_ptr->num_palette)
Glenn Randers-Pehrson945cb1f2012-03-10 08:48:04 -0600700 png_benign_error(png_ptr, "Read palette index exceeding num_palette");
John Bowler6f237b62012-03-02 13:13:15 -0600701#endif
702
Guy Schalnat0d580581995-07-20 02:43:20 -0500703 do
704 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500705 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500706 png_uint_32 chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500707
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500708 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600709 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500710
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500711 else if (chunk_name == png_IEND)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600712 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500713
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600714#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500715 else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
716 PNG_HANDLE_CHUNK_AS_DEFAULT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600717 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500718 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600719 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500720 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500721 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600722 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600723 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500724 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600725 png_ptr->mode |= PNG_HAVE_PLTE;
726 }
727#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500728
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500729 else if (chunk_name == png_IDAT)
Guy Schalnat0d580581995-07-20 02:43:20 -0500730 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731 /* Zero length IDATs are legal after the last IDAT has been
732 * read, but not after other chunks have been read.
733 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500734 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500735 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500736
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500737 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500738 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500739 else if (chunk_name == png_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500740 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500741
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500742#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500743 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500744 png_handle_bKGD(png_ptr, info_ptr, length);
745#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500746
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500747#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500748 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500749 png_handle_cHRM(png_ptr, info_ptr, length);
750#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500751
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500752#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500753 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500754 png_handle_gAMA(png_ptr, info_ptr, length);
755#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500756
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500757#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500758 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500759 png_handle_hIST(png_ptr, info_ptr, length);
760#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500761
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500762#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500763 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500764 png_handle_oFFs(png_ptr, info_ptr, length);
765#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500766
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500767#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500768 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500769 png_handle_pCAL(png_ptr, info_ptr, length);
770#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500771
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500772#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500773 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600774 png_handle_sCAL(png_ptr, info_ptr, length);
775#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500776
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500777#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500778 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500779 png_handle_pHYs(png_ptr, info_ptr, length);
780#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500781
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500782#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500783 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500784 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500785#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500786
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500787#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500788 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600789 png_handle_sRGB(png_ptr, info_ptr, length);
790#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500791
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500792#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500793 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600794 png_handle_iCCP(png_ptr, info_ptr, length);
795#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500796
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500797#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500798 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600799 png_handle_sPLT(png_ptr, info_ptr, length);
800#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500801
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500802#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500803 else if (chunk_name == png_tEXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600804 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500805#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500806
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500807#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500808 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500809 png_handle_tIME(png_ptr, info_ptr, length);
810#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500811
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500812#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500813 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500814 png_handle_tRNS(png_ptr, info_ptr, length);
815#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500816
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500817#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500818 else if (chunk_name == png_zTXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600819 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500820#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500821
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500822#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500823 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600824 png_handle_iTXt(png_ptr, info_ptr, length);
825#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500826
Guy Schalnat0d580581995-07-20 02:43:20 -0500827 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600828 png_handle_unknown(png_ptr, info_ptr, length);
829 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500830}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500831#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500832
John Bowler1d7f56a2011-12-21 20:14:23 -0600833/* Free all memory used in the read struct */
834static void
John Bowler5d567862011-12-24 09:12:00 -0600835png_read_destroy(png_structrp png_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500836{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500837 png_debug(1, "in png_read_destroy");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500838
John Bowler07772cb2011-10-14 18:19:47 -0500839#ifdef PNG_READ_GAMMA_SUPPORTED
840 png_destroy_gamma_table(png_ptr);
841#endif
842
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -0500843 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgard1c422762011-10-17 16:52:19 -0500844 png_free(png_ptr, png_ptr->big_prev_row);
John Bowlerb5d00512012-03-09 09:15:18 -0600845 png_free(png_ptr, png_ptr->read_buffer);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500846
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500847#ifdef PNG_READ_QUANTIZE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600848 png_free(png_ptr, png_ptr->palette_lookup);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500849 png_free(png_ptr, png_ptr->quantize_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500850#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500851
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600852 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600853 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600854 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500855
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600856#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500857 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600858 if (png_ptr->free_me & PNG_FREE_TRNS)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500859 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600860 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500861#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500862
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600863 inflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500864
Guy Schalnat6d764711995-12-19 03:22:19 -0600865#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600866 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -0600867#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500868
John Bowler40b26032011-12-22 08:09:15 -0600869#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
870 png_free(png_ptr, png_ptr->unknown_chunk.data);
871#endif
872
873#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
874 png_free(png_ptr, png_ptr->chunk_list);
875#endif
876
John Bowler1d7f56a2011-12-21 20:14:23 -0600877 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
878 * callbacks are still set at this point. They are required to complete the
879 * destruction of the png_struct itself.
Guy Schalnate5a37791996-06-05 15:50:50 -0500880 */
John Bowler1d7f56a2011-12-21 20:14:23 -0600881}
Guy Schalnate5a37791996-06-05 15:50:50 -0500882
John Bowler1d7f56a2011-12-21 20:14:23 -0600883/* Free all memory used by the read */
884void PNGAPI
885png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
886 png_infopp end_info_ptr_ptr)
887{
John Bowler5d567862011-12-24 09:12:00 -0600888 png_structrp png_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500889
John Bowler1d7f56a2011-12-21 20:14:23 -0600890 png_debug(1, "in png_destroy_read_struct");
Guy Schalnate5a37791996-06-05 15:50:50 -0500891
John Bowler1d7f56a2011-12-21 20:14:23 -0600892 if (png_ptr_ptr != NULL)
893 png_ptr = *png_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500894
John Bowler1d7f56a2011-12-21 20:14:23 -0600895 if (png_ptr == NULL)
896 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600897
John Bowler1d7f56a2011-12-21 20:14:23 -0600898 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
899 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
900 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
901 */
902 png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
903 png_destroy_info_struct(png_ptr, info_ptr_ptr);
904
905 *png_ptr_ptr = NULL;
906 png_read_destroy(png_ptr);
907 png_destroy_png_struct(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500908}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600909
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500910void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600911png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600912{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500913 if (png_ptr == NULL)
914 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500915
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600916 png_ptr->read_row_fn = read_row_fn;
917}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600918
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500919
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500920#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500921#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500922void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600923png_read_png(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600924 int transforms,
925 voidp params)
926{
927 int row;
928
John Bowler6a6d79f2011-02-12 08:56:40 -0600929 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500930 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600931
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500932 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600933 * PNG file before the first IDAT (image data chunk).
934 */
935 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500936 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
937 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600938
939 /* -------------- image transformations start here ------------------- */
940
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500941#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -0500942 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500943 */
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -0500944 if (transforms & PNG_TRANSFORM_SCALE_16)
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500945 {
Glenn Randers-Pehrson6da2f2d2011-06-17 23:07:16 -0500946 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
947 * did in earlier versions, while "scale_16" is now more accurate.
948 */
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500949 png_set_scale_16(png_ptr);
950 }
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500951#endif
952
953#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
John Bowler8d261262011-06-18 13:37:11 -0500954 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
955 * latter by doing SCALE first. This is ok and allows apps not to check for
956 * which is supported to get the right answer.
957 */
958 if (transforms & PNG_TRANSFORM_STRIP_16)
959 png_set_strip_16(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600960#endif
961
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500962#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500963 /* Strip alpha bytes from the input data without combining with
964 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600965 */
966 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500967 png_set_strip_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600968#endif
969
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600970#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500971 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600972 * byte into separate bytes (useful for paletted and grayscale images).
973 */
974 if (transforms & PNG_TRANSFORM_PACKING)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500975 png_set_packing(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600976#endif
977
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500978#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600979 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500980 * (not useful if you are using png_set_packing).
981 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600982 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500983 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600984#endif
985
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500986#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600987 /* Expand paletted colors into true RGB triplets
988 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
989 * Expand paletted or RGB images with transparency to full alpha
990 * channels so the data will be available as RGBA quartets.
991 */
992 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500993 if ((png_ptr->bit_depth < 8) ||
994 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
995 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600996 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600997#endif
998
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500999 /* We don't handle background color or gamma transformation or quantizing.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001000 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001001
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001002#ifdef PNG_READ_INVERT_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001003 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001004 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001005 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001006 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001007#endif
1008
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001009#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001010 /* If you want to shift the pixel values from the range [0,255] or
1011 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1012 * colors were originally in:
1013 */
1014 if ((transforms & PNG_TRANSFORM_SHIFT)
1015 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1016 {
1017 png_color_8p sig_bit;
1018
1019 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1020 png_set_shift(png_ptr, sig_bit);
1021 }
1022#endif
1023
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001024#ifdef PNG_READ_BGR_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001025 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001026 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001027 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001028#endif
1029
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001030#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001031 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001032 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001033 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001034#endif
1035
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001036#ifdef PNG_READ_SWAP_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -05001037 /* Swap bytes of 16-bit files to least significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001038 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001039 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001040#endif
1041
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001042/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001043#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001044 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001045 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001046 png_set_invert_alpha(png_ptr);
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001047#endif
1048
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001049/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001050#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001051 /* Expand grayscale image to RGB */
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001052 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001053 png_set_gray_to_rgb(png_ptr);
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001054#endif
1055
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001056/* Added at libpng-1.5.4 */
John Bowler96cec0e2011-05-08 22:48:12 -05001057#ifdef PNG_READ_EXPAND_16_SUPPORTED
1058 if (transforms & PNG_TRANSFORM_EXPAND_16)
1059 png_set_expand_16(png_ptr);
1060#endif
1061
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001062 /* We don't handle adding filler bytes */
1063
John Bowler6a6d79f2011-02-12 08:56:40 -06001064 /* We use png_read_image and rely on that for interlace handling, but we also
1065 * call png_read_update_info therefore must turn on interlace handling now:
1066 */
1067 (void)png_set_interlace_handling(png_ptr);
1068
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001069 /* Optional call to gamma correct and add the background to the palette
1070 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001071 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001072 */
1073 png_read_update_info(png_ptr, info_ptr);
1074
1075 /* -------------- image transformations end here ------------------- */
1076
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001077 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001078 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001079 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001080 png_uint_32 iptr;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001081
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001082 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001083 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001084 for (iptr=0; iptr<info_ptr->height; iptr++)
1085 info_ptr->row_pointers[iptr] = NULL;
1086
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001087 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001088
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001089 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001090 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001091 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001092 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001093
1094 png_read_image(png_ptr, info_ptr->row_pointers);
1095 info_ptr->valid |= PNG_INFO_IDAT;
1096
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001097 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001098 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001099
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001100 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1101 PNG_UNUSED(params)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001102
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001103}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001104#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001105#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
John Bowler7875d532011-11-07 22:33:49 -06001106
1107#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1108/* SIMPLIFIED READ
1109 *
1110 * This code currently relies on the sequential reader, though it could easily
1111 * be made to work with the progressive one.
1112 */
John Bowler5bc90382012-01-23 22:43:22 -06001113/* Arguments to png_image_finish_read: */
1114
1115/* Encoding of PNG data (used by the color-map code) */
John Bowleraa816c42012-03-16 07:39:49 -05001116/* TODO: change these, dang, ANSI-C reserves the 'E' namespace. */
John Bowler5bc90382012-01-23 22:43:22 -06001117# define E_NOTSET 0 /* File encoding not yet known */
1118# define E_sRGB 1 /* 8-bit encoded to sRGB gamma */
1119# define E_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1120# define E_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1121# define E_LINEAR8 4 /* 8-bit linear: only from a file value */
1122
1123/* Color-map processing: after libpng has run on the PNG image further
1124 * processing may be needed to conver the data to color-map indicies.
1125 */
1126#define PNG_CMAP_NONE 0
1127#define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1128#define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1129#define PNG_CMAP_RGB 3 /* Process RGB data */
1130#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1131
1132/* The following document where the background is for each processing case. */
1133#define PNG_CMAP_NONE_BACKGROUND 256
1134#define PNG_CMAP_GA_BACKGROUND 231
1135#define PNG_CMAP_TRANS_BACKGROUND 254
1136#define PNG_CMAP_RGB_BACKGROUND 256
1137#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1138
1139typedef struct
1140{
1141 /* Arguments: */
1142 png_imagep image;
1143 png_voidp buffer;
1144 png_int_32 row_stride;
1145 png_voidp colormap;
John Bowler40ca77a2012-01-28 23:19:42 -06001146 png_const_colorp background;
John Bowler5bc90382012-01-23 22:43:22 -06001147 /* Local variables: */
1148 png_bytep local_row;
1149 png_bytep first_row;
1150 ptrdiff_t row_bytes; /* step between rows */
1151 int file_encoding; /* E_ values above */
1152 png_fixed_point gamma_to_linear; /* For E_FILE, reciprocal of gamma */
1153 int colormap_processing; /* PNG_CMAP_ values above */
1154} png_image_read_control;
1155
John Bowler7875d532011-11-07 22:33:49 -06001156/* Do all the *safe* initialization - 'safe' means that png_error won't be
John Bowler18c5cfa2011-11-16 14:26:34 -06001157 * called, so setting up the jmp_buf is not required. This means that anything
1158 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1159 * instead so that control is returned safely back to this routine.
John Bowler7875d532011-11-07 22:33:49 -06001160 */
1161static int
1162png_image_read_init(png_imagep image)
1163{
John Bowler5bc90382012-01-23 22:43:22 -06001164 if (image->opaque == NULL)
1165 {
1166 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
John Bowler7875d532011-11-07 22:33:49 -06001167 png_safe_error, png_safe_warning);
1168
John Bowler5bc90382012-01-23 22:43:22 -06001169 /* And set the rest of the structure to NULL to ensure that the various
1170 * fields are consistent.
1171 */
1172 memset(image, 0, sizeof *image);
1173 image->version = PNG_IMAGE_VERSION;
John Bowler7875d532011-11-07 22:33:49 -06001174
John Bowler5bc90382012-01-23 22:43:22 -06001175 if (png_ptr != NULL)
John Bowler7875d532011-11-07 22:33:49 -06001176 {
John Bowler5bc90382012-01-23 22:43:22 -06001177 png_infop info_ptr = png_create_info_struct(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001178
John Bowler5bc90382012-01-23 22:43:22 -06001179 if (info_ptr != NULL)
John Bowler7875d532011-11-07 22:33:49 -06001180 {
John Bowler5bc90382012-01-23 22:43:22 -06001181 png_controlp control = png_voidcast(png_controlp,
1182 png_malloc_warn(png_ptr, sizeof *control));
John Bowler7875d532011-11-07 22:33:49 -06001183
John Bowler5bc90382012-01-23 22:43:22 -06001184 if (control != NULL)
1185 {
1186 png_memset(control, 0, sizeof *control);
John Bowler7875d532011-11-07 22:33:49 -06001187
John Bowler5bc90382012-01-23 22:43:22 -06001188 control->png_ptr = png_ptr;
1189 control->info_ptr = info_ptr;
1190 control->for_write = 0;
1191
1192 image->opaque = control;
1193 return 1;
1194 }
1195
1196 /* Error clean up */
1197 png_destroy_info_struct(png_ptr, &info_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001198 }
1199
John Bowler5bc90382012-01-23 22:43:22 -06001200 png_destroy_read_struct(&png_ptr, NULL, NULL);
John Bowler7875d532011-11-07 22:33:49 -06001201 }
1202
John Bowler5bc90382012-01-23 22:43:22 -06001203 return png_image_error(image, "png_image_read: out of memory");
John Bowler7875d532011-11-07 22:33:49 -06001204 }
1205
John Bowler5bc90382012-01-23 22:43:22 -06001206 return png_image_error(image, "png_image_read: opaque pointer not NULL");
John Bowler7875d532011-11-07 22:33:49 -06001207}
1208
1209/* Utility to find the base format of a PNG file from a png_struct. */
1210static png_uint_32
John Bowler5bc90382012-01-23 22:43:22 -06001211png_image_format(png_structrp png_ptr)
John Bowler7875d532011-11-07 22:33:49 -06001212{
1213 png_uint_32 format = 0;
1214
1215 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1216 format |= PNG_FORMAT_FLAG_COLOR;
1217
1218 if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1219 format |= PNG_FORMAT_FLAG_ALPHA;
1220
John Bowler5bc90382012-01-23 22:43:22 -06001221 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1222 * sets the png_struct fields; that's all we are interested in here. The
1223 * precise interaction with an app call to png_set_tRNS and PNG file reading
1224 * is unclear.
1225 */
1226 else if (png_ptr->num_trans > 0)
John Bowler7875d532011-11-07 22:33:49 -06001227 format |= PNG_FORMAT_FLAG_ALPHA;
1228
1229 if (png_ptr->bit_depth == 16)
1230 format |= PNG_FORMAT_FLAG_LINEAR;
1231
John Bowler5bc90382012-01-23 22:43:22 -06001232 if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE)
1233 format |= PNG_FORMAT_FLAG_COLORMAP;
1234
John Bowler7875d532011-11-07 22:33:49 -06001235 return format;
1236}
1237
John Bowler5bc90382012-01-23 22:43:22 -06001238/* Is the given gamma significantly different from sRGB? The test is the same
1239 * one used in pngrtran.c when deciding whether to do gamma correction. The
1240 * arithmetic optimizes the division by using the fact that the inverse of the
1241 * file sRGB gamma is 2.2
1242 */
1243static int
1244png_gamma_not_sRGB(png_fixed_point g)
1245{
1246 if (g < PNG_FP_1)
1247 {
1248 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
1249 if (g == 0)
1250 return 0;
1251
1252 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1253 }
1254
1255 return 1;
1256}
1257
John Bowler7875d532011-11-07 22:33:49 -06001258/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1259 * header and fill in all the information. This is executed in a safe context,
1260 * unlike the init routine above.
1261 */
1262static int
1263png_image_read_header(png_voidp argument)
1264{
John Bowler4fa96a42011-11-16 16:39:16 -06001265 png_imagep image = png_voidcast(png_imagep, argument);
John Bowler5d567862011-12-24 09:12:00 -06001266 png_structrp png_ptr = image->opaque->png_ptr;
1267 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001268
John Bowleraa816c42012-03-16 07:39:49 -05001269 png_set_benign_errors(png_ptr, 1/*warn*/);
John Bowler7875d532011-11-07 22:33:49 -06001270 png_read_info(png_ptr, info_ptr);
1271
1272 /* Do this the fast way; just read directly out of png_struct. */
1273 image->width = png_ptr->width;
1274 image->height = png_ptr->height;
1275
1276 {
John Bowler5bc90382012-01-23 22:43:22 -06001277 png_uint_32 format = png_image_format(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001278
1279 image->format = format;
John Bowler04336ba2012-01-16 07:48:36 -06001280
John Bowlerb11b31a2012-03-21 07:55:46 -05001281#ifdef PNG_COLORSPACE_SUPPORTED
1282 /* Does the colorspace match sRGB? If there is no color endpoint
1283 * (colorant) information assume yes, otherwise require the
1284 * 'ENDPOINTS_MATCHE_sRGB' colorspace flag to have been set. If the
1285 * colorspace has been determined to be invalid ignore it.
1286 */
1287 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
1288 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
1289 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
1290 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1291#endif
John Bowler7875d532011-11-07 22:33:49 -06001292 }
1293
John Bowler5bc90382012-01-23 22:43:22 -06001294 /* We need the maximum number of entries regardless of the format the
1295 * application sets here.
1296 */
1297 {
1298 png_uint_32 cmap_entries;
1299
1300 switch (png_ptr->color_type)
1301 {
1302 case PNG_COLOR_TYPE_GRAY:
1303 cmap_entries = 1U << png_ptr->bit_depth;
1304 break;
1305
1306 case PNG_COLOR_TYPE_PALETTE:
1307 cmap_entries = png_ptr->num_palette;
1308 break;
1309
1310 default:
1311 cmap_entries = 256;
1312 break;
1313 }
1314
1315 if (cmap_entries > 256)
1316 cmap_entries = 256;
1317
1318 image->colormap_entries = cmap_entries;
1319 }
1320
John Bowler7875d532011-11-07 22:33:49 -06001321 return 1;
1322}
1323
1324#ifdef PNG_STDIO_SUPPORTED
1325int PNGAPI
1326png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1327{
John Bowler5bc90382012-01-23 22:43:22 -06001328 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001329 {
1330 if (file != NULL)
1331 {
1332 if (png_image_read_init(image))
1333 {
1334 /* This is slightly evil, but png_init_io doesn't do anything other
1335 * than this and we haven't changed the standard IO functions so
1336 * this saves a 'safe' function.
1337 */
1338 image->opaque->png_ptr->io_ptr = file;
1339 return png_safe_execute(image, png_image_read_header, image);
1340 }
1341 }
1342
1343 else
1344 return png_image_error(image,
1345 "png_image_begin_read_from_stdio: invalid argument");
1346 }
1347
1348 return 0;
1349}
1350
1351int PNGAPI
1352png_image_begin_read_from_file(png_imagep image, const char *file_name)
1353{
John Bowler5bc90382012-01-23 22:43:22 -06001354 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001355 {
1356 if (file_name != NULL)
1357 {
1358 FILE *fp = fopen(file_name, "rb");
1359
1360 if (fp != NULL)
1361 {
1362 if (png_image_read_init(image))
1363 {
1364 image->opaque->png_ptr->io_ptr = fp;
1365 image->opaque->owned_file = 1;
1366 return png_safe_execute(image, png_image_read_header, image);
1367 }
1368
1369 /* Clean up: just the opened file. */
1370 (void)fclose(fp);
1371 }
1372
1373 else
1374 return png_image_error(image, strerror(errno));
1375 }
1376
1377 else
1378 return png_image_error(image,
1379 "png_image_begin_read_from_file: invalid argument");
1380 }
1381
1382 return 0;
1383}
1384#endif /* PNG_STDIO_SUPPORTED */
1385
1386static void PNGCBAPI
1387png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
1388{
1389 if (png_ptr != NULL)
1390 {
John Bowler4fa96a42011-11-16 16:39:16 -06001391 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001392 if (image != NULL)
1393 {
1394 png_controlp cp = image->opaque;
1395 if (cp != NULL)
1396 {
1397 png_const_bytep memory = cp->memory;
1398 png_size_t size = cp->size;
1399
1400 if (memory != NULL && size >= need)
1401 {
John Bowlerbaeb6d12011-11-26 18:21:02 -06001402 png_memcpy(out, memory, need);
John Bowler7875d532011-11-07 22:33:49 -06001403 cp->memory = memory + need;
1404 cp->size = size - need;
1405 return;
1406 }
1407
1408 png_error(png_ptr, "read beyond end of data");
1409 }
1410 }
1411
1412 png_error(png_ptr, "invalid memory read");
1413 }
1414}
1415
1416int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1417 png_const_voidp memory, png_size_t size)
1418{
John Bowler5bc90382012-01-23 22:43:22 -06001419 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001420 {
1421 if (memory != NULL && size > 0)
1422 {
1423 if (png_image_read_init(image))
1424 {
1425 /* Now set the IO functions to read from the memory buffer and
1426 * store it into io_ptr. Again do this in-place to avoid calling a
1427 * libpng function that requires error handling.
1428 */
John Bowler4fa96a42011-11-16 16:39:16 -06001429 image->opaque->memory = png_voidcast(png_const_bytep, memory);
John Bowler7875d532011-11-07 22:33:49 -06001430 image->opaque->size = size;
1431 image->opaque->png_ptr->io_ptr = image;
1432 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1433
1434 return png_safe_execute(image, png_image_read_header, image);
1435 }
1436 }
1437
1438 else
1439 return png_image_error(image,
1440 "png_image_begin_read_from_memory: invalid argument");
1441 }
1442
1443 return 0;
1444}
1445
John Bowler5bc90382012-01-23 22:43:22 -06001446/* Utility function to skip chunks that are not used by the simplified image
1447 * read functions and an appropriate macro to call it.
1448 */
1449#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1450static void
1451png_image_skip_unused_chunks(png_structrp png_ptr)
John Bowler04336ba2012-01-16 07:48:36 -06001452{
John Bowler5bc90382012-01-23 22:43:22 -06001453 /* Prepare the reader to ignore all recognized chunks whose data will not
1454 * be used, i.e., all chunks recognized by libpng except for those
1455 * involved in basic image reading:
1456 *
1457 * IHDR, PLTE, IDAT, IEND
1458 *
1459 * Or image data handling:
1460 *
1461 * tRNS, bKGD, gAMA, cHRM, sRGB, iCCP and sBIT.
1462 *
1463 * This provides a small performance improvement and eliminates any
1464 * potential vulnerability to security problems in the unused chunks.
1465 *
1466 * TODO: make it so that this is an explicit list to process, not a list
1467 * to ignore?
1468 */
John Bowler04336ba2012-01-16 07:48:36 -06001469 {
John Bowler5bc90382012-01-23 22:43:22 -06001470 static PNG_CONST png_byte chunks_to_ignore[] = {
1471 104, 73, 83, 84, '\0', /* hIST */
1472 105, 84, 88, 116, '\0', /* iTXt */
1473 111, 70, 70, 115, '\0', /* oFFs */
1474 112, 67, 65, 76, '\0', /* pCAL */
1475 112, 72, 89, 115, '\0', /* pHYs */
1476 115, 67, 65, 76, '\0', /* sCAL */
1477 115, 80, 76, 84, '\0', /* sPLT */
1478 116, 69, 88, 116, '\0', /* tEXt */
1479 116, 73, 77, 69, '\0', /* tIME */
1480 122, 84, 88, 116, '\0' /* zTXt */
1481 };
1482
1483 /* Ignore unknown chunks */
1484 png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */,
1485 NULL, 0);
1486
1487 /* Ignore known but unused chunks */
1488 png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */,
1489 chunks_to_ignore, (sizeof chunks_to_ignore)/5);
1490 }
1491}
1492
1493# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1494#else
1495# define PNG_SKIP_CHUNKS(p) ((void)0)
1496#endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */
1497
1498/* The following macro gives the exact rounded answer for all values in the
1499 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1500 * the correct numbers 0..5
1501 */
1502#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1503
1504/* Utility functions to make particular color-maps */
1505static void
1506set_file_encoding(png_image_read_control *display)
1507{
John Bowlerb11b31a2012-03-21 07:55:46 -05001508 png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma;
John Bowler5bc90382012-01-23 22:43:22 -06001509 if (png_gamma_significant(g))
1510 {
1511 if (png_gamma_not_sRGB(g))
John Bowler04336ba2012-01-16 07:48:36 -06001512 {
John Bowler5bc90382012-01-23 22:43:22 -06001513 display->file_encoding = E_FILE;
1514 display->gamma_to_linear = png_reciprocal(g);
John Bowler04336ba2012-01-16 07:48:36 -06001515 }
1516
1517 else
John Bowler5bc90382012-01-23 22:43:22 -06001518 display->file_encoding = E_sRGB;
John Bowler04336ba2012-01-16 07:48:36 -06001519 }
1520
John Bowler5bc90382012-01-23 22:43:22 -06001521 else
1522 display->file_encoding = E_LINEAR8;
John Bowler04336ba2012-01-16 07:48:36 -06001523}
1524
John Bowler5bc90382012-01-23 22:43:22 -06001525static unsigned int
1526decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
John Bowler7875d532011-11-07 22:33:49 -06001527{
John Bowler5bc90382012-01-23 22:43:22 -06001528 if (encoding == E_FILE) /* double check */
1529 encoding = display->file_encoding;
John Bowler7875d532011-11-07 22:33:49 -06001530
John Bowler5bc90382012-01-23 22:43:22 -06001531 if (encoding == E_NOTSET) /* must be the file encoding */
1532 {
1533 set_file_encoding(display);
1534 encoding = display->file_encoding;
1535 }
1536
1537 switch (encoding)
1538 {
1539 case E_FILE:
1540 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
1541 break;
1542
1543 case E_sRGB:
1544 value = png_sRGB_table[value];
1545 break;
1546
1547 case E_LINEAR:
1548 break;
1549
1550 case E_LINEAR8:
1551 value *= 257;
1552 break;
1553
1554 default:
1555 png_error(display->image->opaque->png_ptr,
1556 "unexpected encoding (internal error)");
1557 break;
1558 }
1559
1560 return value;
1561}
1562
1563static png_uint_32
1564png_colormap_compose(png_image_read_control *display,
1565 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
1566 png_uint_32 background, int encoding)
1567{
1568 /* The file value is composed on the background, the background has the given
1569 * encoding and so does the result, the file is encoded with E_FILE and the
1570 * file and alpha are 8-bit values. The (output) encoding will always be
1571 * E_LINEAR or E_sRGB.
1572 */
1573 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
1574 png_uint_32 b = decode_gamma(display, background, encoding);
1575
1576 /* The alpha is always an 8-bit value (it comes from the palette), the value
1577 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1578 */
1579 f = f * alpha + b * (255-alpha);
1580
1581 if (encoding == E_LINEAR)
1582 {
1583 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1584 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1585 */
1586 f *= 257; /* Now scaled by 65535 */
1587 f += f >> 16;
1588 f = (f+32768) >> 16;
1589 }
1590
1591 else /* E_sRGB */
1592 f = PNG_sRGB_FROM_LINEAR(f);
1593
1594 return f;
1595}
1596
1597/* NOTE: E_LINEAR values to this routine must be 16-bit, but E_FILE values must
1598 * be 8-bit.
1599 */
1600static void
1601png_create_colormap_entry(png_image_read_control *display,
1602 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
1603 png_uint_32 alpha, int encoding)
1604{
1605 png_imagep image = display->image;
1606 const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ?
1607 E_LINEAR : E_sRGB;
1608 const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
1609 (red != green || green != blue);
1610
1611 if (ip > 255)
1612 png_error(image->opaque->png_ptr, "color-map index out of range");
1613
1614 /* Update the cache with whether the file gamma is significantly different
1615 * from sRGB.
1616 */
1617 if (encoding == E_FILE)
1618 {
1619 if (display->file_encoding == E_NOTSET)
1620 set_file_encoding(display);
1621
1622 /* Note that the cached value may be E_FILE too, but if it is then the
1623 * gamma_to_linear member has been set.
1624 */
1625 encoding = display->file_encoding;
1626 }
1627
1628 if (encoding == E_FILE)
1629 {
1630 png_fixed_point g = display->gamma_to_linear;
1631
1632 red = png_gamma_16bit_correct(red*257, g);
1633 green = png_gamma_16bit_correct(green*257, g);
1634 blue = png_gamma_16bit_correct(blue*257, g);
1635
1636 if (convert_to_Y || output_encoding == E_LINEAR)
1637 {
1638 alpha *= 257;
1639 encoding = E_LINEAR;
1640 }
1641
1642 else
1643 {
1644 red = PNG_sRGB_FROM_LINEAR(red * 255);
1645 green = PNG_sRGB_FROM_LINEAR(green * 255);
1646 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1647 encoding = E_sRGB;
1648 }
1649 }
1650
1651 else if (encoding == E_LINEAR8)
1652 {
1653 /* This encoding occurs quite frequently in test cases because PngSuite
1654 * includes a gAMA 1.0 chunk with most images.
1655 */
1656 red *= 257;
1657 green *= 257;
1658 blue *= 257;
1659 alpha *= 257;
1660 encoding = E_LINEAR;
1661 }
1662
1663 else if (encoding == E_sRGB && (convert_to_Y || output_encoding == E_LINEAR))
1664 {
1665 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1666 * linear.
1667 */
1668 red = png_sRGB_table[red];
1669 green = png_sRGB_table[green];
1670 blue = png_sRGB_table[blue];
1671 alpha *= 257;
1672 encoding = E_LINEAR;
1673 }
1674
1675 /* This is set if the color isn't gray but the output is. */
1676 if (encoding == E_LINEAR)
1677 {
1678 if (convert_to_Y)
1679 {
1680 /* NOTE: these values are copied from png_do_rgb_to_gray */
1681 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
1682 (png_uint_32)2366 * blue;
1683
1684 if (output_encoding == E_LINEAR)
1685 y = (y + 16384) >> 15;
1686
1687 else
1688 {
1689 /* y is scaled by 32768, we need it scaled by 255: */
1690 y = (y + 128) >> 8;
1691 y *= 255;
1692 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
1693 encoding = E_sRGB;
1694 }
1695
1696 blue = red = green = y;
1697 }
1698
1699 else if (output_encoding == E_sRGB)
1700 {
1701 red = PNG_sRGB_FROM_LINEAR(red * 255);
1702 green = PNG_sRGB_FROM_LINEAR(green * 255);
1703 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1704 alpha = PNG_DIV257(alpha);
1705 encoding = E_sRGB;
1706 }
1707 }
1708
1709 if (encoding != output_encoding)
1710 png_error(image->opaque->png_ptr, "bad encoding (internal error)");
1711
1712 /* Store the value. */
1713 {
1714# ifdef PNG_FORMAT_BGR_SUPPORTED
1715 const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1716 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
1717# else
1718# define afirst 0
1719# endif
1720# ifdef PNG_FORMAT_BGR_SUPPORTED
1721 const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
1722# else
1723# define bgr 0
1724# endif
1725
1726 if (output_encoding == E_LINEAR)
1727 {
1728 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
1729
1730 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1731
1732 /* The linear 16-bit values must be pre-multiplied by the alpha channel
1733 * value, if less than 65535 (this is, effectively, composite on black
1734 * if the alpha channel is removed.)
1735 */
1736 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1737 {
1738 case 4:
1739 entry[afirst ? 0 : 3] = (png_uint_16)alpha;
1740 /* FALL THROUGH */
1741
1742 case 3:
1743 if (alpha < 65535)
1744 {
1745 if (alpha > 0)
1746 {
1747 blue = (blue * alpha + 32767U)/65535U;
1748 green = (green * alpha + 32767U)/65535U;
1749 red = (red * alpha + 32767U)/65535U;
1750 }
1751
1752 else
1753 red = green = blue = 0;
1754 }
1755 entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
1756 entry[afirst + 1] = (png_uint_16)green;
1757 entry[afirst + bgr] = (png_uint_16)red;
1758 break;
1759
1760 case 2:
1761 entry[1 ^ afirst] = (png_uint_16)alpha;
1762 /* FALL THROUGH */
1763
1764 case 1:
1765 if (alpha < 65535)
1766 {
1767 if (alpha > 0)
1768 green = (green * alpha + 32767U)/65535U;
1769
1770 else
1771 green = 0;
1772 }
1773 entry[afirst] = (png_uint_16)green;
1774 break;
1775
1776 default:
1777 break;
1778 }
1779 }
1780
1781 else /* output encoding is E_sRGB */
1782 {
1783 png_bytep entry = png_voidcast(png_bytep, display->colormap);
1784
1785 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1786
1787 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1788 {
1789 case 4:
1790 entry[afirst ? 0 : 3] = (png_byte)alpha;
1791 case 3:
1792 entry[afirst + (2 ^ bgr)] = (png_byte)blue;
1793 entry[afirst + 1] = (png_byte)green;
1794 entry[afirst + bgr] = (png_byte)red;
1795 break;
1796
1797 case 2:
1798 entry[1 ^ afirst] = (png_byte)alpha;
1799 case 1:
1800 entry[afirst] = (png_byte)green;
1801 break;
1802
1803 default:
1804 break;
1805 }
1806 }
1807
1808# ifdef afirst
1809# undef afirst
1810# endif
1811# ifdef bgr
1812# undef bgr
1813# endif
1814 }
1815}
1816
John Bowler7875d532011-11-07 22:33:49 -06001817static int
John Bowlereda53e52012-03-18 22:46:28 -05001818make_gray_file_colormap(png_image_read_control *display)
1819{
1820 unsigned int i;
1821
1822 for (i=0; i<256; ++i)
1823 png_create_colormap_entry(display, i, i, i, i, 255, E_FILE);
1824
1825 return i;
1826}
1827
1828static int
John Bowler5bc90382012-01-23 22:43:22 -06001829make_gray_colormap(png_image_read_control *display)
1830{
1831 unsigned int i;
1832
1833 for (i=0; i<256; ++i)
1834 png_create_colormap_entry(display, i, i, i, i, 255, E_sRGB);
1835
1836 return i;
1837}
1838#define PNG_GRAY_COLORMAP_ENTRIES 256
1839
1840static int
1841make_ga_colormap(png_image_read_control *display)
1842{
1843 unsigned int i, a;
1844
1845 /* Alpha is retained, the output will be a color-map with entries
1846 * selected by six levels of alpha. One transparent entry, 6 gray
1847 * levels for all the intermediate alpha values, leaving 230 entries
1848 * for the opaque grays. The color-map entries are the six values
1849 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
1850 * relevant entry.
1851 *
1852 * if (alpha > 229) // opaque
1853 * {
1854 * // The 231 entries are selected to make the math below work:
1855 * base = 0;
1856 * entry = (231 * gray + 128) >> 8;
1857 * }
1858 * else if (alpha < 26) // transparent
1859 * {
1860 * base = 231;
1861 * entry = 0;
1862 * }
1863 * else // partially opaque
1864 * {
1865 * base = 226 + 6 * PNG_DIV51(alpha);
1866 * entry = PNG_DIV51(gray);
1867 * }
1868 */
1869 i = 0;
1870 while (i < 231)
1871 {
1872 unsigned int gray = (i * 256 + 115) / 231;
1873 png_create_colormap_entry(display, i++, gray, gray, gray, 255, E_sRGB);
1874 }
1875
1876 /* 255 is used here for the component values for consistency with the code
1877 * that undoes premultiplication in pngwrite.c.
1878 */
1879 png_create_colormap_entry(display, i++, 255, 255, 255, 0, E_sRGB);
1880
1881 for (a=1; a<5; ++a)
1882 {
1883 unsigned int g;
1884
1885 for (g=0; g<6; ++g)
1886 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
1887 E_sRGB);
1888 }
1889
1890 return i;
1891}
1892
1893#define PNG_GA_COLORMAP_ENTRIES 256
1894
1895static int
1896make_rgb_colormap(png_image_read_control *display)
1897{
1898 unsigned int i, r;
1899
1900 /* Build a 6x6x6 opaque RGB cube */
1901 for (i=r=0; r<6; ++r)
1902 {
1903 unsigned int g;
1904
1905 for (g=0; g<6; ++g)
1906 {
1907 unsigned int b;
1908
1909 for (b=0; b<6; ++b)
1910 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
1911 E_sRGB);
1912 }
1913 }
1914
1915 return i;
1916}
1917
1918#define PNG_RGB_COLORMAP_ENTRIES 216
1919
1920/* Return a palette index to the above palette given three 8-bit sRGB values. */
1921#define PNG_RGB_INDEX(r,g,b) \
1922 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
1923
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06001924static int
John Bowler5bc90382012-01-23 22:43:22 -06001925png_image_read_colormap(png_voidp argument)
1926{
1927 png_image_read_control *display =
1928 png_voidcast(png_image_read_control*, argument);
1929 const png_imagep image = display->image;
1930
1931 const png_structrp png_ptr = image->opaque->png_ptr;
1932 const png_uint_32 output_format = image->format;
1933 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ?
1934 E_LINEAR : E_sRGB;
1935
1936 unsigned int cmap_entries;
1937 unsigned int output_processing; /* Output processing option */
1938 unsigned int data_encoding = E_NOTSET; /* Encoding libpng must produce */
1939
1940 /* Background information; the background color and the index of this color
1941 * in the color-map if it exists (else 256).
1942 */
1943 unsigned int background_index = 256;
1944 png_uint_32 back_r, back_g, back_b;
1945
1946 /* Flags to accumulate things that need to be done to the input. */
1947 int expand_tRNS = 0;
1948
1949 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
1950 * very difficult to do, the results look awful, and it is difficult to see
1951 * what possible use it is because the application can't control the
1952 * color-map.
1953 */
1954 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
1955 png_ptr->num_trans > 0) /* alpha in input */ &&
1956 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
1957 {
1958 if (output_encoding == E_LINEAR) /* compose on black */
1959 back_b = back_g = back_r = 0;
1960
1961 else if (display->background == NULL /* no way to remove it */)
1962 png_error(png_ptr,
1963 "a background color must be supplied to remove alpha/transparency");
1964
1965 /* Get a copy of the background color (this avoids repeating the checks
1966 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
1967 * output format.
1968 */
1969 else
1970 {
1971 back_g = display->background->green;
1972 if (output_format & PNG_FORMAT_FLAG_COLOR)
1973 {
1974 back_r = display->background->red;
1975 back_b = display->background->blue;
1976 }
1977 else
1978 back_b = back_r = back_g;
1979 }
1980 }
1981
1982 else if (output_encoding == E_LINEAR)
1983 back_b = back_r = back_g = 65535;
1984
1985 else
1986 back_b = back_r = back_g = 255;
1987
1988 /* Decide what to do based on the PNG color type of the input data. The
1989 * utility function png_create_colormap_entry deals with most aspects of the
1990 * output transformations; this code works out how to produce bytes of
1991 * color-map entries from the original format.
1992 */
1993 switch (png_ptr->color_type)
1994 {
1995 case PNG_COLOR_TYPE_GRAY:
1996 if (png_ptr->bit_depth <= 8)
1997 {
1998 /* There at most 256 colors in the output, regardless of
1999 * transparency.
2000 */
2001 unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
2002
2003 cmap_entries = 1U << png_ptr->bit_depth;
2004 if (cmap_entries > image->colormap_entries)
2005 png_error(png_ptr, "gray[8] color-map: too few entries");
2006
2007 step = 255 / (cmap_entries - 1);
2008 output_processing = PNG_CMAP_NONE;
2009
2010 /* If there is a tRNS chunk then this either selects a transparent
2011 * value or, if the output has no alpha, the background color.
2012 */
2013 if (png_ptr->num_trans > 0)
2014 {
2015 trans = png_ptr->trans_color.gray;
2016
2017 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
2018 back_alpha = output_encoding == E_LINEAR ? 65535 : 255;
2019 }
2020
2021 /* png_create_colormap_entry just takes an RGBA and writes the
2022 * corresponding color-map entry using the format from 'image',
2023 * including the required convertion to sRGB or linear as
2024 * appropriate. The input values are always either sRGB (if the
2025 * gamma correction flag is 0) or 0..255 scaled file encoded values
2026 * (if the function must gamma correct them).
2027 */
2028 for (i=val=0; i<cmap_entries; ++i, val += step)
2029 {
2030 /* 'i' is a file value. While this will result in duplicated
2031 * entries for 8-bit non-sRGB encoded files it is necessary to
2032 * have non-gamma corrected values to do tRNS handling.
2033 */
2034 if (i != trans)
2035 png_create_colormap_entry(display, i, val, val, val, 255,
2036 E_FILE/*8-bit with file gamma*/);
2037
2038 /* Else this entry is transparent. The colors don't matter if
2039 * there is an alpha channel (back_alpha == 0), but it does no
2040 * harm to pass them in; the values are not set above so this
2041 * passes in white.
2042 *
2043 * NOTE: this preserves the full precision of the application
2044 * supplied background color when it is used.
2045 */
2046 else
2047 png_create_colormap_entry(display, i, back_r, back_g, back_b,
2048 back_alpha, output_encoding);
2049 }
2050
2051 /* We need libpng to preserve the original encoding. */
2052 data_encoding = E_FILE;
2053
2054 /* The rows from libpng, while technically gray values, are now also
2055 * color-map indicies; however, they may need to be expanded to 1
2056 * byte per pixel. This is what png_set_packing does (i.e., it
2057 * unpacks the bit values into bytes.)
2058 */
2059 if (png_ptr->bit_depth < 8)
2060 png_set_packing(png_ptr);
2061 }
2062
2063 else /* bit depth is 16 */
2064 {
2065 /* The 16-bit input values can be converted directly to 8-bit gamma
2066 * encoded values; however, if a tRNS chunk is present 257 color-map
2067 * entries are required. This means that the extra entry requires
2068 * special processing; add an alpha channel, sacrifice gray level
2069 * 254 and convert transparent (alpha==0) entries to that.
2070 *
2071 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2072 * same time to minimize quality loss. If a tRNS chunk is present
2073 * this means libpng must handle it too; otherwise it is impossible
2074 * to do the exact match on the 16-bit value.
2075 *
2076 * If the output has no alpha channel *and* the background color is
2077 * gray then it is possible to let libpng handle the substitution by
2078 * ensuring that the corresponding gray level matches the background
2079 * color exactly.
2080 */
2081 data_encoding = E_sRGB;
2082
2083 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2084 png_error(png_ptr, "gray[16] color-map: too few entries");
2085
2086 cmap_entries = make_gray_colormap(display);
2087
2088 if (png_ptr->num_trans > 0)
2089 {
2090 unsigned int back_alpha;
2091
2092 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2093 back_alpha = 0;
2094
2095 else
2096 {
2097 if (back_r == back_g && back_g == back_b)
2098 {
2099 /* Background is gray; no special processing will be
2100 * required.
2101 */
2102 png_color_16 c;
2103 png_uint_32 gray = back_g;
2104
2105 if (output_encoding == E_LINEAR)
2106 {
2107 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2108
2109 /* And make sure the corresponding palette entry
2110 * matches.
2111 */
2112 png_create_colormap_entry(display, gray, back_g, back_g,
2113 back_g, 65535, E_LINEAR);
2114 }
2115
2116 /* The background passed to libpng, however, must be the
2117 * sRGB value.
2118 */
2119 c.index = 0; /*unused*/
2120 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2121
2122 /* NOTE: does this work without expanding tRNS to alpha?
2123 * It should be the color->gray case below apparently
2124 * doesn't.
2125 */
2126 png_set_background_fixed(png_ptr, &c,
2127 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2128 0/*gamma: not used*/);
2129
2130 output_processing = PNG_CMAP_NONE;
2131 break;
2132 }
2133
2134 back_alpha = output_encoding == E_LINEAR ? 65535 : 255;
2135 }
2136
2137 /* output_processing means that the libpng-processed row will be
2138 * 8-bit GA and it has to be processing to single byte color-map
2139 * values. Entry 254 is replaced by either a completely
2140 * transparent entry or by the background color at full
2141 * precision (and the background color is not a simple gray leve
2142 * in this case.)
2143 */
2144 expand_tRNS = 1;
2145 output_processing = PNG_CMAP_TRANS;
2146 background_index = 254;
2147
2148 /* And set (overwrite) color-map entry 254 to the actual
2149 * background color at full precision.
2150 */
2151 png_create_colormap_entry(display, 254, back_r, back_g, back_b,
2152 back_alpha, output_encoding);
2153 }
2154
2155 else
2156 output_processing = PNG_CMAP_NONE;
2157 }
2158 break;
2159
2160 case PNG_COLOR_TYPE_GRAY_ALPHA:
2161 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2162 * of 65536 combinations. If, however, the alpha channel is to be
2163 * removed there are only 256 possibilities if the background is gray.
2164 * (Otherwise there is a subset of the 65536 possibilities defined by
2165 * the triangle between black, white and the background color.)
2166 *
2167 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2168 * worry about tRNS matching - tRNS is ignored if there is an alpha
2169 * channel.
2170 */
2171 data_encoding = E_sRGB;
2172
2173 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2174 {
2175 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2176 png_error(png_ptr, "gray+alpha color-map: too few entries");
2177
2178 cmap_entries = make_ga_colormap(display);
2179
2180 background_index = PNG_CMAP_GA_BACKGROUND;
2181 output_processing = PNG_CMAP_GA;
2182 }
2183
2184 else /* alpha is removed */
2185 {
2186 /* Alpha must be removed as the PNG data is processed when the
2187 * background is a color because the G and A channels are
2188 * independent and the vector addition (non-parallel vectors) is a
2189 * 2-D problem.
2190 *
2191 * This can be reduced to the same algorithm as above by making a
2192 * colormap containing gray levels (for the opaque grays), a
2193 * background entry (for a transparent pixel) and a set of four six
2194 * level color values, one set for each intermediate alpha value.
2195 * See the comments in make_ga_colormap for how this works in the
2196 * per-pixel processing.
2197 *
2198 * If the background is gray, however, we only need a 256 entry gray
2199 * level color map. It is sufficient to make the entry generated
2200 * for the background color be exactly the color specified.
2201 */
2202 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2203 (back_r == back_g && back_g == back_b))
2204 {
2205 /* Background is gray; no special processing will be required. */
2206 png_color_16 c;
2207 png_uint_32 gray = back_g;
2208
2209 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2210 png_error(png_ptr, "gray-alpha color-map: too few entries");
2211
2212 cmap_entries = make_gray_colormap(display);
2213
2214 if (output_encoding == E_LINEAR)
2215 {
2216 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2217
2218 /* And make sure the corresponding palette entry matches. */
2219 png_create_colormap_entry(display, gray, back_g, back_g,
2220 back_g, 65535, E_LINEAR);
2221 }
2222
2223 /* The background passed to libpng, however, must be the sRGB
2224 * value.
2225 */
2226 c.index = 0; /*unused*/
2227 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2228
2229 png_set_background_fixed(png_ptr, &c,
2230 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2231 0/*gamma: not used*/);
2232
2233 output_processing = PNG_CMAP_NONE;
2234 }
2235
2236 else
2237 {
2238 png_uint_32 i, a;
2239
2240 /* This is the same as png_make_ga_colormap, above, except that
2241 * the entries are all opaque.
2242 */
2243 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2244 png_error(png_ptr, "ga-alpha color-map: too few entries");
2245
2246 i = 0;
2247 while (i < 231)
2248 {
2249 png_uint_32 gray = (i * 256 + 115) / 231;
2250 png_create_colormap_entry(display, i++, gray, gray, gray,
2251 255, E_sRGB);
2252 }
2253
2254 /* NOTE: this preserves the full precision of the application
2255 * background color.
2256 */
2257 background_index = i;
2258 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
2259 output_encoding == E_LINEAR ? 65535U : 255U, output_encoding);
2260
2261 /* For non-opaque input composite on the sRGB background - this
2262 * requires inverting the encoding for each component. The input
2263 * is still converted to the sRGB encoding because this is a
2264 * reasonable approximate to the logarithmic curve of human
2265 * visual sensitivity, at least over the narrow range which PNG
2266 * represents. Consequently 'G' is always sRGB encoded, while
2267 * 'A' is linear. We need the linear background colors.
2268 */
2269 if (output_encoding == E_sRGB) /* else already linear */
2270 {
2271 /* This may produce a value not exactly matching the
2272 * background, but that's ok because these numbers are only
2273 * used when alpha != 0
2274 */
2275 back_r = png_sRGB_table[back_r];
2276 back_g = png_sRGB_table[back_g];
2277 back_b = png_sRGB_table[back_b];
2278 }
2279
2280 for (a=1; a<5; ++a)
2281 {
2282 unsigned int g;
2283
2284 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2285 * by an 8-bit alpha value (0..255).
2286 */
2287 png_uint_32 alpha = 51 * a;
2288 png_uint_32 back_rx = (255-alpha) * back_r;
2289 png_uint_32 back_gx = (255-alpha) * back_g;
2290 png_uint_32 back_bx = (255-alpha) * back_b;
2291
2292 for (g=0; g<6; ++g)
2293 {
2294 png_uint_32 gray = png_sRGB_table[g*51] * alpha;
2295
2296 png_create_colormap_entry(display, i++,
2297 PNG_sRGB_FROM_LINEAR(gray + back_rx),
2298 PNG_sRGB_FROM_LINEAR(gray + back_gx),
2299 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, E_sRGB);
2300 }
2301 }
2302
2303 cmap_entries = i;
2304 output_processing = PNG_CMAP_GA;
2305 }
2306 }
2307 break;
2308
2309 case PNG_COLOR_TYPE_RGB:
2310 case PNG_COLOR_TYPE_RGB_ALPHA:
2311 /* Exclude the case where the output is gray; we can always handle this
2312 * with the cases above.
2313 */
2314 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
2315 {
2316 /* The color-map will be grayscale, so we may as well convert the
2317 * input RGB values to a simple grayscale and use the grayscale
2318 * code above.
2319 *
2320 * NOTE: calling this apparently damages the recognition of the
2321 * transparent color in background color handling; call
2322 * png_set_tRNS_to_alpha before png_set_background_fixed.
2323 */
2324 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2325 -1);
2326 data_encoding = E_sRGB;
2327
2328 /* The output will now be one or two 8-bit gray or gray+alpha
2329 * channels. The more complex case arises when the input has alpha.
2330 */
2331 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2332 png_ptr->num_trans > 0) &&
2333 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2334 {
2335 /* Both input and output have an alpha channel, so no background
2336 * processing is required; just map the GA bytes to the right
2337 * color-map entry.
2338 */
2339 expand_tRNS = 1;
2340
2341 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2342 png_error(png_ptr, "rgb[ga] color-map: too few entries");
2343
2344 cmap_entries = make_ga_colormap(display);
2345 background_index = PNG_CMAP_GA_BACKGROUND;
2346 output_processing = PNG_CMAP_GA;
2347 }
2348
2349 else
2350 {
2351 /* Either the input or the output has no alpha channel, so there
2352 * will be no non-opaque pixels in the color-map; it will just be
2353 * grayscale.
2354 */
2355 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2356 png_error(png_ptr, "rgb[gray] color-map: too few entries");
2357
John Bowlereda53e52012-03-18 22:46:28 -05002358 /* Ideally this code would use libpng to do the gamma correction,
2359 * but if an input alpha channel is to be removed we will hit the
2360 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2361 * correction bug). Fix this by dropping the gamma correction in
2362 * this case and doing it in the palette; this will result in
2363 * duplicate palette entries, but that's better than the
2364 * alternative of double gamma correction.
2365 */
2366 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
John Bowlerb11b31a2012-03-21 07:55:46 -05002367 png_ptr->num_trans > 0) &&
2368 png_gamma_not_sRGB(png_ptr->colorspace.gamma))
John Bowlereda53e52012-03-18 22:46:28 -05002369 {
2370 cmap_entries = make_gray_file_colormap(display);
2371 data_encoding = E_FILE;
2372 }
2373
2374 else
2375 cmap_entries = make_gray_colormap(display);
John Bowler5bc90382012-01-23 22:43:22 -06002376
2377 /* But if the input has alpha or transparency it must be removed
2378 */
2379 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2380 png_ptr->num_trans > 0)
2381 {
2382 png_color_16 c;
2383 png_uint_32 gray = back_g;
2384
2385 /* We need to ensure that the application background exists in
2386 * the colormap and that completely transparent pixels map to
2387 * it. Achieve this simply by ensuring that the entry
2388 * selected for the background really is the background color.
2389 */
John Bowlereda53e52012-03-18 22:46:28 -05002390 if (data_encoding == E_FILE) /* from the fixup above */
2391 {
2392 /* The app supplied a gray which is in output_encoding, we
2393 * need to convert it to a value of the input (E_FILE)
2394 * encoding then set this palette entry to the required
2395 * output encoding.
2396 */
2397 if (output_encoding == E_sRGB)
2398 gray = png_sRGB_table[gray]; /* now E_LINEAR */
2399
2400 gray = PNG_DIV257(png_gamma_16bit_correct(gray,
John Bowlerb11b31a2012-03-21 07:55:46 -05002401 png_ptr->colorspace.gamma)); /* now E_FILE */
John Bowlereda53e52012-03-18 22:46:28 -05002402
2403 /* And make sure the corresponding palette entry contains
2404 * exactly the required sRGB value.
2405 */
2406 png_create_colormap_entry(display, gray, back_g, back_g,
2407 back_g, 0/*unused*/, output_encoding);
2408 }
2409
2410 else if (output_encoding == E_LINEAR)
John Bowler5bc90382012-01-23 22:43:22 -06002411 {
2412 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2413
2414 /* And make sure the corresponding palette entry matches.
2415 */
2416 png_create_colormap_entry(display, gray, back_g, back_g,
John Bowlereda53e52012-03-18 22:46:28 -05002417 back_g, 0/*unused*/, E_LINEAR);
John Bowler5bc90382012-01-23 22:43:22 -06002418 }
2419
John Bowlereda53e52012-03-18 22:46:28 -05002420 /* The background passed to libpng, however, must be the
2421 * output (normally sRGB) value.
John Bowler5bc90382012-01-23 22:43:22 -06002422 */
2423 c.index = 0; /*unused*/
2424 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2425
2426 /* NOTE: the following is apparently a bug in libpng. Without
2427 * it the transparent color recognition in
2428 * png_set_background_fixed seems to go wrong.
2429 */
2430 expand_tRNS = 1;
2431 png_set_background_fixed(png_ptr, &c,
2432 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2433 0/*gamma: not used*/);
2434 }
2435
2436 output_processing = PNG_CMAP_NONE;
2437 }
2438 }
2439
2440 else /* output is color */
2441 {
2442 /* We could use png_quantize here so long as there is no transparent
2443 * color or alpha; png_quantize ignores alpha. Easier overall just
2444 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2445 * Consequently we always want libpng to produce sRGB data.
2446 */
2447 data_encoding = E_sRGB;
2448
2449 /* Is there any transparency or alpha? */
2450 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2451 png_ptr->num_trans > 0)
2452 {
2453 /* Is there alpha in the output too? If so all four channels are
2454 * processed into a special RGB cube with alpha support.
2455 */
2456 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2457 {
2458 png_uint_32 r;
2459
2460 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2461 png_error(png_ptr, "rgb+alpha color-map: too few entries");
2462
2463 cmap_entries = make_rgb_colormap(display);
2464
2465 /* Add a transparent entry. */
2466 png_create_colormap_entry(display, cmap_entries, 255, 255,
2467 255, 0, E_sRGB);
2468
2469 /* This is stored as the background index for the processing
2470 * algorithm.
2471 */
2472 background_index = cmap_entries++;
2473
2474 /* Add 27 r,g,b entries each with alpha 0.5. */
2475 for (r=0; r<256; r = (r << 1) | 0x7f)
2476 {
2477 png_uint_32 g;
2478
2479 for (g=0; g<256; g = (g << 1) | 0x7f)
2480 {
2481 png_uint_32 b;
2482
2483 /* This generates components with the values 0, 127 and
2484 * 255
2485 */
2486 for (b=0; b<256; b = (b << 1) | 0x7f)
2487 png_create_colormap_entry(display, cmap_entries++,
2488 r, g, b, 128, E_sRGB);
2489 }
2490 }
2491
2492 expand_tRNS = 1;
2493 output_processing = PNG_CMAP_RGB_ALPHA;
2494 }
2495
2496 else
2497 {
2498 /* Alpha/transparency must be removed. The background must
2499 * exist in the color map (achieved by setting adding it after
2500 * the 666 color-map). If the standard processing code will
2501 * pick up this entry automatically that's all that is
2502 * required; libpng can be called to do the background
2503 * processing.
2504 */
2505 unsigned int sample_size =
2506 PNG_IMAGE_SAMPLE_SIZE(output_format);
2507 png_uint_32 r, g, b; /* sRGB background */
2508
2509 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2510 png_error(png_ptr, "rgb-alpha color-map: too few entries");
2511
2512 cmap_entries = make_rgb_colormap(display);
2513
2514 png_create_colormap_entry(display, cmap_entries, back_r,
2515 back_g, back_b, 0/*unused*/, output_encoding);
2516
2517 if (output_encoding == E_LINEAR)
2518 {
2519 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2520 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2521 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2522 }
2523
2524 else
2525 {
2526 r = back_r;
2527 g = back_g;
2528 b = back_g;
2529 }
2530
2531 /* Compare the newly-created color-map entry with the one the
2532 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2533 * match, add the new one and set this as the background index.
2534 */
2535 if (memcmp((png_const_bytep)display->colormap +
2536 sample_size * cmap_entries,
2537 (png_const_bytep)display->colormap +
2538 sample_size * PNG_RGB_INDEX(r,g,b),
2539 sample_size) != 0)
2540 {
2541 /* The background color must be added. */
2542 background_index = cmap_entries++;
2543
2544 /* Add 27 r,g,b entries each with created by composing with
2545 * the background at alpha 0.5.
2546 */
2547 for (r=0; r<256; r = (r << 1) | 0x7f)
2548 {
2549 for (g=0; g<256; g = (g << 1) | 0x7f)
2550 {
2551 /* This generates components with the values 0, 127
2552 * and 255
2553 */
2554 for (b=0; b<256; b = (b << 1) | 0x7f)
2555 png_create_colormap_entry(display, cmap_entries++,
2556 png_colormap_compose(display, r, E_sRGB, 128,
2557 back_r, output_encoding),
2558 png_colormap_compose(display, g, E_sRGB, 128,
2559 back_g, output_encoding),
2560 png_colormap_compose(display, b, E_sRGB, 128,
2561 back_b, output_encoding),
2562 0/*unused*/, output_encoding);
2563 }
2564 }
2565
2566 expand_tRNS = 1;
2567 output_processing = PNG_CMAP_RGB_ALPHA;
2568 }
2569
2570 else /* background color is in the standard color-map */
2571 {
2572 png_color_16 c;
2573
2574 c.index = 0; /*unused*/
2575 c.red = (png_uint_16)back_r;
2576 c.gray = c.green = (png_uint_16)back_g;
2577 c.blue = (png_uint_16)back_b;
2578
2579 png_set_background_fixed(png_ptr, &c,
2580 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2581 0/*gamma: not used*/);
2582
2583 output_processing = PNG_CMAP_RGB;
2584 }
2585 }
2586 }
2587
2588 else /* no alpha or transparency in the input */
2589 {
2590 /* Alpha in the output is irrelevant, simply map the opaque input
2591 * pixels to the 6x6x6 color-map.
2592 */
2593 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2594 png_error(png_ptr, "rgb color-map: too few entries");
2595
2596 cmap_entries = make_rgb_colormap(display);
2597 output_processing = PNG_CMAP_RGB;
2598 }
2599 }
2600 break;
2601
2602 case PNG_COLOR_TYPE_PALETTE:
2603 /* It's already got a color-map. It may be necessary to eliminate the
2604 * tRNS entries though.
2605 */
2606 {
2607 unsigned int num_trans = png_ptr->num_trans;
2608 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2609 png_const_colorp colormap = png_ptr->palette;
2610 const int do_background = trans != NULL &&
2611 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2612 unsigned int i;
2613
2614 /* Just in case: */
2615 if (trans == NULL)
2616 num_trans = 0;
2617
2618 output_processing = PNG_CMAP_NONE;
2619 data_encoding = E_FILE; /* Don't change from color-map indicies */
2620 cmap_entries = png_ptr->num_palette;
2621 if (cmap_entries > 256)
2622 cmap_entries = 256;
2623
2624 if (cmap_entries > image->colormap_entries)
2625 png_error(png_ptr, "palette color-map: too few entries");
2626
2627 for (i=0; i < cmap_entries; ++i)
2628 {
2629 if (do_background && i < num_trans && trans[i] < 255)
2630 {
2631 if (trans[i] == 0)
2632 png_create_colormap_entry(display, i, back_r, back_g,
2633 back_b, 0, output_encoding);
2634
2635 else
2636 {
2637 /* Must compose the PNG file color in the color-map entry
2638 * on the sRGB color in 'back'.
2639 */
2640 png_create_colormap_entry(display, i,
2641 png_colormap_compose(display, colormap[i].red, E_FILE,
2642 trans[i], back_r, output_encoding),
2643 png_colormap_compose(display, colormap[i].green, E_FILE,
2644 trans[i], back_g, output_encoding),
2645 png_colormap_compose(display, colormap[i].blue, E_FILE,
2646 trans[i], back_b, output_encoding),
2647 output_encoding == E_LINEAR ? trans[i] * 257U :
2648 trans[i],
2649 output_encoding);
2650 }
2651 }
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06002652
John Bowler5bc90382012-01-23 22:43:22 -06002653 else
2654 png_create_colormap_entry(display, i, colormap[i].red,
2655 colormap[i].green, colormap[i].blue,
2656 i < num_trans ? trans[i] : 255U, E_FILE/*8-bit*/);
2657 }
2658
2659 /* The PNG data may have indicies packed in fewer than 8 bits, it
2660 * must be expanded if so.
2661 */
2662 if (png_ptr->bit_depth < 8)
2663 png_set_packing(png_ptr);
2664 }
2665 break;
2666
2667 default:
2668 png_error(png_ptr, "invalid PNG color type");
2669 /*NOT REACHED*/
2670 break;
2671 }
2672
2673 /* Now deal with the output processing */
2674 if (expand_tRNS && png_ptr->num_trans > 0 &&
2675 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
2676 png_set_tRNS_to_alpha(png_ptr);
2677
2678 switch (data_encoding)
2679 {
2680 default:
2681 png_error(png_ptr, "bad data option (internal error)");
2682 break;
2683
John Bowler5bc90382012-01-23 22:43:22 -06002684 case E_sRGB:
2685 /* Change to 8-bit sRGB */
2686 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
John Bowlereda53e52012-03-18 22:46:28 -05002687 /* FALL THROUGH */
2688
2689 case E_FILE:
John Bowler5bc90382012-01-23 22:43:22 -06002690 if (png_ptr->bit_depth > 8)
2691 png_set_scale_16(png_ptr);
2692 break;
2693 }
2694
2695 if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2696 png_error(png_ptr, "color map overflow (BAD internal error)");
2697
2698 image->colormap_entries = cmap_entries;
2699
2700 /* Double check using the recorded background index */
2701 switch (output_processing)
2702 {
2703 case PNG_CMAP_NONE:
2704 if (background_index != PNG_CMAP_NONE_BACKGROUND)
2705 goto bad_background;
2706 break;
2707
2708 case PNG_CMAP_GA:
2709 if (background_index != PNG_CMAP_GA_BACKGROUND)
2710 goto bad_background;
2711 break;
2712
2713 case PNG_CMAP_TRANS:
2714 if (background_index >= cmap_entries ||
2715 background_index != PNG_CMAP_TRANS_BACKGROUND)
2716 goto bad_background;
2717 break;
2718
2719 case PNG_CMAP_RGB:
2720 if (background_index != PNG_CMAP_RGB_BACKGROUND)
2721 goto bad_background;
2722 break;
2723
2724 case PNG_CMAP_RGB_ALPHA:
2725 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
2726 goto bad_background;
2727 break;
2728
2729 default:
2730 png_error(png_ptr, "bad processing option (internal error)");
2731
2732 bad_background:
2733 png_error(png_ptr, "bad background index (internal error)");
2734 }
2735
2736 display->colormap_processing = output_processing;
2737
2738 return 1/*ok*/;
2739}
2740
2741/* The final part of the color-map read called from png_image_finish_read. */
2742static int
2743png_image_read_and_map(png_voidp argument)
John Bowler7875d532011-11-07 22:33:49 -06002744{
John Bowler4fa96a42011-11-16 16:39:16 -06002745 png_image_read_control *display = png_voidcast(png_image_read_control*,
2746 argument);
John Bowler7875d532011-11-07 22:33:49 -06002747 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06002748 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler4f67e402011-12-28 08:43:37 -06002749 int passes;
John Bowler7875d532011-11-07 22:33:49 -06002750
John Bowler5bc90382012-01-23 22:43:22 -06002751 /* Called when the libpng data must be transformed into the color-mapped
2752 * form. There is a local row buffer in display->local and this routine must
2753 * do the interlace handling.
2754 */
2755 switch (png_ptr->interlaced)
John Bowler7875d532011-11-07 22:33:49 -06002756 {
2757 case PNG_INTERLACE_NONE:
2758 passes = 1;
2759 break;
2760
2761 case PNG_INTERLACE_ADAM7:
2762 passes = PNG_INTERLACE_ADAM7_PASSES;
2763 break;
2764
2765 default:
John Bowler4f67e402011-12-28 08:43:37 -06002766 passes = 0;
John Bowler7875d532011-11-07 22:33:49 -06002767 png_error(png_ptr, "unknown interlace type");
2768 }
2769
2770 {
John Bowler5bc90382012-01-23 22:43:22 -06002771 png_uint_32 height = image->height;
2772 png_uint_32 width = image->width;
2773 int proc = display->colormap_processing;
2774 png_bytep first_row = display->first_row;
2775 ptrdiff_t step_row = display->row_bytes;
John Bowler7875d532011-11-07 22:33:49 -06002776 int pass;
2777
2778 for (pass = 0; pass < passes; ++pass)
2779 {
John Bowler7875d532011-11-07 22:33:49 -06002780 unsigned int startx, stepx, stepy;
2781 png_uint_32 y;
2782
John Bowler5bc90382012-01-23 22:43:22 -06002783 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
John Bowler7875d532011-11-07 22:33:49 -06002784 {
2785 /* The row may be empty for a short image: */
2786 if (PNG_PASS_COLS(width, pass) == 0)
2787 continue;
2788
2789 startx = PNG_PASS_START_COL(pass);
2790 stepx = PNG_PASS_COL_OFFSET(pass);
2791 y = PNG_PASS_START_ROW(pass);
John Bowler18c5cfa2011-11-16 14:26:34 -06002792 stepy = PNG_PASS_ROW_OFFSET(pass);
John Bowler7875d532011-11-07 22:33:49 -06002793 }
2794
2795 else
2796 {
2797 y = 0;
2798 startx = 0;
2799 stepx = stepy = 1;
2800 }
John Bowler18c5cfa2011-11-16 14:26:34 -06002801
John Bowler7875d532011-11-07 22:33:49 -06002802 for (; y<height; y += stepy)
John Bowler5bc90382012-01-23 22:43:22 -06002803 {
2804 png_bytep inrow = display->local_row;
2805 png_bytep outrow = first_row + y * step_row;
2806 png_const_bytep end_row = outrow + width;
2807
2808 /* Read read the libpng data into the temporary buffer. */
2809 png_read_row(png_ptr, inrow, NULL);
2810
2811 /* Now process the row according to the processing option, note
2812 * that the caller verifies that the format of the libpng output
2813 * data is as required.
2814 */
2815 outrow += startx;
2816 switch (proc)
John Bowler7875d532011-11-07 22:33:49 -06002817 {
John Bowler5bc90382012-01-23 22:43:22 -06002818 case PNG_CMAP_GA:
2819 for (; outrow < end_row; outrow += stepx)
John Bowler7875d532011-11-07 22:33:49 -06002820 {
John Bowler5bc90382012-01-23 22:43:22 -06002821 /* The data is always in the PNG order */
2822 unsigned int gray = *inrow++;
2823 unsigned int alpha = *inrow++;
2824 unsigned int entry;
John Bowler7875d532011-11-07 22:33:49 -06002825
John Bowler5bc90382012-01-23 22:43:22 -06002826 /* NOTE: this code is copied as a comment in
2827 * make_ga_colormap above. Please update the
2828 * comment if you change this code!
2829 */
2830 if (alpha > 229) /* opaque */
John Bowler7875d532011-11-07 22:33:49 -06002831 {
John Bowler5bc90382012-01-23 22:43:22 -06002832 entry = (231 * gray + 128) >> 8;
John Bowler7875d532011-11-07 22:33:49 -06002833 }
John Bowler5bc90382012-01-23 22:43:22 -06002834 else if (alpha < 26) /* transparent */
2835 {
2836 entry = 231;
2837 }
2838 else /* partially opaque */
2839 {
2840 entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
2841 }
John Bowler7875d532011-11-07 22:33:49 -06002842
John Bowler5bc90382012-01-23 22:43:22 -06002843 *outrow = (png_byte)entry;
2844 }
2845 break;
2846
2847 case PNG_CMAP_TRANS:
2848 for (; outrow < end_row; outrow += stepx)
2849 {
2850 png_byte gray = *inrow++;
2851 png_byte alpha = *inrow++;
2852
2853 if (alpha == 0)
2854 *outrow = PNG_CMAP_TRANS_BACKGROUND;
2855
2856 else if (gray != PNG_CMAP_TRANS_BACKGROUND)
2857 *outrow = gray;
2858
2859 else
2860 *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
2861 }
2862 break;
2863
2864 case PNG_CMAP_RGB:
2865 for (; outrow < end_row; outrow += stepx)
2866 {
2867 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
2868 inrow += 3;
2869 }
2870 break;
2871
2872 case PNG_CMAP_RGB_ALPHA:
2873 for (; outrow < end_row; outrow += stepx)
2874 {
2875 unsigned int alpha = inrow[3];
2876
2877 /* Because the alpha entries only hold alpha==0.5 values
2878 * split the processing at alpha==0.25 (64) and 0.75
2879 * (196).
2880 */
2881
2882 if (alpha >= 196)
2883 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
2884 inrow[2]);
2885
2886 else if (alpha < 64)
2887 *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
2888
2889 else
2890 {
2891 /* Likewise there are three entries for each of r, g
2892 * and b. We could select the entry by popcount on
2893 * the top two bits on those architectures that
2894 * support it, this is what the code below does,
2895 * crudely.
2896 */
2897 unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
2898
2899 /* Here are how the values map:
2900 *
2901 * 0x00 .. 0x3f -> 0
2902 * 0x40 .. 0xbf -> 1
2903 * 0xc0 .. 0xff -> 2
2904 *
2905 * So, as above with the explicit alpha checks, the
2906 * breakpoints are at 64 and 196.
2907 */
2908 if (inrow[0] & 0x80) back_i += 9; /* red */
2909 if (inrow[0] & 0x40) back_i += 9;
2910 if (inrow[0] & 0x80) back_i += 3; /* green */
2911 if (inrow[0] & 0x40) back_i += 3;
2912 if (inrow[0] & 0x80) back_i += 1; /* blue */
2913 if (inrow[0] & 0x40) back_i += 1;
2914
2915 *outrow = (png_byte)back_i;
2916 }
2917
2918 inrow += 4;
2919 }
2920 break;
2921
2922 default:
2923 break;
2924 }
2925 }
2926 }
2927 }
2928
2929 return 1;
2930}
2931
2932static int
2933png_image_read_colormapped(png_voidp argument)
2934{
2935 png_image_read_control *display = png_voidcast(png_image_read_control*,
2936 argument);
2937 png_imagep image = display->image;
2938 png_controlp control = image->opaque;
2939 png_structrp png_ptr = control->png_ptr;
2940 png_inforp info_ptr = control->info_ptr;
2941
2942 int passes = 0; /* As a flag */
2943
2944 PNG_SKIP_CHUNKS(png_ptr);
2945
2946 /* Update the 'info' structure and make sure the result is as required; first
2947 * make sure to turn on the interlace handling if it will be required
2948 * (because it can't be turned on *after* the call to png_read_update_info!)
2949 */
2950 if (display->colormap_processing == PNG_CMAP_NONE)
2951 passes = png_set_interlace_handling(png_ptr);
2952
2953 png_read_update_info(png_ptr, info_ptr);
2954
2955 /* The expected output can be deduced from the colormap_processing option. */
2956 switch (display->colormap_processing)
2957 {
2958 case PNG_CMAP_NONE:
2959 /* Output must be one channel and one byte per pixel, the output
2960 * encoding can be anything.
2961 */
2962 if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
2963 info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
2964 info_ptr->bit_depth == 8)
2965 break;
2966
2967 goto bad_output;
2968
2969 case PNG_CMAP_TRANS:
2970 case PNG_CMAP_GA:
2971 /* Output must be two channels and the 'G' one must be sRGB, the latter
2972 * can be checked with an exact number because it should have been set
2973 * to this number above!
2974 */
2975 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
2976 info_ptr->bit_depth == 8 &&
2977 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
2978 image->colormap_entries == 256)
2979 break;
2980
2981 goto bad_output;
2982
2983 case PNG_CMAP_RGB:
2984 /* Output must be 8-bit sRGB encoded RGB */
2985 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
2986 info_ptr->bit_depth == 8 &&
2987 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
2988 image->colormap_entries == 216)
2989 break;
2990
2991 goto bad_output;
2992
2993 case PNG_CMAP_RGB_ALPHA:
2994 /* Output must be 8-bit sRGB encoded RGBA */
2995 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
2996 info_ptr->bit_depth == 8 &&
2997 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
2998 image->colormap_entries == 244 /* 216 + 1 + 27 */)
2999 break;
3000
3001 /* goto bad_output; */
3002 /* FALL THROUGH */
3003
3004 default:
3005 bad_output:
3006 png_error(png_ptr, "bad color-map processing (internal error)");
3007 }
3008
3009 /* Now read the rows. Do this here if it is possible to read directly into
3010 * the output buffer, otherwise allocate a local row buffer of the maximum
3011 * size libpng requires and call the relevant processing routine safely.
3012 */
3013 {
3014 png_bytep first_row = png_voidcast(png_bytep, display->buffer);
3015 ptrdiff_t row_bytes = display->row_stride;
3016
3017 /* The following expression is designed to work correctly whether it gives
3018 * a signed or an unsigned result.
3019 */
3020 if (row_bytes < 0)
3021 first_row += (image->height-1) * (-row_bytes);
3022
3023 display->first_row = first_row;
3024 display->row_bytes = row_bytes;
3025 }
3026
3027 if (passes == 0)
3028 {
3029 int result;
3030 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
3031 png_get_rowbytes(png_ptr, info_ptr)));
3032
3033 display->local_row = row;
3034 result = png_safe_execute(image, png_image_read_and_map, display);
3035 display->local_row = NULL;
3036 png_free(png_ptr, row);
3037
3038 return result;
3039 }
3040
3041 else
3042 {
3043 png_alloc_size_t row_bytes = display->row_bytes;
3044
3045 while (--passes >= 0)
3046 {
3047 png_uint_32 y = image->height;
3048 png_bytep row = display->first_row;
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003049
John Bowler5bc90382012-01-23 22:43:22 -06003050 while (y-- > 0)
3051 {
3052 png_read_row(png_ptr, row, NULL);
3053 row += row_bytes;
3054 }
3055 }
3056
3057 return 1;
3058 }
3059}
3060
3061/* Just the row reading part of png_image_read. */
3062static int
3063png_image_read_composite(png_voidp argument)
3064{
3065 png_image_read_control *display = png_voidcast(png_image_read_control*,
3066 argument);
3067 png_imagep image = display->image;
3068 png_structrp png_ptr = image->opaque->png_ptr;
3069 int passes;
3070
3071 switch (png_ptr->interlaced)
3072 {
3073 case PNG_INTERLACE_NONE:
3074 passes = 1;
3075 break;
3076
3077 case PNG_INTERLACE_ADAM7:
3078 passes = PNG_INTERLACE_ADAM7_PASSES;
3079 break;
3080
3081 default:
3082 passes = 0;
3083 png_error(png_ptr, "unknown interlace type");
3084 }
3085
3086 {
3087 png_uint_32 height = image->height;
3088 png_uint_32 width = image->width;
3089 png_bytep first_row = display->first_row;
3090 ptrdiff_t step_row = display->row_bytes;
3091 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
3092 int pass;
3093
3094 for (pass = 0; pass < passes; ++pass)
3095 {
3096 unsigned int startx, stepx, stepy;
3097 png_uint_32 y;
3098
3099 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3100 {
3101 /* The row may be empty for a short image: */
3102 if (PNG_PASS_COLS(width, pass) == 0)
3103 continue;
3104
3105 startx = PNG_PASS_START_COL(pass) * channels;
3106 stepx = PNG_PASS_COL_OFFSET(pass) * channels;
3107 y = PNG_PASS_START_ROW(pass);
3108 stepy = PNG_PASS_ROW_OFFSET(pass);
3109 }
3110
3111 else
3112 {
3113 y = 0;
3114 startx = 0;
3115 stepx = channels;
3116 stepy = 1;
3117 }
3118
3119 for (; y<height; y += stepy)
3120 {
3121 png_bytep inrow = display->local_row;
3122 png_bytep outrow = first_row + y * step_row;
3123 png_const_bytep end_row = outrow + width * channels;
3124
3125 /* Read the row, which is packed: */
3126 png_read_row(png_ptr, inrow, NULL);
3127
3128 /* Now do the composition on each pixel in this row. */
3129 outrow += startx;
3130 for (; outrow < end_row; outrow += stepx)
3131 {
3132 png_byte alpha = inrow[channels];
3133
3134 if (alpha > 0) /* else no change to the output */
3135 {
3136 unsigned int c;
3137
3138 for (c=0; c<channels; ++c)
3139 {
3140 png_uint_32 component = inrow[c];
3141
3142 if (alpha < 255) /* else just use component */
3143 {
3144 /* This is PNG_OPTIMIZED_ALPHA, the component value
3145 * is a linear 8-bit value. Combine this with the
3146 * current outrow[c] value which is sRGB encoded.
3147 * Arithmetic here is 16-bits to preserve the output
3148 * values correctly.
3149 */
3150 component *= 257*255; /* =65535 */
3151 component += (255-alpha)*png_sRGB_table[outrow[c]];
3152
3153 /* So 'component' is scaled by 255*65535 and is
3154 * therefore appropriate for the sRGB to linear
3155 * conversion table.
3156 */
3157 component = PNG_sRGB_FROM_LINEAR(component);
3158 }
3159
3160 outrow[c] = (png_byte)component;
3161 }
John Bowler7875d532011-11-07 22:33:49 -06003162 }
3163
John Bowler5bc90382012-01-23 22:43:22 -06003164 inrow += channels+1; /* components and alpha channel */
John Bowler7875d532011-11-07 22:33:49 -06003165 }
John Bowler5bc90382012-01-23 22:43:22 -06003166 }
John Bowler7875d532011-11-07 22:33:49 -06003167 }
3168 }
3169
3170 return 1;
3171}
3172
John Bowler18c5cfa2011-11-16 14:26:34 -06003173/* The do_local_background case; called when all the following transforms are to
3174 * be done:
3175 *
3176 * PNG_RGB_TO_GRAY
3177 * PNG_COMPOSITE
3178 * PNG_GAMMA
3179 *
3180 * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and
3181 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
3182 * correction. The fix-up is to prevent the PNG_COMPOSITE operation happening
3183 * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles
3184 * the removal or pre-multiplication of the alpha channel.
3185 */
3186static int
3187png_image_read_background(png_voidp argument)
3188{
John Bowler4fa96a42011-11-16 16:39:16 -06003189 png_image_read_control *display = png_voidcast(png_image_read_control*,
3190 argument);
John Bowler18c5cfa2011-11-16 14:26:34 -06003191 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06003192 png_structrp png_ptr = image->opaque->png_ptr;
3193 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler18c5cfa2011-11-16 14:26:34 -06003194 png_uint_32 height = image->height;
3195 png_uint_32 width = image->width;
John Bowler4f67e402011-12-28 08:43:37 -06003196 int pass, passes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003197
3198 /* Double check the convoluted logic below. We expect to get here with
3199 * libpng doing rgb to gray and gamma correction but background processing
3200 * left to the png_image_read_background function. The rows libpng produce
3201 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
3202 */
3203 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
3204 png_error(png_ptr, "lost rgb to gray");
3205
3206 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
3207 png_error(png_ptr, "unexpected compose");
3208
John Bowler18c5cfa2011-11-16 14:26:34 -06003209 if (png_get_channels(png_ptr, info_ptr) != 2)
3210 png_error(png_ptr, "lost/gained channels");
3211
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003212 /* Expect the 8-bit case to always remove the alpha channel */
3213 if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
3214 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
3215 png_error(png_ptr, "unexpected 8-bit transformation");
3216
John Bowler5bc90382012-01-23 22:43:22 -06003217 switch (png_ptr->interlaced)
John Bowler18c5cfa2011-11-16 14:26:34 -06003218 {
3219 case PNG_INTERLACE_NONE:
3220 passes = 1;
3221 break;
3222
3223 case PNG_INTERLACE_ADAM7:
3224 passes = PNG_INTERLACE_ADAM7_PASSES;
3225 break;
3226
3227 default:
John Bowler4f67e402011-12-28 08:43:37 -06003228 passes = 0;
John Bowler18c5cfa2011-11-16 14:26:34 -06003229 png_error(png_ptr, "unknown interlace type");
3230 }
3231
3232 switch (png_get_bit_depth(png_ptr, info_ptr))
3233 {
3234 default:
3235 png_error(png_ptr, "unexpected bit depth");
3236 break;
3237
3238 case 8:
3239 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
3240 * to be removed by composing on a backgroundi: either the row if
John Bowler5bc90382012-01-23 22:43:22 -06003241 * display->background is NULL or display->background->green if not.
John Bowler18c5cfa2011-11-16 14:26:34 -06003242 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3243 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003244 {
John Bowler5bc90382012-01-23 22:43:22 -06003245 png_bytep first_row = display->first_row;
3246 ptrdiff_t step_row = display->row_bytes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003247
John Bowler5bc90382012-01-23 22:43:22 -06003248 for (pass = 0; pass < passes; ++pass)
John Bowler18c5cfa2011-11-16 14:26:34 -06003249 {
John Bowler5bc90382012-01-23 22:43:22 -06003250 png_bytep row = display->first_row;
3251 unsigned int startx, stepx, stepy;
3252 png_uint_32 y;
John Bowler18c5cfa2011-11-16 14:26:34 -06003253
John Bowler5bc90382012-01-23 22:43:22 -06003254 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3255 {
3256 /* The row may be empty for a short image: */
3257 if (PNG_PASS_COLS(width, pass) == 0)
3258 continue;
John Bowler18c5cfa2011-11-16 14:26:34 -06003259
John Bowler5bc90382012-01-23 22:43:22 -06003260 startx = PNG_PASS_START_COL(pass);
3261 stepx = PNG_PASS_COL_OFFSET(pass);
3262 y = PNG_PASS_START_ROW(pass);
3263 stepy = PNG_PASS_ROW_OFFSET(pass);
3264 }
3265
3266 else
3267 {
3268 y = 0;
3269 startx = 0;
3270 stepx = stepy = 1;
3271 }
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003272
John Bowler5bc90382012-01-23 22:43:22 -06003273 if (display->background == NULL)
3274 {
3275 for (; y<height; y += stepy)
John Bowler18c5cfa2011-11-16 14:26:34 -06003276 {
3277 png_bytep inrow = display->local_row;
John Bowler5bc90382012-01-23 22:43:22 -06003278 png_bytep outrow = first_row + y * step_row;
3279 png_const_bytep end_row = outrow + width;
John Bowler18c5cfa2011-11-16 14:26:34 -06003280
3281 /* Read the row, which is packed: */
3282 png_read_row(png_ptr, inrow, NULL);
3283
3284 /* Now do the composition on each pixel in this row. */
John Bowler5bc90382012-01-23 22:43:22 -06003285 outrow += startx;
John Bowler18c5cfa2011-11-16 14:26:34 -06003286 for (; outrow < end_row; outrow += stepx)
3287 {
3288 png_byte alpha = inrow[1];
3289
3290 if (alpha > 0) /* else no change to the output */
3291 {
3292 png_uint_32 component = inrow[0];
3293
3294 if (alpha < 255) /* else just use component */
3295 {
3296 /* Since PNG_OPTIMIZED_ALPHA was not set it is
3297 * necessary to invert the sRGB transfer
3298 * function and multiply the alpha out.
3299 */
3300 component = png_sRGB_table[component] * alpha;
3301 component += png_sRGB_table[outrow[0]] *
3302 (255-alpha);
3303 component = PNG_sRGB_FROM_LINEAR(component);
3304 }
3305
3306 outrow[0] = (png_byte)component;
3307 }
3308
3309 inrow += 2; /* gray and alpha channel */
3310 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003311 }
John Bowler5bc90382012-01-23 22:43:22 -06003312 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003313
John Bowler5bc90382012-01-23 22:43:22 -06003314 else /* constant background value */
3315 {
3316 png_byte background8 = display->background->green;
3317 png_uint_16 background = png_sRGB_table[background8];
John Bowler18c5cfa2011-11-16 14:26:34 -06003318
John Bowler5bc90382012-01-23 22:43:22 -06003319 for (; y<height; y += stepy)
John Bowler18c5cfa2011-11-16 14:26:34 -06003320 {
3321 png_bytep inrow = display->local_row;
John Bowler5bc90382012-01-23 22:43:22 -06003322 png_bytep outrow = first_row + y * step_row;
3323 png_const_bytep end_row = outrow + width;
John Bowler18c5cfa2011-11-16 14:26:34 -06003324
3325 /* Read the row, which is packed: */
3326 png_read_row(png_ptr, inrow, NULL);
3327
3328 /* Now do the composition on each pixel in this row. */
John Bowler5bc90382012-01-23 22:43:22 -06003329 outrow += startx;
John Bowler18c5cfa2011-11-16 14:26:34 -06003330 for (; outrow < end_row; outrow += stepx)
3331 {
3332 png_byte alpha = inrow[1];
3333
3334 if (alpha > 0) /* else use background */
3335 {
3336 png_uint_32 component = inrow[0];
3337
3338 if (alpha < 255) /* else just use component */
3339 {
3340 component = png_sRGB_table[component] * alpha;
3341 component += background * (255-alpha);
3342 component = PNG_sRGB_FROM_LINEAR(component);
3343 }
3344
3345 outrow[0] = (png_byte)component;
3346 }
3347
3348 else
3349 outrow[0] = background8;
3350
3351 inrow += 2; /* gray and alpha channel */
3352 }
3353
3354 row += display->row_bytes;
3355 }
John Bowler5bc90382012-01-23 22:43:22 -06003356 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003357 }
3358 }
3359 break;
3360
3361 case 16:
3362 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3363 * still be done and, maybe, the alpha channel removed. This code also
3364 * handles the alpha-first option.
3365 */
3366 {
John Bowler5bc90382012-01-23 22:43:22 -06003367 png_bytep first_row = display->first_row;
3368 ptrdiff_t step_row = display->row_bytes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003369 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003370 unsigned int outchannels = 1+preserve_alpha;
John Bowler18c5cfa2011-11-16 14:26:34 -06003371 int swap_alpha = 0;
3372
3373 if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST))
3374 swap_alpha = 1;
3375
3376 for (pass = 0; pass < passes; ++pass)
3377 {
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003378 unsigned int startx, stepx, stepy;
John Bowler18c5cfa2011-11-16 14:26:34 -06003379 png_uint_32 y;
3380
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003381 /* The 'x' start and step are adjusted to output components here.
3382 */
John Bowler5bc90382012-01-23 22:43:22 -06003383 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
John Bowler18c5cfa2011-11-16 14:26:34 -06003384 {
3385 /* The row may be empty for a short image: */
3386 if (PNG_PASS_COLS(width, pass) == 0)
3387 continue;
3388
John Bowler5bc90382012-01-23 22:43:22 -06003389 startx = PNG_PASS_START_COL(pass) * outchannels;
3390 stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
John Bowler18c5cfa2011-11-16 14:26:34 -06003391 y = PNG_PASS_START_ROW(pass);
3392 stepy = PNG_PASS_ROW_OFFSET(pass);
3393 }
3394
3395 else
3396 {
3397 y = 0;
3398 startx = 0;
John Bowler5bc90382012-01-23 22:43:22 -06003399 stepx = outchannels;
3400 stepy = 1;
John Bowler18c5cfa2011-11-16 14:26:34 -06003401 }
3402
John Bowler18c5cfa2011-11-16 14:26:34 -06003403 for (; y<height; y += stepy)
John Bowler5bc90382012-01-23 22:43:22 -06003404 {
3405 png_const_uint_16p inrow;
3406 png_uint_16p outrow = (png_uint_16p)(first_row + y*step_row);
3407 png_uint_16p end_row = outrow + width * outchannels;
3408
3409 /* Read the row, which is packed: */
3410 png_read_row(png_ptr, display->local_row, NULL);
3411 inrow = (png_const_uint_16p)display->local_row;
3412
3413 /* Now do the pre-multiplication on each pixel in this row.
3414 */
3415 outrow += startx;
3416 for (; outrow < end_row; outrow += stepx)
John Bowler18c5cfa2011-11-16 14:26:34 -06003417 {
John Bowler5bc90382012-01-23 22:43:22 -06003418 png_uint_32 component = inrow[0];
3419 png_uint_16 alpha = inrow[1];
John Bowler18c5cfa2011-11-16 14:26:34 -06003420
John Bowler5bc90382012-01-23 22:43:22 -06003421 if (alpha > 0) /* else 0 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003422 {
John Bowler5bc90382012-01-23 22:43:22 -06003423 if (alpha < 65535) /* else just use component */
John Bowler18c5cfa2011-11-16 14:26:34 -06003424 {
John Bowler5bc90382012-01-23 22:43:22 -06003425 component *= alpha;
3426 component += 32767;
3427 component /= 65535;
John Bowler18c5cfa2011-11-16 14:26:34 -06003428 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003429 }
3430
John Bowler5bc90382012-01-23 22:43:22 -06003431 else
3432 component = 0;
3433
3434 outrow[swap_alpha] = (png_uint_16)component;
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003435 if (preserve_alpha)
John Bowler5bc90382012-01-23 22:43:22 -06003436 outrow[1 ^ swap_alpha] = alpha;
3437
3438 inrow += 2; /* components and alpha channel */
John Bowler18c5cfa2011-11-16 14:26:34 -06003439 }
John Bowler5bc90382012-01-23 22:43:22 -06003440 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003441 }
3442 }
3443 break;
3444 }
3445
3446 return 1;
3447}
3448
John Bowler7875d532011-11-07 22:33:49 -06003449/* The guts of png_image_finish_read as a png_safe_execute callback. */
3450static int
John Bowler5bc90382012-01-23 22:43:22 -06003451png_image_read_direct(png_voidp argument)
John Bowler7875d532011-11-07 22:33:49 -06003452{
John Bowler4fa96a42011-11-16 16:39:16 -06003453 png_image_read_control *display = png_voidcast(png_image_read_control*,
3454 argument);
John Bowler7875d532011-11-07 22:33:49 -06003455 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06003456 png_structrp png_ptr = image->opaque->png_ptr;
3457 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06003458
3459 png_uint_32 format = image->format;
3460 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
3461 int do_local_compose = 0;
John Bowler18c5cfa2011-11-16 14:26:34 -06003462 int do_local_background = 0; /* to avoid double gamma correction bug */
John Bowler7875d532011-11-07 22:33:49 -06003463 int passes = 0;
3464
3465 /* Add transforms to ensure the correct output format is produced then check
3466 * that the required implementation support is there. Always expand; always
3467 * need 8 bits minimum, no palette and expanded tRNS.
3468 */
3469 png_set_expand(png_ptr);
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003470
John Bowler7875d532011-11-07 22:33:49 -06003471 /* Now check the format to see if it was modified. */
3472 {
John Bowler5bc90382012-01-23 22:43:22 -06003473 png_uint_32 base_format = png_image_format(png_ptr) &
3474 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
John Bowler7875d532011-11-07 22:33:49 -06003475 png_uint_32 change = format ^ base_format;
John Bowler3615d032011-11-08 10:38:09 -06003476 png_fixed_point output_gamma;
3477 int mode; /* alpha mode */
John Bowler7875d532011-11-07 22:33:49 -06003478
John Bowler18c5cfa2011-11-16 14:26:34 -06003479 /* Do this first so that we have a record if rgb to gray is happening. */
3480 if (change & PNG_FORMAT_FLAG_COLOR)
3481 {
3482 /* gray<->color transformation required. */
3483 if (format & PNG_FORMAT_FLAG_COLOR)
3484 png_set_gray_to_rgb(png_ptr);
3485
3486 else
3487 {
3488 /* libpng can't do both rgb to gray and
3489 * background/pre-multiplication if there is also significant gamma
3490 * correction, because both operations require linear colors and
3491 * the code only supports one transform doing the gamma correction.
3492 * Handle this by doing the pre-multiplication or background
3493 * operation in this code, if necessary.
3494 *
3495 * TODO: fix this by rewriting pngrtran.c (!)
3496 *
3497 * For the moment (given that fixing this in pngrtran.c is an
3498 * enormous change) 'do_local_background' is used to indicate that
3499 * the problem exists.
3500 */
3501 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3502 do_local_background = 1/*maybe*/;
3503
3504 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
3505 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
3506 }
3507
3508 change &= ~PNG_FORMAT_FLAG_COLOR;
3509 }
3510
John Bowler7875d532011-11-07 22:33:49 -06003511 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3512 */
3513 {
John Bowler3615d032011-11-08 10:38:09 -06003514 png_fixed_point input_gamma_default;
John Bowler7875d532011-11-07 22:33:49 -06003515
3516 if (base_format & PNG_FORMAT_FLAG_LINEAR)
3517 input_gamma_default = PNG_GAMMA_LINEAR;
3518 else
3519 input_gamma_default = PNG_DEFAULT_sRGB;
3520
John Bowler18c5cfa2011-11-16 14:26:34 -06003521 /* Call png_set_alpha_mode to set the default for the input gamma; the
3522 * output gamma is set by a second call below.
3523 */
3524 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
3525 }
3526
3527 if (linear)
3528 {
3529 /* If there *is* an alpha channel in the input it must be multiplied
3530 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3531 */
3532 if (base_format & PNG_FORMAT_FLAG_ALPHA)
John Bowler3615d032011-11-08 10:38:09 -06003533 mode = PNG_ALPHA_STANDARD; /* associated alpha */
John Bowler7875d532011-11-07 22:33:49 -06003534
3535 else
John Bowler7875d532011-11-07 22:33:49 -06003536 mode = PNG_ALPHA_PNG;
John Bowler18c5cfa2011-11-16 14:26:34 -06003537
3538 output_gamma = PNG_GAMMA_LINEAR;
3539 }
3540
3541 else
3542 {
3543 mode = PNG_ALPHA_PNG;
3544 output_gamma = PNG_DEFAULT_sRGB;
3545 }
3546
3547 /* If 'do_local_background' is set check for the presence of gamma
3548 * correction; this is part of the work-round for the libpng bug
3549 * described above.
3550 *
3551 * TODO: fix libpng and remove this.
3552 */
3553 if (do_local_background)
3554 {
3555 png_fixed_point gtest;
3556
3557 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3558 * gamma correction, the screen gamma hasn't been set on png_struct
3559 * yet; it's set below. png_struct::gamma, however, is set to the
3560 * final value.
3561 */
John Bowlerb11b31a2012-03-21 07:55:46 -05003562 if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
3563 PNG_FP_1) && !png_gamma_significant(gtest))
John Bowler18c5cfa2011-11-16 14:26:34 -06003564 do_local_background = 0;
3565
3566 else if (mode == PNG_ALPHA_STANDARD)
3567 {
3568 do_local_background = 2/*required*/;
3569 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
John Bowler7875d532011-11-07 22:33:49 -06003570 }
3571
John Bowler18c5cfa2011-11-16 14:26:34 -06003572 /* else leave as 1 for the checks below */
John Bowler3615d032011-11-08 10:38:09 -06003573 }
John Bowler7875d532011-11-07 22:33:49 -06003574
John Bowler3615d032011-11-08 10:38:09 -06003575 /* If the bit-depth changes then handle that here. */
3576 if (change & PNG_FORMAT_FLAG_LINEAR)
3577 {
3578 if (linear /*16-bit output*/)
3579 png_set_expand_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06003580
John Bowler3615d032011-11-08 10:38:09 -06003581 else /* 8-bit output */
3582 png_set_scale_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06003583
John Bowler3615d032011-11-08 10:38:09 -06003584 change &= ~PNG_FORMAT_FLAG_LINEAR;
John Bowler7875d532011-11-07 22:33:49 -06003585 }
3586
3587 /* Now the background/alpha channel changes. */
3588 if (change & PNG_FORMAT_FLAG_ALPHA)
3589 {
3590 /* Removing an alpha channel requires composition for the 8-bit
3591 * formats; for the 16-bit it is already done, above, by the
3592 * pre-multiplication and the channel just needs to be stripped.
3593 */
3594 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3595 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003596 /* If RGB->gray is happening the alpha channel must be left and the
3597 * operation completed locally.
3598 *
3599 * TODO: fix libpng and remove this.
3600 */
3601 if (do_local_background)
3602 do_local_background = 2/*required*/;
3603
John Bowler3615d032011-11-08 10:38:09 -06003604 /* 16-bit output: just remove the channel */
John Bowler18c5cfa2011-11-16 14:26:34 -06003605 else if (linear) /* compose on black (well, pre-multiply) */
John Bowler7875d532011-11-07 22:33:49 -06003606 png_set_strip_alpha(png_ptr);
3607
John Bowler3615d032011-11-08 10:38:09 -06003608 /* 8-bit output: do an appropriate compose */
John Bowler7875d532011-11-07 22:33:49 -06003609 else if (display->background != NULL)
3610 {
3611 png_color_16 c;
3612
3613 c.index = 0; /*unused*/
3614 c.red = display->background->red;
3615 c.green = display->background->green;
3616 c.blue = display->background->blue;
3617 c.gray = display->background->green;
3618
3619 /* This is always an 8-bit sRGB value, using the 'green' channel
3620 * for gray is much better than calculating the luminance here;
3621 * we can get off-by-one errors in that calculation relative to
3622 * the app expectations and that will show up in transparent
3623 * pixels.
3624 */
3625 png_set_background_fixed(png_ptr, &c,
3626 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
3627 0/*gamma: not used*/);
3628 }
3629
3630 else /* compose on row: implemented below. */
3631 {
3632 do_local_compose = 1;
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003633 /* This leaves the alpha channel in the output, so it has to be
John Bowler7875d532011-11-07 22:33:49 -06003634 * removed by the code below. Set the encoding to the 'OPTIMIZE'
3635 * one so the code only has to hack on the pixels that require
3636 * composition.
3637 */
John Bowler3615d032011-11-08 10:38:09 -06003638 mode = PNG_ALPHA_OPTIMIZED;
John Bowler7875d532011-11-07 22:33:49 -06003639 }
3640 }
3641
3642 else /* output needs an alpha channel */
3643 {
3644 /* This is tricky because it happens before the swap operation has
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003645 * been accomplished; however, the swap does *not* swap the added
John Bowlere6fb6912011-11-08 21:35:16 -06003646 * alpha channel (weird API), so it must be added in the correct
3647 * place.
John Bowler7875d532011-11-07 22:33:49 -06003648 */
John Bowler3615d032011-11-08 10:38:09 -06003649 png_uint_32 filler; /* opaque filler */
John Bowlere6fb6912011-11-08 21:35:16 -06003650 int where;
John Bowler3615d032011-11-08 10:38:09 -06003651
3652 if (linear)
3653 filler = 65535;
3654
3655 else
3656 filler = 255;
3657
John Bowlere6fb6912011-11-08 21:35:16 -06003658# ifdef PNG_FORMAT_AFIRST_SUPPORTED
3659 if (format & PNG_FORMAT_FLAG_AFIRST)
3660 {
3661 where = PNG_FILLER_BEFORE;
3662 change &= ~PNG_FORMAT_FLAG_AFIRST;
3663 }
3664
3665 else
3666# endif
3667 where = PNG_FILLER_AFTER;
3668
3669 png_set_add_alpha(png_ptr, filler, where);
John Bowler7875d532011-11-07 22:33:49 -06003670 }
3671
John Bowlere6fb6912011-11-08 21:35:16 -06003672 /* This stops the (irrelevant) call to swap_alpha below. */
John Bowler7875d532011-11-07 22:33:49 -06003673 change &= ~PNG_FORMAT_FLAG_ALPHA;
3674 }
3675
John Bowler3615d032011-11-08 10:38:09 -06003676 /* Now set the alpha mode correctly; this is always done, even if there is
3677 * no alpha channel in either the input or the output because it correctly
3678 * sets the output gamma.
3679 */
3680 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
3681
John Bowler7875d532011-11-07 22:33:49 -06003682# ifdef PNG_FORMAT_BGR_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06003683 if (change & PNG_FORMAT_FLAG_BGR)
John Bowler7875d532011-11-07 22:33:49 -06003684 {
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003685 /* Check only the output format; PNG is never BGR; don't do this if
John Bowler7875d532011-11-07 22:33:49 -06003686 * the output is gray, but fix up the 'format' value in that case.
3687 */
3688 if (format & PNG_FORMAT_FLAG_COLOR)
3689 png_set_bgr(png_ptr);
3690
3691 else
3692 format &= ~PNG_FORMAT_FLAG_BGR;
3693
3694 change &= ~PNG_FORMAT_FLAG_BGR;
3695 }
3696# endif
3697
3698# ifdef PNG_FORMAT_AFIRST_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06003699 if (change & PNG_FORMAT_FLAG_AFIRST)
John Bowler7875d532011-11-07 22:33:49 -06003700 {
3701 /* Only relevant if there is an alpha channel - it's particularly
3702 * important to handle this correctly because do_local_compose may
3703 * be set above and then libpng will keep the alpha channel for this
3704 * code to remove.
3705 */
3706 if (format & PNG_FORMAT_FLAG_ALPHA)
John Bowler18c5cfa2011-11-16 14:26:34 -06003707 {
3708 /* Disable this if doing a local background,
3709 * TODO: remove this when local background is no longer required.
3710 */
3711 if (do_local_background != 2)
3712 png_set_swap_alpha(png_ptr);
3713 }
John Bowler7875d532011-11-07 22:33:49 -06003714
3715 else
3716 format &= ~PNG_FORMAT_FLAG_AFIRST;
3717
3718 change &= ~PNG_FORMAT_FLAG_AFIRST;
3719 }
3720# endif
3721
3722 /* If the *output* is 16-bit then we need to check for a byte-swap on this
3723 * architecture.
3724 */
3725 if (linear)
3726 {
3727 PNG_CONST png_uint_16 le = 0x0001;
3728
3729 if (*(png_const_bytep)&le)
3730 png_set_swap(png_ptr);
3731 }
3732
3733 /* If change is not now 0 some transformation is missing - error out. */
3734 if (change)
3735 png_error(png_ptr, "png_read_image: unsupported transformation");
3736 }
3737
John Bowler5bc90382012-01-23 22:43:22 -06003738 PNG_SKIP_CHUNKS(png_ptr);
John Bowler89c2f842011-11-16 12:04:39 -06003739
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003740 /* Update the 'info' structure and make sure the result is as required; first
John Bowler7875d532011-11-07 22:33:49 -06003741 * make sure to turn on the interlace handling if it will be required
3742 * (because it can't be turned on *after* the call to png_read_update_info!)
John Bowler18c5cfa2011-11-16 14:26:34 -06003743 *
3744 * TODO: remove the do_local_background fixup below.
John Bowler7875d532011-11-07 22:33:49 -06003745 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003746 if (!do_local_compose && do_local_background != 2)
John Bowler7875d532011-11-07 22:33:49 -06003747 passes = png_set_interlace_handling(png_ptr);
3748
3749 png_read_update_info(png_ptr, info_ptr);
3750
3751 {
3752 png_uint_32 info_format = 0;
3753
3754 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
3755 info_format |= PNG_FORMAT_FLAG_COLOR;
3756
3757 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
3758 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003759 /* do_local_compose removes this channel below. */
John Bowler7875d532011-11-07 22:33:49 -06003760 if (!do_local_compose)
John Bowler18c5cfa2011-11-16 14:26:34 -06003761 {
3762 /* do_local_background does the same if required. */
3763 if (do_local_background != 2 ||
3764 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
3765 info_format |= PNG_FORMAT_FLAG_ALPHA;
3766 }
John Bowler7875d532011-11-07 22:33:49 -06003767 }
3768
3769 else if (do_local_compose) /* internal error */
3770 png_error(png_ptr, "png_image_read: alpha channel lost");
3771
3772 if (info_ptr->bit_depth == 16)
3773 info_format |= PNG_FORMAT_FLAG_LINEAR;
3774
3775# ifdef PNG_FORMAT_BGR_SUPPORTED
3776 if (png_ptr->transformations & PNG_BGR)
3777 info_format |= PNG_FORMAT_FLAG_BGR;
3778# endif
3779
3780# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003781 if (do_local_background == 2)
3782 {
3783 if (format & PNG_FORMAT_FLAG_AFIRST)
3784 info_format |= PNG_FORMAT_FLAG_AFIRST;
3785 }
3786
3787 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
John Bowlere6fb6912011-11-08 21:35:16 -06003788 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
3789 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003790 {
3791 if (do_local_background == 2)
3792 png_error(png_ptr, "unexpected alpha swap transformation");
3793
John Bowler7875d532011-11-07 22:33:49 -06003794 info_format |= PNG_FORMAT_FLAG_AFIRST;
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003795 }
John Bowler7875d532011-11-07 22:33:49 -06003796# endif
3797
3798 /* This is actually an internal error. */
3799 if (info_format != format)
3800 png_error(png_ptr, "png_read_image: invalid transformations");
3801 }
3802
3803 /* Now read the rows. If do_local_compose is set then it is necessary to use
3804 * a local row buffer. The output will be GA, RGBA or BGRA and must be
3805 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
3806 * display acts as a flag.
3807 */
3808 {
John Bowler4fa96a42011-11-16 16:39:16 -06003809 png_bytep first_row = png_voidcast(png_bytep, display->buffer);
John Bowler7875d532011-11-07 22:33:49 -06003810 ptrdiff_t row_bytes = display->row_stride;
3811
3812 if (linear)
John Bowler5bc90382012-01-23 22:43:22 -06003813 row_bytes *= 2;
John Bowler7875d532011-11-07 22:33:49 -06003814
3815 /* The following expression is designed to work correctly whether it gives
3816 * a signed or an unsigned result.
3817 */
3818 if (row_bytes < 0)
3819 first_row += (image->height-1) * (-row_bytes);
3820
3821 display->first_row = first_row;
3822 display->row_bytes = row_bytes;
3823 }
3824
3825 if (do_local_compose)
3826 {
3827 int result;
John Bowler4fa96a42011-11-16 16:39:16 -06003828 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
3829 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler7875d532011-11-07 22:33:49 -06003830
3831 display->local_row = row;
3832 result = png_safe_execute(image, png_image_read_composite, display);
3833 display->local_row = NULL;
3834 png_free(png_ptr, row);
3835
3836 return result;
3837 }
3838
John Bowler18c5cfa2011-11-16 14:26:34 -06003839 else if (do_local_background == 2)
3840 {
3841 int result;
John Bowler4fa96a42011-11-16 16:39:16 -06003842 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
3843 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler18c5cfa2011-11-16 14:26:34 -06003844
3845 display->local_row = row;
3846 result = png_safe_execute(image, png_image_read_background, display);
3847 display->local_row = NULL;
3848 png_free(png_ptr, row);
3849
3850 return result;
3851 }
3852
John Bowler7875d532011-11-07 22:33:49 -06003853 else
3854 {
3855 png_alloc_size_t row_bytes = display->row_bytes;
3856
3857 while (--passes >= 0)
3858 {
3859 png_uint_32 y = image->height;
3860 png_bytep row = display->first_row;
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003861
John Bowler7875d532011-11-07 22:33:49 -06003862 while (y-- > 0)
3863 {
3864 png_read_row(png_ptr, row, NULL);
3865 row += row_bytes;
3866 }
3867 }
3868
3869 return 1;
3870 }
3871}
3872
3873int PNGAPI
John Bowler40ca77a2012-01-28 23:19:42 -06003874png_image_finish_read(png_imagep image, png_const_colorp background,
3875 void *buffer, png_int_32 row_stride, void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06003876{
John Bowler5bc90382012-01-23 22:43:22 -06003877 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06003878 {
3879 png_uint_32 check;
3880
John Bowler3706d732011-11-21 10:28:06 -06003881 if (row_stride == 0)
3882 row_stride = PNG_IMAGE_ROW_STRIDE(*image);
3883
John Bowler7875d532011-11-07 22:33:49 -06003884 if (row_stride < 0)
3885 check = -row_stride;
3886
3887 else
3888 check = row_stride;
3889
John Bowler5bc90382012-01-23 22:43:22 -06003890 if (image->opaque != NULL && buffer != NULL &&
3891 check >= PNG_IMAGE_ROW_STRIDE(*image))
John Bowler7875d532011-11-07 22:33:49 -06003892 {
John Bowler5bc90382012-01-23 22:43:22 -06003893 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
3894 (image->colormap_entries > 0 && colormap != NULL))
3895 {
3896 int result;
3897 png_image_read_control display;
John Bowler7875d532011-11-07 22:33:49 -06003898
John Bowler5bc90382012-01-23 22:43:22 -06003899 png_memset(&display, 0, sizeof display);
3900 display.image = image;
3901 display.buffer = buffer;
3902 display.row_stride = row_stride;
3903 display.colormap = colormap;
3904 display.background = background;
3905 display.local_row = NULL;
3906
3907 /* Choose the correct 'end' routine; for the color-map case all the
3908 * setup has already been done.
3909 */
3910 if (image->format & PNG_FORMAT_FLAG_COLORMAP)
3911 result =
3912 png_safe_execute(image, png_image_read_colormap, &display) &&
3913 png_safe_execute(image, png_image_read_colormapped, &display);
3914
3915 else
3916 result =
3917 png_safe_execute(image, png_image_read_direct, &display);
3918
3919 png_image_free(image);
3920 return result;
3921 }
3922
3923 else
3924 return png_image_error(image,
3925 "png_image_finish_read[color-map]: no color-map");
John Bowler7875d532011-11-07 22:33:49 -06003926 }
3927
3928 else
3929 return png_image_error(image,
3930 "png_image_finish_read: invalid argument");
3931 }
3932
3933 return 0;
3934}
3935
3936#endif /* PNG_SIMPLIFIED_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06003937#endif /* PNG_READ_SUPPORTED */