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