Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: third_party/libpng/pngrtran.c

Issue 140074: Updates the PNG library to 1.2.37, enables some functionality for O3D (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/libpng/pngrio.c ('k') | third_party/libpng/pngrutil.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* pngrtran.c - transforms the data in a row for PNG readers 2 /* pngrtran.c - transforms the data in a row for PNG readers
3 * 3 *
4 * Last changed in libpng 1.2.35 [February 14, 2009] 4 * Last changed in libpng 1.2.37 [June 4, 2009]
5 * For conditions of distribution and use, see copyright notice in png.h 5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2009 Glenn Randers-Pehrson 6 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * 9 *
10 * This file contains functions optionally called by an application 10 * This file contains functions optionally called by an application
11 * in order to tell libpng how to handle data when reading a PNG. 11 * in order to tell libpng how to handle data when reading a PNG.
12 * Transformations that are used in both reading and writing are 12 * Transformations that are used in both reading and writing are
13 * in pngtrans.c. 13 * in pngtrans.c.
14 */ 14 */
15 15
16 #define PNG_INTERNAL 16 #define PNG_INTERNAL
17 #include "png.h" 17 #include "png.h"
18 #if defined(PNG_READ_SUPPORTED) 18 #if defined(PNG_READ_SUPPORTED)
19 19
20 /* Set the action on getting a CRC error for an ancillary or critical chunk. */ 20 /* Set the action on getting a CRC error for an ancillary or critical chunk. */
21 void PNGAPI 21 void PNGAPI
22 png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action) 22 png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
23 { 23 {
24 png_debug(1, "in png_set_crc_action"); 24 png_debug(1, "in png_set_crc_action");
25 /* Tell libpng how we react to CRC errors in critical chunks */ 25 /* Tell libpng how we react to CRC errors in critical chunks */
26 if (png_ptr == NULL) return; 26 if (png_ptr == NULL)
27 return;
27 switch (crit_action) 28 switch (crit_action)
28 { 29 {
29 case PNG_CRC_NO_CHANGE: /* leave setting as is */ 30 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
30 break; 31 break;
31 case PNG_CRC_WARN_USE: /* warn/use data */ 32
33 case PNG_CRC_WARN_USE: /* Warn/use data */
32 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; 34 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
33 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE; 35 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
34 break; 36 break;
35 case PNG_CRC_QUIET_USE: /* quiet/use data */ 37
38 case PNG_CRC_QUIET_USE: /* Quiet/use data */
36 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; 39 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
37 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE | 40 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
38 PNG_FLAG_CRC_CRITICAL_IGNORE; 41 PNG_FLAG_CRC_CRITICAL_IGNORE;
39 break; 42 break;
40 case PNG_CRC_WARN_DISCARD: /* not a valid action for critical data */ 43
44 case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
41 png_warning(png_ptr, 45 png_warning(png_ptr,
42 "Can't discard critical data on CRC error."); 46 "Can't discard critical data on CRC error.");
43 case PNG_CRC_ERROR_QUIT: /* error/quit */ 47 case PNG_CRC_ERROR_QUIT: /* Error/quit */
48
44 case PNG_CRC_DEFAULT: 49 case PNG_CRC_DEFAULT:
45 default: 50 default:
46 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; 51 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
47 break; 52 break;
48 } 53 }
49 54
50 switch (ancil_action) 55 switch (ancil_action)
51 { 56 {
52 case PNG_CRC_NO_CHANGE: /* leave setting as is */ 57 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
53 break; 58 break;
54 case PNG_CRC_WARN_USE: /* warn/use data */ 59
60 case PNG_CRC_WARN_USE: /* Warn/use data */
55 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; 61 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
56 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE; 62 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
57 break; 63 break;
58 case PNG_CRC_QUIET_USE: /* quiet/use data */ 64
65 case PNG_CRC_QUIET_USE: /* Quiet/use data */
59 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; 66 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
60 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE | 67 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
61 PNG_FLAG_CRC_ANCILLARY_NOWARN; 68 PNG_FLAG_CRC_ANCILLARY_NOWARN;
62 break; 69 break;
63 case PNG_CRC_ERROR_QUIT: /* error/quit */ 70
71 case PNG_CRC_ERROR_QUIT: /* Error/quit */
64 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; 72 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
65 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN; 73 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
66 break; 74 break;
67 case PNG_CRC_WARN_DISCARD: /* warn/discard data */ 75
76 case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
77
68 case PNG_CRC_DEFAULT: 78 case PNG_CRC_DEFAULT:
69 default: 79 default:
70 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; 80 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
71 break; 81 break;
72 } 82 }
73 } 83 }
74 84
75 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ 85 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
76 defined(PNG_FLOATING_POINT_SUPPORTED) 86 defined(PNG_FLOATING_POINT_SUPPORTED)
77 /* handle alpha and tRNS via a background color */ 87 /* Handle alpha and tRNS via a background color */
78 void PNGAPI 88 void PNGAPI
79 png_set_background(png_structp png_ptr, 89 png_set_background(png_structp png_ptr,
80 png_color_16p background_color, int background_gamma_code, 90 png_color_16p background_color, int background_gamma_code,
81 int need_expand, double background_gamma) 91 int need_expand, double background_gamma)
82 { 92 {
83 png_debug(1, "in png_set_background"); 93 png_debug(1, "in png_set_background");
84 if (png_ptr == NULL) return; 94 if (png_ptr == NULL)
95 return;
85 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) 96 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
86 { 97 {
87 png_warning(png_ptr, "Application must supply a known background gamma"); 98 png_warning(png_ptr, "Application must supply a known background gamma");
88 return; 99 return;
89 } 100 }
90 101
91 png_ptr->transformations |= PNG_BACKGROUND; 102 png_ptr->transformations |= PNG_BACKGROUND;
92 png_memcpy(&(png_ptr->background), background_color, 103 png_memcpy(&(png_ptr->background), background_color,
93 png_sizeof(png_color_16)); 104 png_sizeof(png_color_16));
94 png_ptr->background_gamma = (float)background_gamma; 105 png_ptr->background_gamma = (float)background_gamma;
95 png_ptr->background_gamma_type = (png_byte)(background_gamma_code); 106 png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
96 png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0); 107 png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
97 } 108 }
98 #endif 109 #endif
99 110
100 #if defined(PNG_READ_16_TO_8_SUPPORTED) 111 #if defined(PNG_READ_16_TO_8_SUPPORTED)
101 /* strip 16 bit depth files to 8 bit depth */ 112 /* Strip 16 bit depth files to 8 bit depth */
102 void PNGAPI 113 void PNGAPI
103 png_set_strip_16(png_structp png_ptr) 114 png_set_strip_16(png_structp png_ptr)
104 { 115 {
105 png_debug(1, "in png_set_strip_16"); 116 png_debug(1, "in png_set_strip_16");
106 if (png_ptr == NULL) return; 117 if (png_ptr == NULL)
118 return;
107 png_ptr->transformations |= PNG_16_TO_8; 119 png_ptr->transformations |= PNG_16_TO_8;
108 } 120 }
109 #endif 121 #endif
110 122
111 #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) 123 #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
112 void PNGAPI 124 void PNGAPI
113 png_set_strip_alpha(png_structp png_ptr) 125 png_set_strip_alpha(png_structp png_ptr)
114 { 126 {
115 png_debug(1, "in png_set_strip_alpha"); 127 png_debug(1, "in png_set_strip_alpha");
116 if (png_ptr == NULL) return; 128 if (png_ptr == NULL)
129 return;
117 png_ptr->flags |= PNG_FLAG_STRIP_ALPHA; 130 png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
118 } 131 }
119 #endif 132 #endif
120 133
121 #if defined(PNG_READ_DITHER_SUPPORTED) 134 #if defined(PNG_READ_DITHER_SUPPORTED)
122 /* Dither file to 8 bit. Supply a palette, the current number 135 /* Dither file to 8 bit. Supply a palette, the current number
123 * of elements in the palette, the maximum number of elements 136 * of elements in the palette, the maximum number of elements
124 * allowed, and a histogram if possible. If the current number 137 * allowed, and a histogram if possible. If the current number
125 * of colors is greater then the maximum number, the palette will be 138 * of colors is greater then the maximum number, the palette will be
126 * modified to fit in the maximum number. "full_dither" indicates 139 * modified to fit in the maximum number. "full_dither" indicates
127 * whether we need a dithering cube set up for RGB images, or if we 140 * whether we need a dithering cube set up for RGB images, or if we
128 * simply are reducing the number of colors in a paletted image. 141 * simply are reducing the number of colors in a paletted image.
129 */ 142 */
130 143
131 typedef struct png_dsort_struct 144 typedef struct png_dsort_struct
132 { 145 {
133 struct png_dsort_struct FAR * next; 146 struct png_dsort_struct FAR * next;
134 png_byte left; 147 png_byte left;
135 png_byte right; 148 png_byte right;
136 } png_dsort; 149 } png_dsort;
137 typedef png_dsort FAR * png_dsortp; 150 typedef png_dsort FAR * png_dsortp;
138 typedef png_dsort FAR * FAR * png_dsortpp; 151 typedef png_dsort FAR * FAR * png_dsortpp;
139 152
140 void PNGAPI 153 void PNGAPI
141 png_set_dither(png_structp png_ptr, png_colorp palette, 154 png_set_dither(png_structp png_ptr, png_colorp palette,
142 int num_palette, int maximum_colors, png_uint_16p histogram, 155 int num_palette, int maximum_colors, png_uint_16p histogram,
143 int full_dither) 156 int full_dither)
144 { 157 {
145 png_debug(1, "in png_set_dither"); 158 png_debug(1, "in png_set_dither");
146 if (png_ptr == NULL) return; 159 if (png_ptr == NULL)
160 return;
147 png_ptr->transformations |= PNG_DITHER; 161 png_ptr->transformations |= PNG_DITHER;
148 162
149 if (!full_dither) 163 if (!full_dither)
150 { 164 {
151 int i; 165 int i;
152 166
153 png_ptr->dither_index = (png_bytep)png_malloc(png_ptr, 167 png_ptr->dither_index = (png_bytep)png_malloc(png_ptr,
154 (png_uint_32)(num_palette * png_sizeof(png_byte))); 168 (png_uint_32)(num_palette * png_sizeof(png_byte)));
155 for (i = 0; i < num_palette; i++) 169 for (i = 0; i < num_palette; i++)
156 png_ptr->dither_index[i] = (png_byte)i; 170 png_ptr->dither_index[i] = (png_byte)i;
157 } 171 }
158 172
159 if (num_palette > maximum_colors) 173 if (num_palette > maximum_colors)
160 { 174 {
161 if (histogram != NULL) 175 if (histogram != NULL)
162 { 176 {
163 /* This is easy enough, just throw out the least used colors. 177 /* This is easy enough, just throw out the least used colors.
164 Perhaps not the best solution, but good enough. */ 178 * Perhaps not the best solution, but good enough.
179 */
165 180
166 int i; 181 int i;
167 182
168 /* initialize an array to sort colors */ 183 /* Initialize an array to sort colors */
169 png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr, 184 png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr,
170 (png_uint_32)(num_palette * png_sizeof(png_byte))); 185 (png_uint_32)(num_palette * png_sizeof(png_byte)));
171 186
172 /* initialize the dither_sort array */ 187 /* Initialize the dither_sort array */
173 for (i = 0; i < num_palette; i++) 188 for (i = 0; i < num_palette; i++)
174 png_ptr->dither_sort[i] = (png_byte)i; 189 png_ptr->dither_sort[i] = (png_byte)i;
175 190
176 /* Find the least used palette entries by starting a 191 /* Find the least used palette entries by starting a
177 bubble sort, and running it until we have sorted 192 * bubble sort, and running it until we have sorted
178 out enough colors. Note that we don't care about 193 * out enough colors. Note that we don't care about
179 sorting all the colors, just finding which are 194 * sorting all the colors, just finding which are
180 least used. */ 195 * least used.
196 */
181 197
182 for (i = num_palette - 1; i >= maximum_colors; i--) 198 for (i = num_palette - 1; i >= maximum_colors; i--)
183 { 199 {
184 int done; /* to stop early if the list is pre-sorted */ 200 int done; /* To stop early if the list is pre-sorted */
185 int j; 201 int j;
186 202
187 done = 1; 203 done = 1;
188 for (j = 0; j < i; j++) 204 for (j = 0; j < i; j++)
189 { 205 {
190 if (histogram[png_ptr->dither_sort[j]] 206 if (histogram[png_ptr->dither_sort[j]]
191 < histogram[png_ptr->dither_sort[j + 1]]) 207 < histogram[png_ptr->dither_sort[j + 1]])
192 { 208 {
193 png_byte t; 209 png_byte t;
194 210
195 t = png_ptr->dither_sort[j]; 211 t = png_ptr->dither_sort[j];
196 png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1]; 212 png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1];
197 png_ptr->dither_sort[j + 1] = t; 213 png_ptr->dither_sort[j + 1] = t;
198 done = 0; 214 done = 0;
199 } 215 }
200 } 216 }
201 if (done) 217 if (done)
202 break; 218 break;
203 } 219 }
204 220
205 /* swap the palette around, and set up a table, if necessary */ 221 /* Swap the palette around, and set up a table, if necessary */
206 if (full_dither) 222 if (full_dither)
207 { 223 {
208 int j = num_palette; 224 int j = num_palette;
209 225
210 /* put all the useful colors within the max, but don't 226 /* Put all the useful colors within the max, but don't
211 move the others */ 227 * move the others.
228 */
212 for (i = 0; i < maximum_colors; i++) 229 for (i = 0; i < maximum_colors; i++)
213 { 230 {
214 if ((int)png_ptr->dither_sort[i] >= maximum_colors) 231 if ((int)png_ptr->dither_sort[i] >= maximum_colors)
215 { 232 {
216 do 233 do
217 j--; 234 j--;
218 while ((int)png_ptr->dither_sort[j] >= maximum_colors); 235 while ((int)png_ptr->dither_sort[j] >= maximum_colors);
219 palette[i] = palette[j]; 236 palette[i] = palette[j];
220 } 237 }
221 } 238 }
222 } 239 }
223 else 240 else
224 { 241 {
225 int j = num_palette; 242 int j = num_palette;
226 243
227 /* move all the used colors inside the max limit, and 244 /* Move all the used colors inside the max limit, and
228 develop a translation table */ 245 * develop a translation table.
246 */
229 for (i = 0; i < maximum_colors; i++) 247 for (i = 0; i < maximum_colors; i++)
230 { 248 {
231 /* only move the colors we need to */ 249 /* Only move the colors we need to */
232 if ((int)png_ptr->dither_sort[i] >= maximum_colors) 250 if ((int)png_ptr->dither_sort[i] >= maximum_colors)
233 { 251 {
234 png_color tmp_color; 252 png_color tmp_color;
235 253
236 do 254 do
237 j--; 255 j--;
238 while ((int)png_ptr->dither_sort[j] >= maximum_colors); 256 while ((int)png_ptr->dither_sort[j] >= maximum_colors);
239 257
240 tmp_color = palette[j]; 258 tmp_color = palette[j];
241 palette[j] = palette[i]; 259 palette[j] = palette[i];
242 palette[i] = tmp_color; 260 palette[i] = tmp_color;
243 /* indicate where the color went */ 261 /* Indicate where the color went */
244 png_ptr->dither_index[j] = (png_byte)i; 262 png_ptr->dither_index[j] = (png_byte)i;
245 png_ptr->dither_index[i] = (png_byte)j; 263 png_ptr->dither_index[i] = (png_byte)j;
246 } 264 }
247 } 265 }
248 266
249 /* find closest color for those colors we are not using */ 267 /* Find closest color for those colors we are not using */
250 for (i = 0; i < num_palette; i++) 268 for (i = 0; i < num_palette; i++)
251 { 269 {
252 if ((int)png_ptr->dither_index[i] >= maximum_colors) 270 if ((int)png_ptr->dither_index[i] >= maximum_colors)
253 { 271 {
254 int min_d, k, min_k, d_index; 272 int min_d, k, min_k, d_index;
255 273
256 /* find the closest color to one we threw out */ 274 /* Find the closest color to one we threw out */
257 d_index = png_ptr->dither_index[i]; 275 d_index = png_ptr->dither_index[i];
258 min_d = PNG_COLOR_DIST(palette[d_index], palette[0]); 276 min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
259 for (k = 1, min_k = 0; k < maximum_colors; k++) 277 for (k = 1, min_k = 0; k < maximum_colors; k++)
260 { 278 {
261 int d; 279 int d;
262 280
263 d = PNG_COLOR_DIST(palette[d_index], palette[k]); 281 d = PNG_COLOR_DIST(palette[d_index], palette[k]);
264 282
265 if (d < min_d) 283 if (d < min_d)
266 { 284 {
267 min_d = d; 285 min_d = d;
268 min_k = k; 286 min_k = k;
269 } 287 }
270 } 288 }
271 /* point to closest color */ 289 /* Point to closest color */
272 png_ptr->dither_index[i] = (png_byte)min_k; 290 png_ptr->dither_index[i] = (png_byte)min_k;
273 } 291 }
274 } 292 }
275 } 293 }
276 png_free(png_ptr, png_ptr->dither_sort); 294 png_free(png_ptr, png_ptr->dither_sort);
277 png_ptr->dither_sort = NULL; 295 png_ptr->dither_sort = NULL;
278 } 296 }
279 else 297 else
280 { 298 {
281 /* This is much harder to do simply (and quickly). Perhaps 299 /* This is much harder to do simply (and quickly). Perhaps
282 we need to go through a median cut routine, but those 300 * we need to go through a median cut routine, but those
283 don't always behave themselves with only a few colors 301 * don't always behave themselves with only a few colors
284 as input. So we will just find the closest two colors, 302 * as input. So we will just find the closest two colors,
285 and throw out one of them (chosen somewhat randomly). 303 * and throw out one of them (chosen somewhat randomly).
286 [We don't understand this at all, so if someone wants to 304 * [We don't understand this at all, so if someone wants to
287 work on improving it, be our guest - AED, GRP] 305 * work on improving it, be our guest - AED, GRP]
288 */ 306 */
289 int i; 307 int i;
290 int max_d; 308 int max_d;
291 int num_new_palette; 309 int num_new_palette;
292 png_dsortp t; 310 png_dsortp t;
293 png_dsortpp hash; 311 png_dsortpp hash;
294 312
295 t = NULL; 313 t = NULL;
296 314
297 /* initialize palette index arrays */ 315 /* Initialize palette index arrays */
298 png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr, 316 png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
299 (png_uint_32)(num_palette * png_sizeof(png_byte))); 317 (png_uint_32)(num_palette * png_sizeof(png_byte)));
300 png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr, 318 png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
301 (png_uint_32)(num_palette * png_sizeof(png_byte))); 319 (png_uint_32)(num_palette * png_sizeof(png_byte)));
302 320
303 /* initialize the sort array */ 321 /* Initialize the sort array */
304 for (i = 0; i < num_palette; i++) 322 for (i = 0; i < num_palette; i++)
305 { 323 {
306 png_ptr->index_to_palette[i] = (png_byte)i; 324 png_ptr->index_to_palette[i] = (png_byte)i;
307 png_ptr->palette_to_index[i] = (png_byte)i; 325 png_ptr->palette_to_index[i] = (png_byte)i;
308 } 326 }
309 327
310 hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 * 328 hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 *
311 png_sizeof(png_dsortp))); 329 png_sizeof(png_dsortp)));
312 png_memset(hash, 0, 769 * png_sizeof(png_dsortp)); 330 png_memset(hash, 0, 769 * png_sizeof(png_dsortp));
313 331
314 num_new_palette = num_palette; 332 num_new_palette = num_palette;
315 333
316 /* initial wild guess at how far apart the farthest pixel 334 /* Initial wild guess at how far apart the farthest pixel
317 pair we will be eliminating will be. Larger 335 * pair we will be eliminating will be. Larger
318 numbers mean more areas will be allocated, Smaller 336 * numbers mean more areas will be allocated, Smaller
319 numbers run the risk of not saving enough data, and 337 * numbers run the risk of not saving enough data, and
320 having to do this all over again. 338 * having to do this all over again.
321 339 *
322 I have not done extensive checking on this number. 340 * I have not done extensive checking on this number.
323 */ 341 */
324 max_d = 96; 342 max_d = 96;
325 343
326 while (num_new_palette > maximum_colors) 344 while (num_new_palette > maximum_colors)
327 { 345 {
328 for (i = 0; i < num_new_palette - 1; i++) 346 for (i = 0; i < num_new_palette - 1; i++)
329 { 347 {
330 int j; 348 int j;
331 349
332 for (j = i + 1; j < num_new_palette; j++) 350 for (j = i + 1; j < num_new_palette; j++)
333 { 351 {
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 * also needlessly introduces small errors. 534 * also needlessly introduces small errors.
517 * 535 *
518 * We will turn off gamma transformation later if no semitransparent entries 536 * We will turn off gamma transformation later if no semitransparent entries
519 * are present in the tRNS array for palette images. We can't do it here 537 * are present in the tRNS array for palette images. We can't do it here
520 * because we don't necessarily have the tRNS chunk yet. 538 * because we don't necessarily have the tRNS chunk yet.
521 */ 539 */
522 void PNGAPI 540 void PNGAPI
523 png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma) 541 png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
524 { 542 {
525 png_debug(1, "in png_set_gamma"); 543 png_debug(1, "in png_set_gamma");
526 if (png_ptr == NULL) return; 544 if (png_ptr == NULL)
545 return;
527 if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) || 546 if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) ||
528 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) || 547 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
529 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) 548 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
530 png_ptr->transformations |= PNG_GAMMA; 549 png_ptr->transformations |= PNG_GAMMA;
531 png_ptr->gamma = (float)file_gamma; 550 png_ptr->gamma = (float)file_gamma;
532 png_ptr->screen_gamma = (float)scrn_gamma; 551 png_ptr->screen_gamma = (float)scrn_gamma;
533 } 552 }
534 #endif 553 #endif
535 554
536 #if defined(PNG_READ_EXPAND_SUPPORTED) 555 #if defined(PNG_READ_EXPAND_SUPPORTED)
537 /* Expand paletted images to RGB, expand grayscale images of 556 /* Expand paletted images to RGB, expand grayscale images of
538 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks 557 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
539 * to alpha channels. 558 * to alpha channels.
540 */ 559 */
541 void PNGAPI 560 void PNGAPI
542 png_set_expand(png_structp png_ptr) 561 png_set_expand(png_structp png_ptr)
543 { 562 {
544 png_debug(1, "in png_set_expand"); 563 png_debug(1, "in png_set_expand");
545 if (png_ptr == NULL) return; 564 if (png_ptr == NULL)
565 return;
546 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); 566 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
547 png_ptr->flags &= ~PNG_FLAG_ROW_INIT; 567 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
548 } 568 }
549 569
550 /* GRR 19990627: the following three functions currently are identical 570 /* GRR 19990627: the following three functions currently are identical
551 * to png_set_expand(). However, it is entirely reasonable that someone 571 * to png_set_expand(). However, it is entirely reasonable that someone
552 * might wish to expand an indexed image to RGB but *not* expand a single, 572 * might wish to expand an indexed image to RGB but *not* expand a single,
553 * fully transparent palette entry to a full alpha channel--perhaps instead 573 * fully transparent palette entry to a full alpha channel--perhaps instead
554 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace 574 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
555 * the transparent color with a particular RGB value, or drop tRNS entirely. 575 * the transparent color with a particular RGB value, or drop tRNS entirely.
556 * IOW, a future version of the library may make the transformations flag 576 * IOW, a future version of the library may make the transformations flag
557 * a bit more fine-grained, with separate bits for each of these three 577 * a bit more fine-grained, with separate bits for each of these three
558 * functions. 578 * functions.
559 * 579 *
560 * More to the point, these functions make it obvious what libpng will be 580 * More to the point, these functions make it obvious what libpng will be
561 * doing, whereas "expand" can (and does) mean any number of things. 581 * doing, whereas "expand" can (and does) mean any number of things.
562 * 582 *
563 * GRP 20060307: In libpng-1.4.0, png_set_gray_1_2_4_to_8() was modified 583 * GRP 20060307: In libpng-1.4.0, png_set_gray_1_2_4_to_8() was modified
564 * to expand only the sample depth but not to expand the tRNS to alpha. 584 * to expand only the sample depth but not to expand the tRNS to alpha.
565 */ 585 */
566 586
567 /* Expand paletted images to RGB. */ 587 /* Expand paletted images to RGB. */
568 void PNGAPI 588 void PNGAPI
569 png_set_palette_to_rgb(png_structp png_ptr) 589 png_set_palette_to_rgb(png_structp png_ptr)
570 { 590 {
571 png_debug(1, "in png_set_palette_to_rgb"); 591 png_debug(1, "in png_set_palette_to_rgb");
572 if (png_ptr == NULL) return; 592 if (png_ptr == NULL)
593 return;
573 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); 594 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
574 png_ptr->flags &= ~PNG_FLAG_ROW_INIT; 595 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
575 } 596 }
576 597
577 #if !defined(PNG_1_0_X) 598 #if !defined(PNG_1_0_X)
578 /* Expand grayscale images of less than 8-bit depth to 8 bits. */ 599 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
579 void PNGAPI 600 void PNGAPI
580 png_set_expand_gray_1_2_4_to_8(png_structp png_ptr) 601 png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
581 { 602 {
582 png_debug(1, "in png_set_expand_gray_1_2_4_to_8"); 603 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
583 if (png_ptr == NULL) return; 604 if (png_ptr == NULL)
605 return;
584 png_ptr->transformations |= PNG_EXPAND; 606 png_ptr->transformations |= PNG_EXPAND;
585 png_ptr->flags &= ~PNG_FLAG_ROW_INIT; 607 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
586 } 608 }
587 #endif 609 #endif
588 610
589 #if defined(PNG_1_0_X) || defined(PNG_1_2_X) 611 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
590 /* Expand grayscale images of less than 8-bit depth to 8 bits. */ 612 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
591 /* Deprecated as of libpng-1.2.9 */ 613 /* Deprecated as of libpng-1.2.9 */
592 void PNGAPI 614 void PNGAPI
593 png_set_gray_1_2_4_to_8(png_structp png_ptr) 615 png_set_gray_1_2_4_to_8(png_structp png_ptr)
594 { 616 {
595 png_debug(1, "in png_set_gray_1_2_4_to_8"); 617 png_debug(1, "in png_set_gray_1_2_4_to_8");
596 if (png_ptr == NULL) return; 618 if (png_ptr == NULL)
619 return;
597 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); 620 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
598 } 621 }
599 #endif 622 #endif
600 623
601 624
602 /* Expand tRNS chunks to alpha channels. */ 625 /* Expand tRNS chunks to alpha channels. */
603 void PNGAPI 626 void PNGAPI
604 png_set_tRNS_to_alpha(png_structp png_ptr) 627 png_set_tRNS_to_alpha(png_structp png_ptr)
605 { 628 {
606 png_debug(1, "in png_set_tRNS_to_alpha"); 629 png_debug(1, "in png_set_tRNS_to_alpha");
(...skipping 15 matching lines...) Expand all
622 #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) 645 #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
623 #if defined(PNG_FLOATING_POINT_SUPPORTED) 646 #if defined(PNG_FLOATING_POINT_SUPPORTED)
624 /* Convert a RGB image to a grayscale of the same width. This allows us, 647 /* Convert a RGB image to a grayscale of the same width. This allows us,
625 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image. 648 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
626 */ 649 */
627 650
628 void PNGAPI 651 void PNGAPI
629 png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red, 652 png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
630 double green) 653 double green)
631 { 654 {
632 int red_fixed = (int)((float)red*100000.0 + 0.5); 655 int red_fixed = (int)((float)red*100000.0 + 0.5);
633 int green_fixed = (int)((float)green*100000.0 + 0.5); 656 int green_fixed = (int)((float)green*100000.0 + 0.5);
634 if (png_ptr == NULL) return; 657 if (png_ptr == NULL)
635 png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed); 658 return;
659 png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed);
636 } 660 }
637 #endif 661 #endif
638 662
639 void PNGAPI 663 void PNGAPI
640 png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action, 664 png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
641 png_fixed_point red, png_fixed_point green) 665 png_fixed_point red, png_fixed_point green)
642 { 666 {
643 png_debug(1, "in png_set_rgb_to_gray"); 667 png_debug(1, "in png_set_rgb_to_gray");
644 if (png_ptr == NULL) return; 668 if (png_ptr == NULL)
669 return;
645 switch(error_action) 670 switch(error_action)
646 { 671 {
647 case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY; 672 case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;
648 break; 673 break;
674
649 case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN; 675 case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
650 break; 676 break;
677
651 case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR; 678 case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
652 } 679 }
653 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 680 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
654 #if defined(PNG_READ_EXPAND_SUPPORTED) 681 #if defined(PNG_READ_EXPAND_SUPPORTED)
655 png_ptr->transformations |= PNG_EXPAND; 682 png_ptr->transformations |= PNG_EXPAND;
656 #else 683 #else
657 { 684 {
658 png_warning(png_ptr, 685 png_warning(png_ptr,
659 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED."); 686 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED.");
660 png_ptr->transformations &= ~PNG_RGB_TO_GRAY; 687 png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
661 } 688 }
662 #endif 689 #endif
663 { 690 {
664 png_uint_16 red_int, green_int; 691 png_uint_16 red_int, green_int;
665 if (red < 0 || green < 0) 692 if (red < 0 || green < 0)
666 { 693 {
667 red_int = 6968; /* .212671 * 32768 + .5 */ 694 red_int = 6968; /* .212671 * 32768 + .5 */
668 green_int = 23434; /* .715160 * 32768 + .5 */ 695 green_int = 23434; /* .715160 * 32768 + .5 */
669 } 696 }
670 else if (red + green < 100000L) 697 else if (red + green < 100000L)
671 { 698 {
672 red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L); 699 red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
673 green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L); 700 green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
674 } 701 }
675 else 702 else
676 { 703 {
677 png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients"); 704 png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
678 red_int = 6968; 705 red_int = 6968;
679 green_int = 23434; 706 green_int = 23434;
680 } 707 }
681 png_ptr->rgb_to_gray_red_coeff = red_int; 708 png_ptr->rgb_to_gray_red_coeff = red_int;
682 png_ptr->rgb_to_gray_green_coeff = green_int; 709 png_ptr->rgb_to_gray_green_coeff = green_int;
683 png_ptr->rgb_to_gray_blue_coeff = 710 png_ptr->rgb_to_gray_blue_coeff =
684 (png_uint_16)(32768 - red_int - green_int); 711 (png_uint_16)(32768 - red_int - green_int);
685 } 712 }
686 } 713 }
687 #endif 714 #endif
688 715
689 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ 716 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
690 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \ 717 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
691 defined(PNG_LEGACY_SUPPORTED) 718 defined(PNG_LEGACY_SUPPORTED)
692 void PNGAPI 719 void PNGAPI
693 png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr 720 png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
694 read_user_transform_fn) 721 read_user_transform_fn)
695 { 722 {
696 png_debug(1, "in png_set_read_user_transform_fn"); 723 png_debug(1, "in png_set_read_user_transform_fn");
697 if (png_ptr == NULL) return; 724 if (png_ptr == NULL)
725 return;
698 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) 726 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
699 png_ptr->transformations |= PNG_USER_TRANSFORM; 727 png_ptr->transformations |= PNG_USER_TRANSFORM;
700 png_ptr->read_user_transform_fn = read_user_transform_fn; 728 png_ptr->read_user_transform_fn = read_user_transform_fn;
701 #endif 729 #endif
702 #ifdef PNG_LEGACY_SUPPORTED 730 #ifdef PNG_LEGACY_SUPPORTED
703 if (read_user_transform_fn) 731 if (read_user_transform_fn)
704 png_warning(png_ptr, 732 png_warning(png_ptr,
705 "This version of libpng does not support user transforms"); 733 "This version of libpng does not support user transforms");
706 #endif 734 #endif
707 } 735 }
(...skipping 12 matching lines...) Expand all
720 { 748 {
721 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \ 749 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \
722 || defined(PNG_READ_GAMMA_SUPPORTED) 750 || defined(PNG_READ_GAMMA_SUPPORTED)
723 int color_type = png_ptr->color_type; 751 int color_type = png_ptr->color_type;
724 #endif 752 #endif
725 753
726 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) 754 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
727 755
728 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) 756 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
729 /* Detect gray background and attempt to enable optimization 757 /* Detect gray background and attempt to enable optimization
730 * for gray --> RGB case */ 758 * for gray --> RGB case
731 /* Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or 759 *
760 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
732 * RGB_ALPHA (in which case need_expand is superfluous anyway), the 761 * RGB_ALPHA (in which case need_expand is superfluous anyway), the
733 * background color might actually be gray yet not be flagged as such. 762 * background color might actually be gray yet not be flagged as such.
734 * This is not a problem for the current code, which uses 763 * This is not a problem for the current code, which uses
735 * PNG_BACKGROUND_IS_GRAY only to decide when to do the 764 * PNG_BACKGROUND_IS_GRAY only to decide when to do the
736 * png_do_gray_to_rgb() transformation. 765 * png_do_gray_to_rgb() transformation.
737 */ 766 */
738 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && 767 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
739 !(color_type & PNG_COLOR_MASK_COLOR)) 768 !(color_type & PNG_COLOR_MASK_COLOR))
740 { 769 {
741 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; 770 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
742 } else if ((png_ptr->transformations & PNG_BACKGROUND) && 771 } else if ((png_ptr->transformations & PNG_BACKGROUND) &&
743 !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) && 772 !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
744 (png_ptr->transformations & PNG_GRAY_TO_RGB) && 773 (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
745 png_ptr->background.red == png_ptr->background.green && 774 png_ptr->background.red == png_ptr->background.green &&
746 png_ptr->background.red == png_ptr->background.blue) 775 png_ptr->background.red == png_ptr->background.blue)
747 { 776 {
748 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; 777 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
749 png_ptr->background.gray = png_ptr->background.red; 778 png_ptr->background.gray = png_ptr->background.red;
750 } 779 }
751 #endif 780 #endif
752 781
753 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && 782 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
754 (png_ptr->transformations & PNG_EXPAND)) 783 (png_ptr->transformations & PNG_EXPAND))
755 { 784 {
756 if (!(color_type & PNG_COLOR_MASK_COLOR)) /* i.e., GRAY or GRAY_ALPHA */ 785 if (!(color_type & PNG_COLOR_MASK_COLOR)) /* i.e., GRAY or GRAY_ALPHA */
757 { 786 {
758 /* expand background and tRNS chunks */ 787 /* Expand background and tRNS chunks */
759 switch (png_ptr->bit_depth) 788 switch (png_ptr->bit_depth)
760 { 789 {
761 case 1: 790 case 1:
762 png_ptr->background.gray *= (png_uint_16)0xff; 791 png_ptr->background.gray *= (png_uint_16)0xff;
763 png_ptr->background.red = png_ptr->background.green 792 png_ptr->background.red = png_ptr->background.green
764 = png_ptr->background.blue = png_ptr->background.gray; 793 = png_ptr->background.blue = png_ptr->background.gray;
765 if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) 794 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
766 { 795 {
767 png_ptr->trans_values.gray *= (png_uint_16)0xff; 796 png_ptr->trans_values.gray *= (png_uint_16)0xff;
768 png_ptr->trans_values.red = png_ptr->trans_values.green 797 png_ptr->trans_values.red = png_ptr->trans_values.green
769 = png_ptr->trans_values.blue = png_ptr->trans_values.gray; 798 = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
770 } 799 }
771 break; 800 break;
801
772 case 2: 802 case 2:
773 png_ptr->background.gray *= (png_uint_16)0x55; 803 png_ptr->background.gray *= (png_uint_16)0x55;
774 png_ptr->background.red = png_ptr->background.green 804 png_ptr->background.red = png_ptr->background.green
775 = png_ptr->background.blue = png_ptr->background.gray; 805 = png_ptr->background.blue = png_ptr->background.gray;
776 if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) 806 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
777 { 807 {
778 png_ptr->trans_values.gray *= (png_uint_16)0x55; 808 png_ptr->trans_values.gray *= (png_uint_16)0x55;
779 png_ptr->trans_values.red = png_ptr->trans_values.green 809 png_ptr->trans_values.red = png_ptr->trans_values.green
780 = png_ptr->trans_values.blue = png_ptr->trans_values.gray; 810 = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
781 } 811 }
782 break; 812 break;
813
783 case 4: 814 case 4:
784 png_ptr->background.gray *= (png_uint_16)0x11; 815 png_ptr->background.gray *= (png_uint_16)0x11;
785 png_ptr->background.red = png_ptr->background.green 816 png_ptr->background.red = png_ptr->background.green
786 = png_ptr->background.blue = png_ptr->background.gray; 817 = png_ptr->background.blue = png_ptr->background.gray;
787 if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) 818 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
788 { 819 {
789 png_ptr->trans_values.gray *= (png_uint_16)0x11; 820 png_ptr->trans_values.gray *= (png_uint_16)0x11;
790 png_ptr->trans_values.red = png_ptr->trans_values.green 821 png_ptr->trans_values.red = png_ptr->trans_values.green
791 = png_ptr->trans_values.blue = png_ptr->trans_values.gray; 822 = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
792 } 823 }
793 break; 824 break;
825
794 case 8: 826 case 8:
827
795 case 16: 828 case 16:
796 png_ptr->background.red = png_ptr->background.green 829 png_ptr->background.red = png_ptr->background.green
797 = png_ptr->background.blue = png_ptr->background.gray; 830 = png_ptr->background.blue = png_ptr->background.gray;
798 break; 831 break;
799 } 832 }
800 } 833 }
801 else if (color_type == PNG_COLOR_TYPE_PALETTE) 834 else if (color_type == PNG_COLOR_TYPE_PALETTE)
802 { 835 {
803 png_ptr->background.red = 836 png_ptr->background.red =
804 png_ptr->palette[png_ptr->background.index].red; 837 png_ptr->palette[png_ptr->background.index].red;
805 png_ptr->background.green = 838 png_ptr->background.green =
806 png_ptr->palette[png_ptr->background.index].green; 839 png_ptr->palette[png_ptr->background.index].green;
807 png_ptr->background.blue = 840 png_ptr->background.blue =
808 png_ptr->palette[png_ptr->background.index].blue; 841 png_ptr->palette[png_ptr->background.index].blue;
809 842
810 #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) 843 #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
811 if (png_ptr->transformations & PNG_INVERT_ALPHA) 844 if (png_ptr->transformations & PNG_INVERT_ALPHA)
812 { 845 {
813 #if defined(PNG_READ_EXPAND_SUPPORTED) 846 #if defined(PNG_READ_EXPAND_SUPPORTED)
814 if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) 847 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
815 #endif 848 #endif
816 { 849 {
817 /* invert the alpha channel (in tRNS) unless the pixels are 850 /* Invert the alpha channel (in tRNS) unless the pixels are
818 going to be expanded, in which case leave it for later */ 851 * going to be expanded, in which case leave it for later
852 */
819 int i, istop; 853 int i, istop;
820 istop=(int)png_ptr->num_trans; 854 istop=(int)png_ptr->num_trans;
821 for (i=0; i<istop; i++) 855 for (i=0; i<istop; i++)
822 png_ptr->trans[i] = (png_byte)(255 - png_ptr->trans[i]); 856 png_ptr->trans[i] = (png_byte)(255 - png_ptr->trans[i]);
823 } 857 }
824 } 858 }
825 #endif 859 #endif
826 860
827 } 861 }
828 } 862 }
829 #endif 863 #endif
830 864
831 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED) 865 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
832 png_ptr->background_1 = png_ptr->background; 866 png_ptr->background_1 = png_ptr->background;
833 #endif 867 #endif
834 #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) 868 #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
835 869
836 if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0) 870 if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
837 && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0) 871 && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0)
838 < PNG_GAMMA_THRESHOLD)) 872 < PNG_GAMMA_THRESHOLD))
839 { 873 {
840 int i, k; 874 int i, k;
841 k=0; 875 k=0;
842 for (i=0; i<png_ptr->num_trans; i++) 876 for (i=0; i<png_ptr->num_trans; i++)
843 { 877 {
844 if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff) 878 if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff)
845 k=1; /* partial transparency is present */ 879 k=1; /* Partial transparency is present */
846 } 880 }
847 if (k == 0) 881 if (k == 0)
848 png_ptr->transformations &= ~PNG_GAMMA; 882 png_ptr->transformations &= ~PNG_GAMMA;
849 } 883 }
850 884
851 if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) && 885 if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
852 png_ptr->gamma != 0.0) 886 png_ptr->gamma != 0.0)
853 { 887 {
854 png_build_gamma_table(png_ptr); 888 png_build_gamma_table(png_ptr);
855 #if defined(PNG_READ_BACKGROUND_SUPPORTED) 889 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
856 if (png_ptr->transformations & PNG_BACKGROUND) 890 if (png_ptr->transformations & PNG_BACKGROUND)
857 { 891 {
858 if (color_type == PNG_COLOR_TYPE_PALETTE) 892 if (color_type == PNG_COLOR_TYPE_PALETTE)
859 { 893 {
860 /* could skip if no transparency and 894 /* Could skip if no transparency */
861 */
862 png_color back, back_1; 895 png_color back, back_1;
863 png_colorp palette = png_ptr->palette; 896 png_colorp palette = png_ptr->palette;
864 int num_palette = png_ptr->num_palette; 897 int num_palette = png_ptr->num_palette;
865 int i; 898 int i;
866 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) 899 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
867 { 900 {
868 back.red = png_ptr->gamma_table[png_ptr->background.red]; 901 back.red = png_ptr->gamma_table[png_ptr->background.red];
869 back.green = png_ptr->gamma_table[png_ptr->background.green]; 902 back.green = png_ptr->gamma_table[png_ptr->background.green];
870 back.blue = png_ptr->gamma_table[png_ptr->background.blue]; 903 back.blue = png_ptr->gamma_table[png_ptr->background.blue];
871 904
872 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; 905 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
873 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; 906 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
874 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; 907 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
875 } 908 }
876 else 909 else
877 { 910 {
878 double g, gs; 911 double g, gs;
879 912
880 switch (png_ptr->background_gamma_type) 913 switch (png_ptr->background_gamma_type)
881 { 914 {
882 case PNG_BACKGROUND_GAMMA_SCREEN: 915 case PNG_BACKGROUND_GAMMA_SCREEN:
883 g = (png_ptr->screen_gamma); 916 g = (png_ptr->screen_gamma);
884 gs = 1.0; 917 gs = 1.0;
885 break; 918 break;
919
886 case PNG_BACKGROUND_GAMMA_FILE: 920 case PNG_BACKGROUND_GAMMA_FILE:
887 g = 1.0 / (png_ptr->gamma); 921 g = 1.0 / (png_ptr->gamma);
888 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); 922 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
889 break; 923 break;
924
890 case PNG_BACKGROUND_GAMMA_UNIQUE: 925 case PNG_BACKGROUND_GAMMA_UNIQUE:
891 g = 1.0 / (png_ptr->background_gamma); 926 g = 1.0 / (png_ptr->background_gamma);
892 gs = 1.0 / (png_ptr->background_gamma * 927 gs = 1.0 / (png_ptr->background_gamma *
893 png_ptr->screen_gamma); 928 png_ptr->screen_gamma);
894 break; 929 break;
895 default: 930 default:
896 g = 1.0; /* back_1 */ 931 g = 1.0; /* back_1 */
897 gs = 1.0; /* back */ 932 gs = 1.0; /* back */
898 } 933 }
899 934
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
968 double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1); 1003 double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);
969 double g = 1.0; 1004 double g = 1.0;
970 double gs = 1.0; 1005 double gs = 1.0;
971 1006
972 switch (png_ptr->background_gamma_type) 1007 switch (png_ptr->background_gamma_type)
973 { 1008 {
974 case PNG_BACKGROUND_GAMMA_SCREEN: 1009 case PNG_BACKGROUND_GAMMA_SCREEN:
975 g = (png_ptr->screen_gamma); 1010 g = (png_ptr->screen_gamma);
976 gs = 1.0; 1011 gs = 1.0;
977 break; 1012 break;
1013
978 case PNG_BACKGROUND_GAMMA_FILE: 1014 case PNG_BACKGROUND_GAMMA_FILE:
979 g = 1.0 / (png_ptr->gamma); 1015 g = 1.0 / (png_ptr->gamma);
980 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); 1016 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
981 break; 1017 break;
1018
982 case PNG_BACKGROUND_GAMMA_UNIQUE: 1019 case PNG_BACKGROUND_GAMMA_UNIQUE:
983 g = 1.0 / (png_ptr->background_gamma); 1020 g = 1.0 / (png_ptr->background_gamma);
984 gs = 1.0 / (png_ptr->background_gamma * 1021 gs = 1.0 / (png_ptr->background_gamma *
985 png_ptr->screen_gamma); 1022 png_ptr->screen_gamma);
986 break; 1023 break;
987 } 1024 }
988 1025
989 png_ptr->background_1.gray = (png_uint_16)(pow( 1026 png_ptr->background_1.gray = (png_uint_16)(pow(
990 (double)png_ptr->background.gray / m, g) * m + .5); 1027 (double)png_ptr->background.gray / m, g) * m + .5);
991 png_ptr->background.gray = (png_uint_16)(pow( 1028 png_ptr->background.gray = (png_uint_16)(pow(
(...skipping 21 matching lines...) Expand all
1013 { 1050 {
1014 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */ 1051 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1015 png_ptr->background_1.red = png_ptr->background_1.green 1052 png_ptr->background_1.red = png_ptr->background_1.green
1016 = png_ptr->background_1.blue = png_ptr->background_1.gray; 1053 = png_ptr->background_1.blue = png_ptr->background_1.gray;
1017 png_ptr->background.red = png_ptr->background.green 1054 png_ptr->background.red = png_ptr->background.green
1018 = png_ptr->background.blue = png_ptr->background.gray; 1055 = png_ptr->background.blue = png_ptr->background.gray;
1019 } 1056 }
1020 } 1057 }
1021 } 1058 }
1022 else 1059 else
1023 /* transformation does not include PNG_BACKGROUND */ 1060 /* Transformation does not include PNG_BACKGROUND */
1024 #endif /* PNG_READ_BACKGROUND_SUPPORTED */ 1061 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1025 if (color_type == PNG_COLOR_TYPE_PALETTE) 1062 if (color_type == PNG_COLOR_TYPE_PALETTE)
1026 { 1063 {
1027 png_colorp palette = png_ptr->palette; 1064 png_colorp palette = png_ptr->palette;
1028 int num_palette = png_ptr->num_palette; 1065 int num_palette = png_ptr->num_palette;
1029 int i; 1066 int i;
1030 1067
1031 for (i = 0; i < num_palette; i++) 1068 for (i = 0; i < num_palette; i++)
1032 { 1069 {
1033 palette[i].red = png_ptr->gamma_table[palette[i].red]; 1070 palette[i].red = png_ptr->gamma_table[palette[i].red];
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
1185 1222
1186 #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) 1223 #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
1187 if (png_ptr->transformations & PNG_RGB_TO_GRAY) 1224 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1188 info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR; 1225 info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
1189 #endif 1226 #endif
1190 1227
1191 #if defined(PNG_READ_DITHER_SUPPORTED) 1228 #if defined(PNG_READ_DITHER_SUPPORTED)
1192 if (png_ptr->transformations & PNG_DITHER) 1229 if (png_ptr->transformations & PNG_DITHER)
1193 { 1230 {
1194 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || 1231 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1195 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && 1232 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
1196 png_ptr->palette_lookup && info_ptr->bit_depth == 8) 1233 png_ptr->palette_lookup && info_ptr->bit_depth == 8)
1197 { 1234 {
1198 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; 1235 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
1199 } 1236 }
1200 } 1237 }
1201 #endif 1238 #endif
1202 1239
1203 #if defined(PNG_READ_PACK_SUPPORTED) 1240 #if defined(PNG_READ_PACK_SUPPORTED)
1204 if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8)) 1241 if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
1205 info_ptr->bit_depth = 8; 1242 info_ptr->bit_depth = 8;
1206 #endif 1243 #endif
(...skipping 13 matching lines...) Expand all
1220 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) 1257 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1221 info_ptr->channels++; 1258 info_ptr->channels++;
1222 1259
1223 #if defined(PNG_READ_FILLER_SUPPORTED) 1260 #if defined(PNG_READ_FILLER_SUPPORTED)
1224 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */ 1261 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
1225 if ((png_ptr->transformations & PNG_FILLER) && 1262 if ((png_ptr->transformations & PNG_FILLER) &&
1226 ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || 1263 ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1227 (info_ptr->color_type == PNG_COLOR_TYPE_GRAY))) 1264 (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
1228 { 1265 {
1229 info_ptr->channels++; 1266 info_ptr->channels++;
1230 /* if adding a true alpha channel not just filler */ 1267 /* If adding a true alpha channel not just filler */
1231 #if !defined(PNG_1_0_X) 1268 #if !defined(PNG_1_0_X)
1232 if (png_ptr->transformations & PNG_ADD_ALPHA) 1269 if (png_ptr->transformations & PNG_ADD_ALPHA)
1233 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; 1270 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1234 #endif 1271 #endif
1235 } 1272 }
1236 #endif 1273 #endif
1237 1274
1238 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ 1275 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
1239 defined(PNG_READ_USER_TRANSFORM_SUPPORTED) 1276 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1240 if (png_ptr->transformations & PNG_USER_TRANSFORM) 1277 if (png_ptr->transformations & PNG_USER_TRANSFORM)
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1275 png_ptr->pass); 1312 png_ptr->pass);
1276 png_error(png_ptr, msg); 1313 png_error(png_ptr, msg);
1277 #else 1314 #else
1278 png_error(png_ptr, "NULL row buffer"); 1315 png_error(png_ptr, "NULL row buffer");
1279 #endif 1316 #endif
1280 } 1317 }
1281 #ifdef PNG_WARN_UNINITIALIZED_ROW 1318 #ifdef PNG_WARN_UNINITIALIZED_ROW
1282 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) 1319 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
1283 /* Application has failed to call either png_read_start_image() 1320 /* Application has failed to call either png_read_start_image()
1284 * or png_read_update_info() after setting transforms that expand 1321 * or png_read_update_info() after setting transforms that expand
1285 * pixels. This check added to libpng-1.2.19 */ 1322 * pixels. This check added to libpng-1.2.19
1323 */
1286 #if (PNG_WARN_UNINITIALIZED_ROW==1) 1324 #if (PNG_WARN_UNINITIALIZED_ROW==1)
1287 png_error(png_ptr, "Uninitialized row"); 1325 png_error(png_ptr, "Uninitialized row");
1288 #else 1326 #else
1289 png_warning(png_ptr, "Uninitialized row"); 1327 png_warning(png_ptr, "Uninitialized row");
1290 #endif 1328 #endif
1291 #endif 1329 #endif
1292 1330
1293 #if defined(PNG_READ_EXPAND_SUPPORTED) 1331 #if defined(PNG_READ_EXPAND_SUPPORTED)
1294 if (png_ptr->transformations & PNG_EXPAND) 1332 if (png_ptr->transformations & PNG_EXPAND)
1295 { 1333 {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1328 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 1366 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1329 PNG_RGB_TO_GRAY_WARN) 1367 PNG_RGB_TO_GRAY_WARN)
1330 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); 1368 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1331 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 1369 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1332 PNG_RGB_TO_GRAY_ERR) 1370 PNG_RGB_TO_GRAY_ERR)
1333 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); 1371 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1334 } 1372 }
1335 } 1373 }
1336 #endif 1374 #endif
1337 1375
1338 /* 1376 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
1339 From Andreas Dilger e-mail to png-implement, 26 March 1998: 1377 *
1340 1378 * In most cases, the "simple transparency" should be done prior to doing
1341 In most cases, the "simple transparency" should be done prior to doing 1379 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
1342 gray-to-RGB, or you will have to test 3x as many bytes to check if a 1380 * pixel is transparent. You would also need to make sure that the
1343 pixel is transparent. You would also need to make sure that the 1381 * transparency information is upgraded to RGB.
1344 transparency information is upgraded to RGB. 1382 *
1345 1383 * To summarize, the current flow is:
1346 To summarize, the current flow is: 1384 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
1347 - Gray + simple transparency -> compare 1 or 2 gray bytes and composite 1385 * with background "in place" if transparent,
1348 with background "in place" if transparent, 1386 * convert to RGB if necessary
1349 convert to RGB if necessary 1387 * - Gray + alpha -> composite with gray background and remove alpha bytes,
1350 - Gray + alpha -> composite with gray background and remove alpha bytes, 1388 * convert to RGB if necessary
1351 convert to RGB if necessary 1389 *
1352 1390 * To support RGB backgrounds for gray images we need:
1353 To support RGB backgrounds for gray images we need: 1391 * - Gray + simple transparency -> convert to RGB + simple transparency,
1354 - Gray + simple transparency -> convert to RGB + simple transparency, compare 1392 * compare 3 or 6 bytes and composite with
1355 3 or 6 bytes and composite with background 1393 * background "in place" if transparent
1356 "in place" if transparent (3x compare/pixel 1394 * (3x compare/pixel compared to doing
1357 compared to doing composite with gray bkgrnd) 1395 * composite with gray bkgrnd)
1358 - Gray + alpha -> convert to RGB + alpha, composite with background and 1396 * - Gray + alpha -> convert to RGB + alpha, composite with background and
1359 remove alpha bytes (3x float operations/pixel 1397 * remove alpha bytes (3x float
1360 compared with composite on gray background) 1398 * operations/pixel compared with composite
1361 1399 * on gray background)
1362 Greg's change will do this. The reason it wasn't done before is for 1400 *
1363 performance, as this increases the per-pixel operations. If we would check 1401 * Greg's change will do this. The reason it wasn't done before is for
1364 in advance if the background was gray or RGB, and position the gray-to-RGB 1402 * performance, as this increases the per-pixel operations. If we would check
1365 transform appropriately, then it would save a lot of work/time. 1403 * in advance if the background was gray or RGB, and position the gray-to-RGB
1404 * transform appropriately, then it would save a lot of work/time.
1366 */ 1405 */
1367 1406
1368 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) 1407 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
1369 /* if gray -> RGB, do so now only if background is non-gray; else do later 1408 /* If gray -> RGB, do so now only if background is non-gray; else do later
1370 * for performance reasons */ 1409 * for performance reasons
1410 */
1371 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && 1411 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1372 !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) 1412 !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1373 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1); 1413 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1374 #endif 1414 #endif
1375 1415
1376 #if defined(PNG_READ_BACKGROUND_SUPPORTED) 1416 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1377 if ((png_ptr->transformations & PNG_BACKGROUND) && 1417 if ((png_ptr->transformations & PNG_BACKGROUND) &&
1378 ((png_ptr->num_trans != 0 ) || 1418 ((png_ptr->num_trans != 0 ) ||
1379 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) 1419 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
1380 png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1, 1420 png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
1381 &(png_ptr->trans_values), &(png_ptr->background) 1421 &(png_ptr->trans_values), &(png_ptr->background)
1382 #if defined(PNG_READ_GAMMA_SUPPORTED) 1422 #if defined(PNG_READ_GAMMA_SUPPORTED)
1383 , &(png_ptr->background_1), 1423 , &(png_ptr->background_1),
1384 png_ptr->gamma_table, png_ptr->gamma_from_1, 1424 png_ptr->gamma_table, png_ptr->gamma_from_1,
1385 png_ptr->gamma_to_1, png_ptr->gamma_16_table, 1425 png_ptr->gamma_to_1, png_ptr->gamma_16_table,
1386 png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1, 1426 png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
1387 png_ptr->gamma_shift 1427 png_ptr->gamma_shift
1388 #endif 1428 #endif
1389 ); 1429 );
1390 #endif 1430 #endif
1391 1431
1392 #if defined(PNG_READ_GAMMA_SUPPORTED) 1432 #if defined(PNG_READ_GAMMA_SUPPORTED)
1393 if ((png_ptr->transformations & PNG_GAMMA) && 1433 if ((png_ptr->transformations & PNG_GAMMA) &&
1394 #if defined(PNG_READ_BACKGROUND_SUPPORTED) 1434 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1395 !((png_ptr->transformations & PNG_BACKGROUND) && 1435 !((png_ptr->transformations & PNG_BACKGROUND) &&
1396 ((png_ptr->num_trans != 0) || 1436 ((png_ptr->num_trans != 0) ||
1397 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) && 1437 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
1398 #endif 1438 #endif
1399 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) 1439 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
1400 png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1, 1440 png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
1401 png_ptr->gamma_table, png_ptr->gamma_16_table, 1441 png_ptr->gamma_table, png_ptr->gamma_16_table,
1402 png_ptr->gamma_shift); 1442 png_ptr->gamma_shift);
1403 #endif 1443 #endif
1404 1444
1405 #if defined(PNG_READ_16_TO_8_SUPPORTED) 1445 #if defined(PNG_READ_16_TO_8_SUPPORTED)
1406 if (png_ptr->transformations & PNG_16_TO_8) 1446 if (png_ptr->transformations & PNG_16_TO_8)
1407 png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1); 1447 png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
1408 #endif 1448 #endif
1409 1449
1410 #if defined(PNG_READ_DITHER_SUPPORTED) 1450 #if defined(PNG_READ_DITHER_SUPPORTED)
1411 if (png_ptr->transformations & PNG_DITHER) 1451 if (png_ptr->transformations & PNG_DITHER)
1412 { 1452 {
(...skipping 24 matching lines...) Expand all
1437 if (png_ptr->transformations & PNG_BGR) 1477 if (png_ptr->transformations & PNG_BGR)
1438 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1); 1478 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
1439 #endif 1479 #endif
1440 1480
1441 #if defined(PNG_READ_PACKSWAP_SUPPORTED) 1481 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
1442 if (png_ptr->transformations & PNG_PACKSWAP) 1482 if (png_ptr->transformations & PNG_PACKSWAP)
1443 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1); 1483 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1444 #endif 1484 #endif
1445 1485
1446 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) 1486 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
1447 /* if gray -> RGB, do so now only if we did not do so above */ 1487 /* If gray -> RGB, do so now only if we did not do so above */
1448 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && 1488 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1449 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) 1489 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1450 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1); 1490 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1451 #endif 1491 #endif
1452 1492
1453 #if defined(PNG_READ_FILLER_SUPPORTED) 1493 #if defined(PNG_READ_FILLER_SUPPORTED)
1454 if (png_ptr->transformations & PNG_FILLER) 1494 if (png_ptr->transformations & PNG_FILLER)
1455 png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, 1495 png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1456 (png_uint_32)png_ptr->filler, png_ptr->flags); 1496 (png_uint_32)png_ptr->filler, png_ptr->flags);
1457 #endif 1497 #endif
(...skipping 10 matching lines...) Expand all
1468 1508
1469 #if defined(PNG_READ_SWAP_SUPPORTED) 1509 #if defined(PNG_READ_SWAP_SUPPORTED)
1470 if (png_ptr->transformations & PNG_SWAP_BYTES) 1510 if (png_ptr->transformations & PNG_SWAP_BYTES)
1471 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1); 1511 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1472 #endif 1512 #endif
1473 1513
1474 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) 1514 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1475 if (png_ptr->transformations & PNG_USER_TRANSFORM) 1515 if (png_ptr->transformations & PNG_USER_TRANSFORM)
1476 { 1516 {
1477 if (png_ptr->read_user_transform_fn != NULL) 1517 if (png_ptr->read_user_transform_fn != NULL)
1478 (*(png_ptr->read_user_transform_fn)) /* user read transform function */ 1518 (*(png_ptr->read_user_transform_fn)) /* User read transform function */
1479 (png_ptr, /* png_ptr */ 1519 (png_ptr, /* png_ptr */
1480 &(png_ptr->row_info), /* row_info: */ 1520 &(png_ptr->row_info), /* row_info: */
1481 /* png_uint_32 width; width of row */ 1521 /* png_uint_32 width; width of row */
1482 /* png_uint_32 rowbytes; number of bytes in row */ 1522 /* png_uint_32 rowbytes; number of bytes in row */
1483 /* png_byte color_type; color type of pixels */ 1523 /* png_byte color_type; color type of pixels */
1484 /* png_byte bit_depth; bit depth of samples */ 1524 /* png_byte bit_depth; bit depth of samples */
1485 /* png_byte channels; number of channels (1-4) */ 1525 /* png_byte channels; number of channels (1-4) */
1486 /* png_byte pixel_depth; bits per pixel (depth*channels) */ 1526 /* png_byte pixel_depth; bits per pixel (depth*channels) */
1487 png_ptr->row_buf + 1); /* start of pixel data for row */ 1527 png_ptr->row_buf + 1); /* start of pixel data for row */
1488 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) 1528 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
1489 if (png_ptr->user_transform_depth) 1529 if (png_ptr->user_transform_depth)
1490 png_ptr->row_info.bit_depth = png_ptr->user_transform_depth; 1530 png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
1491 if (png_ptr->user_transform_channels) 1531 if (png_ptr->user_transform_channels)
1492 png_ptr->row_info.channels = png_ptr->user_transform_channels; 1532 png_ptr->row_info.channels = png_ptr->user_transform_channels;
1493 #endif 1533 #endif
1494 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth * 1534 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
1495 png_ptr->row_info.channels); 1535 png_ptr->row_info.channels);
1496 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, 1536 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
1497 png_ptr->row_info.width); 1537 png_ptr->row_info.width);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1535 shift = 0; 1575 shift = 0;
1536 sp--; 1576 sp--;
1537 } 1577 }
1538 else 1578 else
1539 shift++; 1579 shift++;
1540 1580
1541 dp--; 1581 dp--;
1542 } 1582 }
1543 break; 1583 break;
1544 } 1584 }
1585
1545 case 2: 1586 case 2:
1546 { 1587 {
1547 1588
1548 png_bytep sp = row + (png_size_t)((row_width - 1) >> 2); 1589 png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
1549 png_bytep dp = row + (png_size_t)row_width - 1; 1590 png_bytep dp = row + (png_size_t)row_width - 1;
1550 png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); 1591 png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
1551 for (i = 0; i < row_width; i++) 1592 for (i = 0; i < row_width; i++)
1552 { 1593 {
1553 *dp = (png_byte)((*sp >> shift) & 0x03); 1594 *dp = (png_byte)((*sp >> shift) & 0x03);
1554 if (shift == 6) 1595 if (shift == 6)
1555 { 1596 {
1556 shift = 0; 1597 shift = 0;
1557 sp--; 1598 sp--;
1558 } 1599 }
1559 else 1600 else
1560 shift += 2; 1601 shift += 2;
1561 1602
1562 dp--; 1603 dp--;
1563 } 1604 }
1564 break; 1605 break;
1565 } 1606 }
1607
1566 case 4: 1608 case 4:
1567 { 1609 {
1568 png_bytep sp = row + (png_size_t)((row_width - 1) >> 1); 1610 png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
1569 png_bytep dp = row + (png_size_t)row_width - 1; 1611 png_bytep dp = row + (png_size_t)row_width - 1;
1570 png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); 1612 png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
1571 for (i = 0; i < row_width; i++) 1613 for (i = 0; i < row_width; i++)
1572 { 1614 {
1573 *dp = (png_byte)((*sp >> shift) & 0x0f); 1615 *dp = (png_byte)((*sp >> shift) & 0x0f);
1574 if (shift == 4) 1616 if (shift == 4)
1575 { 1617 {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
1647 png_uint_32 i; 1689 png_uint_32 i;
1648 png_uint_32 istop = row_info->rowbytes; 1690 png_uint_32 istop = row_info->rowbytes;
1649 1691
1650 for (bp = row, i = 0; i < istop; i++) 1692 for (bp = row, i = 0; i < istop; i++)
1651 { 1693 {
1652 *bp >>= 1; 1694 *bp >>= 1;
1653 *bp++ &= 0x55; 1695 *bp++ &= 0x55;
1654 } 1696 }
1655 break; 1697 break;
1656 } 1698 }
1699
1657 case 4: 1700 case 4:
1658 { 1701 {
1659 png_bytep bp = row; 1702 png_bytep bp = row;
1660 png_uint_32 i; 1703 png_uint_32 i;
1661 png_uint_32 istop = row_info->rowbytes; 1704 png_uint_32 istop = row_info->rowbytes;
1662 png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) | 1705 png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
1663 (png_byte)((int)0xf >> shift[0])); 1706 (png_byte)((int)0xf >> shift[0]));
1664 1707
1665 for (i = 0; i < istop; i++) 1708 for (i = 0; i < istop; i++)
1666 { 1709 {
1667 *bp >>= shift[0]; 1710 *bp >>= shift[0];
1668 *bp++ &= mask; 1711 *bp++ &= mask;
1669 } 1712 }
1670 break; 1713 break;
1671 } 1714 }
1715
1672 case 8: 1716 case 8:
1673 { 1717 {
1674 png_bytep bp = row; 1718 png_bytep bp = row;
1675 png_uint_32 i; 1719 png_uint_32 i;
1676 png_uint_32 istop = row_width * channels; 1720 png_uint_32 istop = row_width * channels;
1677 1721
1678 for (i = 0; i < istop; i++) 1722 for (i = 0; i < istop; i++)
1679 { 1723 {
1680 *bp++ >>= shift[i%channels]; 1724 *bp++ >>= shift[i%channels];
1681 } 1725 }
1682 break; 1726 break;
1683 } 1727 }
1728
1684 case 16: 1729 case 16:
1685 { 1730 {
1686 png_bytep bp = row; 1731 png_bytep bp = row;
1687 png_uint_32 i; 1732 png_uint_32 i;
1688 png_uint_32 istop = channels * row_width; 1733 png_uint_32 istop = channels * row_width;
1689 1734
1690 for (i = 0; i < istop; i++) 1735 for (i = 0; i < istop; i++)
1691 { 1736 {
1692 value = (png_uint_16)((*bp << 8) + *(bp + 1)); 1737 value = (png_uint_16)((*bp << 8) + *(bp + 1));
1693 value >>= shift[i%channels]; 1738 value >>= shift[i%channels];
1694 *bp++ = (png_byte)(value >> 8); 1739 *bp++ = (png_byte)(value >> 8);
1695 *bp++ = (png_byte)(value & 0xff); 1740 *bp++ = (png_byte)(value & 0xff);
1696 } 1741 }
1697 break; 1742 break;
1698 } 1743 }
1699 } 1744 }
1700 } 1745 }
1701 } 1746 }
1702 #endif 1747 #endif
1703 1748
1704 #if defined(PNG_READ_16_TO_8_SUPPORTED) 1749 #if defined(PNG_READ_16_TO_8_SUPPORTED)
1705 /* chop rows of bit depth 16 down to 8 */ 1750 /* Chop rows of bit depth 16 down to 8 */
1706 void /* PRIVATE */ 1751 void /* PRIVATE */
1707 png_do_chop(png_row_infop row_info, png_bytep row) 1752 png_do_chop(png_row_infop row_info, png_bytep row)
1708 { 1753 {
1709 png_debug(1, "in png_do_chop"); 1754 png_debug(1, "in png_do_chop");
1710 #if defined(PNG_USELESS_TESTS_SUPPORTED) 1755 #if defined(PNG_USELESS_TESTS_SUPPORTED)
1711 if (row != NULL && row_info != NULL && row_info->bit_depth == 16) 1756 if (row != NULL && row_info != NULL && row_info->bit_depth == 16)
1712 #else 1757 #else
1713 if (row_info->bit_depth == 16) 1758 if (row_info->bit_depth == 16)
1714 #endif 1759 #endif
1715 { 1760 {
1716 png_bytep sp = row; 1761 png_bytep sp = row;
1717 png_bytep dp = row; 1762 png_bytep dp = row;
1718 png_uint_32 i; 1763 png_uint_32 i;
1719 png_uint_32 istop = row_info->width * row_info->channels; 1764 png_uint_32 istop = row_info->width * row_info->channels;
1720 1765
1721 for (i = 0; i<istop; i++, sp += 2, dp++) 1766 for (i = 0; i<istop; i++, sp += 2, dp++)
1722 { 1767 {
1723 #if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED) 1768 #if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED)
1724 /* This does a more accurate scaling of the 16-bit color 1769 /* This does a more accurate scaling of the 16-bit color
1725 * value, rather than a simple low-byte truncation. 1770 * value, rather than a simple low-byte truncation.
1726 * 1771 *
1727 * What the ideal calculation should be: 1772 * What the ideal calculation should be:
1728 * *dp = (((((png_uint_32)(*sp) << 8) | 1773 * *dp = (((((png_uint_32)(*sp) << 8) |
1729 * (png_uint_32)(*(sp + 1))) * 255 + 127) / (png_uint_32)65535L; 1774 * (png_uint_32)(*(sp + 1))) * 255 + 127)
1775 * / (png_uint_32)65535L;
1730 * 1776 *
1731 * GRR: no, I think this is what it really should be: 1777 * GRR: no, I think this is what it really should be:
1732 * *dp = (((((png_uint_32)(*sp) << 8) | 1778 * *dp = (((((png_uint_32)(*sp) << 8) |
1733 * (png_uint_32)(*(sp + 1))) + 128L) / (png_uint_32)257L; 1779 * (png_uint_32)(*(sp + 1))) + 128L)
1780 * / (png_uint_32)257L;
1734 * 1781 *
1735 * GRR: here's the exact calculation with shifts: 1782 * GRR: here's the exact calculation with shifts:
1736 * temp = (((png_uint_32)(*sp) << 8) | (png_uint_32)(*(sp + 1))) + 128L; 1783 * temp = (((png_uint_32)(*sp) << 8) |
1784 * (png_uint_32)(*(sp + 1))) + 128L;
1737 * *dp = (temp - (temp >> 8)) >> 8; 1785 * *dp = (temp - (temp >> 8)) >> 8;
1738 * 1786 *
1739 * Approximate calculation with shift/add instead of multiply/divide: 1787 * Approximate calculation with shift/add instead of multiply/divide:
1740 * *dp = ((((png_uint_32)(*sp) << 8) | 1788 * *dp = ((((png_uint_32)(*sp) << 8) |
1741 * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8; 1789 * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
1742 * 1790 *
1743 * What we actually do to avoid extra shifting and conversion: 1791 * What we actually do to avoid extra shifting and conversion:
1744 */ 1792 */
1745 1793
1746 *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0); 1794 *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
2116 row_info->channels = 4; 2164 row_info->channels = 4;
2117 row_info->pixel_depth = 64; 2165 row_info->pixel_depth = 64;
2118 row_info->rowbytes = row_width * 8; 2166 row_info->rowbytes = row_width * 8;
2119 } 2167 }
2120 } 2168 }
2121 } /* COLOR_TYPE == RGB */ 2169 } /* COLOR_TYPE == RGB */
2122 } 2170 }
2123 #endif 2171 #endif
2124 2172
2125 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) 2173 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
2126 /* expand grayscale files to RGB, with or without alpha */ 2174 /* Expand grayscale files to RGB, with or without alpha */
2127 void /* PRIVATE */ 2175 void /* PRIVATE */
2128 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) 2176 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
2129 { 2177 {
2130 png_uint_32 i; 2178 png_uint_32 i;
2131 png_uint_32 row_width = row_info->width; 2179 png_uint_32 row_width = row_info->width;
2132 2180
2133 png_debug(1, "in png_do_gray_to_rgb"); 2181 png_debug(1, "in png_do_gray_to_rgb");
2134 if (row_info->bit_depth >= 8 && 2182 if (row_info->bit_depth >= 8 &&
2135 #if defined(PNG_USELESS_TESTS_SUPPORTED) 2183 #if defined(PNG_USELESS_TESTS_SUPPORTED)
2136 row != NULL && row_info != NULL && 2184 row != NULL && row_info != NULL &&
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
2199 row_info->channels += (png_byte)2; 2247 row_info->channels += (png_byte)2;
2200 row_info->color_type |= PNG_COLOR_MASK_COLOR; 2248 row_info->color_type |= PNG_COLOR_MASK_COLOR;
2201 row_info->pixel_depth = (png_byte)(row_info->channels * 2249 row_info->pixel_depth = (png_byte)(row_info->channels *
2202 row_info->bit_depth); 2250 row_info->bit_depth);
2203 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); 2251 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2204 } 2252 }
2205 } 2253 }
2206 #endif 2254 #endif
2207 2255
2208 #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) 2256 #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
2209 /* reduce RGB files to grayscale, with or without alpha 2257 /* Reduce RGB files to grayscale, with or without alpha
2210 * using the equation given in Poynton's ColorFAQ at 2258 * using the equation given in Poynton's ColorFAQ at
2211 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008) 2259 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008)
2212 * New link: 2260 * New link:
2213 * <http://www.poynton.com/notes/colour_and_gamma/> 2261 * <http://www.poynton.com/notes/colour_and_gamma/>
2214 * Charles Poynton poynton at poynton.com 2262 * Charles Poynton poynton at poynton.com
2215 * 2263 *
2216 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B 2264 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2217 * 2265 *
2218 * We approximate this with 2266 * We approximate this with
2219 * 2267 *
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
2477 png_debug(1, "in png_do_build_grayscale_palette"); 2525 png_debug(1, "in png_do_build_grayscale_palette");
2478 if (palette == NULL) 2526 if (palette == NULL)
2479 return; 2527 return;
2480 2528
2481 switch (bit_depth) 2529 switch (bit_depth)
2482 { 2530 {
2483 case 1: 2531 case 1:
2484 num_palette = 2; 2532 num_palette = 2;
2485 color_inc = 0xff; 2533 color_inc = 0xff;
2486 break; 2534 break;
2535
2487 case 2: 2536 case 2:
2488 num_palette = 4; 2537 num_palette = 4;
2489 color_inc = 0x55; 2538 color_inc = 0x55;
2490 break; 2539 break;
2540
2491 case 4: 2541 case 4:
2492 num_palette = 16; 2542 num_palette = 16;
2493 color_inc = 0x11; 2543 color_inc = 0x11;
2494 break; 2544 break;
2545
2495 case 8: 2546 case 8:
2496 num_palette = 256; 2547 num_palette = 256;
2497 color_inc = 1; 2548 color_inc = 1;
2498 break; 2549 break;
2550
2499 default: 2551 default:
2500 num_palette = 0; 2552 num_palette = 0;
2501 color_inc = 0; 2553 color_inc = 0;
2502 break; 2554 break;
2503 } 2555 }
2504 2556
2505 for (i = 0, v = 0; i < num_palette; i++, v += color_inc) 2557 for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
2506 { 2558 {
2507 palette[i].red = (png_byte)v; 2559 palette[i].red = (png_byte)v;
2508 palette[i].green = (png_byte)v; 2560 palette[i].green = (png_byte)v;
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
2666 { 2718 {
2667 png_composite(palette[i].red, png_ptr->palette[i].red, 2719 png_composite(palette[i].red, png_ptr->palette[i].red,
2668 png_ptr->trans[i], back.red); 2720 png_ptr->trans[i], back.red);
2669 png_composite(palette[i].green, png_ptr->palette[i].green, 2721 png_composite(palette[i].green, png_ptr->palette[i].green,
2670 png_ptr->trans[i], back.green); 2722 png_ptr->trans[i], back.green);
2671 png_composite(palette[i].blue, png_ptr->palette[i].blue, 2723 png_composite(palette[i].blue, png_ptr->palette[i].blue,
2672 png_ptr->trans[i], back.blue); 2724 png_ptr->trans[i], back.blue);
2673 } 2725 }
2674 } 2726 }
2675 } 2727 }
2676 else /* assume grayscale palette (what else could it be?) */ 2728 else /* Assume grayscale palette (what else could it be?) */
2677 { 2729 {
2678 int i; 2730 int i;
2679 2731
2680 for (i = 0; i < num_palette; i++) 2732 for (i = 0; i < num_palette; i++)
2681 { 2733 {
2682 if (i == (png_byte)png_ptr->trans_values.gray) 2734 if (i == (png_byte)png_ptr->trans_values.gray)
2683 { 2735 {
2684 palette[i].red = (png_byte)png_ptr->background.red; 2736 palette[i].red = (png_byte)png_ptr->background.red;
2685 palette[i].green = (png_byte)png_ptr->background.green; 2737 palette[i].green = (png_byte)png_ptr->background.green;
2686 palette[i].blue = (png_byte)png_ptr->background.blue; 2738 palette[i].blue = (png_byte)png_ptr->background.blue;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
2742 if (!shift) 2794 if (!shift)
2743 { 2795 {
2744 shift = 7; 2796 shift = 7;
2745 sp++; 2797 sp++;
2746 } 2798 }
2747 else 2799 else
2748 shift--; 2800 shift--;
2749 } 2801 }
2750 break; 2802 break;
2751 } 2803 }
2804
2752 case 2: 2805 case 2:
2753 { 2806 {
2754 #if defined(PNG_READ_GAMMA_SUPPORTED) 2807 #if defined(PNG_READ_GAMMA_SUPPORTED)
2755 if (gamma_table != NULL) 2808 if (gamma_table != NULL)
2756 { 2809 {
2757 sp = row; 2810 sp = row;
2758 shift = 6; 2811 shift = 6;
2759 for (i = 0; i < row_width; i++) 2812 for (i = 0; i < row_width; i++)
2760 { 2813 {
2761 if ((png_uint_16)((*sp >> shift) & 0x03) 2814 if ((png_uint_16)((*sp >> shift) & 0x03)
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2798 { 2851 {
2799 shift = 6; 2852 shift = 6;
2800 sp++; 2853 sp++;
2801 } 2854 }
2802 else 2855 else
2803 shift -= 2; 2856 shift -= 2;
2804 } 2857 }
2805 } 2858 }
2806 break; 2859 break;
2807 } 2860 }
2861
2808 case 4: 2862 case 4:
2809 { 2863 {
2810 #if defined(PNG_READ_GAMMA_SUPPORTED) 2864 #if defined(PNG_READ_GAMMA_SUPPORTED)
2811 if (gamma_table != NULL) 2865 if (gamma_table != NULL)
2812 { 2866 {
2813 sp = row; 2867 sp = row;
2814 shift = 4; 2868 shift = 4;
2815 for (i = 0; i < row_width; i++) 2869 for (i = 0; i < row_width; i++)
2816 { 2870 {
2817 if ((png_uint_16)((*sp >> shift) & 0x0f) 2871 if ((png_uint_16)((*sp >> shift) & 0x0f)
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2854 { 2908 {
2855 shift = 4; 2909 shift = 4;
2856 sp++; 2910 sp++;
2857 } 2911 }
2858 else 2912 else
2859 shift -= 4; 2913 shift -= 4;
2860 } 2914 }
2861 } 2915 }
2862 break; 2916 break;
2863 } 2917 }
2918
2864 case 8: 2919 case 8:
2865 { 2920 {
2866 #if defined(PNG_READ_GAMMA_SUPPORTED) 2921 #if defined(PNG_READ_GAMMA_SUPPORTED)
2867 if (gamma_table != NULL) 2922 if (gamma_table != NULL)
2868 { 2923 {
2869 sp = row; 2924 sp = row;
2870 for (i = 0; i < row_width; i++, sp++) 2925 for (i = 0; i < row_width; i++, sp++)
2871 { 2926 {
2872 if (*sp == trans_values->gray) 2927 if (*sp == trans_values->gray)
2873 { 2928 {
(...skipping 12 matching lines...) Expand all
2886 for (i = 0; i < row_width; i++, sp++) 2941 for (i = 0; i < row_width; i++, sp++)
2887 { 2942 {
2888 if (*sp == trans_values->gray) 2943 if (*sp == trans_values->gray)
2889 { 2944 {
2890 *sp = (png_byte)background->gray; 2945 *sp = (png_byte)background->gray;
2891 } 2946 }
2892 } 2947 }
2893 } 2948 }
2894 break; 2949 break;
2895 } 2950 }
2951
2896 case 16: 2952 case 16:
2897 { 2953 {
2898 #if defined(PNG_READ_GAMMA_SUPPORTED) 2954 #if defined(PNG_READ_GAMMA_SUPPORTED)
2899 if (gamma_16 != NULL) 2955 if (gamma_16 != NULL)
2900 { 2956 {
2901 sp = row; 2957 sp = row;
2902 for (i = 0; i < row_width; i++, sp += 2) 2958 for (i = 0; i < row_width; i++, sp += 2)
2903 { 2959 {
2904 png_uint_16 v; 2960 png_uint_16 v;
2905 2961
2906 v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); 2962 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
2907 if (v == trans_values->gray) 2963 if (v == trans_values->gray)
2908 { 2964 {
2909 /* background is already in screen gamma */ 2965 /* Background is already in screen gamma */
2910 *sp = (png_byte)((background->gray >> 8) & 0xff); 2966 *sp = (png_byte)((background->gray >> 8) & 0xff);
2911 *(sp + 1) = (png_byte)(background->gray & 0xff); 2967 *(sp + 1) = (png_byte)(background->gray & 0xff);
2912 } 2968 }
2913 else 2969 else
2914 { 2970 {
2915 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; 2971 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
2916 *sp = (png_byte)((v >> 8) & 0xff); 2972 *sp = (png_byte)((v >> 8) & 0xff);
2917 *(sp + 1) = (png_byte)(v & 0xff); 2973 *(sp + 1) = (png_byte)(v & 0xff);
2918 } 2974 }
2919 } 2975 }
(...skipping 12 matching lines...) Expand all
2932 *sp = (png_byte)((background->gray >> 8) & 0xff); 2988 *sp = (png_byte)((background->gray >> 8) & 0xff);
2933 *(sp + 1) = (png_byte)(background->gray & 0xff); 2989 *(sp + 1) = (png_byte)(background->gray & 0xff);
2934 } 2990 }
2935 } 2991 }
2936 } 2992 }
2937 break; 2993 break;
2938 } 2994 }
2939 } 2995 }
2940 break; 2996 break;
2941 } 2997 }
2998
2942 case PNG_COLOR_TYPE_RGB: 2999 case PNG_COLOR_TYPE_RGB:
2943 { 3000 {
2944 if (row_info->bit_depth == 8) 3001 if (row_info->bit_depth == 8)
2945 { 3002 {
2946 #if defined(PNG_READ_GAMMA_SUPPORTED) 3003 #if defined(PNG_READ_GAMMA_SUPPORTED)
2947 if (gamma_table != NULL) 3004 if (gamma_table != NULL)
2948 { 3005 {
2949 sp = row; 3006 sp = row;
2950 for (i = 0; i < row_width; i++, sp += 3) 3007 for (i = 0; i < row_width; i++, sp += 3)
2951 { 3008 {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
2989 { 3046 {
2990 sp = row; 3047 sp = row;
2991 for (i = 0; i < row_width; i++, sp += 6) 3048 for (i = 0; i < row_width; i++, sp += 6)
2992 { 3049 {
2993 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); 3050 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
2994 png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); 3051 png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
2995 png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5)); 3052 png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
2996 if (r == trans_values->red && g == trans_values->green && 3053 if (r == trans_values->red && g == trans_values->green &&
2997 b == trans_values->blue) 3054 b == trans_values->blue)
2998 { 3055 {
2999 /* background is already in screen gamma */ 3056 /* Background is already in screen gamma */
3000 *sp = (png_byte)((background->red >> 8) & 0xff); 3057 *sp = (png_byte)((background->red >> 8) & 0xff);
3001 *(sp + 1) = (png_byte)(background->red & 0xff); 3058 *(sp + 1) = (png_byte)(background->red & 0xff);
3002 *(sp + 2) = (png_byte)((background->green >> 8) & 0xff); 3059 *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3003 *(sp + 3) = (png_byte)(background->green & 0xff); 3060 *(sp + 3) = (png_byte)(background->green & 0xff);
3004 *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff); 3061 *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3005 *(sp + 5) = (png_byte)(background->blue & 0xff); 3062 *(sp + 5) = (png_byte)(background->blue & 0xff);
3006 } 3063 }
3007 else 3064 else
3008 { 3065 {
3009 png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; 3066 png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
(...skipping 26 matching lines...) Expand all
3036 *(sp + 2) = (png_byte)((background->green >> 8) & 0xff); 3093 *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3037 *(sp + 3) = (png_byte)(background->green & 0xff); 3094 *(sp + 3) = (png_byte)(background->green & 0xff);
3038 *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff); 3095 *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3039 *(sp + 5) = (png_byte)(background->blue & 0xff); 3096 *(sp + 5) = (png_byte)(background->blue & 0xff);
3040 } 3097 }
3041 } 3098 }
3042 } 3099 }
3043 } 3100 }
3044 break; 3101 break;
3045 } 3102 }
3103
3046 case PNG_COLOR_TYPE_GRAY_ALPHA: 3104 case PNG_COLOR_TYPE_GRAY_ALPHA:
3047 { 3105 {
3048 if (row_info->bit_depth == 8) 3106 if (row_info->bit_depth == 8)
3049 { 3107 {
3050 #if defined(PNG_READ_GAMMA_SUPPORTED) 3108 #if defined(PNG_READ_GAMMA_SUPPORTED)
3051 if (gamma_to_1 != NULL && gamma_from_1 != NULL && 3109 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3052 gamma_table != NULL) 3110 gamma_table != NULL)
3053 { 3111 {
3054 sp = row; 3112 sp = row;
3055 dp = row; 3113 dp = row;
3056 for (i = 0; i < row_width; i++, sp += 2, dp++) 3114 for (i = 0; i < row_width; i++, sp += 2, dp++)
3057 { 3115 {
3058 png_uint_16 a = *(sp + 1); 3116 png_uint_16 a = *(sp + 1);
3059 3117
3060 if (a == 0xff) 3118 if (a == 0xff)
3061 { 3119 {
3062 *dp = gamma_table[*sp]; 3120 *dp = gamma_table[*sp];
3063 } 3121 }
3064 else if (a == 0) 3122 else if (a == 0)
3065 { 3123 {
3066 /* background is already in screen gamma */ 3124 /* Background is already in screen gamma */
3067 *dp = (png_byte)background->gray; 3125 *dp = (png_byte)background->gray;
3068 } 3126 }
3069 else 3127 else
3070 { 3128 {
3071 png_byte v, w; 3129 png_byte v, w;
3072 3130
3073 v = gamma_to_1[*sp]; 3131 v = gamma_to_1[*sp];
3074 png_composite(w, v, a, background_1->gray); 3132 png_composite(w, v, a, background_1->gray);
3075 *dp = gamma_from_1[w]; 3133 *dp = gamma_from_1[w];
3076 } 3134 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
3123 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; 3181 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3124 *dp = (png_byte)((v >> 8) & 0xff); 3182 *dp = (png_byte)((v >> 8) & 0xff);
3125 *(dp + 1) = (png_byte)(v & 0xff); 3183 *(dp + 1) = (png_byte)(v & 0xff);
3126 } 3184 }
3127 #if defined(PNG_READ_GAMMA_SUPPORTED) 3185 #if defined(PNG_READ_GAMMA_SUPPORTED)
3128 else if (a == 0) 3186 else if (a == 0)
3129 #else 3187 #else
3130 else 3188 else
3131 #endif 3189 #endif
3132 { 3190 {
3133 /* background is already in screen gamma */ 3191 /* Background is already in screen gamma */
3134 *dp = (png_byte)((background->gray >> 8) & 0xff); 3192 *dp = (png_byte)((background->gray >> 8) & 0xff);
3135 *(dp + 1) = (png_byte)(background->gray & 0xff); 3193 *(dp + 1) = (png_byte)(background->gray & 0xff);
3136 } 3194 }
3137 #if defined(PNG_READ_GAMMA_SUPPORTED) 3195 #if defined(PNG_READ_GAMMA_SUPPORTED)
3138 else 3196 else
3139 { 3197 {
3140 png_uint_16 g, v, w; 3198 png_uint_16 g, v, w;
3141 3199
3142 g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; 3200 g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3143 png_composite_16(v, g, a, background_1->gray); 3201 png_composite_16(v, g, a, background_1->gray);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
3178 png_composite_16(v, g, a, background_1->gray); 3236 png_composite_16(v, g, a, background_1->gray);
3179 *dp = (png_byte)((v >> 8) & 0xff); 3237 *dp = (png_byte)((v >> 8) & 0xff);
3180 *(dp + 1) = (png_byte)(v & 0xff); 3238 *(dp + 1) = (png_byte)(v & 0xff);
3181 } 3239 }
3182 #endif 3240 #endif
3183 } 3241 }
3184 } 3242 }
3185 } 3243 }
3186 break; 3244 break;
3187 } 3245 }
3246
3188 case PNG_COLOR_TYPE_RGB_ALPHA: 3247 case PNG_COLOR_TYPE_RGB_ALPHA:
3189 { 3248 {
3190 if (row_info->bit_depth == 8) 3249 if (row_info->bit_depth == 8)
3191 { 3250 {
3192 #if defined(PNG_READ_GAMMA_SUPPORTED) 3251 #if defined(PNG_READ_GAMMA_SUPPORTED)
3193 if (gamma_to_1 != NULL && gamma_from_1 != NULL && 3252 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3194 gamma_table != NULL) 3253 gamma_table != NULL)
3195 { 3254 {
3196 sp = row; 3255 sp = row;
3197 dp = row; 3256 dp = row;
3198 for (i = 0; i < row_width; i++, sp += 4, dp += 3) 3257 for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3199 { 3258 {
3200 png_byte a = *(sp + 3); 3259 png_byte a = *(sp + 3);
3201 3260
3202 if (a == 0xff) 3261 if (a == 0xff)
3203 { 3262 {
3204 *dp = gamma_table[*sp]; 3263 *dp = gamma_table[*sp];
3205 *(dp + 1) = gamma_table[*(sp + 1)]; 3264 *(dp + 1) = gamma_table[*(sp + 1)];
3206 *(dp + 2) = gamma_table[*(sp + 2)]; 3265 *(dp + 2) = gamma_table[*(sp + 2)];
3207 } 3266 }
3208 else if (a == 0) 3267 else if (a == 0)
3209 { 3268 {
3210 /* background is already in screen gamma */ 3269 /* Background is already in screen gamma */
3211 *dp = (png_byte)background->red; 3270 *dp = (png_byte)background->red;
3212 *(dp + 1) = (png_byte)background->green; 3271 *(dp + 1) = (png_byte)background->green;
3213 *(dp + 2) = (png_byte)background->blue; 3272 *(dp + 2) = (png_byte)background->blue;
3214 } 3273 }
3215 else 3274 else
3216 { 3275 {
3217 png_byte v, w; 3276 png_byte v, w;
3218 3277
3219 v = gamma_to_1[*sp]; 3278 v = gamma_to_1[*sp];
3220 png_composite(w, v, a, background_1->red); 3279 png_composite(w, v, a, background_1->red);
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
3281 *(dp + 1) = (png_byte)(v & 0xff); 3340 *(dp + 1) = (png_byte)(v & 0xff);
3282 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; 3341 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3283 *(dp + 2) = (png_byte)((v >> 8) & 0xff); 3342 *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3284 *(dp + 3) = (png_byte)(v & 0xff); 3343 *(dp + 3) = (png_byte)(v & 0xff);
3285 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; 3344 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3286 *(dp + 4) = (png_byte)((v >> 8) & 0xff); 3345 *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3287 *(dp + 5) = (png_byte)(v & 0xff); 3346 *(dp + 5) = (png_byte)(v & 0xff);
3288 } 3347 }
3289 else if (a == 0) 3348 else if (a == 0)
3290 { 3349 {
3291 /* background is already in screen gamma */ 3350 /* Background is already in screen gamma */
3292 *dp = (png_byte)((background->red >> 8) & 0xff); 3351 *dp = (png_byte)((background->red >> 8) & 0xff);
3293 *(dp + 1) = (png_byte)(background->red & 0xff); 3352 *(dp + 1) = (png_byte)(background->red & 0xff);
3294 *(dp + 2) = (png_byte)((background->green >> 8) & 0xff); 3353 *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3295 *(dp + 3) = (png_byte)(background->green & 0xff); 3354 *(dp + 3) = (png_byte)(background->green & 0xff);
3296 *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff); 3355 *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3297 *(dp + 5) = (png_byte)(background->blue & 0xff); 3356 *(dp + 5) = (png_byte)(background->blue & 0xff);
3298 } 3357 }
3299 else 3358 else
3300 { 3359 {
3301 png_uint_16 v, w, x; 3360 png_uint_16 v, w, x;
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
3436 *(sp + 1) = (png_byte)(v & 0xff); 3495 *(sp + 1) = (png_byte)(v & 0xff);
3437 sp += 2; 3496 sp += 2;
3438 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; 3497 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3439 *sp = (png_byte)((v >> 8) & 0xff); 3498 *sp = (png_byte)((v >> 8) & 0xff);
3440 *(sp + 1) = (png_byte)(v & 0xff); 3499 *(sp + 1) = (png_byte)(v & 0xff);
3441 sp += 2; 3500 sp += 2;
3442 } 3501 }
3443 } 3502 }
3444 break; 3503 break;
3445 } 3504 }
3505
3446 case PNG_COLOR_TYPE_RGB_ALPHA: 3506 case PNG_COLOR_TYPE_RGB_ALPHA:
3447 { 3507 {
3448 if (row_info->bit_depth == 8) 3508 if (row_info->bit_depth == 8)
3449 { 3509 {
3450 sp = row; 3510 sp = row;
3451 for (i = 0; i < row_width; i++) 3511 for (i = 0; i < row_width; i++)
3452 { 3512 {
3453 *sp = gamma_table[*sp]; 3513 *sp = gamma_table[*sp];
3454 sp++; 3514 sp++;
3455 *sp = gamma_table[*sp]; 3515 *sp = gamma_table[*sp];
(...skipping 17 matching lines...) Expand all
3473 *(sp + 1) = (png_byte)(v & 0xff); 3533 *(sp + 1) = (png_byte)(v & 0xff);
3474 sp += 2; 3534 sp += 2;
3475 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; 3535 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3476 *sp = (png_byte)((v >> 8) & 0xff); 3536 *sp = (png_byte)((v >> 8) & 0xff);
3477 *(sp + 1) = (png_byte)(v & 0xff); 3537 *(sp + 1) = (png_byte)(v & 0xff);
3478 sp += 4; 3538 sp += 4;
3479 } 3539 }
3480 } 3540 }
3481 break; 3541 break;
3482 } 3542 }
3543
3483 case PNG_COLOR_TYPE_GRAY_ALPHA: 3544 case PNG_COLOR_TYPE_GRAY_ALPHA:
3484 { 3545 {
3485 if (row_info->bit_depth == 8) 3546 if (row_info->bit_depth == 8)
3486 { 3547 {
3487 sp = row; 3548 sp = row;
3488 for (i = 0; i < row_width; i++) 3549 for (i = 0; i < row_width; i++)
3489 { 3550 {
3490 *sp = gamma_table[*sp]; 3551 *sp = gamma_table[*sp];
3491 sp += 2; 3552 sp += 2;
3492 } 3553 }
3493 } 3554 }
3494 else /* if (row_info->bit_depth == 16) */ 3555 else /* if (row_info->bit_depth == 16) */
3495 { 3556 {
3496 sp = row; 3557 sp = row;
3497 for (i = 0; i < row_width; i++) 3558 for (i = 0; i < row_width; i++)
3498 { 3559 {
3499 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; 3560 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3500 *sp = (png_byte)((v >> 8) & 0xff); 3561 *sp = (png_byte)((v >> 8) & 0xff);
3501 *(sp + 1) = (png_byte)(v & 0xff); 3562 *(sp + 1) = (png_byte)(v & 0xff);
3502 sp += 4; 3563 sp += 4;
3503 } 3564 }
3504 } 3565 }
3505 break; 3566 break;
3506 } 3567 }
3568
3507 case PNG_COLOR_TYPE_GRAY: 3569 case PNG_COLOR_TYPE_GRAY:
3508 { 3570 {
3509 if (row_info->bit_depth == 2) 3571 if (row_info->bit_depth == 2)
3510 { 3572 {
3511 sp = row; 3573 sp = row;
3512 for (i = 0; i < row_width; i += 4) 3574 for (i = 0; i < row_width; i += 4)
3513 { 3575 {
3514 int a = *sp & 0xc0; 3576 int a = *sp & 0xc0;
3515 int b = *sp & 0x30; 3577 int b = *sp & 0x30;
3516 int c = *sp & 0x0c; 3578 int c = *sp & 0x0c;
3517 int d = *sp & 0x03; 3579 int d = *sp & 0x03;
3518 3580
3519 *sp = (png_byte)( 3581 *sp = (png_byte)(
3520 ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0) | 3582 ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0) |
3521 ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30) | 3583 ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30) |
3522 ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c) | 3584 ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c) |
3523 ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) )); 3585 ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
3524 sp++; 3586 sp++;
3525 } 3587 }
3526 } 3588 }
3589
3527 if (row_info->bit_depth == 4) 3590 if (row_info->bit_depth == 4)
3528 { 3591 {
3529 sp = row; 3592 sp = row;
3530 for (i = 0; i < row_width; i += 2) 3593 for (i = 0; i < row_width; i += 2)
3531 { 3594 {
3532 int msb = *sp & 0xf0; 3595 int msb = *sp & 0xf0;
3533 int lsb = *sp & 0x0f; 3596 int lsb = *sp & 0x0f;
3534 3597
3535 *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0) 3598 *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
3536 | (((int)gamma_table[(lsb << 4) | lsb]) >> 4)); 3599 | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
3537 sp++; 3600 sp++;
3538 } 3601 }
3539 } 3602 }
3603
3540 else if (row_info->bit_depth == 8) 3604 else if (row_info->bit_depth == 8)
3541 { 3605 {
3542 sp = row; 3606 sp = row;
3543 for (i = 0; i < row_width; i++) 3607 for (i = 0; i < row_width; i++)
3544 { 3608 {
3545 *sp = gamma_table[*sp]; 3609 *sp = gamma_table[*sp];
3546 sp++; 3610 sp++;
3547 } 3611 }
3548 } 3612 }
3613
3549 else if (row_info->bit_depth == 16) 3614 else if (row_info->bit_depth == 16)
3550 { 3615 {
3551 sp = row; 3616 sp = row;
3552 for (i = 0; i < row_width; i++) 3617 for (i = 0; i < row_width; i++)
3553 { 3618 {
3554 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; 3619 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3555 *sp = (png_byte)((v >> 8) & 0xff); 3620 *sp = (png_byte)((v >> 8) & 0xff);
3556 *(sp + 1) = (png_byte)(v & 0xff); 3621 *(sp + 1) = (png_byte)(v & 0xff);
3557 sp += 2; 3622 sp += 2;
3558 } 3623 }
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
3604 shift = 0; 3669 shift = 0;
3605 sp--; 3670 sp--;
3606 } 3671 }
3607 else 3672 else
3608 shift++; 3673 shift++;
3609 3674
3610 dp--; 3675 dp--;
3611 } 3676 }
3612 break; 3677 break;
3613 } 3678 }
3679
3614 case 2: 3680 case 2:
3615 { 3681 {
3616 sp = row + (png_size_t)((row_width - 1) >> 2); 3682 sp = row + (png_size_t)((row_width - 1) >> 2);
3617 dp = row + (png_size_t)row_width - 1; 3683 dp = row + (png_size_t)row_width - 1;
3618 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); 3684 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3619 for (i = 0; i < row_width; i++) 3685 for (i = 0; i < row_width; i++)
3620 { 3686 {
3621 value = (*sp >> shift) & 0x03; 3687 value = (*sp >> shift) & 0x03;
3622 *dp = (png_byte)value; 3688 *dp = (png_byte)value;
3623 if (shift == 6) 3689 if (shift == 6)
3624 { 3690 {
3625 shift = 0; 3691 shift = 0;
3626 sp--; 3692 sp--;
3627 } 3693 }
3628 else 3694 else
3629 shift += 2; 3695 shift += 2;
3630 3696
3631 dp--; 3697 dp--;
3632 } 3698 }
3633 break; 3699 break;
3634 } 3700 }
3701
3635 case 4: 3702 case 4:
3636 { 3703 {
3637 sp = row + (png_size_t)((row_width - 1) >> 1); 3704 sp = row + (png_size_t)((row_width - 1) >> 1);
3638 dp = row + (png_size_t)row_width - 1; 3705 dp = row + (png_size_t)row_width - 1;
3639 shift = (int)((row_width & 0x01) << 2); 3706 shift = (int)((row_width & 0x01) << 2);
3640 for (i = 0; i < row_width; i++) 3707 for (i = 0; i < row_width; i++)
3641 { 3708 {
3642 value = (*sp >> shift) & 0x0f; 3709 value = (*sp >> shift) & 0x0f;
3643 *dp = (png_byte)value; 3710 *dp = (png_byte)value;
3644 if (shift == 4) 3711 if (shift == 4)
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
3689 sp = row + (png_size_t)row_width - 1; 3756 sp = row + (png_size_t)row_width - 1;
3690 dp = row + (png_size_t)(row_width * 3) - 1; 3757 dp = row + (png_size_t)(row_width * 3) - 1;
3691 3758
3692 for (i = 0; i < row_width; i++) 3759 for (i = 0; i < row_width; i++)
3693 { 3760 {
3694 *dp-- = palette[*sp].blue; 3761 *dp-- = palette[*sp].blue;
3695 *dp-- = palette[*sp].green; 3762 *dp-- = palette[*sp].green;
3696 *dp-- = palette[*sp].red; 3763 *dp-- = palette[*sp].red;
3697 sp--; 3764 sp--;
3698 } 3765 }
3766
3699 row_info->bit_depth = 8; 3767 row_info->bit_depth = 8;
3700 row_info->pixel_depth = 24; 3768 row_info->pixel_depth = 24;
3701 row_info->rowbytes = row_width * 3; 3769 row_info->rowbytes = row_width * 3;
3702 row_info->color_type = 2; 3770 row_info->color_type = 2;
3703 row_info->channels = 3; 3771 row_info->channels = 3;
3704 } 3772 }
3705 break; 3773 break;
3706 } 3774 }
3707 } 3775 }
3708 } 3776 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
3750 shift = 0; 3818 shift = 0;
3751 sp--; 3819 sp--;
3752 } 3820 }
3753 else 3821 else
3754 shift++; 3822 shift++;
3755 3823
3756 dp--; 3824 dp--;
3757 } 3825 }
3758 break; 3826 break;
3759 } 3827 }
3828
3760 case 2: 3829 case 2:
3761 { 3830 {
3762 gray = (png_uint_16)((gray&0x03)*0x55); 3831 gray = (png_uint_16)((gray&0x03)*0x55);
3763 sp = row + (png_size_t)((row_width - 1) >> 2); 3832 sp = row + (png_size_t)((row_width - 1) >> 2);
3764 dp = row + (png_size_t)row_width - 1; 3833 dp = row + (png_size_t)row_width - 1;
3765 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); 3834 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3766 for (i = 0; i < row_width; i++) 3835 for (i = 0; i < row_width; i++)
3767 { 3836 {
3768 value = (*sp >> shift) & 0x03; 3837 value = (*sp >> shift) & 0x03;
3769 *dp = (png_byte)(value | (value << 2) | (value << 4) | 3838 *dp = (png_byte)(value | (value << 2) | (value << 4) |
3770 (value << 6)); 3839 (value << 6));
3771 if (shift == 6) 3840 if (shift == 6)
3772 { 3841 {
3773 shift = 0; 3842 shift = 0;
3774 sp--; 3843 sp--;
3775 } 3844 }
3776 else 3845 else
3777 shift += 2; 3846 shift += 2;
3778 3847
3779 dp--; 3848 dp--;
3780 } 3849 }
3781 break; 3850 break;
3782 } 3851 }
3852
3783 case 4: 3853 case 4:
3784 { 3854 {
3785 gray = (png_uint_16)((gray&0x0f)*0x11); 3855 gray = (png_uint_16)((gray&0x0f)*0x11);
3786 sp = row + (png_size_t)((row_width - 1) >> 1); 3856 sp = row + (png_size_t)((row_width - 1) >> 1);
3787 dp = row + (png_size_t)row_width - 1; 3857 dp = row + (png_size_t)row_width - 1;
3788 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); 3858 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
3789 for (i = 0; i < row_width; i++) 3859 for (i = 0; i < row_width; i++)
3790 { 3860 {
3791 value = (*sp >> shift) & 0x0f; 3861 value = (*sp >> shift) & 0x0f;
3792 *dp = (png_byte)(value | (value << 4)); 3862 *dp = (png_byte)(value | (value << 4));
3793 if (shift == 4) 3863 if (shift == 4)
3794 { 3864 {
3795 shift = 0; 3865 shift = 0;
3796 sp--; 3866 sp--;
3797 } 3867 }
3798 else 3868 else
3799 shift = 4; 3869 shift = 4;
3800 3870
3801 dp--; 3871 dp--;
3802 } 3872 }
3803 break; 3873 break;
3804 } 3874 }
3805 } 3875 }
3876
3806 row_info->bit_depth = 8; 3877 row_info->bit_depth = 8;
3807 row_info->pixel_depth = 8; 3878 row_info->pixel_depth = 8;
3808 row_info->rowbytes = row_width; 3879 row_info->rowbytes = row_width;
3809 } 3880 }
3810 3881
3811 if (trans_value != NULL) 3882 if (trans_value != NULL)
3812 { 3883 {
3813 if (row_info->bit_depth == 8) 3884 if (row_info->bit_depth == 8)
3814 { 3885 {
3815 gray = gray & 0xff; 3886 gray = gray & 0xff;
3816 sp = row + (png_size_t)row_width - 1; 3887 sp = row + (png_size_t)row_width - 1;
3817 dp = row + (png_size_t)(row_width << 1) - 1; 3888 dp = row + (png_size_t)(row_width << 1) - 1;
3818 for (i = 0; i < row_width; i++) 3889 for (i = 0; i < row_width; i++)
3819 { 3890 {
3820 if (*sp == gray) 3891 if (*sp == gray)
3821 *dp-- = 0; 3892 *dp-- = 0;
3822 else 3893 else
3823 *dp-- = 0xff; 3894 *dp-- = 0xff;
3824 *dp-- = *sp--; 3895 *dp-- = *sp--;
3825 } 3896 }
3826 } 3897 }
3898
3827 else if (row_info->bit_depth == 16) 3899 else if (row_info->bit_depth == 16)
3828 { 3900 {
3829 png_byte gray_high = (gray >> 8) & 0xff; 3901 png_byte gray_high = (gray >> 8) & 0xff;
3830 png_byte gray_low = gray & 0xff; 3902 png_byte gray_low = gray & 0xff;
3831 sp = row + row_info->rowbytes - 1; 3903 sp = row + row_info->rowbytes - 1;
3832 dp = row + (row_info->rowbytes << 1) - 1; 3904 dp = row + (row_info->rowbytes << 1) - 1;
3833 for (i = 0; i < row_width; i++) 3905 for (i = 0; i < row_width; i++)
3834 { 3906 {
3835 if (*(sp - 1) == gray_high && *(sp) == gray_low) 3907 if (*(sp - 1) == gray_high && *(sp) == gray_low)
3836 { 3908 {
3837 *dp-- = 0; 3909 *dp-- = 0;
3838 *dp-- = 0; 3910 *dp-- = 0;
3839 } 3911 }
3840 else 3912 else
3841 { 3913 {
3842 *dp-- = 0xff; 3914 *dp-- = 0xff;
3843 *dp-- = 0xff; 3915 *dp-- = 0xff;
3844 } 3916 }
3845 *dp-- = *sp--; 3917 *dp-- = *sp--;
3846 *dp-- = *sp--; 3918 *dp-- = *sp--;
3847 } 3919 }
3848 } 3920 }
3921
3849 row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; 3922 row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3850 row_info->channels = 2; 3923 row_info->channels = 2;
3851 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); 3924 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
3852 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, 3925 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
3853 row_width); 3926 row_width);
3854 } 3927 }
3855 } 3928 }
3856 else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value) 3929 else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
3857 { 3930 {
3858 if (row_info->bit_depth == 8) 3931 if (row_info->bit_depth == 8)
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
3936 { 4009 {
3937 int r, g, b, p; 4010 int r, g, b, p;
3938 sp = row; 4011 sp = row;
3939 dp = row; 4012 dp = row;
3940 for (i = 0; i < row_width; i++) 4013 for (i = 0; i < row_width; i++)
3941 { 4014 {
3942 r = *sp++; 4015 r = *sp++;
3943 g = *sp++; 4016 g = *sp++;
3944 b = *sp++; 4017 b = *sp++;
3945 4018
3946 /* this looks real messy, but the compiler will reduce 4019 /* This looks real messy, but the compiler will reduce
3947 it down to a reasonable formula. For example, with 4020 * it down to a reasonable formula. For example, with
3948 5 bits per color, we get: 4021 * 5 bits per color, we get:
3949 p = (((r >> 3) & 0x1f) << 10) | 4022 * p = (((r >> 3) & 0x1f) << 10) |
3950 (((g >> 3) & 0x1f) << 5) | 4023 * (((g >> 3) & 0x1f) << 5) |
3951 ((b >> 3) & 0x1f); 4024 * ((b >> 3) & 0x1f);
3952 */ 4025 */
3953 p = (((r >> (8 - PNG_DITHER_RED_BITS)) & 4026 p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
3954 ((1 << PNG_DITHER_RED_BITS) - 1)) << 4027 ((1 << PNG_DITHER_RED_BITS) - 1)) <<
3955 (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) | 4028 (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
3956 (((g >> (8 - PNG_DITHER_GREEN_BITS)) & 4029 (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
3957 ((1 << PNG_DITHER_GREEN_BITS) - 1)) << 4030 ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
3958 (PNG_DITHER_BLUE_BITS)) | 4031 (PNG_DITHER_BLUE_BITS)) |
3959 ((b >> (8 - PNG_DITHER_BLUE_BITS)) & 4032 ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
3960 ((1 << PNG_DITHER_BLUE_BITS) - 1)); 4033 ((1 << PNG_DITHER_BLUE_BITS) - 1));
3961 4034
3962 *dp++ = palette_lookup[p]; 4035 *dp++ = palette_lookup[p];
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
4023 { 4096 {
4024 png_debug(1, "in png_build_gamma_table"); 4097 png_debug(1, "in png_build_gamma_table");
4025 4098
4026 if (png_ptr->bit_depth <= 8) 4099 if (png_ptr->bit_depth <= 8)
4027 { 4100 {
4028 int i; 4101 int i;
4029 double g; 4102 double g;
4030 4103
4031 if (png_ptr->screen_gamma > .000001) 4104 if (png_ptr->screen_gamma > .000001)
4032 g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); 4105 g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
4106
4033 else 4107 else
4034 g = 1.0; 4108 g = 1.0;
4035 4109
4036 png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr, 4110 png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
4037 (png_uint_32)256); 4111 (png_uint_32)256);
4038 4112
4039 for (i = 0; i < 256; i++) 4113 for (i = 0; i < 256; i++)
4040 { 4114 {
4041 png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0, 4115 png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,
4042 g) * 255.0 + .5); 4116 g) * 255.0 + .5);
(...skipping 14 matching lines...) Expand all
4057 png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0, 4131 png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,
4058 g) * 255.0 + .5); 4132 g) * 255.0 + .5);
4059 } 4133 }
4060 4134
4061 4135
4062 png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr, 4136 png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
4063 (png_uint_32)256); 4137 (png_uint_32)256);
4064 4138
4065 if (png_ptr->screen_gamma > 0.000001) 4139 if (png_ptr->screen_gamma > 0.000001)
4066 g = 1.0 / png_ptr->screen_gamma; 4140 g = 1.0 / png_ptr->screen_gamma;
4141
4067 else 4142 else
4068 g = png_ptr->gamma; /* probably doing rgb_to_gray */ 4143 g = png_ptr->gamma; /* Probably doing rgb_to_gray */
4069 4144
4070 for (i = 0; i < 256; i++) 4145 for (i = 0; i < 256; i++)
4071 { 4146 {
4072 png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0, 4147 png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
4073 g) * 255.0 + .5); 4148 g) * 255.0 + .5);
4074 4149
4075 } 4150 }
4076 } 4151 }
4077 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ 4152 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
4078 } 4153 }
4079 else 4154 else
4080 { 4155 {
4081 double g; 4156 double g;
4082 int i, j, shift, num; 4157 int i, j, shift, num;
4083 int sig_bit; 4158 int sig_bit;
4084 png_uint_32 ig; 4159 png_uint_32 ig;
4085 4160
4086 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) 4161 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
4087 { 4162 {
4088 sig_bit = (int)png_ptr->sig_bit.red; 4163 sig_bit = (int)png_ptr->sig_bit.red;
4164
4089 if ((int)png_ptr->sig_bit.green > sig_bit) 4165 if ((int)png_ptr->sig_bit.green > sig_bit)
4090 sig_bit = png_ptr->sig_bit.green; 4166 sig_bit = png_ptr->sig_bit.green;
4167
4091 if ((int)png_ptr->sig_bit.blue > sig_bit) 4168 if ((int)png_ptr->sig_bit.blue > sig_bit)
4092 sig_bit = png_ptr->sig_bit.blue; 4169 sig_bit = png_ptr->sig_bit.blue;
4093 } 4170 }
4094 else 4171 else
4095 { 4172 {
4096 sig_bit = (int)png_ptr->sig_bit.gray; 4173 sig_bit = (int)png_ptr->sig_bit.gray;
4097 } 4174 }
4098 4175
4099 if (sig_bit > 0) 4176 if (sig_bit > 0)
4100 shift = 16 - sig_bit; 4177 shift = 16 - sig_bit;
4178
4101 else 4179 else
4102 shift = 0; 4180 shift = 0;
4103 4181
4104 if (png_ptr->transformations & PNG_16_TO_8) 4182 if (png_ptr->transformations & PNG_16_TO_8)
4105 { 4183 {
4106 if (shift < (16 - PNG_MAX_GAMMA_8)) 4184 if (shift < (16 - PNG_MAX_GAMMA_8))
4107 shift = (16 - PNG_MAX_GAMMA_8); 4185 shift = (16 - PNG_MAX_GAMMA_8);
4108 } 4186 }
4109 4187
4110 if (shift > 8) 4188 if (shift > 8)
4111 shift = 8; 4189 shift = 8;
4190
4112 if (shift < 0) 4191 if (shift < 0)
4113 shift = 0; 4192 shift = 0;
4114 4193
4115 png_ptr->gamma_shift = (png_byte)shift; 4194 png_ptr->gamma_shift = (png_byte)shift;
4116 4195
4117 num = (1 << (8 - shift)); 4196 num = (1 << (8 - shift));
4118 4197
4119 if (png_ptr->screen_gamma > .000001) 4198 if (png_ptr->screen_gamma > .000001)
4120 g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); 4199 g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
4121 else 4200 else
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
4159 } 4238 }
4160 } 4239 }
4161 else 4240 else
4162 { 4241 {
4163 for (i = 0; i < num; i++) 4242 for (i = 0; i < num; i++)
4164 { 4243 {
4165 png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, 4244 png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
4166 (png_uint_32)(256 * png_sizeof(png_uint_16))); 4245 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4167 4246
4168 ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4); 4247 ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);
4248
4169 for (j = 0; j < 256; j++) 4249 for (j = 0; j < 256; j++)
4170 { 4250 {
4171 png_ptr->gamma_16_table[i][j] = 4251 png_ptr->gamma_16_table[i][j] =
4172 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / 4252 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
4173 65535.0, g) * 65535.0 + .5); 4253 65535.0, g) * 65535.0 + .5);
4174 } 4254 }
4175 } 4255 }
4176 } 4256 }
4177 4257
4178 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ 4258 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
(...skipping 17 matching lines...) Expand all
4196 for (j = 0; j < 256; j++) 4276 for (j = 0; j < 256; j++)
4197 { 4277 {
4198 png_ptr->gamma_16_to_1[i][j] = 4278 png_ptr->gamma_16_to_1[i][j] =
4199 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / 4279 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
4200 65535.0, g) * 65535.0 + .5); 4280 65535.0, g) * 65535.0 + .5);
4201 } 4281 }
4202 } 4282 }
4203 4283
4204 if (png_ptr->screen_gamma > 0.000001) 4284 if (png_ptr->screen_gamma > 0.000001)
4205 g = 1.0 / png_ptr->screen_gamma; 4285 g = 1.0 / png_ptr->screen_gamma;
4286
4206 else 4287 else
4207 g = png_ptr->gamma; /* probably doing rgb_to_gray */ 4288 g = png_ptr->gamma; /* Probably doing rgb_to_gray */
4208 4289
4209 png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr, 4290 png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr,
4210 (png_uint_32)(num * png_sizeof(png_uint_16p))); 4291 (png_uint_32)(num * png_sizeof(png_uint_16p)));
4211 png_memset(png_ptr->gamma_16_from_1, 0, 4292 png_memset(png_ptr->gamma_16_from_1, 0,
4212 num * png_sizeof(png_uint_16p)); 4293 num * png_sizeof(png_uint_16p));
4213 4294
4214 for (i = 0; i < num; i++) 4295 for (i = 0; i < num; i++)
4215 { 4296 {
4216 png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr, 4297 png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
4217 (png_uint_32)(256 * png_sizeof(png_uint_16))); 4298 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4218 4299
4219 ig = (((png_uint_32)i * 4300 ig = (((png_uint_32)i *
4220 (png_uint_32)png_gamma_shift[shift]) >> 4); 4301 (png_uint_32)png_gamma_shift[shift]) >> 4);
4302
4221 for (j = 0; j < 256; j++) 4303 for (j = 0; j < 256; j++)
4222 { 4304 {
4223 png_ptr->gamma_16_from_1[i][j] = 4305 png_ptr->gamma_16_from_1[i][j] =
4224 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / 4306 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
4225 65535.0, g) * 65535.0 + .5); 4307 65535.0, g) * 65535.0 + .5);
4226 } 4308 }
4227 } 4309 }
4228 } 4310 }
4229 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ 4311 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
4230 } 4312 }
4231 } 4313 }
4232 #endif 4314 #endif
4233 /* To do: install integer version of png_build_gamma_table here */ 4315 /* To do: install integer version of png_build_gamma_table here */
4234 #endif 4316 #endif
4235 4317
4236 #if defined(PNG_MNG_FEATURES_SUPPORTED) 4318 #if defined(PNG_MNG_FEATURES_SUPPORTED)
4237 /* undoes intrapixel differencing */ 4319 /* Undoes intrapixel differencing */
4238 void /* PRIVATE */ 4320 void /* PRIVATE */
4239 png_do_read_intrapixel(png_row_infop row_info, png_bytep row) 4321 png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
4240 { 4322 {
4241 png_debug(1, "in png_do_read_intrapixel"); 4323 png_debug(1, "in png_do_read_intrapixel");
4242 if ( 4324 if (
4243 #if defined(PNG_USELESS_TESTS_SUPPORTED) 4325 #if defined(PNG_USELESS_TESTS_SUPPORTED)
4244 row != NULL && row_info != NULL && 4326 row != NULL && row_info != NULL &&
4245 #endif 4327 #endif
4246 (row_info->color_type & PNG_COLOR_MASK_COLOR)) 4328 (row_info->color_type & PNG_COLOR_MASK_COLOR))
4247 { 4329 {
4248 int bytes_per_pixel; 4330 int bytes_per_pixel;
4249 png_uint_32 row_width = row_info->width; 4331 png_uint_32 row_width = row_info->width;
4250 if (row_info->bit_depth == 8) 4332 if (row_info->bit_depth == 8)
4251 { 4333 {
4252 png_bytep rp; 4334 png_bytep rp;
4253 png_uint_32 i; 4335 png_uint_32 i;
4254 4336
4255 if (row_info->color_type == PNG_COLOR_TYPE_RGB) 4337 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4256 bytes_per_pixel = 3; 4338 bytes_per_pixel = 3;
4339
4257 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) 4340 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4258 bytes_per_pixel = 4; 4341 bytes_per_pixel = 4;
4342
4259 else 4343 else
4260 return; 4344 return;
4261 4345
4262 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) 4346 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4263 { 4347 {
4264 *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff); 4348 *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff);
4265 *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff); 4349 *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff);
4266 } 4350 }
4267 } 4351 }
4268 else if (row_info->bit_depth == 16) 4352 else if (row_info->bit_depth == 16)
4269 { 4353 {
4270 png_bytep rp; 4354 png_bytep rp;
4271 png_uint_32 i; 4355 png_uint_32 i;
4272 4356
4273 if (row_info->color_type == PNG_COLOR_TYPE_RGB) 4357 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4274 bytes_per_pixel = 6; 4358 bytes_per_pixel = 6;
4359
4275 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) 4360 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4276 bytes_per_pixel = 8; 4361 bytes_per_pixel = 8;
4362
4277 else 4363 else
4278 return; 4364 return;
4279 4365
4280 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) 4366 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4281 { 4367 {
4282 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); 4368 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
4283 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); 4369 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
4284 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); 4370 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
4285 png_uint_32 red = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL); 4371 png_uint_32 red = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);
4286 png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL); 4372 png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);
4287 *(rp ) = (png_byte)((red >> 8) & 0xff); 4373 *(rp ) = (png_byte)((red >> 8) & 0xff);
4288 *(rp+1) = (png_byte)(red & 0xff); 4374 *(rp+1) = (png_byte)(red & 0xff);
4289 *(rp+4) = (png_byte)((blue >> 8) & 0xff); 4375 *(rp+4) = (png_byte)((blue >> 8) & 0xff);
4290 *(rp+5) = (png_byte)(blue & 0xff); 4376 *(rp+5) = (png_byte)(blue & 0xff);
4291 } 4377 }
4292 } 4378 }
4293 } 4379 }
4294 } 4380 }
4295 #endif /* PNG_MNG_FEATURES_SUPPORTED */ 4381 #endif /* PNG_MNG_FEATURES_SUPPORTED */
4296 #endif /* PNG_READ_SUPPORTED */ 4382 #endif /* PNG_READ_SUPPORTED */
OLDNEW
« no previous file with comments | « third_party/libpng/pngrio.c ('k') | third_party/libpng/pngrutil.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698