blob: 6239503e221ff6f0a4441df35191c1b316b38438 [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-Pehrsonc98f7fb2015-03-26 08:06:25 -05004 * Last changed in libpng 1.6.17 [March 26, 2015]
Glenn Randers-Pehrson6ef579d2015-01-27 07:02:46 -06005 * Copyright (c) 1998-2015 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-Pehrsoncda68df2014-11-06 22:11:39 -060047#endif /* USER_MEM */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050048
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 */
John Bowler8ee821e2015-05-06 20:03:14 -050066# if PNG_RELEASE_BUILD
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060067 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 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500116 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600117 png_chunk_error(png_ptr, "Missing IHDR before IDAT");
118
119 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500120 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600121 png_chunk_error(png_ptr, "Missing PLTE before IDAT");
122
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500123 else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600124 png_chunk_benign_error(png_ptr, "Too many IDATs found");
125
126 png_ptr->mode |= PNG_HAVE_IDAT;
127 }
128
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500129 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600130 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-Pehrsoncda68df2014-11-06 22:11:39 -0600255#endif /* SEQUENTIAL_READ */
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-Pehrsoncda68df2014-11-06 22:11:39 -0600305#endif /* SEQUENTIAL_READ */
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 (
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500318 (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600319 {
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}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600372#endif /* MNG_FEATURES */
John Bowlerc10930a2013-12-19 15:24:06 -0600373
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 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500388 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
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)
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500404 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
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)
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500409 if ((png_ptr->transformations & PNG_FILLER) != 0)
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-Pehrsonb963fee2014-11-01 13:18:36 -0500415 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
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)
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500420 if ((png_ptr->transformations & PNG_PACK) != 0)
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)
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500425 if ((png_ptr->transformations & PNG_SHIFT) != 0)
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)
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500430 if ((png_ptr->transformations & PNG_BGR) != 0)
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)
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500435 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
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-Pehrsoncda68df2014-11-06 22:11:39 -0600439#endif /* WARNINGS */
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 */
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500448 if (png_ptr->interlaced != 0 &&
449 (png_ptr->transformations & PNG_INTERLACE) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500450 {
451 switch (png_ptr->pass)
452 {
453 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600454 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500455 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500457 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600458 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500459 return;
460 }
461 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500462
Guy Schalnat0d580581995-07-20 02:43:20 -0500463 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600464 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500465 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500466 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500467 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500468
Guy Schalnat0d580581995-07-20 02:43:20 -0500469 png_read_finish_row(png_ptr);
470 return;
471 }
472 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500473
Guy Schalnat0d580581995-07-20 02:43:20 -0500474 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600475 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500476 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500477 if (dsp_row != NULL && (png_ptr->row_number & 4))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500478 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500479
Guy Schalnat0d580581995-07-20 02:43:20 -0500480 png_read_finish_row(png_ptr);
481 return;
482 }
483 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500484
Guy Schalnat0d580581995-07-20 02:43:20 -0500485 case 3:
486 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
487 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500488 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500489 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500490
Guy Schalnat0d580581995-07-20 02:43:20 -0500491 png_read_finish_row(png_ptr);
492 return;
493 }
494 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500495
Guy Schalnat0d580581995-07-20 02:43:20 -0500496 case 4:
497 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600498 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500499 if (dsp_row != NULL && (png_ptr->row_number & 2))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500500 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500501
Guy Schalnat0d580581995-07-20 02:43:20 -0500502 png_read_finish_row(png_ptr);
503 return;
504 }
505 break;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600506
Guy Schalnat0d580581995-07-20 02:43:20 -0500507 case 5:
508 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
509 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500510 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500511 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500512
Guy Schalnat0d580581995-07-20 02:43:20 -0500513 png_read_finish_row(png_ptr);
514 return;
515 }
516 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500517
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600518 default:
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600519 case 6:
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500520 if ((png_ptr->row_number & 1) == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500521 {
522 png_read_finish_row(png_ptr);
523 return;
524 }
525 break;
526 }
527 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500528#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500529
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500530 if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500531 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500532
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600533 /* Fill the row with IDAT data: */
534 png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500535
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500536 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
537 {
538 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
Mans Rullgardd3a94802011-11-03 00:47:55 -0500539 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500540 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
541 else
542 png_error(png_ptr, "bad adaptive filter value");
543 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500544
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500545 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
546 * 1.5.6, while the buffer really is this big in current versions of libpng
547 * it may not be in the future, so this was changed just to copy the
548 * interlaced count:
549 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600550 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600551
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500552#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500553 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600554 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600555 {
556 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500557 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600558 }
559#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500560
John Bowler4a12f4a2011-04-17 18:34:22 -0500561#ifdef PNG_READ_TRANSFORMS_SUPPORTED
John Bowler9b872f42011-02-12 09:00:16 -0600562 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500563 png_do_read_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500564#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500565
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500566 /* The transformed pixel depth should match the depth now in row_info. */
567 if (png_ptr->transformed_pixel_depth == 0)
568 {
569 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
570 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
571 png_error(png_ptr, "sequential row overflow");
572 }
573
574 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
575 png_error(png_ptr, "internal sequential row size calculation error");
576
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500577#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonc4b37182014-04-06 08:59:57 -0500578 /* Expand interlaced rows to full size */
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500579 if (png_ptr->interlaced != 0 &&
580 (png_ptr->transformations & PNG_INTERLACE) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500581 {
582 if (png_ptr->pass < 6)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500583 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
584 png_ptr->transformations);
Guy Schalnat0d580581995-07-20 02:43:20 -0500585
Andreas Dilger47a0c421997-05-16 02:46:07 -0500586 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500587 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500588
Andreas Dilger47a0c421997-05-16 02:46:07 -0500589 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500590 png_combine_row(png_ptr, row, 0/*row*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500591 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500592
Guy Schalnat0d580581995-07-20 02:43:20 -0500593 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500594#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500595 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500596 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500597 png_combine_row(png_ptr, row, -1/*ignored*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500598
Andreas Dilger47a0c421997-05-16 02:46:07 -0500599 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500600 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500601 }
602 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600603
604 if (png_ptr->read_row_fn != NULL)
605 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600606
Guy Schalnat0d580581995-07-20 02:43:20 -0500607}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600608#endif /* SEQUENTIAL_READ */
Guy Schalnat0d580581995-07-20 02:43:20 -0500609
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500610#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500611/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600612 * and png_set_interlace_handling() has been called, the rows need to
613 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500614 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600615 * called, the rows contents must be initialized to the contents of the
616 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600617 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600618 * "row" holds the actual image, and pixels are placed in it
619 * as they arrive. If the image is displayed after each pass, it will
620 * appear to "sparkle" in. "display_row" can be used to display a
621 * "chunky" progressive image, with finer detail added as it becomes
622 * available. If you do not want this "chunky" display, you may pass
623 * NULL for display_row. If you do not want the sparkle display, and
624 * you have not called png_handle_alpha(), you may pass NULL for rows.
625 * If you have called png_handle_alpha(), and the image has either an
626 * alpha channel or a transparency chunk, you must provide a buffer for
627 * rows. In this case, you do not have to provide a display_row buffer
628 * also, but you may. If the image is not interlaced, or if you have
629 * not called png_set_interlace_handling(), the display_row buffer will
630 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500631 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600632 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600633 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600634
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500635void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600636png_read_rows(png_structrp png_ptr, png_bytepp row,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600637 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500638{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600639 png_uint_32 i;
640 png_bytepp rp;
641 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500642
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500643 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500644
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500645 if (png_ptr == NULL)
646 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500647
Guy Schalnat0f716451995-11-28 11:22:13 -0600648 rp = row;
649 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500650 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500651 for (i = 0; i < num_rows; i++)
652 {
653 png_bytep rptr = *rp++;
654 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600655
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500656 png_read_row(png_ptr, rptr, dptr);
657 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500658
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500659 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500660 for (i = 0; i < num_rows; i++)
661 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500662 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500663 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500664 rp++;
665 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500666
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500667 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500668 for (i = 0; i < num_rows; i++)
669 {
670 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500671 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500672 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500673 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500674}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600675#endif /* SEQUENTIAL_READ */
Guy Schalnat0d580581995-07-20 02:43:20 -0500676
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500677#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500678/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500679 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600680 * initialize the image to the current image that PNG will be overlaying.
681 * We set the num_rows again here, in case it was incorrectly set in
682 * png_read_start_row() by a call to png_read_update_info() or
683 * png_start_read_image() if png_set_interlace_handling() wasn't called
684 * prior to either of these functions like it should have been. You can
685 * only call this function once. If you desire to have an image for
686 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500687 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600688 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600689 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500690void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600691png_read_image(png_structrp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500692{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500693 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500694 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600695 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500696
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500697 png_debug(1, "in png_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500698
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500699 if (png_ptr == NULL)
700 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500701
702#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500703 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500704 {
705 pass = png_set_interlace_handling(png_ptr);
706 /* And make sure transforms are initialized. */
707 png_start_read_image(png_ptr);
708 }
709 else
710 {
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500711 if (png_ptr->interlaced != 0 &&
712 (png_ptr->transformations & PNG_INTERLACE) == 0)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500713 {
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500714 /* Caller called png_start_read_image or png_read_update_info without
715 * first turning on the PNG_INTERLACE transform. We can fix this here,
716 * but the caller should do it!
717 */
718 png_warning(png_ptr, "Interlace handling should be turned on when "
719 "using png_read_image");
720 /* Make sure this is set correctly */
721 png_ptr->num_rows = png_ptr->height;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500722 }
723
724 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
725 * the above error case.
726 */
727 pass = png_set_interlace_handling(png_ptr);
728 }
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500729#else
730 if (png_ptr->interlaced)
731 png_error(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600732 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500733
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500734 pass = 1;
735#endif
736
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500737 image_height=png_ptr->height;
Guy Schalnate5a37791996-06-05 15:50:50 -0500738
Guy Schalnat0d580581995-07-20 02:43:20 -0500739 for (j = 0; j < pass; j++)
740 {
741 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500742 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500743 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500744 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500745 rp++;
746 }
747 }
748}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600749#endif /* SEQUENTIAL_READ */
Guy Schalnat0d580581995-07-20 02:43:20 -0500750
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500751#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500752/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600753 * file, will verify the end is accurate, and will read any comments
754 * or time information at the end of the file, if info is not NULL.
755 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500756void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600757png_read_end(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500758{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600759#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
760 int keep;
761#endif
762
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500763 png_debug(1, "in png_read_end");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500764
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500765 if (png_ptr == NULL)
766 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500767
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600768 /* If png_read_end is called in the middle of reading the rows there may
769 * still be pending IDAT data and an owned zstream. Deal with this here.
770 */
771#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500772 if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600773#endif
774 png_read_finish_IDAT(png_ptr);
775
776#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
777 /* Report invalid palette index; added at libng-1.5.10 */
778 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
779 png_ptr->num_palette_max > png_ptr->num_palette)
780 png_benign_error(png_ptr, "Read palette index exceeding num_palette");
781#endif
John Bowler6f237b62012-03-02 13:13:15 -0600782
Guy Schalnat0d580581995-07-20 02:43:20 -0500783 do
784 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500785 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500786 png_uint_32 chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500787
Glenn Randers-Pehrson9c5a1ba2014-02-17 09:12:52 -0600788 if (chunk_name == png_IEND)
789 png_handle_IEND(png_ptr, info_ptr, length);
790
791 else if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600792 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500793
Glenn Randers-Pehrson9c5a1ba2014-02-17 09:12:52 -0600794 else if (info_ptr == NULL)
795 png_crc_finish(png_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500796
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600797#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600798 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600799 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500800 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600801 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500802 if ((length > 0) ||
803 (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500804 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600805 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600806 png_handle_unknown(png_ptr, info_ptr, length, keep);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500807 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600808 png_ptr->mode |= PNG_HAVE_PLTE;
809 }
810#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500811
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500812 else if (chunk_name == png_IDAT)
Guy Schalnat0d580581995-07-20 02:43:20 -0500813 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500814 /* Zero length IDATs are legal after the last IDAT has been
815 * read, but not after other chunks have been read.
816 */
Glenn Randers-Pehrson6ef579d2015-01-27 07:02:46 -0600817 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500818 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500819
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500820 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500821 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500822 else if (chunk_name == png_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500823 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500824
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500825#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500826 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500827 png_handle_bKGD(png_ptr, info_ptr, length);
828#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500829
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500830#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500831 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500832 png_handle_cHRM(png_ptr, info_ptr, length);
833#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500834
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500835#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500836 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500837 png_handle_gAMA(png_ptr, info_ptr, length);
838#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500839
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500840#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500841 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500842 png_handle_hIST(png_ptr, info_ptr, length);
843#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500844
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500845#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500846 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500847 png_handle_oFFs(png_ptr, info_ptr, length);
848#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500849
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500850#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500851 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500852 png_handle_pCAL(png_ptr, info_ptr, length);
853#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500854
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500855#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500856 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600857 png_handle_sCAL(png_ptr, info_ptr, length);
858#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500859
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500860#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500861 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500862 png_handle_pHYs(png_ptr, info_ptr, length);
863#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500864
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500865#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500866 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500867 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500868#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500869
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500870#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500871 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600872 png_handle_sRGB(png_ptr, info_ptr, length);
873#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500874
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500875#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500876 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600877 png_handle_iCCP(png_ptr, info_ptr, length);
878#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500879
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500880#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500881 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600882 png_handle_sPLT(png_ptr, info_ptr, length);
883#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500884
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500885#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500886 else if (chunk_name == png_tEXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600887 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500888#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500889
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500890#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500891 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500892 png_handle_tIME(png_ptr, info_ptr, length);
893#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500894
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500895#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500896 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500897 png_handle_tRNS(png_ptr, info_ptr, length);
898#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500899
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500900#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500901 else if (chunk_name == png_zTXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600902 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500903#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500904
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500905#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500906 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600907 png_handle_iTXt(png_ptr, info_ptr, length);
908#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500909
Guy Schalnat0d580581995-07-20 02:43:20 -0500910 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600911 png_handle_unknown(png_ptr, info_ptr, length,
912 PNG_HANDLE_CHUNK_AS_DEFAULT);
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -0500913 } while ((png_ptr->mode & PNG_HAVE_IEND) == 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500914}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600915#endif /* SEQUENTIAL_READ */
Guy Schalnat0d580581995-07-20 02:43:20 -0500916
John Bowler1d7f56a2011-12-21 20:14:23 -0600917/* Free all memory used in the read struct */
918static void
John Bowler5d567862011-12-24 09:12:00 -0600919png_read_destroy(png_structrp png_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500920{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500921 png_debug(1, "in png_read_destroy");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500922
John Bowler07772cb2011-10-14 18:19:47 -0500923#ifdef PNG_READ_GAMMA_SUPPORTED
924 png_destroy_gamma_table(png_ptr);
925#endif
926
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -0500927 png_free(png_ptr, png_ptr->big_row_buf);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500928 png_ptr->big_row_buf = NULL;
Mans Rullgard1c422762011-10-17 16:52:19 -0500929 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500930 png_ptr->big_prev_row = NULL;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600931 png_free(png_ptr, png_ptr->read_buffer);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500932 png_ptr->read_buffer = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500933
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500934#ifdef PNG_READ_QUANTIZE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600935 png_free(png_ptr, png_ptr->palette_lookup);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500936 png_ptr->palette_lookup = NULL;
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500937 png_free(png_ptr, png_ptr->quantize_index);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500938 png_ptr->quantize_index = NULL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500939#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500940
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500941 if ((png_ptr->free_me & PNG_FREE_PLTE) != 0)
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500942 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600943 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500944 png_ptr->palette = NULL;
945 }
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600946 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500947
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600948#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500949 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500950 if ((png_ptr->free_me & PNG_FREE_TRNS) != 0)
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500951 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500952 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500953 png_ptr->trans_alpha = NULL;
954 }
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600955 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500956#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500957
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600958 inflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500959
Guy Schalnat6d764711995-12-19 03:22:19 -0600960#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600961 png_free(png_ptr, png_ptr->save_buffer);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500962 png_ptr->save_buffer = NULL;
Guy Schalnat6d764711995-12-19 03:22:19 -0600963#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500964
Glenn Randers-Pehrsona243ec02014-08-02 18:06:34 -0500965#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600966 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
John Bowler40b26032011-12-22 08:09:15 -0600967 png_free(png_ptr, png_ptr->unknown_chunk.data);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500968 png_ptr->unknown_chunk.data = NULL;
John Bowler40b26032011-12-22 08:09:15 -0600969#endif
970
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600971#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
John Bowler40b26032011-12-22 08:09:15 -0600972 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500973 png_ptr->chunk_list = NULL;
John Bowler40b26032011-12-22 08:09:15 -0600974#endif
975
John Bowler1d7f56a2011-12-21 20:14:23 -0600976 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
977 * callbacks are still set at this point. They are required to complete the
978 * destruction of the png_struct itself.
Guy Schalnate5a37791996-06-05 15:50:50 -0500979 */
John Bowler1d7f56a2011-12-21 20:14:23 -0600980}
Guy Schalnate5a37791996-06-05 15:50:50 -0500981
John Bowler1d7f56a2011-12-21 20:14:23 -0600982/* Free all memory used by the read */
983void PNGAPI
984png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
985 png_infopp end_info_ptr_ptr)
986{
John Bowler5d567862011-12-24 09:12:00 -0600987 png_structrp png_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500988
John Bowler1d7f56a2011-12-21 20:14:23 -0600989 png_debug(1, "in png_destroy_read_struct");
Guy Schalnate5a37791996-06-05 15:50:50 -0500990
John Bowler1d7f56a2011-12-21 20:14:23 -0600991 if (png_ptr_ptr != NULL)
992 png_ptr = *png_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500993
John Bowler1d7f56a2011-12-21 20:14:23 -0600994 if (png_ptr == NULL)
995 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600996
John Bowler1d7f56a2011-12-21 20:14:23 -0600997 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
998 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
999 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
1000 */
1001 png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
1002 png_destroy_info_struct(png_ptr, info_ptr_ptr);
1003
1004 *png_ptr_ptr = NULL;
1005 png_read_destroy(png_ptr);
1006 png_destroy_png_struct(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001007}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001008
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001009void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001010png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001011{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001012 if (png_ptr == NULL)
1013 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001014
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001015 png_ptr->read_row_fn = read_row_fn;
1016}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001017
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001018
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001019#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001020#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001021void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001022png_read_png(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001023 int transforms,
1024 voidp params)
1025{
John Bowler6a6d79f2011-02-12 08:56:40 -06001026 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001027 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001028
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001029 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001030 * PNG file before the first IDAT (image data chunk).
1031 */
1032 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001033 if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001034 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001035
1036 /* -------------- image transformations start here ------------------- */
John Bowler414d7b52014-02-06 11:32:57 -06001037 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
1038 * is not implemented. This will only happen in de-configured (non-default)
1039 * libpng builds. The results can be unexpected - png_read_png may return
1040 * short or mal-formed rows because the transform is skipped.
1041 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001042
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -05001043 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001044 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001045 if ((transforms & PNG_TRANSFORM_SCALE_16) != 0)
Glenn Randers-Pehrson6da2f2d2011-06-17 23:07:16 -05001046 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
1047 * did in earlier versions, while "scale_16" is now more accurate.
1048 */
John Bowler414d7b52014-02-06 11:32:57 -06001049#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001050 png_set_scale_16(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001051#else
1052 png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001053#endif
1054
John Bowler8d261262011-06-18 13:37:11 -05001055 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
1056 * latter by doing SCALE first. This is ok and allows apps not to check for
1057 * which is supported to get the right answer.
1058 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001059 if ((transforms & PNG_TRANSFORM_STRIP_16) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001060#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
John Bowler8d261262011-06-18 13:37:11 -05001061 png_set_strip_16(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001062#else
1063 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001064#endif
1065
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001066 /* Strip alpha bytes from the input data without combining with
1067 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001068 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001069 if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001070#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001071 png_set_strip_alpha(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001072#else
1073 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001074#endif
1075
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001076 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001077 * byte into separate bytes (useful for paletted and grayscale images).
1078 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001079 if ((transforms & PNG_TRANSFORM_PACKING) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001080#ifdef PNG_READ_PACK_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001081 png_set_packing(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001082#else
1083 png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001084#endif
1085
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001086 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001087 * (not useful if you are using png_set_packing).
1088 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001089 if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001090#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001091 png_set_packswap(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001092#else
1093 png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001094#endif
1095
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001096 /* Expand paletted colors into true RGB triplets
1097 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1098 * Expand paletted or RGB images with transparency to full alpha
1099 * channels so the data will be available as RGBA quartets.
1100 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001101 if ((transforms & PNG_TRANSFORM_EXPAND) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001102#ifdef PNG_READ_EXPAND_SUPPORTED
1103 png_set_expand(png_ptr);
1104#else
1105 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001106#endif
1107
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001108 /* We don't handle background color or gamma transformation or quantizing.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001109 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001110
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001111 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001112 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001113 if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001114#ifdef PNG_READ_INVERT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001115 png_set_invert_mono(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001116#else
1117 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001118#endif
1119
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001120 /* If you want to shift the pixel values from the range [0,255] or
1121 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1122 * colors were originally in:
1123 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001124 if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001125#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001126 if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001127 png_set_shift(png_ptr, &info_ptr->sig_bit);
1128#else
1129 png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001130#endif
1131
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001132 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001133 if ((transforms & PNG_TRANSFORM_BGR) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001134#ifdef PNG_READ_BGR_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001135 png_set_bgr(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001136#else
1137 png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001138#endif
1139
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001140 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001141 if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001142#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001143 png_set_swap_alpha(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001144#else
1145 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001146#endif
1147
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -05001148 /* Swap bytes of 16-bit files to least significant byte first */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001149 if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001150#ifdef PNG_READ_SWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001151 png_set_swap(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001152#else
1153 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001154#endif
1155
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001156/* Added at libpng-1.2.41 */
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001157 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001158 if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001159#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001160 png_set_invert_alpha(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001161#else
1162 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001163#endif
1164
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001165/* Added at libpng-1.2.41 */
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001166 /* Expand grayscale image to RGB */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001167 if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001168#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001169 png_set_gray_to_rgb(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001170#else
1171 png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001172#endif
1173
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001174/* Added at libpng-1.5.4 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001175 if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001176#ifdef PNG_READ_EXPAND_16_SUPPORTED
John Bowler96cec0e2011-05-08 22:48:12 -05001177 png_set_expand_16(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001178#else
1179 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
John Bowler96cec0e2011-05-08 22:48:12 -05001180#endif
1181
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001182 /* We don't handle adding filler bytes */
1183
John Bowler6a6d79f2011-02-12 08:56:40 -06001184 /* We use png_read_image and rely on that for interlace handling, but we also
1185 * call png_read_update_info therefore must turn on interlace handling now:
1186 */
1187 (void)png_set_interlace_handling(png_ptr);
1188
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001189 /* Optional call to gamma correct and add the background to the palette
1190 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001191 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001192 */
1193 png_read_update_info(png_ptr, info_ptr);
1194
1195 /* -------------- image transformations end here ------------------- */
1196
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001197 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001198 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001199 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001200 png_uint_32 iptr;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001201
John Bowler414d7b52014-02-06 11:32:57 -06001202 info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
1203 info_ptr->height * (sizeof (png_bytep))));
1204
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001205 for (iptr=0; iptr<info_ptr->height; iptr++)
1206 info_ptr->row_pointers[iptr] = NULL;
1207
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001208 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001209
John Bowler414d7b52014-02-06 11:32:57 -06001210 for (iptr = 0; iptr < info_ptr->height; iptr++)
1211 info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
1212 png_malloc(png_ptr, info_ptr->rowbytes));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001213 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001214
1215 png_read_image(png_ptr, info_ptr->row_pointers);
1216 info_ptr->valid |= PNG_INFO_IDAT;
1217
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001218 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001219 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001220
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001221 PNG_UNUSED(params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001222}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001223#endif /* INFO_IMAGE */
1224#endif /* SEQUENTIAL_READ */
John Bowler7875d532011-11-07 22:33:49 -06001225
1226#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1227/* SIMPLIFIED READ
1228 *
1229 * This code currently relies on the sequential reader, though it could easily
1230 * be made to work with the progressive one.
1231 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001232/* Arguments to png_image_finish_read: */
1233
1234/* Encoding of PNG data (used by the color-map code) */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001235# define P_NOTSET 0 /* File encoding not yet known */
1236# define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
1237# define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1238# define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1239# define P_LINEAR8 4 /* 8-bit linear: only from a file value */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001240
1241/* Color-map processing: after libpng has run on the PNG image further
Glenn Randers-Pehrsona243ec02014-08-02 18:06:34 -05001242 * processing may be needed to convert the data to color-map indices.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001243 */
1244#define PNG_CMAP_NONE 0
1245#define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1246#define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1247#define PNG_CMAP_RGB 3 /* Process RGB data */
1248#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1249
1250/* The following document where the background is for each processing case. */
1251#define PNG_CMAP_NONE_BACKGROUND 256
1252#define PNG_CMAP_GA_BACKGROUND 231
1253#define PNG_CMAP_TRANS_BACKGROUND 254
1254#define PNG_CMAP_RGB_BACKGROUND 256
1255#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1256
1257typedef struct
1258{
1259 /* Arguments: */
1260 png_imagep image;
1261 png_voidp buffer;
1262 png_int_32 row_stride;
1263 png_voidp colormap;
1264 png_const_colorp background;
1265 /* Local variables: */
1266 png_voidp local_row;
1267 png_voidp first_row;
1268 ptrdiff_t row_bytes; /* step between rows */
1269 int file_encoding; /* E_ values above */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001270 png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001271 int colormap_processing; /* PNG_CMAP_ values above */
1272} png_image_read_control;
1273
John Bowler7875d532011-11-07 22:33:49 -06001274/* Do all the *safe* initialization - 'safe' means that png_error won't be
John Bowler18c5cfa2011-11-16 14:26:34 -06001275 * called, so setting up the jmp_buf is not required. This means that anything
1276 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1277 * instead so that control is returned safely back to this routine.
John Bowler7875d532011-11-07 22:33:49 -06001278 */
1279static int
1280png_image_read_init(png_imagep image)
1281{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001282 if (image->opaque == NULL)
1283 {
1284 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
John Bowler7875d532011-11-07 22:33:49 -06001285 png_safe_error, png_safe_warning);
1286
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001287 /* And set the rest of the structure to NULL to ensure that the various
1288 * fields are consistent.
1289 */
1290 memset(image, 0, (sizeof *image));
1291 image->version = PNG_IMAGE_VERSION;
John Bowler7875d532011-11-07 22:33:49 -06001292
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001293 if (png_ptr != NULL)
John Bowler7875d532011-11-07 22:33:49 -06001294 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001295 png_infop info_ptr = png_create_info_struct(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001296
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001297 if (info_ptr != NULL)
John Bowler7875d532011-11-07 22:33:49 -06001298 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001299 png_controlp control = png_voidcast(png_controlp,
1300 png_malloc_warn(png_ptr, (sizeof *control)));
John Bowler7875d532011-11-07 22:33:49 -06001301
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001302 if (control != NULL)
1303 {
1304 memset(control, 0, (sizeof *control));
John Bowler7875d532011-11-07 22:33:49 -06001305
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001306 control->png_ptr = png_ptr;
1307 control->info_ptr = info_ptr;
1308 control->for_write = 0;
1309
1310 image->opaque = control;
1311 return 1;
1312 }
1313
1314 /* Error clean up */
1315 png_destroy_info_struct(png_ptr, &info_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001316 }
1317
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001318 png_destroy_read_struct(&png_ptr, NULL, NULL);
John Bowler7875d532011-11-07 22:33:49 -06001319 }
1320
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001321 return png_image_error(image, "png_image_read: out of memory");
John Bowler7875d532011-11-07 22:33:49 -06001322 }
1323
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001324 return png_image_error(image, "png_image_read: opaque pointer not NULL");
John Bowler7875d532011-11-07 22:33:49 -06001325}
1326
1327/* Utility to find the base format of a PNG file from a png_struct. */
1328static png_uint_32
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001329png_image_format(png_structrp png_ptr)
John Bowler7875d532011-11-07 22:33:49 -06001330{
1331 png_uint_32 format = 0;
1332
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001333 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
John Bowler7875d532011-11-07 22:33:49 -06001334 format |= PNG_FORMAT_FLAG_COLOR;
1335
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001336 if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
John Bowler7875d532011-11-07 22:33:49 -06001337 format |= PNG_FORMAT_FLAG_ALPHA;
1338
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001339 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1340 * sets the png_struct fields; that's all we are interested in here. The
1341 * precise interaction with an app call to png_set_tRNS and PNG file reading
1342 * is unclear.
1343 */
1344 else if (png_ptr->num_trans > 0)
John Bowler7875d532011-11-07 22:33:49 -06001345 format |= PNG_FORMAT_FLAG_ALPHA;
1346
1347 if (png_ptr->bit_depth == 16)
1348 format |= PNG_FORMAT_FLAG_LINEAR;
1349
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001350 if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001351 format |= PNG_FORMAT_FLAG_COLORMAP;
1352
John Bowler7875d532011-11-07 22:33:49 -06001353 return format;
1354}
1355
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001356/* Is the given gamma significantly different from sRGB? The test is the same
1357 * one used in pngrtran.c when deciding whether to do gamma correction. The
1358 * arithmetic optimizes the division by using the fact that the inverse of the
1359 * file sRGB gamma is 2.2
1360 */
1361static int
1362png_gamma_not_sRGB(png_fixed_point g)
1363{
1364 if (g < PNG_FP_1)
1365 {
1366 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
1367 if (g == 0)
1368 return 0;
1369
1370 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1371 }
1372
1373 return 1;
1374}
1375
John Bowler7875d532011-11-07 22:33:49 -06001376/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1377 * header and fill in all the information. This is executed in a safe context,
1378 * unlike the init routine above.
1379 */
1380static int
1381png_image_read_header(png_voidp argument)
1382{
John Bowler4fa96a42011-11-16 16:39:16 -06001383 png_imagep image = png_voidcast(png_imagep, argument);
John Bowler5d567862011-12-24 09:12:00 -06001384 png_structrp png_ptr = image->opaque->png_ptr;
1385 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001386
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001387 png_set_benign_errors(png_ptr, 1/*warn*/);
John Bowler7875d532011-11-07 22:33:49 -06001388 png_read_info(png_ptr, info_ptr);
1389
1390 /* Do this the fast way; just read directly out of png_struct. */
1391 image->width = png_ptr->width;
1392 image->height = png_ptr->height;
1393
1394 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001395 png_uint_32 format = png_image_format(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001396
1397 image->format = format;
John Bowler04336ba2012-01-16 07:48:36 -06001398
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001399#ifdef PNG_COLORSPACE_SUPPORTED
1400 /* Does the colorspace match sRGB? If there is no color endpoint
1401 * (colorant) information assume yes, otherwise require the
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001402 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001403 * colorspace has been determined to be invalid ignore it.
1404 */
1405 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
1406 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
1407 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
1408 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1409#endif
1410 }
1411
1412 /* We need the maximum number of entries regardless of the format the
1413 * application sets here.
1414 */
1415 {
1416 png_uint_32 cmap_entries;
1417
1418 switch (png_ptr->color_type)
John Bowler5bc90382012-01-23 22:43:22 -06001419 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001420 case PNG_COLOR_TYPE_GRAY:
1421 cmap_entries = 1U << png_ptr->bit_depth;
1422 break;
John Bowler5bc90382012-01-23 22:43:22 -06001423
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001424 case PNG_COLOR_TYPE_PALETTE:
1425 cmap_entries = png_ptr->num_palette;
1426 break;
1427
1428 default:
1429 cmap_entries = 256;
1430 break;
John Bowler5bc90382012-01-23 22:43:22 -06001431 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001432
1433 if (cmap_entries > 256)
1434 cmap_entries = 256;
1435
1436 image->colormap_entries = cmap_entries;
John Bowler5bc90382012-01-23 22:43:22 -06001437 }
1438
John Bowler7875d532011-11-07 22:33:49 -06001439 return 1;
1440}
1441
1442#ifdef PNG_STDIO_SUPPORTED
1443int PNGAPI
1444png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1445{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001446 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001447 {
1448 if (file != NULL)
1449 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001450 if (png_image_read_init(image) != 0)
John Bowler7875d532011-11-07 22:33:49 -06001451 {
1452 /* This is slightly evil, but png_init_io doesn't do anything other
1453 * than this and we haven't changed the standard IO functions so
1454 * this saves a 'safe' function.
1455 */
1456 image->opaque->png_ptr->io_ptr = file;
1457 return png_safe_execute(image, png_image_read_header, image);
1458 }
1459 }
1460
1461 else
1462 return png_image_error(image,
1463 "png_image_begin_read_from_stdio: invalid argument");
1464 }
1465
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001466 else if (image != NULL)
1467 return png_image_error(image,
1468 "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
1469
John Bowler7875d532011-11-07 22:33:49 -06001470 return 0;
1471}
1472
1473int PNGAPI
1474png_image_begin_read_from_file(png_imagep image, const char *file_name)
1475{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001476 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001477 {
1478 if (file_name != NULL)
1479 {
1480 FILE *fp = fopen(file_name, "rb");
1481
1482 if (fp != NULL)
1483 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001484 if (png_image_read_init(image) != 0)
John Bowler7875d532011-11-07 22:33:49 -06001485 {
1486 image->opaque->png_ptr->io_ptr = fp;
1487 image->opaque->owned_file = 1;
1488 return png_safe_execute(image, png_image_read_header, image);
1489 }
1490
1491 /* Clean up: just the opened file. */
1492 (void)fclose(fp);
1493 }
1494
1495 else
1496 return png_image_error(image, strerror(errno));
1497 }
1498
1499 else
1500 return png_image_error(image,
1501 "png_image_begin_read_from_file: invalid argument");
1502 }
1503
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001504 else if (image != NULL)
1505 return png_image_error(image,
1506 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
1507
John Bowler7875d532011-11-07 22:33:49 -06001508 return 0;
1509}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001510#endif /* STDIO */
John Bowler7875d532011-11-07 22:33:49 -06001511
1512static void PNGCBAPI
1513png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
1514{
1515 if (png_ptr != NULL)
1516 {
John Bowler4fa96a42011-11-16 16:39:16 -06001517 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001518 if (image != NULL)
1519 {
1520 png_controlp cp = image->opaque;
1521 if (cp != NULL)
1522 {
1523 png_const_bytep memory = cp->memory;
1524 png_size_t size = cp->size;
1525
1526 if (memory != NULL && size >= need)
1527 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001528 memcpy(out, memory, need);
John Bowler7875d532011-11-07 22:33:49 -06001529 cp->memory = memory + need;
1530 cp->size = size - need;
1531 return;
1532 }
1533
1534 png_error(png_ptr, "read beyond end of data");
1535 }
1536 }
1537
1538 png_error(png_ptr, "invalid memory read");
1539 }
1540}
1541
1542int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1543 png_const_voidp memory, png_size_t size)
1544{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001545 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001546 {
1547 if (memory != NULL && size > 0)
1548 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001549 if (png_image_read_init(image) != 0)
John Bowler7875d532011-11-07 22:33:49 -06001550 {
1551 /* Now set the IO functions to read from the memory buffer and
1552 * store it into io_ptr. Again do this in-place to avoid calling a
1553 * libpng function that requires error handling.
1554 */
John Bowler4fa96a42011-11-16 16:39:16 -06001555 image->opaque->memory = png_voidcast(png_const_bytep, memory);
John Bowler7875d532011-11-07 22:33:49 -06001556 image->opaque->size = size;
1557 image->opaque->png_ptr->io_ptr = image;
1558 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1559
1560 return png_safe_execute(image, png_image_read_header, image);
1561 }
1562 }
1563
1564 else
1565 return png_image_error(image,
1566 "png_image_begin_read_from_memory: invalid argument");
1567 }
1568
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001569 else if (image != NULL)
1570 return png_image_error(image,
1571 "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
1572
John Bowler7875d532011-11-07 22:33:49 -06001573 return 0;
1574}
1575
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001576/* Utility function to skip chunks that are not used by the simplified image
1577 * read functions and an appropriate macro to call it.
1578 */
1579#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1580static void
1581png_image_skip_unused_chunks(png_structrp png_ptr)
John Bowler04336ba2012-01-16 07:48:36 -06001582{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001583 /* Prepare the reader to ignore all recognized chunks whose data will not
1584 * be used, i.e., all chunks recognized by libpng except for those
1585 * involved in basic image reading:
1586 *
1587 * IHDR, PLTE, IDAT, IEND
1588 *
1589 * Or image data handling:
1590 *
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001591 * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001592 *
1593 * This provides a small performance improvement and eliminates any
1594 * potential vulnerability to security problems in the unused chunks.
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001595 *
1596 * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
1597 * too. This allows the simplified API to be compiled without iCCP support,
1598 * however if the support is there the chunk is still checked to detect
1599 * errors (which are unfortunately quite common.)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001600 */
1601 {
1602 static PNG_CONST png_byte chunks_to_process[] = {
1603 98, 75, 71, 68, '\0', /* bKGD */
1604 99, 72, 82, 77, '\0', /* cHRM */
1605 103, 65, 77, 65, '\0', /* gAMA */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001606# ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001607 105, 67, 67, 80, '\0', /* iCCP */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001608# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001609 115, 66, 73, 84, '\0', /* sBIT */
1610 115, 82, 71, 66, '\0', /* sRGB */
1611 };
John Bowler5bc90382012-01-23 22:43:22 -06001612
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001613 /* Ignore unknown chunks and all other chunks except for the
1614 * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
1615 */
1616 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
1617 NULL, -1);
1618
1619 /* But do not ignore image data handling chunks */
1620 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
John Bowler03df1892014-11-05 17:19:36 -06001621 chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001622 }
1623}
1624
1625# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1626#else
1627# define PNG_SKIP_CHUNKS(p) ((void)0)
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001628#endif /* HANDLE_AS_UNKNOWN */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001629
1630/* The following macro gives the exact rounded answer for all values in the
1631 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1632 * the correct numbers 0..5
1633 */
1634#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1635
1636/* Utility functions to make particular color-maps */
1637static void
1638set_file_encoding(png_image_read_control *display)
1639{
1640 png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma;
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001641 if (png_gamma_significant(g) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001642 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001643 if (png_gamma_not_sRGB(g) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001644 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001645 display->file_encoding = P_FILE;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001646 display->gamma_to_linear = png_reciprocal(g);
1647 }
1648
1649 else
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001650 display->file_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001651 }
1652
1653 else
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001654 display->file_encoding = P_LINEAR8;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001655}
1656
1657static unsigned int
1658decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
1659{
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001660 if (encoding == P_FILE) /* double check */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001661 encoding = display->file_encoding;
1662
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001663 if (encoding == P_NOTSET) /* must be the file encoding */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001664 {
1665 set_file_encoding(display);
1666 encoding = display->file_encoding;
1667 }
1668
1669 switch (encoding)
1670 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001671 case P_FILE:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001672 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
1673 break;
1674
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001675 case P_sRGB:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001676 value = png_sRGB_table[value];
1677 break;
1678
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001679 case P_LINEAR:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001680 break;
1681
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001682 case P_LINEAR8:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001683 value *= 257;
1684 break;
1685
1686 default:
1687 png_error(display->image->opaque->png_ptr,
1688 "unexpected encoding (internal error)");
1689 break;
1690 }
1691
1692 return value;
1693}
1694
1695static png_uint_32
1696png_colormap_compose(png_image_read_control *display,
1697 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
1698 png_uint_32 background, int encoding)
1699{
1700 /* The file value is composed on the background, the background has the given
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001701 * encoding and so does the result, the file is encoded with P_FILE and the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001702 * file and alpha are 8-bit values. The (output) encoding will always be
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001703 * P_LINEAR or P_sRGB.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001704 */
1705 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
1706 png_uint_32 b = decode_gamma(display, background, encoding);
1707
1708 /* The alpha is always an 8-bit value (it comes from the palette), the value
1709 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1710 */
1711 f = f * alpha + b * (255-alpha);
1712
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001713 if (encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001714 {
1715 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1716 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1717 */
1718 f *= 257; /* Now scaled by 65535 */
1719 f += f >> 16;
1720 f = (f+32768) >> 16;
1721 }
1722
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001723 else /* P_sRGB */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001724 f = PNG_sRGB_FROM_LINEAR(f);
1725
1726 return f;
1727}
1728
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001729/* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001730 * be 8-bit.
1731 */
1732static void
1733png_create_colormap_entry(png_image_read_control *display,
1734 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
1735 png_uint_32 alpha, int encoding)
1736{
1737 png_imagep image = display->image;
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -05001738 const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001739 P_LINEAR : P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001740 const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
1741 (red != green || green != blue);
1742
1743 if (ip > 255)
1744 png_error(image->opaque->png_ptr, "color-map index out of range");
1745
1746 /* Update the cache with whether the file gamma is significantly different
1747 * from sRGB.
1748 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001749 if (encoding == P_FILE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001750 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001751 if (display->file_encoding == P_NOTSET)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001752 set_file_encoding(display);
1753
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001754 /* Note that the cached value may be P_FILE too, but if it is then the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001755 * gamma_to_linear member has been set.
1756 */
1757 encoding = display->file_encoding;
1758 }
1759
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001760 if (encoding == P_FILE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001761 {
1762 png_fixed_point g = display->gamma_to_linear;
1763
1764 red = png_gamma_16bit_correct(red*257, g);
1765 green = png_gamma_16bit_correct(green*257, g);
1766 blue = png_gamma_16bit_correct(blue*257, g);
1767
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001768 if (convert_to_Y != 0 || output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001769 {
1770 alpha *= 257;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001771 encoding = P_LINEAR;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001772 }
1773
1774 else
1775 {
1776 red = PNG_sRGB_FROM_LINEAR(red * 255);
1777 green = PNG_sRGB_FROM_LINEAR(green * 255);
1778 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001779 encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001780 }
1781 }
1782
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001783 else if (encoding == P_LINEAR8)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001784 {
1785 /* This encoding occurs quite frequently in test cases because PngSuite
1786 * includes a gAMA 1.0 chunk with most images.
1787 */
1788 red *= 257;
1789 green *= 257;
1790 blue *= 257;
1791 alpha *= 257;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001792 encoding = P_LINEAR;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001793 }
1794
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001795 else if (encoding == P_sRGB &&
1796 (convert_to_Y != 0 || output_encoding == P_LINEAR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001797 {
1798 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1799 * linear.
1800 */
1801 red = png_sRGB_table[red];
1802 green = png_sRGB_table[green];
1803 blue = png_sRGB_table[blue];
1804 alpha *= 257;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001805 encoding = P_LINEAR;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001806 }
1807
1808 /* This is set if the color isn't gray but the output is. */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001809 if (encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001810 {
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001811 if (convert_to_Y != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001812 {
1813 /* NOTE: these values are copied from png_do_rgb_to_gray */
1814 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
1815 (png_uint_32)2366 * blue;
1816
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001817 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001818 y = (y + 16384) >> 15;
1819
1820 else
1821 {
1822 /* y is scaled by 32768, we need it scaled by 255: */
1823 y = (y + 128) >> 8;
1824 y *= 255;
1825 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
John Bowler6eecfe32015-03-22 19:42:14 -05001826 alpha = PNG_DIV257(alpha);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001827 encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001828 }
1829
1830 blue = red = green = y;
1831 }
1832
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001833 else if (output_encoding == P_sRGB)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001834 {
1835 red = PNG_sRGB_FROM_LINEAR(red * 255);
1836 green = PNG_sRGB_FROM_LINEAR(green * 255);
1837 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1838 alpha = PNG_DIV257(alpha);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001839 encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001840 }
1841 }
1842
1843 if (encoding != output_encoding)
1844 png_error(image->opaque->png_ptr, "bad encoding (internal error)");
1845
1846 /* Store the value. */
1847 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001848# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001849 const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1850 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
1851# else
1852# define afirst 0
1853# endif
1854# ifdef PNG_FORMAT_BGR_SUPPORTED
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -05001855 const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001856# else
1857# define bgr 0
1858# endif
1859
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001860 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001861 {
1862 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
1863
1864 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1865
1866 /* The linear 16-bit values must be pre-multiplied by the alpha channel
1867 * value, if less than 65535 (this is, effectively, composite on black
1868 * if the alpha channel is removed.)
1869 */
1870 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1871 {
1872 case 4:
1873 entry[afirst ? 0 : 3] = (png_uint_16)alpha;
1874 /* FALL THROUGH */
1875
1876 case 3:
1877 if (alpha < 65535)
1878 {
1879 if (alpha > 0)
1880 {
1881 blue = (blue * alpha + 32767U)/65535U;
1882 green = (green * alpha + 32767U)/65535U;
1883 red = (red * alpha + 32767U)/65535U;
1884 }
1885
1886 else
1887 red = green = blue = 0;
1888 }
1889 entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
1890 entry[afirst + 1] = (png_uint_16)green;
1891 entry[afirst + bgr] = (png_uint_16)red;
1892 break;
1893
1894 case 2:
1895 entry[1 ^ afirst] = (png_uint_16)alpha;
1896 /* FALL THROUGH */
1897
1898 case 1:
1899 if (alpha < 65535)
1900 {
1901 if (alpha > 0)
1902 green = (green * alpha + 32767U)/65535U;
1903
1904 else
1905 green = 0;
1906 }
1907 entry[afirst] = (png_uint_16)green;
1908 break;
1909
1910 default:
1911 break;
1912 }
1913 }
1914
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001915 else /* output encoding is P_sRGB */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001916 {
1917 png_bytep entry = png_voidcast(png_bytep, display->colormap);
1918
1919 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1920
1921 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1922 {
1923 case 4:
1924 entry[afirst ? 0 : 3] = (png_byte)alpha;
1925 case 3:
1926 entry[afirst + (2 ^ bgr)] = (png_byte)blue;
1927 entry[afirst + 1] = (png_byte)green;
1928 entry[afirst + bgr] = (png_byte)red;
1929 break;
1930
1931 case 2:
1932 entry[1 ^ afirst] = (png_byte)alpha;
1933 case 1:
1934 entry[afirst] = (png_byte)green;
1935 break;
1936
1937 default:
1938 break;
1939 }
1940 }
1941
1942# ifdef afirst
1943# undef afirst
1944# endif
1945# ifdef bgr
1946# undef bgr
1947# endif
1948 }
1949}
1950
John Bowler7875d532011-11-07 22:33:49 -06001951static int
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001952make_gray_file_colormap(png_image_read_control *display)
1953{
1954 unsigned int i;
1955
1956 for (i=0; i<256; ++i)
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001957 png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001958
1959 return i;
1960}
1961
1962static int
1963make_gray_colormap(png_image_read_control *display)
1964{
1965 unsigned int i;
1966
1967 for (i=0; i<256; ++i)
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001968 png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001969
1970 return i;
1971}
1972#define PNG_GRAY_COLORMAP_ENTRIES 256
1973
1974static int
1975make_ga_colormap(png_image_read_control *display)
1976{
1977 unsigned int i, a;
1978
1979 /* Alpha is retained, the output will be a color-map with entries
1980 * selected by six levels of alpha. One transparent entry, 6 gray
1981 * levels for all the intermediate alpha values, leaving 230 entries
1982 * for the opaque grays. The color-map entries are the six values
1983 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
1984 * relevant entry.
1985 *
1986 * if (alpha > 229) // opaque
1987 * {
1988 * // The 231 entries are selected to make the math below work:
1989 * base = 0;
1990 * entry = (231 * gray + 128) >> 8;
1991 * }
1992 * else if (alpha < 26) // transparent
1993 * {
1994 * base = 231;
1995 * entry = 0;
1996 * }
1997 * else // partially opaque
1998 * {
1999 * base = 226 + 6 * PNG_DIV51(alpha);
2000 * entry = PNG_DIV51(gray);
2001 * }
2002 */
2003 i = 0;
2004 while (i < 231)
2005 {
2006 unsigned int gray = (i * 256 + 115) / 231;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002007 png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002008 }
2009
2010 /* 255 is used here for the component values for consistency with the code
2011 * that undoes premultiplication in pngwrite.c.
2012 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002013 png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002014
2015 for (a=1; a<5; ++a)
2016 {
2017 unsigned int g;
2018
2019 for (g=0; g<6; ++g)
2020 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002021 P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002022 }
2023
2024 return i;
2025}
2026
2027#define PNG_GA_COLORMAP_ENTRIES 256
2028
2029static int
2030make_rgb_colormap(png_image_read_control *display)
2031{
2032 unsigned int i, r;
2033
2034 /* Build a 6x6x6 opaque RGB cube */
2035 for (i=r=0; r<6; ++r)
2036 {
2037 unsigned int g;
2038
2039 for (g=0; g<6; ++g)
2040 {
2041 unsigned int b;
2042
2043 for (b=0; b<6; ++b)
2044 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002045 P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002046 }
2047 }
2048
2049 return i;
2050}
2051
2052#define PNG_RGB_COLORMAP_ENTRIES 216
2053
2054/* Return a palette index to the above palette given three 8-bit sRGB values. */
2055#define PNG_RGB_INDEX(r,g,b) \
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002056 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002057
2058static int
2059png_image_read_colormap(png_voidp argument)
2060{
2061 png_image_read_control *display =
2062 png_voidcast(png_image_read_control*, argument);
2063 const png_imagep image = display->image;
2064
2065 const png_structrp png_ptr = image->opaque->png_ptr;
2066 const png_uint_32 output_format = image->format;
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -05002067 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002068 P_LINEAR : P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002069
2070 unsigned int cmap_entries;
2071 unsigned int output_processing; /* Output processing option */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002072 unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002073
2074 /* Background information; the background color and the index of this color
2075 * in the color-map if it exists (else 256).
2076 */
2077 unsigned int background_index = 256;
2078 png_uint_32 back_r, back_g, back_b;
2079
2080 /* Flags to accumulate things that need to be done to the input. */
2081 int expand_tRNS = 0;
2082
2083 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
2084 * very difficult to do, the results look awful, and it is difficult to see
2085 * what possible use it is because the application can't control the
2086 * color-map.
2087 */
2088 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
2089 png_ptr->num_trans > 0) /* alpha in input */ &&
2090 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
2091 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002092 if (output_encoding == P_LINEAR) /* compose on black */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002093 back_b = back_g = back_r = 0;
2094
2095 else if (display->background == NULL /* no way to remove it */)
2096 png_error(png_ptr,
2097 "a background color must be supplied to remove alpha/transparency");
2098
2099 /* Get a copy of the background color (this avoids repeating the checks
2100 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
2101 * output format.
2102 */
2103 else
2104 {
2105 back_g = display->background->green;
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002106 if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002107 {
2108 back_r = display->background->red;
2109 back_b = display->background->blue;
2110 }
2111 else
2112 back_b = back_r = back_g;
2113 }
2114 }
2115
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002116 else if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002117 back_b = back_r = back_g = 65535;
2118
2119 else
2120 back_b = back_r = back_g = 255;
2121
John Bowler59ae3892013-03-06 22:15:25 -06002122 /* Default the input file gamma if required - this is necessary because
2123 * libpng assumes that if no gamma information is present the data is in the
2124 * output format, but the simplified API deduces the gamma from the input
2125 * format.
2126 */
2127 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0)
2128 {
2129 /* Do this directly, not using the png_colorspace functions, to ensure
2130 * that it happens even if the colorspace is invalid (though probably if
2131 * it is the setting will be ignored) Note that the same thing can be
2132 * achieved at the application interface with png_set_gAMA.
2133 */
2134 if (png_ptr->bit_depth == 16 &&
2135 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
2136 png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR;
2137
2138 else
2139 png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE;
2140
2141 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
2142 }
2143
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002144 /* Decide what to do based on the PNG color type of the input data. The
2145 * utility function png_create_colormap_entry deals with most aspects of the
2146 * output transformations; this code works out how to produce bytes of
2147 * color-map entries from the original format.
2148 */
2149 switch (png_ptr->color_type)
2150 {
2151 case PNG_COLOR_TYPE_GRAY:
2152 if (png_ptr->bit_depth <= 8)
2153 {
2154 /* There at most 256 colors in the output, regardless of
2155 * transparency.
2156 */
2157 unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
2158
2159 cmap_entries = 1U << png_ptr->bit_depth;
2160 if (cmap_entries > image->colormap_entries)
2161 png_error(png_ptr, "gray[8] color-map: too few entries");
2162
2163 step = 255 / (cmap_entries - 1);
2164 output_processing = PNG_CMAP_NONE;
2165
2166 /* If there is a tRNS chunk then this either selects a transparent
2167 * value or, if the output has no alpha, the background color.
2168 */
2169 if (png_ptr->num_trans > 0)
2170 {
2171 trans = png_ptr->trans_color.gray;
2172
2173 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002174 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002175 }
2176
2177 /* png_create_colormap_entry just takes an RGBA and writes the
2178 * corresponding color-map entry using the format from 'image',
2179 * including the required conversion to sRGB or linear as
2180 * appropriate. The input values are always either sRGB (if the
2181 * gamma correction flag is 0) or 0..255 scaled file encoded values
2182 * (if the function must gamma correct them).
2183 */
2184 for (i=val=0; i<cmap_entries; ++i, val += step)
2185 {
2186 /* 'i' is a file value. While this will result in duplicated
2187 * entries for 8-bit non-sRGB encoded files it is necessary to
2188 * have non-gamma corrected values to do tRNS handling.
2189 */
2190 if (i != trans)
2191 png_create_colormap_entry(display, i, val, val, val, 255,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002192 P_FILE/*8-bit with file gamma*/);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002193
2194 /* Else this entry is transparent. The colors don't matter if
2195 * there is an alpha channel (back_alpha == 0), but it does no
2196 * harm to pass them in; the values are not set above so this
2197 * passes in white.
2198 *
2199 * NOTE: this preserves the full precision of the application
2200 * supplied background color when it is used.
2201 */
2202 else
2203 png_create_colormap_entry(display, i, back_r, back_g, back_b,
2204 back_alpha, output_encoding);
2205 }
2206
2207 /* We need libpng to preserve the original encoding. */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002208 data_encoding = P_FILE;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002209
2210 /* The rows from libpng, while technically gray values, are now also
Glenn Randers-Pehrson3efbeca2014-07-21 16:57:30 -05002211 * color-map indices; however, they may need to be expanded to 1
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002212 * byte per pixel. This is what png_set_packing does (i.e., it
2213 * unpacks the bit values into bytes.)
2214 */
2215 if (png_ptr->bit_depth < 8)
2216 png_set_packing(png_ptr);
2217 }
2218
2219 else /* bit depth is 16 */
2220 {
2221 /* The 16-bit input values can be converted directly to 8-bit gamma
2222 * encoded values; however, if a tRNS chunk is present 257 color-map
2223 * entries are required. This means that the extra entry requires
2224 * special processing; add an alpha channel, sacrifice gray level
2225 * 254 and convert transparent (alpha==0) entries to that.
2226 *
2227 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2228 * same time to minimize quality loss. If a tRNS chunk is present
2229 * this means libpng must handle it too; otherwise it is impossible
2230 * to do the exact match on the 16-bit value.
2231 *
2232 * If the output has no alpha channel *and* the background color is
2233 * gray then it is possible to let libpng handle the substitution by
2234 * ensuring that the corresponding gray level matches the background
2235 * color exactly.
2236 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002237 data_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002238
2239 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2240 png_error(png_ptr, "gray[16] color-map: too few entries");
2241
2242 cmap_entries = make_gray_colormap(display);
2243
2244 if (png_ptr->num_trans > 0)
2245 {
2246 unsigned int back_alpha;
2247
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002248 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002249 back_alpha = 0;
2250
2251 else
2252 {
2253 if (back_r == back_g && back_g == back_b)
2254 {
2255 /* Background is gray; no special processing will be
2256 * required.
2257 */
2258 png_color_16 c;
2259 png_uint_32 gray = back_g;
2260
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002261 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002262 {
2263 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2264
2265 /* And make sure the corresponding palette entry
2266 * matches.
2267 */
2268 png_create_colormap_entry(display, gray, back_g, back_g,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002269 back_g, 65535, P_LINEAR);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002270 }
2271
2272 /* The background passed to libpng, however, must be the
2273 * sRGB value.
2274 */
2275 c.index = 0; /*unused*/
2276 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2277
2278 /* NOTE: does this work without expanding tRNS to alpha?
2279 * It should be the color->gray case below apparently
2280 * doesn't.
2281 */
2282 png_set_background_fixed(png_ptr, &c,
2283 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2284 0/*gamma: not used*/);
2285
2286 output_processing = PNG_CMAP_NONE;
2287 break;
2288 }
Glenn Randers-Pehrson8cd78da2015-02-07 09:03:30 -06002289#ifdef __COVERITY__
2290 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
2291 * here.
2292 */
2293 back_alpha = 255;
2294#else
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002295 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
Glenn Randers-Pehrson8cd78da2015-02-07 09:03:30 -06002296#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002297 }
2298
2299 /* output_processing means that the libpng-processed row will be
2300 * 8-bit GA and it has to be processing to single byte color-map
2301 * values. Entry 254 is replaced by either a completely
2302 * transparent entry or by the background color at full
Glenn Randers-Pehrson3efbeca2014-07-21 16:57:30 -05002303 * precision (and the background color is not a simple gray
2304 * level in this case.)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002305 */
2306 expand_tRNS = 1;
2307 output_processing = PNG_CMAP_TRANS;
2308 background_index = 254;
2309
2310 /* And set (overwrite) color-map entry 254 to the actual
2311 * background color at full precision.
2312 */
2313 png_create_colormap_entry(display, 254, back_r, back_g, back_b,
2314 back_alpha, output_encoding);
2315 }
2316
2317 else
2318 output_processing = PNG_CMAP_NONE;
2319 }
2320 break;
2321
2322 case PNG_COLOR_TYPE_GRAY_ALPHA:
2323 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2324 * of 65536 combinations. If, however, the alpha channel is to be
2325 * removed there are only 256 possibilities if the background is gray.
2326 * (Otherwise there is a subset of the 65536 possibilities defined by
2327 * the triangle between black, white and the background color.)
2328 *
2329 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2330 * worry about tRNS matching - tRNS is ignored if there is an alpha
2331 * channel.
2332 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002333 data_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002334
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002335 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002336 {
2337 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2338 png_error(png_ptr, "gray+alpha color-map: too few entries");
2339
2340 cmap_entries = make_ga_colormap(display);
2341
2342 background_index = PNG_CMAP_GA_BACKGROUND;
2343 output_processing = PNG_CMAP_GA;
2344 }
2345
2346 else /* alpha is removed */
2347 {
2348 /* Alpha must be removed as the PNG data is processed when the
2349 * background is a color because the G and A channels are
2350 * independent and the vector addition (non-parallel vectors) is a
2351 * 2-D problem.
2352 *
2353 * This can be reduced to the same algorithm as above by making a
2354 * colormap containing gray levels (for the opaque grays), a
2355 * background entry (for a transparent pixel) and a set of four six
2356 * level color values, one set for each intermediate alpha value.
2357 * See the comments in make_ga_colormap for how this works in the
2358 * per-pixel processing.
2359 *
2360 * If the background is gray, however, we only need a 256 entry gray
2361 * level color map. It is sufficient to make the entry generated
2362 * for the background color be exactly the color specified.
2363 */
2364 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2365 (back_r == back_g && back_g == back_b))
2366 {
2367 /* Background is gray; no special processing will be required. */
2368 png_color_16 c;
2369 png_uint_32 gray = back_g;
2370
2371 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2372 png_error(png_ptr, "gray-alpha color-map: too few entries");
2373
2374 cmap_entries = make_gray_colormap(display);
2375
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002376 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002377 {
2378 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2379
2380 /* And make sure the corresponding palette entry matches. */
2381 png_create_colormap_entry(display, gray, back_g, back_g,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002382 back_g, 65535, P_LINEAR);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002383 }
2384
2385 /* The background passed to libpng, however, must be the sRGB
2386 * value.
2387 */
2388 c.index = 0; /*unused*/
2389 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2390
2391 png_set_background_fixed(png_ptr, &c,
2392 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2393 0/*gamma: not used*/);
2394
2395 output_processing = PNG_CMAP_NONE;
2396 }
2397
2398 else
2399 {
2400 png_uint_32 i, a;
2401
2402 /* This is the same as png_make_ga_colormap, above, except that
2403 * the entries are all opaque.
2404 */
2405 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2406 png_error(png_ptr, "ga-alpha color-map: too few entries");
2407
2408 i = 0;
2409 while (i < 231)
2410 {
2411 png_uint_32 gray = (i * 256 + 115) / 231;
2412 png_create_colormap_entry(display, i++, gray, gray, gray,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002413 255, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002414 }
2415
2416 /* NOTE: this preserves the full precision of the application
2417 * background color.
2418 */
2419 background_index = i;
2420 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
Glenn Randers-Pehrson8cd78da2015-02-07 09:03:30 -06002421#ifdef __COVERITY__
2422 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
2423 * here.
2424 */ 255U,
2425#else
2426 output_encoding == P_LINEAR ? 65535U : 255U,
2427#endif
2428 output_encoding);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002429
2430 /* For non-opaque input composite on the sRGB background - this
2431 * requires inverting the encoding for each component. The input
2432 * is still converted to the sRGB encoding because this is a
2433 * reasonable approximate to the logarithmic curve of human
2434 * visual sensitivity, at least over the narrow range which PNG
2435 * represents. Consequently 'G' is always sRGB encoded, while
2436 * 'A' is linear. We need the linear background colors.
2437 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002438 if (output_encoding == P_sRGB) /* else already linear */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002439 {
2440 /* This may produce a value not exactly matching the
2441 * background, but that's ok because these numbers are only
2442 * used when alpha != 0
2443 */
2444 back_r = png_sRGB_table[back_r];
2445 back_g = png_sRGB_table[back_g];
2446 back_b = png_sRGB_table[back_b];
2447 }
2448
2449 for (a=1; a<5; ++a)
2450 {
2451 unsigned int g;
2452
2453 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2454 * by an 8-bit alpha value (0..255).
2455 */
2456 png_uint_32 alpha = 51 * a;
2457 png_uint_32 back_rx = (255-alpha) * back_r;
2458 png_uint_32 back_gx = (255-alpha) * back_g;
2459 png_uint_32 back_bx = (255-alpha) * back_b;
2460
2461 for (g=0; g<6; ++g)
2462 {
2463 png_uint_32 gray = png_sRGB_table[g*51] * alpha;
2464
2465 png_create_colormap_entry(display, i++,
2466 PNG_sRGB_FROM_LINEAR(gray + back_rx),
2467 PNG_sRGB_FROM_LINEAR(gray + back_gx),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002468 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002469 }
2470 }
2471
2472 cmap_entries = i;
2473 output_processing = PNG_CMAP_GA;
2474 }
2475 }
2476 break;
2477
2478 case PNG_COLOR_TYPE_RGB:
2479 case PNG_COLOR_TYPE_RGB_ALPHA:
2480 /* Exclude the case where the output is gray; we can always handle this
2481 * with the cases above.
2482 */
2483 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
2484 {
2485 /* The color-map will be grayscale, so we may as well convert the
2486 * input RGB values to a simple grayscale and use the grayscale
2487 * code above.
2488 *
2489 * NOTE: calling this apparently damages the recognition of the
2490 * transparent color in background color handling; call
2491 * png_set_tRNS_to_alpha before png_set_background_fixed.
2492 */
2493 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2494 -1);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002495 data_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002496
2497 /* The output will now be one or two 8-bit gray or gray+alpha
2498 * channels. The more complex case arises when the input has alpha.
2499 */
2500 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2501 png_ptr->num_trans > 0) &&
2502 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2503 {
2504 /* Both input and output have an alpha channel, so no background
2505 * processing is required; just map the GA bytes to the right
2506 * color-map entry.
2507 */
2508 expand_tRNS = 1;
2509
2510 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2511 png_error(png_ptr, "rgb[ga] color-map: too few entries");
2512
2513 cmap_entries = make_ga_colormap(display);
2514 background_index = PNG_CMAP_GA_BACKGROUND;
2515 output_processing = PNG_CMAP_GA;
2516 }
2517
2518 else
2519 {
2520 /* Either the input or the output has no alpha channel, so there
2521 * will be no non-opaque pixels in the color-map; it will just be
2522 * grayscale.
2523 */
2524 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2525 png_error(png_ptr, "rgb[gray] color-map: too few entries");
2526
2527 /* Ideally this code would use libpng to do the gamma correction,
2528 * but if an input alpha channel is to be removed we will hit the
2529 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2530 * correction bug). Fix this by dropping the gamma correction in
2531 * this case and doing it in the palette; this will result in
2532 * duplicate palette entries, but that's better than the
2533 * alternative of double gamma correction.
2534 */
2535 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2536 png_ptr->num_trans > 0) &&
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002537 png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002538 {
2539 cmap_entries = make_gray_file_colormap(display);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002540 data_encoding = P_FILE;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002541 }
2542
2543 else
2544 cmap_entries = make_gray_colormap(display);
2545
2546 /* But if the input has alpha or transparency it must be removed
2547 */
2548 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2549 png_ptr->num_trans > 0)
2550 {
2551 png_color_16 c;
2552 png_uint_32 gray = back_g;
2553
2554 /* We need to ensure that the application background exists in
2555 * the colormap and that completely transparent pixels map to
2556 * it. Achieve this simply by ensuring that the entry
2557 * selected for the background really is the background color.
2558 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002559 if (data_encoding == P_FILE) /* from the fixup above */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002560 {
2561 /* The app supplied a gray which is in output_encoding, we
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002562 * need to convert it to a value of the input (P_FILE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002563 * encoding then set this palette entry to the required
2564 * output encoding.
2565 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002566 if (output_encoding == P_sRGB)
2567 gray = png_sRGB_table[gray]; /* now P_LINEAR */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002568
2569 gray = PNG_DIV257(png_gamma_16bit_correct(gray,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002570 png_ptr->colorspace.gamma)); /* now P_FILE */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002571
2572 /* And make sure the corresponding palette entry contains
2573 * exactly the required sRGB value.
2574 */
2575 png_create_colormap_entry(display, gray, back_g, back_g,
2576 back_g, 0/*unused*/, output_encoding);
2577 }
2578
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002579 else if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002580 {
2581 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2582
2583 /* And make sure the corresponding palette entry matches.
2584 */
2585 png_create_colormap_entry(display, gray, back_g, back_g,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002586 back_g, 0/*unused*/, P_LINEAR);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002587 }
2588
2589 /* The background passed to libpng, however, must be the
2590 * output (normally sRGB) value.
2591 */
2592 c.index = 0; /*unused*/
2593 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2594
2595 /* NOTE: the following is apparently a bug in libpng. Without
2596 * it the transparent color recognition in
2597 * png_set_background_fixed seems to go wrong.
2598 */
2599 expand_tRNS = 1;
2600 png_set_background_fixed(png_ptr, &c,
2601 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2602 0/*gamma: not used*/);
2603 }
2604
2605 output_processing = PNG_CMAP_NONE;
2606 }
2607 }
2608
2609 else /* output is color */
2610 {
2611 /* We could use png_quantize here so long as there is no transparent
2612 * color or alpha; png_quantize ignores alpha. Easier overall just
2613 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2614 * Consequently we always want libpng to produce sRGB data.
2615 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002616 data_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002617
2618 /* Is there any transparency or alpha? */
2619 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2620 png_ptr->num_trans > 0)
2621 {
2622 /* Is there alpha in the output too? If so all four channels are
2623 * processed into a special RGB cube with alpha support.
2624 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002625 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002626 {
2627 png_uint_32 r;
2628
2629 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2630 png_error(png_ptr, "rgb+alpha color-map: too few entries");
2631
2632 cmap_entries = make_rgb_colormap(display);
2633
2634 /* Add a transparent entry. */
2635 png_create_colormap_entry(display, cmap_entries, 255, 255,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002636 255, 0, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002637
2638 /* This is stored as the background index for the processing
2639 * algorithm.
2640 */
2641 background_index = cmap_entries++;
2642
2643 /* Add 27 r,g,b entries each with alpha 0.5. */
2644 for (r=0; r<256; r = (r << 1) | 0x7f)
2645 {
2646 png_uint_32 g;
2647
2648 for (g=0; g<256; g = (g << 1) | 0x7f)
2649 {
2650 png_uint_32 b;
2651
2652 /* This generates components with the values 0, 127 and
2653 * 255
2654 */
2655 for (b=0; b<256; b = (b << 1) | 0x7f)
2656 png_create_colormap_entry(display, cmap_entries++,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002657 r, g, b, 128, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002658 }
2659 }
2660
2661 expand_tRNS = 1;
2662 output_processing = PNG_CMAP_RGB_ALPHA;
2663 }
2664
2665 else
2666 {
2667 /* Alpha/transparency must be removed. The background must
2668 * exist in the color map (achieved by setting adding it after
2669 * the 666 color-map). If the standard processing code will
2670 * pick up this entry automatically that's all that is
2671 * required; libpng can be called to do the background
2672 * processing.
2673 */
2674 unsigned int sample_size =
2675 PNG_IMAGE_SAMPLE_SIZE(output_format);
2676 png_uint_32 r, g, b; /* sRGB background */
2677
2678 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2679 png_error(png_ptr, "rgb-alpha color-map: too few entries");
2680
2681 cmap_entries = make_rgb_colormap(display);
2682
2683 png_create_colormap_entry(display, cmap_entries, back_r,
2684 back_g, back_b, 0/*unused*/, output_encoding);
2685
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002686 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002687 {
2688 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2689 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2690 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2691 }
2692
2693 else
2694 {
2695 r = back_r;
2696 g = back_g;
2697 b = back_g;
2698 }
2699
2700 /* Compare the newly-created color-map entry with the one the
2701 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2702 * match, add the new one and set this as the background
2703 * index.
2704 */
2705 if (memcmp((png_const_bytep)display->colormap +
2706 sample_size * cmap_entries,
2707 (png_const_bytep)display->colormap +
2708 sample_size * PNG_RGB_INDEX(r,g,b),
2709 sample_size) != 0)
2710 {
2711 /* The background color must be added. */
2712 background_index = cmap_entries++;
2713
2714 /* Add 27 r,g,b entries each with created by composing with
2715 * the background at alpha 0.5.
2716 */
2717 for (r=0; r<256; r = (r << 1) | 0x7f)
2718 {
2719 for (g=0; g<256; g = (g << 1) | 0x7f)
2720 {
2721 /* This generates components with the values 0, 127
2722 * and 255
2723 */
2724 for (b=0; b<256; b = (b << 1) | 0x7f)
2725 png_create_colormap_entry(display, cmap_entries++,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002726 png_colormap_compose(display, r, P_sRGB, 128,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002727 back_r, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002728 png_colormap_compose(display, g, P_sRGB, 128,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002729 back_g, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002730 png_colormap_compose(display, b, P_sRGB, 128,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002731 back_b, output_encoding),
2732 0/*unused*/, output_encoding);
2733 }
2734 }
2735
2736 expand_tRNS = 1;
2737 output_processing = PNG_CMAP_RGB_ALPHA;
2738 }
2739
2740 else /* background color is in the standard color-map */
2741 {
2742 png_color_16 c;
2743
2744 c.index = 0; /*unused*/
2745 c.red = (png_uint_16)back_r;
2746 c.gray = c.green = (png_uint_16)back_g;
2747 c.blue = (png_uint_16)back_b;
2748
2749 png_set_background_fixed(png_ptr, &c,
2750 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2751 0/*gamma: not used*/);
2752
2753 output_processing = PNG_CMAP_RGB;
2754 }
2755 }
2756 }
2757
2758 else /* no alpha or transparency in the input */
2759 {
2760 /* Alpha in the output is irrelevant, simply map the opaque input
2761 * pixels to the 6x6x6 color-map.
2762 */
2763 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2764 png_error(png_ptr, "rgb color-map: too few entries");
2765
2766 cmap_entries = make_rgb_colormap(display);
2767 output_processing = PNG_CMAP_RGB;
2768 }
2769 }
2770 break;
2771
2772 case PNG_COLOR_TYPE_PALETTE:
2773 /* It's already got a color-map. It may be necessary to eliminate the
2774 * tRNS entries though.
2775 */
2776 {
2777 unsigned int num_trans = png_ptr->num_trans;
2778 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2779 png_const_colorp colormap = png_ptr->palette;
2780 const int do_background = trans != NULL &&
2781 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2782 unsigned int i;
2783
2784 /* Just in case: */
2785 if (trans == NULL)
2786 num_trans = 0;
2787
2788 output_processing = PNG_CMAP_NONE;
Glenn Randers-Pehrson3efbeca2014-07-21 16:57:30 -05002789 data_encoding = P_FILE; /* Don't change from color-map indices */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002790 cmap_entries = png_ptr->num_palette;
2791 if (cmap_entries > 256)
2792 cmap_entries = 256;
2793
2794 if (cmap_entries > image->colormap_entries)
2795 png_error(png_ptr, "palette color-map: too few entries");
2796
2797 for (i=0; i < cmap_entries; ++i)
2798 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002799 if (do_background != 0 && i < num_trans && trans[i] < 255)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002800 {
2801 if (trans[i] == 0)
2802 png_create_colormap_entry(display, i, back_r, back_g,
2803 back_b, 0, output_encoding);
2804
2805 else
2806 {
2807 /* Must compose the PNG file color in the color-map entry
2808 * on the sRGB color in 'back'.
2809 */
2810 png_create_colormap_entry(display, i,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002811 png_colormap_compose(display, colormap[i].red, P_FILE,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002812 trans[i], back_r, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002813 png_colormap_compose(display, colormap[i].green, P_FILE,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002814 trans[i], back_g, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002815 png_colormap_compose(display, colormap[i].blue, P_FILE,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002816 trans[i], back_b, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002817 output_encoding == P_LINEAR ? trans[i] * 257U :
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002818 trans[i],
2819 output_encoding);
2820 }
2821 }
2822
2823 else
2824 png_create_colormap_entry(display, i, colormap[i].red,
2825 colormap[i].green, colormap[i].blue,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002826 i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002827 }
2828
Glenn Randers-Pehrson3efbeca2014-07-21 16:57:30 -05002829 /* The PNG data may have indices packed in fewer than 8 bits, it
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002830 * must be expanded if so.
2831 */
2832 if (png_ptr->bit_depth < 8)
2833 png_set_packing(png_ptr);
2834 }
2835 break;
2836
2837 default:
2838 png_error(png_ptr, "invalid PNG color type");
2839 /*NOT REACHED*/
2840 break;
2841 }
2842
2843 /* Now deal with the output processing */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002844 if (expand_tRNS != 0 && png_ptr->num_trans > 0 &&
2845 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002846 png_set_tRNS_to_alpha(png_ptr);
2847
2848 switch (data_encoding)
2849 {
2850 default:
2851 png_error(png_ptr, "bad data option (internal error)");
2852 break;
2853
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002854 case P_sRGB:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002855 /* Change to 8-bit sRGB */
2856 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
2857 /* FALL THROUGH */
2858
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002859 case P_FILE:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002860 if (png_ptr->bit_depth > 8)
2861 png_set_scale_16(png_ptr);
2862 break;
2863 }
2864
2865 if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2866 png_error(png_ptr, "color map overflow (BAD internal error)");
2867
2868 image->colormap_entries = cmap_entries;
2869
2870 /* Double check using the recorded background index */
2871 switch (output_processing)
2872 {
2873 case PNG_CMAP_NONE:
2874 if (background_index != PNG_CMAP_NONE_BACKGROUND)
2875 goto bad_background;
2876 break;
2877
2878 case PNG_CMAP_GA:
2879 if (background_index != PNG_CMAP_GA_BACKGROUND)
2880 goto bad_background;
2881 break;
2882
2883 case PNG_CMAP_TRANS:
2884 if (background_index >= cmap_entries ||
2885 background_index != PNG_CMAP_TRANS_BACKGROUND)
2886 goto bad_background;
2887 break;
2888
2889 case PNG_CMAP_RGB:
2890 if (background_index != PNG_CMAP_RGB_BACKGROUND)
2891 goto bad_background;
2892 break;
2893
2894 case PNG_CMAP_RGB_ALPHA:
2895 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
2896 goto bad_background;
2897 break;
2898
2899 default:
2900 png_error(png_ptr, "bad processing option (internal error)");
2901
2902 bad_background:
2903 png_error(png_ptr, "bad background index (internal error)");
2904 }
2905
2906 display->colormap_processing = output_processing;
2907
2908 return 1/*ok*/;
2909}
2910
2911/* The final part of the color-map read called from png_image_finish_read. */
2912static int
2913png_image_read_and_map(png_voidp argument)
John Bowler7875d532011-11-07 22:33:49 -06002914{
John Bowler4fa96a42011-11-16 16:39:16 -06002915 png_image_read_control *display = png_voidcast(png_image_read_control*,
2916 argument);
John Bowler7875d532011-11-07 22:33:49 -06002917 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06002918 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler4f67e402011-12-28 08:43:37 -06002919 int passes;
John Bowler7875d532011-11-07 22:33:49 -06002920
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002921 /* Called when the libpng data must be transformed into the color-mapped
2922 * form. There is a local row buffer in display->local and this routine must
2923 * do the interlace handling.
2924 */
2925 switch (png_ptr->interlaced)
John Bowler7875d532011-11-07 22:33:49 -06002926 {
2927 case PNG_INTERLACE_NONE:
2928 passes = 1;
2929 break;
2930
2931 case PNG_INTERLACE_ADAM7:
2932 passes = PNG_INTERLACE_ADAM7_PASSES;
2933 break;
2934
2935 default:
2936 png_error(png_ptr, "unknown interlace type");
2937 }
2938
2939 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002940 png_uint_32 height = image->height;
2941 png_uint_32 width = image->width;
2942 int proc = display->colormap_processing;
2943 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
2944 ptrdiff_t step_row = display->row_bytes;
John Bowler7875d532011-11-07 22:33:49 -06002945 int pass;
2946
2947 for (pass = 0; pass < passes; ++pass)
2948 {
John Bowler7875d532011-11-07 22:33:49 -06002949 unsigned int startx, stepx, stepy;
2950 png_uint_32 y;
2951
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002952 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
John Bowler7875d532011-11-07 22:33:49 -06002953 {
2954 /* The row may be empty for a short image: */
2955 if (PNG_PASS_COLS(width, pass) == 0)
2956 continue;
2957
2958 startx = PNG_PASS_START_COL(pass);
2959 stepx = PNG_PASS_COL_OFFSET(pass);
2960 y = PNG_PASS_START_ROW(pass);
John Bowler18c5cfa2011-11-16 14:26:34 -06002961 stepy = PNG_PASS_ROW_OFFSET(pass);
John Bowler7875d532011-11-07 22:33:49 -06002962 }
2963
2964 else
2965 {
2966 y = 0;
2967 startx = 0;
2968 stepx = stepy = 1;
2969 }
John Bowler18c5cfa2011-11-16 14:26:34 -06002970
John Bowler7875d532011-11-07 22:33:49 -06002971 for (; y<height; y += stepy)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002972 {
2973 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
2974 png_bytep outrow = first_row + y * step_row;
2975 png_const_bytep end_row = outrow + width;
2976
2977 /* Read read the libpng data into the temporary buffer. */
2978 png_read_row(png_ptr, inrow, NULL);
2979
2980 /* Now process the row according to the processing option, note
2981 * that the caller verifies that the format of the libpng output
2982 * data is as required.
2983 */
2984 outrow += startx;
2985 switch (proc)
John Bowler7875d532011-11-07 22:33:49 -06002986 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002987 case PNG_CMAP_GA:
2988 for (; outrow < end_row; outrow += stepx)
John Bowler5bc90382012-01-23 22:43:22 -06002989 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002990 /* The data is always in the PNG order */
2991 unsigned int gray = *inrow++;
2992 unsigned int alpha = *inrow++;
2993 unsigned int entry;
John Bowler5bc90382012-01-23 22:43:22 -06002994
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002995 /* NOTE: this code is copied as a comment in
2996 * make_ga_colormap above. Please update the
2997 * comment if you change this code!
2998 */
2999 if (alpha > 229) /* opaque */
John Bowler5bc90382012-01-23 22:43:22 -06003000 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003001 entry = (231 * gray + 128) >> 8;
John Bowler5bc90382012-01-23 22:43:22 -06003002 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003003 else if (alpha < 26) /* transparent */
3004 {
3005 entry = 231;
3006 }
3007 else /* partially opaque */
3008 {
3009 entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
3010 }
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06003011
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003012 *outrow = (png_byte)entry;
3013 }
3014 break;
3015
3016 case PNG_CMAP_TRANS:
3017 for (; outrow < end_row; outrow += stepx)
3018 {
3019 png_byte gray = *inrow++;
3020 png_byte alpha = *inrow++;
3021
3022 if (alpha == 0)
3023 *outrow = PNG_CMAP_TRANS_BACKGROUND;
3024
3025 else if (gray != PNG_CMAP_TRANS_BACKGROUND)
3026 *outrow = gray;
3027
3028 else
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05003029 *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003030 }
3031 break;
3032
3033 case PNG_CMAP_RGB:
3034 for (; outrow < end_row; outrow += stepx)
3035 {
3036 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
3037 inrow += 3;
3038 }
3039 break;
3040
3041 case PNG_CMAP_RGB_ALPHA:
3042 for (; outrow < end_row; outrow += stepx)
3043 {
3044 unsigned int alpha = inrow[3];
3045
3046 /* Because the alpha entries only hold alpha==0.5 values
3047 * split the processing at alpha==0.25 (64) and 0.75
3048 * (196).
3049 */
3050
3051 if (alpha >= 196)
3052 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
3053 inrow[2]);
3054
3055 else if (alpha < 64)
3056 *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
3057
3058 else
3059 {
3060 /* Likewise there are three entries for each of r, g
3061 * and b. We could select the entry by popcount on
3062 * the top two bits on those architectures that
3063 * support it, this is what the code below does,
3064 * crudely.
3065 */
3066 unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
3067
3068 /* Here are how the values map:
3069 *
3070 * 0x00 .. 0x3f -> 0
3071 * 0x40 .. 0xbf -> 1
3072 * 0xc0 .. 0xff -> 2
3073 *
3074 * So, as above with the explicit alpha checks, the
3075 * breakpoints are at 64 and 196.
3076 */
3077 if (inrow[0] & 0x80) back_i += 9; /* red */
3078 if (inrow[0] & 0x40) back_i += 9;
3079 if (inrow[0] & 0x80) back_i += 3; /* green */
3080 if (inrow[0] & 0x40) back_i += 3;
3081 if (inrow[0] & 0x80) back_i += 1; /* blue */
3082 if (inrow[0] & 0x40) back_i += 1;
3083
3084 *outrow = (png_byte)back_i;
3085 }
3086
3087 inrow += 4;
3088 }
3089 break;
3090
3091 default:
3092 break;
3093 }
3094 }
3095 }
3096 }
3097
3098 return 1;
3099}
3100
3101static int
3102png_image_read_colormapped(png_voidp argument)
3103{
3104 png_image_read_control *display = png_voidcast(png_image_read_control*,
3105 argument);
3106 png_imagep image = display->image;
3107 png_controlp control = image->opaque;
3108 png_structrp png_ptr = control->png_ptr;
3109 png_inforp info_ptr = control->info_ptr;
3110
3111 int passes = 0; /* As a flag */
3112
3113 PNG_SKIP_CHUNKS(png_ptr);
3114
3115 /* Update the 'info' structure and make sure the result is as required; first
3116 * make sure to turn on the interlace handling if it will be required
3117 * (because it can't be turned on *after* the call to png_read_update_info!)
3118 */
3119 if (display->colormap_processing == PNG_CMAP_NONE)
3120 passes = png_set_interlace_handling(png_ptr);
3121
3122 png_read_update_info(png_ptr, info_ptr);
3123
3124 /* The expected output can be deduced from the colormap_processing option. */
3125 switch (display->colormap_processing)
3126 {
3127 case PNG_CMAP_NONE:
3128 /* Output must be one channel and one byte per pixel, the output
3129 * encoding can be anything.
3130 */
3131 if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
3132 info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
3133 info_ptr->bit_depth == 8)
3134 break;
3135
3136 goto bad_output;
3137
3138 case PNG_CMAP_TRANS:
3139 case PNG_CMAP_GA:
3140 /* Output must be two channels and the 'G' one must be sRGB, the latter
3141 * can be checked with an exact number because it should have been set
3142 * to this number above!
3143 */
3144 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
3145 info_ptr->bit_depth == 8 &&
3146 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3147 image->colormap_entries == 256)
3148 break;
3149
3150 goto bad_output;
3151
3152 case PNG_CMAP_RGB:
3153 /* Output must be 8-bit sRGB encoded RGB */
3154 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
3155 info_ptr->bit_depth == 8 &&
3156 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3157 image->colormap_entries == 216)
3158 break;
3159
3160 goto bad_output;
3161
3162 case PNG_CMAP_RGB_ALPHA:
3163 /* Output must be 8-bit sRGB encoded RGBA */
3164 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
3165 info_ptr->bit_depth == 8 &&
3166 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3167 image->colormap_entries == 244 /* 216 + 1 + 27 */)
3168 break;
3169
3170 /* goto bad_output; */
3171 /* FALL THROUGH */
3172
3173 default:
3174 bad_output:
3175 png_error(png_ptr, "bad color-map processing (internal error)");
3176 }
3177
3178 /* Now read the rows. Do this here if it is possible to read directly into
3179 * the output buffer, otherwise allocate a local row buffer of the maximum
3180 * size libpng requires and call the relevant processing routine safely.
3181 */
3182 {
3183 png_voidp first_row = display->buffer;
3184 ptrdiff_t row_bytes = display->row_stride;
3185
3186 /* The following expression is designed to work correctly whether it gives
3187 * a signed or an unsigned result.
3188 */
3189 if (row_bytes < 0)
3190 {
3191 char *ptr = png_voidcast(char*, first_row);
3192 ptr += (image->height-1) * (-row_bytes);
3193 first_row = png_voidcast(png_voidp, ptr);
3194 }
3195
3196 display->first_row = first_row;
3197 display->row_bytes = row_bytes;
3198 }
3199
3200 if (passes == 0)
3201 {
3202 int result;
3203 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3204
3205 display->local_row = row;
3206 result = png_safe_execute(image, png_image_read_and_map, display);
3207 display->local_row = NULL;
3208 png_free(png_ptr, row);
3209
3210 return result;
3211 }
3212
3213 else
3214 {
3215 png_alloc_size_t row_bytes = display->row_bytes;
3216
3217 while (--passes >= 0)
3218 {
3219 png_uint_32 y = image->height;
3220 png_bytep row = png_voidcast(png_bytep, display->first_row);
3221
3222 while (y-- > 0)
3223 {
3224 png_read_row(png_ptr, row, NULL);
3225 row += row_bytes;
3226 }
3227 }
3228
3229 return 1;
3230 }
3231}
3232
3233/* Just the row reading part of png_image_read. */
3234static int
3235png_image_read_composite(png_voidp argument)
3236{
3237 png_image_read_control *display = png_voidcast(png_image_read_control*,
3238 argument);
3239 png_imagep image = display->image;
3240 png_structrp png_ptr = image->opaque->png_ptr;
3241 int passes;
3242
3243 switch (png_ptr->interlaced)
3244 {
3245 case PNG_INTERLACE_NONE:
3246 passes = 1;
3247 break;
3248
3249 case PNG_INTERLACE_ADAM7:
3250 passes = PNG_INTERLACE_ADAM7_PASSES;
3251 break;
3252
3253 default:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003254 png_error(png_ptr, "unknown interlace type");
3255 }
3256
3257 {
3258 png_uint_32 height = image->height;
3259 png_uint_32 width = image->width;
3260 ptrdiff_t step_row = display->row_bytes;
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -05003261 unsigned int channels =
Glenn Randers-Pehrson6ef579d2015-01-27 07:02:46 -06003262 (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003263 int pass;
3264
3265 for (pass = 0; pass < passes; ++pass)
3266 {
3267 unsigned int startx, stepx, stepy;
3268 png_uint_32 y;
3269
3270 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3271 {
3272 /* The row may be empty for a short image: */
3273 if (PNG_PASS_COLS(width, pass) == 0)
3274 continue;
3275
3276 startx = PNG_PASS_START_COL(pass) * channels;
3277 stepx = PNG_PASS_COL_OFFSET(pass) * channels;
3278 y = PNG_PASS_START_ROW(pass);
3279 stepy = PNG_PASS_ROW_OFFSET(pass);
3280 }
3281
3282 else
3283 {
3284 y = 0;
3285 startx = 0;
3286 stepx = channels;
3287 stepy = 1;
3288 }
3289
3290 for (; y<height; y += stepy)
3291 {
3292 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
3293 png_bytep outrow;
3294 png_const_bytep end_row;
3295
3296 /* Read the row, which is packed: */
3297 png_read_row(png_ptr, inrow, NULL);
3298
3299 outrow = png_voidcast(png_bytep, display->first_row);
3300 outrow += y * step_row;
3301 end_row = outrow + width * channels;
3302
3303 /* Now do the composition on each pixel in this row. */
3304 outrow += startx;
3305 for (; outrow < end_row; outrow += stepx)
3306 {
3307 png_byte alpha = inrow[channels];
3308
3309 if (alpha > 0) /* else no change to the output */
3310 {
3311 unsigned int c;
3312
3313 for (c=0; c<channels; ++c)
3314 {
3315 png_uint_32 component = inrow[c];
3316
3317 if (alpha < 255) /* else just use component */
3318 {
3319 /* This is PNG_OPTIMIZED_ALPHA, the component value
3320 * is a linear 8-bit value. Combine this with the
3321 * current outrow[c] value which is sRGB encoded.
3322 * Arithmetic here is 16-bits to preserve the output
3323 * values correctly.
3324 */
3325 component *= 257*255; /* =65535 */
3326 component += (255-alpha)*png_sRGB_table[outrow[c]];
3327
3328 /* So 'component' is scaled by 255*65535 and is
3329 * therefore appropriate for the sRGB to linear
3330 * conversion table.
3331 */
3332 component = PNG_sRGB_FROM_LINEAR(component);
3333 }
3334
3335 outrow[c] = (png_byte)component;
3336 }
John Bowler7875d532011-11-07 22:33:49 -06003337 }
3338
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003339 inrow += channels+1; /* components and alpha channel */
John Bowler7875d532011-11-07 22:33:49 -06003340 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003341 }
John Bowler7875d532011-11-07 22:33:49 -06003342 }
3343 }
3344
3345 return 1;
3346}
3347
John Bowler18c5cfa2011-11-16 14:26:34 -06003348/* The do_local_background case; called when all the following transforms are to
3349 * be done:
3350 *
3351 * PNG_RGB_TO_GRAY
3352 * PNG_COMPOSITE
3353 * PNG_GAMMA
3354 *
Glenn Randers-Pehrson149eea22014-03-17 10:58:02 -05003355 * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and
John Bowler18c5cfa2011-11-16 14:26:34 -06003356 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
Glenn Randers-Pehrson149eea22014-03-17 10:58:02 -05003357 * correction. The fix-up is to prevent the PNG_COMPOSITE operation from
3358 * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha
3359 * row and handles the removal or pre-multiplication of the alpha channel.
John Bowler18c5cfa2011-11-16 14:26:34 -06003360 */
3361static int
3362png_image_read_background(png_voidp argument)
3363{
John Bowler4fa96a42011-11-16 16:39:16 -06003364 png_image_read_control *display = png_voidcast(png_image_read_control*,
3365 argument);
John Bowler18c5cfa2011-11-16 14:26:34 -06003366 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06003367 png_structrp png_ptr = image->opaque->png_ptr;
3368 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler18c5cfa2011-11-16 14:26:34 -06003369 png_uint_32 height = image->height;
3370 png_uint_32 width = image->width;
John Bowler4f67e402011-12-28 08:43:37 -06003371 int pass, passes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003372
3373 /* Double check the convoluted logic below. We expect to get here with
3374 * libpng doing rgb to gray and gamma correction but background processing
3375 * left to the png_image_read_background function. The rows libpng produce
3376 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
3377 */
3378 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
3379 png_error(png_ptr, "lost rgb to gray");
3380
3381 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
3382 png_error(png_ptr, "unexpected compose");
3383
John Bowler18c5cfa2011-11-16 14:26:34 -06003384 if (png_get_channels(png_ptr, info_ptr) != 2)
3385 png_error(png_ptr, "lost/gained channels");
3386
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003387 /* Expect the 8-bit case to always remove the alpha channel */
3388 if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
3389 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
3390 png_error(png_ptr, "unexpected 8-bit transformation");
3391
3392 switch (png_ptr->interlaced)
John Bowler18c5cfa2011-11-16 14:26:34 -06003393 {
3394 case PNG_INTERLACE_NONE:
3395 passes = 1;
3396 break;
3397
3398 case PNG_INTERLACE_ADAM7:
3399 passes = PNG_INTERLACE_ADAM7_PASSES;
3400 break;
3401
3402 default:
3403 png_error(png_ptr, "unknown interlace type");
3404 }
3405
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06003406 /* Use direct access to info_ptr here because otherwise the simplified API
3407 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
3408 * checking the value after libpng expansions, not the original value in the
3409 * PNG.
3410 */
3411 switch (info_ptr->bit_depth)
John Bowler18c5cfa2011-11-16 14:26:34 -06003412 {
3413 default:
3414 png_error(png_ptr, "unexpected bit depth");
3415 break;
3416
3417 case 8:
3418 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
Glenn Randers-Pehrson32440202013-08-21 18:01:32 -05003419 * to be removed by composing on a background: either the row if
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003420 * display->background is NULL or display->background->green if not.
John Bowler18c5cfa2011-11-16 14:26:34 -06003421 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3422 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003423 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003424 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3425 ptrdiff_t step_row = display->row_bytes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003426
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003427 for (pass = 0; pass < passes; ++pass)
John Bowler18c5cfa2011-11-16 14:26:34 -06003428 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003429 png_bytep row = png_voidcast(png_bytep,
3430 display->first_row);
3431 unsigned int startx, stepx, stepy;
3432 png_uint_32 y;
John Bowler18c5cfa2011-11-16 14:26:34 -06003433
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003434 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3435 {
3436 /* The row may be empty for a short image: */
3437 if (PNG_PASS_COLS(width, pass) == 0)
3438 continue;
John Bowler18c5cfa2011-11-16 14:26:34 -06003439
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003440 startx = PNG_PASS_START_COL(pass);
3441 stepx = PNG_PASS_COL_OFFSET(pass);
3442 y = PNG_PASS_START_ROW(pass);
3443 stepy = PNG_PASS_ROW_OFFSET(pass);
3444 }
3445
3446 else
3447 {
3448 y = 0;
3449 startx = 0;
3450 stepx = stepy = 1;
3451 }
3452
3453 if (display->background == NULL)
3454 {
3455 for (; y<height; y += stepy)
John Bowler18c5cfa2011-11-16 14:26:34 -06003456 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003457 png_bytep inrow = png_voidcast(png_bytep,
3458 display->local_row);
3459 png_bytep outrow = first_row + y * step_row;
3460 png_const_bytep end_row = outrow + width;
John Bowler18c5cfa2011-11-16 14:26:34 -06003461
3462 /* Read the row, which is packed: */
3463 png_read_row(png_ptr, inrow, NULL);
3464
3465 /* Now do the composition on each pixel in this row. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003466 outrow += startx;
John Bowler18c5cfa2011-11-16 14:26:34 -06003467 for (; outrow < end_row; outrow += stepx)
3468 {
3469 png_byte alpha = inrow[1];
3470
3471 if (alpha > 0) /* else no change to the output */
3472 {
3473 png_uint_32 component = inrow[0];
3474
3475 if (alpha < 255) /* else just use component */
3476 {
3477 /* Since PNG_OPTIMIZED_ALPHA was not set it is
3478 * necessary to invert the sRGB transfer
3479 * function and multiply the alpha out.
3480 */
3481 component = png_sRGB_table[component] * alpha;
3482 component += png_sRGB_table[outrow[0]] *
3483 (255-alpha);
3484 component = PNG_sRGB_FROM_LINEAR(component);
3485 }
3486
3487 outrow[0] = (png_byte)component;
3488 }
3489
3490 inrow += 2; /* gray and alpha channel */
3491 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003492 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003493 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003494
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003495 else /* constant background value */
3496 {
3497 png_byte background8 = display->background->green;
3498 png_uint_16 background = png_sRGB_table[background8];
John Bowler18c5cfa2011-11-16 14:26:34 -06003499
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003500 for (; y<height; y += stepy)
John Bowler18c5cfa2011-11-16 14:26:34 -06003501 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003502 png_bytep inrow = png_voidcast(png_bytep,
3503 display->local_row);
3504 png_bytep outrow = first_row + y * step_row;
3505 png_const_bytep end_row = outrow + width;
John Bowler18c5cfa2011-11-16 14:26:34 -06003506
3507 /* Read the row, which is packed: */
3508 png_read_row(png_ptr, inrow, NULL);
3509
3510 /* Now do the composition on each pixel in this row. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003511 outrow += startx;
John Bowler18c5cfa2011-11-16 14:26:34 -06003512 for (; outrow < end_row; outrow += stepx)
3513 {
3514 png_byte alpha = inrow[1];
3515
3516 if (alpha > 0) /* else use background */
3517 {
3518 png_uint_32 component = inrow[0];
3519
3520 if (alpha < 255) /* else just use component */
3521 {
3522 component = png_sRGB_table[component] * alpha;
3523 component += background * (255-alpha);
3524 component = PNG_sRGB_FROM_LINEAR(component);
3525 }
3526
3527 outrow[0] = (png_byte)component;
3528 }
3529
3530 else
3531 outrow[0] = background8;
3532
3533 inrow += 2; /* gray and alpha channel */
3534 }
3535
3536 row += display->row_bytes;
3537 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003538 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003539 }
3540 }
3541 break;
3542
3543 case 16:
3544 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3545 * still be done and, maybe, the alpha channel removed. This code also
3546 * handles the alpha-first option.
3547 */
3548 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003549 png_uint_16p first_row = png_voidcast(png_uint_16p,
3550 display->first_row);
3551 /* The division by two is safe because the caller passed in a
3552 * stride which was multiplied by 2 (below) to get row_bytes.
3553 */
3554 ptrdiff_t step_row = display->row_bytes / 2;
John Bowler18c5cfa2011-11-16 14:26:34 -06003555 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003556 unsigned int outchannels = 1+preserve_alpha;
John Bowler18c5cfa2011-11-16 14:26:34 -06003557 int swap_alpha = 0;
3558
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06003559# ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003560 if (preserve_alpha != 0 &&
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -05003561 (image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06003562 swap_alpha = 1;
3563# endif
John Bowler18c5cfa2011-11-16 14:26:34 -06003564
3565 for (pass = 0; pass < passes; ++pass)
3566 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003567 unsigned int startx, stepx, stepy;
John Bowler18c5cfa2011-11-16 14:26:34 -06003568 png_uint_32 y;
3569
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003570 /* The 'x' start and step are adjusted to output components here.
3571 */
3572 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
John Bowler18c5cfa2011-11-16 14:26:34 -06003573 {
3574 /* The row may be empty for a short image: */
3575 if (PNG_PASS_COLS(width, pass) == 0)
3576 continue;
3577
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003578 startx = PNG_PASS_START_COL(pass) * outchannels;
3579 stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
John Bowler18c5cfa2011-11-16 14:26:34 -06003580 y = PNG_PASS_START_ROW(pass);
3581 stepy = PNG_PASS_ROW_OFFSET(pass);
3582 }
3583
3584 else
3585 {
3586 y = 0;
3587 startx = 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003588 stepx = outchannels;
3589 stepy = 1;
John Bowler18c5cfa2011-11-16 14:26:34 -06003590 }
3591
John Bowler18c5cfa2011-11-16 14:26:34 -06003592 for (; y<height; y += stepy)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003593 {
3594 png_const_uint_16p inrow;
3595 png_uint_16p outrow = first_row + y*step_row;
3596 png_uint_16p end_row = outrow + width * outchannels;
3597
3598 /* Read the row, which is packed: */
3599 png_read_row(png_ptr, png_voidcast(png_bytep,
3600 display->local_row), NULL);
3601 inrow = png_voidcast(png_const_uint_16p, display->local_row);
3602
3603 /* Now do the pre-multiplication on each pixel in this row.
3604 */
3605 outrow += startx;
3606 for (; outrow < end_row; outrow += stepx)
John Bowler18c5cfa2011-11-16 14:26:34 -06003607 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003608 png_uint_32 component = inrow[0];
3609 png_uint_16 alpha = inrow[1];
John Bowler18c5cfa2011-11-16 14:26:34 -06003610
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003611 if (alpha > 0) /* else 0 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003612 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003613 if (alpha < 65535) /* else just use component */
John Bowler18c5cfa2011-11-16 14:26:34 -06003614 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003615 component *= alpha;
3616 component += 32767;
3617 component /= 65535;
John Bowler18c5cfa2011-11-16 14:26:34 -06003618 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003619 }
3620
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003621 else
3622 component = 0;
3623
3624 outrow[swap_alpha] = (png_uint_16)component;
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003625 if (preserve_alpha != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003626 outrow[1 ^ swap_alpha] = alpha;
3627
3628 inrow += 2; /* components and alpha channel */
John Bowler18c5cfa2011-11-16 14:26:34 -06003629 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003630 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003631 }
3632 }
3633 break;
3634 }
3635
3636 return 1;
3637}
3638
John Bowler7875d532011-11-07 22:33:49 -06003639/* The guts of png_image_finish_read as a png_safe_execute callback. */
3640static int
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003641png_image_read_direct(png_voidp argument)
John Bowler7875d532011-11-07 22:33:49 -06003642{
John Bowler4fa96a42011-11-16 16:39:16 -06003643 png_image_read_control *display = png_voidcast(png_image_read_control*,
3644 argument);
John Bowler7875d532011-11-07 22:33:49 -06003645 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06003646 png_structrp png_ptr = image->opaque->png_ptr;
3647 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06003648
3649 png_uint_32 format = image->format;
3650 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
3651 int do_local_compose = 0;
John Bowler18c5cfa2011-11-16 14:26:34 -06003652 int do_local_background = 0; /* to avoid double gamma correction bug */
John Bowler7875d532011-11-07 22:33:49 -06003653 int passes = 0;
3654
3655 /* Add transforms to ensure the correct output format is produced then check
3656 * that the required implementation support is there. Always expand; always
3657 * need 8 bits minimum, no palette and expanded tRNS.
3658 */
3659 png_set_expand(png_ptr);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003660
John Bowler7875d532011-11-07 22:33:49 -06003661 /* Now check the format to see if it was modified. */
3662 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003663 png_uint_32 base_format = png_image_format(png_ptr) &
3664 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
John Bowler7875d532011-11-07 22:33:49 -06003665 png_uint_32 change = format ^ base_format;
John Bowler3615d032011-11-08 10:38:09 -06003666 png_fixed_point output_gamma;
3667 int mode; /* alpha mode */
John Bowler7875d532011-11-07 22:33:49 -06003668
John Bowler18c5cfa2011-11-16 14:26:34 -06003669 /* Do this first so that we have a record if rgb to gray is happening. */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003670 if ((change & PNG_FORMAT_FLAG_COLOR) != 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003671 {
3672 /* gray<->color transformation required. */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003673 if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003674 png_set_gray_to_rgb(png_ptr);
3675
3676 else
3677 {
3678 /* libpng can't do both rgb to gray and
3679 * background/pre-multiplication if there is also significant gamma
3680 * correction, because both operations require linear colors and
3681 * the code only supports one transform doing the gamma correction.
3682 * Handle this by doing the pre-multiplication or background
3683 * operation in this code, if necessary.
3684 *
3685 * TODO: fix this by rewriting pngrtran.c (!)
3686 *
3687 * For the moment (given that fixing this in pngrtran.c is an
3688 * enormous change) 'do_local_background' is used to indicate that
3689 * the problem exists.
3690 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003691 if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003692 do_local_background = 1/*maybe*/;
3693
3694 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
3695 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
3696 }
3697
3698 change &= ~PNG_FORMAT_FLAG_COLOR;
3699 }
3700
John Bowler7875d532011-11-07 22:33:49 -06003701 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3702 */
3703 {
John Bowler3615d032011-11-08 10:38:09 -06003704 png_fixed_point input_gamma_default;
John Bowler7875d532011-11-07 22:33:49 -06003705
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003706 if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 &&
3707 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
John Bowler7875d532011-11-07 22:33:49 -06003708 input_gamma_default = PNG_GAMMA_LINEAR;
3709 else
3710 input_gamma_default = PNG_DEFAULT_sRGB;
3711
John Bowler18c5cfa2011-11-16 14:26:34 -06003712 /* Call png_set_alpha_mode to set the default for the input gamma; the
3713 * output gamma is set by a second call below.
3714 */
3715 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
3716 }
3717
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003718 if (linear != 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003719 {
3720 /* If there *is* an alpha channel in the input it must be multiplied
3721 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3722 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003723 if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
John Bowler3615d032011-11-08 10:38:09 -06003724 mode = PNG_ALPHA_STANDARD; /* associated alpha */
John Bowler7875d532011-11-07 22:33:49 -06003725
3726 else
John Bowler7875d532011-11-07 22:33:49 -06003727 mode = PNG_ALPHA_PNG;
John Bowler18c5cfa2011-11-16 14:26:34 -06003728
3729 output_gamma = PNG_GAMMA_LINEAR;
3730 }
3731
3732 else
3733 {
3734 mode = PNG_ALPHA_PNG;
3735 output_gamma = PNG_DEFAULT_sRGB;
3736 }
3737
3738 /* If 'do_local_background' is set check for the presence of gamma
3739 * correction; this is part of the work-round for the libpng bug
3740 * described above.
3741 *
3742 * TODO: fix libpng and remove this.
3743 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003744 if (do_local_background != 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003745 {
3746 png_fixed_point gtest;
3747
3748 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3749 * gamma correction, the screen gamma hasn't been set on png_struct
3750 * yet; it's set below. png_struct::gamma, however, is set to the
3751 * final value.
3752 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003753 if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003754 PNG_FP_1) != 0 && png_gamma_significant(gtest) == 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003755 do_local_background = 0;
3756
3757 else if (mode == PNG_ALPHA_STANDARD)
3758 {
3759 do_local_background = 2/*required*/;
3760 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
John Bowler7875d532011-11-07 22:33:49 -06003761 }
3762
John Bowler18c5cfa2011-11-16 14:26:34 -06003763 /* else leave as 1 for the checks below */
John Bowler3615d032011-11-08 10:38:09 -06003764 }
John Bowler7875d532011-11-07 22:33:49 -06003765
John Bowler3615d032011-11-08 10:38:09 -06003766 /* If the bit-depth changes then handle that here. */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003767 if ((change & PNG_FORMAT_FLAG_LINEAR) != 0)
John Bowler3615d032011-11-08 10:38:09 -06003768 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003769 if (linear != 0 /*16-bit output*/)
John Bowler3615d032011-11-08 10:38:09 -06003770 png_set_expand_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06003771
John Bowler3615d032011-11-08 10:38:09 -06003772 else /* 8-bit output */
3773 png_set_scale_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06003774
John Bowler3615d032011-11-08 10:38:09 -06003775 change &= ~PNG_FORMAT_FLAG_LINEAR;
John Bowler7875d532011-11-07 22:33:49 -06003776 }
3777
3778 /* Now the background/alpha channel changes. */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003779 if ((change & PNG_FORMAT_FLAG_ALPHA) != 0)
John Bowler7875d532011-11-07 22:33:49 -06003780 {
3781 /* Removing an alpha channel requires composition for the 8-bit
3782 * formats; for the 16-bit it is already done, above, by the
3783 * pre-multiplication and the channel just needs to be stripped.
3784 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003785 if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
John Bowler7875d532011-11-07 22:33:49 -06003786 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003787 /* If RGB->gray is happening the alpha channel must be left and the
3788 * operation completed locally.
3789 *
3790 * TODO: fix libpng and remove this.
3791 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003792 if (do_local_background != 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003793 do_local_background = 2/*required*/;
3794
John Bowler3615d032011-11-08 10:38:09 -06003795 /* 16-bit output: just remove the channel */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003796 else if (linear != 0) /* compose on black (well, pre-multiply) */
John Bowler7875d532011-11-07 22:33:49 -06003797 png_set_strip_alpha(png_ptr);
3798
John Bowler3615d032011-11-08 10:38:09 -06003799 /* 8-bit output: do an appropriate compose */
John Bowler7875d532011-11-07 22:33:49 -06003800 else if (display->background != NULL)
3801 {
3802 png_color_16 c;
3803
3804 c.index = 0; /*unused*/
3805 c.red = display->background->red;
3806 c.green = display->background->green;
3807 c.blue = display->background->blue;
3808 c.gray = display->background->green;
3809
3810 /* This is always an 8-bit sRGB value, using the 'green' channel
3811 * for gray is much better than calculating the luminance here;
3812 * we can get off-by-one errors in that calculation relative to
3813 * the app expectations and that will show up in transparent
3814 * pixels.
3815 */
3816 png_set_background_fixed(png_ptr, &c,
3817 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
3818 0/*gamma: not used*/);
3819 }
3820
3821 else /* compose on row: implemented below. */
3822 {
3823 do_local_compose = 1;
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003824 /* This leaves the alpha channel in the output, so it has to be
John Bowler7875d532011-11-07 22:33:49 -06003825 * removed by the code below. Set the encoding to the 'OPTIMIZE'
3826 * one so the code only has to hack on the pixels that require
3827 * composition.
3828 */
John Bowler3615d032011-11-08 10:38:09 -06003829 mode = PNG_ALPHA_OPTIMIZED;
John Bowler7875d532011-11-07 22:33:49 -06003830 }
3831 }
3832
3833 else /* output needs an alpha channel */
3834 {
3835 /* This is tricky because it happens before the swap operation has
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003836 * been accomplished; however, the swap does *not* swap the added
John Bowlere6fb6912011-11-08 21:35:16 -06003837 * alpha channel (weird API), so it must be added in the correct
3838 * place.
John Bowler7875d532011-11-07 22:33:49 -06003839 */
John Bowler3615d032011-11-08 10:38:09 -06003840 png_uint_32 filler; /* opaque filler */
John Bowlere6fb6912011-11-08 21:35:16 -06003841 int where;
John Bowler3615d032011-11-08 10:38:09 -06003842
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003843 if (linear != 0)
John Bowler3615d032011-11-08 10:38:09 -06003844 filler = 65535;
3845
3846 else
3847 filler = 255;
3848
John Bowlere6fb6912011-11-08 21:35:16 -06003849# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003850 if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
John Bowlere6fb6912011-11-08 21:35:16 -06003851 {
3852 where = PNG_FILLER_BEFORE;
3853 change &= ~PNG_FORMAT_FLAG_AFIRST;
3854 }
3855
3856 else
3857# endif
3858 where = PNG_FILLER_AFTER;
3859
3860 png_set_add_alpha(png_ptr, filler, where);
John Bowler7875d532011-11-07 22:33:49 -06003861 }
3862
John Bowlere6fb6912011-11-08 21:35:16 -06003863 /* This stops the (irrelevant) call to swap_alpha below. */
John Bowler7875d532011-11-07 22:33:49 -06003864 change &= ~PNG_FORMAT_FLAG_ALPHA;
3865 }
3866
John Bowler3615d032011-11-08 10:38:09 -06003867 /* Now set the alpha mode correctly; this is always done, even if there is
3868 * no alpha channel in either the input or the output because it correctly
3869 * sets the output gamma.
3870 */
3871 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
3872
John Bowler7875d532011-11-07 22:33:49 -06003873# ifdef PNG_FORMAT_BGR_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003874 if ((change & PNG_FORMAT_FLAG_BGR) != 0)
John Bowler7875d532011-11-07 22:33:49 -06003875 {
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003876 /* Check only the output format; PNG is never BGR; don't do this if
John Bowler7875d532011-11-07 22:33:49 -06003877 * the output is gray, but fix up the 'format' value in that case.
3878 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003879 if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
John Bowler7875d532011-11-07 22:33:49 -06003880 png_set_bgr(png_ptr);
3881
3882 else
3883 format &= ~PNG_FORMAT_FLAG_BGR;
3884
3885 change &= ~PNG_FORMAT_FLAG_BGR;
3886 }
3887# endif
3888
3889# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003890 if ((change & PNG_FORMAT_FLAG_AFIRST) != 0)
John Bowler7875d532011-11-07 22:33:49 -06003891 {
3892 /* Only relevant if there is an alpha channel - it's particularly
3893 * important to handle this correctly because do_local_compose may
3894 * be set above and then libpng will keep the alpha channel for this
3895 * code to remove.
3896 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003897 if ((format & PNG_FORMAT_FLAG_ALPHA) != 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003898 {
3899 /* Disable this if doing a local background,
3900 * TODO: remove this when local background is no longer required.
3901 */
3902 if (do_local_background != 2)
3903 png_set_swap_alpha(png_ptr);
3904 }
John Bowler7875d532011-11-07 22:33:49 -06003905
3906 else
3907 format &= ~PNG_FORMAT_FLAG_AFIRST;
3908
3909 change &= ~PNG_FORMAT_FLAG_AFIRST;
3910 }
3911# endif
3912
3913 /* If the *output* is 16-bit then we need to check for a byte-swap on this
3914 * architecture.
3915 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003916 if (linear != 0)
John Bowler7875d532011-11-07 22:33:49 -06003917 {
3918 PNG_CONST png_uint_16 le = 0x0001;
3919
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003920 if ((*(png_const_bytep) & le) != 0)
John Bowler7875d532011-11-07 22:33:49 -06003921 png_set_swap(png_ptr);
3922 }
3923
3924 /* If change is not now 0 some transformation is missing - error out. */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003925 if (change != 0)
John Bowler7875d532011-11-07 22:33:49 -06003926 png_error(png_ptr, "png_read_image: unsupported transformation");
3927 }
3928
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003929 PNG_SKIP_CHUNKS(png_ptr);
John Bowler89c2f842011-11-16 12:04:39 -06003930
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003931 /* Update the 'info' structure and make sure the result is as required; first
John Bowler7875d532011-11-07 22:33:49 -06003932 * make sure to turn on the interlace handling if it will be required
3933 * (because it can't be turned on *after* the call to png_read_update_info!)
John Bowler18c5cfa2011-11-16 14:26:34 -06003934 *
3935 * TODO: remove the do_local_background fixup below.
John Bowler7875d532011-11-07 22:33:49 -06003936 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003937 if (do_local_compose == 0 && do_local_background != 2)
John Bowler7875d532011-11-07 22:33:49 -06003938 passes = png_set_interlace_handling(png_ptr);
3939
3940 png_read_update_info(png_ptr, info_ptr);
3941
3942 {
3943 png_uint_32 info_format = 0;
3944
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003945 if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
John Bowler7875d532011-11-07 22:33:49 -06003946 info_format |= PNG_FORMAT_FLAG_COLOR;
3947
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003948 if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
John Bowler7875d532011-11-07 22:33:49 -06003949 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003950 /* do_local_compose removes this channel below. */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003951 if (do_local_compose == 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06003952 {
3953 /* do_local_background does the same if required. */
3954 if (do_local_background != 2 ||
3955 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
3956 info_format |= PNG_FORMAT_FLAG_ALPHA;
3957 }
John Bowler7875d532011-11-07 22:33:49 -06003958 }
3959
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003960 else if (do_local_compose != 0) /* internal error */
John Bowler7875d532011-11-07 22:33:49 -06003961 png_error(png_ptr, "png_image_read: alpha channel lost");
3962
3963 if (info_ptr->bit_depth == 16)
3964 info_format |= PNG_FORMAT_FLAG_LINEAR;
3965
3966# ifdef PNG_FORMAT_BGR_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003967 if ((png_ptr->transformations & PNG_BGR) != 0)
John Bowler7875d532011-11-07 22:33:49 -06003968 info_format |= PNG_FORMAT_FLAG_BGR;
3969# endif
3970
3971# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003972 if (do_local_background == 2)
3973 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003974 if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003975 info_format |= PNG_FORMAT_FLAG_AFIRST;
3976 }
3977
3978 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
John Bowlere6fb6912011-11-08 21:35:16 -06003979 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
3980 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003981 {
3982 if (do_local_background == 2)
3983 png_error(png_ptr, "unexpected alpha swap transformation");
3984
John Bowler7875d532011-11-07 22:33:49 -06003985 info_format |= PNG_FORMAT_FLAG_AFIRST;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003986 }
John Bowler7875d532011-11-07 22:33:49 -06003987# endif
3988
3989 /* This is actually an internal error. */
3990 if (info_format != format)
3991 png_error(png_ptr, "png_read_image: invalid transformations");
3992 }
3993
3994 /* Now read the rows. If do_local_compose is set then it is necessary to use
3995 * a local row buffer. The output will be GA, RGBA or BGRA and must be
3996 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
3997 * display acts as a flag.
3998 */
3999 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004000 png_voidp first_row = display->buffer;
John Bowler7875d532011-11-07 22:33:49 -06004001 ptrdiff_t row_bytes = display->row_stride;
4002
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06004003 if (linear != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004004 row_bytes *= 2;
John Bowler7875d532011-11-07 22:33:49 -06004005
4006 /* The following expression is designed to work correctly whether it gives
4007 * a signed or an unsigned result.
4008 */
4009 if (row_bytes < 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004010 {
4011 char *ptr = png_voidcast(char*, first_row);
4012 ptr += (image->height-1) * (-row_bytes);
4013 first_row = png_voidcast(png_voidp, ptr);
4014 }
John Bowler7875d532011-11-07 22:33:49 -06004015
4016 display->first_row = first_row;
4017 display->row_bytes = row_bytes;
4018 }
4019
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06004020 if (do_local_compose != 0)
John Bowler7875d532011-11-07 22:33:49 -06004021 {
4022 int result;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004023 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
John Bowler7875d532011-11-07 22:33:49 -06004024
4025 display->local_row = row;
4026 result = png_safe_execute(image, png_image_read_composite, display);
4027 display->local_row = NULL;
4028 png_free(png_ptr, row);
4029
4030 return result;
4031 }
4032
John Bowler18c5cfa2011-11-16 14:26:34 -06004033 else if (do_local_background == 2)
4034 {
4035 int result;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004036 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
John Bowler18c5cfa2011-11-16 14:26:34 -06004037
4038 display->local_row = row;
4039 result = png_safe_execute(image, png_image_read_background, display);
4040 display->local_row = NULL;
4041 png_free(png_ptr, row);
4042
4043 return result;
4044 }
4045
John Bowler7875d532011-11-07 22:33:49 -06004046 else
4047 {
4048 png_alloc_size_t row_bytes = display->row_bytes;
4049
4050 while (--passes >= 0)
4051 {
4052 png_uint_32 y = image->height;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004053 png_bytep row = png_voidcast(png_bytep, display->first_row);
4054
John Bowler7875d532011-11-07 22:33:49 -06004055 while (y-- > 0)
4056 {
4057 png_read_row(png_ptr, row, NULL);
4058 row += row_bytes;
4059 }
4060 }
4061
4062 return 1;
4063 }
4064}
4065
4066int PNGAPI
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004067png_image_finish_read(png_imagep image, png_const_colorp background,
4068 void *buffer, png_int_32 row_stride, void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06004069{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004070 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06004071 {
4072 png_uint_32 check;
4073
John Bowler3706d732011-11-21 10:28:06 -06004074 if (row_stride == 0)
4075 row_stride = PNG_IMAGE_ROW_STRIDE(*image);
4076
John Bowler7875d532011-11-07 22:33:49 -06004077 if (row_stride < 0)
4078 check = -row_stride;
4079
4080 else
4081 check = row_stride;
4082
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004083 if (image->opaque != NULL && buffer != NULL &&
4084 check >= PNG_IMAGE_ROW_STRIDE(*image))
John Bowler7875d532011-11-07 22:33:49 -06004085 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004086 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
4087 (image->colormap_entries > 0 && colormap != NULL))
4088 {
4089 int result;
4090 png_image_read_control display;
John Bowler7875d532011-11-07 22:33:49 -06004091
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004092 memset(&display, 0, (sizeof display));
4093 display.image = image;
4094 display.buffer = buffer;
4095 display.row_stride = row_stride;
4096 display.colormap = colormap;
4097 display.background = background;
4098 display.local_row = NULL;
4099
4100 /* Choose the correct 'end' routine; for the color-map case all the
4101 * setup has already been done.
4102 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004103 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004104 result =
4105 png_safe_execute(image, png_image_read_colormap, &display) &&
4106 png_safe_execute(image, png_image_read_colormapped, &display);
4107
4108 else
4109 result =
4110 png_safe_execute(image, png_image_read_direct, &display);
4111
4112 png_image_free(image);
4113 return result;
4114 }
4115
4116 else
4117 return png_image_error(image,
4118 "png_image_finish_read[color-map]: no color-map");
John Bowler7875d532011-11-07 22:33:49 -06004119 }
4120
4121 else
4122 return png_image_error(image,
4123 "png_image_finish_read: invalid argument");
4124 }
4125
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004126 else if (image != NULL)
4127 return png_image_error(image,
4128 "png_image_finish_read: damaged PNG_IMAGE_VERSION");
4129
John Bowler7875d532011-11-07 22:33:49 -06004130 return 0;
4131}
4132
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06004133#endif /* SIMPLIFIED_READ */
4134#endif /* READ */