blob: 68304b8b4aca35c67db330bb4bd6dee96885b4c8 [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngread.c - read a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson13831bc2011-12-21 08:28:28 -06004 * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
Glenn Randers-Pehrson1531bd62012-01-01 14:45:04 -06005 * Copyright (c) 1998-2012 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
15 */
Guy Schalnat0d580581995-07-20 02:43:20 -050016
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050017#include "pngpriv.h"
John Bowler7875d532011-11-07 22:33:49 -060018#if defined PNG_SIMPLIFIED_READ_SUPPORTED && defined PNG_STDIO_SUPPORTED
19# include <errno.h>
20#endif
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060021
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060022#ifdef PNG_READ_SUPPORTED
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050023
Andreas Dilger47a0c421997-05-16 02:46:07 -050024/* Create a PNG structure for reading, and allocate any memory needed. */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050025PNG_FUNCTION(png_structp,PNGAPI
26png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
27 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
Guy Schalnat0d580581995-07-20 02:43:20 -050028{
John Bowler1d7f56a2011-12-21 20:14:23 -060029#ifndef PNG_USER_MEM_SUPPORTED
30 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
31 error_fn, warn_fn, NULL, NULL, NULL);
32#else
33 return png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
34 warn_fn, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050035}
36
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060037/* Alternate create PNG structure for reading, and allocate any memory
38 * needed.
39 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050040PNG_FUNCTION(png_structp,PNGAPI
41png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -060042 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050043 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050044{
John Bowler1d7f56a2011-12-21 20:14:23 -060045 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
46 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050047#endif /* PNG_USER_MEM_SUPPORTED */
48
John Bowler1d7f56a2011-12-21 20:14:23 -060049 if (png_ptr != NULL)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050050 {
John Bowlerb5d00512012-03-09 09:15:18 -060051 png_ptr->mode = PNG_IS_READ_STRUCT;
52
53 /* Adding 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.)
55 */
56# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
57 png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE;
58# endif
John Bowler42a2b552012-03-05 20:57:40 -060059 /* TODO: delay this, it can be done in png_init_io (if the app doesn't
60 * do it itself) avoiding setting the default function if it is not
61 * required.
John Bowler1d7f56a2011-12-21 20:14:23 -060062 */
John Bowler42a2b552012-03-05 20:57:40 -060063 png_set_read_fn(png_ptr, NULL, NULL);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -050064 }
65
John Bowler42a2b552012-03-05 20:57:40 -060066 return png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -050067}
68
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050069
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050070#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060071/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -060072 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060073 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -050074 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060075 * via png_set_sig_bytes(), and we will only check the remaining bytes
76 * here. The application can then have access to the signature bytes we
77 * read if it is determined that this isn't a valid PNG file.
78 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050079void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -060080png_read_info(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050081{
Glenn Randers-Pehrsonc81bb8a2009-08-15 22:02:26 -050082 png_debug(1, "in png_read_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -050083
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050084 if (png_ptr == NULL || info_ptr == NULL)
85 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -050086
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -060087 /* Read and check the PNG file signature. */
88 png_read_sig(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050089
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050090 for (;;)
Guy Schalnat0d580581995-07-20 02:43:20 -050091 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050092 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050093 png_uint_32 chunk_name = png_ptr->chunk_name;
Andreas Dilger47a0c421997-05-16 02:46:07 -050094
95 /* This should be a binary subdivision search or a hash for
96 * matching the chunk name rather than a linear search.
97 */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050098 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -060099 if (png_ptr->mode & PNG_AFTER_IDAT)
100 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500101
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500102 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600103 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500104
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500105 else if (chunk_name == png_IEND)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600106 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500107
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600108#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500109 else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
110 PNG_HANDLE_CHUNK_AS_DEFAULT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600111 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500112 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600113 png_ptr->mode |= PNG_HAVE_IDAT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500114
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600115 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500116
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500117 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600118 png_ptr->mode |= PNG_HAVE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500119
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500120 else if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600121 {
122 if (!(png_ptr->mode & PNG_HAVE_IHDR))
123 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500124
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600125 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600126 !(png_ptr->mode & PNG_HAVE_PLTE))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600127 png_error(png_ptr, "Missing PLTE before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500128
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600129 break;
130 }
131 }
132#endif
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500133 else if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600134 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500135
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500136 else if (chunk_name == png_IDAT)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500137 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500138 if (!(png_ptr->mode & PNG_HAVE_IHDR))
139 png_error(png_ptr, "Missing IHDR before IDAT");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500140
Guy Schalnate5a37791996-06-05 15:50:50 -0500141 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600142 !(png_ptr->mode & PNG_HAVE_PLTE))
Guy Schalnate5a37791996-06-05 15:50:50 -0500143 png_error(png_ptr, "Missing PLTE before IDAT");
144
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500145 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500146 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500147 break;
148 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500149
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500150#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500151 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500152 png_handle_bKGD(png_ptr, info_ptr, length);
153#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500154
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500155#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500156 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500157 png_handle_cHRM(png_ptr, info_ptr, length);
158#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500159
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500160#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500161 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500162 png_handle_gAMA(png_ptr, info_ptr, length);
163#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500164
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500165#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500166 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 png_handle_hIST(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_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500171 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500172 png_handle_oFFs(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_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500176 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500177 png_handle_pCAL(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_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500181 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600182 png_handle_sCAL(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_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500186 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187 png_handle_pHYs(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_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500191 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500192 png_handle_sBIT(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_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500196 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600197 png_handle_sRGB(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_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500201 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600202 png_handle_iCCP(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_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500206 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600207 png_handle_sPLT(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_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500211 else if (chunk_name == png_tEXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500212 png_handle_tEXt(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_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500216 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500217 png_handle_tIME(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_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500221 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500222 png_handle_tRNS(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_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500226 else if (chunk_name == png_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500227 png_handle_zTXt(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_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500231 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600232 png_handle_iTXt(png_ptr, info_ptr, length);
233#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500234
Guy Schalnat0d580581995-07-20 02:43:20 -0500235 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600236 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500237 }
238}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500239#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500240
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500241/* Optional call to update the users info_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500242void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600243png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500244{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500245 png_debug(1, "in png_read_update_info");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500246
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500247 if (png_ptr == NULL)
248 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500249
Glenn Randers-Pehrson3dbfd302011-10-13 17:24:36 -0500250 png_read_start_row(png_ptr);
Glenn Randers-Pehrson4cfdb3c2009-11-26 11:49:37 -0600251
John Bowler4a12f4a2011-04-17 18:34:22 -0500252#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600253 png_read_transform_info(png_ptr, info_ptr);
John Bowler4a12f4a2011-04-17 18:34:22 -0500254#else
255 PNG_UNUSED(info_ptr)
256#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500257}
258
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500259#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600260/* Initialize palette, background, etc, after transformations
261 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500262 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600263 * If the user doesn't call this, we will do it ourselves.
264 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500265void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600266png_start_read_image(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500267{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500268 png_debug(1, "in png_start_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500269
Glenn Randers-Pehrson3dbfd302011-10-13 17:24:36 -0500270 if (png_ptr != NULL)
271 png_read_start_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500272}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500273#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500274
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500275#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500276void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600277png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500278{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500279 png_row_info row_info;
280
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500281 if (png_ptr == NULL)
282 return;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500283
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500284 png_debug2(1, "in png_read_row (row %lu, pass %d)",
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -0500285 (unsigned long)png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson6d75d0c2009-08-22 08:45:09 -0500286
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500287 /* png_read_start_row sets the information (in particular iwidth) for this
288 * interlace pass.
289 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500290 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500291 png_read_start_row(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500292
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500293 /* 1.5.6: row_info moved out of png_struct to a local here. */
294 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
295 row_info.color_type = png_ptr->color_type;
296 row_info.bit_depth = png_ptr->bit_depth;
297 row_info.channels = png_ptr->channels;
298 row_info.pixel_depth = png_ptr->pixel_depth;
299 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
300
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500301 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
302 {
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500303 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500304#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
305 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500306 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500307#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500308
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500309#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
310 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500311 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500312#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500313
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600314#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
315 !defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500316 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500317 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500318#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500319
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500320#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
321 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500322 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500323#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500324
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500325#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
326 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500327 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500328#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500329
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500330#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
331 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500332 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500333#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500334
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500335#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
336 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500337 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500338#endif
339 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500340
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500341#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500342 /* If interlaced and we do not need a new row, combine row and return.
343 * Notice that the pixels we have from previous rows have been transformed
344 * already; we can only combine like with like (transformed or
345 * untransformed) and, because of the libpng API for interlaced images, this
346 * means we must transform before de-interlacing.
347 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500348 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
349 {
350 switch (png_ptr->pass)
351 {
352 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600353 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500354 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500355 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500356 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600357 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500358 return;
359 }
360 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500361
Guy Schalnat0d580581995-07-20 02:43:20 -0500362 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600363 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500364 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500365 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500366 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500367
Guy Schalnat0d580581995-07-20 02:43:20 -0500368 png_read_finish_row(png_ptr);
369 return;
370 }
371 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500372
Guy Schalnat0d580581995-07-20 02:43:20 -0500373 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600374 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500375 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500376 if (dsp_row != NULL && (png_ptr->row_number & 4))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500377 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500378
Guy Schalnat0d580581995-07-20 02:43:20 -0500379 png_read_finish_row(png_ptr);
380 return;
381 }
382 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500383
Guy Schalnat0d580581995-07-20 02:43:20 -0500384 case 3:
385 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
386 {
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*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500389
Guy Schalnat0d580581995-07-20 02:43:20 -0500390 png_read_finish_row(png_ptr);
391 return;
392 }
393 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500394
Guy Schalnat0d580581995-07-20 02:43:20 -0500395 case 4:
396 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600397 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500398 if (dsp_row != NULL && (png_ptr->row_number & 2))
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500399 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500400
Guy Schalnat0d580581995-07-20 02:43:20 -0500401 png_read_finish_row(png_ptr);
402 return;
403 }
404 break;
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -0600405
Guy Schalnat0d580581995-07-20 02:43:20 -0500406 case 5:
407 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
408 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500409 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500410 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500411
Guy Schalnat0d580581995-07-20 02:43:20 -0500412 png_read_finish_row(png_ptr);
413 return;
414 }
415 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500416
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600417 default:
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600418 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500419 if (!(png_ptr->row_number & 1))
420 {
421 png_read_finish_row(png_ptr);
422 return;
423 }
424 break;
425 }
426 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500427#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500428
Guy Schalnate5a37791996-06-05 15:50:50 -0500429 if (!(png_ptr->mode & PNG_HAVE_IDAT))
430 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500431
John Bowlerb5d00512012-03-09 09:15:18 -0600432 /* Fill the row with IDAT data: */
433 png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500434
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500435 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
436 {
437 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
Mans Rullgardd3a94802011-11-03 00:47:55 -0500438 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500439 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
440 else
441 png_error(png_ptr, "bad adaptive filter value");
442 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500443
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500444 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
445 * 1.5.6, while the buffer really is this big in current versions of libpng
446 * it may not be in the future, so this was changed just to copy the
447 * interlaced count:
448 */
449 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
Glenn Randers-Pehrsone6474622006-03-04 16:50:47 -0600450
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500451#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500452 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600453 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600454 {
455 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500456 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600457 }
458#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500459
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500460
John Bowler4a12f4a2011-04-17 18:34:22 -0500461#ifdef PNG_READ_TRANSFORMS_SUPPORTED
John Bowler9b872f42011-02-12 09:00:16 -0600462 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500463 png_do_read_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500464#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500465
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500466 /* The transformed pixel depth should match the depth now in row_info. */
467 if (png_ptr->transformed_pixel_depth == 0)
468 {
469 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
470 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
471 png_error(png_ptr, "sequential row overflow");
472 }
473
474 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
475 png_error(png_ptr, "internal sequential row size calculation error");
476
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500477#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500478 /* Blow up interlaced rows to full size */
Guy Schalnat0d580581995-07-20 02:43:20 -0500479 if (png_ptr->interlaced &&
480 (png_ptr->transformations & PNG_INTERLACE))
481 {
482 if (png_ptr->pass < 6)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500483 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
484 png_ptr->transformations);
Guy Schalnat0d580581995-07-20 02:43:20 -0500485
Andreas Dilger47a0c421997-05-16 02:46:07 -0500486 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500487 png_combine_row(png_ptr, dsp_row, 1/*display*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500488
Andreas Dilger47a0c421997-05-16 02:46:07 -0500489 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500490 png_combine_row(png_ptr, row, 0/*row*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500491 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500492
Guy Schalnat0d580581995-07-20 02:43:20 -0500493 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500494#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500495 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500496 if (row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500497 png_combine_row(png_ptr, row, -1/*ignored*/);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500498
Andreas Dilger47a0c421997-05-16 02:46:07 -0500499 if (dsp_row != NULL)
Glenn Randers-Pehrson65c03392011-10-06 21:54:17 -0500500 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
Guy Schalnat0d580581995-07-20 02:43:20 -0500501 }
502 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600503
504 if (png_ptr->read_row_fn != NULL)
505 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson67dba432012-02-16 20:15:13 -0600506
Guy Schalnat0d580581995-07-20 02:43:20 -0500507}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500508#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500509
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500510#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500511/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600512 * and png_set_interlace_handling() has been called, the rows need to
513 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500514 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600515 * called, the rows contents must be initialized to the contents of the
516 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600517 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600518 * "row" holds the actual image, and pixels are placed in it
519 * as they arrive. If the image is displayed after each pass, it will
520 * appear to "sparkle" in. "display_row" can be used to display a
521 * "chunky" progressive image, with finer detail added as it becomes
522 * available. If you do not want this "chunky" display, you may pass
523 * NULL for display_row. If you do not want the sparkle display, and
524 * you have not called png_handle_alpha(), you may pass NULL for rows.
525 * If you have called png_handle_alpha(), and the image has either an
526 * alpha channel or a transparency chunk, you must provide a buffer for
527 * rows. In this case, you do not have to provide a display_row buffer
528 * also, but you may. If the image is not interlaced, or if you have
529 * not called png_set_interlace_handling(), the display_row buffer will
530 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500531 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600532 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600533 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600534
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500535void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600536png_read_rows(png_structrp png_ptr, png_bytepp row,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600537 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500538{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600539 png_uint_32 i;
540 png_bytepp rp;
541 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500542
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500543 png_debug(1, "in png_read_rows");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500544
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500545 if (png_ptr == NULL)
546 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500547
Guy Schalnat0f716451995-11-28 11:22:13 -0600548 rp = row;
549 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500550 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500551 for (i = 0; i < num_rows; i++)
552 {
553 png_bytep rptr = *rp++;
554 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600555
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500556 png_read_row(png_ptr, rptr, dptr);
557 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500558
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500559 else if (rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500560 for (i = 0; i < num_rows; i++)
561 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500562 png_bytep rptr = *rp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500563 png_read_row(png_ptr, rptr, NULL);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500564 rp++;
565 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500566
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500567 else if (dp != NULL)
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500568 for (i = 0; i < num_rows; i++)
569 {
570 png_bytep dptr = *dp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500571 png_read_row(png_ptr, NULL, dptr);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500572 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500573 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500574}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500575#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500576
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500577#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500578/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500579 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600580 * initialize the image to the current image that PNG will be overlaying.
581 * We set the num_rows again here, in case it was incorrectly set in
582 * png_read_start_row() by a call to png_read_update_info() or
583 * png_start_read_image() if png_set_interlace_handling() wasn't called
584 * prior to either of these functions like it should have been. You can
585 * only call this function once. If you desire to have an image for
586 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500587 *
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600588 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600589 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500590void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600591png_read_image(png_structrp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500592{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500593 png_uint_32 i, image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500594 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600595 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500596
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500597 png_debug(1, "in png_read_image");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500598
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500599 if (png_ptr == NULL)
600 return;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500601
602#ifdef PNG_READ_INTERLACING_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500603 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
604 {
605 pass = png_set_interlace_handling(png_ptr);
606 /* And make sure transforms are initialized. */
607 png_start_read_image(png_ptr);
608 }
609 else
610 {
Glenn Randers-Pehrsone15a96b2011-01-14 15:47:37 -0600611 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500612 {
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500613 /* Caller called png_start_read_image or png_read_update_info without
614 * first turning on the PNG_INTERLACE transform. We can fix this here,
615 * but the caller should do it!
616 */
617 png_warning(png_ptr, "Interlace handling should be turned on when "
618 "using png_read_image");
619 /* Make sure this is set correctly */
620 png_ptr->num_rows = png_ptr->height;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500621 }
622
623 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
624 * the above error case.
625 */
626 pass = png_set_interlace_handling(png_ptr);
627 }
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500628#else
629 if (png_ptr->interlaced)
630 png_error(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600631 "Cannot read interlaced image -- interlace handler disabled");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500632
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500633 pass = 1;
634#endif
635
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500636 image_height=png_ptr->height;
Guy Schalnate5a37791996-06-05 15:50:50 -0500637
Guy Schalnat0d580581995-07-20 02:43:20 -0500638 for (j = 0; j < pass; j++)
639 {
640 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500641 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500642 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500643 png_read_row(png_ptr, *rp, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500644 rp++;
645 }
646 }
647}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500648#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500649
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500650#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500651/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600652 * file, will verify the end is accurate, and will read any comments
653 * or time information at the end of the file, if info is not NULL.
654 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500655void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600656png_read_end(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500657{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500658 png_debug(1, "in png_read_end");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500659
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500660 if (png_ptr == NULL)
661 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500662
John Bowlerb5d00512012-03-09 09:15:18 -0600663 /* If png_read_end is called in the middle of reading the rows there may
664 * still be pending IDAT data and an owned zstream. Deal with this here.
665 */
666 png_read_finish_IDAT(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500667
John Bowler6f237b62012-03-02 13:13:15 -0600668#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
669 /* Report invalid palette index; added at libng-1.5.10 */
670 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
671 png_ptr->num_palette_max > png_ptr->num_palette)
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -0600672 png_warning(png_ptr, "Read palette index exceeding num_palette");
John Bowler6f237b62012-03-02 13:13:15 -0600673#endif
674
Guy Schalnat0d580581995-07-20 02:43:20 -0500675 do
676 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500677 png_uint_32 length = png_read_chunk_header(png_ptr);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500678 png_uint_32 chunk_name = png_ptr->chunk_name;
Guy Schalnat0d580581995-07-20 02:43:20 -0500679
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500680 if (chunk_name == png_IHDR)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600681 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500682
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500683 else if (chunk_name == png_IEND)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600684 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500685
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600686#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500687 else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
688 PNG_HANDLE_CHUNK_AS_DEFAULT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600689 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500690 if (chunk_name == png_IDAT)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600691 {
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500692 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500693 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600694 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600695 png_handle_unknown(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500696 if (chunk_name == png_PLTE)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600697 png_ptr->mode |= PNG_HAVE_PLTE;
698 }
699#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500700
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500701 else if (chunk_name == png_IDAT)
Guy Schalnat0d580581995-07-20 02:43:20 -0500702 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500703 /* Zero length IDATs are legal after the last IDAT has been
704 * read, but not after other chunks have been read.
705 */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500706 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500707 png_benign_error(png_ptr, "Too many IDATs found");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500708
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500709 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500710 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500711 else if (chunk_name == png_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500712 png_handle_PLTE(png_ptr, info_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500713
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500714#ifdef PNG_READ_bKGD_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500715 else if (chunk_name == png_bKGD)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500716 png_handle_bKGD(png_ptr, info_ptr, length);
717#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500718
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500719#ifdef PNG_READ_cHRM_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500720 else if (chunk_name == png_cHRM)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500721 png_handle_cHRM(png_ptr, info_ptr, length);
722#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500723
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500724#ifdef PNG_READ_gAMA_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500725 else if (chunk_name == png_gAMA)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500726 png_handle_gAMA(png_ptr, info_ptr, length);
727#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500728
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500729#ifdef PNG_READ_hIST_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500730 else if (chunk_name == png_hIST)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731 png_handle_hIST(png_ptr, info_ptr, length);
732#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500733
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500734#ifdef PNG_READ_oFFs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500735 else if (chunk_name == png_oFFs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500736 png_handle_oFFs(png_ptr, info_ptr, length);
737#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500738
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500739#ifdef PNG_READ_pCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500740 else if (chunk_name == png_pCAL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500741 png_handle_pCAL(png_ptr, info_ptr, length);
742#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500743
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500744#ifdef PNG_READ_sCAL_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500745 else if (chunk_name == png_sCAL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600746 png_handle_sCAL(png_ptr, info_ptr, length);
747#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500748
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500749#ifdef PNG_READ_pHYs_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500750 else if (chunk_name == png_pHYs)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500751 png_handle_pHYs(png_ptr, info_ptr, length);
752#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500753
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500754#ifdef PNG_READ_sBIT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500755 else if (chunk_name == png_sBIT)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500756 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500757#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500758
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500759#ifdef PNG_READ_sRGB_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500760 else if (chunk_name == png_sRGB)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600761 png_handle_sRGB(png_ptr, info_ptr, length);
762#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500763
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500764#ifdef PNG_READ_iCCP_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500765 else if (chunk_name == png_iCCP)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600766 png_handle_iCCP(png_ptr, info_ptr, length);
767#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500768
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500769#ifdef PNG_READ_sPLT_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500770 else if (chunk_name == png_sPLT)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600771 png_handle_sPLT(png_ptr, info_ptr, length);
772#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500773
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500774#ifdef PNG_READ_tEXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500775 else if (chunk_name == png_tEXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600776 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500777#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500778
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500779#ifdef PNG_READ_tIME_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500780 else if (chunk_name == png_tIME)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500781 png_handle_tIME(png_ptr, info_ptr, length);
782#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500783
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500784#ifdef PNG_READ_tRNS_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500785 else if (chunk_name == png_tRNS)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500786 png_handle_tRNS(png_ptr, info_ptr, length);
787#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500788
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500789#ifdef PNG_READ_zTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500790 else if (chunk_name == png_zTXt)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600791 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500792#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500793
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500794#ifdef PNG_READ_iTXt_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500795 else if (chunk_name == png_iTXt)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600796 png_handle_iTXt(png_ptr, info_ptr, length);
797#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500798
Guy Schalnat0d580581995-07-20 02:43:20 -0500799 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600800 png_handle_unknown(png_ptr, info_ptr, length);
801 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500802}
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500803#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500804
John Bowler1d7f56a2011-12-21 20:14:23 -0600805/* Free all memory used in the read struct */
806static void
John Bowler5d567862011-12-24 09:12:00 -0600807png_read_destroy(png_structrp png_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500808{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500809 png_debug(1, "in png_read_destroy");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500810
John Bowler07772cb2011-10-14 18:19:47 -0500811#ifdef PNG_READ_GAMMA_SUPPORTED
812 png_destroy_gamma_table(png_ptr);
813#endif
814
Glenn Randers-Pehrson1b8e5672001-08-25 06:46:06 -0500815 png_free(png_ptr, png_ptr->big_row_buf);
Mans Rullgard1c422762011-10-17 16:52:19 -0500816 png_free(png_ptr, png_ptr->big_prev_row);
John Bowlerb5d00512012-03-09 09:15:18 -0600817 png_free(png_ptr, png_ptr->read_buffer);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500818
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500819#ifdef PNG_READ_QUANTIZE_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600820 png_free(png_ptr, png_ptr->palette_lookup);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500821 png_free(png_ptr, png_ptr->quantize_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500822#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500823
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600824 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600825 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600826 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500827
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600828#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500829 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600830 if (png_ptr->free_me & PNG_FREE_TRNS)
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500831 png_free(png_ptr, png_ptr->trans_alpha);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600832 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500833#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500834
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600835 inflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500836
Guy Schalnat6d764711995-12-19 03:22:19 -0600837#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600838 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -0600839#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500840
John Bowler40b26032011-12-22 08:09:15 -0600841#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
842 png_free(png_ptr, png_ptr->unknown_chunk.data);
843#endif
844
845#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
846 png_free(png_ptr, png_ptr->chunk_list);
847#endif
848
John Bowler1d7f56a2011-12-21 20:14:23 -0600849 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
850 * callbacks are still set at this point. They are required to complete the
851 * destruction of the png_struct itself.
Guy Schalnate5a37791996-06-05 15:50:50 -0500852 */
John Bowler1d7f56a2011-12-21 20:14:23 -0600853}
Guy Schalnate5a37791996-06-05 15:50:50 -0500854
John Bowler1d7f56a2011-12-21 20:14:23 -0600855/* Free all memory used by the read */
856void PNGAPI
857png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
858 png_infopp end_info_ptr_ptr)
859{
John Bowler5d567862011-12-24 09:12:00 -0600860 png_structrp png_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500861
John Bowler1d7f56a2011-12-21 20:14:23 -0600862 png_debug(1, "in png_destroy_read_struct");
Guy Schalnate5a37791996-06-05 15:50:50 -0500863
John Bowler1d7f56a2011-12-21 20:14:23 -0600864 if (png_ptr_ptr != NULL)
865 png_ptr = *png_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500866
John Bowler1d7f56a2011-12-21 20:14:23 -0600867 if (png_ptr == NULL)
868 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600869
John Bowler1d7f56a2011-12-21 20:14:23 -0600870 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
871 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
872 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
873 */
874 png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
875 png_destroy_info_struct(png_ptr, info_ptr_ptr);
876
877 *png_ptr_ptr = NULL;
878 png_read_destroy(png_ptr);
879 png_destroy_png_struct(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500880}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600881
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500882void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600883png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600884{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500885 if (png_ptr == NULL)
886 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500887
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600888 png_ptr->read_row_fn = read_row_fn;
889}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600890
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500891
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500892#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500893#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500894void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600895png_read_png(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600896 int transforms,
897 voidp params)
898{
899 int row;
900
John Bowler6a6d79f2011-02-12 08:56:40 -0600901 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500902 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600903
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500904 /* png_read_info() gives us all of the information from the
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600905 * PNG file before the first IDAT (image data chunk).
906 */
907 png_read_info(png_ptr, info_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500908 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
909 png_error(png_ptr, "Image is too high to process with png_read_png()");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600910
911 /* -------------- image transformations start here ------------------- */
912
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500913#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -0500914 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500915 */
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -0500916 if (transforms & PNG_TRANSFORM_SCALE_16)
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500917 {
Glenn Randers-Pehrson6da2f2d2011-06-17 23:07:16 -0500918 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
919 * did in earlier versions, while "scale_16" is now more accurate.
920 */
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500921 png_set_scale_16(png_ptr);
922 }
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -0500923#endif
924
925#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
John Bowler8d261262011-06-18 13:37:11 -0500926 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
927 * latter by doing SCALE first. This is ok and allows apps not to check for
928 * which is supported to get the right answer.
929 */
930 if (transforms & PNG_TRANSFORM_STRIP_16)
931 png_set_strip_16(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600932#endif
933
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500934#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500935 /* Strip alpha bytes from the input data without combining with
936 * the background (not recommended).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600937 */
938 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500939 png_set_strip_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600940#endif
941
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600942#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500943 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600944 * byte into separate bytes (useful for paletted and grayscale images).
945 */
946 if (transforms & PNG_TRANSFORM_PACKING)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500947 png_set_packing(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600948#endif
949
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500950#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600951 /* Change the order of packed pixels to least significant bit first
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500952 * (not useful if you are using png_set_packing).
953 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600954 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500955 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600956#endif
957
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500958#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600959 /* Expand paletted colors into true RGB triplets
960 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
961 * Expand paletted or RGB images with transparency to full alpha
962 * channels so the data will be available as RGBA quartets.
963 */
964 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500965 if ((png_ptr->bit_depth < 8) ||
966 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
967 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600968 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600969#endif
970
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500971 /* We don't handle background color or gamma transformation or quantizing.
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500972 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600973
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500974#ifdef PNG_READ_INVERT_SUPPORTED
Glenn Randers-Pehrsond8eb62f2009-05-30 20:19:20 -0500975 /* Invert monochrome files to have 0 as white and 1 as black
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500976 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600977 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500978 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600979#endif
980
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500981#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600982 /* If you want to shift the pixel values from the range [0,255] or
983 * [0,65535] to the original [0,7] or [0,31], or whatever range the
984 * colors were originally in:
985 */
986 if ((transforms & PNG_TRANSFORM_SHIFT)
987 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
988 {
989 png_color_8p sig_bit;
990
991 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
992 png_set_shift(png_ptr, sig_bit);
993 }
994#endif
995
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500996#ifdef PNG_READ_BGR_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -0600997 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600998 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500999 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001000#endif
1001
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001002#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001003 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001004 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001005 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001006#endif
1007
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001008#ifdef PNG_READ_SWAP_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -05001009 /* Swap bytes of 16-bit files to least significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001010 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001011 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001012#endif
1013
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001014/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001015#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001016 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001017 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001018 png_set_invert_alpha(png_ptr);
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001019#endif
1020
Glenn Randers-Pehrsonc1a4d642009-10-29 23:29:24 -05001021/* Added at libpng-1.2.41 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001022#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001023 /* Expand grayscale image to RGB */
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001024 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001025 png_set_gray_to_rgb(png_ptr);
Glenn Randers-Pehrson99708d52009-06-29 17:30:00 -05001026#endif
1027
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001028/* Added at libpng-1.5.4 */
John Bowler96cec0e2011-05-08 22:48:12 -05001029#ifdef PNG_READ_EXPAND_16_SUPPORTED
1030 if (transforms & PNG_TRANSFORM_EXPAND_16)
1031 png_set_expand_16(png_ptr);
1032#endif
1033
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001034 /* We don't handle adding filler bytes */
1035
John Bowler6a6d79f2011-02-12 08:56:40 -06001036 /* We use png_read_image and rely on that for interlace handling, but we also
1037 * call png_read_update_info therefore must turn on interlace handling now:
1038 */
1039 (void)png_set_interlace_handling(png_ptr);
1040
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001041 /* Optional call to gamma correct and add the background to the palette
1042 * and update info structure. REQUIRED if you are expecting libpng to
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001043 * update the palette for you (i.e., you selected such a transform above).
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001044 */
1045 png_read_update_info(png_ptr, info_ptr);
1046
1047 /* -------------- image transformations end here ------------------- */
1048
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001049 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001050 if (info_ptr->row_pointers == NULL)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001051 {
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001052 png_uint_32 iptr;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001053
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001054 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson262d0ff2010-03-03 07:06:54 -06001055 info_ptr->height * png_sizeof(png_bytep));
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001056 for (iptr=0; iptr<info_ptr->height; iptr++)
1057 info_ptr->row_pointers[iptr] = NULL;
1058
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001059 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrsond9f21ee2009-08-31 10:52:38 -05001060
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001061 for (row = 0; row < (int)info_ptr->height; row++)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001062 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001063 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001064 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001065
1066 png_read_image(png_ptr, info_ptr->row_pointers);
1067 info_ptr->valid |= PNG_INFO_IDAT;
1068
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001069 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001070 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001071
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001072 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1073 PNG_UNUSED(params)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001074
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001075}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001076#endif /* PNG_INFO_IMAGE_SUPPORTED */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001077#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
John Bowler7875d532011-11-07 22:33:49 -06001078
1079#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1080/* SIMPLIFIED READ
1081 *
1082 * This code currently relies on the sequential reader, though it could easily
1083 * be made to work with the progressive one.
1084 */
John Bowler5bc90382012-01-23 22:43:22 -06001085/* Arguments to png_image_finish_read: */
1086
1087/* Encoding of PNG data (used by the color-map code) */
1088# define E_NOTSET 0 /* File encoding not yet known */
1089# define E_sRGB 1 /* 8-bit encoded to sRGB gamma */
1090# define E_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1091# define E_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1092# define E_LINEAR8 4 /* 8-bit linear: only from a file value */
1093
1094/* Color-map processing: after libpng has run on the PNG image further
1095 * processing may be needed to conver the data to color-map indicies.
1096 */
1097#define PNG_CMAP_NONE 0
1098#define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1099#define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1100#define PNG_CMAP_RGB 3 /* Process RGB data */
1101#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1102
1103/* The following document where the background is for each processing case. */
1104#define PNG_CMAP_NONE_BACKGROUND 256
1105#define PNG_CMAP_GA_BACKGROUND 231
1106#define PNG_CMAP_TRANS_BACKGROUND 254
1107#define PNG_CMAP_RGB_BACKGROUND 256
1108#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1109
1110typedef struct
1111{
1112 /* Arguments: */
1113 png_imagep image;
1114 png_voidp buffer;
1115 png_int_32 row_stride;
1116 png_voidp colormap;
John Bowler40ca77a2012-01-28 23:19:42 -06001117 png_const_colorp background;
John Bowler5bc90382012-01-23 22:43:22 -06001118 /* Local variables: */
1119 png_bytep local_row;
1120 png_bytep first_row;
1121 ptrdiff_t row_bytes; /* step between rows */
1122 int file_encoding; /* E_ values above */
1123 png_fixed_point gamma_to_linear; /* For E_FILE, reciprocal of gamma */
1124 int colormap_processing; /* PNG_CMAP_ values above */
1125} png_image_read_control;
1126
John Bowler7875d532011-11-07 22:33:49 -06001127/* Do all the *safe* initialization - 'safe' means that png_error won't be
John Bowler18c5cfa2011-11-16 14:26:34 -06001128 * called, so setting up the jmp_buf is not required. This means that anything
1129 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1130 * instead so that control is returned safely back to this routine.
John Bowler7875d532011-11-07 22:33:49 -06001131 */
1132static int
1133png_image_read_init(png_imagep image)
1134{
John Bowler5bc90382012-01-23 22:43:22 -06001135 if (image->opaque == NULL)
1136 {
1137 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
John Bowler7875d532011-11-07 22:33:49 -06001138 png_safe_error, png_safe_warning);
1139
John Bowler5bc90382012-01-23 22:43:22 -06001140 /* And set the rest of the structure to NULL to ensure that the various
1141 * fields are consistent.
1142 */
1143 memset(image, 0, sizeof *image);
1144 image->version = PNG_IMAGE_VERSION;
John Bowler7875d532011-11-07 22:33:49 -06001145
John Bowler5bc90382012-01-23 22:43:22 -06001146 if (png_ptr != NULL)
John Bowler7875d532011-11-07 22:33:49 -06001147 {
John Bowler5bc90382012-01-23 22:43:22 -06001148 png_infop info_ptr = png_create_info_struct(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001149
John Bowler5bc90382012-01-23 22:43:22 -06001150 if (info_ptr != NULL)
John Bowler7875d532011-11-07 22:33:49 -06001151 {
John Bowler5bc90382012-01-23 22:43:22 -06001152 png_controlp control = png_voidcast(png_controlp,
1153 png_malloc_warn(png_ptr, sizeof *control));
John Bowler7875d532011-11-07 22:33:49 -06001154
John Bowler5bc90382012-01-23 22:43:22 -06001155 if (control != NULL)
1156 {
1157 png_memset(control, 0, sizeof *control);
John Bowler7875d532011-11-07 22:33:49 -06001158
John Bowler5bc90382012-01-23 22:43:22 -06001159 control->png_ptr = png_ptr;
1160 control->info_ptr = info_ptr;
1161 control->for_write = 0;
1162
1163 image->opaque = control;
1164 return 1;
1165 }
1166
1167 /* Error clean up */
1168 png_destroy_info_struct(png_ptr, &info_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001169 }
1170
John Bowler5bc90382012-01-23 22:43:22 -06001171 png_destroy_read_struct(&png_ptr, NULL, NULL);
John Bowler7875d532011-11-07 22:33:49 -06001172 }
1173
John Bowler5bc90382012-01-23 22:43:22 -06001174 return png_image_error(image, "png_image_read: out of memory");
John Bowler7875d532011-11-07 22:33:49 -06001175 }
1176
John Bowler5bc90382012-01-23 22:43:22 -06001177 return png_image_error(image, "png_image_read: opaque pointer not NULL");
John Bowler7875d532011-11-07 22:33:49 -06001178}
1179
1180/* Utility to find the base format of a PNG file from a png_struct. */
1181static png_uint_32
John Bowler5bc90382012-01-23 22:43:22 -06001182png_image_format(png_structrp png_ptr)
John Bowler7875d532011-11-07 22:33:49 -06001183{
1184 png_uint_32 format = 0;
1185
1186 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1187 format |= PNG_FORMAT_FLAG_COLOR;
1188
1189 if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1190 format |= PNG_FORMAT_FLAG_ALPHA;
1191
John Bowler5bc90382012-01-23 22:43:22 -06001192 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1193 * sets the png_struct fields; that's all we are interested in here. The
1194 * precise interaction with an app call to png_set_tRNS and PNG file reading
1195 * is unclear.
1196 */
1197 else if (png_ptr->num_trans > 0)
John Bowler7875d532011-11-07 22:33:49 -06001198 format |= PNG_FORMAT_FLAG_ALPHA;
1199
1200 if (png_ptr->bit_depth == 16)
1201 format |= PNG_FORMAT_FLAG_LINEAR;
1202
John Bowler5bc90382012-01-23 22:43:22 -06001203 if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE)
1204 format |= PNG_FORMAT_FLAG_COLORMAP;
1205
John Bowler7875d532011-11-07 22:33:49 -06001206 return format;
1207}
1208
John Bowler5bc90382012-01-23 22:43:22 -06001209/* Is the given gamma significantly different from sRGB? The test is the same
1210 * one used in pngrtran.c when deciding whether to do gamma correction. The
1211 * arithmetic optimizes the division by using the fact that the inverse of the
1212 * file sRGB gamma is 2.2
1213 */
1214static int
1215png_gamma_not_sRGB(png_fixed_point g)
1216{
1217 if (g < PNG_FP_1)
1218 {
1219 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
1220 if (g == 0)
1221 return 0;
1222
1223 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1224 }
1225
1226 return 1;
1227}
1228
John Bowler7875d532011-11-07 22:33:49 -06001229/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1230 * header and fill in all the information. This is executed in a safe context,
1231 * unlike the init routine above.
1232 */
1233static int
1234png_image_read_header(png_voidp argument)
1235{
John Bowler4fa96a42011-11-16 16:39:16 -06001236 png_imagep image = png_voidcast(png_imagep, argument);
John Bowler5d567862011-12-24 09:12:00 -06001237 png_structrp png_ptr = image->opaque->png_ptr;
1238 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001239
1240 png_read_info(png_ptr, info_ptr);
1241
1242 /* Do this the fast way; just read directly out of png_struct. */
1243 image->width = png_ptr->width;
1244 image->height = png_ptr->height;
1245
1246 {
John Bowler5bc90382012-01-23 22:43:22 -06001247 png_uint_32 format = png_image_format(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001248
1249 image->format = format;
John Bowler04336ba2012-01-16 07:48:36 -06001250
John Bowler7875d532011-11-07 22:33:49 -06001251 /* Now try to work out whether the color data does not match sRGB. */
1252 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 &&
1253 (info_ptr->valid & PNG_INFO_sRGB) == 0)
1254 {
1255 /* gamma is irrelevant because libpng does gamma correction, what
1256 * matters is if the cHRM chunk doesn't match or, in the absence of
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06001257 * cRHM, if the iCCP profile appears to have different end points.
John Bowler7875d532011-11-07 22:33:49 -06001258 */
1259 if (info_ptr->valid & PNG_INFO_cHRM)
1260 {
1261 /* TODO: this is a copy'n'paste from pngrutil.c, make a common
1262 * checking function. This checks for a 1% error.
1263 */
1264 /* The cHRM chunk is used in preference to iCCP */
1265 if (PNG_OUT_OF_RANGE(info_ptr->x_white, 31270, 1000) ||
1266 PNG_OUT_OF_RANGE(info_ptr->y_white, 32900, 1000) ||
1267 PNG_OUT_OF_RANGE(info_ptr->x_red, 64000, 1000) ||
1268 PNG_OUT_OF_RANGE(info_ptr->y_red, 33000, 1000) ||
1269 PNG_OUT_OF_RANGE(info_ptr->x_green, 30000, 1000) ||
1270 PNG_OUT_OF_RANGE(info_ptr->y_green, 60000, 1000) ||
1271 PNG_OUT_OF_RANGE(info_ptr->x_blue, 15000, 1000) ||
1272 PNG_OUT_OF_RANGE(info_ptr->y_blue, 6000, 1000))
1273 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1274 }
1275
1276 else if (info_ptr->valid & PNG_INFO_iCCP)
1277 {
Glenn Randers-Pehrson6d8e0902011-11-11 18:41:59 -06001278# if 0
John Bowler04336ba2012-01-16 07:48:36 -06001279 /* TODO: IMPLEMENT THIS! */
John Bowler7875d532011-11-07 22:33:49 -06001280 /* Here if we just have an iCCP chunk. */
1281 if (!png_iCCP_is_sRGB(png_ptr, info_ptr))
1282# endif
1283 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1284 }
1285 }
1286 }
1287
John Bowler5bc90382012-01-23 22:43:22 -06001288 /* We need the maximum number of entries regardless of the format the
1289 * application sets here.
1290 */
1291 {
1292 png_uint_32 cmap_entries;
1293
1294 switch (png_ptr->color_type)
1295 {
1296 case PNG_COLOR_TYPE_GRAY:
1297 cmap_entries = 1U << png_ptr->bit_depth;
1298 break;
1299
1300 case PNG_COLOR_TYPE_PALETTE:
1301 cmap_entries = png_ptr->num_palette;
1302 break;
1303
1304 default:
1305 cmap_entries = 256;
1306 break;
1307 }
1308
1309 if (cmap_entries > 256)
1310 cmap_entries = 256;
1311
1312 image->colormap_entries = cmap_entries;
1313 }
1314
John Bowler7875d532011-11-07 22:33:49 -06001315 return 1;
1316}
1317
1318#ifdef PNG_STDIO_SUPPORTED
1319int PNGAPI
1320png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1321{
John Bowler5bc90382012-01-23 22:43:22 -06001322 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001323 {
1324 if (file != NULL)
1325 {
1326 if (png_image_read_init(image))
1327 {
1328 /* This is slightly evil, but png_init_io doesn't do anything other
1329 * than this and we haven't changed the standard IO functions so
1330 * this saves a 'safe' function.
1331 */
1332 image->opaque->png_ptr->io_ptr = file;
1333 return png_safe_execute(image, png_image_read_header, image);
1334 }
1335 }
1336
1337 else
1338 return png_image_error(image,
1339 "png_image_begin_read_from_stdio: invalid argument");
1340 }
1341
1342 return 0;
1343}
1344
1345int PNGAPI
1346png_image_begin_read_from_file(png_imagep image, const char *file_name)
1347{
John Bowler5bc90382012-01-23 22:43:22 -06001348 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001349 {
1350 if (file_name != NULL)
1351 {
1352 FILE *fp = fopen(file_name, "rb");
1353
1354 if (fp != NULL)
1355 {
1356 if (png_image_read_init(image))
1357 {
1358 image->opaque->png_ptr->io_ptr = fp;
1359 image->opaque->owned_file = 1;
1360 return png_safe_execute(image, png_image_read_header, image);
1361 }
1362
1363 /* Clean up: just the opened file. */
1364 (void)fclose(fp);
1365 }
1366
1367 else
1368 return png_image_error(image, strerror(errno));
1369 }
1370
1371 else
1372 return png_image_error(image,
1373 "png_image_begin_read_from_file: invalid argument");
1374 }
1375
1376 return 0;
1377}
1378#endif /* PNG_STDIO_SUPPORTED */
1379
1380static void PNGCBAPI
1381png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
1382{
1383 if (png_ptr != NULL)
1384 {
John Bowler4fa96a42011-11-16 16:39:16 -06001385 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001386 if (image != NULL)
1387 {
1388 png_controlp cp = image->opaque;
1389 if (cp != NULL)
1390 {
1391 png_const_bytep memory = cp->memory;
1392 png_size_t size = cp->size;
1393
1394 if (memory != NULL && size >= need)
1395 {
John Bowlerbaeb6d12011-11-26 18:21:02 -06001396 png_memcpy(out, memory, need);
John Bowler7875d532011-11-07 22:33:49 -06001397 cp->memory = memory + need;
1398 cp->size = size - need;
1399 return;
1400 }
1401
1402 png_error(png_ptr, "read beyond end of data");
1403 }
1404 }
1405
1406 png_error(png_ptr, "invalid memory read");
1407 }
1408}
1409
1410int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1411 png_const_voidp memory, png_size_t size)
1412{
John Bowler5bc90382012-01-23 22:43:22 -06001413 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06001414 {
1415 if (memory != NULL && size > 0)
1416 {
1417 if (png_image_read_init(image))
1418 {
1419 /* Now set the IO functions to read from the memory buffer and
1420 * store it into io_ptr. Again do this in-place to avoid calling a
1421 * libpng function that requires error handling.
1422 */
John Bowler4fa96a42011-11-16 16:39:16 -06001423 image->opaque->memory = png_voidcast(png_const_bytep, memory);
John Bowler7875d532011-11-07 22:33:49 -06001424 image->opaque->size = size;
1425 image->opaque->png_ptr->io_ptr = image;
1426 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1427
1428 return png_safe_execute(image, png_image_read_header, image);
1429 }
1430 }
1431
1432 else
1433 return png_image_error(image,
1434 "png_image_begin_read_from_memory: invalid argument");
1435 }
1436
1437 return 0;
1438}
1439
John Bowler5bc90382012-01-23 22:43:22 -06001440/* Utility function to skip chunks that are not used by the simplified image
1441 * read functions and an appropriate macro to call it.
1442 */
1443#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1444static void
1445png_image_skip_unused_chunks(png_structrp png_ptr)
John Bowler04336ba2012-01-16 07:48:36 -06001446{
John Bowler5bc90382012-01-23 22:43:22 -06001447 /* Prepare the reader to ignore all recognized chunks whose data will not
1448 * be used, i.e., all chunks recognized by libpng except for those
1449 * involved in basic image reading:
1450 *
1451 * IHDR, PLTE, IDAT, IEND
1452 *
1453 * Or image data handling:
1454 *
1455 * tRNS, bKGD, gAMA, cHRM, sRGB, iCCP and sBIT.
1456 *
1457 * This provides a small performance improvement and eliminates any
1458 * potential vulnerability to security problems in the unused chunks.
1459 *
1460 * TODO: make it so that this is an explicit list to process, not a list
1461 * to ignore?
1462 */
John Bowler04336ba2012-01-16 07:48:36 -06001463 {
John Bowler5bc90382012-01-23 22:43:22 -06001464 static PNG_CONST png_byte chunks_to_ignore[] = {
1465 104, 73, 83, 84, '\0', /* hIST */
1466 105, 84, 88, 116, '\0', /* iTXt */
1467 111, 70, 70, 115, '\0', /* oFFs */
1468 112, 67, 65, 76, '\0', /* pCAL */
1469 112, 72, 89, 115, '\0', /* pHYs */
1470 115, 67, 65, 76, '\0', /* sCAL */
1471 115, 80, 76, 84, '\0', /* sPLT */
1472 116, 69, 88, 116, '\0', /* tEXt */
1473 116, 73, 77, 69, '\0', /* tIME */
1474 122, 84, 88, 116, '\0' /* zTXt */
1475 };
1476
1477 /* Ignore unknown chunks */
1478 png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */,
1479 NULL, 0);
1480
1481 /* Ignore known but unused chunks */
1482 png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */,
1483 chunks_to_ignore, (sizeof chunks_to_ignore)/5);
1484 }
1485}
1486
1487# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1488#else
1489# define PNG_SKIP_CHUNKS(p) ((void)0)
1490#endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */
1491
1492/* The following macro gives the exact rounded answer for all values in the
1493 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1494 * the correct numbers 0..5
1495 */
1496#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1497
1498/* Utility functions to make particular color-maps */
1499static void
1500set_file_encoding(png_image_read_control *display)
1501{
1502 png_fixed_point g = display->image->opaque->png_ptr->gamma;
1503 if (png_gamma_significant(g))
1504 {
1505 if (png_gamma_not_sRGB(g))
John Bowler04336ba2012-01-16 07:48:36 -06001506 {
John Bowler5bc90382012-01-23 22:43:22 -06001507 display->file_encoding = E_FILE;
1508 display->gamma_to_linear = png_reciprocal(g);
John Bowler04336ba2012-01-16 07:48:36 -06001509 }
1510
1511 else
John Bowler5bc90382012-01-23 22:43:22 -06001512 display->file_encoding = E_sRGB;
John Bowler04336ba2012-01-16 07:48:36 -06001513 }
1514
John Bowler5bc90382012-01-23 22:43:22 -06001515 else
1516 display->file_encoding = E_LINEAR8;
John Bowler04336ba2012-01-16 07:48:36 -06001517}
1518
John Bowler5bc90382012-01-23 22:43:22 -06001519static unsigned int
1520decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
John Bowler7875d532011-11-07 22:33:49 -06001521{
John Bowler5bc90382012-01-23 22:43:22 -06001522 if (encoding == E_FILE) /* double check */
1523 encoding = display->file_encoding;
John Bowler7875d532011-11-07 22:33:49 -06001524
John Bowler5bc90382012-01-23 22:43:22 -06001525 if (encoding == E_NOTSET) /* must be the file encoding */
1526 {
1527 set_file_encoding(display);
1528 encoding = display->file_encoding;
1529 }
1530
1531 switch (encoding)
1532 {
1533 case E_FILE:
1534 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
1535 break;
1536
1537 case E_sRGB:
1538 value = png_sRGB_table[value];
1539 break;
1540
1541 case E_LINEAR:
1542 break;
1543
1544 case E_LINEAR8:
1545 value *= 257;
1546 break;
1547
1548 default:
1549 png_error(display->image->opaque->png_ptr,
1550 "unexpected encoding (internal error)");
1551 break;
1552 }
1553
1554 return value;
1555}
1556
1557static png_uint_32
1558png_colormap_compose(png_image_read_control *display,
1559 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
1560 png_uint_32 background, int encoding)
1561{
1562 /* The file value is composed on the background, the background has the given
1563 * encoding and so does the result, the file is encoded with E_FILE and the
1564 * file and alpha are 8-bit values. The (output) encoding will always be
1565 * E_LINEAR or E_sRGB.
1566 */
1567 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
1568 png_uint_32 b = decode_gamma(display, background, encoding);
1569
1570 /* The alpha is always an 8-bit value (it comes from the palette), the value
1571 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1572 */
1573 f = f * alpha + b * (255-alpha);
1574
1575 if (encoding == E_LINEAR)
1576 {
1577 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1578 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1579 */
1580 f *= 257; /* Now scaled by 65535 */
1581 f += f >> 16;
1582 f = (f+32768) >> 16;
1583 }
1584
1585 else /* E_sRGB */
1586 f = PNG_sRGB_FROM_LINEAR(f);
1587
1588 return f;
1589}
1590
1591/* NOTE: E_LINEAR values to this routine must be 16-bit, but E_FILE values must
1592 * be 8-bit.
1593 */
1594static void
1595png_create_colormap_entry(png_image_read_control *display,
1596 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
1597 png_uint_32 alpha, int encoding)
1598{
1599 png_imagep image = display->image;
1600 const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ?
1601 E_LINEAR : E_sRGB;
1602 const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
1603 (red != green || green != blue);
1604
1605 if (ip > 255)
1606 png_error(image->opaque->png_ptr, "color-map index out of range");
1607
1608 /* Update the cache with whether the file gamma is significantly different
1609 * from sRGB.
1610 */
1611 if (encoding == E_FILE)
1612 {
1613 if (display->file_encoding == E_NOTSET)
1614 set_file_encoding(display);
1615
1616 /* Note that the cached value may be E_FILE too, but if it is then the
1617 * gamma_to_linear member has been set.
1618 */
1619 encoding = display->file_encoding;
1620 }
1621
1622 if (encoding == E_FILE)
1623 {
1624 png_fixed_point g = display->gamma_to_linear;
1625
1626 red = png_gamma_16bit_correct(red*257, g);
1627 green = png_gamma_16bit_correct(green*257, g);
1628 blue = png_gamma_16bit_correct(blue*257, g);
1629
1630 if (convert_to_Y || output_encoding == E_LINEAR)
1631 {
1632 alpha *= 257;
1633 encoding = E_LINEAR;
1634 }
1635
1636 else
1637 {
1638 red = PNG_sRGB_FROM_LINEAR(red * 255);
1639 green = PNG_sRGB_FROM_LINEAR(green * 255);
1640 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1641 encoding = E_sRGB;
1642 }
1643 }
1644
1645 else if (encoding == E_LINEAR8)
1646 {
1647 /* This encoding occurs quite frequently in test cases because PngSuite
1648 * includes a gAMA 1.0 chunk with most images.
1649 */
1650 red *= 257;
1651 green *= 257;
1652 blue *= 257;
1653 alpha *= 257;
1654 encoding = E_LINEAR;
1655 }
1656
1657 else if (encoding == E_sRGB && (convert_to_Y || output_encoding == E_LINEAR))
1658 {
1659 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1660 * linear.
1661 */
1662 red = png_sRGB_table[red];
1663 green = png_sRGB_table[green];
1664 blue = png_sRGB_table[blue];
1665 alpha *= 257;
1666 encoding = E_LINEAR;
1667 }
1668
1669 /* This is set if the color isn't gray but the output is. */
1670 if (encoding == E_LINEAR)
1671 {
1672 if (convert_to_Y)
1673 {
1674 /* NOTE: these values are copied from png_do_rgb_to_gray */
1675 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
1676 (png_uint_32)2366 * blue;
1677
1678 if (output_encoding == E_LINEAR)
1679 y = (y + 16384) >> 15;
1680
1681 else
1682 {
1683 /* y is scaled by 32768, we need it scaled by 255: */
1684 y = (y + 128) >> 8;
1685 y *= 255;
1686 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
1687 encoding = E_sRGB;
1688 }
1689
1690 blue = red = green = y;
1691 }
1692
1693 else if (output_encoding == E_sRGB)
1694 {
1695 red = PNG_sRGB_FROM_LINEAR(red * 255);
1696 green = PNG_sRGB_FROM_LINEAR(green * 255);
1697 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1698 alpha = PNG_DIV257(alpha);
1699 encoding = E_sRGB;
1700 }
1701 }
1702
1703 if (encoding != output_encoding)
1704 png_error(image->opaque->png_ptr, "bad encoding (internal error)");
1705
1706 /* Store the value. */
1707 {
1708# ifdef PNG_FORMAT_BGR_SUPPORTED
1709 const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1710 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
1711# else
1712# define afirst 0
1713# endif
1714# ifdef PNG_FORMAT_BGR_SUPPORTED
1715 const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
1716# else
1717# define bgr 0
1718# endif
1719
1720 if (output_encoding == E_LINEAR)
1721 {
1722 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
1723
1724 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1725
1726 /* The linear 16-bit values must be pre-multiplied by the alpha channel
1727 * value, if less than 65535 (this is, effectively, composite on black
1728 * if the alpha channel is removed.)
1729 */
1730 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1731 {
1732 case 4:
1733 entry[afirst ? 0 : 3] = (png_uint_16)alpha;
1734 /* FALL THROUGH */
1735
1736 case 3:
1737 if (alpha < 65535)
1738 {
1739 if (alpha > 0)
1740 {
1741 blue = (blue * alpha + 32767U)/65535U;
1742 green = (green * alpha + 32767U)/65535U;
1743 red = (red * alpha + 32767U)/65535U;
1744 }
1745
1746 else
1747 red = green = blue = 0;
1748 }
1749 entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
1750 entry[afirst + 1] = (png_uint_16)green;
1751 entry[afirst + bgr] = (png_uint_16)red;
1752 break;
1753
1754 case 2:
1755 entry[1 ^ afirst] = (png_uint_16)alpha;
1756 /* FALL THROUGH */
1757
1758 case 1:
1759 if (alpha < 65535)
1760 {
1761 if (alpha > 0)
1762 green = (green * alpha + 32767U)/65535U;
1763
1764 else
1765 green = 0;
1766 }
1767 entry[afirst] = (png_uint_16)green;
1768 break;
1769
1770 default:
1771 break;
1772 }
1773 }
1774
1775 else /* output encoding is E_sRGB */
1776 {
1777 png_bytep entry = png_voidcast(png_bytep, display->colormap);
1778
1779 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1780
1781 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1782 {
1783 case 4:
1784 entry[afirst ? 0 : 3] = (png_byte)alpha;
1785 case 3:
1786 entry[afirst + (2 ^ bgr)] = (png_byte)blue;
1787 entry[afirst + 1] = (png_byte)green;
1788 entry[afirst + bgr] = (png_byte)red;
1789 break;
1790
1791 case 2:
1792 entry[1 ^ afirst] = (png_byte)alpha;
1793 case 1:
1794 entry[afirst] = (png_byte)green;
1795 break;
1796
1797 default:
1798 break;
1799 }
1800 }
1801
1802# ifdef afirst
1803# undef afirst
1804# endif
1805# ifdef bgr
1806# undef bgr
1807# endif
1808 }
1809}
1810
John Bowler7875d532011-11-07 22:33:49 -06001811static int
John Bowler5bc90382012-01-23 22:43:22 -06001812make_gray_colormap(png_image_read_control *display)
1813{
1814 unsigned int i;
1815
1816 for (i=0; i<256; ++i)
1817 png_create_colormap_entry(display, i, i, i, i, 255, E_sRGB);
1818
1819 return i;
1820}
1821#define PNG_GRAY_COLORMAP_ENTRIES 256
1822
1823static int
1824make_ga_colormap(png_image_read_control *display)
1825{
1826 unsigned int i, a;
1827
1828 /* Alpha is retained, the output will be a color-map with entries
1829 * selected by six levels of alpha. One transparent entry, 6 gray
1830 * levels for all the intermediate alpha values, leaving 230 entries
1831 * for the opaque grays. The color-map entries are the six values
1832 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
1833 * relevant entry.
1834 *
1835 * if (alpha > 229) // opaque
1836 * {
1837 * // The 231 entries are selected to make the math below work:
1838 * base = 0;
1839 * entry = (231 * gray + 128) >> 8;
1840 * }
1841 * else if (alpha < 26) // transparent
1842 * {
1843 * base = 231;
1844 * entry = 0;
1845 * }
1846 * else // partially opaque
1847 * {
1848 * base = 226 + 6 * PNG_DIV51(alpha);
1849 * entry = PNG_DIV51(gray);
1850 * }
1851 */
1852 i = 0;
1853 while (i < 231)
1854 {
1855 unsigned int gray = (i * 256 + 115) / 231;
1856 png_create_colormap_entry(display, i++, gray, gray, gray, 255, E_sRGB);
1857 }
1858
1859 /* 255 is used here for the component values for consistency with the code
1860 * that undoes premultiplication in pngwrite.c.
1861 */
1862 png_create_colormap_entry(display, i++, 255, 255, 255, 0, E_sRGB);
1863
1864 for (a=1; a<5; ++a)
1865 {
1866 unsigned int g;
1867
1868 for (g=0; g<6; ++g)
1869 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
1870 E_sRGB);
1871 }
1872
1873 return i;
1874}
1875
1876#define PNG_GA_COLORMAP_ENTRIES 256
1877
1878static int
1879make_rgb_colormap(png_image_read_control *display)
1880{
1881 unsigned int i, r;
1882
1883 /* Build a 6x6x6 opaque RGB cube */
1884 for (i=r=0; r<6; ++r)
1885 {
1886 unsigned int g;
1887
1888 for (g=0; g<6; ++g)
1889 {
1890 unsigned int b;
1891
1892 for (b=0; b<6; ++b)
1893 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
1894 E_sRGB);
1895 }
1896 }
1897
1898 return i;
1899}
1900
1901#define PNG_RGB_COLORMAP_ENTRIES 216
1902
1903/* Return a palette index to the above palette given three 8-bit sRGB values. */
1904#define PNG_RGB_INDEX(r,g,b) \
1905 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
1906
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06001907static int
John Bowler5bc90382012-01-23 22:43:22 -06001908png_image_read_colormap(png_voidp argument)
1909{
1910 png_image_read_control *display =
1911 png_voidcast(png_image_read_control*, argument);
1912 const png_imagep image = display->image;
1913
1914 const png_structrp png_ptr = image->opaque->png_ptr;
1915 const png_uint_32 output_format = image->format;
1916 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ?
1917 E_LINEAR : E_sRGB;
1918
1919 unsigned int cmap_entries;
1920 unsigned int output_processing; /* Output processing option */
1921 unsigned int data_encoding = E_NOTSET; /* Encoding libpng must produce */
1922
1923 /* Background information; the background color and the index of this color
1924 * in the color-map if it exists (else 256).
1925 */
1926 unsigned int background_index = 256;
1927 png_uint_32 back_r, back_g, back_b;
1928
1929 /* Flags to accumulate things that need to be done to the input. */
1930 int expand_tRNS = 0;
1931
1932 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
1933 * very difficult to do, the results look awful, and it is difficult to see
1934 * what possible use it is because the application can't control the
1935 * color-map.
1936 */
1937 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
1938 png_ptr->num_trans > 0) /* alpha in input */ &&
1939 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
1940 {
1941 if (output_encoding == E_LINEAR) /* compose on black */
1942 back_b = back_g = back_r = 0;
1943
1944 else if (display->background == NULL /* no way to remove it */)
1945 png_error(png_ptr,
1946 "a background color must be supplied to remove alpha/transparency");
1947
1948 /* Get a copy of the background color (this avoids repeating the checks
1949 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
1950 * output format.
1951 */
1952 else
1953 {
1954 back_g = display->background->green;
1955 if (output_format & PNG_FORMAT_FLAG_COLOR)
1956 {
1957 back_r = display->background->red;
1958 back_b = display->background->blue;
1959 }
1960 else
1961 back_b = back_r = back_g;
1962 }
1963 }
1964
1965 else if (output_encoding == E_LINEAR)
1966 back_b = back_r = back_g = 65535;
1967
1968 else
1969 back_b = back_r = back_g = 255;
1970
1971 /* Decide what to do based on the PNG color type of the input data. The
1972 * utility function png_create_colormap_entry deals with most aspects of the
1973 * output transformations; this code works out how to produce bytes of
1974 * color-map entries from the original format.
1975 */
1976 switch (png_ptr->color_type)
1977 {
1978 case PNG_COLOR_TYPE_GRAY:
1979 if (png_ptr->bit_depth <= 8)
1980 {
1981 /* There at most 256 colors in the output, regardless of
1982 * transparency.
1983 */
1984 unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
1985
1986 cmap_entries = 1U << png_ptr->bit_depth;
1987 if (cmap_entries > image->colormap_entries)
1988 png_error(png_ptr, "gray[8] color-map: too few entries");
1989
1990 step = 255 / (cmap_entries - 1);
1991 output_processing = PNG_CMAP_NONE;
1992
1993 /* If there is a tRNS chunk then this either selects a transparent
1994 * value or, if the output has no alpha, the background color.
1995 */
1996 if (png_ptr->num_trans > 0)
1997 {
1998 trans = png_ptr->trans_color.gray;
1999
2000 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
2001 back_alpha = output_encoding == E_LINEAR ? 65535 : 255;
2002 }
2003
2004 /* png_create_colormap_entry just takes an RGBA and writes the
2005 * corresponding color-map entry using the format from 'image',
2006 * including the required convertion to sRGB or linear as
2007 * appropriate. The input values are always either sRGB (if the
2008 * gamma correction flag is 0) or 0..255 scaled file encoded values
2009 * (if the function must gamma correct them).
2010 */
2011 for (i=val=0; i<cmap_entries; ++i, val += step)
2012 {
2013 /* 'i' is a file value. While this will result in duplicated
2014 * entries for 8-bit non-sRGB encoded files it is necessary to
2015 * have non-gamma corrected values to do tRNS handling.
2016 */
2017 if (i != trans)
2018 png_create_colormap_entry(display, i, val, val, val, 255,
2019 E_FILE/*8-bit with file gamma*/);
2020
2021 /* Else this entry is transparent. The colors don't matter if
2022 * there is an alpha channel (back_alpha == 0), but it does no
2023 * harm to pass them in; the values are not set above so this
2024 * passes in white.
2025 *
2026 * NOTE: this preserves the full precision of the application
2027 * supplied background color when it is used.
2028 */
2029 else
2030 png_create_colormap_entry(display, i, back_r, back_g, back_b,
2031 back_alpha, output_encoding);
2032 }
2033
2034 /* We need libpng to preserve the original encoding. */
2035 data_encoding = E_FILE;
2036
2037 /* The rows from libpng, while technically gray values, are now also
2038 * color-map indicies; however, they may need to be expanded to 1
2039 * byte per pixel. This is what png_set_packing does (i.e., it
2040 * unpacks the bit values into bytes.)
2041 */
2042 if (png_ptr->bit_depth < 8)
2043 png_set_packing(png_ptr);
2044 }
2045
2046 else /* bit depth is 16 */
2047 {
2048 /* The 16-bit input values can be converted directly to 8-bit gamma
2049 * encoded values; however, if a tRNS chunk is present 257 color-map
2050 * entries are required. This means that the extra entry requires
2051 * special processing; add an alpha channel, sacrifice gray level
2052 * 254 and convert transparent (alpha==0) entries to that.
2053 *
2054 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2055 * same time to minimize quality loss. If a tRNS chunk is present
2056 * this means libpng must handle it too; otherwise it is impossible
2057 * to do the exact match on the 16-bit value.
2058 *
2059 * If the output has no alpha channel *and* the background color is
2060 * gray then it is possible to let libpng handle the substitution by
2061 * ensuring that the corresponding gray level matches the background
2062 * color exactly.
2063 */
2064 data_encoding = E_sRGB;
2065
2066 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2067 png_error(png_ptr, "gray[16] color-map: too few entries");
2068
2069 cmap_entries = make_gray_colormap(display);
2070
2071 if (png_ptr->num_trans > 0)
2072 {
2073 unsigned int back_alpha;
2074
2075 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2076 back_alpha = 0;
2077
2078 else
2079 {
2080 if (back_r == back_g && back_g == back_b)
2081 {
2082 /* Background is gray; no special processing will be
2083 * required.
2084 */
2085 png_color_16 c;
2086 png_uint_32 gray = back_g;
2087
2088 if (output_encoding == E_LINEAR)
2089 {
2090 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2091
2092 /* And make sure the corresponding palette entry
2093 * matches.
2094 */
2095 png_create_colormap_entry(display, gray, back_g, back_g,
2096 back_g, 65535, E_LINEAR);
2097 }
2098
2099 /* The background passed to libpng, however, must be the
2100 * sRGB value.
2101 */
2102 c.index = 0; /*unused*/
2103 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2104
2105 /* NOTE: does this work without expanding tRNS to alpha?
2106 * It should be the color->gray case below apparently
2107 * doesn't.
2108 */
2109 png_set_background_fixed(png_ptr, &c,
2110 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2111 0/*gamma: not used*/);
2112
2113 output_processing = PNG_CMAP_NONE;
2114 break;
2115 }
2116
2117 back_alpha = output_encoding == E_LINEAR ? 65535 : 255;
2118 }
2119
2120 /* output_processing means that the libpng-processed row will be
2121 * 8-bit GA and it has to be processing to single byte color-map
2122 * values. Entry 254 is replaced by either a completely
2123 * transparent entry or by the background color at full
2124 * precision (and the background color is not a simple gray leve
2125 * in this case.)
2126 */
2127 expand_tRNS = 1;
2128 output_processing = PNG_CMAP_TRANS;
2129 background_index = 254;
2130
2131 /* And set (overwrite) color-map entry 254 to the actual
2132 * background color at full precision.
2133 */
2134 png_create_colormap_entry(display, 254, back_r, back_g, back_b,
2135 back_alpha, output_encoding);
2136 }
2137
2138 else
2139 output_processing = PNG_CMAP_NONE;
2140 }
2141 break;
2142
2143 case PNG_COLOR_TYPE_GRAY_ALPHA:
2144 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2145 * of 65536 combinations. If, however, the alpha channel is to be
2146 * removed there are only 256 possibilities if the background is gray.
2147 * (Otherwise there is a subset of the 65536 possibilities defined by
2148 * the triangle between black, white and the background color.)
2149 *
2150 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2151 * worry about tRNS matching - tRNS is ignored if there is an alpha
2152 * channel.
2153 */
2154 data_encoding = E_sRGB;
2155
2156 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2157 {
2158 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2159 png_error(png_ptr, "gray+alpha color-map: too few entries");
2160
2161 cmap_entries = make_ga_colormap(display);
2162
2163 background_index = PNG_CMAP_GA_BACKGROUND;
2164 output_processing = PNG_CMAP_GA;
2165 }
2166
2167 else /* alpha is removed */
2168 {
2169 /* Alpha must be removed as the PNG data is processed when the
2170 * background is a color because the G and A channels are
2171 * independent and the vector addition (non-parallel vectors) is a
2172 * 2-D problem.
2173 *
2174 * This can be reduced to the same algorithm as above by making a
2175 * colormap containing gray levels (for the opaque grays), a
2176 * background entry (for a transparent pixel) and a set of four six
2177 * level color values, one set for each intermediate alpha value.
2178 * See the comments in make_ga_colormap for how this works in the
2179 * per-pixel processing.
2180 *
2181 * If the background is gray, however, we only need a 256 entry gray
2182 * level color map. It is sufficient to make the entry generated
2183 * for the background color be exactly the color specified.
2184 */
2185 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2186 (back_r == back_g && back_g == back_b))
2187 {
2188 /* Background is gray; no special processing will be required. */
2189 png_color_16 c;
2190 png_uint_32 gray = back_g;
2191
2192 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2193 png_error(png_ptr, "gray-alpha color-map: too few entries");
2194
2195 cmap_entries = make_gray_colormap(display);
2196
2197 if (output_encoding == E_LINEAR)
2198 {
2199 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2200
2201 /* And make sure the corresponding palette entry matches. */
2202 png_create_colormap_entry(display, gray, back_g, back_g,
2203 back_g, 65535, E_LINEAR);
2204 }
2205
2206 /* The background passed to libpng, however, must be the sRGB
2207 * value.
2208 */
2209 c.index = 0; /*unused*/
2210 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2211
2212 png_set_background_fixed(png_ptr, &c,
2213 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2214 0/*gamma: not used*/);
2215
2216 output_processing = PNG_CMAP_NONE;
2217 }
2218
2219 else
2220 {
2221 png_uint_32 i, a;
2222
2223 /* This is the same as png_make_ga_colormap, above, except that
2224 * the entries are all opaque.
2225 */
2226 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2227 png_error(png_ptr, "ga-alpha color-map: too few entries");
2228
2229 i = 0;
2230 while (i < 231)
2231 {
2232 png_uint_32 gray = (i * 256 + 115) / 231;
2233 png_create_colormap_entry(display, i++, gray, gray, gray,
2234 255, E_sRGB);
2235 }
2236
2237 /* NOTE: this preserves the full precision of the application
2238 * background color.
2239 */
2240 background_index = i;
2241 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
2242 output_encoding == E_LINEAR ? 65535U : 255U, output_encoding);
2243
2244 /* For non-opaque input composite on the sRGB background - this
2245 * requires inverting the encoding for each component. The input
2246 * is still converted to the sRGB encoding because this is a
2247 * reasonable approximate to the logarithmic curve of human
2248 * visual sensitivity, at least over the narrow range which PNG
2249 * represents. Consequently 'G' is always sRGB encoded, while
2250 * 'A' is linear. We need the linear background colors.
2251 */
2252 if (output_encoding == E_sRGB) /* else already linear */
2253 {
2254 /* This may produce a value not exactly matching the
2255 * background, but that's ok because these numbers are only
2256 * used when alpha != 0
2257 */
2258 back_r = png_sRGB_table[back_r];
2259 back_g = png_sRGB_table[back_g];
2260 back_b = png_sRGB_table[back_b];
2261 }
2262
2263 for (a=1; a<5; ++a)
2264 {
2265 unsigned int g;
2266
2267 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2268 * by an 8-bit alpha value (0..255).
2269 */
2270 png_uint_32 alpha = 51 * a;
2271 png_uint_32 back_rx = (255-alpha) * back_r;
2272 png_uint_32 back_gx = (255-alpha) * back_g;
2273 png_uint_32 back_bx = (255-alpha) * back_b;
2274
2275 for (g=0; g<6; ++g)
2276 {
2277 png_uint_32 gray = png_sRGB_table[g*51] * alpha;
2278
2279 png_create_colormap_entry(display, i++,
2280 PNG_sRGB_FROM_LINEAR(gray + back_rx),
2281 PNG_sRGB_FROM_LINEAR(gray + back_gx),
2282 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, E_sRGB);
2283 }
2284 }
2285
2286 cmap_entries = i;
2287 output_processing = PNG_CMAP_GA;
2288 }
2289 }
2290 break;
2291
2292 case PNG_COLOR_TYPE_RGB:
2293 case PNG_COLOR_TYPE_RGB_ALPHA:
2294 /* Exclude the case where the output is gray; we can always handle this
2295 * with the cases above.
2296 */
2297 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
2298 {
2299 /* The color-map will be grayscale, so we may as well convert the
2300 * input RGB values to a simple grayscale and use the grayscale
2301 * code above.
2302 *
2303 * NOTE: calling this apparently damages the recognition of the
2304 * transparent color in background color handling; call
2305 * png_set_tRNS_to_alpha before png_set_background_fixed.
2306 */
2307 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2308 -1);
2309 data_encoding = E_sRGB;
2310
2311 /* The output will now be one or two 8-bit gray or gray+alpha
2312 * channels. The more complex case arises when the input has alpha.
2313 */
2314 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2315 png_ptr->num_trans > 0) &&
2316 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2317 {
2318 /* Both input and output have an alpha channel, so no background
2319 * processing is required; just map the GA bytes to the right
2320 * color-map entry.
2321 */
2322 expand_tRNS = 1;
2323
2324 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2325 png_error(png_ptr, "rgb[ga] color-map: too few entries");
2326
2327 cmap_entries = make_ga_colormap(display);
2328 background_index = PNG_CMAP_GA_BACKGROUND;
2329 output_processing = PNG_CMAP_GA;
2330 }
2331
2332 else
2333 {
2334 /* Either the input or the output has no alpha channel, so there
2335 * will be no non-opaque pixels in the color-map; it will just be
2336 * grayscale.
2337 */
2338 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2339 png_error(png_ptr, "rgb[gray] color-map: too few entries");
2340
2341 cmap_entries = make_gray_colormap(display);
2342
2343 /* But if the input has alpha or transparency it must be removed
2344 */
2345 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2346 png_ptr->num_trans > 0)
2347 {
2348 png_color_16 c;
2349 png_uint_32 gray = back_g;
2350
2351 /* We need to ensure that the application background exists in
2352 * the colormap and that completely transparent pixels map to
2353 * it. Achieve this simply by ensuring that the entry
2354 * selected for the background really is the background color.
2355 */
2356 if (output_encoding == E_LINEAR)
2357 {
2358 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2359
2360 /* And make sure the corresponding palette entry matches.
2361 */
2362 png_create_colormap_entry(display, gray, back_g, back_g,
2363 back_g, 65535, E_LINEAR);
2364 }
2365
2366 /* The background passed to libpng, however, must be the sRGB
2367 * value.
2368 */
2369 c.index = 0; /*unused*/
2370 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2371
2372 /* NOTE: the following is apparently a bug in libpng. Without
2373 * it the transparent color recognition in
2374 * png_set_background_fixed seems to go wrong.
2375 */
2376 expand_tRNS = 1;
2377 png_set_background_fixed(png_ptr, &c,
2378 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2379 0/*gamma: not used*/);
2380 }
2381
2382 output_processing = PNG_CMAP_NONE;
2383 }
2384 }
2385
2386 else /* output is color */
2387 {
2388 /* We could use png_quantize here so long as there is no transparent
2389 * color or alpha; png_quantize ignores alpha. Easier overall just
2390 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2391 * Consequently we always want libpng to produce sRGB data.
2392 */
2393 data_encoding = E_sRGB;
2394
2395 /* Is there any transparency or alpha? */
2396 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2397 png_ptr->num_trans > 0)
2398 {
2399 /* Is there alpha in the output too? If so all four channels are
2400 * processed into a special RGB cube with alpha support.
2401 */
2402 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2403 {
2404 png_uint_32 r;
2405
2406 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2407 png_error(png_ptr, "rgb+alpha color-map: too few entries");
2408
2409 cmap_entries = make_rgb_colormap(display);
2410
2411 /* Add a transparent entry. */
2412 png_create_colormap_entry(display, cmap_entries, 255, 255,
2413 255, 0, E_sRGB);
2414
2415 /* This is stored as the background index for the processing
2416 * algorithm.
2417 */
2418 background_index = cmap_entries++;
2419
2420 /* Add 27 r,g,b entries each with alpha 0.5. */
2421 for (r=0; r<256; r = (r << 1) | 0x7f)
2422 {
2423 png_uint_32 g;
2424
2425 for (g=0; g<256; g = (g << 1) | 0x7f)
2426 {
2427 png_uint_32 b;
2428
2429 /* This generates components with the values 0, 127 and
2430 * 255
2431 */
2432 for (b=0; b<256; b = (b << 1) | 0x7f)
2433 png_create_colormap_entry(display, cmap_entries++,
2434 r, g, b, 128, E_sRGB);
2435 }
2436 }
2437
2438 expand_tRNS = 1;
2439 output_processing = PNG_CMAP_RGB_ALPHA;
2440 }
2441
2442 else
2443 {
2444 /* Alpha/transparency must be removed. The background must
2445 * exist in the color map (achieved by setting adding it after
2446 * the 666 color-map). If the standard processing code will
2447 * pick up this entry automatically that's all that is
2448 * required; libpng can be called to do the background
2449 * processing.
2450 */
2451 unsigned int sample_size =
2452 PNG_IMAGE_SAMPLE_SIZE(output_format);
2453 png_uint_32 r, g, b; /* sRGB background */
2454
2455 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2456 png_error(png_ptr, "rgb-alpha color-map: too few entries");
2457
2458 cmap_entries = make_rgb_colormap(display);
2459
2460 png_create_colormap_entry(display, cmap_entries, back_r,
2461 back_g, back_b, 0/*unused*/, output_encoding);
2462
2463 if (output_encoding == E_LINEAR)
2464 {
2465 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2466 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2467 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2468 }
2469
2470 else
2471 {
2472 r = back_r;
2473 g = back_g;
2474 b = back_g;
2475 }
2476
2477 /* Compare the newly-created color-map entry with the one the
2478 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2479 * match, add the new one and set this as the background index.
2480 */
2481 if (memcmp((png_const_bytep)display->colormap +
2482 sample_size * cmap_entries,
2483 (png_const_bytep)display->colormap +
2484 sample_size * PNG_RGB_INDEX(r,g,b),
2485 sample_size) != 0)
2486 {
2487 /* The background color must be added. */
2488 background_index = cmap_entries++;
2489
2490 /* Add 27 r,g,b entries each with created by composing with
2491 * the background at alpha 0.5.
2492 */
2493 for (r=0; r<256; r = (r << 1) | 0x7f)
2494 {
2495 for (g=0; g<256; g = (g << 1) | 0x7f)
2496 {
2497 /* This generates components with the values 0, 127
2498 * and 255
2499 */
2500 for (b=0; b<256; b = (b << 1) | 0x7f)
2501 png_create_colormap_entry(display, cmap_entries++,
2502 png_colormap_compose(display, r, E_sRGB, 128,
2503 back_r, output_encoding),
2504 png_colormap_compose(display, g, E_sRGB, 128,
2505 back_g, output_encoding),
2506 png_colormap_compose(display, b, E_sRGB, 128,
2507 back_b, output_encoding),
2508 0/*unused*/, output_encoding);
2509 }
2510 }
2511
2512 expand_tRNS = 1;
2513 output_processing = PNG_CMAP_RGB_ALPHA;
2514 }
2515
2516 else /* background color is in the standard color-map */
2517 {
2518 png_color_16 c;
2519
2520 c.index = 0; /*unused*/
2521 c.red = (png_uint_16)back_r;
2522 c.gray = c.green = (png_uint_16)back_g;
2523 c.blue = (png_uint_16)back_b;
2524
2525 png_set_background_fixed(png_ptr, &c,
2526 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2527 0/*gamma: not used*/);
2528
2529 output_processing = PNG_CMAP_RGB;
2530 }
2531 }
2532 }
2533
2534 else /* no alpha or transparency in the input */
2535 {
2536 /* Alpha in the output is irrelevant, simply map the opaque input
2537 * pixels to the 6x6x6 color-map.
2538 */
2539 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2540 png_error(png_ptr, "rgb color-map: too few entries");
2541
2542 cmap_entries = make_rgb_colormap(display);
2543 output_processing = PNG_CMAP_RGB;
2544 }
2545 }
2546 break;
2547
2548 case PNG_COLOR_TYPE_PALETTE:
2549 /* It's already got a color-map. It may be necessary to eliminate the
2550 * tRNS entries though.
2551 */
2552 {
2553 unsigned int num_trans = png_ptr->num_trans;
2554 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2555 png_const_colorp colormap = png_ptr->palette;
2556 const int do_background = trans != NULL &&
2557 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2558 unsigned int i;
2559
2560 /* Just in case: */
2561 if (trans == NULL)
2562 num_trans = 0;
2563
2564 output_processing = PNG_CMAP_NONE;
2565 data_encoding = E_FILE; /* Don't change from color-map indicies */
2566 cmap_entries = png_ptr->num_palette;
2567 if (cmap_entries > 256)
2568 cmap_entries = 256;
2569
2570 if (cmap_entries > image->colormap_entries)
2571 png_error(png_ptr, "palette color-map: too few entries");
2572
2573 for (i=0; i < cmap_entries; ++i)
2574 {
2575 if (do_background && i < num_trans && trans[i] < 255)
2576 {
2577 if (trans[i] == 0)
2578 png_create_colormap_entry(display, i, back_r, back_g,
2579 back_b, 0, output_encoding);
2580
2581 else
2582 {
2583 /* Must compose the PNG file color in the color-map entry
2584 * on the sRGB color in 'back'.
2585 */
2586 png_create_colormap_entry(display, i,
2587 png_colormap_compose(display, colormap[i].red, E_FILE,
2588 trans[i], back_r, output_encoding),
2589 png_colormap_compose(display, colormap[i].green, E_FILE,
2590 trans[i], back_g, output_encoding),
2591 png_colormap_compose(display, colormap[i].blue, E_FILE,
2592 trans[i], back_b, output_encoding),
2593 output_encoding == E_LINEAR ? trans[i] * 257U :
2594 trans[i],
2595 output_encoding);
2596 }
2597 }
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06002598
John Bowler5bc90382012-01-23 22:43:22 -06002599 else
2600 png_create_colormap_entry(display, i, colormap[i].red,
2601 colormap[i].green, colormap[i].blue,
2602 i < num_trans ? trans[i] : 255U, E_FILE/*8-bit*/);
2603 }
2604
2605 /* The PNG data may have indicies packed in fewer than 8 bits, it
2606 * must be expanded if so.
2607 */
2608 if (png_ptr->bit_depth < 8)
2609 png_set_packing(png_ptr);
2610 }
2611 break;
2612
2613 default:
2614 png_error(png_ptr, "invalid PNG color type");
2615 /*NOT REACHED*/
2616 break;
2617 }
2618
2619 /* Now deal with the output processing */
2620 if (expand_tRNS && png_ptr->num_trans > 0 &&
2621 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
2622 png_set_tRNS_to_alpha(png_ptr);
2623
2624 switch (data_encoding)
2625 {
2626 default:
2627 png_error(png_ptr, "bad data option (internal error)");
2628 break;
2629
2630 case E_FILE:
2631 /* Make no changes */
2632 break;
2633
2634 case E_sRGB:
2635 /* Change to 8-bit sRGB */
2636 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
2637 if (png_ptr->bit_depth > 8)
2638 png_set_scale_16(png_ptr);
2639 break;
2640 }
2641
2642 if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2643 png_error(png_ptr, "color map overflow (BAD internal error)");
2644
2645 image->colormap_entries = cmap_entries;
2646
2647 /* Double check using the recorded background index */
2648 switch (output_processing)
2649 {
2650 case PNG_CMAP_NONE:
2651 if (background_index != PNG_CMAP_NONE_BACKGROUND)
2652 goto bad_background;
2653 break;
2654
2655 case PNG_CMAP_GA:
2656 if (background_index != PNG_CMAP_GA_BACKGROUND)
2657 goto bad_background;
2658 break;
2659
2660 case PNG_CMAP_TRANS:
2661 if (background_index >= cmap_entries ||
2662 background_index != PNG_CMAP_TRANS_BACKGROUND)
2663 goto bad_background;
2664 break;
2665
2666 case PNG_CMAP_RGB:
2667 if (background_index != PNG_CMAP_RGB_BACKGROUND)
2668 goto bad_background;
2669 break;
2670
2671 case PNG_CMAP_RGB_ALPHA:
2672 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
2673 goto bad_background;
2674 break;
2675
2676 default:
2677 png_error(png_ptr, "bad processing option (internal error)");
2678
2679 bad_background:
2680 png_error(png_ptr, "bad background index (internal error)");
2681 }
2682
2683 display->colormap_processing = output_processing;
2684
2685 return 1/*ok*/;
2686}
2687
2688/* The final part of the color-map read called from png_image_finish_read. */
2689static int
2690png_image_read_and_map(png_voidp argument)
John Bowler7875d532011-11-07 22:33:49 -06002691{
John Bowler4fa96a42011-11-16 16:39:16 -06002692 png_image_read_control *display = png_voidcast(png_image_read_control*,
2693 argument);
John Bowler7875d532011-11-07 22:33:49 -06002694 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06002695 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler4f67e402011-12-28 08:43:37 -06002696 int passes;
John Bowler7875d532011-11-07 22:33:49 -06002697
John Bowler5bc90382012-01-23 22:43:22 -06002698 /* Called when the libpng data must be transformed into the color-mapped
2699 * form. There is a local row buffer in display->local and this routine must
2700 * do the interlace handling.
2701 */
2702 switch (png_ptr->interlaced)
John Bowler7875d532011-11-07 22:33:49 -06002703 {
2704 case PNG_INTERLACE_NONE:
2705 passes = 1;
2706 break;
2707
2708 case PNG_INTERLACE_ADAM7:
2709 passes = PNG_INTERLACE_ADAM7_PASSES;
2710 break;
2711
2712 default:
John Bowler4f67e402011-12-28 08:43:37 -06002713 passes = 0;
John Bowler7875d532011-11-07 22:33:49 -06002714 png_error(png_ptr, "unknown interlace type");
2715 }
2716
2717 {
John Bowler5bc90382012-01-23 22:43:22 -06002718 png_uint_32 height = image->height;
2719 png_uint_32 width = image->width;
2720 int proc = display->colormap_processing;
2721 png_bytep first_row = display->first_row;
2722 ptrdiff_t step_row = display->row_bytes;
John Bowler7875d532011-11-07 22:33:49 -06002723 int pass;
2724
2725 for (pass = 0; pass < passes; ++pass)
2726 {
John Bowler7875d532011-11-07 22:33:49 -06002727 unsigned int startx, stepx, stepy;
2728 png_uint_32 y;
2729
John Bowler5bc90382012-01-23 22:43:22 -06002730 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
John Bowler7875d532011-11-07 22:33:49 -06002731 {
2732 /* The row may be empty for a short image: */
2733 if (PNG_PASS_COLS(width, pass) == 0)
2734 continue;
2735
2736 startx = PNG_PASS_START_COL(pass);
2737 stepx = PNG_PASS_COL_OFFSET(pass);
2738 y = PNG_PASS_START_ROW(pass);
John Bowler18c5cfa2011-11-16 14:26:34 -06002739 stepy = PNG_PASS_ROW_OFFSET(pass);
John Bowler7875d532011-11-07 22:33:49 -06002740 }
2741
2742 else
2743 {
2744 y = 0;
2745 startx = 0;
2746 stepx = stepy = 1;
2747 }
John Bowler18c5cfa2011-11-16 14:26:34 -06002748
John Bowler7875d532011-11-07 22:33:49 -06002749 for (; y<height; y += stepy)
John Bowler5bc90382012-01-23 22:43:22 -06002750 {
2751 png_bytep inrow = display->local_row;
2752 png_bytep outrow = first_row + y * step_row;
2753 png_const_bytep end_row = outrow + width;
2754
2755 /* Read read the libpng data into the temporary buffer. */
2756 png_read_row(png_ptr, inrow, NULL);
2757
2758 /* Now process the row according to the processing option, note
2759 * that the caller verifies that the format of the libpng output
2760 * data is as required.
2761 */
2762 outrow += startx;
2763 switch (proc)
John Bowler7875d532011-11-07 22:33:49 -06002764 {
John Bowler5bc90382012-01-23 22:43:22 -06002765 case PNG_CMAP_GA:
2766 for (; outrow < end_row; outrow += stepx)
John Bowler7875d532011-11-07 22:33:49 -06002767 {
John Bowler5bc90382012-01-23 22:43:22 -06002768 /* The data is always in the PNG order */
2769 unsigned int gray = *inrow++;
2770 unsigned int alpha = *inrow++;
2771 unsigned int entry;
John Bowler7875d532011-11-07 22:33:49 -06002772
John Bowler5bc90382012-01-23 22:43:22 -06002773 /* NOTE: this code is copied as a comment in
2774 * make_ga_colormap above. Please update the
2775 * comment if you change this code!
2776 */
2777 if (alpha > 229) /* opaque */
John Bowler7875d532011-11-07 22:33:49 -06002778 {
John Bowler5bc90382012-01-23 22:43:22 -06002779 entry = (231 * gray + 128) >> 8;
John Bowler7875d532011-11-07 22:33:49 -06002780 }
John Bowler5bc90382012-01-23 22:43:22 -06002781 else if (alpha < 26) /* transparent */
2782 {
2783 entry = 231;
2784 }
2785 else /* partially opaque */
2786 {
2787 entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
2788 }
John Bowler7875d532011-11-07 22:33:49 -06002789
John Bowler5bc90382012-01-23 22:43:22 -06002790 *outrow = (png_byte)entry;
2791 }
2792 break;
2793
2794 case PNG_CMAP_TRANS:
2795 for (; outrow < end_row; outrow += stepx)
2796 {
2797 png_byte gray = *inrow++;
2798 png_byte alpha = *inrow++;
2799
2800 if (alpha == 0)
2801 *outrow = PNG_CMAP_TRANS_BACKGROUND;
2802
2803 else if (gray != PNG_CMAP_TRANS_BACKGROUND)
2804 *outrow = gray;
2805
2806 else
2807 *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
2808 }
2809 break;
2810
2811 case PNG_CMAP_RGB:
2812 for (; outrow < end_row; outrow += stepx)
2813 {
2814 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
2815 inrow += 3;
2816 }
2817 break;
2818
2819 case PNG_CMAP_RGB_ALPHA:
2820 for (; outrow < end_row; outrow += stepx)
2821 {
2822 unsigned int alpha = inrow[3];
2823
2824 /* Because the alpha entries only hold alpha==0.5 values
2825 * split the processing at alpha==0.25 (64) and 0.75
2826 * (196).
2827 */
2828
2829 if (alpha >= 196)
2830 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
2831 inrow[2]);
2832
2833 else if (alpha < 64)
2834 *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
2835
2836 else
2837 {
2838 /* Likewise there are three entries for each of r, g
2839 * and b. We could select the entry by popcount on
2840 * the top two bits on those architectures that
2841 * support it, this is what the code below does,
2842 * crudely.
2843 */
2844 unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
2845
2846 /* Here are how the values map:
2847 *
2848 * 0x00 .. 0x3f -> 0
2849 * 0x40 .. 0xbf -> 1
2850 * 0xc0 .. 0xff -> 2
2851 *
2852 * So, as above with the explicit alpha checks, the
2853 * breakpoints are at 64 and 196.
2854 */
2855 if (inrow[0] & 0x80) back_i += 9; /* red */
2856 if (inrow[0] & 0x40) back_i += 9;
2857 if (inrow[0] & 0x80) back_i += 3; /* green */
2858 if (inrow[0] & 0x40) back_i += 3;
2859 if (inrow[0] & 0x80) back_i += 1; /* blue */
2860 if (inrow[0] & 0x40) back_i += 1;
2861
2862 *outrow = (png_byte)back_i;
2863 }
2864
2865 inrow += 4;
2866 }
2867 break;
2868
2869 default:
2870 break;
2871 }
2872 }
2873 }
2874 }
2875
2876 return 1;
2877}
2878
2879static int
2880png_image_read_colormapped(png_voidp argument)
2881{
2882 png_image_read_control *display = png_voidcast(png_image_read_control*,
2883 argument);
2884 png_imagep image = display->image;
2885 png_controlp control = image->opaque;
2886 png_structrp png_ptr = control->png_ptr;
2887 png_inforp info_ptr = control->info_ptr;
2888
2889 int passes = 0; /* As a flag */
2890
2891 PNG_SKIP_CHUNKS(png_ptr);
2892
2893 /* Update the 'info' structure and make sure the result is as required; first
2894 * make sure to turn on the interlace handling if it will be required
2895 * (because it can't be turned on *after* the call to png_read_update_info!)
2896 */
2897 if (display->colormap_processing == PNG_CMAP_NONE)
2898 passes = png_set_interlace_handling(png_ptr);
2899
2900 png_read_update_info(png_ptr, info_ptr);
2901
2902 /* The expected output can be deduced from the colormap_processing option. */
2903 switch (display->colormap_processing)
2904 {
2905 case PNG_CMAP_NONE:
2906 /* Output must be one channel and one byte per pixel, the output
2907 * encoding can be anything.
2908 */
2909 if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
2910 info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
2911 info_ptr->bit_depth == 8)
2912 break;
2913
2914 goto bad_output;
2915
2916 case PNG_CMAP_TRANS:
2917 case PNG_CMAP_GA:
2918 /* Output must be two channels and the 'G' one must be sRGB, the latter
2919 * can be checked with an exact number because it should have been set
2920 * to this number above!
2921 */
2922 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
2923 info_ptr->bit_depth == 8 &&
2924 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
2925 image->colormap_entries == 256)
2926 break;
2927
2928 goto bad_output;
2929
2930 case PNG_CMAP_RGB:
2931 /* Output must be 8-bit sRGB encoded RGB */
2932 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
2933 info_ptr->bit_depth == 8 &&
2934 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
2935 image->colormap_entries == 216)
2936 break;
2937
2938 goto bad_output;
2939
2940 case PNG_CMAP_RGB_ALPHA:
2941 /* Output must be 8-bit sRGB encoded RGBA */
2942 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
2943 info_ptr->bit_depth == 8 &&
2944 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
2945 image->colormap_entries == 244 /* 216 + 1 + 27 */)
2946 break;
2947
2948 /* goto bad_output; */
2949 /* FALL THROUGH */
2950
2951 default:
2952 bad_output:
2953 png_error(png_ptr, "bad color-map processing (internal error)");
2954 }
2955
2956 /* Now read the rows. Do this here if it is possible to read directly into
2957 * the output buffer, otherwise allocate a local row buffer of the maximum
2958 * size libpng requires and call the relevant processing routine safely.
2959 */
2960 {
2961 png_bytep first_row = png_voidcast(png_bytep, display->buffer);
2962 ptrdiff_t row_bytes = display->row_stride;
2963
2964 /* The following expression is designed to work correctly whether it gives
2965 * a signed or an unsigned result.
2966 */
2967 if (row_bytes < 0)
2968 first_row += (image->height-1) * (-row_bytes);
2969
2970 display->first_row = first_row;
2971 display->row_bytes = row_bytes;
2972 }
2973
2974 if (passes == 0)
2975 {
2976 int result;
2977 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
2978 png_get_rowbytes(png_ptr, info_ptr)));
2979
2980 display->local_row = row;
2981 result = png_safe_execute(image, png_image_read_and_map, display);
2982 display->local_row = NULL;
2983 png_free(png_ptr, row);
2984
2985 return result;
2986 }
2987
2988 else
2989 {
2990 png_alloc_size_t row_bytes = display->row_bytes;
2991
2992 while (--passes >= 0)
2993 {
2994 png_uint_32 y = image->height;
2995 png_bytep row = display->first_row;
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06002996
John Bowler5bc90382012-01-23 22:43:22 -06002997 while (y-- > 0)
2998 {
2999 png_read_row(png_ptr, row, NULL);
3000 row += row_bytes;
3001 }
3002 }
3003
3004 return 1;
3005 }
3006}
3007
3008/* Just the row reading part of png_image_read. */
3009static int
3010png_image_read_composite(png_voidp argument)
3011{
3012 png_image_read_control *display = png_voidcast(png_image_read_control*,
3013 argument);
3014 png_imagep image = display->image;
3015 png_structrp png_ptr = image->opaque->png_ptr;
3016 int passes;
3017
3018 switch (png_ptr->interlaced)
3019 {
3020 case PNG_INTERLACE_NONE:
3021 passes = 1;
3022 break;
3023
3024 case PNG_INTERLACE_ADAM7:
3025 passes = PNG_INTERLACE_ADAM7_PASSES;
3026 break;
3027
3028 default:
3029 passes = 0;
3030 png_error(png_ptr, "unknown interlace type");
3031 }
3032
3033 {
3034 png_uint_32 height = image->height;
3035 png_uint_32 width = image->width;
3036 png_bytep first_row = display->first_row;
3037 ptrdiff_t step_row = display->row_bytes;
3038 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
3039 int pass;
3040
3041 for (pass = 0; pass < passes; ++pass)
3042 {
3043 unsigned int startx, stepx, stepy;
3044 png_uint_32 y;
3045
3046 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3047 {
3048 /* The row may be empty for a short image: */
3049 if (PNG_PASS_COLS(width, pass) == 0)
3050 continue;
3051
3052 startx = PNG_PASS_START_COL(pass) * channels;
3053 stepx = PNG_PASS_COL_OFFSET(pass) * channels;
3054 y = PNG_PASS_START_ROW(pass);
3055 stepy = PNG_PASS_ROW_OFFSET(pass);
3056 }
3057
3058 else
3059 {
3060 y = 0;
3061 startx = 0;
3062 stepx = channels;
3063 stepy = 1;
3064 }
3065
3066 for (; y<height; y += stepy)
3067 {
3068 png_bytep inrow = display->local_row;
3069 png_bytep outrow = first_row + y * step_row;
3070 png_const_bytep end_row = outrow + width * channels;
3071
3072 /* Read the row, which is packed: */
3073 png_read_row(png_ptr, inrow, NULL);
3074
3075 /* Now do the composition on each pixel in this row. */
3076 outrow += startx;
3077 for (; outrow < end_row; outrow += stepx)
3078 {
3079 png_byte alpha = inrow[channels];
3080
3081 if (alpha > 0) /* else no change to the output */
3082 {
3083 unsigned int c;
3084
3085 for (c=0; c<channels; ++c)
3086 {
3087 png_uint_32 component = inrow[c];
3088
3089 if (alpha < 255) /* else just use component */
3090 {
3091 /* This is PNG_OPTIMIZED_ALPHA, the component value
3092 * is a linear 8-bit value. Combine this with the
3093 * current outrow[c] value which is sRGB encoded.
3094 * Arithmetic here is 16-bits to preserve the output
3095 * values correctly.
3096 */
3097 component *= 257*255; /* =65535 */
3098 component += (255-alpha)*png_sRGB_table[outrow[c]];
3099
3100 /* So 'component' is scaled by 255*65535 and is
3101 * therefore appropriate for the sRGB to linear
3102 * conversion table.
3103 */
3104 component = PNG_sRGB_FROM_LINEAR(component);
3105 }
3106
3107 outrow[c] = (png_byte)component;
3108 }
John Bowler7875d532011-11-07 22:33:49 -06003109 }
3110
John Bowler5bc90382012-01-23 22:43:22 -06003111 inrow += channels+1; /* components and alpha channel */
John Bowler7875d532011-11-07 22:33:49 -06003112 }
John Bowler5bc90382012-01-23 22:43:22 -06003113 }
John Bowler7875d532011-11-07 22:33:49 -06003114 }
3115 }
3116
3117 return 1;
3118}
3119
John Bowler18c5cfa2011-11-16 14:26:34 -06003120/* The do_local_background case; called when all the following transforms are to
3121 * be done:
3122 *
3123 * PNG_RGB_TO_GRAY
3124 * PNG_COMPOSITE
3125 * PNG_GAMMA
3126 *
3127 * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and
3128 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
3129 * correction. The fix-up is to prevent the PNG_COMPOSITE operation happening
3130 * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles
3131 * the removal or pre-multiplication of the alpha channel.
3132 */
3133static int
3134png_image_read_background(png_voidp argument)
3135{
John Bowler4fa96a42011-11-16 16:39:16 -06003136 png_image_read_control *display = png_voidcast(png_image_read_control*,
3137 argument);
John Bowler18c5cfa2011-11-16 14:26:34 -06003138 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06003139 png_structrp png_ptr = image->opaque->png_ptr;
3140 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler18c5cfa2011-11-16 14:26:34 -06003141 png_uint_32 height = image->height;
3142 png_uint_32 width = image->width;
John Bowler4f67e402011-12-28 08:43:37 -06003143 int pass, passes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003144
3145 /* Double check the convoluted logic below. We expect to get here with
3146 * libpng doing rgb to gray and gamma correction but background processing
3147 * left to the png_image_read_background function. The rows libpng produce
3148 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
3149 */
3150 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
3151 png_error(png_ptr, "lost rgb to gray");
3152
3153 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
3154 png_error(png_ptr, "unexpected compose");
3155
John Bowler18c5cfa2011-11-16 14:26:34 -06003156 if (png_get_channels(png_ptr, info_ptr) != 2)
3157 png_error(png_ptr, "lost/gained channels");
3158
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003159 /* Expect the 8-bit case to always remove the alpha channel */
3160 if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
3161 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
3162 png_error(png_ptr, "unexpected 8-bit transformation");
3163
John Bowler5bc90382012-01-23 22:43:22 -06003164 switch (png_ptr->interlaced)
John Bowler18c5cfa2011-11-16 14:26:34 -06003165 {
3166 case PNG_INTERLACE_NONE:
3167 passes = 1;
3168 break;
3169
3170 case PNG_INTERLACE_ADAM7:
3171 passes = PNG_INTERLACE_ADAM7_PASSES;
3172 break;
3173
3174 default:
John Bowler4f67e402011-12-28 08:43:37 -06003175 passes = 0;
John Bowler18c5cfa2011-11-16 14:26:34 -06003176 png_error(png_ptr, "unknown interlace type");
3177 }
3178
3179 switch (png_get_bit_depth(png_ptr, info_ptr))
3180 {
3181 default:
3182 png_error(png_ptr, "unexpected bit depth");
3183 break;
3184
3185 case 8:
3186 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
3187 * to be removed by composing on a backgroundi: either the row if
John Bowler5bc90382012-01-23 22:43:22 -06003188 * display->background is NULL or display->background->green if not.
John Bowler18c5cfa2011-11-16 14:26:34 -06003189 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3190 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003191 {
John Bowler5bc90382012-01-23 22:43:22 -06003192 png_bytep first_row = display->first_row;
3193 ptrdiff_t step_row = display->row_bytes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003194
John Bowler5bc90382012-01-23 22:43:22 -06003195 for (pass = 0; pass < passes; ++pass)
John Bowler18c5cfa2011-11-16 14:26:34 -06003196 {
John Bowler5bc90382012-01-23 22:43:22 -06003197 png_bytep row = display->first_row;
3198 unsigned int startx, stepx, stepy;
3199 png_uint_32 y;
John Bowler18c5cfa2011-11-16 14:26:34 -06003200
John Bowler5bc90382012-01-23 22:43:22 -06003201 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3202 {
3203 /* The row may be empty for a short image: */
3204 if (PNG_PASS_COLS(width, pass) == 0)
3205 continue;
John Bowler18c5cfa2011-11-16 14:26:34 -06003206
John Bowler5bc90382012-01-23 22:43:22 -06003207 startx = PNG_PASS_START_COL(pass);
3208 stepx = PNG_PASS_COL_OFFSET(pass);
3209 y = PNG_PASS_START_ROW(pass);
3210 stepy = PNG_PASS_ROW_OFFSET(pass);
3211 }
3212
3213 else
3214 {
3215 y = 0;
3216 startx = 0;
3217 stepx = stepy = 1;
3218 }
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003219
John Bowler5bc90382012-01-23 22:43:22 -06003220 if (display->background == NULL)
3221 {
3222 for (; y<height; y += stepy)
John Bowler18c5cfa2011-11-16 14:26:34 -06003223 {
3224 png_bytep inrow = display->local_row;
John Bowler5bc90382012-01-23 22:43:22 -06003225 png_bytep outrow = first_row + y * step_row;
3226 png_const_bytep end_row = outrow + width;
John Bowler18c5cfa2011-11-16 14:26:34 -06003227
3228 /* Read the row, which is packed: */
3229 png_read_row(png_ptr, inrow, NULL);
3230
3231 /* Now do the composition on each pixel in this row. */
John Bowler5bc90382012-01-23 22:43:22 -06003232 outrow += startx;
John Bowler18c5cfa2011-11-16 14:26:34 -06003233 for (; outrow < end_row; outrow += stepx)
3234 {
3235 png_byte alpha = inrow[1];
3236
3237 if (alpha > 0) /* else no change to the output */
3238 {
3239 png_uint_32 component = inrow[0];
3240
3241 if (alpha < 255) /* else just use component */
3242 {
3243 /* Since PNG_OPTIMIZED_ALPHA was not set it is
3244 * necessary to invert the sRGB transfer
3245 * function and multiply the alpha out.
3246 */
3247 component = png_sRGB_table[component] * alpha;
3248 component += png_sRGB_table[outrow[0]] *
3249 (255-alpha);
3250 component = PNG_sRGB_FROM_LINEAR(component);
3251 }
3252
3253 outrow[0] = (png_byte)component;
3254 }
3255
3256 inrow += 2; /* gray and alpha channel */
3257 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003258 }
John Bowler5bc90382012-01-23 22:43:22 -06003259 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003260
John Bowler5bc90382012-01-23 22:43:22 -06003261 else /* constant background value */
3262 {
3263 png_byte background8 = display->background->green;
3264 png_uint_16 background = png_sRGB_table[background8];
John Bowler18c5cfa2011-11-16 14:26:34 -06003265
John Bowler5bc90382012-01-23 22:43:22 -06003266 for (; y<height; y += stepy)
John Bowler18c5cfa2011-11-16 14:26:34 -06003267 {
3268 png_bytep inrow = display->local_row;
John Bowler5bc90382012-01-23 22:43:22 -06003269 png_bytep outrow = first_row + y * step_row;
3270 png_const_bytep end_row = outrow + width;
John Bowler18c5cfa2011-11-16 14:26:34 -06003271
3272 /* Read the row, which is packed: */
3273 png_read_row(png_ptr, inrow, NULL);
3274
3275 /* Now do the composition on each pixel in this row. */
John Bowler5bc90382012-01-23 22:43:22 -06003276 outrow += startx;
John Bowler18c5cfa2011-11-16 14:26:34 -06003277 for (; outrow < end_row; outrow += stepx)
3278 {
3279 png_byte alpha = inrow[1];
3280
3281 if (alpha > 0) /* else use background */
3282 {
3283 png_uint_32 component = inrow[0];
3284
3285 if (alpha < 255) /* else just use component */
3286 {
3287 component = png_sRGB_table[component] * alpha;
3288 component += background * (255-alpha);
3289 component = PNG_sRGB_FROM_LINEAR(component);
3290 }
3291
3292 outrow[0] = (png_byte)component;
3293 }
3294
3295 else
3296 outrow[0] = background8;
3297
3298 inrow += 2; /* gray and alpha channel */
3299 }
3300
3301 row += display->row_bytes;
3302 }
John Bowler5bc90382012-01-23 22:43:22 -06003303 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003304 }
3305 }
3306 break;
3307
3308 case 16:
3309 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3310 * still be done and, maybe, the alpha channel removed. This code also
3311 * handles the alpha-first option.
3312 */
3313 {
John Bowler5bc90382012-01-23 22:43:22 -06003314 png_bytep first_row = display->first_row;
3315 ptrdiff_t step_row = display->row_bytes;
John Bowler18c5cfa2011-11-16 14:26:34 -06003316 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003317 unsigned int outchannels = 1+preserve_alpha;
John Bowler18c5cfa2011-11-16 14:26:34 -06003318 int swap_alpha = 0;
3319
3320 if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST))
3321 swap_alpha = 1;
3322
3323 for (pass = 0; pass < passes; ++pass)
3324 {
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003325 unsigned int startx, stepx, stepy;
John Bowler18c5cfa2011-11-16 14:26:34 -06003326 png_uint_32 y;
3327
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003328 /* The 'x' start and step are adjusted to output components here.
3329 */
John Bowler5bc90382012-01-23 22:43:22 -06003330 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
John Bowler18c5cfa2011-11-16 14:26:34 -06003331 {
3332 /* The row may be empty for a short image: */
3333 if (PNG_PASS_COLS(width, pass) == 0)
3334 continue;
3335
John Bowler5bc90382012-01-23 22:43:22 -06003336 startx = PNG_PASS_START_COL(pass) * outchannels;
3337 stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
John Bowler18c5cfa2011-11-16 14:26:34 -06003338 y = PNG_PASS_START_ROW(pass);
3339 stepy = PNG_PASS_ROW_OFFSET(pass);
3340 }
3341
3342 else
3343 {
3344 y = 0;
3345 startx = 0;
John Bowler5bc90382012-01-23 22:43:22 -06003346 stepx = outchannels;
3347 stepy = 1;
John Bowler18c5cfa2011-11-16 14:26:34 -06003348 }
3349
John Bowler18c5cfa2011-11-16 14:26:34 -06003350 for (; y<height; y += stepy)
John Bowler5bc90382012-01-23 22:43:22 -06003351 {
3352 png_const_uint_16p inrow;
3353 png_uint_16p outrow = (png_uint_16p)(first_row + y*step_row);
3354 png_uint_16p end_row = outrow + width * outchannels;
3355
3356 /* Read the row, which is packed: */
3357 png_read_row(png_ptr, display->local_row, NULL);
3358 inrow = (png_const_uint_16p)display->local_row;
3359
3360 /* Now do the pre-multiplication on each pixel in this row.
3361 */
3362 outrow += startx;
3363 for (; outrow < end_row; outrow += stepx)
John Bowler18c5cfa2011-11-16 14:26:34 -06003364 {
John Bowler5bc90382012-01-23 22:43:22 -06003365 png_uint_32 component = inrow[0];
3366 png_uint_16 alpha = inrow[1];
John Bowler18c5cfa2011-11-16 14:26:34 -06003367
John Bowler5bc90382012-01-23 22:43:22 -06003368 if (alpha > 0) /* else 0 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003369 {
John Bowler5bc90382012-01-23 22:43:22 -06003370 if (alpha < 65535) /* else just use component */
John Bowler18c5cfa2011-11-16 14:26:34 -06003371 {
John Bowler5bc90382012-01-23 22:43:22 -06003372 component *= alpha;
3373 component += 32767;
3374 component /= 65535;
John Bowler18c5cfa2011-11-16 14:26:34 -06003375 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003376 }
3377
John Bowler5bc90382012-01-23 22:43:22 -06003378 else
3379 component = 0;
3380
3381 outrow[swap_alpha] = (png_uint_16)component;
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003382 if (preserve_alpha)
John Bowler5bc90382012-01-23 22:43:22 -06003383 outrow[1 ^ swap_alpha] = alpha;
3384
3385 inrow += 2; /* components and alpha channel */
John Bowler18c5cfa2011-11-16 14:26:34 -06003386 }
John Bowler5bc90382012-01-23 22:43:22 -06003387 }
John Bowler18c5cfa2011-11-16 14:26:34 -06003388 }
3389 }
3390 break;
3391 }
3392
3393 return 1;
3394}
3395
John Bowler7875d532011-11-07 22:33:49 -06003396/* The guts of png_image_finish_read as a png_safe_execute callback. */
3397static int
John Bowler5bc90382012-01-23 22:43:22 -06003398png_image_read_direct(png_voidp argument)
John Bowler7875d532011-11-07 22:33:49 -06003399{
John Bowler4fa96a42011-11-16 16:39:16 -06003400 png_image_read_control *display = png_voidcast(png_image_read_control*,
3401 argument);
John Bowler7875d532011-11-07 22:33:49 -06003402 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06003403 png_structrp png_ptr = image->opaque->png_ptr;
3404 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06003405
3406 png_uint_32 format = image->format;
3407 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
3408 int do_local_compose = 0;
John Bowler18c5cfa2011-11-16 14:26:34 -06003409 int do_local_background = 0; /* to avoid double gamma correction bug */
John Bowler7875d532011-11-07 22:33:49 -06003410 int passes = 0;
3411
3412 /* Add transforms to ensure the correct output format is produced then check
3413 * that the required implementation support is there. Always expand; always
3414 * need 8 bits minimum, no palette and expanded tRNS.
3415 */
3416 png_set_expand(png_ptr);
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003417
John Bowler7875d532011-11-07 22:33:49 -06003418 /* Now check the format to see if it was modified. */
3419 {
John Bowler5bc90382012-01-23 22:43:22 -06003420 png_uint_32 base_format = png_image_format(png_ptr) &
3421 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
John Bowler7875d532011-11-07 22:33:49 -06003422 png_uint_32 change = format ^ base_format;
John Bowler3615d032011-11-08 10:38:09 -06003423 png_fixed_point output_gamma;
3424 int mode; /* alpha mode */
John Bowler7875d532011-11-07 22:33:49 -06003425
John Bowler18c5cfa2011-11-16 14:26:34 -06003426 /* Do this first so that we have a record if rgb to gray is happening. */
3427 if (change & PNG_FORMAT_FLAG_COLOR)
3428 {
3429 /* gray<->color transformation required. */
3430 if (format & PNG_FORMAT_FLAG_COLOR)
3431 png_set_gray_to_rgb(png_ptr);
3432
3433 else
3434 {
3435 /* libpng can't do both rgb to gray and
3436 * background/pre-multiplication if there is also significant gamma
3437 * correction, because both operations require linear colors and
3438 * the code only supports one transform doing the gamma correction.
3439 * Handle this by doing the pre-multiplication or background
3440 * operation in this code, if necessary.
3441 *
3442 * TODO: fix this by rewriting pngrtran.c (!)
3443 *
3444 * For the moment (given that fixing this in pngrtran.c is an
3445 * enormous change) 'do_local_background' is used to indicate that
3446 * the problem exists.
3447 */
3448 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3449 do_local_background = 1/*maybe*/;
3450
3451 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
3452 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
3453 }
3454
3455 change &= ~PNG_FORMAT_FLAG_COLOR;
3456 }
3457
John Bowler7875d532011-11-07 22:33:49 -06003458 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3459 */
3460 {
John Bowler3615d032011-11-08 10:38:09 -06003461 png_fixed_point input_gamma_default;
John Bowler7875d532011-11-07 22:33:49 -06003462
3463 if (base_format & PNG_FORMAT_FLAG_LINEAR)
3464 input_gamma_default = PNG_GAMMA_LINEAR;
3465 else
3466 input_gamma_default = PNG_DEFAULT_sRGB;
3467
John Bowler18c5cfa2011-11-16 14:26:34 -06003468 /* Call png_set_alpha_mode to set the default for the input gamma; the
3469 * output gamma is set by a second call below.
3470 */
3471 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
3472 }
3473
3474 if (linear)
3475 {
3476 /* If there *is* an alpha channel in the input it must be multiplied
3477 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3478 */
3479 if (base_format & PNG_FORMAT_FLAG_ALPHA)
John Bowler3615d032011-11-08 10:38:09 -06003480 mode = PNG_ALPHA_STANDARD; /* associated alpha */
John Bowler7875d532011-11-07 22:33:49 -06003481
3482 else
John Bowler7875d532011-11-07 22:33:49 -06003483 mode = PNG_ALPHA_PNG;
John Bowler18c5cfa2011-11-16 14:26:34 -06003484
3485 output_gamma = PNG_GAMMA_LINEAR;
3486 }
3487
3488 else
3489 {
3490 mode = PNG_ALPHA_PNG;
3491 output_gamma = PNG_DEFAULT_sRGB;
3492 }
3493
3494 /* If 'do_local_background' is set check for the presence of gamma
3495 * correction; this is part of the work-round for the libpng bug
3496 * described above.
3497 *
3498 * TODO: fix libpng and remove this.
3499 */
3500 if (do_local_background)
3501 {
3502 png_fixed_point gtest;
3503
3504 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3505 * gamma correction, the screen gamma hasn't been set on png_struct
3506 * yet; it's set below. png_struct::gamma, however, is set to the
3507 * final value.
3508 */
3509 if (png_muldiv(&gtest, output_gamma, png_ptr->gamma, PNG_FP_1) &&
3510 !png_gamma_significant(gtest))
3511 do_local_background = 0;
3512
3513 else if (mode == PNG_ALPHA_STANDARD)
3514 {
3515 do_local_background = 2/*required*/;
3516 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
John Bowler7875d532011-11-07 22:33:49 -06003517 }
3518
John Bowler18c5cfa2011-11-16 14:26:34 -06003519 /* else leave as 1 for the checks below */
John Bowler3615d032011-11-08 10:38:09 -06003520 }
John Bowler7875d532011-11-07 22:33:49 -06003521
John Bowler3615d032011-11-08 10:38:09 -06003522 /* If the bit-depth changes then handle that here. */
3523 if (change & PNG_FORMAT_FLAG_LINEAR)
3524 {
3525 if (linear /*16-bit output*/)
3526 png_set_expand_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06003527
John Bowler3615d032011-11-08 10:38:09 -06003528 else /* 8-bit output */
3529 png_set_scale_16(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06003530
John Bowler3615d032011-11-08 10:38:09 -06003531 change &= ~PNG_FORMAT_FLAG_LINEAR;
John Bowler7875d532011-11-07 22:33:49 -06003532 }
3533
3534 /* Now the background/alpha channel changes. */
3535 if (change & PNG_FORMAT_FLAG_ALPHA)
3536 {
3537 /* Removing an alpha channel requires composition for the 8-bit
3538 * formats; for the 16-bit it is already done, above, by the
3539 * pre-multiplication and the channel just needs to be stripped.
3540 */
3541 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3542 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003543 /* If RGB->gray is happening the alpha channel must be left and the
3544 * operation completed locally.
3545 *
3546 * TODO: fix libpng and remove this.
3547 */
3548 if (do_local_background)
3549 do_local_background = 2/*required*/;
3550
John Bowler3615d032011-11-08 10:38:09 -06003551 /* 16-bit output: just remove the channel */
John Bowler18c5cfa2011-11-16 14:26:34 -06003552 else if (linear) /* compose on black (well, pre-multiply) */
John Bowler7875d532011-11-07 22:33:49 -06003553 png_set_strip_alpha(png_ptr);
3554
John Bowler3615d032011-11-08 10:38:09 -06003555 /* 8-bit output: do an appropriate compose */
John Bowler7875d532011-11-07 22:33:49 -06003556 else if (display->background != NULL)
3557 {
3558 png_color_16 c;
3559
3560 c.index = 0; /*unused*/
3561 c.red = display->background->red;
3562 c.green = display->background->green;
3563 c.blue = display->background->blue;
3564 c.gray = display->background->green;
3565
3566 /* This is always an 8-bit sRGB value, using the 'green' channel
3567 * for gray is much better than calculating the luminance here;
3568 * we can get off-by-one errors in that calculation relative to
3569 * the app expectations and that will show up in transparent
3570 * pixels.
3571 */
3572 png_set_background_fixed(png_ptr, &c,
3573 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
3574 0/*gamma: not used*/);
3575 }
3576
3577 else /* compose on row: implemented below. */
3578 {
3579 do_local_compose = 1;
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003580 /* This leaves the alpha channel in the output, so it has to be
John Bowler7875d532011-11-07 22:33:49 -06003581 * removed by the code below. Set the encoding to the 'OPTIMIZE'
3582 * one so the code only has to hack on the pixels that require
3583 * composition.
3584 */
John Bowler3615d032011-11-08 10:38:09 -06003585 mode = PNG_ALPHA_OPTIMIZED;
John Bowler7875d532011-11-07 22:33:49 -06003586 }
3587 }
3588
3589 else /* output needs an alpha channel */
3590 {
3591 /* This is tricky because it happens before the swap operation has
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003592 * been accomplished; however, the swap does *not* swap the added
John Bowlere6fb6912011-11-08 21:35:16 -06003593 * alpha channel (weird API), so it must be added in the correct
3594 * place.
John Bowler7875d532011-11-07 22:33:49 -06003595 */
John Bowler3615d032011-11-08 10:38:09 -06003596 png_uint_32 filler; /* opaque filler */
John Bowlere6fb6912011-11-08 21:35:16 -06003597 int where;
John Bowler3615d032011-11-08 10:38:09 -06003598
3599 if (linear)
3600 filler = 65535;
3601
3602 else
3603 filler = 255;
3604
John Bowlere6fb6912011-11-08 21:35:16 -06003605# ifdef PNG_FORMAT_AFIRST_SUPPORTED
3606 if (format & PNG_FORMAT_FLAG_AFIRST)
3607 {
3608 where = PNG_FILLER_BEFORE;
3609 change &= ~PNG_FORMAT_FLAG_AFIRST;
3610 }
3611
3612 else
3613# endif
3614 where = PNG_FILLER_AFTER;
3615
3616 png_set_add_alpha(png_ptr, filler, where);
John Bowler7875d532011-11-07 22:33:49 -06003617 }
3618
John Bowlere6fb6912011-11-08 21:35:16 -06003619 /* This stops the (irrelevant) call to swap_alpha below. */
John Bowler7875d532011-11-07 22:33:49 -06003620 change &= ~PNG_FORMAT_FLAG_ALPHA;
3621 }
3622
John Bowler3615d032011-11-08 10:38:09 -06003623 /* Now set the alpha mode correctly; this is always done, even if there is
3624 * no alpha channel in either the input or the output because it correctly
3625 * sets the output gamma.
3626 */
3627 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
3628
John Bowler7875d532011-11-07 22:33:49 -06003629# ifdef PNG_FORMAT_BGR_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06003630 if (change & PNG_FORMAT_FLAG_BGR)
John Bowler7875d532011-11-07 22:33:49 -06003631 {
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003632 /* Check only the output format; PNG is never BGR; don't do this if
John Bowler7875d532011-11-07 22:33:49 -06003633 * the output is gray, but fix up the 'format' value in that case.
3634 */
3635 if (format & PNG_FORMAT_FLAG_COLOR)
3636 png_set_bgr(png_ptr);
3637
3638 else
3639 format &= ~PNG_FORMAT_FLAG_BGR;
3640
3641 change &= ~PNG_FORMAT_FLAG_BGR;
3642 }
3643# endif
3644
3645# ifdef PNG_FORMAT_AFIRST_SUPPORTED
John Bowlere6fb6912011-11-08 21:35:16 -06003646 if (change & PNG_FORMAT_FLAG_AFIRST)
John Bowler7875d532011-11-07 22:33:49 -06003647 {
3648 /* Only relevant if there is an alpha channel - it's particularly
3649 * important to handle this correctly because do_local_compose may
3650 * be set above and then libpng will keep the alpha channel for this
3651 * code to remove.
3652 */
3653 if (format & PNG_FORMAT_FLAG_ALPHA)
John Bowler18c5cfa2011-11-16 14:26:34 -06003654 {
3655 /* Disable this if doing a local background,
3656 * TODO: remove this when local background is no longer required.
3657 */
3658 if (do_local_background != 2)
3659 png_set_swap_alpha(png_ptr);
3660 }
John Bowler7875d532011-11-07 22:33:49 -06003661
3662 else
3663 format &= ~PNG_FORMAT_FLAG_AFIRST;
3664
3665 change &= ~PNG_FORMAT_FLAG_AFIRST;
3666 }
3667# endif
3668
3669 /* If the *output* is 16-bit then we need to check for a byte-swap on this
3670 * architecture.
3671 */
3672 if (linear)
3673 {
3674 PNG_CONST png_uint_16 le = 0x0001;
3675
3676 if (*(png_const_bytep)&le)
3677 png_set_swap(png_ptr);
3678 }
3679
3680 /* If change is not now 0 some transformation is missing - error out. */
3681 if (change)
3682 png_error(png_ptr, "png_read_image: unsupported transformation");
3683 }
3684
John Bowler5bc90382012-01-23 22:43:22 -06003685 PNG_SKIP_CHUNKS(png_ptr);
John Bowler89c2f842011-11-16 12:04:39 -06003686
Glenn Randers-Pehrson84b0da92011-11-10 06:32:19 -06003687 /* Update the 'info' structure and make sure the result is as required; first
John Bowler7875d532011-11-07 22:33:49 -06003688 * make sure to turn on the interlace handling if it will be required
3689 * (because it can't be turned on *after* the call to png_read_update_info!)
John Bowler18c5cfa2011-11-16 14:26:34 -06003690 *
3691 * TODO: remove the do_local_background fixup below.
John Bowler7875d532011-11-07 22:33:49 -06003692 */
John Bowler18c5cfa2011-11-16 14:26:34 -06003693 if (!do_local_compose && do_local_background != 2)
John Bowler7875d532011-11-07 22:33:49 -06003694 passes = png_set_interlace_handling(png_ptr);
3695
3696 png_read_update_info(png_ptr, info_ptr);
3697
3698 {
3699 png_uint_32 info_format = 0;
3700
3701 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
3702 info_format |= PNG_FORMAT_FLAG_COLOR;
3703
3704 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
3705 {
John Bowler18c5cfa2011-11-16 14:26:34 -06003706 /* do_local_compose removes this channel below. */
John Bowler7875d532011-11-07 22:33:49 -06003707 if (!do_local_compose)
John Bowler18c5cfa2011-11-16 14:26:34 -06003708 {
3709 /* do_local_background does the same if required. */
3710 if (do_local_background != 2 ||
3711 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
3712 info_format |= PNG_FORMAT_FLAG_ALPHA;
3713 }
John Bowler7875d532011-11-07 22:33:49 -06003714 }
3715
3716 else if (do_local_compose) /* internal error */
3717 png_error(png_ptr, "png_image_read: alpha channel lost");
3718
3719 if (info_ptr->bit_depth == 16)
3720 info_format |= PNG_FORMAT_FLAG_LINEAR;
3721
3722# ifdef PNG_FORMAT_BGR_SUPPORTED
3723 if (png_ptr->transformations & PNG_BGR)
3724 info_format |= PNG_FORMAT_FLAG_BGR;
3725# endif
3726
3727# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003728 if (do_local_background == 2)
3729 {
3730 if (format & PNG_FORMAT_FLAG_AFIRST)
3731 info_format |= PNG_FORMAT_FLAG_AFIRST;
3732 }
3733
3734 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
John Bowlere6fb6912011-11-08 21:35:16 -06003735 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
3736 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003737 {
3738 if (do_local_background == 2)
3739 png_error(png_ptr, "unexpected alpha swap transformation");
3740
John Bowler7875d532011-11-07 22:33:49 -06003741 info_format |= PNG_FORMAT_FLAG_AFIRST;
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003742 }
John Bowler7875d532011-11-07 22:33:49 -06003743# endif
3744
3745 /* This is actually an internal error. */
3746 if (info_format != format)
3747 png_error(png_ptr, "png_read_image: invalid transformations");
3748 }
3749
3750 /* Now read the rows. If do_local_compose is set then it is necessary to use
3751 * a local row buffer. The output will be GA, RGBA or BGRA and must be
3752 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
3753 * display acts as a flag.
3754 */
3755 {
John Bowler4fa96a42011-11-16 16:39:16 -06003756 png_bytep first_row = png_voidcast(png_bytep, display->buffer);
John Bowler7875d532011-11-07 22:33:49 -06003757 ptrdiff_t row_bytes = display->row_stride;
3758
3759 if (linear)
John Bowler5bc90382012-01-23 22:43:22 -06003760 row_bytes *= 2;
John Bowler7875d532011-11-07 22:33:49 -06003761
3762 /* The following expression is designed to work correctly whether it gives
3763 * a signed or an unsigned result.
3764 */
3765 if (row_bytes < 0)
3766 first_row += (image->height-1) * (-row_bytes);
3767
3768 display->first_row = first_row;
3769 display->row_bytes = row_bytes;
3770 }
3771
3772 if (do_local_compose)
3773 {
3774 int result;
John Bowler4fa96a42011-11-16 16:39:16 -06003775 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
3776 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler7875d532011-11-07 22:33:49 -06003777
3778 display->local_row = row;
3779 result = png_safe_execute(image, png_image_read_composite, display);
3780 display->local_row = NULL;
3781 png_free(png_ptr, row);
3782
3783 return result;
3784 }
3785
John Bowler18c5cfa2011-11-16 14:26:34 -06003786 else if (do_local_background == 2)
3787 {
3788 int result;
John Bowler4fa96a42011-11-16 16:39:16 -06003789 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
3790 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler18c5cfa2011-11-16 14:26:34 -06003791
3792 display->local_row = row;
3793 result = png_safe_execute(image, png_image_read_background, display);
3794 display->local_row = NULL;
3795 png_free(png_ptr, row);
3796
3797 return result;
3798 }
3799
John Bowler7875d532011-11-07 22:33:49 -06003800 else
3801 {
3802 png_alloc_size_t row_bytes = display->row_bytes;
3803
3804 while (--passes >= 0)
3805 {
3806 png_uint_32 y = image->height;
3807 png_bytep row = display->first_row;
Glenn Randers-Pehrson864270e2012-02-10 17:13:13 -06003808
John Bowler7875d532011-11-07 22:33:49 -06003809 while (y-- > 0)
3810 {
3811 png_read_row(png_ptr, row, NULL);
3812 row += row_bytes;
3813 }
3814 }
3815
3816 return 1;
3817 }
3818}
3819
3820int PNGAPI
John Bowler40ca77a2012-01-28 23:19:42 -06003821png_image_finish_read(png_imagep image, png_const_colorp background,
3822 void *buffer, png_int_32 row_stride, void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06003823{
John Bowler5bc90382012-01-23 22:43:22 -06003824 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06003825 {
3826 png_uint_32 check;
3827
John Bowler3706d732011-11-21 10:28:06 -06003828 if (row_stride == 0)
3829 row_stride = PNG_IMAGE_ROW_STRIDE(*image);
3830
John Bowler7875d532011-11-07 22:33:49 -06003831 if (row_stride < 0)
3832 check = -row_stride;
3833
3834 else
3835 check = row_stride;
3836
John Bowler5bc90382012-01-23 22:43:22 -06003837 if (image->opaque != NULL && buffer != NULL &&
3838 check >= PNG_IMAGE_ROW_STRIDE(*image))
John Bowler7875d532011-11-07 22:33:49 -06003839 {
John Bowler5bc90382012-01-23 22:43:22 -06003840 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
3841 (image->colormap_entries > 0 && colormap != NULL))
3842 {
3843 int result;
3844 png_image_read_control display;
John Bowler7875d532011-11-07 22:33:49 -06003845
John Bowler5bc90382012-01-23 22:43:22 -06003846 png_memset(&display, 0, sizeof display);
3847 display.image = image;
3848 display.buffer = buffer;
3849 display.row_stride = row_stride;
3850 display.colormap = colormap;
3851 display.background = background;
3852 display.local_row = NULL;
3853
3854 /* Choose the correct 'end' routine; for the color-map case all the
3855 * setup has already been done.
3856 */
3857 if (image->format & PNG_FORMAT_FLAG_COLORMAP)
3858 result =
3859 png_safe_execute(image, png_image_read_colormap, &display) &&
3860 png_safe_execute(image, png_image_read_colormapped, &display);
3861
3862 else
3863 result =
3864 png_safe_execute(image, png_image_read_direct, &display);
3865
3866 png_image_free(image);
3867 return result;
3868 }
3869
3870 else
3871 return png_image_error(image,
3872 "png_image_finish_read[color-map]: no color-map");
John Bowler7875d532011-11-07 22:33:49 -06003873 }
3874
3875 else
3876 return png_image_error(image,
3877 "png_image_finish_read: invalid argument");
3878 }
3879
3880 return 0;
3881}
3882
3883#endif /* PNG_SIMPLIFIED_READ_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06003884#endif /* PNG_READ_SUPPORTED */