blob: 2a6ffbe982da093d640bc35cf84d143e66c6c8e3 [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* pngread.c - read a PNG file
3 *
Sireesh Tripurarib478e662014-05-09 15:15:10 +05304 * Last changed in libpng 1.6.10 [March 6, 2014]
5 * Copyright (c) 1998-2014 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);
The Android Open Source Project893912b2009-03-03 19:30:05 -080047#endif /* PNG_USER_MEM_SUPPORTED */
48
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 */
66# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
67 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 {
120 if (!(png_ptr->mode & PNG_HAVE_IHDR))
121 png_chunk_error(png_ptr, "Missing IHDR before IDAT");
122
123 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
124 !(png_ptr->mode & PNG_HAVE_PLTE))
125 png_chunk_error(png_ptr, "Missing PLTE before IDAT");
126
127 else if (png_ptr->mode & PNG_AFTER_IDAT)
128 png_chunk_benign_error(png_ptr, "Too many IDATs found");
129
130 png_ptr->mode |= PNG_HAVE_IDAT;
131 }
132
133 else if (png_ptr->mode & PNG_HAVE_IDAT)
134 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}
Patrick Scott5f6bd842010-06-28 16:55:16 -0400259#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
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}
Patrick Scott5f6bd842010-06-28 16:55:16 -0400315#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
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 (
328 (row_info->color_type & PNG_COLOR_MASK_COLOR))
329 {
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}
382#endif /* PNG_MNG_FEATURES_SUPPORTED */
383
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 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800398 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
399 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
The Android Open Source Project893912b2009-03-03 19:30:05 -0800409 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
410 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400411 /* Check for transforms that have been set but were defined out */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800412#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
413 if (png_ptr->transformations & PNG_INVERT_MONO)
Chris Craikb50c2172013-07-29 15:28:30 -0700414 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800415#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700416
The Android Open Source Project893912b2009-03-03 19:30:05 -0800417#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
418 if (png_ptr->transformations & PNG_FILLER)
Chris Craikb50c2172013-07-29 15:28:30 -0700419 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800420#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700421
Patrick Scott5f6bd842010-06-28 16:55:16 -0400422#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
423 !defined(PNG_READ_PACKSWAP_SUPPORTED)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800424 if (png_ptr->transformations & PNG_PACKSWAP)
Chris Craikb50c2172013-07-29 15:28:30 -0700425 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800426#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700427
The Android Open Source Project893912b2009-03-03 19:30:05 -0800428#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
429 if (png_ptr->transformations & PNG_PACK)
Chris Craikb50c2172013-07-29 15:28:30 -0700430 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800431#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700432
The Android Open Source Project893912b2009-03-03 19:30:05 -0800433#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
434 if (png_ptr->transformations & PNG_SHIFT)
Chris Craikb50c2172013-07-29 15:28:30 -0700435 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800436#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700437
The Android Open Source Project893912b2009-03-03 19:30:05 -0800438#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
439 if (png_ptr->transformations & PNG_BGR)
Chris Craikb50c2172013-07-29 15:28:30 -0700440 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800441#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700442
The Android Open Source Project893912b2009-03-03 19:30:05 -0800443#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
444 if (png_ptr->transformations & PNG_SWAP_BYTES)
Chris Craikb50c2172013-07-29 15:28:30 -0700445 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800446#endif
447 }
448
Patrick Scott5f6bd842010-06-28 16:55:16 -0400449#ifdef PNG_READ_INTERLACING_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700450 /* If interlaced and we do not need a new row, combine row and return.
451 * Notice that the pixels we have from previous rows have been transformed
452 * already; we can only combine like with like (transformed or
453 * untransformed) and, because of the libpng API for interlaced images, this
454 * means we must transform before de-interlacing.
455 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800456 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
457 {
458 switch (png_ptr->pass)
459 {
460 case 0:
461 if (png_ptr->row_number & 0x07)
462 {
463 if (dsp_row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700464 png_combine_row(png_ptr, dsp_row, 1/*display*/);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800465 png_read_finish_row(png_ptr);
466 return;
467 }
468 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700469
The Android Open Source Project893912b2009-03-03 19:30:05 -0800470 case 1:
471 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
472 {
473 if (dsp_row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700474 png_combine_row(png_ptr, dsp_row, 1/*display*/);
475
The Android Open Source Project893912b2009-03-03 19:30:05 -0800476 png_read_finish_row(png_ptr);
477 return;
478 }
479 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700480
The Android Open Source Project893912b2009-03-03 19:30:05 -0800481 case 2:
482 if ((png_ptr->row_number & 0x07) != 4)
483 {
484 if (dsp_row != NULL && (png_ptr->row_number & 4))
Chris Craikb50c2172013-07-29 15:28:30 -0700485 png_combine_row(png_ptr, dsp_row, 1/*display*/);
486
The Android Open Source Project893912b2009-03-03 19:30:05 -0800487 png_read_finish_row(png_ptr);
488 return;
489 }
490 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700491
The Android Open Source Project893912b2009-03-03 19:30:05 -0800492 case 3:
493 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
494 {
495 if (dsp_row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700496 png_combine_row(png_ptr, dsp_row, 1/*display*/);
497
The Android Open Source Project893912b2009-03-03 19:30:05 -0800498 png_read_finish_row(png_ptr);
499 return;
500 }
501 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700502
The Android Open Source Project893912b2009-03-03 19:30:05 -0800503 case 4:
504 if ((png_ptr->row_number & 3) != 2)
505 {
506 if (dsp_row != NULL && (png_ptr->row_number & 2))
Chris Craikb50c2172013-07-29 15:28:30 -0700507 png_combine_row(png_ptr, dsp_row, 1/*display*/);
508
The Android Open Source Project893912b2009-03-03 19:30:05 -0800509 png_read_finish_row(png_ptr);
510 return;
511 }
512 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700513
The Android Open Source Project893912b2009-03-03 19:30:05 -0800514 case 5:
515 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
516 {
517 if (dsp_row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700518 png_combine_row(png_ptr, dsp_row, 1/*display*/);
519
The Android Open Source Project893912b2009-03-03 19:30:05 -0800520 png_read_finish_row(png_ptr);
521 return;
522 }
523 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700524
525 default:
The Android Open Source Project893912b2009-03-03 19:30:05 -0800526 case 6:
527 if (!(png_ptr->row_number & 1))
528 {
529 png_read_finish_row(png_ptr);
530 return;
531 }
532 break;
533 }
534 }
535#endif
536
537 if (!(png_ptr->mode & PNG_HAVE_IDAT))
538 png_error(png_ptr, "Invalid attempt to read row data");
539
Chris Craikb50c2172013-07-29 15:28:30 -0700540 /* Fill the row with IDAT data: */
541 png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
542
543 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800544 {
Chris Craikb50c2172013-07-29 15:28:30 -0700545 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
546 png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
547 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
548 else
549 png_error(png_ptr, "bad adaptive filter value");
550 }
Joseph Wen4ce0ee12010-08-20 10:42:22 +0800551
Chris Craikb50c2172013-07-29 15:28:30 -0700552 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
553 * 1.5.6, while the buffer really is this big in current versions of libpng
554 * it may not be in the future, so this was changed just to copy the
555 * interlaced count:
556 */
557 memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800558
Patrick Scott5f6bd842010-06-28 16:55:16 -0400559#ifdef PNG_MNG_FEATURES_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700560 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Chris Craikb50c2172013-07-29 15:28:30 -0700561 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800562 {
563 /* Intrapixel differencing */
Chris Craikb50c2172013-07-29 15:28:30 -0700564 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800565 }
566#endif
567
Chris Craikb50c2172013-07-29 15:28:30 -0700568#ifdef PNG_READ_TRANSFORMS_SUPPORTED
569 if (png_ptr->transformations)
570 png_do_read_transformations(png_ptr, &row_info);
571#endif
572
573 /* The transformed pixel depth should match the depth now in row_info. */
574 if (png_ptr->transformed_pixel_depth == 0)
575 {
576 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
577 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
578 png_error(png_ptr, "sequential row overflow");
579 }
580
581 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
582 png_error(png_ptr, "internal sequential row size calculation error");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800583
Patrick Scott5f6bd842010-06-28 16:55:16 -0400584#ifdef PNG_READ_INTERLACING_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400585 /* Blow up interlaced rows to full size */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800586 if (png_ptr->interlaced &&
587 (png_ptr->transformations & PNG_INTERLACE))
588 {
589 if (png_ptr->pass < 6)
Chris Craikb50c2172013-07-29 15:28:30 -0700590 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
591 png_ptr->transformations);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800592
593 if (dsp_row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700594 png_combine_row(png_ptr, dsp_row, 1/*display*/);
595
The Android Open Source Project893912b2009-03-03 19:30:05 -0800596 if (row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700597 png_combine_row(png_ptr, row, 0/*row*/);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800598 }
Chris Craikb50c2172013-07-29 15:28:30 -0700599
The Android Open Source Project893912b2009-03-03 19:30:05 -0800600 else
601#endif
602 {
603 if (row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700604 png_combine_row(png_ptr, row, -1/*ignored*/);
605
The Android Open Source Project893912b2009-03-03 19:30:05 -0800606 if (dsp_row != NULL)
Chris Craikb50c2172013-07-29 15:28:30 -0700607 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800608 }
609 png_read_finish_row(png_ptr);
610
611 if (png_ptr->read_row_fn != NULL)
612 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Chris Craikb50c2172013-07-29 15:28:30 -0700613
The Android Open Source Project893912b2009-03-03 19:30:05 -0800614}
Patrick Scott5f6bd842010-06-28 16:55:16 -0400615#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800616
Patrick Scott5f6bd842010-06-28 16:55:16 -0400617#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800618/* Read one or more rows of image data. If the image is interlaced,
619 * and png_set_interlace_handling() has been called, the rows need to
620 * contain the contents of the rows from the previous pass. If the
621 * image has alpha or transparency, and png_handle_alpha()[*] has been
622 * called, the rows contents must be initialized to the contents of the
623 * screen.
624 *
625 * "row" holds the actual image, and pixels are placed in it
626 * as they arrive. If the image is displayed after each pass, it will
627 * appear to "sparkle" in. "display_row" can be used to display a
628 * "chunky" progressive image, with finer detail added as it becomes
629 * available. If you do not want this "chunky" display, you may pass
630 * NULL for display_row. If you do not want the sparkle display, and
631 * you have not called png_handle_alpha(), you may pass NULL for rows.
632 * If you have called png_handle_alpha(), and the image has either an
633 * alpha channel or a transparency chunk, you must provide a buffer for
634 * rows. In this case, you do not have to provide a display_row buffer
635 * also, but you may. If the image is not interlaced, or if you have
636 * not called png_set_interlace_handling(), the display_row buffer will
637 * be ignored, so pass NULL to it.
638 *
639 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
640 */
641
642void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700643png_read_rows(png_structrp png_ptr, png_bytepp row,
644 png_bytepp display_row, png_uint_32 num_rows)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800645{
646 png_uint_32 i;
647 png_bytepp rp;
648 png_bytepp dp;
649
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700650 png_debug(1, "in png_read_rows");
Chris Craikb50c2172013-07-29 15:28:30 -0700651
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400652 if (png_ptr == NULL)
653 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700654
The Android Open Source Project893912b2009-03-03 19:30:05 -0800655 rp = row;
656 dp = display_row;
657 if (rp != NULL && dp != NULL)
658 for (i = 0; i < num_rows; i++)
659 {
660 png_bytep rptr = *rp++;
661 png_bytep dptr = *dp++;
662
663 png_read_row(png_ptr, rptr, dptr);
664 }
Chris Craikb50c2172013-07-29 15:28:30 -0700665
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700666 else if (rp != NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800667 for (i = 0; i < num_rows; i++)
668 {
669 png_bytep rptr = *rp;
Chris Craikb50c2172013-07-29 15:28:30 -0700670 png_read_row(png_ptr, rptr, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800671 rp++;
672 }
Chris Craikb50c2172013-07-29 15:28:30 -0700673
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700674 else if (dp != NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800675 for (i = 0; i < num_rows; i++)
676 {
677 png_bytep dptr = *dp;
Chris Craikb50c2172013-07-29 15:28:30 -0700678 png_read_row(png_ptr, NULL, dptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800679 dp++;
680 }
681}
Patrick Scott5f6bd842010-06-28 16:55:16 -0400682#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800683
Sireesh Tripurarib0277d02014-05-09 15:39:12 +0530684#ifdef PNG_INDEX_SUPPORTED
685#define IDAT_HEADER_SIZE 8
686
687/* Set the png read position to a new position based on idat_position and
688 * offset.
689 */
690void
691png_set_read_offset(png_structp png_ptr,
692 png_uint_32 idat_position, png_uint_32 bytes_left)
693{
694 png_seek_data(png_ptr, idat_position);
695 png_ptr->idat_size = png_read_chunk_header(png_ptr);
696
697 // We need to add back IDAT_HEADER_SIZE because in zlib's perspective,
698 // IDAT_HEADER in PNG is already stripped out.
699 png_seek_data(png_ptr, idat_position + IDAT_HEADER_SIZE + png_ptr->idat_size - bytes_left);
700 png_ptr->idat_size = bytes_left;
701}
702
703/* Configure png decoder to decode the pass starting from *row.
704 * The requested row may be adjusted to align with an indexing row.
705 * The actual row for the decoder to start its decoding will be returned in
706 * *row.
707 */
708void PNGAPI
709png_configure_decoder(png_structp png_ptr, int *row, int pass)
710{
711 png_indexp index = png_ptr->index;
712 int n = *row / index->step[pass];
713 png_line_indexp line_index = index->pass_line_index[pass][n];
714
715 // Adjust row to an indexing row.
716 *row = n * index->step[pass];
717 png_ptr->row_number = *row;
718
719#ifdef PNG_READ_INTERLACING_SUPPORTED
720 if (png_ptr->interlaced)
721 png_set_interlaced_pass(png_ptr, pass);
722#endif
723
724 long row_byte_length =
725 PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
726
727 inflateEnd(&png_ptr->zstream);
728 inflateCopy(&png_ptr->zstream, line_index->z_state);
729
730 // Set the png read position to line_index.
731 png_set_read_offset(png_ptr, line_index->stream_idat_position,
732 line_index->bytes_left_in_idat);
733 memcpy(png_ptr->prev_row, line_index->prev_row, row_byte_length);
734 png_ptr->zstream.avail_in = 0;
735}
736
737/* Build the line index and store the index in png_ptr->index.
738 */
739void PNGAPI
740png_build_index(png_structp png_ptr)
741{
742 // number of rows in a 8x8 block for each interlaced pass.
743 int number_rows_in_pass[7] = {1, 1, 1, 2, 2, 4, 4};
744
745 int ret;
746 png_uint_32 i, j;
747 png_bytep rp;
748 int p, pass_number = 1;
749
750#ifdef PNG_READ_INTERLACING_SUPPORTED
751 pass_number = png_set_interlace_handling(png_ptr);
752#endif
753
754 if (png_ptr == NULL)
755 return;
756
757 png_read_start_row(png_ptr);
758
759#ifdef PNG_READ_INTERLACING_SUPPORTED
760 if (!png_ptr->interlaced)
761#endif
762 {
763 number_rows_in_pass[0] = 8;
764 }
765
766 rp = png_malloc(png_ptr, png_ptr->rowbytes);
767
768 png_indexp index = png_malloc(png_ptr, sizeof(png_index));
769 png_ptr->index = index;
770
771 index->stream_idat_position = png_ptr->total_data_read - IDAT_HEADER_SIZE;
772
773 // Set the default size of index in each pass to 0,
774 // so that we can free index correctly in png_destroy_read_struct.
775 for (p = 0; p < 7; p++)
776 index->size[p] = 0;
777
778 for (p = 0; p < pass_number; p++)
779 {
780 // We adjust the index step in each pass to make sure each pass
781 // has roughly the same size of index.
782 // This way, we won't consume to much memory in recording index.
783 index->step[p] = INDEX_SAMPLE_SIZE * (8 / number_rows_in_pass[p]);
784 index->size[p] =
785 (png_ptr->height + index->step[p] - 1) / index->step[p];
786 index->pass_line_index[p] =
787 png_malloc(png_ptr, index->size[p] * sizeof(png_line_indexp));
788
789 // Get the row_byte_length seen by the filter. This value may be
790 // different from the row_byte_length of a bitmap in the case of
791 // color palette mode.
792 int row_byte_length =
793 PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
794
795 // Now, we record index for each indexing row.
796 for (i = 0; i < index->size[p]; i++)
797 {
798 png_line_indexp line_index = png_malloc(png_ptr, sizeof(png_line_index));
799 index->pass_line_index[p][i] = line_index;
800
801 line_index->z_state = png_malloc(png_ptr, sizeof(z_stream));
802 inflateCopy(line_index->z_state, &png_ptr->zstream);
803 line_index->prev_row = png_malloc(png_ptr, row_byte_length);
804 memcpy(line_index->prev_row, png_ptr->prev_row, row_byte_length);
805 line_index->stream_idat_position = index->stream_idat_position;
806 line_index->bytes_left_in_idat = png_ptr->idat_size + png_ptr->zstream.avail_in;
807
808 // Skip the "step" number of rows to the next indexing row.
809 for (j = 0; j < index->step[p] &&
810 i * index->step[p] + j < png_ptr->height; j++)
811 {
812 png_read_row(png_ptr, rp, NULL);
813 }
814 }
815 }
816 png_free(png_ptr, rp);
817}
818#endif
819
Patrick Scott5f6bd842010-06-28 16:55:16 -0400820#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800821/* Read the entire image. If the image has an alpha channel or a tRNS
822 * chunk, and you have called png_handle_alpha()[*], you will need to
823 * initialize the image to the current image that PNG will be overlaying.
824 * We set the num_rows again here, in case it was incorrectly set in
825 * png_read_start_row() by a call to png_read_update_info() or
826 * png_start_read_image() if png_set_interlace_handling() wasn't called
827 * prior to either of these functions like it should have been. You can
828 * only call this function once. If you desire to have an image for
829 * each pass of a interlaced image, use png_read_rows() instead.
830 *
831 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
832 */
833void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700834png_read_image(png_structrp png_ptr, png_bytepp image)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800835{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700836 png_uint_32 i, image_height;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800837 int pass, j;
838 png_bytepp rp;
839
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700840 png_debug(1, "in png_read_image");
Chris Craikb50c2172013-07-29 15:28:30 -0700841
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400842 if (png_ptr == NULL)
843 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800844
845#ifdef PNG_READ_INTERLACING_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700846 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
847 {
848 pass = png_set_interlace_handling(png_ptr);
849 /* And make sure transforms are initialized. */
850 png_start_read_image(png_ptr);
851 }
852 else
853 {
854 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
855 {
856 /* Caller called png_start_read_image or png_read_update_info without
857 * first turning on the PNG_INTERLACE transform. We can fix this here,
858 * but the caller should do it!
859 */
860 png_warning(png_ptr, "Interlace handling should be turned on when "
861 "using png_read_image");
862 /* Make sure this is set correctly */
863 png_ptr->num_rows = png_ptr->height;
864 }
865
866 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
867 * the above error case.
868 */
869 pass = png_set_interlace_handling(png_ptr);
870 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800871#else
872 if (png_ptr->interlaced)
873 png_error(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -0700874 "Cannot read interlaced image -- interlace handler disabled");
875
The Android Open Source Project893912b2009-03-03 19:30:05 -0800876 pass = 1;
877#endif
878
The Android Open Source Project893912b2009-03-03 19:30:05 -0800879 image_height=png_ptr->height;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800880
881 for (j = 0; j < pass; j++)
882 {
883 rp = image;
884 for (i = 0; i < image_height; i++)
885 {
Chris Craikb50c2172013-07-29 15:28:30 -0700886 png_read_row(png_ptr, *rp, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800887 rp++;
888 }
889 }
890}
Patrick Scott5f6bd842010-06-28 16:55:16 -0400891#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800892
Patrick Scott5f6bd842010-06-28 16:55:16 -0400893#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800894/* Read the end of the PNG file. Will not read past the end of the
895 * file, will verify the end is accurate, and will read any comments
896 * or time information at the end of the file, if info is not NULL.
897 */
898void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700899png_read_end(png_structrp png_ptr, png_inforp info_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800900{
Chris Craikb50c2172013-07-29 15:28:30 -0700901#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
902 int keep;
903#endif
904
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700905 png_debug(1, "in png_read_end");
Chris Craikb50c2172013-07-29 15:28:30 -0700906
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400907 if (png_ptr == NULL)
908 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700909
910 /* If png_read_end is called in the middle of reading the rows there may
911 * still be pending IDAT data and an owned zstream. Deal with this here.
912 */
913#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
914 if (!png_chunk_unknown_handling(png_ptr, png_IDAT))
915#endif
916 png_read_finish_IDAT(png_ptr);
917
918#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
919 /* Report invalid palette index; added at libng-1.5.10 */
920 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
921 png_ptr->num_palette_max > png_ptr->num_palette)
922 png_benign_error(png_ptr, "Read palette index exceeding num_palette");
923#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800924
925 do
926 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700927 png_uint_32 length = png_read_chunk_header(png_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -0700928 png_uint_32 chunk_name = png_ptr->chunk_name;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800929
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530930 if (chunk_name == png_IEND)
931 png_handle_IEND(png_ptr, info_ptr, length);
932
933 else if (chunk_name == png_IHDR)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800934 png_handle_IHDR(png_ptr, info_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700935
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530936 else if (info_ptr == NULL)
937 png_crc_finish(png_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700938
The Android Open Source Project893912b2009-03-03 19:30:05 -0800939#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700940 else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800941 {
Chris Craikb50c2172013-07-29 15:28:30 -0700942 if (chunk_name == png_IDAT)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800943 {
944 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Chris Craikb50c2172013-07-29 15:28:30 -0700945 png_benign_error(png_ptr, "Too many IDATs found");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800946 }
Chris Craikb50c2172013-07-29 15:28:30 -0700947 png_handle_unknown(png_ptr, info_ptr, length, keep);
948 if (chunk_name == png_PLTE)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800949 png_ptr->mode |= PNG_HAVE_PLTE;
950 }
951#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700952
953 else if (chunk_name == png_IDAT)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800954 {
955 /* Zero length IDATs are legal after the last IDAT has been
956 * read, but not after other chunks have been read.
957 */
958 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
Chris Craikb50c2172013-07-29 15:28:30 -0700959 png_benign_error(png_ptr, "Too many IDATs found");
960
The Android Open Source Project893912b2009-03-03 19:30:05 -0800961 png_crc_finish(png_ptr, length);
962 }
Chris Craikb50c2172013-07-29 15:28:30 -0700963 else if (chunk_name == png_PLTE)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800964 png_handle_PLTE(png_ptr, info_ptr, length);
Chris Craikb50c2172013-07-29 15:28:30 -0700965
Patrick Scott5f6bd842010-06-28 16:55:16 -0400966#ifdef PNG_READ_bKGD_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700967 else if (chunk_name == png_bKGD)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800968 png_handle_bKGD(png_ptr, info_ptr, length);
969#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700970
Patrick Scott5f6bd842010-06-28 16:55:16 -0400971#ifdef PNG_READ_cHRM_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700972 else if (chunk_name == png_cHRM)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800973 png_handle_cHRM(png_ptr, info_ptr, length);
974#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700975
Patrick Scott5f6bd842010-06-28 16:55:16 -0400976#ifdef PNG_READ_gAMA_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700977 else if (chunk_name == png_gAMA)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800978 png_handle_gAMA(png_ptr, info_ptr, length);
979#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700980
Patrick Scott5f6bd842010-06-28 16:55:16 -0400981#ifdef PNG_READ_hIST_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700982 else if (chunk_name == png_hIST)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800983 png_handle_hIST(png_ptr, info_ptr, length);
984#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700985
Patrick Scott5f6bd842010-06-28 16:55:16 -0400986#ifdef PNG_READ_oFFs_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700987 else if (chunk_name == png_oFFs)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800988 png_handle_oFFs(png_ptr, info_ptr, length);
989#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700990
Patrick Scott5f6bd842010-06-28 16:55:16 -0400991#ifdef PNG_READ_pCAL_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700992 else if (chunk_name == png_pCAL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800993 png_handle_pCAL(png_ptr, info_ptr, length);
994#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700995
Patrick Scott5f6bd842010-06-28 16:55:16 -0400996#ifdef PNG_READ_sCAL_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700997 else if (chunk_name == png_sCAL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800998 png_handle_sCAL(png_ptr, info_ptr, length);
999#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001000
Patrick Scott5f6bd842010-06-28 16:55:16 -04001001#ifdef PNG_READ_pHYs_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001002 else if (chunk_name == png_pHYs)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001003 png_handle_pHYs(png_ptr, info_ptr, length);
1004#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001005
Patrick Scott5f6bd842010-06-28 16:55:16 -04001006#ifdef PNG_READ_sBIT_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001007 else if (chunk_name == png_sBIT)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001008 png_handle_sBIT(png_ptr, info_ptr, length);
1009#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001010
Patrick Scott5f6bd842010-06-28 16:55:16 -04001011#ifdef PNG_READ_sRGB_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001012 else if (chunk_name == png_sRGB)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001013 png_handle_sRGB(png_ptr, info_ptr, length);
1014#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001015
Patrick Scott5f6bd842010-06-28 16:55:16 -04001016#ifdef PNG_READ_iCCP_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001017 else if (chunk_name == png_iCCP)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001018 png_handle_iCCP(png_ptr, info_ptr, length);
1019#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001020
Patrick Scott5f6bd842010-06-28 16:55:16 -04001021#ifdef PNG_READ_sPLT_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001022 else if (chunk_name == png_sPLT)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001023 png_handle_sPLT(png_ptr, info_ptr, length);
1024#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001025
Patrick Scott5f6bd842010-06-28 16:55:16 -04001026#ifdef PNG_READ_tEXt_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001027 else if (chunk_name == png_tEXt)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001028 png_handle_tEXt(png_ptr, info_ptr, length);
1029#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001030
Patrick Scott5f6bd842010-06-28 16:55:16 -04001031#ifdef PNG_READ_tIME_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001032 else if (chunk_name == png_tIME)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001033 png_handle_tIME(png_ptr, info_ptr, length);
1034#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001035
Patrick Scott5f6bd842010-06-28 16:55:16 -04001036#ifdef PNG_READ_tRNS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001037 else if (chunk_name == png_tRNS)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001038 png_handle_tRNS(png_ptr, info_ptr, length);
1039#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001040
Patrick Scott5f6bd842010-06-28 16:55:16 -04001041#ifdef PNG_READ_zTXt_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001042 else if (chunk_name == png_zTXt)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001043 png_handle_zTXt(png_ptr, info_ptr, length);
1044#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001045
Patrick Scott5f6bd842010-06-28 16:55:16 -04001046#ifdef PNG_READ_iTXt_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001047 else if (chunk_name == png_iTXt)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001048 png_handle_iTXt(png_ptr, info_ptr, length);
1049#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001050
The Android Open Source Project893912b2009-03-03 19:30:05 -08001051 else
Chris Craikb50c2172013-07-29 15:28:30 -07001052 png_handle_unknown(png_ptr, info_ptr, length,
1053 PNG_HANDLE_CHUNK_AS_DEFAULT);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001054 } while (!(png_ptr->mode & PNG_HAVE_IEND));
1055}
Patrick Scott5f6bd842010-06-28 16:55:16 -04001056#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001057
Chris Craikb50c2172013-07-29 15:28:30 -07001058/* Free all memory used in the read struct */
1059static void
1060png_read_destroy(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001061{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001062 png_debug(1, "in png_read_destroy");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001063
Patrick Scott5f6bd842010-06-28 16:55:16 -04001064#ifdef PNG_READ_GAMMA_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001065 png_destroy_gamma_table(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001066#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001067
1068 png_free(png_ptr, png_ptr->big_row_buf);
1069 png_free(png_ptr, png_ptr->big_prev_row);
1070 png_free(png_ptr, png_ptr->read_buffer);
1071
1072#ifdef PNG_READ_QUANTIZE_SUPPORTED
1073 png_free(png_ptr, png_ptr->palette_lookup);
1074 png_free(png_ptr, png_ptr->quantize_index);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001075#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001076
The Android Open Source Project893912b2009-03-03 19:30:05 -08001077 if (png_ptr->free_me & PNG_FREE_PLTE)
1078 png_zfree(png_ptr, png_ptr->palette);
1079 png_ptr->free_me &= ~PNG_FREE_PLTE;
Chris Craikb50c2172013-07-29 15:28:30 -07001080
The Android Open Source Project893912b2009-03-03 19:30:05 -08001081#if defined(PNG_tRNS_SUPPORTED) || \
1082 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001083 if (png_ptr->free_me & PNG_FREE_TRNS)
Chris Craikb50c2172013-07-29 15:28:30 -07001084 png_free(png_ptr, png_ptr->trans_alpha);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001085 png_ptr->free_me &= ~PNG_FREE_TRNS;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001086#endif
1087
1088 inflateEnd(&png_ptr->zstream);
Chris Craikb50c2172013-07-29 15:28:30 -07001089
The Android Open Source Project893912b2009-03-03 19:30:05 -08001090#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1091 png_free(png_ptr, png_ptr->save_buffer);
1092#endif
1093
Chris Craikb50c2172013-07-29 15:28:30 -07001094#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) &&\
1095 defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
1096 png_free(png_ptr, png_ptr->unknown_chunk.data);
1097#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001098
Chris Craikb50c2172013-07-29 15:28:30 -07001099#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
1100 png_free(png_ptr, png_ptr->chunk_list);
1101#endif
1102
Sireesh Tripurarib0277d02014-05-09 15:39:12 +05301103#ifdef PNG_INDEX_SUPPORTED
1104 if (png_ptr->index) {
1105 unsigned int i, p;
1106 png_indexp index = png_ptr->index;
1107 for (p = 0; p < 7; p++) {
1108 for (i = 0; i < index->size[p]; i++) {
1109 inflateEnd(index->pass_line_index[p][i]->z_state);
1110 png_free(png_ptr, index->pass_line_index[p][i]->z_state);
1111 png_free(png_ptr, index->pass_line_index[p][i]->prev_row);
1112 png_free(png_ptr, index->pass_line_index[p][i]);
1113 }
1114 if (index->size[p] != 0) {
1115 png_free(png_ptr, index->pass_line_index[p]);
1116 }
1117 }
1118 png_free(png_ptr, index);
1119 }
1120#endif
1121
Chris Craikb50c2172013-07-29 15:28:30 -07001122 /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
1123 * callbacks are still set at this point. They are required to complete the
1124 * destruction of the png_struct itself.
The Android Open Source Project893912b2009-03-03 19:30:05 -08001125 */
Chris Craikb50c2172013-07-29 15:28:30 -07001126}
The Android Open Source Project893912b2009-03-03 19:30:05 -08001127
Chris Craikb50c2172013-07-29 15:28:30 -07001128/* Free all memory used by the read */
1129void PNGAPI
1130png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
1131 png_infopp end_info_ptr_ptr)
1132{
1133 png_structrp png_ptr = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001134
Chris Craikb50c2172013-07-29 15:28:30 -07001135 png_debug(1, "in png_destroy_read_struct");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001136
Chris Craikb50c2172013-07-29 15:28:30 -07001137 if (png_ptr_ptr != NULL)
1138 png_ptr = *png_ptr_ptr;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001139
Chris Craikb50c2172013-07-29 15:28:30 -07001140 if (png_ptr == NULL)
1141 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001142
Chris Craikb50c2172013-07-29 15:28:30 -07001143 /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
1144 * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
1145 * The extra was, apparently, unnecessary yet this hides memory leak bugs.
1146 */
1147 png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
1148 png_destroy_info_struct(png_ptr, info_ptr_ptr);
1149
1150 *png_ptr_ptr = NULL;
1151 png_read_destroy(png_ptr);
1152 png_destroy_png_struct(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001153}
1154
1155void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -07001156png_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 -08001157{
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001158 if (png_ptr == NULL)
1159 return;
Chris Craikb50c2172013-07-29 15:28:30 -07001160
The Android Open Source Project893912b2009-03-03 19:30:05 -08001161 png_ptr->read_row_fn = read_row_fn;
1162}
1163
1164
Patrick Scott5f6bd842010-06-28 16:55:16 -04001165#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1166#ifdef PNG_INFO_IMAGE_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001167void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -07001168png_read_png(png_structrp png_ptr, png_inforp info_ptr,
The Android Open Source Project893912b2009-03-03 19:30:05 -08001169 int transforms,
1170 voidp params)
1171{
Chris Craikb50c2172013-07-29 15:28:30 -07001172 if (png_ptr == NULL || info_ptr == NULL)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001173 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001174
1175 /* png_read_info() gives us all of the information from the
1176 * PNG file before the first IDAT (image data chunk).
1177 */
1178 png_read_info(png_ptr, info_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -07001179 if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001180 png_error(png_ptr, "Image is too high to process with png_read_png()");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001181
1182 /* -------------- image transformations start here ------------------- */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301183 /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
1184 * is not implemented. This will only happen in de-configured (non-default)
1185 * libpng builds. The results can be unexpected - png_read_png may return
1186 * short or mal-formed rows because the transform is skipped.
1187 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001188
Chris Craikb50c2172013-07-29 15:28:30 -07001189 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
1190 */
1191 if (transforms & PNG_TRANSFORM_SCALE_16)
Chris Craikb50c2172013-07-29 15:28:30 -07001192 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
1193 * did in earlier versions, while "scale_16" is now more accurate.
1194 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301195#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001196 png_set_scale_16(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301197#else
1198 png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
Chris Craikb50c2172013-07-29 15:28:30 -07001199#endif
1200
Chris Craikb50c2172013-07-29 15:28:30 -07001201 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
1202 * latter by doing SCALE first. This is ok and allows apps not to check for
1203 * which is supported to get the right answer.
The Android Open Source Project893912b2009-03-03 19:30:05 -08001204 */
1205 if (transforms & PNG_TRANSFORM_STRIP_16)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301206#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001207 png_set_strip_16(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301208#else
1209 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001210#endif
1211
The Android Open Source Project893912b2009-03-03 19:30:05 -08001212 /* Strip alpha bytes from the input data without combining with
1213 * the background (not recommended).
1214 */
1215 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301216#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001217 png_set_strip_alpha(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301218#else
1219 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001220#endif
1221
The Android Open Source Project893912b2009-03-03 19:30:05 -08001222 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1223 * byte into separate bytes (useful for paletted and grayscale images).
1224 */
1225 if (transforms & PNG_TRANSFORM_PACKING)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301226#ifdef PNG_READ_PACK_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001227 png_set_packing(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301228#else
1229 png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001230#endif
1231
The Android Open Source Project893912b2009-03-03 19:30:05 -08001232 /* Change the order of packed pixels to least significant bit first
1233 * (not useful if you are using png_set_packing).
1234 */
1235 if (transforms & PNG_TRANSFORM_PACKSWAP)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301236#ifdef PNG_READ_PACKSWAP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001237 png_set_packswap(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301238#else
1239 png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001240#endif
1241
The Android Open Source Project893912b2009-03-03 19:30:05 -08001242 /* Expand paletted colors into true RGB triplets
1243 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1244 * Expand paletted or RGB images with transparency to full alpha
1245 * channels so the data will be available as RGBA quartets.
1246 */
1247 if (transforms & PNG_TRANSFORM_EXPAND)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301248#ifdef PNG_READ_EXPAND_SUPPORTED
1249 png_set_expand(png_ptr);
1250#else
1251 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001252#endif
1253
Chris Craikb50c2172013-07-29 15:28:30 -07001254 /* We don't handle background color or gamma transformation or quantizing.
The Android Open Source Project893912b2009-03-03 19:30:05 -08001255 */
1256
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001257 /* Invert monochrome files to have 0 as white and 1 as black
The Android Open Source Project893912b2009-03-03 19:30:05 -08001258 */
1259 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301260#ifdef PNG_READ_INVERT_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001261 png_set_invert_mono(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301262#else
1263 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001264#endif
1265
The Android Open Source Project893912b2009-03-03 19:30:05 -08001266 /* If you want to shift the pixel values from the range [0,255] or
1267 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1268 * colors were originally in:
1269 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301270 if (transforms & PNG_TRANSFORM_SHIFT)
1271#ifdef PNG_READ_SHIFT_SUPPORTED
1272 if (info_ptr->valid & PNG_INFO_sBIT)
1273 png_set_shift(png_ptr, &info_ptr->sig_bit);
1274#else
1275 png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001276#endif
1277
Chris Craikb50c2172013-07-29 15:28:30 -07001278 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001279 if (transforms & PNG_TRANSFORM_BGR)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301280#ifdef PNG_READ_BGR_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001281 png_set_bgr(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301282#else
1283 png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001284#endif
1285
Chris Craikb50c2172013-07-29 15:28:30 -07001286 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001287 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301288#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001289 png_set_swap_alpha(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301290#else
1291 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001292#endif
1293
Chris Craikb50c2172013-07-29 15:28:30 -07001294 /* Swap bytes of 16-bit files to least significant byte first */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001295 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301296#ifdef PNG_READ_SWAP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001297 png_set_swap(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301298#else
1299 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001300#endif
1301
Patrick Scott5f6bd842010-06-28 16:55:16 -04001302/* Added at libpng-1.2.41 */
Chris Craikb50c2172013-07-29 15:28:30 -07001303 /* Invert the alpha channel from opacity to transparency */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001304 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301305#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001306 png_set_invert_alpha(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301307#else
1308 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
Patrick Scott5f6bd842010-06-28 16:55:16 -04001309#endif
1310
1311/* Added at libpng-1.2.41 */
Chris Craikb50c2172013-07-29 15:28:30 -07001312 /* Expand grayscale image to RGB */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001313 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301314#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001315 png_set_gray_to_rgb(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301316#else
1317 png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
Chris Craikb50c2172013-07-29 15:28:30 -07001318#endif
1319
1320/* Added at libpng-1.5.4 */
Chris Craikb50c2172013-07-29 15:28:30 -07001321 if (transforms & PNG_TRANSFORM_EXPAND_16)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301322#ifdef PNG_READ_EXPAND_16_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001323 png_set_expand_16(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301324#else
1325 png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
Patrick Scott5f6bd842010-06-28 16:55:16 -04001326#endif
1327
The Android Open Source Project893912b2009-03-03 19:30:05 -08001328 /* We don't handle adding filler bytes */
1329
Chris Craikb50c2172013-07-29 15:28:30 -07001330 /* We use png_read_image and rely on that for interlace handling, but we also
1331 * call png_read_update_info therefore must turn on interlace handling now:
1332 */
1333 (void)png_set_interlace_handling(png_ptr);
1334
The Android Open Source Project893912b2009-03-03 19:30:05 -08001335 /* Optional call to gamma correct and add the background to the palette
1336 * and update info structure. REQUIRED if you are expecting libpng to
1337 * update the palette for you (i.e., you selected such a transform above).
1338 */
1339 png_read_update_info(png_ptr, info_ptr);
1340
1341 /* -------------- image transformations end here ------------------- */
1342
The Android Open Source Project893912b2009-03-03 19:30:05 -08001343 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001344 if (info_ptr->row_pointers == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001345 {
Chris Craikb50c2172013-07-29 15:28:30 -07001346 png_uint_32 iptr;
Patrick Scott5f6bd842010-06-28 16:55:16 -04001347
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301348 info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
1349 info_ptr->height * (sizeof (png_bytep))));
1350
Chris Craikb50c2172013-07-29 15:28:30 -07001351 for (iptr=0; iptr<info_ptr->height; iptr++)
1352 info_ptr->row_pointers[iptr] = NULL;
1353
The Android Open Source Project893912b2009-03-03 19:30:05 -08001354 info_ptr->free_me |= PNG_FREE_ROWS;
Patrick Scott5f6bd842010-06-28 16:55:16 -04001355
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301356 for (iptr = 0; iptr < info_ptr->height; iptr++)
1357 info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
1358 png_malloc(png_ptr, info_ptr->rowbytes));
The Android Open Source Project893912b2009-03-03 19:30:05 -08001359 }
1360
1361 png_read_image(png_ptr, info_ptr->row_pointers);
1362 info_ptr->valid |= PNG_INFO_IDAT;
1363
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001364 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001365 png_read_end(png_ptr, info_ptr);
1366
Chris Craikb50c2172013-07-29 15:28:30 -07001367 PNG_UNUSED(params)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001368}
1369#endif /* PNG_INFO_IMAGE_SUPPORTED */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001370#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
Chris Craikb50c2172013-07-29 15:28:30 -07001371
1372#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1373/* SIMPLIFIED READ
1374 *
1375 * This code currently relies on the sequential reader, though it could easily
1376 * be made to work with the progressive one.
1377 */
1378/* Arguments to png_image_finish_read: */
1379
1380/* Encoding of PNG data (used by the color-map code) */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301381# define P_NOTSET 0 /* File encoding not yet known */
1382# define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
1383# define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1384# define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1385# define P_LINEAR8 4 /* 8-bit linear: only from a file value */
Chris Craikb50c2172013-07-29 15:28:30 -07001386
1387/* Color-map processing: after libpng has run on the PNG image further
1388 * processing may be needed to conver the data to color-map indicies.
1389 */
1390#define PNG_CMAP_NONE 0
1391#define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1392#define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1393#define PNG_CMAP_RGB 3 /* Process RGB data */
1394#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1395
1396/* The following document where the background is for each processing case. */
1397#define PNG_CMAP_NONE_BACKGROUND 256
1398#define PNG_CMAP_GA_BACKGROUND 231
1399#define PNG_CMAP_TRANS_BACKGROUND 254
1400#define PNG_CMAP_RGB_BACKGROUND 256
1401#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1402
1403typedef struct
1404{
1405 /* Arguments: */
1406 png_imagep image;
1407 png_voidp buffer;
1408 png_int_32 row_stride;
1409 png_voidp colormap;
1410 png_const_colorp background;
1411 /* Local variables: */
1412 png_voidp local_row;
1413 png_voidp first_row;
1414 ptrdiff_t row_bytes; /* step between rows */
1415 int file_encoding; /* E_ values above */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301416 png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
Chris Craikb50c2172013-07-29 15:28:30 -07001417 int colormap_processing; /* PNG_CMAP_ values above */
1418} png_image_read_control;
1419
1420/* Do all the *safe* initialization - 'safe' means that png_error won't be
1421 * called, so setting up the jmp_buf is not required. This means that anything
1422 * called from here must *not* call png_malloc - it has to call png_malloc_warn
1423 * instead so that control is returned safely back to this routine.
1424 */
1425static int
1426png_image_read_init(png_imagep image)
1427{
1428 if (image->opaque == NULL)
1429 {
1430 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
1431 png_safe_error, png_safe_warning);
1432
1433 /* And set the rest of the structure to NULL to ensure that the various
1434 * fields are consistent.
1435 */
1436 memset(image, 0, (sizeof *image));
1437 image->version = PNG_IMAGE_VERSION;
1438
1439 if (png_ptr != NULL)
1440 {
1441 png_infop info_ptr = png_create_info_struct(png_ptr);
1442
1443 if (info_ptr != NULL)
1444 {
1445 png_controlp control = png_voidcast(png_controlp,
1446 png_malloc_warn(png_ptr, (sizeof *control)));
1447
1448 if (control != NULL)
1449 {
1450 memset(control, 0, (sizeof *control));
1451
1452 control->png_ptr = png_ptr;
1453 control->info_ptr = info_ptr;
1454 control->for_write = 0;
1455
1456 image->opaque = control;
1457 return 1;
1458 }
1459
1460 /* Error clean up */
1461 png_destroy_info_struct(png_ptr, &info_ptr);
1462 }
1463
1464 png_destroy_read_struct(&png_ptr, NULL, NULL);
1465 }
1466
1467 return png_image_error(image, "png_image_read: out of memory");
1468 }
1469
1470 return png_image_error(image, "png_image_read: opaque pointer not NULL");
1471}
1472
1473/* Utility to find the base format of a PNG file from a png_struct. */
1474static png_uint_32
1475png_image_format(png_structrp png_ptr)
1476{
1477 png_uint_32 format = 0;
1478
1479 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
1480 format |= PNG_FORMAT_FLAG_COLOR;
1481
1482 if (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1483 format |= PNG_FORMAT_FLAG_ALPHA;
1484
1485 /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1486 * sets the png_struct fields; that's all we are interested in here. The
1487 * precise interaction with an app call to png_set_tRNS and PNG file reading
1488 * is unclear.
1489 */
1490 else if (png_ptr->num_trans > 0)
1491 format |= PNG_FORMAT_FLAG_ALPHA;
1492
1493 if (png_ptr->bit_depth == 16)
1494 format |= PNG_FORMAT_FLAG_LINEAR;
1495
1496 if (png_ptr->color_type & PNG_COLOR_MASK_PALETTE)
1497 format |= PNG_FORMAT_FLAG_COLORMAP;
1498
1499 return format;
1500}
1501
1502/* Is the given gamma significantly different from sRGB? The test is the same
1503 * one used in pngrtran.c when deciding whether to do gamma correction. The
1504 * arithmetic optimizes the division by using the fact that the inverse of the
1505 * file sRGB gamma is 2.2
1506 */
1507static int
1508png_gamma_not_sRGB(png_fixed_point g)
1509{
1510 if (g < PNG_FP_1)
1511 {
1512 /* An uninitialized gamma is assumed to be sRGB for the simplified API. */
1513 if (g == 0)
1514 return 0;
1515
1516 return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1517 }
1518
1519 return 1;
1520}
1521
1522/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1523 * header and fill in all the information. This is executed in a safe context,
1524 * unlike the init routine above.
1525 */
1526static int
1527png_image_read_header(png_voidp argument)
1528{
1529 png_imagep image = png_voidcast(png_imagep, argument);
1530 png_structrp png_ptr = image->opaque->png_ptr;
1531 png_inforp info_ptr = image->opaque->info_ptr;
1532
1533 png_set_benign_errors(png_ptr, 1/*warn*/);
1534 png_read_info(png_ptr, info_ptr);
1535
1536 /* Do this the fast way; just read directly out of png_struct. */
1537 image->width = png_ptr->width;
1538 image->height = png_ptr->height;
1539
1540 {
1541 png_uint_32 format = png_image_format(png_ptr);
1542
1543 image->format = format;
1544
1545#ifdef PNG_COLORSPACE_SUPPORTED
1546 /* Does the colorspace match sRGB? If there is no color endpoint
1547 * (colorant) information assume yes, otherwise require the
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301548 * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the
Chris Craikb50c2172013-07-29 15:28:30 -07001549 * colorspace has been determined to be invalid ignore it.
1550 */
1551 if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags
1552 & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB|
1553 PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS))
1554 image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1555#endif
1556 }
1557
1558 /* We need the maximum number of entries regardless of the format the
1559 * application sets here.
1560 */
1561 {
1562 png_uint_32 cmap_entries;
1563
1564 switch (png_ptr->color_type)
1565 {
1566 case PNG_COLOR_TYPE_GRAY:
1567 cmap_entries = 1U << png_ptr->bit_depth;
1568 break;
1569
1570 case PNG_COLOR_TYPE_PALETTE:
1571 cmap_entries = png_ptr->num_palette;
1572 break;
1573
1574 default:
1575 cmap_entries = 256;
1576 break;
1577 }
1578
1579 if (cmap_entries > 256)
1580 cmap_entries = 256;
1581
1582 image->colormap_entries = cmap_entries;
1583 }
1584
1585 return 1;
1586}
1587
1588#ifdef PNG_STDIO_SUPPORTED
1589int PNGAPI
1590png_image_begin_read_from_stdio(png_imagep image, FILE* file)
1591{
1592 if (image != NULL && image->version == PNG_IMAGE_VERSION)
1593 {
1594 if (file != NULL)
1595 {
1596 if (png_image_read_init(image))
1597 {
1598 /* This is slightly evil, but png_init_io doesn't do anything other
1599 * than this and we haven't changed the standard IO functions so
1600 * this saves a 'safe' function.
1601 */
1602 image->opaque->png_ptr->io_ptr = file;
1603 return png_safe_execute(image, png_image_read_header, image);
1604 }
1605 }
1606
1607 else
1608 return png_image_error(image,
1609 "png_image_begin_read_from_stdio: invalid argument");
1610 }
1611
1612 else if (image != NULL)
1613 return png_image_error(image,
1614 "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
1615
1616 return 0;
1617}
1618
1619int PNGAPI
1620png_image_begin_read_from_file(png_imagep image, const char *file_name)
1621{
1622 if (image != NULL && image->version == PNG_IMAGE_VERSION)
1623 {
1624 if (file_name != NULL)
1625 {
1626 FILE *fp = fopen(file_name, "rb");
1627
1628 if (fp != NULL)
1629 {
1630 if (png_image_read_init(image))
1631 {
1632 image->opaque->png_ptr->io_ptr = fp;
1633 image->opaque->owned_file = 1;
1634 return png_safe_execute(image, png_image_read_header, image);
1635 }
1636
1637 /* Clean up: just the opened file. */
1638 (void)fclose(fp);
1639 }
1640
1641 else
1642 return png_image_error(image, strerror(errno));
1643 }
1644
1645 else
1646 return png_image_error(image,
1647 "png_image_begin_read_from_file: invalid argument");
1648 }
1649
1650 else if (image != NULL)
1651 return png_image_error(image,
1652 "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
1653
1654 return 0;
1655}
1656#endif /* PNG_STDIO_SUPPORTED */
1657
1658static void PNGCBAPI
1659png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need)
1660{
1661 if (png_ptr != NULL)
1662 {
1663 png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
1664 if (image != NULL)
1665 {
1666 png_controlp cp = image->opaque;
1667 if (cp != NULL)
1668 {
1669 png_const_bytep memory = cp->memory;
1670 png_size_t size = cp->size;
1671
1672 if (memory != NULL && size >= need)
1673 {
1674 memcpy(out, memory, need);
1675 cp->memory = memory + need;
1676 cp->size = size - need;
1677 return;
1678 }
1679
1680 png_error(png_ptr, "read beyond end of data");
1681 }
1682 }
1683
1684 png_error(png_ptr, "invalid memory read");
1685 }
1686}
1687
1688int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1689 png_const_voidp memory, png_size_t size)
1690{
1691 if (image != NULL && image->version == PNG_IMAGE_VERSION)
1692 {
1693 if (memory != NULL && size > 0)
1694 {
1695 if (png_image_read_init(image))
1696 {
1697 /* Now set the IO functions to read from the memory buffer and
1698 * store it into io_ptr. Again do this in-place to avoid calling a
1699 * libpng function that requires error handling.
1700 */
1701 image->opaque->memory = png_voidcast(png_const_bytep, memory);
1702 image->opaque->size = size;
1703 image->opaque->png_ptr->io_ptr = image;
1704 image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1705
1706 return png_safe_execute(image, png_image_read_header, image);
1707 }
1708 }
1709
1710 else
1711 return png_image_error(image,
1712 "png_image_begin_read_from_memory: invalid argument");
1713 }
1714
1715 else if (image != NULL)
1716 return png_image_error(image,
1717 "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
1718
1719 return 0;
1720}
1721
1722/* Utility function to skip chunks that are not used by the simplified image
1723 * read functions and an appropriate macro to call it.
1724 */
1725#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1726static void
1727png_image_skip_unused_chunks(png_structrp png_ptr)
1728{
1729 /* Prepare the reader to ignore all recognized chunks whose data will not
1730 * be used, i.e., all chunks recognized by libpng except for those
1731 * involved in basic image reading:
1732 *
1733 * IHDR, PLTE, IDAT, IEND
1734 *
1735 * Or image data handling:
1736 *
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301737 * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
Chris Craikb50c2172013-07-29 15:28:30 -07001738 *
1739 * This provides a small performance improvement and eliminates any
1740 * potential vulnerability to security problems in the unused chunks.
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301741 *
1742 * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
1743 * too. This allows the simplified API to be compiled without iCCP support,
1744 * however if the support is there the chunk is still checked to detect
1745 * errors (which are unfortunately quite common.)
Chris Craikb50c2172013-07-29 15:28:30 -07001746 */
1747 {
1748 static PNG_CONST png_byte chunks_to_process[] = {
1749 98, 75, 71, 68, '\0', /* bKGD */
1750 99, 72, 82, 77, '\0', /* cHRM */
1751 103, 65, 77, 65, '\0', /* gAMA */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301752# ifdef PNG_READ_iCCP_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001753 105, 67, 67, 80, '\0', /* iCCP */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301754# endif
Chris Craikb50c2172013-07-29 15:28:30 -07001755 115, 66, 73, 84, '\0', /* sBIT */
1756 115, 82, 71, 66, '\0', /* sRGB */
1757 };
1758
1759 /* Ignore unknown chunks and all other chunks except for the
1760 * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
1761 */
1762 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
1763 NULL, -1);
1764
1765 /* But do not ignore image data handling chunks */
1766 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
1767 chunks_to_process, (sizeof chunks_to_process)/5);
1768 }
1769}
1770
1771# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1772#else
1773# define PNG_SKIP_CHUNKS(p) ((void)0)
1774#endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */
1775
1776/* The following macro gives the exact rounded answer for all values in the
1777 * range 0..255 (it actually divides by 51.2, but the rounding still generates
1778 * the correct numbers 0..5
1779 */
1780#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1781
1782/* Utility functions to make particular color-maps */
1783static void
1784set_file_encoding(png_image_read_control *display)
1785{
1786 png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma;
1787 if (png_gamma_significant(g))
1788 {
1789 if (png_gamma_not_sRGB(g))
1790 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301791 display->file_encoding = P_FILE;
Chris Craikb50c2172013-07-29 15:28:30 -07001792 display->gamma_to_linear = png_reciprocal(g);
1793 }
1794
1795 else
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301796 display->file_encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07001797 }
1798
1799 else
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301800 display->file_encoding = P_LINEAR8;
Chris Craikb50c2172013-07-29 15:28:30 -07001801}
1802
1803static unsigned int
1804decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
1805{
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301806 if (encoding == P_FILE) /* double check */
Chris Craikb50c2172013-07-29 15:28:30 -07001807 encoding = display->file_encoding;
1808
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301809 if (encoding == P_NOTSET) /* must be the file encoding */
Chris Craikb50c2172013-07-29 15:28:30 -07001810 {
1811 set_file_encoding(display);
1812 encoding = display->file_encoding;
1813 }
1814
1815 switch (encoding)
1816 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301817 case P_FILE:
Chris Craikb50c2172013-07-29 15:28:30 -07001818 value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
1819 break;
1820
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301821 case P_sRGB:
Chris Craikb50c2172013-07-29 15:28:30 -07001822 value = png_sRGB_table[value];
1823 break;
1824
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301825 case P_LINEAR:
Chris Craikb50c2172013-07-29 15:28:30 -07001826 break;
1827
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301828 case P_LINEAR8:
Chris Craikb50c2172013-07-29 15:28:30 -07001829 value *= 257;
1830 break;
1831
1832 default:
1833 png_error(display->image->opaque->png_ptr,
1834 "unexpected encoding (internal error)");
1835 break;
1836 }
1837
1838 return value;
1839}
1840
1841static png_uint_32
1842png_colormap_compose(png_image_read_control *display,
1843 png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
1844 png_uint_32 background, int encoding)
1845{
1846 /* The file value is composed on the background, the background has the given
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301847 * encoding and so does the result, the file is encoded with P_FILE and the
Chris Craikb50c2172013-07-29 15:28:30 -07001848 * file and alpha are 8-bit values. The (output) encoding will always be
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301849 * P_LINEAR or P_sRGB.
Chris Craikb50c2172013-07-29 15:28:30 -07001850 */
1851 png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
1852 png_uint_32 b = decode_gamma(display, background, encoding);
1853
1854 /* The alpha is always an 8-bit value (it comes from the palette), the value
1855 * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1856 */
1857 f = f * alpha + b * (255-alpha);
1858
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301859 if (encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07001860 {
1861 /* Scale to 65535; divide by 255, approximately (in fact this is extremely
1862 * accurate, it divides by 255.00000005937181414556, with no overflow.)
1863 */
1864 f *= 257; /* Now scaled by 65535 */
1865 f += f >> 16;
1866 f = (f+32768) >> 16;
1867 }
1868
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301869 else /* P_sRGB */
Chris Craikb50c2172013-07-29 15:28:30 -07001870 f = PNG_sRGB_FROM_LINEAR(f);
1871
1872 return f;
1873}
1874
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301875/* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
Chris Craikb50c2172013-07-29 15:28:30 -07001876 * be 8-bit.
1877 */
1878static void
1879png_create_colormap_entry(png_image_read_control *display,
1880 png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
1881 png_uint_32 alpha, int encoding)
1882{
1883 png_imagep image = display->image;
1884 const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) ?
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301885 P_LINEAR : P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07001886 const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
1887 (red != green || green != blue);
1888
1889 if (ip > 255)
1890 png_error(image->opaque->png_ptr, "color-map index out of range");
1891
1892 /* Update the cache with whether the file gamma is significantly different
1893 * from sRGB.
1894 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301895 if (encoding == P_FILE)
Chris Craikb50c2172013-07-29 15:28:30 -07001896 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301897 if (display->file_encoding == P_NOTSET)
Chris Craikb50c2172013-07-29 15:28:30 -07001898 set_file_encoding(display);
1899
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301900 /* Note that the cached value may be P_FILE too, but if it is then the
Chris Craikb50c2172013-07-29 15:28:30 -07001901 * gamma_to_linear member has been set.
1902 */
1903 encoding = display->file_encoding;
1904 }
1905
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301906 if (encoding == P_FILE)
Chris Craikb50c2172013-07-29 15:28:30 -07001907 {
1908 png_fixed_point g = display->gamma_to_linear;
1909
1910 red = png_gamma_16bit_correct(red*257, g);
1911 green = png_gamma_16bit_correct(green*257, g);
1912 blue = png_gamma_16bit_correct(blue*257, g);
1913
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301914 if (convert_to_Y || output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07001915 {
1916 alpha *= 257;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301917 encoding = P_LINEAR;
Chris Craikb50c2172013-07-29 15:28:30 -07001918 }
1919
1920 else
1921 {
1922 red = PNG_sRGB_FROM_LINEAR(red * 255);
1923 green = PNG_sRGB_FROM_LINEAR(green * 255);
1924 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301925 encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07001926 }
1927 }
1928
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301929 else if (encoding == P_LINEAR8)
Chris Craikb50c2172013-07-29 15:28:30 -07001930 {
1931 /* This encoding occurs quite frequently in test cases because PngSuite
1932 * includes a gAMA 1.0 chunk with most images.
1933 */
1934 red *= 257;
1935 green *= 257;
1936 blue *= 257;
1937 alpha *= 257;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301938 encoding = P_LINEAR;
Chris Craikb50c2172013-07-29 15:28:30 -07001939 }
1940
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301941 else if (encoding == P_sRGB && (convert_to_Y || output_encoding == P_LINEAR))
Chris Craikb50c2172013-07-29 15:28:30 -07001942 {
1943 /* The values are 8-bit sRGB values, but must be converted to 16-bit
1944 * linear.
1945 */
1946 red = png_sRGB_table[red];
1947 green = png_sRGB_table[green];
1948 blue = png_sRGB_table[blue];
1949 alpha *= 257;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301950 encoding = P_LINEAR;
Chris Craikb50c2172013-07-29 15:28:30 -07001951 }
1952
1953 /* This is set if the color isn't gray but the output is. */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301954 if (encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07001955 {
1956 if (convert_to_Y)
1957 {
1958 /* NOTE: these values are copied from png_do_rgb_to_gray */
1959 png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
1960 (png_uint_32)2366 * blue;
1961
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301962 if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07001963 y = (y + 16384) >> 15;
1964
1965 else
1966 {
1967 /* y is scaled by 32768, we need it scaled by 255: */
1968 y = (y + 128) >> 8;
1969 y *= 255;
1970 y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301971 encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07001972 }
1973
1974 blue = red = green = y;
1975 }
1976
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301977 else if (output_encoding == P_sRGB)
Chris Craikb50c2172013-07-29 15:28:30 -07001978 {
1979 red = PNG_sRGB_FROM_LINEAR(red * 255);
1980 green = PNG_sRGB_FROM_LINEAR(green * 255);
1981 blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1982 alpha = PNG_DIV257(alpha);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301983 encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07001984 }
1985 }
1986
1987 if (encoding != output_encoding)
1988 png_error(image->opaque->png_ptr, "bad encoding (internal error)");
1989
1990 /* Store the value. */
1991 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301992# ifdef PNG_FORMAT_AFIRST_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001993 const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1994 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
1995# else
1996# define afirst 0
1997# endif
1998# ifdef PNG_FORMAT_BGR_SUPPORTED
1999 const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
2000# else
2001# define bgr 0
2002# endif
2003
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302004 if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07002005 {
2006 png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
2007
2008 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
2009
2010 /* The linear 16-bit values must be pre-multiplied by the alpha channel
2011 * value, if less than 65535 (this is, effectively, composite on black
2012 * if the alpha channel is removed.)
2013 */
2014 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
2015 {
2016 case 4:
2017 entry[afirst ? 0 : 3] = (png_uint_16)alpha;
2018 /* FALL THROUGH */
2019
2020 case 3:
2021 if (alpha < 65535)
2022 {
2023 if (alpha > 0)
2024 {
2025 blue = (blue * alpha + 32767U)/65535U;
2026 green = (green * alpha + 32767U)/65535U;
2027 red = (red * alpha + 32767U)/65535U;
2028 }
2029
2030 else
2031 red = green = blue = 0;
2032 }
2033 entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
2034 entry[afirst + 1] = (png_uint_16)green;
2035 entry[afirst + bgr] = (png_uint_16)red;
2036 break;
2037
2038 case 2:
2039 entry[1 ^ afirst] = (png_uint_16)alpha;
2040 /* FALL THROUGH */
2041
2042 case 1:
2043 if (alpha < 65535)
2044 {
2045 if (alpha > 0)
2046 green = (green * alpha + 32767U)/65535U;
2047
2048 else
2049 green = 0;
2050 }
2051 entry[afirst] = (png_uint_16)green;
2052 break;
2053
2054 default:
2055 break;
2056 }
2057 }
2058
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302059 else /* output encoding is P_sRGB */
Chris Craikb50c2172013-07-29 15:28:30 -07002060 {
2061 png_bytep entry = png_voidcast(png_bytep, display->colormap);
2062
2063 entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
2064
2065 switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
2066 {
2067 case 4:
2068 entry[afirst ? 0 : 3] = (png_byte)alpha;
2069 case 3:
2070 entry[afirst + (2 ^ bgr)] = (png_byte)blue;
2071 entry[afirst + 1] = (png_byte)green;
2072 entry[afirst + bgr] = (png_byte)red;
2073 break;
2074
2075 case 2:
2076 entry[1 ^ afirst] = (png_byte)alpha;
2077 case 1:
2078 entry[afirst] = (png_byte)green;
2079 break;
2080
2081 default:
2082 break;
2083 }
2084 }
2085
2086# ifdef afirst
2087# undef afirst
2088# endif
2089# ifdef bgr
2090# undef bgr
2091# endif
2092 }
2093}
2094
2095static int
2096make_gray_file_colormap(png_image_read_control *display)
2097{
2098 unsigned int i;
2099
2100 for (i=0; i<256; ++i)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302101 png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
Chris Craikb50c2172013-07-29 15:28:30 -07002102
2103 return i;
2104}
2105
2106static int
2107make_gray_colormap(png_image_read_control *display)
2108{
2109 unsigned int i;
2110
2111 for (i=0; i<256; ++i)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302112 png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002113
2114 return i;
2115}
2116#define PNG_GRAY_COLORMAP_ENTRIES 256
2117
2118static int
2119make_ga_colormap(png_image_read_control *display)
2120{
2121 unsigned int i, a;
2122
2123 /* Alpha is retained, the output will be a color-map with entries
2124 * selected by six levels of alpha. One transparent entry, 6 gray
2125 * levels for all the intermediate alpha values, leaving 230 entries
2126 * for the opaque grays. The color-map entries are the six values
2127 * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
2128 * relevant entry.
2129 *
2130 * if (alpha > 229) // opaque
2131 * {
2132 * // The 231 entries are selected to make the math below work:
2133 * base = 0;
2134 * entry = (231 * gray + 128) >> 8;
2135 * }
2136 * else if (alpha < 26) // transparent
2137 * {
2138 * base = 231;
2139 * entry = 0;
2140 * }
2141 * else // partially opaque
2142 * {
2143 * base = 226 + 6 * PNG_DIV51(alpha);
2144 * entry = PNG_DIV51(gray);
2145 * }
2146 */
2147 i = 0;
2148 while (i < 231)
2149 {
2150 unsigned int gray = (i * 256 + 115) / 231;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302151 png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002152 }
2153
2154 /* 255 is used here for the component values for consistency with the code
2155 * that undoes premultiplication in pngwrite.c.
2156 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302157 png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002158
2159 for (a=1; a<5; ++a)
2160 {
2161 unsigned int g;
2162
2163 for (g=0; g<6; ++g)
2164 png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302165 P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002166 }
2167
2168 return i;
2169}
2170
2171#define PNG_GA_COLORMAP_ENTRIES 256
2172
2173static int
2174make_rgb_colormap(png_image_read_control *display)
2175{
2176 unsigned int i, r;
2177
2178 /* Build a 6x6x6 opaque RGB cube */
2179 for (i=r=0; r<6; ++r)
2180 {
2181 unsigned int g;
2182
2183 for (g=0; g<6; ++g)
2184 {
2185 unsigned int b;
2186
2187 for (b=0; b<6; ++b)
2188 png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302189 P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002190 }
2191 }
2192
2193 return i;
2194}
2195
2196#define PNG_RGB_COLORMAP_ENTRIES 216
2197
2198/* Return a palette index to the above palette given three 8-bit sRGB values. */
2199#define PNG_RGB_INDEX(r,g,b) \
2200 ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
2201
2202static int
2203png_image_read_colormap(png_voidp argument)
2204{
2205 png_image_read_control *display =
2206 png_voidcast(png_image_read_control*, argument);
2207 const png_imagep image = display->image;
2208
2209 const png_structrp png_ptr = image->opaque->png_ptr;
2210 const png_uint_32 output_format = image->format;
2211 const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) ?
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302212 P_LINEAR : P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07002213
2214 unsigned int cmap_entries;
2215 unsigned int output_processing; /* Output processing option */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302216 unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
Chris Craikb50c2172013-07-29 15:28:30 -07002217
2218 /* Background information; the background color and the index of this color
2219 * in the color-map if it exists (else 256).
2220 */
2221 unsigned int background_index = 256;
2222 png_uint_32 back_r, back_g, back_b;
2223
2224 /* Flags to accumulate things that need to be done to the input. */
2225 int expand_tRNS = 0;
2226
2227 /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
2228 * very difficult to do, the results look awful, and it is difficult to see
2229 * what possible use it is because the application can't control the
2230 * color-map.
2231 */
2232 if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
2233 png_ptr->num_trans > 0) /* alpha in input */ &&
2234 ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
2235 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302236 if (output_encoding == P_LINEAR) /* compose on black */
Chris Craikb50c2172013-07-29 15:28:30 -07002237 back_b = back_g = back_r = 0;
2238
2239 else if (display->background == NULL /* no way to remove it */)
2240 png_error(png_ptr,
2241 "a background color must be supplied to remove alpha/transparency");
2242
2243 /* Get a copy of the background color (this avoids repeating the checks
2244 * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
2245 * output format.
2246 */
2247 else
2248 {
2249 back_g = display->background->green;
2250 if (output_format & PNG_FORMAT_FLAG_COLOR)
2251 {
2252 back_r = display->background->red;
2253 back_b = display->background->blue;
2254 }
2255 else
2256 back_b = back_r = back_g;
2257 }
2258 }
2259
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302260 else if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07002261 back_b = back_r = back_g = 65535;
2262
2263 else
2264 back_b = back_r = back_g = 255;
2265
2266 /* Default the input file gamma if required - this is necessary because
2267 * libpng assumes that if no gamma information is present the data is in the
2268 * output format, but the simplified API deduces the gamma from the input
2269 * format.
2270 */
2271 if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0)
2272 {
2273 /* Do this directly, not using the png_colorspace functions, to ensure
2274 * that it happens even if the colorspace is invalid (though probably if
2275 * it is the setting will be ignored) Note that the same thing can be
2276 * achieved at the application interface with png_set_gAMA.
2277 */
2278 if (png_ptr->bit_depth == 16 &&
2279 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
2280 png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR;
2281
2282 else
2283 png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE;
2284
2285 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
2286 }
2287
2288 /* Decide what to do based on the PNG color type of the input data. The
2289 * utility function png_create_colormap_entry deals with most aspects of the
2290 * output transformations; this code works out how to produce bytes of
2291 * color-map entries from the original format.
2292 */
2293 switch (png_ptr->color_type)
2294 {
2295 case PNG_COLOR_TYPE_GRAY:
2296 if (png_ptr->bit_depth <= 8)
2297 {
2298 /* There at most 256 colors in the output, regardless of
2299 * transparency.
2300 */
2301 unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
2302
2303 cmap_entries = 1U << png_ptr->bit_depth;
2304 if (cmap_entries > image->colormap_entries)
2305 png_error(png_ptr, "gray[8] color-map: too few entries");
2306
2307 step = 255 / (cmap_entries - 1);
2308 output_processing = PNG_CMAP_NONE;
2309
2310 /* If there is a tRNS chunk then this either selects a transparent
2311 * value or, if the output has no alpha, the background color.
2312 */
2313 if (png_ptr->num_trans > 0)
2314 {
2315 trans = png_ptr->trans_color.gray;
2316
2317 if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302318 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
Chris Craikb50c2172013-07-29 15:28:30 -07002319 }
2320
2321 /* png_create_colormap_entry just takes an RGBA and writes the
2322 * corresponding color-map entry using the format from 'image',
2323 * including the required conversion to sRGB or linear as
2324 * appropriate. The input values are always either sRGB (if the
2325 * gamma correction flag is 0) or 0..255 scaled file encoded values
2326 * (if the function must gamma correct them).
2327 */
2328 for (i=val=0; i<cmap_entries; ++i, val += step)
2329 {
2330 /* 'i' is a file value. While this will result in duplicated
2331 * entries for 8-bit non-sRGB encoded files it is necessary to
2332 * have non-gamma corrected values to do tRNS handling.
2333 */
2334 if (i != trans)
2335 png_create_colormap_entry(display, i, val, val, val, 255,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302336 P_FILE/*8-bit with file gamma*/);
Chris Craikb50c2172013-07-29 15:28:30 -07002337
2338 /* Else this entry is transparent. The colors don't matter if
2339 * there is an alpha channel (back_alpha == 0), but it does no
2340 * harm to pass them in; the values are not set above so this
2341 * passes in white.
2342 *
2343 * NOTE: this preserves the full precision of the application
2344 * supplied background color when it is used.
2345 */
2346 else
2347 png_create_colormap_entry(display, i, back_r, back_g, back_b,
2348 back_alpha, output_encoding);
2349 }
2350
2351 /* We need libpng to preserve the original encoding. */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302352 data_encoding = P_FILE;
Chris Craikb50c2172013-07-29 15:28:30 -07002353
2354 /* The rows from libpng, while technically gray values, are now also
2355 * color-map indicies; however, they may need to be expanded to 1
2356 * byte per pixel. This is what png_set_packing does (i.e., it
2357 * unpacks the bit values into bytes.)
2358 */
2359 if (png_ptr->bit_depth < 8)
2360 png_set_packing(png_ptr);
2361 }
2362
2363 else /* bit depth is 16 */
2364 {
2365 /* The 16-bit input values can be converted directly to 8-bit gamma
2366 * encoded values; however, if a tRNS chunk is present 257 color-map
2367 * entries are required. This means that the extra entry requires
2368 * special processing; add an alpha channel, sacrifice gray level
2369 * 254 and convert transparent (alpha==0) entries to that.
2370 *
2371 * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2372 * same time to minimize quality loss. If a tRNS chunk is present
2373 * this means libpng must handle it too; otherwise it is impossible
2374 * to do the exact match on the 16-bit value.
2375 *
2376 * If the output has no alpha channel *and* the background color is
2377 * gray then it is possible to let libpng handle the substitution by
2378 * ensuring that the corresponding gray level matches the background
2379 * color exactly.
2380 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302381 data_encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07002382
2383 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2384 png_error(png_ptr, "gray[16] color-map: too few entries");
2385
2386 cmap_entries = make_gray_colormap(display);
2387
2388 if (png_ptr->num_trans > 0)
2389 {
2390 unsigned int back_alpha;
2391
2392 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2393 back_alpha = 0;
2394
2395 else
2396 {
2397 if (back_r == back_g && back_g == back_b)
2398 {
2399 /* Background is gray; no special processing will be
2400 * required.
2401 */
2402 png_color_16 c;
2403 png_uint_32 gray = back_g;
2404
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302405 if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07002406 {
2407 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2408
2409 /* And make sure the corresponding palette entry
2410 * matches.
2411 */
2412 png_create_colormap_entry(display, gray, back_g, back_g,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302413 back_g, 65535, P_LINEAR);
Chris Craikb50c2172013-07-29 15:28:30 -07002414 }
2415
2416 /* The background passed to libpng, however, must be the
2417 * sRGB value.
2418 */
2419 c.index = 0; /*unused*/
2420 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2421
2422 /* NOTE: does this work without expanding tRNS to alpha?
2423 * It should be the color->gray case below apparently
2424 * doesn't.
2425 */
2426 png_set_background_fixed(png_ptr, &c,
2427 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2428 0/*gamma: not used*/);
2429
2430 output_processing = PNG_CMAP_NONE;
2431 break;
2432 }
2433
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302434 back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
Chris Craikb50c2172013-07-29 15:28:30 -07002435 }
2436
2437 /* output_processing means that the libpng-processed row will be
2438 * 8-bit GA and it has to be processing to single byte color-map
2439 * values. Entry 254 is replaced by either a completely
2440 * transparent entry or by the background color at full
2441 * precision (and the background color is not a simple gray leve
2442 * in this case.)
2443 */
2444 expand_tRNS = 1;
2445 output_processing = PNG_CMAP_TRANS;
2446 background_index = 254;
2447
2448 /* And set (overwrite) color-map entry 254 to the actual
2449 * background color at full precision.
2450 */
2451 png_create_colormap_entry(display, 254, back_r, back_g, back_b,
2452 back_alpha, output_encoding);
2453 }
2454
2455 else
2456 output_processing = PNG_CMAP_NONE;
2457 }
2458 break;
2459
2460 case PNG_COLOR_TYPE_GRAY_ALPHA:
2461 /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2462 * of 65536 combinations. If, however, the alpha channel is to be
2463 * removed there are only 256 possibilities if the background is gray.
2464 * (Otherwise there is a subset of the 65536 possibilities defined by
2465 * the triangle between black, white and the background color.)
2466 *
2467 * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2468 * worry about tRNS matching - tRNS is ignored if there is an alpha
2469 * channel.
2470 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302471 data_encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07002472
2473 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2474 {
2475 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2476 png_error(png_ptr, "gray+alpha color-map: too few entries");
2477
2478 cmap_entries = make_ga_colormap(display);
2479
2480 background_index = PNG_CMAP_GA_BACKGROUND;
2481 output_processing = PNG_CMAP_GA;
2482 }
2483
2484 else /* alpha is removed */
2485 {
2486 /* Alpha must be removed as the PNG data is processed when the
2487 * background is a color because the G and A channels are
2488 * independent and the vector addition (non-parallel vectors) is a
2489 * 2-D problem.
2490 *
2491 * This can be reduced to the same algorithm as above by making a
2492 * colormap containing gray levels (for the opaque grays), a
2493 * background entry (for a transparent pixel) and a set of four six
2494 * level color values, one set for each intermediate alpha value.
2495 * See the comments in make_ga_colormap for how this works in the
2496 * per-pixel processing.
2497 *
2498 * If the background is gray, however, we only need a 256 entry gray
2499 * level color map. It is sufficient to make the entry generated
2500 * for the background color be exactly the color specified.
2501 */
2502 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2503 (back_r == back_g && back_g == back_b))
2504 {
2505 /* Background is gray; no special processing will be required. */
2506 png_color_16 c;
2507 png_uint_32 gray = back_g;
2508
2509 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2510 png_error(png_ptr, "gray-alpha color-map: too few entries");
2511
2512 cmap_entries = make_gray_colormap(display);
2513
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302514 if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07002515 {
2516 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2517
2518 /* And make sure the corresponding palette entry matches. */
2519 png_create_colormap_entry(display, gray, back_g, back_g,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302520 back_g, 65535, P_LINEAR);
Chris Craikb50c2172013-07-29 15:28:30 -07002521 }
2522
2523 /* The background passed to libpng, however, must be the sRGB
2524 * value.
2525 */
2526 c.index = 0; /*unused*/
2527 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2528
2529 png_set_background_fixed(png_ptr, &c,
2530 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2531 0/*gamma: not used*/);
2532
2533 output_processing = PNG_CMAP_NONE;
2534 }
2535
2536 else
2537 {
2538 png_uint_32 i, a;
2539
2540 /* This is the same as png_make_ga_colormap, above, except that
2541 * the entries are all opaque.
2542 */
2543 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2544 png_error(png_ptr, "ga-alpha color-map: too few entries");
2545
2546 i = 0;
2547 while (i < 231)
2548 {
2549 png_uint_32 gray = (i * 256 + 115) / 231;
2550 png_create_colormap_entry(display, i++, gray, gray, gray,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302551 255, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002552 }
2553
2554 /* NOTE: this preserves the full precision of the application
2555 * background color.
2556 */
2557 background_index = i;
2558 png_create_colormap_entry(display, i++, back_r, back_g, back_b,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302559 output_encoding == P_LINEAR ? 65535U : 255U, output_encoding);
Chris Craikb50c2172013-07-29 15:28:30 -07002560
2561 /* For non-opaque input composite on the sRGB background - this
2562 * requires inverting the encoding for each component. The input
2563 * is still converted to the sRGB encoding because this is a
2564 * reasonable approximate to the logarithmic curve of human
2565 * visual sensitivity, at least over the narrow range which PNG
2566 * represents. Consequently 'G' is always sRGB encoded, while
2567 * 'A' is linear. We need the linear background colors.
2568 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302569 if (output_encoding == P_sRGB) /* else already linear */
Chris Craikb50c2172013-07-29 15:28:30 -07002570 {
2571 /* This may produce a value not exactly matching the
2572 * background, but that's ok because these numbers are only
2573 * used when alpha != 0
2574 */
2575 back_r = png_sRGB_table[back_r];
2576 back_g = png_sRGB_table[back_g];
2577 back_b = png_sRGB_table[back_b];
2578 }
2579
2580 for (a=1; a<5; ++a)
2581 {
2582 unsigned int g;
2583
2584 /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2585 * by an 8-bit alpha value (0..255).
2586 */
2587 png_uint_32 alpha = 51 * a;
2588 png_uint_32 back_rx = (255-alpha) * back_r;
2589 png_uint_32 back_gx = (255-alpha) * back_g;
2590 png_uint_32 back_bx = (255-alpha) * back_b;
2591
2592 for (g=0; g<6; ++g)
2593 {
2594 png_uint_32 gray = png_sRGB_table[g*51] * alpha;
2595
2596 png_create_colormap_entry(display, i++,
2597 PNG_sRGB_FROM_LINEAR(gray + back_rx),
2598 PNG_sRGB_FROM_LINEAR(gray + back_gx),
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302599 PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002600 }
2601 }
2602
2603 cmap_entries = i;
2604 output_processing = PNG_CMAP_GA;
2605 }
2606 }
2607 break;
2608
2609 case PNG_COLOR_TYPE_RGB:
2610 case PNG_COLOR_TYPE_RGB_ALPHA:
2611 /* Exclude the case where the output is gray; we can always handle this
2612 * with the cases above.
2613 */
2614 if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
2615 {
2616 /* The color-map will be grayscale, so we may as well convert the
2617 * input RGB values to a simple grayscale and use the grayscale
2618 * code above.
2619 *
2620 * NOTE: calling this apparently damages the recognition of the
2621 * transparent color in background color handling; call
2622 * png_set_tRNS_to_alpha before png_set_background_fixed.
2623 */
2624 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2625 -1);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302626 data_encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07002627
2628 /* The output will now be one or two 8-bit gray or gray+alpha
2629 * channels. The more complex case arises when the input has alpha.
2630 */
2631 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2632 png_ptr->num_trans > 0) &&
2633 (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2634 {
2635 /* Both input and output have an alpha channel, so no background
2636 * processing is required; just map the GA bytes to the right
2637 * color-map entry.
2638 */
2639 expand_tRNS = 1;
2640
2641 if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2642 png_error(png_ptr, "rgb[ga] color-map: too few entries");
2643
2644 cmap_entries = make_ga_colormap(display);
2645 background_index = PNG_CMAP_GA_BACKGROUND;
2646 output_processing = PNG_CMAP_GA;
2647 }
2648
2649 else
2650 {
2651 /* Either the input or the output has no alpha channel, so there
2652 * will be no non-opaque pixels in the color-map; it will just be
2653 * grayscale.
2654 */
2655 if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2656 png_error(png_ptr, "rgb[gray] color-map: too few entries");
2657
2658 /* Ideally this code would use libpng to do the gamma correction,
2659 * but if an input alpha channel is to be removed we will hit the
2660 * libpng bug in gamma+compose+rgb-to-gray (the double gamma
2661 * correction bug). Fix this by dropping the gamma correction in
2662 * this case and doing it in the palette; this will result in
2663 * duplicate palette entries, but that's better than the
2664 * alternative of double gamma correction.
2665 */
2666 if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2667 png_ptr->num_trans > 0) &&
2668 png_gamma_not_sRGB(png_ptr->colorspace.gamma))
2669 {
2670 cmap_entries = make_gray_file_colormap(display);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302671 data_encoding = P_FILE;
Chris Craikb50c2172013-07-29 15:28:30 -07002672 }
2673
2674 else
2675 cmap_entries = make_gray_colormap(display);
2676
2677 /* But if the input has alpha or transparency it must be removed
2678 */
2679 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2680 png_ptr->num_trans > 0)
2681 {
2682 png_color_16 c;
2683 png_uint_32 gray = back_g;
2684
2685 /* We need to ensure that the application background exists in
2686 * the colormap and that completely transparent pixels map to
2687 * it. Achieve this simply by ensuring that the entry
2688 * selected for the background really is the background color.
2689 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302690 if (data_encoding == P_FILE) /* from the fixup above */
Chris Craikb50c2172013-07-29 15:28:30 -07002691 {
2692 /* The app supplied a gray which is in output_encoding, we
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302693 * need to convert it to a value of the input (P_FILE)
Chris Craikb50c2172013-07-29 15:28:30 -07002694 * encoding then set this palette entry to the required
2695 * output encoding.
2696 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302697 if (output_encoding == P_sRGB)
2698 gray = png_sRGB_table[gray]; /* now P_LINEAR */
Chris Craikb50c2172013-07-29 15:28:30 -07002699
2700 gray = PNG_DIV257(png_gamma_16bit_correct(gray,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302701 png_ptr->colorspace.gamma)); /* now P_FILE */
Chris Craikb50c2172013-07-29 15:28:30 -07002702
2703 /* And make sure the corresponding palette entry contains
2704 * exactly the required sRGB value.
2705 */
2706 png_create_colormap_entry(display, gray, back_g, back_g,
2707 back_g, 0/*unused*/, output_encoding);
2708 }
2709
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302710 else if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07002711 {
2712 gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2713
2714 /* And make sure the corresponding palette entry matches.
2715 */
2716 png_create_colormap_entry(display, gray, back_g, back_g,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302717 back_g, 0/*unused*/, P_LINEAR);
Chris Craikb50c2172013-07-29 15:28:30 -07002718 }
2719
2720 /* The background passed to libpng, however, must be the
2721 * output (normally sRGB) value.
2722 */
2723 c.index = 0; /*unused*/
2724 c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2725
2726 /* NOTE: the following is apparently a bug in libpng. Without
2727 * it the transparent color recognition in
2728 * png_set_background_fixed seems to go wrong.
2729 */
2730 expand_tRNS = 1;
2731 png_set_background_fixed(png_ptr, &c,
2732 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2733 0/*gamma: not used*/);
2734 }
2735
2736 output_processing = PNG_CMAP_NONE;
2737 }
2738 }
2739
2740 else /* output is color */
2741 {
2742 /* We could use png_quantize here so long as there is no transparent
2743 * color or alpha; png_quantize ignores alpha. Easier overall just
2744 * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2745 * Consequently we always want libpng to produce sRGB data.
2746 */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302747 data_encoding = P_sRGB;
Chris Craikb50c2172013-07-29 15:28:30 -07002748
2749 /* Is there any transparency or alpha? */
2750 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2751 png_ptr->num_trans > 0)
2752 {
2753 /* Is there alpha in the output too? If so all four channels are
2754 * processed into a special RGB cube with alpha support.
2755 */
2756 if (output_format & PNG_FORMAT_FLAG_ALPHA)
2757 {
2758 png_uint_32 r;
2759
2760 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2761 png_error(png_ptr, "rgb+alpha color-map: too few entries");
2762
2763 cmap_entries = make_rgb_colormap(display);
2764
2765 /* Add a transparent entry. */
2766 png_create_colormap_entry(display, cmap_entries, 255, 255,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302767 255, 0, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002768
2769 /* This is stored as the background index for the processing
2770 * algorithm.
2771 */
2772 background_index = cmap_entries++;
2773
2774 /* Add 27 r,g,b entries each with alpha 0.5. */
2775 for (r=0; r<256; r = (r << 1) | 0x7f)
2776 {
2777 png_uint_32 g;
2778
2779 for (g=0; g<256; g = (g << 1) | 0x7f)
2780 {
2781 png_uint_32 b;
2782
2783 /* This generates components with the values 0, 127 and
2784 * 255
2785 */
2786 for (b=0; b<256; b = (b << 1) | 0x7f)
2787 png_create_colormap_entry(display, cmap_entries++,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302788 r, g, b, 128, P_sRGB);
Chris Craikb50c2172013-07-29 15:28:30 -07002789 }
2790 }
2791
2792 expand_tRNS = 1;
2793 output_processing = PNG_CMAP_RGB_ALPHA;
2794 }
2795
2796 else
2797 {
2798 /* Alpha/transparency must be removed. The background must
2799 * exist in the color map (achieved by setting adding it after
2800 * the 666 color-map). If the standard processing code will
2801 * pick up this entry automatically that's all that is
2802 * required; libpng can be called to do the background
2803 * processing.
2804 */
2805 unsigned int sample_size =
2806 PNG_IMAGE_SAMPLE_SIZE(output_format);
2807 png_uint_32 r, g, b; /* sRGB background */
2808
2809 if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2810 png_error(png_ptr, "rgb-alpha color-map: too few entries");
2811
2812 cmap_entries = make_rgb_colormap(display);
2813
2814 png_create_colormap_entry(display, cmap_entries, back_r,
2815 back_g, back_b, 0/*unused*/, output_encoding);
2816
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302817 if (output_encoding == P_LINEAR)
Chris Craikb50c2172013-07-29 15:28:30 -07002818 {
2819 r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2820 g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2821 b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2822 }
2823
2824 else
2825 {
2826 r = back_r;
2827 g = back_g;
2828 b = back_g;
2829 }
2830
2831 /* Compare the newly-created color-map entry with the one the
2832 * PNG_CMAP_RGB algorithm will use. If the two entries don't
2833 * match, add the new one and set this as the background
2834 * index.
2835 */
2836 if (memcmp((png_const_bytep)display->colormap +
2837 sample_size * cmap_entries,
2838 (png_const_bytep)display->colormap +
2839 sample_size * PNG_RGB_INDEX(r,g,b),
2840 sample_size) != 0)
2841 {
2842 /* The background color must be added. */
2843 background_index = cmap_entries++;
2844
2845 /* Add 27 r,g,b entries each with created by composing with
2846 * the background at alpha 0.5.
2847 */
2848 for (r=0; r<256; r = (r << 1) | 0x7f)
2849 {
2850 for (g=0; g<256; g = (g << 1) | 0x7f)
2851 {
2852 /* This generates components with the values 0, 127
2853 * and 255
2854 */
2855 for (b=0; b<256; b = (b << 1) | 0x7f)
2856 png_create_colormap_entry(display, cmap_entries++,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302857 png_colormap_compose(display, r, P_sRGB, 128,
Chris Craikb50c2172013-07-29 15:28:30 -07002858 back_r, output_encoding),
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302859 png_colormap_compose(display, g, P_sRGB, 128,
Chris Craikb50c2172013-07-29 15:28:30 -07002860 back_g, output_encoding),
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302861 png_colormap_compose(display, b, P_sRGB, 128,
Chris Craikb50c2172013-07-29 15:28:30 -07002862 back_b, output_encoding),
2863 0/*unused*/, output_encoding);
2864 }
2865 }
2866
2867 expand_tRNS = 1;
2868 output_processing = PNG_CMAP_RGB_ALPHA;
2869 }
2870
2871 else /* background color is in the standard color-map */
2872 {
2873 png_color_16 c;
2874
2875 c.index = 0; /*unused*/
2876 c.red = (png_uint_16)back_r;
2877 c.gray = c.green = (png_uint_16)back_g;
2878 c.blue = (png_uint_16)back_b;
2879
2880 png_set_background_fixed(png_ptr, &c,
2881 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2882 0/*gamma: not used*/);
2883
2884 output_processing = PNG_CMAP_RGB;
2885 }
2886 }
2887 }
2888
2889 else /* no alpha or transparency in the input */
2890 {
2891 /* Alpha in the output is irrelevant, simply map the opaque input
2892 * pixels to the 6x6x6 color-map.
2893 */
2894 if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2895 png_error(png_ptr, "rgb color-map: too few entries");
2896
2897 cmap_entries = make_rgb_colormap(display);
2898 output_processing = PNG_CMAP_RGB;
2899 }
2900 }
2901 break;
2902
2903 case PNG_COLOR_TYPE_PALETTE:
2904 /* It's already got a color-map. It may be necessary to eliminate the
2905 * tRNS entries though.
2906 */
2907 {
2908 unsigned int num_trans = png_ptr->num_trans;
2909 png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2910 png_const_colorp colormap = png_ptr->palette;
2911 const int do_background = trans != NULL &&
2912 (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2913 unsigned int i;
2914
2915 /* Just in case: */
2916 if (trans == NULL)
2917 num_trans = 0;
2918
2919 output_processing = PNG_CMAP_NONE;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302920 data_encoding = P_FILE; /* Don't change from color-map indicies */
Chris Craikb50c2172013-07-29 15:28:30 -07002921 cmap_entries = png_ptr->num_palette;
2922 if (cmap_entries > 256)
2923 cmap_entries = 256;
2924
2925 if (cmap_entries > image->colormap_entries)
2926 png_error(png_ptr, "palette color-map: too few entries");
2927
2928 for (i=0; i < cmap_entries; ++i)
2929 {
2930 if (do_background && i < num_trans && trans[i] < 255)
2931 {
2932 if (trans[i] == 0)
2933 png_create_colormap_entry(display, i, back_r, back_g,
2934 back_b, 0, output_encoding);
2935
2936 else
2937 {
2938 /* Must compose the PNG file color in the color-map entry
2939 * on the sRGB color in 'back'.
2940 */
2941 png_create_colormap_entry(display, i,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302942 png_colormap_compose(display, colormap[i].red, P_FILE,
Chris Craikb50c2172013-07-29 15:28:30 -07002943 trans[i], back_r, output_encoding),
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302944 png_colormap_compose(display, colormap[i].green, P_FILE,
Chris Craikb50c2172013-07-29 15:28:30 -07002945 trans[i], back_g, output_encoding),
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302946 png_colormap_compose(display, colormap[i].blue, P_FILE,
Chris Craikb50c2172013-07-29 15:28:30 -07002947 trans[i], back_b, output_encoding),
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302948 output_encoding == P_LINEAR ? trans[i] * 257U :
Chris Craikb50c2172013-07-29 15:28:30 -07002949 trans[i],
2950 output_encoding);
2951 }
2952 }
2953
2954 else
2955 png_create_colormap_entry(display, i, colormap[i].red,
2956 colormap[i].green, colormap[i].blue,
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302957 i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
Chris Craikb50c2172013-07-29 15:28:30 -07002958 }
2959
2960 /* The PNG data may have indicies packed in fewer than 8 bits, it
2961 * must be expanded if so.
2962 */
2963 if (png_ptr->bit_depth < 8)
2964 png_set_packing(png_ptr);
2965 }
2966 break;
2967
2968 default:
2969 png_error(png_ptr, "invalid PNG color type");
2970 /*NOT REACHED*/
2971 break;
2972 }
2973
2974 /* Now deal with the output processing */
2975 if (expand_tRNS && png_ptr->num_trans > 0 &&
2976 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
2977 png_set_tRNS_to_alpha(png_ptr);
2978
2979 switch (data_encoding)
2980 {
2981 default:
2982 png_error(png_ptr, "bad data option (internal error)");
2983 break;
2984
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302985 case P_sRGB:
Chris Craikb50c2172013-07-29 15:28:30 -07002986 /* Change to 8-bit sRGB */
2987 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
2988 /* FALL THROUGH */
2989
Sireesh Tripurarib478e662014-05-09 15:15:10 +05302990 case P_FILE:
Chris Craikb50c2172013-07-29 15:28:30 -07002991 if (png_ptr->bit_depth > 8)
2992 png_set_scale_16(png_ptr);
2993 break;
2994 }
2995
2996 if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2997 png_error(png_ptr, "color map overflow (BAD internal error)");
2998
2999 image->colormap_entries = cmap_entries;
3000
3001 /* Double check using the recorded background index */
3002 switch (output_processing)
3003 {
3004 case PNG_CMAP_NONE:
3005 if (background_index != PNG_CMAP_NONE_BACKGROUND)
3006 goto bad_background;
3007 break;
3008
3009 case PNG_CMAP_GA:
3010 if (background_index != PNG_CMAP_GA_BACKGROUND)
3011 goto bad_background;
3012 break;
3013
3014 case PNG_CMAP_TRANS:
3015 if (background_index >= cmap_entries ||
3016 background_index != PNG_CMAP_TRANS_BACKGROUND)
3017 goto bad_background;
3018 break;
3019
3020 case PNG_CMAP_RGB:
3021 if (background_index != PNG_CMAP_RGB_BACKGROUND)
3022 goto bad_background;
3023 break;
3024
3025 case PNG_CMAP_RGB_ALPHA:
3026 if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
3027 goto bad_background;
3028 break;
3029
3030 default:
3031 png_error(png_ptr, "bad processing option (internal error)");
3032
3033 bad_background:
3034 png_error(png_ptr, "bad background index (internal error)");
3035 }
3036
3037 display->colormap_processing = output_processing;
3038
3039 return 1/*ok*/;
3040}
3041
3042/* The final part of the color-map read called from png_image_finish_read. */
3043static int
3044png_image_read_and_map(png_voidp argument)
3045{
3046 png_image_read_control *display = png_voidcast(png_image_read_control*,
3047 argument);
3048 png_imagep image = display->image;
3049 png_structrp png_ptr = image->opaque->png_ptr;
3050 int passes;
3051
3052 /* Called when the libpng data must be transformed into the color-mapped
3053 * form. There is a local row buffer in display->local and this routine must
3054 * do the interlace handling.
3055 */
3056 switch (png_ptr->interlaced)
3057 {
3058 case PNG_INTERLACE_NONE:
3059 passes = 1;
3060 break;
3061
3062 case PNG_INTERLACE_ADAM7:
3063 passes = PNG_INTERLACE_ADAM7_PASSES;
3064 break;
3065
3066 default:
Chris Craikb50c2172013-07-29 15:28:30 -07003067 png_error(png_ptr, "unknown interlace type");
3068 }
3069
3070 {
3071 png_uint_32 height = image->height;
3072 png_uint_32 width = image->width;
3073 int proc = display->colormap_processing;
3074 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3075 ptrdiff_t step_row = display->row_bytes;
3076 int pass;
3077
3078 for (pass = 0; pass < passes; ++pass)
3079 {
3080 unsigned int startx, stepx, stepy;
3081 png_uint_32 y;
3082
3083 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3084 {
3085 /* The row may be empty for a short image: */
3086 if (PNG_PASS_COLS(width, pass) == 0)
3087 continue;
3088
3089 startx = PNG_PASS_START_COL(pass);
3090 stepx = PNG_PASS_COL_OFFSET(pass);
3091 y = PNG_PASS_START_ROW(pass);
3092 stepy = PNG_PASS_ROW_OFFSET(pass);
3093 }
3094
3095 else
3096 {
3097 y = 0;
3098 startx = 0;
3099 stepx = stepy = 1;
3100 }
3101
3102 for (; y<height; y += stepy)
3103 {
3104 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
3105 png_bytep outrow = first_row + y * step_row;
3106 png_const_bytep end_row = outrow + width;
3107
3108 /* Read read the libpng data into the temporary buffer. */
3109 png_read_row(png_ptr, inrow, NULL);
3110
3111 /* Now process the row according to the processing option, note
3112 * that the caller verifies that the format of the libpng output
3113 * data is as required.
3114 */
3115 outrow += startx;
3116 switch (proc)
3117 {
3118 case PNG_CMAP_GA:
3119 for (; outrow < end_row; outrow += stepx)
3120 {
3121 /* The data is always in the PNG order */
3122 unsigned int gray = *inrow++;
3123 unsigned int alpha = *inrow++;
3124 unsigned int entry;
3125
3126 /* NOTE: this code is copied as a comment in
3127 * make_ga_colormap above. Please update the
3128 * comment if you change this code!
3129 */
3130 if (alpha > 229) /* opaque */
3131 {
3132 entry = (231 * gray + 128) >> 8;
3133 }
3134 else if (alpha < 26) /* transparent */
3135 {
3136 entry = 231;
3137 }
3138 else /* partially opaque */
3139 {
3140 entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
3141 }
3142
3143 *outrow = (png_byte)entry;
3144 }
3145 break;
3146
3147 case PNG_CMAP_TRANS:
3148 for (; outrow < end_row; outrow += stepx)
3149 {
3150 png_byte gray = *inrow++;
3151 png_byte alpha = *inrow++;
3152
3153 if (alpha == 0)
3154 *outrow = PNG_CMAP_TRANS_BACKGROUND;
3155
3156 else if (gray != PNG_CMAP_TRANS_BACKGROUND)
3157 *outrow = gray;
3158
3159 else
3160 *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
3161 }
3162 break;
3163
3164 case PNG_CMAP_RGB:
3165 for (; outrow < end_row; outrow += stepx)
3166 {
3167 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
3168 inrow += 3;
3169 }
3170 break;
3171
3172 case PNG_CMAP_RGB_ALPHA:
3173 for (; outrow < end_row; outrow += stepx)
3174 {
3175 unsigned int alpha = inrow[3];
3176
3177 /* Because the alpha entries only hold alpha==0.5 values
3178 * split the processing at alpha==0.25 (64) and 0.75
3179 * (196).
3180 */
3181
3182 if (alpha >= 196)
3183 *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
3184 inrow[2]);
3185
3186 else if (alpha < 64)
3187 *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
3188
3189 else
3190 {
3191 /* Likewise there are three entries for each of r, g
3192 * and b. We could select the entry by popcount on
3193 * the top two bits on those architectures that
3194 * support it, this is what the code below does,
3195 * crudely.
3196 */
3197 unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
3198
3199 /* Here are how the values map:
3200 *
3201 * 0x00 .. 0x3f -> 0
3202 * 0x40 .. 0xbf -> 1
3203 * 0xc0 .. 0xff -> 2
3204 *
3205 * So, as above with the explicit alpha checks, the
3206 * breakpoints are at 64 and 196.
3207 */
3208 if (inrow[0] & 0x80) back_i += 9; /* red */
3209 if (inrow[0] & 0x40) back_i += 9;
3210 if (inrow[0] & 0x80) back_i += 3; /* green */
3211 if (inrow[0] & 0x40) back_i += 3;
3212 if (inrow[0] & 0x80) back_i += 1; /* blue */
3213 if (inrow[0] & 0x40) back_i += 1;
3214
3215 *outrow = (png_byte)back_i;
3216 }
3217
3218 inrow += 4;
3219 }
3220 break;
3221
3222 default:
3223 break;
3224 }
3225 }
3226 }
3227 }
3228
3229 return 1;
3230}
3231
3232static int
3233png_image_read_colormapped(png_voidp argument)
3234{
3235 png_image_read_control *display = png_voidcast(png_image_read_control*,
3236 argument);
3237 png_imagep image = display->image;
3238 png_controlp control = image->opaque;
3239 png_structrp png_ptr = control->png_ptr;
3240 png_inforp info_ptr = control->info_ptr;
3241
3242 int passes = 0; /* As a flag */
3243
3244 PNG_SKIP_CHUNKS(png_ptr);
3245
3246 /* Update the 'info' structure and make sure the result is as required; first
3247 * make sure to turn on the interlace handling if it will be required
3248 * (because it can't be turned on *after* the call to png_read_update_info!)
3249 */
3250 if (display->colormap_processing == PNG_CMAP_NONE)
3251 passes = png_set_interlace_handling(png_ptr);
3252
3253 png_read_update_info(png_ptr, info_ptr);
3254
3255 /* The expected output can be deduced from the colormap_processing option. */
3256 switch (display->colormap_processing)
3257 {
3258 case PNG_CMAP_NONE:
3259 /* Output must be one channel and one byte per pixel, the output
3260 * encoding can be anything.
3261 */
3262 if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
3263 info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
3264 info_ptr->bit_depth == 8)
3265 break;
3266
3267 goto bad_output;
3268
3269 case PNG_CMAP_TRANS:
3270 case PNG_CMAP_GA:
3271 /* Output must be two channels and the 'G' one must be sRGB, the latter
3272 * can be checked with an exact number because it should have been set
3273 * to this number above!
3274 */
3275 if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
3276 info_ptr->bit_depth == 8 &&
3277 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3278 image->colormap_entries == 256)
3279 break;
3280
3281 goto bad_output;
3282
3283 case PNG_CMAP_RGB:
3284 /* Output must be 8-bit sRGB encoded RGB */
3285 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
3286 info_ptr->bit_depth == 8 &&
3287 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3288 image->colormap_entries == 216)
3289 break;
3290
3291 goto bad_output;
3292
3293 case PNG_CMAP_RGB_ALPHA:
3294 /* Output must be 8-bit sRGB encoded RGBA */
3295 if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
3296 info_ptr->bit_depth == 8 &&
3297 png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3298 image->colormap_entries == 244 /* 216 + 1 + 27 */)
3299 break;
3300
3301 /* goto bad_output; */
3302 /* FALL THROUGH */
3303
3304 default:
3305 bad_output:
3306 png_error(png_ptr, "bad color-map processing (internal error)");
3307 }
3308
3309 /* Now read the rows. Do this here if it is possible to read directly into
3310 * the output buffer, otherwise allocate a local row buffer of the maximum
3311 * size libpng requires and call the relevant processing routine safely.
3312 */
3313 {
3314 png_voidp first_row = display->buffer;
3315 ptrdiff_t row_bytes = display->row_stride;
3316
3317 /* The following expression is designed to work correctly whether it gives
3318 * a signed or an unsigned result.
3319 */
3320 if (row_bytes < 0)
3321 {
3322 char *ptr = png_voidcast(char*, first_row);
3323 ptr += (image->height-1) * (-row_bytes);
3324 first_row = png_voidcast(png_voidp, ptr);
3325 }
3326
3327 display->first_row = first_row;
3328 display->row_bytes = row_bytes;
3329 }
3330
3331 if (passes == 0)
3332 {
3333 int result;
3334 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3335
3336 display->local_row = row;
3337 result = png_safe_execute(image, png_image_read_and_map, display);
3338 display->local_row = NULL;
3339 png_free(png_ptr, row);
3340
3341 return result;
3342 }
3343
3344 else
3345 {
3346 png_alloc_size_t row_bytes = display->row_bytes;
3347
3348 while (--passes >= 0)
3349 {
3350 png_uint_32 y = image->height;
3351 png_bytep row = png_voidcast(png_bytep, display->first_row);
3352
3353 while (y-- > 0)
3354 {
3355 png_read_row(png_ptr, row, NULL);
3356 row += row_bytes;
3357 }
3358 }
3359
3360 return 1;
3361 }
3362}
3363
3364/* Just the row reading part of png_image_read. */
3365static int
3366png_image_read_composite(png_voidp argument)
3367{
3368 png_image_read_control *display = png_voidcast(png_image_read_control*,
3369 argument);
3370 png_imagep image = display->image;
3371 png_structrp png_ptr = image->opaque->png_ptr;
3372 int passes;
3373
3374 switch (png_ptr->interlaced)
3375 {
3376 case PNG_INTERLACE_NONE:
3377 passes = 1;
3378 break;
3379
3380 case PNG_INTERLACE_ADAM7:
3381 passes = PNG_INTERLACE_ADAM7_PASSES;
3382 break;
3383
3384 default:
Chris Craikb50c2172013-07-29 15:28:30 -07003385 png_error(png_ptr, "unknown interlace type");
3386 }
3387
3388 {
3389 png_uint_32 height = image->height;
3390 png_uint_32 width = image->width;
3391 ptrdiff_t step_row = display->row_bytes;
3392 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
3393 int pass;
3394
3395 for (pass = 0; pass < passes; ++pass)
3396 {
3397 unsigned int startx, stepx, stepy;
3398 png_uint_32 y;
3399
3400 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3401 {
3402 /* The row may be empty for a short image: */
3403 if (PNG_PASS_COLS(width, pass) == 0)
3404 continue;
3405
3406 startx = PNG_PASS_START_COL(pass) * channels;
3407 stepx = PNG_PASS_COL_OFFSET(pass) * channels;
3408 y = PNG_PASS_START_ROW(pass);
3409 stepy = PNG_PASS_ROW_OFFSET(pass);
3410 }
3411
3412 else
3413 {
3414 y = 0;
3415 startx = 0;
3416 stepx = channels;
3417 stepy = 1;
3418 }
3419
3420 for (; y<height; y += stepy)
3421 {
3422 png_bytep inrow = png_voidcast(png_bytep, display->local_row);
3423 png_bytep outrow;
3424 png_const_bytep end_row;
3425
3426 /* Read the row, which is packed: */
3427 png_read_row(png_ptr, inrow, NULL);
3428
3429 outrow = png_voidcast(png_bytep, display->first_row);
3430 outrow += y * step_row;
3431 end_row = outrow + width * channels;
3432
3433 /* Now do the composition on each pixel in this row. */
3434 outrow += startx;
3435 for (; outrow < end_row; outrow += stepx)
3436 {
3437 png_byte alpha = inrow[channels];
3438
3439 if (alpha > 0) /* else no change to the output */
3440 {
3441 unsigned int c;
3442
3443 for (c=0; c<channels; ++c)
3444 {
3445 png_uint_32 component = inrow[c];
3446
3447 if (alpha < 255) /* else just use component */
3448 {
3449 /* This is PNG_OPTIMIZED_ALPHA, the component value
3450 * is a linear 8-bit value. Combine this with the
3451 * current outrow[c] value which is sRGB encoded.
3452 * Arithmetic here is 16-bits to preserve the output
3453 * values correctly.
3454 */
3455 component *= 257*255; /* =65535 */
3456 component += (255-alpha)*png_sRGB_table[outrow[c]];
3457
3458 /* So 'component' is scaled by 255*65535 and is
3459 * therefore appropriate for the sRGB to linear
3460 * conversion table.
3461 */
3462 component = PNG_sRGB_FROM_LINEAR(component);
3463 }
3464
3465 outrow[c] = (png_byte)component;
3466 }
3467 }
3468
3469 inrow += channels+1; /* components and alpha channel */
3470 }
3471 }
3472 }
3473 }
3474
3475 return 1;
3476}
3477
3478/* The do_local_background case; called when all the following transforms are to
3479 * be done:
3480 *
3481 * PNG_RGB_TO_GRAY
3482 * PNG_COMPOSITE
3483 * PNG_GAMMA
3484 *
3485 * This is a work-round for the fact that both the PNG_RGB_TO_GRAY and
3486 * PNG_COMPOSITE code performs gamma correction, so we get double gamma
3487 * correction. The fix-up is to prevent the PNG_COMPOSITE operation happening
3488 * inside libpng, so this routine sees an 8 or 16-bit gray+alpha row and handles
3489 * the removal or pre-multiplication of the alpha channel.
3490 */
3491static int
3492png_image_read_background(png_voidp argument)
3493{
3494 png_image_read_control *display = png_voidcast(png_image_read_control*,
3495 argument);
3496 png_imagep image = display->image;
3497 png_structrp png_ptr = image->opaque->png_ptr;
3498 png_inforp info_ptr = image->opaque->info_ptr;
3499 png_uint_32 height = image->height;
3500 png_uint_32 width = image->width;
3501 int pass, passes;
3502
3503 /* Double check the convoluted logic below. We expect to get here with
3504 * libpng doing rgb to gray and gamma correction but background processing
3505 * left to the png_image_read_background function. The rows libpng produce
3506 * might be 8 or 16-bit but should always have two channels; gray plus alpha.
3507 */
3508 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
3509 png_error(png_ptr, "lost rgb to gray");
3510
3511 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
3512 png_error(png_ptr, "unexpected compose");
3513
3514 if (png_get_channels(png_ptr, info_ptr) != 2)
3515 png_error(png_ptr, "lost/gained channels");
3516
3517 /* Expect the 8-bit case to always remove the alpha channel */
3518 if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
3519 (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
3520 png_error(png_ptr, "unexpected 8-bit transformation");
3521
3522 switch (png_ptr->interlaced)
3523 {
3524 case PNG_INTERLACE_NONE:
3525 passes = 1;
3526 break;
3527
3528 case PNG_INTERLACE_ADAM7:
3529 passes = PNG_INTERLACE_ADAM7_PASSES;
3530 break;
3531
3532 default:
Chris Craikb50c2172013-07-29 15:28:30 -07003533 png_error(png_ptr, "unknown interlace type");
3534 }
3535
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303536 /* Use direct access to info_ptr here because otherwise the simplified API
3537 * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
3538 * checking the value after libpng expansions, not the original value in the
3539 * PNG.
3540 */
3541 switch (info_ptr->bit_depth)
Chris Craikb50c2172013-07-29 15:28:30 -07003542 {
3543 default:
3544 png_error(png_ptr, "unexpected bit depth");
3545 break;
3546
3547 case 8:
3548 /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303549 * to be removed by composing on a background: either the row if
Chris Craikb50c2172013-07-29 15:28:30 -07003550 * display->background is NULL or display->background->green if not.
3551 * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3552 */
3553 {
3554 png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3555 ptrdiff_t step_row = display->row_bytes;
3556
3557 for (pass = 0; pass < passes; ++pass)
3558 {
3559 png_bytep row = png_voidcast(png_bytep,
3560 display->first_row);
3561 unsigned int startx, stepx, stepy;
3562 png_uint_32 y;
3563
3564 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3565 {
3566 /* The row may be empty for a short image: */
3567 if (PNG_PASS_COLS(width, pass) == 0)
3568 continue;
3569
3570 startx = PNG_PASS_START_COL(pass);
3571 stepx = PNG_PASS_COL_OFFSET(pass);
3572 y = PNG_PASS_START_ROW(pass);
3573 stepy = PNG_PASS_ROW_OFFSET(pass);
3574 }
3575
3576 else
3577 {
3578 y = 0;
3579 startx = 0;
3580 stepx = stepy = 1;
3581 }
3582
3583 if (display->background == NULL)
3584 {
3585 for (; y<height; y += stepy)
3586 {
3587 png_bytep inrow = png_voidcast(png_bytep,
3588 display->local_row);
3589 png_bytep outrow = first_row + y * step_row;
3590 png_const_bytep end_row = outrow + width;
3591
3592 /* Read the row, which is packed: */
3593 png_read_row(png_ptr, inrow, NULL);
3594
3595 /* Now do the composition on each pixel in this row. */
3596 outrow += startx;
3597 for (; outrow < end_row; outrow += stepx)
3598 {
3599 png_byte alpha = inrow[1];
3600
3601 if (alpha > 0) /* else no change to the output */
3602 {
3603 png_uint_32 component = inrow[0];
3604
3605 if (alpha < 255) /* else just use component */
3606 {
3607 /* Since PNG_OPTIMIZED_ALPHA was not set it is
3608 * necessary to invert the sRGB transfer
3609 * function and multiply the alpha out.
3610 */
3611 component = png_sRGB_table[component] * alpha;
3612 component += png_sRGB_table[outrow[0]] *
3613 (255-alpha);
3614 component = PNG_sRGB_FROM_LINEAR(component);
3615 }
3616
3617 outrow[0] = (png_byte)component;
3618 }
3619
3620 inrow += 2; /* gray and alpha channel */
3621 }
3622 }
3623 }
3624
3625 else /* constant background value */
3626 {
3627 png_byte background8 = display->background->green;
3628 png_uint_16 background = png_sRGB_table[background8];
3629
3630 for (; y<height; y += stepy)
3631 {
3632 png_bytep inrow = png_voidcast(png_bytep,
3633 display->local_row);
3634 png_bytep outrow = first_row + y * step_row;
3635 png_const_bytep end_row = outrow + width;
3636
3637 /* Read the row, which is packed: */
3638 png_read_row(png_ptr, inrow, NULL);
3639
3640 /* Now do the composition on each pixel in this row. */
3641 outrow += startx;
3642 for (; outrow < end_row; outrow += stepx)
3643 {
3644 png_byte alpha = inrow[1];
3645
3646 if (alpha > 0) /* else use background */
3647 {
3648 png_uint_32 component = inrow[0];
3649
3650 if (alpha < 255) /* else just use component */
3651 {
3652 component = png_sRGB_table[component] * alpha;
3653 component += background * (255-alpha);
3654 component = PNG_sRGB_FROM_LINEAR(component);
3655 }
3656
3657 outrow[0] = (png_byte)component;
3658 }
3659
3660 else
3661 outrow[0] = background8;
3662
3663 inrow += 2; /* gray and alpha channel */
3664 }
3665
3666 row += display->row_bytes;
3667 }
3668 }
3669 }
3670 }
3671 break;
3672
3673 case 16:
3674 /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3675 * still be done and, maybe, the alpha channel removed. This code also
3676 * handles the alpha-first option.
3677 */
3678 {
3679 png_uint_16p first_row = png_voidcast(png_uint_16p,
3680 display->first_row);
3681 /* The division by two is safe because the caller passed in a
3682 * stride which was multiplied by 2 (below) to get row_bytes.
3683 */
3684 ptrdiff_t step_row = display->row_bytes / 2;
3685 int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
3686 unsigned int outchannels = 1+preserve_alpha;
3687 int swap_alpha = 0;
3688
Sireesh Tripurarib478e662014-05-09 15:15:10 +05303689# ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
3690 if (preserve_alpha && (image->format & PNG_FORMAT_FLAG_AFIRST))
3691 swap_alpha = 1;
3692# endif
Chris Craikb50c2172013-07-29 15:28:30 -07003693
3694 for (pass = 0; pass < passes; ++pass)
3695 {
3696 unsigned int startx, stepx, stepy;
3697 png_uint_32 y;
3698
3699 /* The 'x' start and step are adjusted to output components here.
3700 */
3701 if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3702 {
3703 /* The row may be empty for a short image: */
3704 if (PNG_PASS_COLS(width, pass) == 0)
3705 continue;
3706
3707 startx = PNG_PASS_START_COL(pass) * outchannels;
3708 stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
3709 y = PNG_PASS_START_ROW(pass);
3710 stepy = PNG_PASS_ROW_OFFSET(pass);
3711 }
3712
3713 else
3714 {
3715 y = 0;
3716 startx = 0;
3717 stepx = outchannels;
3718 stepy = 1;
3719 }
3720
3721 for (; y<height; y += stepy)
3722 {
3723 png_const_uint_16p inrow;
3724 png_uint_16p outrow = first_row + y*step_row;
3725 png_uint_16p end_row = outrow + width * outchannels;
3726
3727 /* Read the row, which is packed: */
3728 png_read_row(png_ptr, png_voidcast(png_bytep,
3729 display->local_row), NULL);
3730 inrow = png_voidcast(png_const_uint_16p, display->local_row);
3731
3732 /* Now do the pre-multiplication on each pixel in this row.
3733 */
3734 outrow += startx;
3735 for (; outrow < end_row; outrow += stepx)
3736 {
3737 png_uint_32 component = inrow[0];
3738 png_uint_16 alpha = inrow[1];
3739
3740 if (alpha > 0) /* else 0 */
3741 {
3742 if (alpha < 65535) /* else just use component */
3743 {
3744 component *= alpha;
3745 component += 32767;
3746 component /= 65535;
3747 }
3748 }
3749
3750 else
3751 component = 0;
3752
3753 outrow[swap_alpha] = (png_uint_16)component;
3754 if (preserve_alpha)
3755 outrow[1 ^ swap_alpha] = alpha;
3756
3757 inrow += 2; /* components and alpha channel */
3758 }
3759 }
3760 }
3761 }
3762 break;
3763 }
3764
3765 return 1;
3766}
3767
3768/* The guts of png_image_finish_read as a png_safe_execute callback. */
3769static int
3770png_image_read_direct(png_voidp argument)
3771{
3772 png_image_read_control *display = png_voidcast(png_image_read_control*,
3773 argument);
3774 png_imagep image = display->image;
3775 png_structrp png_ptr = image->opaque->png_ptr;
3776 png_inforp info_ptr = image->opaque->info_ptr;
3777
3778 png_uint_32 format = image->format;
3779 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
3780 int do_local_compose = 0;
3781 int do_local_background = 0; /* to avoid double gamma correction bug */
3782 int passes = 0;
3783
3784 /* Add transforms to ensure the correct output format is produced then check
3785 * that the required implementation support is there. Always expand; always
3786 * need 8 bits minimum, no palette and expanded tRNS.
3787 */
3788 png_set_expand(png_ptr);
3789
3790 /* Now check the format to see if it was modified. */
3791 {
3792 png_uint_32 base_format = png_image_format(png_ptr) &
3793 ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
3794 png_uint_32 change = format ^ base_format;
3795 png_fixed_point output_gamma;
3796 int mode; /* alpha mode */
3797
3798 /* Do this first so that we have a record if rgb to gray is happening. */
3799 if (change & PNG_FORMAT_FLAG_COLOR)
3800 {
3801 /* gray<->color transformation required. */
3802 if (format & PNG_FORMAT_FLAG_COLOR)
3803 png_set_gray_to_rgb(png_ptr);
3804
3805 else
3806 {
3807 /* libpng can't do both rgb to gray and
3808 * background/pre-multiplication if there is also significant gamma
3809 * correction, because both operations require linear colors and
3810 * the code only supports one transform doing the gamma correction.
3811 * Handle this by doing the pre-multiplication or background
3812 * operation in this code, if necessary.
3813 *
3814 * TODO: fix this by rewriting pngrtran.c (!)
3815 *
3816 * For the moment (given that fixing this in pngrtran.c is an
3817 * enormous change) 'do_local_background' is used to indicate that
3818 * the problem exists.
3819 */
3820 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3821 do_local_background = 1/*maybe*/;
3822
3823 png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
3824 PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
3825 }
3826
3827 change &= ~PNG_FORMAT_FLAG_COLOR;
3828 }
3829
3830 /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3831 */
3832 {
3833 png_fixed_point input_gamma_default;
3834
3835 if ((base_format & PNG_FORMAT_FLAG_LINEAR) &&
3836 (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
3837 input_gamma_default = PNG_GAMMA_LINEAR;
3838 else
3839 input_gamma_default = PNG_DEFAULT_sRGB;
3840
3841 /* Call png_set_alpha_mode to set the default for the input gamma; the
3842 * output gamma is set by a second call below.
3843 */
3844 png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
3845 }
3846
3847 if (linear)
3848 {
3849 /* If there *is* an alpha channel in the input it must be multiplied
3850 * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3851 */
3852 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3853 mode = PNG_ALPHA_STANDARD; /* associated alpha */
3854
3855 else
3856 mode = PNG_ALPHA_PNG;
3857
3858 output_gamma = PNG_GAMMA_LINEAR;
3859 }
3860
3861 else
3862 {
3863 mode = PNG_ALPHA_PNG;
3864 output_gamma = PNG_DEFAULT_sRGB;
3865 }
3866
3867 /* If 'do_local_background' is set check for the presence of gamma
3868 * correction; this is part of the work-round for the libpng bug
3869 * described above.
3870 *
3871 * TODO: fix libpng and remove this.
3872 */
3873 if (do_local_background)
3874 {
3875 png_fixed_point gtest;
3876
3877 /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3878 * gamma correction, the screen gamma hasn't been set on png_struct
3879 * yet; it's set below. png_struct::gamma, however, is set to the
3880 * final value.
3881 */
3882 if (png_muldiv(&gtest, output_gamma, png_ptr->colorspace.gamma,
3883 PNG_FP_1) && !png_gamma_significant(gtest))
3884 do_local_background = 0;
3885
3886 else if (mode == PNG_ALPHA_STANDARD)
3887 {
3888 do_local_background = 2/*required*/;
3889 mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
3890 }
3891
3892 /* else leave as 1 for the checks below */
3893 }
3894
3895 /* If the bit-depth changes then handle that here. */
3896 if (change & PNG_FORMAT_FLAG_LINEAR)
3897 {
3898 if (linear /*16-bit output*/)
3899 png_set_expand_16(png_ptr);
3900
3901 else /* 8-bit output */
3902 png_set_scale_16(png_ptr);
3903
3904 change &= ~PNG_FORMAT_FLAG_LINEAR;
3905 }
3906
3907 /* Now the background/alpha channel changes. */
3908 if (change & PNG_FORMAT_FLAG_ALPHA)
3909 {
3910 /* Removing an alpha channel requires composition for the 8-bit
3911 * formats; for the 16-bit it is already done, above, by the
3912 * pre-multiplication and the channel just needs to be stripped.
3913 */
3914 if (base_format & PNG_FORMAT_FLAG_ALPHA)
3915 {
3916 /* If RGB->gray is happening the alpha channel must be left and the
3917 * operation completed locally.
3918 *
3919 * TODO: fix libpng and remove this.
3920 */
3921 if (do_local_background)
3922 do_local_background = 2/*required*/;
3923
3924 /* 16-bit output: just remove the channel */
3925 else if (linear) /* compose on black (well, pre-multiply) */
3926 png_set_strip_alpha(png_ptr);
3927
3928 /* 8-bit output: do an appropriate compose */
3929 else if (display->background != NULL)
3930 {
3931 png_color_16 c;
3932
3933 c.index = 0; /*unused*/
3934 c.red = display->background->red;
3935 c.green = display->background->green;
3936 c.blue = display->background->blue;
3937 c.gray = display->background->green;
3938
3939 /* This is always an 8-bit sRGB value, using the 'green' channel
3940 * for gray is much better than calculating the luminance here;
3941 * we can get off-by-one errors in that calculation relative to
3942 * the app expectations and that will show up in transparent
3943 * pixels.
3944 */
3945 png_set_background_fixed(png_ptr, &c,
3946 PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
3947 0/*gamma: not used*/);
3948 }
3949
3950 else /* compose on row: implemented below. */
3951 {
3952 do_local_compose = 1;
3953 /* This leaves the alpha channel in the output, so it has to be
3954 * removed by the code below. Set the encoding to the 'OPTIMIZE'
3955 * one so the code only has to hack on the pixels that require
3956 * composition.
3957 */
3958 mode = PNG_ALPHA_OPTIMIZED;
3959 }
3960 }
3961
3962 else /* output needs an alpha channel */
3963 {
3964 /* This is tricky because it happens before the swap operation has
3965 * been accomplished; however, the swap does *not* swap the added
3966 * alpha channel (weird API), so it must be added in the correct
3967 * place.
3968 */
3969 png_uint_32 filler; /* opaque filler */
3970 int where;
3971
3972 if (linear)
3973 filler = 65535;
3974
3975 else
3976 filler = 255;
3977
3978# ifdef PNG_FORMAT_AFIRST_SUPPORTED
3979 if (format & PNG_FORMAT_FLAG_AFIRST)
3980 {
3981 where = PNG_FILLER_BEFORE;
3982 change &= ~PNG_FORMAT_FLAG_AFIRST;
3983 }
3984
3985 else
3986# endif
3987 where = PNG_FILLER_AFTER;
3988
3989 png_set_add_alpha(png_ptr, filler, where);
3990 }
3991
3992 /* This stops the (irrelevant) call to swap_alpha below. */
3993 change &= ~PNG_FORMAT_FLAG_ALPHA;
3994 }
3995
3996 /* Now set the alpha mode correctly; this is always done, even if there is
3997 * no alpha channel in either the input or the output because it correctly
3998 * sets the output gamma.
3999 */
4000 png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
4001
4002# ifdef PNG_FORMAT_BGR_SUPPORTED
4003 if (change & PNG_FORMAT_FLAG_BGR)
4004 {
4005 /* Check only the output format; PNG is never BGR; don't do this if
4006 * the output is gray, but fix up the 'format' value in that case.
4007 */
4008 if (format & PNG_FORMAT_FLAG_COLOR)
4009 png_set_bgr(png_ptr);
4010
4011 else
4012 format &= ~PNG_FORMAT_FLAG_BGR;
4013
4014 change &= ~PNG_FORMAT_FLAG_BGR;
4015 }
4016# endif
4017
4018# ifdef PNG_FORMAT_AFIRST_SUPPORTED
4019 if (change & PNG_FORMAT_FLAG_AFIRST)
4020 {
4021 /* Only relevant if there is an alpha channel - it's particularly
4022 * important to handle this correctly because do_local_compose may
4023 * be set above and then libpng will keep the alpha channel for this
4024 * code to remove.
4025 */
4026 if (format & PNG_FORMAT_FLAG_ALPHA)
4027 {
4028 /* Disable this if doing a local background,
4029 * TODO: remove this when local background is no longer required.
4030 */
4031 if (do_local_background != 2)
4032 png_set_swap_alpha(png_ptr);
4033 }
4034
4035 else
4036 format &= ~PNG_FORMAT_FLAG_AFIRST;
4037
4038 change &= ~PNG_FORMAT_FLAG_AFIRST;
4039 }
4040# endif
4041
4042 /* If the *output* is 16-bit then we need to check for a byte-swap on this
4043 * architecture.
4044 */
4045 if (linear)
4046 {
4047 PNG_CONST png_uint_16 le = 0x0001;
4048
4049 if (*(png_const_bytep)&le)
4050 png_set_swap(png_ptr);
4051 }
4052
4053 /* If change is not now 0 some transformation is missing - error out. */
4054 if (change)
4055 png_error(png_ptr, "png_read_image: unsupported transformation");
4056 }
4057
4058 PNG_SKIP_CHUNKS(png_ptr);
4059
4060 /* Update the 'info' structure and make sure the result is as required; first
4061 * make sure to turn on the interlace handling if it will be required
4062 * (because it can't be turned on *after* the call to png_read_update_info!)
4063 *
4064 * TODO: remove the do_local_background fixup below.
4065 */
4066 if (!do_local_compose && do_local_background != 2)
4067 passes = png_set_interlace_handling(png_ptr);
4068
4069 png_read_update_info(png_ptr, info_ptr);
4070
4071 {
4072 png_uint_32 info_format = 0;
4073
4074 if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
4075 info_format |= PNG_FORMAT_FLAG_COLOR;
4076
4077 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
4078 {
4079 /* do_local_compose removes this channel below. */
4080 if (!do_local_compose)
4081 {
4082 /* do_local_background does the same if required. */
4083 if (do_local_background != 2 ||
4084 (format & PNG_FORMAT_FLAG_ALPHA) != 0)
4085 info_format |= PNG_FORMAT_FLAG_ALPHA;
4086 }
4087 }
4088
4089 else if (do_local_compose) /* internal error */
4090 png_error(png_ptr, "png_image_read: alpha channel lost");
4091
4092 if (info_ptr->bit_depth == 16)
4093 info_format |= PNG_FORMAT_FLAG_LINEAR;
4094
4095# ifdef PNG_FORMAT_BGR_SUPPORTED
4096 if (png_ptr->transformations & PNG_BGR)
4097 info_format |= PNG_FORMAT_FLAG_BGR;
4098# endif
4099
4100# ifdef PNG_FORMAT_AFIRST_SUPPORTED
4101 if (do_local_background == 2)
4102 {
4103 if (format & PNG_FORMAT_FLAG_AFIRST)
4104 info_format |= PNG_FORMAT_FLAG_AFIRST;
4105 }
4106
4107 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
4108 ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
4109 (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
4110 {
4111 if (do_local_background == 2)
4112 png_error(png_ptr, "unexpected alpha swap transformation");
4113
4114 info_format |= PNG_FORMAT_FLAG_AFIRST;
4115 }
4116# endif
4117
4118 /* This is actually an internal error. */
4119 if (info_format != format)
4120 png_error(png_ptr, "png_read_image: invalid transformations");
4121 }
4122
4123 /* Now read the rows. If do_local_compose is set then it is necessary to use
4124 * a local row buffer. The output will be GA, RGBA or BGRA and must be
4125 * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
4126 * display acts as a flag.
4127 */
4128 {
4129 png_voidp first_row = display->buffer;
4130 ptrdiff_t row_bytes = display->row_stride;
4131
4132 if (linear)
4133 row_bytes *= 2;
4134
4135 /* The following expression is designed to work correctly whether it gives
4136 * a signed or an unsigned result.
4137 */
4138 if (row_bytes < 0)
4139 {
4140 char *ptr = png_voidcast(char*, first_row);
4141 ptr += (image->height-1) * (-row_bytes);
4142 first_row = png_voidcast(png_voidp, ptr);
4143 }
4144
4145 display->first_row = first_row;
4146 display->row_bytes = row_bytes;
4147 }
4148
4149 if (do_local_compose)
4150 {
4151 int result;
4152 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
4153
4154 display->local_row = row;
4155 result = png_safe_execute(image, png_image_read_composite, display);
4156 display->local_row = NULL;
4157 png_free(png_ptr, row);
4158
4159 return result;
4160 }
4161
4162 else if (do_local_background == 2)
4163 {
4164 int result;
4165 png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
4166
4167 display->local_row = row;
4168 result = png_safe_execute(image, png_image_read_background, display);
4169 display->local_row = NULL;
4170 png_free(png_ptr, row);
4171
4172 return result;
4173 }
4174
4175 else
4176 {
4177 png_alloc_size_t row_bytes = display->row_bytes;
4178
4179 while (--passes >= 0)
4180 {
4181 png_uint_32 y = image->height;
4182 png_bytep row = png_voidcast(png_bytep, display->first_row);
4183
4184 while (y-- > 0)
4185 {
4186 png_read_row(png_ptr, row, NULL);
4187 row += row_bytes;
4188 }
4189 }
4190
4191 return 1;
4192 }
4193}
4194
4195int PNGAPI
4196png_image_finish_read(png_imagep image, png_const_colorp background,
4197 void *buffer, png_int_32 row_stride, void *colormap)
4198{
4199 if (image != NULL && image->version == PNG_IMAGE_VERSION)
4200 {
4201 png_uint_32 check;
4202
4203 if (row_stride == 0)
4204 row_stride = PNG_IMAGE_ROW_STRIDE(*image);
4205
4206 if (row_stride < 0)
4207 check = -row_stride;
4208
4209 else
4210 check = row_stride;
4211
4212 if (image->opaque != NULL && buffer != NULL &&
4213 check >= PNG_IMAGE_ROW_STRIDE(*image))
4214 {
4215 if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
4216 (image->colormap_entries > 0 && colormap != NULL))
4217 {
4218 int result;
4219 png_image_read_control display;
4220
4221 memset(&display, 0, (sizeof display));
4222 display.image = image;
4223 display.buffer = buffer;
4224 display.row_stride = row_stride;
4225 display.colormap = colormap;
4226 display.background = background;
4227 display.local_row = NULL;
4228
4229 /* Choose the correct 'end' routine; for the color-map case all the
4230 * setup has already been done.
4231 */
4232 if (image->format & PNG_FORMAT_FLAG_COLORMAP)
4233 result =
4234 png_safe_execute(image, png_image_read_colormap, &display) &&
4235 png_safe_execute(image, png_image_read_colormapped, &display);
4236
4237 else
4238 result =
4239 png_safe_execute(image, png_image_read_direct, &display);
4240
4241 png_image_free(image);
4242 return result;
4243 }
4244
4245 else
4246 return png_image_error(image,
4247 "png_image_finish_read[color-map]: no color-map");
4248 }
4249
4250 else
4251 return png_image_error(image,
4252 "png_image_finish_read: invalid argument");
4253 }
4254
4255 else if (image != NULL)
4256 return png_image_error(image,
4257 "png_image_finish_read: damaged PNG_IMAGE_VERSION");
4258
4259 return 0;
4260}
4261
4262#endif /* PNG_SIMPLIFIED_READ_SUPPORTED */
The Android Open Source Project893912b2009-03-03 19:30:05 -08004263#endif /* PNG_READ_SUPPORTED */