The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1 | |
| 2 | /* pngread.c - read a PNG file |
| 3 | * |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4 | * Last changed in libpng 1.6.17 [March 26, 2015] |
| 5 | * Copyright (c) 1998-2015 Glenn Randers-Pehrson |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 6 | * (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 Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 9 | * 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 13 | * This file contains routines that an application calls directly to |
| 14 | * read a PNG file or stream. |
| 15 | */ |
| 16 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 17 | #include "pngpriv.h" |
| 18 | #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED) |
| 19 | # include <errno.h> |
| 20 | #endif |
| 21 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 22 | #ifdef PNG_READ_SUPPORTED |
| 23 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 24 | /* Create a PNG structure for reading, and allocate any memory needed. */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 25 | PNG_FUNCTION(png_structp,PNGAPI |
| 26 | png_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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 28 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 29 | #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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 37 | /* Alternate create PNG structure for reading, and allocate any memory |
| 38 | * needed. |
| 39 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 40 | PNG_FUNCTION(png_structp,PNGAPI |
| 41 | png_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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 44 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 45 | png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr, |
| 46 | error_fn, warn_fn, mem_ptr, malloc_fn, free_fn); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 47 | #endif /* USER_MEM */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 48 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 49 | if (png_ptr != NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 50 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 51 | png_ptr->mode = PNG_IS_READ_STRUCT; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 52 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 53 | /* 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 Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 55 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 56 | # ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
| 57 | png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE; |
| 58 | # endif |
| 59 | |
| 60 | # ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED |
| 61 | png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; |
| 62 | |
| 63 | /* In stable builds only warn if an application error can be completely |
| 64 | * handled. |
| 65 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 66 | # if PNG_RELEASE_BUILD |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 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 Tripurari | b0277d0 | 2014-05-09 15:39:12 +0530 | [diff] [blame] | 76 | |
| 77 | #ifdef PNG_INDEX_SUPPORTED |
| 78 | png_ptr->index = NULL; |
| 79 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 82 | return png_ptr; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 83 | } |
| 84 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 85 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 86 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 87 | /* 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 | */ |
| 95 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 96 | png_read_info(png_structrp png_ptr, png_inforp info_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 97 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 98 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 99 | int keep; |
| 100 | #endif |
| 101 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 102 | png_debug(1, "in png_read_info"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 103 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 104 | if (png_ptr == NULL || info_ptr == NULL) |
| 105 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 106 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 107 | /* Read and check the PNG file signature. */ |
| 108 | png_read_sig(png_ptr, info_ptr); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 109 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 110 | for (;;) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 111 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 112 | png_uint_32 length = png_read_chunk_header(png_ptr); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 113 | png_uint_32 chunk_name = png_ptr->chunk_name; |
| 114 | |
| 115 | /* IDAT logic needs to happen here to simplify getting the two flags |
| 116 | * right. |
| 117 | */ |
| 118 | if (chunk_name == png_IDAT) |
| 119 | { |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 120 | if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 121 | png_chunk_error(png_ptr, "Missing IHDR before IDAT"); |
| 122 | |
| 123 | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 124 | (png_ptr->mode & PNG_HAVE_PLTE) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 125 | png_chunk_error(png_ptr, "Missing PLTE before IDAT"); |
| 126 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 127 | else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 128 | png_chunk_benign_error(png_ptr, "Too many IDATs found"); |
| 129 | |
| 130 | png_ptr->mode |= PNG_HAVE_IDAT; |
| 131 | } |
| 132 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 133 | else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 134 | png_ptr->mode |= PNG_AFTER_IDAT; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 135 | |
| 136 | /* This should be a binary subdivision search or a hash for |
| 137 | * matching the chunk name rather than a linear search. |
| 138 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 139 | if (chunk_name == png_IHDR) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 140 | png_handle_IHDR(png_ptr, info_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 141 | |
| 142 | else if (chunk_name == png_IEND) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 143 | png_handle_IEND(png_ptr, info_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 144 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 145 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 146 | else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 147 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 148 | png_handle_unknown(png_ptr, info_ptr, length, keep); |
| 149 | |
| 150 | if (chunk_name == png_PLTE) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 151 | png_ptr->mode |= PNG_HAVE_PLTE; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 152 | |
| 153 | else if (chunk_name == png_IDAT) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 154 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 155 | png_ptr->idat_size = 0; /* It has been consumed */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | } |
| 159 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 160 | else if (chunk_name == png_PLTE) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 161 | png_handle_PLTE(png_ptr, info_ptr, length); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 162 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 163 | else if (chunk_name == png_IDAT) |
| 164 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 165 | png_ptr->idat_size = length; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 166 | break; |
| 167 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 168 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 169 | #ifdef PNG_READ_bKGD_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 170 | else if (chunk_name == png_bKGD) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 171 | png_handle_bKGD(png_ptr, info_ptr, length); |
| 172 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 173 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 174 | #ifdef PNG_READ_cHRM_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 175 | else if (chunk_name == png_cHRM) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 176 | png_handle_cHRM(png_ptr, info_ptr, length); |
| 177 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 178 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 179 | #ifdef PNG_READ_gAMA_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 180 | else if (chunk_name == png_gAMA) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 181 | png_handle_gAMA(png_ptr, info_ptr, length); |
| 182 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 183 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 184 | #ifdef PNG_READ_hIST_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 185 | else if (chunk_name == png_hIST) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 186 | png_handle_hIST(png_ptr, info_ptr, length); |
| 187 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 188 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 189 | #ifdef PNG_READ_oFFs_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 190 | else if (chunk_name == png_oFFs) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 191 | png_handle_oFFs(png_ptr, info_ptr, length); |
| 192 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 193 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 194 | #ifdef PNG_READ_pCAL_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 195 | else if (chunk_name == png_pCAL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 196 | png_handle_pCAL(png_ptr, info_ptr, length); |
| 197 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 198 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 199 | #ifdef PNG_READ_sCAL_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 200 | else if (chunk_name == png_sCAL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 201 | png_handle_sCAL(png_ptr, info_ptr, length); |
| 202 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 203 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 204 | #ifdef PNG_READ_pHYs_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 205 | else if (chunk_name == png_pHYs) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 206 | png_handle_pHYs(png_ptr, info_ptr, length); |
| 207 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 208 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 209 | #ifdef PNG_READ_sBIT_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 210 | else if (chunk_name == png_sBIT) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 211 | png_handle_sBIT(png_ptr, info_ptr, length); |
| 212 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 213 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 214 | #ifdef PNG_READ_sRGB_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 215 | else if (chunk_name == png_sRGB) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 216 | png_handle_sRGB(png_ptr, info_ptr, length); |
| 217 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 218 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 219 | #ifdef PNG_READ_iCCP_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 220 | else if (chunk_name == png_iCCP) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 221 | png_handle_iCCP(png_ptr, info_ptr, length); |
| 222 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 223 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 224 | #ifdef PNG_READ_sPLT_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 225 | else if (chunk_name == png_sPLT) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 226 | png_handle_sPLT(png_ptr, info_ptr, length); |
| 227 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 228 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 229 | #ifdef PNG_READ_tEXt_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 230 | else if (chunk_name == png_tEXt) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 231 | png_handle_tEXt(png_ptr, info_ptr, length); |
| 232 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 233 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 234 | #ifdef PNG_READ_tIME_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 235 | else if (chunk_name == png_tIME) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 236 | png_handle_tIME(png_ptr, info_ptr, length); |
| 237 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 238 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 239 | #ifdef PNG_READ_tRNS_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 240 | else if (chunk_name == png_tRNS) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 241 | png_handle_tRNS(png_ptr, info_ptr, length); |
| 242 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 243 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 244 | #ifdef PNG_READ_zTXt_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 245 | else if (chunk_name == png_zTXt) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 246 | png_handle_zTXt(png_ptr, info_ptr, length); |
| 247 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 248 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 249 | #ifdef PNG_READ_iTXt_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 250 | else if (chunk_name == png_iTXt) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 251 | png_handle_iTXt(png_ptr, info_ptr, length); |
| 252 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 253 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 254 | else |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 255 | png_handle_unknown(png_ptr, info_ptr, length, |
| 256 | PNG_HANDLE_CHUNK_AS_DEFAULT); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 259 | #endif /* SEQUENTIAL_READ */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 260 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 261 | /* Optional call to update the users info_ptr structure */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 262 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 263 | png_read_update_info(png_structrp png_ptr, png_inforp info_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 264 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 265 | png_debug(1, "in png_read_update_info"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 266 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 267 | if (png_ptr != NULL) |
| 268 | { |
Sireesh Tripurari | b0277d0 | 2014-05-09 15:39:12 +0530 | [diff] [blame] | 269 | #ifdef PNG_INDEX_SUPPORTED |
| 270 | if (png_ptr->index) { |
| 271 | png_read_start_row(png_ptr); |
| 272 | } |
| 273 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 274 | 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 Tripurari | b0277d0 | 2014-05-09 15:39:12 +0530 | [diff] [blame] | 284 | #ifndef PNG_INDEX_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 285 | /* 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 Tripurari | b0277d0 | 2014-05-09 15:39:12 +0530 | [diff] [blame] | 289 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 290 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 293 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 294 | /* 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 | */ |
| 299 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 300 | png_start_read_image(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 301 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 302 | png_debug(1, "in png_start_read_image"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 303 | |
| 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 Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 314 | } |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 315 | #endif /* SEQUENTIAL_READ */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 316 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 317 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 318 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
| 319 | /* Undoes intrapixel differencing, |
| 320 | * NOTE: this is apparently only supported in the 'sequential' reader. |
| 321 | */ |
| 322 | static void |
| 323 | png_do_read_intrapixel(png_row_infop row_info, png_bytep row) |
| 324 | { |
| 325 | png_debug(1, "in png_do_read_intrapixel"); |
| 326 | |
| 327 | if ( |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 328 | (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 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 | } |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 382 | #endif /* MNG_FEATURES */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 383 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 384 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 385 | png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 386 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 387 | png_row_info row_info; |
| 388 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 389 | if (png_ptr == NULL) |
| 390 | return; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 391 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 392 | png_debug2(1, "in png_read_row (row %lu, pass %d)", |
| 393 | (unsigned long)png_ptr->row_number, png_ptr->pass); |
| 394 | |
| 395 | /* png_read_start_row sets the information (in particular iwidth) for this |
| 396 | * interlace pass. |
| 397 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 398 | if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 399 | png_read_start_row(png_ptr); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 400 | |
| 401 | /* 1.5.6: row_info moved out of png_struct to a local here. */ |
| 402 | row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */ |
| 403 | row_info.color_type = png_ptr->color_type; |
| 404 | row_info.bit_depth = png_ptr->bit_depth; |
| 405 | row_info.channels = png_ptr->channels; |
| 406 | row_info.pixel_depth = png_ptr->pixel_depth; |
| 407 | row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); |
| 408 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 409 | #ifdef PNG_WARNINGS_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 410 | if (png_ptr->row_number == 0 && png_ptr->pass == 0) |
| 411 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 412 | /* Check for transforms that have been set but were defined out */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 413 | #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 414 | if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 415 | png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 416 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 417 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 418 | #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 419 | if ((png_ptr->transformations & PNG_FILLER) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 420 | png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 421 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 422 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 423 | #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ |
| 424 | !defined(PNG_READ_PACKSWAP_SUPPORTED) |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 425 | if ((png_ptr->transformations & PNG_PACKSWAP) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 426 | png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 427 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 428 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 429 | #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 430 | if ((png_ptr->transformations & PNG_PACK) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 431 | png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 432 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 433 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 434 | #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 435 | if ((png_ptr->transformations & PNG_SHIFT) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 436 | png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 437 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 438 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 439 | #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 440 | if ((png_ptr->transformations & PNG_BGR) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 441 | png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 442 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 443 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 444 | #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 445 | if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 446 | png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 447 | #endif |
| 448 | } |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 449 | #endif /* WARNINGS */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 450 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 451 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 452 | /* If interlaced and we do not need a new row, combine row and return. |
| 453 | * Notice that the pixels we have from previous rows have been transformed |
| 454 | * already; we can only combine like with like (transformed or |
| 455 | * untransformed) and, because of the libpng API for interlaced images, this |
| 456 | * means we must transform before de-interlacing. |
| 457 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 458 | if (png_ptr->interlaced != 0 && |
| 459 | (png_ptr->transformations & PNG_INTERLACE) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 460 | { |
| 461 | switch (png_ptr->pass) |
| 462 | { |
| 463 | case 0: |
| 464 | if (png_ptr->row_number & 0x07) |
| 465 | { |
| 466 | if (dsp_row != NULL) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 467 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 468 | png_read_finish_row(png_ptr); |
| 469 | return; |
| 470 | } |
| 471 | break; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 472 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 473 | case 1: |
| 474 | if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) |
| 475 | { |
| 476 | if (dsp_row != NULL) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 477 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
| 478 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 479 | png_read_finish_row(png_ptr); |
| 480 | return; |
| 481 | } |
| 482 | break; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 483 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 484 | case 2: |
| 485 | if ((png_ptr->row_number & 0x07) != 4) |
| 486 | { |
| 487 | if (dsp_row != NULL && (png_ptr->row_number & 4)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 488 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
| 489 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 490 | png_read_finish_row(png_ptr); |
| 491 | return; |
| 492 | } |
| 493 | break; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 494 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 495 | case 3: |
| 496 | if ((png_ptr->row_number & 3) || png_ptr->width < 3) |
| 497 | { |
| 498 | if (dsp_row != NULL) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 499 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
| 500 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 501 | png_read_finish_row(png_ptr); |
| 502 | return; |
| 503 | } |
| 504 | break; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 505 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 506 | case 4: |
| 507 | if ((png_ptr->row_number & 3) != 2) |
| 508 | { |
| 509 | if (dsp_row != NULL && (png_ptr->row_number & 2)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 510 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
| 511 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 512 | png_read_finish_row(png_ptr); |
| 513 | return; |
| 514 | } |
| 515 | break; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 516 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 517 | case 5: |
| 518 | if ((png_ptr->row_number & 1) || png_ptr->width < 2) |
| 519 | { |
| 520 | if (dsp_row != NULL) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 521 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
| 522 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 523 | png_read_finish_row(png_ptr); |
| 524 | return; |
| 525 | } |
| 526 | break; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 527 | |
| 528 | default: |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 529 | case 6: |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 530 | if ((png_ptr->row_number & 1) == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 531 | { |
| 532 | png_read_finish_row(png_ptr); |
| 533 | return; |
| 534 | } |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | #endif |
| 539 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 540 | if ((png_ptr->mode & PNG_HAVE_IDAT) == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 541 | png_error(png_ptr, "Invalid attempt to read row data"); |
| 542 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 543 | /* Fill the row with IDAT data: */ |
| 544 | png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1); |
| 545 | |
| 546 | if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 547 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 548 | if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST) |
| 549 | png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1, |
| 550 | png_ptr->prev_row + 1, png_ptr->row_buf[0]); |
| 551 | else |
| 552 | png_error(png_ptr, "bad adaptive filter value"); |
| 553 | } |
Joseph Wen | 4ce0ee1 | 2010-08-20 10:42:22 +0800 | [diff] [blame] | 554 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 555 | /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before |
| 556 | * 1.5.6, while the buffer really is this big in current versions of libpng |
| 557 | * it may not be in the future, so this was changed just to copy the |
| 558 | * interlaced count: |
| 559 | */ |
| 560 | memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 561 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 562 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 563 | if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 564 | (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 565 | { |
| 566 | /* Intrapixel differencing */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 567 | png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 568 | } |
| 569 | #endif |
| 570 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 571 | #ifdef PNG_READ_TRANSFORMS_SUPPORTED |
| 572 | if (png_ptr->transformations) |
| 573 | png_do_read_transformations(png_ptr, &row_info); |
| 574 | #endif |
| 575 | |
| 576 | /* The transformed pixel depth should match the depth now in row_info. */ |
| 577 | if (png_ptr->transformed_pixel_depth == 0) |
| 578 | { |
| 579 | png_ptr->transformed_pixel_depth = row_info.pixel_depth; |
| 580 | if (row_info.pixel_depth > png_ptr->maximum_pixel_depth) |
| 581 | png_error(png_ptr, "sequential row overflow"); |
| 582 | } |
| 583 | |
| 584 | else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth) |
| 585 | png_error(png_ptr, "internal sequential row size calculation error"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 586 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 587 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 588 | /* Expand interlaced rows to full size */ |
| 589 | if (png_ptr->interlaced != 0 && |
| 590 | (png_ptr->transformations & PNG_INTERLACE) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 591 | { |
| 592 | if (png_ptr->pass < 6) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 593 | png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass, |
| 594 | png_ptr->transformations); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 595 | |
| 596 | if (dsp_row != NULL) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 597 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
| 598 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 599 | if (row != NULL) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 600 | png_combine_row(png_ptr, row, 0/*row*/); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 601 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 602 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 603 | else |
| 604 | #endif |
| 605 | { |
| 606 | if (row != NULL) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 607 | png_combine_row(png_ptr, row, -1/*ignored*/); |
| 608 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 609 | if (dsp_row != NULL) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 610 | png_combine_row(png_ptr, dsp_row, -1/*ignored*/); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 611 | } |
| 612 | png_read_finish_row(png_ptr); |
| 613 | |
| 614 | if (png_ptr->read_row_fn != NULL) |
| 615 | (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 616 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 617 | } |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 618 | #endif /* SEQUENTIAL_READ */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 619 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 620 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 621 | /* Read one or more rows of image data. If the image is interlaced, |
| 622 | * and png_set_interlace_handling() has been called, the rows need to |
| 623 | * contain the contents of the rows from the previous pass. If the |
| 624 | * image has alpha or transparency, and png_handle_alpha()[*] has been |
| 625 | * called, the rows contents must be initialized to the contents of the |
| 626 | * screen. |
| 627 | * |
| 628 | * "row" holds the actual image, and pixels are placed in it |
| 629 | * as they arrive. If the image is displayed after each pass, it will |
| 630 | * appear to "sparkle" in. "display_row" can be used to display a |
| 631 | * "chunky" progressive image, with finer detail added as it becomes |
| 632 | * available. If you do not want this "chunky" display, you may pass |
| 633 | * NULL for display_row. If you do not want the sparkle display, and |
| 634 | * you have not called png_handle_alpha(), you may pass NULL for rows. |
| 635 | * If you have called png_handle_alpha(), and the image has either an |
| 636 | * alpha channel or a transparency chunk, you must provide a buffer for |
| 637 | * rows. In this case, you do not have to provide a display_row buffer |
| 638 | * also, but you may. If the image is not interlaced, or if you have |
| 639 | * not called png_set_interlace_handling(), the display_row buffer will |
| 640 | * be ignored, so pass NULL to it. |
| 641 | * |
| 642 | * [*] png_handle_alpha() does not exist yet, as of this version of libpng |
| 643 | */ |
| 644 | |
| 645 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 646 | png_read_rows(png_structrp png_ptr, png_bytepp row, |
| 647 | png_bytepp display_row, png_uint_32 num_rows) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 648 | { |
| 649 | png_uint_32 i; |
| 650 | png_bytepp rp; |
| 651 | png_bytepp dp; |
| 652 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 653 | png_debug(1, "in png_read_rows"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 654 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 655 | if (png_ptr == NULL) |
| 656 | return; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 657 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 658 | rp = row; |
| 659 | dp = display_row; |
| 660 | if (rp != NULL && dp != NULL) |
| 661 | for (i = 0; i < num_rows; i++) |
| 662 | { |
| 663 | png_bytep rptr = *rp++; |
| 664 | png_bytep dptr = *dp++; |
| 665 | |
| 666 | png_read_row(png_ptr, rptr, dptr); |
| 667 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 668 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 669 | else if (rp != NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 670 | for (i = 0; i < num_rows; i++) |
| 671 | { |
| 672 | png_bytep rptr = *rp; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 673 | png_read_row(png_ptr, rptr, NULL); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 674 | rp++; |
| 675 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 676 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 677 | else if (dp != NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 678 | for (i = 0; i < num_rows; i++) |
| 679 | { |
| 680 | png_bytep dptr = *dp; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 681 | png_read_row(png_ptr, NULL, dptr); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 682 | dp++; |
| 683 | } |
| 684 | } |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 685 | #endif /* SEQUENTIAL_READ */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 686 | |
Sireesh Tripurari | b0277d0 | 2014-05-09 15:39:12 +0530 | [diff] [blame] | 687 | #ifdef PNG_INDEX_SUPPORTED |
| 688 | #define IDAT_HEADER_SIZE 8 |
| 689 | |
| 690 | /* Set the png read position to a new position based on idat_position and |
| 691 | * offset. |
| 692 | */ |
| 693 | void |
| 694 | png_set_read_offset(png_structp png_ptr, |
| 695 | png_uint_32 idat_position, png_uint_32 bytes_left) |
| 696 | { |
| 697 | png_seek_data(png_ptr, idat_position); |
| 698 | png_ptr->idat_size = png_read_chunk_header(png_ptr); |
| 699 | |
| 700 | // We need to add back IDAT_HEADER_SIZE because in zlib's perspective, |
| 701 | // IDAT_HEADER in PNG is already stripped out. |
| 702 | png_seek_data(png_ptr, idat_position + IDAT_HEADER_SIZE + png_ptr->idat_size - bytes_left); |
| 703 | png_ptr->idat_size = bytes_left; |
| 704 | } |
| 705 | |
| 706 | /* Configure png decoder to decode the pass starting from *row. |
| 707 | * The requested row may be adjusted to align with an indexing row. |
| 708 | * The actual row for the decoder to start its decoding will be returned in |
| 709 | * *row. |
| 710 | */ |
| 711 | void PNGAPI |
| 712 | png_configure_decoder(png_structp png_ptr, int *row, int pass) |
| 713 | { |
| 714 | png_indexp index = png_ptr->index; |
| 715 | int n = *row / index->step[pass]; |
| 716 | png_line_indexp line_index = index->pass_line_index[pass][n]; |
| 717 | |
| 718 | // Adjust row to an indexing row. |
| 719 | *row = n * index->step[pass]; |
| 720 | png_ptr->row_number = *row; |
| 721 | |
| 722 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
| 723 | if (png_ptr->interlaced) |
| 724 | png_set_interlaced_pass(png_ptr, pass); |
| 725 | #endif |
| 726 | |
| 727 | long row_byte_length = |
| 728 | PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1; |
| 729 | |
| 730 | inflateEnd(&png_ptr->zstream); |
| 731 | inflateCopy(&png_ptr->zstream, line_index->z_state); |
| 732 | |
| 733 | // Set the png read position to line_index. |
| 734 | png_set_read_offset(png_ptr, line_index->stream_idat_position, |
| 735 | line_index->bytes_left_in_idat); |
| 736 | memcpy(png_ptr->prev_row, line_index->prev_row, row_byte_length); |
| 737 | png_ptr->zstream.avail_in = 0; |
| 738 | } |
| 739 | |
| 740 | /* Build the line index and store the index in png_ptr->index. |
| 741 | */ |
| 742 | void PNGAPI |
| 743 | png_build_index(png_structp png_ptr) |
| 744 | { |
| 745 | // number of rows in a 8x8 block for each interlaced pass. |
| 746 | int number_rows_in_pass[7] = {1, 1, 1, 2, 2, 4, 4}; |
| 747 | |
| 748 | int ret; |
| 749 | png_uint_32 i, j; |
| 750 | png_bytep rp; |
| 751 | int p, pass_number = 1; |
| 752 | |
| 753 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
| 754 | pass_number = png_set_interlace_handling(png_ptr); |
| 755 | #endif |
| 756 | |
| 757 | if (png_ptr == NULL) |
| 758 | return; |
| 759 | |
| 760 | png_read_start_row(png_ptr); |
| 761 | |
| 762 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
| 763 | if (!png_ptr->interlaced) |
| 764 | #endif |
| 765 | { |
| 766 | number_rows_in_pass[0] = 8; |
| 767 | } |
| 768 | |
Henrik Smiding | 55ada88 | 2015-02-17 17:56:24 +0100 | [diff] [blame] | 769 | // Allocate a buffer big enough for any transform. |
| 770 | rp = png_malloc(png_ptr, PNG_ROWBYTES(png_ptr->maximum_pixel_depth, png_ptr->width)); |
Sireesh Tripurari | b0277d0 | 2014-05-09 15:39:12 +0530 | [diff] [blame] | 771 | |
| 772 | png_indexp index = png_malloc(png_ptr, sizeof(png_index)); |
| 773 | png_ptr->index = index; |
| 774 | |
| 775 | index->stream_idat_position = png_ptr->total_data_read - IDAT_HEADER_SIZE; |
| 776 | |
| 777 | // Set the default size of index in each pass to 0, |
| 778 | // so that we can free index correctly in png_destroy_read_struct. |
| 779 | for (p = 0; p < 7; p++) |
| 780 | index->size[p] = 0; |
| 781 | |
| 782 | for (p = 0; p < pass_number; p++) |
| 783 | { |
| 784 | // We adjust the index step in each pass to make sure each pass |
| 785 | // has roughly the same size of index. |
| 786 | // This way, we won't consume to much memory in recording index. |
| 787 | index->step[p] = INDEX_SAMPLE_SIZE * (8 / number_rows_in_pass[p]); |
Henrik Smiding | 55ada88 | 2015-02-17 17:56:24 +0100 | [diff] [blame] | 788 | const png_uint_32 temp_size = |
Sireesh Tripurari | b0277d0 | 2014-05-09 15:39:12 +0530 | [diff] [blame] | 789 | (png_ptr->height + index->step[p] - 1) / index->step[p]; |
| 790 | index->pass_line_index[p] = |
Derek Sollenberger | 8594e1a | 2014-09-22 16:28:36 -0400 | [diff] [blame] | 791 | png_malloc(png_ptr, temp_size * sizeof(png_line_indexp)); |
Sireesh Tripurari | b0277d0 | 2014-05-09 15:39:12 +0530 | [diff] [blame] | 792 | |
| 793 | // Get the row_byte_length seen by the filter. This value may be |
| 794 | // different from the row_byte_length of a bitmap in the case of |
| 795 | // color palette mode. |
| 796 | int row_byte_length = |
| 797 | PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1; |
| 798 | |
| 799 | // Now, we record index for each indexing row. |
Derek Sollenberger | 8594e1a | 2014-09-22 16:28:36 -0400 | [diff] [blame] | 800 | for (i = 0; i < temp_size; i++) |
Sireesh Tripurari | b0277d0 | 2014-05-09 15:39:12 +0530 | [diff] [blame] | 801 | { |
| 802 | png_line_indexp line_index = png_malloc(png_ptr, sizeof(png_line_index)); |
| 803 | index->pass_line_index[p][i] = line_index; |
| 804 | |
| 805 | line_index->z_state = png_malloc(png_ptr, sizeof(z_stream)); |
| 806 | inflateCopy(line_index->z_state, &png_ptr->zstream); |
| 807 | line_index->prev_row = png_malloc(png_ptr, row_byte_length); |
| 808 | memcpy(line_index->prev_row, png_ptr->prev_row, row_byte_length); |
| 809 | line_index->stream_idat_position = index->stream_idat_position; |
| 810 | line_index->bytes_left_in_idat = png_ptr->idat_size + png_ptr->zstream.avail_in; |
| 811 | |
Derek Sollenberger | 8594e1a | 2014-09-22 16:28:36 -0400 | [diff] [blame] | 812 | // increment the size now that we have the backing data structures. |
| 813 | // This prevents a crash in the event that png_read_row fails and |
| 814 | // we need to cleanup the partially constructed png_index_struct; |
| 815 | index->size[p] += 1; |
| 816 | |
Sireesh Tripurari | b0277d0 | 2014-05-09 15:39:12 +0530 | [diff] [blame] | 817 | // Skip the "step" number of rows to the next indexing row. |
| 818 | for (j = 0; j < index->step[p] && |
| 819 | i * index->step[p] + j < png_ptr->height; j++) |
| 820 | { |
| 821 | png_read_row(png_ptr, rp, NULL); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | png_free(png_ptr, rp); |
| 826 | } |
| 827 | #endif |
| 828 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 829 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 830 | /* Read the entire image. If the image has an alpha channel or a tRNS |
| 831 | * chunk, and you have called png_handle_alpha()[*], you will need to |
| 832 | * initialize the image to the current image that PNG will be overlaying. |
| 833 | * We set the num_rows again here, in case it was incorrectly set in |
| 834 | * png_read_start_row() by a call to png_read_update_info() or |
| 835 | * png_start_read_image() if png_set_interlace_handling() wasn't called |
| 836 | * prior to either of these functions like it should have been. You can |
| 837 | * only call this function once. If you desire to have an image for |
| 838 | * each pass of a interlaced image, use png_read_rows() instead. |
| 839 | * |
| 840 | * [*] png_handle_alpha() does not exist yet, as of this version of libpng |
| 841 | */ |
| 842 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 843 | png_read_image(png_structrp png_ptr, png_bytepp image) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 844 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 845 | png_uint_32 i, image_height; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 846 | int pass, j; |
| 847 | png_bytepp rp; |
| 848 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 849 | png_debug(1, "in png_read_image"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 850 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 851 | if (png_ptr == NULL) |
| 852 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 853 | |
| 854 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 855 | if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 856 | { |
| 857 | pass = png_set_interlace_handling(png_ptr); |
| 858 | /* And make sure transforms are initialized. */ |
| 859 | png_start_read_image(png_ptr); |
| 860 | } |
| 861 | else |
| 862 | { |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 863 | if (png_ptr->interlaced != 0 && |
| 864 | (png_ptr->transformations & PNG_INTERLACE) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 865 | { |
| 866 | /* Caller called png_start_read_image or png_read_update_info without |
| 867 | * first turning on the PNG_INTERLACE transform. We can fix this here, |
| 868 | * but the caller should do it! |
| 869 | */ |
| 870 | png_warning(png_ptr, "Interlace handling should be turned on when " |
| 871 | "using png_read_image"); |
| 872 | /* Make sure this is set correctly */ |
| 873 | png_ptr->num_rows = png_ptr->height; |
| 874 | } |
| 875 | |
| 876 | /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in |
| 877 | * the above error case. |
| 878 | */ |
| 879 | pass = png_set_interlace_handling(png_ptr); |
| 880 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 881 | #else |
| 882 | if (png_ptr->interlaced) |
| 883 | png_error(png_ptr, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 884 | "Cannot read interlaced image -- interlace handler disabled"); |
| 885 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 886 | pass = 1; |
| 887 | #endif |
| 888 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 889 | image_height=png_ptr->height; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 890 | |
| 891 | for (j = 0; j < pass; j++) |
| 892 | { |
| 893 | rp = image; |
| 894 | for (i = 0; i < image_height; i++) |
| 895 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 896 | png_read_row(png_ptr, *rp, NULL); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 897 | rp++; |
| 898 | } |
| 899 | } |
| 900 | } |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 901 | #endif /* SEQUENTIAL_READ */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 902 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 903 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 904 | /* Read the end of the PNG file. Will not read past the end of the |
| 905 | * file, will verify the end is accurate, and will read any comments |
| 906 | * or time information at the end of the file, if info is not NULL. |
| 907 | */ |
| 908 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 909 | png_read_end(png_structrp png_ptr, png_inforp info_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 910 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 911 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 912 | int keep; |
| 913 | #endif |
| 914 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 915 | png_debug(1, "in png_read_end"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 916 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 917 | if (png_ptr == NULL) |
| 918 | return; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 919 | |
| 920 | /* If png_read_end is called in the middle of reading the rows there may |
| 921 | * still be pending IDAT data and an owned zstream. Deal with this here. |
| 922 | */ |
| 923 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 924 | if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 925 | #endif |
| 926 | png_read_finish_IDAT(png_ptr); |
| 927 | |
| 928 | #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 929 | /* Report invalid palette index; added at libng-1.5.10 */ |
| 930 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 931 | png_ptr->num_palette_max > png_ptr->num_palette) |
| 932 | png_benign_error(png_ptr, "Read palette index exceeding num_palette"); |
| 933 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 934 | |
| 935 | do |
| 936 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 937 | png_uint_32 length = png_read_chunk_header(png_ptr); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 938 | png_uint_32 chunk_name = png_ptr->chunk_name; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 939 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 940 | if (chunk_name == png_IEND) |
| 941 | png_handle_IEND(png_ptr, info_ptr, length); |
| 942 | |
| 943 | else if (chunk_name == png_IHDR) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 944 | png_handle_IHDR(png_ptr, info_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 945 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 946 | else if (info_ptr == NULL) |
| 947 | png_crc_finish(png_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 948 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 949 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 950 | else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 951 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 952 | if (chunk_name == png_IDAT) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 953 | { |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 954 | if ((length > 0) || |
| 955 | (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 956 | png_benign_error(png_ptr, "Too many IDATs found"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 957 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 958 | png_handle_unknown(png_ptr, info_ptr, length, keep); |
| 959 | if (chunk_name == png_PLTE) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 960 | png_ptr->mode |= PNG_HAVE_PLTE; |
| 961 | } |
| 962 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 963 | |
| 964 | else if (chunk_name == png_IDAT) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 965 | { |
| 966 | /* Zero length IDATs are legal after the last IDAT has been |
| 967 | * read, but not after other chunks have been read. |
| 968 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 969 | if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 970 | png_benign_error(png_ptr, "Too many IDATs found"); |
| 971 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 972 | png_crc_finish(png_ptr, length); |
| 973 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 974 | else if (chunk_name == png_PLTE) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 975 | png_handle_PLTE(png_ptr, info_ptr, length); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 976 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 977 | #ifdef PNG_READ_bKGD_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 978 | else if (chunk_name == png_bKGD) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 979 | png_handle_bKGD(png_ptr, info_ptr, length); |
| 980 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 981 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 982 | #ifdef PNG_READ_cHRM_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 983 | else if (chunk_name == png_cHRM) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 984 | png_handle_cHRM(png_ptr, info_ptr, length); |
| 985 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 986 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 987 | #ifdef PNG_READ_gAMA_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 988 | else if (chunk_name == png_gAMA) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 989 | png_handle_gAMA(png_ptr, info_ptr, length); |
| 990 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 991 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 992 | #ifdef PNG_READ_hIST_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 993 | else if (chunk_name == png_hIST) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 994 | png_handle_hIST(png_ptr, info_ptr, length); |
| 995 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 996 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 997 | #ifdef PNG_READ_oFFs_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 998 | else if (chunk_name == png_oFFs) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 999 | png_handle_oFFs(png_ptr, info_ptr, length); |
| 1000 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1001 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1002 | #ifdef PNG_READ_pCAL_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1003 | else if (chunk_name == png_pCAL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1004 | png_handle_pCAL(png_ptr, info_ptr, length); |
| 1005 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1006 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1007 | #ifdef PNG_READ_sCAL_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1008 | else if (chunk_name == png_sCAL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1009 | png_handle_sCAL(png_ptr, info_ptr, length); |
| 1010 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1011 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1012 | #ifdef PNG_READ_pHYs_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1013 | else if (chunk_name == png_pHYs) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1014 | png_handle_pHYs(png_ptr, info_ptr, length); |
| 1015 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1016 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1017 | #ifdef PNG_READ_sBIT_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1018 | else if (chunk_name == png_sBIT) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1019 | png_handle_sBIT(png_ptr, info_ptr, length); |
| 1020 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1021 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1022 | #ifdef PNG_READ_sRGB_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1023 | else if (chunk_name == png_sRGB) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1024 | png_handle_sRGB(png_ptr, info_ptr, length); |
| 1025 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1026 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1027 | #ifdef PNG_READ_iCCP_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1028 | else if (chunk_name == png_iCCP) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1029 | png_handle_iCCP(png_ptr, info_ptr, length); |
| 1030 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1031 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1032 | #ifdef PNG_READ_sPLT_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1033 | else if (chunk_name == png_sPLT) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1034 | png_handle_sPLT(png_ptr, info_ptr, length); |
| 1035 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1036 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1037 | #ifdef PNG_READ_tEXt_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1038 | else if (chunk_name == png_tEXt) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1039 | png_handle_tEXt(png_ptr, info_ptr, length); |
| 1040 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1041 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1042 | #ifdef PNG_READ_tIME_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1043 | else if (chunk_name == png_tIME) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1044 | png_handle_tIME(png_ptr, info_ptr, length); |
| 1045 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1046 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1047 | #ifdef PNG_READ_tRNS_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1048 | else if (chunk_name == png_tRNS) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1049 | png_handle_tRNS(png_ptr, info_ptr, length); |
| 1050 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1051 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1052 | #ifdef PNG_READ_zTXt_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1053 | else if (chunk_name == png_zTXt) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1054 | png_handle_zTXt(png_ptr, info_ptr, length); |
| 1055 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1056 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1057 | #ifdef PNG_READ_iTXt_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1058 | else if (chunk_name == png_iTXt) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1059 | png_handle_iTXt(png_ptr, info_ptr, length); |
| 1060 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1061 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1062 | else |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1063 | png_handle_unknown(png_ptr, info_ptr, length, |
| 1064 | PNG_HANDLE_CHUNK_AS_DEFAULT); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1065 | } while ((png_ptr->mode & PNG_HAVE_IEND) == 0); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1066 | } |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1067 | #endif /* SEQUENTIAL_READ */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1068 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1069 | /* Free all memory used in the read struct */ |
| 1070 | static void |
| 1071 | png_read_destroy(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1072 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1073 | png_debug(1, "in png_read_destroy"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1074 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1075 | #ifdef PNG_READ_GAMMA_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1076 | png_destroy_gamma_table(png_ptr); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1077 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1078 | |
| 1079 | png_free(png_ptr, png_ptr->big_row_buf); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1080 | png_ptr->big_row_buf = NULL; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1081 | png_free(png_ptr, png_ptr->big_prev_row); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1082 | png_ptr->big_prev_row = NULL; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1083 | png_free(png_ptr, png_ptr->read_buffer); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1084 | png_ptr->read_buffer = NULL; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1085 | |
| 1086 | #ifdef PNG_READ_QUANTIZE_SUPPORTED |
| 1087 | png_free(png_ptr, png_ptr->palette_lookup); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1088 | png_ptr->palette_lookup = NULL; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1089 | png_free(png_ptr, png_ptr->quantize_index); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1090 | png_ptr->quantize_index = NULL; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1091 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1092 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1093 | if ((png_ptr->free_me & PNG_FREE_PLTE) != 0) |
| 1094 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1095 | png_zfree(png_ptr, png_ptr->palette); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1096 | png_ptr->palette = NULL; |
| 1097 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1098 | png_ptr->free_me &= ~PNG_FREE_PLTE; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1099 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1100 | #if defined(PNG_tRNS_SUPPORTED) || \ |
| 1101 | defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1102 | if ((png_ptr->free_me & PNG_FREE_TRNS) != 0) |
| 1103 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1104 | png_free(png_ptr, png_ptr->trans_alpha); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1105 | png_ptr->trans_alpha = NULL; |
| 1106 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1107 | png_ptr->free_me &= ~PNG_FREE_TRNS; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1108 | #endif |
| 1109 | |
| 1110 | inflateEnd(&png_ptr->zstream); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1111 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1112 | #ifdef PNG_PROGRESSIVE_READ_SUPPORTED |
| 1113 | png_free(png_ptr, png_ptr->save_buffer); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1114 | png_ptr->save_buffer = NULL; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1115 | #endif |
| 1116 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1117 | #if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1118 | defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) |
| 1119 | png_free(png_ptr, png_ptr->unknown_chunk.data); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1120 | png_ptr->unknown_chunk.data = NULL; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1121 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1122 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1123 | #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED |
| 1124 | png_free(png_ptr, png_ptr->chunk_list); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1125 | png_ptr->chunk_list = NULL; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1126 | #endif |
| 1127 | |
Sireesh Tripurari | b0277d0 | 2014-05-09 15:39:12 +0530 | [diff] [blame] | 1128 | #ifdef PNG_INDEX_SUPPORTED |
| 1129 | if (png_ptr->index) { |
| 1130 | unsigned int i, p; |
| 1131 | png_indexp index = png_ptr->index; |
| 1132 | for (p = 0; p < 7; p++) { |
| 1133 | for (i = 0; i < index->size[p]; i++) { |
| 1134 | inflateEnd(index->pass_line_index[p][i]->z_state); |
| 1135 | png_free(png_ptr, index->pass_line_index[p][i]->z_state); |
| 1136 | png_free(png_ptr, index->pass_line_index[p][i]->prev_row); |
| 1137 | png_free(png_ptr, index->pass_line_index[p][i]); |
| 1138 | } |
| 1139 | if (index->size[p] != 0) { |
| 1140 | png_free(png_ptr, index->pass_line_index[p]); |
| 1141 | } |
| 1142 | } |
| 1143 | png_free(png_ptr, index); |
| 1144 | } |
| 1145 | #endif |
| 1146 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1147 | /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error |
| 1148 | * callbacks are still set at this point. They are required to complete the |
| 1149 | * destruction of the png_struct itself. |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1150 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1151 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1152 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1153 | /* Free all memory used by the read */ |
| 1154 | void PNGAPI |
| 1155 | png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, |
| 1156 | png_infopp end_info_ptr_ptr) |
| 1157 | { |
| 1158 | png_structrp png_ptr = NULL; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1159 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1160 | png_debug(1, "in png_destroy_read_struct"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1161 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1162 | if (png_ptr_ptr != NULL) |
| 1163 | png_ptr = *png_ptr_ptr; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1164 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1165 | if (png_ptr == NULL) |
| 1166 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1167 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1168 | /* libpng 1.6.0: use the API to destroy info structs to ensure consistent |
| 1169 | * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API. |
| 1170 | * The extra was, apparently, unnecessary yet this hides memory leak bugs. |
| 1171 | */ |
| 1172 | png_destroy_info_struct(png_ptr, end_info_ptr_ptr); |
| 1173 | png_destroy_info_struct(png_ptr, info_ptr_ptr); |
| 1174 | |
| 1175 | *png_ptr_ptr = NULL; |
| 1176 | png_read_destroy(png_ptr); |
| 1177 | png_destroy_png_struct(png_ptr); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1181 | png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1182 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1183 | if (png_ptr == NULL) |
| 1184 | return; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1185 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1186 | png_ptr->read_row_fn = read_row_fn; |
| 1187 | } |
| 1188 | |
| 1189 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1190 | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
| 1191 | #ifdef PNG_INFO_IMAGE_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1192 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1193 | png_read_png(png_structrp png_ptr, png_inforp info_ptr, |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1194 | int transforms, |
| 1195 | voidp params) |
| 1196 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1197 | if (png_ptr == NULL || info_ptr == NULL) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1198 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1199 | |
| 1200 | /* png_read_info() gives us all of the information from the |
| 1201 | * PNG file before the first IDAT (image data chunk). |
| 1202 | */ |
| 1203 | png_read_info(png_ptr, info_ptr); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1204 | if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep))) |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1205 | png_error(png_ptr, "Image is too high to process with png_read_png()"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1206 | |
| 1207 | /* -------------- image transformations start here ------------------- */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1208 | /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM |
| 1209 | * is not implemented. This will only happen in de-configured (non-default) |
| 1210 | * libpng builds. The results can be unexpected - png_read_png may return |
| 1211 | * short or mal-formed rows because the transform is skipped. |
| 1212 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1213 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1214 | /* Tell libpng to strip 16-bit/color files down to 8 bits per color. |
| 1215 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1216 | if ((transforms & PNG_TRANSFORM_SCALE_16) != 0) |
| 1217 | /* Added at libpng-1.5.4. "strip_16" produces the same result that it |
| 1218 | * did in earlier versions, while "scale_16" is now more accurate. |
| 1219 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1220 | #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1221 | png_set_scale_16(png_ptr); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1222 | #else |
| 1223 | png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1224 | #endif |
| 1225 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1226 | /* If both SCALE and STRIP are required pngrtran will effectively cancel the |
| 1227 | * latter by doing SCALE first. This is ok and allows apps not to check for |
| 1228 | * which is supported to get the right answer. |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1229 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1230 | if ((transforms & PNG_TRANSFORM_STRIP_16) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1231 | #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1232 | png_set_strip_16(png_ptr); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1233 | #else |
| 1234 | png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1235 | #endif |
| 1236 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1237 | /* Strip alpha bytes from the input data without combining with |
| 1238 | * the background (not recommended). |
| 1239 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1240 | if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1241 | #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1242 | png_set_strip_alpha(png_ptr); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1243 | #else |
| 1244 | png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1245 | #endif |
| 1246 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1247 | /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single |
| 1248 | * byte into separate bytes (useful for paletted and grayscale images). |
| 1249 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1250 | if ((transforms & PNG_TRANSFORM_PACKING) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1251 | #ifdef PNG_READ_PACK_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1252 | png_set_packing(png_ptr); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1253 | #else |
| 1254 | png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1255 | #endif |
| 1256 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1257 | /* Change the order of packed pixels to least significant bit first |
| 1258 | * (not useful if you are using png_set_packing). |
| 1259 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1260 | if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1261 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1262 | png_set_packswap(png_ptr); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1263 | #else |
| 1264 | png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1265 | #endif |
| 1266 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1267 | /* Expand paletted colors into true RGB triplets |
| 1268 | * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel |
| 1269 | * Expand paletted or RGB images with transparency to full alpha |
| 1270 | * channels so the data will be available as RGBA quartets. |
| 1271 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1272 | if ((transforms & PNG_TRANSFORM_EXPAND) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1273 | #ifdef PNG_READ_EXPAND_SUPPORTED |
| 1274 | png_set_expand(png_ptr); |
| 1275 | #else |
| 1276 | png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1277 | #endif |
| 1278 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1279 | /* We don't handle background color or gamma transformation or quantizing. |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1280 | */ |
| 1281 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1282 | /* Invert monochrome files to have 0 as white and 1 as black |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1283 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1284 | if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1285 | #ifdef PNG_READ_INVERT_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1286 | png_set_invert_mono(png_ptr); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1287 | #else |
| 1288 | png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1289 | #endif |
| 1290 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1291 | /* If you want to shift the pixel values from the range [0,255] or |
| 1292 | * [0,65535] to the original [0,7] or [0,31], or whatever range the |
| 1293 | * colors were originally in: |
| 1294 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1295 | if ((transforms & PNG_TRANSFORM_SHIFT) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1296 | #ifdef PNG_READ_SHIFT_SUPPORTED |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1297 | if ((info_ptr->valid & PNG_INFO_sBIT) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1298 | png_set_shift(png_ptr, &info_ptr->sig_bit); |
| 1299 | #else |
| 1300 | png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1301 | #endif |
| 1302 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1303 | /* Flip the RGB pixels to BGR (or RGBA to BGRA) */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1304 | if ((transforms & PNG_TRANSFORM_BGR) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1305 | #ifdef PNG_READ_BGR_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1306 | png_set_bgr(png_ptr); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1307 | #else |
| 1308 | png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1309 | #endif |
| 1310 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1311 | /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1312 | if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1313 | #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1314 | png_set_swap_alpha(png_ptr); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1315 | #else |
| 1316 | png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1317 | #endif |
| 1318 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1319 | /* Swap bytes of 16-bit files to least significant byte first */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1320 | if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1321 | #ifdef PNG_READ_SWAP_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1322 | png_set_swap(png_ptr); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1323 | #else |
| 1324 | png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1325 | #endif |
| 1326 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1327 | /* Added at libpng-1.2.41 */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1328 | /* Invert the alpha channel from opacity to transparency */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1329 | if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1330 | #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1331 | png_set_invert_alpha(png_ptr); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1332 | #else |
| 1333 | png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1334 | #endif |
| 1335 | |
| 1336 | /* Added at libpng-1.2.41 */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1337 | /* Expand grayscale image to RGB */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1338 | if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1339 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1340 | png_set_gray_to_rgb(png_ptr); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1341 | #else |
| 1342 | png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1343 | #endif |
| 1344 | |
| 1345 | /* Added at libpng-1.5.4 */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1346 | if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1347 | #ifdef PNG_READ_EXPAND_16_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1348 | png_set_expand_16(png_ptr); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1349 | #else |
| 1350 | png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1351 | #endif |
| 1352 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1353 | /* We don't handle adding filler bytes */ |
| 1354 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1355 | /* We use png_read_image and rely on that for interlace handling, but we also |
| 1356 | * call png_read_update_info therefore must turn on interlace handling now: |
| 1357 | */ |
| 1358 | (void)png_set_interlace_handling(png_ptr); |
| 1359 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1360 | /* Optional call to gamma correct and add the background to the palette |
| 1361 | * and update info structure. REQUIRED if you are expecting libpng to |
| 1362 | * update the palette for you (i.e., you selected such a transform above). |
| 1363 | */ |
| 1364 | png_read_update_info(png_ptr, info_ptr); |
| 1365 | |
| 1366 | /* -------------- image transformations end here ------------------- */ |
| 1367 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1368 | png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1369 | if (info_ptr->row_pointers == NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1370 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1371 | png_uint_32 iptr; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1372 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1373 | info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr, |
| 1374 | info_ptr->height * (sizeof (png_bytep)))); |
| 1375 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1376 | for (iptr=0; iptr<info_ptr->height; iptr++) |
| 1377 | info_ptr->row_pointers[iptr] = NULL; |
| 1378 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1379 | info_ptr->free_me |= PNG_FREE_ROWS; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1380 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1381 | for (iptr = 0; iptr < info_ptr->height; iptr++) |
| 1382 | info_ptr->row_pointers[iptr] = png_voidcast(png_bytep, |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1383 | png_malloc(png_ptr, info_ptr->rowbytes)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | png_read_image(png_ptr, info_ptr->row_pointers); |
| 1387 | info_ptr->valid |= PNG_INFO_IDAT; |
| 1388 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1389 | /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1390 | png_read_end(png_ptr, info_ptr); |
| 1391 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1392 | PNG_UNUSED(params) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1393 | } |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1394 | #endif /* INFO_IMAGE */ |
| 1395 | #endif /* SEQUENTIAL_READ */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1396 | |
| 1397 | #ifdef PNG_SIMPLIFIED_READ_SUPPORTED |
| 1398 | /* SIMPLIFIED READ |
| 1399 | * |
| 1400 | * This code currently relies on the sequential reader, though it could easily |
| 1401 | * be made to work with the progressive one. |
| 1402 | */ |
| 1403 | /* Arguments to png_image_finish_read: */ |
| 1404 | |
| 1405 | /* Encoding of PNG data (used by the color-map code) */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1406 | # define P_NOTSET 0 /* File encoding not yet known */ |
| 1407 | # define P_sRGB 1 /* 8-bit encoded to sRGB gamma */ |
| 1408 | # define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */ |
| 1409 | # define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */ |
| 1410 | # define P_LINEAR8 4 /* 8-bit linear: only from a file value */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1411 | |
| 1412 | /* Color-map processing: after libpng has run on the PNG image further |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1413 | * processing may be needed to convert the data to color-map indices. |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1414 | */ |
| 1415 | #define PNG_CMAP_NONE 0 |
| 1416 | #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */ |
| 1417 | #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */ |
| 1418 | #define PNG_CMAP_RGB 3 /* Process RGB data */ |
| 1419 | #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */ |
| 1420 | |
| 1421 | /* The following document where the background is for each processing case. */ |
| 1422 | #define PNG_CMAP_NONE_BACKGROUND 256 |
| 1423 | #define PNG_CMAP_GA_BACKGROUND 231 |
| 1424 | #define PNG_CMAP_TRANS_BACKGROUND 254 |
| 1425 | #define PNG_CMAP_RGB_BACKGROUND 256 |
| 1426 | #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216 |
| 1427 | |
| 1428 | typedef struct |
| 1429 | { |
| 1430 | /* Arguments: */ |
| 1431 | png_imagep image; |
| 1432 | png_voidp buffer; |
| 1433 | png_int_32 row_stride; |
| 1434 | png_voidp colormap; |
| 1435 | png_const_colorp background; |
| 1436 | /* Local variables: */ |
| 1437 | png_voidp local_row; |
| 1438 | png_voidp first_row; |
| 1439 | ptrdiff_t row_bytes; /* step between rows */ |
| 1440 | int file_encoding; /* E_ values above */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1441 | png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1442 | int colormap_processing; /* PNG_CMAP_ values above */ |
| 1443 | } png_image_read_control; |
| 1444 | |
| 1445 | /* Do all the *safe* initialization - 'safe' means that png_error won't be |
| 1446 | * called, so setting up the jmp_buf is not required. This means that anything |
| 1447 | * called from here must *not* call png_malloc - it has to call png_malloc_warn |
| 1448 | * instead so that control is returned safely back to this routine. |
| 1449 | */ |
| 1450 | static int |
| 1451 | png_image_read_init(png_imagep image) |
| 1452 | { |
| 1453 | if (image->opaque == NULL) |
| 1454 | { |
| 1455 | png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image, |
| 1456 | png_safe_error, png_safe_warning); |
| 1457 | |
| 1458 | /* And set the rest of the structure to NULL to ensure that the various |
| 1459 | * fields are consistent. |
| 1460 | */ |
| 1461 | memset(image, 0, (sizeof *image)); |
| 1462 | image->version = PNG_IMAGE_VERSION; |
| 1463 | |
| 1464 | if (png_ptr != NULL) |
| 1465 | { |
| 1466 | png_infop info_ptr = png_create_info_struct(png_ptr); |
| 1467 | |
| 1468 | if (info_ptr != NULL) |
| 1469 | { |
| 1470 | png_controlp control = png_voidcast(png_controlp, |
| 1471 | png_malloc_warn(png_ptr, (sizeof *control))); |
| 1472 | |
| 1473 | if (control != NULL) |
| 1474 | { |
| 1475 | memset(control, 0, (sizeof *control)); |
| 1476 | |
| 1477 | control->png_ptr = png_ptr; |
| 1478 | control->info_ptr = info_ptr; |
| 1479 | control->for_write = 0; |
| 1480 | |
| 1481 | image->opaque = control; |
| 1482 | return 1; |
| 1483 | } |
| 1484 | |
| 1485 | /* Error clean up */ |
| 1486 | png_destroy_info_struct(png_ptr, &info_ptr); |
| 1487 | } |
| 1488 | |
| 1489 | png_destroy_read_struct(&png_ptr, NULL, NULL); |
| 1490 | } |
| 1491 | |
| 1492 | return png_image_error(image, "png_image_read: out of memory"); |
| 1493 | } |
| 1494 | |
| 1495 | return png_image_error(image, "png_image_read: opaque pointer not NULL"); |
| 1496 | } |
| 1497 | |
| 1498 | /* Utility to find the base format of a PNG file from a png_struct. */ |
| 1499 | static png_uint_32 |
| 1500 | png_image_format(png_structrp png_ptr) |
| 1501 | { |
| 1502 | png_uint_32 format = 0; |
| 1503 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1504 | if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1505 | format |= PNG_FORMAT_FLAG_COLOR; |
| 1506 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1507 | if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1508 | format |= PNG_FORMAT_FLAG_ALPHA; |
| 1509 | |
| 1510 | /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS |
| 1511 | * sets the png_struct fields; that's all we are interested in here. The |
| 1512 | * precise interaction with an app call to png_set_tRNS and PNG file reading |
| 1513 | * is unclear. |
| 1514 | */ |
| 1515 | else if (png_ptr->num_trans > 0) |
| 1516 | format |= PNG_FORMAT_FLAG_ALPHA; |
| 1517 | |
| 1518 | if (png_ptr->bit_depth == 16) |
| 1519 | format |= PNG_FORMAT_FLAG_LINEAR; |
| 1520 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1521 | if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1522 | format |= PNG_FORMAT_FLAG_COLORMAP; |
| 1523 | |
| 1524 | return format; |
| 1525 | } |
| 1526 | |
| 1527 | /* Is the given gamma significantly different from sRGB? The test is the same |
| 1528 | * one used in pngrtran.c when deciding whether to do gamma correction. The |
| 1529 | * arithmetic optimizes the division by using the fact that the inverse of the |
| 1530 | * file sRGB gamma is 2.2 |
| 1531 | */ |
| 1532 | static int |
| 1533 | png_gamma_not_sRGB(png_fixed_point g) |
| 1534 | { |
| 1535 | if (g < PNG_FP_1) |
| 1536 | { |
| 1537 | /* An uninitialized gamma is assumed to be sRGB for the simplified API. */ |
| 1538 | if (g == 0) |
| 1539 | return 0; |
| 1540 | |
| 1541 | return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */); |
| 1542 | } |
| 1543 | |
| 1544 | return 1; |
| 1545 | } |
| 1546 | |
| 1547 | /* Do the main body of a 'png_image_begin_read' function; read the PNG file |
| 1548 | * header and fill in all the information. This is executed in a safe context, |
| 1549 | * unlike the init routine above. |
| 1550 | */ |
| 1551 | static int |
| 1552 | png_image_read_header(png_voidp argument) |
| 1553 | { |
| 1554 | png_imagep image = png_voidcast(png_imagep, argument); |
| 1555 | png_structrp png_ptr = image->opaque->png_ptr; |
| 1556 | png_inforp info_ptr = image->opaque->info_ptr; |
| 1557 | |
| 1558 | png_set_benign_errors(png_ptr, 1/*warn*/); |
| 1559 | png_read_info(png_ptr, info_ptr); |
| 1560 | |
| 1561 | /* Do this the fast way; just read directly out of png_struct. */ |
| 1562 | image->width = png_ptr->width; |
| 1563 | image->height = png_ptr->height; |
| 1564 | |
| 1565 | { |
| 1566 | png_uint_32 format = png_image_format(png_ptr); |
| 1567 | |
| 1568 | image->format = format; |
| 1569 | |
| 1570 | #ifdef PNG_COLORSPACE_SUPPORTED |
| 1571 | /* Does the colorspace match sRGB? If there is no color endpoint |
| 1572 | * (colorant) information assume yes, otherwise require the |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1573 | * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1574 | * colorspace has been determined to be invalid ignore it. |
| 1575 | */ |
| 1576 | if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags |
| 1577 | & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB| |
| 1578 | PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS)) |
| 1579 | image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB; |
| 1580 | #endif |
| 1581 | } |
| 1582 | |
| 1583 | /* We need the maximum number of entries regardless of the format the |
| 1584 | * application sets here. |
| 1585 | */ |
| 1586 | { |
| 1587 | png_uint_32 cmap_entries; |
| 1588 | |
| 1589 | switch (png_ptr->color_type) |
| 1590 | { |
| 1591 | case PNG_COLOR_TYPE_GRAY: |
| 1592 | cmap_entries = 1U << png_ptr->bit_depth; |
| 1593 | break; |
| 1594 | |
| 1595 | case PNG_COLOR_TYPE_PALETTE: |
| 1596 | cmap_entries = png_ptr->num_palette; |
| 1597 | break; |
| 1598 | |
| 1599 | default: |
| 1600 | cmap_entries = 256; |
| 1601 | break; |
| 1602 | } |
| 1603 | |
| 1604 | if (cmap_entries > 256) |
| 1605 | cmap_entries = 256; |
| 1606 | |
| 1607 | image->colormap_entries = cmap_entries; |
| 1608 | } |
| 1609 | |
| 1610 | return 1; |
| 1611 | } |
| 1612 | |
| 1613 | #ifdef PNG_STDIO_SUPPORTED |
| 1614 | int PNGAPI |
| 1615 | png_image_begin_read_from_stdio(png_imagep image, FILE* file) |
| 1616 | { |
| 1617 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
| 1618 | { |
| 1619 | if (file != NULL) |
| 1620 | { |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1621 | if (png_image_read_init(image) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1622 | { |
| 1623 | /* This is slightly evil, but png_init_io doesn't do anything other |
| 1624 | * than this and we haven't changed the standard IO functions so |
| 1625 | * this saves a 'safe' function. |
| 1626 | */ |
| 1627 | image->opaque->png_ptr->io_ptr = file; |
| 1628 | return png_safe_execute(image, png_image_read_header, image); |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | else |
| 1633 | return png_image_error(image, |
| 1634 | "png_image_begin_read_from_stdio: invalid argument"); |
| 1635 | } |
| 1636 | |
| 1637 | else if (image != NULL) |
| 1638 | return png_image_error(image, |
| 1639 | "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION"); |
| 1640 | |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
| 1644 | int PNGAPI |
| 1645 | png_image_begin_read_from_file(png_imagep image, const char *file_name) |
| 1646 | { |
| 1647 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
| 1648 | { |
| 1649 | if (file_name != NULL) |
| 1650 | { |
| 1651 | FILE *fp = fopen(file_name, "rb"); |
| 1652 | |
| 1653 | if (fp != NULL) |
| 1654 | { |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1655 | if (png_image_read_init(image) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1656 | { |
| 1657 | image->opaque->png_ptr->io_ptr = fp; |
| 1658 | image->opaque->owned_file = 1; |
| 1659 | return png_safe_execute(image, png_image_read_header, image); |
| 1660 | } |
| 1661 | |
| 1662 | /* Clean up: just the opened file. */ |
| 1663 | (void)fclose(fp); |
| 1664 | } |
| 1665 | |
| 1666 | else |
| 1667 | return png_image_error(image, strerror(errno)); |
| 1668 | } |
| 1669 | |
| 1670 | else |
| 1671 | return png_image_error(image, |
| 1672 | "png_image_begin_read_from_file: invalid argument"); |
| 1673 | } |
| 1674 | |
| 1675 | else if (image != NULL) |
| 1676 | return png_image_error(image, |
| 1677 | "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION"); |
| 1678 | |
| 1679 | return 0; |
| 1680 | } |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1681 | #endif /* STDIO */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1682 | |
| 1683 | static void PNGCBAPI |
| 1684 | png_image_memory_read(png_structp png_ptr, png_bytep out, png_size_t need) |
| 1685 | { |
| 1686 | if (png_ptr != NULL) |
| 1687 | { |
| 1688 | png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr); |
| 1689 | if (image != NULL) |
| 1690 | { |
| 1691 | png_controlp cp = image->opaque; |
| 1692 | if (cp != NULL) |
| 1693 | { |
| 1694 | png_const_bytep memory = cp->memory; |
| 1695 | png_size_t size = cp->size; |
| 1696 | |
| 1697 | if (memory != NULL && size >= need) |
| 1698 | { |
| 1699 | memcpy(out, memory, need); |
| 1700 | cp->memory = memory + need; |
| 1701 | cp->size = size - need; |
| 1702 | return; |
| 1703 | } |
| 1704 | |
| 1705 | png_error(png_ptr, "read beyond end of data"); |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | png_error(png_ptr, "invalid memory read"); |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | int PNGAPI png_image_begin_read_from_memory(png_imagep image, |
| 1714 | png_const_voidp memory, png_size_t size) |
| 1715 | { |
| 1716 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
| 1717 | { |
| 1718 | if (memory != NULL && size > 0) |
| 1719 | { |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1720 | if (png_image_read_init(image) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1721 | { |
| 1722 | /* Now set the IO functions to read from the memory buffer and |
| 1723 | * store it into io_ptr. Again do this in-place to avoid calling a |
| 1724 | * libpng function that requires error handling. |
| 1725 | */ |
| 1726 | image->opaque->memory = png_voidcast(png_const_bytep, memory); |
| 1727 | image->opaque->size = size; |
| 1728 | image->opaque->png_ptr->io_ptr = image; |
| 1729 | image->opaque->png_ptr->read_data_fn = png_image_memory_read; |
| 1730 | |
| 1731 | return png_safe_execute(image, png_image_read_header, image); |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | else |
| 1736 | return png_image_error(image, |
| 1737 | "png_image_begin_read_from_memory: invalid argument"); |
| 1738 | } |
| 1739 | |
| 1740 | else if (image != NULL) |
| 1741 | return png_image_error(image, |
| 1742 | "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION"); |
| 1743 | |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
| 1747 | /* Utility function to skip chunks that are not used by the simplified image |
| 1748 | * read functions and an appropriate macro to call it. |
| 1749 | */ |
| 1750 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 1751 | static void |
| 1752 | png_image_skip_unused_chunks(png_structrp png_ptr) |
| 1753 | { |
| 1754 | /* Prepare the reader to ignore all recognized chunks whose data will not |
| 1755 | * be used, i.e., all chunks recognized by libpng except for those |
| 1756 | * involved in basic image reading: |
| 1757 | * |
| 1758 | * IHDR, PLTE, IDAT, IEND |
| 1759 | * |
| 1760 | * Or image data handling: |
| 1761 | * |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1762 | * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT. |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1763 | * |
| 1764 | * This provides a small performance improvement and eliminates any |
| 1765 | * potential vulnerability to security problems in the unused chunks. |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1766 | * |
| 1767 | * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored |
| 1768 | * too. This allows the simplified API to be compiled without iCCP support, |
| 1769 | * however if the support is there the chunk is still checked to detect |
| 1770 | * errors (which are unfortunately quite common.) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1771 | */ |
| 1772 | { |
| 1773 | static PNG_CONST png_byte chunks_to_process[] = { |
| 1774 | 98, 75, 71, 68, '\0', /* bKGD */ |
| 1775 | 99, 72, 82, 77, '\0', /* cHRM */ |
| 1776 | 103, 65, 77, 65, '\0', /* gAMA */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1777 | # ifdef PNG_READ_iCCP_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1778 | 105, 67, 67, 80, '\0', /* iCCP */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1779 | # endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1780 | 115, 66, 73, 84, '\0', /* sBIT */ |
| 1781 | 115, 82, 71, 66, '\0', /* sRGB */ |
| 1782 | }; |
| 1783 | |
| 1784 | /* Ignore unknown chunks and all other chunks except for the |
| 1785 | * IHDR, PLTE, tRNS, IDAT, and IEND chunks. |
| 1786 | */ |
| 1787 | png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER, |
| 1788 | NULL, -1); |
| 1789 | |
| 1790 | /* But do not ignore image data handling chunks */ |
| 1791 | png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT, |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1792 | chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p) |
| 1797 | #else |
| 1798 | # define PNG_SKIP_CHUNKS(p) ((void)0) |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1799 | #endif /* HANDLE_AS_UNKNOWN */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1800 | |
| 1801 | /* The following macro gives the exact rounded answer for all values in the |
| 1802 | * range 0..255 (it actually divides by 51.2, but the rounding still generates |
| 1803 | * the correct numbers 0..5 |
| 1804 | */ |
| 1805 | #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8) |
| 1806 | |
| 1807 | /* Utility functions to make particular color-maps */ |
| 1808 | static void |
| 1809 | set_file_encoding(png_image_read_control *display) |
| 1810 | { |
| 1811 | png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma; |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1812 | if (png_gamma_significant(g) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1813 | { |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1814 | if (png_gamma_not_sRGB(g) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1815 | { |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1816 | display->file_encoding = P_FILE; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1817 | display->gamma_to_linear = png_reciprocal(g); |
| 1818 | } |
| 1819 | |
| 1820 | else |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1821 | display->file_encoding = P_sRGB; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | else |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1825 | display->file_encoding = P_LINEAR8; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | static unsigned int |
| 1829 | decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding) |
| 1830 | { |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1831 | if (encoding == P_FILE) /* double check */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1832 | encoding = display->file_encoding; |
| 1833 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1834 | if (encoding == P_NOTSET) /* must be the file encoding */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1835 | { |
| 1836 | set_file_encoding(display); |
| 1837 | encoding = display->file_encoding; |
| 1838 | } |
| 1839 | |
| 1840 | switch (encoding) |
| 1841 | { |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1842 | case P_FILE: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1843 | value = png_gamma_16bit_correct(value*257, display->gamma_to_linear); |
| 1844 | break; |
| 1845 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1846 | case P_sRGB: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1847 | value = png_sRGB_table[value]; |
| 1848 | break; |
| 1849 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1850 | case P_LINEAR: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1851 | break; |
| 1852 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1853 | case P_LINEAR8: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1854 | value *= 257; |
| 1855 | break; |
| 1856 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1857 | #ifdef __GNUC__ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1858 | default: |
| 1859 | png_error(display->image->opaque->png_ptr, |
| 1860 | "unexpected encoding (internal error)"); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1861 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | return value; |
| 1865 | } |
| 1866 | |
| 1867 | static png_uint_32 |
| 1868 | png_colormap_compose(png_image_read_control *display, |
| 1869 | png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha, |
| 1870 | png_uint_32 background, int encoding) |
| 1871 | { |
| 1872 | /* The file value is composed on the background, the background has the given |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1873 | * encoding and so does the result, the file is encoded with P_FILE and the |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1874 | * file and alpha are 8-bit values. The (output) encoding will always be |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1875 | * P_LINEAR or P_sRGB. |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1876 | */ |
| 1877 | png_uint_32 f = decode_gamma(display, foreground, foreground_encoding); |
| 1878 | png_uint_32 b = decode_gamma(display, background, encoding); |
| 1879 | |
| 1880 | /* The alpha is always an 8-bit value (it comes from the palette), the value |
| 1881 | * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires. |
| 1882 | */ |
| 1883 | f = f * alpha + b * (255-alpha); |
| 1884 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1885 | if (encoding == P_LINEAR) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1886 | { |
| 1887 | /* Scale to 65535; divide by 255, approximately (in fact this is extremely |
| 1888 | * accurate, it divides by 255.00000005937181414556, with no overflow.) |
| 1889 | */ |
| 1890 | f *= 257; /* Now scaled by 65535 */ |
| 1891 | f += f >> 16; |
| 1892 | f = (f+32768) >> 16; |
| 1893 | } |
| 1894 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1895 | else /* P_sRGB */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1896 | f = PNG_sRGB_FROM_LINEAR(f); |
| 1897 | |
| 1898 | return f; |
| 1899 | } |
| 1900 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1901 | /* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1902 | * be 8-bit. |
| 1903 | */ |
| 1904 | static void |
| 1905 | png_create_colormap_entry(png_image_read_control *display, |
| 1906 | png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue, |
| 1907 | png_uint_32 alpha, int encoding) |
| 1908 | { |
| 1909 | png_imagep image = display->image; |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1910 | const int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ? |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1911 | P_LINEAR : P_sRGB; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1912 | const int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 && |
| 1913 | (red != green || green != blue); |
| 1914 | |
| 1915 | if (ip > 255) |
| 1916 | png_error(image->opaque->png_ptr, "color-map index out of range"); |
| 1917 | |
| 1918 | /* Update the cache with whether the file gamma is significantly different |
| 1919 | * from sRGB. |
| 1920 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1921 | if (encoding == P_FILE) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1922 | { |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1923 | if (display->file_encoding == P_NOTSET) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1924 | set_file_encoding(display); |
| 1925 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1926 | /* Note that the cached value may be P_FILE too, but if it is then the |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1927 | * gamma_to_linear member has been set. |
| 1928 | */ |
| 1929 | encoding = display->file_encoding; |
| 1930 | } |
| 1931 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1932 | if (encoding == P_FILE) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1933 | { |
| 1934 | png_fixed_point g = display->gamma_to_linear; |
| 1935 | |
| 1936 | red = png_gamma_16bit_correct(red*257, g); |
| 1937 | green = png_gamma_16bit_correct(green*257, g); |
| 1938 | blue = png_gamma_16bit_correct(blue*257, g); |
| 1939 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1940 | if (convert_to_Y != 0 || output_encoding == P_LINEAR) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1941 | { |
| 1942 | alpha *= 257; |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1943 | encoding = P_LINEAR; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | else |
| 1947 | { |
| 1948 | red = PNG_sRGB_FROM_LINEAR(red * 255); |
| 1949 | green = PNG_sRGB_FROM_LINEAR(green * 255); |
| 1950 | blue = PNG_sRGB_FROM_LINEAR(blue * 255); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1951 | encoding = P_sRGB; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1952 | } |
| 1953 | } |
| 1954 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1955 | else if (encoding == P_LINEAR8) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1956 | { |
| 1957 | /* This encoding occurs quite frequently in test cases because PngSuite |
| 1958 | * includes a gAMA 1.0 chunk with most images. |
| 1959 | */ |
| 1960 | red *= 257; |
| 1961 | green *= 257; |
| 1962 | blue *= 257; |
| 1963 | alpha *= 257; |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1964 | encoding = P_LINEAR; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1967 | else if (encoding == P_sRGB && |
| 1968 | (convert_to_Y != 0 || output_encoding == P_LINEAR)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1969 | { |
| 1970 | /* The values are 8-bit sRGB values, but must be converted to 16-bit |
| 1971 | * linear. |
| 1972 | */ |
| 1973 | red = png_sRGB_table[red]; |
| 1974 | green = png_sRGB_table[green]; |
| 1975 | blue = png_sRGB_table[blue]; |
| 1976 | alpha *= 257; |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1977 | encoding = P_LINEAR; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | /* This is set if the color isn't gray but the output is. */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1981 | if (encoding == P_LINEAR) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1982 | { |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1983 | if (convert_to_Y != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1984 | { |
| 1985 | /* NOTE: these values are copied from png_do_rgb_to_gray */ |
| 1986 | png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green + |
| 1987 | (png_uint_32)2366 * blue; |
| 1988 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1989 | if (output_encoding == P_LINEAR) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1990 | y = (y + 16384) >> 15; |
| 1991 | |
| 1992 | else |
| 1993 | { |
| 1994 | /* y is scaled by 32768, we need it scaled by 255: */ |
| 1995 | y = (y + 128) >> 8; |
| 1996 | y *= 255; |
| 1997 | y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7); |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 1998 | alpha = PNG_DIV257(alpha); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1999 | encoding = P_sRGB; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | blue = red = green = y; |
| 2003 | } |
| 2004 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2005 | else if (output_encoding == P_sRGB) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2006 | { |
| 2007 | red = PNG_sRGB_FROM_LINEAR(red * 255); |
| 2008 | green = PNG_sRGB_FROM_LINEAR(green * 255); |
| 2009 | blue = PNG_sRGB_FROM_LINEAR(blue * 255); |
| 2010 | alpha = PNG_DIV257(alpha); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2011 | encoding = P_sRGB; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2012 | } |
| 2013 | } |
| 2014 | |
| 2015 | if (encoding != output_encoding) |
| 2016 | png_error(image->opaque->png_ptr, "bad encoding (internal error)"); |
| 2017 | |
| 2018 | /* Store the value. */ |
| 2019 | { |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2020 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2021 | const int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 && |
| 2022 | (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; |
| 2023 | # else |
| 2024 | # define afirst 0 |
| 2025 | # endif |
| 2026 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2027 | const int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2028 | # else |
| 2029 | # define bgr 0 |
| 2030 | # endif |
| 2031 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2032 | if (output_encoding == P_LINEAR) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2033 | { |
| 2034 | png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap); |
| 2035 | |
| 2036 | entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); |
| 2037 | |
| 2038 | /* The linear 16-bit values must be pre-multiplied by the alpha channel |
| 2039 | * value, if less than 65535 (this is, effectively, composite on black |
| 2040 | * if the alpha channel is removed.) |
| 2041 | */ |
| 2042 | switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) |
| 2043 | { |
| 2044 | case 4: |
| 2045 | entry[afirst ? 0 : 3] = (png_uint_16)alpha; |
| 2046 | /* FALL THROUGH */ |
| 2047 | |
| 2048 | case 3: |
| 2049 | if (alpha < 65535) |
| 2050 | { |
| 2051 | if (alpha > 0) |
| 2052 | { |
| 2053 | blue = (blue * alpha + 32767U)/65535U; |
| 2054 | green = (green * alpha + 32767U)/65535U; |
| 2055 | red = (red * alpha + 32767U)/65535U; |
| 2056 | } |
| 2057 | |
| 2058 | else |
| 2059 | red = green = blue = 0; |
| 2060 | } |
| 2061 | entry[afirst + (2 ^ bgr)] = (png_uint_16)blue; |
| 2062 | entry[afirst + 1] = (png_uint_16)green; |
| 2063 | entry[afirst + bgr] = (png_uint_16)red; |
| 2064 | break; |
| 2065 | |
| 2066 | case 2: |
| 2067 | entry[1 ^ afirst] = (png_uint_16)alpha; |
| 2068 | /* FALL THROUGH */ |
| 2069 | |
| 2070 | case 1: |
| 2071 | if (alpha < 65535) |
| 2072 | { |
| 2073 | if (alpha > 0) |
| 2074 | green = (green * alpha + 32767U)/65535U; |
| 2075 | |
| 2076 | else |
| 2077 | green = 0; |
| 2078 | } |
| 2079 | entry[afirst] = (png_uint_16)green; |
| 2080 | break; |
| 2081 | |
| 2082 | default: |
| 2083 | break; |
| 2084 | } |
| 2085 | } |
| 2086 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2087 | else /* output encoding is P_sRGB */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2088 | { |
| 2089 | png_bytep entry = png_voidcast(png_bytep, display->colormap); |
| 2090 | |
| 2091 | entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); |
| 2092 | |
| 2093 | switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) |
| 2094 | { |
| 2095 | case 4: |
| 2096 | entry[afirst ? 0 : 3] = (png_byte)alpha; |
| 2097 | case 3: |
| 2098 | entry[afirst + (2 ^ bgr)] = (png_byte)blue; |
| 2099 | entry[afirst + 1] = (png_byte)green; |
| 2100 | entry[afirst + bgr] = (png_byte)red; |
| 2101 | break; |
| 2102 | |
| 2103 | case 2: |
| 2104 | entry[1 ^ afirst] = (png_byte)alpha; |
| 2105 | case 1: |
| 2106 | entry[afirst] = (png_byte)green; |
| 2107 | break; |
| 2108 | |
| 2109 | default: |
| 2110 | break; |
| 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | # ifdef afirst |
| 2115 | # undef afirst |
| 2116 | # endif |
| 2117 | # ifdef bgr |
| 2118 | # undef bgr |
| 2119 | # endif |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | static int |
| 2124 | make_gray_file_colormap(png_image_read_control *display) |
| 2125 | { |
| 2126 | unsigned int i; |
| 2127 | |
| 2128 | for (i=0; i<256; ++i) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2129 | png_create_colormap_entry(display, i, i, i, i, 255, P_FILE); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2130 | |
| 2131 | return i; |
| 2132 | } |
| 2133 | |
| 2134 | static int |
| 2135 | make_gray_colormap(png_image_read_control *display) |
| 2136 | { |
| 2137 | unsigned int i; |
| 2138 | |
| 2139 | for (i=0; i<256; ++i) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2140 | png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2141 | |
| 2142 | return i; |
| 2143 | } |
| 2144 | #define PNG_GRAY_COLORMAP_ENTRIES 256 |
| 2145 | |
| 2146 | static int |
| 2147 | make_ga_colormap(png_image_read_control *display) |
| 2148 | { |
| 2149 | unsigned int i, a; |
| 2150 | |
| 2151 | /* Alpha is retained, the output will be a color-map with entries |
| 2152 | * selected by six levels of alpha. One transparent entry, 6 gray |
| 2153 | * levels for all the intermediate alpha values, leaving 230 entries |
| 2154 | * for the opaque grays. The color-map entries are the six values |
| 2155 | * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the |
| 2156 | * relevant entry. |
| 2157 | * |
| 2158 | * if (alpha > 229) // opaque |
| 2159 | * { |
| 2160 | * // The 231 entries are selected to make the math below work: |
| 2161 | * base = 0; |
| 2162 | * entry = (231 * gray + 128) >> 8; |
| 2163 | * } |
| 2164 | * else if (alpha < 26) // transparent |
| 2165 | * { |
| 2166 | * base = 231; |
| 2167 | * entry = 0; |
| 2168 | * } |
| 2169 | * else // partially opaque |
| 2170 | * { |
| 2171 | * base = 226 + 6 * PNG_DIV51(alpha); |
| 2172 | * entry = PNG_DIV51(gray); |
| 2173 | * } |
| 2174 | */ |
| 2175 | i = 0; |
| 2176 | while (i < 231) |
| 2177 | { |
| 2178 | unsigned int gray = (i * 256 + 115) / 231; |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2179 | png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | /* 255 is used here for the component values for consistency with the code |
| 2183 | * that undoes premultiplication in pngwrite.c. |
| 2184 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2185 | png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2186 | |
| 2187 | for (a=1; a<5; ++a) |
| 2188 | { |
| 2189 | unsigned int g; |
| 2190 | |
| 2191 | for (g=0; g<6; ++g) |
| 2192 | png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2193 | P_sRGB); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | return i; |
| 2197 | } |
| 2198 | |
| 2199 | #define PNG_GA_COLORMAP_ENTRIES 256 |
| 2200 | |
| 2201 | static int |
| 2202 | make_rgb_colormap(png_image_read_control *display) |
| 2203 | { |
| 2204 | unsigned int i, r; |
| 2205 | |
| 2206 | /* Build a 6x6x6 opaque RGB cube */ |
| 2207 | for (i=r=0; r<6; ++r) |
| 2208 | { |
| 2209 | unsigned int g; |
| 2210 | |
| 2211 | for (g=0; g<6; ++g) |
| 2212 | { |
| 2213 | unsigned int b; |
| 2214 | |
| 2215 | for (b=0; b<6; ++b) |
| 2216 | png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2217 | P_sRGB); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | return i; |
| 2222 | } |
| 2223 | |
| 2224 | #define PNG_RGB_COLORMAP_ENTRIES 216 |
| 2225 | |
| 2226 | /* Return a palette index to the above palette given three 8-bit sRGB values. */ |
| 2227 | #define PNG_RGB_INDEX(r,g,b) \ |
| 2228 | ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b))) |
| 2229 | |
| 2230 | static int |
| 2231 | png_image_read_colormap(png_voidp argument) |
| 2232 | { |
| 2233 | png_image_read_control *display = |
| 2234 | png_voidcast(png_image_read_control*, argument); |
| 2235 | const png_imagep image = display->image; |
| 2236 | |
| 2237 | const png_structrp png_ptr = image->opaque->png_ptr; |
| 2238 | const png_uint_32 output_format = image->format; |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2239 | const int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ? |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2240 | P_LINEAR : P_sRGB; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2241 | |
| 2242 | unsigned int cmap_entries; |
| 2243 | unsigned int output_processing; /* Output processing option */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2244 | unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2245 | |
| 2246 | /* Background information; the background color and the index of this color |
| 2247 | * in the color-map if it exists (else 256). |
| 2248 | */ |
| 2249 | unsigned int background_index = 256; |
| 2250 | png_uint_32 back_r, back_g, back_b; |
| 2251 | |
| 2252 | /* Flags to accumulate things that need to be done to the input. */ |
| 2253 | int expand_tRNS = 0; |
| 2254 | |
| 2255 | /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is |
| 2256 | * very difficult to do, the results look awful, and it is difficult to see |
| 2257 | * what possible use it is because the application can't control the |
| 2258 | * color-map. |
| 2259 | */ |
| 2260 | if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 || |
| 2261 | png_ptr->num_trans > 0) /* alpha in input */ && |
| 2262 | ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */) |
| 2263 | { |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2264 | if (output_encoding == P_LINEAR) /* compose on black */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2265 | back_b = back_g = back_r = 0; |
| 2266 | |
| 2267 | else if (display->background == NULL /* no way to remove it */) |
| 2268 | png_error(png_ptr, |
| 2269 | "a background color must be supplied to remove alpha/transparency"); |
| 2270 | |
| 2271 | /* Get a copy of the background color (this avoids repeating the checks |
| 2272 | * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the |
| 2273 | * output format. |
| 2274 | */ |
| 2275 | else |
| 2276 | { |
| 2277 | back_g = display->background->green; |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2278 | if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2279 | { |
| 2280 | back_r = display->background->red; |
| 2281 | back_b = display->background->blue; |
| 2282 | } |
| 2283 | else |
| 2284 | back_b = back_r = back_g; |
| 2285 | } |
| 2286 | } |
| 2287 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2288 | else if (output_encoding == P_LINEAR) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2289 | back_b = back_r = back_g = 65535; |
| 2290 | |
| 2291 | else |
| 2292 | back_b = back_r = back_g = 255; |
| 2293 | |
| 2294 | /* Default the input file gamma if required - this is necessary because |
| 2295 | * libpng assumes that if no gamma information is present the data is in the |
| 2296 | * output format, but the simplified API deduces the gamma from the input |
| 2297 | * format. |
| 2298 | */ |
| 2299 | if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0) |
| 2300 | { |
| 2301 | /* Do this directly, not using the png_colorspace functions, to ensure |
| 2302 | * that it happens even if the colorspace is invalid (though probably if |
| 2303 | * it is the setting will be ignored) Note that the same thing can be |
| 2304 | * achieved at the application interface with png_set_gAMA. |
| 2305 | */ |
| 2306 | if (png_ptr->bit_depth == 16 && |
| 2307 | (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) |
| 2308 | png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR; |
| 2309 | |
| 2310 | else |
| 2311 | png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE; |
| 2312 | |
| 2313 | png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; |
| 2314 | } |
| 2315 | |
| 2316 | /* Decide what to do based on the PNG color type of the input data. The |
| 2317 | * utility function png_create_colormap_entry deals with most aspects of the |
| 2318 | * output transformations; this code works out how to produce bytes of |
| 2319 | * color-map entries from the original format. |
| 2320 | */ |
| 2321 | switch (png_ptr->color_type) |
| 2322 | { |
| 2323 | case PNG_COLOR_TYPE_GRAY: |
| 2324 | if (png_ptr->bit_depth <= 8) |
| 2325 | { |
| 2326 | /* There at most 256 colors in the output, regardless of |
| 2327 | * transparency. |
| 2328 | */ |
| 2329 | unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0; |
| 2330 | |
| 2331 | cmap_entries = 1U << png_ptr->bit_depth; |
| 2332 | if (cmap_entries > image->colormap_entries) |
| 2333 | png_error(png_ptr, "gray[8] color-map: too few entries"); |
| 2334 | |
| 2335 | step = 255 / (cmap_entries - 1); |
| 2336 | output_processing = PNG_CMAP_NONE; |
| 2337 | |
| 2338 | /* If there is a tRNS chunk then this either selects a transparent |
| 2339 | * value or, if the output has no alpha, the background color. |
| 2340 | */ |
| 2341 | if (png_ptr->num_trans > 0) |
| 2342 | { |
| 2343 | trans = png_ptr->trans_color.gray; |
| 2344 | |
| 2345 | if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2346 | back_alpha = output_encoding == P_LINEAR ? 65535 : 255; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | /* png_create_colormap_entry just takes an RGBA and writes the |
| 2350 | * corresponding color-map entry using the format from 'image', |
| 2351 | * including the required conversion to sRGB or linear as |
| 2352 | * appropriate. The input values are always either sRGB (if the |
| 2353 | * gamma correction flag is 0) or 0..255 scaled file encoded values |
| 2354 | * (if the function must gamma correct them). |
| 2355 | */ |
| 2356 | for (i=val=0; i<cmap_entries; ++i, val += step) |
| 2357 | { |
| 2358 | /* 'i' is a file value. While this will result in duplicated |
| 2359 | * entries for 8-bit non-sRGB encoded files it is necessary to |
| 2360 | * have non-gamma corrected values to do tRNS handling. |
| 2361 | */ |
| 2362 | if (i != trans) |
| 2363 | png_create_colormap_entry(display, i, val, val, val, 255, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2364 | P_FILE/*8-bit with file gamma*/); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2365 | |
| 2366 | /* Else this entry is transparent. The colors don't matter if |
| 2367 | * there is an alpha channel (back_alpha == 0), but it does no |
| 2368 | * harm to pass them in; the values are not set above so this |
| 2369 | * passes in white. |
| 2370 | * |
| 2371 | * NOTE: this preserves the full precision of the application |
| 2372 | * supplied background color when it is used. |
| 2373 | */ |
| 2374 | else |
| 2375 | png_create_colormap_entry(display, i, back_r, back_g, back_b, |
| 2376 | back_alpha, output_encoding); |
| 2377 | } |
| 2378 | |
| 2379 | /* We need libpng to preserve the original encoding. */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2380 | data_encoding = P_FILE; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2381 | |
| 2382 | /* The rows from libpng, while technically gray values, are now also |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2383 | * color-map indices; however, they may need to be expanded to 1 |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2384 | * byte per pixel. This is what png_set_packing does (i.e., it |
| 2385 | * unpacks the bit values into bytes.) |
| 2386 | */ |
| 2387 | if (png_ptr->bit_depth < 8) |
| 2388 | png_set_packing(png_ptr); |
| 2389 | } |
| 2390 | |
| 2391 | else /* bit depth is 16 */ |
| 2392 | { |
| 2393 | /* The 16-bit input values can be converted directly to 8-bit gamma |
| 2394 | * encoded values; however, if a tRNS chunk is present 257 color-map |
| 2395 | * entries are required. This means that the extra entry requires |
| 2396 | * special processing; add an alpha channel, sacrifice gray level |
| 2397 | * 254 and convert transparent (alpha==0) entries to that. |
| 2398 | * |
| 2399 | * Use libpng to chop the data to 8 bits. Convert it to sRGB at the |
| 2400 | * same time to minimize quality loss. If a tRNS chunk is present |
| 2401 | * this means libpng must handle it too; otherwise it is impossible |
| 2402 | * to do the exact match on the 16-bit value. |
| 2403 | * |
| 2404 | * If the output has no alpha channel *and* the background color is |
| 2405 | * gray then it is possible to let libpng handle the substitution by |
| 2406 | * ensuring that the corresponding gray level matches the background |
| 2407 | * color exactly. |
| 2408 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2409 | data_encoding = P_sRGB; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2410 | |
| 2411 | if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
| 2412 | png_error(png_ptr, "gray[16] color-map: too few entries"); |
| 2413 | |
| 2414 | cmap_entries = make_gray_colormap(display); |
| 2415 | |
| 2416 | if (png_ptr->num_trans > 0) |
| 2417 | { |
| 2418 | unsigned int back_alpha; |
| 2419 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2420 | if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2421 | back_alpha = 0; |
| 2422 | |
| 2423 | else |
| 2424 | { |
| 2425 | if (back_r == back_g && back_g == back_b) |
| 2426 | { |
| 2427 | /* Background is gray; no special processing will be |
| 2428 | * required. |
| 2429 | */ |
| 2430 | png_color_16 c; |
| 2431 | png_uint_32 gray = back_g; |
| 2432 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2433 | if (output_encoding == P_LINEAR) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2434 | { |
| 2435 | gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
| 2436 | |
| 2437 | /* And make sure the corresponding palette entry |
| 2438 | * matches. |
| 2439 | */ |
| 2440 | png_create_colormap_entry(display, gray, back_g, back_g, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2441 | back_g, 65535, P_LINEAR); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | /* The background passed to libpng, however, must be the |
| 2445 | * sRGB value. |
| 2446 | */ |
| 2447 | c.index = 0; /*unused*/ |
| 2448 | c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
| 2449 | |
| 2450 | /* NOTE: does this work without expanding tRNS to alpha? |
| 2451 | * It should be the color->gray case below apparently |
| 2452 | * doesn't. |
| 2453 | */ |
| 2454 | png_set_background_fixed(png_ptr, &c, |
| 2455 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 2456 | 0/*gamma: not used*/); |
| 2457 | |
| 2458 | output_processing = PNG_CMAP_NONE; |
| 2459 | break; |
| 2460 | } |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2461 | #ifdef __COVERITY__ |
| 2462 | /* Coverity claims that output_encoding cannot be 2 (P_LINEAR) |
| 2463 | * here. |
| 2464 | */ |
| 2465 | back_alpha = 255; |
| 2466 | #else |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2467 | back_alpha = output_encoding == P_LINEAR ? 65535 : 255; |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2468 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | /* output_processing means that the libpng-processed row will be |
| 2472 | * 8-bit GA and it has to be processing to single byte color-map |
| 2473 | * values. Entry 254 is replaced by either a completely |
| 2474 | * transparent entry or by the background color at full |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2475 | * precision (and the background color is not a simple gray |
| 2476 | * level in this case.) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2477 | */ |
| 2478 | expand_tRNS = 1; |
| 2479 | output_processing = PNG_CMAP_TRANS; |
| 2480 | background_index = 254; |
| 2481 | |
| 2482 | /* And set (overwrite) color-map entry 254 to the actual |
| 2483 | * background color at full precision. |
| 2484 | */ |
| 2485 | png_create_colormap_entry(display, 254, back_r, back_g, back_b, |
| 2486 | back_alpha, output_encoding); |
| 2487 | } |
| 2488 | |
| 2489 | else |
| 2490 | output_processing = PNG_CMAP_NONE; |
| 2491 | } |
| 2492 | break; |
| 2493 | |
| 2494 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 2495 | /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum |
| 2496 | * of 65536 combinations. If, however, the alpha channel is to be |
| 2497 | * removed there are only 256 possibilities if the background is gray. |
| 2498 | * (Otherwise there is a subset of the 65536 possibilities defined by |
| 2499 | * the triangle between black, white and the background color.) |
| 2500 | * |
| 2501 | * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to |
| 2502 | * worry about tRNS matching - tRNS is ignored if there is an alpha |
| 2503 | * channel. |
| 2504 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2505 | data_encoding = P_sRGB; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2506 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2507 | if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2508 | { |
| 2509 | if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
| 2510 | png_error(png_ptr, "gray+alpha color-map: too few entries"); |
| 2511 | |
| 2512 | cmap_entries = make_ga_colormap(display); |
| 2513 | |
| 2514 | background_index = PNG_CMAP_GA_BACKGROUND; |
| 2515 | output_processing = PNG_CMAP_GA; |
| 2516 | } |
| 2517 | |
| 2518 | else /* alpha is removed */ |
| 2519 | { |
| 2520 | /* Alpha must be removed as the PNG data is processed when the |
| 2521 | * background is a color because the G and A channels are |
| 2522 | * independent and the vector addition (non-parallel vectors) is a |
| 2523 | * 2-D problem. |
| 2524 | * |
| 2525 | * This can be reduced to the same algorithm as above by making a |
| 2526 | * colormap containing gray levels (for the opaque grays), a |
| 2527 | * background entry (for a transparent pixel) and a set of four six |
| 2528 | * level color values, one set for each intermediate alpha value. |
| 2529 | * See the comments in make_ga_colormap for how this works in the |
| 2530 | * per-pixel processing. |
| 2531 | * |
| 2532 | * If the background is gray, however, we only need a 256 entry gray |
| 2533 | * level color map. It is sufficient to make the entry generated |
| 2534 | * for the background color be exactly the color specified. |
| 2535 | */ |
| 2536 | if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 || |
| 2537 | (back_r == back_g && back_g == back_b)) |
| 2538 | { |
| 2539 | /* Background is gray; no special processing will be required. */ |
| 2540 | png_color_16 c; |
| 2541 | png_uint_32 gray = back_g; |
| 2542 | |
| 2543 | if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
| 2544 | png_error(png_ptr, "gray-alpha color-map: too few entries"); |
| 2545 | |
| 2546 | cmap_entries = make_gray_colormap(display); |
| 2547 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2548 | if (output_encoding == P_LINEAR) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2549 | { |
| 2550 | gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
| 2551 | |
| 2552 | /* And make sure the corresponding palette entry matches. */ |
| 2553 | png_create_colormap_entry(display, gray, back_g, back_g, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2554 | back_g, 65535, P_LINEAR); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | /* The background passed to libpng, however, must be the sRGB |
| 2558 | * value. |
| 2559 | */ |
| 2560 | c.index = 0; /*unused*/ |
| 2561 | c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
| 2562 | |
| 2563 | png_set_background_fixed(png_ptr, &c, |
| 2564 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 2565 | 0/*gamma: not used*/); |
| 2566 | |
| 2567 | output_processing = PNG_CMAP_NONE; |
| 2568 | } |
| 2569 | |
| 2570 | else |
| 2571 | { |
| 2572 | png_uint_32 i, a; |
| 2573 | |
| 2574 | /* This is the same as png_make_ga_colormap, above, except that |
| 2575 | * the entries are all opaque. |
| 2576 | */ |
| 2577 | if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
| 2578 | png_error(png_ptr, "ga-alpha color-map: too few entries"); |
| 2579 | |
| 2580 | i = 0; |
| 2581 | while (i < 231) |
| 2582 | { |
| 2583 | png_uint_32 gray = (i * 256 + 115) / 231; |
| 2584 | png_create_colormap_entry(display, i++, gray, gray, gray, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2585 | 255, P_sRGB); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | /* NOTE: this preserves the full precision of the application |
| 2589 | * background color. |
| 2590 | */ |
| 2591 | background_index = i; |
| 2592 | png_create_colormap_entry(display, i++, back_r, back_g, back_b, |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2593 | #ifdef __COVERITY__ |
| 2594 | /* Coverity claims that output_encoding cannot be 2 (P_LINEAR) |
| 2595 | * here. |
| 2596 | */ 255U, |
| 2597 | #else |
| 2598 | output_encoding == P_LINEAR ? 65535U : 255U, |
| 2599 | #endif |
| 2600 | output_encoding); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2601 | |
| 2602 | /* For non-opaque input composite on the sRGB background - this |
| 2603 | * requires inverting the encoding for each component. The input |
| 2604 | * is still converted to the sRGB encoding because this is a |
| 2605 | * reasonable approximate to the logarithmic curve of human |
| 2606 | * visual sensitivity, at least over the narrow range which PNG |
| 2607 | * represents. Consequently 'G' is always sRGB encoded, while |
| 2608 | * 'A' is linear. We need the linear background colors. |
| 2609 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2610 | if (output_encoding == P_sRGB) /* else already linear */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2611 | { |
| 2612 | /* This may produce a value not exactly matching the |
| 2613 | * background, but that's ok because these numbers are only |
| 2614 | * used when alpha != 0 |
| 2615 | */ |
| 2616 | back_r = png_sRGB_table[back_r]; |
| 2617 | back_g = png_sRGB_table[back_g]; |
| 2618 | back_b = png_sRGB_table[back_b]; |
| 2619 | } |
| 2620 | |
| 2621 | for (a=1; a<5; ++a) |
| 2622 | { |
| 2623 | unsigned int g; |
| 2624 | |
| 2625 | /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled |
| 2626 | * by an 8-bit alpha value (0..255). |
| 2627 | */ |
| 2628 | png_uint_32 alpha = 51 * a; |
| 2629 | png_uint_32 back_rx = (255-alpha) * back_r; |
| 2630 | png_uint_32 back_gx = (255-alpha) * back_g; |
| 2631 | png_uint_32 back_bx = (255-alpha) * back_b; |
| 2632 | |
| 2633 | for (g=0; g<6; ++g) |
| 2634 | { |
| 2635 | png_uint_32 gray = png_sRGB_table[g*51] * alpha; |
| 2636 | |
| 2637 | png_create_colormap_entry(display, i++, |
| 2638 | PNG_sRGB_FROM_LINEAR(gray + back_rx), |
| 2639 | PNG_sRGB_FROM_LINEAR(gray + back_gx), |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2640 | PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2641 | } |
| 2642 | } |
| 2643 | |
| 2644 | cmap_entries = i; |
| 2645 | output_processing = PNG_CMAP_GA; |
| 2646 | } |
| 2647 | } |
| 2648 | break; |
| 2649 | |
| 2650 | case PNG_COLOR_TYPE_RGB: |
| 2651 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 2652 | /* Exclude the case where the output is gray; we can always handle this |
| 2653 | * with the cases above. |
| 2654 | */ |
| 2655 | if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0) |
| 2656 | { |
| 2657 | /* The color-map will be grayscale, so we may as well convert the |
| 2658 | * input RGB values to a simple grayscale and use the grayscale |
| 2659 | * code above. |
| 2660 | * |
| 2661 | * NOTE: calling this apparently damages the recognition of the |
| 2662 | * transparent color in background color handling; call |
| 2663 | * png_set_tRNS_to_alpha before png_set_background_fixed. |
| 2664 | */ |
| 2665 | png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1, |
| 2666 | -1); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2667 | data_encoding = P_sRGB; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2668 | |
| 2669 | /* The output will now be one or two 8-bit gray or gray+alpha |
| 2670 | * channels. The more complex case arises when the input has alpha. |
| 2671 | */ |
| 2672 | if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2673 | png_ptr->num_trans > 0) && |
| 2674 | (output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 2675 | { |
| 2676 | /* Both input and output have an alpha channel, so no background |
| 2677 | * processing is required; just map the GA bytes to the right |
| 2678 | * color-map entry. |
| 2679 | */ |
| 2680 | expand_tRNS = 1; |
| 2681 | |
| 2682 | if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
| 2683 | png_error(png_ptr, "rgb[ga] color-map: too few entries"); |
| 2684 | |
| 2685 | cmap_entries = make_ga_colormap(display); |
| 2686 | background_index = PNG_CMAP_GA_BACKGROUND; |
| 2687 | output_processing = PNG_CMAP_GA; |
| 2688 | } |
| 2689 | |
| 2690 | else |
| 2691 | { |
| 2692 | /* Either the input or the output has no alpha channel, so there |
| 2693 | * will be no non-opaque pixels in the color-map; it will just be |
| 2694 | * grayscale. |
| 2695 | */ |
| 2696 | if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
| 2697 | png_error(png_ptr, "rgb[gray] color-map: too few entries"); |
| 2698 | |
| 2699 | /* Ideally this code would use libpng to do the gamma correction, |
| 2700 | * but if an input alpha channel is to be removed we will hit the |
| 2701 | * libpng bug in gamma+compose+rgb-to-gray (the double gamma |
| 2702 | * correction bug). Fix this by dropping the gamma correction in |
| 2703 | * this case and doing it in the palette; this will result in |
| 2704 | * duplicate palette entries, but that's better than the |
| 2705 | * alternative of double gamma correction. |
| 2706 | */ |
| 2707 | if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2708 | png_ptr->num_trans > 0) && |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2709 | png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2710 | { |
| 2711 | cmap_entries = make_gray_file_colormap(display); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2712 | data_encoding = P_FILE; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | else |
| 2716 | cmap_entries = make_gray_colormap(display); |
| 2717 | |
| 2718 | /* But if the input has alpha or transparency it must be removed |
| 2719 | */ |
| 2720 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2721 | png_ptr->num_trans > 0) |
| 2722 | { |
| 2723 | png_color_16 c; |
| 2724 | png_uint_32 gray = back_g; |
| 2725 | |
| 2726 | /* We need to ensure that the application background exists in |
| 2727 | * the colormap and that completely transparent pixels map to |
| 2728 | * it. Achieve this simply by ensuring that the entry |
| 2729 | * selected for the background really is the background color. |
| 2730 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2731 | if (data_encoding == P_FILE) /* from the fixup above */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2732 | { |
| 2733 | /* The app supplied a gray which is in output_encoding, we |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2734 | * need to convert it to a value of the input (P_FILE) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2735 | * encoding then set this palette entry to the required |
| 2736 | * output encoding. |
| 2737 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2738 | if (output_encoding == P_sRGB) |
| 2739 | gray = png_sRGB_table[gray]; /* now P_LINEAR */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2740 | |
| 2741 | gray = PNG_DIV257(png_gamma_16bit_correct(gray, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2742 | png_ptr->colorspace.gamma)); /* now P_FILE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2743 | |
| 2744 | /* And make sure the corresponding palette entry contains |
| 2745 | * exactly the required sRGB value. |
| 2746 | */ |
| 2747 | png_create_colormap_entry(display, gray, back_g, back_g, |
| 2748 | back_g, 0/*unused*/, output_encoding); |
| 2749 | } |
| 2750 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2751 | else if (output_encoding == P_LINEAR) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2752 | { |
| 2753 | gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
| 2754 | |
| 2755 | /* And make sure the corresponding palette entry matches. |
| 2756 | */ |
| 2757 | png_create_colormap_entry(display, gray, back_g, back_g, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2758 | back_g, 0/*unused*/, P_LINEAR); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2759 | } |
| 2760 | |
| 2761 | /* The background passed to libpng, however, must be the |
| 2762 | * output (normally sRGB) value. |
| 2763 | */ |
| 2764 | c.index = 0; /*unused*/ |
| 2765 | c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
| 2766 | |
| 2767 | /* NOTE: the following is apparently a bug in libpng. Without |
| 2768 | * it the transparent color recognition in |
| 2769 | * png_set_background_fixed seems to go wrong. |
| 2770 | */ |
| 2771 | expand_tRNS = 1; |
| 2772 | png_set_background_fixed(png_ptr, &c, |
| 2773 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 2774 | 0/*gamma: not used*/); |
| 2775 | } |
| 2776 | |
| 2777 | output_processing = PNG_CMAP_NONE; |
| 2778 | } |
| 2779 | } |
| 2780 | |
| 2781 | else /* output is color */ |
| 2782 | { |
| 2783 | /* We could use png_quantize here so long as there is no transparent |
| 2784 | * color or alpha; png_quantize ignores alpha. Easier overall just |
| 2785 | * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube. |
| 2786 | * Consequently we always want libpng to produce sRGB data. |
| 2787 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2788 | data_encoding = P_sRGB; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2789 | |
| 2790 | /* Is there any transparency or alpha? */ |
| 2791 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2792 | png_ptr->num_trans > 0) |
| 2793 | { |
| 2794 | /* Is there alpha in the output too? If so all four channels are |
| 2795 | * processed into a special RGB cube with alpha support. |
| 2796 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2797 | if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2798 | { |
| 2799 | png_uint_32 r; |
| 2800 | |
| 2801 | if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) |
| 2802 | png_error(png_ptr, "rgb+alpha color-map: too few entries"); |
| 2803 | |
| 2804 | cmap_entries = make_rgb_colormap(display); |
| 2805 | |
| 2806 | /* Add a transparent entry. */ |
| 2807 | png_create_colormap_entry(display, cmap_entries, 255, 255, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2808 | 255, 0, P_sRGB); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2809 | |
| 2810 | /* This is stored as the background index for the processing |
| 2811 | * algorithm. |
| 2812 | */ |
| 2813 | background_index = cmap_entries++; |
| 2814 | |
| 2815 | /* Add 27 r,g,b entries each with alpha 0.5. */ |
| 2816 | for (r=0; r<256; r = (r << 1) | 0x7f) |
| 2817 | { |
| 2818 | png_uint_32 g; |
| 2819 | |
| 2820 | for (g=0; g<256; g = (g << 1) | 0x7f) |
| 2821 | { |
| 2822 | png_uint_32 b; |
| 2823 | |
| 2824 | /* This generates components with the values 0, 127 and |
| 2825 | * 255 |
| 2826 | */ |
| 2827 | for (b=0; b<256; b = (b << 1) | 0x7f) |
| 2828 | png_create_colormap_entry(display, cmap_entries++, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2829 | r, g, b, 128, P_sRGB); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | expand_tRNS = 1; |
| 2834 | output_processing = PNG_CMAP_RGB_ALPHA; |
| 2835 | } |
| 2836 | |
| 2837 | else |
| 2838 | { |
| 2839 | /* Alpha/transparency must be removed. The background must |
| 2840 | * exist in the color map (achieved by setting adding it after |
| 2841 | * the 666 color-map). If the standard processing code will |
| 2842 | * pick up this entry automatically that's all that is |
| 2843 | * required; libpng can be called to do the background |
| 2844 | * processing. |
| 2845 | */ |
| 2846 | unsigned int sample_size = |
| 2847 | PNG_IMAGE_SAMPLE_SIZE(output_format); |
| 2848 | png_uint_32 r, g, b; /* sRGB background */ |
| 2849 | |
| 2850 | if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) |
| 2851 | png_error(png_ptr, "rgb-alpha color-map: too few entries"); |
| 2852 | |
| 2853 | cmap_entries = make_rgb_colormap(display); |
| 2854 | |
| 2855 | png_create_colormap_entry(display, cmap_entries, back_r, |
| 2856 | back_g, back_b, 0/*unused*/, output_encoding); |
| 2857 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2858 | if (output_encoding == P_LINEAR) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2859 | { |
| 2860 | r = PNG_sRGB_FROM_LINEAR(back_r * 255); |
| 2861 | g = PNG_sRGB_FROM_LINEAR(back_g * 255); |
| 2862 | b = PNG_sRGB_FROM_LINEAR(back_b * 255); |
| 2863 | } |
| 2864 | |
| 2865 | else |
| 2866 | { |
| 2867 | r = back_r; |
| 2868 | g = back_g; |
| 2869 | b = back_g; |
| 2870 | } |
| 2871 | |
| 2872 | /* Compare the newly-created color-map entry with the one the |
| 2873 | * PNG_CMAP_RGB algorithm will use. If the two entries don't |
| 2874 | * match, add the new one and set this as the background |
| 2875 | * index. |
| 2876 | */ |
| 2877 | if (memcmp((png_const_bytep)display->colormap + |
| 2878 | sample_size * cmap_entries, |
| 2879 | (png_const_bytep)display->colormap + |
| 2880 | sample_size * PNG_RGB_INDEX(r,g,b), |
| 2881 | sample_size) != 0) |
| 2882 | { |
| 2883 | /* The background color must be added. */ |
| 2884 | background_index = cmap_entries++; |
| 2885 | |
| 2886 | /* Add 27 r,g,b entries each with created by composing with |
| 2887 | * the background at alpha 0.5. |
| 2888 | */ |
| 2889 | for (r=0; r<256; r = (r << 1) | 0x7f) |
| 2890 | { |
| 2891 | for (g=0; g<256; g = (g << 1) | 0x7f) |
| 2892 | { |
| 2893 | /* This generates components with the values 0, 127 |
| 2894 | * and 255 |
| 2895 | */ |
| 2896 | for (b=0; b<256; b = (b << 1) | 0x7f) |
| 2897 | png_create_colormap_entry(display, cmap_entries++, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2898 | png_colormap_compose(display, r, P_sRGB, 128, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2899 | back_r, output_encoding), |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2900 | png_colormap_compose(display, g, P_sRGB, 128, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2901 | back_g, output_encoding), |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2902 | png_colormap_compose(display, b, P_sRGB, 128, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2903 | back_b, output_encoding), |
| 2904 | 0/*unused*/, output_encoding); |
| 2905 | } |
| 2906 | } |
| 2907 | |
| 2908 | expand_tRNS = 1; |
| 2909 | output_processing = PNG_CMAP_RGB_ALPHA; |
| 2910 | } |
| 2911 | |
| 2912 | else /* background color is in the standard color-map */ |
| 2913 | { |
| 2914 | png_color_16 c; |
| 2915 | |
| 2916 | c.index = 0; /*unused*/ |
| 2917 | c.red = (png_uint_16)back_r; |
| 2918 | c.gray = c.green = (png_uint_16)back_g; |
| 2919 | c.blue = (png_uint_16)back_b; |
| 2920 | |
| 2921 | png_set_background_fixed(png_ptr, &c, |
| 2922 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 2923 | 0/*gamma: not used*/); |
| 2924 | |
| 2925 | output_processing = PNG_CMAP_RGB; |
| 2926 | } |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | else /* no alpha or transparency in the input */ |
| 2931 | { |
| 2932 | /* Alpha in the output is irrelevant, simply map the opaque input |
| 2933 | * pixels to the 6x6x6 color-map. |
| 2934 | */ |
| 2935 | if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries) |
| 2936 | png_error(png_ptr, "rgb color-map: too few entries"); |
| 2937 | |
| 2938 | cmap_entries = make_rgb_colormap(display); |
| 2939 | output_processing = PNG_CMAP_RGB; |
| 2940 | } |
| 2941 | } |
| 2942 | break; |
| 2943 | |
| 2944 | case PNG_COLOR_TYPE_PALETTE: |
| 2945 | /* It's already got a color-map. It may be necessary to eliminate the |
| 2946 | * tRNS entries though. |
| 2947 | */ |
| 2948 | { |
| 2949 | unsigned int num_trans = png_ptr->num_trans; |
| 2950 | png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL; |
| 2951 | png_const_colorp colormap = png_ptr->palette; |
| 2952 | const int do_background = trans != NULL && |
| 2953 | (output_format & PNG_FORMAT_FLAG_ALPHA) == 0; |
| 2954 | unsigned int i; |
| 2955 | |
| 2956 | /* Just in case: */ |
| 2957 | if (trans == NULL) |
| 2958 | num_trans = 0; |
| 2959 | |
| 2960 | output_processing = PNG_CMAP_NONE; |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2961 | data_encoding = P_FILE; /* Don't change from color-map indices */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2962 | cmap_entries = png_ptr->num_palette; |
| 2963 | if (cmap_entries > 256) |
| 2964 | cmap_entries = 256; |
| 2965 | |
| 2966 | if (cmap_entries > image->colormap_entries) |
| 2967 | png_error(png_ptr, "palette color-map: too few entries"); |
| 2968 | |
| 2969 | for (i=0; i < cmap_entries; ++i) |
| 2970 | { |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 2971 | if (do_background != 0 && i < num_trans && trans[i] < 255) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2972 | { |
| 2973 | if (trans[i] == 0) |
| 2974 | png_create_colormap_entry(display, i, back_r, back_g, |
| 2975 | back_b, 0, output_encoding); |
| 2976 | |
| 2977 | else |
| 2978 | { |
| 2979 | /* Must compose the PNG file color in the color-map entry |
| 2980 | * on the sRGB color in 'back'. |
| 2981 | */ |
| 2982 | png_create_colormap_entry(display, i, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2983 | png_colormap_compose(display, colormap[i].red, P_FILE, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2984 | trans[i], back_r, output_encoding), |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2985 | png_colormap_compose(display, colormap[i].green, P_FILE, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2986 | trans[i], back_g, output_encoding), |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2987 | png_colormap_compose(display, colormap[i].blue, P_FILE, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2988 | trans[i], back_b, output_encoding), |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2989 | output_encoding == P_LINEAR ? trans[i] * 257U : |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2990 | trans[i], |
| 2991 | output_encoding); |
| 2992 | } |
| 2993 | } |
| 2994 | |
| 2995 | else |
| 2996 | png_create_colormap_entry(display, i, colormap[i].red, |
| 2997 | colormap[i].green, colormap[i].blue, |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2998 | i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2999 | } |
| 3000 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3001 | /* The PNG data may have indices packed in fewer than 8 bits, it |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3002 | * must be expanded if so. |
| 3003 | */ |
| 3004 | if (png_ptr->bit_depth < 8) |
| 3005 | png_set_packing(png_ptr); |
| 3006 | } |
| 3007 | break; |
| 3008 | |
| 3009 | default: |
| 3010 | png_error(png_ptr, "invalid PNG color type"); |
| 3011 | /*NOT REACHED*/ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | /* Now deal with the output processing */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3015 | if (expand_tRNS != 0 && png_ptr->num_trans > 0 && |
| 3016 | (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3017 | png_set_tRNS_to_alpha(png_ptr); |
| 3018 | |
| 3019 | switch (data_encoding) |
| 3020 | { |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 3021 | case P_sRGB: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3022 | /* Change to 8-bit sRGB */ |
| 3023 | png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB); |
| 3024 | /* FALL THROUGH */ |
| 3025 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 3026 | case P_FILE: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3027 | if (png_ptr->bit_depth > 8) |
| 3028 | png_set_scale_16(png_ptr); |
| 3029 | break; |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3030 | |
| 3031 | #ifdef __GNUC__ |
| 3032 | default: |
| 3033 | png_error(png_ptr, "bad data option (internal error)"); |
| 3034 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | if (cmap_entries > 256 || cmap_entries > image->colormap_entries) |
| 3038 | png_error(png_ptr, "color map overflow (BAD internal error)"); |
| 3039 | |
| 3040 | image->colormap_entries = cmap_entries; |
| 3041 | |
| 3042 | /* Double check using the recorded background index */ |
| 3043 | switch (output_processing) |
| 3044 | { |
| 3045 | case PNG_CMAP_NONE: |
| 3046 | if (background_index != PNG_CMAP_NONE_BACKGROUND) |
| 3047 | goto bad_background; |
| 3048 | break; |
| 3049 | |
| 3050 | case PNG_CMAP_GA: |
| 3051 | if (background_index != PNG_CMAP_GA_BACKGROUND) |
| 3052 | goto bad_background; |
| 3053 | break; |
| 3054 | |
| 3055 | case PNG_CMAP_TRANS: |
| 3056 | if (background_index >= cmap_entries || |
| 3057 | background_index != PNG_CMAP_TRANS_BACKGROUND) |
| 3058 | goto bad_background; |
| 3059 | break; |
| 3060 | |
| 3061 | case PNG_CMAP_RGB: |
| 3062 | if (background_index != PNG_CMAP_RGB_BACKGROUND) |
| 3063 | goto bad_background; |
| 3064 | break; |
| 3065 | |
| 3066 | case PNG_CMAP_RGB_ALPHA: |
| 3067 | if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND) |
| 3068 | goto bad_background; |
| 3069 | break; |
| 3070 | |
| 3071 | default: |
| 3072 | png_error(png_ptr, "bad processing option (internal error)"); |
| 3073 | |
| 3074 | bad_background: |
| 3075 | png_error(png_ptr, "bad background index (internal error)"); |
| 3076 | } |
| 3077 | |
| 3078 | display->colormap_processing = output_processing; |
| 3079 | |
| 3080 | return 1/*ok*/; |
| 3081 | } |
| 3082 | |
| 3083 | /* The final part of the color-map read called from png_image_finish_read. */ |
| 3084 | static int |
| 3085 | png_image_read_and_map(png_voidp argument) |
| 3086 | { |
| 3087 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3088 | argument); |
| 3089 | png_imagep image = display->image; |
| 3090 | png_structrp png_ptr = image->opaque->png_ptr; |
| 3091 | int passes; |
| 3092 | |
| 3093 | /* Called when the libpng data must be transformed into the color-mapped |
| 3094 | * form. There is a local row buffer in display->local and this routine must |
| 3095 | * do the interlace handling. |
| 3096 | */ |
| 3097 | switch (png_ptr->interlaced) |
| 3098 | { |
| 3099 | case PNG_INTERLACE_NONE: |
| 3100 | passes = 1; |
| 3101 | break; |
| 3102 | |
| 3103 | case PNG_INTERLACE_ADAM7: |
| 3104 | passes = PNG_INTERLACE_ADAM7_PASSES; |
| 3105 | break; |
| 3106 | |
| 3107 | default: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3108 | png_error(png_ptr, "unknown interlace type"); |
| 3109 | } |
| 3110 | |
| 3111 | { |
| 3112 | png_uint_32 height = image->height; |
| 3113 | png_uint_32 width = image->width; |
| 3114 | int proc = display->colormap_processing; |
| 3115 | png_bytep first_row = png_voidcast(png_bytep, display->first_row); |
| 3116 | ptrdiff_t step_row = display->row_bytes; |
| 3117 | int pass; |
| 3118 | |
| 3119 | for (pass = 0; pass < passes; ++pass) |
| 3120 | { |
| 3121 | unsigned int startx, stepx, stepy; |
| 3122 | png_uint_32 y; |
| 3123 | |
| 3124 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
| 3125 | { |
| 3126 | /* The row may be empty for a short image: */ |
| 3127 | if (PNG_PASS_COLS(width, pass) == 0) |
| 3128 | continue; |
| 3129 | |
| 3130 | startx = PNG_PASS_START_COL(pass); |
| 3131 | stepx = PNG_PASS_COL_OFFSET(pass); |
| 3132 | y = PNG_PASS_START_ROW(pass); |
| 3133 | stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3134 | } |
| 3135 | |
| 3136 | else |
| 3137 | { |
| 3138 | y = 0; |
| 3139 | startx = 0; |
| 3140 | stepx = stepy = 1; |
| 3141 | } |
| 3142 | |
| 3143 | for (; y<height; y += stepy) |
| 3144 | { |
| 3145 | png_bytep inrow = png_voidcast(png_bytep, display->local_row); |
| 3146 | png_bytep outrow = first_row + y * step_row; |
| 3147 | png_const_bytep end_row = outrow + width; |
| 3148 | |
| 3149 | /* Read read the libpng data into the temporary buffer. */ |
| 3150 | png_read_row(png_ptr, inrow, NULL); |
| 3151 | |
| 3152 | /* Now process the row according to the processing option, note |
| 3153 | * that the caller verifies that the format of the libpng output |
| 3154 | * data is as required. |
| 3155 | */ |
| 3156 | outrow += startx; |
| 3157 | switch (proc) |
| 3158 | { |
| 3159 | case PNG_CMAP_GA: |
| 3160 | for (; outrow < end_row; outrow += stepx) |
| 3161 | { |
| 3162 | /* The data is always in the PNG order */ |
| 3163 | unsigned int gray = *inrow++; |
| 3164 | unsigned int alpha = *inrow++; |
| 3165 | unsigned int entry; |
| 3166 | |
| 3167 | /* NOTE: this code is copied as a comment in |
| 3168 | * make_ga_colormap above. Please update the |
| 3169 | * comment if you change this code! |
| 3170 | */ |
| 3171 | if (alpha > 229) /* opaque */ |
| 3172 | { |
| 3173 | entry = (231 * gray + 128) >> 8; |
| 3174 | } |
| 3175 | else if (alpha < 26) /* transparent */ |
| 3176 | { |
| 3177 | entry = 231; |
| 3178 | } |
| 3179 | else /* partially opaque */ |
| 3180 | { |
| 3181 | entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray); |
| 3182 | } |
| 3183 | |
| 3184 | *outrow = (png_byte)entry; |
| 3185 | } |
| 3186 | break; |
| 3187 | |
| 3188 | case PNG_CMAP_TRANS: |
| 3189 | for (; outrow < end_row; outrow += stepx) |
| 3190 | { |
| 3191 | png_byte gray = *inrow++; |
| 3192 | png_byte alpha = *inrow++; |
| 3193 | |
| 3194 | if (alpha == 0) |
| 3195 | *outrow = PNG_CMAP_TRANS_BACKGROUND; |
| 3196 | |
| 3197 | else if (gray != PNG_CMAP_TRANS_BACKGROUND) |
| 3198 | *outrow = gray; |
| 3199 | |
| 3200 | else |
| 3201 | *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1); |
| 3202 | } |
| 3203 | break; |
| 3204 | |
| 3205 | case PNG_CMAP_RGB: |
| 3206 | for (; outrow < end_row; outrow += stepx) |
| 3207 | { |
| 3208 | *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]); |
| 3209 | inrow += 3; |
| 3210 | } |
| 3211 | break; |
| 3212 | |
| 3213 | case PNG_CMAP_RGB_ALPHA: |
| 3214 | for (; outrow < end_row; outrow += stepx) |
| 3215 | { |
| 3216 | unsigned int alpha = inrow[3]; |
| 3217 | |
| 3218 | /* Because the alpha entries only hold alpha==0.5 values |
| 3219 | * split the processing at alpha==0.25 (64) and 0.75 |
| 3220 | * (196). |
| 3221 | */ |
| 3222 | |
| 3223 | if (alpha >= 196) |
| 3224 | *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], |
| 3225 | inrow[2]); |
| 3226 | |
| 3227 | else if (alpha < 64) |
| 3228 | *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND; |
| 3229 | |
| 3230 | else |
| 3231 | { |
| 3232 | /* Likewise there are three entries for each of r, g |
| 3233 | * and b. We could select the entry by popcount on |
| 3234 | * the top two bits on those architectures that |
| 3235 | * support it, this is what the code below does, |
| 3236 | * crudely. |
| 3237 | */ |
| 3238 | unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1; |
| 3239 | |
| 3240 | /* Here are how the values map: |
| 3241 | * |
| 3242 | * 0x00 .. 0x3f -> 0 |
| 3243 | * 0x40 .. 0xbf -> 1 |
| 3244 | * 0xc0 .. 0xff -> 2 |
| 3245 | * |
| 3246 | * So, as above with the explicit alpha checks, the |
| 3247 | * breakpoints are at 64 and 196. |
| 3248 | */ |
| 3249 | if (inrow[0] & 0x80) back_i += 9; /* red */ |
| 3250 | if (inrow[0] & 0x40) back_i += 9; |
| 3251 | if (inrow[0] & 0x80) back_i += 3; /* green */ |
| 3252 | if (inrow[0] & 0x40) back_i += 3; |
| 3253 | if (inrow[0] & 0x80) back_i += 1; /* blue */ |
| 3254 | if (inrow[0] & 0x40) back_i += 1; |
| 3255 | |
| 3256 | *outrow = (png_byte)back_i; |
| 3257 | } |
| 3258 | |
| 3259 | inrow += 4; |
| 3260 | } |
| 3261 | break; |
| 3262 | |
| 3263 | default: |
| 3264 | break; |
| 3265 | } |
| 3266 | } |
| 3267 | } |
| 3268 | } |
| 3269 | |
| 3270 | return 1; |
| 3271 | } |
| 3272 | |
| 3273 | static int |
| 3274 | png_image_read_colormapped(png_voidp argument) |
| 3275 | { |
| 3276 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3277 | argument); |
| 3278 | png_imagep image = display->image; |
| 3279 | png_controlp control = image->opaque; |
| 3280 | png_structrp png_ptr = control->png_ptr; |
| 3281 | png_inforp info_ptr = control->info_ptr; |
| 3282 | |
| 3283 | int passes = 0; /* As a flag */ |
| 3284 | |
| 3285 | PNG_SKIP_CHUNKS(png_ptr); |
| 3286 | |
| 3287 | /* Update the 'info' structure and make sure the result is as required; first |
| 3288 | * make sure to turn on the interlace handling if it will be required |
| 3289 | * (because it can't be turned on *after* the call to png_read_update_info!) |
| 3290 | */ |
| 3291 | if (display->colormap_processing == PNG_CMAP_NONE) |
| 3292 | passes = png_set_interlace_handling(png_ptr); |
| 3293 | |
| 3294 | png_read_update_info(png_ptr, info_ptr); |
| 3295 | |
| 3296 | /* The expected output can be deduced from the colormap_processing option. */ |
| 3297 | switch (display->colormap_processing) |
| 3298 | { |
| 3299 | case PNG_CMAP_NONE: |
| 3300 | /* Output must be one channel and one byte per pixel, the output |
| 3301 | * encoding can be anything. |
| 3302 | */ |
| 3303 | if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE || |
| 3304 | info_ptr->color_type == PNG_COLOR_TYPE_GRAY) && |
| 3305 | info_ptr->bit_depth == 8) |
| 3306 | break; |
| 3307 | |
| 3308 | goto bad_output; |
| 3309 | |
| 3310 | case PNG_CMAP_TRANS: |
| 3311 | case PNG_CMAP_GA: |
| 3312 | /* Output must be two channels and the 'G' one must be sRGB, the latter |
| 3313 | * can be checked with an exact number because it should have been set |
| 3314 | * to this number above! |
| 3315 | */ |
| 3316 | if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && |
| 3317 | info_ptr->bit_depth == 8 && |
| 3318 | png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
| 3319 | image->colormap_entries == 256) |
| 3320 | break; |
| 3321 | |
| 3322 | goto bad_output; |
| 3323 | |
| 3324 | case PNG_CMAP_RGB: |
| 3325 | /* Output must be 8-bit sRGB encoded RGB */ |
| 3326 | if (info_ptr->color_type == PNG_COLOR_TYPE_RGB && |
| 3327 | info_ptr->bit_depth == 8 && |
| 3328 | png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
| 3329 | image->colormap_entries == 216) |
| 3330 | break; |
| 3331 | |
| 3332 | goto bad_output; |
| 3333 | |
| 3334 | case PNG_CMAP_RGB_ALPHA: |
| 3335 | /* Output must be 8-bit sRGB encoded RGBA */ |
| 3336 | if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA && |
| 3337 | info_ptr->bit_depth == 8 && |
| 3338 | png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
| 3339 | image->colormap_entries == 244 /* 216 + 1 + 27 */) |
| 3340 | break; |
| 3341 | |
| 3342 | /* goto bad_output; */ |
| 3343 | /* FALL THROUGH */ |
| 3344 | |
| 3345 | default: |
| 3346 | bad_output: |
| 3347 | png_error(png_ptr, "bad color-map processing (internal error)"); |
| 3348 | } |
| 3349 | |
| 3350 | /* Now read the rows. Do this here if it is possible to read directly into |
| 3351 | * the output buffer, otherwise allocate a local row buffer of the maximum |
| 3352 | * size libpng requires and call the relevant processing routine safely. |
| 3353 | */ |
| 3354 | { |
| 3355 | png_voidp first_row = display->buffer; |
| 3356 | ptrdiff_t row_bytes = display->row_stride; |
| 3357 | |
| 3358 | /* The following expression is designed to work correctly whether it gives |
| 3359 | * a signed or an unsigned result. |
| 3360 | */ |
| 3361 | if (row_bytes < 0) |
| 3362 | { |
| 3363 | char *ptr = png_voidcast(char*, first_row); |
| 3364 | ptr += (image->height-1) * (-row_bytes); |
| 3365 | first_row = png_voidcast(png_voidp, ptr); |
| 3366 | } |
| 3367 | |
| 3368 | display->first_row = first_row; |
| 3369 | display->row_bytes = row_bytes; |
| 3370 | } |
| 3371 | |
| 3372 | if (passes == 0) |
| 3373 | { |
| 3374 | int result; |
| 3375 | png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); |
| 3376 | |
| 3377 | display->local_row = row; |
| 3378 | result = png_safe_execute(image, png_image_read_and_map, display); |
| 3379 | display->local_row = NULL; |
| 3380 | png_free(png_ptr, row); |
| 3381 | |
| 3382 | return result; |
| 3383 | } |
| 3384 | |
| 3385 | else |
| 3386 | { |
| 3387 | png_alloc_size_t row_bytes = display->row_bytes; |
| 3388 | |
| 3389 | while (--passes >= 0) |
| 3390 | { |
| 3391 | png_uint_32 y = image->height; |
| 3392 | png_bytep row = png_voidcast(png_bytep, display->first_row); |
| 3393 | |
| 3394 | while (y-- > 0) |
| 3395 | { |
| 3396 | png_read_row(png_ptr, row, NULL); |
| 3397 | row += row_bytes; |
| 3398 | } |
| 3399 | } |
| 3400 | |
| 3401 | return 1; |
| 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | /* Just the row reading part of png_image_read. */ |
| 3406 | static int |
| 3407 | png_image_read_composite(png_voidp argument) |
| 3408 | { |
| 3409 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3410 | argument); |
| 3411 | png_imagep image = display->image; |
| 3412 | png_structrp png_ptr = image->opaque->png_ptr; |
| 3413 | int passes; |
| 3414 | |
| 3415 | switch (png_ptr->interlaced) |
| 3416 | { |
| 3417 | case PNG_INTERLACE_NONE: |
| 3418 | passes = 1; |
| 3419 | break; |
| 3420 | |
| 3421 | case PNG_INTERLACE_ADAM7: |
| 3422 | passes = PNG_INTERLACE_ADAM7_PASSES; |
| 3423 | break; |
| 3424 | |
| 3425 | default: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3426 | png_error(png_ptr, "unknown interlace type"); |
| 3427 | } |
| 3428 | |
| 3429 | { |
| 3430 | png_uint_32 height = image->height; |
| 3431 | png_uint_32 width = image->width; |
| 3432 | ptrdiff_t step_row = display->row_bytes; |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3433 | unsigned int channels = |
| 3434 | (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3435 | int pass; |
| 3436 | |
| 3437 | for (pass = 0; pass < passes; ++pass) |
| 3438 | { |
| 3439 | unsigned int startx, stepx, stepy; |
| 3440 | png_uint_32 y; |
| 3441 | |
| 3442 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
| 3443 | { |
| 3444 | /* The row may be empty for a short image: */ |
| 3445 | if (PNG_PASS_COLS(width, pass) == 0) |
| 3446 | continue; |
| 3447 | |
| 3448 | startx = PNG_PASS_START_COL(pass) * channels; |
| 3449 | stepx = PNG_PASS_COL_OFFSET(pass) * channels; |
| 3450 | y = PNG_PASS_START_ROW(pass); |
| 3451 | stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3452 | } |
| 3453 | |
| 3454 | else |
| 3455 | { |
| 3456 | y = 0; |
| 3457 | startx = 0; |
| 3458 | stepx = channels; |
| 3459 | stepy = 1; |
| 3460 | } |
| 3461 | |
| 3462 | for (; y<height; y += stepy) |
| 3463 | { |
| 3464 | png_bytep inrow = png_voidcast(png_bytep, display->local_row); |
| 3465 | png_bytep outrow; |
| 3466 | png_const_bytep end_row; |
| 3467 | |
| 3468 | /* Read the row, which is packed: */ |
| 3469 | png_read_row(png_ptr, inrow, NULL); |
| 3470 | |
| 3471 | outrow = png_voidcast(png_bytep, display->first_row); |
| 3472 | outrow += y * step_row; |
| 3473 | end_row = outrow + width * channels; |
| 3474 | |
| 3475 | /* Now do the composition on each pixel in this row. */ |
| 3476 | outrow += startx; |
| 3477 | for (; outrow < end_row; outrow += stepx) |
| 3478 | { |
| 3479 | png_byte alpha = inrow[channels]; |
| 3480 | |
| 3481 | if (alpha > 0) /* else no change to the output */ |
| 3482 | { |
| 3483 | unsigned int c; |
| 3484 | |
| 3485 | for (c=0; c<channels; ++c) |
| 3486 | { |
| 3487 | png_uint_32 component = inrow[c]; |
| 3488 | |
| 3489 | if (alpha < 255) /* else just use component */ |
| 3490 | { |
| 3491 | /* This is PNG_OPTIMIZED_ALPHA, the component value |
| 3492 | * is a linear 8-bit value. Combine this with the |
| 3493 | * current outrow[c] value which is sRGB encoded. |
| 3494 | * Arithmetic here is 16-bits to preserve the output |
| 3495 | * values correctly. |
| 3496 | */ |
| 3497 | component *= 257*255; /* =65535 */ |
| 3498 | component += (255-alpha)*png_sRGB_table[outrow[c]]; |
| 3499 | |
| 3500 | /* So 'component' is scaled by 255*65535 and is |
| 3501 | * therefore appropriate for the sRGB to linear |
| 3502 | * conversion table. |
| 3503 | */ |
| 3504 | component = PNG_sRGB_FROM_LINEAR(component); |
| 3505 | } |
| 3506 | |
| 3507 | outrow[c] = (png_byte)component; |
| 3508 | } |
| 3509 | } |
| 3510 | |
| 3511 | inrow += channels+1; /* components and alpha channel */ |
| 3512 | } |
| 3513 | } |
| 3514 | } |
| 3515 | } |
| 3516 | |
| 3517 | return 1; |
| 3518 | } |
| 3519 | |
| 3520 | /* The do_local_background case; called when all the following transforms are to |
| 3521 | * be done: |
| 3522 | * |
| 3523 | * PNG_RGB_TO_GRAY |
| 3524 | * PNG_COMPOSITE |
| 3525 | * PNG_GAMMA |
| 3526 | * |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3527 | * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3528 | * PNG_COMPOSITE code performs gamma correction, so we get double gamma |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3529 | * correction. The fix-up is to prevent the PNG_COMPOSITE operation from |
| 3530 | * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha |
| 3531 | * row and handles the removal or pre-multiplication of the alpha channel. |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3532 | */ |
| 3533 | static int |
| 3534 | png_image_read_background(png_voidp argument) |
| 3535 | { |
| 3536 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3537 | argument); |
| 3538 | png_imagep image = display->image; |
| 3539 | png_structrp png_ptr = image->opaque->png_ptr; |
| 3540 | png_inforp info_ptr = image->opaque->info_ptr; |
| 3541 | png_uint_32 height = image->height; |
| 3542 | png_uint_32 width = image->width; |
| 3543 | int pass, passes; |
| 3544 | |
| 3545 | /* Double check the convoluted logic below. We expect to get here with |
| 3546 | * libpng doing rgb to gray and gamma correction but background processing |
| 3547 | * left to the png_image_read_background function. The rows libpng produce |
| 3548 | * might be 8 or 16-bit but should always have two channels; gray plus alpha. |
| 3549 | */ |
| 3550 | if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) |
| 3551 | png_error(png_ptr, "lost rgb to gray"); |
| 3552 | |
| 3553 | if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
| 3554 | png_error(png_ptr, "unexpected compose"); |
| 3555 | |
| 3556 | if (png_get_channels(png_ptr, info_ptr) != 2) |
| 3557 | png_error(png_ptr, "lost/gained channels"); |
| 3558 | |
| 3559 | /* Expect the 8-bit case to always remove the alpha channel */ |
| 3560 | if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 && |
| 3561 | (image->format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 3562 | png_error(png_ptr, "unexpected 8-bit transformation"); |
| 3563 | |
| 3564 | switch (png_ptr->interlaced) |
| 3565 | { |
| 3566 | case PNG_INTERLACE_NONE: |
| 3567 | passes = 1; |
| 3568 | break; |
| 3569 | |
| 3570 | case PNG_INTERLACE_ADAM7: |
| 3571 | passes = PNG_INTERLACE_ADAM7_PASSES; |
| 3572 | break; |
| 3573 | |
| 3574 | default: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3575 | png_error(png_ptr, "unknown interlace type"); |
| 3576 | } |
| 3577 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 3578 | /* Use direct access to info_ptr here because otherwise the simplified API |
| 3579 | * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is |
| 3580 | * checking the value after libpng expansions, not the original value in the |
| 3581 | * PNG. |
| 3582 | */ |
| 3583 | switch (info_ptr->bit_depth) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3584 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3585 | case 8: |
| 3586 | /* 8-bit sRGB gray values with an alpha channel; the alpha channel is |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 3587 | * to be removed by composing on a background: either the row if |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3588 | * display->background is NULL or display->background->green if not. |
| 3589 | * Unlike the code above ALPHA_OPTIMIZED has *not* been done. |
| 3590 | */ |
| 3591 | { |
| 3592 | png_bytep first_row = png_voidcast(png_bytep, display->first_row); |
| 3593 | ptrdiff_t step_row = display->row_bytes; |
| 3594 | |
| 3595 | for (pass = 0; pass < passes; ++pass) |
| 3596 | { |
| 3597 | png_bytep row = png_voidcast(png_bytep, |
| 3598 | display->first_row); |
| 3599 | unsigned int startx, stepx, stepy; |
| 3600 | png_uint_32 y; |
| 3601 | |
| 3602 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
| 3603 | { |
| 3604 | /* The row may be empty for a short image: */ |
| 3605 | if (PNG_PASS_COLS(width, pass) == 0) |
| 3606 | continue; |
| 3607 | |
| 3608 | startx = PNG_PASS_START_COL(pass); |
| 3609 | stepx = PNG_PASS_COL_OFFSET(pass); |
| 3610 | y = PNG_PASS_START_ROW(pass); |
| 3611 | stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3612 | } |
| 3613 | |
| 3614 | else |
| 3615 | { |
| 3616 | y = 0; |
| 3617 | startx = 0; |
| 3618 | stepx = stepy = 1; |
| 3619 | } |
| 3620 | |
| 3621 | if (display->background == NULL) |
| 3622 | { |
| 3623 | for (; y<height; y += stepy) |
| 3624 | { |
| 3625 | png_bytep inrow = png_voidcast(png_bytep, |
| 3626 | display->local_row); |
| 3627 | png_bytep outrow = first_row + y * step_row; |
| 3628 | png_const_bytep end_row = outrow + width; |
| 3629 | |
| 3630 | /* Read the row, which is packed: */ |
| 3631 | png_read_row(png_ptr, inrow, NULL); |
| 3632 | |
| 3633 | /* Now do the composition on each pixel in this row. */ |
| 3634 | outrow += startx; |
| 3635 | for (; outrow < end_row; outrow += stepx) |
| 3636 | { |
| 3637 | png_byte alpha = inrow[1]; |
| 3638 | |
| 3639 | if (alpha > 0) /* else no change to the output */ |
| 3640 | { |
| 3641 | png_uint_32 component = inrow[0]; |
| 3642 | |
| 3643 | if (alpha < 255) /* else just use component */ |
| 3644 | { |
| 3645 | /* Since PNG_OPTIMIZED_ALPHA was not set it is |
| 3646 | * necessary to invert the sRGB transfer |
| 3647 | * function and multiply the alpha out. |
| 3648 | */ |
| 3649 | component = png_sRGB_table[component] * alpha; |
| 3650 | component += png_sRGB_table[outrow[0]] * |
| 3651 | (255-alpha); |
| 3652 | component = PNG_sRGB_FROM_LINEAR(component); |
| 3653 | } |
| 3654 | |
| 3655 | outrow[0] = (png_byte)component; |
| 3656 | } |
| 3657 | |
| 3658 | inrow += 2; /* gray and alpha channel */ |
| 3659 | } |
| 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | else /* constant background value */ |
| 3664 | { |
| 3665 | png_byte background8 = display->background->green; |
| 3666 | png_uint_16 background = png_sRGB_table[background8]; |
| 3667 | |
| 3668 | for (; y<height; y += stepy) |
| 3669 | { |
| 3670 | png_bytep inrow = png_voidcast(png_bytep, |
| 3671 | display->local_row); |
| 3672 | png_bytep outrow = first_row + y * step_row; |
| 3673 | png_const_bytep end_row = outrow + width; |
| 3674 | |
| 3675 | /* Read the row, which is packed: */ |
| 3676 | png_read_row(png_ptr, inrow, NULL); |
| 3677 | |
| 3678 | /* Now do the composition on each pixel in this row. */ |
| 3679 | outrow += startx; |
| 3680 | for (; outrow < end_row; outrow += stepx) |
| 3681 | { |
| 3682 | png_byte alpha = inrow[1]; |
| 3683 | |
| 3684 | if (alpha > 0) /* else use background */ |
| 3685 | { |
| 3686 | png_uint_32 component = inrow[0]; |
| 3687 | |
| 3688 | if (alpha < 255) /* else just use component */ |
| 3689 | { |
| 3690 | component = png_sRGB_table[component] * alpha; |
| 3691 | component += background * (255-alpha); |
| 3692 | component = PNG_sRGB_FROM_LINEAR(component); |
| 3693 | } |
| 3694 | |
| 3695 | outrow[0] = (png_byte)component; |
| 3696 | } |
| 3697 | |
| 3698 | else |
| 3699 | outrow[0] = background8; |
| 3700 | |
| 3701 | inrow += 2; /* gray and alpha channel */ |
| 3702 | } |
| 3703 | |
| 3704 | row += display->row_bytes; |
| 3705 | } |
| 3706 | } |
| 3707 | } |
| 3708 | } |
| 3709 | break; |
| 3710 | |
| 3711 | case 16: |
| 3712 | /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must |
| 3713 | * still be done and, maybe, the alpha channel removed. This code also |
| 3714 | * handles the alpha-first option. |
| 3715 | */ |
| 3716 | { |
| 3717 | png_uint_16p first_row = png_voidcast(png_uint_16p, |
| 3718 | display->first_row); |
| 3719 | /* The division by two is safe because the caller passed in a |
| 3720 | * stride which was multiplied by 2 (below) to get row_bytes. |
| 3721 | */ |
| 3722 | ptrdiff_t step_row = display->row_bytes / 2; |
| 3723 | int preserve_alpha = (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; |
| 3724 | unsigned int outchannels = 1+preserve_alpha; |
| 3725 | int swap_alpha = 0; |
| 3726 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 3727 | # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3728 | if (preserve_alpha != 0 && |
| 3729 | (image->format & PNG_FORMAT_FLAG_AFIRST) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 3730 | swap_alpha = 1; |
| 3731 | # endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3732 | |
| 3733 | for (pass = 0; pass < passes; ++pass) |
| 3734 | { |
| 3735 | unsigned int startx, stepx, stepy; |
| 3736 | png_uint_32 y; |
| 3737 | |
| 3738 | /* The 'x' start and step are adjusted to output components here. |
| 3739 | */ |
| 3740 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
| 3741 | { |
| 3742 | /* The row may be empty for a short image: */ |
| 3743 | if (PNG_PASS_COLS(width, pass) == 0) |
| 3744 | continue; |
| 3745 | |
| 3746 | startx = PNG_PASS_START_COL(pass) * outchannels; |
| 3747 | stepx = PNG_PASS_COL_OFFSET(pass) * outchannels; |
| 3748 | y = PNG_PASS_START_ROW(pass); |
| 3749 | stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3750 | } |
| 3751 | |
| 3752 | else |
| 3753 | { |
| 3754 | y = 0; |
| 3755 | startx = 0; |
| 3756 | stepx = outchannels; |
| 3757 | stepy = 1; |
| 3758 | } |
| 3759 | |
| 3760 | for (; y<height; y += stepy) |
| 3761 | { |
| 3762 | png_const_uint_16p inrow; |
| 3763 | png_uint_16p outrow = first_row + y*step_row; |
| 3764 | png_uint_16p end_row = outrow + width * outchannels; |
| 3765 | |
| 3766 | /* Read the row, which is packed: */ |
| 3767 | png_read_row(png_ptr, png_voidcast(png_bytep, |
| 3768 | display->local_row), NULL); |
| 3769 | inrow = png_voidcast(png_const_uint_16p, display->local_row); |
| 3770 | |
| 3771 | /* Now do the pre-multiplication on each pixel in this row. |
| 3772 | */ |
| 3773 | outrow += startx; |
| 3774 | for (; outrow < end_row; outrow += stepx) |
| 3775 | { |
| 3776 | png_uint_32 component = inrow[0]; |
| 3777 | png_uint_16 alpha = inrow[1]; |
| 3778 | |
| 3779 | if (alpha > 0) /* else 0 */ |
| 3780 | { |
| 3781 | if (alpha < 65535) /* else just use component */ |
| 3782 | { |
| 3783 | component *= alpha; |
| 3784 | component += 32767; |
| 3785 | component /= 65535; |
| 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | else |
| 3790 | component = 0; |
| 3791 | |
| 3792 | outrow[swap_alpha] = (png_uint_16)component; |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3793 | if (preserve_alpha != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3794 | outrow[1 ^ swap_alpha] = alpha; |
| 3795 | |
| 3796 | inrow += 2; /* components and alpha channel */ |
| 3797 | } |
| 3798 | } |
| 3799 | } |
| 3800 | } |
| 3801 | break; |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3802 | |
| 3803 | #ifdef __GNUC__ |
| 3804 | default: |
| 3805 | png_error(png_ptr, "unexpected bit depth"); |
| 3806 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | return 1; |
| 3810 | } |
| 3811 | |
| 3812 | /* The guts of png_image_finish_read as a png_safe_execute callback. */ |
| 3813 | static int |
| 3814 | png_image_read_direct(png_voidp argument) |
| 3815 | { |
| 3816 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3817 | argument); |
| 3818 | png_imagep image = display->image; |
| 3819 | png_structrp png_ptr = image->opaque->png_ptr; |
| 3820 | png_inforp info_ptr = image->opaque->info_ptr; |
| 3821 | |
| 3822 | png_uint_32 format = image->format; |
| 3823 | int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0; |
| 3824 | int do_local_compose = 0; |
| 3825 | int do_local_background = 0; /* to avoid double gamma correction bug */ |
| 3826 | int passes = 0; |
| 3827 | |
| 3828 | /* Add transforms to ensure the correct output format is produced then check |
| 3829 | * that the required implementation support is there. Always expand; always |
| 3830 | * need 8 bits minimum, no palette and expanded tRNS. |
| 3831 | */ |
| 3832 | png_set_expand(png_ptr); |
| 3833 | |
| 3834 | /* Now check the format to see if it was modified. */ |
| 3835 | { |
| 3836 | png_uint_32 base_format = png_image_format(png_ptr) & |
| 3837 | ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */; |
| 3838 | png_uint_32 change = format ^ base_format; |
| 3839 | png_fixed_point output_gamma; |
| 3840 | int mode; /* alpha mode */ |
| 3841 | |
| 3842 | /* Do this first so that we have a record if rgb to gray is happening. */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3843 | if ((change & PNG_FORMAT_FLAG_COLOR) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3844 | { |
| 3845 | /* gray<->color transformation required. */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3846 | if ((format & PNG_FORMAT_FLAG_COLOR) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3847 | png_set_gray_to_rgb(png_ptr); |
| 3848 | |
| 3849 | else |
| 3850 | { |
| 3851 | /* libpng can't do both rgb to gray and |
| 3852 | * background/pre-multiplication if there is also significant gamma |
| 3853 | * correction, because both operations require linear colors and |
| 3854 | * the code only supports one transform doing the gamma correction. |
| 3855 | * Handle this by doing the pre-multiplication or background |
| 3856 | * operation in this code, if necessary. |
| 3857 | * |
| 3858 | * TODO: fix this by rewriting pngrtran.c (!) |
| 3859 | * |
| 3860 | * For the moment (given that fixing this in pngrtran.c is an |
| 3861 | * enormous change) 'do_local_background' is used to indicate that |
| 3862 | * the problem exists. |
| 3863 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3864 | if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3865 | do_local_background = 1/*maybe*/; |
| 3866 | |
| 3867 | png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, |
| 3868 | PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT); |
| 3869 | } |
| 3870 | |
| 3871 | change &= ~PNG_FORMAT_FLAG_COLOR; |
| 3872 | } |
| 3873 | |
| 3874 | /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise. |
| 3875 | */ |
| 3876 | { |
| 3877 | png_fixed_point input_gamma_default; |
| 3878 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3879 | if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 && |
| 3880 | (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3881 | input_gamma_default = PNG_GAMMA_LINEAR; |
| 3882 | else |
| 3883 | input_gamma_default = PNG_DEFAULT_sRGB; |
| 3884 | |
| 3885 | /* Call png_set_alpha_mode to set the default for the input gamma; the |
| 3886 | * output gamma is set by a second call below. |
| 3887 | */ |
| 3888 | png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default); |
| 3889 | } |
| 3890 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3891 | if (linear != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3892 | { |
| 3893 | /* If there *is* an alpha channel in the input it must be multiplied |
| 3894 | * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG. |
| 3895 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3896 | if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3897 | mode = PNG_ALPHA_STANDARD; /* associated alpha */ |
| 3898 | |
| 3899 | else |
| 3900 | mode = PNG_ALPHA_PNG; |
| 3901 | |
| 3902 | output_gamma = PNG_GAMMA_LINEAR; |
| 3903 | } |
| 3904 | |
| 3905 | else |
| 3906 | { |
| 3907 | mode = PNG_ALPHA_PNG; |
| 3908 | output_gamma = PNG_DEFAULT_sRGB; |
| 3909 | } |
| 3910 | |
| 3911 | /* If 'do_local_background' is set check for the presence of gamma |
| 3912 | * correction; this is part of the work-round for the libpng bug |
| 3913 | * described above. |
| 3914 | * |
| 3915 | * TODO: fix libpng and remove this. |
| 3916 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3917 | if (do_local_background != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3918 | { |
| 3919 | png_fixed_point gtest; |
| 3920 | |
| 3921 | /* This is 'png_gamma_threshold' from pngrtran.c; the test used for |
| 3922 | * gamma correction, the screen gamma hasn't been set on png_struct |
| 3923 | * yet; it's set below. png_struct::gamma, however, is set to the |
| 3924 | * final value. |
| 3925 | */ |
| 3926 | if (png_muldiv(>est, output_gamma, png_ptr->colorspace.gamma, |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3927 | PNG_FP_1) != 0 && png_gamma_significant(gtest) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3928 | do_local_background = 0; |
| 3929 | |
| 3930 | else if (mode == PNG_ALPHA_STANDARD) |
| 3931 | { |
| 3932 | do_local_background = 2/*required*/; |
| 3933 | mode = PNG_ALPHA_PNG; /* prevent libpng doing it */ |
| 3934 | } |
| 3935 | |
| 3936 | /* else leave as 1 for the checks below */ |
| 3937 | } |
| 3938 | |
| 3939 | /* If the bit-depth changes then handle that here. */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3940 | if ((change & PNG_FORMAT_FLAG_LINEAR) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3941 | { |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3942 | if (linear != 0 /*16-bit output*/) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3943 | png_set_expand_16(png_ptr); |
| 3944 | |
| 3945 | else /* 8-bit output */ |
| 3946 | png_set_scale_16(png_ptr); |
| 3947 | |
| 3948 | change &= ~PNG_FORMAT_FLAG_LINEAR; |
| 3949 | } |
| 3950 | |
| 3951 | /* Now the background/alpha channel changes. */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3952 | if ((change & PNG_FORMAT_FLAG_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3953 | { |
| 3954 | /* Removing an alpha channel requires composition for the 8-bit |
| 3955 | * formats; for the 16-bit it is already done, above, by the |
| 3956 | * pre-multiplication and the channel just needs to be stripped. |
| 3957 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3958 | if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3959 | { |
| 3960 | /* If RGB->gray is happening the alpha channel must be left and the |
| 3961 | * operation completed locally. |
| 3962 | * |
| 3963 | * TODO: fix libpng and remove this. |
| 3964 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3965 | if (do_local_background != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3966 | do_local_background = 2/*required*/; |
| 3967 | |
| 3968 | /* 16-bit output: just remove the channel */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 3969 | else if (linear != 0) /* compose on black (well, pre-multiply) */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3970 | png_set_strip_alpha(png_ptr); |
| 3971 | |
| 3972 | /* 8-bit output: do an appropriate compose */ |
| 3973 | else if (display->background != NULL) |
| 3974 | { |
| 3975 | png_color_16 c; |
| 3976 | |
| 3977 | c.index = 0; /*unused*/ |
| 3978 | c.red = display->background->red; |
| 3979 | c.green = display->background->green; |
| 3980 | c.blue = display->background->blue; |
| 3981 | c.gray = display->background->green; |
| 3982 | |
| 3983 | /* This is always an 8-bit sRGB value, using the 'green' channel |
| 3984 | * for gray is much better than calculating the luminance here; |
| 3985 | * we can get off-by-one errors in that calculation relative to |
| 3986 | * the app expectations and that will show up in transparent |
| 3987 | * pixels. |
| 3988 | */ |
| 3989 | png_set_background_fixed(png_ptr, &c, |
| 3990 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
| 3991 | 0/*gamma: not used*/); |
| 3992 | } |
| 3993 | |
| 3994 | else /* compose on row: implemented below. */ |
| 3995 | { |
| 3996 | do_local_compose = 1; |
| 3997 | /* This leaves the alpha channel in the output, so it has to be |
| 3998 | * removed by the code below. Set the encoding to the 'OPTIMIZE' |
| 3999 | * one so the code only has to hack on the pixels that require |
| 4000 | * composition. |
| 4001 | */ |
| 4002 | mode = PNG_ALPHA_OPTIMIZED; |
| 4003 | } |
| 4004 | } |
| 4005 | |
| 4006 | else /* output needs an alpha channel */ |
| 4007 | { |
| 4008 | /* This is tricky because it happens before the swap operation has |
| 4009 | * been accomplished; however, the swap does *not* swap the added |
| 4010 | * alpha channel (weird API), so it must be added in the correct |
| 4011 | * place. |
| 4012 | */ |
| 4013 | png_uint_32 filler; /* opaque filler */ |
| 4014 | int where; |
| 4015 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4016 | if (linear != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4017 | filler = 65535; |
| 4018 | |
| 4019 | else |
| 4020 | filler = 255; |
| 4021 | |
| 4022 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4023 | if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4024 | { |
| 4025 | where = PNG_FILLER_BEFORE; |
| 4026 | change &= ~PNG_FORMAT_FLAG_AFIRST; |
| 4027 | } |
| 4028 | |
| 4029 | else |
| 4030 | # endif |
| 4031 | where = PNG_FILLER_AFTER; |
| 4032 | |
| 4033 | png_set_add_alpha(png_ptr, filler, where); |
| 4034 | } |
| 4035 | |
| 4036 | /* This stops the (irrelevant) call to swap_alpha below. */ |
| 4037 | change &= ~PNG_FORMAT_FLAG_ALPHA; |
| 4038 | } |
| 4039 | |
| 4040 | /* Now set the alpha mode correctly; this is always done, even if there is |
| 4041 | * no alpha channel in either the input or the output because it correctly |
| 4042 | * sets the output gamma. |
| 4043 | */ |
| 4044 | png_set_alpha_mode_fixed(png_ptr, mode, output_gamma); |
| 4045 | |
| 4046 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4047 | if ((change & PNG_FORMAT_FLAG_BGR) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4048 | { |
| 4049 | /* Check only the output format; PNG is never BGR; don't do this if |
| 4050 | * the output is gray, but fix up the 'format' value in that case. |
| 4051 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4052 | if ((format & PNG_FORMAT_FLAG_COLOR) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4053 | png_set_bgr(png_ptr); |
| 4054 | |
| 4055 | else |
| 4056 | format &= ~PNG_FORMAT_FLAG_BGR; |
| 4057 | |
| 4058 | change &= ~PNG_FORMAT_FLAG_BGR; |
| 4059 | } |
| 4060 | # endif |
| 4061 | |
| 4062 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4063 | if ((change & PNG_FORMAT_FLAG_AFIRST) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4064 | { |
| 4065 | /* Only relevant if there is an alpha channel - it's particularly |
| 4066 | * important to handle this correctly because do_local_compose may |
| 4067 | * be set above and then libpng will keep the alpha channel for this |
| 4068 | * code to remove. |
| 4069 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4070 | if ((format & PNG_FORMAT_FLAG_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4071 | { |
| 4072 | /* Disable this if doing a local background, |
| 4073 | * TODO: remove this when local background is no longer required. |
| 4074 | */ |
| 4075 | if (do_local_background != 2) |
| 4076 | png_set_swap_alpha(png_ptr); |
| 4077 | } |
| 4078 | |
| 4079 | else |
| 4080 | format &= ~PNG_FORMAT_FLAG_AFIRST; |
| 4081 | |
| 4082 | change &= ~PNG_FORMAT_FLAG_AFIRST; |
| 4083 | } |
| 4084 | # endif |
| 4085 | |
| 4086 | /* If the *output* is 16-bit then we need to check for a byte-swap on this |
| 4087 | * architecture. |
| 4088 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4089 | if (linear != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4090 | { |
| 4091 | PNG_CONST png_uint_16 le = 0x0001; |
| 4092 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4093 | if ((*(png_const_bytep) & le) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4094 | png_set_swap(png_ptr); |
| 4095 | } |
| 4096 | |
| 4097 | /* If change is not now 0 some transformation is missing - error out. */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4098 | if (change != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4099 | png_error(png_ptr, "png_read_image: unsupported transformation"); |
| 4100 | } |
| 4101 | |
| 4102 | PNG_SKIP_CHUNKS(png_ptr); |
| 4103 | |
| 4104 | /* Update the 'info' structure and make sure the result is as required; first |
| 4105 | * make sure to turn on the interlace handling if it will be required |
| 4106 | * (because it can't be turned on *after* the call to png_read_update_info!) |
| 4107 | * |
| 4108 | * TODO: remove the do_local_background fixup below. |
| 4109 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4110 | if (do_local_compose == 0 && do_local_background != 2) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4111 | passes = png_set_interlace_handling(png_ptr); |
| 4112 | |
| 4113 | png_read_update_info(png_ptr, info_ptr); |
| 4114 | |
| 4115 | { |
| 4116 | png_uint_32 info_format = 0; |
| 4117 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4118 | if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4119 | info_format |= PNG_FORMAT_FLAG_COLOR; |
| 4120 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4121 | if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4122 | { |
| 4123 | /* do_local_compose removes this channel below. */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4124 | if (do_local_compose == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4125 | { |
| 4126 | /* do_local_background does the same if required. */ |
| 4127 | if (do_local_background != 2 || |
| 4128 | (format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 4129 | info_format |= PNG_FORMAT_FLAG_ALPHA; |
| 4130 | } |
| 4131 | } |
| 4132 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4133 | else if (do_local_compose != 0) /* internal error */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4134 | png_error(png_ptr, "png_image_read: alpha channel lost"); |
| 4135 | |
| 4136 | if (info_ptr->bit_depth == 16) |
| 4137 | info_format |= PNG_FORMAT_FLAG_LINEAR; |
| 4138 | |
| 4139 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4140 | if ((png_ptr->transformations & PNG_BGR) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4141 | info_format |= PNG_FORMAT_FLAG_BGR; |
| 4142 | # endif |
| 4143 | |
| 4144 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
| 4145 | if (do_local_background == 2) |
| 4146 | { |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4147 | if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4148 | info_format |= PNG_FORMAT_FLAG_AFIRST; |
| 4149 | } |
| 4150 | |
| 4151 | if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 || |
| 4152 | ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 && |
| 4153 | (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0)) |
| 4154 | { |
| 4155 | if (do_local_background == 2) |
| 4156 | png_error(png_ptr, "unexpected alpha swap transformation"); |
| 4157 | |
| 4158 | info_format |= PNG_FORMAT_FLAG_AFIRST; |
| 4159 | } |
| 4160 | # endif |
| 4161 | |
| 4162 | /* This is actually an internal error. */ |
| 4163 | if (info_format != format) |
| 4164 | png_error(png_ptr, "png_read_image: invalid transformations"); |
| 4165 | } |
| 4166 | |
| 4167 | /* Now read the rows. If do_local_compose is set then it is necessary to use |
| 4168 | * a local row buffer. The output will be GA, RGBA or BGRA and must be |
| 4169 | * converted to G, RGB or BGR as appropriate. The 'local_row' member of the |
| 4170 | * display acts as a flag. |
| 4171 | */ |
| 4172 | { |
| 4173 | png_voidp first_row = display->buffer; |
| 4174 | ptrdiff_t row_bytes = display->row_stride; |
| 4175 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4176 | if (linear != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4177 | row_bytes *= 2; |
| 4178 | |
| 4179 | /* The following expression is designed to work correctly whether it gives |
| 4180 | * a signed or an unsigned result. |
| 4181 | */ |
| 4182 | if (row_bytes < 0) |
| 4183 | { |
| 4184 | char *ptr = png_voidcast(char*, first_row); |
| 4185 | ptr += (image->height-1) * (-row_bytes); |
| 4186 | first_row = png_voidcast(png_voidp, ptr); |
| 4187 | } |
| 4188 | |
| 4189 | display->first_row = first_row; |
| 4190 | display->row_bytes = row_bytes; |
| 4191 | } |
| 4192 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4193 | if (do_local_compose != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4194 | { |
| 4195 | int result; |
| 4196 | png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); |
| 4197 | |
| 4198 | display->local_row = row; |
| 4199 | result = png_safe_execute(image, png_image_read_composite, display); |
| 4200 | display->local_row = NULL; |
| 4201 | png_free(png_ptr, row); |
| 4202 | |
| 4203 | return result; |
| 4204 | } |
| 4205 | |
| 4206 | else if (do_local_background == 2) |
| 4207 | { |
| 4208 | int result; |
| 4209 | png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); |
| 4210 | |
| 4211 | display->local_row = row; |
| 4212 | result = png_safe_execute(image, png_image_read_background, display); |
| 4213 | display->local_row = NULL; |
| 4214 | png_free(png_ptr, row); |
| 4215 | |
| 4216 | return result; |
| 4217 | } |
| 4218 | |
| 4219 | else |
| 4220 | { |
| 4221 | png_alloc_size_t row_bytes = display->row_bytes; |
| 4222 | |
| 4223 | while (--passes >= 0) |
| 4224 | { |
| 4225 | png_uint_32 y = image->height; |
| 4226 | png_bytep row = png_voidcast(png_bytep, display->first_row); |
| 4227 | |
| 4228 | while (y-- > 0) |
| 4229 | { |
| 4230 | png_read_row(png_ptr, row, NULL); |
| 4231 | row += row_bytes; |
| 4232 | } |
| 4233 | } |
| 4234 | |
| 4235 | return 1; |
| 4236 | } |
| 4237 | } |
| 4238 | |
| 4239 | int PNGAPI |
| 4240 | png_image_finish_read(png_imagep image, png_const_colorp background, |
| 4241 | void *buffer, png_int_32 row_stride, void *colormap) |
| 4242 | { |
| 4243 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
| 4244 | { |
| 4245 | png_uint_32 check; |
| 4246 | |
| 4247 | if (row_stride == 0) |
| 4248 | row_stride = PNG_IMAGE_ROW_STRIDE(*image); |
| 4249 | |
| 4250 | if (row_stride < 0) |
| 4251 | check = -row_stride; |
| 4252 | |
| 4253 | else |
| 4254 | check = row_stride; |
| 4255 | |
| 4256 | if (image->opaque != NULL && buffer != NULL && |
| 4257 | check >= PNG_IMAGE_ROW_STRIDE(*image)) |
| 4258 | { |
| 4259 | if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 || |
| 4260 | (image->colormap_entries > 0 && colormap != NULL)) |
| 4261 | { |
| 4262 | int result; |
| 4263 | png_image_read_control display; |
| 4264 | |
| 4265 | memset(&display, 0, (sizeof display)); |
| 4266 | display.image = image; |
| 4267 | display.buffer = buffer; |
| 4268 | display.row_stride = row_stride; |
| 4269 | display.colormap = colormap; |
| 4270 | display.background = background; |
| 4271 | display.local_row = NULL; |
| 4272 | |
| 4273 | /* Choose the correct 'end' routine; for the color-map case all the |
| 4274 | * setup has already been done. |
| 4275 | */ |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4276 | if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4277 | result = |
| 4278 | png_safe_execute(image, png_image_read_colormap, &display) && |
| 4279 | png_safe_execute(image, png_image_read_colormapped, &display); |
| 4280 | |
| 4281 | else |
| 4282 | result = |
| 4283 | png_safe_execute(image, png_image_read_direct, &display); |
| 4284 | |
| 4285 | png_image_free(image); |
| 4286 | return result; |
| 4287 | } |
| 4288 | |
| 4289 | else |
| 4290 | return png_image_error(image, |
| 4291 | "png_image_finish_read[color-map]: no color-map"); |
| 4292 | } |
| 4293 | |
| 4294 | else |
| 4295 | return png_image_error(image, |
| 4296 | "png_image_finish_read: invalid argument"); |
| 4297 | } |
| 4298 | |
| 4299 | else if (image != NULL) |
| 4300 | return png_image_error(image, |
| 4301 | "png_image_finish_read: damaged PNG_IMAGE_VERSION"); |
| 4302 | |
| 4303 | return 0; |
| 4304 | } |
| 4305 | |
Matt Sarett | 96be908 | 2016-05-03 13:29:54 -0400 | [diff] [blame^] | 4306 | #endif /* SIMPLIFIED_READ */ |
| 4307 | #endif /* READ */ |