blob: 4bd8c7c93c00a7c2850bf9dde5708694737719f9 [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-Pehrsonbe3977d2013-12-19 09:04:52 -06004 * Last changed in libpng 1.6.8 [December 19, 2013]
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06005 * Copyright (c) 1998-2013 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
15 */
Guy Schalnat0d580581995-07-20 02:43:20 -050016
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050017#include "pngpriv.h"
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060018#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
John Bowler7875d532011-11-07 22:33:49 -060019# include <errno.h>
20#endif
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060021
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060022#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050023
Andreas Dilger47a0c421997-05-16 02:46:07 -050024/* Create a PNG structure for reading, and allocate any memory needed. */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050025PNG_FUNCTION(png_structp,PNGAPI
26png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
27 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
Guy Schalnat0d580581995-07-20 02:43:20 -050028{
John Bowler1d7f56a2011-12-21 20:14:23 -060029#ifndef PNG_USER_MEM_SUPPORTED
30 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
31 error_fn, warn_fn, NULL, NULL, NULL);
32#else
33 return png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
34 warn_fn, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050035}
36
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060037/* Alternate create PNG structure for reading, and allocate any memory
38 * needed.
39 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050040PNG_FUNCTION(png_structp,PNGAPI
41png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -060042 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050043 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050044{
John Bowler1d7f56a2011-12-21 20:14:23 -060045 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
46 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050047#endif /* PNG_USER_MEM_SUPPORTED */
48
John Bowler1d7f56a2011-12-21 20:14:23 -060049 if (png_ptr != NULL)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050050 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060051 png_ptr->mode = PNG_IS_READ_STRUCT;
John Bowlerb5d00512012-03-09 09:15:18 -060052
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060053 /* Added in libpng-1.6.0; this can be used to detect a read structure if
54 * required (it will be zero in a write structure.)
John Bowlerb5d00512012-03-09 09:15:18 -060055 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060056# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
57 png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE;
58# endif
John Bowleraa816c42012-03-16 07:39:49 -050059
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060060# ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
61 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
John Bowler2bc76ff2012-03-18 22:37:25 -050062
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060063 /* In stable builds only warn if an application error can be completely
64 * handled.
John Bowler2bc76ff2012-03-18 22:37:25 -050065 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060066# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
67 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
68# endif
69# endif
John Bowleraa816c42012-03-16 07:39:49 -050070
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060071 /* TODO: delay this, it can be done in png_init_io (if the app doesn't
72 * do it itself) avoiding setting the default function if it is not
73 * required.
John Bowler1d7f56a2011-12-21 20:14:23 -060074 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060075 png_set_read_fn(png_ptr, NULL, NULL);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050076 }
77
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060078 return png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -050079}
80
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050081
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050082#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060083/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -060084 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060085 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -050086 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060087 * via png_set_sig_bytes(), and we will only check the remaining bytes
88 * here. The application can then have access to the signature bytes we
89 * read if it is determined that this isn't a valid PNG file.
90 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050091void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -060092png_read_info(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050093{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060094#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
95 int keep;
96#endif
97
Glenn Randers-Pehrsonc81bb8a2009-08-15 22:02:26 -050098 png_debug(1, "in png_read_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -050099
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500100 if (png_ptr == NULL || info_ptr == NULL)
101 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500102
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600103 /* Read and check the PNG file signature. */
104 png_read_sig(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500105
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500106 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500107 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500108 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500109 png_uint_32 chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500110
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600111 /* IDAT logic needs to happen here to simplify getting the two flags
112 * right.
113 */
114 if (chunk_name == png_IDAT)
115 {
116 if (!(png_ptr->mode & PNG_HAVE_IHDR))
117 png_chunk_error(png_ptr, "Missing IHDR before IDAT");
118
119 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
120 !(png_ptr->mode & PNG_HAVE_PLTE))
121 png_chunk_error(png_ptr, "Missing PLTE before IDAT");
122
123 else if (png_ptr->mode & PNG_AFTER_IDAT)
124 png_chunk_benign_error(png_ptr, "Too many IDATs found");
125
126 png_ptr->mode |= PNG_HAVE_IDAT;
127 }
128
129 else if (png_ptr->mode & PNG_HAVE_IDAT)
130 png_ptr->mode |= PNG_AFTER_IDAT;
131
Andreas Dilger47a0c421997-05-16 02:46:07 -0500132 /* This should be a binary subdivision search or a hash for
133 * matching the chunk name rather than a linear search.
134 */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500135 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600136 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500137
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500138 else if (chunk_name == png_IEND)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600139 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500140
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600141#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600142 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600143 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600144 png_handle_unknown(png_ptr, info_ptr, length, keep);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500145
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500146 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600147 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500148
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500149 else if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600150 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600151 png_ptr->idat_size = 0; /* It has been consumed */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600152 break;
153 }
154 }
155#endif
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500156 else if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600157 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500158
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500159 else if (chunk_name == png_IDAT)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500160 {
161 png_ptr->idat_size = length;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500162 break;
163 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500164
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500165#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500166 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 png_handle_bKGD(png_ptr, info_ptr, length);
168#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500169
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500170#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500171 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500172 png_handle_cHRM(png_ptr, info_ptr, length);
173#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500174
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500175#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500176 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500177 png_handle_gAMA(png_ptr, info_ptr, length);
178#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500179
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500180#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500181 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500182 png_handle_hIST(png_ptr, info_ptr, length);
183#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500184
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500185#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500186 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187 png_handle_oFFs(png_ptr, info_ptr, length);
188#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500189
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500190#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500191 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500192 png_handle_pCAL(png_ptr, info_ptr, length);
193#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500194
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500195#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500196 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600197 png_handle_sCAL(png_ptr, info_ptr, length);
198#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500199
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500200#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500201 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500202 png_handle_pHYs(png_ptr, info_ptr, length);
203#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500204
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500205#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500206 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500207 png_handle_sBIT(png_ptr, info_ptr, length);
208#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500209
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500210#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500211 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600212 png_handle_sRGB(png_ptr, info_ptr, length);
213#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500214
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500215#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500216 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600217 png_handle_iCCP(png_ptr, info_ptr, length);
218#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500219
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500220#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500221 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600222 png_handle_sPLT(png_ptr, info_ptr, length);
223#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500224
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500225#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500226 else if (chunk_name == png_tEXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500227 png_handle_tEXt(png_ptr, info_ptr, length);
228#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500229
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500230#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500231 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500232 png_handle_tIME(png_ptr, info_ptr, length);
233#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500234
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500235#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500236 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500237 png_handle_tRNS(png_ptr, info_ptr, length);
238#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500239
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500240#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500241 else if (chunk_name == png_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500242 png_handle_zTXt(png_ptr, info_ptr, length);
243#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500244
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500245#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500246 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600247 png_handle_iTXt(png_ptr, info_ptr, length);
248#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500249
Guy Schalnat0d580581995-07-20 02:43:20 -0500250 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600251 png_handle_unknown(png_ptr, info_ptr, length,
252 PNG_HANDLE_CHUNK_AS_DEFAULT);
Guy Schalnat0d580581995-07-20 02:43:20 -0500253 }
254}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500255#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500256
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500257/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500258void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600259png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500260{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500261 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500262
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600263 if (png_ptr != NULL)
264 {
265 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
266 {
267 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500268
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600269# ifdef PNG_READ_TRANSFORMS_SUPPORTED
270 png_read_transform_info(png_ptr, info_ptr);
271# else
272 PNG_UNUSED(info_ptr)
273# endif
274 }
Glenn Randers-Pehrson4cfdb3c2009-11-26 11:49:37 -0600275
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600276 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
277 else
278 png_app_error(png_ptr,
279 "png_read_update_info/png_start_read_image: duplicate call");
280 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500281}
282
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500283#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600284/* Initialize palette, background, etc, after transformations
285 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500286 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600287 * If the user doesn't call this, we will do it ourselves.
288 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500289void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600290png_start_read_image(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500291{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500292 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500293
Glenn Randers-Pehrson3dbfd302011-10-13 17:24:36 -0500294 if (png_ptr != NULL)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600295 {
296 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
297 png_read_start_row(png_ptr);
298
299 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
300 else
301 png_app_error(png_ptr,
302 "png_start_read_image/png_read_update_info: duplicate call");
303 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500304}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500305#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500306
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500307#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500308void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600309png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500310{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500311 png_row_info row_info;
312
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500313 if (png_ptr == NULL)
314 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500315
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500316 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -0500317 (unsigned long)png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500318
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500319 /* png_read_start_row sets the information (in particular iwidth) for this
320 * interlace pass.
321 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500322 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500323 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500324
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500325 /* 1.5.6: row_info moved out of png_struct to a local here. */
326 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
327 row_info.color_type = png_ptr->color_type;
328 row_info.bit_depth = png_ptr->bit_depth;
329 row_info.channels = png_ptr->channels;
330 row_info.pixel_depth = png_ptr->pixel_depth;
331 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
332
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500333 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
334 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500335 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500336#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
337 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500338 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500339#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500340
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500341#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
342 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500343 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500344#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500345
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600346#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
347 !defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500348 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500349 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500350#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500351
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500352#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
353 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500354 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500355#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500356
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500357#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
358 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500359 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500360#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500361
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500362#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
363 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500364 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500365#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500366
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500367#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
368 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500369 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500370#endif
371 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500372
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500373#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500374 /* If interlaced and we do not need a new row, combine row and return.
375 * Notice that the pixels we have from previous rows have been transformed
376 * already; we can only combine like with like (transformed or
377 * untransformed) and, because of the libpng API for interlaced images, this
378 * means we must transform before de-interlacing.
379 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500380 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
381 {
382 switch (png_ptr->pass)
383 {
384 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600385 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500386 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500388 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600389 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500390 return;
391 }
392 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500393
Guy Schalnat0d580581995-07-20 02:43:20 -0500394 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600395 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500396 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500398 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500399
Guy Schalnat0d580581995-07-20 02:43:20 -0500400 png_read_finish_row(png_ptr);
401 return;
402 }
403 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500404
Guy Schalnat0d580581995-07-20 02:43:20 -0500405 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600406 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500407 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500408 if (dsp_row != NULL && (png_ptr->row_number & 4))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500409 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500410
Guy Schalnat0d580581995-07-20 02:43:20 -0500411 png_read_finish_row(png_ptr);
412 return;
413 }
414 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500415
Guy Schalnat0d580581995-07-20 02:43:20 -0500416 case 3:
417 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
418 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500419 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500420 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500421
Guy Schalnat0d580581995-07-20 02:43:20 -0500422 png_read_finish_row(png_ptr);
423 return;
424 }
425 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500426
Guy Schalnat0d580581995-07-20 02:43:20 -0500427 case 4:
428 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600429 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500430 if (dsp_row != NULL && (png_ptr->row_number & 2))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500431 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500432
Guy Schalnat0d580581995-07-20 02:43:20 -0500433 png_read_finish_row(png_ptr);
434 return;
435 }
436 break;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600437
Guy Schalnat0d580581995-07-20 02:43:20 -0500438 case 5:
439 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
440 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500441 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500442 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500443
Guy Schalnat0d580581995-07-20 02:43:20 -0500444 png_read_finish_row(png_ptr);
445 return;
446 }
447 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500448
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600449 default:
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600450 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500451 if (!(png_ptr->row_number & 1))
452 {
453 png_read_finish_row(png_ptr);
454 return;
455 }
456 break;
457 }
458 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500459#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500460
Guy Schalnate5a37791996-06-05 15:50:50 -0500461 if (!(png_ptr->mode & PNG_HAVE_IDAT))
462 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500463
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600464 /* Fill the row with IDAT data: */
465 png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500466
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500467 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
468 {
469 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
Mans Rullgardd3a94802011-11-03 00:47:55 -0500470 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500471 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
472 else
473 png_error(png_ptr, "bad adaptive filter value");
474 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500475
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500476 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
477 * 1.5.6, while the buffer really is this big in current versions of libpng
478 * it may not be in the future, so this was changed just to copy the
479 * interlaced count:
480 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600481 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600482
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500483#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500484 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600485 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600486 {
487 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500488 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600489 }
490#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500491
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500492
John Bowler4a12f4a2011-04-17 18:34:22 -0500493#ifdef PNG_READ_TRANSFORMS_SUPPORTED
John Bowler9b872f42011-02-12 09:00:16 -0600494 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500495 png_do_read_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500496#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500497
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500498 /* The transformed pixel depth should match the depth now in row_info. */
499 if (png_ptr->transformed_pixel_depth == 0)
500 {
501 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
502 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
503 png_error(png_ptr, "sequential row overflow");
504 }
505
506 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
507 png_error(png_ptr, "internal sequential row size calculation error");
508
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500509#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500510 /* Blow up interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500511 if (png_ptr->interlaced &&
512 (png_ptr->transformations & PNG_INTERLACE))
513 {
514 if (png_ptr->pass < 6)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500515 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
516 png_ptr->transformations);
Guy Schalnat0d580581995-07-20 02:43:20 -0500517
Andreas Dilger47a0c421997-05-16 02:46:07 -0500518 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500519 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500520
Andreas Dilger47a0c421997-05-16 02:46:07 -0500521 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500522 png_combine_row(png_ptr, row, 0/*row*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500523 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500524
Guy Schalnat0d580581995-07-20 02:43:20 -0500525 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500526#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500527 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500528 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500529 png_combine_row(png_ptr, row, -1/*ignored*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500530
Andreas Dilger47a0c421997-05-16 02:46:07 -0500531 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500532 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500533 }
534 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600535
536 if (png_ptr->read_row_fn != NULL)
537 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600538
Guy Schalnat0d580581995-07-20 02:43:20 -0500539}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500540#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500541
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500542#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500543/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600544 * and png_set_interlace_handling() has been called, the rows need to
545 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500546 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600547 * called, the rows contents must be initialized to the contents of the
548 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600549 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600550 * "row" holds the actual image, and pixels are placed in it
551 * as they arrive. If the image is displayed after each pass, it will
552 * appear to "sparkle" in. "display_row" can be used to display a
553 * "chunky" progressive image, with finer detail added as it becomes
554 * available. If you do not want this "chunky" display, you may pass
555 * NULL for display_row. If you do not want the sparkle display, and
556 * you have not called png_handle_alpha(), you may pass NULL for rows.
557 * If you have called png_handle_alpha(), and the image has either an
558 * alpha channel or a transparency chunk, you must provide a buffer for
559 * rows. In this case, you do not have to provide a display_row buffer
560 * also, but you may. If the image is not interlaced, or if you have
561 * not called png_set_interlace_handling(), the display_row buffer will
562 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500563 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600564 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600565 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600566
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500567void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600568png_read_rows(png_structrp png_ptr, png_bytepp row,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600569 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500570{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600571 png_uint_32 i;
572 png_bytepp rp;
573 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500574
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500575 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500576
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500577 if (png_ptr == NULL)
578 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500579
Guy Schalnat0f716451995-11-28 11:22:13 -0600580 rp = row;
581 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500582 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500583 for (i = 0; i < num_rows; i++)
584 {
585 png_bytep rptr = *rp++;
586 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600587
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500588 png_read_row(png_ptr, rptr, dptr);
589 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500590
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500591 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500592 for (i = 0; i < num_rows; i++)
593 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500594 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500595 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500596 rp++;
597 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500598
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500599 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500600 for (i = 0; i < num_rows; i++)
601 {
602 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500603 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500604 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500605 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500606}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500607#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500608
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500609#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500610/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500611 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600612 * initialize the image to the current image that PNG will be overlaying.
613 * We set the num_rows again here, in case it was incorrectly set in
614 * png_read_start_row() by a call to png_read_update_info() or
615 * png_start_read_image() if png_set_interlace_handling() wasn't called
616 * prior to either of these functions like it should have been. You can
617 * only call this function once. If you desire to have an image for
618 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500619 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600620 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600621 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500622void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600623png_read_image(png_structrp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500624{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500625 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500626 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600627 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500628
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500629 png_debug(1, "in png_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500630
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500631 if (png_ptr == NULL)
632 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500633
634#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500635 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
636 {
637 pass = png_set_interlace_handling(png_ptr);
638 /* And make sure transforms are initialized. */
639 png_start_read_image(png_ptr);
640 }
641 else
642 {
Glenn Randers-Pehrsone15a96b2011-01-14 15:47:37 -0600643 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500644 {
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500645 /* Caller called png_start_read_image or png_read_update_info without
646 * first turning on the PNG_INTERLACE transform. We can fix this here,
647 * but the caller should do it!
648 */
649 png_warning(png_ptr, "Interlace handling should be turned on when "
650 "using png_read_image");
651 /* Make sure this is set correctly */
652 png_ptr->num_rows = png_ptr->height;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500653 }
654
655 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
656 * the above error case.
657 */
658 pass = png_set_interlace_handling(png_ptr);
659 }
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500660#else
661 if (png_ptr->interlaced)
662 png_error(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600663 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500664
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500665 pass = 1;
666#endif
667
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500668 image_height=png_ptr->height;
Guy Schalnate5a37791996-06-05 15:50:50 -0500669
Guy Schalnat0d580581995-07-20 02:43:20 -0500670 for (j = 0; j < pass; j++)
671 {
672 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500673 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500674 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500675 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500676 rp++;
677 }
678 }
679}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500680#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500681
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500682#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500683/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600684 * file, will verify the end is accurate, and will read any comments
685 * or time information at the end of the file, if info is not NULL.
686 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500687void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600688png_read_end(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500689{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600690#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
691 int keep;
692#endif
693
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500694 png_debug(1, "in png_read_end");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500695
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500696 if (png_ptr == NULL)
697 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500698
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600699 /* If png_read_end is called in the middle of reading the rows there may
700 * still be pending IDAT data and an owned zstream. Deal with this here.
701 */
702#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
703 if (!png_chunk_unknown_handling(png_ptr, png_IDAT))
704#endif
705 png_read_finish_IDAT(png_ptr);
706
707#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
708 /* Report invalid palette index; added at libng-1.5.10 */
709 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
710 png_ptr->num_palette_max > png_ptr->num_palette)
711 png_benign_error(png_ptr, "Read palette index exceeding num_palette");
712#endif
John Bowler6f237b62012-03-02 13:13:15 -0600713
Guy Schalnat0d580581995-07-20 02:43:20 -0500714 do
715 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500716 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500717 png_uint_32 chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500718
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500719 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600720 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500721
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500722 else if (chunk_name == png_IEND)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600723 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500724
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600725#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600726 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600727 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500728 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600729 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500730 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500731 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600732 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600733 png_handle_unknown(png_ptr, info_ptr, length, keep);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500734 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600735 png_ptr->mode |= PNG_HAVE_PLTE;
736 }
737#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500738
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500739 else if (chunk_name == png_IDAT)
Guy Schalnat0d580581995-07-20 02:43:20 -0500740 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500741 /* Zero length IDATs are legal after the last IDAT has been
742 * read, but not after other chunks have been read.
743 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500744 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500745 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500746
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500747 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500748 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500749 else if (chunk_name == png_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500750 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500751
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500752#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500753 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500754 png_handle_bKGD(png_ptr, info_ptr, length);
755#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500756
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500757#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500758 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500759 png_handle_cHRM(png_ptr, info_ptr, length);
760#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500761
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500762#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500763 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500764 png_handle_gAMA(png_ptr, info_ptr, length);
765#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500766
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500767#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500768 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500769 png_handle_hIST(png_ptr, info_ptr, length);
770#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500771
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500772#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500773 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500774 png_handle_oFFs(png_ptr, info_ptr, length);
775#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500776
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500777#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500778 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500779 png_handle_pCAL(png_ptr, info_ptr, length);
780#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500781
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500782#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500783 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600784 png_handle_sCAL(png_ptr, info_ptr, length);
785#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500786
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500787#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500788 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500789 png_handle_pHYs(png_ptr, info_ptr, length);
790#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500791
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500792#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500793 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500794 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500795#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500796
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500797#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500798 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600799 png_handle_sRGB(png_ptr, info_ptr, length);
800#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500801
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500802#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500803 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600804 png_handle_iCCP(png_ptr, info_ptr, length);
805#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500806
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500807#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500808 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600809 png_handle_sPLT(png_ptr, info_ptr, length);
810#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500811
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500812#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500813 else if (chunk_name == png_tEXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600814 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500815#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500816
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500817#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500818 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500819 png_handle_tIME(png_ptr, info_ptr, length);
820#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500821
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500822#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500823 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500824 png_handle_tRNS(png_ptr, info_ptr, length);
825#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500826
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500827#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500828 else if (chunk_name == png_zTXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600829 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500830#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500831
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500832#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500833 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600834 png_handle_iTXt(png_ptr, info_ptr, length);
835#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500836
Guy Schalnat0d580581995-07-20 02:43:20 -0500837 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600838 png_handle_unknown(png_ptr, info_ptr, length,
839 PNG_HANDLE_CHUNK_AS_DEFAULT);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600840 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500841}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500842#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500843
John Bowler1d7f56a2011-12-21 20:14:23 -0600844/* Free all memory used in the read struct */
845static void
John Bowler5d567862011-12-24 09:12:00 -0600846png_read_destroy(png_structrp png_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500847{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500848 png_debug(1, "in png_read_destroy");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500849
John Bowler07772cb2011-10-14 18:19:47 -0500850#ifdef PNG_READ_GAMMA_SUPPORTED
851 png_destroy_gamma_table(png_ptr);
852#endif
853
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -0500854 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgard1c422762011-10-17 16:52:19 -0500855 png_free(png_ptr, png_ptr->big_prev_row);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600856 png_free(png_ptr, png_ptr->read_buffer);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500857
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500858#ifdef PNG_READ_QUANTIZE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600859 png_free(png_ptr, png_ptr->palette_lookup);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500860 png_free(png_ptr, png_ptr->quantize_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500861#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500862
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600863 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600864 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600865 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500866
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600867#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500868 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600869 if (png_ptr->free_me & PNG_FREE_TRNS)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500870 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600871 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500872#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500873
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600874 inflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500875
Guy Schalnat6d764711995-12-19 03:22:19 -0600876#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600877 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -0600878#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500879
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600880#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) &&\
881 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
John Bowler40b26032011-12-22 08:09:15 -0600882 png_free(png_ptr, png_ptr->unknown_chunk.data);
883#endif
884
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600885#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
John Bowler40b26032011-12-22 08:09:15 -0600886 png_free(png_ptr, png_ptr->chunk_list);
887#endif
888
John Bowler1d7f56a2011-12-21 20:14:23 -0600889 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
890 * callbacks are still set at this point. They are required to complete the
891 * destruction of the png_struct itself.
Guy Schalnate5a37791996-06-05 15:50:50 -0500892 */
John Bowler1d7f56a2011-12-21 20:14:23 -0600893}
Guy Schalnate5a37791996-06-05 15:50:50 -0500894
John Bowler1d7f56a2011-12-21 20:14:23 -0600895/* Free all memory used by the read */
896void PNGAPI
897png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
898 png_infopp end_info_ptr_ptr)
899{
John Bowler5d567862011-12-24 09:12:00 -0600900 png_structrp png_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500901
John Bowler1d7f56a2011-12-21 20:14:23 -0600902 png_debug(1, "in png_destroy_read_struct");
Guy Schalnate5a37791996-06-05 15:50:50 -0500903
John Bowler1d7f56a2011-12-21 20:14:23 -0600904 if (png_ptr_ptr != NULL)
905 png_ptr = *png_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500906
John Bowler1d7f56a2011-12-21 20:14:23 -0600907 if (png_ptr == NULL)
908 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600909
John Bowler1d7f56a2011-12-21 20:14:23 -0600910 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
911 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
912 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
913 */
914 png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
915 png_destroy_info_struct(png_ptr, info_ptr_ptr);
916
917 *png_ptr_ptr = NULL;
918 png_read_destroy(png_ptr);
919 png_destroy_png_struct(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500920}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600921
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500922void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600923png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600924{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500925 if (png_ptr == NULL)
926 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500927
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600928 png_ptr->read_row_fn = read_row_fn;
929}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600930
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500931
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500932#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500933#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500934void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600935png_read_png(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600936 int transforms,
937 voidp params)
938{
939 int row;
940
John Bowler6a6d79f2011-02-12 08:56:40 -0600941 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500942 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600943
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500944 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600945 * PNG file before the first IDAT (image data chunk).
946 */
947 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600948 if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500949 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600950
951 /* -------------- image transformations start here ------------------- */
952
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500953#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -0500954 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500955 */
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -0500956 if (transforms & PNG_TRANSFORM_SCALE_16)
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500957 {
Glenn Randers-Pehrson6da2f2d2011-06-17 23:07:16 -0500958 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
959 * did in earlier versions, while "scale_16" is now more accurate.
960 */
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500961 png_set_scale_16(png_ptr);
962 }
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500963#endif
964
965#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
John Bowler8d261262011-06-18 13:37:11 -0500966 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
967 * latter by doing SCALE first. This is ok and allows apps not to check for
968 * which is supported to get the right answer.
969 */
970 if (transforms & PNG_TRANSFORM_STRIP_16)
971 png_set_strip_16(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600972#endif
973
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500974#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500975 /* Strip alpha bytes from the input data without combining with
976 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600977 */
978 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500979 png_set_strip_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600980#endif
981
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600982#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500983 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600984 * byte into separate bytes (useful for paletted and grayscale images).
985 */
986 if (transforms & PNG_TRANSFORM_PACKING)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500987 png_set_packing(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600988#endif
989
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500990#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600991 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500992 * (not useful if you are using png_set_packing).
993 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600994 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500995 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600996#endif
997
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500998#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600999 /* Expand paletted colors into true RGB triplets
1000 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1001 * Expand paletted or RGB images with transparency to full alpha
1002 * channels so the data will be available as RGBA quartets.
1003 */
1004 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001005 if ((png_ptr->bit_depth < 8) ||
1006 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1007 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001008 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001009#endif
1010
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001011 /* We don't handle background color or gamma transformation or quantizing.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001012 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001013
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001014#ifdef PNG_READ_INVERT_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -05001015 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001016 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001017 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001018 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001019#endif
1020
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001021#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001022 /* If you want to shift the pixel values from the range [0,255] or
1023 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1024 * colors were originally in:
1025 */
1026 if ((transforms & PNG_TRANSFORM_SHIFT)
1027 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1028 {
1029 png_color_8p sig_bit;
1030
1031 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1032 png_set_shift(png_ptr, sig_bit);
1033 }
1034#endif
1035
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001036#ifdef PNG_READ_BGR_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001037 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001038 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001039 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001040#endif
1041
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001042#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001043 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001044 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001045 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001046#endif
1047
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001048#ifdef PNG_READ_SWAP_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -05001049 /* Swap bytes of 16-bit files to least significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001050 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001051 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001052#endif
1053
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001054/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001055#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001056 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001057 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001058 png_set_invert_alpha(png_ptr);
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001059#endif
1060
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001061/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001062#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001063 /* Expand grayscale image to RGB */
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001064 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001065 png_set_gray_to_rgb(png_ptr);
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001066#endif
1067
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001068/* Added at libpng-1.5.4 */
John Bowler96cec0e2011-05-08 22:48:12 -05001069#ifdef PNG_READ_EXPAND_16_SUPPORTED
1070 if (transforms & PNG_TRANSFORM_EXPAND_16)
1071 png_set_expand_16(png_ptr);
1072#endif
1073
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001074 /* We don't handle adding filler bytes */
1075
John Bowler6a6d79f2011-02-12 08:56:40 -06001076 /* We use png_read_image and rely on that for interlace handling, but we also
1077 * call png_read_update_info therefore must turn on interlace handling now:
1078 */
1079 (void)png_set_interlace_handling(png_ptr);
1080
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001081 /* Optional call to gamma correct and add the background to the palette
1082 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001083 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001084 */
1085 png_read_update_info(png_ptr, info_ptr);
1086
1087 /* -------------- image transformations end here ------------------- */
1088
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001089 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001090 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001091 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001092 png_uint_32 iptr;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001093
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001094 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001095 info_ptr->height * (sizeof (png_bytep)));
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001096 for (iptr=0; iptr<info_ptr->height; iptr++)
1097 info_ptr->row_pointers[iptr] = NULL;
1098
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001099 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001100
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001101 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001102 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001103 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001104 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001105
1106 png_read_image(png_ptr, info_ptr->row_pointers);
1107 info_ptr->valid |= PNG_INFO_IDAT;
1108
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001109 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001110 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001111
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001112 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1113 PNG_UNUSED(params)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001114
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001115}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001116#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001117#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
John Bowler7875d532011-11-07 22:33:49 -06001118
1119#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1120/* SIMPLIFIED READ
1121 *
1122 * This code currently relies on the sequential reader, though it could easily
1123 * be made to work with the progressive one.
1124 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001125/* Arguments to png_image_finish_read: */
1126
1127/* Encoding of PNG data (used by the color-map code) */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001128# define P_NOTSET 0 /* File encoding not yet known */
1129# define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
1130# define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1131# define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1132# define P_LINEAR8 4 /* 8-bit linear: only from a file value */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001133
1134/* Color-map processing: after libpng has run on the PNG image further
1135 * processing may be needed to conver the data to color-map indicies.
1136 */
1137#define PNG_CMAP_NONE 0
1138#define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1139#define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1140#define PNG_CMAP_RGB 3 /* Process RGB data */
1141#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1142
1143/* The following document where the background is for each processing case. */
1144#define PNG_CMAP_NONE_BACKGROUND 256
1145#define PNG_CMAP_GA_BACKGROUND 231
1146#define PNG_CMAP_TRANS_BACKGROUND 254
1147#define PNG_CMAP_RGB_BACKGROUND 256
1148#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1149
1150typedef struct
1151{
1152 /* Arguments: */
1153 png_imagep image;
1154 png_voidp buffer;
1155 png_int_32 row_stride;
1156 png_voidp colormap;
1157 png_const_colorp background;
1158 /* Local variables: */
1159 png_voidp local_row;
1160 png_voidp first_row;
1161 ptrdiff_t row_bytes; /* step between rows */
1162 int file_encoding; /* E_ values above */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001163 png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001164 int colormap_processing; /* PNG_CMAP_ values above */
1165} png_image_read_control;
1166
John Bowler7875d532011-11-07 22:33:49 -06001167/* Do all the *safe* initialization - 'safe' means that png_error won't be
John Bowler18c5cfa2011-11-16 14:26:34 -06001168 * called, so setting up the jmp_buf is not required. This means that anything
1169 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1170 * instead so that control is returned safely back to this routine.
John Bowler7875d532011-11-07 22:33:49 -06001171 */
1172static int
1173png_image_read_init(png_imagep image)
1174{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001175 if (image->opaque == NULL)
1176 {
1177 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
John Bowler7875d532011-11-07 22:33:49 -06001178 png_safe_error, png_safe_warning);
1179
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001180 /* And set the rest of the structure to NULL to ensure that the various
1181 * fields are consistent.
1182 */
1183 memset(image, 0, (sizeof *image));
1184 image->version = PNG_IMAGE_VERSION;
John Bowler7875d532011-11-07 22:33:49 -06001185
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001186 if (png_ptr != NULL)
John Bowler7875d532011-11-07 22:33:49 -06001187 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001188 png_infop info_ptr = png_create_info_struct(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001189
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001190 if (info_ptr != NULL)
John Bowler7875d532011-11-07 22:33:49 -06001191 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001192 png_controlp control = png_voidcast(png_controlp,
1193 png_malloc_warn(png_ptr, (sizeof *control)));
John Bowler7875d532011-11-07 22:33:49 -06001194
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001195 if (control != NULL)
1196 {
1197 memset(control, 0, (sizeof *control));
John Bowler7875d532011-11-07 22:33:49 -06001198
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001199 control->png_ptr = png_ptr;
1200 control->info_ptr = info_ptr;
1201 control->for_write = 0;
1202
1203 image->opaque = control;
1204 return 1;
1205 }
1206
1207 /* Error clean up */
1208 png_destroy_info_struct(png_ptr, &info_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001209 }
1210
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001211 png_destroy_read_struct(&png_ptr, NULL, NULL);
John Bowler7875d532011-11-07 22:33:49 -06001212 }
1213
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001214 return png_image_error(image, "png_image_read: out of memory");
John Bowler7875d532011-11-07 22:33:49 -06001215 }
1216
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001217 return png_image_error(image, "png_image_read: opaque pointer not NULL");
John Bowler7875d532011-11-07 22:33:49 -06001218}
1219
1220/* Utility to find the base format of a PNG file from a png_struct. */
1221static png_uint_32
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001222png_image_format(png_structrp png_ptr)
John Bowler7875d532011-11-07 22:33:49 -06001223{
1224 png_uint_32 format = 0;
1225
1226 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1227 format |= PNG_FORMAT_FLAG_COLOR;
1228
1229 if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1230 format |= PNG_FORMAT_FLAG_ALPHA;
1231
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001232 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1233 * sets the png_struct fields; that's all we are interested in here. The
1234 * precise interaction with an app call to png_set_tRNS and PNG file reading
1235 * is unclear.
1236 */
1237 else if (png_ptr->num_trans > 0)
John Bowler7875d532011-11-07 22:33:49 -06001238 format |= PNG_FORMAT_FLAG_ALPHA;
1239
1240 if (png_ptr->bit_depth == 16)
1241 format |= PNG_FORMAT_FLAG_LINEAR;
1242
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001243 if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE)
1244 format |= PNG_FORMAT_FLAG_COLORMAP;
1245
John Bowler7875d532011-11-07 22:33:49 -06001246 return format;
1247}
1248
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001249/* Is the given gamma significantly different from sRGB? The test is the same
1250 * one used in pngrtran.c when deciding whether to do gamma correction. The
1251 * arithmetic optimizes the division by using the fact that the inverse of the
1252 * file sRGB gamma is 2.2
1253 */
1254static int
1255png_gamma_not_sRGB(png_fixed_point g)
1256{
1257 if (g < PNG_FP_1)
1258 {
1259 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
1260 if (g == 0)
1261 return 0;
1262
1263 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1264 }
1265
1266 return 1;
1267}
1268
John Bowler7875d532011-11-07 22:33:49 -06001269/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1270 * header and fill in all the information. This is executed in a safe context,
1271 * unlike the init routine above.
1272 */
1273static int
1274png_image_read_header(png_voidp argument)
1275{
John Bowler4fa96a42011-11-16 16:39:16 -06001276 png_imagep image = png_voidcast(png_imagep, argument);
John Bowler5d567862011-12-24 09:12:00 -06001277 png_structrp png_ptr = image->opaque->png_ptr;
1278 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001279
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001280 png_set_benign_errors(png_ptr, 1/*warn*/);
John Bowler7875d532011-11-07 22:33:49 -06001281 png_read_info(png_ptr, info_ptr);
1282
1283 /* Do this the fast way; just read directly out of png_struct. */
1284 image->width = png_ptr->width;
1285 image->height = png_ptr->height;
1286
1287 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001288 png_uint_32 format = png_image_format(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001289
1290 image->format = format;
John Bowler04336ba2012-01-16 07:48:36 -06001291
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001292#ifdef PNG_COLORSPACE_SUPPORTED
1293 /* Does the colorspace match sRGB? If there is no color endpoint
1294 * (colorant) information assume yes, otherwise require the
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001295 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001296 * colorspace has been determined to be invalid ignore it.
1297 */
1298 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
1299 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
1300 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
1301 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1302#endif
1303 }
1304
1305 /* We need the maximum number of entries regardless of the format the
1306 * application sets here.
1307 */
1308 {
1309 png_uint_32 cmap_entries;
1310
1311 switch (png_ptr->color_type)
John Bowler5bc90382012-01-23 22:43:22 -06001312 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001313 case PNG_COLOR_TYPE_GRAY:
1314 cmap_entries = 1U << png_ptr->bit_depth;
1315 break;
John Bowler5bc90382012-01-23 22:43:22 -06001316
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001317 case PNG_COLOR_TYPE_PALETTE:
1318 cmap_entries = png_ptr->num_palette;
1319 break;
1320
1321 default:
1322 cmap_entries = 256;
1323 break;
John Bowler5bc90382012-01-23 22:43:22 -06001324 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001325
1326 if (cmap_entries > 256)
1327 cmap_entries = 256;
1328
1329 image->colormap_entries = cmap_entries;
John Bowler5bc90382012-01-23 22:43:22 -06001330 }
1331
John Bowler7875d532011-11-07 22:33:49 -06001332 return 1;
1333}
1334
1335#ifdef PNG_STDIO_SUPPORTED
1336int PNGAPI
1337png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1338{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001339 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001340 {
1341 if (file != NULL)
1342 {
1343 if (png_image_read_init(image))
1344 {
1345 /* This is slightly evil, but png_init_io doesn't do anything other
1346 * than this and we haven't changed the standard IO functions so
1347 * this saves a 'safe' function.
1348 */
1349 image->opaque->png_ptr->io_ptr = file;
1350 return png_safe_execute(image, png_image_read_header, image);
1351 }
1352 }
1353
1354 else
1355 return png_image_error(image,
1356 "png_image_begin_read_from_stdio: invalid argument");
1357 }
1358
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001359 else if (image != NULL)
1360 return png_image_error(image,
1361 "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
1362
John Bowler7875d532011-11-07 22:33:49 -06001363 return 0;
1364}
1365
1366int PNGAPI
1367png_image_begin_read_from_file(png_imagep image, const char *file_name)
1368{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001369 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001370 {
1371 if (file_name != NULL)
1372 {
1373 FILE *fp = fopen(file_name, "rb");
1374
1375 if (fp != NULL)
1376 {
1377 if (png_image_read_init(image))
1378 {
1379 image->opaque->png_ptr->io_ptr = fp;
1380 image->opaque->owned_file = 1;
1381 return png_safe_execute(image, png_image_read_header, image);
1382 }
1383
1384 /* Clean up: just the opened file. */
1385 (void)fclose(fp);
1386 }
1387
1388 else
1389 return png_image_error(image, strerror(errno));
1390 }
1391
1392 else
1393 return png_image_error(image,
1394 "png_image_begin_read_from_file: invalid argument");
1395 }
1396
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001397 else if (image != NULL)
1398 return png_image_error(image,
1399 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
1400
John Bowler7875d532011-11-07 22:33:49 -06001401 return 0;
1402}
1403#endif /* PNG_STDIO_SUPPORTED */
1404
1405static void PNGCBAPI
1406png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
1407{
1408 if (png_ptr != NULL)
1409 {
John Bowler4fa96a42011-11-16 16:39:16 -06001410 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001411 if (image != NULL)
1412 {
1413 png_controlp cp = image->opaque;
1414 if (cp != NULL)
1415 {
1416 png_const_bytep memory = cp->memory;
1417 png_size_t size = cp->size;
1418
1419 if (memory != NULL && size >= need)
1420 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001421 memcpy(out, memory, need);
John Bowler7875d532011-11-07 22:33:49 -06001422 cp->memory = memory + need;
1423 cp->size = size - need;
1424 return;
1425 }
1426
1427 png_error(png_ptr, "read beyond end of data");
1428 }
1429 }
1430
1431 png_error(png_ptr, "invalid memory read");
1432 }
1433}
1434
1435int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1436 png_const_voidp memory, png_size_t size)
1437{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001438 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001439 {
1440 if (memory != NULL && size > 0)
1441 {
1442 if (png_image_read_init(image))
1443 {
1444 /* Now set the IO functions to read from the memory buffer and
1445 * store it into io_ptr. Again do this in-place to avoid calling a
1446 * libpng function that requires error handling.
1447 */
John Bowler4fa96a42011-11-16 16:39:16 -06001448 image->opaque->memory = png_voidcast(png_const_bytep, memory);
John Bowler7875d532011-11-07 22:33:49 -06001449 image->opaque->size = size;
1450 image->opaque->png_ptr->io_ptr = image;
1451 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1452
1453 return png_safe_execute(image, png_image_read_header, image);
1454 }
1455 }
1456
1457 else
1458 return png_image_error(image,
1459 "png_image_begin_read_from_memory: invalid argument");
1460 }
1461
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001462 else if (image != NULL)
1463 return png_image_error(image,
1464 "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
1465
John Bowler7875d532011-11-07 22:33:49 -06001466 return 0;
1467}
1468
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001469/* Utility function to skip chunks that are not used by the simplified image
1470 * read functions and an appropriate macro to call it.
1471 */
1472#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1473static void
1474png_image_skip_unused_chunks(png_structrp png_ptr)
John Bowler04336ba2012-01-16 07:48:36 -06001475{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001476 /* Prepare the reader to ignore all recognized chunks whose data will not
1477 * be used, i.e., all chunks recognized by libpng except for those
1478 * involved in basic image reading:
1479 *
1480 * IHDR, PLTE, IDAT, IEND
1481 *
1482 * Or image data handling:
1483 *
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001484 * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001485 *
1486 * This provides a small performance improvement and eliminates any
1487 * potential vulnerability to security problems in the unused chunks.
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001488 *
1489 * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
1490 * too. This allows the simplified API to be compiled without iCCP support,
1491 * however if the support is there the chunk is still checked to detect
1492 * errors (which are unfortunately quite common.)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001493 */
1494 {
1495 static PNG_CONST png_byte chunks_to_process[] = {
1496 98, 75, 71, 68, '\0', /* bKGD */
1497 99, 72, 82, 77, '\0', /* cHRM */
1498 103, 65, 77, 65, '\0', /* gAMA */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001499# ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001500 105, 67, 67, 80, '\0', /* iCCP */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001501# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001502 115, 66, 73, 84, '\0', /* sBIT */
1503 115, 82, 71, 66, '\0', /* sRGB */
1504 };
John Bowler5bc90382012-01-23 22:43:22 -06001505
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001506 /* Ignore unknown chunks and all other chunks except for the
1507 * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
1508 */
1509 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
1510 NULL, -1);
1511
1512 /* But do not ignore image data handling chunks */
1513 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
1514 chunks_to_process, (sizeof chunks_to_process)/5);
1515 }
1516}
1517
1518# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1519#else
1520# define PNG_SKIP_CHUNKS(p) ((void)0)
1521#endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */
1522
1523/* The following macro gives the exact rounded answer for all values in the
1524 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1525 * the correct numbers 0..5
1526 */
1527#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1528
1529/* Utility functions to make particular color-maps */
1530static void
1531set_file_encoding(png_image_read_control *display)
1532{
1533 png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma;
1534 if (png_gamma_significant(g))
1535 {
1536 if (png_gamma_not_sRGB(g))
1537 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001538 display->file_encoding = P_FILE;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001539 display->gamma_to_linear = png_reciprocal(g);
1540 }
1541
1542 else
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001543 display->file_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001544 }
1545
1546 else
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001547 display->file_encoding = P_LINEAR8;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001548}
1549
1550static unsigned int
1551decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
1552{
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001553 if (encoding == P_FILE) /* double check */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001554 encoding = display->file_encoding;
1555
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001556 if (encoding == P_NOTSET) /* must be the file encoding */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001557 {
1558 set_file_encoding(display);
1559 encoding = display->file_encoding;
1560 }
1561
1562 switch (encoding)
1563 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001564 case P_FILE:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001565 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
1566 break;
1567
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001568 case P_sRGB:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001569 value = png_sRGB_table[value];
1570 break;
1571
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001572 case P_LINEAR:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001573 break;
1574
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001575 case P_LINEAR8:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001576 value *= 257;
1577 break;
1578
1579 default:
1580 png_error(display->image->opaque->png_ptr,
1581 "unexpected encoding (internal error)");
1582 break;
1583 }
1584
1585 return value;
1586}
1587
1588static png_uint_32
1589png_colormap_compose(png_image_read_control *display,
1590 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
1591 png_uint_32 background, int encoding)
1592{
1593 /* The file value is composed on the background, the background has the given
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001594 * encoding and so does the result, the file is encoded with P_FILE and the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001595 * file and alpha are 8-bit values. The (output) encoding will always be
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001596 * P_LINEAR or P_sRGB.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001597 */
1598 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
1599 png_uint_32 b = decode_gamma(display, background, encoding);
1600
1601 /* The alpha is always an 8-bit value (it comes from the palette), the value
1602 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1603 */
1604 f = f * alpha + b * (255-alpha);
1605
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001606 if (encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001607 {
1608 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1609 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1610 */
1611 f *= 257; /* Now scaled by 65535 */
1612 f += f >> 16;
1613 f = (f+32768) >> 16;
1614 }
1615
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001616 else /* P_sRGB */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001617 f = PNG_sRGB_FROM_LINEAR(f);
1618
1619 return f;
1620}
1621
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001622/* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001623 * be 8-bit.
1624 */
1625static void
1626png_create_colormap_entry(png_image_read_control *display,
1627 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
1628 png_uint_32 alpha, int encoding)
1629{
1630 png_imagep image = display->image;
1631 const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ?
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001632 P_LINEAR : P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001633 const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
1634 (red != green || green != blue);
1635
1636 if (ip > 255)
1637 png_error(image->opaque->png_ptr, "color-map index out of range");
1638
1639 /* Update the cache with whether the file gamma is significantly different
1640 * from sRGB.
1641 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001642 if (encoding == P_FILE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001643 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001644 if (display->file_encoding == P_NOTSET)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001645 set_file_encoding(display);
1646
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001647 /* Note that the cached value may be P_FILE too, but if it is then the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001648 * gamma_to_linear member has been set.
1649 */
1650 encoding = display->file_encoding;
1651 }
1652
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001653 if (encoding == P_FILE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001654 {
1655 png_fixed_point g = display->gamma_to_linear;
1656
1657 red = png_gamma_16bit_correct(red*257, g);
1658 green = png_gamma_16bit_correct(green*257, g);
1659 blue = png_gamma_16bit_correct(blue*257, g);
1660
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001661 if (convert_to_Y || output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001662 {
1663 alpha *= 257;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001664 encoding = P_LINEAR;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001665 }
1666
1667 else
1668 {
1669 red = PNG_sRGB_FROM_LINEAR(red * 255);
1670 green = PNG_sRGB_FROM_LINEAR(green * 255);
1671 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001672 encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001673 }
1674 }
1675
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001676 else if (encoding == P_LINEAR8)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001677 {
1678 /* This encoding occurs quite frequently in test cases because PngSuite
1679 * includes a gAMA 1.0 chunk with most images.
1680 */
1681 red *= 257;
1682 green *= 257;
1683 blue *= 257;
1684 alpha *= 257;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001685 encoding = P_LINEAR;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001686 }
1687
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001688 else if (encoding == P_sRGB && (convert_to_Y || output_encoding == P_LINEAR))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001689 {
1690 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1691 * linear.
1692 */
1693 red = png_sRGB_table[red];
1694 green = png_sRGB_table[green];
1695 blue = png_sRGB_table[blue];
1696 alpha *= 257;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001697 encoding = P_LINEAR;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001698 }
1699
1700 /* This is set if the color isn't gray but the output is. */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001701 if (encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001702 {
1703 if (convert_to_Y)
1704 {
1705 /* NOTE: these values are copied from png_do_rgb_to_gray */
1706 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
1707 (png_uint_32)2366 * blue;
1708
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001709 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001710 y = (y + 16384) >> 15;
1711
1712 else
1713 {
1714 /* y is scaled by 32768, we need it scaled by 255: */
1715 y = (y + 128) >> 8;
1716 y *= 255;
1717 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001718 encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001719 }
1720
1721 blue = red = green = y;
1722 }
1723
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001724 else if (output_encoding == P_sRGB)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001725 {
1726 red = PNG_sRGB_FROM_LINEAR(red * 255);
1727 green = PNG_sRGB_FROM_LINEAR(green * 255);
1728 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1729 alpha = PNG_DIV257(alpha);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001730 encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001731 }
1732 }
1733
1734 if (encoding != output_encoding)
1735 png_error(image->opaque->png_ptr, "bad encoding (internal error)");
1736
1737 /* Store the value. */
1738 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001739# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001740 const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1741 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
1742# else
1743# define afirst 0
1744# endif
1745# ifdef PNG_FORMAT_BGR_SUPPORTED
1746 const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
1747# else
1748# define bgr 0
1749# endif
1750
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001751 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001752 {
1753 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
1754
1755 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1756
1757 /* The linear 16-bit values must be pre-multiplied by the alpha channel
1758 * value, if less than 65535 (this is, effectively, composite on black
1759 * if the alpha channel is removed.)
1760 */
1761 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1762 {
1763 case 4:
1764 entry[afirst ? 0 : 3] = (png_uint_16)alpha;
1765 /* FALL THROUGH */
1766
1767 case 3:
1768 if (alpha < 65535)
1769 {
1770 if (alpha > 0)
1771 {
1772 blue = (blue * alpha + 32767U)/65535U;
1773 green = (green * alpha + 32767U)/65535U;
1774 red = (red * alpha + 32767U)/65535U;
1775 }
1776
1777 else
1778 red = green = blue = 0;
1779 }
1780 entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
1781 entry[afirst + 1] = (png_uint_16)green;
1782 entry[afirst + bgr] = (png_uint_16)red;
1783 break;
1784
1785 case 2:
1786 entry[1 ^ afirst] = (png_uint_16)alpha;
1787 /* FALL THROUGH */
1788
1789 case 1:
1790 if (alpha < 65535)
1791 {
1792 if (alpha > 0)
1793 green = (green * alpha + 32767U)/65535U;
1794
1795 else
1796 green = 0;
1797 }
1798 entry[afirst] = (png_uint_16)green;
1799 break;
1800
1801 default:
1802 break;
1803 }
1804 }
1805
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001806 else /* output encoding is P_sRGB */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001807 {
1808 png_bytep entry = png_voidcast(png_bytep, display->colormap);
1809
1810 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1811
1812 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1813 {
1814 case 4:
1815 entry[afirst ? 0 : 3] = (png_byte)alpha;
1816 case 3:
1817 entry[afirst + (2 ^ bgr)] = (png_byte)blue;
1818 entry[afirst + 1] = (png_byte)green;
1819 entry[afirst + bgr] = (png_byte)red;
1820 break;
1821
1822 case 2:
1823 entry[1 ^ afirst] = (png_byte)alpha;
1824 case 1:
1825 entry[afirst] = (png_byte)green;
1826 break;
1827
1828 default:
1829 break;
1830 }
1831 }
1832
1833# ifdef afirst
1834# undef afirst
1835# endif
1836# ifdef bgr
1837# undef bgr
1838# endif
1839 }
1840}
1841
John Bowler7875d532011-11-07 22:33:49 -06001842static int
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001843make_gray_file_colormap(png_image_read_control *display)
1844{
1845 unsigned int i;
1846
1847 for (i=0; i<256; ++i)
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001848 png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001849
1850 return i;
1851}
1852
1853static int
1854make_gray_colormap(png_image_read_control *display)
1855{
1856 unsigned int i;
1857
1858 for (i=0; i<256; ++i)
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001859 png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001860
1861 return i;
1862}
1863#define PNG_GRAY_COLORMAP_ENTRIES 256
1864
1865static int
1866make_ga_colormap(png_image_read_control *display)
1867{
1868 unsigned int i, a;
1869
1870 /* Alpha is retained, the output will be a color-map with entries
1871 * selected by six levels of alpha. One transparent entry, 6 gray
1872 * levels for all the intermediate alpha values, leaving 230 entries
1873 * for the opaque grays. The color-map entries are the six values
1874 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
1875 * relevant entry.
1876 *
1877 * if (alpha > 229) // opaque
1878 * {
1879 * // The 231 entries are selected to make the math below work:
1880 * base = 0;
1881 * entry = (231 * gray + 128) >> 8;
1882 * }
1883 * else if (alpha < 26) // transparent
1884 * {
1885 * base = 231;
1886 * entry = 0;
1887 * }
1888 * else // partially opaque
1889 * {
1890 * base = 226 + 6 * PNG_DIV51(alpha);
1891 * entry = PNG_DIV51(gray);
1892 * }
1893 */
1894 i = 0;
1895 while (i < 231)
1896 {
1897 unsigned int gray = (i * 256 + 115) / 231;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001898 png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001899 }
1900
1901 /* 255 is used here for the component values for consistency with the code
1902 * that undoes premultiplication in pngwrite.c.
1903 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001904 png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001905
1906 for (a=1; a<5; ++a)
1907 {
1908 unsigned int g;
1909
1910 for (g=0; g<6; ++g)
1911 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001912 P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001913 }
1914
1915 return i;
1916}
1917
1918#define PNG_GA_COLORMAP_ENTRIES 256
1919
1920static int
1921make_rgb_colormap(png_image_read_control *display)
1922{
1923 unsigned int i, r;
1924
1925 /* Build a 6x6x6 opaque RGB cube */
1926 for (i=r=0; r<6; ++r)
1927 {
1928 unsigned int g;
1929
1930 for (g=0; g<6; ++g)
1931 {
1932 unsigned int b;
1933
1934 for (b=0; b<6; ++b)
1935 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001936 P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001937 }
1938 }
1939
1940 return i;
1941}
1942
1943#define PNG_RGB_COLORMAP_ENTRIES 216
1944
1945/* Return a palette index to the above palette given three 8-bit sRGB values. */
1946#define PNG_RGB_INDEX(r,g,b) \
1947 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
1948
1949static int
1950png_image_read_colormap(png_voidp argument)
1951{
1952 png_image_read_control *display =
1953 png_voidcast(png_image_read_control*, argument);
1954 const png_imagep image = display->image;
1955
1956 const png_structrp png_ptr = image->opaque->png_ptr;
1957 const png_uint_32 output_format = image->format;
1958 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ?
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001959 P_LINEAR : P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001960
1961 unsigned int cmap_entries;
1962 unsigned int output_processing; /* Output processing option */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001963 unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001964
1965 /* Background information; the background color and the index of this color
1966 * in the color-map if it exists (else 256).
1967 */
1968 unsigned int background_index = 256;
1969 png_uint_32 back_r, back_g, back_b;
1970
1971 /* Flags to accumulate things that need to be done to the input. */
1972 int expand_tRNS = 0;
1973
1974 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
1975 * very difficult to do, the results look awful, and it is difficult to see
1976 * what possible use it is because the application can't control the
1977 * color-map.
1978 */
1979 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
1980 png_ptr->num_trans > 0) /* alpha in input */ &&
1981 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
1982 {
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001983 if (output_encoding == P_LINEAR) /* compose on black */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001984 back_b = back_g = back_r = 0;
1985
1986 else if (display->background == NULL /* no way to remove it */)
1987 png_error(png_ptr,
1988 "a background color must be supplied to remove alpha/transparency");
1989
1990 /* Get a copy of the background color (this avoids repeating the checks
1991 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
1992 * output format.
1993 */
1994 else
1995 {
1996 back_g = display->background->green;
1997 if (output_format & PNG_FORMAT_FLAG_COLOR)
1998 {
1999 back_r = display->background->red;
2000 back_b = display->background->blue;
2001 }
2002 else
2003 back_b = back_r = back_g;
2004 }
2005 }
2006
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002007 else if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002008 back_b = back_r = back_g = 65535;
2009
2010 else
2011 back_b = back_r = back_g = 255;
2012
John Bowler59ae3892013-03-06 22:15:25 -06002013 /* Default the input file gamma if required - this is necessary because
2014 * libpng assumes that if no gamma information is present the data is in the
2015 * output format, but the simplified API deduces the gamma from the input
2016 * format.
2017 */
2018 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0)
2019 {
2020 /* Do this directly, not using the png_colorspace functions, to ensure
2021 * that it happens even if the colorspace is invalid (though probably if
2022 * it is the setting will be ignored) Note that the same thing can be
2023 * achieved at the application interface with png_set_gAMA.
2024 */
2025 if (png_ptr->bit_depth == 16 &&
2026 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
2027 png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR;
2028
2029 else
2030 png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE;
2031
2032 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
2033 }
2034
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002035 /* Decide what to do based on the PNG color type of the input data. The
2036 * utility function png_create_colormap_entry deals with most aspects of the
2037 * output transformations; this code works out how to produce bytes of
2038 * color-map entries from the original format.
2039 */
2040 switch (png_ptr->color_type)
2041 {
2042 case PNG_COLOR_TYPE_GRAY:
2043 if (png_ptr->bit_depth <= 8)
2044 {
2045 /* There at most 256 colors in the output, regardless of
2046 * transparency.
2047 */
2048 unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
2049
2050 cmap_entries = 1U << png_ptr->bit_depth;
2051 if (cmap_entries > image->colormap_entries)
2052 png_error(png_ptr, "gray[8] color-map: too few entries");
2053
2054 step = 255 / (cmap_entries - 1);
2055 output_processing = PNG_CMAP_NONE;
2056
2057 /* If there is a tRNS chunk then this either selects a transparent
2058 * value or, if the output has no alpha, the background color.
2059 */
2060 if (png_ptr->num_trans > 0)
2061 {
2062 trans = png_ptr->trans_color.gray;
2063
2064 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002065 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002066 }
2067
2068 /* png_create_colormap_entry just takes an RGBA and writes the
2069 * corresponding color-map entry using the format from 'image',
2070 * including the required conversion to sRGB or linear as
2071 * appropriate. The input values are always either sRGB (if the
2072 * gamma correction flag is 0) or 0..255 scaled file encoded values
2073 * (if the function must gamma correct them).
2074 */
2075 for (i=val=0; i<cmap_entries; ++i, val += step)
2076 {
2077 /* 'i' is a file value. While this will result in duplicated
2078 * entries for 8-bit non-sRGB encoded files it is necessary to
2079 * have non-gamma corrected values to do tRNS handling.
2080 */
2081 if (i != trans)
2082 png_create_colormap_entry(display, i, val, val, val, 255,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002083 P_FILE/*8-bit with file gamma*/);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002084
2085 /* Else this entry is transparent. The colors don't matter if
2086 * there is an alpha channel (back_alpha == 0), but it does no
2087 * harm to pass them in; the values are not set above so this
2088 * passes in white.
2089 *
2090 * NOTE: this preserves the full precision of the application
2091 * supplied background color when it is used.
2092 */
2093 else
2094 png_create_colormap_entry(display, i, back_r, back_g, back_b,
2095 back_alpha, output_encoding);
2096 }
2097
2098 /* We need libpng to preserve the original encoding. */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002099 data_encoding = P_FILE;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002100
2101 /* The rows from libpng, while technically gray values, are now also
2102 * color-map indicies; however, they may need to be expanded to 1
2103 * byte per pixel. This is what png_set_packing does (i.e., it
2104 * unpacks the bit values into bytes.)
2105 */
2106 if (png_ptr->bit_depth < 8)
2107 png_set_packing(png_ptr);
2108 }
2109
2110 else /* bit depth is 16 */
2111 {
2112 /* The 16-bit input values can be converted directly to 8-bit gamma
2113 * encoded values; however, if a tRNS chunk is present 257 color-map
2114 * entries are required. This means that the extra entry requires
2115 * special processing; add an alpha channel, sacrifice gray level
2116 * 254 and convert transparent (alpha==0) entries to that.
2117 *
2118 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2119 * same time to minimize quality loss. If a tRNS chunk is present
2120 * this means libpng must handle it too; otherwise it is impossible
2121 * to do the exact match on the 16-bit value.
2122 *
2123 * If the output has no alpha channel *and* the background color is
2124 * gray then it is possible to let libpng handle the substitution by
2125 * ensuring that the corresponding gray level matches the background
2126 * color exactly.
2127 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002128 data_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002129
2130 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2131 png_error(png_ptr, "gray[16] color-map: too few entries");
2132
2133 cmap_entries = make_gray_colormap(display);
2134
2135 if (png_ptr->num_trans > 0)
2136 {
2137 unsigned int back_alpha;
2138
2139 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2140 back_alpha = 0;
2141
2142 else
2143 {
2144 if (back_r == back_g && back_g == back_b)
2145 {
2146 /* Background is gray; no special processing will be
2147 * required.
2148 */
2149 png_color_16 c;
2150 png_uint_32 gray = back_g;
2151
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002152 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002153 {
2154 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2155
2156 /* And make sure the corresponding palette entry
2157 * matches.
2158 */
2159 png_create_colormap_entry(display, gray, back_g, back_g,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002160 back_g, 65535, P_LINEAR);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002161 }
2162
2163 /* The background passed to libpng, however, must be the
2164 * sRGB value.
2165 */
2166 c.index = 0; /*unused*/
2167 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2168
2169 /* NOTE: does this work without expanding tRNS to alpha?
2170 * It should be the color->gray case below apparently
2171 * doesn't.
2172 */
2173 png_set_background_fixed(png_ptr, &c,
2174 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2175 0/*gamma: not used*/);
2176
2177 output_processing = PNG_CMAP_NONE;
2178 break;
2179 }
2180
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002181 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002182 }
2183
2184 /* output_processing means that the libpng-processed row will be
2185 * 8-bit GA and it has to be processing to single byte color-map
2186 * values. Entry 254 is replaced by either a completely
2187 * transparent entry or by the background color at full
2188 * precision (and the background color is not a simple gray leve
2189 * in this case.)
2190 */
2191 expand_tRNS = 1;
2192 output_processing = PNG_CMAP_TRANS;
2193 background_index = 254;
2194
2195 /* And set (overwrite) color-map entry 254 to the actual
2196 * background color at full precision.
2197 */
2198 png_create_colormap_entry(display, 254, back_r, back_g, back_b,
2199 back_alpha, output_encoding);
2200 }
2201
2202 else
2203 output_processing = PNG_CMAP_NONE;
2204 }
2205 break;
2206
2207 case PNG_COLOR_TYPE_GRAY_ALPHA:
2208 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2209 * of 65536 combinations. If, however, the alpha channel is to be
2210 * removed there are only 256 possibilities if the background is gray.
2211 * (Otherwise there is a subset of the 65536 possibilities defined by
2212 * the triangle between black, white and the background color.)
2213 *
2214 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2215 * worry about tRNS matching - tRNS is ignored if there is an alpha
2216 * channel.
2217 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002218 data_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002219
2220 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2221 {
2222 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2223 png_error(png_ptr, "gray+alpha color-map: too few entries");
2224
2225 cmap_entries = make_ga_colormap(display);
2226
2227 background_index = PNG_CMAP_GA_BACKGROUND;
2228 output_processing = PNG_CMAP_GA;
2229 }
2230
2231 else /* alpha is removed */
2232 {
2233 /* Alpha must be removed as the PNG data is processed when the
2234 * background is a color because the G and A channels are
2235 * independent and the vector addition (non-parallel vectors) is a
2236 * 2-D problem.
2237 *
2238 * This can be reduced to the same algorithm as above by making a
2239 * colormap containing gray levels (for the opaque grays), a
2240 * background entry (for a transparent pixel) and a set of four six
2241 * level color values, one set for each intermediate alpha value.
2242 * See the comments in make_ga_colormap for how this works in the
2243 * per-pixel processing.
2244 *
2245 * If the background is gray, however, we only need a 256 entry gray
2246 * level color map. It is sufficient to make the entry generated
2247 * for the background color be exactly the color specified.
2248 */
2249 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2250 (back_r == back_g && back_g == back_b))
2251 {
2252 /* Background is gray; no special processing will be required. */
2253 png_color_16 c;
2254 png_uint_32 gray = back_g;
2255
2256 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2257 png_error(png_ptr, "gray-alpha color-map: too few entries");
2258
2259 cmap_entries = make_gray_colormap(display);
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 matches. */
2266 png_create_colormap_entry(display, gray, back_g, back_g,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002267 back_g, 65535, P_LINEAR);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002268 }
2269
2270 /* The background passed to libpng, however, must be the sRGB
2271 * value.
2272 */
2273 c.index = 0; /*unused*/
2274 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2275
2276 png_set_background_fixed(png_ptr, &c,
2277 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2278 0/*gamma: not used*/);
2279
2280 output_processing = PNG_CMAP_NONE;
2281 }
2282
2283 else
2284 {
2285 png_uint_32 i, a;
2286
2287 /* This is the same as png_make_ga_colormap, above, except that
2288 * the entries are all opaque.
2289 */
2290 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2291 png_error(png_ptr, "ga-alpha color-map: too few entries");
2292
2293 i = 0;
2294 while (i < 231)
2295 {
2296 png_uint_32 gray = (i * 256 + 115) / 231;
2297 png_create_colormap_entry(display, i++, gray, gray, gray,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002298 255, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002299 }
2300
2301 /* NOTE: this preserves the full precision of the application
2302 * background color.
2303 */
2304 background_index = i;
2305 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002306 output_encoding == P_LINEAR ? 65535U : 255U, output_encoding);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002307
2308 /* For non-opaque input composite on the sRGB background - this
2309 * requires inverting the encoding for each component. The input
2310 * is still converted to the sRGB encoding because this is a
2311 * reasonable approximate to the logarithmic curve of human
2312 * visual sensitivity, at least over the narrow range which PNG
2313 * represents. Consequently 'G' is always sRGB encoded, while
2314 * 'A' is linear. We need the linear background colors.
2315 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002316 if (output_encoding == P_sRGB) /* else already linear */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002317 {
2318 /* This may produce a value not exactly matching the
2319 * background, but that's ok because these numbers are only
2320 * used when alpha != 0
2321 */
2322 back_r = png_sRGB_table[back_r];
2323 back_g = png_sRGB_table[back_g];
2324 back_b = png_sRGB_table[back_b];
2325 }
2326
2327 for (a=1; a<5; ++a)
2328 {
2329 unsigned int g;
2330
2331 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2332 * by an 8-bit alpha value (0..255).
2333 */
2334 png_uint_32 alpha = 51 * a;
2335 png_uint_32 back_rx = (255-alpha) * back_r;
2336 png_uint_32 back_gx = (255-alpha) * back_g;
2337 png_uint_32 back_bx = (255-alpha) * back_b;
2338
2339 for (g=0; g<6; ++g)
2340 {
2341 png_uint_32 gray = png_sRGB_table[g*51] * alpha;
2342
2343 png_create_colormap_entry(display, i++,
2344 PNG_sRGB_FROM_LINEAR(gray + back_rx),
2345 PNG_sRGB_FROM_LINEAR(gray + back_gx),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002346 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002347 }
2348 }
2349
2350 cmap_entries = i;
2351 output_processing = PNG_CMAP_GA;
2352 }
2353 }
2354 break;
2355
2356 case PNG_COLOR_TYPE_RGB:
2357 case PNG_COLOR_TYPE_RGB_ALPHA:
2358 /* Exclude the case where the output is gray; we can always handle this
2359 * with the cases above.
2360 */
2361 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
2362 {
2363 /* The color-map will be grayscale, so we may as well convert the
2364 * input RGB values to a simple grayscale and use the grayscale
2365 * code above.
2366 *
2367 * NOTE: calling this apparently damages the recognition of the
2368 * transparent color in background color handling; call
2369 * png_set_tRNS_to_alpha before png_set_background_fixed.
2370 */
2371 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2372 -1);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002373 data_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002374
2375 /* The output will now be one or two 8-bit gray or gray+alpha
2376 * channels. The more complex case arises when the input has alpha.
2377 */
2378 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2379 png_ptr->num_trans > 0) &&
2380 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2381 {
2382 /* Both input and output have an alpha channel, so no background
2383 * processing is required; just map the GA bytes to the right
2384 * color-map entry.
2385 */
2386 expand_tRNS = 1;
2387
2388 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2389 png_error(png_ptr, "rgb[ga] color-map: too few entries");
2390
2391 cmap_entries = make_ga_colormap(display);
2392 background_index = PNG_CMAP_GA_BACKGROUND;
2393 output_processing = PNG_CMAP_GA;
2394 }
2395
2396 else
2397 {
2398 /* Either the input or the output has no alpha channel, so there
2399 * will be no non-opaque pixels in the color-map; it will just be
2400 * grayscale.
2401 */
2402 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2403 png_error(png_ptr, "rgb[gray] color-map: too few entries");
2404
2405 /* Ideally this code would use libpng to do the gamma correction,
2406 * but if an input alpha channel is to be removed we will hit the
2407 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2408 * correction bug). Fix this by dropping the gamma correction in
2409 * this case and doing it in the palette; this will result in
2410 * duplicate palette entries, but that's better than the
2411 * alternative of double gamma correction.
2412 */
2413 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2414 png_ptr->num_trans > 0) &&
2415 png_gamma_not_sRGB(png_ptr->colorspace.gamma))
2416 {
2417 cmap_entries = make_gray_file_colormap(display);
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002418 data_encoding = P_FILE;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002419 }
2420
2421 else
2422 cmap_entries = make_gray_colormap(display);
2423
2424 /* But if the input has alpha or transparency it must be removed
2425 */
2426 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2427 png_ptr->num_trans > 0)
2428 {
2429 png_color_16 c;
2430 png_uint_32 gray = back_g;
2431
2432 /* We need to ensure that the application background exists in
2433 * the colormap and that completely transparent pixels map to
2434 * it. Achieve this simply by ensuring that the entry
2435 * selected for the background really is the background color.
2436 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002437 if (data_encoding == P_FILE) /* from the fixup above */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002438 {
2439 /* The app supplied a gray which is in output_encoding, we
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002440 * need to convert it to a value of the input (P_FILE)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002441 * encoding then set this palette entry to the required
2442 * output encoding.
2443 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002444 if (output_encoding == P_sRGB)
2445 gray = png_sRGB_table[gray]; /* now P_LINEAR */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002446
2447 gray = PNG_DIV257(png_gamma_16bit_correct(gray,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002448 png_ptr->colorspace.gamma)); /* now P_FILE */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002449
2450 /* And make sure the corresponding palette entry contains
2451 * exactly the required sRGB value.
2452 */
2453 png_create_colormap_entry(display, gray, back_g, back_g,
2454 back_g, 0/*unused*/, output_encoding);
2455 }
2456
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002457 else if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002458 {
2459 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2460
2461 /* And make sure the corresponding palette entry matches.
2462 */
2463 png_create_colormap_entry(display, gray, back_g, back_g,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002464 back_g, 0/*unused*/, P_LINEAR);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002465 }
2466
2467 /* The background passed to libpng, however, must be the
2468 * output (normally sRGB) value.
2469 */
2470 c.index = 0; /*unused*/
2471 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2472
2473 /* NOTE: the following is apparently a bug in libpng. Without
2474 * it the transparent color recognition in
2475 * png_set_background_fixed seems to go wrong.
2476 */
2477 expand_tRNS = 1;
2478 png_set_background_fixed(png_ptr, &c,
2479 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2480 0/*gamma: not used*/);
2481 }
2482
2483 output_processing = PNG_CMAP_NONE;
2484 }
2485 }
2486
2487 else /* output is color */
2488 {
2489 /* We could use png_quantize here so long as there is no transparent
2490 * color or alpha; png_quantize ignores alpha. Easier overall just
2491 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2492 * Consequently we always want libpng to produce sRGB data.
2493 */
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002494 data_encoding = P_sRGB;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002495
2496 /* Is there any transparency or alpha? */
2497 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2498 png_ptr->num_trans > 0)
2499 {
2500 /* Is there alpha in the output too? If so all four channels are
2501 * processed into a special RGB cube with alpha support.
2502 */
2503 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2504 {
2505 png_uint_32 r;
2506
2507 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2508 png_error(png_ptr, "rgb+alpha color-map: too few entries");
2509
2510 cmap_entries = make_rgb_colormap(display);
2511
2512 /* Add a transparent entry. */
2513 png_create_colormap_entry(display, cmap_entries, 255, 255,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002514 255, 0, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002515
2516 /* This is stored as the background index for the processing
2517 * algorithm.
2518 */
2519 background_index = cmap_entries++;
2520
2521 /* Add 27 r,g,b entries each with alpha 0.5. */
2522 for (r=0; r<256; r = (r << 1) | 0x7f)
2523 {
2524 png_uint_32 g;
2525
2526 for (g=0; g<256; g = (g << 1) | 0x7f)
2527 {
2528 png_uint_32 b;
2529
2530 /* This generates components with the values 0, 127 and
2531 * 255
2532 */
2533 for (b=0; b<256; b = (b << 1) | 0x7f)
2534 png_create_colormap_entry(display, cmap_entries++,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002535 r, g, b, 128, P_sRGB);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002536 }
2537 }
2538
2539 expand_tRNS = 1;
2540 output_processing = PNG_CMAP_RGB_ALPHA;
2541 }
2542
2543 else
2544 {
2545 /* Alpha/transparency must be removed. The background must
2546 * exist in the color map (achieved by setting adding it after
2547 * the 666 color-map). If the standard processing code will
2548 * pick up this entry automatically that's all that is
2549 * required; libpng can be called to do the background
2550 * processing.
2551 */
2552 unsigned int sample_size =
2553 PNG_IMAGE_SAMPLE_SIZE(output_format);
2554 png_uint_32 r, g, b; /* sRGB background */
2555
2556 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2557 png_error(png_ptr, "rgb-alpha color-map: too few entries");
2558
2559 cmap_entries = make_rgb_colormap(display);
2560
2561 png_create_colormap_entry(display, cmap_entries, back_r,
2562 back_g, back_b, 0/*unused*/, output_encoding);
2563
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002564 if (output_encoding == P_LINEAR)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002565 {
2566 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2567 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2568 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2569 }
2570
2571 else
2572 {
2573 r = back_r;
2574 g = back_g;
2575 b = back_g;
2576 }
2577
2578 /* Compare the newly-created color-map entry with the one the
2579 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2580 * match, add the new one and set this as the background
2581 * index.
2582 */
2583 if (memcmp((png_const_bytep)display->colormap +
2584 sample_size * cmap_entries,
2585 (png_const_bytep)display->colormap +
2586 sample_size * PNG_RGB_INDEX(r,g,b),
2587 sample_size) != 0)
2588 {
2589 /* The background color must be added. */
2590 background_index = cmap_entries++;
2591
2592 /* Add 27 r,g,b entries each with created by composing with
2593 * the background at alpha 0.5.
2594 */
2595 for (r=0; r<256; r = (r << 1) | 0x7f)
2596 {
2597 for (g=0; g<256; g = (g << 1) | 0x7f)
2598 {
2599 /* This generates components with the values 0, 127
2600 * and 255
2601 */
2602 for (b=0; b<256; b = (b << 1) | 0x7f)
2603 png_create_colormap_entry(display, cmap_entries++,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002604 png_colormap_compose(display, r, P_sRGB, 128,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002605 back_r, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002606 png_colormap_compose(display, g, P_sRGB, 128,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002607 back_g, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002608 png_colormap_compose(display, b, P_sRGB, 128,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002609 back_b, output_encoding),
2610 0/*unused*/, output_encoding);
2611 }
2612 }
2613
2614 expand_tRNS = 1;
2615 output_processing = PNG_CMAP_RGB_ALPHA;
2616 }
2617
2618 else /* background color is in the standard color-map */
2619 {
2620 png_color_16 c;
2621
2622 c.index = 0; /*unused*/
2623 c.red = (png_uint_16)back_r;
2624 c.gray = c.green = (png_uint_16)back_g;
2625 c.blue = (png_uint_16)back_b;
2626
2627 png_set_background_fixed(png_ptr, &c,
2628 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2629 0/*gamma: not used*/);
2630
2631 output_processing = PNG_CMAP_RGB;
2632 }
2633 }
2634 }
2635
2636 else /* no alpha or transparency in the input */
2637 {
2638 /* Alpha in the output is irrelevant, simply map the opaque input
2639 * pixels to the 6x6x6 color-map.
2640 */
2641 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2642 png_error(png_ptr, "rgb color-map: too few entries");
2643
2644 cmap_entries = make_rgb_colormap(display);
2645 output_processing = PNG_CMAP_RGB;
2646 }
2647 }
2648 break;
2649
2650 case PNG_COLOR_TYPE_PALETTE:
2651 /* It's already got a color-map. It may be necessary to eliminate the
2652 * tRNS entries though.
2653 */
2654 {
2655 unsigned int num_trans = png_ptr->num_trans;
2656 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2657 png_const_colorp colormap = png_ptr->palette;
2658 const int do_background = trans != NULL &&
2659 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2660 unsigned int i;
2661
2662 /* Just in case: */
2663 if (trans == NULL)
2664 num_trans = 0;
2665
2666 output_processing = PNG_CMAP_NONE;
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002667 data_encoding = P_FILE; /* Don't change from color-map indicies */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002668 cmap_entries = png_ptr->num_palette;
2669 if (cmap_entries > 256)
2670 cmap_entries = 256;
2671
2672 if (cmap_entries > image->colormap_entries)
2673 png_error(png_ptr, "palette color-map: too few entries");
2674
2675 for (i=0; i < cmap_entries; ++i)
2676 {
2677 if (do_background && i < num_trans && trans[i] < 255)
2678 {
2679 if (trans[i] == 0)
2680 png_create_colormap_entry(display, i, back_r, back_g,
2681 back_b, 0, output_encoding);
2682
2683 else
2684 {
2685 /* Must compose the PNG file color in the color-map entry
2686 * on the sRGB color in 'back'.
2687 */
2688 png_create_colormap_entry(display, i,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002689 png_colormap_compose(display, colormap[i].red, P_FILE,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002690 trans[i], back_r, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002691 png_colormap_compose(display, colormap[i].green, P_FILE,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002692 trans[i], back_g, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002693 png_colormap_compose(display, colormap[i].blue, P_FILE,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002694 trans[i], back_b, output_encoding),
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002695 output_encoding == P_LINEAR ? trans[i] * 257U :
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002696 trans[i],
2697 output_encoding);
2698 }
2699 }
2700
2701 else
2702 png_create_colormap_entry(display, i, colormap[i].red,
2703 colormap[i].green, colormap[i].blue,
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002704 i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002705 }
2706
2707 /* The PNG data may have indicies packed in fewer than 8 bits, it
2708 * must be expanded if so.
2709 */
2710 if (png_ptr->bit_depth < 8)
2711 png_set_packing(png_ptr);
2712 }
2713 break;
2714
2715 default:
2716 png_error(png_ptr, "invalid PNG color type");
2717 /*NOT REACHED*/
2718 break;
2719 }
2720
2721 /* Now deal with the output processing */
2722 if (expand_tRNS && png_ptr->num_trans > 0 &&
2723 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
2724 png_set_tRNS_to_alpha(png_ptr);
2725
2726 switch (data_encoding)
2727 {
2728 default:
2729 png_error(png_ptr, "bad data option (internal error)");
2730 break;
2731
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002732 case P_sRGB:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002733 /* Change to 8-bit sRGB */
2734 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
2735 /* FALL THROUGH */
2736
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06002737 case P_FILE:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002738 if (png_ptr->bit_depth > 8)
2739 png_set_scale_16(png_ptr);
2740 break;
2741 }
2742
2743 if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2744 png_error(png_ptr, "color map overflow (BAD internal error)");
2745
2746 image->colormap_entries = cmap_entries;
2747
2748 /* Double check using the recorded background index */
2749 switch (output_processing)
2750 {
2751 case PNG_CMAP_NONE:
2752 if (background_index != PNG_CMAP_NONE_BACKGROUND)
2753 goto bad_background;
2754 break;
2755
2756 case PNG_CMAP_GA:
2757 if (background_index != PNG_CMAP_GA_BACKGROUND)
2758 goto bad_background;
2759 break;
2760
2761 case PNG_CMAP_TRANS:
2762 if (background_index >= cmap_entries ||
2763 background_index != PNG_CMAP_TRANS_BACKGROUND)
2764 goto bad_background;
2765 break;
2766
2767 case PNG_CMAP_RGB:
2768 if (background_index != PNG_CMAP_RGB_BACKGROUND)
2769 goto bad_background;
2770 break;
2771
2772 case PNG_CMAP_RGB_ALPHA:
2773 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
2774 goto bad_background;
2775 break;
2776
2777 default:
2778 png_error(png_ptr, "bad processing option (internal error)");
2779
2780 bad_background:
2781 png_error(png_ptr, "bad background index (internal error)");
2782 }
2783
2784 display->colormap_processing = output_processing;
2785
2786 return 1/*ok*/;
2787}
2788
2789/* The final part of the color-map read called from png_image_finish_read. */
2790static int
2791png_image_read_and_map(png_voidp argument)
John Bowler7875d532011-11-07 22:33:49 -06002792{
John Bowler4fa96a42011-11-16 16:39:16 -06002793 png_image_read_control *display = png_voidcast(png_image_read_control*,
2794 argument);
John Bowler7875d532011-11-07 22:33:49 -06002795 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06002796 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler4f67e402011-12-28 08:43:37 -06002797 int passes;
John Bowler7875d532011-11-07 22:33:49 -06002798
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002799 /* Called when the libpng data must be transformed into the color-mapped
2800 * form. There is a local row buffer in display->local and this routine must
2801 * do the interlace handling.
2802 */
2803 switch (png_ptr->interlaced)
John Bowler7875d532011-11-07 22:33:49 -06002804 {
2805 case PNG_INTERLACE_NONE:
2806 passes = 1;
2807 break;
2808
2809 case PNG_INTERLACE_ADAM7:
2810 passes = PNG_INTERLACE_ADAM7_PASSES;
2811 break;
2812
2813 default:
2814 png_error(png_ptr, "unknown interlace type");
2815 }
2816
2817 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002818 png_uint_32 height = image->height;
2819 png_uint_32 width = image->width;
2820 int proc = display->colormap_processing;
2821 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
2822 ptrdiff_t step_row = display->row_bytes;
John Bowler7875d532011-11-07 22:33:49 -06002823 int pass;
2824
2825 for (pass = 0; pass < passes; ++pass)
2826 {
John Bowler7875d532011-11-07 22:33:49 -06002827 unsigned int startx, stepx, stepy;
2828 png_uint_32 y;
2829
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002830 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
John Bowler7875d532011-11-07 22:33:49 -06002831 {
2832 /* The row may be empty for a short image: */
2833 if (PNG_PASS_COLS(width, pass) == 0)
2834 continue;
2835
2836 startx = PNG_PASS_START_COL(pass);
2837 stepx = PNG_PASS_COL_OFFSET(pass);
2838 y = PNG_PASS_START_ROW(pass);
John Bowler18c5cfa2011-11-16 14:26:34 -06002839 stepy = PNG_PASS_ROW_OFFSET(pass);
John Bowler7875d532011-11-07 22:33:49 -06002840 }
2841
2842 else
2843 {
2844 y = 0;
2845 startx = 0;
2846 stepx = stepy = 1;
2847 }
John Bowler18c5cfa2011-11-16 14:26:34 -06002848
John Bowler7875d532011-11-07 22:33:49 -06002849 for (; y<height; y += stepy)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002850 {
2851 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
2852 png_bytep outrow = first_row + y * step_row;
2853 png_const_bytep end_row = outrow + width;
2854
2855 /* Read read the libpng data into the temporary buffer. */
2856 png_read_row(png_ptr, inrow, NULL);
2857
2858 /* Now process the row according to the processing option, note
2859 * that the caller verifies that the format of the libpng output
2860 * data is as required.
2861 */
2862 outrow += startx;
2863 switch (proc)
John Bowler7875d532011-11-07 22:33:49 -06002864 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002865 case PNG_CMAP_GA:
2866 for (; outrow < end_row; outrow += stepx)
John Bowler5bc90382012-01-23 22:43:22 -06002867 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002868 /* The data is always in the PNG order */
2869 unsigned int gray = *inrow++;
2870 unsigned int alpha = *inrow++;
2871 unsigned int entry;
John Bowler5bc90382012-01-23 22:43:22 -06002872
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002873 /* NOTE: this code is copied as a comment in
2874 * make_ga_colormap above. Please update the
2875 * comment if you change this code!
2876 */
2877 if (alpha > 229) /* opaque */
John Bowler5bc90382012-01-23 22:43:22 -06002878 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002879 entry = (231 * gray + 128) >> 8;
John Bowler5bc90382012-01-23 22:43:22 -06002880 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002881 else if (alpha < 26) /* transparent */
2882 {
2883 entry = 231;
2884 }
2885 else /* partially opaque */
2886 {
2887 entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
2888 }
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06002889
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002890 *outrow = (png_byte)entry;
2891 }
2892 break;
2893
2894 case PNG_CMAP_TRANS:
2895 for (; outrow < end_row; outrow += stepx)
2896 {
2897 png_byte gray = *inrow++;
2898 png_byte alpha = *inrow++;
2899
2900 if (alpha == 0)
2901 *outrow = PNG_CMAP_TRANS_BACKGROUND;
2902
2903 else if (gray != PNG_CMAP_TRANS_BACKGROUND)
2904 *outrow = gray;
2905
2906 else
2907 *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
2908 }
2909 break;
2910
2911 case PNG_CMAP_RGB:
2912 for (; outrow < end_row; outrow += stepx)
2913 {
2914 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
2915 inrow += 3;
2916 }
2917 break;
2918
2919 case PNG_CMAP_RGB_ALPHA:
2920 for (; outrow < end_row; outrow += stepx)
2921 {
2922 unsigned int alpha = inrow[3];
2923
2924 /* Because the alpha entries only hold alpha==0.5 values
2925 * split the processing at alpha==0.25 (64) and 0.75
2926 * (196).
2927 */
2928
2929 if (alpha >= 196)
2930 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
2931 inrow[2]);
2932
2933 else if (alpha < 64)
2934 *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
2935
2936 else
2937 {
2938 /* Likewise there are three entries for each of r, g
2939 * and b. We could select the entry by popcount on
2940 * the top two bits on those architectures that
2941 * support it, this is what the code below does,
2942 * crudely.
2943 */
2944 unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
2945
2946 /* Here are how the values map:
2947 *
2948 * 0x00 .. 0x3f -> 0
2949 * 0x40 .. 0xbf -> 1
2950 * 0xc0 .. 0xff -> 2
2951 *
2952 * So, as above with the explicit alpha checks, the
2953 * breakpoints are at 64 and 196.
2954 */
2955 if (inrow[0] & 0x80) back_i += 9; /* red */
2956 if (inrow[0] & 0x40) back_i += 9;
2957 if (inrow[0] & 0x80) back_i += 3; /* green */
2958 if (inrow[0] & 0x40) back_i += 3;
2959 if (inrow[0] & 0x80) back_i += 1; /* blue */
2960 if (inrow[0] & 0x40) back_i += 1;
2961
2962 *outrow = (png_byte)back_i;
2963 }
2964
2965 inrow += 4;
2966 }
2967 break;
2968
2969 default:
2970 break;
2971 }
2972 }
2973 }
2974 }
2975
2976 return 1;
2977}
2978
2979static int
2980png_image_read_colormapped(png_voidp argument)
2981{
2982 png_image_read_control *display = png_voidcast(png_image_read_control*,
2983 argument);
2984 png_imagep image = display->image;
2985 png_controlp control = image->opaque;
2986 png_structrp png_ptr = control->png_ptr;
2987 png_inforp info_ptr = control->info_ptr;
2988
2989 int passes = 0; /* As a flag */
2990
2991 PNG_SKIP_CHUNKS(png_ptr);
2992
2993 /* Update the 'info' structure and make sure the result is as required; first
2994 * make sure to turn on the interlace handling if it will be required
2995 * (because it can't be turned on *after* the call to png_read_update_info!)
2996 */
2997 if (display->colormap_processing == PNG_CMAP_NONE)
2998 passes = png_set_interlace_handling(png_ptr);
2999
3000 png_read_update_info(png_ptr, info_ptr);
3001
3002 /* The expected output can be deduced from the colormap_processing option. */
3003 switch (display->colormap_processing)
3004 {
3005 case PNG_CMAP_NONE:
3006 /* Output must be one channel and one byte per pixel, the output
3007 * encoding can be anything.
3008 */
3009 if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
3010 info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
3011 info_ptr->bit_depth == 8)
3012 break;
3013
3014 goto bad_output;
3015
3016 case PNG_CMAP_TRANS:
3017 case PNG_CMAP_GA:
3018 /* Output must be two channels and the 'G' one must be sRGB, the latter
3019 * can be checked with an exact number because it should have been set
3020 * to this number above!
3021 */
3022 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
3023 info_ptr->bit_depth == 8 &&
3024 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3025 image->colormap_entries == 256)
3026 break;
3027
3028 goto bad_output;
3029
3030 case PNG_CMAP_RGB:
3031 /* Output must be 8-bit sRGB encoded RGB */
3032 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
3033 info_ptr->bit_depth == 8 &&
3034 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3035 image->colormap_entries == 216)
3036 break;
3037
3038 goto bad_output;
3039
3040 case PNG_CMAP_RGB_ALPHA:
3041 /* Output must be 8-bit sRGB encoded RGBA */
3042 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
3043 info_ptr->bit_depth == 8 &&
3044 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3045 image->colormap_entries == 244 /* 216 + 1 + 27 */)
3046 break;
3047
3048 /* goto bad_output; */
3049 /* FALL THROUGH */
3050
3051 default:
3052 bad_output:
3053 png_error(png_ptr, "bad color-map processing (internal error)");
3054 }
3055
3056 /* Now read the rows. Do this here if it is possible to read directly into
3057 * the output buffer, otherwise allocate a local row buffer of the maximum
3058 * size libpng requires and call the relevant processing routine safely.
3059 */
3060 {
3061 png_voidp first_row = display->buffer;
3062 ptrdiff_t row_bytes = display->row_stride;
3063
3064 /* The following expression is designed to work correctly whether it gives
3065 * a signed or an unsigned result.
3066 */
3067 if (row_bytes < 0)
3068 {
3069 char *ptr = png_voidcast(char*, first_row);
3070 ptr += (image->height-1) * (-row_bytes);
3071 first_row = png_voidcast(png_voidp, ptr);
3072 }
3073
3074 display->first_row = first_row;
3075 display->row_bytes = row_bytes;
3076 }
3077
3078 if (passes == 0)
3079 {
3080 int result;
3081 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3082
3083 display->local_row = row;
3084 result = png_safe_execute(image, png_image_read_and_map, display);
3085 display->local_row = NULL;
3086 png_free(png_ptr, row);
3087
3088 return result;
3089 }
3090
3091 else
3092 {
3093 png_alloc_size_t row_bytes = display->row_bytes;
3094
3095 while (--passes >= 0)
3096 {
3097 png_uint_32 y = image->height;
3098 png_bytep row = png_voidcast(png_bytep, display->first_row);
3099
3100 while (y-- > 0)
3101 {
3102 png_read_row(png_ptr, row, NULL);
3103 row += row_bytes;
3104 }
3105 }
3106
3107 return 1;
3108 }
3109}
3110
3111/* Just the row reading part of png_image_read. */
3112static int
3113png_image_read_composite(png_voidp argument)
3114{
3115 png_image_read_control *display = png_voidcast(png_image_read_control*,
3116 argument);
3117 png_imagep image = display->image;
3118 png_structrp png_ptr = image->opaque->png_ptr;
3119 int passes;
3120
3121 switch (png_ptr->interlaced)
3122 {
3123 case PNG_INTERLACE_NONE:
3124 passes = 1;
3125 break;
3126
3127 case PNG_INTERLACE_ADAM7:
3128 passes = PNG_INTERLACE_ADAM7_PASSES;
3129 break;
3130
3131 default:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003132 png_error(png_ptr, "unknown interlace type");
3133 }
3134
3135 {
3136 png_uint_32 height = image->height;
3137 png_uint_32 width = image->width;
3138 ptrdiff_t step_row = display->row_bytes;
3139 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
3140 int pass;
3141
3142 for (pass = 0; pass < passes; ++pass)
3143 {
3144 unsigned int startx, stepx, stepy;
3145 png_uint_32 y;
3146
3147 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3148 {
3149 /* The row may be empty for a short image: */
3150 if (PNG_PASS_COLS(width, pass) == 0)
3151 continue;
3152
3153 startx = PNG_PASS_START_COL(pass) * channels;
3154 stepx = PNG_PASS_COL_OFFSET(pass) * channels;
3155 y = PNG_PASS_START_ROW(pass);
3156 stepy = PNG_PASS_ROW_OFFSET(pass);
3157 }
3158
3159 else
3160 {
3161 y = 0;
3162 startx = 0;
3163 stepx = channels;
3164 stepy = 1;
3165 }
3166
3167 for (; y<height; y += stepy)
3168 {
3169 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
3170 png_bytep outrow;
3171 png_const_bytep end_row;
3172
3173 /* Read the row, which is packed: */
3174 png_read_row(png_ptr, inrow, NULL);
3175
3176 outrow = png_voidcast(png_bytep, display->first_row);
3177 outrow += y * step_row;
3178 end_row = outrow + width * channels;
3179
3180 /* Now do the composition on each pixel in this row. */
3181 outrow += startx;
3182 for (; outrow < end_row; outrow += stepx)
3183 {
3184 png_byte alpha = inrow[channels];
3185
3186 if (alpha > 0) /* else no change to the output */
3187 {
3188 unsigned int c;
3189
3190 for (c=0; c<channels; ++c)
3191 {
3192 png_uint_32 component = inrow[c];
3193
3194 if (alpha < 255) /* else just use component */
3195 {
3196 /* This is PNG_OPTIMIZED_ALPHA, the component value
3197 * is a linear 8-bit value. Combine this with the
3198 * current outrow[c] value which is sRGB encoded.
3199 * Arithmetic here is 16-bits to preserve the output
3200 * values correctly.
3201 */
3202 component *= 257*255; /* =65535 */
3203 component += (255-alpha)*png_sRGB_table[outrow[c]];
3204
3205 /* So 'component' is scaled by 255*65535 and is
3206 * therefore appropriate for the sRGB to linear
3207 * conversion table.
3208 */
3209 component = PNG_sRGB_FROM_LINEAR(component);
3210 }
3211
3212 outrow[c] = (png_byte)component;
3213 }
John Bowler7875d532011-11-07 22:33:49 -06003214 }
3215
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003216 inrow += channels+1; /* components and alpha channel */
John Bowler7875d532011-11-07 22:33:49 -06003217 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003218 }
John Bowler7875d532011-11-07 22:33:49 -06003219 }
3220 }
3221
3222 return 1;
3223}
3224
John Bowler18c5cfa2011-11-16 14:26:34 -06003225/* The do_local_background case; called when all the following transforms are to
3226 * be done:
3227 *
3228 * PNG_RGB_TO_GRAY
3229 * PNG_COMPOSITE
3230 * PNG_GAMMA
3231 *
3232 * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and
3233 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
3234 * correction. The fix-up is to prevent the PNG_COMPOSITE operation happening
3235 * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles
3236 * the removal or pre-multiplication of the alpha channel.
3237 */
3238static int
3239png_image_read_background(png_voidp argument)
3240{
John Bowler4fa96a42011-11-16 16:39:16 -06003241 png_image_read_control *display = png_voidcast(png_image_read_control*,
3242 argument);
John Bowler18c5cfa2011-11-16 14:26:34 -06003243 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06003244 png_structrp png_ptr = image->opaque->png_ptr;
3245 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler18c5cfa2011-11-16 14:26:34 -06003246 png_uint_32 height = image->height;
3247 png_uint_32 width = image->width;
John Bowler4f67e402011-12-28 08:43:37 -06003248 int pass, passes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003249
3250 /* Double check the convoluted logic below. We expect to get here with
3251 * libpng doing rgb to gray and gamma correction but background processing
3252 * left to the png_image_read_background function. The rows libpng produce
3253 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
3254 */
3255 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
3256 png_error(png_ptr, "lost rgb to gray");
3257
3258 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
3259 png_error(png_ptr, "unexpected compose");
3260
John Bowler18c5cfa2011-11-16 14:26:34 -06003261 if (png_get_channels(png_ptr, info_ptr) != 2)
3262 png_error(png_ptr, "lost/gained channels");
3263
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003264 /* Expect the 8-bit case to always remove the alpha channel */
3265 if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
3266 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
3267 png_error(png_ptr, "unexpected 8-bit transformation");
3268
3269 switch (png_ptr->interlaced)
John Bowler18c5cfa2011-11-16 14:26:34 -06003270 {
3271 case PNG_INTERLACE_NONE:
3272 passes = 1;
3273 break;
3274
3275 case PNG_INTERLACE_ADAM7:
3276 passes = PNG_INTERLACE_ADAM7_PASSES;
3277 break;
3278
3279 default:
3280 png_error(png_ptr, "unknown interlace type");
3281 }
3282
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06003283 /* Use direct access to info_ptr here because otherwise the simplified API
3284 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
3285 * checking the value after libpng expansions, not the original value in the
3286 * PNG.
3287 */
3288 switch (info_ptr->bit_depth)
John Bowler18c5cfa2011-11-16 14:26:34 -06003289 {
3290 default:
3291 png_error(png_ptr, "unexpected bit depth");
3292 break;
3293
3294 case 8:
3295 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
Glenn Randers-Pehrson32440202013-08-21 18:01:32 -05003296 * to be removed by composing on a background: either the row if
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003297 * display->background is NULL or display->background->green if not.
John Bowler18c5cfa2011-11-16 14:26:34 -06003298 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3299 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003300 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003301 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3302 ptrdiff_t step_row = display->row_bytes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003303
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003304 for (pass = 0; pass < passes; ++pass)
John Bowler18c5cfa2011-11-16 14:26:34 -06003305 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003306 png_bytep row = png_voidcast(png_bytep,
3307 display->first_row);
3308 unsigned int startx, stepx, stepy;
3309 png_uint_32 y;
John Bowler18c5cfa2011-11-16 14:26:34 -06003310
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003311 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3312 {
3313 /* The row may be empty for a short image: */
3314 if (PNG_PASS_COLS(width, pass) == 0)
3315 continue;
John Bowler18c5cfa2011-11-16 14:26:34 -06003316
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003317 startx = PNG_PASS_START_COL(pass);
3318 stepx = PNG_PASS_COL_OFFSET(pass);
3319 y = PNG_PASS_START_ROW(pass);
3320 stepy = PNG_PASS_ROW_OFFSET(pass);
3321 }
3322
3323 else
3324 {
3325 y = 0;
3326 startx = 0;
3327 stepx = stepy = 1;
3328 }
3329
3330 if (display->background == NULL)
3331 {
3332 for (; y<height; y += stepy)
John Bowler18c5cfa2011-11-16 14:26:34 -06003333 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003334 png_bytep inrow = png_voidcast(png_bytep,
3335 display->local_row);
3336 png_bytep outrow = first_row + y * step_row;
3337 png_const_bytep end_row = outrow + width;
John Bowler18c5cfa2011-11-16 14:26:34 -06003338
3339 /* Read the row, which is packed: */
3340 png_read_row(png_ptr, inrow, NULL);
3341
3342 /* Now do the composition on each pixel in this row. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003343 outrow += startx;
John Bowler18c5cfa2011-11-16 14:26:34 -06003344 for (; outrow < end_row; outrow += stepx)
3345 {
3346 png_byte alpha = inrow[1];
3347
3348 if (alpha > 0) /* else no change to the output */
3349 {
3350 png_uint_32 component = inrow[0];
3351
3352 if (alpha < 255) /* else just use component */
3353 {
3354 /* Since PNG_OPTIMIZED_ALPHA was not set it is
3355 * necessary to invert the sRGB transfer
3356 * function and multiply the alpha out.
3357 */
3358 component = png_sRGB_table[component] * alpha;
3359 component += png_sRGB_table[outrow[0]] *
3360 (255-alpha);
3361 component = PNG_sRGB_FROM_LINEAR(component);
3362 }
3363
3364 outrow[0] = (png_byte)component;
3365 }
3366
3367 inrow += 2; /* gray and alpha channel */
3368 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003369 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003370 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003371
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003372 else /* constant background value */
3373 {
3374 png_byte background8 = display->background->green;
3375 png_uint_16 background = png_sRGB_table[background8];
John Bowler18c5cfa2011-11-16 14:26:34 -06003376
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003377 for (; y<height; y += stepy)
John Bowler18c5cfa2011-11-16 14:26:34 -06003378 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003379 png_bytep inrow = png_voidcast(png_bytep,
3380 display->local_row);
3381 png_bytep outrow = first_row + y * step_row;
3382 png_const_bytep end_row = outrow + width;
John Bowler18c5cfa2011-11-16 14:26:34 -06003383
3384 /* Read the row, which is packed: */
3385 png_read_row(png_ptr, inrow, NULL);
3386
3387 /* Now do the composition on each pixel in this row. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003388 outrow += startx;
John Bowler18c5cfa2011-11-16 14:26:34 -06003389 for (; outrow < end_row; outrow += stepx)
3390 {
3391 png_byte alpha = inrow[1];
3392
3393 if (alpha > 0) /* else use background */
3394 {
3395 png_uint_32 component = inrow[0];
3396
3397 if (alpha < 255) /* else just use component */
3398 {
3399 component = png_sRGB_table[component] * alpha;
3400 component += background * (255-alpha);
3401 component = PNG_sRGB_FROM_LINEAR(component);
3402 }
3403
3404 outrow[0] = (png_byte)component;
3405 }
3406
3407 else
3408 outrow[0] = background8;
3409
3410 inrow += 2; /* gray and alpha channel */
3411 }
3412
3413 row += display->row_bytes;
3414 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003415 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003416 }
3417 }
3418 break;
3419
3420 case 16:
3421 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3422 * still be done and, maybe, the alpha channel removed. This code also
3423 * handles the alpha-first option.
3424 */
3425 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003426 png_uint_16p first_row = png_voidcast(png_uint_16p,
3427 display->first_row);
3428 /* The division by two is safe because the caller passed in a
3429 * stride which was multiplied by 2 (below) to get row_bytes.
3430 */
3431 ptrdiff_t step_row = display->row_bytes / 2;
John Bowler18c5cfa2011-11-16 14:26:34 -06003432 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003433 unsigned int outchannels = 1+preserve_alpha;
John Bowler18c5cfa2011-11-16 14:26:34 -06003434 int swap_alpha = 0;
3435
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06003436# ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
3437 if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST))
3438 swap_alpha = 1;
3439# endif
John Bowler18c5cfa2011-11-16 14:26:34 -06003440
3441 for (pass = 0; pass < passes; ++pass)
3442 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003443 unsigned int startx, stepx, stepy;
John Bowler18c5cfa2011-11-16 14:26:34 -06003444 png_uint_32 y;
3445
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003446 /* The 'x' start and step are adjusted to output components here.
3447 */
3448 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
John Bowler18c5cfa2011-11-16 14:26:34 -06003449 {
3450 /* The row may be empty for a short image: */
3451 if (PNG_PASS_COLS(width, pass) == 0)
3452 continue;
3453
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003454 startx = PNG_PASS_START_COL(pass) * outchannels;
3455 stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
John Bowler18c5cfa2011-11-16 14:26:34 -06003456 y = PNG_PASS_START_ROW(pass);
3457 stepy = PNG_PASS_ROW_OFFSET(pass);
3458 }
3459
3460 else
3461 {
3462 y = 0;
3463 startx = 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003464 stepx = outchannels;
3465 stepy = 1;
John Bowler18c5cfa2011-11-16 14:26:34 -06003466 }
3467
John Bowler18c5cfa2011-11-16 14:26:34 -06003468 for (; y<height; y += stepy)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003469 {
3470 png_const_uint_16p inrow;
3471 png_uint_16p outrow = first_row + y*step_row;
3472 png_uint_16p end_row = outrow + width * outchannels;
3473
3474 /* Read the row, which is packed: */
3475 png_read_row(png_ptr, png_voidcast(png_bytep,
3476 display->local_row), NULL);
3477 inrow = png_voidcast(png_const_uint_16p, display->local_row);
3478
3479 /* Now do the pre-multiplication on each pixel in this row.
3480 */
3481 outrow += startx;
3482 for (; outrow < end_row; outrow += stepx)
John Bowler18c5cfa2011-11-16 14:26:34 -06003483 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003484 png_uint_32 component = inrow[0];
3485 png_uint_16 alpha = inrow[1];
John Bowler18c5cfa2011-11-16 14:26:34 -06003486
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003487 if (alpha > 0) /* else 0 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003488 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003489 if (alpha < 65535) /* else just use component */
John Bowler18c5cfa2011-11-16 14:26:34 -06003490 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003491 component *= alpha;
3492 component += 32767;
3493 component /= 65535;
John Bowler18c5cfa2011-11-16 14:26:34 -06003494 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003495 }
3496
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003497 else
3498 component = 0;
3499
3500 outrow[swap_alpha] = (png_uint_16)component;
3501 if (preserve_alpha)
3502 outrow[1 ^ swap_alpha] = alpha;
3503
3504 inrow += 2; /* components and alpha channel */
John Bowler18c5cfa2011-11-16 14:26:34 -06003505 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003506 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003507 }
3508 }
3509 break;
3510 }
3511
3512 return 1;
3513}
3514
John Bowler7875d532011-11-07 22:33:49 -06003515/* The guts of png_image_finish_read as a png_safe_execute callback. */
3516static int
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003517png_image_read_direct(png_voidp argument)
John Bowler7875d532011-11-07 22:33:49 -06003518{
John Bowler4fa96a42011-11-16 16:39:16 -06003519 png_image_read_control *display = png_voidcast(png_image_read_control*,
3520 argument);
John Bowler7875d532011-11-07 22:33:49 -06003521 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06003522 png_structrp png_ptr = image->opaque->png_ptr;
3523 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06003524
3525 png_uint_32 format = image->format;
3526 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
3527 int do_local_compose = 0;
John Bowler18c5cfa2011-11-16 14:26:34 -06003528 int do_local_background = 0; /* to avoid double gamma correction bug */
John Bowler7875d532011-11-07 22:33:49 -06003529 int passes = 0;
3530
3531 /* Add transforms to ensure the correct output format is produced then check
3532 * that the required implementation support is there. Always expand; always
3533 * need 8 bits minimum, no palette and expanded tRNS.
3534 */
3535 png_set_expand(png_ptr);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003536
John Bowler7875d532011-11-07 22:33:49 -06003537 /* Now check the format to see if it was modified. */
3538 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003539 png_uint_32 base_format = png_image_format(png_ptr) &
3540 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
John Bowler7875d532011-11-07 22:33:49 -06003541 png_uint_32 change = format ^ base_format;
John Bowler3615d032011-11-08 10:38:09 -06003542 png_fixed_point output_gamma;
3543 int mode; /* alpha mode */
John Bowler7875d532011-11-07 22:33:49 -06003544
John Bowler18c5cfa2011-11-16 14:26:34 -06003545 /* Do this first so that we have a record if rgb to gray is happening. */
3546 if (change & PNG_FORMAT_FLAG_COLOR)
3547 {
3548 /* gray<->color transformation required. */
3549 if (format & PNG_FORMAT_FLAG_COLOR)
3550 png_set_gray_to_rgb(png_ptr);
3551
3552 else
3553 {
3554 /* libpng can't do both rgb to gray and
3555 * background/pre-multiplication if there is also significant gamma
3556 * correction, because both operations require linear colors and
3557 * the code only supports one transform doing the gamma correction.
3558 * Handle this by doing the pre-multiplication or background
3559 * operation in this code, if necessary.
3560 *
3561 * TODO: fix this by rewriting pngrtran.c (!)
3562 *
3563 * For the moment (given that fixing this in pngrtran.c is an
3564 * enormous change) 'do_local_background' is used to indicate that
3565 * the problem exists.
3566 */
3567 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3568 do_local_background = 1/*maybe*/;
3569
3570 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
3571 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
3572 }
3573
3574 change &= ~PNG_FORMAT_FLAG_COLOR;
3575 }
3576
John Bowler7875d532011-11-07 22:33:49 -06003577 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3578 */
3579 {
John Bowler3615d032011-11-08 10:38:09 -06003580 png_fixed_point input_gamma_default;
John Bowler7875d532011-11-07 22:33:49 -06003581
John Bowler59ae3892013-03-06 22:15:25 -06003582 if ((base_format & PNG_FORMAT_FLAG_LINEAR) &&
3583 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
John Bowler7875d532011-11-07 22:33:49 -06003584 input_gamma_default = PNG_GAMMA_LINEAR;
3585 else
3586 input_gamma_default = PNG_DEFAULT_sRGB;
3587
John Bowler18c5cfa2011-11-16 14:26:34 -06003588 /* Call png_set_alpha_mode to set the default for the input gamma; the
3589 * output gamma is set by a second call below.
3590 */
3591 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
3592 }
3593
3594 if (linear)
3595 {
3596 /* If there *is* an alpha channel in the input it must be multiplied
3597 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3598 */
3599 if (base_format & PNG_FORMAT_FLAG_ALPHA)
John Bowler3615d032011-11-08 10:38:09 -06003600 mode = PNG_ALPHA_STANDARD; /* associated alpha */
John Bowler7875d532011-11-07 22:33:49 -06003601
3602 else
John Bowler7875d532011-11-07 22:33:49 -06003603 mode = PNG_ALPHA_PNG;
John Bowler18c5cfa2011-11-16 14:26:34 -06003604
3605 output_gamma = PNG_GAMMA_LINEAR;
3606 }
3607
3608 else
3609 {
3610 mode = PNG_ALPHA_PNG;
3611 output_gamma = PNG_DEFAULT_sRGB;
3612 }
3613
3614 /* If 'do_local_background' is set check for the presence of gamma
3615 * correction; this is part of the work-round for the libpng bug
3616 * described above.
3617 *
3618 * TODO: fix libpng and remove this.
3619 */
3620 if (do_local_background)
3621 {
3622 png_fixed_point gtest;
3623
3624 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3625 * gamma correction, the screen gamma hasn't been set on png_struct
3626 * yet; it's set below. png_struct::gamma, however, is set to the
3627 * final value.
3628 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003629 if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
3630 PNG_FP_1) && !png_gamma_significant(gtest))
John Bowler18c5cfa2011-11-16 14:26:34 -06003631 do_local_background = 0;
3632
3633 else if (mode == PNG_ALPHA_STANDARD)
3634 {
3635 do_local_background = 2/*required*/;
3636 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
John Bowler7875d532011-11-07 22:33:49 -06003637 }
3638
John Bowler18c5cfa2011-11-16 14:26:34 -06003639 /* else leave as 1 for the checks below */
John Bowler3615d032011-11-08 10:38:09 -06003640 }
John Bowler7875d532011-11-07 22:33:49 -06003641
John Bowler3615d032011-11-08 10:38:09 -06003642 /* If the bit-depth changes then handle that here. */
3643 if (change & PNG_FORMAT_FLAG_LINEAR)
3644 {
3645 if (linear /*16-bit output*/)
3646 png_set_expand_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06003647
John Bowler3615d032011-11-08 10:38:09 -06003648 else /* 8-bit output */
3649 png_set_scale_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06003650
John Bowler3615d032011-11-08 10:38:09 -06003651 change &= ~PNG_FORMAT_FLAG_LINEAR;
John Bowler7875d532011-11-07 22:33:49 -06003652 }
3653
3654 /* Now the background/alpha channel changes. */
3655 if (change & PNG_FORMAT_FLAG_ALPHA)
3656 {
3657 /* Removing an alpha channel requires composition for the 8-bit
3658 * formats; for the 16-bit it is already done, above, by the
3659 * pre-multiplication and the channel just needs to be stripped.
3660 */
3661 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3662 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003663 /* If RGB->gray is happening the alpha channel must be left and the
3664 * operation completed locally.
3665 *
3666 * TODO: fix libpng and remove this.
3667 */
3668 if (do_local_background)
3669 do_local_background = 2/*required*/;
3670
John Bowler3615d032011-11-08 10:38:09 -06003671 /* 16-bit output: just remove the channel */
John Bowler18c5cfa2011-11-16 14:26:34 -06003672 else if (linear) /* compose on black (well, pre-multiply) */
John Bowler7875d532011-11-07 22:33:49 -06003673 png_set_strip_alpha(png_ptr);
3674
John Bowler3615d032011-11-08 10:38:09 -06003675 /* 8-bit output: do an appropriate compose */
John Bowler7875d532011-11-07 22:33:49 -06003676 else if (display->background != NULL)
3677 {
3678 png_color_16 c;
3679
3680 c.index = 0; /*unused*/
3681 c.red = display->background->red;
3682 c.green = display->background->green;
3683 c.blue = display->background->blue;
3684 c.gray = display->background->green;
3685
3686 /* This is always an 8-bit sRGB value, using the 'green' channel
3687 * for gray is much better than calculating the luminance here;
3688 * we can get off-by-one errors in that calculation relative to
3689 * the app expectations and that will show up in transparent
3690 * pixels.
3691 */
3692 png_set_background_fixed(png_ptr, &c,
3693 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
3694 0/*gamma: not used*/);
3695 }
3696
3697 else /* compose on row: implemented below. */
3698 {
3699 do_local_compose = 1;
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003700 /* This leaves the alpha channel in the output, so it has to be
John Bowler7875d532011-11-07 22:33:49 -06003701 * removed by the code below. Set the encoding to the 'OPTIMIZE'
3702 * one so the code only has to hack on the pixels that require
3703 * composition.
3704 */
John Bowler3615d032011-11-08 10:38:09 -06003705 mode = PNG_ALPHA_OPTIMIZED;
John Bowler7875d532011-11-07 22:33:49 -06003706 }
3707 }
3708
3709 else /* output needs an alpha channel */
3710 {
3711 /* This is tricky because it happens before the swap operation has
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003712 * been accomplished; however, the swap does *not* swap the added
John Bowlere6fb6912011-11-08 21:35:16 -06003713 * alpha channel (weird API), so it must be added in the correct
3714 * place.
John Bowler7875d532011-11-07 22:33:49 -06003715 */
John Bowler3615d032011-11-08 10:38:09 -06003716 png_uint_32 filler; /* opaque filler */
John Bowlere6fb6912011-11-08 21:35:16 -06003717 int where;
John Bowler3615d032011-11-08 10:38:09 -06003718
3719 if (linear)
3720 filler = 65535;
3721
3722 else
3723 filler = 255;
3724
John Bowlere6fb6912011-11-08 21:35:16 -06003725# ifdef PNG_FORMAT_AFIRST_SUPPORTED
3726 if (format & PNG_FORMAT_FLAG_AFIRST)
3727 {
3728 where = PNG_FILLER_BEFORE;
3729 change &= ~PNG_FORMAT_FLAG_AFIRST;
3730 }
3731
3732 else
3733# endif
3734 where = PNG_FILLER_AFTER;
3735
3736 png_set_add_alpha(png_ptr, filler, where);
John Bowler7875d532011-11-07 22:33:49 -06003737 }
3738
John Bowlere6fb6912011-11-08 21:35:16 -06003739 /* This stops the (irrelevant) call to swap_alpha below. */
John Bowler7875d532011-11-07 22:33:49 -06003740 change &= ~PNG_FORMAT_FLAG_ALPHA;
3741 }
3742
John Bowler3615d032011-11-08 10:38:09 -06003743 /* Now set the alpha mode correctly; this is always done, even if there is
3744 * no alpha channel in either the input or the output because it correctly
3745 * sets the output gamma.
3746 */
3747 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
3748
John Bowler7875d532011-11-07 22:33:49 -06003749# ifdef PNG_FORMAT_BGR_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06003750 if (change & PNG_FORMAT_FLAG_BGR)
John Bowler7875d532011-11-07 22:33:49 -06003751 {
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003752 /* Check only the output format; PNG is never BGR; don't do this if
John Bowler7875d532011-11-07 22:33:49 -06003753 * the output is gray, but fix up the 'format' value in that case.
3754 */
3755 if (format & PNG_FORMAT_FLAG_COLOR)
3756 png_set_bgr(png_ptr);
3757
3758 else
3759 format &= ~PNG_FORMAT_FLAG_BGR;
3760
3761 change &= ~PNG_FORMAT_FLAG_BGR;
3762 }
3763# endif
3764
3765# ifdef PNG_FORMAT_AFIRST_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06003766 if (change & PNG_FORMAT_FLAG_AFIRST)
John Bowler7875d532011-11-07 22:33:49 -06003767 {
3768 /* Only relevant if there is an alpha channel - it's particularly
3769 * important to handle this correctly because do_local_compose may
3770 * be set above and then libpng will keep the alpha channel for this
3771 * code to remove.
3772 */
3773 if (format & PNG_FORMAT_FLAG_ALPHA)
John Bowler18c5cfa2011-11-16 14:26:34 -06003774 {
3775 /* Disable this if doing a local background,
3776 * TODO: remove this when local background is no longer required.
3777 */
3778 if (do_local_background != 2)
3779 png_set_swap_alpha(png_ptr);
3780 }
John Bowler7875d532011-11-07 22:33:49 -06003781
3782 else
3783 format &= ~PNG_FORMAT_FLAG_AFIRST;
3784
3785 change &= ~PNG_FORMAT_FLAG_AFIRST;
3786 }
3787# endif
3788
3789 /* If the *output* is 16-bit then we need to check for a byte-swap on this
3790 * architecture.
3791 */
3792 if (linear)
3793 {
3794 PNG_CONST png_uint_16 le = 0x0001;
3795
3796 if (*(png_const_bytep)&le)
3797 png_set_swap(png_ptr);
3798 }
3799
3800 /* If change is not now 0 some transformation is missing - error out. */
3801 if (change)
3802 png_error(png_ptr, "png_read_image: unsupported transformation");
3803 }
3804
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003805 PNG_SKIP_CHUNKS(png_ptr);
John Bowler89c2f842011-11-16 12:04:39 -06003806
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003807 /* Update the 'info' structure and make sure the result is as required; first
John Bowler7875d532011-11-07 22:33:49 -06003808 * make sure to turn on the interlace handling if it will be required
3809 * (because it can't be turned on *after* the call to png_read_update_info!)
John Bowler18c5cfa2011-11-16 14:26:34 -06003810 *
3811 * TODO: remove the do_local_background fixup below.
John Bowler7875d532011-11-07 22:33:49 -06003812 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003813 if (!do_local_compose && do_local_background != 2)
John Bowler7875d532011-11-07 22:33:49 -06003814 passes = png_set_interlace_handling(png_ptr);
3815
3816 png_read_update_info(png_ptr, info_ptr);
3817
3818 {
3819 png_uint_32 info_format = 0;
3820
3821 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
3822 info_format |= PNG_FORMAT_FLAG_COLOR;
3823
3824 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
3825 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003826 /* do_local_compose removes this channel below. */
John Bowler7875d532011-11-07 22:33:49 -06003827 if (!do_local_compose)
John Bowler18c5cfa2011-11-16 14:26:34 -06003828 {
3829 /* do_local_background does the same if required. */
3830 if (do_local_background != 2 ||
3831 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
3832 info_format |= PNG_FORMAT_FLAG_ALPHA;
3833 }
John Bowler7875d532011-11-07 22:33:49 -06003834 }
3835
3836 else if (do_local_compose) /* internal error */
3837 png_error(png_ptr, "png_image_read: alpha channel lost");
3838
3839 if (info_ptr->bit_depth == 16)
3840 info_format |= PNG_FORMAT_FLAG_LINEAR;
3841
3842# ifdef PNG_FORMAT_BGR_SUPPORTED
3843 if (png_ptr->transformations & PNG_BGR)
3844 info_format |= PNG_FORMAT_FLAG_BGR;
3845# endif
3846
3847# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003848 if (do_local_background == 2)
3849 {
3850 if (format & PNG_FORMAT_FLAG_AFIRST)
3851 info_format |= PNG_FORMAT_FLAG_AFIRST;
3852 }
3853
3854 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
John Bowlere6fb6912011-11-08 21:35:16 -06003855 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
3856 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003857 {
3858 if (do_local_background == 2)
3859 png_error(png_ptr, "unexpected alpha swap transformation");
3860
John Bowler7875d532011-11-07 22:33:49 -06003861 info_format |= PNG_FORMAT_FLAG_AFIRST;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003862 }
John Bowler7875d532011-11-07 22:33:49 -06003863# endif
3864
3865 /* This is actually an internal error. */
3866 if (info_format != format)
3867 png_error(png_ptr, "png_read_image: invalid transformations");
3868 }
3869
3870 /* Now read the rows. If do_local_compose is set then it is necessary to use
3871 * a local row buffer. The output will be GA, RGBA or BGRA and must be
3872 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
3873 * display acts as a flag.
3874 */
3875 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003876 png_voidp first_row = display->buffer;
John Bowler7875d532011-11-07 22:33:49 -06003877 ptrdiff_t row_bytes = display->row_stride;
3878
3879 if (linear)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003880 row_bytes *= 2;
John Bowler7875d532011-11-07 22:33:49 -06003881
3882 /* The following expression is designed to work correctly whether it gives
3883 * a signed or an unsigned result.
3884 */
3885 if (row_bytes < 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003886 {
3887 char *ptr = png_voidcast(char*, first_row);
3888 ptr += (image->height-1) * (-row_bytes);
3889 first_row = png_voidcast(png_voidp, ptr);
3890 }
John Bowler7875d532011-11-07 22:33:49 -06003891
3892 display->first_row = first_row;
3893 display->row_bytes = row_bytes;
3894 }
3895
3896 if (do_local_compose)
3897 {
3898 int result;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003899 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
John Bowler7875d532011-11-07 22:33:49 -06003900
3901 display->local_row = row;
3902 result = png_safe_execute(image, png_image_read_composite, display);
3903 display->local_row = NULL;
3904 png_free(png_ptr, row);
3905
3906 return result;
3907 }
3908
John Bowler18c5cfa2011-11-16 14:26:34 -06003909 else if (do_local_background == 2)
3910 {
3911 int result;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003912 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
John Bowler18c5cfa2011-11-16 14:26:34 -06003913
3914 display->local_row = row;
3915 result = png_safe_execute(image, png_image_read_background, display);
3916 display->local_row = NULL;
3917 png_free(png_ptr, row);
3918
3919 return result;
3920 }
3921
John Bowler7875d532011-11-07 22:33:49 -06003922 else
3923 {
3924 png_alloc_size_t row_bytes = display->row_bytes;
3925
3926 while (--passes >= 0)
3927 {
3928 png_uint_32 y = image->height;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003929 png_bytep row = png_voidcast(png_bytep, display->first_row);
3930
John Bowler7875d532011-11-07 22:33:49 -06003931 while (y-- > 0)
3932 {
3933 png_read_row(png_ptr, row, NULL);
3934 row += row_bytes;
3935 }
3936 }
3937
3938 return 1;
3939 }
3940}
3941
3942int PNGAPI
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003943png_image_finish_read(png_imagep image, png_const_colorp background,
3944 void *buffer, png_int_32 row_stride, void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06003945{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003946 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06003947 {
3948 png_uint_32 check;
3949
John Bowler3706d732011-11-21 10:28:06 -06003950 if (row_stride == 0)
3951 row_stride = PNG_IMAGE_ROW_STRIDE(*image);
3952
John Bowler7875d532011-11-07 22:33:49 -06003953 if (row_stride < 0)
3954 check = -row_stride;
3955
3956 else
3957 check = row_stride;
3958
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003959 if (image->opaque != NULL && buffer != NULL &&
3960 check >= PNG_IMAGE_ROW_STRIDE(*image))
John Bowler7875d532011-11-07 22:33:49 -06003961 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003962 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
3963 (image->colormap_entries > 0 && colormap != NULL))
3964 {
3965 int result;
3966 png_image_read_control display;
John Bowler7875d532011-11-07 22:33:49 -06003967
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003968 memset(&display, 0, (sizeof display));
3969 display.image = image;
3970 display.buffer = buffer;
3971 display.row_stride = row_stride;
3972 display.colormap = colormap;
3973 display.background = background;
3974 display.local_row = NULL;
3975
3976 /* Choose the correct 'end' routine; for the color-map case all the
3977 * setup has already been done.
3978 */
3979 if (image->format & PNG_FORMAT_FLAG_COLORMAP)
3980 result =
3981 png_safe_execute(image, png_image_read_colormap, &display) &&
3982 png_safe_execute(image, png_image_read_colormapped, &display);
3983
3984 else
3985 result =
3986 png_safe_execute(image, png_image_read_direct, &display);
3987
3988 png_image_free(image);
3989 return result;
3990 }
3991
3992 else
3993 return png_image_error(image,
3994 "png_image_finish_read[color-map]: no color-map");
John Bowler7875d532011-11-07 22:33:49 -06003995 }
3996
3997 else
3998 return png_image_error(image,
3999 "png_image_finish_read: invalid argument");
4000 }
4001
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06004002 else if (image != NULL)
4003 return png_image_error(image,
4004 "png_image_finish_read: damaged PNG_IMAGE_VERSION");
4005
John Bowler7875d532011-11-07 22:33:49 -06004006 return 0;
4007}
4008
4009#endif /* PNG_SIMPLIFIED_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06004010#endif /* PNG_READ_SUPPORTED */