The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1 | |
| 2 | /* pngrtran.c - transforms the data in a row for PNG readers |
| 3 | * |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 4 | * Last changed in libpng 1.6.33 [September 28, 2017] |
| 5 | * Copyright (c) 1998-2002,2004,2006-2017 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 functions optionally called by an application |
| 14 | * in order to tell libpng how to handle data when reading a PNG. |
| 15 | * Transformations that are used in both reading and writing are |
| 16 | * in pngtrans.c. |
| 17 | */ |
| 18 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 19 | #include "pngpriv.h" |
| 20 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 21 | #ifdef PNG_READ_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 22 | |
| 23 | /* Set the action on getting a CRC error for an ancillary or critical chunk. */ |
| 24 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 25 | png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 26 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 27 | png_debug(1, "in png_set_crc_action"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 28 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 29 | if (png_ptr == NULL) |
| 30 | return; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 31 | |
| 32 | /* Tell libpng how we react to CRC errors in critical chunks */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 33 | switch (crit_action) |
| 34 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 35 | case PNG_CRC_NO_CHANGE: /* Leave setting as is */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 36 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 37 | |
| 38 | case PNG_CRC_WARN_USE: /* Warn/use data */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 39 | png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; |
| 40 | png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE; |
| 41 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 42 | |
| 43 | case PNG_CRC_QUIET_USE: /* Quiet/use data */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 44 | png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; |
| 45 | png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE | |
| 46 | PNG_FLAG_CRC_CRITICAL_IGNORE; |
| 47 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 48 | |
| 49 | case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */ |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 50 | png_warning(png_ptr, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 51 | "Can't discard critical data on CRC error"); |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 52 | /* FALLTHROUGH */ |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 53 | case PNG_CRC_ERROR_QUIT: /* Error/quit */ |
| 54 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 55 | case PNG_CRC_DEFAULT: |
| 56 | default: |
| 57 | png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; |
| 58 | break; |
| 59 | } |
| 60 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 61 | /* Tell libpng how we react to CRC errors in ancillary chunks */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 62 | switch (ancil_action) |
| 63 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 64 | case PNG_CRC_NO_CHANGE: /* Leave setting as is */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 65 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 66 | |
| 67 | case PNG_CRC_WARN_USE: /* Warn/use data */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 68 | png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
| 69 | png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE; |
| 70 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 71 | |
| 72 | case PNG_CRC_QUIET_USE: /* Quiet/use data */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 73 | png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
| 74 | png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE | |
| 75 | PNG_FLAG_CRC_ANCILLARY_NOWARN; |
| 76 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 77 | |
| 78 | case PNG_CRC_ERROR_QUIT: /* Error/quit */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 79 | png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
| 80 | png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN; |
| 81 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 82 | |
| 83 | case PNG_CRC_WARN_DISCARD: /* Warn/discard data */ |
| 84 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 85 | case PNG_CRC_DEFAULT: |
| 86 | default: |
| 87 | png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 92 | #ifdef PNG_READ_TRANSFORMS_SUPPORTED |
| 93 | /* Is it OK to set a transformation now? Only if png_start_read_image or |
| 94 | * png_read_update_info have not been called. It is not necessary for the IHDR |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 95 | * to have been read in all cases; the need_IHDR parameter allows for this |
| 96 | * check too. |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 97 | */ |
| 98 | static int |
| 99 | png_rtran_ok(png_structrp png_ptr, int need_IHDR) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 100 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 101 | if (png_ptr != NULL) |
| 102 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 103 | if ((png_ptr->flags & PNG_FLAG_ROW_INIT) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 104 | png_app_error(png_ptr, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 105 | "invalid after png_start_read_image or png_read_update_info"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 106 | |
| 107 | else if (need_IHDR && (png_ptr->mode & PNG_HAVE_IHDR) == 0) |
| 108 | png_app_error(png_ptr, "invalid before the PNG header has been read"); |
| 109 | |
| 110 | else |
| 111 | { |
| 112 | /* Turn on failure to initialize correctly for all transforms. */ |
| 113 | png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED; |
| 114 | |
| 115 | return 1; /* Ok */ |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return 0; /* no png_error possible! */ |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | #ifdef PNG_READ_BACKGROUND_SUPPORTED |
| 124 | /* Handle alpha and tRNS via a background color */ |
| 125 | void PNGFAPI |
| 126 | png_set_background_fixed(png_structrp png_ptr, |
| 127 | png_const_color_16p background_color, int background_gamma_code, |
| 128 | int need_expand, png_fixed_point background_gamma) |
| 129 | { |
| 130 | png_debug(1, "in png_set_background_fixed"); |
| 131 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 132 | if (png_rtran_ok(png_ptr, 0) == 0 || background_color == NULL) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 133 | return; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 134 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 135 | if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) |
| 136 | { |
| 137 | png_warning(png_ptr, "Application must supply a known background gamma"); |
| 138 | return; |
| 139 | } |
| 140 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 141 | png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA; |
| 142 | png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 143 | png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 144 | |
| 145 | png_ptr->background = *background_color; |
| 146 | png_ptr->background_gamma = background_gamma; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 147 | png_ptr->background_gamma_type = (png_byte)(background_gamma_code); |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 148 | if (need_expand != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 149 | png_ptr->transformations |= PNG_BACKGROUND_EXPAND; |
| 150 | else |
| 151 | png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; |
| 152 | } |
| 153 | |
| 154 | # ifdef PNG_FLOATING_POINT_SUPPORTED |
| 155 | void PNGAPI |
| 156 | png_set_background(png_structrp png_ptr, |
| 157 | png_const_color_16p background_color, int background_gamma_code, |
| 158 | int need_expand, double background_gamma) |
| 159 | { |
| 160 | png_set_background_fixed(png_ptr, background_color, background_gamma_code, |
| 161 | need_expand, png_fixed(png_ptr, background_gamma, "png_set_background")); |
| 162 | } |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 163 | # endif /* FLOATING_POINT */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 164 | #endif /* READ_BACKGROUND */ |
| 165 | |
| 166 | /* Scale 16-bit depth files to 8-bit depth. If both of these are set then the |
| 167 | * one that pngrtran does first (scale) happens. This is necessary to allow the |
| 168 | * TRANSFORM and API behavior to be somewhat consistent, and it's simpler. |
| 169 | */ |
| 170 | #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
| 171 | void PNGAPI |
| 172 | png_set_scale_16(png_structrp png_ptr) |
| 173 | { |
| 174 | png_debug(1, "in png_set_scale_16"); |
| 175 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 176 | if (png_rtran_ok(png_ptr, 0) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 177 | return; |
| 178 | |
| 179 | png_ptr->transformations |= PNG_SCALE_16_TO_8; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 180 | } |
| 181 | #endif |
| 182 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 183 | #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
| 184 | /* Chop 16-bit depth files to 8-bit depth */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 185 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 186 | png_set_strip_16(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 187 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 188 | png_debug(1, "in png_set_strip_16"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 189 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 190 | if (png_rtran_ok(png_ptr, 0) == 0) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 191 | return; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 192 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 193 | png_ptr->transformations |= PNG_16_TO_8; |
| 194 | } |
| 195 | #endif |
| 196 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 197 | #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 198 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 199 | png_set_strip_alpha(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 200 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 201 | png_debug(1, "in png_set_strip_alpha"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 202 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 203 | if (png_rtran_ok(png_ptr, 0) == 0) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 204 | return; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 205 | |
| 206 | png_ptr->transformations |= PNG_STRIP_ALPHA; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 207 | } |
| 208 | #endif |
| 209 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 210 | #if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED) |
| 211 | static png_fixed_point |
| 212 | translate_gamma_flags(png_structrp png_ptr, png_fixed_point output_gamma, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 213 | int is_screen) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 214 | { |
| 215 | /* Check for flag values. The main reason for having the old Mac value as a |
| 216 | * flag is that it is pretty near impossible to work out what the correct |
| 217 | * value is from Apple documentation - a working Mac system is needed to |
| 218 | * discover the value! |
| 219 | */ |
| 220 | if (output_gamma == PNG_DEFAULT_sRGB || |
| 221 | output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB) |
| 222 | { |
| 223 | /* If there is no sRGB support this just sets the gamma to the standard |
| 224 | * sRGB value. (This is a side effect of using this function!) |
| 225 | */ |
| 226 | # ifdef PNG_READ_sRGB_SUPPORTED |
| 227 | png_ptr->flags |= PNG_FLAG_ASSUME_sRGB; |
| 228 | # else |
| 229 | PNG_UNUSED(png_ptr) |
| 230 | # endif |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 231 | if (is_screen != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 232 | output_gamma = PNG_GAMMA_sRGB; |
| 233 | else |
| 234 | output_gamma = PNG_GAMMA_sRGB_INVERSE; |
| 235 | } |
| 236 | |
| 237 | else if (output_gamma == PNG_GAMMA_MAC_18 || |
| 238 | output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18) |
| 239 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 240 | if (is_screen != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 241 | output_gamma = PNG_GAMMA_MAC_OLD; |
| 242 | else |
| 243 | output_gamma = PNG_GAMMA_MAC_INVERSE; |
| 244 | } |
| 245 | |
| 246 | return output_gamma; |
| 247 | } |
| 248 | |
| 249 | # ifdef PNG_FLOATING_POINT_SUPPORTED |
| 250 | static png_fixed_point |
| 251 | convert_gamma_value(png_structrp png_ptr, double output_gamma) |
| 252 | { |
| 253 | /* The following silently ignores cases where fixed point (times 100,000) |
| 254 | * gamma values are passed to the floating point API. This is safe and it |
| 255 | * means the fixed point constants work just fine with the floating point |
| 256 | * API. The alternative would just lead to undetected errors and spurious |
| 257 | * bug reports. Negative values fail inside the _fixed API unless they |
| 258 | * correspond to the flag values. |
| 259 | */ |
| 260 | if (output_gamma > 0 && output_gamma < 128) |
| 261 | output_gamma *= PNG_FP_1; |
| 262 | |
| 263 | /* This preserves -1 and -2 exactly: */ |
| 264 | output_gamma = floor(output_gamma + .5); |
| 265 | |
| 266 | if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN) |
| 267 | png_fixed_error(png_ptr, "gamma value"); |
| 268 | |
| 269 | return (png_fixed_point)output_gamma; |
| 270 | } |
| 271 | # endif |
| 272 | #endif /* READ_ALPHA_MODE || READ_GAMMA */ |
| 273 | |
| 274 | #ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
| 275 | void PNGFAPI |
| 276 | png_set_alpha_mode_fixed(png_structrp png_ptr, int mode, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 277 | png_fixed_point output_gamma) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 278 | { |
| 279 | int compose = 0; |
| 280 | png_fixed_point file_gamma; |
| 281 | |
| 282 | png_debug(1, "in png_set_alpha_mode"); |
| 283 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 284 | if (png_rtran_ok(png_ptr, 0) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 285 | return; |
| 286 | |
| 287 | output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/); |
| 288 | |
| 289 | /* Validate the value to ensure it is in a reasonable range. The value |
| 290 | * is expected to be 1 or greater, but this range test allows for some |
| 291 | * viewing correction values. The intent is to weed out users of this API |
| 292 | * who use the inverse of the gamma value accidentally! Since some of these |
Matt Sarett | 1146686 | 2016-02-19 13:41:30 -0500 | [diff] [blame] | 293 | * values are reasonable this may have to be changed: |
| 294 | * |
| 295 | * 1.6.x: changed from 0.07..3 to 0.01..100 (to accomodate the optimal 16-bit |
| 296 | * gamma of 36, and its reciprocal.) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 297 | */ |
Matt Sarett | 1146686 | 2016-02-19 13:41:30 -0500 | [diff] [blame] | 298 | if (output_gamma < 1000 || output_gamma > 10000000) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 299 | png_error(png_ptr, "output gamma out of expected range"); |
| 300 | |
| 301 | /* The default file gamma is the inverse of the output gamma; the output |
| 302 | * gamma may be changed below so get the file value first: |
| 303 | */ |
| 304 | file_gamma = png_reciprocal(output_gamma); |
| 305 | |
| 306 | /* There are really 8 possibilities here, composed of any combination |
| 307 | * of: |
| 308 | * |
| 309 | * premultiply the color channels |
| 310 | * do not encode non-opaque pixels |
| 311 | * encode the alpha as well as the color channels |
| 312 | * |
| 313 | * The differences disappear if the input/output ('screen') gamma is 1.0, |
| 314 | * because then the encoding is a no-op and there is only the choice of |
| 315 | * premultiplying the color channels or not. |
| 316 | * |
| 317 | * png_set_alpha_mode and png_set_background interact because both use |
| 318 | * png_compose to do the work. Calling both is only useful when |
| 319 | * png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along |
| 320 | * with a default gamma value. Otherwise PNG_COMPOSE must not be set. |
| 321 | */ |
| 322 | switch (mode) |
| 323 | { |
| 324 | case PNG_ALPHA_PNG: /* default: png standard */ |
| 325 | /* No compose, but it may be set by png_set_background! */ |
| 326 | png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 327 | png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 328 | break; |
| 329 | |
| 330 | case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */ |
| 331 | compose = 1; |
| 332 | png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 333 | png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 334 | /* The output is linear: */ |
| 335 | output_gamma = PNG_FP_1; |
| 336 | break; |
| 337 | |
| 338 | case PNG_ALPHA_OPTIMIZED: /* associated, non-opaque pixels linear */ |
| 339 | compose = 1; |
| 340 | png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 341 | png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA; |
| 342 | /* output_gamma records the encoding of opaque pixels! */ |
| 343 | break; |
| 344 | |
| 345 | case PNG_ALPHA_BROKEN: /* associated, non-linear, alpha encoded */ |
| 346 | compose = 1; |
| 347 | png_ptr->transformations |= PNG_ENCODE_ALPHA; |
| 348 | png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 349 | break; |
| 350 | |
| 351 | default: |
| 352 | png_error(png_ptr, "invalid alpha mode"); |
| 353 | } |
| 354 | |
| 355 | /* Only set the default gamma if the file gamma has not been set (this has |
| 356 | * the side effect that the gamma in a second call to png_set_alpha_mode will |
| 357 | * be ignored.) |
| 358 | */ |
| 359 | if (png_ptr->colorspace.gamma == 0) |
| 360 | { |
| 361 | png_ptr->colorspace.gamma = file_gamma; |
| 362 | png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; |
| 363 | } |
| 364 | |
| 365 | /* But always set the output gamma: */ |
| 366 | png_ptr->screen_gamma = output_gamma; |
| 367 | |
| 368 | /* Finally, if pre-multiplying, set the background fields to achieve the |
| 369 | * desired result. |
| 370 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 371 | if (compose != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 372 | { |
| 373 | /* And obtain alpha pre-multiplication by composing on black: */ |
| 374 | memset(&png_ptr->background, 0, (sizeof png_ptr->background)); |
| 375 | png_ptr->background_gamma = png_ptr->colorspace.gamma; /* just in case */ |
| 376 | png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE; |
| 377 | png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; |
| 378 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 379 | if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 380 | png_error(png_ptr, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 381 | "conflicting calls to set alpha mode and background"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 382 | |
| 383 | png_ptr->transformations |= PNG_COMPOSE; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | # ifdef PNG_FLOATING_POINT_SUPPORTED |
| 388 | void PNGAPI |
| 389 | png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma) |
| 390 | { |
| 391 | png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 392 | output_gamma)); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 393 | } |
| 394 | # endif |
| 395 | #endif |
| 396 | |
| 397 | #ifdef PNG_READ_QUANTIZE_SUPPORTED |
| 398 | /* Dither file to 8-bit. Supply a palette, the current number |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 399 | * of elements in the palette, the maximum number of elements |
| 400 | * allowed, and a histogram if possible. If the current number |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 401 | * of colors is greater than the maximum number, the palette will be |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 402 | * modified to fit in the maximum number. "full_quantize" indicates |
| 403 | * whether we need a quantizing cube set up for RGB images, or if we |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 404 | * simply are reducing the number of colors in a paletted image. |
| 405 | */ |
| 406 | |
| 407 | typedef struct png_dsort_struct |
| 408 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 409 | struct png_dsort_struct * next; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 410 | png_byte left; |
| 411 | png_byte right; |
| 412 | } png_dsort; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 413 | typedef png_dsort * png_dsortp; |
| 414 | typedef png_dsort * * png_dsortpp; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 415 | |
| 416 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 417 | png_set_quantize(png_structrp png_ptr, png_colorp palette, |
| 418 | int num_palette, int maximum_colors, png_const_uint_16p histogram, |
| 419 | int full_quantize) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 420 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 421 | png_debug(1, "in png_set_quantize"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 422 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 423 | if (png_rtran_ok(png_ptr, 0) == 0) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 424 | return; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 425 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 426 | png_ptr->transformations |= PNG_QUANTIZE; |
| 427 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 428 | if (full_quantize == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 429 | { |
| 430 | int i; |
| 431 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 432 | png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr, |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 433 | (png_alloc_size_t)((png_uint_32)num_palette * (sizeof (png_byte)))); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 434 | for (i = 0; i < num_palette; i++) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 435 | png_ptr->quantize_index[i] = (png_byte)i; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | if (num_palette > maximum_colors) |
| 439 | { |
| 440 | if (histogram != NULL) |
| 441 | { |
| 442 | /* This is easy enough, just throw out the least used colors. |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 443 | * Perhaps not the best solution, but good enough. |
| 444 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 445 | |
| 446 | int i; |
| 447 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 448 | /* Initialize an array to sort colors */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 449 | png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr, |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 450 | (png_alloc_size_t)((png_uint_32)num_palette * (sizeof (png_byte)))); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 451 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 452 | /* Initialize the quantize_sort array */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 453 | for (i = 0; i < num_palette; i++) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 454 | png_ptr->quantize_sort[i] = (png_byte)i; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 455 | |
| 456 | /* Find the least used palette entries by starting a |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 457 | * bubble sort, and running it until we have sorted |
| 458 | * out enough colors. Note that we don't care about |
| 459 | * sorting all the colors, just finding which are |
| 460 | * least used. |
| 461 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 462 | |
| 463 | for (i = num_palette - 1; i >= maximum_colors; i--) |
| 464 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 465 | int done; /* To stop early if the list is pre-sorted */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 466 | int j; |
| 467 | |
| 468 | done = 1; |
| 469 | for (j = 0; j < i; j++) |
| 470 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 471 | if (histogram[png_ptr->quantize_sort[j]] |
| 472 | < histogram[png_ptr->quantize_sort[j + 1]]) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 473 | { |
| 474 | png_byte t; |
| 475 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 476 | t = png_ptr->quantize_sort[j]; |
| 477 | png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1]; |
| 478 | png_ptr->quantize_sort[j + 1] = t; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 479 | done = 0; |
| 480 | } |
| 481 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 482 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 483 | if (done != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 484 | break; |
| 485 | } |
| 486 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 487 | /* Swap the palette around, and set up a table, if necessary */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 488 | if (full_quantize != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 489 | { |
| 490 | int j = num_palette; |
| 491 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 492 | /* Put all the useful colors within the max, but don't |
| 493 | * move the others. |
| 494 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 495 | for (i = 0; i < maximum_colors; i++) |
| 496 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 497 | if ((int)png_ptr->quantize_sort[i] >= maximum_colors) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 498 | { |
| 499 | do |
| 500 | j--; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 501 | while ((int)png_ptr->quantize_sort[j] >= maximum_colors); |
| 502 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 503 | palette[i] = palette[j]; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | else |
| 508 | { |
| 509 | int j = num_palette; |
| 510 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 511 | /* Move all the used colors inside the max limit, and |
| 512 | * develop a translation table. |
| 513 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 514 | for (i = 0; i < maximum_colors; i++) |
| 515 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 516 | /* Only move the colors we need to */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 517 | if ((int)png_ptr->quantize_sort[i] >= maximum_colors) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 518 | { |
| 519 | png_color tmp_color; |
| 520 | |
| 521 | do |
| 522 | j--; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 523 | while ((int)png_ptr->quantize_sort[j] >= maximum_colors); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 524 | |
| 525 | tmp_color = palette[j]; |
| 526 | palette[j] = palette[i]; |
| 527 | palette[i] = tmp_color; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 528 | /* Indicate where the color went */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 529 | png_ptr->quantize_index[j] = (png_byte)i; |
| 530 | png_ptr->quantize_index[i] = (png_byte)j; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 534 | /* Find closest color for those colors we are not using */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 535 | for (i = 0; i < num_palette; i++) |
| 536 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 537 | if ((int)png_ptr->quantize_index[i] >= maximum_colors) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 538 | { |
| 539 | int min_d, k, min_k, d_index; |
| 540 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 541 | /* Find the closest color to one we threw out */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 542 | d_index = png_ptr->quantize_index[i]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 543 | min_d = PNG_COLOR_DIST(palette[d_index], palette[0]); |
| 544 | for (k = 1, min_k = 0; k < maximum_colors; k++) |
| 545 | { |
| 546 | int d; |
| 547 | |
| 548 | d = PNG_COLOR_DIST(palette[d_index], palette[k]); |
| 549 | |
| 550 | if (d < min_d) |
| 551 | { |
| 552 | min_d = d; |
| 553 | min_k = k; |
| 554 | } |
| 555 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 556 | /* Point to closest color */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 557 | png_ptr->quantize_index[i] = (png_byte)min_k; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 561 | png_free(png_ptr, png_ptr->quantize_sort); |
| 562 | png_ptr->quantize_sort = NULL; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 563 | } |
| 564 | else |
| 565 | { |
| 566 | /* This is much harder to do simply (and quickly). Perhaps |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 567 | * we need to go through a median cut routine, but those |
| 568 | * don't always behave themselves with only a few colors |
| 569 | * as input. So we will just find the closest two colors, |
| 570 | * and throw out one of them (chosen somewhat randomly). |
| 571 | * [We don't understand this at all, so if someone wants to |
| 572 | * work on improving it, be our guest - AED, GRP] |
| 573 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 574 | int i; |
| 575 | int max_d; |
| 576 | int num_new_palette; |
| 577 | png_dsortp t; |
| 578 | png_dsortpp hash; |
| 579 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 580 | t = NULL; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 581 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 582 | /* Initialize palette index arrays */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 583 | png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr, |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 584 | (png_alloc_size_t)((png_uint_32)num_palette * |
| 585 | (sizeof (png_byte)))); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 586 | png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr, |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 587 | (png_alloc_size_t)((png_uint_32)num_palette * |
| 588 | (sizeof (png_byte)))); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 589 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 590 | /* Initialize the sort array */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 591 | for (i = 0; i < num_palette; i++) |
| 592 | { |
| 593 | png_ptr->index_to_palette[i] = (png_byte)i; |
| 594 | png_ptr->palette_to_index[i] = (png_byte)i; |
| 595 | } |
| 596 | |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 597 | hash = (png_dsortpp)png_calloc(png_ptr, (png_alloc_size_t)(769 * |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 598 | (sizeof (png_dsortp)))); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 599 | |
| 600 | num_new_palette = num_palette; |
| 601 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 602 | /* Initial wild guess at how far apart the farthest pixel |
| 603 | * pair we will be eliminating will be. Larger |
| 604 | * numbers mean more areas will be allocated, Smaller |
| 605 | * numbers run the risk of not saving enough data, and |
| 606 | * having to do this all over again. |
| 607 | * |
| 608 | * I have not done extensive checking on this number. |
| 609 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 610 | max_d = 96; |
| 611 | |
| 612 | while (num_new_palette > maximum_colors) |
| 613 | { |
| 614 | for (i = 0; i < num_new_palette - 1; i++) |
| 615 | { |
| 616 | int j; |
| 617 | |
| 618 | for (j = i + 1; j < num_new_palette; j++) |
| 619 | { |
| 620 | int d; |
| 621 | |
| 622 | d = PNG_COLOR_DIST(palette[i], palette[j]); |
| 623 | |
| 624 | if (d <= max_d) |
| 625 | { |
| 626 | |
| 627 | t = (png_dsortp)png_malloc_warn(png_ptr, |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 628 | (png_alloc_size_t)(sizeof (png_dsort))); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 629 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 630 | if (t == NULL) |
| 631 | break; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 632 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 633 | t->next = hash[d]; |
| 634 | t->left = (png_byte)i; |
| 635 | t->right = (png_byte)j; |
| 636 | hash[d] = t; |
| 637 | } |
| 638 | } |
| 639 | if (t == NULL) |
| 640 | break; |
| 641 | } |
| 642 | |
| 643 | if (t != NULL) |
| 644 | for (i = 0; i <= max_d; i++) |
| 645 | { |
| 646 | if (hash[i] != NULL) |
| 647 | { |
| 648 | png_dsortp p; |
| 649 | |
| 650 | for (p = hash[i]; p; p = p->next) |
| 651 | { |
| 652 | if ((int)png_ptr->index_to_palette[p->left] |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 653 | < num_new_palette && |
| 654 | (int)png_ptr->index_to_palette[p->right] |
| 655 | < num_new_palette) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 656 | { |
| 657 | int j, next_j; |
| 658 | |
| 659 | if (num_new_palette & 0x01) |
| 660 | { |
| 661 | j = p->left; |
| 662 | next_j = p->right; |
| 663 | } |
| 664 | else |
| 665 | { |
| 666 | j = p->right; |
| 667 | next_j = p->left; |
| 668 | } |
| 669 | |
| 670 | num_new_palette--; |
| 671 | palette[png_ptr->index_to_palette[j]] |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 672 | = palette[num_new_palette]; |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 673 | if (full_quantize == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 674 | { |
| 675 | int k; |
| 676 | |
| 677 | for (k = 0; k < num_palette; k++) |
| 678 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 679 | if (png_ptr->quantize_index[k] == |
| 680 | png_ptr->index_to_palette[j]) |
| 681 | png_ptr->quantize_index[k] = |
| 682 | png_ptr->index_to_palette[next_j]; |
| 683 | |
| 684 | if ((int)png_ptr->quantize_index[k] == |
| 685 | num_new_palette) |
| 686 | png_ptr->quantize_index[k] = |
| 687 | png_ptr->index_to_palette[j]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | png_ptr->index_to_palette[png_ptr->palette_to_index |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 692 | [num_new_palette]] = png_ptr->index_to_palette[j]; |
| 693 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 694 | png_ptr->palette_to_index[png_ptr->index_to_palette[j]] |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 695 | = png_ptr->palette_to_index[num_new_palette]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 696 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 697 | png_ptr->index_to_palette[j] = |
| 698 | (png_byte)num_new_palette; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 699 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 700 | png_ptr->palette_to_index[num_new_palette] = |
| 701 | (png_byte)j; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 702 | } |
| 703 | if (num_new_palette <= maximum_colors) |
| 704 | break; |
| 705 | } |
| 706 | if (num_new_palette <= maximum_colors) |
| 707 | break; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | for (i = 0; i < 769; i++) |
| 712 | { |
| 713 | if (hash[i] != NULL) |
| 714 | { |
| 715 | png_dsortp p = hash[i]; |
| 716 | while (p) |
| 717 | { |
| 718 | t = p->next; |
| 719 | png_free(png_ptr, p); |
| 720 | p = t; |
| 721 | } |
| 722 | } |
| 723 | hash[i] = 0; |
| 724 | } |
| 725 | max_d += 96; |
| 726 | } |
| 727 | png_free(png_ptr, hash); |
| 728 | png_free(png_ptr, png_ptr->palette_to_index); |
| 729 | png_free(png_ptr, png_ptr->index_to_palette); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 730 | png_ptr->palette_to_index = NULL; |
| 731 | png_ptr->index_to_palette = NULL; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 732 | } |
| 733 | num_palette = maximum_colors; |
| 734 | } |
| 735 | if (png_ptr->palette == NULL) |
| 736 | { |
| 737 | png_ptr->palette = palette; |
| 738 | } |
| 739 | png_ptr->num_palette = (png_uint_16)num_palette; |
| 740 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 741 | if (full_quantize != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 742 | { |
| 743 | int i; |
| 744 | png_bytep distance; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 745 | int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS + |
| 746 | PNG_QUANTIZE_BLUE_BITS; |
| 747 | int num_red = (1 << PNG_QUANTIZE_RED_BITS); |
| 748 | int num_green = (1 << PNG_QUANTIZE_GREEN_BITS); |
| 749 | int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 750 | png_size_t num_entries = ((png_size_t)1 << total_bits); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 751 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 752 | png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr, |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 753 | (png_alloc_size_t)(num_entries * (sizeof (png_byte)))); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 754 | |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 755 | distance = (png_bytep)png_malloc(png_ptr, (png_alloc_size_t)(num_entries * |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 756 | (sizeof (png_byte)))); |
| 757 | |
| 758 | memset(distance, 0xff, num_entries * (sizeof (png_byte))); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 759 | |
| 760 | for (i = 0; i < num_palette; i++) |
| 761 | { |
| 762 | int ir, ig, ib; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 763 | int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS)); |
| 764 | int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS)); |
| 765 | int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 766 | |
| 767 | for (ir = 0; ir < num_red; ir++) |
| 768 | { |
| 769 | /* int dr = abs(ir - r); */ |
| 770 | int dr = ((ir > r) ? ir - r : r - ir); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 771 | int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS + |
| 772 | PNG_QUANTIZE_GREEN_BITS)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 773 | |
| 774 | for (ig = 0; ig < num_green; ig++) |
| 775 | { |
| 776 | /* int dg = abs(ig - g); */ |
| 777 | int dg = ((ig > g) ? ig - g : g - ig); |
| 778 | int dt = dr + dg; |
| 779 | int dm = ((dr > dg) ? dr : dg); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 780 | int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 781 | |
| 782 | for (ib = 0; ib < num_blue; ib++) |
| 783 | { |
| 784 | int d_index = index_g | ib; |
| 785 | /* int db = abs(ib - b); */ |
| 786 | int db = ((ib > b) ? ib - b : b - ib); |
| 787 | int dmax = ((dm > db) ? dm : db); |
| 788 | int d = dmax + dt + db; |
| 789 | |
| 790 | if (d < (int)distance[d_index]) |
| 791 | { |
| 792 | distance[d_index] = (png_byte)d; |
| 793 | png_ptr->palette_lookup[d_index] = (png_byte)i; |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | png_free(png_ptr, distance); |
| 801 | } |
| 802 | } |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 803 | #endif /* READ_QUANTIZE */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 804 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 805 | #ifdef PNG_READ_GAMMA_SUPPORTED |
| 806 | void PNGFAPI |
| 807 | png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 808 | png_fixed_point file_gamma) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 809 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 810 | png_debug(1, "in png_set_gamma_fixed"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 811 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 812 | if (png_rtran_ok(png_ptr, 0) == 0) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 813 | return; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 814 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 815 | /* New in libpng-1.5.4 - reserve particular negative values as flags. */ |
| 816 | scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/); |
| 817 | file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/); |
| 818 | |
| 819 | /* Checking the gamma values for being >0 was added in 1.5.4 along with the |
| 820 | * premultiplied alpha support; this actually hides an undocumented feature |
| 821 | * of the previous implementation which allowed gamma processing to be |
| 822 | * disabled in background handling. There is no evidence (so far) that this |
| 823 | * was being used; however, png_set_background itself accepted and must still |
| 824 | * accept '0' for the gamma value it takes, because it isn't always used. |
| 825 | * |
| 826 | * Since this is an API change (albeit a very minor one that removes an |
| 827 | * undocumented API feature) the following checks were only enabled in |
| 828 | * libpng-1.6.0. |
| 829 | */ |
| 830 | if (file_gamma <= 0) |
| 831 | png_error(png_ptr, "invalid file gamma in png_set_gamma"); |
| 832 | |
| 833 | if (scrn_gamma <= 0) |
| 834 | png_error(png_ptr, "invalid screen gamma in png_set_gamma"); |
| 835 | |
| 836 | /* Set the gamma values unconditionally - this overrides the value in the PNG |
| 837 | * file if a gAMA chunk was present. png_set_alpha_mode provides a |
| 838 | * different, easier, way to default the file gamma. |
| 839 | */ |
| 840 | png_ptr->colorspace.gamma = file_gamma; |
| 841 | png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; |
| 842 | png_ptr->screen_gamma = scrn_gamma; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 843 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 844 | |
| 845 | # ifdef PNG_FLOATING_POINT_SUPPORTED |
| 846 | void PNGAPI |
| 847 | png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma) |
| 848 | { |
| 849 | png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma), |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 850 | convert_gamma_value(png_ptr, file_gamma)); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 851 | } |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 852 | # endif /* FLOATING_POINT */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 853 | #endif /* READ_GAMMA */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 854 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 855 | #ifdef PNG_READ_EXPAND_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 856 | /* Expand paletted images to RGB, expand grayscale images of |
| 857 | * less than 8-bit depth to 8-bit depth, and expand tRNS chunks |
| 858 | * to alpha channels. |
| 859 | */ |
| 860 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 861 | png_set_expand(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 862 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 863 | png_debug(1, "in png_set_expand"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 864 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 865 | if (png_rtran_ok(png_ptr, 0) == 0) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 866 | return; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 867 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 868 | png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* GRR 19990627: the following three functions currently are identical |
| 872 | * to png_set_expand(). However, it is entirely reasonable that someone |
| 873 | * might wish to expand an indexed image to RGB but *not* expand a single, |
| 874 | * fully transparent palette entry to a full alpha channel--perhaps instead |
| 875 | * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace |
| 876 | * the transparent color with a particular RGB value, or drop tRNS entirely. |
| 877 | * IOW, a future version of the library may make the transformations flag |
| 878 | * a bit more fine-grained, with separate bits for each of these three |
| 879 | * functions. |
| 880 | * |
| 881 | * More to the point, these functions make it obvious what libpng will be |
| 882 | * doing, whereas "expand" can (and does) mean any number of things. |
| 883 | * |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 884 | * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified |
| 885 | * to expand only the sample depth but not to expand the tRNS to alpha |
| 886 | * and its name was changed to png_set_expand_gray_1_2_4_to_8(). |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 887 | */ |
| 888 | |
| 889 | /* Expand paletted images to RGB. */ |
| 890 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 891 | png_set_palette_to_rgb(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 892 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 893 | png_debug(1, "in png_set_palette_to_rgb"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 894 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 895 | if (png_rtran_ok(png_ptr, 0) == 0) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 896 | return; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 897 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 898 | png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 899 | } |
| 900 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 901 | /* Expand grayscale images of less than 8-bit depth to 8 bits. */ |
| 902 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 903 | png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 904 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 905 | png_debug(1, "in png_set_expand_gray_1_2_4_to_8"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 906 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 907 | if (png_rtran_ok(png_ptr, 0) == 0) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 908 | return; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 909 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 910 | png_ptr->transformations |= PNG_EXPAND; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 911 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 912 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 913 | /* Expand tRNS chunks to alpha channels. */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 914 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 915 | png_set_tRNS_to_alpha(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 916 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 917 | png_debug(1, "in png_set_tRNS_to_alpha"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 918 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 919 | if (png_rtran_ok(png_ptr, 0) == 0) |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 920 | return; |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 921 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 922 | png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); |
| 923 | } |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 924 | #endif /* READ_EXPAND */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 925 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 926 | #ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 927 | /* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise |
| 928 | * it may not work correctly.) |
| 929 | */ |
| 930 | void PNGAPI |
| 931 | png_set_expand_16(png_structrp png_ptr) |
| 932 | { |
| 933 | png_debug(1, "in png_set_expand_16"); |
| 934 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 935 | if (png_rtran_ok(png_ptr, 0) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 936 | return; |
| 937 | |
| 938 | png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS); |
| 939 | } |
| 940 | #endif |
| 941 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 942 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 943 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 944 | png_set_gray_to_rgb(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 945 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 946 | png_debug(1, "in png_set_gray_to_rgb"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 947 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 948 | if (png_rtran_ok(png_ptr, 0) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 949 | return; |
| 950 | |
| 951 | /* Because rgb must be 8 bits or more: */ |
| 952 | png_set_expand_gray_1_2_4_to_8(png_ptr); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 953 | png_ptr->transformations |= PNG_GRAY_TO_RGB; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 954 | } |
| 955 | #endif |
| 956 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 957 | #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 958 | void PNGFAPI |
| 959 | png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action, |
| 960 | png_fixed_point red, png_fixed_point green) |
| 961 | { |
| 962 | png_debug(1, "in png_set_rgb_to_gray"); |
| 963 | |
| 964 | /* Need the IHDR here because of the check on color_type below. */ |
| 965 | /* TODO: fix this */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 966 | if (png_rtran_ok(png_ptr, 1) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 967 | return; |
| 968 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 969 | switch (error_action) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 970 | { |
| 971 | case PNG_ERROR_ACTION_NONE: |
| 972 | png_ptr->transformations |= PNG_RGB_TO_GRAY; |
| 973 | break; |
| 974 | |
| 975 | case PNG_ERROR_ACTION_WARN: |
| 976 | png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN; |
| 977 | break; |
| 978 | |
| 979 | case PNG_ERROR_ACTION_ERROR: |
| 980 | png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR; |
| 981 | break; |
| 982 | |
| 983 | default: |
| 984 | png_error(png_ptr, "invalid error action to rgb_to_gray"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 988 | #ifdef PNG_READ_EXPAND_SUPPORTED |
| 989 | png_ptr->transformations |= PNG_EXPAND; |
| 990 | #else |
| 991 | { |
| 992 | /* Make this an error in 1.6 because otherwise the application may assume |
| 993 | * that it just worked and get a memory overwrite. |
| 994 | */ |
| 995 | png_error(png_ptr, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 996 | "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 997 | |
| 998 | /* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */ |
| 999 | } |
| 1000 | #endif |
| 1001 | { |
| 1002 | if (red >= 0 && green >= 0 && red + green <= PNG_FP_1) |
| 1003 | { |
| 1004 | png_uint_16 red_int, green_int; |
| 1005 | |
| 1006 | /* NOTE: this calculation does not round, but this behavior is retained |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1007 | * for consistency; the inaccuracy is very small. The code here always |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1008 | * overwrites the coefficients, regardless of whether they have been |
| 1009 | * defaulted or set already. |
| 1010 | */ |
| 1011 | red_int = (png_uint_16)(((png_uint_32)red*32768)/100000); |
| 1012 | green_int = (png_uint_16)(((png_uint_32)green*32768)/100000); |
| 1013 | |
| 1014 | png_ptr->rgb_to_gray_red_coeff = red_int; |
| 1015 | png_ptr->rgb_to_gray_green_coeff = green_int; |
| 1016 | png_ptr->rgb_to_gray_coefficients_set = 1; |
| 1017 | } |
| 1018 | |
| 1019 | else |
| 1020 | { |
| 1021 | if (red >= 0 && green >= 0) |
| 1022 | png_app_warning(png_ptr, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1023 | "ignoring out of range rgb_to_gray coefficients"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1024 | |
| 1025 | /* Use the defaults, from the cHRM chunk if set, else the historical |
| 1026 | * values which are close to the sRGB/HDTV/ITU-Rec 709 values. See |
| 1027 | * png_do_rgb_to_gray for more discussion of the values. In this case |
| 1028 | * the coefficients are not marked as 'set' and are not overwritten if |
| 1029 | * something has already provided a default. |
| 1030 | */ |
| 1031 | if (png_ptr->rgb_to_gray_red_coeff == 0 && |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1032 | png_ptr->rgb_to_gray_green_coeff == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1033 | { |
| 1034 | png_ptr->rgb_to_gray_red_coeff = 6968; |
| 1035 | png_ptr->rgb_to_gray_green_coeff = 23434; |
| 1036 | /* png_ptr->rgb_to_gray_blue_coeff = 2366; */ |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1042 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1043 | /* Convert a RGB image to a grayscale of the same width. This allows us, |
| 1044 | * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image. |
| 1045 | */ |
| 1046 | |
| 1047 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1048 | png_set_rgb_to_gray(png_structrp png_ptr, int error_action, double red, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1049 | double green) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1050 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1051 | png_set_rgb_to_gray_fixed(png_ptr, error_action, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1052 | png_fixed(png_ptr, red, "rgb to gray red coefficient"), |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1053 | png_fixed(png_ptr, green, "rgb to gray green coefficient")); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1054 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1055 | #endif /* FLOATING POINT */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1056 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1057 | #endif /* RGB_TO_GRAY */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1058 | |
| 1059 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1060 | defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1061 | void PNGAPI |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1062 | png_set_read_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr |
| 1063 | read_user_transform_fn) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1064 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1065 | png_debug(1, "in png_set_read_user_transform_fn"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1066 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1067 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1068 | png_ptr->transformations |= PNG_USER_TRANSFORM; |
| 1069 | png_ptr->read_user_transform_fn = read_user_transform_fn; |
| 1070 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1071 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1072 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1073 | |
| 1074 | #ifdef PNG_READ_TRANSFORMS_SUPPORTED |
| 1075 | #ifdef PNG_READ_GAMMA_SUPPORTED |
| 1076 | /* In the case of gamma transformations only do transformations on images where |
| 1077 | * the [file] gamma and screen_gamma are not close reciprocals, otherwise it |
| 1078 | * slows things down slightly, and also needlessly introduces small errors. |
| 1079 | */ |
| 1080 | static int /* PRIVATE */ |
| 1081 | png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma) |
| 1082 | { |
| 1083 | /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma |
| 1084 | * correction as a difference of the overall transform from 1.0 |
| 1085 | * |
| 1086 | * We want to compare the threshold with s*f - 1, if we get |
| 1087 | * overflow here it is because of wacky gamma values so we |
| 1088 | * turn on processing anyway. |
| 1089 | */ |
| 1090 | png_fixed_point gtest; |
| 1091 | return !png_muldiv(>est, screen_gamma, file_gamma, PNG_FP_1) || |
| 1092 | png_gamma_significant(gtest); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1093 | } |
| 1094 | #endif |
| 1095 | |
| 1096 | /* Initialize everything needed for the read. This includes modifying |
| 1097 | * the palette. |
| 1098 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1099 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1100 | /* For the moment 'png_init_palette_transformations' and |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1101 | * 'png_init_rgb_transformations' only do some flag canceling optimizations. |
| 1102 | * The intent is that these two routines should have palette or rgb operations |
| 1103 | * extracted from 'png_init_read_transformations'. |
| 1104 | */ |
| 1105 | static void /* PRIVATE */ |
| 1106 | png_init_palette_transformations(png_structrp png_ptr) |
| 1107 | { |
| 1108 | /* Called to handle the (input) palette case. In png_do_read_transformations |
| 1109 | * the first step is to expand the palette if requested, so this code must |
| 1110 | * take care to only make changes that are invariant with respect to the |
| 1111 | * palette expansion, or only do them if there is no expansion. |
| 1112 | * |
| 1113 | * STRIP_ALPHA has already been handled in the caller (by setting num_trans |
| 1114 | * to 0.) |
| 1115 | */ |
| 1116 | int input_has_alpha = 0; |
| 1117 | int input_has_transparency = 0; |
| 1118 | |
| 1119 | if (png_ptr->num_trans > 0) |
| 1120 | { |
| 1121 | int i; |
| 1122 | |
| 1123 | /* Ignore if all the entries are opaque (unlikely!) */ |
| 1124 | for (i=0; i<png_ptr->num_trans; ++i) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1125 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1126 | if (png_ptr->trans_alpha[i] == 255) |
| 1127 | continue; |
| 1128 | else if (png_ptr->trans_alpha[i] == 0) |
| 1129 | input_has_transparency = 1; |
| 1130 | else |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1131 | { |
| 1132 | input_has_transparency = 1; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1133 | input_has_alpha = 1; |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1134 | break; |
| 1135 | } |
| 1136 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /* If no alpha we can optimize. */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1140 | if (input_has_alpha == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1141 | { |
| 1142 | /* Any alpha means background and associative alpha processing is |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1143 | * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1144 | * and ENCODE_ALPHA are irrelevant. |
| 1145 | */ |
| 1146 | png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 1147 | png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 1148 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1149 | if (input_has_transparency == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1150 | png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); |
| 1151 | } |
| 1152 | |
| 1153 | #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 1154 | /* png_set_background handling - deals with the complexity of whether the |
| 1155 | * background color is in the file format or the screen format in the case |
| 1156 | * where an 'expand' will happen. |
| 1157 | */ |
| 1158 | |
| 1159 | /* The following code cannot be entered in the alpha pre-multiplication case |
| 1160 | * because PNG_BACKGROUND_EXPAND is cancelled below. |
| 1161 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1162 | if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 && |
| 1163 | (png_ptr->transformations & PNG_EXPAND) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1164 | { |
| 1165 | { |
| 1166 | png_ptr->background.red = |
| 1167 | png_ptr->palette[png_ptr->background.index].red; |
| 1168 | png_ptr->background.green = |
| 1169 | png_ptr->palette[png_ptr->background.index].green; |
| 1170 | png_ptr->background.blue = |
| 1171 | png_ptr->palette[png_ptr->background.index].blue; |
| 1172 | |
| 1173 | #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1174 | if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1175 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1176 | if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1177 | { |
| 1178 | /* Invert the alpha channel (in tRNS) unless the pixels are |
| 1179 | * going to be expanded, in which case leave it for later |
| 1180 | */ |
| 1181 | int i, istop = png_ptr->num_trans; |
| 1182 | |
| 1183 | for (i=0; i<istop; i++) |
| 1184 | png_ptr->trans_alpha[i] = (png_byte)(255 - |
| 1185 | png_ptr->trans_alpha[i]); |
| 1186 | } |
| 1187 | } |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1188 | #endif /* READ_INVERT_ALPHA */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1189 | } |
| 1190 | } /* background expand and (therefore) no alpha association. */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1191 | #endif /* READ_EXPAND && READ_BACKGROUND */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | static void /* PRIVATE */ |
| 1195 | png_init_rgb_transformations(png_structrp png_ptr) |
| 1196 | { |
| 1197 | /* Added to libpng-1.5.4: check the color type to determine whether there |
| 1198 | * is any alpha or transparency in the image and simply cancel the |
| 1199 | * background and alpha mode stuff if there isn't. |
| 1200 | */ |
| 1201 | int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0; |
| 1202 | int input_has_transparency = png_ptr->num_trans > 0; |
| 1203 | |
| 1204 | /* If no alpha we can optimize. */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1205 | if (input_has_alpha == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1206 | { |
| 1207 | /* Any alpha means background and associative alpha processing is |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1208 | * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1209 | * and ENCODE_ALPHA are irrelevant. |
| 1210 | */ |
| 1211 | # ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
| 1212 | png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 1213 | png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 1214 | # endif |
| 1215 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1216 | if (input_has_transparency == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1217 | png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); |
| 1218 | } |
| 1219 | |
| 1220 | #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 1221 | /* png_set_background handling - deals with the complexity of whether the |
| 1222 | * background color is in the file format or the screen format in the case |
| 1223 | * where an 'expand' will happen. |
| 1224 | */ |
| 1225 | |
| 1226 | /* The following code cannot be entered in the alpha pre-multiplication case |
| 1227 | * because PNG_BACKGROUND_EXPAND is cancelled below. |
| 1228 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1229 | if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 && |
| 1230 | (png_ptr->transformations & PNG_EXPAND) != 0 && |
| 1231 | (png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1232 | /* i.e., GRAY or GRAY_ALPHA */ |
| 1233 | { |
| 1234 | { |
| 1235 | /* Expand background and tRNS chunks */ |
| 1236 | int gray = png_ptr->background.gray; |
| 1237 | int trans_gray = png_ptr->trans_color.gray; |
| 1238 | |
| 1239 | switch (png_ptr->bit_depth) |
| 1240 | { |
| 1241 | case 1: |
| 1242 | gray *= 0xff; |
| 1243 | trans_gray *= 0xff; |
| 1244 | break; |
| 1245 | |
| 1246 | case 2: |
| 1247 | gray *= 0x55; |
| 1248 | trans_gray *= 0x55; |
| 1249 | break; |
| 1250 | |
| 1251 | case 4: |
| 1252 | gray *= 0x11; |
| 1253 | trans_gray *= 0x11; |
| 1254 | break; |
| 1255 | |
| 1256 | default: |
| 1257 | |
| 1258 | case 8: |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 1259 | /* FALLTHROUGH */ /* (Already 8 bits) */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1260 | |
| 1261 | case 16: |
| 1262 | /* Already a full 16 bits */ |
| 1263 | break; |
| 1264 | } |
| 1265 | |
| 1266 | png_ptr->background.red = png_ptr->background.green = |
| 1267 | png_ptr->background.blue = (png_uint_16)gray; |
| 1268 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1269 | if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1270 | { |
| 1271 | png_ptr->trans_color.red = png_ptr->trans_color.green = |
| 1272 | png_ptr->trans_color.blue = (png_uint_16)trans_gray; |
| 1273 | } |
| 1274 | } |
| 1275 | } /* background expand and (therefore) no alpha association. */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1276 | #endif /* READ_EXPAND && READ_BACKGROUND */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1279 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1280 | png_init_read_transformations(png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1281 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1282 | png_debug(1, "in png_init_read_transformations"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1283 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1284 | /* This internal function is called from png_read_start_row in pngrutil.c |
| 1285 | * and it is called before the 'rowbytes' calculation is done, so the code |
| 1286 | * in here can change or update the transformations flags. |
| 1287 | * |
| 1288 | * First do updates that do not depend on the details of the PNG image data |
| 1289 | * being processed. |
| 1290 | */ |
| 1291 | |
| 1292 | #ifdef PNG_READ_GAMMA_SUPPORTED |
| 1293 | /* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds |
| 1294 | * png_set_alpha_mode and this is another source for a default file gamma so |
| 1295 | * the test needs to be performed later - here. In addition prior to 1.5.4 |
| 1296 | * the tests were repeated for the PALETTE color type here - this is no |
| 1297 | * longer necessary (and doesn't seem to have been necessary before.) |
| 1298 | */ |
| 1299 | { |
| 1300 | /* The following temporary indicates if overall gamma correction is |
| 1301 | * required. |
| 1302 | */ |
| 1303 | int gamma_correction = 0; |
| 1304 | |
| 1305 | if (png_ptr->colorspace.gamma != 0) /* has been set */ |
| 1306 | { |
| 1307 | if (png_ptr->screen_gamma != 0) /* screen set too */ |
| 1308 | gamma_correction = png_gamma_threshold(png_ptr->colorspace.gamma, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1309 | png_ptr->screen_gamma); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1310 | |
| 1311 | else |
| 1312 | /* Assume the output matches the input; a long time default behavior |
| 1313 | * of libpng, although the standard has nothing to say about this. |
| 1314 | */ |
| 1315 | png_ptr->screen_gamma = png_reciprocal(png_ptr->colorspace.gamma); |
| 1316 | } |
| 1317 | |
| 1318 | else if (png_ptr->screen_gamma != 0) |
| 1319 | /* The converse - assume the file matches the screen, note that this |
| 1320 | * perhaps undesireable default can (from 1.5.4) be changed by calling |
| 1321 | * png_set_alpha_mode (even if the alpha handling mode isn't required |
| 1322 | * or isn't changed from the default.) |
| 1323 | */ |
| 1324 | png_ptr->colorspace.gamma = png_reciprocal(png_ptr->screen_gamma); |
| 1325 | |
| 1326 | else /* neither are set */ |
| 1327 | /* Just in case the following prevents any processing - file and screen |
| 1328 | * are both assumed to be linear and there is no way to introduce a |
| 1329 | * third gamma value other than png_set_background with 'UNIQUE', and, |
| 1330 | * prior to 1.5.4 |
| 1331 | */ |
| 1332 | png_ptr->screen_gamma = png_ptr->colorspace.gamma = PNG_FP_1; |
| 1333 | |
| 1334 | /* We have a gamma value now. */ |
| 1335 | png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; |
| 1336 | |
| 1337 | /* Now turn the gamma transformation on or off as appropriate. Notice |
| 1338 | * that PNG_GAMMA just refers to the file->screen correction. Alpha |
| 1339 | * composition may independently cause gamma correction because it needs |
| 1340 | * linear data (e.g. if the file has a gAMA chunk but the screen gamma |
| 1341 | * hasn't been specified.) In any case this flag may get turned off in |
| 1342 | * the code immediately below if the transform can be handled outside the |
| 1343 | * row loop. |
| 1344 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1345 | if (gamma_correction != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1346 | png_ptr->transformations |= PNG_GAMMA; |
| 1347 | |
| 1348 | else |
| 1349 | png_ptr->transformations &= ~PNG_GAMMA; |
| 1350 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1351 | #endif |
| 1352 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1353 | /* Certain transformations have the effect of preventing other |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1354 | * transformations that happen afterward in png_do_read_transformations; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1355 | * resolve the interdependencies here. From the code of |
| 1356 | * png_do_read_transformations the order is: |
| 1357 | * |
| 1358 | * 1) PNG_EXPAND (including PNG_EXPAND_tRNS) |
| 1359 | * 2) PNG_STRIP_ALPHA (if no compose) |
| 1360 | * 3) PNG_RGB_TO_GRAY |
| 1361 | * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY |
| 1362 | * 5) PNG_COMPOSE |
| 1363 | * 6) PNG_GAMMA |
| 1364 | * 7) PNG_STRIP_ALPHA (if compose) |
| 1365 | * 8) PNG_ENCODE_ALPHA |
| 1366 | * 9) PNG_SCALE_16_TO_8 |
| 1367 | * 10) PNG_16_TO_8 |
| 1368 | * 11) PNG_QUANTIZE (converts to palette) |
| 1369 | * 12) PNG_EXPAND_16 |
| 1370 | * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY |
| 1371 | * 14) PNG_INVERT_MONO |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1372 | * 15) PNG_INVERT_ALPHA |
| 1373 | * 16) PNG_SHIFT |
| 1374 | * 17) PNG_PACK |
| 1375 | * 18) PNG_BGR |
| 1376 | * 19) PNG_PACKSWAP |
| 1377 | * 20) PNG_FILLER (includes PNG_ADD_ALPHA) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1378 | * 21) PNG_SWAP_ALPHA |
| 1379 | * 22) PNG_SWAP_BYTES |
| 1380 | * 23) PNG_USER_TRANSFORM [must be last] |
| 1381 | */ |
| 1382 | #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1383 | if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && |
| 1384 | (png_ptr->transformations & PNG_COMPOSE) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1385 | { |
| 1386 | /* Stripping the alpha channel happens immediately after the 'expand' |
| 1387 | * transformations, before all other transformation, so it cancels out |
| 1388 | * the alpha handling. It has the side effect negating the effect of |
| 1389 | * PNG_EXPAND_tRNS too: |
| 1390 | */ |
| 1391 | png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA | |
| 1392 | PNG_EXPAND_tRNS); |
| 1393 | png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 1394 | |
| 1395 | /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen |
| 1396 | * so transparency information would remain just so long as it wasn't |
| 1397 | * expanded. This produces unexpected API changes if the set of things |
| 1398 | * that do PNG_EXPAND_tRNS changes (perfectly possible given the |
| 1399 | * documentation - which says ask for what you want, accept what you |
| 1400 | * get.) This makes the behavior consistent from 1.5.4: |
| 1401 | */ |
| 1402 | png_ptr->num_trans = 0; |
| 1403 | } |
| 1404 | #endif /* STRIP_ALPHA supported, no COMPOSE */ |
| 1405 | |
| 1406 | #ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
| 1407 | /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA |
| 1408 | * settings will have no effect. |
| 1409 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1410 | if (png_gamma_significant(png_ptr->screen_gamma) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1411 | { |
| 1412 | png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 1413 | png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 1414 | } |
| 1415 | #endif |
| 1416 | |
| 1417 | #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
| 1418 | /* Make sure the coefficients for the rgb to gray conversion are set |
| 1419 | * appropriately. |
| 1420 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1421 | if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1422 | png_colorspace_set_rgb_coefficients(png_ptr); |
| 1423 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1424 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1425 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1426 | #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 1427 | /* Detect gray background and attempt to enable optimization for |
| 1428 | * gray --> RGB case. |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1429 | * |
| 1430 | * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1431 | * RGB_ALPHA (in which case need_expand is superfluous anyway), the |
| 1432 | * background color might actually be gray yet not be flagged as such. |
| 1433 | * This is not a problem for the current code, which uses |
| 1434 | * PNG_BACKGROUND_IS_GRAY only to decide when to do the |
| 1435 | * png_do_gray_to_rgb() transformation. |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1436 | * |
| 1437 | * TODO: this code needs to be revised to avoid the complexity and |
| 1438 | * interdependencies. The color type of the background should be recorded in |
| 1439 | * png_set_background, along with the bit depth, then the code has a record |
| 1440 | * of exactly what color space the background is currently in. |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1441 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1442 | if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1443 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1444 | /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if |
| 1445 | * the file was grayscale the background value is gray. |
| 1446 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1447 | if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1448 | png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1449 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1450 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1451 | else if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1452 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1453 | /* PNG_COMPOSE: png_set_background was called with need_expand false, |
| 1454 | * so the color is in the color space of the output or png_set_alpha_mode |
| 1455 | * was called and the color is black. Ignore RGB_TO_GRAY because that |
| 1456 | * happens before GRAY_TO_RGB. |
| 1457 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1458 | if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1459 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1460 | if (png_ptr->background.red == png_ptr->background.green && |
| 1461 | png_ptr->background.red == png_ptr->background.blue) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1462 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1463 | png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; |
| 1464 | png_ptr->background.gray = png_ptr->background.red; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1465 | } |
| 1466 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1467 | } |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1468 | #endif /* READ_EXPAND && READ_BACKGROUND */ |
| 1469 | #endif /* READ_GRAY_TO_RGB */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1470 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1471 | /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations |
| 1472 | * can be performed directly on the palette, and some (such as rgb to gray) |
| 1473 | * can be optimized inside the palette. This is particularly true of the |
| 1474 | * composite (background and alpha) stuff, which can be pretty much all done |
| 1475 | * in the palette even if the result is expanded to RGB or gray afterward. |
| 1476 | * |
| 1477 | * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and |
| 1478 | * earlier and the palette stuff is actually handled on the first row. This |
| 1479 | * leads to the reported bug that the palette returned by png_get_PLTE is not |
| 1480 | * updated. |
| 1481 | */ |
| 1482 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1483 | png_init_palette_transformations(png_ptr); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1484 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1485 | else |
| 1486 | png_init_rgb_transformations(png_ptr); |
| 1487 | |
| 1488 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ |
| 1489 | defined(PNG_READ_EXPAND_16_SUPPORTED) |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1490 | if ((png_ptr->transformations & PNG_EXPAND_16) != 0 && |
| 1491 | (png_ptr->transformations & PNG_COMPOSE) != 0 && |
| 1492 | (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 && |
| 1493 | png_ptr->bit_depth != 16) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1494 | { |
| 1495 | /* TODO: fix this. Because the expand_16 operation is after the compose |
| 1496 | * handling the background color must be 8, not 16, bits deep, but the |
| 1497 | * application will supply a 16-bit value so reduce it here. |
| 1498 | * |
| 1499 | * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at |
| 1500 | * present, so that case is ok (until do_expand_16 is moved.) |
| 1501 | * |
| 1502 | * NOTE: this discards the low 16 bits of the user supplied background |
| 1503 | * color, but until expand_16 works properly there is no choice! |
| 1504 | */ |
| 1505 | # define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x)) |
| 1506 | CHOP(png_ptr->background.red); |
| 1507 | CHOP(png_ptr->background.green); |
| 1508 | CHOP(png_ptr->background.blue); |
| 1509 | CHOP(png_ptr->background.gray); |
| 1510 | # undef CHOP |
| 1511 | } |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1512 | #endif /* READ_BACKGROUND && READ_EXPAND_16 */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1513 | |
| 1514 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ |
| 1515 | (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \ |
| 1516 | defined(PNG_READ_STRIP_16_TO_8_SUPPORTED)) |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1517 | if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) != 0 && |
| 1518 | (png_ptr->transformations & PNG_COMPOSE) != 0 && |
| 1519 | (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 && |
| 1520 | png_ptr->bit_depth == 16) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1521 | { |
| 1522 | /* On the other hand, if a 16-bit file is to be reduced to 8-bits per |
| 1523 | * component this will also happen after PNG_COMPOSE and so the background |
| 1524 | * color must be pre-expanded here. |
| 1525 | * |
| 1526 | * TODO: fix this too. |
| 1527 | */ |
| 1528 | png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257); |
| 1529 | png_ptr->background.green = |
| 1530 | (png_uint_16)(png_ptr->background.green * 257); |
| 1531 | png_ptr->background.blue = (png_uint_16)(png_ptr->background.blue * 257); |
| 1532 | png_ptr->background.gray = (png_uint_16)(png_ptr->background.gray * 257); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1533 | } |
| 1534 | #endif |
| 1535 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1536 | /* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the |
| 1537 | * background support (see the comments in scripts/pnglibconf.dfa), this |
| 1538 | * allows pre-multiplication of the alpha channel to be implemented as |
| 1539 | * compositing on black. This is probably sub-optimal and has been done in |
| 1540 | * 1.5.4 betas simply to enable external critique and testing (i.e. to |
| 1541 | * implement the new API quickly, without lots of internal changes.) |
| 1542 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1543 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1544 | #ifdef PNG_READ_GAMMA_SUPPORTED |
| 1545 | # ifdef PNG_READ_BACKGROUND_SUPPORTED |
| 1546 | /* Includes ALPHA_MODE */ |
| 1547 | png_ptr->background_1 = png_ptr->background; |
| 1548 | # endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1549 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1550 | /* This needs to change - in the palette image case a whole set of tables are |
| 1551 | * built when it would be quicker to just calculate the correct value for |
| 1552 | * each palette entry directly. Also, the test is too tricky - why check |
| 1553 | * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that |
| 1554 | * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the |
| 1555 | * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction |
| 1556 | * the gamma tables will not be built even if composition is required on a |
| 1557 | * gamma encoded value. |
| 1558 | * |
| 1559 | * In 1.5.4 this is addressed below by an additional check on the individual |
| 1560 | * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the |
| 1561 | * tables. |
| 1562 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1563 | if ((png_ptr->transformations & PNG_GAMMA) != 0 || |
| 1564 | ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0 && |
| 1565 | (png_gamma_significant(png_ptr->colorspace.gamma) != 0 || |
| 1566 | png_gamma_significant(png_ptr->screen_gamma) != 0)) || |
| 1567 | ((png_ptr->transformations & PNG_COMPOSE) != 0 && |
| 1568 | (png_gamma_significant(png_ptr->colorspace.gamma) != 0 || |
| 1569 | png_gamma_significant(png_ptr->screen_gamma) != 0 |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1570 | # ifdef PNG_READ_BACKGROUND_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1571 | || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE && |
| 1572 | png_gamma_significant(png_ptr->background_gamma) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1573 | # endif |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1574 | )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 && |
| 1575 | png_gamma_significant(png_ptr->screen_gamma) != 0)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1576 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1577 | png_build_gamma_table(png_ptr, png_ptr->bit_depth); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1578 | |
| 1579 | #ifdef PNG_READ_BACKGROUND_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1580 | if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1581 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1582 | /* Issue a warning about this combination: because RGB_TO_GRAY is |
| 1583 | * optimized to do the gamma transform if present yet do_background has |
| 1584 | * to do the same thing if both options are set a |
| 1585 | * double-gamma-correction happens. This is true in all versions of |
| 1586 | * libpng to date. |
| 1587 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1588 | if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1589 | png_warning(png_ptr, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1590 | "libpng does not support gamma+background+rgb_to_gray"); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1591 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1592 | if ((png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1593 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1594 | /* We don't get to here unless there is a tRNS chunk with non-opaque |
| 1595 | * entries - see the checking code at the start of this function. |
| 1596 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1597 | png_color back, back_1; |
| 1598 | png_colorp palette = png_ptr->palette; |
| 1599 | int num_palette = png_ptr->num_palette; |
| 1600 | int i; |
| 1601 | if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) |
| 1602 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1603 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1604 | back.red = png_ptr->gamma_table[png_ptr->background.red]; |
| 1605 | back.green = png_ptr->gamma_table[png_ptr->background.green]; |
| 1606 | back.blue = png_ptr->gamma_table[png_ptr->background.blue]; |
| 1607 | |
| 1608 | back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; |
| 1609 | back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; |
| 1610 | back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; |
| 1611 | } |
| 1612 | else |
| 1613 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1614 | png_fixed_point g, gs; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1615 | |
| 1616 | switch (png_ptr->background_gamma_type) |
| 1617 | { |
| 1618 | case PNG_BACKGROUND_GAMMA_SCREEN: |
| 1619 | g = (png_ptr->screen_gamma); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1620 | gs = PNG_FP_1; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1621 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1622 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1623 | case PNG_BACKGROUND_GAMMA_FILE: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1624 | g = png_reciprocal(png_ptr->colorspace.gamma); |
| 1625 | gs = png_reciprocal2(png_ptr->colorspace.gamma, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1626 | png_ptr->screen_gamma); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1627 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1628 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1629 | case PNG_BACKGROUND_GAMMA_UNIQUE: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1630 | g = png_reciprocal(png_ptr->background_gamma); |
| 1631 | gs = png_reciprocal2(png_ptr->background_gamma, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1632 | png_ptr->screen_gamma); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1633 | break; |
| 1634 | default: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1635 | g = PNG_FP_1; /* back_1 */ |
| 1636 | gs = PNG_FP_1; /* back */ |
| 1637 | break; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1638 | } |
| 1639 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1640 | if (png_gamma_significant(gs) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1641 | { |
| 1642 | back.red = png_gamma_8bit_correct(png_ptr->background.red, |
| 1643 | gs); |
| 1644 | back.green = png_gamma_8bit_correct(png_ptr->background.green, |
| 1645 | gs); |
| 1646 | back.blue = png_gamma_8bit_correct(png_ptr->background.blue, |
| 1647 | gs); |
| 1648 | } |
| 1649 | |
| 1650 | else |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1651 | { |
| 1652 | back.red = (png_byte)png_ptr->background.red; |
| 1653 | back.green = (png_byte)png_ptr->background.green; |
| 1654 | back.blue = (png_byte)png_ptr->background.blue; |
| 1655 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1656 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1657 | if (png_gamma_significant(g) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1658 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1659 | back_1.red = png_gamma_8bit_correct(png_ptr->background.red, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1660 | g); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1661 | back_1.green = png_gamma_8bit_correct( |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1662 | png_ptr->background.green, g); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1663 | back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1664 | g); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1665 | } |
| 1666 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1667 | else |
| 1668 | { |
| 1669 | back_1.red = (png_byte)png_ptr->background.red; |
| 1670 | back_1.green = (png_byte)png_ptr->background.green; |
| 1671 | back_1.blue = (png_byte)png_ptr->background.blue; |
| 1672 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1673 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1674 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1675 | for (i = 0; i < num_palette; i++) |
| 1676 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1677 | if (i < (int)png_ptr->num_trans && |
| 1678 | png_ptr->trans_alpha[i] != 0xff) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1679 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1680 | if (png_ptr->trans_alpha[i] == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1681 | { |
| 1682 | palette[i] = back; |
| 1683 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1684 | else /* if (png_ptr->trans_alpha[i] != 0xff) */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1685 | { |
| 1686 | png_byte v, w; |
| 1687 | |
| 1688 | v = png_ptr->gamma_to_1[palette[i].red]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1689 | png_composite(w, v, png_ptr->trans_alpha[i], back_1.red); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1690 | palette[i].red = png_ptr->gamma_from_1[w]; |
| 1691 | |
| 1692 | v = png_ptr->gamma_to_1[palette[i].green]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1693 | png_composite(w, v, png_ptr->trans_alpha[i], back_1.green); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1694 | palette[i].green = png_ptr->gamma_from_1[w]; |
| 1695 | |
| 1696 | v = png_ptr->gamma_to_1[palette[i].blue]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1697 | png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1698 | palette[i].blue = png_ptr->gamma_from_1[w]; |
| 1699 | } |
| 1700 | } |
| 1701 | else |
| 1702 | { |
| 1703 | palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 1704 | palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 1705 | palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 1706 | } |
| 1707 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1708 | |
| 1709 | /* Prevent the transformations being done again. |
| 1710 | * |
| 1711 | * NOTE: this is highly dubious; it removes the transformations in |
| 1712 | * place. This seems inconsistent with the general treatment of the |
| 1713 | * transformations elsewhere. |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1714 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1715 | png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA); |
| 1716 | } /* color_type == PNG_COLOR_TYPE_PALETTE */ |
| 1717 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1718 | /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1719 | else /* color_type != PNG_COLOR_TYPE_PALETTE */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1720 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1721 | int gs_sig, g_sig; |
| 1722 | png_fixed_point g = PNG_FP_1; /* Correction to linear */ |
| 1723 | png_fixed_point gs = PNG_FP_1; /* Correction to screen */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1724 | |
| 1725 | switch (png_ptr->background_gamma_type) |
| 1726 | { |
| 1727 | case PNG_BACKGROUND_GAMMA_SCREEN: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1728 | g = png_ptr->screen_gamma; |
| 1729 | /* gs = PNG_FP_1; */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1730 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1731 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1732 | case PNG_BACKGROUND_GAMMA_FILE: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1733 | g = png_reciprocal(png_ptr->colorspace.gamma); |
| 1734 | gs = png_reciprocal2(png_ptr->colorspace.gamma, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1735 | png_ptr->screen_gamma); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1736 | break; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1737 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1738 | case PNG_BACKGROUND_GAMMA_UNIQUE: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1739 | g = png_reciprocal(png_ptr->background_gamma); |
| 1740 | gs = png_reciprocal2(png_ptr->background_gamma, |
| 1741 | png_ptr->screen_gamma); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1742 | break; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1743 | |
| 1744 | default: |
| 1745 | png_error(png_ptr, "invalid background gamma type"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1746 | } |
| 1747 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1748 | g_sig = png_gamma_significant(g); |
| 1749 | gs_sig = png_gamma_significant(gs); |
| 1750 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1751 | if (g_sig != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1752 | png_ptr->background_1.gray = png_gamma_correct(png_ptr, |
| 1753 | png_ptr->background.gray, g); |
| 1754 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1755 | if (gs_sig != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1756 | png_ptr->background.gray = png_gamma_correct(png_ptr, |
| 1757 | png_ptr->background.gray, gs); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1758 | |
| 1759 | if ((png_ptr->background.red != png_ptr->background.green) || |
| 1760 | (png_ptr->background.red != png_ptr->background.blue) || |
| 1761 | (png_ptr->background.red != png_ptr->background.gray)) |
| 1762 | { |
| 1763 | /* RGB or RGBA with color background */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1764 | if (g_sig != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1765 | { |
| 1766 | png_ptr->background_1.red = png_gamma_correct(png_ptr, |
| 1767 | png_ptr->background.red, g); |
| 1768 | |
| 1769 | png_ptr->background_1.green = png_gamma_correct(png_ptr, |
| 1770 | png_ptr->background.green, g); |
| 1771 | |
| 1772 | png_ptr->background_1.blue = png_gamma_correct(png_ptr, |
| 1773 | png_ptr->background.blue, g); |
| 1774 | } |
| 1775 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1776 | if (gs_sig != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1777 | { |
| 1778 | png_ptr->background.red = png_gamma_correct(png_ptr, |
| 1779 | png_ptr->background.red, gs); |
| 1780 | |
| 1781 | png_ptr->background.green = png_gamma_correct(png_ptr, |
| 1782 | png_ptr->background.green, gs); |
| 1783 | |
| 1784 | png_ptr->background.blue = png_gamma_correct(png_ptr, |
| 1785 | png_ptr->background.blue, gs); |
| 1786 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1787 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1788 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1789 | else |
| 1790 | { |
| 1791 | /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */ |
| 1792 | png_ptr->background_1.red = png_ptr->background_1.green |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1793 | = png_ptr->background_1.blue = png_ptr->background_1.gray; |
| 1794 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1795 | png_ptr->background.red = png_ptr->background.green |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1796 | = png_ptr->background.blue = png_ptr->background.gray; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1797 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1798 | |
| 1799 | /* The background is now in screen gamma: */ |
| 1800 | png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN; |
| 1801 | } /* color_type != PNG_COLOR_TYPE_PALETTE */ |
| 1802 | }/* png_ptr->transformations & PNG_BACKGROUND */ |
| 1803 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1804 | else |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 1805 | /* Transformation does not include PNG_BACKGROUND */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1806 | #endif /* READ_BACKGROUND */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1807 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE |
| 1808 | #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
| 1809 | /* RGB_TO_GRAY needs to have non-gamma-corrected values! */ |
| 1810 | && ((png_ptr->transformations & PNG_EXPAND) == 0 || |
| 1811 | (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) |
| 1812 | #endif |
| 1813 | ) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1814 | { |
| 1815 | png_colorp palette = png_ptr->palette; |
| 1816 | int num_palette = png_ptr->num_palette; |
| 1817 | int i; |
| 1818 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1819 | /* NOTE: there are other transformations that should probably be in |
| 1820 | * here too. |
| 1821 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1822 | for (i = 0; i < num_palette; i++) |
| 1823 | { |
| 1824 | palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 1825 | palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 1826 | palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 1827 | } |
| 1828 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1829 | /* Done the gamma correction. */ |
| 1830 | png_ptr->transformations &= ~PNG_GAMMA; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1831 | } /* color_type == PALETTE && !PNG_BACKGROUND transformation */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1832 | } |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1833 | #ifdef PNG_READ_BACKGROUND_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1834 | else |
| 1835 | #endif |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1836 | #endif /* READ_GAMMA */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1837 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1838 | #ifdef PNG_READ_BACKGROUND_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1839 | /* No GAMMA transformation (see the hanging else 4 lines above) */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1840 | if ((png_ptr->transformations & PNG_COMPOSE) != 0 && |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1841 | (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1842 | { |
| 1843 | int i; |
| 1844 | int istop = (int)png_ptr->num_trans; |
| 1845 | png_color back; |
| 1846 | png_colorp palette = png_ptr->palette; |
| 1847 | |
| 1848 | back.red = (png_byte)png_ptr->background.red; |
| 1849 | back.green = (png_byte)png_ptr->background.green; |
| 1850 | back.blue = (png_byte)png_ptr->background.blue; |
| 1851 | |
| 1852 | for (i = 0; i < istop; i++) |
| 1853 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1854 | if (png_ptr->trans_alpha[i] == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1855 | { |
| 1856 | palette[i] = back; |
| 1857 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1858 | |
| 1859 | else if (png_ptr->trans_alpha[i] != 0xff) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1860 | { |
| 1861 | /* The png_composite() macro is defined in png.h */ |
| 1862 | png_composite(palette[i].red, palette[i].red, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1863 | png_ptr->trans_alpha[i], back.red); |
| 1864 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1865 | png_composite(palette[i].green, palette[i].green, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1866 | png_ptr->trans_alpha[i], back.green); |
| 1867 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1868 | png_composite(palette[i].blue, palette[i].blue, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1869 | png_ptr->trans_alpha[i], back.blue); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1870 | } |
| 1871 | } |
| 1872 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1873 | png_ptr->transformations &= ~PNG_COMPOSE; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1874 | } |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1875 | #endif /* READ_BACKGROUND */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1876 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1877 | #ifdef PNG_READ_SHIFT_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1878 | if ((png_ptr->transformations & PNG_SHIFT) != 0 && |
| 1879 | (png_ptr->transformations & PNG_EXPAND) == 0 && |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1880 | (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1881 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1882 | int i; |
| 1883 | int istop = png_ptr->num_palette; |
| 1884 | int shift = 8 - png_ptr->sig_bit.red; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1885 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1886 | png_ptr->transformations &= ~PNG_SHIFT; |
| 1887 | |
| 1888 | /* significant bits can be in the range 1 to 7 for a meaninful result, if |
| 1889 | * the number of significant bits is 0 then no shift is done (this is an |
| 1890 | * error condition which is silently ignored.) |
| 1891 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1892 | if (shift > 0 && shift < 8) |
| 1893 | for (i=0; i<istop; ++i) |
| 1894 | { |
| 1895 | int component = png_ptr->palette[i].red; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1896 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1897 | component >>= shift; |
| 1898 | png_ptr->palette[i].red = (png_byte)component; |
| 1899 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1900 | |
| 1901 | shift = 8 - png_ptr->sig_bit.green; |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1902 | if (shift > 0 && shift < 8) |
| 1903 | for (i=0; i<istop; ++i) |
| 1904 | { |
| 1905 | int component = png_ptr->palette[i].green; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1906 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1907 | component >>= shift; |
| 1908 | png_ptr->palette[i].green = (png_byte)component; |
| 1909 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1910 | |
| 1911 | shift = 8 - png_ptr->sig_bit.blue; |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1912 | if (shift > 0 && shift < 8) |
| 1913 | for (i=0; i<istop; ++i) |
| 1914 | { |
| 1915 | int component = png_ptr->palette[i].blue; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1916 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1917 | component >>= shift; |
| 1918 | png_ptr->palette[i].blue = (png_byte)component; |
| 1919 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1920 | } |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 1921 | #endif /* READ_SHIFT */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | /* Modify the info structure to reflect the transformations. The |
| 1925 | * info should be updated so a PNG file could be written with it, |
| 1926 | * assuming the transformations result in valid PNG data. |
| 1927 | */ |
| 1928 | void /* PRIVATE */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1929 | png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1930 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1931 | png_debug(1, "in png_read_transform_info"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1932 | |
| 1933 | #ifdef PNG_READ_EXPAND_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1934 | if ((png_ptr->transformations & PNG_EXPAND) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1935 | { |
| 1936 | if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1937 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1938 | /* This check must match what actually happens in |
| 1939 | * png_do_expand_palette; if it ever checks the tRNS chunk to see if |
| 1940 | * it is all opaque we must do the same (at present it does not.) |
| 1941 | */ |
| 1942 | if (png_ptr->num_trans > 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1943 | info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1944 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1945 | else |
| 1946 | info_ptr->color_type = PNG_COLOR_TYPE_RGB; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1947 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1948 | info_ptr->bit_depth = 8; |
| 1949 | info_ptr->num_trans = 0; |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 1950 | |
| 1951 | if (png_ptr->palette == NULL) |
| 1952 | png_error (png_ptr, "Palette is NULL in indexed image"); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1953 | } |
| 1954 | else |
| 1955 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1956 | if (png_ptr->num_trans != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1957 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1958 | if ((png_ptr->transformations & PNG_EXPAND_tRNS) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1959 | info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1960 | } |
| 1961 | if (info_ptr->bit_depth < 8) |
| 1962 | info_ptr->bit_depth = 8; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1963 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1964 | info_ptr->num_trans = 0; |
| 1965 | } |
| 1966 | } |
| 1967 | #endif |
| 1968 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1969 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ |
| 1970 | defined(PNG_READ_ALPHA_MODE_SUPPORTED) |
| 1971 | /* The following is almost certainly wrong unless the background value is in |
| 1972 | * the screen space! |
| 1973 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1974 | if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1975 | info_ptr->background = png_ptr->background; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1976 | #endif |
| 1977 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 1978 | #ifdef PNG_READ_GAMMA_SUPPORTED |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1979 | /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4), |
| 1980 | * however it seems that the code in png_init_read_transformations, which has |
| 1981 | * been called before this from png_read_update_info->png_read_start_row |
| 1982 | * sometimes does the gamma transform and cancels the flag. |
| 1983 | * |
| 1984 | * TODO: this looks wrong; the info_ptr should end up with a gamma equal to |
| 1985 | * the screen_gamma value. The following probably results in weirdness if |
| 1986 | * the info_ptr is used by the app after the rows have been read. |
| 1987 | */ |
| 1988 | info_ptr->colorspace.gamma = png_ptr->colorspace.gamma; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1989 | #endif |
| 1990 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1991 | if (info_ptr->bit_depth == 16) |
| 1992 | { |
| 1993 | # ifdef PNG_READ_16BIT_SUPPORTED |
| 1994 | # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 1995 | if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 1996 | info_ptr->bit_depth = 8; |
| 1997 | # endif |
| 1998 | |
| 1999 | # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2000 | if ((png_ptr->transformations & PNG_16_TO_8) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2001 | info_ptr->bit_depth = 8; |
| 2002 | # endif |
| 2003 | |
| 2004 | # else |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2005 | /* No 16-bit support: force chopping 16-bit input down to 8, in this case |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2006 | * the app program can chose if both APIs are available by setting the |
| 2007 | * correct scaling to use. |
| 2008 | */ |
| 2009 | # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
| 2010 | /* For compatibility with previous versions use the strip method by |
| 2011 | * default. This code works because if PNG_SCALE_16_TO_8 is already |
| 2012 | * set the code below will do that in preference to the chop. |
| 2013 | */ |
| 2014 | png_ptr->transformations |= PNG_16_TO_8; |
| 2015 | info_ptr->bit_depth = 8; |
| 2016 | # else |
| 2017 | |
| 2018 | # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
| 2019 | png_ptr->transformations |= PNG_SCALE_16_TO_8; |
| 2020 | info_ptr->bit_depth = 8; |
| 2021 | # else |
| 2022 | |
| 2023 | CONFIGURATION ERROR: you must enable at least one 16 to 8 method |
| 2024 | # endif |
| 2025 | # endif |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2026 | #endif /* !READ_16BIT */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2027 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2028 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2029 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2030 | if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2031 | info_ptr->color_type = (png_byte)(info_ptr->color_type | |
| 2032 | PNG_COLOR_MASK_COLOR); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2033 | #endif |
| 2034 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2035 | #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2036 | if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2037 | info_ptr->color_type = (png_byte)(info_ptr->color_type & |
| 2038 | ~PNG_COLOR_MASK_COLOR); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2039 | #endif |
| 2040 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2041 | #ifdef PNG_READ_QUANTIZE_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2042 | if ((png_ptr->transformations & PNG_QUANTIZE) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2043 | { |
| 2044 | if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2045 | (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2046 | png_ptr->palette_lookup != 0 && info_ptr->bit_depth == 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2047 | { |
| 2048 | info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; |
| 2049 | } |
| 2050 | } |
| 2051 | #endif |
| 2052 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2053 | #ifdef PNG_READ_EXPAND_16_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2054 | if ((png_ptr->transformations & PNG_EXPAND_16) != 0 && |
| 2055 | info_ptr->bit_depth == 8 && |
| 2056 | info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2057 | { |
| 2058 | info_ptr->bit_depth = 16; |
| 2059 | } |
| 2060 | #endif |
| 2061 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2062 | #ifdef PNG_READ_PACK_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2063 | if ((png_ptr->transformations & PNG_PACK) != 0 && |
| 2064 | (info_ptr->bit_depth < 8)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2065 | info_ptr->bit_depth = 8; |
| 2066 | #endif |
| 2067 | |
| 2068 | if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 2069 | info_ptr->channels = 1; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2070 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2071 | else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2072 | info_ptr->channels = 3; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2073 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2074 | else |
| 2075 | info_ptr->channels = 1; |
| 2076 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2077 | #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2078 | if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2079 | { |
| 2080 | info_ptr->color_type = (png_byte)(info_ptr->color_type & |
| 2081 | ~PNG_COLOR_MASK_ALPHA); |
| 2082 | info_ptr->num_trans = 0; |
| 2083 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2084 | #endif |
| 2085 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2086 | if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2087 | info_ptr->channels++; |
| 2088 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2089 | #ifdef PNG_READ_FILLER_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2090 | /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2091 | if ((png_ptr->transformations & PNG_FILLER) != 0 && |
| 2092 | (info_ptr->color_type == PNG_COLOR_TYPE_RGB || |
| 2093 | info_ptr->color_type == PNG_COLOR_TYPE_GRAY)) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2094 | { |
| 2095 | info_ptr->channels++; |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2096 | /* If adding a true alpha channel not just filler */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2097 | if ((png_ptr->transformations & PNG_ADD_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2098 | info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2099 | } |
| 2100 | #endif |
| 2101 | |
| 2102 | #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ |
| 2103 | defined(PNG_READ_USER_TRANSFORM_SUPPORTED) |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2104 | if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2105 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2106 | if (png_ptr->user_transform_depth != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2107 | info_ptr->bit_depth = png_ptr->user_transform_depth; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2108 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2109 | if (png_ptr->user_transform_channels != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2110 | info_ptr->channels = png_ptr->user_transform_channels; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2111 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2112 | #endif |
| 2113 | |
| 2114 | info_ptr->pixel_depth = (png_byte)(info_ptr->channels * |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2115 | info_ptr->bit_depth); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2116 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2117 | info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2118 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2119 | /* Adding in 1.5.4: cache the above value in png_struct so that we can later |
| 2120 | * check in png_rowbytes that the user buffer won't get overwritten. Note |
| 2121 | * that the field is not always set - if png_read_update_info isn't called |
| 2122 | * the application has to either not do any transforms or get the calculation |
| 2123 | * right itself. |
| 2124 | */ |
| 2125 | png_ptr->info_rowbytes = info_ptr->rowbytes; |
| 2126 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2127 | #ifndef PNG_READ_EXPAND_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2128 | if (png_ptr != NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2129 | return; |
| 2130 | #endif |
| 2131 | } |
| 2132 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2133 | #ifdef PNG_READ_PACK_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2134 | /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel, |
| 2135 | * without changing the actual values. Thus, if you had a row with |
| 2136 | * a bit depth of 1, you would end up with bytes that only contained |
| 2137 | * the numbers 0 or 1. If you would rather they contain 0 and 255, use |
| 2138 | * png_do_shift() after this. |
| 2139 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2140 | static void |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2141 | png_do_unpack(png_row_infop row_info, png_bytep row) |
| 2142 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2143 | png_debug(1, "in png_do_unpack"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2144 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2145 | if (row_info->bit_depth < 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2146 | { |
| 2147 | png_uint_32 i; |
| 2148 | png_uint_32 row_width=row_info->width; |
| 2149 | |
| 2150 | switch (row_info->bit_depth) |
| 2151 | { |
| 2152 | case 1: |
| 2153 | { |
| 2154 | png_bytep sp = row + (png_size_t)((row_width - 1) >> 3); |
| 2155 | png_bytep dp = row + (png_size_t)row_width - 1; |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 2156 | png_uint_32 shift = 7U - ((row_width + 7U) & 0x07); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2157 | for (i = 0; i < row_width; i++) |
| 2158 | { |
| 2159 | *dp = (png_byte)((*sp >> shift) & 0x01); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2160 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2161 | if (shift == 7) |
| 2162 | { |
| 2163 | shift = 0; |
| 2164 | sp--; |
| 2165 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2166 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2167 | else |
| 2168 | shift++; |
| 2169 | |
| 2170 | dp--; |
| 2171 | } |
| 2172 | break; |
| 2173 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2174 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2175 | case 2: |
| 2176 | { |
| 2177 | |
| 2178 | png_bytep sp = row + (png_size_t)((row_width - 1) >> 2); |
| 2179 | png_bytep dp = row + (png_size_t)row_width - 1; |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 2180 | png_uint_32 shift = ((3U - ((row_width + 3U) & 0x03)) << 1); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2181 | for (i = 0; i < row_width; i++) |
| 2182 | { |
| 2183 | *dp = (png_byte)((*sp >> shift) & 0x03); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2184 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2185 | if (shift == 6) |
| 2186 | { |
| 2187 | shift = 0; |
| 2188 | sp--; |
| 2189 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2190 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2191 | else |
| 2192 | shift += 2; |
| 2193 | |
| 2194 | dp--; |
| 2195 | } |
| 2196 | break; |
| 2197 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2198 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2199 | case 4: |
| 2200 | { |
| 2201 | png_bytep sp = row + (png_size_t)((row_width - 1) >> 1); |
| 2202 | png_bytep dp = row + (png_size_t)row_width - 1; |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 2203 | png_uint_32 shift = ((1U - ((row_width + 1U) & 0x01)) << 2); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2204 | for (i = 0; i < row_width; i++) |
| 2205 | { |
| 2206 | *dp = (png_byte)((*sp >> shift) & 0x0f); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2207 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2208 | if (shift == 4) |
| 2209 | { |
| 2210 | shift = 0; |
| 2211 | sp--; |
| 2212 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2213 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2214 | else |
| 2215 | shift = 4; |
| 2216 | |
| 2217 | dp--; |
| 2218 | } |
| 2219 | break; |
| 2220 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2221 | |
| 2222 | default: |
| 2223 | break; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2224 | } |
| 2225 | row_info->bit_depth = 8; |
| 2226 | row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
| 2227 | row_info->rowbytes = row_width * row_info->channels; |
| 2228 | } |
| 2229 | } |
| 2230 | #endif |
| 2231 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2232 | #ifdef PNG_READ_SHIFT_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2233 | /* Reverse the effects of png_do_shift. This routine merely shifts the |
| 2234 | * pixels back to their significant bits values. Thus, if you have |
| 2235 | * a row of bit depth 8, but only 5 are significant, this will shift |
| 2236 | * the values back to 0 through 31. |
| 2237 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2238 | static void |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2239 | png_do_unshift(png_row_infop row_info, png_bytep row, |
| 2240 | png_const_color_8p sig_bits) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2241 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2242 | int color_type; |
| 2243 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2244 | png_debug(1, "in png_do_unshift"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2245 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2246 | /* The palette case has already been handled in the _init routine. */ |
| 2247 | color_type = row_info->color_type; |
| 2248 | |
| 2249 | if (color_type != PNG_COLOR_TYPE_PALETTE) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2250 | { |
| 2251 | int shift[4]; |
| 2252 | int channels = 0; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2253 | int bit_depth = row_info->bit_depth; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2254 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2255 | if ((color_type & PNG_COLOR_MASK_COLOR) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2256 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2257 | shift[channels++] = bit_depth - sig_bits->red; |
| 2258 | shift[channels++] = bit_depth - sig_bits->green; |
| 2259 | shift[channels++] = bit_depth - sig_bits->blue; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2260 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2261 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2262 | else |
| 2263 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2264 | shift[channels++] = bit_depth - sig_bits->gray; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2265 | } |
| 2266 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2267 | if ((color_type & PNG_COLOR_MASK_ALPHA) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2268 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2269 | shift[channels++] = bit_depth - sig_bits->alpha; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2270 | } |
| 2271 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2272 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2273 | int c, have_shift; |
| 2274 | |
| 2275 | for (c = have_shift = 0; c < channels; ++c) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2276 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2277 | /* A shift of more than the bit depth is an error condition but it |
| 2278 | * gets ignored here. |
| 2279 | */ |
| 2280 | if (shift[c] <= 0 || shift[c] >= bit_depth) |
| 2281 | shift[c] = 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2282 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2283 | else |
| 2284 | have_shift = 1; |
| 2285 | } |
| 2286 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2287 | if (have_shift == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2288 | return; |
| 2289 | } |
| 2290 | |
| 2291 | switch (bit_depth) |
| 2292 | { |
| 2293 | default: |
| 2294 | /* Must be 1bpp gray: should not be here! */ |
| 2295 | /* NOTREACHED */ |
| 2296 | break; |
| 2297 | |
| 2298 | case 2: |
| 2299 | /* Must be 2bpp gray */ |
| 2300 | /* assert(channels == 1 && shift[0] == 1) */ |
| 2301 | { |
| 2302 | png_bytep bp = row; |
| 2303 | png_bytep bp_end = bp + row_info->rowbytes; |
| 2304 | |
| 2305 | while (bp < bp_end) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2306 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2307 | int b = (*bp >> 1) & 0x55; |
| 2308 | *bp++ = (png_byte)b; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2309 | } |
| 2310 | break; |
| 2311 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2312 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2313 | case 4: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2314 | /* Must be 4bpp gray */ |
| 2315 | /* assert(channels == 1) */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2316 | { |
| 2317 | png_bytep bp = row; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2318 | png_bytep bp_end = bp + row_info->rowbytes; |
| 2319 | int gray_shift = shift[0]; |
| 2320 | int mask = 0xf >> gray_shift; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2321 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2322 | mask |= mask << 4; |
| 2323 | |
| 2324 | while (bp < bp_end) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2325 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2326 | int b = (*bp >> gray_shift) & mask; |
| 2327 | *bp++ = (png_byte)b; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2328 | } |
| 2329 | break; |
| 2330 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2331 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2332 | case 8: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2333 | /* Single byte components, G, GA, RGB, RGBA */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2334 | { |
| 2335 | png_bytep bp = row; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2336 | png_bytep bp_end = bp + row_info->rowbytes; |
| 2337 | int channel = 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2338 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2339 | while (bp < bp_end) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2340 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2341 | int b = *bp >> shift[channel]; |
| 2342 | if (++channel >= channels) |
| 2343 | channel = 0; |
| 2344 | *bp++ = (png_byte)b; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2345 | } |
| 2346 | break; |
| 2347 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2348 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2349 | #ifdef PNG_READ_16BIT_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2350 | case 16: |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2351 | /* Double byte components, G, GA, RGB, RGBA */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2352 | { |
| 2353 | png_bytep bp = row; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2354 | png_bytep bp_end = bp + row_info->rowbytes; |
| 2355 | int channel = 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2356 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2357 | while (bp < bp_end) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2358 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2359 | int value = (bp[0] << 8) + bp[1]; |
| 2360 | |
| 2361 | value >>= shift[channel]; |
| 2362 | if (++channel >= channels) |
| 2363 | channel = 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2364 | *bp++ = (png_byte)(value >> 8); |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2365 | *bp++ = (png_byte)value; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2366 | } |
| 2367 | break; |
| 2368 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2369 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | #endif |
| 2374 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2375 | #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
| 2376 | /* Scale rows of bit depth 16 down to 8 accurately */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2377 | static void |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2378 | png_do_scale_16_to_8(png_row_infop row_info, png_bytep row) |
| 2379 | { |
| 2380 | png_debug(1, "in png_do_scale_16_to_8"); |
| 2381 | |
| 2382 | if (row_info->bit_depth == 16) |
| 2383 | { |
| 2384 | png_bytep sp = row; /* source */ |
| 2385 | png_bytep dp = row; /* destination */ |
| 2386 | png_bytep ep = sp + row_info->rowbytes; /* end+1 */ |
| 2387 | |
| 2388 | while (sp < ep) |
| 2389 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2390 | /* The input is an array of 16-bit components, these must be scaled to |
| 2391 | * 8 bits each. For a 16-bit value V the required value (from the PNG |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2392 | * specification) is: |
| 2393 | * |
| 2394 | * (V * 255) / 65535 |
| 2395 | * |
| 2396 | * This reduces to round(V / 257), or floor((V + 128.5)/257) |
| 2397 | * |
| 2398 | * Represent V as the two byte value vhi.vlo. Make a guess that the |
| 2399 | * result is the top byte of V, vhi, then the correction to this value |
| 2400 | * is: |
| 2401 | * |
| 2402 | * error = floor(((V-vhi.vhi) + 128.5) / 257) |
| 2403 | * = floor(((vlo-vhi) + 128.5) / 257) |
| 2404 | * |
| 2405 | * This can be approximated using integer arithmetic (and a signed |
| 2406 | * shift): |
| 2407 | * |
| 2408 | * error = (vlo-vhi+128) >> 8; |
| 2409 | * |
| 2410 | * The approximate differs from the exact answer only when (vlo-vhi) is |
| 2411 | * 128; it then gives a correction of +1 when the exact correction is |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2412 | * 0. This gives 128 errors. The exact answer (correct for all 16-bit |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2413 | * input values) is: |
| 2414 | * |
| 2415 | * error = (vlo-vhi+128)*65535 >> 24; |
| 2416 | * |
| 2417 | * An alternative arithmetic calculation which also gives no errors is: |
| 2418 | * |
| 2419 | * (V * 255 + 32895) >> 16 |
| 2420 | */ |
| 2421 | |
| 2422 | png_int_32 tmp = *sp++; /* must be signed! */ |
| 2423 | tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24; |
| 2424 | *dp++ = (png_byte)tmp; |
| 2425 | } |
| 2426 | |
| 2427 | row_info->bit_depth = 8; |
| 2428 | row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
| 2429 | row_info->rowbytes = row_info->width * row_info->channels; |
| 2430 | } |
| 2431 | } |
| 2432 | #endif |
| 2433 | |
| 2434 | #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2435 | static void |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2436 | /* Simply discard the low byte. This was the default behavior prior |
| 2437 | * to libpng-1.5.4. |
| 2438 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2439 | png_do_chop(png_row_infop row_info, png_bytep row) |
| 2440 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2441 | png_debug(1, "in png_do_chop"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2442 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2443 | if (row_info->bit_depth == 16) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2444 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2445 | png_bytep sp = row; /* source */ |
| 2446 | png_bytep dp = row; /* destination */ |
| 2447 | png_bytep ep = sp + row_info->rowbytes; /* end+1 */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2448 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2449 | while (sp < ep) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2450 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2451 | *dp++ = *sp; |
| 2452 | sp += 2; /* skip low byte */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2453 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2454 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2455 | row_info->bit_depth = 8; |
| 2456 | row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
| 2457 | row_info->rowbytes = row_info->width * row_info->channels; |
| 2458 | } |
| 2459 | } |
| 2460 | #endif |
| 2461 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2462 | #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2463 | static void |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2464 | png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) |
| 2465 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2466 | png_debug(1, "in png_do_read_swap_alpha"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2467 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2468 | { |
| 2469 | png_uint_32 row_width = row_info->width; |
| 2470 | if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 2471 | { |
| 2472 | /* This converts from RGBA to ARGB */ |
| 2473 | if (row_info->bit_depth == 8) |
| 2474 | { |
| 2475 | png_bytep sp = row + row_info->rowbytes; |
| 2476 | png_bytep dp = sp; |
| 2477 | png_byte save; |
| 2478 | png_uint_32 i; |
| 2479 | |
| 2480 | for (i = 0; i < row_width; i++) |
| 2481 | { |
| 2482 | save = *(--sp); |
| 2483 | *(--dp) = *(--sp); |
| 2484 | *(--dp) = *(--sp); |
| 2485 | *(--dp) = *(--sp); |
| 2486 | *(--dp) = save; |
| 2487 | } |
| 2488 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2489 | |
| 2490 | #ifdef PNG_READ_16BIT_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2491 | /* This converts from RRGGBBAA to AARRGGBB */ |
| 2492 | else |
| 2493 | { |
| 2494 | png_bytep sp = row + row_info->rowbytes; |
| 2495 | png_bytep dp = sp; |
| 2496 | png_byte save[2]; |
| 2497 | png_uint_32 i; |
| 2498 | |
| 2499 | for (i = 0; i < row_width; i++) |
| 2500 | { |
| 2501 | save[0] = *(--sp); |
| 2502 | save[1] = *(--sp); |
| 2503 | *(--dp) = *(--sp); |
| 2504 | *(--dp) = *(--sp); |
| 2505 | *(--dp) = *(--sp); |
| 2506 | *(--dp) = *(--sp); |
| 2507 | *(--dp) = *(--sp); |
| 2508 | *(--dp) = *(--sp); |
| 2509 | *(--dp) = save[0]; |
| 2510 | *(--dp) = save[1]; |
| 2511 | } |
| 2512 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2513 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2514 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2515 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2516 | else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 2517 | { |
| 2518 | /* This converts from GA to AG */ |
| 2519 | if (row_info->bit_depth == 8) |
| 2520 | { |
| 2521 | png_bytep sp = row + row_info->rowbytes; |
| 2522 | png_bytep dp = sp; |
| 2523 | png_byte save; |
| 2524 | png_uint_32 i; |
| 2525 | |
| 2526 | for (i = 0; i < row_width; i++) |
| 2527 | { |
| 2528 | save = *(--sp); |
| 2529 | *(--dp) = *(--sp); |
| 2530 | *(--dp) = save; |
| 2531 | } |
| 2532 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2533 | |
| 2534 | #ifdef PNG_READ_16BIT_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2535 | /* This converts from GGAA to AAGG */ |
| 2536 | else |
| 2537 | { |
| 2538 | png_bytep sp = row + row_info->rowbytes; |
| 2539 | png_bytep dp = sp; |
| 2540 | png_byte save[2]; |
| 2541 | png_uint_32 i; |
| 2542 | |
| 2543 | for (i = 0; i < row_width; i++) |
| 2544 | { |
| 2545 | save[0] = *(--sp); |
| 2546 | save[1] = *(--sp); |
| 2547 | *(--dp) = *(--sp); |
| 2548 | *(--dp) = *(--sp); |
| 2549 | *(--dp) = save[0]; |
| 2550 | *(--dp) = save[1]; |
| 2551 | } |
| 2552 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2553 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2554 | } |
| 2555 | } |
| 2556 | } |
| 2557 | #endif |
| 2558 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2559 | #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2560 | static void |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2561 | png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) |
| 2562 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2563 | png_uint_32 row_width; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2564 | png_debug(1, "in png_do_read_invert_alpha"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2565 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2566 | row_width = row_info->width; |
| 2567 | if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2568 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2569 | if (row_info->bit_depth == 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2570 | { |
| 2571 | /* This inverts the alpha channel in RGBA */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2572 | png_bytep sp = row + row_info->rowbytes; |
| 2573 | png_bytep dp = sp; |
| 2574 | png_uint_32 i; |
| 2575 | |
| 2576 | for (i = 0; i < row_width; i++) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2577 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2578 | *(--dp) = (png_byte)(255 - *(--sp)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2579 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2580 | /* This does nothing: |
| 2581 | *(--dp) = *(--sp); |
| 2582 | *(--dp) = *(--sp); |
| 2583 | *(--dp) = *(--sp); |
| 2584 | We can replace it with: |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2585 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2586 | sp-=3; |
| 2587 | dp=sp; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2588 | } |
| 2589 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2590 | |
| 2591 | #ifdef PNG_READ_16BIT_SUPPORTED |
| 2592 | /* This inverts the alpha channel in RRGGBBAA */ |
| 2593 | else |
| 2594 | { |
| 2595 | png_bytep sp = row + row_info->rowbytes; |
| 2596 | png_bytep dp = sp; |
| 2597 | png_uint_32 i; |
| 2598 | |
| 2599 | for (i = 0; i < row_width; i++) |
| 2600 | { |
| 2601 | *(--dp) = (png_byte)(255 - *(--sp)); |
| 2602 | *(--dp) = (png_byte)(255 - *(--sp)); |
| 2603 | |
| 2604 | /* This does nothing: |
| 2605 | *(--dp) = *(--sp); |
| 2606 | *(--dp) = *(--sp); |
| 2607 | *(--dp) = *(--sp); |
| 2608 | *(--dp) = *(--sp); |
| 2609 | *(--dp) = *(--sp); |
| 2610 | *(--dp) = *(--sp); |
| 2611 | We can replace it with: |
| 2612 | */ |
| 2613 | sp-=6; |
| 2614 | dp=sp; |
| 2615 | } |
| 2616 | } |
| 2617 | #endif |
| 2618 | } |
| 2619 | else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 2620 | { |
| 2621 | if (row_info->bit_depth == 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2622 | { |
| 2623 | /* This inverts the alpha channel in GA */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2624 | png_bytep sp = row + row_info->rowbytes; |
| 2625 | png_bytep dp = sp; |
| 2626 | png_uint_32 i; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2627 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2628 | for (i = 0; i < row_width; i++) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2629 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2630 | *(--dp) = (png_byte)(255 - *(--sp)); |
| 2631 | *(--dp) = *(--sp); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2632 | } |
| 2633 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2634 | |
| 2635 | #ifdef PNG_READ_16BIT_SUPPORTED |
| 2636 | else |
| 2637 | { |
| 2638 | /* This inverts the alpha channel in GGAA */ |
| 2639 | png_bytep sp = row + row_info->rowbytes; |
| 2640 | png_bytep dp = sp; |
| 2641 | png_uint_32 i; |
| 2642 | |
| 2643 | for (i = 0; i < row_width; i++) |
| 2644 | { |
| 2645 | *(--dp) = (png_byte)(255 - *(--sp)); |
| 2646 | *(--dp) = (png_byte)(255 - *(--sp)); |
| 2647 | /* |
| 2648 | *(--dp) = *(--sp); |
| 2649 | *(--dp) = *(--sp); |
| 2650 | */ |
| 2651 | sp-=2; |
| 2652 | dp=sp; |
| 2653 | } |
| 2654 | } |
| 2655 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2656 | } |
| 2657 | } |
| 2658 | #endif |
| 2659 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2660 | #ifdef PNG_READ_FILLER_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2661 | /* Add filler channel if we have RGB color */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2662 | static void |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2663 | png_do_read_filler(png_row_infop row_info, png_bytep row, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2664 | png_uint_32 filler, png_uint_32 flags) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2665 | { |
| 2666 | png_uint_32 i; |
| 2667 | png_uint_32 row_width = row_info->width; |
| 2668 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2669 | #ifdef PNG_READ_16BIT_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2670 | png_byte hi_filler = (png_byte)(filler>>8); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2671 | #endif |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2672 | png_byte lo_filler = (png_byte)filler; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2673 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2674 | png_debug(1, "in png_do_read_filler"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2675 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2676 | if ( |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2677 | row_info->color_type == PNG_COLOR_TYPE_GRAY) |
| 2678 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2679 | if (row_info->bit_depth == 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2680 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2681 | if ((flags & PNG_FLAG_FILLER_AFTER) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2682 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2683 | /* This changes the data from G to GX */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2684 | png_bytep sp = row + (png_size_t)row_width; |
| 2685 | png_bytep dp = sp + (png_size_t)row_width; |
| 2686 | for (i = 1; i < row_width; i++) |
| 2687 | { |
| 2688 | *(--dp) = lo_filler; |
| 2689 | *(--dp) = *(--sp); |
| 2690 | } |
| 2691 | *(--dp) = lo_filler; |
| 2692 | row_info->channels = 2; |
| 2693 | row_info->pixel_depth = 16; |
| 2694 | row_info->rowbytes = row_width * 2; |
| 2695 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2696 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2697 | else |
| 2698 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2699 | /* This changes the data from G to XG */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2700 | png_bytep sp = row + (png_size_t)row_width; |
| 2701 | png_bytep dp = sp + (png_size_t)row_width; |
| 2702 | for (i = 0; i < row_width; i++) |
| 2703 | { |
| 2704 | *(--dp) = *(--sp); |
| 2705 | *(--dp) = lo_filler; |
| 2706 | } |
| 2707 | row_info->channels = 2; |
| 2708 | row_info->pixel_depth = 16; |
| 2709 | row_info->rowbytes = row_width * 2; |
| 2710 | } |
| 2711 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2712 | |
| 2713 | #ifdef PNG_READ_16BIT_SUPPORTED |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2714 | else if (row_info->bit_depth == 16) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2715 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2716 | if ((flags & PNG_FLAG_FILLER_AFTER) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2717 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2718 | /* This changes the data from GG to GGXX */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2719 | png_bytep sp = row + (png_size_t)row_width * 2; |
| 2720 | png_bytep dp = sp + (png_size_t)row_width * 2; |
| 2721 | for (i = 1; i < row_width; i++) |
| 2722 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2723 | *(--dp) = lo_filler; |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2724 | *(--dp) = hi_filler; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2725 | *(--dp) = *(--sp); |
| 2726 | *(--dp) = *(--sp); |
| 2727 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2728 | *(--dp) = lo_filler; |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2729 | *(--dp) = hi_filler; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2730 | row_info->channels = 2; |
| 2731 | row_info->pixel_depth = 32; |
| 2732 | row_info->rowbytes = row_width * 4; |
| 2733 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2734 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2735 | else |
| 2736 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2737 | /* This changes the data from GG to XXGG */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2738 | png_bytep sp = row + (png_size_t)row_width * 2; |
| 2739 | png_bytep dp = sp + (png_size_t)row_width * 2; |
| 2740 | for (i = 0; i < row_width; i++) |
| 2741 | { |
| 2742 | *(--dp) = *(--sp); |
| 2743 | *(--dp) = *(--sp); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2744 | *(--dp) = lo_filler; |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2745 | *(--dp) = hi_filler; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2746 | } |
| 2747 | row_info->channels = 2; |
| 2748 | row_info->pixel_depth = 32; |
| 2749 | row_info->rowbytes = row_width * 4; |
| 2750 | } |
| 2751 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2752 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2753 | } /* COLOR_TYPE == GRAY */ |
| 2754 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 2755 | { |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2756 | if (row_info->bit_depth == 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2757 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2758 | if ((flags & PNG_FLAG_FILLER_AFTER) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2759 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2760 | /* This changes the data from RGB to RGBX */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2761 | png_bytep sp = row + (png_size_t)row_width * 3; |
| 2762 | png_bytep dp = sp + (png_size_t)row_width; |
| 2763 | for (i = 1; i < row_width; i++) |
| 2764 | { |
| 2765 | *(--dp) = lo_filler; |
| 2766 | *(--dp) = *(--sp); |
| 2767 | *(--dp) = *(--sp); |
| 2768 | *(--dp) = *(--sp); |
| 2769 | } |
| 2770 | *(--dp) = lo_filler; |
| 2771 | row_info->channels = 4; |
| 2772 | row_info->pixel_depth = 32; |
| 2773 | row_info->rowbytes = row_width * 4; |
| 2774 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2775 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2776 | else |
| 2777 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2778 | /* This changes the data from RGB to XRGB */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2779 | png_bytep sp = row + (png_size_t)row_width * 3; |
| 2780 | png_bytep dp = sp + (png_size_t)row_width; |
| 2781 | for (i = 0; i < row_width; i++) |
| 2782 | { |
| 2783 | *(--dp) = *(--sp); |
| 2784 | *(--dp) = *(--sp); |
| 2785 | *(--dp) = *(--sp); |
| 2786 | *(--dp) = lo_filler; |
| 2787 | } |
| 2788 | row_info->channels = 4; |
| 2789 | row_info->pixel_depth = 32; |
| 2790 | row_info->rowbytes = row_width * 4; |
| 2791 | } |
| 2792 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2793 | |
| 2794 | #ifdef PNG_READ_16BIT_SUPPORTED |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2795 | else if (row_info->bit_depth == 16) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2796 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2797 | if ((flags & PNG_FLAG_FILLER_AFTER) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2798 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2799 | /* This changes the data from RRGGBB to RRGGBBXX */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2800 | png_bytep sp = row + (png_size_t)row_width * 6; |
| 2801 | png_bytep dp = sp + (png_size_t)row_width * 2; |
| 2802 | for (i = 1; i < row_width; i++) |
| 2803 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2804 | *(--dp) = lo_filler; |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2805 | *(--dp) = hi_filler; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2806 | *(--dp) = *(--sp); |
| 2807 | *(--dp) = *(--sp); |
| 2808 | *(--dp) = *(--sp); |
| 2809 | *(--dp) = *(--sp); |
| 2810 | *(--dp) = *(--sp); |
| 2811 | *(--dp) = *(--sp); |
| 2812 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2813 | *(--dp) = lo_filler; |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2814 | *(--dp) = hi_filler; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2815 | row_info->channels = 4; |
| 2816 | row_info->pixel_depth = 64; |
| 2817 | row_info->rowbytes = row_width * 8; |
| 2818 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2819 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2820 | else |
| 2821 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2822 | /* This changes the data from RRGGBB to XXRRGGBB */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2823 | png_bytep sp = row + (png_size_t)row_width * 6; |
| 2824 | png_bytep dp = sp + (png_size_t)row_width * 2; |
| 2825 | for (i = 0; i < row_width; i++) |
| 2826 | { |
| 2827 | *(--dp) = *(--sp); |
| 2828 | *(--dp) = *(--sp); |
| 2829 | *(--dp) = *(--sp); |
| 2830 | *(--dp) = *(--sp); |
| 2831 | *(--dp) = *(--sp); |
| 2832 | *(--dp) = *(--sp); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2833 | *(--dp) = lo_filler; |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2834 | *(--dp) = hi_filler; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2835 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2836 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2837 | row_info->channels = 4; |
| 2838 | row_info->pixel_depth = 64; |
| 2839 | row_info->rowbytes = row_width * 8; |
| 2840 | } |
| 2841 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2842 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2843 | } /* COLOR_TYPE == RGB */ |
| 2844 | } |
| 2845 | #endif |
| 2846 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2847 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2848 | /* Expand grayscale files to RGB, with or without alpha */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2849 | static void |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2850 | png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) |
| 2851 | { |
| 2852 | png_uint_32 i; |
| 2853 | png_uint_32 row_width = row_info->width; |
| 2854 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2855 | png_debug(1, "in png_do_gray_to_rgb"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2856 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2857 | if (row_info->bit_depth >= 8 && |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2858 | (row_info->color_type & PNG_COLOR_MASK_COLOR) == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2859 | { |
| 2860 | if (row_info->color_type == PNG_COLOR_TYPE_GRAY) |
| 2861 | { |
| 2862 | if (row_info->bit_depth == 8) |
| 2863 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2864 | /* This changes G to RGB */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2865 | png_bytep sp = row + (png_size_t)row_width - 1; |
| 2866 | png_bytep dp = sp + (png_size_t)row_width * 2; |
| 2867 | for (i = 0; i < row_width; i++) |
| 2868 | { |
| 2869 | *(dp--) = *sp; |
| 2870 | *(dp--) = *sp; |
| 2871 | *(dp--) = *(sp--); |
| 2872 | } |
| 2873 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2874 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2875 | else |
| 2876 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2877 | /* This changes GG to RRGGBB */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2878 | png_bytep sp = row + (png_size_t)row_width * 2 - 1; |
| 2879 | png_bytep dp = sp + (png_size_t)row_width * 4; |
| 2880 | for (i = 0; i < row_width; i++) |
| 2881 | { |
| 2882 | *(dp--) = *sp; |
| 2883 | *(dp--) = *(sp - 1); |
| 2884 | *(dp--) = *sp; |
| 2885 | *(dp--) = *(sp - 1); |
| 2886 | *(dp--) = *(sp--); |
| 2887 | *(dp--) = *(sp--); |
| 2888 | } |
| 2889 | } |
| 2890 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2891 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2892 | else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 2893 | { |
| 2894 | if (row_info->bit_depth == 8) |
| 2895 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2896 | /* This changes GA to RGBA */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2897 | png_bytep sp = row + (png_size_t)row_width * 2 - 1; |
| 2898 | png_bytep dp = sp + (png_size_t)row_width * 2; |
| 2899 | for (i = 0; i < row_width; i++) |
| 2900 | { |
| 2901 | *(dp--) = *(sp--); |
| 2902 | *(dp--) = *sp; |
| 2903 | *(dp--) = *sp; |
| 2904 | *(dp--) = *(sp--); |
| 2905 | } |
| 2906 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2907 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2908 | else |
| 2909 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2910 | /* This changes GGAA to RRGGBBAA */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2911 | png_bytep sp = row + (png_size_t)row_width * 4 - 1; |
| 2912 | png_bytep dp = sp + (png_size_t)row_width * 4; |
| 2913 | for (i = 0; i < row_width; i++) |
| 2914 | { |
| 2915 | *(dp--) = *(sp--); |
| 2916 | *(dp--) = *(sp--); |
| 2917 | *(dp--) = *sp; |
| 2918 | *(dp--) = *(sp - 1); |
| 2919 | *(dp--) = *sp; |
| 2920 | *(dp--) = *(sp - 1); |
| 2921 | *(dp--) = *(sp--); |
| 2922 | *(dp--) = *(sp--); |
| 2923 | } |
| 2924 | } |
| 2925 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2926 | row_info->channels = (png_byte)(row_info->channels + 2); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2927 | row_info->color_type |= PNG_COLOR_MASK_COLOR; |
| 2928 | row_info->pixel_depth = (png_byte)(row_info->channels * |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2929 | row_info->bit_depth); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2930 | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2931 | } |
| 2932 | } |
| 2933 | #endif |
| 2934 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2935 | #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 2936 | /* Reduce RGB files to grayscale, with or without alpha |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2937 | * using the equation given in Poynton's ColorFAQ of 1998-01-04 at |
| 2938 | * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008 but |
| 2939 | * versions dated 1998 through November 2002 have been archived at |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 2940 | * https://web.archive.org/web/20000816232553/www.inforamp.net/ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2941 | * ~poynton/notes/colour_and_gamma/ColorFAQ.txt ) |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2942 | * Charles Poynton poynton at poynton.com |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2943 | * |
| 2944 | * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B |
| 2945 | * |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2946 | * which can be expressed with integers as |
| 2947 | * |
| 2948 | * Y = (6969 * R + 23434 * G + 2365 * B)/32768 |
| 2949 | * |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2950 | * Poynton's current link (as of January 2003 through July 2011): |
| 2951 | * <http://www.poynton.com/notes/colour_and_gamma/> |
| 2952 | * has changed the numbers slightly: |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2953 | * |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2954 | * Y = 0.2126*R + 0.7152*G + 0.0722*B |
| 2955 | * |
| 2956 | * which can be expressed with integers as |
| 2957 | * |
| 2958 | * Y = (6966 * R + 23436 * G + 2366 * B)/32768 |
| 2959 | * |
| 2960 | * Historically, however, libpng uses numbers derived from the ITU-R Rec 709 |
| 2961 | * end point chromaticities and the D65 white point. Depending on the |
| 2962 | * precision used for the D65 white point this produces a variety of different |
| 2963 | * numbers, however if the four decimal place value used in ITU-R Rec 709 is |
| 2964 | * used (0.3127,0.3290) the Y calculation would be: |
| 2965 | * |
| 2966 | * Y = (6968 * R + 23435 * G + 2366 * B)/32768 |
| 2967 | * |
| 2968 | * While this is correct the rounding results in an overflow for white, because |
| 2969 | * the sum of the rounded coefficients is 32769, not 32768. Consequently |
| 2970 | * libpng uses, instead, the closest non-overflowing approximation: |
| 2971 | * |
| 2972 | * Y = (6968 * R + 23434 * G + 2366 * B)/32768 |
| 2973 | * |
| 2974 | * Starting with libpng-1.5.5, if the image being converted has a cHRM chunk |
| 2975 | * (including an sRGB chunk) then the chromaticities are used to calculate the |
| 2976 | * coefficients. See the chunk handling in pngrutil.c for more information. |
| 2977 | * |
| 2978 | * In all cases the calculation is to be done in a linear colorspace. If no |
| 2979 | * gamma information is available to correct the encoding of the original RGB |
| 2980 | * values this results in an implicit assumption that the original PNG RGB |
| 2981 | * values were linear. |
| 2982 | * |
| 2983 | * Other integer coefficents can be used via png_set_rgb_to_gray(). Because |
| 2984 | * the API takes just red and green coefficients the blue coefficient is |
| 2985 | * calculated to make the sum 32768. This will result in different rounding |
| 2986 | * to that used above. |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2987 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 2988 | static int |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2989 | png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2990 | |
| 2991 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2992 | int rgb_error = 0; |
| 2993 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 2994 | png_debug(1, "in png_do_rgb_to_gray"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 2995 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 2996 | if ((row_info->color_type & PNG_COLOR_MASK_PALETTE) == 0 && |
| 2997 | (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2998 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 2999 | PNG_CONST png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff; |
| 3000 | PNG_CONST png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff; |
| 3001 | PNG_CONST png_uint_32 bc = 32768 - rc - gc; |
| 3002 | PNG_CONST png_uint_32 row_width = row_info->width; |
| 3003 | PNG_CONST int have_alpha = |
| 3004 | (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3005 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3006 | if (row_info->bit_depth == 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3007 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3008 | #ifdef PNG_READ_GAMMA_SUPPORTED |
| 3009 | /* Notice that gamma to/from 1 are not necessarily inverses (if |
| 3010 | * there is an overall gamma correction). Prior to 1.5.5 this code |
| 3011 | * checked the linearized values for equality; this doesn't match |
| 3012 | * the documentation, the original values must be checked. |
| 3013 | */ |
| 3014 | if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3015 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3016 | png_bytep sp = row; |
| 3017 | png_bytep dp = row; |
| 3018 | png_uint_32 i; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3019 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3020 | for (i = 0; i < row_width; i++) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3021 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3022 | png_byte red = *(sp++); |
| 3023 | png_byte green = *(sp++); |
| 3024 | png_byte blue = *(sp++); |
| 3025 | |
| 3026 | if (red != green || red != blue) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3027 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3028 | red = png_ptr->gamma_to_1[red]; |
| 3029 | green = png_ptr->gamma_to_1[green]; |
| 3030 | blue = png_ptr->gamma_to_1[blue]; |
| 3031 | |
| 3032 | rgb_error |= 1; |
| 3033 | *(dp++) = png_ptr->gamma_from_1[ |
| 3034 | (rc*red + gc*green + bc*blue + 16384)>>15]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3035 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3036 | |
| 3037 | else |
| 3038 | { |
| 3039 | /* If there is no overall correction the table will not be |
| 3040 | * set. |
| 3041 | */ |
| 3042 | if (png_ptr->gamma_table != NULL) |
| 3043 | red = png_ptr->gamma_table[red]; |
| 3044 | |
| 3045 | *(dp++) = red; |
| 3046 | } |
| 3047 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3048 | if (have_alpha != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3049 | *(dp++) = *(sp++); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3050 | } |
| 3051 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3052 | else |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3053 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3054 | { |
| 3055 | png_bytep sp = row; |
| 3056 | png_bytep dp = row; |
| 3057 | png_uint_32 i; |
| 3058 | |
| 3059 | for (i = 0; i < row_width; i++) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3060 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3061 | png_byte red = *(sp++); |
| 3062 | png_byte green = *(sp++); |
| 3063 | png_byte blue = *(sp++); |
| 3064 | |
| 3065 | if (red != green || red != blue) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3066 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3067 | rgb_error |= 1; |
| 3068 | /* NOTE: this is the historical approach which simply |
| 3069 | * truncates the results. |
| 3070 | */ |
| 3071 | *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3072 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3073 | |
| 3074 | else |
| 3075 | *(dp++) = red; |
| 3076 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3077 | if (have_alpha != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3078 | *(dp++) = *(sp++); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3079 | } |
| 3080 | } |
| 3081 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3082 | |
| 3083 | else /* RGB bit_depth == 16 */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3084 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3085 | #ifdef PNG_READ_GAMMA_SUPPORTED |
| 3086 | if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3087 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3088 | png_bytep sp = row; |
| 3089 | png_bytep dp = row; |
| 3090 | png_uint_32 i; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3091 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3092 | for (i = 0; i < row_width; i++) |
| 3093 | { |
| 3094 | png_uint_16 red, green, blue, w; |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3095 | png_byte hi,lo; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3096 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3097 | hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo)); |
| 3098 | hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo)); |
| 3099 | hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo)); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3100 | |
| 3101 | if (red == green && red == blue) |
| 3102 | { |
| 3103 | if (png_ptr->gamma_16_table != NULL) |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3104 | w = png_ptr->gamma_16_table[(red & 0xff) |
| 3105 | >> png_ptr->gamma_shift][red >> 8]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3106 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3107 | else |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3108 | w = red; |
| 3109 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3110 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3111 | else |
| 3112 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3113 | png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red & 0xff) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3114 | >> png_ptr->gamma_shift][red>>8]; |
| 3115 | png_uint_16 green_1 = |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3116 | png_ptr->gamma_16_to_1[(green & 0xff) >> |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3117 | png_ptr->gamma_shift][green>>8]; |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3118 | png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue & 0xff) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3119 | >> png_ptr->gamma_shift][blue>>8]; |
| 3120 | png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1 |
| 3121 | + bc*blue_1 + 16384)>>15); |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3122 | w = png_ptr->gamma_16_from_1[(gray16 & 0xff) >> |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3123 | png_ptr->gamma_shift][gray16 >> 8]; |
| 3124 | rgb_error |= 1; |
| 3125 | } |
| 3126 | |
| 3127 | *(dp++) = (png_byte)((w>>8) & 0xff); |
| 3128 | *(dp++) = (png_byte)(w & 0xff); |
| 3129 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3130 | if (have_alpha != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3131 | { |
| 3132 | *(dp++) = *(sp++); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3133 | *(dp++) = *(sp++); |
| 3134 | } |
| 3135 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3136 | } |
| 3137 | else |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3138 | #endif |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3139 | { |
| 3140 | png_bytep sp = row; |
| 3141 | png_bytep dp = row; |
| 3142 | png_uint_32 i; |
| 3143 | |
| 3144 | for (i = 0; i < row_width; i++) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3145 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3146 | png_uint_16 red, green, blue, gray16; |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3147 | png_byte hi,lo; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3148 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3149 | hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo)); |
| 3150 | hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo)); |
| 3151 | hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo)); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3152 | |
| 3153 | if (red != green || red != blue) |
| 3154 | rgb_error |= 1; |
| 3155 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3156 | /* From 1.5.5 in the 16-bit case do the accurate conversion even |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3157 | * in the 'fast' case - this is because this is where the code |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3158 | * ends up when handling linear 16-bit data. |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3159 | */ |
| 3160 | gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >> |
| 3161 | 15); |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3162 | *(dp++) = (png_byte)((gray16 >> 8) & 0xff); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3163 | *(dp++) = (png_byte)(gray16 & 0xff); |
| 3164 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3165 | if (have_alpha != 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3166 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3167 | *(dp++) = *(sp++); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3168 | *(dp++) = *(sp++); |
| 3169 | } |
| 3170 | } |
| 3171 | } |
| 3172 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3173 | |
| 3174 | row_info->channels = (png_byte)(row_info->channels - 2); |
| 3175 | row_info->color_type = (png_byte)(row_info->color_type & |
| 3176 | ~PNG_COLOR_MASK_COLOR); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3177 | row_info->pixel_depth = (png_byte)(row_info->channels * |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3178 | row_info->bit_depth); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3179 | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3180 | } |
| 3181 | return rgb_error; |
| 3182 | } |
| 3183 | #endif |
| 3184 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3185 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ |
| 3186 | defined(PNG_READ_ALPHA_MODE_SUPPORTED) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3187 | /* Replace any alpha or transparency with the supplied background color. |
| 3188 | * "background" is already in the screen gamma, while "background_1" is |
| 3189 | * at a gamma of 1.0. Paletted files have already been taken care of. |
| 3190 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 3191 | static void |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3192 | png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3193 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3194 | #ifdef PNG_READ_GAMMA_SUPPORTED |
| 3195 | png_const_bytep gamma_table = png_ptr->gamma_table; |
| 3196 | png_const_bytep gamma_from_1 = png_ptr->gamma_from_1; |
| 3197 | png_const_bytep gamma_to_1 = png_ptr->gamma_to_1; |
| 3198 | png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table; |
| 3199 | png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1; |
| 3200 | png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1; |
| 3201 | int gamma_shift = png_ptr->gamma_shift; |
| 3202 | int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0; |
| 3203 | #endif |
| 3204 | |
| 3205 | png_bytep sp; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3206 | png_uint_32 i; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3207 | png_uint_32 row_width = row_info->width; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3208 | int shift; |
| 3209 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3210 | png_debug(1, "in png_do_compose"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3211 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3212 | { |
| 3213 | switch (row_info->color_type) |
| 3214 | { |
| 3215 | case PNG_COLOR_TYPE_GRAY: |
| 3216 | { |
| 3217 | switch (row_info->bit_depth) |
| 3218 | { |
| 3219 | case 1: |
| 3220 | { |
| 3221 | sp = row; |
| 3222 | shift = 7; |
| 3223 | for (i = 0; i < row_width; i++) |
| 3224 | { |
| 3225 | if ((png_uint_16)((*sp >> shift) & 0x01) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3226 | == png_ptr->trans_color.gray) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3227 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3228 | unsigned int tmp = *sp & (0x7f7f >> (7 - shift)); |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 3229 | tmp |= |
| 3230 | (unsigned int)(png_ptr->background.gray << shift); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3231 | *sp = (png_byte)(tmp & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3232 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3233 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3234 | if (shift == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3235 | { |
| 3236 | shift = 7; |
| 3237 | sp++; |
| 3238 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3239 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3240 | else |
| 3241 | shift--; |
| 3242 | } |
| 3243 | break; |
| 3244 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3245 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3246 | case 2: |
| 3247 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3248 | #ifdef PNG_READ_GAMMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3249 | if (gamma_table != NULL) |
| 3250 | { |
| 3251 | sp = row; |
| 3252 | shift = 6; |
| 3253 | for (i = 0; i < row_width; i++) |
| 3254 | { |
| 3255 | if ((png_uint_16)((*sp >> shift) & 0x03) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3256 | == png_ptr->trans_color.gray) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3257 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3258 | unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 3259 | tmp |= |
| 3260 | (unsigned int)png_ptr->background.gray << shift; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3261 | *sp = (png_byte)(tmp & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3262 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3263 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3264 | else |
| 3265 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3266 | unsigned int p = (*sp >> shift) & 0x03; |
| 3267 | unsigned int g = (gamma_table [p | (p << 2) | |
| 3268 | (p << 4) | (p << 6)] >> 6) & 0x03; |
| 3269 | unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 3270 | tmp |= (unsigned int)(g << shift); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3271 | *sp = (png_byte)(tmp & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3272 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3273 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3274 | if (shift == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3275 | { |
| 3276 | shift = 6; |
| 3277 | sp++; |
| 3278 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3279 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3280 | else |
| 3281 | shift -= 2; |
| 3282 | } |
| 3283 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3284 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3285 | else |
| 3286 | #endif |
| 3287 | { |
| 3288 | sp = row; |
| 3289 | shift = 6; |
| 3290 | for (i = 0; i < row_width; i++) |
| 3291 | { |
| 3292 | if ((png_uint_16)((*sp >> shift) & 0x03) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3293 | == png_ptr->trans_color.gray) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3294 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3295 | unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 3296 | tmp |= |
| 3297 | (unsigned int)png_ptr->background.gray << shift; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3298 | *sp = (png_byte)(tmp & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3299 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3300 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3301 | if (shift == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3302 | { |
| 3303 | shift = 6; |
| 3304 | sp++; |
| 3305 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3306 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3307 | else |
| 3308 | shift -= 2; |
| 3309 | } |
| 3310 | } |
| 3311 | break; |
| 3312 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3313 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3314 | case 4: |
| 3315 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3316 | #ifdef PNG_READ_GAMMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3317 | if (gamma_table != NULL) |
| 3318 | { |
| 3319 | sp = row; |
| 3320 | shift = 4; |
| 3321 | for (i = 0; i < row_width; i++) |
| 3322 | { |
| 3323 | if ((png_uint_16)((*sp >> shift) & 0x0f) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3324 | == png_ptr->trans_color.gray) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3325 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3326 | unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 3327 | tmp |= |
| 3328 | (unsigned int)(png_ptr->background.gray << shift); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3329 | *sp = (png_byte)(tmp & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3330 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3331 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3332 | else |
| 3333 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3334 | unsigned int p = (*sp >> shift) & 0x0f; |
| 3335 | unsigned int g = (gamma_table[p | (p << 4)] >> 4) & |
| 3336 | 0x0f; |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3337 | unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 3338 | tmp |= (unsigned int)(g << shift); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3339 | *sp = (png_byte)(tmp & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3340 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3341 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3342 | if (shift == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3343 | { |
| 3344 | shift = 4; |
| 3345 | sp++; |
| 3346 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3347 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3348 | else |
| 3349 | shift -= 4; |
| 3350 | } |
| 3351 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3352 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3353 | else |
| 3354 | #endif |
| 3355 | { |
| 3356 | sp = row; |
| 3357 | shift = 4; |
| 3358 | for (i = 0; i < row_width; i++) |
| 3359 | { |
| 3360 | if ((png_uint_16)((*sp >> shift) & 0x0f) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3361 | == png_ptr->trans_color.gray) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3362 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3363 | unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 3364 | tmp |= |
| 3365 | (unsigned int)(png_ptr->background.gray << shift); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3366 | *sp = (png_byte)(tmp & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3367 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3368 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3369 | if (shift == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3370 | { |
| 3371 | shift = 4; |
| 3372 | sp++; |
| 3373 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3374 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3375 | else |
| 3376 | shift -= 4; |
| 3377 | } |
| 3378 | } |
| 3379 | break; |
| 3380 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3381 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3382 | case 8: |
| 3383 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3384 | #ifdef PNG_READ_GAMMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3385 | if (gamma_table != NULL) |
| 3386 | { |
| 3387 | sp = row; |
| 3388 | for (i = 0; i < row_width; i++, sp++) |
| 3389 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3390 | if (*sp == png_ptr->trans_color.gray) |
| 3391 | *sp = (png_byte)png_ptr->background.gray; |
| 3392 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3393 | else |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3394 | *sp = gamma_table[*sp]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3395 | } |
| 3396 | } |
| 3397 | else |
| 3398 | #endif |
| 3399 | { |
| 3400 | sp = row; |
| 3401 | for (i = 0; i < row_width; i++, sp++) |
| 3402 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3403 | if (*sp == png_ptr->trans_color.gray) |
| 3404 | *sp = (png_byte)png_ptr->background.gray; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3405 | } |
| 3406 | } |
| 3407 | break; |
| 3408 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3409 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3410 | case 16: |
| 3411 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3412 | #ifdef PNG_READ_GAMMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3413 | if (gamma_16 != NULL) |
| 3414 | { |
| 3415 | sp = row; |
| 3416 | for (i = 0; i < row_width; i++, sp += 2) |
| 3417 | { |
| 3418 | png_uint_16 v; |
| 3419 | |
| 3420 | v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3421 | |
| 3422 | if (v == png_ptr->trans_color.gray) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3423 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3424 | /* Background is already in screen gamma */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3425 | *sp = (png_byte)((png_ptr->background.gray >> 8) |
| 3426 | & 0xff); |
| 3427 | *(sp + 1) = (png_byte)(png_ptr->background.gray |
| 3428 | & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3429 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3430 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3431 | else |
| 3432 | { |
| 3433 | v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
| 3434 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3435 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3436 | } |
| 3437 | } |
| 3438 | } |
| 3439 | else |
| 3440 | #endif |
| 3441 | { |
| 3442 | sp = row; |
| 3443 | for (i = 0; i < row_width; i++, sp += 2) |
| 3444 | { |
| 3445 | png_uint_16 v; |
| 3446 | |
| 3447 | v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3448 | |
| 3449 | if (v == png_ptr->trans_color.gray) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3450 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3451 | *sp = (png_byte)((png_ptr->background.gray >> 8) |
| 3452 | & 0xff); |
| 3453 | *(sp + 1) = (png_byte)(png_ptr->background.gray |
| 3454 | & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3455 | } |
| 3456 | } |
| 3457 | } |
| 3458 | break; |
| 3459 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3460 | |
| 3461 | default: |
| 3462 | break; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3463 | } |
| 3464 | break; |
| 3465 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3466 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3467 | case PNG_COLOR_TYPE_RGB: |
| 3468 | { |
| 3469 | if (row_info->bit_depth == 8) |
| 3470 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3471 | #ifdef PNG_READ_GAMMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3472 | if (gamma_table != NULL) |
| 3473 | { |
| 3474 | sp = row; |
| 3475 | for (i = 0; i < row_width; i++, sp += 3) |
| 3476 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3477 | if (*sp == png_ptr->trans_color.red && |
| 3478 | *(sp + 1) == png_ptr->trans_color.green && |
| 3479 | *(sp + 2) == png_ptr->trans_color.blue) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3480 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3481 | *sp = (png_byte)png_ptr->background.red; |
| 3482 | *(sp + 1) = (png_byte)png_ptr->background.green; |
| 3483 | *(sp + 2) = (png_byte)png_ptr->background.blue; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3484 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3485 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3486 | else |
| 3487 | { |
| 3488 | *sp = gamma_table[*sp]; |
| 3489 | *(sp + 1) = gamma_table[*(sp + 1)]; |
| 3490 | *(sp + 2) = gamma_table[*(sp + 2)]; |
| 3491 | } |
| 3492 | } |
| 3493 | } |
| 3494 | else |
| 3495 | #endif |
| 3496 | { |
| 3497 | sp = row; |
| 3498 | for (i = 0; i < row_width; i++, sp += 3) |
| 3499 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3500 | if (*sp == png_ptr->trans_color.red && |
| 3501 | *(sp + 1) == png_ptr->trans_color.green && |
| 3502 | *(sp + 2) == png_ptr->trans_color.blue) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3503 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3504 | *sp = (png_byte)png_ptr->background.red; |
| 3505 | *(sp + 1) = (png_byte)png_ptr->background.green; |
| 3506 | *(sp + 2) = (png_byte)png_ptr->background.blue; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3507 | } |
| 3508 | } |
| 3509 | } |
| 3510 | } |
| 3511 | else /* if (row_info->bit_depth == 16) */ |
| 3512 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3513 | #ifdef PNG_READ_GAMMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3514 | if (gamma_16 != NULL) |
| 3515 | { |
| 3516 | sp = row; |
| 3517 | for (i = 0; i < row_width; i++, sp += 6) |
| 3518 | { |
| 3519 | png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3520 | |
| 3521 | png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) |
| 3522 | + *(sp + 3)); |
| 3523 | |
| 3524 | png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) |
| 3525 | + *(sp + 5)); |
| 3526 | |
| 3527 | if (r == png_ptr->trans_color.red && |
| 3528 | g == png_ptr->trans_color.green && |
| 3529 | b == png_ptr->trans_color.blue) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3530 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3531 | /* Background is already in screen gamma */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3532 | *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); |
| 3533 | *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); |
| 3534 | *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) |
| 3535 | & 0xff); |
| 3536 | *(sp + 3) = (png_byte)(png_ptr->background.green |
| 3537 | & 0xff); |
| 3538 | *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) |
| 3539 | & 0xff); |
| 3540 | *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3541 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3542 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3543 | else |
| 3544 | { |
| 3545 | png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
| 3546 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3547 | *(sp + 1) = (png_byte)(v & 0xff); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3548 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3549 | v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
| 3550 | *(sp + 2) = (png_byte)((v >> 8) & 0xff); |
| 3551 | *(sp + 3) = (png_byte)(v & 0xff); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3552 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3553 | v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
| 3554 | *(sp + 4) = (png_byte)((v >> 8) & 0xff); |
| 3555 | *(sp + 5) = (png_byte)(v & 0xff); |
| 3556 | } |
| 3557 | } |
| 3558 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3559 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3560 | else |
| 3561 | #endif |
| 3562 | { |
| 3563 | sp = row; |
| 3564 | for (i = 0; i < row_width; i++, sp += 6) |
| 3565 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3566 | png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3567 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3568 | png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) |
| 3569 | + *(sp + 3)); |
| 3570 | |
| 3571 | png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) |
| 3572 | + *(sp + 5)); |
| 3573 | |
| 3574 | if (r == png_ptr->trans_color.red && |
| 3575 | g == png_ptr->trans_color.green && |
| 3576 | b == png_ptr->trans_color.blue) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3577 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3578 | *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); |
| 3579 | *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); |
| 3580 | *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) |
| 3581 | & 0xff); |
| 3582 | *(sp + 3) = (png_byte)(png_ptr->background.green |
| 3583 | & 0xff); |
| 3584 | *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) |
| 3585 | & 0xff); |
| 3586 | *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3587 | } |
| 3588 | } |
| 3589 | } |
| 3590 | } |
| 3591 | break; |
| 3592 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3593 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3594 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 3595 | { |
| 3596 | if (row_info->bit_depth == 8) |
| 3597 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3598 | #ifdef PNG_READ_GAMMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3599 | if (gamma_to_1 != NULL && gamma_from_1 != NULL && |
| 3600 | gamma_table != NULL) |
| 3601 | { |
| 3602 | sp = row; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3603 | for (i = 0; i < row_width; i++, sp += 2) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3604 | { |
| 3605 | png_uint_16 a = *(sp + 1); |
| 3606 | |
| 3607 | if (a == 0xff) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3608 | *sp = gamma_table[*sp]; |
| 3609 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3610 | else if (a == 0) |
| 3611 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3612 | /* Background is already in screen gamma */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3613 | *sp = (png_byte)png_ptr->background.gray; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3614 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3615 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3616 | else |
| 3617 | { |
| 3618 | png_byte v, w; |
| 3619 | |
| 3620 | v = gamma_to_1[*sp]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3621 | png_composite(w, v, a, png_ptr->background_1.gray); |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3622 | if (optimize == 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3623 | w = gamma_from_1[w]; |
| 3624 | *sp = w; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3625 | } |
| 3626 | } |
| 3627 | } |
| 3628 | else |
| 3629 | #endif |
| 3630 | { |
| 3631 | sp = row; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3632 | for (i = 0; i < row_width; i++, sp += 2) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3633 | { |
| 3634 | png_byte a = *(sp + 1); |
| 3635 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3636 | if (a == 0) |
| 3637 | *sp = (png_byte)png_ptr->background.gray; |
| 3638 | |
| 3639 | else if (a < 0xff) |
| 3640 | png_composite(*sp, *sp, a, png_ptr->background.gray); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3641 | } |
| 3642 | } |
| 3643 | } |
| 3644 | else /* if (png_ptr->bit_depth == 16) */ |
| 3645 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3646 | #ifdef PNG_READ_GAMMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3647 | if (gamma_16 != NULL && gamma_16_from_1 != NULL && |
| 3648 | gamma_16_to_1 != NULL) |
| 3649 | { |
| 3650 | sp = row; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3651 | for (i = 0; i < row_width; i++, sp += 4) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3652 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3653 | png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8) |
| 3654 | + *(sp + 3)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3655 | |
| 3656 | if (a == (png_uint_16)0xffff) |
| 3657 | { |
| 3658 | png_uint_16 v; |
| 3659 | |
| 3660 | v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3661 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3662 | *(sp + 1) = (png_byte)(v & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3663 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3664 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3665 | else if (a == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3666 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3667 | /* Background is already in screen gamma */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3668 | *sp = (png_byte)((png_ptr->background.gray >> 8) |
| 3669 | & 0xff); |
| 3670 | *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3671 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3672 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3673 | else |
| 3674 | { |
| 3675 | png_uint_16 g, v, w; |
| 3676 | |
| 3677 | g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3678 | png_composite_16(v, g, a, png_ptr->background_1.gray); |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3679 | if (optimize != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3680 | w = v; |
| 3681 | else |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3682 | w = gamma_16_from_1[(v & 0xff) >> |
| 3683 | gamma_shift][v >> 8]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3684 | *sp = (png_byte)((w >> 8) & 0xff); |
| 3685 | *(sp + 1) = (png_byte)(w & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3686 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3687 | } |
| 3688 | } |
| 3689 | else |
| 3690 | #endif |
| 3691 | { |
| 3692 | sp = row; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3693 | for (i = 0; i < row_width; i++, sp += 4) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3694 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3695 | png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8) |
| 3696 | + *(sp + 3)); |
| 3697 | |
| 3698 | if (a == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3699 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3700 | *sp = (png_byte)((png_ptr->background.gray >> 8) |
| 3701 | & 0xff); |
| 3702 | *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3703 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3704 | |
| 3705 | else if (a < 0xffff) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3706 | { |
| 3707 | png_uint_16 g, v; |
| 3708 | |
| 3709 | g = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3710 | png_composite_16(v, g, a, png_ptr->background.gray); |
| 3711 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3712 | *(sp + 1) = (png_byte)(v & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3713 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3714 | } |
| 3715 | } |
| 3716 | } |
| 3717 | break; |
| 3718 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3719 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3720 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 3721 | { |
| 3722 | if (row_info->bit_depth == 8) |
| 3723 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3724 | #ifdef PNG_READ_GAMMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3725 | if (gamma_to_1 != NULL && gamma_from_1 != NULL && |
| 3726 | gamma_table != NULL) |
| 3727 | { |
| 3728 | sp = row; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3729 | for (i = 0; i < row_width; i++, sp += 4) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3730 | { |
| 3731 | png_byte a = *(sp + 3); |
| 3732 | |
| 3733 | if (a == 0xff) |
| 3734 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3735 | *sp = gamma_table[*sp]; |
| 3736 | *(sp + 1) = gamma_table[*(sp + 1)]; |
| 3737 | *(sp + 2) = gamma_table[*(sp + 2)]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3738 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3739 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3740 | else if (a == 0) |
| 3741 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3742 | /* Background is already in screen gamma */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3743 | *sp = (png_byte)png_ptr->background.red; |
| 3744 | *(sp + 1) = (png_byte)png_ptr->background.green; |
| 3745 | *(sp + 2) = (png_byte)png_ptr->background.blue; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3746 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3747 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3748 | else |
| 3749 | { |
| 3750 | png_byte v, w; |
| 3751 | |
| 3752 | v = gamma_to_1[*sp]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3753 | png_composite(w, v, a, png_ptr->background_1.red); |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3754 | if (optimize == 0) w = gamma_from_1[w]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3755 | *sp = w; |
| 3756 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3757 | v = gamma_to_1[*(sp + 1)]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3758 | png_composite(w, v, a, png_ptr->background_1.green); |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3759 | if (optimize == 0) w = gamma_from_1[w]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3760 | *(sp + 1) = w; |
| 3761 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3762 | v = gamma_to_1[*(sp + 2)]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3763 | png_composite(w, v, a, png_ptr->background_1.blue); |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3764 | if (optimize == 0) w = gamma_from_1[w]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3765 | *(sp + 2) = w; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3766 | } |
| 3767 | } |
| 3768 | } |
| 3769 | else |
| 3770 | #endif |
| 3771 | { |
| 3772 | sp = row; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3773 | for (i = 0; i < row_width; i++, sp += 4) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3774 | { |
| 3775 | png_byte a = *(sp + 3); |
| 3776 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3777 | if (a == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3778 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3779 | *sp = (png_byte)png_ptr->background.red; |
| 3780 | *(sp + 1) = (png_byte)png_ptr->background.green; |
| 3781 | *(sp + 2) = (png_byte)png_ptr->background.blue; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3782 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3783 | |
| 3784 | else if (a < 0xff) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3785 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3786 | png_composite(*sp, *sp, a, png_ptr->background.red); |
| 3787 | |
| 3788 | png_composite(*(sp + 1), *(sp + 1), a, |
| 3789 | png_ptr->background.green); |
| 3790 | |
| 3791 | png_composite(*(sp + 2), *(sp + 2), a, |
| 3792 | png_ptr->background.blue); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3793 | } |
| 3794 | } |
| 3795 | } |
| 3796 | } |
| 3797 | else /* if (row_info->bit_depth == 16) */ |
| 3798 | { |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3799 | #ifdef PNG_READ_GAMMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3800 | if (gamma_16 != NULL && gamma_16_from_1 != NULL && |
| 3801 | gamma_16_to_1 != NULL) |
| 3802 | { |
| 3803 | sp = row; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3804 | for (i = 0; i < row_width; i++, sp += 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3805 | { |
| 3806 | png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) |
| 3807 | << 8) + (png_uint_16)(*(sp + 7))); |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3808 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3809 | if (a == (png_uint_16)0xffff) |
| 3810 | { |
| 3811 | png_uint_16 v; |
| 3812 | |
| 3813 | v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3814 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3815 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3816 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3817 | v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3818 | *(sp + 2) = (png_byte)((v >> 8) & 0xff); |
| 3819 | *(sp + 3) = (png_byte)(v & 0xff); |
| 3820 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3821 | v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3822 | *(sp + 4) = (png_byte)((v >> 8) & 0xff); |
| 3823 | *(sp + 5) = (png_byte)(v & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3824 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3825 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3826 | else if (a == 0) |
| 3827 | { |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3828 | /* Background is already in screen gamma */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3829 | *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); |
| 3830 | *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); |
| 3831 | *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) |
| 3832 | & 0xff); |
| 3833 | *(sp + 3) = (png_byte)(png_ptr->background.green |
| 3834 | & 0xff); |
| 3835 | *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) |
| 3836 | & 0xff); |
| 3837 | *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3838 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3839 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3840 | else |
| 3841 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3842 | png_uint_16 v, w; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3843 | |
| 3844 | v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3845 | png_composite_16(w, v, a, png_ptr->background_1.red); |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3846 | if (optimize == 0) |
| 3847 | w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3848 | 8]; |
| 3849 | *sp = (png_byte)((w >> 8) & 0xff); |
| 3850 | *(sp + 1) = (png_byte)(w & 0xff); |
| 3851 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3852 | v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3853 | png_composite_16(w, v, a, png_ptr->background_1.green); |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3854 | if (optimize == 0) |
| 3855 | w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3856 | 8]; |
| 3857 | |
| 3858 | *(sp + 2) = (png_byte)((w >> 8) & 0xff); |
| 3859 | *(sp + 3) = (png_byte)(w & 0xff); |
| 3860 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3861 | v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3862 | png_composite_16(w, v, a, png_ptr->background_1.blue); |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3863 | if (optimize == 0) |
| 3864 | w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3865 | 8]; |
| 3866 | |
| 3867 | *(sp + 4) = (png_byte)((w >> 8) & 0xff); |
| 3868 | *(sp + 5) = (png_byte)(w & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3869 | } |
| 3870 | } |
| 3871 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3872 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3873 | else |
| 3874 | #endif |
| 3875 | { |
| 3876 | sp = row; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3877 | for (i = 0; i < row_width; i++, sp += 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3878 | { |
| 3879 | png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3880 | << 8) + (png_uint_16)(*(sp + 7))); |
| 3881 | |
| 3882 | if (a == 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3883 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3884 | *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); |
| 3885 | *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); |
| 3886 | *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) |
| 3887 | & 0xff); |
| 3888 | *(sp + 3) = (png_byte)(png_ptr->background.green |
| 3889 | & 0xff); |
| 3890 | *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) |
| 3891 | & 0xff); |
| 3892 | *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3893 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3894 | |
| 3895 | else if (a < 0xffff) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3896 | { |
| 3897 | png_uint_16 v; |
| 3898 | |
| 3899 | png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
| 3900 | png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) |
| 3901 | + *(sp + 3)); |
| 3902 | png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) |
| 3903 | + *(sp + 5)); |
| 3904 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3905 | png_composite_16(v, r, a, png_ptr->background.red); |
| 3906 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3907 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3908 | |
| 3909 | png_composite_16(v, g, a, png_ptr->background.green); |
| 3910 | *(sp + 2) = (png_byte)((v >> 8) & 0xff); |
| 3911 | *(sp + 3) = (png_byte)(v & 0xff); |
| 3912 | |
| 3913 | png_composite_16(v, b, a, png_ptr->background.blue); |
| 3914 | *(sp + 4) = (png_byte)((v >> 8) & 0xff); |
| 3915 | *(sp + 5) = (png_byte)(v & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3916 | } |
| 3917 | } |
| 3918 | } |
| 3919 | } |
| 3920 | break; |
| 3921 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3922 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3923 | default: |
| 3924 | break; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3925 | } |
| 3926 | } |
| 3927 | } |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 3928 | #endif /* READ_BACKGROUND || READ_ALPHA_MODE */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3929 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3930 | #ifdef PNG_READ_GAMMA_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3931 | /* Gamma correct the image, avoiding the alpha channel. Make sure |
| 3932 | * you do this after you deal with the transparency issue on grayscale |
| 3933 | * or RGB images. If your bit depth is 8, use gamma_table, if it |
| 3934 | * is 16, use gamma_16_table and gamma_shift. Build these with |
| 3935 | * build_gamma_table(). |
| 3936 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 3937 | static void |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3938 | png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3939 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3940 | png_const_bytep gamma_table = png_ptr->gamma_table; |
| 3941 | png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table; |
| 3942 | int gamma_shift = png_ptr->gamma_shift; |
| 3943 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3944 | png_bytep sp; |
| 3945 | png_uint_32 i; |
| 3946 | png_uint_32 row_width=row_info->width; |
| 3947 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 3948 | png_debug(1, "in png_do_gamma"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 3949 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3950 | if (((row_info->bit_depth <= 8 && gamma_table != NULL) || |
| 3951 | (row_info->bit_depth == 16 && gamma_16_table != NULL))) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3952 | { |
| 3953 | switch (row_info->color_type) |
| 3954 | { |
| 3955 | case PNG_COLOR_TYPE_RGB: |
| 3956 | { |
| 3957 | if (row_info->bit_depth == 8) |
| 3958 | { |
| 3959 | sp = row; |
| 3960 | for (i = 0; i < row_width; i++) |
| 3961 | { |
| 3962 | *sp = gamma_table[*sp]; |
| 3963 | sp++; |
| 3964 | *sp = gamma_table[*sp]; |
| 3965 | sp++; |
| 3966 | *sp = gamma_table[*sp]; |
| 3967 | sp++; |
| 3968 | } |
| 3969 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3970 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3971 | else /* if (row_info->bit_depth == 16) */ |
| 3972 | { |
| 3973 | sp = row; |
| 3974 | for (i = 0; i < row_width; i++) |
| 3975 | { |
| 3976 | png_uint_16 v; |
| 3977 | |
| 3978 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 3979 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3980 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3981 | sp += 2; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3982 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3983 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 3984 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3985 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3986 | sp += 2; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 3987 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3988 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 3989 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3990 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3991 | sp += 2; |
| 3992 | } |
| 3993 | } |
| 3994 | break; |
| 3995 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 3996 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3997 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 3998 | { |
| 3999 | if (row_info->bit_depth == 8) |
| 4000 | { |
| 4001 | sp = row; |
| 4002 | for (i = 0; i < row_width; i++) |
| 4003 | { |
| 4004 | *sp = gamma_table[*sp]; |
| 4005 | sp++; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4006 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4007 | *sp = gamma_table[*sp]; |
| 4008 | sp++; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4009 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4010 | *sp = gamma_table[*sp]; |
| 4011 | sp++; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4012 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4013 | sp++; |
| 4014 | } |
| 4015 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4016 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4017 | else /* if (row_info->bit_depth == 16) */ |
| 4018 | { |
| 4019 | sp = row; |
| 4020 | for (i = 0; i < row_width; i++) |
| 4021 | { |
| 4022 | png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 4023 | *sp = (png_byte)((v >> 8) & 0xff); |
| 4024 | *(sp + 1) = (png_byte)(v & 0xff); |
| 4025 | sp += 2; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4026 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4027 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 4028 | *sp = (png_byte)((v >> 8) & 0xff); |
| 4029 | *(sp + 1) = (png_byte)(v & 0xff); |
| 4030 | sp += 2; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4031 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4032 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 4033 | *sp = (png_byte)((v >> 8) & 0xff); |
| 4034 | *(sp + 1) = (png_byte)(v & 0xff); |
| 4035 | sp += 4; |
| 4036 | } |
| 4037 | } |
| 4038 | break; |
| 4039 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4040 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4041 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 4042 | { |
| 4043 | if (row_info->bit_depth == 8) |
| 4044 | { |
| 4045 | sp = row; |
| 4046 | for (i = 0; i < row_width; i++) |
| 4047 | { |
| 4048 | *sp = gamma_table[*sp]; |
| 4049 | sp += 2; |
| 4050 | } |
| 4051 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4052 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4053 | else /* if (row_info->bit_depth == 16) */ |
| 4054 | { |
| 4055 | sp = row; |
| 4056 | for (i = 0; i < row_width; i++) |
| 4057 | { |
| 4058 | png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 4059 | *sp = (png_byte)((v >> 8) & 0xff); |
| 4060 | *(sp + 1) = (png_byte)(v & 0xff); |
| 4061 | sp += 4; |
| 4062 | } |
| 4063 | } |
| 4064 | break; |
| 4065 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4066 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4067 | case PNG_COLOR_TYPE_GRAY: |
| 4068 | { |
| 4069 | if (row_info->bit_depth == 2) |
| 4070 | { |
| 4071 | sp = row; |
| 4072 | for (i = 0; i < row_width; i += 4) |
| 4073 | { |
| 4074 | int a = *sp & 0xc0; |
| 4075 | int b = *sp & 0x30; |
| 4076 | int c = *sp & 0x0c; |
| 4077 | int d = *sp & 0x03; |
| 4078 | |
| 4079 | *sp = (png_byte)( |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4080 | ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)| |
| 4081 | ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)| |
| 4082 | ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)| |
| 4083 | ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) )); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4084 | sp++; |
| 4085 | } |
| 4086 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4087 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4088 | if (row_info->bit_depth == 4) |
| 4089 | { |
| 4090 | sp = row; |
| 4091 | for (i = 0; i < row_width; i += 2) |
| 4092 | { |
| 4093 | int msb = *sp & 0xf0; |
| 4094 | int lsb = *sp & 0x0f; |
| 4095 | |
| 4096 | *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0) |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4097 | | (((int)gamma_table[(lsb << 4) | lsb]) >> 4)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4098 | sp++; |
| 4099 | } |
| 4100 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4101 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4102 | else if (row_info->bit_depth == 8) |
| 4103 | { |
| 4104 | sp = row; |
| 4105 | for (i = 0; i < row_width; i++) |
| 4106 | { |
| 4107 | *sp = gamma_table[*sp]; |
| 4108 | sp++; |
| 4109 | } |
| 4110 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4111 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4112 | else if (row_info->bit_depth == 16) |
| 4113 | { |
| 4114 | sp = row; |
| 4115 | for (i = 0; i < row_width; i++) |
| 4116 | { |
| 4117 | png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 4118 | *sp = (png_byte)((v >> 8) & 0xff); |
| 4119 | *(sp + 1) = (png_byte)(v & 0xff); |
| 4120 | sp += 2; |
| 4121 | } |
| 4122 | } |
| 4123 | break; |
| 4124 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4125 | |
| 4126 | default: |
| 4127 | break; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4128 | } |
| 4129 | } |
| 4130 | } |
| 4131 | #endif |
| 4132 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4133 | #ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
| 4134 | /* Encode the alpha channel to the output gamma (the input channel is always |
| 4135 | * linear.) Called only with color types that have an alpha channel. Needs the |
| 4136 | * from_1 tables. |
| 4137 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4138 | static void |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4139 | png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr) |
| 4140 | { |
| 4141 | png_uint_32 row_width = row_info->width; |
| 4142 | |
| 4143 | png_debug(1, "in png_do_encode_alpha"); |
| 4144 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4145 | if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4146 | { |
| 4147 | if (row_info->bit_depth == 8) |
| 4148 | { |
| 4149 | PNG_CONST png_bytep table = png_ptr->gamma_from_1; |
| 4150 | |
| 4151 | if (table != NULL) |
| 4152 | { |
| 4153 | PNG_CONST int step = |
| 4154 | (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2; |
| 4155 | |
| 4156 | /* The alpha channel is the last component: */ |
| 4157 | row += step - 1; |
| 4158 | |
| 4159 | for (; row_width > 0; --row_width, row += step) |
| 4160 | *row = table[*row]; |
| 4161 | |
| 4162 | return; |
| 4163 | } |
| 4164 | } |
| 4165 | |
| 4166 | else if (row_info->bit_depth == 16) |
| 4167 | { |
| 4168 | PNG_CONST png_uint_16pp table = png_ptr->gamma_16_from_1; |
| 4169 | PNG_CONST int gamma_shift = png_ptr->gamma_shift; |
| 4170 | |
| 4171 | if (table != NULL) |
| 4172 | { |
| 4173 | PNG_CONST int step = |
| 4174 | (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4; |
| 4175 | |
| 4176 | /* The alpha channel is the last component: */ |
| 4177 | row += step - 2; |
| 4178 | |
| 4179 | for (; row_width > 0; --row_width, row += step) |
| 4180 | { |
| 4181 | png_uint_16 v; |
| 4182 | |
| 4183 | v = table[*(row + 1) >> gamma_shift][*row]; |
| 4184 | *row = (png_byte)((v >> 8) & 0xff); |
| 4185 | *(row + 1) = (png_byte)(v & 0xff); |
| 4186 | } |
| 4187 | |
| 4188 | return; |
| 4189 | } |
| 4190 | } |
| 4191 | } |
| 4192 | |
| 4193 | /* Only get to here if called with a weird row_info; no harm has been done, |
| 4194 | * so just issue a warning. |
| 4195 | */ |
| 4196 | png_warning(png_ptr, "png_do_encode_alpha: unexpected call"); |
| 4197 | } |
| 4198 | #endif |
| 4199 | |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4200 | #ifdef PNG_READ_EXPAND_SUPPORTED |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4201 | /* Expands a palette row to an RGB or RGBA row depending |
| 4202 | * upon whether you supply trans and num_trans. |
| 4203 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4204 | static void |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4205 | png_do_expand_palette(png_row_infop row_info, png_bytep row, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 4206 | png_const_colorp palette, png_const_bytep trans_alpha, int num_trans) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4207 | { |
| 4208 | int shift, value; |
| 4209 | png_bytep sp, dp; |
| 4210 | png_uint_32 i; |
| 4211 | png_uint_32 row_width=row_info->width; |
| 4212 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4213 | png_debug(1, "in png_do_expand_palette"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4214 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4215 | if (row_info->color_type == PNG_COLOR_TYPE_PALETTE) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4216 | { |
| 4217 | if (row_info->bit_depth < 8) |
| 4218 | { |
| 4219 | switch (row_info->bit_depth) |
| 4220 | { |
| 4221 | case 1: |
| 4222 | { |
| 4223 | sp = row + (png_size_t)((row_width - 1) >> 3); |
| 4224 | dp = row + (png_size_t)row_width - 1; |
| 4225 | shift = 7 - (int)((row_width + 7) & 0x07); |
| 4226 | for (i = 0; i < row_width; i++) |
| 4227 | { |
| 4228 | if ((*sp >> shift) & 0x01) |
| 4229 | *dp = 1; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4230 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4231 | else |
| 4232 | *dp = 0; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4233 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4234 | if (shift == 7) |
| 4235 | { |
| 4236 | shift = 0; |
| 4237 | sp--; |
| 4238 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4239 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4240 | else |
| 4241 | shift++; |
| 4242 | |
| 4243 | dp--; |
| 4244 | } |
| 4245 | break; |
| 4246 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4247 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4248 | case 2: |
| 4249 | { |
| 4250 | sp = row + (png_size_t)((row_width - 1) >> 2); |
| 4251 | dp = row + (png_size_t)row_width - 1; |
| 4252 | shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
| 4253 | for (i = 0; i < row_width; i++) |
| 4254 | { |
| 4255 | value = (*sp >> shift) & 0x03; |
| 4256 | *dp = (png_byte)value; |
| 4257 | if (shift == 6) |
| 4258 | { |
| 4259 | shift = 0; |
| 4260 | sp--; |
| 4261 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4262 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4263 | else |
| 4264 | shift += 2; |
| 4265 | |
| 4266 | dp--; |
| 4267 | } |
| 4268 | break; |
| 4269 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4270 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4271 | case 4: |
| 4272 | { |
| 4273 | sp = row + (png_size_t)((row_width - 1) >> 1); |
| 4274 | dp = row + (png_size_t)row_width - 1; |
| 4275 | shift = (int)((row_width & 0x01) << 2); |
| 4276 | for (i = 0; i < row_width; i++) |
| 4277 | { |
| 4278 | value = (*sp >> shift) & 0x0f; |
| 4279 | *dp = (png_byte)value; |
| 4280 | if (shift == 4) |
| 4281 | { |
| 4282 | shift = 0; |
| 4283 | sp--; |
| 4284 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4285 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4286 | else |
| 4287 | shift += 4; |
| 4288 | |
| 4289 | dp--; |
| 4290 | } |
| 4291 | break; |
| 4292 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4293 | |
| 4294 | default: |
| 4295 | break; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4296 | } |
| 4297 | row_info->bit_depth = 8; |
| 4298 | row_info->pixel_depth = 8; |
| 4299 | row_info->rowbytes = row_width; |
| 4300 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4301 | |
| 4302 | if (row_info->bit_depth == 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4303 | { |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4304 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4305 | if (num_trans > 0) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4306 | { |
| 4307 | sp = row + (png_size_t)row_width - 1; |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 4308 | dp = row + ((png_size_t)row_width << 2) - 1; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4309 | |
| 4310 | for (i = 0; i < row_width; i++) |
| 4311 | { |
| 4312 | if ((int)(*sp) >= num_trans) |
| 4313 | *dp-- = 0xff; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4314 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4315 | else |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4316 | *dp-- = trans_alpha[*sp]; |
| 4317 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4318 | *dp-- = palette[*sp].blue; |
| 4319 | *dp-- = palette[*sp].green; |
| 4320 | *dp-- = palette[*sp].red; |
| 4321 | sp--; |
| 4322 | } |
| 4323 | row_info->bit_depth = 8; |
| 4324 | row_info->pixel_depth = 32; |
| 4325 | row_info->rowbytes = row_width * 4; |
| 4326 | row_info->color_type = 6; |
| 4327 | row_info->channels = 4; |
| 4328 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4329 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4330 | else |
| 4331 | { |
| 4332 | sp = row + (png_size_t)row_width - 1; |
| 4333 | dp = row + (png_size_t)(row_width * 3) - 1; |
| 4334 | |
| 4335 | for (i = 0; i < row_width; i++) |
| 4336 | { |
| 4337 | *dp-- = palette[*sp].blue; |
| 4338 | *dp-- = palette[*sp].green; |
| 4339 | *dp-- = palette[*sp].red; |
| 4340 | sp--; |
| 4341 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4342 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4343 | row_info->bit_depth = 8; |
| 4344 | row_info->pixel_depth = 24; |
| 4345 | row_info->rowbytes = row_width * 3; |
| 4346 | row_info->color_type = 2; |
| 4347 | row_info->channels = 3; |
| 4348 | } |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4349 | } |
| 4350 | } |
| 4351 | } |
| 4352 | } |
| 4353 | |
| 4354 | /* If the bit depth < 8, it is expanded to 8. Also, if the already |
| 4355 | * expanded transparency value is supplied, an alpha channel is built. |
| 4356 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4357 | static void |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4358 | png_do_expand(png_row_infop row_info, png_bytep row, |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4359 | png_const_color_16p trans_color) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4360 | { |
| 4361 | int shift, value; |
| 4362 | png_bytep sp, dp; |
| 4363 | png_uint_32 i; |
| 4364 | png_uint_32 row_width=row_info->width; |
| 4365 | |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4366 | png_debug(1, "in png_do_expand"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4367 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4368 | { |
| 4369 | if (row_info->color_type == PNG_COLOR_TYPE_GRAY) |
| 4370 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4371 | unsigned int gray = trans_color != NULL ? trans_color->gray : 0; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4372 | |
| 4373 | if (row_info->bit_depth < 8) |
| 4374 | { |
| 4375 | switch (row_info->bit_depth) |
| 4376 | { |
| 4377 | case 1: |
| 4378 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4379 | gray = (gray & 0x01) * 0xff; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4380 | sp = row + (png_size_t)((row_width - 1) >> 3); |
| 4381 | dp = row + (png_size_t)row_width - 1; |
| 4382 | shift = 7 - (int)((row_width + 7) & 0x07); |
| 4383 | for (i = 0; i < row_width; i++) |
| 4384 | { |
| 4385 | if ((*sp >> shift) & 0x01) |
| 4386 | *dp = 0xff; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4387 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4388 | else |
| 4389 | *dp = 0; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4390 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4391 | if (shift == 7) |
| 4392 | { |
| 4393 | shift = 0; |
| 4394 | sp--; |
| 4395 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4396 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4397 | else |
| 4398 | shift++; |
| 4399 | |
| 4400 | dp--; |
| 4401 | } |
| 4402 | break; |
| 4403 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4404 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4405 | case 2: |
| 4406 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4407 | gray = (gray & 0x03) * 0x55; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4408 | sp = row + (png_size_t)((row_width - 1) >> 2); |
| 4409 | dp = row + (png_size_t)row_width - 1; |
| 4410 | shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
| 4411 | for (i = 0; i < row_width; i++) |
| 4412 | { |
| 4413 | value = (*sp >> shift) & 0x03; |
| 4414 | *dp = (png_byte)(value | (value << 2) | (value << 4) | |
| 4415 | (value << 6)); |
| 4416 | if (shift == 6) |
| 4417 | { |
| 4418 | shift = 0; |
| 4419 | sp--; |
| 4420 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4421 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4422 | else |
| 4423 | shift += 2; |
| 4424 | |
| 4425 | dp--; |
| 4426 | } |
| 4427 | break; |
| 4428 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4429 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4430 | case 4: |
| 4431 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4432 | gray = (gray & 0x0f) * 0x11; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4433 | sp = row + (png_size_t)((row_width - 1) >> 1); |
| 4434 | dp = row + (png_size_t)row_width - 1; |
| 4435 | shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); |
| 4436 | for (i = 0; i < row_width; i++) |
| 4437 | { |
| 4438 | value = (*sp >> shift) & 0x0f; |
| 4439 | *dp = (png_byte)(value | (value << 4)); |
| 4440 | if (shift == 4) |
| 4441 | { |
| 4442 | shift = 0; |
| 4443 | sp--; |
| 4444 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4445 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4446 | else |
| 4447 | shift = 4; |
| 4448 | |
| 4449 | dp--; |
| 4450 | } |
| 4451 | break; |
| 4452 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4453 | |
| 4454 | default: |
| 4455 | break; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4456 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4457 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4458 | row_info->bit_depth = 8; |
| 4459 | row_info->pixel_depth = 8; |
| 4460 | row_info->rowbytes = row_width; |
| 4461 | } |
| 4462 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4463 | if (trans_color != NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4464 | { |
| 4465 | if (row_info->bit_depth == 8) |
| 4466 | { |
| 4467 | gray = gray & 0xff; |
| 4468 | sp = row + (png_size_t)row_width - 1; |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 4469 | dp = row + ((png_size_t)row_width << 1) - 1; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4470 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4471 | for (i = 0; i < row_width; i++) |
| 4472 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4473 | if ((*sp & 0xffU) == gray) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4474 | *dp-- = 0; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4475 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4476 | else |
| 4477 | *dp-- = 0xff; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4478 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4479 | *dp-- = *sp--; |
| 4480 | } |
| 4481 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4482 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4483 | else if (row_info->bit_depth == 16) |
| 4484 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4485 | unsigned int gray_high = (gray >> 8) & 0xff; |
| 4486 | unsigned int gray_low = gray & 0xff; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4487 | sp = row + row_info->rowbytes - 1; |
| 4488 | dp = row + (row_info->rowbytes << 1) - 1; |
| 4489 | for (i = 0; i < row_width; i++) |
| 4490 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4491 | if ((*(sp - 1) & 0xffU) == gray_high && |
| 4492 | (*(sp) & 0xffU) == gray_low) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4493 | { |
| 4494 | *dp-- = 0; |
| 4495 | *dp-- = 0; |
| 4496 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4497 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4498 | else |
| 4499 | { |
| 4500 | *dp-- = 0xff; |
| 4501 | *dp-- = 0xff; |
| 4502 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4503 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4504 | *dp-- = *sp--; |
| 4505 | *dp-- = *sp--; |
| 4506 | } |
| 4507 | } |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4508 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4509 | row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; |
| 4510 | row_info->channels = 2; |
| 4511 | row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); |
| 4512 | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 4513 | row_width); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4514 | } |
| 4515 | } |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4516 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB && |
| 4517 | trans_color != NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4518 | { |
| 4519 | if (row_info->bit_depth == 8) |
| 4520 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4521 | png_byte red = (png_byte)(trans_color->red & 0xff); |
| 4522 | png_byte green = (png_byte)(trans_color->green & 0xff); |
| 4523 | png_byte blue = (png_byte)(trans_color->blue & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4524 | sp = row + (png_size_t)row_info->rowbytes - 1; |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 4525 | dp = row + ((png_size_t)row_width << 2) - 1; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4526 | for (i = 0; i < row_width; i++) |
| 4527 | { |
| 4528 | if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue) |
| 4529 | *dp-- = 0; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4530 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4531 | else |
| 4532 | *dp-- = 0xff; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4533 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4534 | *dp-- = *sp--; |
| 4535 | *dp-- = *sp--; |
| 4536 | *dp-- = *sp--; |
| 4537 | } |
| 4538 | } |
| 4539 | else if (row_info->bit_depth == 16) |
| 4540 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4541 | png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff); |
| 4542 | png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff); |
| 4543 | png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff); |
| 4544 | png_byte red_low = (png_byte)(trans_color->red & 0xff); |
| 4545 | png_byte green_low = (png_byte)(trans_color->green & 0xff); |
| 4546 | png_byte blue_low = (png_byte)(trans_color->blue & 0xff); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4547 | sp = row + row_info->rowbytes - 1; |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 4548 | dp = row + ((png_size_t)row_width << 3) - 1; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4549 | for (i = 0; i < row_width; i++) |
| 4550 | { |
| 4551 | if (*(sp - 5) == red_high && |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4552 | *(sp - 4) == red_low && |
| 4553 | *(sp - 3) == green_high && |
| 4554 | *(sp - 2) == green_low && |
| 4555 | *(sp - 1) == blue_high && |
| 4556 | *(sp ) == blue_low) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4557 | { |
| 4558 | *dp-- = 0; |
| 4559 | *dp-- = 0; |
| 4560 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4561 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4562 | else |
| 4563 | { |
| 4564 | *dp-- = 0xff; |
| 4565 | *dp-- = 0xff; |
| 4566 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4567 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4568 | *dp-- = *sp--; |
| 4569 | *dp-- = *sp--; |
| 4570 | *dp-- = *sp--; |
| 4571 | *dp-- = *sp--; |
| 4572 | *dp-- = *sp--; |
| 4573 | *dp-- = *sp--; |
| 4574 | } |
| 4575 | } |
| 4576 | row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
| 4577 | row_info->channels = 4; |
| 4578 | row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2); |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4579 | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4580 | } |
| 4581 | } |
| 4582 | } |
| 4583 | #endif |
| 4584 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4585 | #ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 4586 | /* If the bit depth is 8 and the color type is not a palette type expand the |
| 4587 | * whole row to 16 bits. Has no effect otherwise. |
| 4588 | */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4589 | static void |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4590 | png_do_expand_16(png_row_infop row_info, png_bytep row) |
| 4591 | { |
| 4592 | if (row_info->bit_depth == 8 && |
| 4593 | row_info->color_type != PNG_COLOR_TYPE_PALETTE) |
| 4594 | { |
| 4595 | /* The row have a sequence of bytes containing [0..255] and we need |
| 4596 | * to turn it into another row containing [0..65535], to do this we |
| 4597 | * calculate: |
| 4598 | * |
| 4599 | * (input / 255) * 65535 |
| 4600 | * |
| 4601 | * Which happens to be exactly input * 257 and this can be achieved |
| 4602 | * simply by byte replication in place (copying backwards). |
| 4603 | */ |
| 4604 | png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */ |
| 4605 | png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */ |
| 4606 | while (dp > sp) |
Leon Scroggins III | 3cc83ac | 2017-10-06 11:02:56 -0400 | [diff] [blame] | 4607 | { |
| 4608 | dp[-2] = dp[-1] = *--sp; dp -= 2; |
| 4609 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4610 | |
| 4611 | row_info->rowbytes *= 2; |
| 4612 | row_info->bit_depth = 16; |
| 4613 | row_info->pixel_depth = (png_byte)(row_info->channels * 16); |
| 4614 | } |
| 4615 | } |
| 4616 | #endif |
| 4617 | |
| 4618 | #ifdef PNG_READ_QUANTIZE_SUPPORTED |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4619 | static void |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4620 | png_do_quantize(png_row_infop row_info, png_bytep row, |
| 4621 | png_const_bytep palette_lookup, png_const_bytep quantize_lookup) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4622 | { |
| 4623 | png_bytep sp, dp; |
| 4624 | png_uint_32 i; |
| 4625 | png_uint_32 row_width=row_info->width; |
| 4626 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4627 | png_debug(1, "in png_do_quantize"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4628 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4629 | if (row_info->bit_depth == 8) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4630 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4631 | if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4632 | { |
| 4633 | int r, g, b, p; |
| 4634 | sp = row; |
| 4635 | dp = row; |
| 4636 | for (i = 0; i < row_width; i++) |
| 4637 | { |
| 4638 | r = *sp++; |
| 4639 | g = *sp++; |
| 4640 | b = *sp++; |
| 4641 | |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4642 | /* This looks real messy, but the compiler will reduce |
| 4643 | * it down to a reasonable formula. For example, with |
| 4644 | * 5 bits per color, we get: |
| 4645 | * p = (((r >> 3) & 0x1f) << 10) | |
| 4646 | * (((g >> 3) & 0x1f) << 5) | |
| 4647 | * ((b >> 3) & 0x1f); |
| 4648 | */ |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4649 | p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) & |
| 4650 | ((1 << PNG_QUANTIZE_RED_BITS) - 1)) << |
| 4651 | (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) | |
| 4652 | (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) & |
| 4653 | ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) << |
| 4654 | (PNG_QUANTIZE_BLUE_BITS)) | |
| 4655 | ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) & |
| 4656 | ((1 << PNG_QUANTIZE_BLUE_BITS) - 1)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4657 | |
| 4658 | *dp++ = palette_lookup[p]; |
| 4659 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4660 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4661 | row_info->color_type = PNG_COLOR_TYPE_PALETTE; |
| 4662 | row_info->channels = 1; |
| 4663 | row_info->pixel_depth = row_info->bit_depth; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4664 | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4665 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4666 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4667 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4668 | palette_lookup != NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4669 | { |
| 4670 | int r, g, b, p; |
| 4671 | sp = row; |
| 4672 | dp = row; |
| 4673 | for (i = 0; i < row_width; i++) |
| 4674 | { |
| 4675 | r = *sp++; |
| 4676 | g = *sp++; |
| 4677 | b = *sp++; |
| 4678 | sp++; |
| 4679 | |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4680 | p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) & |
| 4681 | ((1 << PNG_QUANTIZE_RED_BITS) - 1)) << |
| 4682 | (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) | |
| 4683 | (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) & |
| 4684 | ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) << |
| 4685 | (PNG_QUANTIZE_BLUE_BITS)) | |
| 4686 | ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) & |
| 4687 | ((1 << PNG_QUANTIZE_BLUE_BITS) - 1)); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4688 | |
| 4689 | *dp++ = palette_lookup[p]; |
| 4690 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4691 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4692 | row_info->color_type = PNG_COLOR_TYPE_PALETTE; |
| 4693 | row_info->channels = 1; |
| 4694 | row_info->pixel_depth = row_info->bit_depth; |
The Android Open Source Project | 4215dd1 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 4695 | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4696 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4697 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4698 | else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4699 | quantize_lookup) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4700 | { |
| 4701 | sp = row; |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4702 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4703 | for (i = 0; i < row_width; i++, sp++) |
| 4704 | { |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4705 | *sp = quantize_lookup[*sp]; |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4706 | } |
| 4707 | } |
| 4708 | } |
| 4709 | } |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4710 | #endif /* READ_QUANTIZE */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4711 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4712 | /* Transform the row. The order of transformations is significant, |
| 4713 | * and is very touchy. If you add a transformation, take care to |
| 4714 | * decide how it fits in with the other transformations here. |
| 4715 | */ |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4716 | void /* PRIVATE */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4717 | png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4718 | { |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4719 | png_debug(1, "in png_do_read_transformations"); |
Patrick Scott | 5f6bd84 | 2010-06-28 16:55:16 -0400 | [diff] [blame] | 4720 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4721 | if (png_ptr->row_buf == NULL) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4722 | { |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4723 | /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this |
| 4724 | * error is incredibly rare and incredibly easy to debug without this |
| 4725 | * information. |
| 4726 | */ |
| 4727 | png_error(png_ptr, "NULL row buffer"); |
| 4728 | } |
Chris Craik | b50c217 | 2013-07-29 15:28:30 -0700 | [diff] [blame] | 4729 | |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4730 | /* The following is debugging; prior to 1.5.4 the code was never compiled in; |
| 4731 | * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro |
| 4732 | * PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for |
| 4733 | * all transformations, however in practice the ROW_INIT always gets done on |
| 4734 | * demand, if necessary. |
| 4735 | */ |
| 4736 | if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 && |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4737 | (png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4738 | { |
| 4739 | /* Application has failed to call either png_read_start_image() or |
| 4740 | * png_read_update_info() after setting transforms that expand pixels. |
| 4741 | * This check added to libpng-1.2.19 (but not enabled until 1.5.4). |
| 4742 | */ |
| 4743 | png_error(png_ptr, "Uninitialized row"); |
| 4744 | } |
| 4745 | |
| 4746 | #ifdef PNG_READ_EXPAND_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4747 | if ((png_ptr->transformations & PNG_EXPAND) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4748 | { |
| 4749 | if (row_info->color_type == PNG_COLOR_TYPE_PALETTE) |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4750 | { |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4751 | png_do_expand_palette(row_info, png_ptr->row_buf + 1, |
| 4752 | png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4753 | } |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4754 | |
| 4755 | else |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4756 | { |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4757 | if (png_ptr->num_trans != 0 && |
| 4758 | (png_ptr->transformations & PNG_EXPAND_tRNS) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4759 | png_do_expand(row_info, png_ptr->row_buf + 1, |
| 4760 | &(png_ptr->trans_color)); |
Patrick Scott | a0bb96c | 2009-07-22 11:50:02 -0400 | [diff] [blame] | 4761 | |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4762 | else |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4763 | png_do_expand(row_info, png_ptr->row_buf + 1, |
| 4764 | NULL); |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 4765 | } |
| 4766 | } |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4767 | #endif |
| 4768 | |
| 4769 | #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4770 | if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && |
| 4771 | (png_ptr->transformations & PNG_COMPOSE) == 0 && |
| 4772 | (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 4773 | row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4774 | png_do_strip_channel(row_info, png_ptr->row_buf + 1, |
Alex Naidis | 7a055fd | 2016-10-01 12:23:07 +0200 | [diff] [blame] | 4775 | 0 /* at_start == false, because SWAP_ALPHA happens later */); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4776 | #endif |
| 4777 | |
| 4778 | #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4779 | if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4780 | { |
| 4781 | int rgb_error = |
| 4782 | png_do_rgb_to_gray(png_ptr, row_info, |
| 4783 | png_ptr->row_buf + 1); |
| 4784 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4785 | if (rgb_error != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4786 | { |
| 4787 | png_ptr->rgb_to_gray_status=1; |
| 4788 | if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == |
| 4789 | PNG_RGB_TO_GRAY_WARN) |
| 4790 | png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); |
| 4791 | |
| 4792 | if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == |
| 4793 | PNG_RGB_TO_GRAY_ERR) |
| 4794 | png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); |
| 4795 | } |
| 4796 | } |
| 4797 | #endif |
| 4798 | |
| 4799 | /* From Andreas Dilger e-mail to png-implement, 26 March 1998: |
| 4800 | * |
| 4801 | * In most cases, the "simple transparency" should be done prior to doing |
| 4802 | * gray-to-RGB, or you will have to test 3x as many bytes to check if a |
| 4803 | * pixel is transparent. You would also need to make sure that the |
| 4804 | * transparency information is upgraded to RGB. |
| 4805 | * |
| 4806 | * To summarize, the current flow is: |
| 4807 | * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite |
| 4808 | * with background "in place" if transparent, |
| 4809 | * convert to RGB if necessary |
| 4810 | * - Gray + alpha -> composite with gray background and remove alpha bytes, |
| 4811 | * convert to RGB if necessary |
| 4812 | * |
| 4813 | * To support RGB backgrounds for gray images we need: |
| 4814 | * - Gray + simple transparency -> convert to RGB + simple transparency, |
| 4815 | * compare 3 or 6 bytes and composite with |
| 4816 | * background "in place" if transparent |
| 4817 | * (3x compare/pixel compared to doing |
| 4818 | * composite with gray bkgrnd) |
| 4819 | * - Gray + alpha -> convert to RGB + alpha, composite with background and |
| 4820 | * remove alpha bytes (3x float |
| 4821 | * operations/pixel compared with composite |
| 4822 | * on gray background) |
| 4823 | * |
| 4824 | * Greg's change will do this. The reason it wasn't done before is for |
| 4825 | * performance, as this increases the per-pixel operations. If we would check |
| 4826 | * in advance if the background was gray or RGB, and position the gray-to-RGB |
| 4827 | * transform appropriately, then it would save a lot of work/time. |
| 4828 | */ |
| 4829 | |
| 4830 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
| 4831 | /* If gray -> RGB, do so now only if background is non-gray; else do later |
| 4832 | * for performance reasons |
| 4833 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4834 | if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 && |
| 4835 | (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) == 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4836 | png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); |
| 4837 | #endif |
| 4838 | |
| 4839 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ |
| 4840 | defined(PNG_READ_ALPHA_MODE_SUPPORTED) |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4841 | if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4842 | png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr); |
| 4843 | #endif |
| 4844 | |
| 4845 | #ifdef PNG_READ_GAMMA_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4846 | if ((png_ptr->transformations & PNG_GAMMA) != 0 && |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4847 | #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
| 4848 | /* Because RGB_TO_GRAY does the gamma transform. */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4849 | (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0 && |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4850 | #endif |
| 4851 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ |
| 4852 | defined(PNG_READ_ALPHA_MODE_SUPPORTED) |
| 4853 | /* Because PNG_COMPOSE does the gamma transform if there is something to |
| 4854 | * do (if there is an alpha channel or transparency.) |
| 4855 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4856 | !((png_ptr->transformations & PNG_COMPOSE) != 0 && |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4857 | ((png_ptr->num_trans != 0) || |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4858 | (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)) && |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4859 | #endif |
| 4860 | /* Because png_init_read_transformations transforms the palette, unless |
| 4861 | * RGB_TO_GRAY will do the transform. |
| 4862 | */ |
| 4863 | (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) |
| 4864 | png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr); |
| 4865 | #endif |
| 4866 | |
| 4867 | #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4868 | if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && |
| 4869 | (png_ptr->transformations & PNG_COMPOSE) != 0 && |
| 4870 | (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 4871 | row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4872 | png_do_strip_channel(row_info, png_ptr->row_buf + 1, |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4873 | 0 /* at_start == false, because SWAP_ALPHA happens later */); |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4874 | #endif |
| 4875 | |
| 4876 | #ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4877 | if ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 && |
| 4878 | (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4879 | png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr); |
| 4880 | #endif |
| 4881 | |
| 4882 | #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4883 | if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4884 | png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1); |
| 4885 | #endif |
| 4886 | |
| 4887 | #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
| 4888 | /* There is no harm in doing both of these because only one has any effect, |
| 4889 | * by putting the 'scale' option first if the app asks for scale (either by |
| 4890 | * calling the API or in a TRANSFORM flag) this is what happens. |
| 4891 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4892 | if ((png_ptr->transformations & PNG_16_TO_8) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4893 | png_do_chop(row_info, png_ptr->row_buf + 1); |
| 4894 | #endif |
| 4895 | |
| 4896 | #ifdef PNG_READ_QUANTIZE_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4897 | if ((png_ptr->transformations & PNG_QUANTIZE) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4898 | { |
| 4899 | png_do_quantize(row_info, png_ptr->row_buf + 1, |
| 4900 | png_ptr->palette_lookup, png_ptr->quantize_index); |
| 4901 | |
| 4902 | if (row_info->rowbytes == 0) |
| 4903 | png_error(png_ptr, "png_do_quantize returned rowbytes=0"); |
| 4904 | } |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4905 | #endif /* READ_QUANTIZE */ |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4906 | |
| 4907 | #ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 4908 | /* Do the expansion now, after all the arithmetic has been done. Notice |
| 4909 | * that previous transformations can handle the PNG_EXPAND_16 flag if this |
| 4910 | * is efficient (particularly true in the case of gamma correction, where |
| 4911 | * better accuracy results faster!) |
| 4912 | */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4913 | if ((png_ptr->transformations & PNG_EXPAND_16) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4914 | png_do_expand_16(row_info, png_ptr->row_buf + 1); |
| 4915 | #endif |
| 4916 | |
| 4917 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
| 4918 | /* NOTE: moved here in 1.5.4 (from much later in this list.) */ |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4919 | if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 && |
| 4920 | (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4921 | png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); |
| 4922 | #endif |
| 4923 | |
| 4924 | #ifdef PNG_READ_INVERT_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4925 | if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4926 | png_do_invert(row_info, png_ptr->row_buf + 1); |
| 4927 | #endif |
| 4928 | |
| 4929 | #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4930 | if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4931 | png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1); |
| 4932 | #endif |
| 4933 | |
| 4934 | #ifdef PNG_READ_SHIFT_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4935 | if ((png_ptr->transformations & PNG_SHIFT) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4936 | png_do_unshift(row_info, png_ptr->row_buf + 1, |
| 4937 | &(png_ptr->shift)); |
| 4938 | #endif |
| 4939 | |
| 4940 | #ifdef PNG_READ_PACK_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4941 | if ((png_ptr->transformations & PNG_PACK) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4942 | png_do_unpack(row_info, png_ptr->row_buf + 1); |
| 4943 | #endif |
| 4944 | |
| 4945 | #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 4946 | /* Added at libpng-1.5.10 */ |
| 4947 | if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && |
| 4948 | png_ptr->num_palette_max >= 0) |
| 4949 | png_do_check_palette_indexes(png_ptr, row_info); |
| 4950 | #endif |
| 4951 | |
| 4952 | #ifdef PNG_READ_BGR_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4953 | if ((png_ptr->transformations & PNG_BGR) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4954 | png_do_bgr(row_info, png_ptr->row_buf + 1); |
| 4955 | #endif |
| 4956 | |
| 4957 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4958 | if ((png_ptr->transformations & PNG_PACKSWAP) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4959 | png_do_packswap(row_info, png_ptr->row_buf + 1); |
| 4960 | #endif |
| 4961 | |
| 4962 | #ifdef PNG_READ_FILLER_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4963 | if ((png_ptr->transformations & PNG_FILLER) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4964 | png_do_read_filler(row_info, png_ptr->row_buf + 1, |
| 4965 | (png_uint_32)png_ptr->filler, png_ptr->flags); |
| 4966 | #endif |
| 4967 | |
| 4968 | #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4969 | if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4970 | png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1); |
| 4971 | #endif |
| 4972 | |
| 4973 | #ifdef PNG_READ_16BIT_SUPPORTED |
| 4974 | #ifdef PNG_READ_SWAP_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4975 | if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4976 | png_do_swap(row_info, png_ptr->row_buf + 1); |
| 4977 | #endif |
| 4978 | #endif |
| 4979 | |
| 4980 | #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4981 | if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) |
| 4982 | { |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4983 | if (png_ptr->read_user_transform_fn != NULL) |
| 4984 | (*(png_ptr->read_user_transform_fn)) /* User read transform function */ |
| 4985 | (png_ptr, /* png_ptr */ |
| 4986 | row_info, /* row_info: */ |
| 4987 | /* png_uint_32 width; width of row */ |
| 4988 | /* png_size_t rowbytes; number of bytes in row */ |
| 4989 | /* png_byte color_type; color type of pixels */ |
| 4990 | /* png_byte bit_depth; bit depth of samples */ |
| 4991 | /* png_byte channels; number of channels (1-4) */ |
| 4992 | /* png_byte pixel_depth; bits per pixel (depth*channels) */ |
| 4993 | png_ptr->row_buf + 1); /* start of pixel data for row */ |
| 4994 | #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4995 | if (png_ptr->user_transform_depth != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4996 | row_info->bit_depth = png_ptr->user_transform_depth; |
| 4997 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 4998 | if (png_ptr->user_transform_channels != 0) |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 4999 | row_info->channels = png_ptr->user_transform_channels; |
| 5000 | #endif |
| 5001 | row_info->pixel_depth = (png_byte)(row_info->bit_depth * |
| 5002 | row_info->channels); |
| 5003 | |
| 5004 | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width); |
| 5005 | } |
| 5006 | #endif |
The Android Open Source Project | 893912b | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 5007 | } |
Sireesh Tripurari | b478e66 | 2014-05-09 15:15:10 +0530 | [diff] [blame] | 5008 | |
Matt Sarett | 9b1fe63 | 2015-11-25 10:21:17 -0500 | [diff] [blame] | 5009 | #endif /* READ_TRANSFORMS */ |
| 5010 | #endif /* READ */ |