blob: 769960850952c10ea56417b1c9803b04c65a83bd [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* pngread.c - read a PNG file
3 *
Matt Sarett9ea75692016-01-08 13:00:42 -05004 * Last changed in libpng 1.6.17 [March 26, 2015]
5 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
The Android Open Source Project893912b2009-03-03 19:30:05 -08006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8 *
Patrick Scotta0bb96c2009-07-22 11:50:02 -04009 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
12 *
The Android Open Source Project893912b2009-03-03 19:30:05 -080013 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
15 */
16
Chris Craikb50c2172013-07-29 15:28:30 -070017#include "pngpriv.h"
18#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
19# include <errno.h>
20#endif
21
Patrick Scott5f6bd842010-06-28 16:55:16 -040022#ifdef PNG_READ_SUPPORTED
23
The Android Open Source Project893912b2009-03-03 19:30:05 -080024/* Create a PNG structure for reading, and allocate any memory needed. */
Chris Craikb50c2172013-07-29 15:28:30 -070025PNG_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)
The Android Open Source Project893912b2009-03-03 19:30:05 -080028{
Chris Craikb50c2172013-07-29 15:28:30 -070029#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);
The Android Open Source Project893912b2009-03-03 19:30:05 -080035}
36
Patrick Scott5f6bd842010-06-28 16:55:16 -040037/* Alternate create PNG structure for reading, and allocate any memory
38 * needed.
39 */
Chris Craikb50c2172013-07-29 15:28:30 -070040PNG_FUNCTION(png_structp,PNGAPI
41png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
42 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
43 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
The Android Open Source Project893912b2009-03-03 19:30:05 -080044{
Chris Craikb50c2172013-07-29 15:28:30 -070045 png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
46 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
Matt Sarett9ea75692016-01-08 13:00:42 -050047#endif /* USER_MEM */
The Android Open Source Project893912b2009-03-03 19:30:05 -080048
Chris Craikb50c2172013-07-29 15:28:30 -070049 if (png_ptr != NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -080050 {
Chris Craikb50c2172013-07-29 15:28:30 -070051 png_ptr->mode = PNG_IS_READ_STRUCT;
The Android Open Source Project893912b2009-03-03 19:30:05 -080052
Chris Craikb50c2172013-07-29 15:28:30 -070053 /* Added in libpng-1.6.0; this can be used to detect a read structure if
54 * required (it will be zero in a write structure.)
Patrick Scott5f6bd842010-06-28 16:55:16 -040055 */
Chris Craikb50c2172013-07-29 15:28:30 -070056# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
57 png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE;
58# endif
59
60# ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
61 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
62
63 /* In stable builds only warn if an application error can be completely
64 * handled.
65 */
Matt Sarett9ea75692016-01-08 13:00:42 -050066# if PNG_RELEASE_BUILD
Chris Craikb50c2172013-07-29 15:28:30 -070067 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
68# endif
69# endif
70
71 /* TODO: delay this, it can be done in png_init_io (if the app doesn't
72 * do it itself) avoiding setting the default function if it is not
73 * required.
74 */
75 png_set_read_fn(png_ptr, NULL, NULL);
Sireesh Tripurarib0277d02014-05-09 15:39:12 +053076
77#ifdef PNG_INDEX_SUPPORTED
78 png_ptr->index = NULL;
79#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -080080 }
81
Chris Craikb50c2172013-07-29 15:28:30 -070082 return png_ptr;
The Android Open Source Project893912b2009-03-03 19:30:05 -080083}
84
The Android Open Source Project893912b2009-03-03 19:30:05 -080085
Patrick Scott5f6bd842010-06-28 16:55:16 -040086#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -080087/* Read the information before the actual image data. This has been
88 * changed in v0.90 to allow reading a file that already has the magic
89 * bytes read from the stream. You can tell libpng how many bytes have
90 * been read from the beginning of the stream (up to the maximum of 8)
91 * via png_set_sig_bytes(), and we will only check the remaining bytes
92 * here. The application can then have access to the signature bytes we
93 * read if it is determined that this isn't a valid PNG file.
94 */
95void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -070096png_read_info(png_structrp png_ptr, png_inforp info_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -080097{
Chris Craikb50c2172013-07-29 15:28:30 -070098#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
99 int keep;
100#endif
101
Patrick Scott5f6bd842010-06-28 16:55:16 -0400102 png_debug(1, "in png_read_info");
Chris Craikb50c2172013-07-29 15:28:30 -0700103
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400104 if (png_ptr == NULL || info_ptr == NULL)
105 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800106
Chris Craikb50c2172013-07-29 15:28:30 -0700107 /* Read and check the PNG file signature. */
108 png_read_sig(png_ptr, info_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800109
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700110 for (;;)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800111 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700112 png_uint_32 length = png_read_chunk_header(png_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -0700113 png_uint_32 chunk_name = png_ptr->chunk_name;
114
115 /* IDAT logic needs to happen here to simplify getting the two flags
116 * right.
117 */
118 if (chunk_name == png_IDAT)
119 {
Matt Sarett9ea75692016-01-08 13:00:42 -0500120 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700121 png_chunk_error(png_ptr, "Missing IHDR before IDAT");
122
123 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Matt Sarett9ea75692016-01-08 13:00:42 -0500124 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700125 png_chunk_error(png_ptr, "Missing PLTE before IDAT");
126
Matt Sarett9ea75692016-01-08 13:00:42 -0500127 else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700128 png_chunk_benign_error(png_ptr, "Too many IDATs found");
129
130 png_ptr->mode |= PNG_HAVE_IDAT;
131 }
132
Matt Sarett9ea75692016-01-08 13:00:42 -0500133 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700134 png_ptr->mode |= PNG_AFTER_IDAT;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800135
136 /* This should be a binary subdivision search or a hash for
137 * matching the chunk name rather than a linear search.
138 */
Chris Craikb50c2172013-07-29 15:28:30 -0700139 if (chunk_name == png_IHDR)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800140 png_handle_IHDR(png_ptr, info_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700141
142 else if (chunk_name == png_IEND)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800143 png_handle_IEND(png_ptr, info_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700144
The Android Open Source Project893912b2009-03-03 19:30:05 -0800145#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700146 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800147 {
Chris Craikb50c2172013-07-29 15:28:30 -0700148 png_handle_unknown(png_ptr, info_ptr, length, keep);
149
150 if (chunk_name == png_PLTE)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800151 png_ptr->mode |= PNG_HAVE_PLTE;
Chris Craikb50c2172013-07-29 15:28:30 -0700152
153 else if (chunk_name == png_IDAT)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800154 {
Chris Craikb50c2172013-07-29 15:28:30 -0700155 png_ptr->idat_size = 0; /* It has been consumed */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800156 break;
157 }
158 }
159#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700160 else if (chunk_name == png_PLTE)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800161 png_handle_PLTE(png_ptr, info_ptr, length);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800162
Chris Craikb50c2172013-07-29 15:28:30 -0700163 else if (chunk_name == png_IDAT)
164 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800165 png_ptr->idat_size = length;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800166 break;
167 }
Chris Craikb50c2172013-07-29 15:28:30 -0700168
Patrick Scott5f6bd842010-06-28 16:55:16 -0400169#ifdef PNG_READ_bKGD_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700170 else if (chunk_name == png_bKGD)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800171 png_handle_bKGD(png_ptr, info_ptr, length);
172#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700173
Patrick Scott5f6bd842010-06-28 16:55:16 -0400174#ifdef PNG_READ_cHRM_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700175 else if (chunk_name == png_cHRM)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800176 png_handle_cHRM(png_ptr, info_ptr, length);
177#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700178
Patrick Scott5f6bd842010-06-28 16:55:16 -0400179#ifdef PNG_READ_gAMA_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700180 else if (chunk_name == png_gAMA)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800181 png_handle_gAMA(png_ptr, info_ptr, length);
182#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700183
Patrick Scott5f6bd842010-06-28 16:55:16 -0400184#ifdef PNG_READ_hIST_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700185 else if (chunk_name == png_hIST)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800186 png_handle_hIST(png_ptr, info_ptr, length);
187#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700188
Patrick Scott5f6bd842010-06-28 16:55:16 -0400189#ifdef PNG_READ_oFFs_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700190 else if (chunk_name == png_oFFs)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800191 png_handle_oFFs(png_ptr, info_ptr, length);
192#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700193
Patrick Scott5f6bd842010-06-28 16:55:16 -0400194#ifdef PNG_READ_pCAL_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700195 else if (chunk_name == png_pCAL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800196 png_handle_pCAL(png_ptr, info_ptr, length);
197#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700198
Patrick Scott5f6bd842010-06-28 16:55:16 -0400199#ifdef PNG_READ_sCAL_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700200 else if (chunk_name == png_sCAL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800201 png_handle_sCAL(png_ptr, info_ptr, length);
202#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700203
Patrick Scott5f6bd842010-06-28 16:55:16 -0400204#ifdef PNG_READ_pHYs_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700205 else if (chunk_name == png_pHYs)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800206 png_handle_pHYs(png_ptr, info_ptr, length);
207#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700208
Patrick Scott5f6bd842010-06-28 16:55:16 -0400209#ifdef PNG_READ_sBIT_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700210 else if (chunk_name == png_sBIT)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800211 png_handle_sBIT(png_ptr, info_ptr, length);
212#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700213
Patrick Scott5f6bd842010-06-28 16:55:16 -0400214#ifdef PNG_READ_sRGB_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700215 else if (chunk_name == png_sRGB)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800216 png_handle_sRGB(png_ptr, info_ptr, length);
217#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700218
Patrick Scott5f6bd842010-06-28 16:55:16 -0400219#ifdef PNG_READ_iCCP_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700220 else if (chunk_name == png_iCCP)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800221 png_handle_iCCP(png_ptr, info_ptr, length);
222#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700223
Patrick Scott5f6bd842010-06-28 16:55:16 -0400224#ifdef PNG_READ_sPLT_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700225 else if (chunk_name == png_sPLT)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800226 png_handle_sPLT(png_ptr, info_ptr, length);
227#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700228
Patrick Scott5f6bd842010-06-28 16:55:16 -0400229#ifdef PNG_READ_tEXt_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700230 else if (chunk_name == png_tEXt)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800231 png_handle_tEXt(png_ptr, info_ptr, length);
232#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700233
Patrick Scott5f6bd842010-06-28 16:55:16 -0400234#ifdef PNG_READ_tIME_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700235 else if (chunk_name == png_tIME)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800236 png_handle_tIME(png_ptr, info_ptr, length);
237#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700238
Patrick Scott5f6bd842010-06-28 16:55:16 -0400239#ifdef PNG_READ_tRNS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700240 else if (chunk_name == png_tRNS)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800241 png_handle_tRNS(png_ptr, info_ptr, length);
242#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700243
Patrick Scott5f6bd842010-06-28 16:55:16 -0400244#ifdef PNG_READ_zTXt_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700245 else if (chunk_name == png_zTXt)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800246 png_handle_zTXt(png_ptr, info_ptr, length);
247#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700248
Patrick Scott5f6bd842010-06-28 16:55:16 -0400249#ifdef PNG_READ_iTXt_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700250 else if (chunk_name == png_iTXt)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800251 png_handle_iTXt(png_ptr, info_ptr, length);
252#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700253
The Android Open Source Project893912b2009-03-03 19:30:05 -0800254 else
Chris Craikb50c2172013-07-29 15:28:30 -0700255 png_handle_unknown(png_ptr, info_ptr, length,
256 PNG_HANDLE_CHUNK_AS_DEFAULT);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800257 }
258}
Matt Sarett9ea75692016-01-08 13:00:42 -0500259#endif /* SEQUENTIAL_READ */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800260
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400261/* Optional call to update the users info_ptr structure */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800262void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700263png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800264{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700265 png_debug(1, "in png_read_update_info");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400266
Chris Craikb50c2172013-07-29 15:28:30 -0700267 if (png_ptr != NULL)
268 {
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530269#ifdef PNG_INDEX_SUPPORTED
270 if (png_ptr->index) {
271 png_read_start_row(png_ptr);
272 }
273#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700274 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
275 {
276 png_read_start_row(png_ptr);
277
278# ifdef PNG_READ_TRANSFORMS_SUPPORTED
279 png_read_transform_info(png_ptr, info_ptr);
280# else
281 PNG_UNUSED(info_ptr)
282# endif
283 }
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530284#ifndef PNG_INDEX_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700285 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
286 else
287 png_app_error(png_ptr,
288 "png_read_update_info/png_start_read_image: duplicate call");
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530289#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700290 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800291}
292
Patrick Scott5f6bd842010-06-28 16:55:16 -0400293#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800294/* Initialize palette, background, etc, after transformations
295 * are set, but before any reading takes place. This allows
296 * the user to obtain a gamma-corrected palette, for example.
297 * If the user doesn't call this, we will do it ourselves.
298 */
299void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700300png_start_read_image(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800301{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700302 png_debug(1, "in png_start_read_image");
Chris Craikb50c2172013-07-29 15:28:30 -0700303
304 if (png_ptr != NULL)
305 {
306 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
307 png_read_start_row(png_ptr);
308
309 /* New in 1.6.0 this avoids the bug of doing the initializations twice */
310 else
311 png_app_error(png_ptr,
312 "png_start_read_image/png_read_update_info: duplicate call");
313 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800314}
Matt Sarett9ea75692016-01-08 13:00:42 -0500315#endif /* SEQUENTIAL_READ */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800316
Patrick Scott5f6bd842010-06-28 16:55:16 -0400317#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530318#ifdef PNG_MNG_FEATURES_SUPPORTED
319/* Undoes intrapixel differencing,
320 * NOTE: this is apparently only supported in the 'sequential' reader.
321 */
322static void
323png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
324{
325 png_debug(1, "in png_do_read_intrapixel");
326
327 if (
Matt Sarett9ea75692016-01-08 13:00:42 -0500328 (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530329 {
330 int bytes_per_pixel;
331 png_uint_32 row_width = row_info->width;
332
333 if (row_info->bit_depth == 8)
334 {
335 png_bytep rp;
336 png_uint_32 i;
337
338 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
339 bytes_per_pixel = 3;
340
341 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
342 bytes_per_pixel = 4;
343
344 else
345 return;
346
347 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
348 {
349 *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
350 *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
351 }
352 }
353 else if (row_info->bit_depth == 16)
354 {
355 png_bytep rp;
356 png_uint_32 i;
357
358 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
359 bytes_per_pixel = 6;
360
361 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
362 bytes_per_pixel = 8;
363
364 else
365 return;
366
367 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
368 {
369 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
370 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
371 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
372 png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
373 png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
374 *(rp ) = (png_byte)((red >> 8) & 0xff);
375 *(rp + 1) = (png_byte)(red & 0xff);
376 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
377 *(rp + 5) = (png_byte)(blue & 0xff);
378 }
379 }
380 }
381}
Matt Sarett9ea75692016-01-08 13:00:42 -0500382#endif /* MNG_FEATURES */
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530383
The Android Open Source Project893912b2009-03-03 19:30:05 -0800384void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700385png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800386{
Chris Craikb50c2172013-07-29 15:28:30 -0700387 png_row_info row_info;
388
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400389 if (png_ptr == NULL)
390 return;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400391
Chris Craikb50c2172013-07-29 15:28:30 -0700392 png_debug2(1, "in png_read_row (row %lu, pass %d)",
393 (unsigned long)png_ptr->row_number, png_ptr->pass);
394
395 /* png_read_start_row sets the information (in particular iwidth) for this
396 * interlace pass.
397 */
Matt Sarett9ea75692016-01-08 13:00:42 -0500398 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800399 png_read_start_row(png_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -0700400
401 /* 1.5.6: row_info moved out of png_struct to a local here. */
402 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
403 row_info.color_type = png_ptr->color_type;
404 row_info.bit_depth = png_ptr->bit_depth;
405 row_info.channels = png_ptr->channels;
406 row_info.pixel_depth = png_ptr->pixel_depth;
407 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
408
Matt Sarett9ea75692016-01-08 13:00:42 -0500409#ifdef PNG_WARNINGS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800410 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
411 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400412 /* Check for transforms that have been set but were defined out */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800413#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500414 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700415 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800416#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700417
The Android Open Source Project893912b2009-03-03 19:30:05 -0800418#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500419 if ((png_ptr->transformations & PNG_FILLER) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700420 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800421#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700422
Patrick Scott5f6bd842010-06-28 16:55:16 -0400423#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
424 !defined(PNG_READ_PACKSWAP_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500425 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700426 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800427#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700428
The Android Open Source Project893912b2009-03-03 19:30:05 -0800429#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500430 if ((png_ptr->transformations & PNG_PACK) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700431 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800432#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700433
The Android Open Source Project893912b2009-03-03 19:30:05 -0800434#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500435 if ((png_ptr->transformations & PNG_SHIFT) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700436 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800437#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700438
The Android Open Source Project893912b2009-03-03 19:30:05 -0800439#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500440 if ((png_ptr->transformations & PNG_BGR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700441 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800442#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700443
The Android Open Source Project893912b2009-03-03 19:30:05 -0800444#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500445 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700446 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800447#endif
448 }
Matt Sarett9ea75692016-01-08 13:00:42 -0500449#endif /* WARNINGS */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800450
Patrick Scott5f6bd842010-06-28 16:55:16 -0400451#ifdef PNG_READ_INTERLACING_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700452 /* If interlaced and we do not need a new row, combine row and return.
453 * Notice that the pixels we have from previous rows have been transformed
454 * already; we can only combine like with like (transformed or
455 * untransformed) and, because of the libpng API for interlaced images, this
456 * means we must transform before de-interlacing.
457 */
Matt Sarett9ea75692016-01-08 13:00:42 -0500458 if (png_ptr->interlaced != 0 &&
459 (png_ptr->transformations & PNG_INTERLACE) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800460 {
461 switch (png_ptr->pass)
462 {
463 case 0:
464 if (png_ptr->row_number & 0x07)
465 {
466 if (dsp_row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700467 png_combine_row(png_ptr, dsp_row, 1/*display*/);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800468 png_read_finish_row(png_ptr);
469 return;
470 }
471 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700472
The Android Open Source Project893912b2009-03-03 19:30:05 -0800473 case 1:
474 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
475 {
476 if (dsp_row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700477 png_combine_row(png_ptr, dsp_row, 1/*display*/);
478
The Android Open Source Project893912b2009-03-03 19:30:05 -0800479 png_read_finish_row(png_ptr);
480 return;
481 }
482 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700483
The Android Open Source Project893912b2009-03-03 19:30:05 -0800484 case 2:
485 if ((png_ptr->row_number & 0x07) != 4)
486 {
487 if (dsp_row != NULL && (png_ptr->row_number & 4))
Chris Craikb50c2172013-07-29 15:28:30 -0700488 png_combine_row(png_ptr, dsp_row, 1/*display*/);
489
The Android Open Source Project893912b2009-03-03 19:30:05 -0800490 png_read_finish_row(png_ptr);
491 return;
492 }
493 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700494
The Android Open Source Project893912b2009-03-03 19:30:05 -0800495 case 3:
496 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
497 {
498 if (dsp_row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700499 png_combine_row(png_ptr, dsp_row, 1/*display*/);
500
The Android Open Source Project893912b2009-03-03 19:30:05 -0800501 png_read_finish_row(png_ptr);
502 return;
503 }
504 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700505
The Android Open Source Project893912b2009-03-03 19:30:05 -0800506 case 4:
507 if ((png_ptr->row_number & 3) != 2)
508 {
509 if (dsp_row != NULL && (png_ptr->row_number & 2))
Chris Craikb50c2172013-07-29 15:28:30 -0700510 png_combine_row(png_ptr, dsp_row, 1/*display*/);
511
The Android Open Source Project893912b2009-03-03 19:30:05 -0800512 png_read_finish_row(png_ptr);
513 return;
514 }
515 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700516
The Android Open Source Project893912b2009-03-03 19:30:05 -0800517 case 5:
518 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
519 {
520 if (dsp_row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700521 png_combine_row(png_ptr, dsp_row, 1/*display*/);
522
The Android Open Source Project893912b2009-03-03 19:30:05 -0800523 png_read_finish_row(png_ptr);
524 return;
525 }
526 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700527
528 default:
The Android Open Source Project893912b2009-03-03 19:30:05 -0800529 case 6:
Matt Sarett9ea75692016-01-08 13:00:42 -0500530 if ((png_ptr->row_number & 1) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800531 {
532 png_read_finish_row(png_ptr);
533 return;
534 }
535 break;
536 }
537 }
538#endif
539
Matt Sarett9ea75692016-01-08 13:00:42 -0500540 if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800541 png_error(png_ptr, "Invalid attempt to read row data");
542
Chris Craikb50c2172013-07-29 15:28:30 -0700543 /* Fill the row with IDAT data: */
544 png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
545
546 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800547 {
Chris Craikb50c2172013-07-29 15:28:30 -0700548 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
549 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
550 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
551 else
552 png_error(png_ptr, "bad adaptive filter value");
553 }
Joseph Wen4ce0ee12010-08-20 10:42:22 +0800554
Chris Craikb50c2172013-07-29 15:28:30 -0700555 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
556 * 1.5.6, while the buffer really is this big in current versions of libpng
557 * it may not be in the future, so this was changed just to copy the
558 * interlaced count:
559 */
560 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800561
Patrick Scott5f6bd842010-06-28 16:55:16 -0400562#ifdef PNG_MNG_FEATURES_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500563 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
Chris Craikb50c2172013-07-29 15:28:30 -0700564 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800565 {
566 /* Intrapixel differencing */
Chris Craikb50c2172013-07-29 15:28:30 -0700567 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800568 }
569#endif
570
Chris Craikb50c2172013-07-29 15:28:30 -0700571#ifdef PNG_READ_TRANSFORMS_SUPPORTED
572 if (png_ptr->transformations)
573 png_do_read_transformations(png_ptr, &row_info);
574#endif
575
576 /* The transformed pixel depth should match the depth now in row_info. */
577 if (png_ptr->transformed_pixel_depth == 0)
578 {
579 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
580 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
581 png_error(png_ptr, "sequential row overflow");
582 }
583
584 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
585 png_error(png_ptr, "internal sequential row size calculation error");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800586
Patrick Scott5f6bd842010-06-28 16:55:16 -0400587#ifdef PNG_READ_INTERLACING_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500588 /* Expand interlaced rows to full size */
589 if (png_ptr->interlaced != 0 &&
590 (png_ptr->transformations & PNG_INTERLACE) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800591 {
592 if (png_ptr->pass < 6)
Chris Craikb50c2172013-07-29 15:28:30 -0700593 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
594 png_ptr->transformations);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800595
596 if (dsp_row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700597 png_combine_row(png_ptr, dsp_row, 1/*display*/);
598
The Android Open Source Project893912b2009-03-03 19:30:05 -0800599 if (row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700600 png_combine_row(png_ptr, row, 0/*row*/);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800601 }
Chris Craikb50c2172013-07-29 15:28:30 -0700602
The Android Open Source Project893912b2009-03-03 19:30:05 -0800603 else
604#endif
605 {
606 if (row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700607 png_combine_row(png_ptr, row, -1/*ignored*/);
608
The Android Open Source Project893912b2009-03-03 19:30:05 -0800609 if (dsp_row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700610 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800611 }
612 png_read_finish_row(png_ptr);
613
614 if (png_ptr->read_row_fn != NULL)
615 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Chris Craikb50c2172013-07-29 15:28:30 -0700616
The Android Open Source Project893912b2009-03-03 19:30:05 -0800617}
Matt Sarett9ea75692016-01-08 13:00:42 -0500618#endif /* SEQUENTIAL_READ */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800619
Patrick Scott5f6bd842010-06-28 16:55:16 -0400620#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800621/* Read one or more rows of image data. If the image is interlaced,
622 * and png_set_interlace_handling() has been called, the rows need to
623 * contain the contents of the rows from the previous pass. If the
624 * image has alpha or transparency, and png_handle_alpha()[*] has been
625 * called, the rows contents must be initialized to the contents of the
626 * screen.
627 *
628 * "row" holds the actual image, and pixels are placed in it
629 * as they arrive. If the image is displayed after each pass, it will
630 * appear to "sparkle" in. "display_row" can be used to display a
631 * "chunky" progressive image, with finer detail added as it becomes
632 * available. If you do not want this "chunky" display, you may pass
633 * NULL for display_row. If you do not want the sparkle display, and
634 * you have not called png_handle_alpha(), you may pass NULL for rows.
635 * If you have called png_handle_alpha(), and the image has either an
636 * alpha channel or a transparency chunk, you must provide a buffer for
637 * rows. In this case, you do not have to provide a display_row buffer
638 * also, but you may. If the image is not interlaced, or if you have
639 * not called png_set_interlace_handling(), the display_row buffer will
640 * be ignored, so pass NULL to it.
641 *
642 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
643 */
644
645void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700646png_read_rows(png_structrp png_ptr, png_bytepp row,
647 png_bytepp display_row, png_uint_32 num_rows)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800648{
649 png_uint_32 i;
650 png_bytepp rp;
651 png_bytepp dp;
652
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700653 png_debug(1, "in png_read_rows");
Chris Craikb50c2172013-07-29 15:28:30 -0700654
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400655 if (png_ptr == NULL)
656 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700657
The Android Open Source Project893912b2009-03-03 19:30:05 -0800658 rp = row;
659 dp = display_row;
660 if (rp != NULL && dp != NULL)
661 for (i = 0; i < num_rows; i++)
662 {
663 png_bytep rptr = *rp++;
664 png_bytep dptr = *dp++;
665
666 png_read_row(png_ptr, rptr, dptr);
667 }
Chris Craikb50c2172013-07-29 15:28:30 -0700668
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700669 else if (rp != NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800670 for (i = 0; i < num_rows; i++)
671 {
672 png_bytep rptr = *rp;
Chris Craikb50c2172013-07-29 15:28:30 -0700673 png_read_row(png_ptr, rptr, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800674 rp++;
675 }
Chris Craikb50c2172013-07-29 15:28:30 -0700676
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700677 else if (dp != NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800678 for (i = 0; i < num_rows; i++)
679 {
680 png_bytep dptr = *dp;
Chris Craikb50c2172013-07-29 15:28:30 -0700681 png_read_row(png_ptr, NULL, dptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800682 dp++;
683 }
684}
Matt Sarett9ea75692016-01-08 13:00:42 -0500685#endif /* SEQUENTIAL_READ */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800686
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530687#ifdef PNG_INDEX_SUPPORTED
688#define IDAT_HEADER_SIZE 8
689
690/* Set the png read position to a new position based on idat_position and
691 * offset.
692 */
693void
694png_set_read_offset(png_structp png_ptr,
695 png_uint_32 idat_position, png_uint_32 bytes_left)
696{
697 png_seek_data(png_ptr, idat_position);
698 png_ptr->idat_size = png_read_chunk_header(png_ptr);
699
700 // We need to add back IDAT_HEADER_SIZE because in zlib's perspective,
701 // IDAT_HEADER in PNG is already stripped out.
702 png_seek_data(png_ptr, idat_position + IDAT_HEADER_SIZE + png_ptr->idat_size - bytes_left);
703 png_ptr->idat_size = bytes_left;
704}
705
706/* Configure png decoder to decode the pass starting from *row.
707 * The requested row may be adjusted to align with an indexing row.
708 * The actual row for the decoder to start its decoding will be returned in
709 * *row.
710 */
711void PNGAPI
712png_configure_decoder(png_structp png_ptr, int *row, int pass)
713{
714 png_indexp index = png_ptr->index;
715 int n = *row / index->step[pass];
716 png_line_indexp line_index = index->pass_line_index[pass][n];
717
718 // Adjust row to an indexing row.
719 *row = n * index->step[pass];
720 png_ptr->row_number = *row;
721
722#ifdef PNG_READ_INTERLACING_SUPPORTED
723 if (png_ptr->interlaced)
724 png_set_interlaced_pass(png_ptr, pass);
725#endif
726
727 long row_byte_length =
728 PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
729
730 inflateEnd(&png_ptr->zstream);
731 inflateCopy(&png_ptr->zstream, line_index->z_state);
732
733 // Set the png read position to line_index.
734 png_set_read_offset(png_ptr, line_index->stream_idat_position,
735 line_index->bytes_left_in_idat);
736 memcpy(png_ptr->prev_row, line_index->prev_row, row_byte_length);
737 png_ptr->zstream.avail_in = 0;
738}
739
740/* Build the line index and store the index in png_ptr->index.
741 */
742void PNGAPI
743png_build_index(png_structp png_ptr)
744{
745 // number of rows in a 8x8 block for each interlaced pass.
746 int number_rows_in_pass[7] = {1, 1, 1, 2, 2, 4, 4};
747
748 int ret;
749 png_uint_32 i, j;
750 png_bytep rp;
751 int p, pass_number = 1;
752
753#ifdef PNG_READ_INTERLACING_SUPPORTED
754 pass_number = png_set_interlace_handling(png_ptr);
755#endif
756
757 if (png_ptr == NULL)
758 return;
759
760 png_read_start_row(png_ptr);
761
762#ifdef PNG_READ_INTERLACING_SUPPORTED
763 if (!png_ptr->interlaced)
764#endif
765 {
766 number_rows_in_pass[0] = 8;
767 }
768
Henrik Smidingd3ff9df2015-02-17 17:56:24 +0100769 // Allocate a buffer big enough for any transform.
770 rp = png_malloc(png_ptr, PNG_ROWBYTES(png_ptr->maximum_pixel_depth, png_ptr->width));
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530771
772 png_indexp index = png_malloc(png_ptr, sizeof(png_index));
773 png_ptr->index = index;
774
775 index->stream_idat_position = png_ptr->total_data_read - IDAT_HEADER_SIZE;
776
777 // Set the default size of index in each pass to 0,
778 // so that we can free index correctly in png_destroy_read_struct.
779 for (p = 0; p < 7; p++)
780 index->size[p] = 0;
781
782 for (p = 0; p < pass_number; p++)
783 {
784 // We adjust the index step in each pass to make sure each pass
785 // has roughly the same size of index.
786 // This way, we won't consume to much memory in recording index.
787 index->step[p] = INDEX_SAMPLE_SIZE * (8 / number_rows_in_pass[p]);
Henrik Smidingd3ff9df2015-02-17 17:56:24 +0100788 const png_uint_32 temp_size =
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530789 (png_ptr->height + index->step[p] - 1) / index->step[p];
790 index->pass_line_index[p] =
Derek Sollenberger8594e1a2014-09-22 16:28:36 -0400791 png_malloc(png_ptr, temp_size * sizeof(png_line_indexp));
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530792
793 // Get the row_byte_length seen by the filter. This value may be
794 // different from the row_byte_length of a bitmap in the case of
795 // color palette mode.
796 int row_byte_length =
797 PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
798
799 // Now, we record index for each indexing row.
Derek Sollenberger8594e1a2014-09-22 16:28:36 -0400800 for (i = 0; i < temp_size; i++)
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530801 {
802 png_line_indexp line_index = png_malloc(png_ptr, sizeof(png_line_index));
803 index->pass_line_index[p][i] = line_index;
804
805 line_index->z_state = png_malloc(png_ptr, sizeof(z_stream));
806 inflateCopy(line_index->z_state, &png_ptr->zstream);
807 line_index->prev_row = png_malloc(png_ptr, row_byte_length);
808 memcpy(line_index->prev_row, png_ptr->prev_row, row_byte_length);
809 line_index->stream_idat_position = index->stream_idat_position;
810 line_index->bytes_left_in_idat = png_ptr->idat_size + png_ptr->zstream.avail_in;
811
Derek Sollenberger8594e1a2014-09-22 16:28:36 -0400812 // increment the size now that we have the backing data structures.
813 // This prevents a crash in the event that png_read_row fails and
814 // we need to cleanup the partially constructed png_index_struct;
815 index->size[p] += 1;
816
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530817 // Skip the "step" number of rows to the next indexing row.
818 for (j = 0; j < index->step[p] &&
819 i * index->step[p] + j < png_ptr->height; j++)
820 {
821 png_read_row(png_ptr, rp, NULL);
822 }
823 }
824 }
825 png_free(png_ptr, rp);
826}
827#endif
828
Patrick Scott5f6bd842010-06-28 16:55:16 -0400829#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800830/* Read the entire image. If the image has an alpha channel or a tRNS
831 * chunk, and you have called png_handle_alpha()[*], you will need to
832 * initialize the image to the current image that PNG will be overlaying.
833 * We set the num_rows again here, in case it was incorrectly set in
834 * png_read_start_row() by a call to png_read_update_info() or
835 * png_start_read_image() if png_set_interlace_handling() wasn't called
836 * prior to either of these functions like it should have been. You can
837 * only call this function once. If you desire to have an image for
838 * each pass of a interlaced image, use png_read_rows() instead.
839 *
840 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
841 */
842void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700843png_read_image(png_structrp png_ptr, png_bytepp image)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800844{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700845 png_uint_32 i, image_height;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800846 int pass, j;
847 png_bytepp rp;
848
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700849 png_debug(1, "in png_read_image");
Chris Craikb50c2172013-07-29 15:28:30 -0700850
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400851 if (png_ptr == NULL)
852 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800853
854#ifdef PNG_READ_INTERLACING_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500855 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700856 {
857 pass = png_set_interlace_handling(png_ptr);
858 /* And make sure transforms are initialized. */
859 png_start_read_image(png_ptr);
860 }
861 else
862 {
Matt Sarett9ea75692016-01-08 13:00:42 -0500863 if (png_ptr->interlaced != 0 &&
864 (png_ptr->transformations & PNG_INTERLACE) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700865 {
866 /* Caller called png_start_read_image or png_read_update_info without
867 * first turning on the PNG_INTERLACE transform. We can fix this here,
868 * but the caller should do it!
869 */
870 png_warning(png_ptr, "Interlace handling should be turned on when "
871 "using png_read_image");
872 /* Make sure this is set correctly */
873 png_ptr->num_rows = png_ptr->height;
874 }
875
876 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
877 * the above error case.
878 */
879 pass = png_set_interlace_handling(png_ptr);
880 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800881#else
882 if (png_ptr->interlaced)
883 png_error(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -0700884 "Cannot read interlaced image -- interlace handler disabled");
885
The Android Open Source Project893912b2009-03-03 19:30:05 -0800886 pass = 1;
887#endif
888
The Android Open Source Project893912b2009-03-03 19:30:05 -0800889 image_height=png_ptr->height;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800890
891 for (j = 0; j < pass; j++)
892 {
893 rp = image;
894 for (i = 0; i < image_height; i++)
895 {
Chris Craikb50c2172013-07-29 15:28:30 -0700896 png_read_row(png_ptr, *rp, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800897 rp++;
898 }
899 }
900}
Matt Sarett9ea75692016-01-08 13:00:42 -0500901#endif /* SEQUENTIAL_READ */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800902
Patrick Scott5f6bd842010-06-28 16:55:16 -0400903#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800904/* Read the end of the PNG file. Will not read past the end of the
905 * file, will verify the end is accurate, and will read any comments
906 * or time information at the end of the file, if info is not NULL.
907 */
908void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700909png_read_end(png_structrp png_ptr, png_inforp info_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800910{
Chris Craikb50c2172013-07-29 15:28:30 -0700911#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
912 int keep;
913#endif
914
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700915 png_debug(1, "in png_read_end");
Chris Craikb50c2172013-07-29 15:28:30 -0700916
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400917 if (png_ptr == NULL)
918 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700919
920 /* If png_read_end is called in the middle of reading the rows there may
921 * still be pending IDAT data and an owned zstream. Deal with this here.
922 */
923#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500924 if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700925#endif
926 png_read_finish_IDAT(png_ptr);
927
928#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
929 /* Report invalid palette index; added at libng-1.5.10 */
930 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
931 png_ptr->num_palette_max > png_ptr->num_palette)
932 png_benign_error(png_ptr, "Read palette index exceeding num_palette");
933#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800934
935 do
936 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700937 png_uint_32 length = png_read_chunk_header(png_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -0700938 png_uint_32 chunk_name = png_ptr->chunk_name;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800939
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530940 if (chunk_name == png_IEND)
941 png_handle_IEND(png_ptr, info_ptr, length);
942
943 else if (chunk_name == png_IHDR)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800944 png_handle_IHDR(png_ptr, info_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700945
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530946 else if (info_ptr == NULL)
947 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700948
The Android Open Source Project893912b2009-03-03 19:30:05 -0800949#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700950 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800951 {
Chris Craikb50c2172013-07-29 15:28:30 -0700952 if (chunk_name == png_IDAT)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800953 {
Matt Sarett9ea75692016-01-08 13:00:42 -0500954 if ((length > 0) ||
955 (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700956 png_benign_error(png_ptr, "Too many IDATs found");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800957 }
Chris Craikb50c2172013-07-29 15:28:30 -0700958 png_handle_unknown(png_ptr, info_ptr, length, keep);
959 if (chunk_name == png_PLTE)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800960 png_ptr->mode |= PNG_HAVE_PLTE;
961 }
962#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700963
964 else if (chunk_name == png_IDAT)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800965 {
966 /* Zero length IDATs are legal after the last IDAT has been
967 * read, but not after other chunks have been read.
968 */
Matt Sarett9ea75692016-01-08 13:00:42 -0500969 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700970 png_benign_error(png_ptr, "Too many IDATs found");
971
The Android Open Source Project893912b2009-03-03 19:30:05 -0800972 png_crc_finish(png_ptr, length);
973 }
Chris Craikb50c2172013-07-29 15:28:30 -0700974 else if (chunk_name == png_PLTE)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800975 png_handle_PLTE(png_ptr, info_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700976
Patrick Scott5f6bd842010-06-28 16:55:16 -0400977#ifdef PNG_READ_bKGD_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700978 else if (chunk_name == png_bKGD)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800979 png_handle_bKGD(png_ptr, info_ptr, length);
980#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700981
Patrick Scott5f6bd842010-06-28 16:55:16 -0400982#ifdef PNG_READ_cHRM_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700983 else if (chunk_name == png_cHRM)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800984 png_handle_cHRM(png_ptr, info_ptr, length);
985#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700986
Patrick Scott5f6bd842010-06-28 16:55:16 -0400987#ifdef PNG_READ_gAMA_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700988 else if (chunk_name == png_gAMA)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800989 png_handle_gAMA(png_ptr, info_ptr, length);
990#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700991
Patrick Scott5f6bd842010-06-28 16:55:16 -0400992#ifdef PNG_READ_hIST_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700993 else if (chunk_name == png_hIST)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800994 png_handle_hIST(png_ptr, info_ptr, length);
995#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700996
Patrick Scott5f6bd842010-06-28 16:55:16 -0400997#ifdef PNG_READ_oFFs_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700998 else if (chunk_name == png_oFFs)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800999 png_handle_oFFs(png_ptr, info_ptr, length);
1000#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001001
Patrick Scott5f6bd842010-06-28 16:55:16 -04001002#ifdef PNG_READ_pCAL_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001003 else if (chunk_name == png_pCAL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001004 png_handle_pCAL(png_ptr, info_ptr, length);
1005#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001006
Patrick Scott5f6bd842010-06-28 16:55:16 -04001007#ifdef PNG_READ_sCAL_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001008 else if (chunk_name == png_sCAL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001009 png_handle_sCAL(png_ptr, info_ptr, length);
1010#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001011
Patrick Scott5f6bd842010-06-28 16:55:16 -04001012#ifdef PNG_READ_pHYs_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001013 else if (chunk_name == png_pHYs)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001014 png_handle_pHYs(png_ptr, info_ptr, length);
1015#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001016
Patrick Scott5f6bd842010-06-28 16:55:16 -04001017#ifdef PNG_READ_sBIT_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001018 else if (chunk_name == png_sBIT)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001019 png_handle_sBIT(png_ptr, info_ptr, length);
1020#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001021
Patrick Scott5f6bd842010-06-28 16:55:16 -04001022#ifdef PNG_READ_sRGB_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001023 else if (chunk_name == png_sRGB)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001024 png_handle_sRGB(png_ptr, info_ptr, length);
1025#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001026
Patrick Scott5f6bd842010-06-28 16:55:16 -04001027#ifdef PNG_READ_iCCP_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001028 else if (chunk_name == png_iCCP)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001029 png_handle_iCCP(png_ptr, info_ptr, length);
1030#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001031
Patrick Scott5f6bd842010-06-28 16:55:16 -04001032#ifdef PNG_READ_sPLT_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001033 else if (chunk_name == png_sPLT)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001034 png_handle_sPLT(png_ptr, info_ptr, length);
1035#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001036
Patrick Scott5f6bd842010-06-28 16:55:16 -04001037#ifdef PNG_READ_tEXt_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001038 else if (chunk_name == png_tEXt)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001039 png_handle_tEXt(png_ptr, info_ptr, length);
1040#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001041
Patrick Scott5f6bd842010-06-28 16:55:16 -04001042#ifdef PNG_READ_tIME_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001043 else if (chunk_name == png_tIME)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001044 png_handle_tIME(png_ptr, info_ptr, length);
1045#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001046
Patrick Scott5f6bd842010-06-28 16:55:16 -04001047#ifdef PNG_READ_tRNS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001048 else if (chunk_name == png_tRNS)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001049 png_handle_tRNS(png_ptr, info_ptr, length);
1050#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001051
Patrick Scott5f6bd842010-06-28 16:55:16 -04001052#ifdef PNG_READ_zTXt_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001053 else if (chunk_name == png_zTXt)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001054 png_handle_zTXt(png_ptr, info_ptr, length);
1055#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001056
Patrick Scott5f6bd842010-06-28 16:55:16 -04001057#ifdef PNG_READ_iTXt_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001058 else if (chunk_name == png_iTXt)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001059 png_handle_iTXt(png_ptr, info_ptr, length);
1060#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001061
The Android Open Source Project893912b2009-03-03 19:30:05 -08001062 else
Chris Craikb50c2172013-07-29 15:28:30 -07001063 png_handle_unknown(png_ptr, info_ptr, length,
1064 PNG_HANDLE_CHUNK_AS_DEFAULT);
Matt Sarett9ea75692016-01-08 13:00:42 -05001065 } while ((png_ptr->mode & PNG_HAVE_IEND) == 0);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001066}
Matt Sarett9ea75692016-01-08 13:00:42 -05001067#endif /* SEQUENTIAL_READ */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001068
Chris Craikb50c2172013-07-29 15:28:30 -07001069/* Free all memory used in the read struct */
1070static void
1071png_read_destroy(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001072{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001073 png_debug(1, "in png_read_destroy");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001074
Patrick Scott5f6bd842010-06-28 16:55:16 -04001075#ifdef PNG_READ_GAMMA_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001076 png_destroy_gamma_table(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001077#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001078
1079 png_free(png_ptr, png_ptr->big_row_buf);
Matt Sarett9ea75692016-01-08 13:00:42 -05001080 png_ptr->big_row_buf = NULL;
Chris Craikb50c2172013-07-29 15:28:30 -07001081 png_free(png_ptr, png_ptr->big_prev_row);
Matt Sarett9ea75692016-01-08 13:00:42 -05001082 png_ptr->big_prev_row = NULL;
Chris Craikb50c2172013-07-29 15:28:30 -07001083 png_free(png_ptr, png_ptr->read_buffer);
Matt Sarett9ea75692016-01-08 13:00:42 -05001084 png_ptr->read_buffer = NULL;
Chris Craikb50c2172013-07-29 15:28:30 -07001085
1086#ifdef PNG_READ_QUANTIZE_SUPPORTED
1087 png_free(png_ptr, png_ptr->palette_lookup);
Matt Sarett9ea75692016-01-08 13:00:42 -05001088 png_ptr->palette_lookup = NULL;
Chris Craikb50c2172013-07-29 15:28:30 -07001089 png_free(png_ptr, png_ptr->quantize_index);
Matt Sarett9ea75692016-01-08 13:00:42 -05001090 png_ptr->quantize_index = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001091#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001092
Matt Sarett9ea75692016-01-08 13:00:42 -05001093 if ((png_ptr->free_me & PNG_FREE_PLTE) != 0)
1094 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001095 png_zfree(png_ptr, png_ptr->palette);
Matt Sarett9ea75692016-01-08 13:00:42 -05001096 png_ptr->palette = NULL;
1097 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001098 png_ptr->free_me &= ~PNG_FREE_PLTE;
Chris Craikb50c2172013-07-29 15:28:30 -07001099
The Android Open Source Project893912b2009-03-03 19:30:05 -08001100#if defined(PNG_tRNS_SUPPORTED) || \
1101 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -05001102 if ((png_ptr->free_me & PNG_FREE_TRNS) != 0)
1103 {
Chris Craikb50c2172013-07-29 15:28:30 -07001104 png_free(png_ptr, png_ptr->trans_alpha);
Matt Sarett9ea75692016-01-08 13:00:42 -05001105 png_ptr->trans_alpha = NULL;
1106 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001107 png_ptr->free_me &= ~PNG_FREE_TRNS;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001108#endif
1109
1110 inflateEnd(&png_ptr->zstream);
Chris Craikb50c2172013-07-29 15:28:30 -07001111
The Android Open Source Project893912b2009-03-03 19:30:05 -08001112#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1113 png_free(png_ptr, png_ptr->save_buffer);
Matt Sarett9ea75692016-01-08 13:00:42 -05001114 png_ptr->save_buffer = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001115#endif
1116
Matt Sarett9ea75692016-01-08 13:00:42 -05001117#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
Chris Craikb50c2172013-07-29 15:28:30 -07001118 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1119 png_free(png_ptr, png_ptr->unknown_chunk.data);
Matt Sarett9ea75692016-01-08 13:00:42 -05001120 png_ptr->unknown_chunk.data = NULL;
Chris Craikb50c2172013-07-29 15:28:30 -07001121#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001122
Chris Craikb50c2172013-07-29 15:28:30 -07001123#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
1124 png_free(png_ptr, png_ptr->chunk_list);
Matt Sarett9ea75692016-01-08 13:00:42 -05001125 png_ptr->chunk_list = NULL;
Chris Craikb50c2172013-07-29 15:28:30 -07001126#endif
1127
Sireesh Tripurarib0277d02014-05-09 15:39:12 +05301128#ifdef PNG_INDEX_SUPPORTED
1129 if (png_ptr->index) {
1130 unsigned int i, p;
1131 png_indexp index = png_ptr->index;
1132 for (p = 0; p < 7; p++) {
1133 for (i = 0; i < index->size[p]; i++) {
1134 inflateEnd(index->pass_line_index[p][i]->z_state);
1135 png_free(png_ptr, index->pass_line_index[p][i]->z_state);
1136 png_free(png_ptr, index->pass_line_index[p][i]->prev_row);
1137 png_free(png_ptr, index->pass_line_index[p][i]);
1138 }
1139 if (index->size[p] != 0) {
1140 png_free(png_ptr, index->pass_line_index[p]);
1141 }
1142 }
1143 png_free(png_ptr, index);
1144 }
1145#endif
1146
Chris Craikb50c2172013-07-29 15:28:30 -07001147 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
1148 * callbacks are still set at this point. They are required to complete the
1149 * destruction of the png_struct itself.
The Android Open Source Project893912b2009-03-03 19:30:05 -08001150 */
Chris Craikb50c2172013-07-29 15:28:30 -07001151}
The Android Open Source Project893912b2009-03-03 19:30:05 -08001152
Chris Craikb50c2172013-07-29 15:28:30 -07001153/* Free all memory used by the read */
1154void PNGAPI
1155png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
1156 png_infopp end_info_ptr_ptr)
1157{
1158 png_structrp png_ptr = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001159
Chris Craikb50c2172013-07-29 15:28:30 -07001160 png_debug(1, "in png_destroy_read_struct");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001161
Chris Craikb50c2172013-07-29 15:28:30 -07001162 if (png_ptr_ptr != NULL)
1163 png_ptr = *png_ptr_ptr;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001164
Chris Craikb50c2172013-07-29 15:28:30 -07001165 if (png_ptr == NULL)
1166 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001167
Chris Craikb50c2172013-07-29 15:28:30 -07001168 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
1169 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
1170 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
1171 */
1172 png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
1173 png_destroy_info_struct(png_ptr, info_ptr_ptr);
1174
1175 *png_ptr_ptr = NULL;
1176 png_read_destroy(png_ptr);
1177 png_destroy_png_struct(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001178}
1179
1180void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -07001181png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001182{
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001183 if (png_ptr == NULL)
1184 return;
Chris Craikb50c2172013-07-29 15:28:30 -07001185
The Android Open Source Project893912b2009-03-03 19:30:05 -08001186 png_ptr->read_row_fn = read_row_fn;
1187}
1188
1189
Patrick Scott5f6bd842010-06-28 16:55:16 -04001190#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1191#ifdef PNG_INFO_IMAGE_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001192void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -07001193png_read_png(png_structrp png_ptr, png_inforp info_ptr,
The Android Open Source Project893912b2009-03-03 19:30:05 -08001194 int transforms,
1195 voidp params)
1196{
Chris Craikb50c2172013-07-29 15:28:30 -07001197 if (png_ptr == NULL || info_ptr == NULL)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001198 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001199
1200 /* png_read_info() gives us all of the information from the
1201 * PNG file before the first IDAT (image data chunk).
1202 */
1203 png_read_info(png_ptr, info_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -07001204 if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001205 png_error(png_ptr, "Image is too high to process with png_read_png()");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001206
1207 /* -------------- image transformations start here ------------------- */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301208 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
1209 * is not implemented. This will only happen in de-configured (non-default)
1210 * libpng builds. The results can be unexpected - png_read_png may return
1211 * short or mal-formed rows because the transform is skipped.
1212 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001213
Chris Craikb50c2172013-07-29 15:28:30 -07001214 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
1215 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001216 if ((transforms & PNG_TRANSFORM_SCALE_16) != 0)
1217 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
1218 * did in earlier versions, while "scale_16" is now more accurate.
1219 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301220#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001221 png_set_scale_16(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301222#else
1223 png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
Chris Craikb50c2172013-07-29 15:28:30 -07001224#endif
1225
Chris Craikb50c2172013-07-29 15:28:30 -07001226 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
1227 * latter by doing SCALE first. This is ok and allows apps not to check for
1228 * which is supported to get the right answer.
The Android Open Source Project893912b2009-03-03 19:30:05 -08001229 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001230 if ((transforms & PNG_TRANSFORM_STRIP_16) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301231#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001232 png_set_strip_16(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301233#else
1234 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001235#endif
1236
The Android Open Source Project893912b2009-03-03 19:30:05 -08001237 /* Strip alpha bytes from the input data without combining with
1238 * the background (not recommended).
1239 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001240 if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301241#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001242 png_set_strip_alpha(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301243#else
1244 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001245#endif
1246
The Android Open Source Project893912b2009-03-03 19:30:05 -08001247 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1248 * byte into separate bytes (useful for paletted and grayscale images).
1249 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001250 if ((transforms & PNG_TRANSFORM_PACKING) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301251#ifdef PNG_READ_PACK_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001252 png_set_packing(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301253#else
1254 png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001255#endif
1256
The Android Open Source Project893912b2009-03-03 19:30:05 -08001257 /* Change the order of packed pixels to least significant bit first
1258 * (not useful if you are using png_set_packing).
1259 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001260 if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301261#ifdef PNG_READ_PACKSWAP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001262 png_set_packswap(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301263#else
1264 png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001265#endif
1266
The Android Open Source Project893912b2009-03-03 19:30:05 -08001267 /* Expand paletted colors into true RGB triplets
1268 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1269 * Expand paletted or RGB images with transparency to full alpha
1270 * channels so the data will be available as RGBA quartets.
1271 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001272 if ((transforms & PNG_TRANSFORM_EXPAND) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301273#ifdef PNG_READ_EXPAND_SUPPORTED
1274 png_set_expand(png_ptr);
1275#else
1276 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001277#endif
1278
Chris Craikb50c2172013-07-29 15:28:30 -07001279 /* We don't handle background color or gamma transformation or quantizing.
The Android Open Source Project893912b2009-03-03 19:30:05 -08001280 */
1281
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001282 /* Invert monochrome files to have 0 as white and 1 as black
The Android Open Source Project893912b2009-03-03 19:30:05 -08001283 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001284 if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301285#ifdef PNG_READ_INVERT_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001286 png_set_invert_mono(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301287#else
1288 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001289#endif
1290
The Android Open Source Project893912b2009-03-03 19:30:05 -08001291 /* If you want to shift the pixel values from the range [0,255] or
1292 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1293 * colors were originally in:
1294 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001295 if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301296#ifdef PNG_READ_SHIFT_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -05001297 if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301298 png_set_shift(png_ptr, &info_ptr->sig_bit);
1299#else
1300 png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001301#endif
1302
Chris Craikb50c2172013-07-29 15:28:30 -07001303 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Matt Sarett9ea75692016-01-08 13:00:42 -05001304 if ((transforms & PNG_TRANSFORM_BGR) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301305#ifdef PNG_READ_BGR_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001306 png_set_bgr(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301307#else
1308 png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001309#endif
1310
Chris Craikb50c2172013-07-29 15:28:30 -07001311 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Matt Sarett9ea75692016-01-08 13:00:42 -05001312 if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301313#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001314 png_set_swap_alpha(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301315#else
1316 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001317#endif
1318
Chris Craikb50c2172013-07-29 15:28:30 -07001319 /* Swap bytes of 16-bit files to least significant byte first */
Matt Sarett9ea75692016-01-08 13:00:42 -05001320 if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301321#ifdef PNG_READ_SWAP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001322 png_set_swap(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301323#else
1324 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001325#endif
1326
Patrick Scott5f6bd842010-06-28 16:55:16 -04001327/* Added at libpng-1.2.41 */
Chris Craikb50c2172013-07-29 15:28:30 -07001328 /* Invert the alpha channel from opacity to transparency */
Matt Sarett9ea75692016-01-08 13:00:42 -05001329 if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301330#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001331 png_set_invert_alpha(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301332#else
1333 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
Patrick Scott5f6bd842010-06-28 16:55:16 -04001334#endif
1335
1336/* Added at libpng-1.2.41 */
Chris Craikb50c2172013-07-29 15:28:30 -07001337 /* Expand grayscale image to RGB */
Matt Sarett9ea75692016-01-08 13:00:42 -05001338 if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301339#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001340 png_set_gray_to_rgb(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301341#else
1342 png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
Chris Craikb50c2172013-07-29 15:28:30 -07001343#endif
1344
1345/* Added at libpng-1.5.4 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001346 if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301347#ifdef PNG_READ_EXPAND_16_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001348 png_set_expand_16(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301349#else
1350 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
Patrick Scott5f6bd842010-06-28 16:55:16 -04001351#endif
1352
The Android Open Source Project893912b2009-03-03 19:30:05 -08001353 /* We don't handle adding filler bytes */
1354
Chris Craikb50c2172013-07-29 15:28:30 -07001355 /* We use png_read_image and rely on that for interlace handling, but we also
1356 * call png_read_update_info therefore must turn on interlace handling now:
1357 */
1358 (void)png_set_interlace_handling(png_ptr);
1359
The Android Open Source Project893912b2009-03-03 19:30:05 -08001360 /* Optional call to gamma correct and add the background to the palette
1361 * and update info structure. REQUIRED if you are expecting libpng to
1362 * update the palette for you (i.e., you selected such a transform above).
1363 */
1364 png_read_update_info(png_ptr, info_ptr);
1365
1366 /* -------------- image transformations end here ------------------- */
1367
The Android Open Source Project893912b2009-03-03 19:30:05 -08001368 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001369 if (info_ptr->row_pointers == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001370 {
Chris Craikb50c2172013-07-29 15:28:30 -07001371 png_uint_32 iptr;
Patrick Scott5f6bd842010-06-28 16:55:16 -04001372
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301373 info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
1374 info_ptr->height * (sizeof (png_bytep))));
1375
Chris Craikb50c2172013-07-29 15:28:30 -07001376 for (iptr=0; iptr<info_ptr->height; iptr++)
1377 info_ptr->row_pointers[iptr] = NULL;
1378
The Android Open Source Project893912b2009-03-03 19:30:05 -08001379 info_ptr->free_me |= PNG_FREE_ROWS;
Patrick Scott5f6bd842010-06-28 16:55:16 -04001380
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301381 for (iptr = 0; iptr < info_ptr->height; iptr++)
1382 info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
Matt Sarett9ea75692016-01-08 13:00:42 -05001383 png_malloc(png_ptr, info_ptr->rowbytes));
The Android Open Source Project893912b2009-03-03 19:30:05 -08001384 }
1385
1386 png_read_image(png_ptr, info_ptr->row_pointers);
1387 info_ptr->valid |= PNG_INFO_IDAT;
1388
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001389 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001390 png_read_end(png_ptr, info_ptr);
1391
Chris Craikb50c2172013-07-29 15:28:30 -07001392 PNG_UNUSED(params)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001393}
Matt Sarett9ea75692016-01-08 13:00:42 -05001394#endif /* INFO_IMAGE */
1395#endif /* SEQUENTIAL_READ */
Chris Craikb50c2172013-07-29 15:28:30 -07001396
1397#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1398/* SIMPLIFIED READ
1399 *
1400 * This code currently relies on the sequential reader, though it could easily
1401 * be made to work with the progressive one.
1402 */
1403/* Arguments to png_image_finish_read: */
1404
1405/* Encoding of PNG data (used by the color-map code) */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301406# define P_NOTSET 0 /* File encoding not yet known */
1407# define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
1408# define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1409# define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1410# define P_LINEAR8 4 /* 8-bit linear: only from a file value */
Chris Craikb50c2172013-07-29 15:28:30 -07001411
1412/* Color-map processing: after libpng has run on the PNG image further
Matt Sarett9ea75692016-01-08 13:00:42 -05001413 * processing may be needed to convert the data to color-map indices.
Chris Craikb50c2172013-07-29 15:28:30 -07001414 */
1415#define PNG_CMAP_NONE 0
1416#define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1417#define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1418#define PNG_CMAP_RGB 3 /* Process RGB data */
1419#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1420
1421/* The following document where the background is for each processing case. */
1422#define PNG_CMAP_NONE_BACKGROUND 256
1423#define PNG_CMAP_GA_BACKGROUND 231
1424#define PNG_CMAP_TRANS_BACKGROUND 254
1425#define PNG_CMAP_RGB_BACKGROUND 256
1426#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1427
1428typedef struct
1429{
1430 /* Arguments: */
1431 png_imagep image;
1432 png_voidp buffer;
1433 png_int_32 row_stride;
1434 png_voidp colormap;
1435 png_const_colorp background;
1436 /* Local variables: */
1437 png_voidp local_row;
1438 png_voidp first_row;
1439 ptrdiff_t row_bytes; /* step between rows */
1440 int file_encoding; /* E_ values above */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301441 png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
Chris Craikb50c2172013-07-29 15:28:30 -07001442 int colormap_processing; /* PNG_CMAP_ values above */
1443} png_image_read_control;
1444
1445/* Do all the *safe* initialization - 'safe' means that png_error won't be
1446 * called, so setting up the jmp_buf is not required. This means that anything
1447 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1448 * instead so that control is returned safely back to this routine.
1449 */
1450static int
1451png_image_read_init(png_imagep image)
1452{
1453 if (image->opaque == NULL)
1454 {
1455 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
1456 png_safe_error, png_safe_warning);
1457
1458 /* And set the rest of the structure to NULL to ensure that the various
1459 * fields are consistent.
1460 */
1461 memset(image, 0, (sizeof *image));
1462 image->version = PNG_IMAGE_VERSION;
1463
1464 if (png_ptr != NULL)
1465 {
1466 png_infop info_ptr = png_create_info_struct(png_ptr);
1467
1468 if (info_ptr != NULL)
1469 {
1470 png_controlp control = png_voidcast(png_controlp,
1471 png_malloc_warn(png_ptr, (sizeof *control)));
1472
1473 if (control != NULL)
1474 {
1475 memset(control, 0, (sizeof *control));
1476
1477 control->png_ptr = png_ptr;
1478 control->info_ptr = info_ptr;
1479 control->for_write = 0;
1480
1481 image->opaque = control;
1482 return 1;
1483 }
1484
1485 /* Error clean up */
1486 png_destroy_info_struct(png_ptr, &info_ptr);
1487 }
1488
1489 png_destroy_read_struct(&png_ptr, NULL, NULL);
1490 }
1491
1492 return png_image_error(image, "png_image_read: out of memory");
1493 }
1494
1495 return png_image_error(image, "png_image_read: opaque pointer not NULL");
1496}
1497
1498/* Utility to find the base format of a PNG file from a png_struct. */
1499static png_uint_32
1500png_image_format(png_structrp png_ptr)
1501{
1502 png_uint_32 format = 0;
1503
Matt Sarett9ea75692016-01-08 13:00:42 -05001504 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001505 format |= PNG_FORMAT_FLAG_COLOR;
1506
Matt Sarett9ea75692016-01-08 13:00:42 -05001507 if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001508 format |= PNG_FORMAT_FLAG_ALPHA;
1509
1510 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1511 * sets the png_struct fields; that's all we are interested in here. The
1512 * precise interaction with an app call to png_set_tRNS and PNG file reading
1513 * is unclear.
1514 */
1515 else if (png_ptr->num_trans > 0)
1516 format |= PNG_FORMAT_FLAG_ALPHA;
1517
1518 if (png_ptr->bit_depth == 16)
1519 format |= PNG_FORMAT_FLAG_LINEAR;
1520
Matt Sarett9ea75692016-01-08 13:00:42 -05001521 if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001522 format |= PNG_FORMAT_FLAG_COLORMAP;
1523
1524 return format;
1525}
1526
1527/* Is the given gamma significantly different from sRGB? The test is the same
1528 * one used in pngrtran.c when deciding whether to do gamma correction. The
1529 * arithmetic optimizes the division by using the fact that the inverse of the
1530 * file sRGB gamma is 2.2
1531 */
1532static int
1533png_gamma_not_sRGB(png_fixed_point g)
1534{
1535 if (g < PNG_FP_1)
1536 {
1537 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
1538 if (g == 0)
1539 return 0;
1540
1541 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1542 }
1543
1544 return 1;
1545}
1546
1547/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1548 * header and fill in all the information. This is executed in a safe context,
1549 * unlike the init routine above.
1550 */
1551static int
1552png_image_read_header(png_voidp argument)
1553{
1554 png_imagep image = png_voidcast(png_imagep, argument);
1555 png_structrp png_ptr = image->opaque->png_ptr;
1556 png_inforp info_ptr = image->opaque->info_ptr;
1557
1558 png_set_benign_errors(png_ptr, 1/*warn*/);
1559 png_read_info(png_ptr, info_ptr);
1560
1561 /* Do this the fast way; just read directly out of png_struct. */
1562 image->width = png_ptr->width;
1563 image->height = png_ptr->height;
1564
1565 {
1566 png_uint_32 format = png_image_format(png_ptr);
1567
1568 image->format = format;
1569
1570#ifdef PNG_COLORSPACE_SUPPORTED
1571 /* Does the colorspace match sRGB? If there is no color endpoint
1572 * (colorant) information assume yes, otherwise require the
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301573 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
Chris Craikb50c2172013-07-29 15:28:30 -07001574 * colorspace has been determined to be invalid ignore it.
1575 */
1576 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
1577 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
1578 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
1579 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1580#endif
1581 }
1582
1583 /* We need the maximum number of entries regardless of the format the
1584 * application sets here.
1585 */
1586 {
1587 png_uint_32 cmap_entries;
1588
1589 switch (png_ptr->color_type)
1590 {
1591 case PNG_COLOR_TYPE_GRAY:
1592 cmap_entries = 1U << png_ptr->bit_depth;
1593 break;
1594
1595 case PNG_COLOR_TYPE_PALETTE:
1596 cmap_entries = png_ptr->num_palette;
1597 break;
1598
1599 default:
1600 cmap_entries = 256;
1601 break;
1602 }
1603
1604 if (cmap_entries > 256)
1605 cmap_entries = 256;
1606
1607 image->colormap_entries = cmap_entries;
1608 }
1609
1610 return 1;
1611}
1612
1613#ifdef PNG_STDIO_SUPPORTED
1614int PNGAPI
1615png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1616{
1617 if (image != NULL && image->version == PNG_IMAGE_VERSION)
1618 {
1619 if (file != NULL)
1620 {
Matt Sarett9ea75692016-01-08 13:00:42 -05001621 if (png_image_read_init(image) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001622 {
1623 /* This is slightly evil, but png_init_io doesn't do anything other
1624 * than this and we haven't changed the standard IO functions so
1625 * this saves a 'safe' function.
1626 */
1627 image->opaque->png_ptr->io_ptr = file;
1628 return png_safe_execute(image, png_image_read_header, image);
1629 }
1630 }
1631
1632 else
1633 return png_image_error(image,
1634 "png_image_begin_read_from_stdio: invalid argument");
1635 }
1636
1637 else if (image != NULL)
1638 return png_image_error(image,
1639 "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
1640
1641 return 0;
1642}
1643
1644int PNGAPI
1645png_image_begin_read_from_file(png_imagep image, const char *file_name)
1646{
1647 if (image != NULL && image->version == PNG_IMAGE_VERSION)
1648 {
1649 if (file_name != NULL)
1650 {
1651 FILE *fp = fopen(file_name, "rb");
1652
1653 if (fp != NULL)
1654 {
Matt Sarett9ea75692016-01-08 13:00:42 -05001655 if (png_image_read_init(image) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001656 {
1657 image->opaque->png_ptr->io_ptr = fp;
1658 image->opaque->owned_file = 1;
1659 return png_safe_execute(image, png_image_read_header, image);
1660 }
1661
1662 /* Clean up: just the opened file. */
1663 (void)fclose(fp);
1664 }
1665
1666 else
1667 return png_image_error(image, strerror(errno));
1668 }
1669
1670 else
1671 return png_image_error(image,
1672 "png_image_begin_read_from_file: invalid argument");
1673 }
1674
1675 else if (image != NULL)
1676 return png_image_error(image,
1677 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
1678
1679 return 0;
1680}
Matt Sarett9ea75692016-01-08 13:00:42 -05001681#endif /* STDIO */
Chris Craikb50c2172013-07-29 15:28:30 -07001682
1683static void PNGCBAPI
1684png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
1685{
1686 if (png_ptr != NULL)
1687 {
1688 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
1689 if (image != NULL)
1690 {
1691 png_controlp cp = image->opaque;
1692 if (cp != NULL)
1693 {
1694 png_const_bytep memory = cp->memory;
1695 png_size_t size = cp->size;
1696
1697 if (memory != NULL && size >= need)
1698 {
1699 memcpy(out, memory, need);
1700 cp->memory = memory + need;
1701 cp->size = size - need;
1702 return;
1703 }
1704
1705 png_error(png_ptr, "read beyond end of data");
1706 }
1707 }
1708
1709 png_error(png_ptr, "invalid memory read");
1710 }
1711}
1712
1713int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1714 png_const_voidp memory, png_size_t size)
1715{
1716 if (image != NULL && image->version == PNG_IMAGE_VERSION)
1717 {
1718 if (memory != NULL && size > 0)
1719 {
Matt Sarett9ea75692016-01-08 13:00:42 -05001720 if (png_image_read_init(image) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001721 {
1722 /* Now set the IO functions to read from the memory buffer and
1723 * store it into io_ptr. Again do this in-place to avoid calling a
1724 * libpng function that requires error handling.
1725 */
1726 image->opaque->memory = png_voidcast(png_const_bytep, memory);
1727 image->opaque->size = size;
1728 image->opaque->png_ptr->io_ptr = image;
1729 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1730
1731 return png_safe_execute(image, png_image_read_header, image);
1732 }
1733 }
1734
1735 else
1736 return png_image_error(image,
1737 "png_image_begin_read_from_memory: invalid argument");
1738 }
1739
1740 else if (image != NULL)
1741 return png_image_error(image,
1742 "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
1743
1744 return 0;
1745}
1746
1747/* Utility function to skip chunks that are not used by the simplified image
1748 * read functions and an appropriate macro to call it.
1749 */
1750#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1751static void
1752png_image_skip_unused_chunks(png_structrp png_ptr)
1753{
1754 /* Prepare the reader to ignore all recognized chunks whose data will not
1755 * be used, i.e., all chunks recognized by libpng except for those
1756 * involved in basic image reading:
1757 *
1758 * IHDR, PLTE, IDAT, IEND
1759 *
1760 * Or image data handling:
1761 *
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301762 * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
Chris Craikb50c2172013-07-29 15:28:30 -07001763 *
1764 * This provides a small performance improvement and eliminates any
1765 * potential vulnerability to security problems in the unused chunks.
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301766 *
1767 * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
1768 * too. This allows the simplified API to be compiled without iCCP support,
1769 * however if the support is there the chunk is still checked to detect
1770 * errors (which are unfortunately quite common.)
Chris Craikb50c2172013-07-29 15:28:30 -07001771 */
1772 {
1773 static PNG_CONST png_byte chunks_to_process[] = {
1774 98, 75, 71, 68, '\0', /* bKGD */
1775 99, 72, 82, 77, '\0', /* cHRM */
1776 103, 65, 77, 65, '\0', /* gAMA */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301777# ifdef PNG_READ_iCCP_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001778 105, 67, 67, 80, '\0', /* iCCP */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301779# endif
Chris Craikb50c2172013-07-29 15:28:30 -07001780 115, 66, 73, 84, '\0', /* sBIT */
1781 115, 82, 71, 66, '\0', /* sRGB */
1782 };
1783
1784 /* Ignore unknown chunks and all other chunks except for the
1785 * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
1786 */
1787 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
1788 NULL, -1);
1789
1790 /* But do not ignore image data handling chunks */
1791 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
Matt Sarett9ea75692016-01-08 13:00:42 -05001792 chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5);
Chris Craikb50c2172013-07-29 15:28:30 -07001793 }
1794}
1795
1796# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1797#else
1798# define PNG_SKIP_CHUNKS(p) ((void)0)
Matt Sarett9ea75692016-01-08 13:00:42 -05001799#endif /* HANDLE_AS_UNKNOWN */
Chris Craikb50c2172013-07-29 15:28:30 -07001800
1801/* The following macro gives the exact rounded answer for all values in the
1802 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1803 * the correct numbers 0..5
1804 */
1805#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1806
1807/* Utility functions to make particular color-maps */
1808static void
1809set_file_encoding(png_image_read_control *display)
1810{
1811 png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma;
Matt Sarett9ea75692016-01-08 13:00:42 -05001812 if (png_gamma_significant(g) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001813 {
Matt Sarett9ea75692016-01-08 13:00:42 -05001814 if (png_gamma_not_sRGB(g) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001815 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301816 display->file_encoding = P_FILE;
Chris Craikb50c2172013-07-29 15:28:30 -07001817 display->gamma_to_linear = png_reciprocal(g);
1818 }
1819
1820 else
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301821 display->file_encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07001822 }
1823
1824 else
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301825 display->file_encoding = P_LINEAR8;
Chris Craikb50c2172013-07-29 15:28:30 -07001826}
1827
1828static unsigned int
1829decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
1830{
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301831 if (encoding == P_FILE) /* double check */
Chris Craikb50c2172013-07-29 15:28:30 -07001832 encoding = display->file_encoding;
1833
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301834 if (encoding == P_NOTSET) /* must be the file encoding */
Chris Craikb50c2172013-07-29 15:28:30 -07001835 {
1836 set_file_encoding(display);
1837 encoding = display->file_encoding;
1838 }
1839
1840 switch (encoding)
1841 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301842 case P_FILE:
Chris Craikb50c2172013-07-29 15:28:30 -07001843 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
1844 break;
1845
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301846 case P_sRGB:
Chris Craikb50c2172013-07-29 15:28:30 -07001847 value = png_sRGB_table[value];
1848 break;
1849
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301850 case P_LINEAR:
Chris Craikb50c2172013-07-29 15:28:30 -07001851 break;
1852
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301853 case P_LINEAR8:
Chris Craikb50c2172013-07-29 15:28:30 -07001854 value *= 257;
1855 break;
1856
Matt Sarett9ea75692016-01-08 13:00:42 -05001857#ifdef __GNUC__
Chris Craikb50c2172013-07-29 15:28:30 -07001858 default:
1859 png_error(display->image->opaque->png_ptr,
1860 "unexpected encoding (internal error)");
Matt Sarett9ea75692016-01-08 13:00:42 -05001861#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001862 }
1863
1864 return value;
1865}
1866
1867static png_uint_32
1868png_colormap_compose(png_image_read_control *display,
1869 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
1870 png_uint_32 background, int encoding)
1871{
1872 /* The file value is composed on the background, the background has the given
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301873 * encoding and so does the result, the file is encoded with P_FILE and the
Chris Craikb50c2172013-07-29 15:28:30 -07001874 * file and alpha are 8-bit values. The (output) encoding will always be
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301875 * P_LINEAR or P_sRGB.
Chris Craikb50c2172013-07-29 15:28:30 -07001876 */
1877 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
1878 png_uint_32 b = decode_gamma(display, background, encoding);
1879
1880 /* The alpha is always an 8-bit value (it comes from the palette), the value
1881 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1882 */
1883 f = f * alpha + b * (255-alpha);
1884
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301885 if (encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07001886 {
1887 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1888 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1889 */
1890 f *= 257; /* Now scaled by 65535 */
1891 f += f >> 16;
1892 f = (f+32768) >> 16;
1893 }
1894
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301895 else /* P_sRGB */
Chris Craikb50c2172013-07-29 15:28:30 -07001896 f = PNG_sRGB_FROM_LINEAR(f);
1897
1898 return f;
1899}
1900
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301901/* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
Chris Craikb50c2172013-07-29 15:28:30 -07001902 * be 8-bit.
1903 */
1904static void
1905png_create_colormap_entry(png_image_read_control *display,
1906 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
1907 png_uint_32 alpha, int encoding)
1908{
1909 png_imagep image = display->image;
Matt Sarett9ea75692016-01-08 13:00:42 -05001910 const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301911 P_LINEAR : P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07001912 const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
1913 (red != green || green != blue);
1914
1915 if (ip > 255)
1916 png_error(image->opaque->png_ptr, "color-map index out of range");
1917
1918 /* Update the cache with whether the file gamma is significantly different
1919 * from sRGB.
1920 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301921 if (encoding == P_FILE)
Chris Craikb50c2172013-07-29 15:28:30 -07001922 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301923 if (display->file_encoding == P_NOTSET)
Chris Craikb50c2172013-07-29 15:28:30 -07001924 set_file_encoding(display);
1925
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301926 /* Note that the cached value may be P_FILE too, but if it is then the
Chris Craikb50c2172013-07-29 15:28:30 -07001927 * gamma_to_linear member has been set.
1928 */
1929 encoding = display->file_encoding;
1930 }
1931
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301932 if (encoding == P_FILE)
Chris Craikb50c2172013-07-29 15:28:30 -07001933 {
1934 png_fixed_point g = display->gamma_to_linear;
1935
1936 red = png_gamma_16bit_correct(red*257, g);
1937 green = png_gamma_16bit_correct(green*257, g);
1938 blue = png_gamma_16bit_correct(blue*257, g);
1939
Matt Sarett9ea75692016-01-08 13:00:42 -05001940 if (convert_to_Y != 0 || output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07001941 {
1942 alpha *= 257;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301943 encoding = P_LINEAR;
Chris Craikb50c2172013-07-29 15:28:30 -07001944 }
1945
1946 else
1947 {
1948 red = PNG_sRGB_FROM_LINEAR(red * 255);
1949 green = PNG_sRGB_FROM_LINEAR(green * 255);
1950 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301951 encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07001952 }
1953 }
1954
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301955 else if (encoding == P_LINEAR8)
Chris Craikb50c2172013-07-29 15:28:30 -07001956 {
1957 /* This encoding occurs quite frequently in test cases because PngSuite
1958 * includes a gAMA 1.0 chunk with most images.
1959 */
1960 red *= 257;
1961 green *= 257;
1962 blue *= 257;
1963 alpha *= 257;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301964 encoding = P_LINEAR;
Chris Craikb50c2172013-07-29 15:28:30 -07001965 }
1966
Matt Sarett9ea75692016-01-08 13:00:42 -05001967 else if (encoding == P_sRGB &&
1968 (convert_to_Y != 0 || output_encoding == P_LINEAR))
Chris Craikb50c2172013-07-29 15:28:30 -07001969 {
1970 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1971 * linear.
1972 */
1973 red = png_sRGB_table[red];
1974 green = png_sRGB_table[green];
1975 blue = png_sRGB_table[blue];
1976 alpha *= 257;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301977 encoding = P_LINEAR;
Chris Craikb50c2172013-07-29 15:28:30 -07001978 }
1979
1980 /* This is set if the color isn't gray but the output is. */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301981 if (encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07001982 {
Matt Sarett9ea75692016-01-08 13:00:42 -05001983 if (convert_to_Y != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001984 {
1985 /* NOTE: these values are copied from png_do_rgb_to_gray */
1986 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
1987 (png_uint_32)2366 * blue;
1988
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301989 if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07001990 y = (y + 16384) >> 15;
1991
1992 else
1993 {
1994 /* y is scaled by 32768, we need it scaled by 255: */
1995 y = (y + 128) >> 8;
1996 y *= 255;
1997 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
Matt Sarett9ea75692016-01-08 13:00:42 -05001998 alpha = PNG_DIV257(alpha);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301999 encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07002000 }
2001
2002 blue = red = green = y;
2003 }
2004
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302005 else if (output_encoding == P_sRGB)
Chris Craikb50c2172013-07-29 15:28:30 -07002006 {
2007 red = PNG_sRGB_FROM_LINEAR(red * 255);
2008 green = PNG_sRGB_FROM_LINEAR(green * 255);
2009 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
2010 alpha = PNG_DIV257(alpha);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302011 encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07002012 }
2013 }
2014
2015 if (encoding != output_encoding)
2016 png_error(image->opaque->png_ptr, "bad encoding (internal error)");
2017
2018 /* Store the value. */
2019 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302020# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07002021 const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
2022 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
2023# else
2024# define afirst 0
2025# endif
2026# ifdef PNG_FORMAT_BGR_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -05002027 const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
Chris Craikb50c2172013-07-29 15:28:30 -07002028# else
2029# define bgr 0
2030# endif
2031
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302032 if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07002033 {
2034 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
2035
2036 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
2037
2038 /* The linear 16-bit values must be pre-multiplied by the alpha channel
2039 * value, if less than 65535 (this is, effectively, composite on black
2040 * if the alpha channel is removed.)
2041 */
2042 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
2043 {
2044 case 4:
2045 entry[afirst ? 0 : 3] = (png_uint_16)alpha;
2046 /* FALL THROUGH */
2047
2048 case 3:
2049 if (alpha < 65535)
2050 {
2051 if (alpha > 0)
2052 {
2053 blue = (blue * alpha + 32767U)/65535U;
2054 green = (green * alpha + 32767U)/65535U;
2055 red = (red * alpha + 32767U)/65535U;
2056 }
2057
2058 else
2059 red = green = blue = 0;
2060 }
2061 entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
2062 entry[afirst + 1] = (png_uint_16)green;
2063 entry[afirst + bgr] = (png_uint_16)red;
2064 break;
2065
2066 case 2:
2067 entry[1 ^ afirst] = (png_uint_16)alpha;
2068 /* FALL THROUGH */
2069
2070 case 1:
2071 if (alpha < 65535)
2072 {
2073 if (alpha > 0)
2074 green = (green * alpha + 32767U)/65535U;
2075
2076 else
2077 green = 0;
2078 }
2079 entry[afirst] = (png_uint_16)green;
2080 break;
2081
2082 default:
2083 break;
2084 }
2085 }
2086
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302087 else /* output encoding is P_sRGB */
Chris Craikb50c2172013-07-29 15:28:30 -07002088 {
2089 png_bytep entry = png_voidcast(png_bytep, display->colormap);
2090
2091 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
2092
2093 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
2094 {
2095 case 4:
2096 entry[afirst ? 0 : 3] = (png_byte)alpha;
2097 case 3:
2098 entry[afirst + (2 ^ bgr)] = (png_byte)blue;
2099 entry[afirst + 1] = (png_byte)green;
2100 entry[afirst + bgr] = (png_byte)red;
2101 break;
2102
2103 case 2:
2104 entry[1 ^ afirst] = (png_byte)alpha;
2105 case 1:
2106 entry[afirst] = (png_byte)green;
2107 break;
2108
2109 default:
2110 break;
2111 }
2112 }
2113
2114# ifdef afirst
2115# undef afirst
2116# endif
2117# ifdef bgr
2118# undef bgr
2119# endif
2120 }
2121}
2122
2123static int
2124make_gray_file_colormap(png_image_read_control *display)
2125{
2126 unsigned int i;
2127
2128 for (i=0; i<256; ++i)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302129 png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
Chris Craikb50c2172013-07-29 15:28:30 -07002130
2131 return i;
2132}
2133
2134static int
2135make_gray_colormap(png_image_read_control *display)
2136{
2137 unsigned int i;
2138
2139 for (i=0; i<256; ++i)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302140 png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002141
2142 return i;
2143}
2144#define PNG_GRAY_COLORMAP_ENTRIES 256
2145
2146static int
2147make_ga_colormap(png_image_read_control *display)
2148{
2149 unsigned int i, a;
2150
2151 /* Alpha is retained, the output will be a color-map with entries
2152 * selected by six levels of alpha. One transparent entry, 6 gray
2153 * levels for all the intermediate alpha values, leaving 230 entries
2154 * for the opaque grays. The color-map entries are the six values
2155 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
2156 * relevant entry.
2157 *
2158 * if (alpha > 229) // opaque
2159 * {
2160 * // The 231 entries are selected to make the math below work:
2161 * base = 0;
2162 * entry = (231 * gray + 128) >> 8;
2163 * }
2164 * else if (alpha < 26) // transparent
2165 * {
2166 * base = 231;
2167 * entry = 0;
2168 * }
2169 * else // partially opaque
2170 * {
2171 * base = 226 + 6 * PNG_DIV51(alpha);
2172 * entry = PNG_DIV51(gray);
2173 * }
2174 */
2175 i = 0;
2176 while (i < 231)
2177 {
2178 unsigned int gray = (i * 256 + 115) / 231;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302179 png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002180 }
2181
2182 /* 255 is used here for the component values for consistency with the code
2183 * that undoes premultiplication in pngwrite.c.
2184 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302185 png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002186
2187 for (a=1; a<5; ++a)
2188 {
2189 unsigned int g;
2190
2191 for (g=0; g<6; ++g)
2192 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302193 P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002194 }
2195
2196 return i;
2197}
2198
2199#define PNG_GA_COLORMAP_ENTRIES 256
2200
2201static int
2202make_rgb_colormap(png_image_read_control *display)
2203{
2204 unsigned int i, r;
2205
2206 /* Build a 6x6x6 opaque RGB cube */
2207 for (i=r=0; r<6; ++r)
2208 {
2209 unsigned int g;
2210
2211 for (g=0; g<6; ++g)
2212 {
2213 unsigned int b;
2214
2215 for (b=0; b<6; ++b)
2216 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302217 P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002218 }
2219 }
2220
2221 return i;
2222}
2223
2224#define PNG_RGB_COLORMAP_ENTRIES 216
2225
2226/* Return a palette index to the above palette given three 8-bit sRGB values. */
2227#define PNG_RGB_INDEX(r,g,b) \
2228 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
2229
2230static int
2231png_image_read_colormap(png_voidp argument)
2232{
2233 png_image_read_control *display =
2234 png_voidcast(png_image_read_control*, argument);
2235 const png_imagep image = display->image;
2236
2237 const png_structrp png_ptr = image->opaque->png_ptr;
2238 const png_uint_32 output_format = image->format;
Matt Sarett9ea75692016-01-08 13:00:42 -05002239 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302240 P_LINEAR : P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07002241
2242 unsigned int cmap_entries;
2243 unsigned int output_processing; /* Output processing option */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302244 unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
Chris Craikb50c2172013-07-29 15:28:30 -07002245
2246 /* Background information; the background color and the index of this color
2247 * in the color-map if it exists (else 256).
2248 */
2249 unsigned int background_index = 256;
2250 png_uint_32 back_r, back_g, back_b;
2251
2252 /* Flags to accumulate things that need to be done to the input. */
2253 int expand_tRNS = 0;
2254
2255 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
2256 * very difficult to do, the results look awful, and it is difficult to see
2257 * what possible use it is because the application can't control the
2258 * color-map.
2259 */
2260 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
2261 png_ptr->num_trans > 0) /* alpha in input */ &&
2262 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
2263 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302264 if (output_encoding == P_LINEAR) /* compose on black */
Chris Craikb50c2172013-07-29 15:28:30 -07002265 back_b = back_g = back_r = 0;
2266
2267 else if (display->background == NULL /* no way to remove it */)
2268 png_error(png_ptr,
2269 "a background color must be supplied to remove alpha/transparency");
2270
2271 /* Get a copy of the background color (this avoids repeating the checks
2272 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
2273 * output format.
2274 */
2275 else
2276 {
2277 back_g = display->background->green;
Matt Sarett9ea75692016-01-08 13:00:42 -05002278 if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002279 {
2280 back_r = display->background->red;
2281 back_b = display->background->blue;
2282 }
2283 else
2284 back_b = back_r = back_g;
2285 }
2286 }
2287
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302288 else if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07002289 back_b = back_r = back_g = 65535;
2290
2291 else
2292 back_b = back_r = back_g = 255;
2293
2294 /* Default the input file gamma if required - this is necessary because
2295 * libpng assumes that if no gamma information is present the data is in the
2296 * output format, but the simplified API deduces the gamma from the input
2297 * format.
2298 */
2299 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0)
2300 {
2301 /* Do this directly, not using the png_colorspace functions, to ensure
2302 * that it happens even if the colorspace is invalid (though probably if
2303 * it is the setting will be ignored) Note that the same thing can be
2304 * achieved at the application interface with png_set_gAMA.
2305 */
2306 if (png_ptr->bit_depth == 16 &&
2307 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
2308 png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR;
2309
2310 else
2311 png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE;
2312
2313 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
2314 }
2315
2316 /* Decide what to do based on the PNG color type of the input data. The
2317 * utility function png_create_colormap_entry deals with most aspects of the
2318 * output transformations; this code works out how to produce bytes of
2319 * color-map entries from the original format.
2320 */
2321 switch (png_ptr->color_type)
2322 {
2323 case PNG_COLOR_TYPE_GRAY:
2324 if (png_ptr->bit_depth <= 8)
2325 {
2326 /* There at most 256 colors in the output, regardless of
2327 * transparency.
2328 */
2329 unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
2330
2331 cmap_entries = 1U << png_ptr->bit_depth;
2332 if (cmap_entries > image->colormap_entries)
2333 png_error(png_ptr, "gray[8] color-map: too few entries");
2334
2335 step = 255 / (cmap_entries - 1);
2336 output_processing = PNG_CMAP_NONE;
2337
2338 /* If there is a tRNS chunk then this either selects a transparent
2339 * value or, if the output has no alpha, the background color.
2340 */
2341 if (png_ptr->num_trans > 0)
2342 {
2343 trans = png_ptr->trans_color.gray;
2344
2345 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302346 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
Chris Craikb50c2172013-07-29 15:28:30 -07002347 }
2348
2349 /* png_create_colormap_entry just takes an RGBA and writes the
2350 * corresponding color-map entry using the format from 'image',
2351 * including the required conversion to sRGB or linear as
2352 * appropriate. The input values are always either sRGB (if the
2353 * gamma correction flag is 0) or 0..255 scaled file encoded values
2354 * (if the function must gamma correct them).
2355 */
2356 for (i=val=0; i<cmap_entries; ++i, val += step)
2357 {
2358 /* 'i' is a file value. While this will result in duplicated
2359 * entries for 8-bit non-sRGB encoded files it is necessary to
2360 * have non-gamma corrected values to do tRNS handling.
2361 */
2362 if (i != trans)
2363 png_create_colormap_entry(display, i, val, val, val, 255,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302364 P_FILE/*8-bit with file gamma*/);
Chris Craikb50c2172013-07-29 15:28:30 -07002365
2366 /* Else this entry is transparent. The colors don't matter if
2367 * there is an alpha channel (back_alpha == 0), but it does no
2368 * harm to pass them in; the values are not set above so this
2369 * passes in white.
2370 *
2371 * NOTE: this preserves the full precision of the application
2372 * supplied background color when it is used.
2373 */
2374 else
2375 png_create_colormap_entry(display, i, back_r, back_g, back_b,
2376 back_alpha, output_encoding);
2377 }
2378
2379 /* We need libpng to preserve the original encoding. */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302380 data_encoding = P_FILE;
Chris Craikb50c2172013-07-29 15:28:30 -07002381
2382 /* The rows from libpng, while technically gray values, are now also
Matt Sarett9ea75692016-01-08 13:00:42 -05002383 * color-map indices; however, they may need to be expanded to 1
Chris Craikb50c2172013-07-29 15:28:30 -07002384 * byte per pixel. This is what png_set_packing does (i.e., it
2385 * unpacks the bit values into bytes.)
2386 */
2387 if (png_ptr->bit_depth < 8)
2388 png_set_packing(png_ptr);
2389 }
2390
2391 else /* bit depth is 16 */
2392 {
2393 /* The 16-bit input values can be converted directly to 8-bit gamma
2394 * encoded values; however, if a tRNS chunk is present 257 color-map
2395 * entries are required. This means that the extra entry requires
2396 * special processing; add an alpha channel, sacrifice gray level
2397 * 254 and convert transparent (alpha==0) entries to that.
2398 *
2399 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2400 * same time to minimize quality loss. If a tRNS chunk is present
2401 * this means libpng must handle it too; otherwise it is impossible
2402 * to do the exact match on the 16-bit value.
2403 *
2404 * If the output has no alpha channel *and* the background color is
2405 * gray then it is possible to let libpng handle the substitution by
2406 * ensuring that the corresponding gray level matches the background
2407 * color exactly.
2408 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302409 data_encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07002410
2411 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2412 png_error(png_ptr, "gray[16] color-map: too few entries");
2413
2414 cmap_entries = make_gray_colormap(display);
2415
2416 if (png_ptr->num_trans > 0)
2417 {
2418 unsigned int back_alpha;
2419
Matt Sarett9ea75692016-01-08 13:00:42 -05002420 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002421 back_alpha = 0;
2422
2423 else
2424 {
2425 if (back_r == back_g && back_g == back_b)
2426 {
2427 /* Background is gray; no special processing will be
2428 * required.
2429 */
2430 png_color_16 c;
2431 png_uint_32 gray = back_g;
2432
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302433 if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07002434 {
2435 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2436
2437 /* And make sure the corresponding palette entry
2438 * matches.
2439 */
2440 png_create_colormap_entry(display, gray, back_g, back_g,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302441 back_g, 65535, P_LINEAR);
Chris Craikb50c2172013-07-29 15:28:30 -07002442 }
2443
2444 /* The background passed to libpng, however, must be the
2445 * sRGB value.
2446 */
2447 c.index = 0; /*unused*/
2448 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2449
2450 /* NOTE: does this work without expanding tRNS to alpha?
2451 * It should be the color->gray case below apparently
2452 * doesn't.
2453 */
2454 png_set_background_fixed(png_ptr, &c,
2455 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2456 0/*gamma: not used*/);
2457
2458 output_processing = PNG_CMAP_NONE;
2459 break;
2460 }
Matt Sarett9ea75692016-01-08 13:00:42 -05002461#ifdef __COVERITY__
2462 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
2463 * here.
2464 */
2465 back_alpha = 255;
2466#else
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302467 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
Matt Sarett9ea75692016-01-08 13:00:42 -05002468#endif
Chris Craikb50c2172013-07-29 15:28:30 -07002469 }
2470
2471 /* output_processing means that the libpng-processed row will be
2472 * 8-bit GA and it has to be processing to single byte color-map
2473 * values. Entry 254 is replaced by either a completely
2474 * transparent entry or by the background color at full
Matt Sarett9ea75692016-01-08 13:00:42 -05002475 * precision (and the background color is not a simple gray
2476 * level in this case.)
Chris Craikb50c2172013-07-29 15:28:30 -07002477 */
2478 expand_tRNS = 1;
2479 output_processing = PNG_CMAP_TRANS;
2480 background_index = 254;
2481
2482 /* And set (overwrite) color-map entry 254 to the actual
2483 * background color at full precision.
2484 */
2485 png_create_colormap_entry(display, 254, back_r, back_g, back_b,
2486 back_alpha, output_encoding);
2487 }
2488
2489 else
2490 output_processing = PNG_CMAP_NONE;
2491 }
2492 break;
2493
2494 case PNG_COLOR_TYPE_GRAY_ALPHA:
2495 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2496 * of 65536 combinations. If, however, the alpha channel is to be
2497 * removed there are only 256 possibilities if the background is gray.
2498 * (Otherwise there is a subset of the 65536 possibilities defined by
2499 * the triangle between black, white and the background color.)
2500 *
2501 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2502 * worry about tRNS matching - tRNS is ignored if there is an alpha
2503 * channel.
2504 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302505 data_encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07002506
Matt Sarett9ea75692016-01-08 13:00:42 -05002507 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002508 {
2509 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2510 png_error(png_ptr, "gray+alpha color-map: too few entries");
2511
2512 cmap_entries = make_ga_colormap(display);
2513
2514 background_index = PNG_CMAP_GA_BACKGROUND;
2515 output_processing = PNG_CMAP_GA;
2516 }
2517
2518 else /* alpha is removed */
2519 {
2520 /* Alpha must be removed as the PNG data is processed when the
2521 * background is a color because the G and A channels are
2522 * independent and the vector addition (non-parallel vectors) is a
2523 * 2-D problem.
2524 *
2525 * This can be reduced to the same algorithm as above by making a
2526 * colormap containing gray levels (for the opaque grays), a
2527 * background entry (for a transparent pixel) and a set of four six
2528 * level color values, one set for each intermediate alpha value.
2529 * See the comments in make_ga_colormap for how this works in the
2530 * per-pixel processing.
2531 *
2532 * If the background is gray, however, we only need a 256 entry gray
2533 * level color map. It is sufficient to make the entry generated
2534 * for the background color be exactly the color specified.
2535 */
2536 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2537 (back_r == back_g && back_g == back_b))
2538 {
2539 /* Background is gray; no special processing will be required. */
2540 png_color_16 c;
2541 png_uint_32 gray = back_g;
2542
2543 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2544 png_error(png_ptr, "gray-alpha color-map: too few entries");
2545
2546 cmap_entries = make_gray_colormap(display);
2547
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302548 if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07002549 {
2550 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2551
2552 /* And make sure the corresponding palette entry matches. */
2553 png_create_colormap_entry(display, gray, back_g, back_g,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302554 back_g, 65535, P_LINEAR);
Chris Craikb50c2172013-07-29 15:28:30 -07002555 }
2556
2557 /* The background passed to libpng, however, must be the sRGB
2558 * value.
2559 */
2560 c.index = 0; /*unused*/
2561 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2562
2563 png_set_background_fixed(png_ptr, &c,
2564 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2565 0/*gamma: not used*/);
2566
2567 output_processing = PNG_CMAP_NONE;
2568 }
2569
2570 else
2571 {
2572 png_uint_32 i, a;
2573
2574 /* This is the same as png_make_ga_colormap, above, except that
2575 * the entries are all opaque.
2576 */
2577 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2578 png_error(png_ptr, "ga-alpha color-map: too few entries");
2579
2580 i = 0;
2581 while (i < 231)
2582 {
2583 png_uint_32 gray = (i * 256 + 115) / 231;
2584 png_create_colormap_entry(display, i++, gray, gray, gray,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302585 255, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002586 }
2587
2588 /* NOTE: this preserves the full precision of the application
2589 * background color.
2590 */
2591 background_index = i;
2592 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
Matt Sarett9ea75692016-01-08 13:00:42 -05002593#ifdef __COVERITY__
2594 /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
2595 * here.
2596 */ 255U,
2597#else
2598 output_encoding == P_LINEAR ? 65535U : 255U,
2599#endif
2600 output_encoding);
Chris Craikb50c2172013-07-29 15:28:30 -07002601
2602 /* For non-opaque input composite on the sRGB background - this
2603 * requires inverting the encoding for each component. The input
2604 * is still converted to the sRGB encoding because this is a
2605 * reasonable approximate to the logarithmic curve of human
2606 * visual sensitivity, at least over the narrow range which PNG
2607 * represents. Consequently 'G' is always sRGB encoded, while
2608 * 'A' is linear. We need the linear background colors.
2609 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302610 if (output_encoding == P_sRGB) /* else already linear */
Chris Craikb50c2172013-07-29 15:28:30 -07002611 {
2612 /* This may produce a value not exactly matching the
2613 * background, but that's ok because these numbers are only
2614 * used when alpha != 0
2615 */
2616 back_r = png_sRGB_table[back_r];
2617 back_g = png_sRGB_table[back_g];
2618 back_b = png_sRGB_table[back_b];
2619 }
2620
2621 for (a=1; a<5; ++a)
2622 {
2623 unsigned int g;
2624
2625 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2626 * by an 8-bit alpha value (0..255).
2627 */
2628 png_uint_32 alpha = 51 * a;
2629 png_uint_32 back_rx = (255-alpha) * back_r;
2630 png_uint_32 back_gx = (255-alpha) * back_g;
2631 png_uint_32 back_bx = (255-alpha) * back_b;
2632
2633 for (g=0; g<6; ++g)
2634 {
2635 png_uint_32 gray = png_sRGB_table[g*51] * alpha;
2636
2637 png_create_colormap_entry(display, i++,
2638 PNG_sRGB_FROM_LINEAR(gray + back_rx),
2639 PNG_sRGB_FROM_LINEAR(gray + back_gx),
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302640 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002641 }
2642 }
2643
2644 cmap_entries = i;
2645 output_processing = PNG_CMAP_GA;
2646 }
2647 }
2648 break;
2649
2650 case PNG_COLOR_TYPE_RGB:
2651 case PNG_COLOR_TYPE_RGB_ALPHA:
2652 /* Exclude the case where the output is gray; we can always handle this
2653 * with the cases above.
2654 */
2655 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
2656 {
2657 /* The color-map will be grayscale, so we may as well convert the
2658 * input RGB values to a simple grayscale and use the grayscale
2659 * code above.
2660 *
2661 * NOTE: calling this apparently damages the recognition of the
2662 * transparent color in background color handling; call
2663 * png_set_tRNS_to_alpha before png_set_background_fixed.
2664 */
2665 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2666 -1);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302667 data_encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07002668
2669 /* The output will now be one or two 8-bit gray or gray+alpha
2670 * channels. The more complex case arises when the input has alpha.
2671 */
2672 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2673 png_ptr->num_trans > 0) &&
2674 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2675 {
2676 /* Both input and output have an alpha channel, so no background
2677 * processing is required; just map the GA bytes to the right
2678 * color-map entry.
2679 */
2680 expand_tRNS = 1;
2681
2682 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2683 png_error(png_ptr, "rgb[ga] color-map: too few entries");
2684
2685 cmap_entries = make_ga_colormap(display);
2686 background_index = PNG_CMAP_GA_BACKGROUND;
2687 output_processing = PNG_CMAP_GA;
2688 }
2689
2690 else
2691 {
2692 /* Either the input or the output has no alpha channel, so there
2693 * will be no non-opaque pixels in the color-map; it will just be
2694 * grayscale.
2695 */
2696 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2697 png_error(png_ptr, "rgb[gray] color-map: too few entries");
2698
2699 /* Ideally this code would use libpng to do the gamma correction,
2700 * but if an input alpha channel is to be removed we will hit the
2701 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2702 * correction bug). Fix this by dropping the gamma correction in
2703 * this case and doing it in the palette; this will result in
2704 * duplicate palette entries, but that's better than the
2705 * alternative of double gamma correction.
2706 */
2707 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2708 png_ptr->num_trans > 0) &&
Matt Sarett9ea75692016-01-08 13:00:42 -05002709 png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002710 {
2711 cmap_entries = make_gray_file_colormap(display);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302712 data_encoding = P_FILE;
Chris Craikb50c2172013-07-29 15:28:30 -07002713 }
2714
2715 else
2716 cmap_entries = make_gray_colormap(display);
2717
2718 /* But if the input has alpha or transparency it must be removed
2719 */
2720 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2721 png_ptr->num_trans > 0)
2722 {
2723 png_color_16 c;
2724 png_uint_32 gray = back_g;
2725
2726 /* We need to ensure that the application background exists in
2727 * the colormap and that completely transparent pixels map to
2728 * it. Achieve this simply by ensuring that the entry
2729 * selected for the background really is the background color.
2730 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302731 if (data_encoding == P_FILE) /* from the fixup above */
Chris Craikb50c2172013-07-29 15:28:30 -07002732 {
2733 /* The app supplied a gray which is in output_encoding, we
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302734 * need to convert it to a value of the input (P_FILE)
Chris Craikb50c2172013-07-29 15:28:30 -07002735 * encoding then set this palette entry to the required
2736 * output encoding.
2737 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302738 if (output_encoding == P_sRGB)
2739 gray = png_sRGB_table[gray]; /* now P_LINEAR */
Chris Craikb50c2172013-07-29 15:28:30 -07002740
2741 gray = PNG_DIV257(png_gamma_16bit_correct(gray,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302742 png_ptr->colorspace.gamma)); /* now P_FILE */
Chris Craikb50c2172013-07-29 15:28:30 -07002743
2744 /* And make sure the corresponding palette entry contains
2745 * exactly the required sRGB value.
2746 */
2747 png_create_colormap_entry(display, gray, back_g, back_g,
2748 back_g, 0/*unused*/, output_encoding);
2749 }
2750
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302751 else if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07002752 {
2753 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2754
2755 /* And make sure the corresponding palette entry matches.
2756 */
2757 png_create_colormap_entry(display, gray, back_g, back_g,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302758 back_g, 0/*unused*/, P_LINEAR);
Chris Craikb50c2172013-07-29 15:28:30 -07002759 }
2760
2761 /* The background passed to libpng, however, must be the
2762 * output (normally sRGB) value.
2763 */
2764 c.index = 0; /*unused*/
2765 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2766
2767 /* NOTE: the following is apparently a bug in libpng. Without
2768 * it the transparent color recognition in
2769 * png_set_background_fixed seems to go wrong.
2770 */
2771 expand_tRNS = 1;
2772 png_set_background_fixed(png_ptr, &c,
2773 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2774 0/*gamma: not used*/);
2775 }
2776
2777 output_processing = PNG_CMAP_NONE;
2778 }
2779 }
2780
2781 else /* output is color */
2782 {
2783 /* We could use png_quantize here so long as there is no transparent
2784 * color or alpha; png_quantize ignores alpha. Easier overall just
2785 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2786 * Consequently we always want libpng to produce sRGB data.
2787 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302788 data_encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07002789
2790 /* Is there any transparency or alpha? */
2791 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2792 png_ptr->num_trans > 0)
2793 {
2794 /* Is there alpha in the output too? If so all four channels are
2795 * processed into a special RGB cube with alpha support.
2796 */
Matt Sarett9ea75692016-01-08 13:00:42 -05002797 if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002798 {
2799 png_uint_32 r;
2800
2801 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2802 png_error(png_ptr, "rgb+alpha color-map: too few entries");
2803
2804 cmap_entries = make_rgb_colormap(display);
2805
2806 /* Add a transparent entry. */
2807 png_create_colormap_entry(display, cmap_entries, 255, 255,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302808 255, 0, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002809
2810 /* This is stored as the background index for the processing
2811 * algorithm.
2812 */
2813 background_index = cmap_entries++;
2814
2815 /* Add 27 r,g,b entries each with alpha 0.5. */
2816 for (r=0; r<256; r = (r << 1) | 0x7f)
2817 {
2818 png_uint_32 g;
2819
2820 for (g=0; g<256; g = (g << 1) | 0x7f)
2821 {
2822 png_uint_32 b;
2823
2824 /* This generates components with the values 0, 127 and
2825 * 255
2826 */
2827 for (b=0; b<256; b = (b << 1) | 0x7f)
2828 png_create_colormap_entry(display, cmap_entries++,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302829 r, g, b, 128, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002830 }
2831 }
2832
2833 expand_tRNS = 1;
2834 output_processing = PNG_CMAP_RGB_ALPHA;
2835 }
2836
2837 else
2838 {
2839 /* Alpha/transparency must be removed. The background must
2840 * exist in the color map (achieved by setting adding it after
2841 * the 666 color-map). If the standard processing code will
2842 * pick up this entry automatically that's all that is
2843 * required; libpng can be called to do the background
2844 * processing.
2845 */
2846 unsigned int sample_size =
2847 PNG_IMAGE_SAMPLE_SIZE(output_format);
2848 png_uint_32 r, g, b; /* sRGB background */
2849
2850 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2851 png_error(png_ptr, "rgb-alpha color-map: too few entries");
2852
2853 cmap_entries = make_rgb_colormap(display);
2854
2855 png_create_colormap_entry(display, cmap_entries, back_r,
2856 back_g, back_b, 0/*unused*/, output_encoding);
2857
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302858 if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07002859 {
2860 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2861 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2862 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2863 }
2864
2865 else
2866 {
2867 r = back_r;
2868 g = back_g;
2869 b = back_g;
2870 }
2871
2872 /* Compare the newly-created color-map entry with the one the
2873 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2874 * match, add the new one and set this as the background
2875 * index.
2876 */
2877 if (memcmp((png_const_bytep)display->colormap +
2878 sample_size * cmap_entries,
2879 (png_const_bytep)display->colormap +
2880 sample_size * PNG_RGB_INDEX(r,g,b),
2881 sample_size) != 0)
2882 {
2883 /* The background color must be added. */
2884 background_index = cmap_entries++;
2885
2886 /* Add 27 r,g,b entries each with created by composing with
2887 * the background at alpha 0.5.
2888 */
2889 for (r=0; r<256; r = (r << 1) | 0x7f)
2890 {
2891 for (g=0; g<256; g = (g << 1) | 0x7f)
2892 {
2893 /* This generates components with the values 0, 127
2894 * and 255
2895 */
2896 for (b=0; b<256; b = (b << 1) | 0x7f)
2897 png_create_colormap_entry(display, cmap_entries++,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302898 png_colormap_compose(display, r, P_sRGB, 128,
Chris Craikb50c2172013-07-29 15:28:30 -07002899 back_r, output_encoding),
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302900 png_colormap_compose(display, g, P_sRGB, 128,
Chris Craikb50c2172013-07-29 15:28:30 -07002901 back_g, output_encoding),
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302902 png_colormap_compose(display, b, P_sRGB, 128,
Chris Craikb50c2172013-07-29 15:28:30 -07002903 back_b, output_encoding),
2904 0/*unused*/, output_encoding);
2905 }
2906 }
2907
2908 expand_tRNS = 1;
2909 output_processing = PNG_CMAP_RGB_ALPHA;
2910 }
2911
2912 else /* background color is in the standard color-map */
2913 {
2914 png_color_16 c;
2915
2916 c.index = 0; /*unused*/
2917 c.red = (png_uint_16)back_r;
2918 c.gray = c.green = (png_uint_16)back_g;
2919 c.blue = (png_uint_16)back_b;
2920
2921 png_set_background_fixed(png_ptr, &c,
2922 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2923 0/*gamma: not used*/);
2924
2925 output_processing = PNG_CMAP_RGB;
2926 }
2927 }
2928 }
2929
2930 else /* no alpha or transparency in the input */
2931 {
2932 /* Alpha in the output is irrelevant, simply map the opaque input
2933 * pixels to the 6x6x6 color-map.
2934 */
2935 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2936 png_error(png_ptr, "rgb color-map: too few entries");
2937
2938 cmap_entries = make_rgb_colormap(display);
2939 output_processing = PNG_CMAP_RGB;
2940 }
2941 }
2942 break;
2943
2944 case PNG_COLOR_TYPE_PALETTE:
2945 /* It's already got a color-map. It may be necessary to eliminate the
2946 * tRNS entries though.
2947 */
2948 {
2949 unsigned int num_trans = png_ptr->num_trans;
2950 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2951 png_const_colorp colormap = png_ptr->palette;
2952 const int do_background = trans != NULL &&
2953 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2954 unsigned int i;
2955
2956 /* Just in case: */
2957 if (trans == NULL)
2958 num_trans = 0;
2959
2960 output_processing = PNG_CMAP_NONE;
Matt Sarett9ea75692016-01-08 13:00:42 -05002961 data_encoding = P_FILE; /* Don't change from color-map indices */
Chris Craikb50c2172013-07-29 15:28:30 -07002962 cmap_entries = png_ptr->num_palette;
2963 if (cmap_entries > 256)
2964 cmap_entries = 256;
2965
2966 if (cmap_entries > image->colormap_entries)
2967 png_error(png_ptr, "palette color-map: too few entries");
2968
2969 for (i=0; i < cmap_entries; ++i)
2970 {
Matt Sarett9ea75692016-01-08 13:00:42 -05002971 if (do_background != 0 && i < num_trans && trans[i] < 255)
Chris Craikb50c2172013-07-29 15:28:30 -07002972 {
2973 if (trans[i] == 0)
2974 png_create_colormap_entry(display, i, back_r, back_g,
2975 back_b, 0, output_encoding);
2976
2977 else
2978 {
2979 /* Must compose the PNG file color in the color-map entry
2980 * on the sRGB color in 'back'.
2981 */
2982 png_create_colormap_entry(display, i,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302983 png_colormap_compose(display, colormap[i].red, P_FILE,
Chris Craikb50c2172013-07-29 15:28:30 -07002984 trans[i], back_r, output_encoding),
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302985 png_colormap_compose(display, colormap[i].green, P_FILE,
Chris Craikb50c2172013-07-29 15:28:30 -07002986 trans[i], back_g, output_encoding),
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302987 png_colormap_compose(display, colormap[i].blue, P_FILE,
Chris Craikb50c2172013-07-29 15:28:30 -07002988 trans[i], back_b, output_encoding),
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302989 output_encoding == P_LINEAR ? trans[i] * 257U :
Chris Craikb50c2172013-07-29 15:28:30 -07002990 trans[i],
2991 output_encoding);
2992 }
2993 }
2994
2995 else
2996 png_create_colormap_entry(display, i, colormap[i].red,
2997 colormap[i].green, colormap[i].blue,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302998 i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
Chris Craikb50c2172013-07-29 15:28:30 -07002999 }
3000
Matt Sarett9ea75692016-01-08 13:00:42 -05003001 /* The PNG data may have indices packed in fewer than 8 bits, it
Chris Craikb50c2172013-07-29 15:28:30 -07003002 * must be expanded if so.
3003 */
3004 if (png_ptr->bit_depth < 8)
3005 png_set_packing(png_ptr);
3006 }
3007 break;
3008
3009 default:
3010 png_error(png_ptr, "invalid PNG color type");
3011 /*NOT REACHED*/
Chris Craikb50c2172013-07-29 15:28:30 -07003012 }
3013
3014 /* Now deal with the output processing */
Matt Sarett9ea75692016-01-08 13:00:42 -05003015 if (expand_tRNS != 0 && png_ptr->num_trans > 0 &&
3016 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003017 png_set_tRNS_to_alpha(png_ptr);
3018
3019 switch (data_encoding)
3020 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303021 case P_sRGB:
Chris Craikb50c2172013-07-29 15:28:30 -07003022 /* Change to 8-bit sRGB */
3023 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
3024 /* FALL THROUGH */
3025
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303026 case P_FILE:
Chris Craikb50c2172013-07-29 15:28:30 -07003027 if (png_ptr->bit_depth > 8)
3028 png_set_scale_16(png_ptr);
3029 break;
Matt Sarett9ea75692016-01-08 13:00:42 -05003030
3031#ifdef __GNUC__
3032 default:
3033 png_error(png_ptr, "bad data option (internal error)");
3034#endif
Chris Craikb50c2172013-07-29 15:28:30 -07003035 }
3036
3037 if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
3038 png_error(png_ptr, "color map overflow (BAD internal error)");
3039
3040 image->colormap_entries = cmap_entries;
3041
3042 /* Double check using the recorded background index */
3043 switch (output_processing)
3044 {
3045 case PNG_CMAP_NONE:
3046 if (background_index != PNG_CMAP_NONE_BACKGROUND)
3047 goto bad_background;
3048 break;
3049
3050 case PNG_CMAP_GA:
3051 if (background_index != PNG_CMAP_GA_BACKGROUND)
3052 goto bad_background;
3053 break;
3054
3055 case PNG_CMAP_TRANS:
3056 if (background_index >= cmap_entries ||
3057 background_index != PNG_CMAP_TRANS_BACKGROUND)
3058 goto bad_background;
3059 break;
3060
3061 case PNG_CMAP_RGB:
3062 if (background_index != PNG_CMAP_RGB_BACKGROUND)
3063 goto bad_background;
3064 break;
3065
3066 case PNG_CMAP_RGB_ALPHA:
3067 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
3068 goto bad_background;
3069 break;
3070
3071 default:
3072 png_error(png_ptr, "bad processing option (internal error)");
3073
3074 bad_background:
3075 png_error(png_ptr, "bad background index (internal error)");
3076 }
3077
3078 display->colormap_processing = output_processing;
3079
3080 return 1/*ok*/;
3081}
3082
3083/* The final part of the color-map read called from png_image_finish_read. */
3084static int
3085png_image_read_and_map(png_voidp argument)
3086{
3087 png_image_read_control *display = png_voidcast(png_image_read_control*,
3088 argument);
3089 png_imagep image = display->image;
3090 png_structrp png_ptr = image->opaque->png_ptr;
3091 int passes;
3092
3093 /* Called when the libpng data must be transformed into the color-mapped
3094 * form. There is a local row buffer in display->local and this routine must
3095 * do the interlace handling.
3096 */
3097 switch (png_ptr->interlaced)
3098 {
3099 case PNG_INTERLACE_NONE:
3100 passes = 1;
3101 break;
3102
3103 case PNG_INTERLACE_ADAM7:
3104 passes = PNG_INTERLACE_ADAM7_PASSES;
3105 break;
3106
3107 default:
Chris Craikb50c2172013-07-29 15:28:30 -07003108 png_error(png_ptr, "unknown interlace type");
3109 }
3110
3111 {
3112 png_uint_32 height = image->height;
3113 png_uint_32 width = image->width;
3114 int proc = display->colormap_processing;
3115 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3116 ptrdiff_t step_row = display->row_bytes;
3117 int pass;
3118
3119 for (pass = 0; pass < passes; ++pass)
3120 {
3121 unsigned int startx, stepx, stepy;
3122 png_uint_32 y;
3123
3124 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3125 {
3126 /* The row may be empty for a short image: */
3127 if (PNG_PASS_COLS(width, pass) == 0)
3128 continue;
3129
3130 startx = PNG_PASS_START_COL(pass);
3131 stepx = PNG_PASS_COL_OFFSET(pass);
3132 y = PNG_PASS_START_ROW(pass);
3133 stepy = PNG_PASS_ROW_OFFSET(pass);
3134 }
3135
3136 else
3137 {
3138 y = 0;
3139 startx = 0;
3140 stepx = stepy = 1;
3141 }
3142
3143 for (; y<height; y += stepy)
3144 {
3145 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
3146 png_bytep outrow = first_row + y * step_row;
3147 png_const_bytep end_row = outrow + width;
3148
3149 /* Read read the libpng data into the temporary buffer. */
3150 png_read_row(png_ptr, inrow, NULL);
3151
3152 /* Now process the row according to the processing option, note
3153 * that the caller verifies that the format of the libpng output
3154 * data is as required.
3155 */
3156 outrow += startx;
3157 switch (proc)
3158 {
3159 case PNG_CMAP_GA:
3160 for (; outrow < end_row; outrow += stepx)
3161 {
3162 /* The data is always in the PNG order */
3163 unsigned int gray = *inrow++;
3164 unsigned int alpha = *inrow++;
3165 unsigned int entry;
3166
3167 /* NOTE: this code is copied as a comment in
3168 * make_ga_colormap above. Please update the
3169 * comment if you change this code!
3170 */
3171 if (alpha > 229) /* opaque */
3172 {
3173 entry = (231 * gray + 128) >> 8;
3174 }
3175 else if (alpha < 26) /* transparent */
3176 {
3177 entry = 231;
3178 }
3179 else /* partially opaque */
3180 {
3181 entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
3182 }
3183
3184 *outrow = (png_byte)entry;
3185 }
3186 break;
3187
3188 case PNG_CMAP_TRANS:
3189 for (; outrow < end_row; outrow += stepx)
3190 {
3191 png_byte gray = *inrow++;
3192 png_byte alpha = *inrow++;
3193
3194 if (alpha == 0)
3195 *outrow = PNG_CMAP_TRANS_BACKGROUND;
3196
3197 else if (gray != PNG_CMAP_TRANS_BACKGROUND)
3198 *outrow = gray;
3199
3200 else
3201 *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
3202 }
3203 break;
3204
3205 case PNG_CMAP_RGB:
3206 for (; outrow < end_row; outrow += stepx)
3207 {
3208 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
3209 inrow += 3;
3210 }
3211 break;
3212
3213 case PNG_CMAP_RGB_ALPHA:
3214 for (; outrow < end_row; outrow += stepx)
3215 {
3216 unsigned int alpha = inrow[3];
3217
3218 /* Because the alpha entries only hold alpha==0.5 values
3219 * split the processing at alpha==0.25 (64) and 0.75
3220 * (196).
3221 */
3222
3223 if (alpha >= 196)
3224 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
3225 inrow[2]);
3226
3227 else if (alpha < 64)
3228 *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
3229
3230 else
3231 {
3232 /* Likewise there are three entries for each of r, g
3233 * and b. We could select the entry by popcount on
3234 * the top two bits on those architectures that
3235 * support it, this is what the code below does,
3236 * crudely.
3237 */
3238 unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
3239
3240 /* Here are how the values map:
3241 *
3242 * 0x00 .. 0x3f -> 0
3243 * 0x40 .. 0xbf -> 1
3244 * 0xc0 .. 0xff -> 2
3245 *
3246 * So, as above with the explicit alpha checks, the
3247 * breakpoints are at 64 and 196.
3248 */
3249 if (inrow[0] & 0x80) back_i += 9; /* red */
3250 if (inrow[0] & 0x40) back_i += 9;
3251 if (inrow[0] & 0x80) back_i += 3; /* green */
3252 if (inrow[0] & 0x40) back_i += 3;
3253 if (inrow[0] & 0x80) back_i += 1; /* blue */
3254 if (inrow[0] & 0x40) back_i += 1;
3255
3256 *outrow = (png_byte)back_i;
3257 }
3258
3259 inrow += 4;
3260 }
3261 break;
3262
3263 default:
3264 break;
3265 }
3266 }
3267 }
3268 }
3269
3270 return 1;
3271}
3272
3273static int
3274png_image_read_colormapped(png_voidp argument)
3275{
3276 png_image_read_control *display = png_voidcast(png_image_read_control*,
3277 argument);
3278 png_imagep image = display->image;
3279 png_controlp control = image->opaque;
3280 png_structrp png_ptr = control->png_ptr;
3281 png_inforp info_ptr = control->info_ptr;
3282
3283 int passes = 0; /* As a flag */
3284
3285 PNG_SKIP_CHUNKS(png_ptr);
3286
3287 /* Update the 'info' structure and make sure the result is as required; first
3288 * make sure to turn on the interlace handling if it will be required
3289 * (because it can't be turned on *after* the call to png_read_update_info!)
3290 */
3291 if (display->colormap_processing == PNG_CMAP_NONE)
3292 passes = png_set_interlace_handling(png_ptr);
3293
3294 png_read_update_info(png_ptr, info_ptr);
3295
3296 /* The expected output can be deduced from the colormap_processing option. */
3297 switch (display->colormap_processing)
3298 {
3299 case PNG_CMAP_NONE:
3300 /* Output must be one channel and one byte per pixel, the output
3301 * encoding can be anything.
3302 */
3303 if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
3304 info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
3305 info_ptr->bit_depth == 8)
3306 break;
3307
3308 goto bad_output;
3309
3310 case PNG_CMAP_TRANS:
3311 case PNG_CMAP_GA:
3312 /* Output must be two channels and the 'G' one must be sRGB, the latter
3313 * can be checked with an exact number because it should have been set
3314 * to this number above!
3315 */
3316 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
3317 info_ptr->bit_depth == 8 &&
3318 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3319 image->colormap_entries == 256)
3320 break;
3321
3322 goto bad_output;
3323
3324 case PNG_CMAP_RGB:
3325 /* Output must be 8-bit sRGB encoded RGB */
3326 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
3327 info_ptr->bit_depth == 8 &&
3328 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3329 image->colormap_entries == 216)
3330 break;
3331
3332 goto bad_output;
3333
3334 case PNG_CMAP_RGB_ALPHA:
3335 /* Output must be 8-bit sRGB encoded RGBA */
3336 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
3337 info_ptr->bit_depth == 8 &&
3338 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3339 image->colormap_entries == 244 /* 216 + 1 + 27 */)
3340 break;
3341
3342 /* goto bad_output; */
3343 /* FALL THROUGH */
3344
3345 default:
3346 bad_output:
3347 png_error(png_ptr, "bad color-map processing (internal error)");
3348 }
3349
3350 /* Now read the rows. Do this here if it is possible to read directly into
3351 * the output buffer, otherwise allocate a local row buffer of the maximum
3352 * size libpng requires and call the relevant processing routine safely.
3353 */
3354 {
3355 png_voidp first_row = display->buffer;
3356 ptrdiff_t row_bytes = display->row_stride;
3357
3358 /* The following expression is designed to work correctly whether it gives
3359 * a signed or an unsigned result.
3360 */
3361 if (row_bytes < 0)
3362 {
3363 char *ptr = png_voidcast(char*, first_row);
3364 ptr += (image->height-1) * (-row_bytes);
3365 first_row = png_voidcast(png_voidp, ptr);
3366 }
3367
3368 display->first_row = first_row;
3369 display->row_bytes = row_bytes;
3370 }
3371
3372 if (passes == 0)
3373 {
3374 int result;
3375 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3376
3377 display->local_row = row;
3378 result = png_safe_execute(image, png_image_read_and_map, display);
3379 display->local_row = NULL;
3380 png_free(png_ptr, row);
3381
3382 return result;
3383 }
3384
3385 else
3386 {
3387 png_alloc_size_t row_bytes = display->row_bytes;
3388
3389 while (--passes >= 0)
3390 {
3391 png_uint_32 y = image->height;
3392 png_bytep row = png_voidcast(png_bytep, display->first_row);
3393
3394 while (y-- > 0)
3395 {
3396 png_read_row(png_ptr, row, NULL);
3397 row += row_bytes;
3398 }
3399 }
3400
3401 return 1;
3402 }
3403}
3404
3405/* Just the row reading part of png_image_read. */
3406static int
3407png_image_read_composite(png_voidp argument)
3408{
3409 png_image_read_control *display = png_voidcast(png_image_read_control*,
3410 argument);
3411 png_imagep image = display->image;
3412 png_structrp png_ptr = image->opaque->png_ptr;
3413 int passes;
3414
3415 switch (png_ptr->interlaced)
3416 {
3417 case PNG_INTERLACE_NONE:
3418 passes = 1;
3419 break;
3420
3421 case PNG_INTERLACE_ADAM7:
3422 passes = PNG_INTERLACE_ADAM7_PASSES;
3423 break;
3424
3425 default:
Chris Craikb50c2172013-07-29 15:28:30 -07003426 png_error(png_ptr, "unknown interlace type");
3427 }
3428
3429 {
3430 png_uint_32 height = image->height;
3431 png_uint_32 width = image->width;
3432 ptrdiff_t step_row = display->row_bytes;
Matt Sarett9ea75692016-01-08 13:00:42 -05003433 unsigned int channels =
3434 (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
Chris Craikb50c2172013-07-29 15:28:30 -07003435 int pass;
3436
3437 for (pass = 0; pass < passes; ++pass)
3438 {
3439 unsigned int startx, stepx, stepy;
3440 png_uint_32 y;
3441
3442 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3443 {
3444 /* The row may be empty for a short image: */
3445 if (PNG_PASS_COLS(width, pass) == 0)
3446 continue;
3447
3448 startx = PNG_PASS_START_COL(pass) * channels;
3449 stepx = PNG_PASS_COL_OFFSET(pass) * channels;
3450 y = PNG_PASS_START_ROW(pass);
3451 stepy = PNG_PASS_ROW_OFFSET(pass);
3452 }
3453
3454 else
3455 {
3456 y = 0;
3457 startx = 0;
3458 stepx = channels;
3459 stepy = 1;
3460 }
3461
3462 for (; y<height; y += stepy)
3463 {
3464 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
3465 png_bytep outrow;
3466 png_const_bytep end_row;
3467
3468 /* Read the row, which is packed: */
3469 png_read_row(png_ptr, inrow, NULL);
3470
3471 outrow = png_voidcast(png_bytep, display->first_row);
3472 outrow += y * step_row;
3473 end_row = outrow + width * channels;
3474
3475 /* Now do the composition on each pixel in this row. */
3476 outrow += startx;
3477 for (; outrow < end_row; outrow += stepx)
3478 {
3479 png_byte alpha = inrow[channels];
3480
3481 if (alpha > 0) /* else no change to the output */
3482 {
3483 unsigned int c;
3484
3485 for (c=0; c<channels; ++c)
3486 {
3487 png_uint_32 component = inrow[c];
3488
3489 if (alpha < 255) /* else just use component */
3490 {
3491 /* This is PNG_OPTIMIZED_ALPHA, the component value
3492 * is a linear 8-bit value. Combine this with the
3493 * current outrow[c] value which is sRGB encoded.
3494 * Arithmetic here is 16-bits to preserve the output
3495 * values correctly.
3496 */
3497 component *= 257*255; /* =65535 */
3498 component += (255-alpha)*png_sRGB_table[outrow[c]];
3499
3500 /* So 'component' is scaled by 255*65535 and is
3501 * therefore appropriate for the sRGB to linear
3502 * conversion table.
3503 */
3504 component = PNG_sRGB_FROM_LINEAR(component);
3505 }
3506
3507 outrow[c] = (png_byte)component;
3508 }
3509 }
3510
3511 inrow += channels+1; /* components and alpha channel */
3512 }
3513 }
3514 }
3515 }
3516
3517 return 1;
3518}
3519
3520/* The do_local_background case; called when all the following transforms are to
3521 * be done:
3522 *
3523 * PNG_RGB_TO_GRAY
3524 * PNG_COMPOSITE
3525 * PNG_GAMMA
3526 *
Matt Sarett9ea75692016-01-08 13:00:42 -05003527 * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and
Chris Craikb50c2172013-07-29 15:28:30 -07003528 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
Matt Sarett9ea75692016-01-08 13:00:42 -05003529 * correction. The fix-up is to prevent the PNG_COMPOSITE operation from
3530 * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha
3531 * row and handles the removal or pre-multiplication of the alpha channel.
Chris Craikb50c2172013-07-29 15:28:30 -07003532 */
3533static int
3534png_image_read_background(png_voidp argument)
3535{
3536 png_image_read_control *display = png_voidcast(png_image_read_control*,
3537 argument);
3538 png_imagep image = display->image;
3539 png_structrp png_ptr = image->opaque->png_ptr;
3540 png_inforp info_ptr = image->opaque->info_ptr;
3541 png_uint_32 height = image->height;
3542 png_uint_32 width = image->width;
3543 int pass, passes;
3544
3545 /* Double check the convoluted logic below. We expect to get here with
3546 * libpng doing rgb to gray and gamma correction but background processing
3547 * left to the png_image_read_background function. The rows libpng produce
3548 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
3549 */
3550 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
3551 png_error(png_ptr, "lost rgb to gray");
3552
3553 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
3554 png_error(png_ptr, "unexpected compose");
3555
3556 if (png_get_channels(png_ptr, info_ptr) != 2)
3557 png_error(png_ptr, "lost/gained channels");
3558
3559 /* Expect the 8-bit case to always remove the alpha channel */
3560 if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
3561 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
3562 png_error(png_ptr, "unexpected 8-bit transformation");
3563
3564 switch (png_ptr->interlaced)
3565 {
3566 case PNG_INTERLACE_NONE:
3567 passes = 1;
3568 break;
3569
3570 case PNG_INTERLACE_ADAM7:
3571 passes = PNG_INTERLACE_ADAM7_PASSES;
3572 break;
3573
3574 default:
Chris Craikb50c2172013-07-29 15:28:30 -07003575 png_error(png_ptr, "unknown interlace type");
3576 }
3577
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303578 /* Use direct access to info_ptr here because otherwise the simplified API
3579 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
3580 * checking the value after libpng expansions, not the original value in the
3581 * PNG.
3582 */
3583 switch (info_ptr->bit_depth)
Chris Craikb50c2172013-07-29 15:28:30 -07003584 {
Chris Craikb50c2172013-07-29 15:28:30 -07003585 case 8:
3586 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303587 * to be removed by composing on a background: either the row if
Chris Craikb50c2172013-07-29 15:28:30 -07003588 * display->background is NULL or display->background->green if not.
3589 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3590 */
3591 {
3592 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3593 ptrdiff_t step_row = display->row_bytes;
3594
3595 for (pass = 0; pass < passes; ++pass)
3596 {
3597 png_bytep row = png_voidcast(png_bytep,
3598 display->first_row);
3599 unsigned int startx, stepx, stepy;
3600 png_uint_32 y;
3601
3602 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3603 {
3604 /* The row may be empty for a short image: */
3605 if (PNG_PASS_COLS(width, pass) == 0)
3606 continue;
3607
3608 startx = PNG_PASS_START_COL(pass);
3609 stepx = PNG_PASS_COL_OFFSET(pass);
3610 y = PNG_PASS_START_ROW(pass);
3611 stepy = PNG_PASS_ROW_OFFSET(pass);
3612 }
3613
3614 else
3615 {
3616 y = 0;
3617 startx = 0;
3618 stepx = stepy = 1;
3619 }
3620
3621 if (display->background == NULL)
3622 {
3623 for (; y<height; y += stepy)
3624 {
3625 png_bytep inrow = png_voidcast(png_bytep,
3626 display->local_row);
3627 png_bytep outrow = first_row + y * step_row;
3628 png_const_bytep end_row = outrow + width;
3629
3630 /* Read the row, which is packed: */
3631 png_read_row(png_ptr, inrow, NULL);
3632
3633 /* Now do the composition on each pixel in this row. */
3634 outrow += startx;
3635 for (; outrow < end_row; outrow += stepx)
3636 {
3637 png_byte alpha = inrow[1];
3638
3639 if (alpha > 0) /* else no change to the output */
3640 {
3641 png_uint_32 component = inrow[0];
3642
3643 if (alpha < 255) /* else just use component */
3644 {
3645 /* Since PNG_OPTIMIZED_ALPHA was not set it is
3646 * necessary to invert the sRGB transfer
3647 * function and multiply the alpha out.
3648 */
3649 component = png_sRGB_table[component] * alpha;
3650 component += png_sRGB_table[outrow[0]] *
3651 (255-alpha);
3652 component = PNG_sRGB_FROM_LINEAR(component);
3653 }
3654
3655 outrow[0] = (png_byte)component;
3656 }
3657
3658 inrow += 2; /* gray and alpha channel */
3659 }
3660 }
3661 }
3662
3663 else /* constant background value */
3664 {
3665 png_byte background8 = display->background->green;
3666 png_uint_16 background = png_sRGB_table[background8];
3667
3668 for (; y<height; y += stepy)
3669 {
3670 png_bytep inrow = png_voidcast(png_bytep,
3671 display->local_row);
3672 png_bytep outrow = first_row + y * step_row;
3673 png_const_bytep end_row = outrow + width;
3674
3675 /* Read the row, which is packed: */
3676 png_read_row(png_ptr, inrow, NULL);
3677
3678 /* Now do the composition on each pixel in this row. */
3679 outrow += startx;
3680 for (; outrow < end_row; outrow += stepx)
3681 {
3682 png_byte alpha = inrow[1];
3683
3684 if (alpha > 0) /* else use background */
3685 {
3686 png_uint_32 component = inrow[0];
3687
3688 if (alpha < 255) /* else just use component */
3689 {
3690 component = png_sRGB_table[component] * alpha;
3691 component += background * (255-alpha);
3692 component = PNG_sRGB_FROM_LINEAR(component);
3693 }
3694
3695 outrow[0] = (png_byte)component;
3696 }
3697
3698 else
3699 outrow[0] = background8;
3700
3701 inrow += 2; /* gray and alpha channel */
3702 }
3703
3704 row += display->row_bytes;
3705 }
3706 }
3707 }
3708 }
3709 break;
3710
3711 case 16:
3712 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3713 * still be done and, maybe, the alpha channel removed. This code also
3714 * handles the alpha-first option.
3715 */
3716 {
3717 png_uint_16p first_row = png_voidcast(png_uint_16p,
3718 display->first_row);
3719 /* The division by two is safe because the caller passed in a
3720 * stride which was multiplied by 2 (below) to get row_bytes.
3721 */
3722 ptrdiff_t step_row = display->row_bytes / 2;
3723 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
3724 unsigned int outchannels = 1+preserve_alpha;
3725 int swap_alpha = 0;
3726
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303727# ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -05003728 if (preserve_alpha != 0 &&
3729 (image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303730 swap_alpha = 1;
3731# endif
Chris Craikb50c2172013-07-29 15:28:30 -07003732
3733 for (pass = 0; pass < passes; ++pass)
3734 {
3735 unsigned int startx, stepx, stepy;
3736 png_uint_32 y;
3737
3738 /* The 'x' start and step are adjusted to output components here.
3739 */
3740 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3741 {
3742 /* The row may be empty for a short image: */
3743 if (PNG_PASS_COLS(width, pass) == 0)
3744 continue;
3745
3746 startx = PNG_PASS_START_COL(pass) * outchannels;
3747 stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
3748 y = PNG_PASS_START_ROW(pass);
3749 stepy = PNG_PASS_ROW_OFFSET(pass);
3750 }
3751
3752 else
3753 {
3754 y = 0;
3755 startx = 0;
3756 stepx = outchannels;
3757 stepy = 1;
3758 }
3759
3760 for (; y<height; y += stepy)
3761 {
3762 png_const_uint_16p inrow;
3763 png_uint_16p outrow = first_row + y*step_row;
3764 png_uint_16p end_row = outrow + width * outchannels;
3765
3766 /* Read the row, which is packed: */
3767 png_read_row(png_ptr, png_voidcast(png_bytep,
3768 display->local_row), NULL);
3769 inrow = png_voidcast(png_const_uint_16p, display->local_row);
3770
3771 /* Now do the pre-multiplication on each pixel in this row.
3772 */
3773 outrow += startx;
3774 for (; outrow < end_row; outrow += stepx)
3775 {
3776 png_uint_32 component = inrow[0];
3777 png_uint_16 alpha = inrow[1];
3778
3779 if (alpha > 0) /* else 0 */
3780 {
3781 if (alpha < 65535) /* else just use component */
3782 {
3783 component *= alpha;
3784 component += 32767;
3785 component /= 65535;
3786 }
3787 }
3788
3789 else
3790 component = 0;
3791
3792 outrow[swap_alpha] = (png_uint_16)component;
Matt Sarett9ea75692016-01-08 13:00:42 -05003793 if (preserve_alpha != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003794 outrow[1 ^ swap_alpha] = alpha;
3795
3796 inrow += 2; /* components and alpha channel */
3797 }
3798 }
3799 }
3800 }
3801 break;
Matt Sarett9ea75692016-01-08 13:00:42 -05003802
3803#ifdef __GNUC__
3804 default:
3805 png_error(png_ptr, "unexpected bit depth");
3806#endif
Chris Craikb50c2172013-07-29 15:28:30 -07003807 }
3808
3809 return 1;
3810}
3811
3812/* The guts of png_image_finish_read as a png_safe_execute callback. */
3813static int
3814png_image_read_direct(png_voidp argument)
3815{
3816 png_image_read_control *display = png_voidcast(png_image_read_control*,
3817 argument);
3818 png_imagep image = display->image;
3819 png_structrp png_ptr = image->opaque->png_ptr;
3820 png_inforp info_ptr = image->opaque->info_ptr;
3821
3822 png_uint_32 format = image->format;
3823 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
3824 int do_local_compose = 0;
3825 int do_local_background = 0; /* to avoid double gamma correction bug */
3826 int passes = 0;
3827
3828 /* Add transforms to ensure the correct output format is produced then check
3829 * that the required implementation support is there. Always expand; always
3830 * need 8 bits minimum, no palette and expanded tRNS.
3831 */
3832 png_set_expand(png_ptr);
3833
3834 /* Now check the format to see if it was modified. */
3835 {
3836 png_uint_32 base_format = png_image_format(png_ptr) &
3837 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
3838 png_uint_32 change = format ^ base_format;
3839 png_fixed_point output_gamma;
3840 int mode; /* alpha mode */
3841
3842 /* Do this first so that we have a record if rgb to gray is happening. */
Matt Sarett9ea75692016-01-08 13:00:42 -05003843 if ((change & PNG_FORMAT_FLAG_COLOR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003844 {
3845 /* gray<->color transformation required. */
Matt Sarett9ea75692016-01-08 13:00:42 -05003846 if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003847 png_set_gray_to_rgb(png_ptr);
3848
3849 else
3850 {
3851 /* libpng can't do both rgb to gray and
3852 * background/pre-multiplication if there is also significant gamma
3853 * correction, because both operations require linear colors and
3854 * the code only supports one transform doing the gamma correction.
3855 * Handle this by doing the pre-multiplication or background
3856 * operation in this code, if necessary.
3857 *
3858 * TODO: fix this by rewriting pngrtran.c (!)
3859 *
3860 * For the moment (given that fixing this in pngrtran.c is an
3861 * enormous change) 'do_local_background' is used to indicate that
3862 * the problem exists.
3863 */
Matt Sarett9ea75692016-01-08 13:00:42 -05003864 if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003865 do_local_background = 1/*maybe*/;
3866
3867 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
3868 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
3869 }
3870
3871 change &= ~PNG_FORMAT_FLAG_COLOR;
3872 }
3873
3874 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3875 */
3876 {
3877 png_fixed_point input_gamma_default;
3878
Matt Sarett9ea75692016-01-08 13:00:42 -05003879 if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 &&
3880 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003881 input_gamma_default = PNG_GAMMA_LINEAR;
3882 else
3883 input_gamma_default = PNG_DEFAULT_sRGB;
3884
3885 /* Call png_set_alpha_mode to set the default for the input gamma; the
3886 * output gamma is set by a second call below.
3887 */
3888 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
3889 }
3890
Matt Sarett9ea75692016-01-08 13:00:42 -05003891 if (linear != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003892 {
3893 /* If there *is* an alpha channel in the input it must be multiplied
3894 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3895 */
Matt Sarett9ea75692016-01-08 13:00:42 -05003896 if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003897 mode = PNG_ALPHA_STANDARD; /* associated alpha */
3898
3899 else
3900 mode = PNG_ALPHA_PNG;
3901
3902 output_gamma = PNG_GAMMA_LINEAR;
3903 }
3904
3905 else
3906 {
3907 mode = PNG_ALPHA_PNG;
3908 output_gamma = PNG_DEFAULT_sRGB;
3909 }
3910
3911 /* If 'do_local_background' is set check for the presence of gamma
3912 * correction; this is part of the work-round for the libpng bug
3913 * described above.
3914 *
3915 * TODO: fix libpng and remove this.
3916 */
Matt Sarett9ea75692016-01-08 13:00:42 -05003917 if (do_local_background != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003918 {
3919 png_fixed_point gtest;
3920
3921 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3922 * gamma correction, the screen gamma hasn't been set on png_struct
3923 * yet; it's set below. png_struct::gamma, however, is set to the
3924 * final value.
3925 */
3926 if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
Matt Sarett9ea75692016-01-08 13:00:42 -05003927 PNG_FP_1) != 0 && png_gamma_significant(gtest) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003928 do_local_background = 0;
3929
3930 else if (mode == PNG_ALPHA_STANDARD)
3931 {
3932 do_local_background = 2/*required*/;
3933 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
3934 }
3935
3936 /* else leave as 1 for the checks below */
3937 }
3938
3939 /* If the bit-depth changes then handle that here. */
Matt Sarett9ea75692016-01-08 13:00:42 -05003940 if ((change & PNG_FORMAT_FLAG_LINEAR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003941 {
Matt Sarett9ea75692016-01-08 13:00:42 -05003942 if (linear != 0 /*16-bit output*/)
Chris Craikb50c2172013-07-29 15:28:30 -07003943 png_set_expand_16(png_ptr);
3944
3945 else /* 8-bit output */
3946 png_set_scale_16(png_ptr);
3947
3948 change &= ~PNG_FORMAT_FLAG_LINEAR;
3949 }
3950
3951 /* Now the background/alpha channel changes. */
Matt Sarett9ea75692016-01-08 13:00:42 -05003952 if ((change & PNG_FORMAT_FLAG_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003953 {
3954 /* Removing an alpha channel requires composition for the 8-bit
3955 * formats; for the 16-bit it is already done, above, by the
3956 * pre-multiplication and the channel just needs to be stripped.
3957 */
Matt Sarett9ea75692016-01-08 13:00:42 -05003958 if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003959 {
3960 /* If RGB->gray is happening the alpha channel must be left and the
3961 * operation completed locally.
3962 *
3963 * TODO: fix libpng and remove this.
3964 */
Matt Sarett9ea75692016-01-08 13:00:42 -05003965 if (do_local_background != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07003966 do_local_background = 2/*required*/;
3967
3968 /* 16-bit output: just remove the channel */
Matt Sarett9ea75692016-01-08 13:00:42 -05003969 else if (linear != 0) /* compose on black (well, pre-multiply) */
Chris Craikb50c2172013-07-29 15:28:30 -07003970 png_set_strip_alpha(png_ptr);
3971
3972 /* 8-bit output: do an appropriate compose */
3973 else if (display->background != NULL)
3974 {
3975 png_color_16 c;
3976
3977 c.index = 0; /*unused*/
3978 c.red = display->background->red;
3979 c.green = display->background->green;
3980 c.blue = display->background->blue;
3981 c.gray = display->background->green;
3982
3983 /* This is always an 8-bit sRGB value, using the 'green' channel
3984 * for gray is much better than calculating the luminance here;
3985 * we can get off-by-one errors in that calculation relative to
3986 * the app expectations and that will show up in transparent
3987 * pixels.
3988 */
3989 png_set_background_fixed(png_ptr, &c,
3990 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
3991 0/*gamma: not used*/);
3992 }
3993
3994 else /* compose on row: implemented below. */
3995 {
3996 do_local_compose = 1;
3997 /* This leaves the alpha channel in the output, so it has to be
3998 * removed by the code below. Set the encoding to the 'OPTIMIZE'
3999 * one so the code only has to hack on the pixels that require
4000 * composition.
4001 */
4002 mode = PNG_ALPHA_OPTIMIZED;
4003 }
4004 }
4005
4006 else /* output needs an alpha channel */
4007 {
4008 /* This is tricky because it happens before the swap operation has
4009 * been accomplished; however, the swap does *not* swap the added
4010 * alpha channel (weird API), so it must be added in the correct
4011 * place.
4012 */
4013 png_uint_32 filler; /* opaque filler */
4014 int where;
4015
Matt Sarett9ea75692016-01-08 13:00:42 -05004016 if (linear != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004017 filler = 65535;
4018
4019 else
4020 filler = 255;
4021
4022# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -05004023 if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004024 {
4025 where = PNG_FILLER_BEFORE;
4026 change &= ~PNG_FORMAT_FLAG_AFIRST;
4027 }
4028
4029 else
4030# endif
4031 where = PNG_FILLER_AFTER;
4032
4033 png_set_add_alpha(png_ptr, filler, where);
4034 }
4035
4036 /* This stops the (irrelevant) call to swap_alpha below. */
4037 change &= ~PNG_FORMAT_FLAG_ALPHA;
4038 }
4039
4040 /* Now set the alpha mode correctly; this is always done, even if there is
4041 * no alpha channel in either the input or the output because it correctly
4042 * sets the output gamma.
4043 */
4044 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
4045
4046# ifdef PNG_FORMAT_BGR_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -05004047 if ((change & PNG_FORMAT_FLAG_BGR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004048 {
4049 /* Check only the output format; PNG is never BGR; don't do this if
4050 * the output is gray, but fix up the 'format' value in that case.
4051 */
Matt Sarett9ea75692016-01-08 13:00:42 -05004052 if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004053 png_set_bgr(png_ptr);
4054
4055 else
4056 format &= ~PNG_FORMAT_FLAG_BGR;
4057
4058 change &= ~PNG_FORMAT_FLAG_BGR;
4059 }
4060# endif
4061
4062# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -05004063 if ((change & PNG_FORMAT_FLAG_AFIRST) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004064 {
4065 /* Only relevant if there is an alpha channel - it's particularly
4066 * important to handle this correctly because do_local_compose may
4067 * be set above and then libpng will keep the alpha channel for this
4068 * code to remove.
4069 */
Matt Sarett9ea75692016-01-08 13:00:42 -05004070 if ((format & PNG_FORMAT_FLAG_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004071 {
4072 /* Disable this if doing a local background,
4073 * TODO: remove this when local background is no longer required.
4074 */
4075 if (do_local_background != 2)
4076 png_set_swap_alpha(png_ptr);
4077 }
4078
4079 else
4080 format &= ~PNG_FORMAT_FLAG_AFIRST;
4081
4082 change &= ~PNG_FORMAT_FLAG_AFIRST;
4083 }
4084# endif
4085
4086 /* If the *output* is 16-bit then we need to check for a byte-swap on this
4087 * architecture.
4088 */
Matt Sarett9ea75692016-01-08 13:00:42 -05004089 if (linear != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004090 {
4091 PNG_CONST png_uint_16 le = 0x0001;
4092
Matt Sarett9ea75692016-01-08 13:00:42 -05004093 if ((*(png_const_bytep) & le) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004094 png_set_swap(png_ptr);
4095 }
4096
4097 /* If change is not now 0 some transformation is missing - error out. */
Matt Sarett9ea75692016-01-08 13:00:42 -05004098 if (change != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004099 png_error(png_ptr, "png_read_image: unsupported transformation");
4100 }
4101
4102 PNG_SKIP_CHUNKS(png_ptr);
4103
4104 /* Update the 'info' structure and make sure the result is as required; first
4105 * make sure to turn on the interlace handling if it will be required
4106 * (because it can't be turned on *after* the call to png_read_update_info!)
4107 *
4108 * TODO: remove the do_local_background fixup below.
4109 */
Matt Sarett9ea75692016-01-08 13:00:42 -05004110 if (do_local_compose == 0 && do_local_background != 2)
Chris Craikb50c2172013-07-29 15:28:30 -07004111 passes = png_set_interlace_handling(png_ptr);
4112
4113 png_read_update_info(png_ptr, info_ptr);
4114
4115 {
4116 png_uint_32 info_format = 0;
4117
Matt Sarett9ea75692016-01-08 13:00:42 -05004118 if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004119 info_format |= PNG_FORMAT_FLAG_COLOR;
4120
Matt Sarett9ea75692016-01-08 13:00:42 -05004121 if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004122 {
4123 /* do_local_compose removes this channel below. */
Matt Sarett9ea75692016-01-08 13:00:42 -05004124 if (do_local_compose == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004125 {
4126 /* do_local_background does the same if required. */
4127 if (do_local_background != 2 ||
4128 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
4129 info_format |= PNG_FORMAT_FLAG_ALPHA;
4130 }
4131 }
4132
Matt Sarett9ea75692016-01-08 13:00:42 -05004133 else if (do_local_compose != 0) /* internal error */
Chris Craikb50c2172013-07-29 15:28:30 -07004134 png_error(png_ptr, "png_image_read: alpha channel lost");
4135
4136 if (info_ptr->bit_depth == 16)
4137 info_format |= PNG_FORMAT_FLAG_LINEAR;
4138
4139# ifdef PNG_FORMAT_BGR_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -05004140 if ((png_ptr->transformations & PNG_BGR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004141 info_format |= PNG_FORMAT_FLAG_BGR;
4142# endif
4143
4144# ifdef PNG_FORMAT_AFIRST_SUPPORTED
4145 if (do_local_background == 2)
4146 {
Matt Sarett9ea75692016-01-08 13:00:42 -05004147 if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004148 info_format |= PNG_FORMAT_FLAG_AFIRST;
4149 }
4150
4151 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
4152 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
4153 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
4154 {
4155 if (do_local_background == 2)
4156 png_error(png_ptr, "unexpected alpha swap transformation");
4157
4158 info_format |= PNG_FORMAT_FLAG_AFIRST;
4159 }
4160# endif
4161
4162 /* This is actually an internal error. */
4163 if (info_format != format)
4164 png_error(png_ptr, "png_read_image: invalid transformations");
4165 }
4166
4167 /* Now read the rows. If do_local_compose is set then it is necessary to use
4168 * a local row buffer. The output will be GA, RGBA or BGRA and must be
4169 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
4170 * display acts as a flag.
4171 */
4172 {
4173 png_voidp first_row = display->buffer;
4174 ptrdiff_t row_bytes = display->row_stride;
4175
Matt Sarett9ea75692016-01-08 13:00:42 -05004176 if (linear != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004177 row_bytes *= 2;
4178
4179 /* The following expression is designed to work correctly whether it gives
4180 * a signed or an unsigned result.
4181 */
4182 if (row_bytes < 0)
4183 {
4184 char *ptr = png_voidcast(char*, first_row);
4185 ptr += (image->height-1) * (-row_bytes);
4186 first_row = png_voidcast(png_voidp, ptr);
4187 }
4188
4189 display->first_row = first_row;
4190 display->row_bytes = row_bytes;
4191 }
4192
Matt Sarett9ea75692016-01-08 13:00:42 -05004193 if (do_local_compose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004194 {
4195 int result;
4196 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
4197
4198 display->local_row = row;
4199 result = png_safe_execute(image, png_image_read_composite, display);
4200 display->local_row = NULL;
4201 png_free(png_ptr, row);
4202
4203 return result;
4204 }
4205
4206 else if (do_local_background == 2)
4207 {
4208 int result;
4209 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
4210
4211 display->local_row = row;
4212 result = png_safe_execute(image, png_image_read_background, display);
4213 display->local_row = NULL;
4214 png_free(png_ptr, row);
4215
4216 return result;
4217 }
4218
4219 else
4220 {
4221 png_alloc_size_t row_bytes = display->row_bytes;
4222
4223 while (--passes >= 0)
4224 {
4225 png_uint_32 y = image->height;
4226 png_bytep row = png_voidcast(png_bytep, display->first_row);
4227
4228 while (y-- > 0)
4229 {
4230 png_read_row(png_ptr, row, NULL);
4231 row += row_bytes;
4232 }
4233 }
4234
4235 return 1;
4236 }
4237}
4238
4239int PNGAPI
4240png_image_finish_read(png_imagep image, png_const_colorp background,
4241 void *buffer, png_int_32 row_stride, void *colormap)
4242{
4243 if (image != NULL && image->version == PNG_IMAGE_VERSION)
4244 {
4245 png_uint_32 check;
4246
4247 if (row_stride == 0)
4248 row_stride = PNG_IMAGE_ROW_STRIDE(*image);
4249
4250 if (row_stride < 0)
4251 check = -row_stride;
4252
4253 else
4254 check = row_stride;
4255
4256 if (image->opaque != NULL && buffer != NULL &&
4257 check >= PNG_IMAGE_ROW_STRIDE(*image))
4258 {
4259 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
4260 (image->colormap_entries > 0 && colormap != NULL))
4261 {
4262 int result;
4263 png_image_read_control display;
4264
4265 memset(&display, 0, (sizeof display));
4266 display.image = image;
4267 display.buffer = buffer;
4268 display.row_stride = row_stride;
4269 display.colormap = colormap;
4270 display.background = background;
4271 display.local_row = NULL;
4272
4273 /* Choose the correct 'end' routine; for the color-map case all the
4274 * setup has already been done.
4275 */
Matt Sarett9ea75692016-01-08 13:00:42 -05004276 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07004277 result =
4278 png_safe_execute(image, png_image_read_colormap, &display) &&
4279 png_safe_execute(image, png_image_read_colormapped, &display);
4280
4281 else
4282 result =
4283 png_safe_execute(image, png_image_read_direct, &display);
4284
4285 png_image_free(image);
4286 return result;
4287 }
4288
4289 else
4290 return png_image_error(image,
4291 "png_image_finish_read[color-map]: no color-map");
4292 }
4293
4294 else
4295 return png_image_error(image,
4296 "png_image_finish_read: invalid argument");
4297 }
4298
4299 else if (image != NULL)
4300 return png_image_error(image,
4301 "png_image_finish_read: damaged PNG_IMAGE_VERSION");
4302
4303 return 0;
4304}
4305
Matt Sarett9ea75692016-01-08 13:00:42 -05004306#endif /* SIMPLIFIED_READ */
4307#endif /* READ */