blob: 53b9c3b9b37008ae4d0f166bd01bf3d52deba2d9 [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-Pehrson212fd572014-06-11 21:10:16 -05004 * Last changed in libpng 1.6.11 [June 5, 2014]
Glenn Randers-Pehrson95a19732013-12-31 21:10:13 -06005 * Copyright (c) 1998-2014 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
15 */
Guy Schalnat0d580581995-07-20 02:43:20 -050016
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050017#include "pngpriv.h"
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060018#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
John Bowler7875d532011-11-07 22:33:49 -060019# 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 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060051 png_ptr->mode = PNG_IS_READ_STRUCT;
John Bowlerb5d00512012-03-09 09:15:18 -060052
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060053 /* Added in libpng-1.6.0; this can be used to detect a read structure if
54 * required (it will be zero in a write structure.)
John Bowlerb5d00512012-03-09 09:15:18 -060055 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060056# 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
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060060# ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
61 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
John Bowler2bc76ff2012-03-18 22:37:25 -050062
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060063 /* In stable builds only warn if an application error can be completely
64 * handled.
John Bowler2bc76ff2012-03-18 22:37:25 -050065 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060066# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
67 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
68# endif
69# endif
John Bowleraa816c42012-03-16 07:39:49 -050070
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -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 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060075 png_set_read_fn(png_ptr, NULL, NULL);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050076 }
77
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -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-Pehrson871b1d02013-03-02 14:58:22 -060094#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
95 int keep;
96#endif
97
Glenn Randers-Pehrsonc81bb8a2009-08-15 22:02:26 -050098 png_debug(1, "in png_read_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -050099
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500100 if (png_ptr == NULL || info_ptr == NULL)
101 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500102
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600103 /* Read and check the PNG file signature. */
104 png_read_sig(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500105
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500106 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500107 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500108 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500109 png_uint_32 chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500110
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600111 /* IDAT logic needs to happen here to simplify getting the two flags
112 * right.
113 */
114 if (chunk_name == png_IDAT)
115 {
116 if (!(png_ptr->mode & PNG_HAVE_IHDR))
117 png_chunk_error(png_ptr, "Missing IHDR before IDAT");
118
119 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
120 !(png_ptr->mode & PNG_HAVE_PLTE))
121 png_chunk_error(png_ptr, "Missing PLTE before IDAT");
122
123 else if (png_ptr->mode & PNG_AFTER_IDAT)
124 png_chunk_benign_error(png_ptr, "Too many IDATs found");
125
126 png_ptr->mode |= PNG_HAVE_IDAT;
127 }
128
129 else if (png_ptr->mode & PNG_HAVE_IDAT)
130 png_ptr->mode |= PNG_AFTER_IDAT;
131
Andreas Dilger47a0c421997-05-16 02:46:07 -0500132 /* This should be a binary subdivision search or a hash for
133 * matching the chunk name rather than a linear search.
134 */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500135 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600136 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500137
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500138 else if (chunk_name == png_IEND)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600139 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500140
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600141#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600142 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600143 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600144 png_handle_unknown(png_ptr, info_ptr, length, keep);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500145
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500146 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600147 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500148
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500149 else if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600150 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600151 png_ptr->idat_size = 0; /* It has been consumed */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600152 break;
153 }
154 }
155#endif
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500156 else if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600157 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500158
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500159 else if (chunk_name == png_IDAT)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500160 {
161 png_ptr->idat_size = length;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500162 break;
163 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500164
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500165#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500166 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 png_handle_bKGD(png_ptr, info_ptr, length);
168#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500169
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500170#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500171 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500172 png_handle_cHRM(png_ptr, info_ptr, length);
173#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500174
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500175#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500176 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500177 png_handle_gAMA(png_ptr, info_ptr, length);
178#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500179
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500180#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500181 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500182 png_handle_hIST(png_ptr, info_ptr, length);
183#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500184
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500185#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500186 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187 png_handle_oFFs(png_ptr, info_ptr, length);
188#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500189
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500190#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500191 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500192 png_handle_pCAL(png_ptr, info_ptr, length);
193#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500194
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500195#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500196 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600197 png_handle_sCAL(png_ptr, info_ptr, length);
198#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500199
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500200#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500201 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500202 png_handle_pHYs(png_ptr, info_ptr, length);
203#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500204
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500205#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500206 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500207 png_handle_sBIT(png_ptr, info_ptr, length);
208#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500209
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500210#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500211 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600212 png_handle_sRGB(png_ptr, info_ptr, length);
213#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500214
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500215#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500216 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600217 png_handle_iCCP(png_ptr, info_ptr, length);
218#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500219
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500220#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500221 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600222 png_handle_sPLT(png_ptr, info_ptr, length);
223#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500224
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500225#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500226 else if (chunk_name == png_tEXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500227 png_handle_tEXt(png_ptr, info_ptr, length);
228#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500229
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500230#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500231 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500232 png_handle_tIME(png_ptr, info_ptr, length);
233#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500234
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500235#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500236 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500237 png_handle_tRNS(png_ptr, info_ptr, length);
238#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500239
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500240#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500241 else if (chunk_name == png_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500242 png_handle_zTXt(png_ptr, info_ptr, length);
243#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500244
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500245#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500246 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600247 png_handle_iTXt(png_ptr, info_ptr, length);
248#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500249
Guy Schalnat0d580581995-07-20 02:43:20 -0500250 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600251 png_handle_unknown(png_ptr, info_ptr, length,
252 PNG_HANDLE_CHUNK_AS_DEFAULT);
Guy Schalnat0d580581995-07-20 02:43:20 -0500253 }
254}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500255#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500256
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500257/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500258void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600259png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500260{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500261 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500262
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600263 if (png_ptr != NULL)
264 {
265 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
266 {
267 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500268
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600269# ifdef PNG_READ_TRANSFORMS_SUPPORTED
270 png_read_transform_info(png_ptr, info_ptr);
271# else
272 PNG_UNUSED(info_ptr)
273# endif
274 }
Glenn Randers-Pehrson4cfdb3c2009-11-26 11:49:37 -0600275
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600276 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
277 else
278 png_app_error(png_ptr,
279 "png_read_update_info/png_start_read_image: duplicate call");
280 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500281}
282
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500283#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600284/* Initialize palette, background, etc, after transformations
285 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500286 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600287 * If the user doesn't call this, we will do it ourselves.
288 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500289void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600290png_start_read_image(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500291{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500292 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500293
Glenn Randers-Pehrson3dbfd302011-10-13 17:24:36 -0500294 if (png_ptr != NULL)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600295 {
296 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
297 png_read_start_row(png_ptr);
298
299 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
300 else
301 png_app_error(png_ptr,
302 "png_start_read_image/png_read_update_info: duplicate call");
303 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500304}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500305#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500306
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500307#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
John Bowlerc10930a2013-12-19 15:24:06 -0600308#ifdef PNG_MNG_FEATURES_SUPPORTED
309/* Undoes intrapixel differencing,
310 * NOTE: this is apparently only supported in the 'sequential' reader.
311 */
312static void
313png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
314{
315 png_debug(1, "in png_do_read_intrapixel");
316
317 if (
318 (row_info->color_type & PNG_COLOR_MASK_COLOR))
319 {
320 int bytes_per_pixel;
321 png_uint_32 row_width = row_info->width;
322
323 if (row_info->bit_depth == 8)
324 {
325 png_bytep rp;
326 png_uint_32 i;
327
328 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
329 bytes_per_pixel = 3;
330
331 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
332 bytes_per_pixel = 4;
333
334 else
335 return;
336
337 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
338 {
339 *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
340 *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
341 }
342 }
343 else if (row_info->bit_depth == 16)
344 {
345 png_bytep rp;
346 png_uint_32 i;
347
348 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
349 bytes_per_pixel = 6;
350
351 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
352 bytes_per_pixel = 8;
353
354 else
355 return;
356
357 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
358 {
359 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
360 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
361 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
362 png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
363 png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
364 *(rp ) = (png_byte)((red >> 8) & 0xff);
365 *(rp + 1) = (png_byte)(red & 0xff);
366 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
367 *(rp + 5) = (png_byte)(blue & 0xff);
368 }
369 }
370 }
371}
372#endif /* PNG_MNG_FEATURES_SUPPORTED */
373
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500374void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600375png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500376{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500377 png_row_info row_info;
378
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500379 if (png_ptr == NULL)
380 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500381
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500382 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -0500383 (unsigned long)png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500384
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500385 /* png_read_start_row sets the information (in particular iwidth) for this
386 * interlace pass.
387 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500388 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500389 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500390
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500391 /* 1.5.6: row_info moved out of png_struct to a local here. */
392 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
393 row_info.color_type = png_ptr->color_type;
394 row_info.bit_depth = png_ptr->bit_depth;
395 row_info.channels = png_ptr->channels;
396 row_info.pixel_depth = png_ptr->pixel_depth;
397 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
398
Glenn Randers-Pehrsona11cd842014-10-05 13:45:38 -0500399#ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500400 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
401 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500402 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500403#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
404 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500405 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500406#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500407
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500408#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
409 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500410 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500411#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500412
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600413#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
414 !defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500415 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500416 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500417#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500418
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500419#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
420 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500421 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500422#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500423
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500424#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
425 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500426 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500427#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500428
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500429#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
430 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500431 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500432#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500433
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500434#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
435 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500436 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500437#endif
438 }
Glenn Randers-Pehrsona11cd842014-10-05 13:45:38 -0500439#endif /* PNG_WARNINGS_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500440
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500441#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500442 /* If interlaced and we do not need a new row, combine row and return.
443 * Notice that the pixels we have from previous rows have been transformed
444 * already; we can only combine like with like (transformed or
445 * untransformed) and, because of the libpng API for interlaced images, this
446 * means we must transform before de-interlacing.
447 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500448 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
449 {
450 switch (png_ptr->pass)
451 {
452 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600453 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500454 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500455 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500456 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600457 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500458 return;
459 }
460 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500461
Guy Schalnat0d580581995-07-20 02:43:20 -0500462 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600463 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500464 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500465 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500466 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500467
Guy Schalnat0d580581995-07-20 02:43:20 -0500468 png_read_finish_row(png_ptr);
469 return;
470 }
471 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500472
Guy Schalnat0d580581995-07-20 02:43:20 -0500473 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600474 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500475 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500476 if (dsp_row != NULL && (png_ptr->row_number & 4))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500477 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500478
Guy Schalnat0d580581995-07-20 02:43:20 -0500479 png_read_finish_row(png_ptr);
480 return;
481 }
482 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500483
Guy Schalnat0d580581995-07-20 02:43:20 -0500484 case 3:
485 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
486 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500487 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500488 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500489
Guy Schalnat0d580581995-07-20 02:43:20 -0500490 png_read_finish_row(png_ptr);
491 return;
492 }
493 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500494
Guy Schalnat0d580581995-07-20 02:43:20 -0500495 case 4:
496 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600497 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500498 if (dsp_row != NULL && (png_ptr->row_number & 2))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500499 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500500
Guy Schalnat0d580581995-07-20 02:43:20 -0500501 png_read_finish_row(png_ptr);
502 return;
503 }
504 break;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600505
Guy Schalnat0d580581995-07-20 02:43:20 -0500506 case 5:
507 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
508 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500509 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500510 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500511
Guy Schalnat0d580581995-07-20 02:43:20 -0500512 png_read_finish_row(png_ptr);
513 return;
514 }
515 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500516
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600517 default:
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600518 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500519 if (!(png_ptr->row_number & 1))
520 {
521 png_read_finish_row(png_ptr);
522 return;
523 }
524 break;
525 }
526 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500527#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500528
Guy Schalnate5a37791996-06-05 15:50:50 -0500529 if (!(png_ptr->mode & PNG_HAVE_IDAT))
530 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500531
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600532 /* Fill the row with IDAT data: */
533 png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500534
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500535 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
536 {
537 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
Mans Rullgardd3a94802011-11-03 00:47:55 -0500538 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500539 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
540 else
541 png_error(png_ptr, "bad adaptive filter value");
542 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500543
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500544 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
545 * 1.5.6, while the buffer really is this big in current versions of libpng
546 * it may not be in the future, so this was changed just to copy the
547 * interlaced count:
548 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600549 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600550
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500551#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500552 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600553 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600554 {
555 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500556 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600557 }
558#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500559
John Bowler4a12f4a2011-04-17 18:34:22 -0500560#ifdef PNG_READ_TRANSFORMS_SUPPORTED
John Bowler9b872f42011-02-12 09:00:16 -0600561 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500562 png_do_read_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500563#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500564
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500565 /* The transformed pixel depth should match the depth now in row_info. */
566 if (png_ptr->transformed_pixel_depth == 0)
567 {
568 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
569 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
570 png_error(png_ptr, "sequential row overflow");
571 }
572
573 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
574 png_error(png_ptr, "internal sequential row size calculation error");
575
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500576#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonc4b37182014-04-06 08:59:57 -0500577 /* Expand interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500578 if (png_ptr->interlaced &&
579 (png_ptr->transformations & PNG_INTERLACE))
580 {
581 if (png_ptr->pass < 6)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500582 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
583 png_ptr->transformations);
Guy Schalnat0d580581995-07-20 02:43:20 -0500584
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500586 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500587
Andreas Dilger47a0c421997-05-16 02:46:07 -0500588 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500589 png_combine_row(png_ptr, row, 0/*row*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500590 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500591
Guy Schalnat0d580581995-07-20 02:43:20 -0500592 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500593#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500594 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500595 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500596 png_combine_row(png_ptr, row, -1/*ignored*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500597
Andreas Dilger47a0c421997-05-16 02:46:07 -0500598 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500599 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500600 }
601 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600602
603 if (png_ptr->read_row_fn != NULL)
604 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600605
Guy Schalnat0d580581995-07-20 02:43:20 -0500606}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500607#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500608
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500609#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500610/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600611 * and png_set_interlace_handling() has been called, the rows need to
612 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500613 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600614 * called, the rows contents must be initialized to the contents of the
615 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600616 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600617 * "row" holds the actual image, and pixels are placed in it
618 * as they arrive. If the image is displayed after each pass, it will
619 * appear to "sparkle" in. "display_row" can be used to display a
620 * "chunky" progressive image, with finer detail added as it becomes
621 * available. If you do not want this "chunky" display, you may pass
622 * NULL for display_row. If you do not want the sparkle display, and
623 * you have not called png_handle_alpha(), you may pass NULL for rows.
624 * If you have called png_handle_alpha(), and the image has either an
625 * alpha channel or a transparency chunk, you must provide a buffer for
626 * rows. In this case, you do not have to provide a display_row buffer
627 * also, but you may. If the image is not interlaced, or if you have
628 * not called png_set_interlace_handling(), the display_row buffer will
629 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500630 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600631 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600632 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600633
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500634void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600635png_read_rows(png_structrp png_ptr, png_bytepp row,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600636 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500637{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600638 png_uint_32 i;
639 png_bytepp rp;
640 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500641
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500642 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500643
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500644 if (png_ptr == NULL)
645 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500646
Guy Schalnat0f716451995-11-28 11:22:13 -0600647 rp = row;
648 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500649 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500650 for (i = 0; i < num_rows; i++)
651 {
652 png_bytep rptr = *rp++;
653 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600654
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500655 png_read_row(png_ptr, rptr, dptr);
656 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500657
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500658 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500659 for (i = 0; i < num_rows; i++)
660 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500661 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500662 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500663 rp++;
664 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500665
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500666 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500667 for (i = 0; i < num_rows; i++)
668 {
669 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500670 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500671 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500672 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500673}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500674#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500675
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500676#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500677/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500678 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600679 * initialize the image to the current image that PNG will be overlaying.
680 * We set the num_rows again here, in case it was incorrectly set in
681 * png_read_start_row() by a call to png_read_update_info() or
682 * png_start_read_image() if png_set_interlace_handling() wasn't called
683 * prior to either of these functions like it should have been. You can
684 * only call this function once. If you desire to have an image for
685 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500686 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600687 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600688 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500689void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600690png_read_image(png_structrp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500691{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500692 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500693 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600694 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500695
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500696 png_debug(1, "in png_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500697
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500698 if (png_ptr == NULL)
699 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500700
701#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500702 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
703 {
704 pass = png_set_interlace_handling(png_ptr);
705 /* And make sure transforms are initialized. */
706 png_start_read_image(png_ptr);
707 }
708 else
709 {
Glenn Randers-Pehrsone15a96b2011-01-14 15:47:37 -0600710 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500711 {
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500712 /* Caller called png_start_read_image or png_read_update_info without
713 * first turning on the PNG_INTERLACE transform. We can fix this here,
714 * but the caller should do it!
715 */
716 png_warning(png_ptr, "Interlace handling should be turned on when "
717 "using png_read_image");
718 /* Make sure this is set correctly */
719 png_ptr->num_rows = png_ptr->height;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500720 }
721
722 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
723 * the above error case.
724 */
725 pass = png_set_interlace_handling(png_ptr);
726 }
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500727#else
728 if (png_ptr->interlaced)
729 png_error(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600730 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500731
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500732 pass = 1;
733#endif
734
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500735 image_height=png_ptr->height;
Guy Schalnate5a37791996-06-05 15:50:50 -0500736
Guy Schalnat0d580581995-07-20 02:43:20 -0500737 for (j = 0; j < pass; j++)
738 {
739 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500740 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500741 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500742 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500743 rp++;
744 }
745 }
746}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500747#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500748
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500749#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500750/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600751 * file, will verify the end is accurate, and will read any comments
752 * or time information at the end of the file, if info is not NULL.
753 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500754void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600755png_read_end(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500756{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600757#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
758 int keep;
759#endif
760
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500761 png_debug(1, "in png_read_end");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500762
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500763 if (png_ptr == NULL)
764 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500765
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600766 /* If png_read_end is called in the middle of reading the rows there may
767 * still be pending IDAT data and an owned zstream. Deal with this here.
768 */
769#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
770 if (!png_chunk_unknown_handling(png_ptr, png_IDAT))
771#endif
772 png_read_finish_IDAT(png_ptr);
773
774#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
775 /* Report invalid palette index; added at libng-1.5.10 */
776 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
777 png_ptr->num_palette_max > png_ptr->num_palette)
778 png_benign_error(png_ptr, "Read palette index exceeding num_palette");
779#endif
John Bowler6f237b62012-03-02 13:13:15 -0600780
Guy Schalnat0d580581995-07-20 02:43:20 -0500781 do
782 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500783 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500784 png_uint_32 chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500785
Glenn Randers-Pehrson9c5a1ba2014-02-17 09:12:52 -0600786 if (chunk_name == png_IEND)
787 png_handle_IEND(png_ptr, info_ptr, length);
788
789 else if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600790 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500791
Glenn Randers-Pehrson9c5a1ba2014-02-17 09:12:52 -0600792 else if (info_ptr == NULL)
793 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500794
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600795#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600796 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600797 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500798 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600799 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500800 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500801 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600802 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600803 png_handle_unknown(png_ptr, info_ptr, length, keep);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500804 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600805 png_ptr->mode |= PNG_HAVE_PLTE;
806 }
807#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500808
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500809 else if (chunk_name == png_IDAT)
Guy Schalnat0d580581995-07-20 02:43:20 -0500810 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500811 /* Zero length IDATs are legal after the last IDAT has been
812 * read, but not after other chunks have been read.
813 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500814 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500815 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500816
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500817 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500818 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500819 else if (chunk_name == png_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500820 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500821
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500822#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500823 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500824 png_handle_bKGD(png_ptr, info_ptr, length);
825#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500826
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500827#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500828 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500829 png_handle_cHRM(png_ptr, info_ptr, length);
830#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500831
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500832#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500833 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500834 png_handle_gAMA(png_ptr, info_ptr, length);
835#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500836
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500837#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500838 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500839 png_handle_hIST(png_ptr, info_ptr, length);
840#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500841
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500842#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500843 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500844 png_handle_oFFs(png_ptr, info_ptr, length);
845#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500846
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500847#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500848 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500849 png_handle_pCAL(png_ptr, info_ptr, length);
850#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500851
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500852#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500853 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600854 png_handle_sCAL(png_ptr, info_ptr, length);
855#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500856
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500857#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500858 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500859 png_handle_pHYs(png_ptr, info_ptr, length);
860#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500861
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500862#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500863 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500864 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500865#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500866
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500867#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500868 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600869 png_handle_sRGB(png_ptr, info_ptr, length);
870#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500871
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500872#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500873 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600874 png_handle_iCCP(png_ptr, info_ptr, length);
875#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500876
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500877#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500878 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600879 png_handle_sPLT(png_ptr, info_ptr, length);
880#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500881
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500882#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500883 else if (chunk_name == png_tEXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600884 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500885#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500886
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500887#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500888 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500889 png_handle_tIME(png_ptr, info_ptr, length);
890#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500891
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500892#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500893 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500894 png_handle_tRNS(png_ptr, info_ptr, length);
895#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500896
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500897#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500898 else if (chunk_name == png_zTXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600899 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500900#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500901
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500902#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500903 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600904 png_handle_iTXt(png_ptr, info_ptr, length);
905#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500906
Guy Schalnat0d580581995-07-20 02:43:20 -0500907 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600908 png_handle_unknown(png_ptr, info_ptr, length,
909 PNG_HANDLE_CHUNK_AS_DEFAULT);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600910 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500911}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500912#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500913
John Bowler1d7f56a2011-12-21 20:14:23 -0600914/* Free all memory used in the read struct */
915static void
John Bowler5d567862011-12-24 09:12:00 -0600916png_read_destroy(png_structrp png_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500917{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500918 png_debug(1, "in png_read_destroy");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500919
John Bowler07772cb2011-10-14 18:19:47 -0500920#ifdef PNG_READ_GAMMA_SUPPORTED
921 png_destroy_gamma_table(png_ptr);
922#endif
923
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -0500924 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgard1c422762011-10-17 16:52:19 -0500925 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600926 png_free(png_ptr, png_ptr->read_buffer);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500927
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500928#ifdef PNG_READ_QUANTIZE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600929 png_free(png_ptr, png_ptr->palette_lookup);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500930 png_free(png_ptr, png_ptr->quantize_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500931#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500932
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600933 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600934 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600935 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500936
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600937#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500938 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600939 if (png_ptr->free_me & PNG_FREE_TRNS)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500940 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600941 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500942#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500943
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600944 inflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500945
Guy Schalnat6d764711995-12-19 03:22:19 -0600946#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600947 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -0600948#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500949
Glenn Randers-Pehrsona243ec02014-08-02 18:06:34 -0500950#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600951 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
John Bowler40b26032011-12-22 08:09:15 -0600952 png_free(png_ptr, png_ptr->unknown_chunk.data);
953#endif
954
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600955#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
John Bowler40b26032011-12-22 08:09:15 -0600956 png_free(png_ptr, png_ptr->chunk_list);
957#endif
958
John Bowler1d7f56a2011-12-21 20:14:23 -0600959 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
960 * callbacks are still set at this point. They are required to complete the
961 * destruction of the png_struct itself.
Guy Schalnate5a37791996-06-05 15:50:50 -0500962 */
John Bowler1d7f56a2011-12-21 20:14:23 -0600963}
Guy Schalnate5a37791996-06-05 15:50:50 -0500964
John Bowler1d7f56a2011-12-21 20:14:23 -0600965/* Free all memory used by the read */
966void PNGAPI
967png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
968 png_infopp end_info_ptr_ptr)
969{
John Bowler5d567862011-12-24 09:12:00 -0600970 png_structrp png_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500971
John Bowler1d7f56a2011-12-21 20:14:23 -0600972 png_debug(1, "in png_destroy_read_struct");
Guy Schalnate5a37791996-06-05 15:50:50 -0500973
John Bowler1d7f56a2011-12-21 20:14:23 -0600974 if (png_ptr_ptr != NULL)
975 png_ptr = *png_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500976
John Bowler1d7f56a2011-12-21 20:14:23 -0600977 if (png_ptr == NULL)
978 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600979
John Bowler1d7f56a2011-12-21 20:14:23 -0600980 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
981 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
982 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
983 */
984 png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
985 png_destroy_info_struct(png_ptr, info_ptr_ptr);
986
987 *png_ptr_ptr = NULL;
988 png_read_destroy(png_ptr);
989 png_destroy_png_struct(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500990}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600991
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500992void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600993png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600994{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500995 if (png_ptr == NULL)
996 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500997
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600998 png_ptr->read_row_fn = read_row_fn;
999}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001000
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001001
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001002#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001003#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001004void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001005png_read_png(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001006 int transforms,
1007 voidp params)
1008{
John Bowler6a6d79f2011-02-12 08:56:40 -06001009 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001010 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001011
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001012 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001013 * PNG file before the first IDAT (image data chunk).
1014 */
1015 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001016 if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001017 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001018
1019 /* -------------- image transformations start here ------------------- */
John Bowler414d7b52014-02-06 11:32:57 -06001020 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
1021 * is not implemented. This will only happen in de-configured (non-default)
1022 * libpng builds. The results can be unexpected - png_read_png may return
1023 * short or mal-formed rows because the transform is skipped.
1024 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001025
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -05001026 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001027 */
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -05001028 if (transforms & PNG_TRANSFORM_SCALE_16)
Glenn Randers-Pehrson6da2f2d2011-06-17 23:07:16 -05001029 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
1030 * did in earlier versions, while "scale_16" is now more accurate.
1031 */
John Bowler414d7b52014-02-06 11:32:57 -06001032#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001033 png_set_scale_16(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001034#else
1035 png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001036#endif
1037
John Bowler8d261262011-06-18 13:37:11 -05001038 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
1039 * latter by doing SCALE first. This is ok and allows apps not to check for
1040 * which is supported to get the right answer.
1041 */
1042 if (transforms & PNG_TRANSFORM_STRIP_16)
John Bowler414d7b52014-02-06 11:32:57 -06001043#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
John Bowler8d261262011-06-18 13:37:11 -05001044 png_set_strip_16(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001045#else
1046 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001047#endif
1048
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001049 /* Strip alpha bytes from the input data without combining with
1050 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001051 */
1052 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
John Bowler414d7b52014-02-06 11:32:57 -06001053#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001054 png_set_strip_alpha(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001055#else
1056 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001057#endif
1058
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001059 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001060 * byte into separate bytes (useful for paletted and grayscale images).
1061 */
1062 if (transforms & PNG_TRANSFORM_PACKING)
John Bowler414d7b52014-02-06 11:32:57 -06001063#ifdef PNG_READ_PACK_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001064 png_set_packing(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001065#else
1066 png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001067#endif
1068
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001069 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001070 * (not useful if you are using png_set_packing).
1071 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001072 if (transforms & PNG_TRANSFORM_PACKSWAP)
John Bowler414d7b52014-02-06 11:32:57 -06001073#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001074 png_set_packswap(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001075#else
1076 png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001077#endif
1078
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001079 /* Expand paletted colors into true RGB triplets
1080 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1081 * Expand paletted or RGB images with transparency to full alpha
1082 * channels so the data will be available as RGBA quartets.
1083 */
1084 if (transforms & PNG_TRANSFORM_EXPAND)
John Bowler414d7b52014-02-06 11:32:57 -06001085#ifdef PNG_READ_EXPAND_SUPPORTED
1086 png_set_expand(png_ptr);
1087#else
1088 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001089#endif
1090
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001091 /* We don't handle background color or gamma transformation or quantizing.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001092 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001093
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001094 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001095 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001096 if (transforms & PNG_TRANSFORM_INVERT_MONO)
John Bowler414d7b52014-02-06 11:32:57 -06001097#ifdef PNG_READ_INVERT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001098 png_set_invert_mono(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001099#else
1100 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001101#endif
1102
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001103 /* If you want to shift the pixel values from the range [0,255] or
1104 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1105 * colors were originally in:
1106 */
John Bowler414d7b52014-02-06 11:32:57 -06001107 if (transforms & PNG_TRANSFORM_SHIFT)
1108#ifdef PNG_READ_SHIFT_SUPPORTED
1109 if (info_ptr->valid & PNG_INFO_sBIT)
1110 png_set_shift(png_ptr, &info_ptr->sig_bit);
1111#else
1112 png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001113#endif
1114
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001115 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001116 if (transforms & PNG_TRANSFORM_BGR)
John Bowler414d7b52014-02-06 11:32:57 -06001117#ifdef PNG_READ_BGR_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001118 png_set_bgr(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001119#else
1120 png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001121#endif
1122
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001123 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001124 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
John Bowler414d7b52014-02-06 11:32:57 -06001125#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001126 png_set_swap_alpha(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001127#else
1128 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001129#endif
1130
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -05001131 /* Swap bytes of 16-bit files to least significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001132 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
John Bowler414d7b52014-02-06 11:32:57 -06001133#ifdef PNG_READ_SWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001134 png_set_swap(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001135#else
1136 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001137#endif
1138
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001139/* Added at libpng-1.2.41 */
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001140 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001141 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
John Bowler414d7b52014-02-06 11:32:57 -06001142#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001143 png_set_invert_alpha(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001144#else
1145 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001146#endif
1147
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001148/* Added at libpng-1.2.41 */
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001149 /* Expand grayscale image to RGB */
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001150 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
John Bowler414d7b52014-02-06 11:32:57 -06001151#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001152 png_set_gray_to_rgb(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001153#else
1154 png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001155#endif
1156
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001157/* Added at libpng-1.5.4 */
John Bowler96cec0e2011-05-08 22:48:12 -05001158 if (transforms & PNG_TRANSFORM_EXPAND_16)
John Bowler414d7b52014-02-06 11:32:57 -06001159#ifdef PNG_READ_EXPAND_16_SUPPORTED
John Bowler96cec0e2011-05-08 22:48:12 -05001160 png_set_expand_16(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001161#else
1162 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
John Bowler96cec0e2011-05-08 22:48:12 -05001163#endif
1164
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001165 /* We don't handle adding filler bytes */
1166
John Bowler6a6d79f2011-02-12 08:56:40 -06001167 /* We use png_read_image and rely on that for interlace handling, but we also
1168 * call png_read_update_info therefore must turn on interlace handling now:
1169 */
1170 (void)png_set_interlace_handling(png_ptr);
1171
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001172 /* Optional call to gamma correct and add the background to the palette
1173 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001174 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001175 */
1176 png_read_update_info(png_ptr, info_ptr);
1177
1178 /* -------------- image transformations end here ------------------- */
1179
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001180 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001181 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001182 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001183 png_uint_32 iptr;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001184
John Bowler414d7b52014-02-06 11:32:57 -06001185 info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
1186 info_ptr->height * (sizeof (png_bytep))));
1187
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001188 for (iptr=0; iptr<info_ptr->height; iptr++)
1189 info_ptr->row_pointers[iptr] = NULL;
1190
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001191 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001192
John Bowler414d7b52014-02-06 11:32:57 -06001193 for (iptr = 0; iptr < info_ptr->height; iptr++)
1194 info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
1195 png_malloc(png_ptr, info_ptr->rowbytes));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001196 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001197
1198 png_read_image(png_ptr, info_ptr->row_pointers);
1199 info_ptr->valid |= PNG_INFO_IDAT;
1200
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001201 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001202 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001203
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001204 PNG_UNUSED(params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001205}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001206#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001207#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
John Bowler7875d532011-11-07 22:33:49 -06001208
1209#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1210/* SIMPLIFIED READ
1211 *
1212 * This code currently relies on the sequential reader, though it could easily
1213 * be made to work with the progressive one.
1214 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001215/* Arguments to png_image_finish_read: */
1216
1217/* Encoding of PNG data (used by the color-map code) */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001218# define P_NOTSET 0 /* File encoding not yet known */
1219# define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
1220# define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1221# define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1222# define P_LINEAR8 4 /* 8-bit linear: only from a file value */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001223
1224/* Color-map processing: after libpng has run on the PNG image further
Glenn Randers-Pehrsona243ec02014-08-02 18:06:34 -05001225 * processing may be needed to convert the data to color-map indices.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001226 */
1227#define PNG_CMAP_NONE 0
1228#define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1229#define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1230#define PNG_CMAP_RGB 3 /* Process RGB data */
1231#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1232
1233/* The following document where the background is for each processing case. */
1234#define PNG_CMAP_NONE_BACKGROUND 256
1235#define PNG_CMAP_GA_BACKGROUND 231
1236#define PNG_CMAP_TRANS_BACKGROUND 254
1237#define PNG_CMAP_RGB_BACKGROUND 256
1238#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1239
1240typedef struct
1241{
1242 /* Arguments: */
1243 png_imagep image;
1244 png_voidp buffer;
1245 png_int_32 row_stride;
1246 png_voidp colormap;
1247 png_const_colorp background;
1248 /* Local variables: */
1249 png_voidp local_row;
1250 png_voidp first_row;
1251 ptrdiff_t row_bytes; /* step between rows */
1252 int file_encoding; /* E_ values above */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001253 png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001254 int colormap_processing; /* PNG_CMAP_ values above */
1255} png_image_read_control;
1256
John Bowler7875d532011-11-07 22:33:49 -06001257/* Do all the *safe* initialization - 'safe' means that png_error won't be
John Bowler18c5cfa2011-11-16 14:26:34 -06001258 * called, so setting up the jmp_buf is not required. This means that anything
1259 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1260 * instead so that control is returned safely back to this routine.
John Bowler7875d532011-11-07 22:33:49 -06001261 */
1262static int
1263png_image_read_init(png_imagep image)
1264{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001265 if (image->opaque == NULL)
1266 {
1267 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
John Bowler7875d532011-11-07 22:33:49 -06001268 png_safe_error, png_safe_warning);
1269
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001270 /* And set the rest of the structure to NULL to ensure that the various
1271 * fields are consistent.
1272 */
1273 memset(image, 0, (sizeof *image));
1274 image->version = PNG_IMAGE_VERSION;
John Bowler7875d532011-11-07 22:33:49 -06001275
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001276 if (png_ptr != NULL)
John Bowler7875d532011-11-07 22:33:49 -06001277 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001278 png_infop info_ptr = png_create_info_struct(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001279
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001280 if (info_ptr != NULL)
John Bowler7875d532011-11-07 22:33:49 -06001281 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001282 png_controlp control = png_voidcast(png_controlp,
1283 png_malloc_warn(png_ptr, (sizeof *control)));
John Bowler7875d532011-11-07 22:33:49 -06001284
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001285 if (control != NULL)
1286 {
1287 memset(control, 0, (sizeof *control));
John Bowler7875d532011-11-07 22:33:49 -06001288
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001289 control->png_ptr = png_ptr;
1290 control->info_ptr = info_ptr;
1291 control->for_write = 0;
1292
1293 image->opaque = control;
1294 return 1;
1295 }
1296
1297 /* Error clean up */
1298 png_destroy_info_struct(png_ptr, &info_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001299 }
1300
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001301 png_destroy_read_struct(&png_ptr, NULL, NULL);
John Bowler7875d532011-11-07 22:33:49 -06001302 }
1303
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001304 return png_image_error(image, "png_image_read: out of memory");
John Bowler7875d532011-11-07 22:33:49 -06001305 }
1306
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001307 return png_image_error(image, "png_image_read: opaque pointer not NULL");
John Bowler7875d532011-11-07 22:33:49 -06001308}
1309
1310/* Utility to find the base format of a PNG file from a png_struct. */
1311static png_uint_32
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001312png_image_format(png_structrp png_ptr)
John Bowler7875d532011-11-07 22:33:49 -06001313{
1314 png_uint_32 format = 0;
1315
1316 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1317 format |= PNG_FORMAT_FLAG_COLOR;
1318
1319 if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1320 format |= PNG_FORMAT_FLAG_ALPHA;
1321
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001322 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1323 * sets the png_struct fields; that's all we are interested in here. The
1324 * precise interaction with an app call to png_set_tRNS and PNG file reading
1325 * is unclear.
1326 */
1327 else if (png_ptr->num_trans > 0)
John Bowler7875d532011-11-07 22:33:49 -06001328 format |= PNG_FORMAT_FLAG_ALPHA;
1329
1330 if (png_ptr->bit_depth == 16)
1331 format |= PNG_FORMAT_FLAG_LINEAR;
1332
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001333 if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE)
1334 format |= PNG_FORMAT_FLAG_COLORMAP;
1335
John Bowler7875d532011-11-07 22:33:49 -06001336 return format;
1337}
1338
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001339/* Is the given gamma significantly different from sRGB? The test is the same
1340 * one used in pngrtran.c when deciding whether to do gamma correction. The
1341 * arithmetic optimizes the division by using the fact that the inverse of the
1342 * file sRGB gamma is 2.2
1343 */
1344static int
1345png_gamma_not_sRGB(png_fixed_point g)
1346{
1347 if (g < PNG_FP_1)
1348 {
1349 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
1350 if (g == 0)
1351 return 0;
1352
1353 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1354 }
1355
1356 return 1;
1357}
1358
John Bowler7875d532011-11-07 22:33:49 -06001359/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1360 * header and fill in all the information. This is executed in a safe context,
1361 * unlike the init routine above.
1362 */
1363static int
1364png_image_read_header(png_voidp argument)
1365{
John Bowler4fa96a42011-11-16 16:39:16 -06001366 png_imagep image = png_voidcast(png_imagep, argument);
John Bowler5d567862011-12-24 09:12:00 -06001367 png_structrp png_ptr = image->opaque->png_ptr;
1368 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001369
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001370 png_set_benign_errors(png_ptr, 1/*warn*/);
John Bowler7875d532011-11-07 22:33:49 -06001371 png_read_info(png_ptr, info_ptr);
1372
1373 /* Do this the fast way; just read directly out of png_struct. */
1374 image->width = png_ptr->width;
1375 image->height = png_ptr->height;
1376
1377 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001378 png_uint_32 format = png_image_format(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001379
1380 image->format = format;
John Bowler04336ba2012-01-16 07:48:36 -06001381
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001382#ifdef PNG_COLORSPACE_SUPPORTED
1383 /* Does the colorspace match sRGB? If there is no color endpoint
1384 * (colorant) information assume yes, otherwise require the
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001385 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001386 * colorspace has been determined to be invalid ignore it.
1387 */
1388 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
1389 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
1390 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
1391 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1392#endif
1393 }
1394
1395 /* We need the maximum number of entries regardless of the format the
1396 * application sets here.
1397 */
1398 {
1399 png_uint_32 cmap_entries;
1400
1401 switch (png_ptr->color_type)
John Bowler5bc90382012-01-23 22:43:22 -06001402 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001403 case PNG_COLOR_TYPE_GRAY:
1404 cmap_entries = 1U << png_ptr->bit_depth;
1405 break;
John Bowler5bc90382012-01-23 22:43:22 -06001406
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001407 case PNG_COLOR_TYPE_PALETTE:
1408 cmap_entries = png_ptr->num_palette;
1409 break;
1410
1411 default:
1412 cmap_entries = 256;
1413 break;
John Bowler5bc90382012-01-23 22:43:22 -06001414 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001415
1416 if (cmap_entries > 256)
1417 cmap_entries = 256;
1418
1419 image->colormap_entries = cmap_entries;
John Bowler5bc90382012-01-23 22:43:22 -06001420 }
1421
John Bowler7875d532011-11-07 22:33:49 -06001422 return 1;
1423}
1424
1425#ifdef PNG_STDIO_SUPPORTED
1426int PNGAPI
1427png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1428{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001429 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001430 {
1431 if (file != NULL)
1432 {
1433 if (png_image_read_init(image))
1434 {
1435 /* This is slightly evil, but png_init_io doesn't do anything other
1436 * than this and we haven't changed the standard IO functions so
1437 * this saves a 'safe' function.
1438 */
1439 image->opaque->png_ptr->io_ptr = file;
1440 return png_safe_execute(image, png_image_read_header, image);
1441 }
1442 }
1443
1444 else
1445 return png_image_error(image,
1446 "png_image_begin_read_from_stdio: invalid argument");
1447 }
1448
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001449 else if (image != NULL)
1450 return png_image_error(image,
1451 "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
1452
John Bowler7875d532011-11-07 22:33:49 -06001453 return 0;
1454}
1455
1456int PNGAPI
1457png_image_begin_read_from_file(png_imagep image, const char *file_name)
1458{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001459 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001460 {
1461 if (file_name != NULL)
1462 {
1463 FILE *fp = fopen(file_name, "rb");
1464
1465 if (fp != NULL)
1466 {
1467 if (png_image_read_init(image))
1468 {
1469 image->opaque->png_ptr->io_ptr = fp;
1470 image->opaque->owned_file = 1;
1471 return png_safe_execute(image, png_image_read_header, image);
1472 }
1473
1474 /* Clean up: just the opened file. */
1475 (void)fclose(fp);
1476 }
1477
1478 else
1479 return png_image_error(image, strerror(errno));
1480 }
1481
1482 else
1483 return png_image_error(image,
1484 "png_image_begin_read_from_file: invalid argument");
1485 }
1486
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001487 else if (image != NULL)
1488 return png_image_error(image,
1489 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
1490
John Bowler7875d532011-11-07 22:33:49 -06001491 return 0;
1492}
1493#endif /* PNG_STDIO_SUPPORTED */
1494
1495static void PNGCBAPI
1496png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
1497{
1498 if (png_ptr != NULL)
1499 {
John Bowler4fa96a42011-11-16 16:39:16 -06001500 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001501 if (image != NULL)
1502 {
1503 png_controlp cp = image->opaque;
1504 if (cp != NULL)
1505 {
1506 png_const_bytep memory = cp->memory;
1507 png_size_t size = cp->size;
1508
1509 if (memory != NULL && size >= need)
1510 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001511 memcpy(out, memory, need);
John Bowler7875d532011-11-07 22:33:49 -06001512 cp->memory = memory + need;
1513 cp->size = size - need;
1514 return;
1515 }
1516
1517 png_error(png_ptr, "read beyond end of data");
1518 }
1519 }
1520
1521 png_error(png_ptr, "invalid memory read");
1522 }
1523}
1524
1525int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1526 png_const_voidp memory, png_size_t size)
1527{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001528 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001529 {
1530 if (memory != NULL && size > 0)
1531 {
1532 if (png_image_read_init(image))
1533 {
1534 /* Now set the IO functions to read from the memory buffer and
1535 * store it into io_ptr. Again do this in-place to avoid calling a
1536 * libpng function that requires error handling.
1537 */
John Bowler4fa96a42011-11-16 16:39:16 -06001538 image->opaque->memory = png_voidcast(png_const_bytep, memory);
John Bowler7875d532011-11-07 22:33:49 -06001539 image->opaque->size = size;
1540 image->opaque->png_ptr->io_ptr = image;
1541 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1542
1543 return png_safe_execute(image, png_image_read_header, image);
1544 }
1545 }
1546
1547 else
1548 return png_image_error(image,
1549 "png_image_begin_read_from_memory: invalid argument");
1550 }
1551
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001552 else if (image != NULL)
1553 return png_image_error(image,
1554 "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
1555
John Bowler7875d532011-11-07 22:33:49 -06001556 return 0;
1557}
1558
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001559/* Utility function to skip chunks that are not used by the simplified image
1560 * read functions and an appropriate macro to call it.
1561 */
1562#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1563static void
1564png_image_skip_unused_chunks(png_structrp png_ptr)
John Bowler04336ba2012-01-16 07:48:36 -06001565{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001566 /* Prepare the reader to ignore all recognized chunks whose data will not
1567 * be used, i.e., all chunks recognized by libpng except for those
1568 * involved in basic image reading:
1569 *
1570 * IHDR, PLTE, IDAT, IEND
1571 *
1572 * Or image data handling:
1573 *
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001574 * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001575 *
1576 * This provides a small performance improvement and eliminates any
1577 * potential vulnerability to security problems in the unused chunks.
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001578 *
1579 * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
1580 * too. This allows the simplified API to be compiled without iCCP support,
1581 * however if the support is there the chunk is still checked to detect
1582 * errors (which are unfortunately quite common.)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001583 */
1584 {
1585 static PNG_CONST png_byte chunks_to_process[] = {
1586 98, 75, 71, 68, '\0', /* bKGD */
1587 99, 72, 82, 77, '\0', /* cHRM */
1588 103, 65, 77, 65, '\0', /* gAMA */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001589# ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001590 105, 67, 67, 80, '\0', /* iCCP */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001591# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001592 115, 66, 73, 84, '\0', /* sBIT */
1593 115, 82, 71, 66, '\0', /* sRGB */
1594 };
John Bowler5bc90382012-01-23 22:43:22 -06001595
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001596 /* Ignore unknown chunks and all other chunks except for the
1597 * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
1598 */
1599 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
1600 NULL, -1);
1601
1602 /* But do not ignore image data handling chunks */
1603 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
1604 chunks_to_process, (sizeof chunks_to_process)/5);
1605 }
1606}
1607
1608# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1609#else
1610# define PNG_SKIP_CHUNKS(p) ((void)0)
1611#endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */
1612
1613/* The following macro gives the exact rounded answer for all values in the
1614 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1615 * the correct numbers 0..5
1616 */
1617#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1618
1619/* Utility functions to make particular color-maps */
1620static void
1621set_file_encoding(png_image_read_control *display)
1622{
1623 png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma;
1624 if (png_gamma_significant(g))
1625 {
1626 if (png_gamma_not_sRGB(g))
1627 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001628 display->file_encoding = P_FILE;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001629 display->gamma_to_linear = png_reciprocal(g);
1630 }
1631
1632 else
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001633 display->file_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001634 }
1635
1636 else
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001637 display->file_encoding = P_LINEAR8;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001638}
1639
1640static unsigned int
1641decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
1642{
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001643 if (encoding == P_FILE) /* double check */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001644 encoding = display->file_encoding;
1645
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001646 if (encoding == P_NOTSET) /* must be the file encoding */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001647 {
1648 set_file_encoding(display);
1649 encoding = display->file_encoding;
1650 }
1651
1652 switch (encoding)
1653 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001654 case P_FILE:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001655 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
1656 break;
1657
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001658 case P_sRGB:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001659 value = png_sRGB_table[value];
1660 break;
1661
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001662 case P_LINEAR:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001663 break;
1664
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001665 case P_LINEAR8:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001666 value *= 257;
1667 break;
1668
1669 default:
1670 png_error(display->image->opaque->png_ptr,
1671 "unexpected encoding (internal error)");
1672 break;
1673 }
1674
1675 return value;
1676}
1677
1678static png_uint_32
1679png_colormap_compose(png_image_read_control *display,
1680 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
1681 png_uint_32 background, int encoding)
1682{
1683 /* The file value is composed on the background, the background has the given
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001684 * encoding and so does the result, the file is encoded with P_FILE and the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001685 * file and alpha are 8-bit values. The (output) encoding will always be
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001686 * P_LINEAR or P_sRGB.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001687 */
1688 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
1689 png_uint_32 b = decode_gamma(display, background, encoding);
1690
1691 /* The alpha is always an 8-bit value (it comes from the palette), the value
1692 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1693 */
1694 f = f * alpha + b * (255-alpha);
1695
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001696 if (encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001697 {
1698 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1699 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1700 */
1701 f *= 257; /* Now scaled by 65535 */
1702 f += f >> 16;
1703 f = (f+32768) >> 16;
1704 }
1705
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001706 else /* P_sRGB */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001707 f = PNG_sRGB_FROM_LINEAR(f);
1708
1709 return f;
1710}
1711
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001712/* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001713 * be 8-bit.
1714 */
1715static void
1716png_create_colormap_entry(png_image_read_control *display,
1717 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
1718 png_uint_32 alpha, int encoding)
1719{
1720 png_imagep image = display->image;
1721 const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ?
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001722 P_LINEAR : P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001723 const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
1724 (red != green || green != blue);
1725
1726 if (ip > 255)
1727 png_error(image->opaque->png_ptr, "color-map index out of range");
1728
1729 /* Update the cache with whether the file gamma is significantly different
1730 * from sRGB.
1731 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001732 if (encoding == P_FILE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001733 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001734 if (display->file_encoding == P_NOTSET)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001735 set_file_encoding(display);
1736
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001737 /* Note that the cached value may be P_FILE too, but if it is then the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001738 * gamma_to_linear member has been set.
1739 */
1740 encoding = display->file_encoding;
1741 }
1742
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001743 if (encoding == P_FILE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001744 {
1745 png_fixed_point g = display->gamma_to_linear;
1746
1747 red = png_gamma_16bit_correct(red*257, g);
1748 green = png_gamma_16bit_correct(green*257, g);
1749 blue = png_gamma_16bit_correct(blue*257, g);
1750
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001751 if (convert_to_Y || output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001752 {
1753 alpha *= 257;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001754 encoding = P_LINEAR;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001755 }
1756
1757 else
1758 {
1759 red = PNG_sRGB_FROM_LINEAR(red * 255);
1760 green = PNG_sRGB_FROM_LINEAR(green * 255);
1761 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001762 encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001763 }
1764 }
1765
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001766 else if (encoding == P_LINEAR8)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001767 {
1768 /* This encoding occurs quite frequently in test cases because PngSuite
1769 * includes a gAMA 1.0 chunk with most images.
1770 */
1771 red *= 257;
1772 green *= 257;
1773 blue *= 257;
1774 alpha *= 257;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001775 encoding = P_LINEAR;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001776 }
1777
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001778 else if (encoding == P_sRGB && (convert_to_Y || output_encoding == P_LINEAR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001779 {
1780 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1781 * linear.
1782 */
1783 red = png_sRGB_table[red];
1784 green = png_sRGB_table[green];
1785 blue = png_sRGB_table[blue];
1786 alpha *= 257;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001787 encoding = P_LINEAR;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001788 }
1789
1790 /* This is set if the color isn't gray but the output is. */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001791 if (encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001792 {
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001793 if (convert_to_Y != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001794 {
1795 /* NOTE: these values are copied from png_do_rgb_to_gray */
1796 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
1797 (png_uint_32)2366 * blue;
1798
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001799 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001800 y = (y + 16384) >> 15;
1801
1802 else
1803 {
1804 /* y is scaled by 32768, we need it scaled by 255: */
1805 y = (y + 128) >> 8;
1806 y *= 255;
1807 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001808 encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001809 }
1810
1811 blue = red = green = y;
1812 }
1813
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001814 else if (output_encoding == P_sRGB)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001815 {
1816 red = PNG_sRGB_FROM_LINEAR(red * 255);
1817 green = PNG_sRGB_FROM_LINEAR(green * 255);
1818 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1819 alpha = PNG_DIV257(alpha);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001820 encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001821 }
1822 }
1823
1824 if (encoding != output_encoding)
1825 png_error(image->opaque->png_ptr, "bad encoding (internal error)");
1826
1827 /* Store the value. */
1828 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001829# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001830 const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1831 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
1832# else
1833# define afirst 0
1834# endif
1835# ifdef PNG_FORMAT_BGR_SUPPORTED
1836 const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
1837# else
1838# define bgr 0
1839# endif
1840
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001841 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001842 {
1843 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
1844
1845 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1846
1847 /* The linear 16-bit values must be pre-multiplied by the alpha channel
1848 * value, if less than 65535 (this is, effectively, composite on black
1849 * if the alpha channel is removed.)
1850 */
1851 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1852 {
1853 case 4:
1854 entry[afirst ? 0 : 3] = (png_uint_16)alpha;
1855 /* FALL THROUGH */
1856
1857 case 3:
1858 if (alpha < 65535)
1859 {
1860 if (alpha > 0)
1861 {
1862 blue = (blue * alpha + 32767U)/65535U;
1863 green = (green * alpha + 32767U)/65535U;
1864 red = (red * alpha + 32767U)/65535U;
1865 }
1866
1867 else
1868 red = green = blue = 0;
1869 }
1870 entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
1871 entry[afirst + 1] = (png_uint_16)green;
1872 entry[afirst + bgr] = (png_uint_16)red;
1873 break;
1874
1875 case 2:
1876 entry[1 ^ afirst] = (png_uint_16)alpha;
1877 /* FALL THROUGH */
1878
1879 case 1:
1880 if (alpha < 65535)
1881 {
1882 if (alpha > 0)
1883 green = (green * alpha + 32767U)/65535U;
1884
1885 else
1886 green = 0;
1887 }
1888 entry[afirst] = (png_uint_16)green;
1889 break;
1890
1891 default:
1892 break;
1893 }
1894 }
1895
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001896 else /* output encoding is P_sRGB */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001897 {
1898 png_bytep entry = png_voidcast(png_bytep, display->colormap);
1899
1900 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1901
1902 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1903 {
1904 case 4:
1905 entry[afirst ? 0 : 3] = (png_byte)alpha;
1906 case 3:
1907 entry[afirst + (2 ^ bgr)] = (png_byte)blue;
1908 entry[afirst + 1] = (png_byte)green;
1909 entry[afirst + bgr] = (png_byte)red;
1910 break;
1911
1912 case 2:
1913 entry[1 ^ afirst] = (png_byte)alpha;
1914 case 1:
1915 entry[afirst] = (png_byte)green;
1916 break;
1917
1918 default:
1919 break;
1920 }
1921 }
1922
1923# ifdef afirst
1924# undef afirst
1925# endif
1926# ifdef bgr
1927# undef bgr
1928# endif
1929 }
1930}
1931
John Bowler7875d532011-11-07 22:33:49 -06001932static int
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001933make_gray_file_colormap(png_image_read_control *display)
1934{
1935 unsigned int i;
1936
1937 for (i=0; i<256; ++i)
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001938 png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001939
1940 return i;
1941}
1942
1943static int
1944make_gray_colormap(png_image_read_control *display)
1945{
1946 unsigned int i;
1947
1948 for (i=0; i<256; ++i)
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001949 png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001950
1951 return i;
1952}
1953#define PNG_GRAY_COLORMAP_ENTRIES 256
1954
1955static int
1956make_ga_colormap(png_image_read_control *display)
1957{
1958 unsigned int i, a;
1959
1960 /* Alpha is retained, the output will be a color-map with entries
1961 * selected by six levels of alpha. One transparent entry, 6 gray
1962 * levels for all the intermediate alpha values, leaving 230 entries
1963 * for the opaque grays. The color-map entries are the six values
1964 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
1965 * relevant entry.
1966 *
1967 * if (alpha > 229) // opaque
1968 * {
1969 * // The 231 entries are selected to make the math below work:
1970 * base = 0;
1971 * entry = (231 * gray + 128) >> 8;
1972 * }
1973 * else if (alpha < 26) // transparent
1974 * {
1975 * base = 231;
1976 * entry = 0;
1977 * }
1978 * else // partially opaque
1979 * {
1980 * base = 226 + 6 * PNG_DIV51(alpha);
1981 * entry = PNG_DIV51(gray);
1982 * }
1983 */
1984 i = 0;
1985 while (i < 231)
1986 {
1987 unsigned int gray = (i * 256 + 115) / 231;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001988 png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001989 }
1990
1991 /* 255 is used here for the component values for consistency with the code
1992 * that undoes premultiplication in pngwrite.c.
1993 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001994 png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001995
1996 for (a=1; a<5; ++a)
1997 {
1998 unsigned int g;
1999
2000 for (g=0; g<6; ++g)
2001 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002002 P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002003 }
2004
2005 return i;
2006}
2007
2008#define PNG_GA_COLORMAP_ENTRIES 256
2009
2010static int
2011make_rgb_colormap(png_image_read_control *display)
2012{
2013 unsigned int i, r;
2014
2015 /* Build a 6x6x6 opaque RGB cube */
2016 for (i=r=0; r<6; ++r)
2017 {
2018 unsigned int g;
2019
2020 for (g=0; g<6; ++g)
2021 {
2022 unsigned int b;
2023
2024 for (b=0; b<6; ++b)
2025 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002026 P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002027 }
2028 }
2029
2030 return i;
2031}
2032
2033#define PNG_RGB_COLORMAP_ENTRIES 216
2034
2035/* Return a palette index to the above palette given three 8-bit sRGB values. */
2036#define PNG_RGB_INDEX(r,g,b) \
2037 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
2038
2039static int
2040png_image_read_colormap(png_voidp argument)
2041{
2042 png_image_read_control *display =
2043 png_voidcast(png_image_read_control*, argument);
2044 const png_imagep image = display->image;
2045
2046 const png_structrp png_ptr = image->opaque->png_ptr;
2047 const png_uint_32 output_format = image->format;
2048 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ?
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002049 P_LINEAR : P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002050
2051 unsigned int cmap_entries;
2052 unsigned int output_processing; /* Output processing option */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002053 unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002054
2055 /* Background information; the background color and the index of this color
2056 * in the color-map if it exists (else 256).
2057 */
2058 unsigned int background_index = 256;
2059 png_uint_32 back_r, back_g, back_b;
2060
2061 /* Flags to accumulate things that need to be done to the input. */
2062 int expand_tRNS = 0;
2063
2064 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
2065 * very difficult to do, the results look awful, and it is difficult to see
2066 * what possible use it is because the application can't control the
2067 * color-map.
2068 */
2069 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
2070 png_ptr->num_trans > 0) /* alpha in input */ &&
2071 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
2072 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002073 if (output_encoding == P_LINEAR) /* compose on black */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002074 back_b = back_g = back_r = 0;
2075
2076 else if (display->background == NULL /* no way to remove it */)
2077 png_error(png_ptr,
2078 "a background color must be supplied to remove alpha/transparency");
2079
2080 /* Get a copy of the background color (this avoids repeating the checks
2081 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
2082 * output format.
2083 */
2084 else
2085 {
2086 back_g = display->background->green;
2087 if (output_format & PNG_FORMAT_FLAG_COLOR)
2088 {
2089 back_r = display->background->red;
2090 back_b = display->background->blue;
2091 }
2092 else
2093 back_b = back_r = back_g;
2094 }
2095 }
2096
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002097 else if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002098 back_b = back_r = back_g = 65535;
2099
2100 else
2101 back_b = back_r = back_g = 255;
2102
John Bowler59ae3892013-03-06 22:15:25 -06002103 /* Default the input file gamma if required - this is necessary because
2104 * libpng assumes that if no gamma information is present the data is in the
2105 * output format, but the simplified API deduces the gamma from the input
2106 * format.
2107 */
2108 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0)
2109 {
2110 /* Do this directly, not using the png_colorspace functions, to ensure
2111 * that it happens even if the colorspace is invalid (though probably if
2112 * it is the setting will be ignored) Note that the same thing can be
2113 * achieved at the application interface with png_set_gAMA.
2114 */
2115 if (png_ptr->bit_depth == 16 &&
2116 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
2117 png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR;
2118
2119 else
2120 png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE;
2121
2122 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
2123 }
2124
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002125 /* Decide what to do based on the PNG color type of the input data. The
2126 * utility function png_create_colormap_entry deals with most aspects of the
2127 * output transformations; this code works out how to produce bytes of
2128 * color-map entries from the original format.
2129 */
2130 switch (png_ptr->color_type)
2131 {
2132 case PNG_COLOR_TYPE_GRAY:
2133 if (png_ptr->bit_depth <= 8)
2134 {
2135 /* There at most 256 colors in the output, regardless of
2136 * transparency.
2137 */
2138 unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
2139
2140 cmap_entries = 1U << png_ptr->bit_depth;
2141 if (cmap_entries > image->colormap_entries)
2142 png_error(png_ptr, "gray[8] color-map: too few entries");
2143
2144 step = 255 / (cmap_entries - 1);
2145 output_processing = PNG_CMAP_NONE;
2146
2147 /* If there is a tRNS chunk then this either selects a transparent
2148 * value or, if the output has no alpha, the background color.
2149 */
2150 if (png_ptr->num_trans > 0)
2151 {
2152 trans = png_ptr->trans_color.gray;
2153
2154 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002155 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002156 }
2157
2158 /* png_create_colormap_entry just takes an RGBA and writes the
2159 * corresponding color-map entry using the format from 'image',
2160 * including the required conversion to sRGB or linear as
2161 * appropriate. The input values are always either sRGB (if the
2162 * gamma correction flag is 0) or 0..255 scaled file encoded values
2163 * (if the function must gamma correct them).
2164 */
2165 for (i=val=0; i<cmap_entries; ++i, val += step)
2166 {
2167 /* 'i' is a file value. While this will result in duplicated
2168 * entries for 8-bit non-sRGB encoded files it is necessary to
2169 * have non-gamma corrected values to do tRNS handling.
2170 */
2171 if (i != trans)
2172 png_create_colormap_entry(display, i, val, val, val, 255,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002173 P_FILE/*8-bit with file gamma*/);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002174
2175 /* Else this entry is transparent. The colors don't matter if
2176 * there is an alpha channel (back_alpha == 0), but it does no
2177 * harm to pass them in; the values are not set above so this
2178 * passes in white.
2179 *
2180 * NOTE: this preserves the full precision of the application
2181 * supplied background color when it is used.
2182 */
2183 else
2184 png_create_colormap_entry(display, i, back_r, back_g, back_b,
2185 back_alpha, output_encoding);
2186 }
2187
2188 /* We need libpng to preserve the original encoding. */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002189 data_encoding = P_FILE;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002190
2191 /* The rows from libpng, while technically gray values, are now also
Glenn Randers-Pehrson3efbeca2014-07-21 16:57:30 -05002192 * color-map indices; however, they may need to be expanded to 1
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002193 * byte per pixel. This is what png_set_packing does (i.e., it
2194 * unpacks the bit values into bytes.)
2195 */
2196 if (png_ptr->bit_depth < 8)
2197 png_set_packing(png_ptr);
2198 }
2199
2200 else /* bit depth is 16 */
2201 {
2202 /* The 16-bit input values can be converted directly to 8-bit gamma
2203 * encoded values; however, if a tRNS chunk is present 257 color-map
2204 * entries are required. This means that the extra entry requires
2205 * special processing; add an alpha channel, sacrifice gray level
2206 * 254 and convert transparent (alpha==0) entries to that.
2207 *
2208 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2209 * same time to minimize quality loss. If a tRNS chunk is present
2210 * this means libpng must handle it too; otherwise it is impossible
2211 * to do the exact match on the 16-bit value.
2212 *
2213 * If the output has no alpha channel *and* the background color is
2214 * gray then it is possible to let libpng handle the substitution by
2215 * ensuring that the corresponding gray level matches the background
2216 * color exactly.
2217 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002218 data_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002219
2220 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2221 png_error(png_ptr, "gray[16] color-map: too few entries");
2222
2223 cmap_entries = make_gray_colormap(display);
2224
2225 if (png_ptr->num_trans > 0)
2226 {
2227 unsigned int back_alpha;
2228
2229 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2230 back_alpha = 0;
2231
2232 else
2233 {
2234 if (back_r == back_g && back_g == back_b)
2235 {
2236 /* Background is gray; no special processing will be
2237 * required.
2238 */
2239 png_color_16 c;
2240 png_uint_32 gray = back_g;
2241
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002242 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002243 {
2244 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2245
2246 /* And make sure the corresponding palette entry
2247 * matches.
2248 */
2249 png_create_colormap_entry(display, gray, back_g, back_g,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002250 back_g, 65535, P_LINEAR);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002251 }
2252
2253 /* The background passed to libpng, however, must be the
2254 * sRGB value.
2255 */
2256 c.index = 0; /*unused*/
2257 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2258
2259 /* NOTE: does this work without expanding tRNS to alpha?
2260 * It should be the color->gray case below apparently
2261 * doesn't.
2262 */
2263 png_set_background_fixed(png_ptr, &c,
2264 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2265 0/*gamma: not used*/);
2266
2267 output_processing = PNG_CMAP_NONE;
2268 break;
2269 }
2270
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002271 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002272 }
2273
2274 /* output_processing means that the libpng-processed row will be
2275 * 8-bit GA and it has to be processing to single byte color-map
2276 * values. Entry 254 is replaced by either a completely
2277 * transparent entry or by the background color at full
Glenn Randers-Pehrson3efbeca2014-07-21 16:57:30 -05002278 * precision (and the background color is not a simple gray
2279 * level in this case.)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002280 */
2281 expand_tRNS = 1;
2282 output_processing = PNG_CMAP_TRANS;
2283 background_index = 254;
2284
2285 /* And set (overwrite) color-map entry 254 to the actual
2286 * background color at full precision.
2287 */
2288 png_create_colormap_entry(display, 254, back_r, back_g, back_b,
2289 back_alpha, output_encoding);
2290 }
2291
2292 else
2293 output_processing = PNG_CMAP_NONE;
2294 }
2295 break;
2296
2297 case PNG_COLOR_TYPE_GRAY_ALPHA:
2298 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2299 * of 65536 combinations. If, however, the alpha channel is to be
2300 * removed there are only 256 possibilities if the background is gray.
2301 * (Otherwise there is a subset of the 65536 possibilities defined by
2302 * the triangle between black, white and the background color.)
2303 *
2304 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2305 * worry about tRNS matching - tRNS is ignored if there is an alpha
2306 * channel.
2307 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002308 data_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002309
2310 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2311 {
2312 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2313 png_error(png_ptr, "gray+alpha color-map: too few entries");
2314
2315 cmap_entries = make_ga_colormap(display);
2316
2317 background_index = PNG_CMAP_GA_BACKGROUND;
2318 output_processing = PNG_CMAP_GA;
2319 }
2320
2321 else /* alpha is removed */
2322 {
2323 /* Alpha must be removed as the PNG data is processed when the
2324 * background is a color because the G and A channels are
2325 * independent and the vector addition (non-parallel vectors) is a
2326 * 2-D problem.
2327 *
2328 * This can be reduced to the same algorithm as above by making a
2329 * colormap containing gray levels (for the opaque grays), a
2330 * background entry (for a transparent pixel) and a set of four six
2331 * level color values, one set for each intermediate alpha value.
2332 * See the comments in make_ga_colormap for how this works in the
2333 * per-pixel processing.
2334 *
2335 * If the background is gray, however, we only need a 256 entry gray
2336 * level color map. It is sufficient to make the entry generated
2337 * for the background color be exactly the color specified.
2338 */
2339 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2340 (back_r == back_g && back_g == back_b))
2341 {
2342 /* Background is gray; no special processing will be required. */
2343 png_color_16 c;
2344 png_uint_32 gray = back_g;
2345
2346 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2347 png_error(png_ptr, "gray-alpha color-map: too few entries");
2348
2349 cmap_entries = make_gray_colormap(display);
2350
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002351 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002352 {
2353 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2354
2355 /* And make sure the corresponding palette entry matches. */
2356 png_create_colormap_entry(display, gray, back_g, back_g,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002357 back_g, 65535, P_LINEAR);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002358 }
2359
2360 /* The background passed to libpng, however, must be the sRGB
2361 * value.
2362 */
2363 c.index = 0; /*unused*/
2364 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2365
2366 png_set_background_fixed(png_ptr, &c,
2367 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2368 0/*gamma: not used*/);
2369
2370 output_processing = PNG_CMAP_NONE;
2371 }
2372
2373 else
2374 {
2375 png_uint_32 i, a;
2376
2377 /* This is the same as png_make_ga_colormap, above, except that
2378 * the entries are all opaque.
2379 */
2380 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2381 png_error(png_ptr, "ga-alpha color-map: too few entries");
2382
2383 i = 0;
2384 while (i < 231)
2385 {
2386 png_uint_32 gray = (i * 256 + 115) / 231;
2387 png_create_colormap_entry(display, i++, gray, gray, gray,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002388 255, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002389 }
2390
2391 /* NOTE: this preserves the full precision of the application
2392 * background color.
2393 */
2394 background_index = i;
2395 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002396 output_encoding == P_LINEAR ? 65535U : 255U, output_encoding);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002397
2398 /* For non-opaque input composite on the sRGB background - this
2399 * requires inverting the encoding for each component. The input
2400 * is still converted to the sRGB encoding because this is a
2401 * reasonable approximate to the logarithmic curve of human
2402 * visual sensitivity, at least over the narrow range which PNG
2403 * represents. Consequently 'G' is always sRGB encoded, while
2404 * 'A' is linear. We need the linear background colors.
2405 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002406 if (output_encoding == P_sRGB) /* else already linear */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002407 {
2408 /* This may produce a value not exactly matching the
2409 * background, but that's ok because these numbers are only
2410 * used when alpha != 0
2411 */
2412 back_r = png_sRGB_table[back_r];
2413 back_g = png_sRGB_table[back_g];
2414 back_b = png_sRGB_table[back_b];
2415 }
2416
2417 for (a=1; a<5; ++a)
2418 {
2419 unsigned int g;
2420
2421 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2422 * by an 8-bit alpha value (0..255).
2423 */
2424 png_uint_32 alpha = 51 * a;
2425 png_uint_32 back_rx = (255-alpha) * back_r;
2426 png_uint_32 back_gx = (255-alpha) * back_g;
2427 png_uint_32 back_bx = (255-alpha) * back_b;
2428
2429 for (g=0; g<6; ++g)
2430 {
2431 png_uint_32 gray = png_sRGB_table[g*51] * alpha;
2432
2433 png_create_colormap_entry(display, i++,
2434 PNG_sRGB_FROM_LINEAR(gray + back_rx),
2435 PNG_sRGB_FROM_LINEAR(gray + back_gx),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002436 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002437 }
2438 }
2439
2440 cmap_entries = i;
2441 output_processing = PNG_CMAP_GA;
2442 }
2443 }
2444 break;
2445
2446 case PNG_COLOR_TYPE_RGB:
2447 case PNG_COLOR_TYPE_RGB_ALPHA:
2448 /* Exclude the case where the output is gray; we can always handle this
2449 * with the cases above.
2450 */
2451 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
2452 {
2453 /* The color-map will be grayscale, so we may as well convert the
2454 * input RGB values to a simple grayscale and use the grayscale
2455 * code above.
2456 *
2457 * NOTE: calling this apparently damages the recognition of the
2458 * transparent color in background color handling; call
2459 * png_set_tRNS_to_alpha before png_set_background_fixed.
2460 */
2461 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2462 -1);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002463 data_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002464
2465 /* The output will now be one or two 8-bit gray or gray+alpha
2466 * channels. The more complex case arises when the input has alpha.
2467 */
2468 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2469 png_ptr->num_trans > 0) &&
2470 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2471 {
2472 /* Both input and output have an alpha channel, so no background
2473 * processing is required; just map the GA bytes to the right
2474 * color-map entry.
2475 */
2476 expand_tRNS = 1;
2477
2478 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2479 png_error(png_ptr, "rgb[ga] color-map: too few entries");
2480
2481 cmap_entries = make_ga_colormap(display);
2482 background_index = PNG_CMAP_GA_BACKGROUND;
2483 output_processing = PNG_CMAP_GA;
2484 }
2485
2486 else
2487 {
2488 /* Either the input or the output has no alpha channel, so there
2489 * will be no non-opaque pixels in the color-map; it will just be
2490 * grayscale.
2491 */
2492 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2493 png_error(png_ptr, "rgb[gray] color-map: too few entries");
2494
2495 /* Ideally this code would use libpng to do the gamma correction,
2496 * but if an input alpha channel is to be removed we will hit the
2497 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2498 * correction bug). Fix this by dropping the gamma correction in
2499 * this case and doing it in the palette; this will result in
2500 * duplicate palette entries, but that's better than the
2501 * alternative of double gamma correction.
2502 */
2503 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2504 png_ptr->num_trans > 0) &&
2505 png_gamma_not_sRGB(png_ptr->colorspace.gamma))
2506 {
2507 cmap_entries = make_gray_file_colormap(display);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002508 data_encoding = P_FILE;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002509 }
2510
2511 else
2512 cmap_entries = make_gray_colormap(display);
2513
2514 /* But if the input has alpha or transparency it must be removed
2515 */
2516 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2517 png_ptr->num_trans > 0)
2518 {
2519 png_color_16 c;
2520 png_uint_32 gray = back_g;
2521
2522 /* We need to ensure that the application background exists in
2523 * the colormap and that completely transparent pixels map to
2524 * it. Achieve this simply by ensuring that the entry
2525 * selected for the background really is the background color.
2526 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002527 if (data_encoding == P_FILE) /* from the fixup above */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002528 {
2529 /* The app supplied a gray which is in output_encoding, we
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002530 * need to convert it to a value of the input (P_FILE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002531 * encoding then set this palette entry to the required
2532 * output encoding.
2533 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002534 if (output_encoding == P_sRGB)
2535 gray = png_sRGB_table[gray]; /* now P_LINEAR */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002536
2537 gray = PNG_DIV257(png_gamma_16bit_correct(gray,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002538 png_ptr->colorspace.gamma)); /* now P_FILE */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002539
2540 /* And make sure the corresponding palette entry contains
2541 * exactly the required sRGB value.
2542 */
2543 png_create_colormap_entry(display, gray, back_g, back_g,
2544 back_g, 0/*unused*/, output_encoding);
2545 }
2546
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002547 else if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002548 {
2549 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2550
2551 /* And make sure the corresponding palette entry matches.
2552 */
2553 png_create_colormap_entry(display, gray, back_g, back_g,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002554 back_g, 0/*unused*/, P_LINEAR);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002555 }
2556
2557 /* The background passed to libpng, however, must be the
2558 * output (normally sRGB) value.
2559 */
2560 c.index = 0; /*unused*/
2561 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2562
2563 /* NOTE: the following is apparently a bug in libpng. Without
2564 * it the transparent color recognition in
2565 * png_set_background_fixed seems to go wrong.
2566 */
2567 expand_tRNS = 1;
2568 png_set_background_fixed(png_ptr, &c,
2569 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2570 0/*gamma: not used*/);
2571 }
2572
2573 output_processing = PNG_CMAP_NONE;
2574 }
2575 }
2576
2577 else /* output is color */
2578 {
2579 /* We could use png_quantize here so long as there is no transparent
2580 * color or alpha; png_quantize ignores alpha. Easier overall just
2581 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2582 * Consequently we always want libpng to produce sRGB data.
2583 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002584 data_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002585
2586 /* Is there any transparency or alpha? */
2587 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2588 png_ptr->num_trans > 0)
2589 {
2590 /* Is there alpha in the output too? If so all four channels are
2591 * processed into a special RGB cube with alpha support.
2592 */
2593 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2594 {
2595 png_uint_32 r;
2596
2597 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2598 png_error(png_ptr, "rgb+alpha color-map: too few entries");
2599
2600 cmap_entries = make_rgb_colormap(display);
2601
2602 /* Add a transparent entry. */
2603 png_create_colormap_entry(display, cmap_entries, 255, 255,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002604 255, 0, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002605
2606 /* This is stored as the background index for the processing
2607 * algorithm.
2608 */
2609 background_index = cmap_entries++;
2610
2611 /* Add 27 r,g,b entries each with alpha 0.5. */
2612 for (r=0; r<256; r = (r << 1) | 0x7f)
2613 {
2614 png_uint_32 g;
2615
2616 for (g=0; g<256; g = (g << 1) | 0x7f)
2617 {
2618 png_uint_32 b;
2619
2620 /* This generates components with the values 0, 127 and
2621 * 255
2622 */
2623 for (b=0; b<256; b = (b << 1) | 0x7f)
2624 png_create_colormap_entry(display, cmap_entries++,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002625 r, g, b, 128, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002626 }
2627 }
2628
2629 expand_tRNS = 1;
2630 output_processing = PNG_CMAP_RGB_ALPHA;
2631 }
2632
2633 else
2634 {
2635 /* Alpha/transparency must be removed. The background must
2636 * exist in the color map (achieved by setting adding it after
2637 * the 666 color-map). If the standard processing code will
2638 * pick up this entry automatically that's all that is
2639 * required; libpng can be called to do the background
2640 * processing.
2641 */
2642 unsigned int sample_size =
2643 PNG_IMAGE_SAMPLE_SIZE(output_format);
2644 png_uint_32 r, g, b; /* sRGB background */
2645
2646 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2647 png_error(png_ptr, "rgb-alpha color-map: too few entries");
2648
2649 cmap_entries = make_rgb_colormap(display);
2650
2651 png_create_colormap_entry(display, cmap_entries, back_r,
2652 back_g, back_b, 0/*unused*/, output_encoding);
2653
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002654 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002655 {
2656 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2657 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2658 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2659 }
2660
2661 else
2662 {
2663 r = back_r;
2664 g = back_g;
2665 b = back_g;
2666 }
2667
2668 /* Compare the newly-created color-map entry with the one the
2669 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2670 * match, add the new one and set this as the background
2671 * index.
2672 */
2673 if (memcmp((png_const_bytep)display->colormap +
2674 sample_size * cmap_entries,
2675 (png_const_bytep)display->colormap +
2676 sample_size * PNG_RGB_INDEX(r,g,b),
2677 sample_size) != 0)
2678 {
2679 /* The background color must be added. */
2680 background_index = cmap_entries++;
2681
2682 /* Add 27 r,g,b entries each with created by composing with
2683 * the background at alpha 0.5.
2684 */
2685 for (r=0; r<256; r = (r << 1) | 0x7f)
2686 {
2687 for (g=0; g<256; g = (g << 1) | 0x7f)
2688 {
2689 /* This generates components with the values 0, 127
2690 * and 255
2691 */
2692 for (b=0; b<256; b = (b << 1) | 0x7f)
2693 png_create_colormap_entry(display, cmap_entries++,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002694 png_colormap_compose(display, r, P_sRGB, 128,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002695 back_r, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002696 png_colormap_compose(display, g, P_sRGB, 128,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002697 back_g, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002698 png_colormap_compose(display, b, P_sRGB, 128,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002699 back_b, output_encoding),
2700 0/*unused*/, output_encoding);
2701 }
2702 }
2703
2704 expand_tRNS = 1;
2705 output_processing = PNG_CMAP_RGB_ALPHA;
2706 }
2707
2708 else /* background color is in the standard color-map */
2709 {
2710 png_color_16 c;
2711
2712 c.index = 0; /*unused*/
2713 c.red = (png_uint_16)back_r;
2714 c.gray = c.green = (png_uint_16)back_g;
2715 c.blue = (png_uint_16)back_b;
2716
2717 png_set_background_fixed(png_ptr, &c,
2718 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2719 0/*gamma: not used*/);
2720
2721 output_processing = PNG_CMAP_RGB;
2722 }
2723 }
2724 }
2725
2726 else /* no alpha or transparency in the input */
2727 {
2728 /* Alpha in the output is irrelevant, simply map the opaque input
2729 * pixels to the 6x6x6 color-map.
2730 */
2731 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2732 png_error(png_ptr, "rgb color-map: too few entries");
2733
2734 cmap_entries = make_rgb_colormap(display);
2735 output_processing = PNG_CMAP_RGB;
2736 }
2737 }
2738 break;
2739
2740 case PNG_COLOR_TYPE_PALETTE:
2741 /* It's already got a color-map. It may be necessary to eliminate the
2742 * tRNS entries though.
2743 */
2744 {
2745 unsigned int num_trans = png_ptr->num_trans;
2746 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2747 png_const_colorp colormap = png_ptr->palette;
2748 const int do_background = trans != NULL &&
2749 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2750 unsigned int i;
2751
2752 /* Just in case: */
2753 if (trans == NULL)
2754 num_trans = 0;
2755
2756 output_processing = PNG_CMAP_NONE;
Glenn Randers-Pehrson3efbeca2014-07-21 16:57:30 -05002757 data_encoding = P_FILE; /* Don't change from color-map indices */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002758 cmap_entries = png_ptr->num_palette;
2759 if (cmap_entries > 256)
2760 cmap_entries = 256;
2761
2762 if (cmap_entries > image->colormap_entries)
2763 png_error(png_ptr, "palette color-map: too few entries");
2764
2765 for (i=0; i < cmap_entries; ++i)
2766 {
2767 if (do_background && i < num_trans && trans[i] < 255)
2768 {
2769 if (trans[i] == 0)
2770 png_create_colormap_entry(display, i, back_r, back_g,
2771 back_b, 0, output_encoding);
2772
2773 else
2774 {
2775 /* Must compose the PNG file color in the color-map entry
2776 * on the sRGB color in 'back'.
2777 */
2778 png_create_colormap_entry(display, i,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002779 png_colormap_compose(display, colormap[i].red, P_FILE,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002780 trans[i], back_r, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002781 png_colormap_compose(display, colormap[i].green, P_FILE,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002782 trans[i], back_g, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002783 png_colormap_compose(display, colormap[i].blue, P_FILE,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002784 trans[i], back_b, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002785 output_encoding == P_LINEAR ? trans[i] * 257U :
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002786 trans[i],
2787 output_encoding);
2788 }
2789 }
2790
2791 else
2792 png_create_colormap_entry(display, i, colormap[i].red,
2793 colormap[i].green, colormap[i].blue,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002794 i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002795 }
2796
Glenn Randers-Pehrson3efbeca2014-07-21 16:57:30 -05002797 /* The PNG data may have indices packed in fewer than 8 bits, it
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002798 * must be expanded if so.
2799 */
2800 if (png_ptr->bit_depth < 8)
2801 png_set_packing(png_ptr);
2802 }
2803 break;
2804
2805 default:
2806 png_error(png_ptr, "invalid PNG color type");
2807 /*NOT REACHED*/
2808 break;
2809 }
2810
2811 /* Now deal with the output processing */
2812 if (expand_tRNS && png_ptr->num_trans > 0 &&
2813 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
2814 png_set_tRNS_to_alpha(png_ptr);
2815
2816 switch (data_encoding)
2817 {
2818 default:
2819 png_error(png_ptr, "bad data option (internal error)");
2820 break;
2821
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002822 case P_sRGB:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002823 /* Change to 8-bit sRGB */
2824 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
2825 /* FALL THROUGH */
2826
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002827 case P_FILE:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002828 if (png_ptr->bit_depth > 8)
2829 png_set_scale_16(png_ptr);
2830 break;
2831 }
2832
2833 if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2834 png_error(png_ptr, "color map overflow (BAD internal error)");
2835
2836 image->colormap_entries = cmap_entries;
2837
2838 /* Double check using the recorded background index */
2839 switch (output_processing)
2840 {
2841 case PNG_CMAP_NONE:
2842 if (background_index != PNG_CMAP_NONE_BACKGROUND)
2843 goto bad_background;
2844 break;
2845
2846 case PNG_CMAP_GA:
2847 if (background_index != PNG_CMAP_GA_BACKGROUND)
2848 goto bad_background;
2849 break;
2850
2851 case PNG_CMAP_TRANS:
2852 if (background_index >= cmap_entries ||
2853 background_index != PNG_CMAP_TRANS_BACKGROUND)
2854 goto bad_background;
2855 break;
2856
2857 case PNG_CMAP_RGB:
2858 if (background_index != PNG_CMAP_RGB_BACKGROUND)
2859 goto bad_background;
2860 break;
2861
2862 case PNG_CMAP_RGB_ALPHA:
2863 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
2864 goto bad_background;
2865 break;
2866
2867 default:
2868 png_error(png_ptr, "bad processing option (internal error)");
2869
2870 bad_background:
2871 png_error(png_ptr, "bad background index (internal error)");
2872 }
2873
2874 display->colormap_processing = output_processing;
2875
2876 return 1/*ok*/;
2877}
2878
2879/* The final part of the color-map read called from png_image_finish_read. */
2880static int
2881png_image_read_and_map(png_voidp argument)
John Bowler7875d532011-11-07 22:33:49 -06002882{
John Bowler4fa96a42011-11-16 16:39:16 -06002883 png_image_read_control *display = png_voidcast(png_image_read_control*,
2884 argument);
John Bowler7875d532011-11-07 22:33:49 -06002885 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06002886 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler4f67e402011-12-28 08:43:37 -06002887 int passes;
John Bowler7875d532011-11-07 22:33:49 -06002888
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002889 /* Called when the libpng data must be transformed into the color-mapped
2890 * form. There is a local row buffer in display->local and this routine must
2891 * do the interlace handling.
2892 */
2893 switch (png_ptr->interlaced)
John Bowler7875d532011-11-07 22:33:49 -06002894 {
2895 case PNG_INTERLACE_NONE:
2896 passes = 1;
2897 break;
2898
2899 case PNG_INTERLACE_ADAM7:
2900 passes = PNG_INTERLACE_ADAM7_PASSES;
2901 break;
2902
2903 default:
2904 png_error(png_ptr, "unknown interlace type");
2905 }
2906
2907 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002908 png_uint_32 height = image->height;
2909 png_uint_32 width = image->width;
2910 int proc = display->colormap_processing;
2911 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
2912 ptrdiff_t step_row = display->row_bytes;
John Bowler7875d532011-11-07 22:33:49 -06002913 int pass;
2914
2915 for (pass = 0; pass < passes; ++pass)
2916 {
John Bowler7875d532011-11-07 22:33:49 -06002917 unsigned int startx, stepx, stepy;
2918 png_uint_32 y;
2919
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002920 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
John Bowler7875d532011-11-07 22:33:49 -06002921 {
2922 /* The row may be empty for a short image: */
2923 if (PNG_PASS_COLS(width, pass) == 0)
2924 continue;
2925
2926 startx = PNG_PASS_START_COL(pass);
2927 stepx = PNG_PASS_COL_OFFSET(pass);
2928 y = PNG_PASS_START_ROW(pass);
John Bowler18c5cfa2011-11-16 14:26:34 -06002929 stepy = PNG_PASS_ROW_OFFSET(pass);
John Bowler7875d532011-11-07 22:33:49 -06002930 }
2931
2932 else
2933 {
2934 y = 0;
2935 startx = 0;
2936 stepx = stepy = 1;
2937 }
John Bowler18c5cfa2011-11-16 14:26:34 -06002938
John Bowler7875d532011-11-07 22:33:49 -06002939 for (; y<height; y += stepy)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002940 {
2941 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
2942 png_bytep outrow = first_row + y * step_row;
2943 png_const_bytep end_row = outrow + width;
2944
2945 /* Read read the libpng data into the temporary buffer. */
2946 png_read_row(png_ptr, inrow, NULL);
2947
2948 /* Now process the row according to the processing option, note
2949 * that the caller verifies that the format of the libpng output
2950 * data is as required.
2951 */
2952 outrow += startx;
2953 switch (proc)
John Bowler7875d532011-11-07 22:33:49 -06002954 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002955 case PNG_CMAP_GA:
2956 for (; outrow < end_row; outrow += stepx)
John Bowler5bc90382012-01-23 22:43:22 -06002957 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002958 /* The data is always in the PNG order */
2959 unsigned int gray = *inrow++;
2960 unsigned int alpha = *inrow++;
2961 unsigned int entry;
John Bowler5bc90382012-01-23 22:43:22 -06002962
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002963 /* NOTE: this code is copied as a comment in
2964 * make_ga_colormap above. Please update the
2965 * comment if you change this code!
2966 */
2967 if (alpha > 229) /* opaque */
John Bowler5bc90382012-01-23 22:43:22 -06002968 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002969 entry = (231 * gray + 128) >> 8;
John Bowler5bc90382012-01-23 22:43:22 -06002970 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002971 else if (alpha < 26) /* transparent */
2972 {
2973 entry = 231;
2974 }
2975 else /* partially opaque */
2976 {
2977 entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
2978 }
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002979
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002980 *outrow = (png_byte)entry;
2981 }
2982 break;
2983
2984 case PNG_CMAP_TRANS:
2985 for (; outrow < end_row; outrow += stepx)
2986 {
2987 png_byte gray = *inrow++;
2988 png_byte alpha = *inrow++;
2989
2990 if (alpha == 0)
2991 *outrow = PNG_CMAP_TRANS_BACKGROUND;
2992
2993 else if (gray != PNG_CMAP_TRANS_BACKGROUND)
2994 *outrow = gray;
2995
2996 else
2997 *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
2998 }
2999 break;
3000
3001 case PNG_CMAP_RGB:
3002 for (; outrow < end_row; outrow += stepx)
3003 {
3004 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
3005 inrow += 3;
3006 }
3007 break;
3008
3009 case PNG_CMAP_RGB_ALPHA:
3010 for (; outrow < end_row; outrow += stepx)
3011 {
3012 unsigned int alpha = inrow[3];
3013
3014 /* Because the alpha entries only hold alpha==0.5 values
3015 * split the processing at alpha==0.25 (64) and 0.75
3016 * (196).
3017 */
3018
3019 if (alpha >= 196)
3020 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
3021 inrow[2]);
3022
3023 else if (alpha < 64)
3024 *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
3025
3026 else
3027 {
3028 /* Likewise there are three entries for each of r, g
3029 * and b. We could select the entry by popcount on
3030 * the top two bits on those architectures that
3031 * support it, this is what the code below does,
3032 * crudely.
3033 */
3034 unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
3035
3036 /* Here are how the values map:
3037 *
3038 * 0x00 .. 0x3f -> 0
3039 * 0x40 .. 0xbf -> 1
3040 * 0xc0 .. 0xff -> 2
3041 *
3042 * So, as above with the explicit alpha checks, the
3043 * breakpoints are at 64 and 196.
3044 */
3045 if (inrow[0] & 0x80) back_i += 9; /* red */
3046 if (inrow[0] & 0x40) back_i += 9;
3047 if (inrow[0] & 0x80) back_i += 3; /* green */
3048 if (inrow[0] & 0x40) back_i += 3;
3049 if (inrow[0] & 0x80) back_i += 1; /* blue */
3050 if (inrow[0] & 0x40) back_i += 1;
3051
3052 *outrow = (png_byte)back_i;
3053 }
3054
3055 inrow += 4;
3056 }
3057 break;
3058
3059 default:
3060 break;
3061 }
3062 }
3063 }
3064 }
3065
3066 return 1;
3067}
3068
3069static int
3070png_image_read_colormapped(png_voidp argument)
3071{
3072 png_image_read_control *display = png_voidcast(png_image_read_control*,
3073 argument);
3074 png_imagep image = display->image;
3075 png_controlp control = image->opaque;
3076 png_structrp png_ptr = control->png_ptr;
3077 png_inforp info_ptr = control->info_ptr;
3078
3079 int passes = 0; /* As a flag */
3080
3081 PNG_SKIP_CHUNKS(png_ptr);
3082
3083 /* Update the 'info' structure and make sure the result is as required; first
3084 * make sure to turn on the interlace handling if it will be required
3085 * (because it can't be turned on *after* the call to png_read_update_info!)
3086 */
3087 if (display->colormap_processing == PNG_CMAP_NONE)
3088 passes = png_set_interlace_handling(png_ptr);
3089
3090 png_read_update_info(png_ptr, info_ptr);
3091
3092 /* The expected output can be deduced from the colormap_processing option. */
3093 switch (display->colormap_processing)
3094 {
3095 case PNG_CMAP_NONE:
3096 /* Output must be one channel and one byte per pixel, the output
3097 * encoding can be anything.
3098 */
3099 if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
3100 info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
3101 info_ptr->bit_depth == 8)
3102 break;
3103
3104 goto bad_output;
3105
3106 case PNG_CMAP_TRANS:
3107 case PNG_CMAP_GA:
3108 /* Output must be two channels and the 'G' one must be sRGB, the latter
3109 * can be checked with an exact number because it should have been set
3110 * to this number above!
3111 */
3112 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
3113 info_ptr->bit_depth == 8 &&
3114 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3115 image->colormap_entries == 256)
3116 break;
3117
3118 goto bad_output;
3119
3120 case PNG_CMAP_RGB:
3121 /* Output must be 8-bit sRGB encoded RGB */
3122 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
3123 info_ptr->bit_depth == 8 &&
3124 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3125 image->colormap_entries == 216)
3126 break;
3127
3128 goto bad_output;
3129
3130 case PNG_CMAP_RGB_ALPHA:
3131 /* Output must be 8-bit sRGB encoded RGBA */
3132 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
3133 info_ptr->bit_depth == 8 &&
3134 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3135 image->colormap_entries == 244 /* 216 + 1 + 27 */)
3136 break;
3137
3138 /* goto bad_output; */
3139 /* FALL THROUGH */
3140
3141 default:
3142 bad_output:
3143 png_error(png_ptr, "bad color-map processing (internal error)");
3144 }
3145
3146 /* Now read the rows. Do this here if it is possible to read directly into
3147 * the output buffer, otherwise allocate a local row buffer of the maximum
3148 * size libpng requires and call the relevant processing routine safely.
3149 */
3150 {
3151 png_voidp first_row = display->buffer;
3152 ptrdiff_t row_bytes = display->row_stride;
3153
3154 /* The following expression is designed to work correctly whether it gives
3155 * a signed or an unsigned result.
3156 */
3157 if (row_bytes < 0)
3158 {
3159 char *ptr = png_voidcast(char*, first_row);
3160 ptr += (image->height-1) * (-row_bytes);
3161 first_row = png_voidcast(png_voidp, ptr);
3162 }
3163
3164 display->first_row = first_row;
3165 display->row_bytes = row_bytes;
3166 }
3167
3168 if (passes == 0)
3169 {
3170 int result;
3171 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3172
3173 display->local_row = row;
3174 result = png_safe_execute(image, png_image_read_and_map, display);
3175 display->local_row = NULL;
3176 png_free(png_ptr, row);
3177
3178 return result;
3179 }
3180
3181 else
3182 {
3183 png_alloc_size_t row_bytes = display->row_bytes;
3184
3185 while (--passes >= 0)
3186 {
3187 png_uint_32 y = image->height;
3188 png_bytep row = png_voidcast(png_bytep, display->first_row);
3189
3190 while (y-- > 0)
3191 {
3192 png_read_row(png_ptr, row, NULL);
3193 row += row_bytes;
3194 }
3195 }
3196
3197 return 1;
3198 }
3199}
3200
3201/* Just the row reading part of png_image_read. */
3202static int
3203png_image_read_composite(png_voidp argument)
3204{
3205 png_image_read_control *display = png_voidcast(png_image_read_control*,
3206 argument);
3207 png_imagep image = display->image;
3208 png_structrp png_ptr = image->opaque->png_ptr;
3209 int passes;
3210
3211 switch (png_ptr->interlaced)
3212 {
3213 case PNG_INTERLACE_NONE:
3214 passes = 1;
3215 break;
3216
3217 case PNG_INTERLACE_ADAM7:
3218 passes = PNG_INTERLACE_ADAM7_PASSES;
3219 break;
3220
3221 default:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003222 png_error(png_ptr, "unknown interlace type");
3223 }
3224
3225 {
3226 png_uint_32 height = image->height;
3227 png_uint_32 width = image->width;
3228 ptrdiff_t step_row = display->row_bytes;
3229 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
3230 int pass;
3231
3232 for (pass = 0; pass < passes; ++pass)
3233 {
3234 unsigned int startx, stepx, stepy;
3235 png_uint_32 y;
3236
3237 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3238 {
3239 /* The row may be empty for a short image: */
3240 if (PNG_PASS_COLS(width, pass) == 0)
3241 continue;
3242
3243 startx = PNG_PASS_START_COL(pass) * channels;
3244 stepx = PNG_PASS_COL_OFFSET(pass) * channels;
3245 y = PNG_PASS_START_ROW(pass);
3246 stepy = PNG_PASS_ROW_OFFSET(pass);
3247 }
3248
3249 else
3250 {
3251 y = 0;
3252 startx = 0;
3253 stepx = channels;
3254 stepy = 1;
3255 }
3256
3257 for (; y<height; y += stepy)
3258 {
3259 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
3260 png_bytep outrow;
3261 png_const_bytep end_row;
3262
3263 /* Read the row, which is packed: */
3264 png_read_row(png_ptr, inrow, NULL);
3265
3266 outrow = png_voidcast(png_bytep, display->first_row);
3267 outrow += y * step_row;
3268 end_row = outrow + width * channels;
3269
3270 /* Now do the composition on each pixel in this row. */
3271 outrow += startx;
3272 for (; outrow < end_row; outrow += stepx)
3273 {
3274 png_byte alpha = inrow[channels];
3275
3276 if (alpha > 0) /* else no change to the output */
3277 {
3278 unsigned int c;
3279
3280 for (c=0; c<channels; ++c)
3281 {
3282 png_uint_32 component = inrow[c];
3283
3284 if (alpha < 255) /* else just use component */
3285 {
3286 /* This is PNG_OPTIMIZED_ALPHA, the component value
3287 * is a linear 8-bit value. Combine this with the
3288 * current outrow[c] value which is sRGB encoded.
3289 * Arithmetic here is 16-bits to preserve the output
3290 * values correctly.
3291 */
3292 component *= 257*255; /* =65535 */
3293 component += (255-alpha)*png_sRGB_table[outrow[c]];
3294
3295 /* So 'component' is scaled by 255*65535 and is
3296 * therefore appropriate for the sRGB to linear
3297 * conversion table.
3298 */
3299 component = PNG_sRGB_FROM_LINEAR(component);
3300 }
3301
3302 outrow[c] = (png_byte)component;
3303 }
John Bowler7875d532011-11-07 22:33:49 -06003304 }
3305
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003306 inrow += channels+1; /* components and alpha channel */
John Bowler7875d532011-11-07 22:33:49 -06003307 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003308 }
John Bowler7875d532011-11-07 22:33:49 -06003309 }
3310 }
3311
3312 return 1;
3313}
3314
John Bowler18c5cfa2011-11-16 14:26:34 -06003315/* The do_local_background case; called when all the following transforms are to
3316 * be done:
3317 *
3318 * PNG_RGB_TO_GRAY
3319 * PNG_COMPOSITE
3320 * PNG_GAMMA
3321 *
Glenn Randers-Pehrson149eea22014-03-17 10:58:02 -05003322 * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and
John Bowler18c5cfa2011-11-16 14:26:34 -06003323 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
Glenn Randers-Pehrson149eea22014-03-17 10:58:02 -05003324 * correction. The fix-up is to prevent the PNG_COMPOSITE operation from
3325 * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha
3326 * row and handles the removal or pre-multiplication of the alpha channel.
John Bowler18c5cfa2011-11-16 14:26:34 -06003327 */
3328static int
3329png_image_read_background(png_voidp argument)
3330{
John Bowler4fa96a42011-11-16 16:39:16 -06003331 png_image_read_control *display = png_voidcast(png_image_read_control*,
3332 argument);
John Bowler18c5cfa2011-11-16 14:26:34 -06003333 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06003334 png_structrp png_ptr = image->opaque->png_ptr;
3335 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler18c5cfa2011-11-16 14:26:34 -06003336 png_uint_32 height = image->height;
3337 png_uint_32 width = image->width;
John Bowler4f67e402011-12-28 08:43:37 -06003338 int pass, passes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003339
3340 /* Double check the convoluted logic below. We expect to get here with
3341 * libpng doing rgb to gray and gamma correction but background processing
3342 * left to the png_image_read_background function. The rows libpng produce
3343 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
3344 */
3345 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
3346 png_error(png_ptr, "lost rgb to gray");
3347
3348 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
3349 png_error(png_ptr, "unexpected compose");
3350
John Bowler18c5cfa2011-11-16 14:26:34 -06003351 if (png_get_channels(png_ptr, info_ptr) != 2)
3352 png_error(png_ptr, "lost/gained channels");
3353
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003354 /* Expect the 8-bit case to always remove the alpha channel */
3355 if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
3356 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
3357 png_error(png_ptr, "unexpected 8-bit transformation");
3358
3359 switch (png_ptr->interlaced)
John Bowler18c5cfa2011-11-16 14:26:34 -06003360 {
3361 case PNG_INTERLACE_NONE:
3362 passes = 1;
3363 break;
3364
3365 case PNG_INTERLACE_ADAM7:
3366 passes = PNG_INTERLACE_ADAM7_PASSES;
3367 break;
3368
3369 default:
3370 png_error(png_ptr, "unknown interlace type");
3371 }
3372
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06003373 /* Use direct access to info_ptr here because otherwise the simplified API
3374 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
3375 * checking the value after libpng expansions, not the original value in the
3376 * PNG.
3377 */
3378 switch (info_ptr->bit_depth)
John Bowler18c5cfa2011-11-16 14:26:34 -06003379 {
3380 default:
3381 png_error(png_ptr, "unexpected bit depth");
3382 break;
3383
3384 case 8:
3385 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
Glenn Randers-Pehrson32440202013-08-21 18:01:32 -05003386 * to be removed by composing on a background: either the row if
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003387 * display->background is NULL or display->background->green if not.
John Bowler18c5cfa2011-11-16 14:26:34 -06003388 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3389 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003390 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003391 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3392 ptrdiff_t step_row = display->row_bytes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003393
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003394 for (pass = 0; pass < passes; ++pass)
John Bowler18c5cfa2011-11-16 14:26:34 -06003395 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003396 png_bytep row = png_voidcast(png_bytep,
3397 display->first_row);
3398 unsigned int startx, stepx, stepy;
3399 png_uint_32 y;
John Bowler18c5cfa2011-11-16 14:26:34 -06003400
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003401 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3402 {
3403 /* The row may be empty for a short image: */
3404 if (PNG_PASS_COLS(width, pass) == 0)
3405 continue;
John Bowler18c5cfa2011-11-16 14:26:34 -06003406
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003407 startx = PNG_PASS_START_COL(pass);
3408 stepx = PNG_PASS_COL_OFFSET(pass);
3409 y = PNG_PASS_START_ROW(pass);
3410 stepy = PNG_PASS_ROW_OFFSET(pass);
3411 }
3412
3413 else
3414 {
3415 y = 0;
3416 startx = 0;
3417 stepx = stepy = 1;
3418 }
3419
3420 if (display->background == NULL)
3421 {
3422 for (; y<height; y += stepy)
John Bowler18c5cfa2011-11-16 14:26:34 -06003423 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003424 png_bytep inrow = png_voidcast(png_bytep,
3425 display->local_row);
3426 png_bytep outrow = first_row + y * step_row;
3427 png_const_bytep end_row = outrow + width;
John Bowler18c5cfa2011-11-16 14:26:34 -06003428
3429 /* Read the row, which is packed: */
3430 png_read_row(png_ptr, inrow, NULL);
3431
3432 /* Now do the composition on each pixel in this row. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003433 outrow += startx;
John Bowler18c5cfa2011-11-16 14:26:34 -06003434 for (; outrow < end_row; outrow += stepx)
3435 {
3436 png_byte alpha = inrow[1];
3437
3438 if (alpha > 0) /* else no change to the output */
3439 {
3440 png_uint_32 component = inrow[0];
3441
3442 if (alpha < 255) /* else just use component */
3443 {
3444 /* Since PNG_OPTIMIZED_ALPHA was not set it is
3445 * necessary to invert the sRGB transfer
3446 * function and multiply the alpha out.
3447 */
3448 component = png_sRGB_table[component] * alpha;
3449 component += png_sRGB_table[outrow[0]] *
3450 (255-alpha);
3451 component = PNG_sRGB_FROM_LINEAR(component);
3452 }
3453
3454 outrow[0] = (png_byte)component;
3455 }
3456
3457 inrow += 2; /* gray and alpha channel */
3458 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003459 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003460 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003461
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003462 else /* constant background value */
3463 {
3464 png_byte background8 = display->background->green;
3465 png_uint_16 background = png_sRGB_table[background8];
John Bowler18c5cfa2011-11-16 14:26:34 -06003466
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003467 for (; y<height; y += stepy)
John Bowler18c5cfa2011-11-16 14:26:34 -06003468 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003469 png_bytep inrow = png_voidcast(png_bytep,
3470 display->local_row);
3471 png_bytep outrow = first_row + y * step_row;
3472 png_const_bytep end_row = outrow + width;
John Bowler18c5cfa2011-11-16 14:26:34 -06003473
3474 /* Read the row, which is packed: */
3475 png_read_row(png_ptr, inrow, NULL);
3476
3477 /* Now do the composition on each pixel in this row. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003478 outrow += startx;
John Bowler18c5cfa2011-11-16 14:26:34 -06003479 for (; outrow < end_row; outrow += stepx)
3480 {
3481 png_byte alpha = inrow[1];
3482
3483 if (alpha > 0) /* else use background */
3484 {
3485 png_uint_32 component = inrow[0];
3486
3487 if (alpha < 255) /* else just use component */
3488 {
3489 component = png_sRGB_table[component] * alpha;
3490 component += background * (255-alpha);
3491 component = PNG_sRGB_FROM_LINEAR(component);
3492 }
3493
3494 outrow[0] = (png_byte)component;
3495 }
3496
3497 else
3498 outrow[0] = background8;
3499
3500 inrow += 2; /* gray and alpha channel */
3501 }
3502
3503 row += display->row_bytes;
3504 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003505 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003506 }
3507 }
3508 break;
3509
3510 case 16:
3511 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3512 * still be done and, maybe, the alpha channel removed. This code also
3513 * handles the alpha-first option.
3514 */
3515 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003516 png_uint_16p first_row = png_voidcast(png_uint_16p,
3517 display->first_row);
3518 /* The division by two is safe because the caller passed in a
3519 * stride which was multiplied by 2 (below) to get row_bytes.
3520 */
3521 ptrdiff_t step_row = display->row_bytes / 2;
John Bowler18c5cfa2011-11-16 14:26:34 -06003522 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003523 unsigned int outchannels = 1+preserve_alpha;
John Bowler18c5cfa2011-11-16 14:26:34 -06003524 int swap_alpha = 0;
3525
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06003526# ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
3527 if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST))
3528 swap_alpha = 1;
3529# endif
John Bowler18c5cfa2011-11-16 14:26:34 -06003530
3531 for (pass = 0; pass < passes; ++pass)
3532 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003533 unsigned int startx, stepx, stepy;
John Bowler18c5cfa2011-11-16 14:26:34 -06003534 png_uint_32 y;
3535
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003536 /* The 'x' start and step are adjusted to output components here.
3537 */
3538 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
John Bowler18c5cfa2011-11-16 14:26:34 -06003539 {
3540 /* The row may be empty for a short image: */
3541 if (PNG_PASS_COLS(width, pass) == 0)
3542 continue;
3543
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003544 startx = PNG_PASS_START_COL(pass) * outchannels;
3545 stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
John Bowler18c5cfa2011-11-16 14:26:34 -06003546 y = PNG_PASS_START_ROW(pass);
3547 stepy = PNG_PASS_ROW_OFFSET(pass);
3548 }
3549
3550 else
3551 {
3552 y = 0;
3553 startx = 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003554 stepx = outchannels;
3555 stepy = 1;
John Bowler18c5cfa2011-11-16 14:26:34 -06003556 }
3557
John Bowler18c5cfa2011-11-16 14:26:34 -06003558 for (; y<height; y += stepy)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003559 {
3560 png_const_uint_16p inrow;
3561 png_uint_16p outrow = first_row + y*step_row;
3562 png_uint_16p end_row = outrow + width * outchannels;
3563
3564 /* Read the row, which is packed: */
3565 png_read_row(png_ptr, png_voidcast(png_bytep,
3566 display->local_row), NULL);
3567 inrow = png_voidcast(png_const_uint_16p, display->local_row);
3568
3569 /* Now do the pre-multiplication on each pixel in this row.
3570 */
3571 outrow += startx;
3572 for (; outrow < end_row; outrow += stepx)
John Bowler18c5cfa2011-11-16 14:26:34 -06003573 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003574 png_uint_32 component = inrow[0];
3575 png_uint_16 alpha = inrow[1];
John Bowler18c5cfa2011-11-16 14:26:34 -06003576
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003577 if (alpha > 0) /* else 0 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003578 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003579 if (alpha < 65535) /* else just use component */
John Bowler18c5cfa2011-11-16 14:26:34 -06003580 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003581 component *= alpha;
3582 component += 32767;
3583 component /= 65535;
John Bowler18c5cfa2011-11-16 14:26:34 -06003584 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003585 }
3586
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003587 else
3588 component = 0;
3589
3590 outrow[swap_alpha] = (png_uint_16)component;
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003591 if (preserve_alpha != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003592 outrow[1 ^ swap_alpha] = alpha;
3593
3594 inrow += 2; /* components and alpha channel */
John Bowler18c5cfa2011-11-16 14:26:34 -06003595 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003596 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003597 }
3598 }
3599 break;
3600 }
3601
3602 return 1;
3603}
3604
John Bowler7875d532011-11-07 22:33:49 -06003605/* The guts of png_image_finish_read as a png_safe_execute callback. */
3606static int
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003607png_image_read_direct(png_voidp argument)
John Bowler7875d532011-11-07 22:33:49 -06003608{
John Bowler4fa96a42011-11-16 16:39:16 -06003609 png_image_read_control *display = png_voidcast(png_image_read_control*,
3610 argument);
John Bowler7875d532011-11-07 22:33:49 -06003611 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06003612 png_structrp png_ptr = image->opaque->png_ptr;
3613 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06003614
3615 png_uint_32 format = image->format;
3616 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
3617 int do_local_compose = 0;
John Bowler18c5cfa2011-11-16 14:26:34 -06003618 int do_local_background = 0; /* to avoid double gamma correction bug */
John Bowler7875d532011-11-07 22:33:49 -06003619 int passes = 0;
3620
3621 /* Add transforms to ensure the correct output format is produced then check
3622 * that the required implementation support is there. Always expand; always
3623 * need 8 bits minimum, no palette and expanded tRNS.
3624 */
3625 png_set_expand(png_ptr);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003626
John Bowler7875d532011-11-07 22:33:49 -06003627 /* Now check the format to see if it was modified. */
3628 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003629 png_uint_32 base_format = png_image_format(png_ptr) &
3630 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
John Bowler7875d532011-11-07 22:33:49 -06003631 png_uint_32 change = format ^ base_format;
John Bowler3615d032011-11-08 10:38:09 -06003632 png_fixed_point output_gamma;
3633 int mode; /* alpha mode */
John Bowler7875d532011-11-07 22:33:49 -06003634
John Bowler18c5cfa2011-11-16 14:26:34 -06003635 /* Do this first so that we have a record if rgb to gray is happening. */
3636 if (change & PNG_FORMAT_FLAG_COLOR)
3637 {
3638 /* gray<->color transformation required. */
3639 if (format & PNG_FORMAT_FLAG_COLOR)
3640 png_set_gray_to_rgb(png_ptr);
3641
3642 else
3643 {
3644 /* libpng can't do both rgb to gray and
3645 * background/pre-multiplication if there is also significant gamma
3646 * correction, because both operations require linear colors and
3647 * the code only supports one transform doing the gamma correction.
3648 * Handle this by doing the pre-multiplication or background
3649 * operation in this code, if necessary.
3650 *
3651 * TODO: fix this by rewriting pngrtran.c (!)
3652 *
3653 * For the moment (given that fixing this in pngrtran.c is an
3654 * enormous change) 'do_local_background' is used to indicate that
3655 * the problem exists.
3656 */
3657 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3658 do_local_background = 1/*maybe*/;
3659
3660 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
3661 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
3662 }
3663
3664 change &= ~PNG_FORMAT_FLAG_COLOR;
3665 }
3666
John Bowler7875d532011-11-07 22:33:49 -06003667 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3668 */
3669 {
John Bowler3615d032011-11-08 10:38:09 -06003670 png_fixed_point input_gamma_default;
John Bowler7875d532011-11-07 22:33:49 -06003671
John Bowler59ae3892013-03-06 22:15:25 -06003672 if ((base_format & PNG_FORMAT_FLAG_LINEAR) &&
3673 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
John Bowler7875d532011-11-07 22:33:49 -06003674 input_gamma_default = PNG_GAMMA_LINEAR;
3675 else
3676 input_gamma_default = PNG_DEFAULT_sRGB;
3677
John Bowler18c5cfa2011-11-16 14:26:34 -06003678 /* Call png_set_alpha_mode to set the default for the input gamma; the
3679 * output gamma is set by a second call below.
3680 */
3681 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
3682 }
3683
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003684 if (linear != 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003685 {
3686 /* If there *is* an alpha channel in the input it must be multiplied
3687 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3688 */
3689 if (base_format & PNG_FORMAT_FLAG_ALPHA)
John Bowler3615d032011-11-08 10:38:09 -06003690 mode = PNG_ALPHA_STANDARD; /* associated alpha */
John Bowler7875d532011-11-07 22:33:49 -06003691
3692 else
John Bowler7875d532011-11-07 22:33:49 -06003693 mode = PNG_ALPHA_PNG;
John Bowler18c5cfa2011-11-16 14:26:34 -06003694
3695 output_gamma = PNG_GAMMA_LINEAR;
3696 }
3697
3698 else
3699 {
3700 mode = PNG_ALPHA_PNG;
3701 output_gamma = PNG_DEFAULT_sRGB;
3702 }
3703
3704 /* If 'do_local_background' is set check for the presence of gamma
3705 * correction; this is part of the work-round for the libpng bug
3706 * described above.
3707 *
3708 * TODO: fix libpng and remove this.
3709 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003710 if (do_local_background != 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003711 {
3712 png_fixed_point gtest;
3713
3714 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3715 * gamma correction, the screen gamma hasn't been set on png_struct
3716 * yet; it's set below. png_struct::gamma, however, is set to the
3717 * final value.
3718 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003719 if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
3720 PNG_FP_1) && !png_gamma_significant(gtest))
John Bowler18c5cfa2011-11-16 14:26:34 -06003721 do_local_background = 0;
3722
3723 else if (mode == PNG_ALPHA_STANDARD)
3724 {
3725 do_local_background = 2/*required*/;
3726 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
John Bowler7875d532011-11-07 22:33:49 -06003727 }
3728
John Bowler18c5cfa2011-11-16 14:26:34 -06003729 /* else leave as 1 for the checks below */
John Bowler3615d032011-11-08 10:38:09 -06003730 }
John Bowler7875d532011-11-07 22:33:49 -06003731
John Bowler3615d032011-11-08 10:38:09 -06003732 /* If the bit-depth changes then handle that here. */
3733 if (change & PNG_FORMAT_FLAG_LINEAR)
3734 {
3735 if (linear /*16-bit output*/)
3736 png_set_expand_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06003737
John Bowler3615d032011-11-08 10:38:09 -06003738 else /* 8-bit output */
3739 png_set_scale_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06003740
John Bowler3615d032011-11-08 10:38:09 -06003741 change &= ~PNG_FORMAT_FLAG_LINEAR;
John Bowler7875d532011-11-07 22:33:49 -06003742 }
3743
3744 /* Now the background/alpha channel changes. */
3745 if (change & PNG_FORMAT_FLAG_ALPHA)
3746 {
3747 /* Removing an alpha channel requires composition for the 8-bit
3748 * formats; for the 16-bit it is already done, above, by the
3749 * pre-multiplication and the channel just needs to be stripped.
3750 */
3751 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3752 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003753 /* If RGB->gray is happening the alpha channel must be left and the
3754 * operation completed locally.
3755 *
3756 * TODO: fix libpng and remove this.
3757 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003758 if (do_local_background != 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003759 do_local_background = 2/*required*/;
3760
John Bowler3615d032011-11-08 10:38:09 -06003761 /* 16-bit output: just remove the channel */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003762 else if (linear != 0) /* compose on black (well, pre-multiply) */
John Bowler7875d532011-11-07 22:33:49 -06003763 png_set_strip_alpha(png_ptr);
3764
John Bowler3615d032011-11-08 10:38:09 -06003765 /* 8-bit output: do an appropriate compose */
John Bowler7875d532011-11-07 22:33:49 -06003766 else if (display->background != NULL)
3767 {
3768 png_color_16 c;
3769
3770 c.index = 0; /*unused*/
3771 c.red = display->background->red;
3772 c.green = display->background->green;
3773 c.blue = display->background->blue;
3774 c.gray = display->background->green;
3775
3776 /* This is always an 8-bit sRGB value, using the 'green' channel
3777 * for gray is much better than calculating the luminance here;
3778 * we can get off-by-one errors in that calculation relative to
3779 * the app expectations and that will show up in transparent
3780 * pixels.
3781 */
3782 png_set_background_fixed(png_ptr, &c,
3783 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
3784 0/*gamma: not used*/);
3785 }
3786
3787 else /* compose on row: implemented below. */
3788 {
3789 do_local_compose = 1;
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003790 /* This leaves the alpha channel in the output, so it has to be
John Bowler7875d532011-11-07 22:33:49 -06003791 * removed by the code below. Set the encoding to the 'OPTIMIZE'
3792 * one so the code only has to hack on the pixels that require
3793 * composition.
3794 */
John Bowler3615d032011-11-08 10:38:09 -06003795 mode = PNG_ALPHA_OPTIMIZED;
John Bowler7875d532011-11-07 22:33:49 -06003796 }
3797 }
3798
3799 else /* output needs an alpha channel */
3800 {
3801 /* This is tricky because it happens before the swap operation has
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003802 * been accomplished; however, the swap does *not* swap the added
John Bowlere6fb6912011-11-08 21:35:16 -06003803 * alpha channel (weird API), so it must be added in the correct
3804 * place.
John Bowler7875d532011-11-07 22:33:49 -06003805 */
John Bowler3615d032011-11-08 10:38:09 -06003806 png_uint_32 filler; /* opaque filler */
John Bowlere6fb6912011-11-08 21:35:16 -06003807 int where;
John Bowler3615d032011-11-08 10:38:09 -06003808
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003809 if (linear != 0)
John Bowler3615d032011-11-08 10:38:09 -06003810 filler = 65535;
3811
3812 else
3813 filler = 255;
3814
John Bowlere6fb6912011-11-08 21:35:16 -06003815# ifdef PNG_FORMAT_AFIRST_SUPPORTED
3816 if (format & PNG_FORMAT_FLAG_AFIRST)
3817 {
3818 where = PNG_FILLER_BEFORE;
3819 change &= ~PNG_FORMAT_FLAG_AFIRST;
3820 }
3821
3822 else
3823# endif
3824 where = PNG_FILLER_AFTER;
3825
3826 png_set_add_alpha(png_ptr, filler, where);
John Bowler7875d532011-11-07 22:33:49 -06003827 }
3828
John Bowlere6fb6912011-11-08 21:35:16 -06003829 /* This stops the (irrelevant) call to swap_alpha below. */
John Bowler7875d532011-11-07 22:33:49 -06003830 change &= ~PNG_FORMAT_FLAG_ALPHA;
3831 }
3832
John Bowler3615d032011-11-08 10:38:09 -06003833 /* Now set the alpha mode correctly; this is always done, even if there is
3834 * no alpha channel in either the input or the output because it correctly
3835 * sets the output gamma.
3836 */
3837 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
3838
John Bowler7875d532011-11-07 22:33:49 -06003839# ifdef PNG_FORMAT_BGR_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06003840 if (change & PNG_FORMAT_FLAG_BGR)
John Bowler7875d532011-11-07 22:33:49 -06003841 {
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003842 /* Check only the output format; PNG is never BGR; don't do this if
John Bowler7875d532011-11-07 22:33:49 -06003843 * the output is gray, but fix up the 'format' value in that case.
3844 */
3845 if (format & PNG_FORMAT_FLAG_COLOR)
3846 png_set_bgr(png_ptr);
3847
3848 else
3849 format &= ~PNG_FORMAT_FLAG_BGR;
3850
3851 change &= ~PNG_FORMAT_FLAG_BGR;
3852 }
3853# endif
3854
3855# ifdef PNG_FORMAT_AFIRST_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06003856 if (change & PNG_FORMAT_FLAG_AFIRST)
John Bowler7875d532011-11-07 22:33:49 -06003857 {
3858 /* Only relevant if there is an alpha channel - it's particularly
3859 * important to handle this correctly because do_local_compose may
3860 * be set above and then libpng will keep the alpha channel for this
3861 * code to remove.
3862 */
3863 if (format & PNG_FORMAT_FLAG_ALPHA)
John Bowler18c5cfa2011-11-16 14:26:34 -06003864 {
3865 /* Disable this if doing a local background,
3866 * TODO: remove this when local background is no longer required.
3867 */
3868 if (do_local_background != 2)
3869 png_set_swap_alpha(png_ptr);
3870 }
John Bowler7875d532011-11-07 22:33:49 -06003871
3872 else
3873 format &= ~PNG_FORMAT_FLAG_AFIRST;
3874
3875 change &= ~PNG_FORMAT_FLAG_AFIRST;
3876 }
3877# endif
3878
3879 /* If the *output* is 16-bit then we need to check for a byte-swap on this
3880 * architecture.
3881 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003882 if (linear != 0)
John Bowler7875d532011-11-07 22:33:49 -06003883 {
3884 PNG_CONST png_uint_16 le = 0x0001;
3885
3886 if (*(png_const_bytep)&le)
3887 png_set_swap(png_ptr);
3888 }
3889
3890 /* If change is not now 0 some transformation is missing - error out. */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003891 if (change != 0)
John Bowler7875d532011-11-07 22:33:49 -06003892 png_error(png_ptr, "png_read_image: unsupported transformation");
3893 }
3894
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003895 PNG_SKIP_CHUNKS(png_ptr);
John Bowler89c2f842011-11-16 12:04:39 -06003896
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003897 /* Update the 'info' structure and make sure the result is as required; first
John Bowler7875d532011-11-07 22:33:49 -06003898 * make sure to turn on the interlace handling if it will be required
3899 * (because it can't be turned on *after* the call to png_read_update_info!)
John Bowler18c5cfa2011-11-16 14:26:34 -06003900 *
3901 * TODO: remove the do_local_background fixup below.
John Bowler7875d532011-11-07 22:33:49 -06003902 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003903 if (do_local_compose == 0 && do_local_background != 2)
John Bowler7875d532011-11-07 22:33:49 -06003904 passes = png_set_interlace_handling(png_ptr);
3905
3906 png_read_update_info(png_ptr, info_ptr);
3907
3908 {
3909 png_uint_32 info_format = 0;
3910
3911 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
3912 info_format |= PNG_FORMAT_FLAG_COLOR;
3913
3914 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
3915 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003916 /* do_local_compose removes this channel below. */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003917 if (do_local_compose == 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003918 {
3919 /* do_local_background does the same if required. */
3920 if (do_local_background != 2 ||
3921 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
3922 info_format |= PNG_FORMAT_FLAG_ALPHA;
3923 }
John Bowler7875d532011-11-07 22:33:49 -06003924 }
3925
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003926 else if (do_local_compose != 0) /* internal error */
John Bowler7875d532011-11-07 22:33:49 -06003927 png_error(png_ptr, "png_image_read: alpha channel lost");
3928
3929 if (info_ptr->bit_depth == 16)
3930 info_format |= PNG_FORMAT_FLAG_LINEAR;
3931
3932# ifdef PNG_FORMAT_BGR_SUPPORTED
3933 if (png_ptr->transformations & PNG_BGR)
3934 info_format |= PNG_FORMAT_FLAG_BGR;
3935# endif
3936
3937# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003938 if (do_local_background == 2)
3939 {
3940 if (format & PNG_FORMAT_FLAG_AFIRST)
3941 info_format |= PNG_FORMAT_FLAG_AFIRST;
3942 }
3943
3944 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
John Bowlere6fb6912011-11-08 21:35:16 -06003945 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
3946 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003947 {
3948 if (do_local_background == 2)
3949 png_error(png_ptr, "unexpected alpha swap transformation");
3950
John Bowler7875d532011-11-07 22:33:49 -06003951 info_format |= PNG_FORMAT_FLAG_AFIRST;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003952 }
John Bowler7875d532011-11-07 22:33:49 -06003953# endif
3954
3955 /* This is actually an internal error. */
3956 if (info_format != format)
3957 png_error(png_ptr, "png_read_image: invalid transformations");
3958 }
3959
3960 /* Now read the rows. If do_local_compose is set then it is necessary to use
3961 * a local row buffer. The output will be GA, RGBA or BGRA and must be
3962 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
3963 * display acts as a flag.
3964 */
3965 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003966 png_voidp first_row = display->buffer;
John Bowler7875d532011-11-07 22:33:49 -06003967 ptrdiff_t row_bytes = display->row_stride;
3968
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003969 if (linear != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003970 row_bytes *= 2;
John Bowler7875d532011-11-07 22:33:49 -06003971
3972 /* The following expression is designed to work correctly whether it gives
3973 * a signed or an unsigned result.
3974 */
3975 if (row_bytes < 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003976 {
3977 char *ptr = png_voidcast(char*, first_row);
3978 ptr += (image->height-1) * (-row_bytes);
3979 first_row = png_voidcast(png_voidp, ptr);
3980 }
John Bowler7875d532011-11-07 22:33:49 -06003981
3982 display->first_row = first_row;
3983 display->row_bytes = row_bytes;
3984 }
3985
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003986 if (do_local_compose != 0)
John Bowler7875d532011-11-07 22:33:49 -06003987 {
3988 int result;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003989 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
John Bowler7875d532011-11-07 22:33:49 -06003990
3991 display->local_row = row;
3992 result = png_safe_execute(image, png_image_read_composite, display);
3993 display->local_row = NULL;
3994 png_free(png_ptr, row);
3995
3996 return result;
3997 }
3998
John Bowler18c5cfa2011-11-16 14:26:34 -06003999 else if (do_local_background == 2)
4000 {
4001 int result;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004002 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
John Bowler18c5cfa2011-11-16 14:26:34 -06004003
4004 display->local_row = row;
4005 result = png_safe_execute(image, png_image_read_background, display);
4006 display->local_row = NULL;
4007 png_free(png_ptr, row);
4008
4009 return result;
4010 }
4011
John Bowler7875d532011-11-07 22:33:49 -06004012 else
4013 {
4014 png_alloc_size_t row_bytes = display->row_bytes;
4015
4016 while (--passes >= 0)
4017 {
4018 png_uint_32 y = image->height;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004019 png_bytep row = png_voidcast(png_bytep, display->first_row);
4020
John Bowler7875d532011-11-07 22:33:49 -06004021 while (y-- > 0)
4022 {
4023 png_read_row(png_ptr, row, NULL);
4024 row += row_bytes;
4025 }
4026 }
4027
4028 return 1;
4029 }
4030}
4031
4032int PNGAPI
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004033png_image_finish_read(png_imagep image, png_const_colorp background,
4034 void *buffer, png_int_32 row_stride, void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06004035{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004036 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06004037 {
4038 png_uint_32 check;
4039
John Bowler3706d732011-11-21 10:28:06 -06004040 if (row_stride == 0)
4041 row_stride = PNG_IMAGE_ROW_STRIDE(*image);
4042
John Bowler7875d532011-11-07 22:33:49 -06004043 if (row_stride < 0)
4044 check = -row_stride;
4045
4046 else
4047 check = row_stride;
4048
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004049 if (image->opaque != NULL && buffer != NULL &&
4050 check >= PNG_IMAGE_ROW_STRIDE(*image))
John Bowler7875d532011-11-07 22:33:49 -06004051 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004052 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
4053 (image->colormap_entries > 0 && colormap != NULL))
4054 {
4055 int result;
4056 png_image_read_control display;
John Bowler7875d532011-11-07 22:33:49 -06004057
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004058 memset(&display, 0, (sizeof display));
4059 display.image = image;
4060 display.buffer = buffer;
4061 display.row_stride = row_stride;
4062 display.colormap = colormap;
4063 display.background = background;
4064 display.local_row = NULL;
4065
4066 /* Choose the correct 'end' routine; for the color-map case all the
4067 * setup has already been done.
4068 */
4069 if (image->format & PNG_FORMAT_FLAG_COLORMAP)
4070 result =
4071 png_safe_execute(image, png_image_read_colormap, &display) &&
4072 png_safe_execute(image, png_image_read_colormapped, &display);
4073
4074 else
4075 result =
4076 png_safe_execute(image, png_image_read_direct, &display);
4077
4078 png_image_free(image);
4079 return result;
4080 }
4081
4082 else
4083 return png_image_error(image,
4084 "png_image_finish_read[color-map]: no color-map");
John Bowler7875d532011-11-07 22:33:49 -06004085 }
4086
4087 else
4088 return png_image_error(image,
4089 "png_image_finish_read: invalid argument");
4090 }
4091
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004092 else if (image != NULL)
4093 return png_image_error(image,
4094 "png_image_finish_read: damaged PNG_IMAGE_VERSION");
4095
John Bowler7875d532011-11-07 22:33:49 -06004096 return 0;
4097}
4098
4099#endif /* PNG_SIMPLIFIED_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06004100#endif /* PNG_READ_SUPPORTED */