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 | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame^] | 4 | * libpng 1.2.2beta5 - March 26, 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; |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame^] | 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; |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame^] | 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; |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame^] | 711 | png_ptr->background.red = png_ptr->background.green |
| 712 | = png_ptr->background.blue = png_ptr->background.gray; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 713 | break; |
| 714 | case 8: |
| 715 | case 16: |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame^] | 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 | |
Glenn Randers-Pehrson | 377657d | 2002-03-08 01:31:27 -0600 | [diff] [blame] | 900 | png_ptr->background_1.gray = (png_uint_16)(pow( |
| 901 | (double)png_ptr->background.gray / m, g) * m + .5); |
| 902 | png_ptr->background.gray = (png_uint_16)(pow( |
| 903 | (double)png_ptr->background.gray / m, gs) * m + .5); |
| 904 | |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame^] | 905 | if ((png_ptr->background.red != png_ptr->background.green) || |
| 906 | (png_ptr->background.red != png_ptr->background.blue) || |
| 907 | (png_ptr->background.red != png_ptr->background.gray)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 908 | { |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame^] | 909 | /* RGB or RGBA with color background */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 910 | png_ptr->background_1.red = (png_uint_16)(pow( |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 911 | (double)png_ptr->background.red / m, g) * m + .5); |
| 912 | png_ptr->background_1.green = (png_uint_16)(pow( |
| 913 | (double)png_ptr->background.green / m, g) * m + .5); |
| 914 | png_ptr->background_1.blue = (png_uint_16)(pow( |
| 915 | (double)png_ptr->background.blue / m, g) * m + .5); |
| 916 | png_ptr->background.red = (png_uint_16)(pow( |
| 917 | (double)png_ptr->background.red / m, gs) * m + .5); |
| 918 | png_ptr->background.green = (png_uint_16)(pow( |
| 919 | (double)png_ptr->background.green / m, gs) * m + .5); |
| 920 | png_ptr->background.blue = (png_uint_16)(pow( |
| 921 | (double)png_ptr->background.blue / m, gs) * m + .5); |
| 922 | } |
| 923 | else |
| 924 | { |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame^] | 925 | /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */ |
| 926 | png_ptr->background_1.red = png_ptr->background_1.green |
| 927 | = png_ptr->background_1.blue = png_ptr->background_1.gray; |
| 928 | png_ptr->background.red = png_ptr->background.green |
| 929 | = png_ptr->background.blue = png_ptr->background.gray; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 933 | else |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 934 | /* transformation does not include PNG_BACKGROUND */ |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 935 | #endif /* PNG_READ_BACKGROUND_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 936 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
| 937 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 938 | png_colorp palette = png_ptr->palette; |
| 939 | int num_palette = png_ptr->num_palette; |
| 940 | int i; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 941 | |
| 942 | for (i = 0; i < num_palette; i++) |
| 943 | { |
| 944 | palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 945 | palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 946 | palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 951 | else |
| 952 | #endif |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 953 | #endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 954 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 955 | /* No GAMMA transformation */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 956 | if ((png_ptr->transformations & PNG_BACKGROUND) && |
| 957 | (color_type == PNG_COLOR_TYPE_PALETTE)) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 958 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 959 | int i; |
| 960 | int istop = (int)png_ptr->num_trans; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 961 | png_color back; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 962 | png_colorp palette = png_ptr->palette; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 963 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 964 | back.red = (png_byte)png_ptr->background.red; |
| 965 | back.green = (png_byte)png_ptr->background.green; |
| 966 | back.blue = (png_byte)png_ptr->background.blue; |
| 967 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 968 | for (i = 0; i < istop; i++) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 969 | { |
| 970 | if (png_ptr->trans[i] == 0) |
| 971 | { |
| 972 | palette[i] = back; |
| 973 | } |
| 974 | else if (png_ptr->trans[i] != 0xff) |
| 975 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 976 | /* The png_composite() macro is defined in png.h */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 977 | png_composite(palette[i].red, palette[i].red, |
| 978 | png_ptr->trans[i], back.red); |
| 979 | png_composite(palette[i].green, palette[i].green, |
| 980 | png_ptr->trans[i], back.green); |
| 981 | png_composite(palette[i].blue, palette[i].blue, |
| 982 | png_ptr->trans[i], back.blue); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 983 | } |
| 984 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 985 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 986 | #endif /* PNG_READ_BACKGROUND_SUPPORTED */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 987 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 988 | #if defined(PNG_READ_SHIFT_SUPPORTED) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 989 | if ((png_ptr->transformations & PNG_SHIFT) && |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 990 | (color_type == PNG_COLOR_TYPE_PALETTE)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 991 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 992 | png_uint_16 i; |
| 993 | png_uint_16 istop = png_ptr->num_palette; |
| 994 | int sr = 8 - png_ptr->sig_bit.red; |
| 995 | int sg = 8 - png_ptr->sig_bit.green; |
| 996 | int sb = 8 - png_ptr->sig_bit.blue; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 997 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 998 | if (sr < 0 || sr > 8) |
| 999 | sr = 0; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1000 | if (sg < 0 || sg > 8) |
| 1001 | sg = 0; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1002 | if (sb < 0 || sb > 8) |
| 1003 | sb = 0; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1004 | for (i = 0; i < istop; i++) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1005 | { |
| 1006 | png_ptr->palette[i].red >>= sr; |
| 1007 | png_ptr->palette[i].green >>= sg; |
| 1008 | png_ptr->palette[i].blue >>= sb; |
| 1009 | } |
| 1010 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 1011 | #endif /* PNG_READ_SHIFT_SUPPORTED */ |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 1012 | } |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1013 | #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \ |
| 1014 | && !defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 1015 | if(png_ptr) |
| 1016 | return; |
| 1017 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1018 | } |
| 1019 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1020 | /* Modify the info structure to reflect the transformations. The |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1021 | * info should be updated so a PNG file could be written with it, |
| 1022 | * assuming the transformations result in valid PNG data. |
| 1023 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1024 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1025 | png_read_transform_info(png_structp png_ptr, png_infop info_ptr) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1026 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1027 | png_debug(1, "in png_read_transform_info\n"); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1028 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1029 | if (png_ptr->transformations & PNG_EXPAND) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1030 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1031 | if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1032 | { |
| 1033 | if (png_ptr->num_trans) |
| 1034 | info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
| 1035 | else |
| 1036 | info_ptr->color_type = PNG_COLOR_TYPE_RGB; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1037 | info_ptr->bit_depth = 8; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1038 | info_ptr->num_trans = 0; |
| 1039 | } |
| 1040 | else |
| 1041 | { |
| 1042 | if (png_ptr->num_trans) |
| 1043 | info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; |
| 1044 | if (info_ptr->bit_depth < 8) |
| 1045 | info_ptr->bit_depth = 8; |
| 1046 | info_ptr->num_trans = 0; |
| 1047 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1048 | } |
| 1049 | #endif |
| 1050 | |
| 1051 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 1052 | if (png_ptr->transformations & PNG_BACKGROUND) |
| 1053 | { |
| 1054 | info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA; |
| 1055 | info_ptr->num_trans = 0; |
| 1056 | info_ptr->background = png_ptr->background; |
| 1057 | } |
| 1058 | #endif |
| 1059 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1060 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 1061 | if (png_ptr->transformations & PNG_GAMMA) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1062 | { |
| 1063 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1064 | info_ptr->gamma = png_ptr->gamma; |
| 1065 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1066 | #ifdef PNG_FIXED_POINT_SUPPORTED |
| 1067 | info_ptr->int_gamma = png_ptr->int_gamma; |
| 1068 | #endif |
| 1069 | } |
| 1070 | #endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1071 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1072 | #if defined(PNG_READ_16_TO_8_SUPPORTED) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1073 | 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] | 1074 | info_ptr->bit_depth = 8; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1075 | #endif |
| 1076 | |
| 1077 | #if defined(PNG_READ_DITHER_SUPPORTED) |
| 1078 | if (png_ptr->transformations & PNG_DITHER) |
| 1079 | { |
| 1080 | if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || |
| 1081 | (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && |
| 1082 | png_ptr->palette_lookup && info_ptr->bit_depth == 8) |
| 1083 | { |
| 1084 | info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; |
| 1085 | } |
| 1086 | } |
| 1087 | #endif |
| 1088 | |
| 1089 | #if defined(PNG_READ_PACK_SUPPORTED) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1090 | if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1091 | info_ptr->bit_depth = 8; |
| 1092 | #endif |
| 1093 | |
| 1094 | #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 1095 | if (png_ptr->transformations & PNG_GRAY_TO_RGB) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1096 | info_ptr->color_type |= PNG_COLOR_MASK_COLOR; |
| 1097 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1098 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1099 | #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
| 1100 | if (png_ptr->transformations & PNG_RGB_TO_GRAY) |
| 1101 | info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR; |
| 1102 | #endif |
| 1103 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1104 | if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1105 | info_ptr->channels = 1; |
| 1106 | else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 1107 | info_ptr->channels = 3; |
| 1108 | else |
| 1109 | info_ptr->channels = 1; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1110 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1111 | #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) |
Glenn Randers-Pehrson | f9f2fe0 | 1998-03-15 18:20:23 -0600 | [diff] [blame] | 1112 | if (png_ptr->transformations & PNG_STRIP_ALPHA) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1113 | info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1114 | #endif |
| 1115 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1116 | if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) |
| 1117 | info_ptr->channels++; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1118 | |
| 1119 | #if defined(PNG_READ_FILLER_SUPPORTED) |
| 1120 | /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1121 | if ((png_ptr->transformations & PNG_FILLER) && |
| 1122 | ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || |
| 1123 | (info_ptr->color_type == PNG_COLOR_TYPE_GRAY))) |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1124 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1125 | info_ptr->channels++; |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1126 | #if 0 /* if adding a true alpha channel not just filler */ |
| 1127 | info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; |
| 1128 | #endif |
| 1129 | } |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1130 | #endif |
| 1131 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1132 | #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ |
| 1133 | defined(PNG_READ_USER_TRANSFORM_SUPPORTED) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1134 | if(png_ptr->transformations & PNG_USER_TRANSFORM) |
| 1135 | { |
| 1136 | if(info_ptr->bit_depth < png_ptr->user_transform_depth) |
| 1137 | info_ptr->bit_depth = png_ptr->user_transform_depth; |
| 1138 | if(info_ptr->channels < png_ptr->user_transform_channels) |
| 1139 | info_ptr->channels = png_ptr->user_transform_channels; |
| 1140 | } |
| 1141 | #endif |
| 1142 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1143 | info_ptr->pixel_depth = (png_byte)(info_ptr->channels * |
| 1144 | info_ptr->bit_depth); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1145 | 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] | 1146 | |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 1147 | #if !defined(PNG_READ_EXPAND_SUPPORTED) |
| 1148 | if(png_ptr) |
| 1149 | return; |
| 1150 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1151 | } |
| 1152 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1153 | /* Transform the row. The order of transformations is significant, |
| 1154 | * and is very touchy. If you add a transformation, take care to |
| 1155 | * decide how it fits in with the other transformations here. |
| 1156 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1157 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1158 | png_do_read_transformations(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1159 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1160 | png_debug(1, "in png_do_read_transformations\n"); |
| 1161 | #if !defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1162 | if (png_ptr->row_buf == NULL) |
| 1163 | { |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 1164 | #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1165 | char msg[50]; |
| 1166 | |
| 1167 | sprintf(msg, "NULL row buffer for row %ld, pass %d", png_ptr->row_number, |
| 1168 | png_ptr->pass); |
| 1169 | png_error(png_ptr, msg); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1170 | #else |
| 1171 | png_error(png_ptr, "NULL row buffer"); |
| 1172 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1173 | } |
| 1174 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1175 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1176 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1177 | if (png_ptr->transformations & PNG_EXPAND) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1178 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1179 | if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE) |
| 1180 | { |
| 1181 | png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1182 | png_ptr->palette, png_ptr->trans, png_ptr->num_trans); |
| 1183 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1184 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1185 | { |
| 1186 | if (png_ptr->num_trans) |
| 1187 | png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1188 | &(png_ptr->trans_values)); |
| 1189 | else |
| 1190 | png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1191 | NULL); |
| 1192 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1193 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1194 | #endif |
| 1195 | |
| 1196 | #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) |
| 1197 | if (png_ptr->transformations & PNG_STRIP_ALPHA) |
| 1198 | png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1199 | PNG_FLAG_FILLER_AFTER); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1200 | #endif |
| 1201 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1202 | #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
| 1203 | if (png_ptr->transformations & PNG_RGB_TO_GRAY) |
| 1204 | { |
| 1205 | int rgb_error = |
| 1206 | png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1); |
| 1207 | if(rgb_error) |
| 1208 | { |
| 1209 | png_ptr->rgb_to_gray_status=1; |
| 1210 | if(png_ptr->transformations == PNG_RGB_TO_GRAY_WARN) |
| 1211 | png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); |
| 1212 | if(png_ptr->transformations == PNG_RGB_TO_GRAY_ERR) |
| 1213 | png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); |
| 1214 | } |
| 1215 | } |
| 1216 | #endif |
| 1217 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1218 | /* |
| 1219 | From Andreas Dilger e-mail to png-implement, 26 March 1998: |
| 1220 | |
| 1221 | In most cases, the "simple transparency" should be done prior to doing |
| 1222 | 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] | 1223 | 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] | 1224 | transparency information is upgraded to RGB. |
| 1225 | |
| 1226 | To summarize, the current flow is: |
| 1227 | - Gray + simple transparency -> compare 1 or 2 gray bytes and composite |
| 1228 | with background "in place" if transparent, |
| 1229 | convert to RGB if necessary |
| 1230 | - Gray + alpha -> composite with gray background and remove alpha bytes, |
| 1231 | convert to RGB if necessary |
| 1232 | |
| 1233 | To support RGB backgrounds for gray images we need: |
| 1234 | - Gray + simple transparency -> convert to RGB + simple transparency, compare |
| 1235 | 3 or 6 bytes and composite with background |
| 1236 | "in place" if transparent (3x compare/pixel |
| 1237 | compared to doing composite with gray bkgrnd) |
| 1238 | - Gray + alpha -> convert to RGB + alpha, composite with background and |
| 1239 | remove alpha bytes (3x float operations/pixel |
| 1240 | compared with composite on gray background) |
| 1241 | |
| 1242 | Greg's change will do this. The reason it wasn't done before is for |
| 1243 | performance, as this increases the per-pixel operations. If we would check |
| 1244 | in advance if the background was gray or RGB, and position the gray-to-RGB |
| 1245 | transform appropriately, then it would save a lot of work/time. |
| 1246 | */ |
| 1247 | |
| 1248 | #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) |
| 1249 | /* if gray -> RGB, do so now only if background is non-gray; else do later |
| 1250 | * for performance reasons */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1251 | if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 1252 | !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1253 | png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 1254 | #endif |
| 1255 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1256 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1257 | if ((png_ptr->transformations & PNG_BACKGROUND) && |
| 1258 | ((png_ptr->num_trans != 0 ) || |
| 1259 | (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1260 | 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] | 1261 | &(png_ptr->trans_values), &(png_ptr->background) |
| 1262 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 1263 | , &(png_ptr->background_1), |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1264 | png_ptr->gamma_table, png_ptr->gamma_from_1, |
| 1265 | png_ptr->gamma_to_1, png_ptr->gamma_16_table, |
| 1266 | 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] | 1267 | png_ptr->gamma_shift |
| 1268 | #endif |
| 1269 | ); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1270 | #endif |
| 1271 | |
| 1272 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 1273 | if ((png_ptr->transformations & PNG_GAMMA) && |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 1274 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 1275 | !((png_ptr->transformations & PNG_BACKGROUND) && |
| 1276 | ((png_ptr->num_trans != 0) || |
| 1277 | (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) && |
| 1278 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1279 | (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1280 | png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1281 | png_ptr->gamma_table, png_ptr->gamma_16_table, |
| 1282 | png_ptr->gamma_shift); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1283 | #endif |
| 1284 | |
| 1285 | #if defined(PNG_READ_16_TO_8_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1286 | if (png_ptr->transformations & PNG_16_TO_8) |
| 1287 | png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1288 | #endif |
| 1289 | |
| 1290 | #if defined(PNG_READ_DITHER_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1291 | if (png_ptr->transformations & PNG_DITHER) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1292 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1293 | png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1294 | png_ptr->palette_lookup, png_ptr->dither_index); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1295 | if(png_ptr->row_info.rowbytes == (png_uint_32)0) |
| 1296 | png_error(png_ptr, "png_do_dither returned rowbytes=0"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1297 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1298 | #endif |
| 1299 | |
| 1300 | #if defined(PNG_READ_INVERT_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1301 | if (png_ptr->transformations & PNG_INVERT_MONO) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1302 | png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1303 | #endif |
| 1304 | |
| 1305 | #if defined(PNG_READ_SHIFT_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1306 | if (png_ptr->transformations & PNG_SHIFT) |
| 1307 | png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 1308 | &(png_ptr->shift)); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1309 | #endif |
| 1310 | |
| 1311 | #if defined(PNG_READ_PACK_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1312 | if (png_ptr->transformations & PNG_PACK) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1313 | png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1314 | #endif |
| 1315 | |
| 1316 | #if defined(PNG_READ_BGR_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1317 | if (png_ptr->transformations & PNG_BGR) |
| 1318 | png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1319 | #endif |
| 1320 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1321 | #if defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 1322 | if (png_ptr->transformations & PNG_PACKSWAP) |
| 1323 | png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 1324 | #endif |
| 1325 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1326 | #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1327 | /* 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] | 1328 | if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && |
| 1329 | (png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1330 | 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] | 1331 | #endif |
| 1332 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1333 | #if defined(PNG_READ_FILLER_SUPPORTED) |
| 1334 | if (png_ptr->transformations & PNG_FILLER) |
| 1335 | 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] | 1336 | (png_uint_32)png_ptr->filler, png_ptr->flags); |
| 1337 | #endif |
| 1338 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1339 | #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) |
| 1340 | if (png_ptr->transformations & PNG_INVERT_ALPHA) |
| 1341 | png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 1342 | #endif |
| 1343 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1344 | #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) |
| 1345 | if (png_ptr->transformations & PNG_SWAP_ALPHA) |
| 1346 | png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 1347 | #endif |
| 1348 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1349 | #if defined(PNG_READ_SWAP_SUPPORTED) |
| 1350 | if (png_ptr->transformations & PNG_SWAP_BYTES) |
| 1351 | png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1352 | #endif |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1353 | |
| 1354 | #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) |
| 1355 | if (png_ptr->transformations & PNG_USER_TRANSFORM) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1356 | { |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1357 | if(png_ptr->read_user_transform_fn != NULL) |
| 1358 | (*(png_ptr->read_user_transform_fn)) /* user read transform function */ |
| 1359 | (png_ptr, /* png_ptr */ |
| 1360 | &(png_ptr->row_info), /* row_info: */ |
| 1361 | /* png_uint_32 width; width of row */ |
| 1362 | /* png_uint_32 rowbytes; number of bytes in row */ |
| 1363 | /* png_byte color_type; color type of pixels */ |
| 1364 | /* png_byte bit_depth; bit depth of samples */ |
| 1365 | /* png_byte channels; number of channels (1-4) */ |
| 1366 | /* png_byte pixel_depth; bits per pixel (depth*channels) */ |
| 1367 | png_ptr->row_buf + 1); /* start of pixel data for row */ |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1368 | #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1369 | if(png_ptr->user_transform_depth) |
| 1370 | png_ptr->row_info.bit_depth = png_ptr->user_transform_depth; |
| 1371 | if(png_ptr->user_transform_channels) |
| 1372 | png_ptr->row_info.channels = png_ptr->user_transform_channels; |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1373 | #endif |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1374 | png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth * |
| 1375 | png_ptr->row_info.channels); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1376 | png_ptr->row_info.rowbytes = (png_ptr->row_info.width * |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1377 | png_ptr->row_info.pixel_depth+7)>>3; |
| 1378 | } |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1379 | #endif |
| 1380 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1381 | } |
| 1382 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1383 | #if defined(PNG_READ_PACK_SUPPORTED) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1384 | /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel, |
| 1385 | * without changing the actual values. Thus, if you had a row with |
| 1386 | * a bit depth of 1, you would end up with bytes that only contained |
| 1387 | * the numbers 0 or 1. If you would rather they contain 0 and 255, use |
| 1388 | * png_do_shift() after this. |
| 1389 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1390 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1391 | png_do_unpack(png_row_infop row_info, png_bytep row) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1392 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1393 | png_debug(1, "in png_do_unpack\n"); |
| 1394 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1395 | if (row != NULL && row_info != NULL && row_info->bit_depth < 8) |
| 1396 | #else |
| 1397 | if (row_info->bit_depth < 8) |
| 1398 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1399 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1400 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1401 | png_uint_32 row_width=row_info->width; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1402 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1403 | switch (row_info->bit_depth) |
| 1404 | { |
| 1405 | case 1: |
| 1406 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1407 | png_bytep sp = row + (png_size_t)((row_width - 1) >> 3); |
| 1408 | png_bytep dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1409 | png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1410 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1411 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1412 | *dp = (png_byte)((*sp >> shift) & 0x01); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1413 | if (shift == 7) |
| 1414 | { |
| 1415 | shift = 0; |
| 1416 | sp--; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1417 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1418 | else |
| 1419 | shift++; |
| 1420 | |
| 1421 | dp--; |
| 1422 | } |
| 1423 | break; |
| 1424 | } |
| 1425 | case 2: |
| 1426 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1427 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1428 | png_bytep sp = row + (png_size_t)((row_width - 1) >> 2); |
| 1429 | png_bytep dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1430 | png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1431 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1432 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1433 | *dp = (png_byte)((*sp >> shift) & 0x03); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1434 | if (shift == 6) |
| 1435 | { |
| 1436 | shift = 0; |
| 1437 | sp--; |
| 1438 | } |
| 1439 | else |
| 1440 | shift += 2; |
| 1441 | |
| 1442 | dp--; |
| 1443 | } |
| 1444 | break; |
| 1445 | } |
| 1446 | case 4: |
| 1447 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1448 | png_bytep sp = row + (png_size_t)((row_width - 1) >> 1); |
| 1449 | png_bytep dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1450 | png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1451 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1452 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1453 | *dp = (png_byte)((*sp >> shift) & 0x0f); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1454 | if (shift == 4) |
| 1455 | { |
| 1456 | shift = 0; |
| 1457 | sp--; |
| 1458 | } |
| 1459 | else |
| 1460 | shift = 4; |
| 1461 | |
| 1462 | dp--; |
| 1463 | } |
| 1464 | break; |
| 1465 | } |
| 1466 | } |
| 1467 | row_info->bit_depth = 8; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1468 | row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1469 | row_info->rowbytes = row_width * row_info->channels; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1470 | } |
| 1471 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1472 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1473 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1474 | #if defined(PNG_READ_SHIFT_SUPPORTED) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1475 | /* Reverse the effects of png_do_shift. This routine merely shifts the |
| 1476 | * pixels back to their significant bits values. Thus, if you have |
| 1477 | * a row of bit depth 8, but only 5 are significant, this will shift |
| 1478 | * the values back to 0 through 31. |
| 1479 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1480 | void /* PRIVATE */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1481 | 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] | 1482 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1483 | png_debug(1, "in png_do_unshift\n"); |
| 1484 | if ( |
| 1485 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1486 | row != NULL && row_info != NULL && sig_bits != NULL && |
| 1487 | #endif |
| 1488 | row_info->color_type != PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1489 | { |
| 1490 | int shift[4]; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1491 | int channels = 0; |
| 1492 | int c; |
| 1493 | png_uint_16 value = 0; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1494 | png_uint_32 row_width = row_info->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1495 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1496 | if (row_info->color_type & PNG_COLOR_MASK_COLOR) |
| 1497 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1498 | shift[channels++] = row_info->bit_depth - sig_bits->red; |
| 1499 | shift[channels++] = row_info->bit_depth - sig_bits->green; |
| 1500 | shift[channels++] = row_info->bit_depth - sig_bits->blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1501 | } |
| 1502 | else |
| 1503 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1504 | shift[channels++] = row_info->bit_depth - sig_bits->gray; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1505 | } |
| 1506 | if (row_info->color_type & PNG_COLOR_MASK_ALPHA) |
| 1507 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1508 | shift[channels++] = row_info->bit_depth - sig_bits->alpha; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1509 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1510 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1511 | for (c = 0; c < channels; c++) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1512 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1513 | if (shift[c] <= 0) |
| 1514 | shift[c] = 0; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1515 | else |
| 1516 | value = 1; |
| 1517 | } |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1518 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1519 | if (!value) |
| 1520 | return; |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1521 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1522 | switch (row_info->bit_depth) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1523 | { |
| 1524 | case 2: |
| 1525 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1526 | png_bytep bp; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1527 | png_uint_32 i; |
| 1528 | png_uint_32 istop = row_info->rowbytes; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1529 | |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1530 | for (bp = row, i = 0; i < istop; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1531 | { |
| 1532 | *bp >>= 1; |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1533 | *bp++ &= 0x55; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1534 | } |
| 1535 | break; |
| 1536 | } |
| 1537 | case 4: |
| 1538 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 1539 | png_bytep bp = row; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1540 | png_uint_32 i; |
| 1541 | png_uint_32 istop = row_info->rowbytes; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1542 | png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) | |
| 1543 | (png_byte)((int)0xf >> shift[0])); |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1544 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 1545 | for (i = 0; i < istop; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1546 | { |
| 1547 | *bp >>= shift[0]; |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1548 | *bp++ &= mask; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1549 | } |
| 1550 | break; |
| 1551 | } |
| 1552 | case 8: |
| 1553 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1554 | png_bytep bp = row; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1555 | png_uint_32 i; |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1556 | png_uint_32 istop = row_width * channels; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1557 | |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1558 | for (i = 0; i < istop; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1559 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1560 | *bp++ >>= shift[i%channels]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1561 | } |
| 1562 | break; |
| 1563 | } |
| 1564 | case 16: |
| 1565 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1566 | png_bytep bp = row; |
| 1567 | png_uint_32 i; |
| 1568 | png_uint_32 istop = channels * row_width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1569 | |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1570 | for (i = 0; i < istop; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1571 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1572 | value = (png_uint_16)((*bp << 8) + *(bp + 1)); |
| 1573 | value >>= shift[i%channels]; |
| 1574 | *bp++ = (png_byte)(value >> 8); |
| 1575 | *bp++ = (png_byte)(value & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1576 | } |
| 1577 | break; |
| 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1582 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1583 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1584 | #if defined(PNG_READ_16_TO_8_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1585 | /* chop rows of bit depth 16 down to 8 */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1586 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1587 | png_do_chop(png_row_infop row_info, png_bytep row) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1588 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1589 | png_debug(1, "in png_do_chop\n"); |
| 1590 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1591 | if (row != NULL && row_info != NULL && row_info->bit_depth == 16) |
| 1592 | #else |
| 1593 | if (row_info->bit_depth == 16) |
| 1594 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1595 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1596 | png_bytep sp = row; |
| 1597 | png_bytep dp = row; |
| 1598 | png_uint_32 i; |
| 1599 | png_uint_32 istop = row_info->width * row_info->channels; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1600 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1601 | for (i = 0; i<istop; i++, sp += 2, dp++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1602 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1603 | #if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1604 | /* This does a more accurate scaling of the 16-bit color |
| 1605 | * value, rather than a simple low-byte truncation. |
| 1606 | * |
| 1607 | * What the ideal calculation should be: |
| 1608 | * *dp = (((((png_uint_32)(*sp) << 8) | |
| 1609 | * (png_uint_32)(*(sp + 1))) * 255 + 127) / (png_uint_32)65535L; |
| 1610 | * |
| 1611 | * GRR: no, I think this is what it really should be: |
| 1612 | * *dp = (((((png_uint_32)(*sp) << 8) | |
| 1613 | * (png_uint_32)(*(sp + 1))) + 128L) / (png_uint_32)257L; |
| 1614 | * |
| 1615 | * GRR: here's the exact calculation with shifts: |
| 1616 | * temp = (((png_uint_32)(*sp) << 8) | (png_uint_32)(*(sp + 1))) + 128L; |
| 1617 | * *dp = (temp - (temp >> 8)) >> 8; |
| 1618 | * |
| 1619 | * Approximate calculation with shift/add instead of multiply/divide: |
| 1620 | * *dp = ((((png_uint_32)(*sp) << 8) | |
| 1621 | * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8; |
| 1622 | * |
| 1623 | * What we actually do to avoid extra shifting and conversion: |
| 1624 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1625 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1626 | *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0); |
| 1627 | #else |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1628 | /* Simply discard the low order byte */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1629 | *dp = *sp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1630 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1631 | } |
| 1632 | row_info->bit_depth = 8; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1633 | row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1634 | row_info->rowbytes = row_info->width * row_info->channels; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | #endif |
| 1638 | |
| 1639 | #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1640 | void /* PRIVATE */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1641 | png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) |
| 1642 | { |
| 1643 | png_debug(1, "in png_do_read_swap_alpha\n"); |
| 1644 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1645 | if (row != NULL && row_info != NULL) |
| 1646 | #endif |
| 1647 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1648 | png_uint_32 row_width = row_info->width; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1649 | if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 1650 | { |
| 1651 | /* This converts from RGBA to ARGB */ |
| 1652 | if (row_info->bit_depth == 8) |
| 1653 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1654 | png_bytep sp = row + row_info->rowbytes; |
| 1655 | png_bytep dp = sp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1656 | png_byte save; |
| 1657 | png_uint_32 i; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1658 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1659 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1660 | { |
| 1661 | save = *(--sp); |
| 1662 | *(--dp) = *(--sp); |
| 1663 | *(--dp) = *(--sp); |
| 1664 | *(--dp) = *(--sp); |
| 1665 | *(--dp) = save; |
| 1666 | } |
| 1667 | } |
| 1668 | /* This converts from RRGGBBAA to AARRGGBB */ |
| 1669 | else |
| 1670 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1671 | png_bytep sp = row + row_info->rowbytes; |
| 1672 | png_bytep dp = sp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1673 | png_byte save[2]; |
| 1674 | png_uint_32 i; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1675 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1676 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1677 | { |
| 1678 | save[0] = *(--sp); |
| 1679 | save[1] = *(--sp); |
| 1680 | *(--dp) = *(--sp); |
| 1681 | *(--dp) = *(--sp); |
| 1682 | *(--dp) = *(--sp); |
| 1683 | *(--dp) = *(--sp); |
| 1684 | *(--dp) = *(--sp); |
| 1685 | *(--dp) = *(--sp); |
| 1686 | *(--dp) = save[0]; |
| 1687 | *(--dp) = save[1]; |
| 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 1692 | { |
| 1693 | /* This converts from GA to AG */ |
| 1694 | if (row_info->bit_depth == 8) |
| 1695 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1696 | png_bytep sp = row + row_info->rowbytes; |
| 1697 | png_bytep dp = sp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1698 | png_byte save; |
| 1699 | png_uint_32 i; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1700 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1701 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1702 | { |
| 1703 | save = *(--sp); |
| 1704 | *(--dp) = *(--sp); |
| 1705 | *(--dp) = save; |
| 1706 | } |
| 1707 | } |
| 1708 | /* This converts from GGAA to AAGG */ |
| 1709 | else |
| 1710 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1711 | png_bytep sp = row + row_info->rowbytes; |
| 1712 | png_bytep dp = sp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1713 | png_byte save[2]; |
| 1714 | png_uint_32 i; |
| 1715 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1716 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1717 | { |
| 1718 | save[0] = *(--sp); |
| 1719 | save[1] = *(--sp); |
| 1720 | *(--dp) = *(--sp); |
| 1721 | *(--dp) = *(--sp); |
| 1722 | *(--dp) = save[0]; |
| 1723 | *(--dp) = save[1]; |
| 1724 | } |
| 1725 | } |
| 1726 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1727 | } |
| 1728 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1729 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1730 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1731 | #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1732 | void /* PRIVATE */ |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1733 | png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) |
| 1734 | { |
| 1735 | png_debug(1, "in png_do_read_invert_alpha\n"); |
| 1736 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1737 | if (row != NULL && row_info != NULL) |
| 1738 | #endif |
| 1739 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1740 | png_uint_32 row_width = row_info->width; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1741 | if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 1742 | { |
| 1743 | /* This inverts the alpha channel in RGBA */ |
| 1744 | if (row_info->bit_depth == 8) |
| 1745 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1746 | png_bytep sp = row + row_info->rowbytes; |
| 1747 | png_bytep dp = sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1748 | png_uint_32 i; |
| 1749 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1750 | for (i = 0; i < row_width; i++) |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1751 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1752 | *(--dp) = (png_byte)(255 - *(--sp)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1753 | |
| 1754 | /* This does nothing: |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1755 | *(--dp) = *(--sp); |
| 1756 | *(--dp) = *(--sp); |
| 1757 | *(--dp) = *(--sp); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1758 | We can replace it with: |
| 1759 | */ |
| 1760 | sp-=3; |
| 1761 | dp=sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1762 | } |
| 1763 | } |
| 1764 | /* This inverts the alpha channel in RRGGBBAA */ |
| 1765 | else |
| 1766 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1767 | png_bytep sp = row + row_info->rowbytes; |
| 1768 | png_bytep dp = sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1769 | png_uint_32 i; |
| 1770 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1771 | for (i = 0; i < row_width; i++) |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1772 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1773 | *(--dp) = (png_byte)(255 - *(--sp)); |
| 1774 | *(--dp) = (png_byte)(255 - *(--sp)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1775 | |
| 1776 | /* This does nothing: |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1777 | *(--dp) = *(--sp); |
| 1778 | *(--dp) = *(--sp); |
| 1779 | *(--dp) = *(--sp); |
| 1780 | *(--dp) = *(--sp); |
| 1781 | *(--dp) = *(--sp); |
| 1782 | *(--dp) = *(--sp); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1783 | We can replace it with: |
| 1784 | */ |
| 1785 | sp-=6; |
| 1786 | dp=sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1787 | } |
| 1788 | } |
| 1789 | } |
| 1790 | else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 1791 | { |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1792 | /* This inverts the alpha channel in GA */ |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1793 | if (row_info->bit_depth == 8) |
| 1794 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1795 | png_bytep sp = row + row_info->rowbytes; |
| 1796 | png_bytep dp = sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1797 | png_uint_32 i; |
| 1798 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1799 | for (i = 0; i < row_width; i++) |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1800 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1801 | *(--dp) = (png_byte)(255 - *(--sp)); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1802 | *(--dp) = *(--sp); |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1803 | } |
| 1804 | } |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1805 | /* This inverts the alpha channel in GGAA */ |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1806 | else |
| 1807 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1808 | png_bytep sp = row + row_info->rowbytes; |
| 1809 | png_bytep dp = sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1810 | png_uint_32 i; |
| 1811 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1812 | for (i = 0; i < row_width; i++) |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1813 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1814 | *(--dp) = (png_byte)(255 - *(--sp)); |
| 1815 | *(--dp) = (png_byte)(255 - *(--sp)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1816 | /* |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 1817 | *(--dp) = *(--sp); |
| 1818 | *(--dp) = *(--sp); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1819 | */ |
| 1820 | sp-=2; |
| 1821 | dp=sp; |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | } |
| 1827 | #endif |
| 1828 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1829 | #if defined(PNG_READ_FILLER_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1830 | /* Add filler channel if we have RGB color */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1831 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1832 | png_do_read_filler(png_row_infop row_info, png_bytep row, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1833 | png_uint_32 filler, png_uint_32 flags) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1834 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1835 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1836 | png_uint_32 row_width = row_info->width; |
| 1837 | |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 1838 | png_byte hi_filler = (png_byte)((filler>>8) & 0xff); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1839 | png_byte lo_filler = (png_byte)(filler & 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1840 | |
| 1841 | png_debug(1, "in png_do_read_filler\n"); |
| 1842 | if ( |
| 1843 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1844 | row != NULL && row_info != NULL && |
| 1845 | #endif |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1846 | row_info->color_type == PNG_COLOR_TYPE_GRAY) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1847 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1848 | if(row_info->bit_depth == 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1849 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1850 | /* This changes the data from G to GX */ |
| 1851 | if (flags & PNG_FLAG_FILLER_AFTER) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1852 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1853 | png_bytep sp = row + (png_size_t)row_width; |
| 1854 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1855 | for (i = 1; i < row_width; i++) |
| 1856 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1857 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1858 | *(--dp) = *(--sp); |
| 1859 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1860 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1861 | row_info->channels = 2; |
| 1862 | row_info->pixel_depth = 16; |
| 1863 | row_info->rowbytes = row_width * 2; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1864 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1865 | /* This changes the data from G to XG */ |
| 1866 | else |
| 1867 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1868 | png_bytep sp = row + (png_size_t)row_width; |
| 1869 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1870 | for (i = 0; i < row_width; i++) |
| 1871 | { |
| 1872 | *(--dp) = *(--sp); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1873 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1874 | } |
| 1875 | row_info->channels = 2; |
| 1876 | row_info->pixel_depth = 16; |
| 1877 | row_info->rowbytes = row_width * 2; |
| 1878 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1879 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1880 | else if(row_info->bit_depth == 16) |
| 1881 | { |
| 1882 | /* This changes the data from GG to GGXX */ |
| 1883 | if (flags & PNG_FLAG_FILLER_AFTER) |
| 1884 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1885 | png_bytep sp = row + (png_size_t)row_width; |
| 1886 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1887 | for (i = 1; i < row_width; i++) |
| 1888 | { |
| 1889 | *(--dp) = hi_filler; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1890 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1891 | *(--dp) = *(--sp); |
| 1892 | *(--dp) = *(--sp); |
| 1893 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1894 | *(--dp) = hi_filler; |
| 1895 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1896 | row_info->channels = 2; |
| 1897 | row_info->pixel_depth = 32; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1898 | row_info->rowbytes = row_width * 4; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1899 | } |
| 1900 | /* This changes the data from GG to XXGG */ |
| 1901 | else |
| 1902 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1903 | png_bytep sp = row + (png_size_t)row_width; |
| 1904 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1905 | for (i = 0; i < row_width; i++) |
| 1906 | { |
| 1907 | *(--dp) = *(--sp); |
| 1908 | *(--dp) = *(--sp); |
| 1909 | *(--dp) = hi_filler; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1910 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1911 | } |
| 1912 | row_info->channels = 2; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1913 | row_info->pixel_depth = 32; |
| 1914 | row_info->rowbytes = row_width * 4; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1915 | } |
| 1916 | } |
| 1917 | } /* COLOR_TYPE == GRAY */ |
| 1918 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 1919 | { |
| 1920 | if(row_info->bit_depth == 8) |
| 1921 | { |
| 1922 | /* This changes the data from RGB to RGBX */ |
| 1923 | if (flags & PNG_FLAG_FILLER_AFTER) |
| 1924 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1925 | png_bytep sp = row + (png_size_t)row_width * 3; |
| 1926 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1927 | for (i = 1; i < row_width; i++) |
| 1928 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1929 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1930 | *(--dp) = *(--sp); |
| 1931 | *(--dp) = *(--sp); |
| 1932 | *(--dp) = *(--sp); |
| 1933 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1934 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1935 | row_info->channels = 4; |
| 1936 | row_info->pixel_depth = 32; |
| 1937 | row_info->rowbytes = row_width * 4; |
| 1938 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1939 | /* This changes the data from RGB to XRGB */ |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1940 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1941 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1942 | png_bytep sp = row + (png_size_t)row_width * 3; |
| 1943 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1944 | for (i = 0; i < row_width; i++) |
| 1945 | { |
| 1946 | *(--dp) = *(--sp); |
| 1947 | *(--dp) = *(--sp); |
| 1948 | *(--dp) = *(--sp); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1949 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1950 | } |
| 1951 | row_info->channels = 4; |
| 1952 | row_info->pixel_depth = 32; |
| 1953 | row_info->rowbytes = row_width * 4; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1954 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1955 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1956 | else if(row_info->bit_depth == 16) |
| 1957 | { |
| 1958 | /* This changes the data from RRGGBB to RRGGBBXX */ |
| 1959 | if (flags & PNG_FLAG_FILLER_AFTER) |
| 1960 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1961 | png_bytep sp = row + (png_size_t)row_width * 3; |
| 1962 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1963 | for (i = 1; i < row_width; i++) |
| 1964 | { |
| 1965 | *(--dp) = hi_filler; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1966 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1967 | *(--dp) = *(--sp); |
| 1968 | *(--dp) = *(--sp); |
| 1969 | *(--dp) = *(--sp); |
| 1970 | *(--dp) = *(--sp); |
| 1971 | *(--dp) = *(--sp); |
| 1972 | *(--dp) = *(--sp); |
| 1973 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1974 | *(--dp) = hi_filler; |
| 1975 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1976 | row_info->channels = 4; |
| 1977 | row_info->pixel_depth = 64; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1978 | row_info->rowbytes = row_width * 8; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1979 | } |
| 1980 | /* This changes the data from RRGGBB to XXRRGGBB */ |
| 1981 | else |
| 1982 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 1983 | png_bytep sp = row + (png_size_t)row_width * 3; |
| 1984 | png_bytep dp = sp + (png_size_t)row_width; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1985 | for (i = 0; i < row_width; i++) |
| 1986 | { |
| 1987 | *(--dp) = *(--sp); |
| 1988 | *(--dp) = *(--sp); |
| 1989 | *(--dp) = *(--sp); |
| 1990 | *(--dp) = *(--sp); |
| 1991 | *(--dp) = *(--sp); |
| 1992 | *(--dp) = *(--sp); |
| 1993 | *(--dp) = hi_filler; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1994 | *(--dp) = lo_filler; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1995 | } |
| 1996 | row_info->channels = 4; |
| 1997 | row_info->pixel_depth = 64; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1998 | row_info->rowbytes = row_width * 8; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 1999 | } |
| 2000 | } |
| 2001 | } /* COLOR_TYPE == RGB */ |
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 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2004 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2005 | #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2006 | /* expand grayscale files to RGB, with or without alpha */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2007 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2008 | 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] | 2009 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2010 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2011 | png_uint_32 row_width = row_info->width; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2012 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2013 | png_debug(1, "in png_do_gray_to_rgb\n"); |
| 2014 | if (row_info->bit_depth >= 8 && |
| 2015 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 2016 | row != NULL && row_info != NULL && |
| 2017 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2018 | !(row_info->color_type & PNG_COLOR_MASK_COLOR)) |
| 2019 | { |
| 2020 | if (row_info->color_type == PNG_COLOR_TYPE_GRAY) |
| 2021 | { |
| 2022 | if (row_info->bit_depth == 8) |
| 2023 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2024 | png_bytep sp = row + (png_size_t)row_width - 1; |
| 2025 | png_bytep dp = sp + (png_size_t)row_width * 2; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2026 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2027 | { |
| 2028 | *(dp--) = *sp; |
| 2029 | *(dp--) = *sp; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2030 | *(dp--) = *(sp--); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2031 | } |
| 2032 | } |
| 2033 | else |
| 2034 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2035 | png_bytep sp = row + (png_size_t)row_width * 2 - 1; |
| 2036 | png_bytep dp = sp + (png_size_t)row_width * 4; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2037 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2038 | { |
| 2039 | *(dp--) = *sp; |
| 2040 | *(dp--) = *(sp - 1); |
| 2041 | *(dp--) = *sp; |
| 2042 | *(dp--) = *(sp - 1); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2043 | *(dp--) = *(sp--); |
| 2044 | *(dp--) = *(sp--); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | } |
| 2048 | else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 2049 | { |
| 2050 | if (row_info->bit_depth == 8) |
| 2051 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2052 | png_bytep sp = row + (png_size_t)row_width * 2 - 1; |
| 2053 | png_bytep dp = sp + (png_size_t)row_width * 2; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2054 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2055 | { |
| 2056 | *(dp--) = *(sp--); |
| 2057 | *(dp--) = *sp; |
| 2058 | *(dp--) = *sp; |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2059 | *(dp--) = *(sp--); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2060 | } |
| 2061 | } |
| 2062 | else |
| 2063 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2064 | png_bytep sp = row + (png_size_t)row_width * 4 - 1; |
| 2065 | png_bytep dp = sp + (png_size_t)row_width * 4; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2066 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2067 | { |
| 2068 | *(dp--) = *(sp--); |
| 2069 | *(dp--) = *(sp--); |
| 2070 | *(dp--) = *sp; |
| 2071 | *(dp--) = *(sp - 1); |
| 2072 | *(dp--) = *sp; |
| 2073 | *(dp--) = *(sp - 1); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 2074 | *(dp--) = *(sp--); |
| 2075 | *(dp--) = *(sp--); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2079 | row_info->channels += (png_byte)2; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2080 | row_info->color_type |= PNG_COLOR_MASK_COLOR; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2081 | row_info->pixel_depth = (png_byte)(row_info->channels * |
| 2082 | row_info->bit_depth); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2083 | row_info->rowbytes = ((row_width * |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2084 | row_info->pixel_depth + 7) >> 3); |
| 2085 | } |
| 2086 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2087 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2088 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2089 | #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2090 | /* reduce RGB files to grayscale, with or without alpha |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2091 | * using the equation given in Poynton's ColorFAQ at |
| 2092 | * <http://www.inforamp.net/~poynton/> |
| 2093 | * Copyright (c) 1998-01-04 Charles Poynton poynton@inforamp.net |
| 2094 | * |
| 2095 | * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B |
| 2096 | * |
| 2097 | * We approximate this with |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2098 | * |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2099 | * Y = 0.21268 * R + 0.7151 * G + 0.07217 * B |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2100 | * |
| 2101 | * which can be expressed with integers as |
| 2102 | * |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2103 | * Y = (6969 * R + 23434 * G + 2365 * B)/32768 |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2104 | * |
| 2105 | * The calculation is to be done in a linear colorspace. |
| 2106 | * |
| 2107 | * Other integer coefficents can be used via png_set_rgb_to_gray(). |
| 2108 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2109 | int /* PRIVATE */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2110 | png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) |
| 2111 | |
| 2112 | { |
| 2113 | png_uint_32 i; |
| 2114 | |
| 2115 | png_uint_32 row_width = row_info->width; |
| 2116 | int rgb_error = 0; |
| 2117 | |
| 2118 | png_debug(1, "in png_do_rgb_to_gray\n"); |
| 2119 | if ( |
| 2120 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 2121 | row != NULL && row_info != NULL && |
| 2122 | #endif |
| 2123 | (row_info->color_type & PNG_COLOR_MASK_COLOR)) |
| 2124 | { |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2125 | png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff; |
| 2126 | png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff; |
| 2127 | png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2128 | |
| 2129 | if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 2130 | { |
| 2131 | if (row_info->bit_depth == 8) |
| 2132 | { |
| 2133 | #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 2134 | if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) |
| 2135 | { |
| 2136 | png_bytep sp = row; |
| 2137 | png_bytep dp = row; |
| 2138 | |
| 2139 | for (i = 0; i < row_width; i++) |
| 2140 | { |
| 2141 | png_byte red = png_ptr->gamma_to_1[*(sp++)]; |
| 2142 | png_byte green = png_ptr->gamma_to_1[*(sp++)]; |
| 2143 | png_byte blue = png_ptr->gamma_to_1[*(sp++)]; |
| 2144 | if(red != green || red != blue) |
| 2145 | { |
| 2146 | rgb_error |= 1; |
| 2147 | *(dp++) = png_ptr->gamma_from_1[ |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2148 | (rc*red+gc*green+bc*blue)>>15]; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2149 | } |
| 2150 | else |
| 2151 | *(dp++) = *(sp-1); |
| 2152 | } |
| 2153 | } |
| 2154 | else |
| 2155 | #endif |
| 2156 | { |
| 2157 | png_bytep sp = row; |
| 2158 | png_bytep dp = row; |
| 2159 | for (i = 0; i < row_width; i++) |
| 2160 | { |
| 2161 | png_byte red = *(sp++); |
| 2162 | png_byte green = *(sp++); |
| 2163 | png_byte blue = *(sp++); |
| 2164 | if(red != green || red != blue) |
| 2165 | { |
| 2166 | rgb_error |= 1; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2167 | *(dp++) = (png_byte)((rc*red+gc*green+bc*blue)>>15); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2168 | } |
| 2169 | else |
| 2170 | *(dp++) = *(sp-1); |
| 2171 | } |
| 2172 | } |
| 2173 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2174 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2175 | else /* RGB bit_depth == 16 */ |
| 2176 | { |
| 2177 | #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 2178 | if (png_ptr->gamma_16_to_1 != NULL && |
| 2179 | png_ptr->gamma_16_from_1 != NULL) |
| 2180 | { |
| 2181 | png_bytep sp = row; |
| 2182 | png_bytep dp = row; |
| 2183 | for (i = 0; i < row_width; i++) |
| 2184 | { |
| 2185 | png_uint_16 red, green, blue, w; |
| 2186 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2187 | red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
| 2188 | green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
| 2189 | blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2190 | |
| 2191 | if(red == green && red == blue) |
| 2192 | w = red; |
| 2193 | else |
| 2194 | { |
| 2195 | png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >> |
| 2196 | png_ptr->gamma_shift][red>>8]; |
| 2197 | png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >> |
| 2198 | png_ptr->gamma_shift][green>>8]; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2199 | 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] | 2200 | png_ptr->gamma_shift][blue>>8]; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2201 | 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] | 2202 | + bc*blue_1)>>15); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2203 | w = png_ptr->gamma_16_from_1[(gray16&0xff) >> |
| 2204 | png_ptr->gamma_shift][gray16 >> 8]; |
| 2205 | rgb_error |= 1; |
| 2206 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2207 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2208 | *(dp++) = (png_byte)((w>>8) & 0xff); |
| 2209 | *(dp++) = (png_byte)(w & 0xff); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2210 | } |
| 2211 | } |
| 2212 | else |
| 2213 | #endif |
| 2214 | { |
| 2215 | png_bytep sp = row; |
| 2216 | png_bytep dp = row; |
| 2217 | for (i = 0; i < row_width; i++) |
| 2218 | { |
| 2219 | png_uint_16 red, green, blue, gray16; |
| 2220 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2221 | red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
| 2222 | green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
| 2223 | blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2224 | |
| 2225 | if(red != green || red != blue) |
| 2226 | rgb_error |= 1; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2227 | gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2228 | *(dp++) = (png_byte)((gray16>>8) & 0xff); |
| 2229 | *(dp++) = (png_byte)(gray16 & 0xff); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2230 | } |
| 2231 | } |
| 2232 | } |
| 2233 | } |
| 2234 | if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 2235 | { |
| 2236 | if (row_info->bit_depth == 8) |
| 2237 | { |
| 2238 | #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 2239 | if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) |
| 2240 | { |
| 2241 | png_bytep sp = row; |
| 2242 | png_bytep dp = row; |
| 2243 | for (i = 0; i < row_width; i++) |
| 2244 | { |
| 2245 | png_byte red = png_ptr->gamma_to_1[*(sp++)]; |
| 2246 | png_byte green = png_ptr->gamma_to_1[*(sp++)]; |
| 2247 | png_byte blue = png_ptr->gamma_to_1[*(sp++)]; |
| 2248 | if(red != green || red != blue) |
| 2249 | rgb_error |= 1; |
| 2250 | *(dp++) = png_ptr->gamma_from_1 |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2251 | [(rc*red + gc*green + bc*blue)>>15]; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2252 | *(dp++) = *(sp++); /* alpha */ |
| 2253 | } |
| 2254 | } |
| 2255 | else |
| 2256 | #endif |
| 2257 | { |
| 2258 | png_bytep sp = row; |
| 2259 | png_bytep dp = row; |
| 2260 | for (i = 0; i < row_width; i++) |
| 2261 | { |
| 2262 | png_byte red = *(sp++); |
| 2263 | png_byte green = *(sp++); |
| 2264 | png_byte blue = *(sp++); |
| 2265 | if(red != green || red != blue) |
| 2266 | rgb_error |= 1; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2267 | *(dp++) = (png_byte)((gc*red + gc*green + bc*blue)>>8); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2268 | *(dp++) = *(sp++); /* alpha */ |
| 2269 | } |
| 2270 | } |
| 2271 | } |
| 2272 | else /* RGBA bit_depth == 16 */ |
| 2273 | { |
| 2274 | #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 2275 | if (png_ptr->gamma_16_to_1 != NULL && |
| 2276 | png_ptr->gamma_16_from_1 != NULL) |
| 2277 | { |
| 2278 | png_bytep sp = row; |
| 2279 | png_bytep dp = row; |
| 2280 | for (i = 0; i < row_width; i++) |
| 2281 | { |
| 2282 | png_uint_16 red, green, blue, w; |
| 2283 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2284 | red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
| 2285 | green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
| 2286 | blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2287 | |
| 2288 | if(red == green && red == blue) |
| 2289 | w = red; |
| 2290 | else |
| 2291 | { |
| 2292 | png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >> |
| 2293 | png_ptr->gamma_shift][red>>8]; |
| 2294 | png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >> |
| 2295 | png_ptr->gamma_shift][green>>8]; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2296 | 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] | 2297 | png_ptr->gamma_shift][blue>>8]; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2298 | png_uint_16 gray16 = (png_uint_16)((rc * red_1 |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2299 | + gc * green_1 + bc * blue_1)>>15); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2300 | w = png_ptr->gamma_16_from_1[(gray16&0xff) >> |
| 2301 | png_ptr->gamma_shift][gray16 >> 8]; |
| 2302 | rgb_error |= 1; |
| 2303 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2304 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2305 | *(dp++) = (png_byte)((w>>8) & 0xff); |
| 2306 | *(dp++) = (png_byte)(w & 0xff); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2307 | *(dp++) = *(sp++); /* alpha */ |
| 2308 | *(dp++) = *(sp++); |
| 2309 | } |
| 2310 | } |
| 2311 | else |
| 2312 | #endif |
| 2313 | { |
| 2314 | png_bytep sp = row; |
| 2315 | png_bytep dp = row; |
| 2316 | for (i = 0; i < row_width; i++) |
| 2317 | { |
| 2318 | png_uint_16 red, green, blue, gray16; |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2319 | red = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; |
| 2320 | green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; |
| 2321 | blue = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2322 | if(red != green || red != blue) |
| 2323 | rgb_error |= 1; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 2324 | gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2325 | *(dp++) = (png_byte)((gray16>>8) & 0xff); |
| 2326 | *(dp++) = (png_byte)(gray16 & 0xff); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2327 | *(dp++) = *(sp++); /* alpha */ |
| 2328 | *(dp++) = *(sp++); |
| 2329 | } |
| 2330 | } |
| 2331 | } |
| 2332 | } |
| 2333 | row_info->channels -= (png_byte)2; |
| 2334 | row_info->color_type &= ~PNG_COLOR_MASK_COLOR; |
| 2335 | row_info->pixel_depth = (png_byte)(row_info->channels * |
| 2336 | row_info->bit_depth); |
| 2337 | row_info->rowbytes = ((row_width * |
| 2338 | row_info->pixel_depth + 7) >> 3); |
| 2339 | } |
| 2340 | return rgb_error; |
| 2341 | } |
| 2342 | #endif |
| 2343 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2344 | /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth |
| 2345 | * large of png_color. This lets grayscale images be treated as |
| 2346 | * paletted. Most useful for gamma correction and simplification |
| 2347 | * of code. |
| 2348 | */ |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame^] | 2349 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2350 | png_build_grayscale_palette(int bit_depth, png_colorp palette) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2351 | { |
| 2352 | int num_palette; |
| 2353 | int color_inc; |
| 2354 | int i; |
| 2355 | int v; |
| 2356 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2357 | png_debug(1, "in png_do_build_grayscale_palette\n"); |
| 2358 | if (palette == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2359 | return; |
| 2360 | |
| 2361 | switch (bit_depth) |
| 2362 | { |
| 2363 | case 1: |
| 2364 | num_palette = 2; |
| 2365 | color_inc = 0xff; |
| 2366 | break; |
| 2367 | case 2: |
| 2368 | num_palette = 4; |
| 2369 | color_inc = 0x55; |
| 2370 | break; |
| 2371 | case 4: |
| 2372 | num_palette = 16; |
| 2373 | color_inc = 0x11; |
| 2374 | break; |
| 2375 | case 8: |
| 2376 | num_palette = 256; |
| 2377 | color_inc = 1; |
| 2378 | break; |
| 2379 | default: |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2380 | num_palette = 0; |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 2381 | color_inc = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2382 | break; |
| 2383 | } |
| 2384 | |
| 2385 | for (i = 0, v = 0; i < num_palette; i++, v += color_inc) |
| 2386 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2387 | palette[i].red = (png_byte)v; |
| 2388 | palette[i].green = (png_byte)v; |
| 2389 | palette[i].blue = (png_byte)v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2390 | } |
| 2391 | } |
| 2392 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2393 | /* This function is currently unused. Do we really need it? */ |
| 2394 | #if defined(PNG_READ_DITHER_SUPPORTED) && defined(PNG_CORRECT_PALETTE_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2395 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2396 | png_correct_palette(png_structp png_ptr, png_colorp palette, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2397 | int num_palette) |
| 2398 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2399 | png_debug(1, "in png_correct_palette\n"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2400 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ |
| 2401 | defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2402 | if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2403 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2404 | png_color back, back_1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2405 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2406 | if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) |
| 2407 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2408 | back.red = png_ptr->gamma_table[png_ptr->background.red]; |
| 2409 | back.green = png_ptr->gamma_table[png_ptr->background.green]; |
| 2410 | back.blue = png_ptr->gamma_table[png_ptr->background.blue]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2411 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2412 | back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; |
| 2413 | back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; |
| 2414 | back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2415 | } |
| 2416 | else |
| 2417 | { |
| 2418 | double g; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2419 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 2420 | g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2421 | |
| 2422 | if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN || |
| 2423 | fabs(g - 1.0) < PNG_GAMMA_THRESHOLD) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2424 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2425 | back.red = png_ptr->background.red; |
| 2426 | back.green = png_ptr->background.green; |
| 2427 | back.blue = png_ptr->background.blue; |
| 2428 | } |
| 2429 | else |
| 2430 | { |
| 2431 | back.red = |
| 2432 | (png_byte)(pow((double)png_ptr->background.red/255, g) * |
| 2433 | 255.0 + 0.5); |
| 2434 | back.green = |
| 2435 | (png_byte)(pow((double)png_ptr->background.green/255, g) * |
| 2436 | 255.0 + 0.5); |
| 2437 | back.blue = |
| 2438 | (png_byte)(pow((double)png_ptr->background.blue/255, g) * |
| 2439 | 255.0 + 0.5); |
| 2440 | } |
| 2441 | |
| 2442 | g = 1.0 / png_ptr->background_gamma; |
| 2443 | |
| 2444 | back_1.red = |
| 2445 | (png_byte)(pow((double)png_ptr->background.red/255, g) * |
| 2446 | 255.0 + 0.5); |
| 2447 | back_1.green = |
| 2448 | (png_byte)(pow((double)png_ptr->background.green/255, g) * |
| 2449 | 255.0 + 0.5); |
| 2450 | back_1.blue = |
| 2451 | (png_byte)(pow((double)png_ptr->background.blue/255, g) * |
| 2452 | 255.0 + 0.5); |
| 2453 | } |
| 2454 | |
| 2455 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 2456 | { |
| 2457 | png_uint_32 i; |
| 2458 | |
| 2459 | for (i = 0; i < (png_uint_32)num_palette; i++) |
| 2460 | { |
| 2461 | if (i < png_ptr->num_trans && png_ptr->trans[i] == 0) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2462 | { |
| 2463 | palette[i] = back; |
| 2464 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2465 | else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2466 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2467 | png_byte v, w; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2468 | |
| 2469 | v = png_ptr->gamma_to_1[png_ptr->palette[i].red]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2470 | png_composite(w, v, png_ptr->trans[i], back_1.red); |
| 2471 | palette[i].red = png_ptr->gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2472 | |
| 2473 | v = png_ptr->gamma_to_1[png_ptr->palette[i].green]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2474 | png_composite(w, v, png_ptr->trans[i], back_1.green); |
| 2475 | palette[i].green = png_ptr->gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2476 | |
| 2477 | v = png_ptr->gamma_to_1[png_ptr->palette[i].blue]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2478 | png_composite(w, v, png_ptr->trans[i], back_1.blue); |
| 2479 | palette[i].blue = png_ptr->gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2480 | } |
| 2481 | else |
| 2482 | { |
| 2483 | palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 2484 | palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 2485 | palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 2486 | } |
| 2487 | } |
| 2488 | } |
| 2489 | else |
| 2490 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2491 | int i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2492 | |
| 2493 | for (i = 0; i < num_palette; i++) |
| 2494 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2495 | if (palette[i].red == (png_byte)png_ptr->trans_values.gray) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2496 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2497 | palette[i] = back; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2498 | } |
| 2499 | else |
| 2500 | { |
| 2501 | palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 2502 | palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 2503 | palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 2504 | } |
| 2505 | } |
| 2506 | } |
| 2507 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2508 | else |
| 2509 | #endif |
| 2510 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2511 | if (png_ptr->transformations & PNG_GAMMA) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2512 | { |
| 2513 | int i; |
| 2514 | |
| 2515 | for (i = 0; i < num_palette; i++) |
| 2516 | { |
| 2517 | palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 2518 | palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 2519 | palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 2520 | } |
| 2521 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2522 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 2523 | else |
| 2524 | #endif |
| 2525 | #endif |
| 2526 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2527 | if (png_ptr->transformations & PNG_BACKGROUND) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2528 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2529 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2530 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2531 | png_color back; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2532 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2533 | back.red = (png_byte)png_ptr->background.red; |
| 2534 | back.green = (png_byte)png_ptr->background.green; |
| 2535 | back.blue = (png_byte)png_ptr->background.blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2536 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 2537 | for (i = 0; i < (int)png_ptr->num_trans; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2538 | { |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 2539 | if (png_ptr->trans[i] == 0) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2540 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2541 | palette[i].red = back.red; |
| 2542 | palette[i].green = back.green; |
| 2543 | palette[i].blue = back.blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2544 | } |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 2545 | else if (png_ptr->trans[i] != 0xff) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2546 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2547 | png_composite(palette[i].red, png_ptr->palette[i].red, |
| 2548 | png_ptr->trans[i], back.red); |
| 2549 | png_composite(palette[i].green, png_ptr->palette[i].green, |
| 2550 | png_ptr->trans[i], back.green); |
| 2551 | png_composite(palette[i].blue, png_ptr->palette[i].blue, |
| 2552 | png_ptr->trans[i], back.blue); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2553 | } |
| 2554 | } |
| 2555 | } |
| 2556 | else /* assume grayscale palette (what else could it be?) */ |
| 2557 | { |
| 2558 | int i; |
| 2559 | |
| 2560 | for (i = 0; i < num_palette; i++) |
| 2561 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2562 | if (i == (png_byte)png_ptr->trans_values.gray) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2563 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2564 | palette[i].red = (png_byte)png_ptr->background.red; |
| 2565 | palette[i].green = (png_byte)png_ptr->background.green; |
| 2566 | palette[i].blue = (png_byte)png_ptr->background.blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2571 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2572 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2573 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2574 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2575 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2576 | /* Replace any alpha or transparency with the supplied background color. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2577 | * "background" is already in the screen gamma, while "background_1" is |
| 2578 | * at a gamma of 1.0. Paletted files have already been taken care of. |
| 2579 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2580 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2581 | png_do_background(png_row_infop row_info, png_bytep row, |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 2582 | png_color_16p trans_values, png_color_16p background |
| 2583 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 2584 | , png_color_16p background_1, |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2585 | 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] | 2586 | 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] | 2587 | png_uint_16pp gamma_16_to_1, int gamma_shift |
| 2588 | #endif |
| 2589 | ) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2590 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2591 | png_bytep sp, dp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2592 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2593 | png_uint_32 row_width=row_info->width; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2594 | int shift; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2595 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2596 | png_debug(1, "in png_do_background\n"); |
| 2597 | if (background != NULL && |
| 2598 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 2599 | row != NULL && row_info != NULL && |
| 2600 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2601 | (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) || |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2602 | (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values))) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2603 | { |
| 2604 | switch (row_info->color_type) |
| 2605 | { |
| 2606 | case PNG_COLOR_TYPE_GRAY: |
| 2607 | { |
| 2608 | switch (row_info->bit_depth) |
| 2609 | { |
| 2610 | case 1: |
| 2611 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2612 | sp = row; |
| 2613 | shift = 7; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2614 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2615 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2616 | if ((png_uint_16)((*sp >> shift) & 0x01) |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 2617 | == trans_values->gray) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2618 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2619 | *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); |
| 2620 | *sp |= (png_byte)(background->gray << shift); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2621 | } |
| 2622 | if (!shift) |
| 2623 | { |
| 2624 | shift = 7; |
| 2625 | sp++; |
| 2626 | } |
| 2627 | else |
| 2628 | shift--; |
| 2629 | } |
| 2630 | break; |
| 2631 | } |
| 2632 | case 2: |
| 2633 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2634 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 2635 | if (gamma_table != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2636 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2637 | sp = row; |
| 2638 | shift = 6; |
| 2639 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2640 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2641 | if ((png_uint_16)((*sp >> shift) & 0x03) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2642 | == trans_values->gray) |
| 2643 | { |
| 2644 | *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); |
| 2645 | *sp |= (png_byte)(background->gray << shift); |
| 2646 | } |
| 2647 | else |
| 2648 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2649 | png_byte p = (png_byte)((*sp >> shift) & 0x03); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2650 | png_byte g = (png_byte)((gamma_table [p | (p << 2) | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2651 | (p << 4) | (p << 6)] >> 6) & 0x03); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2652 | *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); |
| 2653 | *sp |= (png_byte)(g << shift); |
| 2654 | } |
| 2655 | if (!shift) |
| 2656 | { |
| 2657 | shift = 6; |
| 2658 | sp++; |
| 2659 | } |
| 2660 | else |
| 2661 | shift -= 2; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2662 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2663 | } |
| 2664 | else |
| 2665 | #endif |
| 2666 | { |
| 2667 | sp = row; |
| 2668 | shift = 6; |
| 2669 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2670 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2671 | if ((png_uint_16)((*sp >> shift) & 0x03) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2672 | == trans_values->gray) |
| 2673 | { |
| 2674 | *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); |
| 2675 | *sp |= (png_byte)(background->gray << shift); |
| 2676 | } |
| 2677 | if (!shift) |
| 2678 | { |
| 2679 | shift = 6; |
| 2680 | sp++; |
| 2681 | } |
| 2682 | else |
| 2683 | shift -= 2; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2684 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2685 | } |
| 2686 | break; |
| 2687 | } |
| 2688 | case 4: |
| 2689 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2690 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 2691 | if (gamma_table != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2692 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2693 | sp = row; |
| 2694 | shift = 4; |
| 2695 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2696 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2697 | if ((png_uint_16)((*sp >> shift) & 0x0f) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2698 | == trans_values->gray) |
| 2699 | { |
| 2700 | *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); |
| 2701 | *sp |= (png_byte)(background->gray << shift); |
| 2702 | } |
| 2703 | else |
| 2704 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2705 | png_byte p = (png_byte)((*sp >> shift) & 0x0f); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2706 | png_byte g = (png_byte)((gamma_table[p | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2707 | (p << 4)] >> 4) & 0x0f); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2708 | *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); |
| 2709 | *sp |= (png_byte)(g << shift); |
| 2710 | } |
| 2711 | if (!shift) |
| 2712 | { |
| 2713 | shift = 4; |
| 2714 | sp++; |
| 2715 | } |
| 2716 | else |
| 2717 | shift -= 4; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2718 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2719 | } |
| 2720 | else |
| 2721 | #endif |
| 2722 | { |
| 2723 | sp = row; |
| 2724 | shift = 4; |
| 2725 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2726 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2727 | if ((png_uint_16)((*sp >> shift) & 0x0f) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2728 | == trans_values->gray) |
| 2729 | { |
| 2730 | *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); |
| 2731 | *sp |= (png_byte)(background->gray << shift); |
| 2732 | } |
| 2733 | if (!shift) |
| 2734 | { |
| 2735 | shift = 4; |
| 2736 | sp++; |
| 2737 | } |
| 2738 | else |
| 2739 | shift -= 4; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2740 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2741 | } |
| 2742 | break; |
| 2743 | } |
| 2744 | case 8: |
| 2745 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2746 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2747 | if (gamma_table != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2748 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2749 | sp = row; |
| 2750 | for (i = 0; i < row_width; i++, sp++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2751 | { |
| 2752 | if (*sp == trans_values->gray) |
| 2753 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2754 | *sp = (png_byte)background->gray; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2755 | } |
| 2756 | else |
| 2757 | { |
| 2758 | *sp = gamma_table[*sp]; |
| 2759 | } |
| 2760 | } |
| 2761 | } |
| 2762 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2763 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2764 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2765 | sp = row; |
| 2766 | for (i = 0; i < row_width; i++, sp++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2767 | { |
| 2768 | if (*sp == trans_values->gray) |
| 2769 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2770 | *sp = (png_byte)background->gray; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2771 | } |
| 2772 | } |
| 2773 | } |
| 2774 | break; |
| 2775 | } |
| 2776 | case 16: |
| 2777 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2778 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2779 | if (gamma_16 != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2780 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2781 | sp = row; |
| 2782 | for (i = 0; i < row_width; i++, sp += 2) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2783 | { |
| 2784 | png_uint_16 v; |
| 2785 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2786 | v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2787 | if (v == trans_values->gray) |
| 2788 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2789 | /* background is already in screen gamma */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2790 | *sp = (png_byte)((background->gray >> 8) & 0xff); |
| 2791 | *(sp + 1) = (png_byte)(background->gray & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2792 | } |
| 2793 | else |
| 2794 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2795 | v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2796 | *sp = (png_byte)((v >> 8) & 0xff); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 2797 | *(sp + 1) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2798 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2799 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2800 | } |
| 2801 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2802 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2803 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2804 | sp = row; |
| 2805 | for (i = 0; i < row_width; i++, sp += 2) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2806 | { |
| 2807 | png_uint_16 v; |
| 2808 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2809 | v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2810 | if (v == trans_values->gray) |
| 2811 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2812 | *sp = (png_byte)((background->gray >> 8) & 0xff); |
| 2813 | *(sp + 1) = (png_byte)(background->gray & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2814 | } |
| 2815 | } |
| 2816 | } |
| 2817 | break; |
| 2818 | } |
| 2819 | } |
| 2820 | break; |
| 2821 | } |
| 2822 | case PNG_COLOR_TYPE_RGB: |
| 2823 | { |
| 2824 | if (row_info->bit_depth == 8) |
| 2825 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2826 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2827 | if (gamma_table != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2828 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2829 | sp = row; |
| 2830 | for (i = 0; i < row_width; i++, sp += 3) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2831 | { |
| 2832 | if (*sp == trans_values->red && |
| 2833 | *(sp + 1) == trans_values->green && |
| 2834 | *(sp + 2) == trans_values->blue) |
| 2835 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2836 | *sp = (png_byte)background->red; |
| 2837 | *(sp + 1) = (png_byte)background->green; |
| 2838 | *(sp + 2) = (png_byte)background->blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2839 | } |
| 2840 | else |
| 2841 | { |
| 2842 | *sp = gamma_table[*sp]; |
| 2843 | *(sp + 1) = gamma_table[*(sp + 1)]; |
| 2844 | *(sp + 2) = gamma_table[*(sp + 2)]; |
| 2845 | } |
| 2846 | } |
| 2847 | } |
| 2848 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2849 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2850 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2851 | sp = row; |
| 2852 | for (i = 0; i < row_width; i++, sp += 3) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2853 | { |
| 2854 | if (*sp == trans_values->red && |
| 2855 | *(sp + 1) == trans_values->green && |
| 2856 | *(sp + 2) == trans_values->blue) |
| 2857 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2858 | *sp = (png_byte)background->red; |
| 2859 | *(sp + 1) = (png_byte)background->green; |
| 2860 | *(sp + 2) = (png_byte)background->blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2861 | } |
| 2862 | } |
| 2863 | } |
| 2864 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2865 | else /* if (row_info->bit_depth == 16) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2866 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2867 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2868 | if (gamma_16 != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2869 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2870 | sp = row; |
| 2871 | for (i = 0; i < row_width; i++, sp += 6) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2872 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2873 | png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
| 2874 | png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); |
| 2875 | png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2876 | if (r == trans_values->red && g == trans_values->green && |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2877 | b == trans_values->blue) |
| 2878 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2879 | /* background is already in screen gamma */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2880 | *sp = (png_byte)((background->red >> 8) & 0xff); |
| 2881 | *(sp + 1) = (png_byte)(background->red & 0xff); |
| 2882 | *(sp + 2) = (png_byte)((background->green >> 8) & 0xff); |
| 2883 | *(sp + 3) = (png_byte)(background->green & 0xff); |
| 2884 | *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff); |
| 2885 | *(sp + 5) = (png_byte)(background->blue & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2886 | } |
| 2887 | else |
| 2888 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2889 | png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2890 | *sp = (png_byte)((v >> 8) & 0xff); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 2891 | *(sp + 1) = (png_byte)(v & 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2892 | v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2893 | *(sp + 2) = (png_byte)((v >> 8) & 0xff); |
| 2894 | *(sp + 3) = (png_byte)(v & 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2895 | v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2896 | *(sp + 4) = (png_byte)((v >> 8) & 0xff); |
| 2897 | *(sp + 5) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2898 | } |
| 2899 | } |
| 2900 | } |
| 2901 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2902 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2903 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2904 | sp = row; |
| 2905 | for (i = 0; i < row_width; i++, sp += 6) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2906 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2907 | png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1)); |
| 2908 | png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); |
| 2909 | png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2910 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2911 | if (r == trans_values->red && g == trans_values->green && |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2912 | b == trans_values->blue) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2913 | { |
| 2914 | *sp = (png_byte)((background->red >> 8) & 0xff); |
| 2915 | *(sp + 1) = (png_byte)(background->red & 0xff); |
| 2916 | *(sp + 2) = (png_byte)((background->green >> 8) & 0xff); |
| 2917 | *(sp + 3) = (png_byte)(background->green & 0xff); |
| 2918 | *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff); |
| 2919 | *(sp + 5) = (png_byte)(background->blue & 0xff); |
| 2920 | } |
| 2921 | } |
| 2922 | } |
| 2923 | } |
| 2924 | break; |
| 2925 | } |
| 2926 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2927 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2928 | if (row_info->bit_depth == 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2929 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2930 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2931 | if (gamma_to_1 != NULL && gamma_from_1 != NULL && |
| 2932 | gamma_table != NULL) |
| 2933 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2934 | sp = row; |
| 2935 | dp = row; |
| 2936 | for (i = 0; i < row_width; i++, sp += 2, dp++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2937 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2938 | png_uint_16 a = *(sp + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2939 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2940 | if (a == 0xff) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2941 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2942 | *dp = gamma_table[*sp]; |
| 2943 | } |
| 2944 | else if (a == 0) |
| 2945 | { |
| 2946 | /* background is already in screen gamma */ |
| 2947 | *dp = (png_byte)background->gray; |
| 2948 | } |
| 2949 | else |
| 2950 | { |
| 2951 | png_byte v, w; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2952 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2953 | v = gamma_to_1[*sp]; |
| 2954 | png_composite(w, v, a, background_1->gray); |
| 2955 | *dp = gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2956 | } |
| 2957 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2958 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2959 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2960 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2961 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2962 | sp = row; |
| 2963 | dp = row; |
| 2964 | for (i = 0; i < row_width; i++, sp += 2, dp++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2965 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2966 | png_byte a = *(sp + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2967 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2968 | if (a == 0xff) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2969 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2970 | *dp = *sp; |
| 2971 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 2972 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2973 | else if (a == 0) |
| 2974 | { |
| 2975 | *dp = (png_byte)background->gray; |
| 2976 | } |
| 2977 | else |
| 2978 | { |
| 2979 | png_composite(*dp, *sp, a, background_1->gray); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2980 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 2981 | #else |
| 2982 | *dp = (png_byte)background->gray; |
| 2983 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2984 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2985 | } |
| 2986 | } |
| 2987 | else /* if (png_ptr->bit_depth == 16) */ |
| 2988 | { |
| 2989 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
| 2990 | if (gamma_16 != NULL && gamma_16_from_1 != NULL && |
| 2991 | gamma_16_to_1 != NULL) |
| 2992 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2993 | sp = row; |
| 2994 | dp = row; |
| 2995 | for (i = 0; i < row_width; i++, sp += 4, dp += 2) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2996 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2997 | png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2998 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2999 | if (a == (png_uint_16)0xffff) |
| 3000 | { |
| 3001 | png_uint_16 v; |
| 3002 | |
| 3003 | v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
| 3004 | *dp = (png_byte)((v >> 8) & 0xff); |
| 3005 | *(dp + 1) = (png_byte)(v & 0xff); |
| 3006 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3007 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3008 | else if (a == 0) |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3009 | #else |
| 3010 | else |
| 3011 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3012 | { |
| 3013 | /* background is already in screen gamma */ |
| 3014 | *dp = (png_byte)((background->gray >> 8) & 0xff); |
| 3015 | *(dp + 1) = (png_byte)(background->gray & 0xff); |
| 3016 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3017 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3018 | else |
| 3019 | { |
| 3020 | png_uint_16 g, v, w; |
| 3021 | |
| 3022 | g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; |
| 3023 | png_composite_16(v, g, a, background_1->gray); |
| 3024 | w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8]; |
| 3025 | *dp = (png_byte)((w >> 8) & 0xff); |
| 3026 | *(dp + 1) = (png_byte)(w & 0xff); |
| 3027 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3028 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3029 | } |
| 3030 | } |
| 3031 | else |
| 3032 | #endif |
| 3033 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3034 | sp = row; |
| 3035 | dp = row; |
| 3036 | for (i = 0; i < row_width; i++, sp += 4, dp += 2) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3037 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3038 | png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3039 | if (a == (png_uint_16)0xffff) |
| 3040 | { |
| 3041 | png_memcpy(dp, sp, 2); |
| 3042 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3043 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3044 | else if (a == 0) |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3045 | #else |
| 3046 | else |
| 3047 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3048 | { |
| 3049 | *dp = (png_byte)((background->gray >> 8) & 0xff); |
| 3050 | *(dp + 1) = (png_byte)(background->gray & 0xff); |
| 3051 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3052 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3053 | else |
| 3054 | { |
| 3055 | png_uint_16 g, v; |
| 3056 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3057 | g = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3058 | png_composite_16(v, g, a, background_1->gray); |
| 3059 | *dp = (png_byte)((v >> 8) & 0xff); |
| 3060 | *(dp + 1) = (png_byte)(v & 0xff); |
| 3061 | } |
Glenn Randers-Pehrson | 5a0be34 | 2001-10-18 20:55:13 -0500 | [diff] [blame] | 3062 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3063 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3064 | } |
| 3065 | } |
| 3066 | break; |
| 3067 | } |
| 3068 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 3069 | { |
| 3070 | if (row_info->bit_depth == 8) |
| 3071 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3072 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3073 | if (gamma_to_1 != NULL && gamma_from_1 != NULL && |
| 3074 | gamma_table != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3075 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3076 | sp = row; |
| 3077 | dp = row; |
| 3078 | for (i = 0; i < row_width; i++, sp += 4, dp += 3) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3079 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3080 | png_byte a = *(sp + 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3081 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3082 | if (a == 0xff) |
| 3083 | { |
| 3084 | *dp = gamma_table[*sp]; |
| 3085 | *(dp + 1) = gamma_table[*(sp + 1)]; |
| 3086 | *(dp + 2) = gamma_table[*(sp + 2)]; |
| 3087 | } |
| 3088 | else if (a == 0) |
| 3089 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3090 | /* background is already in screen gamma */ |
| 3091 | *dp = (png_byte)background->red; |
| 3092 | *(dp + 1) = (png_byte)background->green; |
| 3093 | *(dp + 2) = (png_byte)background->blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3094 | } |
| 3095 | else |
| 3096 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3097 | png_byte v, w; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3098 | |
| 3099 | v = gamma_to_1[*sp]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3100 | png_composite(w, v, a, background_1->red); |
| 3101 | *dp = gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3102 | v = gamma_to_1[*(sp + 1)]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3103 | png_composite(w, v, a, background_1->green); |
| 3104 | *(dp + 1) = gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3105 | v = gamma_to_1[*(sp + 2)]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3106 | png_composite(w, v, a, background_1->blue); |
| 3107 | *(dp + 2) = gamma_from_1[w]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3108 | } |
| 3109 | } |
| 3110 | } |
| 3111 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3112 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3113 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3114 | sp = row; |
| 3115 | dp = row; |
| 3116 | for (i = 0; i < row_width; i++, sp += 4, dp += 3) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3117 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3118 | png_byte a = *(sp + 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3119 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3120 | if (a == 0xff) |
| 3121 | { |
| 3122 | *dp = *sp; |
| 3123 | *(dp + 1) = *(sp + 1); |
| 3124 | *(dp + 2) = *(sp + 2); |
| 3125 | } |
| 3126 | else if (a == 0) |
| 3127 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3128 | *dp = (png_byte)background->red; |
| 3129 | *(dp + 1) = (png_byte)background->green; |
| 3130 | *(dp + 2) = (png_byte)background->blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3131 | } |
| 3132 | else |
| 3133 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3134 | png_composite(*dp, *sp, a, background->red); |
| 3135 | png_composite(*(dp + 1), *(sp + 1), a, |
| 3136 | background->green); |
| 3137 | png_composite(*(dp + 2), *(sp + 2), a, |
| 3138 | background->blue); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3139 | } |
| 3140 | } |
| 3141 | } |
| 3142 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3143 | else /* if (row_info->bit_depth == 16) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3144 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3145 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3146 | if (gamma_16 != NULL && gamma_16_from_1 != NULL && |
| 3147 | gamma_16_to_1 != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3148 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3149 | sp = row; |
| 3150 | dp = row; |
| 3151 | for (i = 0; i < row_width; i++, sp += 8, dp += 6) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3152 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3153 | png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) |
| 3154 | << 8) + (png_uint_16)(*(sp + 7))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3155 | if (a == (png_uint_16)0xffff) |
| 3156 | { |
| 3157 | png_uint_16 v; |
| 3158 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3159 | v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3160 | *dp = (png_byte)((v >> 8) & 0xff); |
| 3161 | *(dp + 1) = (png_byte)(v & 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3162 | v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3163 | *(dp + 2) = (png_byte)((v >> 8) & 0xff); |
| 3164 | *(dp + 3) = (png_byte)(v & 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3165 | v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3166 | *(dp + 4) = (png_byte)((v >> 8) & 0xff); |
| 3167 | *(dp + 5) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3168 | } |
| 3169 | else if (a == 0) |
| 3170 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3171 | /* background is already in screen gamma */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3172 | *dp = (png_byte)((background->red >> 8) & 0xff); |
| 3173 | *(dp + 1) = (png_byte)(background->red & 0xff); |
| 3174 | *(dp + 2) = (png_byte)((background->green >> 8) & 0xff); |
| 3175 | *(dp + 3) = (png_byte)(background->green & 0xff); |
| 3176 | *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff); |
| 3177 | *(dp + 5) = (png_byte)(background->blue & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3178 | } |
| 3179 | else |
| 3180 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3181 | png_uint_16 v, w, x; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3182 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3183 | v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame] | 3184 | png_composite_16(w, v, a, background_1->red); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3185 | x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; |
| 3186 | *dp = (png_byte)((x >> 8) & 0xff); |
| 3187 | *(dp + 1) = (png_byte)(x & 0xff); |
| 3188 | v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame] | 3189 | png_composite_16(w, v, a, background_1->green); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3190 | x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; |
| 3191 | *(dp + 2) = (png_byte)((x >> 8) & 0xff); |
| 3192 | *(dp + 3) = (png_byte)(x & 0xff); |
| 3193 | v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
Glenn Randers-Pehrson | c6de22d | 2002-02-23 18:55:25 -0600 | [diff] [blame] | 3194 | png_composite_16(w, v, a, background_1->blue); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3195 | x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8]; |
| 3196 | *(dp + 4) = (png_byte)((x >> 8) & 0xff); |
| 3197 | *(dp + 5) = (png_byte)(x & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3198 | } |
| 3199 | } |
| 3200 | } |
| 3201 | else |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3202 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3203 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3204 | sp = row; |
| 3205 | dp = row; |
| 3206 | for (i = 0; i < row_width; i++, sp += 8, dp += 6) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3207 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3208 | png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) |
| 3209 | << 8) + (png_uint_16)(*(sp + 7))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3210 | if (a == (png_uint_16)0xffff) |
| 3211 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3212 | png_memcpy(dp, sp, 6); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3213 | } |
| 3214 | else if (a == 0) |
| 3215 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3216 | *dp = (png_byte)((background->red >> 8) & 0xff); |
| 3217 | *(dp + 1) = (png_byte)(background->red & 0xff); |
| 3218 | *(dp + 2) = (png_byte)((background->green >> 8) & 0xff); |
| 3219 | *(dp + 3) = (png_byte)(background->green & 0xff); |
| 3220 | *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff); |
| 3221 | *(dp + 5) = (png_byte)(background->blue & 0xff); |
| 3222 | } |
| 3223 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3224 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3225 | png_uint_16 v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3226 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3227 | png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
| 3228 | png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) |
| 3229 | + *(sp + 3)); |
| 3230 | png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) |
| 3231 | + *(sp + 5)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3232 | |
| 3233 | png_composite_16(v, r, a, background->red); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3234 | *dp = (png_byte)((v >> 8) & 0xff); |
| 3235 | *(dp + 1) = (png_byte)(v & 0xff); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3236 | png_composite_16(v, g, a, background->green); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3237 | *(dp + 2) = (png_byte)((v >> 8) & 0xff); |
| 3238 | *(dp + 3) = (png_byte)(v & 0xff); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3239 | png_composite_16(v, b, a, background->blue); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3240 | *(dp + 4) = (png_byte)((v >> 8) & 0xff); |
| 3241 | *(dp + 5) = (png_byte)(v & 0xff); |
| 3242 | } |
| 3243 | } |
| 3244 | } |
| 3245 | } |
| 3246 | break; |
| 3247 | } |
| 3248 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3249 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3250 | if (row_info->color_type & PNG_COLOR_MASK_ALPHA) |
| 3251 | { |
| 3252 | row_info->color_type &= ~PNG_COLOR_MASK_ALPHA; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3253 | row_info->channels--; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3254 | row_info->pixel_depth = (png_byte)(row_info->channels * |
| 3255 | row_info->bit_depth); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3256 | row_info->rowbytes = ((row_width * |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3257 | row_info->pixel_depth + 7) >> 3); |
| 3258 | } |
| 3259 | } |
| 3260 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3261 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3262 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3263 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3264 | /* Gamma correct the image, avoiding the alpha channel. Make sure |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 3265 | * 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] | 3266 | * 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] | 3267 | * is 16, use gamma_16_table and gamma_shift. Build these with |
| 3268 | * build_gamma_table(). |
| 3269 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3270 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3271 | png_do_gamma(png_row_infop row_info, png_bytep row, |
| 3272 | png_bytep gamma_table, png_uint_16pp gamma_16_table, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3273 | int gamma_shift) |
| 3274 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3275 | png_bytep sp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3276 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3277 | png_uint_32 row_width=row_info->width; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3278 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3279 | png_debug(1, "in png_do_gamma\n"); |
| 3280 | if ( |
| 3281 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 3282 | row != NULL && row_info != NULL && |
| 3283 | #endif |
| 3284 | ((row_info->bit_depth <= 8 && gamma_table != NULL) || |
| 3285 | (row_info->bit_depth == 16 && gamma_16_table != NULL))) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3286 | { |
| 3287 | switch (row_info->color_type) |
| 3288 | { |
| 3289 | case PNG_COLOR_TYPE_RGB: |
| 3290 | { |
| 3291 | if (row_info->bit_depth == 8) |
| 3292 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3293 | sp = row; |
| 3294 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3295 | { |
| 3296 | *sp = gamma_table[*sp]; |
| 3297 | sp++; |
| 3298 | *sp = gamma_table[*sp]; |
| 3299 | sp++; |
| 3300 | *sp = gamma_table[*sp]; |
| 3301 | sp++; |
| 3302 | } |
| 3303 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3304 | else /* if (row_info->bit_depth == 16) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3305 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3306 | sp = row; |
| 3307 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3308 | { |
| 3309 | png_uint_16 v; |
| 3310 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3311 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3312 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3313 | *(sp + 1) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3314 | sp += 2; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3315 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3316 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3317 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3318 | sp += 2; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3319 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3320 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3321 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3322 | sp += 2; |
| 3323 | } |
| 3324 | } |
| 3325 | break; |
| 3326 | } |
| 3327 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 3328 | { |
| 3329 | if (row_info->bit_depth == 8) |
| 3330 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3331 | sp = row; |
| 3332 | for (i = 0; i < row_width; i++) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3333 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3334 | *sp = gamma_table[*sp]; |
| 3335 | sp++; |
| 3336 | *sp = gamma_table[*sp]; |
| 3337 | sp++; |
| 3338 | *sp = gamma_table[*sp]; |
| 3339 | sp++; |
| 3340 | sp++; |
| 3341 | } |
| 3342 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3343 | else /* if (row_info->bit_depth == 16) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3344 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3345 | sp = row; |
| 3346 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3347 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3348 | png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3349 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3350 | *(sp + 1) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3351 | sp += 2; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3352 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3353 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3354 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3355 | sp += 2; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3356 | v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3357 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3358 | *(sp + 1) = (png_byte)(v & 0xff); |
| 3359 | sp += 4; |
| 3360 | } |
| 3361 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3362 | break; |
| 3363 | } |
| 3364 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 3365 | { |
| 3366 | if (row_info->bit_depth == 8) |
| 3367 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3368 | sp = row; |
| 3369 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3370 | { |
| 3371 | *sp = gamma_table[*sp]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3372 | sp += 2; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3373 | } |
| 3374 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3375 | else /* if (row_info->bit_depth == 16) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3376 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3377 | sp = row; |
| 3378 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3379 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3380 | png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3381 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3382 | *(sp + 1) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3383 | sp += 4; |
| 3384 | } |
| 3385 | } |
| 3386 | break; |
| 3387 | } |
| 3388 | case PNG_COLOR_TYPE_GRAY: |
| 3389 | { |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 3390 | if (row_info->bit_depth == 2) |
| 3391 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3392 | sp = row; |
| 3393 | for (i = 0; i < row_width; i += 4) |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 3394 | { |
| 3395 | int a = *sp & 0xc0; |
| 3396 | int b = *sp & 0x30; |
| 3397 | int c = *sp & 0x0c; |
| 3398 | int d = *sp & 0x03; |
| 3399 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3400 | *sp = (png_byte)( |
| 3401 | ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)| |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 3402 | ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)| |
| 3403 | ((((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] | 3404 | ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) )); |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 3405 | sp++; |
| 3406 | } |
| 3407 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3408 | if (row_info->bit_depth == 4) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3409 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3410 | sp = row; |
| 3411 | for (i = 0; i < row_width; i += 2) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3412 | { |
| 3413 | int msb = *sp & 0xf0; |
| 3414 | int lsb = *sp & 0x0f; |
| 3415 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3416 | *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0) |
| 3417 | | (((int)gamma_table[(lsb << 4) | lsb]) >> 4)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3418 | sp++; |
| 3419 | } |
| 3420 | } |
| 3421 | else if (row_info->bit_depth == 8) |
| 3422 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3423 | sp = row; |
| 3424 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3425 | { |
| 3426 | *sp = gamma_table[*sp]; |
| 3427 | sp++; |
| 3428 | } |
| 3429 | } |
| 3430 | else if (row_info->bit_depth == 16) |
| 3431 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3432 | sp = row; |
| 3433 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3434 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3435 | png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3436 | *sp = (png_byte)((v >> 8) & 0xff); |
| 3437 | *(sp + 1) = (png_byte)(v & 0xff); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3438 | sp += 2; |
| 3439 | } |
| 3440 | } |
| 3441 | break; |
| 3442 | } |
| 3443 | } |
| 3444 | } |
| 3445 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3446 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3447 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3448 | #if defined(PNG_READ_EXPAND_SUPPORTED) |
Glenn Randers-Pehrson | 352ca6b | 1999-09-18 15:49:20 -0500 | [diff] [blame] | 3449 | /* Expands a palette row to an RGB or RGBA row depending |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3450 | * upon whether you supply trans and num_trans. |
| 3451 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3452 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3453 | png_do_expand_palette(png_row_infop row_info, png_bytep row, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3454 | png_colorp palette, png_bytep trans, int num_trans) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3455 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3456 | int shift, value; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3457 | png_bytep sp, dp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3458 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3459 | png_uint_32 row_width=row_info->width; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3460 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3461 | png_debug(1, "in png_do_expand_palette\n"); |
| 3462 | if ( |
| 3463 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 3464 | row != NULL && row_info != NULL && |
| 3465 | #endif |
| 3466 | row_info->color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3467 | { |
| 3468 | if (row_info->bit_depth < 8) |
| 3469 | { |
| 3470 | switch (row_info->bit_depth) |
| 3471 | { |
| 3472 | case 1: |
| 3473 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3474 | sp = row + (png_size_t)((row_width - 1) >> 3); |
| 3475 | dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3476 | shift = 7 - (int)((row_width + 7) & 0x07); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3477 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3478 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3479 | if ((*sp >> shift) & 0x01) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3480 | *dp = 1; |
| 3481 | else |
| 3482 | *dp = 0; |
| 3483 | if (shift == 7) |
| 3484 | { |
| 3485 | shift = 0; |
| 3486 | sp--; |
| 3487 | } |
| 3488 | else |
| 3489 | shift++; |
| 3490 | |
| 3491 | dp--; |
| 3492 | } |
| 3493 | break; |
| 3494 | } |
| 3495 | case 2: |
| 3496 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3497 | sp = row + (png_size_t)((row_width - 1) >> 2); |
| 3498 | dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3499 | shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3500 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3501 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3502 | value = (*sp >> shift) & 0x03; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3503 | *dp = (png_byte)value; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3504 | if (shift == 6) |
| 3505 | { |
| 3506 | shift = 0; |
| 3507 | sp--; |
| 3508 | } |
| 3509 | else |
| 3510 | shift += 2; |
| 3511 | |
| 3512 | dp--; |
| 3513 | } |
| 3514 | break; |
| 3515 | } |
| 3516 | case 4: |
| 3517 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3518 | sp = row + (png_size_t)((row_width - 1) >> 1); |
| 3519 | dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3520 | shift = (int)((row_width & 0x01) << 2); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3521 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3522 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3523 | value = (*sp >> shift) & 0x0f; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3524 | *dp = (png_byte)value; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3525 | if (shift == 4) |
| 3526 | { |
| 3527 | shift = 0; |
| 3528 | sp--; |
| 3529 | } |
| 3530 | else |
| 3531 | shift += 4; |
| 3532 | |
| 3533 | dp--; |
| 3534 | } |
| 3535 | break; |
| 3536 | } |
| 3537 | } |
| 3538 | row_info->bit_depth = 8; |
| 3539 | row_info->pixel_depth = 8; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3540 | row_info->rowbytes = row_width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3541 | } |
| 3542 | switch (row_info->bit_depth) |
| 3543 | { |
| 3544 | case 8: |
| 3545 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3546 | if (trans != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3547 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3548 | sp = row + (png_size_t)row_width - 1; |
| 3549 | dp = row + (png_size_t)(row_width << 2) - 1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3550 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3551 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3552 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3553 | if ((int)(*sp) >= num_trans) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3554 | *dp-- = 0xff; |
| 3555 | else |
| 3556 | *dp-- = trans[*sp]; |
| 3557 | *dp-- = palette[*sp].blue; |
| 3558 | *dp-- = palette[*sp].green; |
| 3559 | *dp-- = palette[*sp].red; |
| 3560 | sp--; |
| 3561 | } |
| 3562 | row_info->bit_depth = 8; |
| 3563 | row_info->pixel_depth = 32; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3564 | row_info->rowbytes = row_width * 4; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3565 | row_info->color_type = 6; |
| 3566 | row_info->channels = 4; |
| 3567 | } |
| 3568 | else |
| 3569 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3570 | sp = row + (png_size_t)row_width - 1; |
| 3571 | dp = row + (png_size_t)(row_width * 3) - 1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3572 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3573 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3574 | { |
| 3575 | *dp-- = palette[*sp].blue; |
| 3576 | *dp-- = palette[*sp].green; |
| 3577 | *dp-- = palette[*sp].red; |
| 3578 | sp--; |
| 3579 | } |
| 3580 | row_info->bit_depth = 8; |
| 3581 | row_info->pixel_depth = 24; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3582 | row_info->rowbytes = row_width * 3; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3583 | row_info->color_type = 2; |
| 3584 | row_info->channels = 3; |
| 3585 | } |
| 3586 | break; |
| 3587 | } |
| 3588 | } |
| 3589 | } |
| 3590 | } |
| 3591 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3592 | /* If the bit depth < 8, it is expanded to 8. Also, if the |
| 3593 | * transparency value is supplied, an alpha channel is built. |
| 3594 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3595 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3596 | png_do_expand(png_row_infop row_info, png_bytep row, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3597 | png_color_16p trans_value) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3598 | { |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3599 | int shift, value; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3600 | png_bytep sp, dp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3601 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3602 | png_uint_32 row_width=row_info->width; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3603 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3604 | png_debug(1, "in png_do_expand\n"); |
| 3605 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 3606 | if (row != NULL && row_info != NULL) |
| 3607 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3608 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3609 | if (row_info->color_type == PNG_COLOR_TYPE_GRAY) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3610 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3611 | 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] | 3612 | |
| 3613 | if (row_info->bit_depth < 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3614 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3615 | switch (row_info->bit_depth) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3616 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3617 | case 1: |
| 3618 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3619 | gray = (png_uint_16)(gray*0xff); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3620 | sp = row + (png_size_t)((row_width - 1) >> 3); |
| 3621 | dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3622 | shift = 7 - (int)((row_width + 7) & 0x07); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3623 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3624 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3625 | if ((*sp >> shift) & 0x01) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3626 | *dp = 0xff; |
| 3627 | else |
| 3628 | *dp = 0; |
| 3629 | if (shift == 7) |
| 3630 | { |
| 3631 | shift = 0; |
| 3632 | sp--; |
| 3633 | } |
| 3634 | else |
| 3635 | shift++; |
| 3636 | |
| 3637 | dp--; |
| 3638 | } |
| 3639 | break; |
| 3640 | } |
| 3641 | case 2: |
| 3642 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3643 | gray = (png_uint_16)(gray*0x55); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3644 | sp = row + (png_size_t)((row_width - 1) >> 2); |
| 3645 | dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3646 | shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3647 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3648 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3649 | value = (*sp >> shift) & 0x03; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3650 | *dp = (png_byte)(value | (value << 2) | (value << 4) | |
| 3651 | (value << 6)); |
| 3652 | if (shift == 6) |
| 3653 | { |
| 3654 | shift = 0; |
| 3655 | sp--; |
| 3656 | } |
| 3657 | else |
| 3658 | shift += 2; |
| 3659 | |
| 3660 | dp--; |
| 3661 | } |
| 3662 | break; |
| 3663 | } |
| 3664 | case 4: |
| 3665 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 3666 | gray = (png_uint_16)(gray*0x11); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3667 | sp = row + (png_size_t)((row_width - 1) >> 1); |
| 3668 | dp = row + (png_size_t)row_width - 1; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3669 | shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3670 | for (i = 0; i < row_width; i++) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3671 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3672 | value = (*sp >> shift) & 0x0f; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3673 | *dp = (png_byte)(value | (value << 4)); |
| 3674 | if (shift == 4) |
| 3675 | { |
| 3676 | shift = 0; |
| 3677 | sp--; |
| 3678 | } |
| 3679 | else |
| 3680 | shift = 4; |
| 3681 | |
| 3682 | dp--; |
| 3683 | } |
| 3684 | break; |
| 3685 | } |
| 3686 | } |
| 3687 | row_info->bit_depth = 8; |
| 3688 | row_info->pixel_depth = 8; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3689 | row_info->rowbytes = row_width; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3690 | } |
| 3691 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3692 | if (trans_value != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3693 | { |
| 3694 | if (row_info->bit_depth == 8) |
| 3695 | { |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3696 | sp = row + (png_size_t)row_width - 1; |
| 3697 | dp = row + (png_size_t)(row_width << 1) - 1; |
| 3698 | for (i = 0; i < row_width; i++) |
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 | if (*sp == gray) |
| 3701 | *dp-- = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3702 | else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3703 | *dp-- = 0xff; |
| 3704 | *dp-- = *sp--; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3705 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3706 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3707 | else if (row_info->bit_depth == 16) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3708 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3709 | sp = row + row_info->rowbytes - 1; |
| 3710 | dp = row + (row_info->rowbytes << 1) - 1; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3711 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3712 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3713 | if (((png_uint_16)*(sp) | |
| 3714 | ((png_uint_16)*(sp - 1) << 8)) == gray) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3715 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3716 | *dp-- = 0; |
| 3717 | *dp-- = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3718 | } |
| 3719 | else |
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 | *dp-- = 0xff; |
| 3722 | *dp-- = 0xff; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3723 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3724 | *dp-- = *sp--; |
| 3725 | *dp-- = *sp--; |
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 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3728 | row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; |
| 3729 | row_info->channels = 2; |
| 3730 | row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); |
| 3731 | row_info->rowbytes = |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3732 | ((row_width * row_info->pixel_depth) >> 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3733 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3734 | } |
| 3735 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value) |
| 3736 | { |
| 3737 | if (row_info->bit_depth == 8) |
| 3738 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3739 | sp = row + (png_size_t)row_info->rowbytes - 1; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3740 | dp = row + (png_size_t)(row_width << 2) - 1; |
| 3741 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3742 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3743 | if (*(sp - 2) == trans_value->red && |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3744 | *(sp - 1) == trans_value->green && |
| 3745 | *(sp - 0) == trans_value->blue) |
| 3746 | *dp-- = 0; |
| 3747 | else |
| 3748 | *dp-- = 0xff; |
| 3749 | *dp-- = *sp--; |
| 3750 | *dp-- = *sp--; |
| 3751 | *dp-- = *sp--; |
| 3752 | } |
| 3753 | } |
| 3754 | else if (row_info->bit_depth == 16) |
| 3755 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3756 | sp = row + row_info->rowbytes - 1; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3757 | dp = row + (png_size_t)(row_width << 3) - 1; |
| 3758 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3759 | { |
| 3760 | if ((((png_uint_16)*(sp - 4) | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3761 | ((png_uint_16)*(sp - 5) << 8)) == trans_value->red) && |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3762 | (((png_uint_16)*(sp - 2) | |
| 3763 | ((png_uint_16)*(sp - 3) << 8)) == trans_value->green) && |
| 3764 | (((png_uint_16)*(sp - 0) | |
| 3765 | ((png_uint_16)*(sp - 1) << 8)) == trans_value->blue)) |
| 3766 | { |
| 3767 | *dp-- = 0; |
| 3768 | *dp-- = 0; |
| 3769 | } |
| 3770 | else |
| 3771 | { |
| 3772 | *dp-- = 0xff; |
| 3773 | *dp-- = 0xff; |
| 3774 | } |
| 3775 | *dp-- = *sp--; |
| 3776 | *dp-- = *sp--; |
| 3777 | *dp-- = *sp--; |
| 3778 | *dp-- = *sp--; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3779 | *dp-- = *sp--; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3780 | *dp-- = *sp--; |
| 3781 | } |
| 3782 | } |
| 3783 | row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
| 3784 | row_info->channels = 4; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3785 | row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3786 | row_info->rowbytes = |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3787 | ((row_width * row_info->pixel_depth) >> 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3788 | } |
| 3789 | } |
| 3790 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3791 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3792 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3793 | #if defined(PNG_READ_DITHER_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3794 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3795 | png_do_dither(png_row_infop row_info, png_bytep row, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3796 | png_bytep palette_lookup, png_bytep dither_lookup) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3797 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3798 | png_bytep sp, dp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3799 | png_uint_32 i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3800 | png_uint_32 row_width=row_info->width; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3801 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3802 | png_debug(1, "in png_do_dither\n"); |
| 3803 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 3804 | if (row != NULL && row_info != NULL) |
| 3805 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3806 | { |
| 3807 | if (row_info->color_type == PNG_COLOR_TYPE_RGB && |
| 3808 | palette_lookup && row_info->bit_depth == 8) |
| 3809 | { |
| 3810 | int r, g, b, p; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3811 | sp = row; |
| 3812 | dp = row; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3813 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3814 | { |
| 3815 | r = *sp++; |
| 3816 | g = *sp++; |
| 3817 | b = *sp++; |
| 3818 | |
| 3819 | /* this looks real messy, but the compiler will reduce |
| 3820 | it down to a reasonable formula. For example, with |
| 3821 | 5 bits per color, we get: |
| 3822 | p = (((r >> 3) & 0x1f) << 10) | |
| 3823 | (((g >> 3) & 0x1f) << 5) | |
| 3824 | ((b >> 3) & 0x1f); |
| 3825 | */ |
| 3826 | p = (((r >> (8 - PNG_DITHER_RED_BITS)) & |
| 3827 | ((1 << PNG_DITHER_RED_BITS) - 1)) << |
| 3828 | (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) | |
| 3829 | (((g >> (8 - PNG_DITHER_GREEN_BITS)) & |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3830 | ((1 << PNG_DITHER_GREEN_BITS) - 1)) << |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3831 | (PNG_DITHER_BLUE_BITS)) | |
| 3832 | ((b >> (8 - PNG_DITHER_BLUE_BITS)) & |
| 3833 | ((1 << PNG_DITHER_BLUE_BITS) - 1)); |
| 3834 | |
| 3835 | *dp++ = palette_lookup[p]; |
| 3836 | } |
| 3837 | row_info->color_type = PNG_COLOR_TYPE_PALETTE; |
| 3838 | row_info->channels = 1; |
| 3839 | row_info->pixel_depth = row_info->bit_depth; |
| 3840 | row_info->rowbytes = |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3841 | ((row_width * row_info->pixel_depth + 7) >> 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3842 | } |
| 3843 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3844 | palette_lookup != NULL && row_info->bit_depth == 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3845 | { |
| 3846 | int r, g, b, p; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3847 | sp = row; |
| 3848 | dp = row; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3849 | for (i = 0; i < row_width; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3850 | { |
| 3851 | r = *sp++; |
| 3852 | g = *sp++; |
| 3853 | b = *sp++; |
| 3854 | sp++; |
| 3855 | |
| 3856 | p = (((r >> (8 - PNG_DITHER_RED_BITS)) & |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3857 | ((1 << PNG_DITHER_RED_BITS) - 1)) << |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3858 | (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) | |
| 3859 | (((g >> (8 - PNG_DITHER_GREEN_BITS)) & |
| 3860 | ((1 << PNG_DITHER_GREEN_BITS) - 1)) << |
| 3861 | (PNG_DITHER_BLUE_BITS)) | |
| 3862 | ((b >> (8 - PNG_DITHER_BLUE_BITS)) & |
| 3863 | ((1 << PNG_DITHER_BLUE_BITS) - 1)); |
| 3864 | |
| 3865 | *dp++ = palette_lookup[p]; |
| 3866 | } |
| 3867 | row_info->color_type = PNG_COLOR_TYPE_PALETTE; |
| 3868 | row_info->channels = 1; |
| 3869 | row_info->pixel_depth = row_info->bit_depth; |
| 3870 | row_info->rowbytes = |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3871 | ((row_width * row_info->pixel_depth + 7) >> 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3872 | } |
| 3873 | else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && |
| 3874 | dither_lookup && row_info->bit_depth == 8) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3875 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3876 | sp = row; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 3877 | for (i = 0; i < row_width; i++, sp++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3878 | { |
| 3879 | *sp = dither_lookup[*sp]; |
| 3880 | } |
| 3881 | } |
| 3882 | } |
| 3883 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3884 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3885 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 3886 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3887 | #if defined(PNG_READ_GAMMA_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3888 | static int png_gamma_shift[] = |
| 3889 | {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0}; |
| 3890 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3891 | /* 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] | 3892 | * tables, we don't make a full table if we are reducing to 8-bit in |
| 3893 | * the future. Note also how the gamma_16 tables are segmented so that |
| 3894 | * we don't need to allocate > 64K chunks for a full 16-bit table. |
| 3895 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3896 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 3897 | png_build_gamma_table(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3898 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3899 | png_debug(1, "in png_build_gamma_table\n"); |
| 3900 | if(png_ptr->gamma != 0.0) |
| 3901 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3902 | if (png_ptr->bit_depth <= 8) |
| 3903 | { |
| 3904 | int i; |
| 3905 | double g; |
| 3906 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3907 | if (png_ptr->screen_gamma > .000001) |
| 3908 | g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); |
| 3909 | else |
| 3910 | g = 1.0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3911 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3912 | png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3913 | (png_uint_32)256); |
| 3914 | |
| 3915 | for (i = 0; i < 256; i++) |
| 3916 | { |
| 3917 | png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0, |
| 3918 | g) * 255.0 + .5); |
| 3919 | } |
| 3920 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3921 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ |
| 3922 | defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 3923 | if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3924 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3925 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3926 | g = 1.0 / (png_ptr->gamma); |
| 3927 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3928 | png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3929 | (png_uint_32)256); |
| 3930 | |
| 3931 | for (i = 0; i < 256; i++) |
| 3932 | { |
| 3933 | png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0, |
| 3934 | g) * 255.0 + .5); |
| 3935 | } |
| 3936 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 3937 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3938 | png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr, |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3939 | (png_uint_32)256); |
| 3940 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3941 | if(png_ptr->screen_gamma > 0.000001) |
| 3942 | g = 1.0 / png_ptr->screen_gamma; |
| 3943 | else |
| 3944 | g = png_ptr->gamma; /* probably doing rgb_to_gray */ |
| 3945 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3946 | for (i = 0; i < 256; i++) |
| 3947 | { |
| 3948 | png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0, |
| 3949 | g) * 255.0 + .5); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3950 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3951 | } |
| 3952 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3953 | #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3954 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3955 | else |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3956 | { |
| 3957 | double g; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3958 | int i, j, shift, num; |
| 3959 | int sig_bit; |
| 3960 | png_uint_32 ig; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3961 | |
| 3962 | if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3963 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3964 | sig_bit = (int)png_ptr->sig_bit.red; |
| 3965 | if ((int)png_ptr->sig_bit.green > sig_bit) |
| 3966 | sig_bit = png_ptr->sig_bit.green; |
| 3967 | if ((int)png_ptr->sig_bit.blue > sig_bit) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3968 | sig_bit = png_ptr->sig_bit.blue; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3969 | } |
| 3970 | else |
| 3971 | { |
| 3972 | sig_bit = (int)png_ptr->sig_bit.gray; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3973 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3974 | |
| 3975 | if (sig_bit > 0) |
| 3976 | shift = 16 - sig_bit; |
| 3977 | else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3978 | shift = 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3979 | |
| 3980 | if (png_ptr->transformations & PNG_16_TO_8) |
| 3981 | { |
| 3982 | if (shift < (16 - PNG_MAX_GAMMA_8)) |
| 3983 | shift = (16 - PNG_MAX_GAMMA_8); |
| 3984 | } |
| 3985 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3986 | if (shift > 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3987 | shift = 8; |
| 3988 | if (shift < 0) |
| 3989 | shift = 0; |
| 3990 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3991 | png_ptr->gamma_shift = (png_byte)shift; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3992 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 3993 | num = (1 << (8 - shift)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3994 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 3995 | if (png_ptr->screen_gamma > .000001) |
| 3996 | g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); |
| 3997 | else |
| 3998 | g = 1.0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3999 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4000 | 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] | 4001 | (png_uint_32)(num * sizeof (png_uint_16p))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4002 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4003 | if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 4004 | { |
| 4005 | double fin, fout; |
| 4006 | png_uint_32 last, max; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4007 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 4008 | for (i = 0; i < num; i++) |
| 4009 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4010 | 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] | 4011 | (png_uint_32)(256 * sizeof (png_uint_16))); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 4012 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4013 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 4014 | g = 1.0 / g; |
| 4015 | last = 0; |
| 4016 | for (i = 0; i < 256; i++) |
| 4017 | { |
| 4018 | fout = ((double)i + 0.5) / 256.0; |
| 4019 | fin = pow(fout, g); |
| 4020 | max = (png_uint_32)(fin * (double)((png_uint_32)num << 8)); |
| 4021 | while (last <= max) |
| 4022 | { |
| 4023 | png_ptr->gamma_16_table[(int)(last & (0xff >> shift))] |
| 4024 | [(int)(last >> (8 - shift))] = (png_uint_16)( |
| 4025 | (png_uint_16)i | ((png_uint_16)i << 8)); |
| 4026 | last++; |
| 4027 | } |
| 4028 | } |
| 4029 | while (last < ((png_uint_32)num << 8)) |
| 4030 | { |
| 4031 | png_ptr->gamma_16_table[(int)(last & (0xff >> shift))] |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 4032 | [(int)(last >> (8 - shift))] = (png_uint_16)65535L; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 4033 | last++; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 4034 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4035 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 4036 | else |
| 4037 | { |
| 4038 | for (i = 0; i < num; i++) |
| 4039 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4040 | 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] | 4041 | (png_uint_32)(256 * sizeof (png_uint_16))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4042 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 4043 | 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] | 4044 | for (j = 0; j < 256; j++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4045 | { |
| 4046 | png_ptr->gamma_16_table[i][j] = |
| 4047 | (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / |
| 4048 | 65535.0, g) * 65535.0 + .5); |
| 4049 | } |
| 4050 | } |
| 4051 | } |
| 4052 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4053 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ |
| 4054 | defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
| 4055 | if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4056 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4057 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4058 | g = 1.0 / (png_ptr->gamma); |
| 4059 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4060 | 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] | 4061 | (png_uint_32)(num * sizeof (png_uint_16p ))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4062 | |
| 4063 | for (i = 0; i < num; i++) |
| 4064 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4065 | 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] | 4066 | (png_uint_32)(256 * sizeof (png_uint_16))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4067 | |
| 4068 | ig = (((png_uint_32)i * |
| 4069 | (png_uint_32)png_gamma_shift[shift]) >> 4); |
| 4070 | for (j = 0; j < 256; j++) |
| 4071 | { |
| 4072 | png_ptr->gamma_16_to_1[i][j] = |
| 4073 | (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / |
| 4074 | 65535.0, g) * 65535.0 + .5); |
| 4075 | } |
| 4076 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4077 | |
| 4078 | if(png_ptr->screen_gamma > 0.000001) |
| 4079 | g = 1.0 / png_ptr->screen_gamma; |
| 4080 | else |
| 4081 | g = png_ptr->gamma; /* probably doing rgb_to_gray */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4082 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4083 | 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] | 4084 | (png_uint_32)(num * sizeof (png_uint_16p))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4085 | |
| 4086 | for (i = 0; i < num; i++) |
| 4087 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 4088 | 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] | 4089 | (png_uint_32)(256 * sizeof (png_uint_16))); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4090 | |
| 4091 | ig = (((png_uint_32)i * |
| 4092 | (png_uint_32)png_gamma_shift[shift]) >> 4); |
| 4093 | for (j = 0; j < 256; j++) |
| 4094 | { |
| 4095 | png_ptr->gamma_16_from_1[i][j] = |
| 4096 | (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / |
| 4097 | 65535.0, g) * 65535.0 + .5); |
| 4098 | } |
| 4099 | } |
| 4100 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4101 | #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4102 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 4103 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4104 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4105 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 4106 | /* To do: install integer version of png_build_gamma_table here */ |
| 4107 | #endif |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 4108 | |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 4109 | #if defined(PNG_MNG_FEATURES_SUPPORTED) |
| 4110 | /* undoes intrapixel differencing */ |
| 4111 | void /* PRIVATE */ |
| 4112 | png_do_read_intrapixel(png_row_infop row_info, png_bytep row) |
| 4113 | { |
| 4114 | png_debug(1, "in png_do_read_intrapixel\n"); |
| 4115 | if ( |
| 4116 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 4117 | row != NULL && row_info != NULL && |
| 4118 | #endif |
| 4119 | (row_info->color_type & PNG_COLOR_MASK_COLOR)) |
| 4120 | { |
| 4121 | int bytes_per_pixel; |
| 4122 | png_uint_32 row_width = row_info->width; |
| 4123 | if (row_info->bit_depth == 8) |
| 4124 | { |
| 4125 | png_bytep rp; |
| 4126 | png_uint_32 i; |
| 4127 | |
| 4128 | if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 4129 | bytes_per_pixel = 3; |
| 4130 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 4131 | bytes_per_pixel = 4; |
| 4132 | else |
| 4133 | return; |
| 4134 | |
| 4135 | for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) |
| 4136 | { |
| 4137 | *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff); |
| 4138 | *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff); |
| 4139 | } |
| 4140 | } |
| 4141 | else if (row_info->bit_depth == 16) |
| 4142 | { |
| 4143 | png_bytep rp; |
| 4144 | png_uint_32 i; |
| 4145 | |
| 4146 | if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 4147 | bytes_per_pixel = 6; |
| 4148 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 4149 | bytes_per_pixel = 8; |
| 4150 | else |
| 4151 | return; |
| 4152 | |
| 4153 | for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) |
| 4154 | { |
| 4155 | png_uint_32 s0=*(rp )<<8 | *(rp+1); |
| 4156 | png_uint_32 s1=*(rp+2)<<8 | *(rp+3); |
| 4157 | png_uint_32 s2=*(rp+4)<<8 | *(rp+5); |
| 4158 | png_uint_32 red=(65536+s0+s1)&0xffff; |
| 4159 | png_uint_32 blue=(65536+s2+s1)&0xffff; |
| 4160 | *(rp ) = (png_byte)((red>>8)&0xff); |
| 4161 | *(rp+1) = (png_byte)(red&0xff); |
| 4162 | *(rp+4) = (png_byte)((blue>>8)&0xff); |
| 4163 | *(rp+5) = (png_byte)(blue&0xff); |
| 4164 | } |
| 4165 | } |
| 4166 | } |
| 4167 | } |
| 4168 | #endif /* PNG_MNG_FEATURES_SUPPORTED */ |