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