Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2 | /* pngrtran.c - transforms the data in a row for PNG readers |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3 | * |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame^] | 4 | * libpng 1.2.2beta2 - February 24, 2002 |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 5 | * For conditions of distribution and use, see copyright notice in png.h |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame^] | 6 | * Copyright (c) 1998-2002 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 7 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 8 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 9 | * |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 10 | * This file contains functions optionally called by an application |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 11 | * in order to tell libpng how to handle data when reading a PNG. |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 12 | * Transformations that are used in both reading and writing are |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 13 | * in pngtrans.c. |
| 14 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 15 | |
| 16 | #define PNG_INTERNAL |
| 17 | #include "png.h" |
| 18 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 19 | /* Set the action on getting a CRC error for an ancillary or critical chunk. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 20 | void PNGAPI |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 21 | png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action) |
| 22 | { |
| 23 | png_debug(1, "in png_set_crc_action\n"); |
| 24 | /* Tell libpng how we react to CRC errors in critical chunks */ |
| 25 | switch (crit_action) |
| 26 | { |
| 27 | case PNG_CRC_NO_CHANGE: /* leave setting as is */ |
| 28 | break; |
| 29 | case PNG_CRC_WARN_USE: /* warn/use data */ |
| 30 | png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; |
| 31 | png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE; |
| 32 | break; |
| 33 | case PNG_CRC_QUIET_USE: /* quiet/use data */ |
| 34 | png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; |
| 35 | png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE | |
| 36 | PNG_FLAG_CRC_CRITICAL_IGNORE; |
| 37 | break; |
| 38 | case PNG_CRC_WARN_DISCARD: /* not a valid action for critical data */ |
| 39 | png_warning(png_ptr, "Can't discard critical data on CRC error."); |
| 40 | case PNG_CRC_ERROR_QUIT: /* error/quit */ |
| 41 | case PNG_CRC_DEFAULT: |
| 42 | default: |
| 43 | png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; |
| 44 | break; |
| 45 | } |
| 46 | |
| 47 | switch (ancil_action) |
| 48 | { |
| 49 | case PNG_CRC_NO_CHANGE: /* leave setting as is */ |
| 50 | break; |
| 51 | case PNG_CRC_WARN_USE: /* warn/use data */ |
| 52 | png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
| 53 | png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE; |
| 54 | break; |
| 55 | case PNG_CRC_QUIET_USE: /* quiet/use data */ |
| 56 | png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
| 57 | png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE | |
| 58 | PNG_FLAG_CRC_ANCILLARY_NOWARN; |
| 59 | break; |
| 60 | case PNG_CRC_ERROR_QUIT: /* error/quit */ |
| 61 | png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
| 62 | png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN; |
| 63 | break; |
| 64 | case PNG_CRC_WARN_DISCARD: /* warn/discard data */ |
| 65 | case PNG_CRC_DEFAULT: |
| 66 | default: |
| 67 | png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 72 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ |
| 73 | defined(PNG_FLOATING_POINT_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 74 | /* handle alpha and tRNS via a background color */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 75 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 76 | png_set_background(png_structp png_ptr, |
| 77 | png_color_16p background_color, int background_gamma_code, |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 78 | int need_expand, double background_gamma) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 79 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 80 | png_debug(1, "in png_set_background\n"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 81 | if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) |
| 82 | { |
| 83 | png_warning(png_ptr, "Application must supply a known background gamma"); |
| 84 | return; |
| 85 | } |
| 86 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 87 | png_ptr->transformations |= PNG_BACKGROUND; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 88 | png_memcpy(&(png_ptr->background), background_color, sizeof(png_color_16)); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 89 | png_ptr->background_gamma = (float)background_gamma; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 90 | png_ptr->background_gamma_type = (png_byte)(background_gamma_code); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 91 | png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 92 | |
| 93 | /* Note: if need_expand is set and color_type is either RGB or RGB_ALPHA |
| 94 | * (in which case need_expand is superfluous anyway), the background color |
| 95 | * might actually be gray yet not be flagged as such. This is not a problem |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 96 | * for the current code, which uses PNG_BACKGROUND_IS_GRAY only to |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 97 | * decide when to do the png_do_gray_to_rgb() transformation. |
| 98 | */ |
| 99 | if ((need_expand && !(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) || |
| 100 | (!need_expand && background_color->red == background_color->green && |
| 101 | background_color->red == background_color->blue)) |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 102 | png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 103 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 104 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 105 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 106 | #if defined(PNG_READ_16_TO_8_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 107 | /* strip 16 bit depth files to 8 bit depth */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 108 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 109 | png_set_strip_16(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 110 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 111 | png_debug(1, "in png_set_strip_16\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 112 | png_ptr->transformations |= PNG_16_TO_8; |
| 113 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 114 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 115 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 116 | #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 117 | void PNGAPI |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 118 | png_set_strip_alpha(png_structp png_ptr) |
| 119 | { |
| 120 | png_debug(1, "in png_set_strip_alpha\n"); |
| 121 | png_ptr->transformations |= PNG_STRIP_ALPHA; |
| 122 | } |
| 123 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 124 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 125 | #if defined(PNG_READ_DITHER_SUPPORTED) |
| 126 | /* Dither file to 8 bit. Supply a palette, the current number |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 127 | * of elements in the palette, the maximum number of elements |
| 128 | * allowed, and a histogram if possible. If the current number |
| 129 | * of colors is greater then the maximum number, the palette will be |
| 130 | * modified to fit in the maximum number. "full_dither" indicates |
| 131 | * whether we need a dithering cube set up for RGB images, or if we |
| 132 | * simply are reducing the number of colors in a paletted image. |
| 133 | */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 134 | |
| 135 | typedef struct png_dsort_struct |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 136 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 137 | struct png_dsort_struct FAR * next; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 138 | png_byte left; |
| 139 | png_byte right; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 140 | } png_dsort; |
| 141 | typedef png_dsort FAR * png_dsortp; |
| 142 | typedef png_dsort FAR * FAR * png_dsortpp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 143 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 144 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 145 | png_set_dither(png_structp png_ptr, png_colorp palette, |
| 146 | int num_palette, int maximum_colors, png_uint_16p histogram, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 147 | int full_dither) |
| 148 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 149 | png_debug(1, "in png_set_dither\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 150 | png_ptr->transformations |= PNG_DITHER; |
| 151 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 152 | if (!full_dither) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 153 | { |
| 154 | int i; |
| 155 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 156 | png_ptr->dither_index = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 157 | (png_uint_32)(num_palette * sizeof (png_byte))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 158 | for (i = 0; i < num_palette; i++) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 159 | png_ptr->dither_index[i] = (png_byte)i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | if (num_palette > maximum_colors) |
| 163 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 164 | if (histogram != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 165 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 166 | /* This is easy enough, just throw out the least used colors. |
| 167 | Perhaps not the best solution, but good enough. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 168 | |
| 169 | int i; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 170 | png_bytep sort; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 171 | |
| 172 | /* initialize an array to sort colors */ |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 173 | sort = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_palette |
| 174 | * sizeof (png_byte))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 175 | |
| 176 | /* initialize the sort array */ |
| 177 | for (i = 0; i < num_palette; i++) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 178 | sort[i] = (png_byte)i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 179 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 180 | /* Find the least used palette entries by starting a |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 181 | bubble sort, and running it until we have sorted |
| 182 | out enough colors. Note that we don't care about |
| 183 | sorting all the colors, just finding which are |
| 184 | least used. */ |
| 185 | |
| 186 | for (i = num_palette - 1; i >= maximum_colors; i--) |
| 187 | { |
| 188 | int done; /* to stop early if the list is pre-sorted */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 189 | int j; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 190 | |
| 191 | done = 1; |
| 192 | for (j = 0; j < i; j++) |
| 193 | { |
| 194 | if (histogram[sort[j]] < histogram[sort[j + 1]]) |
| 195 | { |
| 196 | png_byte t; |
| 197 | |
| 198 | t = sort[j]; |
| 199 | sort[j] = sort[j + 1]; |
| 200 | sort[j + 1] = t; |
| 201 | done = 0; |
| 202 | } |
| 203 | } |
| 204 | if (done) |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | /* swap the palette around, and set up a table, if necessary */ |
| 209 | if (full_dither) |
| 210 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 211 | int j = num_palette; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 212 | |
| 213 | /* put all the useful colors within the max, but don't |
| 214 | move the others */ |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 215 | for (i = 0; i < maximum_colors; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 216 | { |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 217 | if ((int)sort[i] >= maximum_colors) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 218 | { |
| 219 | do |
| 220 | j--; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 221 | while ((int)sort[j] >= maximum_colors); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 222 | palette[i] = palette[j]; |
| 223 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 224 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 225 | } |
| 226 | else |
| 227 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 228 | int j = num_palette; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 229 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 230 | /* move all the used colors inside the max limit, and |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 231 | develop a translation table */ |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 232 | for (i = 0; i < maximum_colors; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 233 | { |
| 234 | /* only move the colors we need to */ |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 235 | if ((int)sort[i] >= maximum_colors) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 236 | { |
| 237 | png_color tmp_color; |
| 238 | |
| 239 | do |
| 240 | j--; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 241 | while ((int)sort[j] >= maximum_colors); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 242 | |
| 243 | tmp_color = palette[j]; |
| 244 | palette[j] = palette[i]; |
| 245 | palette[i] = tmp_color; |
| 246 | /* indicate where the color went */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 247 | png_ptr->dither_index[j] = (png_byte)i; |
| 248 | png_ptr->dither_index[i] = (png_byte)j; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 249 | } |
| 250 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 251 | |
| 252 | /* find closest color for those colors we are not using */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 253 | for (i = 0; i < num_palette; i++) |
| 254 | { |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 255 | if ((int)png_ptr->dither_index[i] >= maximum_colors) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 256 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 257 | int min_d, k, min_k, d_index; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 258 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 259 | /* find the closest color to one we threw out */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 260 | d_index = png_ptr->dither_index[i]; |
| 261 | min_d = PNG_COLOR_DIST(palette[d_index], palette[0]); |
| 262 | for (k = 1, min_k = 0; k < maximum_colors; k++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 263 | { |
| 264 | int d; |
| 265 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 266 | d = PNG_COLOR_DIST(palette[d_index], palette[k]); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 267 | |
| 268 | if (d < min_d) |
| 269 | { |
| 270 | min_d = d; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 271 | min_k = k; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 272 | } |
| 273 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 274 | /* point to closest color */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 275 | png_ptr->dither_index[i] = (png_byte)min_k; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 279 | png_free(png_ptr, sort); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 280 | } |
| 281 | else |
| 282 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 283 | /* This is much harder to do simply (and quickly). Perhaps |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 284 | we need to go through a median cut routine, but those |
| 285 | don't always behave themselves with only a few colors |
| 286 | as input. So we will just find the closest two colors, |
| 287 | and throw out one of them (chosen somewhat randomly). |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 288 | [We don't understand this at all, so if someone wants to |
| 289 | work on improving it, be our guest - AED, GRP] |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 290 | */ |
| 291 | int i; |
| 292 | int max_d; |
| 293 | int num_new_palette; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 294 | png_dsortpp hash; |
| 295 | png_bytep index_to_palette; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 296 | /* where the original index currently is in the palette */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 297 | png_bytep palette_to_index; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 298 | /* which original index points to this palette color */ |
| 299 | |
| 300 | /* initialize palette index arrays */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 301 | index_to_palette = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 302 | (png_uint_32)(num_palette * sizeof (png_byte))); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 303 | palette_to_index = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 304 | (png_uint_32)(num_palette * sizeof (png_byte))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 305 | |
| 306 | /* initialize the sort array */ |
| 307 | for (i = 0; i < num_palette; i++) |
| 308 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 309 | index_to_palette[i] = (png_byte)i; |
| 310 | palette_to_index[i] = (png_byte)i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 313 | hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 * |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 314 | sizeof (png_dsortp))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 315 | for (i = 0; i < 769; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 316 | hash[i] = NULL; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 317 | /* png_memset(hash, 0, 769 * sizeof (png_dsortp)); */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 318 | |
| 319 | num_new_palette = num_palette; |
| 320 | |
| 321 | /* initial wild guess at how far apart the farthest pixel |
| 322 | pair we will be eliminating will be. Larger |
| 323 | numbers mean more areas will be allocated, Smaller |
| 324 | numbers run the risk of not saving enough data, and |
| 325 | having to do this all over again. |
| 326 | |
| 327 | I have not done extensive checking on this number. |
| 328 | */ |
| 329 | max_d = 96; |
| 330 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 331 | while (num_new_palette > maximum_colors) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 332 | { |
| 333 | for (i = 0; i < num_new_palette - 1; i++) |
| 334 | { |
| 335 | int j; |
| 336 | |
| 337 | for (j = i + 1; j < num_new_palette; j++) |
| 338 | { |
| 339 | int d; |
| 340 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 341 | d = PNG_COLOR_DIST(palette[i], palette[j]); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 342 | |
| 343 | if (d <= max_d) |
| 344 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 345 | png_dsortp t; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 346 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 347 | t = (png_dsortp)png_malloc(png_ptr, (png_uint_32)(sizeof |
| 348 | (png_dsort))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 349 | t->next = hash[d]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 350 | t->left = (png_byte)i; |
| 351 | t->right = (png_byte)j; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 352 | hash[d] = t; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | for (i = 0; i <= max_d; i++) |
| 358 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 359 | if (hash[i] != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 360 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 361 | png_dsortp p; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 362 | |
| 363 | for (p = hash[i]; p; p = p->next) |
| 364 | { |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 365 | if ((int)index_to_palette[p->left] < num_new_palette && |
| 366 | (int)index_to_palette[p->right] < num_new_palette) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 367 | { |
| 368 | int j, next_j; |
| 369 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 370 | if (num_new_palette & 0x01) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 371 | { |
| 372 | j = p->left; |
| 373 | next_j = p->right; |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | j = p->right; |
| 378 | next_j = p->left; |
| 379 | } |
| 380 | |
| 381 | num_new_palette--; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 382 | palette[index_to_palette[j]] = palette[num_new_palette]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 383 | if (!full_dither) |
| 384 | { |
| 385 | int k; |
| 386 | |
| 387 | for (k = 0; k < num_palette; k++) |
| 388 | { |
| 389 | if (png_ptr->dither_index[k] == |
| 390 | index_to_palette[j]) |
| 391 | png_ptr->dither_index[k] = |
| 392 | index_to_palette[next_j]; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 393 | if ((int)png_ptr->dither_index[k] == |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 394 | num_new_palette) |
| 395 | png_ptr->dither_index[k] = |
| 396 | index_to_palette[j]; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | index_to_palette[palette_to_index[num_new_palette]] = |
| 401 | index_to_palette[j]; |
| 402 | palette_to_index[index_to_palette[j]] = |
| 403 | palette_to_index[num_new_palette]; |
| 404 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 405 | index_to_palette[j] = (png_byte)num_new_palette; |
| 406 | palette_to_index[num_new_palette] = (png_byte)j; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 407 | } |
| 408 | if (num_new_palette <= maximum_colors) |
| 409 | break; |
| 410 | } |
| 411 | if (num_new_palette <= maximum_colors) |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | for (i = 0; i < 769; i++) |
| 417 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 418 | if (hash[i] != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 419 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 420 | png_dsortp p = hash[i]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 421 | while (p) |
| 422 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 423 | png_dsortp t; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 424 | |
| 425 | t = p->next; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 426 | png_free(png_ptr, p); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 427 | p = t; |
| 428 | } |
| 429 | } |
| 430 | hash[i] = 0; |
| 431 | } |
| 432 | max_d += 96; |
| 433 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 434 | png_free(png_ptr, hash); |
| 435 | png_free(png_ptr, palette_to_index); |
| 436 | png_free(png_ptr, index_to_palette); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 437 | } |
| 438 | num_palette = maximum_colors; |
| 439 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 440 | if (png_ptr->palette == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 441 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 442 | png_ptr->palette = palette; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 443 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 444 | png_ptr->num_palette = (png_uint_16)num_palette; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 445 | |
| 446 | if (full_dither) |
| 447 | { |
| 448 | int i; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 449 | png_bytep distance; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 450 | int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS + |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 451 | PNG_DITHER_BLUE_BITS; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 452 | int num_red = (1 << PNG_DITHER_RED_BITS); |
| 453 | int num_green = (1 << PNG_DITHER_GREEN_BITS); |
| 454 | int num_blue = (1 << PNG_DITHER_BLUE_BITS); |
| 455 | png_size_t num_entries = ((png_size_t)1 << total_bits); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 456 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 457 | png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 458 | (png_uint_32)(num_entries * sizeof (png_byte))); |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 459 | |
| 460 | png_memset(png_ptr->palette_lookup, 0, num_entries * sizeof (png_byte)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 461 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 462 | distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries * |
| 463 | sizeof(png_byte))); |
Glenn Randers-Pehrson | e1eff58 | 2001-04-14 20:15:41 -0500 | [diff] [blame] | 464 | |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 465 | png_memset(distance, 0xff, num_entries * sizeof(png_byte)); |
| 466 | |
| 467 | for (i = 0; i < num_palette; i++) |
| 468 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 469 | int ir, ig, ib; |
| 470 | int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS)); |
| 471 | int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS)); |
| 472 | int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 473 | |
| 474 | for (ir = 0; ir < num_red; ir++) |
| 475 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 476 | int dr = abs(ir - r); |
| 477 | int index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 478 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 479 | for (ig = 0; ig < num_green; ig++) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 480 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 481 | int dg = abs(ig - g); |
| 482 | int dt = dr + dg; |
| 483 | int dm = ((dr > dg) ? dr : dg); |
| 484 | int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 485 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 486 | for (ib = 0; ib < num_blue; ib++) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 487 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 488 | int d_index = index_g | ib; |
| 489 | int db = abs(ib - b); |
| 490 | int dmax = ((dm > db) ? dm : db); |
| 491 | int d = dmax + dt + db; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 492 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 493 | if (d < (int)distance[d_index]) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 494 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 495 | distance[d_index] = (png_byte)d; |
| 496 | png_ptr->palette_lookup[d_index] = (png_byte)i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 497 | } |
| 498 | } |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 502 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 503 | png_free(png_ptr, distance); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 504 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 505 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 506 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 507 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 508 | #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 509 | /* Transform the image from the file_gamma to the screen_gamma. We |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 510 | * only do transformations on images where the file_gamma and screen_gamma |
| 511 | * are not close reciprocals, otherwise it slows things down slightly, and |
| 512 | * also needlessly introduces small errors. |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame^] | 513 | * |
| 514 | * We will turn off gamma transformation later if no semitransparent entries |
| 515 | * are present in the tRNS array for palette images. We can't do it here |
| 516 | * because we don't necessarily have the tRNS chunk yet. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 517 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 518 | void PNGAPI |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 519 | png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 520 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 521 | png_debug(1, "in png_set_gamma\n"); |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame^] | 522 | if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) || |
| 523 | (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) || |
| 524 | (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) |
| 525 | png_ptr->transformations |= PNG_GAMMA; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 526 | png_ptr->gamma = (float)file_gamma; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 527 | png_ptr->screen_gamma = (float)scrn_gamma; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 528 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 529 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 530 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 531 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
Glenn Randers-Pehrson | 352ca6b | 1999-09-18 15:49:20 -0500 | [diff] [blame] | 532 | /* Expand paletted images to RGB, expand grayscale images of |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 533 | * less than 8-bit depth to 8-bit depth, and expand tRNS chunks |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 534 | * to alpha channels. |
| 535 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 536 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 537 | png_set_expand(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 538 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 539 | png_debug(1, "in png_set_expand\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 540 | png_ptr->transformations |= PNG_EXPAND; |
| 541 | } |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 542 | |
| 543 | /* GRR 19990627: the following three functions currently are identical |
| 544 | * to png_set_expand(). However, it is entirely reasonable that someone |
| 545 | * might wish to expand an indexed image to RGB but *not* expand a single, |
| 546 | * fully transparent palette entry to a full alpha channel--perhaps instead |
| 547 | * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace |
| 548 | * the transparent color with a particular RGB value, or drop tRNS entirely. |
| 549 | * IOW, a future version of the library may make the transformations flag |
| 550 | * a bit more fine-grained, with separate bits for each of these three |
| 551 | * functions. |
| 552 | * |
| 553 | * More to the point, these functions make it obvious what libpng will be |
| 554 | * doing, whereas "expand" can (and does) mean any number of things. |
| 555 | */ |
| 556 | |
| 557 | /* Expand paletted images to RGB. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 558 | void PNGAPI |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 559 | png_set_palette_to_rgb(png_structp png_ptr) |
| 560 | { |
| 561 | png_debug(1, "in png_set_expand\n"); |
| 562 | png_ptr->transformations |= PNG_EXPAND; |
| 563 | } |
| 564 | |
| 565 | /* Expand grayscale images of less than 8-bit depth to 8 bits. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 566 | void PNGAPI |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 567 | png_set_gray_1_2_4_to_8(png_structp png_ptr) |
| 568 | { |
| 569 | png_debug(1, "in png_set_expand\n"); |
| 570 | png_ptr->transformations |= PNG_EXPAND; |
| 571 | } |
| 572 | |
| 573 | /* Expand tRNS chunks to alpha channels. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 574 | void PNGAPI |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 575 | png_set_tRNS_to_alpha(png_structp png_ptr) |
| 576 | { |
| 577 | png_debug(1, "in png_set_expand\n"); |
| 578 | png_ptr->transformations |= PNG_EXPAND; |
| 579 | } |
| 580 | #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 581 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 582 | #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 583 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 584 | png_set_gray_to_rgb(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 585 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 586 | png_debug(1, "in png_set_gray_to_rgb\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 587 | png_ptr->transformations |= PNG_GRAY_TO_RGB; |
| 588 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 589 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 590 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 591 | #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
| 592 | #if defined(PNG_FLOATING_POINT_SUPPORTED) |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 593 | /* Convert a RGB image to a grayscale of the same width. This allows us, |
| 594 | * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 595 | */ |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 596 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 597 | void PNGAPI |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 598 | png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red, |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 599 | double green) |
| 600 | { |
| 601 | int red_fixed = (int)((float)red*100000.0 + 0.5); |
| 602 | int green_fixed = (int)((float)green*100000.0 + 0.5); |
| 603 | png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed); |
| 604 | } |
| 605 | #endif |
| 606 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 607 | void PNGAPI |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 608 | png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action, |
| 609 | png_fixed_point red, png_fixed_point green) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 610 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 611 | png_debug(1, "in png_set_rgb_to_gray\n"); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 612 | switch(error_action) |
| 613 | { |
| 614 | case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY; |
| 615 | break; |
| 616 | case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN; |
| 617 | break; |
| 618 | case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR; |
| 619 | } |
| 620 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 621 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
| 622 | png_ptr->transformations |= PNG_EXPAND; |
| 623 | #else |
| 624 | { |
| 625 | png_warning(png_ptr, "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED."); |
| 626 | png_ptr->transformations &= ~PNG_RGB_TO_GRAY; |
| 627 | } |
| 628 | #endif |
| 629 | { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 630 | png_uint_16 red_int, green_int; |
Glenn Randers-Pehrson | 68ea243 | 2000-04-01 21:10:05 -0600 | [diff] [blame] | 631 | if(red < 0 || green < 0) |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 632 | { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 633 | red_int = 6968; /* .212671 * 32768 + .5 */ |
| 634 | green_int = 23434; /* .715160 * 32768 + .5 */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 635 | } |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 636 | else if(red + green < 100000L) |
| 637 | { |
| 638 | red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L); |
| 639 | green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L); |
| 640 | } |
| 641 | else |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 642 | { |
| 643 | png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients"); |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 644 | red_int = 6968; |
| 645 | green_int = 23434; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 646 | } |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 647 | png_ptr->rgb_to_gray_red_coeff = red_int; |
| 648 | png_ptr->rgb_to_gray_green_coeff = green_int; |
| 649 | png_ptr->rgb_to_gray_blue_coeff = (png_uint_16)(32768-red_int-green_int); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 650 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 651 | } |
| 652 | #endif |
| 653 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 654 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ |
| 655 | defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \ |
| 656 | defined(PNG_LEGACY_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 657 | void PNGAPI |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 658 | png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr |
| 659 | read_user_transform_fn) |
| 660 | { |
| 661 | png_debug(1, "in png_set_read_user_transform_fn\n"); |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 662 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 663 | png_ptr->transformations |= PNG_USER_TRANSFORM; |
| 664 | png_ptr->read_user_transform_fn = read_user_transform_fn; |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 665 | #endif |
| 666 | #ifdef PNG_LEGACY_SUPPORTED |
| 667 | if(read_user_transform_fn) |
| 668 | png_warning(png_ptr, |
| 669 | "This version of libpng does not support user transforms"); |
| 670 | #endif |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 671 | } |
| 672 | #endif |
| 673 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 674 | /* Initialize everything needed for the read. This includes modifying |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 675 | * the palette. |
| 676 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 677 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 678 | png_init_read_transformations(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 679 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 680 | png_debug(1, "in png_init_read_transformations\n"); |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 681 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 682 | if(png_ptr != NULL) |
| 683 | #endif |
| 684 | { |
| 685 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \ |
| 686 | || defined(PNG_READ_GAMMA_SUPPORTED) |
| 687 | int color_type = png_ptr->color_type; |
| 688 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 689 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 690 | #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 691 | if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && |
| 692 | (png_ptr->transformations & PNG_EXPAND)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 693 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 694 | if (!(color_type & PNG_COLOR_MASK_COLOR)) /* i.e., GRAY or GRAY_ALPHA */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 695 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 696 | /* expand background chunk. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 697 | switch (png_ptr->bit_depth) |
| 698 | { |
| 699 | case 1: |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 700 | png_ptr->background.gray *= (png_uint_16)0xff; |
| 701 | png_ptr->background.red = png_ptr->background.green = |
| 702 | png_ptr->background.blue = png_ptr->background.gray; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 703 | break; |
| 704 | case 2: |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 705 | png_ptr->background.gray *= (png_uint_16)0x55; |
| 706 | png_ptr->background.red = png_ptr->background.green = |
| 707 | png_ptr->background.blue = png_ptr->background.gray; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 708 | break; |
| 709 | case 4: |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 710 | png_ptr->background.gray *= (png_uint_16)0x11; |
| 711 | png_ptr->background.red = png_ptr->background.green = |
| 712 | png_ptr->background.blue = png_ptr->background.gray; |
| 713 | break; |
| 714 | case 8: |
| 715 | case 16: |
| 716 | png_ptr->background.red = png_ptr->background.green = |
| 717 | png_ptr->background.blue = png_ptr->background.gray; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 718 | break; |
| 719 | } |
| 720 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 721 | else if (color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 722 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 723 | png_ptr->background.red = |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 724 | png_ptr->palette[png_ptr->background.index].red; |
| 725 | png_ptr->background.green = |
| 726 | png_ptr->palette[png_ptr->background.index].green; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 727 | png_ptr->background.blue = |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 728 | png_ptr->palette[png_ptr->background.index].blue; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 729 | |
| 730 | #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) |
| 731 | if (png_ptr->transformations & PNG_INVERT_ALPHA) |
| 732 | { |
| 733 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
Glenn Randers-Pehrson | 8f8fb6a | 1998-03-09 23:02:06 -0600 | [diff] [blame] | 734 | if (!(png_ptr->transformations & PNG_EXPAND)) |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 735 | #endif |
| 736 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 737 | /* invert the alpha channel (in tRNS) unless the pixels are |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 738 | going to be expanded, in which case leave it for later */ |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 739 | int i,istop; |
| 740 | istop=(int)png_ptr->num_trans; |
| 741 | for (i=0; i<istop; i++) |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 742 | png_ptr->trans[i] = (png_byte)(255 - png_ptr->trans[i]); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | #endif |
| 746 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 747 | } |
| 748 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 749 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 750 | |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 751 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 752 | png_ptr->background_1 = png_ptr->background; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 753 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 754 | #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame^] | 755 | |
| 756 | if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0) |
| 757 | && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0) |
| 758 | < PNG_GAMMA_THRESHOLD)) |
| 759 | { |
| 760 | int i,k; |
| 761 | k=0; |
| 762 | for (i=0; i<png_ptr->num_trans; i++) |
| 763 | { |
| 764 | if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff) |
| 765 | k=1; /* partial transparency is present */ |
| 766 | } |
| 767 | if (k == 0) |
| 768 | png_ptr->transformations &= (~PNG_GAMMA); |
| 769 | } |
| 770 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 771 | if (png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 772 | { |
| 773 | png_build_gamma_table(png_ptr); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 774 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 775 | if (png_ptr->transformations & PNG_BACKGROUND) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 776 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 777 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
| 778 | { |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame^] | 779 | /* could skip if no transparency and |
| 780 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 781 | png_color back, back_1; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 782 | png_colorp palette = png_ptr->palette; |
| 783 | int num_palette = png_ptr->num_palette; |
| 784 | int i; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 785 | if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) |
| 786 | { |
| 787 | back.red = png_ptr->gamma_table[png_ptr->background.red]; |
| 788 | back.green = png_ptr->gamma_table[png_ptr->background.green]; |
| 789 | back.blue = png_ptr->gamma_table[png_ptr->background.blue]; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 790 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 791 | back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; |
| 792 | back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; |
| 793 | back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; |
| 794 | } |
| 795 | else |
| 796 | { |
Glenn Randers-Pehrson | 8f8fb6a | 1998-03-09 23:02:06 -0600 | [diff] [blame] | 797 | double g, gs; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 798 | |
Glenn Randers-Pehrson | 8f8fb6a | 1998-03-09 23:02:06 -0600 | [diff] [blame] | 799 | switch (png_ptr->background_gamma_type) |
Glenn Randers-Pehrson | 4922b1b | 1998-03-08 22:55:17 -0600 | [diff] [blame] | 800 | { |
Glenn Randers-Pehrson | 8f8fb6a | 1998-03-09 23:02:06 -0600 | [diff] [blame] | 801 | case PNG_BACKGROUND_GAMMA_SCREEN: |
| 802 | g = (png_ptr->screen_gamma); |
| 803 | gs = 1.0; |
| 804 | break; |
| 805 | case PNG_BACKGROUND_GAMMA_FILE: |
| 806 | g = 1.0 / (png_ptr->gamma); |
| 807 | gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); |
| 808 | break; |
| 809 | case PNG_BACKGROUND_GAMMA_UNIQUE: |
| 810 | g = 1.0 / (png_ptr->background_gamma); |
| 811 | gs = 1.0 / (png_ptr->background_gamma * |
| 812 | png_ptr->screen_gamma); |
| 813 | break; |
| 814 | default: |
| 815 | g = 1.0; /* back_1 */ |
| 816 | gs = 1.0; /* back */ |
| 817 | } |
| 818 | |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 819 | if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD) |
Glenn Randers-Pehrson | 8f8fb6a | 1998-03-09 23:02:06 -0600 | [diff] [blame] | 820 | { |
| 821 | back.red = (png_byte)png_ptr->background.red; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 822 | back.green = (png_byte)png_ptr->background.green; |
Glenn Randers-Pehrson | 8f8fb6a | 1998-03-09 23:02:06 -0600 | [diff] [blame] | 823 | back.blue = (png_byte)png_ptr->background.blue; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 824 | } |
| 825 | else |
| 826 | { |
Glenn Randers-Pehrson | 8f8fb6a | 1998-03-09 23:02:06 -0600 | [diff] [blame] | 827 | back.red = (png_byte)(pow( |
| 828 | (double)png_ptr->background.red/255, gs) * 255.0 + .5); |
| 829 | back.green = (png_byte)(pow( |
| 830 | (double)png_ptr->background.green/255, gs) * 255.0 + .5); |
| 831 | back.blue = (png_byte)(pow( |
| 832 | (double)png_ptr->background.blue/255, gs) * 255.0 + .5); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 833 | } |
| 834 | |
Glenn Randers-Pehrson | 8f8fb6a | 1998-03-09 23:02:06 -0600 | [diff] [blame] | 835 | back_1.red = (png_byte)(pow( |
| 836 | (double)png_ptr->background.red/255, g) * 255.0 + .5); |
| 837 | back_1.green = (png_byte)(pow( |
| 838 | (double)png_ptr->background.green/255, g) * 255.0 + .5); |
| 839 | back_1.blue = (png_byte)(pow( |
| 840 | (double)png_ptr->background.blue/255, g) * 255.0 + .5); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 841 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 842 | for (i = 0; i < num_palette; i++) |
| 843 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 844 | if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 845 | { |
| 846 | if (png_ptr->trans[i] == 0) |
| 847 | { |
| 848 | palette[i] = back; |
| 849 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 850 | else /* if (png_ptr->trans[i] != 0xff) */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 851 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 852 | png_byte v, w; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 853 | |
| 854 | v = png_ptr->gamma_to_1[palette[i].red]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 855 | png_composite(w, v, png_ptr->trans[i], back_1.red); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 856 | palette[i].red = png_ptr->gamma_from_1[w]; |
| 857 | |
| 858 | v = png_ptr->gamma_to_1[palette[i].green]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 859 | png_composite(w, v, png_ptr->trans[i], back_1.green); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 860 | palette[i].green = png_ptr->gamma_from_1[w]; |
| 861 | |
| 862 | v = png_ptr->gamma_to_1[palette[i].blue]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 863 | png_composite(w, v, png_ptr->trans[i], back_1.blue); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 864 | palette[i].blue = png_ptr->gamma_from_1[w]; |
| 865 | } |
| 866 | } |
| 867 | else |
| 868 | { |
| 869 | palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 870 | palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 871 | palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 872 | } |
| 873 | } |
| 874 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 875 | /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN)*/ |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 876 | else |
| 877 | /* color_type != PNG_COLOR_TYPE_PALETTE */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 878 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 879 | double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1); |
| 880 | double g = 1.0; |
| 881 | double gs = 1.0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 882 | |
| 883 | switch (png_ptr->background_gamma_type) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 884 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 885 | case PNG_BACKGROUND_GAMMA_SCREEN: |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 886 | g = (png_ptr->screen_gamma); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 887 | gs = 1.0; |
| 888 | break; |
| 889 | case PNG_BACKGROUND_GAMMA_FILE: |
| 890 | g = 1.0 / (png_ptr->gamma); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 891 | gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 892 | break; |
| 893 | case PNG_BACKGROUND_GAMMA_UNIQUE: |
| 894 | g = 1.0 / (png_ptr->background_gamma); |
| 895 | gs = 1.0 / (png_ptr->background_gamma * |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 896 | png_ptr->screen_gamma); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 897 | break; |
| 898 | } |
| 899 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 900 | if (color_type & PNG_COLOR_MASK_COLOR) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 901 | { |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 902 | /* RGB or RGBA */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 903 | png_ptr->background_1.red = (png_uint_16)(pow( |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 904 | (double)png_ptr->background.red / m, g) * m + .5); |
| 905 | png_ptr->background_1.green = (png_uint_16)(pow( |
| 906 | (double)png_ptr->background.green / m, g) * m + .5); |
| 907 | png_ptr->background_1.blue = (png_uint_16)(pow( |
| 908 | (double)png_ptr->background.blue / m, g) * m + .5); |
| 909 | png_ptr->background.red = (png_uint_16)(pow( |
| 910 | (double)png_ptr->background.red / m, gs) * m + .5); |
| 911 | png_ptr->background.green = (png_uint_16)(pow( |
| 912 | (double)png_ptr->background.green / m, gs) * m + .5); |
| 913 | png_ptr->background.blue = (png_uint_16)(pow( |
| 914 | (double)png_ptr->background.blue / m, gs) * m + .5); |
| 915 | } |
| 916 | else |
| 917 | { |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 918 | /* GRAY or GRAY ALPHA */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 919 | png_ptr->background_1.gray = (png_uint_16)(pow( |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 920 | (double)png_ptr->background.gray / m, g) * m + .5); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 921 | png_ptr->background.gray = (png_uint_16)(pow( |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 922 | (double)png_ptr->background.gray / m, gs) * m + .5); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 926 | else |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 927 | /* transformation does not include PNG_BACKGROUND */ |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 928 | #endif /* PNG_READ_BACKGROUND_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 929 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
| 930 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 931 | png_colorp palette = png_ptr->palette; |
| 932 | int num_palette = png_ptr->num_palette; |
| 933 | int i; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 934 | |
| 935 | for (i = 0; i < num_palette; i++) |
| 936 | { |
| 937 | palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 938 | palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 939 | palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 944 | else |
| 945 | #endif |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 946 | #endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 947 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 948 | /* No GAMMA transformation */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 949 | if ((png_ptr->transformations & PNG_BACKGROUND) && |
| 950 | (color_type == PNG_COLOR_TYPE_PALETTE)) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 951 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 952 | int i; |
| 953 | int istop = (int)png_ptr->num_trans; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 954 | png_color back; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 955 | png_colorp palette = png_ptr->palette; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 956 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 957 | back.red = (png_byte)png_ptr->background.red; |
| 958 | back.green = (png_byte)png_ptr->background.green; |
| 959 | back.blue = (png_byte)png_ptr->background.blue; |
| 960 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 961 | for (i = 0; i < istop; i++) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 962 | { |
| 963 | if (png_ptr->trans[i] == 0) |
| 964 | { |
| 965 | palette[i] = back; |
| 966 | } |
| 967 | else if (png_ptr->trans[i] != 0xff) |
| 968 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 969 | /* The png_composite() macro is defined in png.h */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 970 | png_composite(palette[i].red, palette[i].red, |
| 971 | png_ptr->trans[i], back.red); |
| 972 | png_composite(palette[i].green, palette[i].green, |
| 973 | png_ptr->trans[i], back.green); |
| 974 | png_composite(palette[i].blue, palette[i].blue, |
| 975 | png_ptr->trans[i], back.blue); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 976 | } |
| 977 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 978 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 979 | #endif /* PNG_READ_BACKGROUND_SUPPORTED */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 980 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 981 | #if defined(PNG_READ_SHIFT_SUPPORTED) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 982 | if ((png_ptr->transformations & PNG_SHIFT) && |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 983 | (color_type == PNG_COLOR_TYPE_PALETTE)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 984 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 985 | png_uint_16 i; |
| 986 | png_uint_16 istop = png_ptr->num_palette; |
| 987 | int sr = 8 - png_ptr->sig_bit.red; |
| 988 | int sg = 8 - png_ptr->sig_bit.green; |
| 989 | int sb = 8 - png_ptr->sig_bit.blue; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 990 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 991 | if (sr < 0 || sr > 8) |
| 992 | sr = 0; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 993 | if (sg < 0 || sg > 8) |
| 994 | sg = 0; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 995 | if (sb < 0 || sb > 8) |
| 996 | sb = 0; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 997 | for (i = 0; i < istop; i++) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 998 | { |
| 999 | png_ptr->palette[i].red >>= sr; |
| 1000 | png_ptr->palette[i].green >>= sg; |
| 1001 | png_ptr->palette[i].blue >>= sb; |
| 1002 | } |
| 1003 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 1004 | #endif /* PNG_READ_SHIFT_SUPPORTED */ |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 1005 | } |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1006 | #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \ |
| 1007 | && !defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 1008 | if(png_ptr) |
| 1009 | return; |
| 1010 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1011 | } |
| 1012 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1013 | /* Modify the info structure to reflect the transformations. The |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1014 | * info should be updated so a PNG file could be written with it, |
| 1015 | * assuming the transformations result in valid PNG data. |
| 1016 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1017 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1018 | png_read_transform_info(png_structp png_ptr, png_infop info_ptr) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1019 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1020 | png_debug(1, "in png_read_transform_info\n"); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1021 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1022 | if (png_ptr->transformations & PNG_EXPAND) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1023 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1024 | if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1025 | { |
| 1026 | if (png_ptr->num_trans) |
| 1027 | info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
| 1028 | else |
| 1029 | info_ptr->color_type = PNG_COLOR_TYPE_RGB; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1030 | info_ptr->bit_depth = 8; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1031 | info_ptr->num_trans = 0; |
| 1032 | } |
| 1033 | else |
| 1034 | { |
| 1035 | if (png_ptr->num_trans) |
| 1036 | info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; |
| 1037 | if (info_ptr->bit_depth < 8) |
| 1038 | info_ptr->bit_depth = 8; |
| 1039 | info_ptr->num_trans = 0; |
| 1040 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1041 | } |
| 1042 | #endif |
| 1043 | |
| 1044 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 1045 | if (png_ptr->transformations & PNG_BACKGROUND) |
| 1046 | { |
| 1047 | info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA; |
| 1048 | info_ptr->num_trans = 0; |
| 1049 | info_ptr->background = png_ptr->background; |
| 1050 | } |
| 1051 | #endif |
| 1052 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1053 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 1054 | if (png_ptr->transformations & PNG_GAMMA) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1055 | { |
| 1056 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1057 | info_ptr->gamma = png_ptr->gamma; |
| 1058 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1059 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 1060 | info_ptr->int_gamma = png_ptr->int_gamma; |
| 1061 | #endif |
| 1062 | } |
| 1063 | #endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1064 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1065 | #if defined(PNG_READ_16_TO_8_SUPPORTED) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1066 | if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1067 | info_ptr->bit_depth = 8; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1068 | #endif |
| 1069 | |
| 1070 | #if defined(PNG_READ_DITHER_SUPPORTED) |
| 1071 | if (png_ptr->transformations & PNG_DITHER) |
| 1072 | { |
| 1073 | if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || |
| 1074 | (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && |
| 1075 | png_ptr->palette_lookup && info_ptr->bit_depth == 8) |
| 1076 | { |
| 1077 | info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; |
| 1078 | } |
| 1079 | } |
| 1080 | #endif |
| 1081 | |
| 1082 | #if defined(PNG_READ_PACK_SUPPORTED) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1083 | if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1084 | info_ptr->bit_depth = 8; |
| 1085 | #endif |
| 1086 | |
| 1087 | #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 1088 | if (png_ptr->transformations & PNG_GRAY_TO_RGB) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1089 | info_ptr->color_type |= PNG_COLOR_MASK_COLOR; |
| 1090 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1091 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1092 | #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
| 1093 | if (png_ptr->transformations & PNG_RGB_TO_GRAY) |
| 1094 | info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR; |
| 1095 | #endif |
| 1096 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1097 | if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1098 | info_ptr->channels = 1; |
| 1099 | else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 1100 | info_ptr->channels = 3; |
| 1101 | else |
| 1102 | info_ptr->channels = 1; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1103 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1104 | #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 1105 | if (png_ptr->transformations & PNG_STRIP_ALPHA) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1106 | info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1107 | #endif |
| 1108 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1109 | if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) |
| 1110 | info_ptr->channels++; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1111 | |
| 1112 | #if defined(PNG_READ_FILLER_SUPPORTED) |
| 1113 | /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1114 | if ((png_ptr->transformations & PNG_FILLER) && |
| 1115 | ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || |
| 1116 | (info_ptr->color_type == PNG_COLOR_TYPE_GRAY))) |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1117 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1118 | info_ptr->channels++; |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1119 | #if 0 /* if adding a true alpha channel not just filler */ |
| 1120 | info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; |
| 1121 | #endif |
| 1122 | } |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1123 | #endif |
| 1124 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1125 | #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ |
| 1126 | defined(PNG_READ_USER_TRANSFORM_SUPPORTED) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1127 | if(png_ptr->transformations & PNG_USER_TRANSFORM) |
| 1128 | { |
| 1129 | if(info_ptr->bit_depth < png_ptr->user_transform_depth) |
| 1130 | info_ptr->bit_depth = png_ptr->user_transform_depth; |
| 1131 | if(info_ptr->channels < png_ptr->user_transform_channels) |
| 1132 | info_ptr->channels = png_ptr->user_transform_channels; |
| 1133 | } |
| 1134 | #endif |
| 1135 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1136 | info_ptr->pixel_depth = (png_byte)(info_ptr->channels * |
| 1137 | info_ptr->bit_depth); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1138 | info_ptr->rowbytes = ((info_ptr->width * info_ptr->pixel_depth + 7) >> 3); |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 1139 | |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1140 | #if !defined(PNG_READ_EXPAND_SUPPORTED) |
| 1141 | if(png_ptr) |
| 1142 | return; |
| 1143 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1144 | } |
| 1145 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1146 | /* Transform the row. The order of transformations is significant, |
| 1147 | * and is very touchy. If you add a transformation, take care to |
| 1148 | * decide how it fits in with the other transformations here. |
| 1149 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1150 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1151 | png_do_read_transformations(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1152 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1153 | png_debug(1, "in png_do_read_transformations\n"); |
| 1154 | #if !defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1155 | if (png_ptr->row_buf == NULL) |
| 1156 | { |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 1157 | #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1158 | char msg[50]; |
| 1159 | |
| 1160 | sprintf(msg, "NULL row buffer for row %ld, pass %d", png_ptr->row_number, |
| 1161 | png_ptr->pass); |
| 1162 | png_error(png_ptr, msg); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1163 | #else |
| 1164 | png_error(png_ptr, "NULL row buffer"); |
| 1165 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1166 | } |
| 1167 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1168 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1169 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1170 | if (png_ptr->transformations & PNG_EXPAND) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1171 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1172 | if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE) |
| 1173 | { |
| 1174 | png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1175 | png_ptr->palette, png_ptr->trans, png_ptr->num_trans); |
| 1176 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1177 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1178 | { |
| 1179 | if (png_ptr->num_trans) |
| 1180 | png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1181 | &(png_ptr->trans_values)); |
| 1182 | else |
| 1183 | png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1184 | NULL); |
| 1185 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1186 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1187 | #endif |
| 1188 | |
| 1189 | #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) |
| 1190 | if (png_ptr->transformations & PNG_STRIP_ALPHA) |
| 1191 | png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1192 | PNG_FLAG_FILLER_AFTER); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1193 | #endif |
| 1194 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1195 | #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
| 1196 | if (png_ptr->transformations & PNG_RGB_TO_GRAY) |
| 1197 | { |
| 1198 | int rgb_error = |
| 1199 | png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1); |
| 1200 | if(rgb_error) |
| 1201 | { |
| 1202 | png_ptr->rgb_to_gray_status=1; |
| 1203 | if(png_ptr->transformations == PNG_RGB_TO_GRAY_WARN) |
| 1204 | png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); |
| 1205 | if(png_ptr->transformations == PNG_RGB_TO_GRAY_ERR) |
| 1206 | png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); |
| 1207 | } |
| 1208 | } |
| 1209 | #endif |
| 1210 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1211 | /* |
| 1212 | From Andreas Dilger e-mail to png-implement, 26 March 1998: |
| 1213 | |
| 1214 | In most cases, the "simple transparency" should be done prior to doing |
| 1215 | gray-to-RGB, or you will have to test 3x as many bytes to check if a |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1216 | pixel is transparent. You would also need to make sure that the |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1217 | transparency information is upgraded to RGB. |
| 1218 | |
| 1219 | To summarize, the current flow is: |
| 1220 | - Gray + simple transparency -> compare 1 or 2 gray bytes and composite |
| 1221 | with background "in place" if transparent, |
| 1222 | convert to RGB if necessary |
| 1223 | - Gray + alpha -> composite with gray background and remove alpha bytes, |
| 1224 | convert to RGB if necessary |
| 1225 | |
| 1226 | To support RGB backgrounds for gray images we need: |
| 1227 | - Gray + simple transparency -> convert to RGB + simple transparency, compare |
| 1228 | 3 or 6 bytes and composite with background |
| 1229 | "in place" if transparent (3x compare/pixel |
| 1230 | compared to doing composite with gray bkgrnd) |
| 1231 | - Gray + alpha -> convert to RGB + alpha, composite with background and |
| 1232 | remove alpha bytes (3x float operations/pixel |
| 1233 | compared with composite on gray background) |
| 1234 | |
| 1235 | Greg's change will do this. The reason it wasn't done before is for |
| 1236 | performance, as this increases the per-pixel operations. If we would check |
| 1237 | in advance if the background was gray or RGB, and position the gray-to-RGB |
| 1238 | transform appropriately, then it would save a lot of work/time. |
| 1239 | */ |
| 1240 | |
| 1241 | #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) |
| 1242 | /* if gray -> RGB, do so now only if background is non-gray; else do later |
| 1243 | * for performance reasons */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1244 | if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 1245 | !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1246 | png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 1247 | #endif |
| 1248 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1249 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1250 | if ((png_ptr->transformations & PNG_BACKGROUND) && |
| 1251 | ((png_ptr->num_trans != 0 ) || |
| 1252 | (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1253 | png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1, |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 1254 | &(png_ptr->trans_values), &(png_ptr->background) |
| 1255 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 1256 | , &(png_ptr->background_1), |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1257 | png_ptr->gamma_table, png_ptr->gamma_from_1, |
| 1258 | png_ptr->gamma_to_1, png_ptr->gamma_16_table, |
| 1259 | png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1, |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 1260 | png_ptr->gamma_shift |
| 1261 | #endif |
| 1262 | ); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1263 | #endif |
| 1264 | |
| 1265 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 1266 | if ((png_ptr->transformations & PNG_GAMMA) && |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1267 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 1268 | !((png_ptr->transformations & PNG_BACKGROUND) && |
| 1269 | ((png_ptr->num_trans != 0) || |
| 1270 | (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) && |
| 1271 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1272 | (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1273 | png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1274 | png_ptr->gamma_table, png_ptr->gamma_16_table, |
| 1275 | png_ptr->gamma_shift); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1276 | #endif |
| 1277 | |
| 1278 | #if defined(PNG_READ_16_TO_8_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1279 | if (png_ptr->transformations & PNG_16_TO_8) |
| 1280 | png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1281 | #endif |
| 1282 | |
| 1283 | #if defined(PNG_READ_DITHER_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1284 | if (png_ptr->transformations & PNG_DITHER) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1285 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1286 | png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1287 | png_ptr->palette_lookup, png_ptr->dither_index); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1288 | if(png_ptr->row_info.rowbytes == (png_uint_32)0) |
| 1289 | png_error(png_ptr, "png_do_dither returned rowbytes=0"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1290 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1291 | #endif |
| 1292 | |
| 1293 | #if defined(PNG_READ_INVERT_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1294 | if (png_ptr->transformations & PNG_INVERT_MONO) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1295 | png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1296 | #endif |
| 1297 | |
| 1298 | #if defined(PNG_READ_SHIFT_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1299 | if (png_ptr->transformations & PNG_SHIFT) |
| 1300 | png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1301 | &(png_ptr->shift)); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1302 | #endif |
| 1303 | |
| 1304 | #if defined(PNG_READ_PACK_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1305 | if (png_ptr->transformations & PNG_PACK) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1306 | png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1307 | #endif |
| 1308 | |
| 1309 | #if defined(PNG_READ_BGR_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1310 | if (png_ptr->transformations & PNG_BGR) |
| 1311 | png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1312 | #endif |
| 1313 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1314 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 1315 | if (png_ptr->transformations & PNG_PACKSWAP) |
| 1316 | png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 1317 | #endif |
| 1318 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1319 | #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1320 | /* if gray -> RGB, do so now only if we did not do so above */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1321 | if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && |
| 1322 | (png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1323 | png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1324 | #endif |
| 1325 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1326 | #if defined(PNG_READ_FILLER_SUPPORTED) |
| 1327 | if (png_ptr->transformations & PNG_FILLER) |
| 1328 | png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1329 | (png_uint_32)png_ptr->filler, png_ptr->flags); |
| 1330 | #endif |
| 1331 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1332 | #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) |
| 1333 | if (png_ptr->transformations & PNG_INVERT_ALPHA) |
| 1334 | png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 1335 | #endif |
| 1336 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1337 | #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) |
| 1338 | if (png_ptr->transformations & PNG_SWAP_ALPHA) |
| 1339 | png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 1340 | #endif |
| 1341 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1342 | #if defined(PNG_READ_SWAP_SUPPORTED) |
| 1343 | if (png_ptr->transformations & PNG_SWAP_BYTES) |
| 1344 | png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1345 | #endif |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1346 | |
| 1347 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) |
| 1348 | if (png_ptr->transformations & PNG_USER_TRANSFORM) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1349 | { |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1350 | if(png_ptr->read_user_transform_fn != NULL) |
| 1351 | (*(png_ptr->read_user_transform_fn)) /* user read transform function */ |
| 1352 | (png_ptr, /* png_ptr */ |
| 1353 | &(png_ptr->row_info), /* row_info: */ |
| 1354 | /* png_uint_32 width; width of row */ |
| 1355 | /* png_uint_32 rowbytes; number of bytes in row */ |
| 1356 | /* png_byte color_type; color type of pixels */ |
| 1357 | /* png_byte bit_depth; bit depth of samples */ |
| 1358 | /* png_byte channels; number of channels (1-4) */ |
| 1359 | /* png_byte pixel_depth; bits per pixel (depth*channels) */ |
| 1360 | png_ptr->row_buf + 1); /* start of pixel data for row */ |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1361 | #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1362 | if(png_ptr->user_transform_depth) |
| 1363 | png_ptr->row_info.bit_depth = png_ptr->user_transform_depth; |
| 1364 | if(png_ptr->user_transform_channels) |
| 1365 | png_ptr->row_info.channels = png_ptr->user_transform_channels; |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1366 | #endif |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1367 | png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth * |
| 1368 | png_ptr->row_info.channels); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1369 | png_ptr->row_info.rowbytes = (png_ptr->row_info.width * |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1370 | png_ptr->row_info.pixel_depth+7)>>3; |
| 1371 | } |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1372 | #endif |
| 1373 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1374 | } |
| 1375 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1376 | #if defined(PNG_READ_PACK_SUPPORTED) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1377 | /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel, |
| 1378 | * without changing the actual values. Thus, if you had a row with |
| 1379 | * a bit depth of 1, you would end up with bytes that only contained |
| 1380 | * the numbers 0 or 1. If you would rather they contain 0 and 255, use |
| 1381 | * png_do_shift() after this. |
| 1382 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1383 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1384 | png_do_unpack(png_row_infop row_info, png_bytep row) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1385 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1386 | png_debug(1, "in png_do_unpack\n"); |
| 1387 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1388 | if (row != NULL && row_info != NULL && row_info->bit_depth < 8) |
| 1389 | #else |
| 1390 | if (row_info->bit_depth < 8) |
| 1391 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1392 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1393 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1394 | png_uint_32 row_width=row_info->width; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1395 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1396 | switch (row_info->bit_depth) |
| 1397 | { |
| 1398 | case 1: |
| 1399 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1400 | png_bytep sp = row + (png_size_t)((row_width - 1) >> 3); |
| 1401 | png_bytep dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1402 | png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1403 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1404 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1405 | *dp = (png_byte)((*sp >> shift) & 0x01); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1406 | if (shift == 7) |
| 1407 | { |
| 1408 | shift = 0; |
| 1409 | sp--; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1410 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1411 | else |
| 1412 | shift++; |
| 1413 | |
| 1414 | dp--; |
| 1415 | } |
| 1416 | break; |
| 1417 | } |
| 1418 | case 2: |
| 1419 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1420 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1421 | png_bytep sp = row + (png_size_t)((row_width - 1) >> 2); |
| 1422 | png_bytep dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1423 | png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1424 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1425 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1426 | *dp = (png_byte)((*sp >> shift) & 0x03); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1427 | if (shift == 6) |
| 1428 | { |
| 1429 | shift = 0; |
| 1430 | sp--; |
| 1431 | } |
| 1432 | else |
| 1433 | shift += 2; |
| 1434 | |
| 1435 | dp--; |
| 1436 | } |
| 1437 | break; |
| 1438 | } |
| 1439 | case 4: |
| 1440 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1441 | png_bytep sp = row + (png_size_t)((row_width - 1) >> 1); |
| 1442 | png_bytep dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1443 | png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1444 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1445 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1446 | *dp = (png_byte)((*sp >> shift) & 0x0f); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1447 | if (shift == 4) |
| 1448 | { |
| 1449 | shift = 0; |
| 1450 | sp--; |
| 1451 | } |
| 1452 | else |
| 1453 | shift = 4; |
| 1454 | |
| 1455 | dp--; |
| 1456 | } |
| 1457 | break; |
| 1458 | } |
| 1459 | } |
| 1460 | row_info->bit_depth = 8; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1461 | row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1462 | row_info->rowbytes = row_width * row_info->channels; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1463 | } |
| 1464 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1465 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1466 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1467 | #if defined(PNG_READ_SHIFT_SUPPORTED) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1468 | /* Reverse the effects of png_do_shift. This routine merely shifts the |
| 1469 | * pixels back to their significant bits values. Thus, if you have |
| 1470 | * a row of bit depth 8, but only 5 are significant, this will shift |
| 1471 | * the values back to 0 through 31. |
| 1472 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1473 | void /* PRIVATE */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1474 | png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1475 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1476 | png_debug(1, "in png_do_unshift\n"); |
| 1477 | if ( |
| 1478 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1479 | row != NULL && row_info != NULL && sig_bits != NULL && |
| 1480 | #endif |
| 1481 | row_info->color_type != PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1482 | { |
| 1483 | int shift[4]; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1484 | int channels = 0; |
| 1485 | int c; |
| 1486 | png_uint_16 value = 0; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1487 | png_uint_32 row_width = row_info->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1488 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1489 | if (row_info->color_type & PNG_COLOR_MASK_COLOR) |
| 1490 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1491 | shift[channels++] = row_info->bit_depth - sig_bits->red; |
| 1492 | shift[channels++] = row_info->bit_depth - sig_bits->green; |
| 1493 | shift[channels++] = row_info->bit_depth - sig_bits->blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1494 | } |
| 1495 | else |
| 1496 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1497 | shift[channels++] = row_info->bit_depth - sig_bits->gray; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1498 | } |
| 1499 | if (row_info->color_type & PNG_COLOR_MASK_ALPHA) |
| 1500 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1501 | shift[channels++] = row_info->bit_depth - sig_bits->alpha; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1502 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1503 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1504 | for (c = 0; c < channels; c++) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1505 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1506 | if (shift[c] <= 0) |
| 1507 | shift[c] = 0; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1508 | else |
| 1509 | value = 1; |
| 1510 | } |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1511 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1512 | if (!value) |
| 1513 | return; |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1514 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1515 | switch (row_info->bit_depth) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1516 | { |
| 1517 | case 2: |
| 1518 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1519 | png_bytep bp; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1520 | png_uint_32 i; |
| 1521 | png_uint_32 istop = row_info->rowbytes; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1522 | |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1523 | for (bp = row, i = 0; i < istop; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1524 | { |
| 1525 | *bp >>= 1; |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1526 | *bp++ &= 0x55; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1527 | } |
| 1528 | break; |
| 1529 | } |
| 1530 | case 4: |
| 1531 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 1532 | png_bytep bp = row; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1533 | png_uint_32 i; |
| 1534 | png_uint_32 istop = row_info->rowbytes; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1535 | png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) | |
| 1536 | (png_byte)((int)0xf >> shift[0])); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1537 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 1538 | for (i = 0; i < istop; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1539 | { |
| 1540 | *bp >>= shift[0]; |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1541 | *bp++ &= mask; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1542 | } |
| 1543 | break; |
| 1544 | } |
| 1545 | case 8: |
| 1546 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1547 | png_bytep bp = row; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1548 | png_uint_32 i; |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1549 | png_uint_32 istop = row_width * channels; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1550 | |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1551 | for (i = 0; i < istop; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1552 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1553 | *bp++ >>= shift[i%channels]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1554 | } |
| 1555 | break; |
| 1556 | } |
| 1557 | case 16: |
| 1558 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1559 | png_bytep bp = row; |
| 1560 | png_uint_32 i; |
| 1561 | png_uint_32 istop = channels * row_width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1562 | |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1563 | for (i = 0; i < istop; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1564 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1565 | value = (png_uint_16)((*bp << 8) + *(bp + 1)); |
| 1566 | value >>= shift[i%channels]; |
| 1567 | *bp++ = (png_byte)(value >> 8); |
| 1568 | *bp++ = (png_byte)(value & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1569 | } |
| 1570 | break; |
| 1571 | } |
| 1572 | } |
| 1573 | } |
| 1574 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1575 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1576 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1577 | #if defined(PNG_READ_16_TO_8_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1578 | /* chop rows of bit depth 16 down to 8 */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1579 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1580 | png_do_chop(png_row_infop row_info, png_bytep row) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1581 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1582 | png_debug(1, "in png_do_chop\n"); |
| 1583 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1584 | if (row != NULL && row_info != NULL && row_info->bit_depth == 16) |
| 1585 | #else |
| 1586 | if (row_info->bit_depth == 16) |
| 1587 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1588 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1589 | png_bytep sp = row; |
| 1590 | png_bytep dp = row; |
| 1591 | png_uint_32 i; |
| 1592 | png_uint_32 istop = row_info->width * row_info->channels; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1593 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1594 | for (i = 0; i<istop; i++, sp += 2, dp++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1595 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1596 | #if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1597 | /* This does a more accurate scaling of the 16-bit color |
| 1598 | * value, rather than a simple low-byte truncation. |
| 1599 | * |
| 1600 | * What the ideal calculation should be: |
| 1601 | * *dp = (((((png_uint_32)(*sp) << 8) | |
| 1602 | * (png_uint_32)(*(sp + 1))) * 255 + 127) / (png_uint_32)65535L; |
| 1603 | * |
| 1604 | * GRR: no, I think this is what it really should be: |
| 1605 | * *dp = (((((png_uint_32)(*sp) << 8) | |
| 1606 | * (png_uint_32)(*(sp + 1))) + 128L) / (png_uint_32)257L; |
| 1607 | * |
| 1608 | * GRR: here's the exact calculation with shifts: |
| 1609 | * temp = (((png_uint_32)(*sp) << 8) | (png_uint_32)(*(sp + 1))) + 128L; |
| 1610 | * *dp = (temp - (temp >> 8)) >> 8; |
| 1611 | * |
| 1612 | * Approximate calculation with shift/add instead of multiply/divide: |
| 1613 | * *dp = ((((png_uint_32)(*sp) << 8) | |
| 1614 | * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8; |
| 1615 | * |
| 1616 | * What we actually do to avoid extra shifting and conversion: |
| 1617 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1618 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1619 | *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0); |
| 1620 | #else |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1621 | /* Simply discard the low order byte */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1622 | *dp = *sp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1623 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1624 | } |
| 1625 | row_info->bit_depth = 8; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1626 | row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1627 | row_info->rowbytes = row_info->width * row_info->channels; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1628 | } |
| 1629 | } |
| 1630 | #endif |
| 1631 | |
| 1632 | #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1633 | void /* PRIVATE */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1634 | png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) |
| 1635 | { |
| 1636 | png_debug(1, "in png_do_read_swap_alpha\n"); |
| 1637 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1638 | if (row != NULL && row_info != NULL) |
| 1639 | #endif |
| 1640 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1641 | png_uint_32 row_width = row_info->width; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1642 | if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 1643 | { |
| 1644 | /* This converts from RGBA to ARGB */ |
| 1645 | if (row_info->bit_depth == 8) |
| 1646 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1647 | png_bytep sp = row + row_info->rowbytes; |
| 1648 | png_bytep dp = sp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1649 | png_byte save; |
| 1650 | png_uint_32 i; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1651 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1652 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1653 | { |
| 1654 | save = *(--sp); |
| 1655 | *(--dp) = *(--sp); |
| 1656 | *(--dp) = *(--sp); |
| 1657 | *(--dp) = *(--sp); |
| 1658 | *(--dp) = save; |
| 1659 | } |
| 1660 | } |
| 1661 | /* This converts from RRGGBBAA to AARRGGBB */ |
| 1662 | else |
| 1663 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1664 | png_bytep sp = row + row_info->rowbytes; |
| 1665 | png_bytep dp = sp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1666 | png_byte save[2]; |
| 1667 | png_uint_32 i; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1668 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1669 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1670 | { |
| 1671 | save[0] = *(--sp); |
| 1672 | save[1] = *(--sp); |
| 1673 | *(--dp) = *(--sp); |
| 1674 | *(--dp) = *(--sp); |
| 1675 | *(--dp) = *(--sp); |
| 1676 | *(--dp) = *(--sp); |
| 1677 | *(--dp) = *(--sp); |
| 1678 | *(--dp) = *(--sp); |
| 1679 | *(--dp) = save[0]; |
| 1680 | *(--dp) = save[1]; |
| 1681 | } |
| 1682 | } |
| 1683 | } |
| 1684 | else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 1685 | { |
| 1686 | /* This converts from GA to AG */ |
| 1687 | if (row_info->bit_depth == 8) |
| 1688 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1689 | png_bytep sp = row + row_info->rowbytes; |
| 1690 | png_bytep dp = sp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1691 | png_byte save; |
| 1692 | png_uint_32 i; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1693 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1694 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1695 | { |
| 1696 | save = *(--sp); |
| 1697 | *(--dp) = *(--sp); |
| 1698 | *(--dp) = save; |
| 1699 | } |
| 1700 | } |
| 1701 | /* This converts from GGAA to AAGG */ |
| 1702 | else |
| 1703 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1704 | png_bytep sp = row + row_info->rowbytes; |
| 1705 | png_bytep dp = sp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1706 | png_byte save[2]; |
| 1707 | png_uint_32 i; |
| 1708 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1709 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1710 | { |
| 1711 | save[0] = *(--sp); |
| 1712 | save[1] = *(--sp); |
| 1713 | *(--dp) = *(--sp); |
| 1714 | *(--dp) = *(--sp); |
| 1715 | *(--dp) = save[0]; |
| 1716 | *(--dp) = save[1]; |
| 1717 | } |
| 1718 | } |
| 1719 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1720 | } |
| 1721 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1722 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1723 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1724 | #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1725 | void /* PRIVATE */ |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1726 | png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) |
| 1727 | { |
| 1728 | png_debug(1, "in png_do_read_invert_alpha\n"); |
| 1729 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1730 | if (row != NULL && row_info != NULL) |
| 1731 | #endif |
| 1732 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1733 | png_uint_32 row_width = row_info->width; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1734 | if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 1735 | { |
| 1736 | /* This inverts the alpha channel in RGBA */ |
| 1737 | if (row_info->bit_depth == 8) |
| 1738 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1739 | png_bytep sp = row + row_info->rowbytes; |
| 1740 | png_bytep dp = sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1741 | png_uint_32 i; |
| 1742 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1743 | for (i = 0; i < row_width; i++) |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1744 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1745 | *(--dp) = (png_byte)(255 - *(--sp)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1746 | |
| 1747 | /* This does nothing: |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1748 | *(--dp) = *(--sp); |
| 1749 | *(--dp) = *(--sp); |
| 1750 | *(--dp) = *(--sp); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1751 | We can replace it with: |
| 1752 | */ |
| 1753 | sp-=3; |
| 1754 | dp=sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1755 | } |
| 1756 | } |
| 1757 | /* This inverts the alpha channel in RRGGBBAA */ |
| 1758 | else |
| 1759 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1760 | png_bytep sp = row + row_info->rowbytes; |
| 1761 | png_bytep dp = sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1762 | png_uint_32 i; |
| 1763 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1764 | for (i = 0; i < row_width; i++) |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1765 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1766 | *(--dp) = (png_byte)(255 - *(--sp)); |
| 1767 | *(--dp) = (png_byte)(255 - *(--sp)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1768 | |
| 1769 | /* This does nothing: |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1770 | *(--dp) = *(--sp); |
| 1771 | *(--dp) = *(--sp); |
| 1772 | *(--dp) = *(--sp); |
| 1773 | *(--dp) = *(--sp); |
| 1774 | *(--dp) = *(--sp); |
| 1775 | *(--dp) = *(--sp); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1776 | We can replace it with: |
| 1777 | */ |
| 1778 | sp-=6; |
| 1779 | dp=sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 1784 | { |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1785 | /* This inverts the alpha channel in GA */ |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1786 | if (row_info->bit_depth == 8) |
| 1787 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1788 | png_bytep sp = row + row_info->rowbytes; |
| 1789 | png_bytep dp = sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1790 | png_uint_32 i; |
| 1791 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1792 | for (i = 0; i < row_width; i++) |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1793 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1794 | *(--dp) = (png_byte)(255 - *(--sp)); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1795 | *(--dp) = *(--sp); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1796 | } |
| 1797 | } |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1798 | /* This inverts the alpha channel in GGAA */ |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1799 | else |
| 1800 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1801 | png_bytep sp = row + row_info->rowbytes; |
| 1802 | png_bytep dp = sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1803 | png_uint_32 i; |
| 1804 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1805 | for (i = 0; i < row_width; i++) |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1806 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1807 | *(--dp) = (png_byte)(255 - *(--sp)); |
| 1808 | *(--dp) = (png_byte)(255 - *(--sp)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1809 | /* |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1810 | *(--dp) = *(--sp); |
| 1811 | *(--dp) = *(--sp); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1812 | */ |
| 1813 | sp-=2; |
| 1814 | dp=sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1815 | } |
| 1816 | } |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | #endif |
| 1821 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1822 | #if defined(PNG_READ_FILLER_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1823 | /* Add filler channel if we have RGB color */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1824 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1825 | png_do_read_filler(png_row_infop row_info, png_bytep row, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1826 | png_uint_32 filler, png_uint_32 flags) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1827 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1828 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1829 | png_uint_32 row_width = row_info->width; |
| 1830 | |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1831 | png_byte hi_filler = (png_byte)((filler>>8) & 0xff); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1832 | png_byte lo_filler = (png_byte)(filler & 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1833 | |
| 1834 | png_debug(1, "in png_do_read_filler\n"); |
| 1835 | if ( |
| 1836 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1837 | row != NULL && row_info != NULL && |
| 1838 | #endif |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1839 | row_info->color_type == PNG_COLOR_TYPE_GRAY) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1840 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1841 | if(row_info->bit_depth == 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1842 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1843 | /* This changes the data from G to GX */ |
| 1844 | if (flags & PNG_FLAG_FILLER_AFTER) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1845 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1846 | png_bytep sp = row + (png_size_t)row_width; |
| 1847 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1848 | for (i = 1; i < row_width; i++) |
| 1849 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1850 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1851 | *(--dp) = *(--sp); |
| 1852 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1853 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1854 | row_info->channels = 2; |
| 1855 | row_info->pixel_depth = 16; |
| 1856 | row_info->rowbytes = row_width * 2; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1857 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1858 | /* This changes the data from G to XG */ |
| 1859 | else |
| 1860 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1861 | png_bytep sp = row + (png_size_t)row_width; |
| 1862 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1863 | for (i = 0; i < row_width; i++) |
| 1864 | { |
| 1865 | *(--dp) = *(--sp); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1866 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1867 | } |
| 1868 | row_info->channels = 2; |
| 1869 | row_info->pixel_depth = 16; |
| 1870 | row_info->rowbytes = row_width * 2; |
| 1871 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1872 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1873 | else if(row_info->bit_depth == 16) |
| 1874 | { |
| 1875 | /* This changes the data from GG to GGXX */ |
| 1876 | if (flags & PNG_FLAG_FILLER_AFTER) |
| 1877 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1878 | png_bytep sp = row + (png_size_t)row_width; |
| 1879 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1880 | for (i = 1; i < row_width; i++) |
| 1881 | { |
| 1882 | *(--dp) = hi_filler; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1883 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1884 | *(--dp) = *(--sp); |
| 1885 | *(--dp) = *(--sp); |
| 1886 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1887 | *(--dp) = hi_filler; |
| 1888 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1889 | row_info->channels = 2; |
| 1890 | row_info->pixel_depth = 32; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1891 | row_info->rowbytes = row_width * 4; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1892 | } |
| 1893 | /* This changes the data from GG to XXGG */ |
| 1894 | else |
| 1895 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1896 | png_bytep sp = row + (png_size_t)row_width; |
| 1897 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1898 | for (i = 0; i < row_width; i++) |
| 1899 | { |
| 1900 | *(--dp) = *(--sp); |
| 1901 | *(--dp) = *(--sp); |
| 1902 | *(--dp) = hi_filler; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1903 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1904 | } |
| 1905 | row_info->channels = 2; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1906 | row_info->pixel_depth = 32; |
| 1907 | row_info->rowbytes = row_width * 4; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | } /* COLOR_TYPE == GRAY */ |
| 1911 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 1912 | { |
| 1913 | if(row_info->bit_depth == 8) |
| 1914 | { |
| 1915 | /* This changes the data from RGB to RGBX */ |
| 1916 | if (flags & PNG_FLAG_FILLER_AFTER) |
| 1917 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1918 | png_bytep sp = row + (png_size_t)row_width * 3; |
| 1919 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1920 | for (i = 1; i < row_width; i++) |
| 1921 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1922 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1923 | *(--dp) = *(--sp); |
| 1924 | *(--dp) = *(--sp); |
| 1925 | *(--dp) = *(--sp); |
| 1926 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1927 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1928 | row_info->channels = 4; |
| 1929 | row_info->pixel_depth = 32; |
| 1930 | row_info->rowbytes = row_width * 4; |
| 1931 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1932 | /* This changes the data from RGB to XRGB */ |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1933 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1934 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1935 | png_bytep sp = row + (png_size_t)row_width * 3; |
| 1936 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1937 | for (i = 0; i < row_width; i++) |
| 1938 | { |
| 1939 | *(--dp) = *(--sp); |
| 1940 | *(--dp) = *(--sp); |
| 1941 | *(--dp) = *(--sp); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1942 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1943 | } |
| 1944 | row_info->channels = 4; |
| 1945 | row_info->pixel_depth = 32; |
| 1946 | row_info->rowbytes = row_width * 4; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1947 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1948 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1949 | else if(row_info->bit_depth == 16) |
| 1950 | { |
| 1951 | /* This changes the data from RRGGBB to RRGGBBXX */ |
| 1952 | if (flags & PNG_FLAG_FILLER_AFTER) |
| 1953 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1954 | png_bytep sp = row + (png_size_t)row_width * 3; |
| 1955 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1956 | for (i = 1; i < row_width; i++) |
| 1957 | { |
| 1958 | *(--dp) = hi_filler; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1959 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1960 | *(--dp) = *(--sp); |
| 1961 | *(--dp) = *(--sp); |
| 1962 | *(--dp) = *(--sp); |
| 1963 | *(--dp) = *(--sp); |
| 1964 | *(--dp) = *(--sp); |
| 1965 | *(--dp) = *(--sp); |
| 1966 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1967 | *(--dp) = hi_filler; |
| 1968 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1969 | row_info->channels = 4; |
| 1970 | row_info->pixel_depth = 64; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1971 | row_info->rowbytes = row_width * 8; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1972 | } |
| 1973 | /* This changes the data from RRGGBB to XXRRGGBB */ |
| 1974 | else |
| 1975 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1976 | png_bytep sp = row + (png_size_t)row_width * 3; |
| 1977 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1978 | for (i = 0; i < row_width; i++) |
| 1979 | { |
| 1980 | *(--dp) = *(--sp); |
| 1981 | *(--dp) = *(--sp); |
| 1982 | *(--dp) = *(--sp); |
| 1983 | *(--dp) = *(--sp); |
| 1984 | *(--dp) = *(--sp); |
| 1985 | *(--dp) = *(--sp); |
| 1986 | *(--dp) = hi_filler; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1987 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1988 | } |
| 1989 | row_info->channels = 4; |
| 1990 | row_info->pixel_depth = 64; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1991 | row_info->rowbytes = row_width * 8; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1992 | } |
| 1993 | } |
| 1994 | } /* COLOR_TYPE == RGB */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1995 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1996 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1997 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1998 | #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1999 | /* expand grayscale files to RGB, with or without alpha */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2000 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2001 | png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2002 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2003 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2004 | png_uint_32 row_width = row_info->width; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2005 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2006 | png_debug(1, "in png_do_gray_to_rgb\n"); |
| 2007 | if (row_info->bit_depth >= 8 && |
| 2008 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 2009 | row != NULL && row_info != NULL && |
| 2010 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2011 | !(row_info->color_type & PNG_COLOR_MASK_COLOR)) |
| 2012 | { |
| 2013 | if (row_info->color_type == PNG_COLOR_TYPE_GRAY) |
| 2014 | { |
| 2015 | if (row_info->bit_depth == 8) |
| 2016 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2017 | png_bytep sp = row + (png_size_t)row_width - 1; |
| 2018 | png_bytep dp = sp + (png_size_t)row_width * 2; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2019 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2020 | { |
| 2021 | *(dp--) = *sp; |
| 2022 | *(dp--) = *sp; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2023 | *(dp--) = *(sp--); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2024 | } |
| 2025 | } |
| 2026 | else |
| 2027 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2028 | png_bytep sp = row + (png_size_t)row_width * 2 - 1; |
| 2029 | png_bytep dp = sp + (png_size_t)row_width * 4; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2030 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2031 | { |
| 2032 | *(dp--) = *sp; |
| 2033 | *(dp--) = *(sp - 1); |
| 2034 | *(dp--) = *sp; |
| 2035 | *(dp--) = *(sp - 1); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2036 | *(dp--) = *(sp--); |
| 2037 | *(dp--) = *(sp--); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | } |
| 2041 | else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 2042 | { |
| 2043 | if (row_info->bit_depth == 8) |
| 2044 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2045 | png_bytep sp = row + (png_size_t)row_width * 2 - 1; |
| 2046 | png_bytep dp = sp + (png_size_t)row_width * 2; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2047 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2048 | { |
| 2049 | *(dp--) = *(sp--); |
| 2050 | *(dp--) = *sp; |
| 2051 | *(dp--) = *sp; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2052 | *(dp--) = *(sp--); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2053 | } |
| 2054 | } |
| 2055 | else |
| 2056 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2057 | png_bytep sp = row + (png_size_t)row_width * 4 - 1; |
| 2058 | png_bytep dp = sp + (png_size_t)row_width * 4; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2059 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2060 | { |
| 2061 | *(dp--) = *(sp--); |
| 2062 | *(dp--) = *(sp--); |
| 2063 | *(dp--) = *sp; |
| 2064 | *(dp--) = *(sp - 1); |
| 2065 | *(dp--) = *sp; |
| 2066 | *(dp--) = *(sp - 1); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2067 | *(dp--) = *(sp--); |
| 2068 | *(dp--) = *(sp--); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2069 | } |
| 2070 | } |
| 2071 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2072 | row_info->channels += (png_byte)2; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2073 | row_info->color_type |= PNG_COLOR_MASK_COLOR; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2074 | row_info->pixel_depth = (png_byte)(row_info->channels * |
| 2075 | row_info->bit_depth); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2076 | row_info->rowbytes = ((row_width * |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2077 | row_info->pixel_depth + 7) >> 3); |
| 2078 | } |
| 2079 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2080 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2081 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2082 | #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2083 | /* reduce RGB files to grayscale, with or without alpha |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2084 | * using the equation given in Poynton's ColorFAQ at |
| 2085 | * <http://www.inforamp.net/~poynton/> |
| 2086 | * Copyright (c) 1998-01-04 Charles Poynton poynton@inforamp.net |
| 2087 | * |
| 2088 | * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B |
| 2089 | * |
| 2090 | * We approximate this with |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2091 | * |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2092 | * Y = 0.21268 * R + 0.7151 * G + 0.07217 * B |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2093 | * |
| 2094 | * which can be expressed with integers as |
| 2095 | * |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2096 | * Y = (6969 * R + 23434 * G + 2365 * B)/32768 |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2097 | * |
| 2098 | * The calculation is to be done in a linear colorspace. |
| 2099 | * |
| 2100 | * Other integer coefficents can be used via png_set_rgb_to_gray(). |
| 2101 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2102 | int /* PRIVATE */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2103 | png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) |
| 2104 | |
| 2105 | { |
| 2106 | png_uint_32 i; |
| 2107 | |
| 2108 | png_uint_32 row_width = row_info->width; |
| 2109 | int rgb_error = 0; |
| 2110 | |
| 2111 | png_debug(1, "in png_do_rgb_to_gray\n"); |
| 2112 | if ( |
| 2113 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 2114 | row != NULL && row_info != NULL && |
| 2115 | #endif |
| 2116 | (row_info->color_type & PNG_COLOR_MASK_COLOR)) |
| 2117 | { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2118 | png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff; |
| 2119 | png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff; |
| 2120 | png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2121 | |
| 2122 | if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 2123 | { |
| 2124 | if (row_info->bit_depth == 8) |
| 2125 | { |
| 2126 | #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 2127 | if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) |
| 2128 | { |
| 2129 | png_bytep sp = row; |
| 2130 | png_bytep dp = row; |
| 2131 | |
| 2132 | for (i = 0; i < row_width; i++) |
| 2133 | { |
| 2134 | png_byte red = png_ptr->gamma_to_1[*(sp++)]; |
| 2135 | png_byte green = png_ptr->gamma_to_1[*(sp++)]; |
| 2136 | png_byte blue = png_ptr->gamma_to_1[*(sp++)]; |
| 2137 | if(red != green || red != blue) |
| 2138 | { |
| 2139 | rgb_error |= 1; |
| 2140 | *(dp++) = png_ptr->gamma_from_1[ |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2141 | (rc*red+gc*green+bc*blue)>>15]; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2142 | } |
| 2143 | else |
| 2144 | *(dp++) = *(sp-1); |
| 2145 | } |
| 2146 | } |
| 2147 | else |
| 2148 | #endif |
| 2149 | { |
| 2150 | png_bytep sp = row; |
| 2151 | png_bytep dp = row; |
| 2152 | for (i = 0; i < row_width; i++) |
| 2153 | { |
| 2154 | png_byte red = *(sp++); |
| 2155 | png_byte green = *(sp++); |
| 2156 | png_byte blue = *(sp++); |
| 2157 | if(red != green || red != blue) |
| 2158 | { |
| 2159 | rgb_error |= 1; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2160 | *(dp++) = (png_byte)((rc*red+gc*green+bc*blue)>>15); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2161 | } |
| 2162 | else |
| 2163 | *(dp++) = *(sp-1); |
| 2164 | } |
| 2165 | } |
| 2166 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2167 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2168 | else /* RGB bit_depth == 16 */ |
| 2169 | { |
| 2170 | #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 2171 | if (png_ptr->gamma_16_to_1 != NULL && |
| 2172 | png_ptr->gamma_16_from_1 != NULL) |
| 2173 | { |
| 2174 | png_bytep sp = row; |
| 2175 | png_bytep dp = row; |
| 2176 | for (i = 0; i < row_width; i++) |
| 2177 | { |
| 2178 | png_uint_16 red, green, blue, w; |
| 2179 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2180 | red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
| 2181 | green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
| 2182 | blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2183 | |
| 2184 | if(red == green && red == blue) |
| 2185 | w = red; |
| 2186 | else |
| 2187 | { |
| 2188 | png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >> |
| 2189 | png_ptr->gamma_shift][red>>8]; |
| 2190 | png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >> |
| 2191 | png_ptr->gamma_shift][green>>8]; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2192 | png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >> |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2193 | png_ptr->gamma_shift][blue>>8]; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2194 | png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1 |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2195 | + bc*blue_1)>>15); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2196 | w = png_ptr->gamma_16_from_1[(gray16&0xff) >> |
| 2197 | png_ptr->gamma_shift][gray16 >> 8]; |
| 2198 | rgb_error |= 1; |
| 2199 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2200 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2201 | *(dp++) = (png_byte)((w>>8) & 0xff); |
| 2202 | *(dp++) = (png_byte)(w & 0xff); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | else |
| 2206 | #endif |
| 2207 | { |
| 2208 | png_bytep sp = row; |
| 2209 | png_bytep dp = row; |
| 2210 | for (i = 0; i < row_width; i++) |
| 2211 | { |
| 2212 | png_uint_16 red, green, blue, gray16; |
| 2213 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2214 | red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
| 2215 | green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
| 2216 | blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2217 | |
| 2218 | if(red != green || red != blue) |
| 2219 | rgb_error |= 1; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2220 | gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2221 | *(dp++) = (png_byte)((gray16>>8) & 0xff); |
| 2222 | *(dp++) = (png_byte)(gray16 & 0xff); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | } |
| 2227 | if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 2228 | { |
| 2229 | if (row_info->bit_depth == 8) |
| 2230 | { |
| 2231 | #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 2232 | if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) |
| 2233 | { |
| 2234 | png_bytep sp = row; |
| 2235 | png_bytep dp = row; |
| 2236 | for (i = 0; i < row_width; i++) |
| 2237 | { |
| 2238 | png_byte red = png_ptr->gamma_to_1[*(sp++)]; |
| 2239 | png_byte green = png_ptr->gamma_to_1[*(sp++)]; |
| 2240 | png_byte blue = png_ptr->gamma_to_1[*(sp++)]; |
| 2241 | if(red != green || red != blue) |
| 2242 | rgb_error |= 1; |
| 2243 | *(dp++) = png_ptr->gamma_from_1 |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2244 | [(rc*red + gc*green + bc*blue)>>15]; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2245 | *(dp++) = *(sp++); /* alpha */ |
| 2246 | } |
| 2247 | } |
| 2248 | else |
| 2249 | #endif |
| 2250 | { |
| 2251 | png_bytep sp = row; |
| 2252 | png_bytep dp = row; |
| 2253 | for (i = 0; i < row_width; i++) |
| 2254 | { |
| 2255 | png_byte red = *(sp++); |
| 2256 | png_byte green = *(sp++); |
| 2257 | png_byte blue = *(sp++); |
| 2258 | if(red != green || red != blue) |
| 2259 | rgb_error |= 1; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2260 | *(dp++) = (png_byte)((gc*red + gc*green + bc*blue)>>8); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2261 | *(dp++) = *(sp++); /* alpha */ |
| 2262 | } |
| 2263 | } |
| 2264 | } |
| 2265 | else /* RGBA bit_depth == 16 */ |
| 2266 | { |
| 2267 | #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 2268 | if (png_ptr->gamma_16_to_1 != NULL && |
| 2269 | png_ptr->gamma_16_from_1 != NULL) |
| 2270 | { |
| 2271 | png_bytep sp = row; |
| 2272 | png_bytep dp = row; |
| 2273 | for (i = 0; i < row_width; i++) |
| 2274 | { |
| 2275 | png_uint_16 red, green, blue, w; |
| 2276 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2277 | red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
| 2278 | green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
| 2279 | blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2280 | |
| 2281 | if(red == green && red == blue) |
| 2282 | w = red; |
| 2283 | else |
| 2284 | { |
| 2285 | png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >> |
| 2286 | png_ptr->gamma_shift][red>>8]; |
| 2287 | png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >> |
| 2288 | png_ptr->gamma_shift][green>>8]; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2289 | png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >> |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2290 | png_ptr->gamma_shift][blue>>8]; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2291 | png_uint_16 gray16 = (png_uint_16)((rc * red_1 |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2292 | + gc * green_1 + bc * blue_1)>>15); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2293 | w = png_ptr->gamma_16_from_1[(gray16&0xff) >> |
| 2294 | png_ptr->gamma_shift][gray16 >> 8]; |
| 2295 | rgb_error |= 1; |
| 2296 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2297 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2298 | *(dp++) = (png_byte)((w>>8) & 0xff); |
| 2299 | *(dp++) = (png_byte)(w & 0xff); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2300 | *(dp++) = *(sp++); /* alpha */ |
| 2301 | *(dp++) = *(sp++); |
| 2302 | } |
| 2303 | } |
| 2304 | else |
| 2305 | #endif |
| 2306 | { |
| 2307 | png_bytep sp = row; |
| 2308 | png_bytep dp = row; |
| 2309 | for (i = 0; i < row_width; i++) |
| 2310 | { |
| 2311 | png_uint_16 red, green, blue, gray16; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2312 | red = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; |
| 2313 | green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; |
| 2314 | blue = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2315 | if(red != green || red != blue) |
| 2316 | rgb_error |= 1; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2317 | gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2318 | *(dp++) = (png_byte)((gray16>>8) & 0xff); |
| 2319 | *(dp++) = (png_byte)(gray16 & 0xff); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2320 | *(dp++) = *(sp++); /* alpha */ |
| 2321 | *(dp++) = *(sp++); |
| 2322 | } |
| 2323 | } |
| 2324 | } |
| 2325 | } |
| 2326 | row_info->channels -= (png_byte)2; |
| 2327 | row_info->color_type &= ~PNG_COLOR_MASK_COLOR; |
| 2328 | row_info->pixel_depth = (png_byte)(row_info->channels * |
| 2329 | row_info->bit_depth); |
| 2330 | row_info->rowbytes = ((row_width * |
| 2331 | row_info->pixel_depth + 7) >> 3); |
| 2332 | } |
| 2333 | return rgb_error; |
| 2334 | } |
| 2335 | #endif |
| 2336 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2337 | /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth |
| 2338 | * large of png_color. This lets grayscale images be treated as |
| 2339 | * paletted. Most useful for gamma correction and simplification |
| 2340 | * of code. |
| 2341 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2342 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2343 | png_build_grayscale_palette(int bit_depth, png_colorp palette) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2344 | { |
| 2345 | int num_palette; |
| 2346 | int color_inc; |
| 2347 | int i; |
| 2348 | int v; |
| 2349 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2350 | png_debug(1, "in png_do_build_grayscale_palette\n"); |
| 2351 | if (palette == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2352 | return; |
| 2353 | |
| 2354 | switch (bit_depth) |
| 2355 | { |
| 2356 | case 1: |
| 2357 | num_palette = 2; |
| 2358 | color_inc = 0xff; |
| 2359 | break; |
| 2360 | case 2: |
| 2361 | num_palette = 4; |
| 2362 | color_inc = 0x55; |
| 2363 | break; |
| 2364 | case 4: |
| 2365 | num_palette = 16; |
| 2366 | color_inc = 0x11; |
| 2367 | break; |
| 2368 | case 8: |
| 2369 | num_palette = 256; |
| 2370 | color_inc = 1; |
| 2371 | break; |
| 2372 | default: |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2373 | num_palette = 0; |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 2374 | color_inc = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2375 | break; |
| 2376 | } |
| 2377 | |
| 2378 | for (i = 0, v = 0; i < num_palette; i++, v += color_inc) |
| 2379 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2380 | palette[i].red = (png_byte)v; |
| 2381 | palette[i].green = (png_byte)v; |
| 2382 | palette[i].blue = (png_byte)v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2383 | } |
| 2384 | } |
| 2385 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2386 | /* This function is currently unused. Do we really need it? */ |
| 2387 | #if defined(PNG_READ_DITHER_SUPPORTED) && defined(PNG_CORRECT_PALETTE_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2388 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2389 | png_correct_palette(png_structp png_ptr, png_colorp palette, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2390 | int num_palette) |
| 2391 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2392 | png_debug(1, "in png_correct_palette\n"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2393 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ |
| 2394 | defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2395 | if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2396 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2397 | png_color back, back_1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2398 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2399 | if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) |
| 2400 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2401 | back.red = png_ptr->gamma_table[png_ptr->background.red]; |
| 2402 | back.green = png_ptr->gamma_table[png_ptr->background.green]; |
| 2403 | back.blue = png_ptr->gamma_table[png_ptr->background.blue]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2404 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2405 | back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; |
| 2406 | back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; |
| 2407 | back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2408 | } |
| 2409 | else |
| 2410 | { |
| 2411 | double g; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2412 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 2413 | g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2414 | |
| 2415 | if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN || |
| 2416 | fabs(g - 1.0) < PNG_GAMMA_THRESHOLD) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2417 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2418 | back.red = png_ptr->background.red; |
| 2419 | back.green = png_ptr->background.green; |
| 2420 | back.blue = png_ptr->background.blue; |
| 2421 | } |
| 2422 | else |
| 2423 | { |
| 2424 | back.red = |
| 2425 | (png_byte)(pow((double)png_ptr->background.red/255, g) * |
| 2426 | 255.0 + 0.5); |
| 2427 | back.green = |
| 2428 | (png_byte)(pow((double)png_ptr->background.green/255, g) * |
| 2429 | 255.0 + 0.5); |
| 2430 | back.blue = |
| 2431 | (png_byte)(pow((double)png_ptr->background.blue/255, g) * |
| 2432 | 255.0 + 0.5); |
| 2433 | } |
| 2434 | |
| 2435 | g = 1.0 / png_ptr->background_gamma; |
| 2436 | |
| 2437 | back_1.red = |
| 2438 | (png_byte)(pow((double)png_ptr->background.red/255, g) * |
| 2439 | 255.0 + 0.5); |
| 2440 | back_1.green = |
| 2441 | (png_byte)(pow((double)png_ptr->background.green/255, g) * |
| 2442 | 255.0 + 0.5); |
| 2443 | back_1.blue = |
| 2444 | (png_byte)(pow((double)png_ptr->background.blue/255, g) * |
| 2445 | 255.0 + 0.5); |
| 2446 | } |
| 2447 | |
| 2448 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 2449 | { |
| 2450 | png_uint_32 i; |
| 2451 | |
| 2452 | for (i = 0; i < (png_uint_32)num_palette; i++) |
| 2453 | { |
| 2454 | if (i < png_ptr->num_trans && png_ptr->trans[i] == 0) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2455 | { |
| 2456 | palette[i] = back; |
| 2457 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2458 | else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2459 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2460 | png_byte v, w; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2461 | |
| 2462 | v = png_ptr->gamma_to_1[png_ptr->palette[i].red]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2463 | png_composite(w, v, png_ptr->trans[i], back_1.red); |
| 2464 | palette[i].red = png_ptr->gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2465 | |
| 2466 | v = png_ptr->gamma_to_1[png_ptr->palette[i].green]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2467 | png_composite(w, v, png_ptr->trans[i], back_1.green); |
| 2468 | palette[i].green = png_ptr->gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2469 | |
| 2470 | v = png_ptr->gamma_to_1[png_ptr->palette[i].blue]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2471 | png_composite(w, v, png_ptr->trans[i], back_1.blue); |
| 2472 | palette[i].blue = png_ptr->gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2473 | } |
| 2474 | else |
| 2475 | { |
| 2476 | palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 2477 | palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 2478 | palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 2479 | } |
| 2480 | } |
| 2481 | } |
| 2482 | else |
| 2483 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2484 | int i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2485 | |
| 2486 | for (i = 0; i < num_palette; i++) |
| 2487 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2488 | if (palette[i].red == (png_byte)png_ptr->trans_values.gray) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2489 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2490 | palette[i] = back; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2491 | } |
| 2492 | else |
| 2493 | { |
| 2494 | palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 2495 | palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 2496 | palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 2497 | } |
| 2498 | } |
| 2499 | } |
| 2500 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2501 | else |
| 2502 | #endif |
| 2503 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2504 | if (png_ptr->transformations & PNG_GAMMA) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2505 | { |
| 2506 | int i; |
| 2507 | |
| 2508 | for (i = 0; i < num_palette; i++) |
| 2509 | { |
| 2510 | palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 2511 | palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 2512 | palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 2513 | } |
| 2514 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2515 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 2516 | else |
| 2517 | #endif |
| 2518 | #endif |
| 2519 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2520 | if (png_ptr->transformations & PNG_BACKGROUND) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2521 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2522 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2523 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2524 | png_color back; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2525 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2526 | back.red = (png_byte)png_ptr->background.red; |
| 2527 | back.green = (png_byte)png_ptr->background.green; |
| 2528 | back.blue = (png_byte)png_ptr->background.blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2529 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 2530 | for (i = 0; i < (int)png_ptr->num_trans; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2531 | { |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 2532 | if (png_ptr->trans[i] == 0) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2533 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2534 | palette[i].red = back.red; |
| 2535 | palette[i].green = back.green; |
| 2536 | palette[i].blue = back.blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2537 | } |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 2538 | else if (png_ptr->trans[i] != 0xff) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2539 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2540 | png_composite(palette[i].red, png_ptr->palette[i].red, |
| 2541 | png_ptr->trans[i], back.red); |
| 2542 | png_composite(palette[i].green, png_ptr->palette[i].green, |
| 2543 | png_ptr->trans[i], back.green); |
| 2544 | png_composite(palette[i].blue, png_ptr->palette[i].blue, |
| 2545 | png_ptr->trans[i], back.blue); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2546 | } |
| 2547 | } |
| 2548 | } |
| 2549 | else /* assume grayscale palette (what else could it be?) */ |
| 2550 | { |
| 2551 | int i; |
| 2552 | |
| 2553 | for (i = 0; i < num_palette; i++) |
| 2554 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2555 | if (i == (png_byte)png_ptr->trans_values.gray) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2556 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2557 | palette[i].red = (png_byte)png_ptr->background.red; |
| 2558 | palette[i].green = (png_byte)png_ptr->background.green; |
| 2559 | palette[i].blue = (png_byte)png_ptr->background.blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2560 | } |
| 2561 | } |
| 2562 | } |
| 2563 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2564 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2565 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2566 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2567 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2568 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2569 | /* Replace any alpha or transparency with the supplied background color. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2570 | * "background" is already in the screen gamma, while "background_1" is |
| 2571 | * at a gamma of 1.0. Paletted files have already been taken care of. |
| 2572 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2573 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2574 | png_do_background(png_row_infop row_info, png_bytep row, |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 2575 | png_color_16p trans_values, png_color_16p background |
| 2576 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 2577 | , png_color_16p background_1, |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2578 | png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2579 | png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1, |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 2580 | png_uint_16pp gamma_16_to_1, int gamma_shift |
| 2581 | #endif |
| 2582 | ) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2583 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2584 | png_bytep sp, dp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2585 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2586 | png_uint_32 row_width=row_info->width; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2587 | int shift; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2588 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2589 | png_debug(1, "in png_do_background\n"); |
| 2590 | if (background != NULL && |
| 2591 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 2592 | row != NULL && row_info != NULL && |
| 2593 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2594 | (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) || |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2595 | (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values))) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2596 | { |
| 2597 | switch (row_info->color_type) |
| 2598 | { |
| 2599 | case PNG_COLOR_TYPE_GRAY: |
| 2600 | { |
| 2601 | switch (row_info->bit_depth) |
| 2602 | { |
| 2603 | case 1: |
| 2604 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2605 | sp = row; |
| 2606 | shift = 7; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2607 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2608 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2609 | if ((png_uint_16)((*sp >> shift) & 0x01) |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 2610 | == trans_values->gray) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2611 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2612 | *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); |
| 2613 | *sp |= (png_byte)(background->gray << shift); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2614 | } |
| 2615 | if (!shift) |
| 2616 | { |
| 2617 | shift = 7; |
| 2618 | sp++; |
| 2619 | } |
| 2620 | else |
| 2621 | shift--; |
| 2622 | } |
| 2623 | break; |
| 2624 | } |
| 2625 | case 2: |
| 2626 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2627 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 2628 | if (gamma_table != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2629 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2630 | sp = row; |
| 2631 | shift = 6; |
| 2632 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2633 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2634 | if ((png_uint_16)((*sp >> shift) & 0x03) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2635 | == trans_values->gray) |
| 2636 | { |
| 2637 | *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); |
| 2638 | *sp |= (png_byte)(background->gray << shift); |
| 2639 | } |
| 2640 | else |
| 2641 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2642 | png_byte p = (png_byte)((*sp >> shift) & 0x03); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2643 | png_byte g = (png_byte)((gamma_table [p | (p << 2) | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2644 | (p << 4) | (p << 6)] >> 6) & 0x03); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2645 | *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); |
| 2646 | *sp |= (png_byte)(g << shift); |
| 2647 | } |
| 2648 | if (!shift) |
| 2649 | { |
| 2650 | shift = 6; |
| 2651 | sp++; |
| 2652 | } |
| 2653 | else |
| 2654 | shift -= 2; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2655 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2656 | } |
| 2657 | else |
| 2658 | #endif |
| 2659 | { |
| 2660 | sp = row; |
| 2661 | shift = 6; |
| 2662 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2663 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2664 | if ((png_uint_16)((*sp >> shift) & 0x03) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2665 | == trans_values->gray) |
| 2666 | { |
| 2667 | *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); |
| 2668 | *sp |= (png_byte)(background->gray << shift); |
| 2669 | } |
| 2670 | if (!shift) |
| 2671 | { |
| 2672 | shift = 6; |
| 2673 | sp++; |
| 2674 | } |
| 2675 | else |
| 2676 | shift -= 2; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2677 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2678 | } |
| 2679 | break; |
| 2680 | } |
| 2681 | case 4: |
| 2682 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2683 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 2684 | if (gamma_table != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2685 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2686 | sp = row; |
| 2687 | shift = 4; |
| 2688 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2689 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2690 | if ((png_uint_16)((*sp >> shift) & 0x0f) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2691 | == trans_values->gray) |
| 2692 | { |
| 2693 | *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); |
| 2694 | *sp |= (png_byte)(background->gray << shift); |
| 2695 | } |
| 2696 | else |
| 2697 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2698 | png_byte p = (png_byte)((*sp >> shift) & 0x0f); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2699 | png_byte g = (png_byte)((gamma_table[p | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2700 | (p << 4)] >> 4) & 0x0f); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2701 | *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); |
| 2702 | *sp |= (png_byte)(g << shift); |
| 2703 | } |
| 2704 | if (!shift) |
| 2705 | { |
| 2706 | shift = 4; |
| 2707 | sp++; |
| 2708 | } |
| 2709 | else |
| 2710 | shift -= 4; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2711 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2712 | } |
| 2713 | else |
| 2714 | #endif |
| 2715 | { |
| 2716 | sp = row; |
| 2717 | shift = 4; |
| 2718 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2719 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2720 | if ((png_uint_16)((*sp >> shift) & 0x0f) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2721 | == trans_values->gray) |
| 2722 | { |
| 2723 | *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); |
| 2724 | *sp |= (png_byte)(background->gray << shift); |
| 2725 | } |
| 2726 | if (!shift) |
| 2727 | { |
| 2728 | shift = 4; |
| 2729 | sp++; |
| 2730 | } |
| 2731 | else |
| 2732 | shift -= 4; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2733 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2734 | } |
| 2735 | break; |
| 2736 | } |
| 2737 | case 8: |
| 2738 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2739 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2740 | if (gamma_table != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2741 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2742 | sp = row; |
| 2743 | for (i = 0; i < row_width; i++, sp++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2744 | { |
| 2745 | if (*sp == trans_values->gray) |
| 2746 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2747 | *sp = (png_byte)background->gray; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2748 | } |
| 2749 | else |
| 2750 | { |
| 2751 | *sp = gamma_table[*sp]; |
| 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2756 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2757 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2758 | sp = row; |
| 2759 | for (i = 0; i < row_width; i++, sp++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2760 | { |
| 2761 | if (*sp == trans_values->gray) |
| 2762 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2763 | *sp = (png_byte)background->gray; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2764 | } |
| 2765 | } |
| 2766 | } |
| 2767 | break; |
| 2768 | } |
| 2769 | case 16: |
| 2770 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2771 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2772 | if (gamma_16 != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2773 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2774 | sp = row; |
| 2775 | for (i = 0; i < row_width; i++, sp += 2) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2776 | { |
| 2777 | png_uint_16 v; |
| 2778 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2779 | v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2780 | if (v == trans_values->gray) |
| 2781 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2782 | /* background is already in screen gamma */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2783 | *sp = (png_byte)((background->gray >> 8) & 0xff); |
| 2784 | *(sp + 1) = (png_byte)(background->gray & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2785 | } |
| 2786 | else |
| 2787 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2788 | v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2789 | *sp = (png_byte)((v >> 8) & 0xff); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 2790 | *(sp + 1) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2791 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2792 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2793 | } |
| 2794 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2795 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2796 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2797 | sp = row; |
| 2798 | for (i = 0; i < row_width; i++, sp += 2) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2799 | { |
| 2800 | png_uint_16 v; |
| 2801 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2802 | v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2803 | if (v == trans_values->gray) |
| 2804 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2805 | *sp = (png_byte)((background->gray >> 8) & 0xff); |
| 2806 | *(sp + 1) = (png_byte)(background->gray & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2807 | } |
| 2808 | } |
| 2809 | } |
| 2810 | break; |
| 2811 | } |
| 2812 | } |
| 2813 | break; |
| 2814 | } |
| 2815 | case PNG_COLOR_TYPE_RGB: |
| 2816 | { |
| 2817 | if (row_info->bit_depth == 8) |
| 2818 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2819 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2820 | if (gamma_table != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2821 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2822 | sp = row; |
| 2823 | for (i = 0; i < row_width; i++, sp += 3) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2824 | { |
| 2825 | if (*sp == trans_values->red && |
| 2826 | *(sp + 1) == trans_values->green && |
| 2827 | *(sp + 2) == trans_values->blue) |
| 2828 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2829 | *sp = (png_byte)background->red; |
| 2830 | *(sp + 1) = (png_byte)background->green; |
| 2831 | *(sp + 2) = (png_byte)background->blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2832 | } |
| 2833 | else |
| 2834 | { |
| 2835 | *sp = gamma_table[*sp]; |
| 2836 | *(sp + 1) = gamma_table[*(sp + 1)]; |
| 2837 | *(sp + 2) = gamma_table[*(sp + 2)]; |
| 2838 | } |
| 2839 | } |
| 2840 | } |
| 2841 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2842 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2843 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2844 | sp = row; |
| 2845 | for (i = 0; i < row_width; i++, sp += 3) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2846 | { |
| 2847 | if (*sp == trans_values->red && |
| 2848 | *(sp + 1) == trans_values->green && |
| 2849 | *(sp + 2) == trans_values->blue) |
| 2850 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2851 | *sp = (png_byte)background->red; |
| 2852 | *(sp + 1) = (png_byte)background->green; |
| 2853 | *(sp + 2) = (png_byte)background->blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2854 | } |
| 2855 | } |
| 2856 | } |
| 2857 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2858 | else /* if (row_info->bit_depth == 16) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2859 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2860 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2861 | if (gamma_16 != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2862 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2863 | sp = row; |
| 2864 | for (i = 0; i < row_width; i++, sp += 6) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2865 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2866 | png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
| 2867 | png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); |
| 2868 | png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2869 | if (r == trans_values->red && g == trans_values->green && |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2870 | b == trans_values->blue) |
| 2871 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2872 | /* background is already in screen gamma */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2873 | *sp = (png_byte)((background->red >> 8) & 0xff); |
| 2874 | *(sp + 1) = (png_byte)(background->red & 0xff); |
| 2875 | *(sp + 2) = (png_byte)((background->green >> 8) & 0xff); |
| 2876 | *(sp + 3) = (png_byte)(background->green & 0xff); |
| 2877 | *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff); |
| 2878 | *(sp + 5) = (png_byte)(background->blue & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2879 | } |
| 2880 | else |
| 2881 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2882 | png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2883 | *sp = (png_byte)((v >> 8) & 0xff); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 2884 | *(sp + 1) = (png_byte)(v & 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2885 | v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2886 | *(sp + 2) = (png_byte)((v >> 8) & 0xff); |
| 2887 | *(sp + 3) = (png_byte)(v & 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2888 | v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2889 | *(sp + 4) = (png_byte)((v >> 8) & 0xff); |
| 2890 | *(sp + 5) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2891 | } |
| 2892 | } |
| 2893 | } |
| 2894 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2895 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2896 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2897 | sp = row; |
| 2898 | for (i = 0; i < row_width; i++, sp += 6) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2899 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2900 | png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1)); |
| 2901 | png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); |
| 2902 | png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2903 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2904 | if (r == trans_values->red && g == trans_values->green && |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2905 | b == trans_values->blue) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2906 | { |
| 2907 | *sp = (png_byte)((background->red >> 8) & 0xff); |
| 2908 | *(sp + 1) = (png_byte)(background->red & 0xff); |
| 2909 | *(sp + 2) = (png_byte)((background->green >> 8) & 0xff); |
| 2910 | *(sp + 3) = (png_byte)(background->green & 0xff); |
| 2911 | *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff); |
| 2912 | *(sp + 5) = (png_byte)(background->blue & 0xff); |
| 2913 | } |
| 2914 | } |
| 2915 | } |
| 2916 | } |
| 2917 | break; |
| 2918 | } |
| 2919 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2920 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2921 | if (row_info->bit_depth == 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2922 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2923 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2924 | if (gamma_to_1 != NULL && gamma_from_1 != NULL && |
| 2925 | gamma_table != NULL) |
| 2926 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2927 | sp = row; |
| 2928 | dp = row; |
| 2929 | for (i = 0; i < row_width; i++, sp += 2, dp++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2930 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2931 | png_uint_16 a = *(sp + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2932 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2933 | if (a == 0xff) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2934 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2935 | *dp = gamma_table[*sp]; |
| 2936 | } |
| 2937 | else if (a == 0) |
| 2938 | { |
| 2939 | /* background is already in screen gamma */ |
| 2940 | *dp = (png_byte)background->gray; |
| 2941 | } |
| 2942 | else |
| 2943 | { |
| 2944 | png_byte v, w; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2945 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2946 | v = gamma_to_1[*sp]; |
| 2947 | png_composite(w, v, a, background_1->gray); |
| 2948 | *dp = gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2949 | } |
| 2950 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2951 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2952 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2953 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2954 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2955 | sp = row; |
| 2956 | dp = row; |
| 2957 | for (i = 0; i < row_width; i++, sp += 2, dp++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2958 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2959 | png_byte a = *(sp + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2960 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2961 | if (a == 0xff) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2962 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2963 | *dp = *sp; |
| 2964 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 2965 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2966 | else if (a == 0) |
| 2967 | { |
| 2968 | *dp = (png_byte)background->gray; |
| 2969 | } |
| 2970 | else |
| 2971 | { |
| 2972 | png_composite(*dp, *sp, a, background_1->gray); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2973 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 2974 | #else |
| 2975 | *dp = (png_byte)background->gray; |
| 2976 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2977 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2978 | } |
| 2979 | } |
| 2980 | else /* if (png_ptr->bit_depth == 16) */ |
| 2981 | { |
| 2982 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 2983 | if (gamma_16 != NULL && gamma_16_from_1 != NULL && |
| 2984 | gamma_16_to_1 != NULL) |
| 2985 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2986 | sp = row; |
| 2987 | dp = row; |
| 2988 | for (i = 0; i < row_width; i++, sp += 4, dp += 2) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2989 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2990 | png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2991 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2992 | if (a == (png_uint_16)0xffff) |
| 2993 | { |
| 2994 | png_uint_16 v; |
| 2995 | |
| 2996 | v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
| 2997 | *dp = (png_byte)((v >> 8) & 0xff); |
| 2998 | *(dp + 1) = (png_byte)(v & 0xff); |
| 2999 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3000 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3001 | else if (a == 0) |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3002 | #else |
| 3003 | else |
| 3004 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3005 | { |
| 3006 | /* background is already in screen gamma */ |
| 3007 | *dp = (png_byte)((background->gray >> 8) & 0xff); |
| 3008 | *(dp + 1) = (png_byte)(background->gray & 0xff); |
| 3009 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3010 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3011 | else |
| 3012 | { |
| 3013 | png_uint_16 g, v, w; |
| 3014 | |
| 3015 | g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; |
| 3016 | png_composite_16(v, g, a, background_1->gray); |
| 3017 | w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8]; |
| 3018 | *dp = (png_byte)((w >> 8) & 0xff); |
| 3019 | *(dp + 1) = (png_byte)(w & 0xff); |
| 3020 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3021 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3022 | } |
| 3023 | } |
| 3024 | else |
| 3025 | #endif |
| 3026 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3027 | sp = row; |
| 3028 | dp = row; |
| 3029 | for (i = 0; i < row_width; i++, sp += 4, dp += 2) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3030 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3031 | png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3032 | if (a == (png_uint_16)0xffff) |
| 3033 | { |
| 3034 | png_memcpy(dp, sp, 2); |
| 3035 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3036 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3037 | else if (a == 0) |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3038 | #else |
| 3039 | else |
| 3040 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3041 | { |
| 3042 | *dp = (png_byte)((background->gray >> 8) & 0xff); |
| 3043 | *(dp + 1) = (png_byte)(background->gray & 0xff); |
| 3044 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3045 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3046 | else |
| 3047 | { |
| 3048 | png_uint_16 g, v; |
| 3049 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3050 | g = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3051 | png_composite_16(v, g, a, background_1->gray); |
| 3052 | *dp = (png_byte)((v >> 8) & 0xff); |
| 3053 | *(dp + 1) = (png_byte)(v & 0xff); |
| 3054 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3055 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3056 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3057 | } |
| 3058 | } |
| 3059 | break; |
| 3060 | } |
| 3061 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 3062 | { |
| 3063 | if (row_info->bit_depth == 8) |
| 3064 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3065 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3066 | if (gamma_to_1 != NULL && gamma_from_1 != NULL && |
| 3067 | gamma_table != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3068 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3069 | sp = row; |
| 3070 | dp = row; |
| 3071 | for (i = 0; i < row_width; i++, sp += 4, dp += 3) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3072 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3073 | png_byte a = *(sp + 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3074 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3075 | if (a == 0xff) |
| 3076 | { |
| 3077 | *dp = gamma_table[*sp]; |
| 3078 | *(dp + 1) = gamma_table[*(sp + 1)]; |
| 3079 | *(dp + 2) = gamma_table[*(sp + 2)]; |
| 3080 | } |
| 3081 | else if (a == 0) |
| 3082 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3083 | /* background is already in screen gamma */ |
| 3084 | *dp = (png_byte)background->red; |
| 3085 | *(dp + 1) = (png_byte)background->green; |
| 3086 | *(dp + 2) = (png_byte)background->blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3087 | } |
| 3088 | else |
| 3089 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3090 | png_byte v, w; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3091 | |
| 3092 | v = gamma_to_1[*sp]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3093 | png_composite(w, v, a, background_1->red); |
| 3094 | *dp = gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3095 | v = gamma_to_1[*(sp + 1)]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3096 | png_composite(w, v, a, background_1->green); |
| 3097 | *(dp + 1) = gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3098 | v = gamma_to_1[*(sp + 2)]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3099 | png_composite(w, v, a, background_1->blue); |
| 3100 | *(dp + 2) = gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3101 | } |
| 3102 | } |
| 3103 | } |
| 3104 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3105 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3106 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3107 | sp = row; |
| 3108 | dp = row; |
| 3109 | for (i = 0; i < row_width; i++, sp += 4, dp += 3) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3110 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3111 | png_byte a = *(sp + 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3112 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3113 | if (a == 0xff) |
| 3114 | { |
| 3115 | *dp = *sp; |
| 3116 | *(dp + 1) = *(sp + 1); |
| 3117 | *(dp + 2) = *(sp + 2); |
| 3118 | } |
| 3119 | else if (a == 0) |
| 3120 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3121 | *dp = (png_byte)background->red; |
| 3122 | *(dp + 1) = (png_byte)background->green; |
| 3123 | *(dp + 2) = (png_byte)background->blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3124 | } |
| 3125 | else |
| 3126 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3127 | png_composite(*dp, *sp, a, background->red); |
| 3128 | png_composite(*(dp + 1), *(sp + 1), a, |
| 3129 | background->green); |
| 3130 | png_composite(*(dp + 2), *(sp + 2), a, |
| 3131 | background->blue); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3132 | } |
| 3133 | } |
| 3134 | } |
| 3135 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3136 | else /* if (row_info->bit_depth == 16) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3137 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3138 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3139 | if (gamma_16 != NULL && gamma_16_from_1 != NULL && |
| 3140 | gamma_16_to_1 != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3141 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3142 | sp = row; |
| 3143 | dp = row; |
| 3144 | for (i = 0; i < row_width; i++, sp += 8, dp += 6) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3145 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3146 | png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) |
| 3147 | << 8) + (png_uint_16)(*(sp + 7))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3148 | if (a == (png_uint_16)0xffff) |
| 3149 | { |
| 3150 | png_uint_16 v; |
| 3151 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3152 | v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3153 | *dp = (png_byte)((v >> 8) & 0xff); |
| 3154 | *(dp + 1) = (png_byte)(v & 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3155 | v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3156 | *(dp + 2) = (png_byte)((v >> 8) & 0xff); |
| 3157 | *(dp + 3) = (png_byte)(v & 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3158 | v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3159 | *(dp + 4) = (png_byte)((v >> 8) & 0xff); |
| 3160 | *(dp + 5) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3161 | } |
| 3162 | else if (a == 0) |
| 3163 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3164 | /* background is already in screen gamma */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3165 | *dp = (png_byte)((background->red >> 8) & 0xff); |
| 3166 | *(dp + 1) = (png_byte)(background->red & 0xff); |
| 3167 | *(dp + 2) = (png_byte)((background->green >> 8) & 0xff); |
| 3168 | *(dp + 3) = (png_byte)(background->green & 0xff); |
| 3169 | *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff); |
| 3170 | *(dp + 5) = (png_byte)(background->blue & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3171 | } |
| 3172 | else |
| 3173 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3174 | png_uint_16 v, w, x; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3175 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3176 | v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame^] | 3177 | png_composite_16(w, v, a, background_1->red); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3178 | x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; |
| 3179 | *dp = (png_byte)((x >> 8) & 0xff); |
| 3180 | *(dp + 1) = (png_byte)(x & 0xff); |
| 3181 | v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame^] | 3182 | png_composite_16(w, v, a, background_1->green); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3183 | x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; |
| 3184 | *(dp + 2) = (png_byte)((x >> 8) & 0xff); |
| 3185 | *(dp + 3) = (png_byte)(x & 0xff); |
| 3186 | v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame^] | 3187 | png_composite_16(w, v, a, background_1->blue); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3188 | x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8]; |
| 3189 | *(dp + 4) = (png_byte)((x >> 8) & 0xff); |
| 3190 | *(dp + 5) = (png_byte)(x & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3191 | } |
| 3192 | } |
| 3193 | } |
| 3194 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3195 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3196 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3197 | sp = row; |
| 3198 | dp = row; |
| 3199 | for (i = 0; i < row_width; i++, sp += 8, dp += 6) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3200 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3201 | png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) |
| 3202 | << 8) + (png_uint_16)(*(sp + 7))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3203 | if (a == (png_uint_16)0xffff) |
| 3204 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3205 | png_memcpy(dp, sp, 6); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3206 | } |
| 3207 | else if (a == 0) |
| 3208 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3209 | *dp = (png_byte)((background->red >> 8) & 0xff); |
| 3210 | *(dp + 1) = (png_byte)(background->red & 0xff); |
| 3211 | *(dp + 2) = (png_byte)((background->green >> 8) & 0xff); |
| 3212 | *(dp + 3) = (png_byte)(background->green & 0xff); |
| 3213 | *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff); |
| 3214 | *(dp + 5) = (png_byte)(background->blue & 0xff); |
| 3215 | } |
| 3216 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3217 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3218 | png_uint_16 v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3219 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3220 | png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
| 3221 | png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) |
| 3222 | + *(sp + 3)); |
| 3223 | png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) |
| 3224 | + *(sp + 5)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3225 | |
| 3226 | png_composite_16(v, r, a, background->red); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3227 | *dp = (png_byte)((v >> 8) & 0xff); |
| 3228 | *(dp + 1) = (png_byte)(v & 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3229 | png_composite_16(v, g, a, background->green); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3230 | *(dp + 2) = (png_byte)((v >> 8) & 0xff); |
| 3231 | *(dp + 3) = (png_byte)(v & 0xff); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3232 | png_composite_16(v, b, a, background->blue); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3233 | *(dp + 4) = (png_byte)((v >> 8) & 0xff); |
| 3234 | *(dp + 5) = (png_byte)(v & 0xff); |
| 3235 | } |
| 3236 | } |
| 3237 | } |
| 3238 | } |
| 3239 | break; |
| 3240 | } |
| 3241 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3242 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3243 | if (row_info->color_type & PNG_COLOR_MASK_ALPHA) |
| 3244 | { |
| 3245 | row_info->color_type &= ~PNG_COLOR_MASK_ALPHA; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3246 | row_info->channels--; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3247 | row_info->pixel_depth = (png_byte)(row_info->channels * |
| 3248 | row_info->bit_depth); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3249 | row_info->rowbytes = ((row_width * |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3250 | row_info->pixel_depth + 7) >> 3); |
| 3251 | } |
| 3252 | } |
| 3253 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3254 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3255 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3256 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3257 | /* Gamma correct the image, avoiding the alpha channel. Make sure |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 3258 | * you do this after you deal with the transparency issue on grayscale |
Glenn Randers-Pehrson | 352ca6b | 1999-09-18 15:49:20 -0500 | [diff] [blame] | 3259 | * or RGB images. If your bit depth is 8, use gamma_table, if it |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3260 | * is 16, use gamma_16_table and gamma_shift. Build these with |
| 3261 | * build_gamma_table(). |
| 3262 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3263 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3264 | png_do_gamma(png_row_infop row_info, png_bytep row, |
| 3265 | png_bytep gamma_table, png_uint_16pp gamma_16_table, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3266 | int gamma_shift) |
| 3267 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3268 | png_bytep sp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3269 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3270 | png_uint_32 row_width=row_info->width; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3271 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3272 | png_debug(1, "in png_do_gamma\n"); |
| 3273 | if ( |
| 3274 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 3275 | row != NULL && row_info != NULL && |
| 3276 | #endif |
| 3277 | ((row_info->bit_depth <= 8 && gamma_table != NULL) || |
| 3278 | (row_info->bit_depth == 16 && gamma_16_table != NULL))) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3279 | { |
| 3280 | switch (row_info->color_type) |
| 3281 | { |
| 3282 | case PNG_COLOR_TYPE_RGB: |
| 3283 | { |
| 3284 | if (row_info->bit_depth == 8) |
| 3285 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3286 | sp = row; |
| 3287 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3288 | { |
| 3289 | *sp = gamma_table[*sp]; |
| 3290 | sp++; |
| 3291 | *sp = gamma_table[*sp]; |
| 3292 | sp++; |
| 3293 | *sp = gamma_table[*sp]; |
| 3294 | sp++; |
| 3295 | } |
| 3296 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3297 | else /* if (row_info->bit_depth == 16) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3298 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3299 | sp = row; |
| 3300 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3301 | { |
| 3302 | png_uint_16 v; |
| 3303 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3304 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3305 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3306 | *(sp + 1) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3307 | sp += 2; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3308 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3309 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3310 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3311 | sp += 2; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3312 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3313 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3314 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3315 | sp += 2; |
| 3316 | } |
| 3317 | } |
| 3318 | break; |
| 3319 | } |
| 3320 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 3321 | { |
| 3322 | if (row_info->bit_depth == 8) |
| 3323 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3324 | sp = row; |
| 3325 | for (i = 0; i < row_width; i++) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3326 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3327 | *sp = gamma_table[*sp]; |
| 3328 | sp++; |
| 3329 | *sp = gamma_table[*sp]; |
| 3330 | sp++; |
| 3331 | *sp = gamma_table[*sp]; |
| 3332 | sp++; |
| 3333 | sp++; |
| 3334 | } |
| 3335 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3336 | else /* if (row_info->bit_depth == 16) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3337 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3338 | sp = row; |
| 3339 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3340 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3341 | png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3342 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3343 | *(sp + 1) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3344 | sp += 2; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3345 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3346 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3347 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3348 | sp += 2; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3349 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3350 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3351 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3352 | sp += 4; |
| 3353 | } |
| 3354 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3355 | break; |
| 3356 | } |
| 3357 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 3358 | { |
| 3359 | if (row_info->bit_depth == 8) |
| 3360 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3361 | sp = row; |
| 3362 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3363 | { |
| 3364 | *sp = gamma_table[*sp]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3365 | sp += 2; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3366 | } |
| 3367 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3368 | else /* if (row_info->bit_depth == 16) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3369 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3370 | sp = row; |
| 3371 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3372 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3373 | png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3374 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3375 | *(sp + 1) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3376 | sp += 4; |
| 3377 | } |
| 3378 | } |
| 3379 | break; |
| 3380 | } |
| 3381 | case PNG_COLOR_TYPE_GRAY: |
| 3382 | { |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 3383 | if (row_info->bit_depth == 2) |
| 3384 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3385 | sp = row; |
| 3386 | for (i = 0; i < row_width; i += 4) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 3387 | { |
| 3388 | int a = *sp & 0xc0; |
| 3389 | int b = *sp & 0x30; |
| 3390 | int c = *sp & 0x0c; |
| 3391 | int d = *sp & 0x03; |
| 3392 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3393 | *sp = (png_byte)( |
| 3394 | ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)| |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 3395 | ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)| |
| 3396 | ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)| |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3397 | ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) )); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 3398 | sp++; |
| 3399 | } |
| 3400 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3401 | if (row_info->bit_depth == 4) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3402 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3403 | sp = row; |
| 3404 | for (i = 0; i < row_width; i += 2) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3405 | { |
| 3406 | int msb = *sp & 0xf0; |
| 3407 | int lsb = *sp & 0x0f; |
| 3408 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3409 | *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0) |
| 3410 | | (((int)gamma_table[(lsb << 4) | lsb]) >> 4)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3411 | sp++; |
| 3412 | } |
| 3413 | } |
| 3414 | else if (row_info->bit_depth == 8) |
| 3415 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3416 | sp = row; |
| 3417 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3418 | { |
| 3419 | *sp = gamma_table[*sp]; |
| 3420 | sp++; |
| 3421 | } |
| 3422 | } |
| 3423 | else if (row_info->bit_depth == 16) |
| 3424 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3425 | sp = row; |
| 3426 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3427 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3428 | png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3429 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3430 | *(sp + 1) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3431 | sp += 2; |
| 3432 | } |
| 3433 | } |
| 3434 | break; |
| 3435 | } |
| 3436 | } |
| 3437 | } |
| 3438 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3439 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3440 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3441 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
Glenn Randers-Pehrson | 352ca6b | 1999-09-18 15:49:20 -0500 | [diff] [blame] | 3442 | /* Expands a palette row to an RGB or RGBA row depending |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3443 | * upon whether you supply trans and num_trans. |
| 3444 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3445 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3446 | png_do_expand_palette(png_row_infop row_info, png_bytep row, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3447 | png_colorp palette, png_bytep trans, int num_trans) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3448 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3449 | int shift, value; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3450 | png_bytep sp, dp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3451 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3452 | png_uint_32 row_width=row_info->width; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3453 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3454 | png_debug(1, "in png_do_expand_palette\n"); |
| 3455 | if ( |
| 3456 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 3457 | row != NULL && row_info != NULL && |
| 3458 | #endif |
| 3459 | row_info->color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3460 | { |
| 3461 | if (row_info->bit_depth < 8) |
| 3462 | { |
| 3463 | switch (row_info->bit_depth) |
| 3464 | { |
| 3465 | case 1: |
| 3466 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3467 | sp = row + (png_size_t)((row_width - 1) >> 3); |
| 3468 | dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3469 | shift = 7 - (int)((row_width + 7) & 0x07); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3470 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3471 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3472 | if ((*sp >> shift) & 0x01) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3473 | *dp = 1; |
| 3474 | else |
| 3475 | *dp = 0; |
| 3476 | if (shift == 7) |
| 3477 | { |
| 3478 | shift = 0; |
| 3479 | sp--; |
| 3480 | } |
| 3481 | else |
| 3482 | shift++; |
| 3483 | |
| 3484 | dp--; |
| 3485 | } |
| 3486 | break; |
| 3487 | } |
| 3488 | case 2: |
| 3489 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3490 | sp = row + (png_size_t)((row_width - 1) >> 2); |
| 3491 | dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3492 | shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3493 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3494 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3495 | value = (*sp >> shift) & 0x03; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3496 | *dp = (png_byte)value; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3497 | if (shift == 6) |
| 3498 | { |
| 3499 | shift = 0; |
| 3500 | sp--; |
| 3501 | } |
| 3502 | else |
| 3503 | shift += 2; |
| 3504 | |
| 3505 | dp--; |
| 3506 | } |
| 3507 | break; |
| 3508 | } |
| 3509 | case 4: |
| 3510 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3511 | sp = row + (png_size_t)((row_width - 1) >> 1); |
| 3512 | dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3513 | shift = (int)((row_width & 0x01) << 2); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3514 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3515 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3516 | value = (*sp >> shift) & 0x0f; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3517 | *dp = (png_byte)value; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3518 | if (shift == 4) |
| 3519 | { |
| 3520 | shift = 0; |
| 3521 | sp--; |
| 3522 | } |
| 3523 | else |
| 3524 | shift += 4; |
| 3525 | |
| 3526 | dp--; |
| 3527 | } |
| 3528 | break; |
| 3529 | } |
| 3530 | } |
| 3531 | row_info->bit_depth = 8; |
| 3532 | row_info->pixel_depth = 8; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3533 | row_info->rowbytes = row_width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3534 | } |
| 3535 | switch (row_info->bit_depth) |
| 3536 | { |
| 3537 | case 8: |
| 3538 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3539 | if (trans != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3540 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3541 | sp = row + (png_size_t)row_width - 1; |
| 3542 | dp = row + (png_size_t)(row_width << 2) - 1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3543 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3544 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3545 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3546 | if ((int)(*sp) >= num_trans) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3547 | *dp-- = 0xff; |
| 3548 | else |
| 3549 | *dp-- = trans[*sp]; |
| 3550 | *dp-- = palette[*sp].blue; |
| 3551 | *dp-- = palette[*sp].green; |
| 3552 | *dp-- = palette[*sp].red; |
| 3553 | sp--; |
| 3554 | } |
| 3555 | row_info->bit_depth = 8; |
| 3556 | row_info->pixel_depth = 32; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3557 | row_info->rowbytes = row_width * 4; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3558 | row_info->color_type = 6; |
| 3559 | row_info->channels = 4; |
| 3560 | } |
| 3561 | else |
| 3562 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3563 | sp = row + (png_size_t)row_width - 1; |
| 3564 | dp = row + (png_size_t)(row_width * 3) - 1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3565 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3566 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3567 | { |
| 3568 | *dp-- = palette[*sp].blue; |
| 3569 | *dp-- = palette[*sp].green; |
| 3570 | *dp-- = palette[*sp].red; |
| 3571 | sp--; |
| 3572 | } |
| 3573 | row_info->bit_depth = 8; |
| 3574 | row_info->pixel_depth = 24; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3575 | row_info->rowbytes = row_width * 3; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3576 | row_info->color_type = 2; |
| 3577 | row_info->channels = 3; |
| 3578 | } |
| 3579 | break; |
| 3580 | } |
| 3581 | } |
| 3582 | } |
| 3583 | } |
| 3584 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3585 | /* If the bit depth < 8, it is expanded to 8. Also, if the |
| 3586 | * transparency value is supplied, an alpha channel is built. |
| 3587 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3588 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3589 | png_do_expand(png_row_infop row_info, png_bytep row, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3590 | png_color_16p trans_value) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3591 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3592 | int shift, value; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3593 | png_bytep sp, dp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3594 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3595 | png_uint_32 row_width=row_info->width; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3596 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3597 | png_debug(1, "in png_do_expand\n"); |
| 3598 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 3599 | if (row != NULL && row_info != NULL) |
| 3600 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3601 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3602 | if (row_info->color_type == PNG_COLOR_TYPE_GRAY) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3603 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3604 | png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3605 | |
| 3606 | if (row_info->bit_depth < 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3607 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3608 | switch (row_info->bit_depth) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3609 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3610 | case 1: |
| 3611 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3612 | gray = (png_uint_16)(gray*0xff); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3613 | sp = row + (png_size_t)((row_width - 1) >> 3); |
| 3614 | dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3615 | shift = 7 - (int)((row_width + 7) & 0x07); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3616 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3617 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3618 | if ((*sp >> shift) & 0x01) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3619 | *dp = 0xff; |
| 3620 | else |
| 3621 | *dp = 0; |
| 3622 | if (shift == 7) |
| 3623 | { |
| 3624 | shift = 0; |
| 3625 | sp--; |
| 3626 | } |
| 3627 | else |
| 3628 | shift++; |
| 3629 | |
| 3630 | dp--; |
| 3631 | } |
| 3632 | break; |
| 3633 | } |
| 3634 | case 2: |
| 3635 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3636 | gray = (png_uint_16)(gray*0x55); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3637 | sp = row + (png_size_t)((row_width - 1) >> 2); |
| 3638 | dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3639 | shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3640 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3641 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3642 | value = (*sp >> shift) & 0x03; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3643 | *dp = (png_byte)(value | (value << 2) | (value << 4) | |
| 3644 | (value << 6)); |
| 3645 | if (shift == 6) |
| 3646 | { |
| 3647 | shift = 0; |
| 3648 | sp--; |
| 3649 | } |
| 3650 | else |
| 3651 | shift += 2; |
| 3652 | |
| 3653 | dp--; |
| 3654 | } |
| 3655 | break; |
| 3656 | } |
| 3657 | case 4: |
| 3658 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3659 | gray = (png_uint_16)(gray*0x11); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3660 | sp = row + (png_size_t)((row_width - 1) >> 1); |
| 3661 | dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3662 | shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3663 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3664 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3665 | value = (*sp >> shift) & 0x0f; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3666 | *dp = (png_byte)(value | (value << 4)); |
| 3667 | if (shift == 4) |
| 3668 | { |
| 3669 | shift = 0; |
| 3670 | sp--; |
| 3671 | } |
| 3672 | else |
| 3673 | shift = 4; |
| 3674 | |
| 3675 | dp--; |
| 3676 | } |
| 3677 | break; |
| 3678 | } |
| 3679 | } |
| 3680 | row_info->bit_depth = 8; |
| 3681 | row_info->pixel_depth = 8; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3682 | row_info->rowbytes = row_width; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3683 | } |
| 3684 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3685 | if (trans_value != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3686 | { |
| 3687 | if (row_info->bit_depth == 8) |
| 3688 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3689 | sp = row + (png_size_t)row_width - 1; |
| 3690 | dp = row + (png_size_t)(row_width << 1) - 1; |
| 3691 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3692 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3693 | if (*sp == gray) |
| 3694 | *dp-- = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3695 | else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3696 | *dp-- = 0xff; |
| 3697 | *dp-- = *sp--; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3698 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3699 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3700 | else if (row_info->bit_depth == 16) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3701 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3702 | sp = row + row_info->rowbytes - 1; |
| 3703 | dp = row + (row_info->rowbytes << 1) - 1; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3704 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3705 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3706 | if (((png_uint_16)*(sp) | |
| 3707 | ((png_uint_16)*(sp - 1) << 8)) == gray) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3708 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3709 | *dp-- = 0; |
| 3710 | *dp-- = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3711 | } |
| 3712 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3713 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3714 | *dp-- = 0xff; |
| 3715 | *dp-- = 0xff; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3716 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3717 | *dp-- = *sp--; |
| 3718 | *dp-- = *sp--; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3719 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3720 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3721 | row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; |
| 3722 | row_info->channels = 2; |
| 3723 | row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); |
| 3724 | row_info->rowbytes = |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3725 | ((row_width * row_info->pixel_depth) >> 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3726 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3727 | } |
| 3728 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value) |
| 3729 | { |
| 3730 | if (row_info->bit_depth == 8) |
| 3731 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3732 | sp = row + (png_size_t)row_info->rowbytes - 1; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3733 | dp = row + (png_size_t)(row_width << 2) - 1; |
| 3734 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3735 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3736 | if (*(sp - 2) == trans_value->red && |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3737 | *(sp - 1) == trans_value->green && |
| 3738 | *(sp - 0) == trans_value->blue) |
| 3739 | *dp-- = 0; |
| 3740 | else |
| 3741 | *dp-- = 0xff; |
| 3742 | *dp-- = *sp--; |
| 3743 | *dp-- = *sp--; |
| 3744 | *dp-- = *sp--; |
| 3745 | } |
| 3746 | } |
| 3747 | else if (row_info->bit_depth == 16) |
| 3748 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3749 | sp = row + row_info->rowbytes - 1; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3750 | dp = row + (png_size_t)(row_width << 3) - 1; |
| 3751 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3752 | { |
| 3753 | if ((((png_uint_16)*(sp - 4) | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3754 | ((png_uint_16)*(sp - 5) << 8)) == trans_value->red) && |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3755 | (((png_uint_16)*(sp - 2) | |
| 3756 | ((png_uint_16)*(sp - 3) << 8)) == trans_value->green) && |
| 3757 | (((png_uint_16)*(sp - 0) | |
| 3758 | ((png_uint_16)*(sp - 1) << 8)) == trans_value->blue)) |
| 3759 | { |
| 3760 | *dp-- = 0; |
| 3761 | *dp-- = 0; |
| 3762 | } |
| 3763 | else |
| 3764 | { |
| 3765 | *dp-- = 0xff; |
| 3766 | *dp-- = 0xff; |
| 3767 | } |
| 3768 | *dp-- = *sp--; |
| 3769 | *dp-- = *sp--; |
| 3770 | *dp-- = *sp--; |
| 3771 | *dp-- = *sp--; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3772 | *dp-- = *sp--; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3773 | *dp-- = *sp--; |
| 3774 | } |
| 3775 | } |
| 3776 | row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
| 3777 | row_info->channels = 4; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3778 | row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3779 | row_info->rowbytes = |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3780 | ((row_width * row_info->pixel_depth) >> 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3781 | } |
| 3782 | } |
| 3783 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3784 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3785 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3786 | #if defined(PNG_READ_DITHER_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3787 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3788 | png_do_dither(png_row_infop row_info, png_bytep row, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3789 | png_bytep palette_lookup, png_bytep dither_lookup) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3790 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3791 | png_bytep sp, dp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3792 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3793 | png_uint_32 row_width=row_info->width; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3794 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3795 | png_debug(1, "in png_do_dither\n"); |
| 3796 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 3797 | if (row != NULL && row_info != NULL) |
| 3798 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3799 | { |
| 3800 | if (row_info->color_type == PNG_COLOR_TYPE_RGB && |
| 3801 | palette_lookup && row_info->bit_depth == 8) |
| 3802 | { |
| 3803 | int r, g, b, p; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3804 | sp = row; |
| 3805 | dp = row; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3806 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3807 | { |
| 3808 | r = *sp++; |
| 3809 | g = *sp++; |
| 3810 | b = *sp++; |
| 3811 | |
| 3812 | /* this looks real messy, but the compiler will reduce |
| 3813 | it down to a reasonable formula. For example, with |
| 3814 | 5 bits per color, we get: |
| 3815 | p = (((r >> 3) & 0x1f) << 10) | |
| 3816 | (((g >> 3) & 0x1f) << 5) | |
| 3817 | ((b >> 3) & 0x1f); |
| 3818 | */ |
| 3819 | p = (((r >> (8 - PNG_DITHER_RED_BITS)) & |
| 3820 | ((1 << PNG_DITHER_RED_BITS) - 1)) << |
| 3821 | (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) | |
| 3822 | (((g >> (8 - PNG_DITHER_GREEN_BITS)) & |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3823 | ((1 << PNG_DITHER_GREEN_BITS) - 1)) << |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3824 | (PNG_DITHER_BLUE_BITS)) | |
| 3825 | ((b >> (8 - PNG_DITHER_BLUE_BITS)) & |
| 3826 | ((1 << PNG_DITHER_BLUE_BITS) - 1)); |
| 3827 | |
| 3828 | *dp++ = palette_lookup[p]; |
| 3829 | } |
| 3830 | row_info->color_type = PNG_COLOR_TYPE_PALETTE; |
| 3831 | row_info->channels = 1; |
| 3832 | row_info->pixel_depth = row_info->bit_depth; |
| 3833 | row_info->rowbytes = |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3834 | ((row_width * row_info->pixel_depth + 7) >> 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3835 | } |
| 3836 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3837 | palette_lookup != NULL && row_info->bit_depth == 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3838 | { |
| 3839 | int r, g, b, p; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3840 | sp = row; |
| 3841 | dp = row; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3842 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3843 | { |
| 3844 | r = *sp++; |
| 3845 | g = *sp++; |
| 3846 | b = *sp++; |
| 3847 | sp++; |
| 3848 | |
| 3849 | p = (((r >> (8 - PNG_DITHER_RED_BITS)) & |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3850 | ((1 << PNG_DITHER_RED_BITS) - 1)) << |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3851 | (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) | |
| 3852 | (((g >> (8 - PNG_DITHER_GREEN_BITS)) & |
| 3853 | ((1 << PNG_DITHER_GREEN_BITS) - 1)) << |
| 3854 | (PNG_DITHER_BLUE_BITS)) | |
| 3855 | ((b >> (8 - PNG_DITHER_BLUE_BITS)) & |
| 3856 | ((1 << PNG_DITHER_BLUE_BITS) - 1)); |
| 3857 | |
| 3858 | *dp++ = palette_lookup[p]; |
| 3859 | } |
| 3860 | row_info->color_type = PNG_COLOR_TYPE_PALETTE; |
| 3861 | row_info->channels = 1; |
| 3862 | row_info->pixel_depth = row_info->bit_depth; |
| 3863 | row_info->rowbytes = |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3864 | ((row_width * row_info->pixel_depth + 7) >> 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3865 | } |
| 3866 | else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && |
| 3867 | dither_lookup && row_info->bit_depth == 8) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3868 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3869 | sp = row; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3870 | for (i = 0; i < row_width; i++, sp++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3871 | { |
| 3872 | *sp = dither_lookup[*sp]; |
| 3873 | } |
| 3874 | } |
| 3875 | } |
| 3876 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3877 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3878 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 3879 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3880 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3881 | static int png_gamma_shift[] = |
| 3882 | {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0}; |
| 3883 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3884 | /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3885 | * tables, we don't make a full table if we are reducing to 8-bit in |
| 3886 | * the future. Note also how the gamma_16 tables are segmented so that |
| 3887 | * we don't need to allocate > 64K chunks for a full 16-bit table. |
| 3888 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3889 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3890 | png_build_gamma_table(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3891 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3892 | png_debug(1, "in png_build_gamma_table\n"); |
| 3893 | if(png_ptr->gamma != 0.0) |
| 3894 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3895 | if (png_ptr->bit_depth <= 8) |
| 3896 | { |
| 3897 | int i; |
| 3898 | double g; |
| 3899 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3900 | if (png_ptr->screen_gamma > .000001) |
| 3901 | g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); |
| 3902 | else |
| 3903 | g = 1.0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3904 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3905 | png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3906 | (png_uint_32)256); |
| 3907 | |
| 3908 | for (i = 0; i < 256; i++) |
| 3909 | { |
| 3910 | png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0, |
| 3911 | g) * 255.0 + .5); |
| 3912 | } |
| 3913 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3914 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ |
| 3915 | defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3916 | if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3917 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3918 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3919 | g = 1.0 / (png_ptr->gamma); |
| 3920 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3921 | png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3922 | (png_uint_32)256); |
| 3923 | |
| 3924 | for (i = 0; i < 256; i++) |
| 3925 | { |
| 3926 | png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0, |
| 3927 | g) * 255.0 + .5); |
| 3928 | } |
| 3929 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 3930 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3931 | png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3932 | (png_uint_32)256); |
| 3933 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3934 | if(png_ptr->screen_gamma > 0.000001) |
| 3935 | g = 1.0 / png_ptr->screen_gamma; |
| 3936 | else |
| 3937 | g = png_ptr->gamma; /* probably doing rgb_to_gray */ |
| 3938 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3939 | for (i = 0; i < 256; i++) |
| 3940 | { |
| 3941 | png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0, |
| 3942 | g) * 255.0 + .5); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3943 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3944 | } |
| 3945 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3946 | #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3947 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3948 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3949 | { |
| 3950 | double g; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3951 | int i, j, shift, num; |
| 3952 | int sig_bit; |
| 3953 | png_uint_32 ig; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3954 | |
| 3955 | if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3956 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3957 | sig_bit = (int)png_ptr->sig_bit.red; |
| 3958 | if ((int)png_ptr->sig_bit.green > sig_bit) |
| 3959 | sig_bit = png_ptr->sig_bit.green; |
| 3960 | if ((int)png_ptr->sig_bit.blue > sig_bit) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3961 | sig_bit = png_ptr->sig_bit.blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3962 | } |
| 3963 | else |
| 3964 | { |
| 3965 | sig_bit = (int)png_ptr->sig_bit.gray; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3966 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3967 | |
| 3968 | if (sig_bit > 0) |
| 3969 | shift = 16 - sig_bit; |
| 3970 | else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3971 | shift = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3972 | |
| 3973 | if (png_ptr->transformations & PNG_16_TO_8) |
| 3974 | { |
| 3975 | if (shift < (16 - PNG_MAX_GAMMA_8)) |
| 3976 | shift = (16 - PNG_MAX_GAMMA_8); |
| 3977 | } |
| 3978 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3979 | if (shift > 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3980 | shift = 8; |
| 3981 | if (shift < 0) |
| 3982 | shift = 0; |
| 3983 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3984 | png_ptr->gamma_shift = (png_byte)shift; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3985 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3986 | num = (1 << (8 - shift)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3987 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3988 | if (png_ptr->screen_gamma > .000001) |
| 3989 | g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); |
| 3990 | else |
| 3991 | g = 1.0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3992 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3993 | png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 3994 | (png_uint_32)(num * sizeof (png_uint_16p))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3995 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3996 | if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3997 | { |
| 3998 | double fin, fout; |
| 3999 | png_uint_32 last, max; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4000 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 4001 | for (i = 0; i < num; i++) |
| 4002 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4003 | png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 4004 | (png_uint_32)(256 * sizeof (png_uint_16))); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 4005 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4006 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 4007 | g = 1.0 / g; |
| 4008 | last = 0; |
| 4009 | for (i = 0; i < 256; i++) |
| 4010 | { |
| 4011 | fout = ((double)i + 0.5) / 256.0; |
| 4012 | fin = pow(fout, g); |
| 4013 | max = (png_uint_32)(fin * (double)((png_uint_32)num << 8)); |
| 4014 | while (last <= max) |
| 4015 | { |
| 4016 | png_ptr->gamma_16_table[(int)(last & (0xff >> shift))] |
| 4017 | [(int)(last >> (8 - shift))] = (png_uint_16)( |
| 4018 | (png_uint_16)i | ((png_uint_16)i << 8)); |
| 4019 | last++; |
| 4020 | } |
| 4021 | } |
| 4022 | while (last < ((png_uint_32)num << 8)) |
| 4023 | { |
| 4024 | png_ptr->gamma_16_table[(int)(last & (0xff >> shift))] |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 4025 | [(int)(last >> (8 - shift))] = (png_uint_16)65535L; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 4026 | last++; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 4027 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4028 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 4029 | else |
| 4030 | { |
| 4031 | for (i = 0; i < num; i++) |
| 4032 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4033 | png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 4034 | (png_uint_32)(256 * sizeof (png_uint_16))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4035 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 4036 | ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 4037 | for (j = 0; j < 256; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4038 | { |
| 4039 | png_ptr->gamma_16_table[i][j] = |
| 4040 | (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / |
| 4041 | 65535.0, g) * 65535.0 + .5); |
| 4042 | } |
| 4043 | } |
| 4044 | } |
| 4045 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4046 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ |
| 4047 | defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
| 4048 | if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4049 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4050 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4051 | g = 1.0 / (png_ptr->gamma); |
| 4052 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4053 | png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 4054 | (png_uint_32)(num * sizeof (png_uint_16p ))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4055 | |
| 4056 | for (i = 0; i < num; i++) |
| 4057 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4058 | png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 4059 | (png_uint_32)(256 * sizeof (png_uint_16))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4060 | |
| 4061 | ig = (((png_uint_32)i * |
| 4062 | (png_uint_32)png_gamma_shift[shift]) >> 4); |
| 4063 | for (j = 0; j < 256; j++) |
| 4064 | { |
| 4065 | png_ptr->gamma_16_to_1[i][j] = |
| 4066 | (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / |
| 4067 | 65535.0, g) * 65535.0 + .5); |
| 4068 | } |
| 4069 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4070 | |
| 4071 | if(png_ptr->screen_gamma > 0.000001) |
| 4072 | g = 1.0 / png_ptr->screen_gamma; |
| 4073 | else |
| 4074 | g = png_ptr->gamma; /* probably doing rgb_to_gray */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4075 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4076 | png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 4077 | (png_uint_32)(num * sizeof (png_uint_16p))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4078 | |
| 4079 | for (i = 0; i < num; i++) |
| 4080 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4081 | png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 4082 | (png_uint_32)(256 * sizeof (png_uint_16))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4083 | |
| 4084 | ig = (((png_uint_32)i * |
| 4085 | (png_uint_32)png_gamma_shift[shift]) >> 4); |
| 4086 | for (j = 0; j < 256; j++) |
| 4087 | { |
| 4088 | png_ptr->gamma_16_from_1[i][j] = |
| 4089 | (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / |
| 4090 | 65535.0, g) * 65535.0 + .5); |
| 4091 | } |
| 4092 | } |
| 4093 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4094 | #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4095 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4096 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4097 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4098 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4099 | /* To do: install integer version of png_build_gamma_table here */ |
| 4100 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4101 | |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 4102 | #if defined(PNG_MNG_FEATURES_SUPPORTED) |
| 4103 | /* undoes intrapixel differencing */ |
| 4104 | void /* PRIVATE */ |
| 4105 | png_do_read_intrapixel(png_row_infop row_info, png_bytep row) |
| 4106 | { |
| 4107 | png_debug(1, "in png_do_read_intrapixel\n"); |
| 4108 | if ( |
| 4109 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 4110 | row != NULL && row_info != NULL && |
| 4111 | #endif |
| 4112 | (row_info->color_type & PNG_COLOR_MASK_COLOR)) |
| 4113 | { |
| 4114 | int bytes_per_pixel; |
| 4115 | png_uint_32 row_width = row_info->width; |
| 4116 | if (row_info->bit_depth == 8) |
| 4117 | { |
| 4118 | png_bytep rp; |
| 4119 | png_uint_32 i; |
| 4120 | |
| 4121 | if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 4122 | bytes_per_pixel = 3; |
| 4123 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 4124 | bytes_per_pixel = 4; |
| 4125 | else |
| 4126 | return; |
| 4127 | |
| 4128 | for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) |
| 4129 | { |
| 4130 | *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff); |
| 4131 | *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff); |
| 4132 | } |
| 4133 | } |
| 4134 | else if (row_info->bit_depth == 16) |
| 4135 | { |
| 4136 | png_bytep rp; |
| 4137 | png_uint_32 i; |
| 4138 | |
| 4139 | if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 4140 | bytes_per_pixel = 6; |
| 4141 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 4142 | bytes_per_pixel = 8; |
| 4143 | else |
| 4144 | return; |
| 4145 | |
| 4146 | for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) |
| 4147 | { |
| 4148 | png_uint_32 s0=*(rp )<<8 | *(rp+1); |
| 4149 | png_uint_32 s1=*(rp+2)<<8 | *(rp+3); |
| 4150 | png_uint_32 s2=*(rp+4)<<8 | *(rp+5); |
| 4151 | png_uint_32 red=(65536+s0+s1)&0xffff; |
| 4152 | png_uint_32 blue=(65536+s2+s1)&0xffff; |
| 4153 | *(rp ) = (png_byte)((red>>8)&0xff); |
| 4154 | *(rp+1) = (png_byte)(red&0xff); |
| 4155 | *(rp+4) = (png_byte)((blue>>8)&0xff); |
| 4156 | *(rp+5) = (png_byte)(blue&0xff); |
| 4157 | } |
| 4158 | } |
| 4159 | } |
| 4160 | } |
| 4161 | #endif /* PNG_MNG_FEATURES_SUPPORTED */ |