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

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

Issue 1591483003: XFA: Upgrade libpng to 1.6.20. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: rebase Created 4 years, 11 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
« 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 /* pngrtran.c - transforms the data in a row for PNG readers 2 /* pngrtran.c - transforms the data in a row for PNG readers
2 * 3 *
3 * Last changed in libpng 1.6.2 [April 25, 2013] 4 * Last changed in libpng 1.6.19 [November 12, 2015]
4 * Copyright (c) 1998-2013 Glenn Randers-Pehrson 5 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
5 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
6 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
7 * 8 *
8 * This code is released under the libpng license. 9 * This code is released under the libpng license.
9 * For conditions of distribution and use, see the disclaimer 10 * For conditions of distribution and use, see the disclaimer
10 * and license in png.h 11 * and license in png.h
11 * 12 *
12 * This file contains functions optionally called by an application 13 * This file contains functions optionally called by an application
13 * in order to tell libpng how to handle data when reading a PNG. 14 * in order to tell libpng how to handle data when reading a PNG.
14 * Transformations that are used in both reading and writing are 15 * Transformations that are used in both reading and writing are
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 case PNG_CRC_DEFAULT: 84 case PNG_CRC_DEFAULT:
84 default: 85 default:
85 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; 86 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
86 break; 87 break;
87 } 88 }
88 } 89 }
89 90
90 #ifdef PNG_READ_TRANSFORMS_SUPPORTED 91 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
91 /* Is it OK to set a transformation now? Only if png_start_read_image or 92 /* Is it OK to set a transformation now? Only if png_start_read_image or
92 * png_read_update_info have not been called. It is not necessary for the IHDR 93 * png_read_update_info have not been called. It is not necessary for the IHDR
93 * to have been read in all cases, the parameter allows for this check too. 94 * to have been read in all cases; the need_IHDR parameter allows for this
95 * check too.
94 */ 96 */
95 static int 97 static int
96 png_rtran_ok(png_structrp png_ptr, int need_IHDR) 98 png_rtran_ok(png_structrp png_ptr, int need_IHDR)
97 { 99 {
98 if (png_ptr != NULL) 100 if (png_ptr != NULL)
99 { 101 {
100 if (png_ptr->flags & PNG_FLAG_ROW_INIT) 102 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) != 0)
101 png_app_error(png_ptr, 103 png_app_error(png_ptr,
102 "invalid after png_start_read_image or png_read_update_info"); 104 "invalid after png_start_read_image or png_read_update_info");
103 105
104 else if (need_IHDR && (png_ptr->mode & PNG_HAVE_IHDR) == 0) 106 else if (need_IHDR && (png_ptr->mode & PNG_HAVE_IHDR) == 0)
105 png_app_error(png_ptr, "invalid before the PNG header has been read"); 107 png_app_error(png_ptr, "invalid before the PNG header has been read");
106 108
107 else 109 else
108 { 110 {
109 /* Turn on failure to initialize correctly for all transforms. */ 111 /* Turn on failure to initialize correctly for all transforms. */
110 png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED; 112 png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
111 113
112 return 1; /* Ok */ 114 return 1; /* Ok */
113 } 115 }
114 } 116 }
115 117
116 return 0; /* no png_error possible! */ 118 return 0; /* no png_error possible! */
117 } 119 }
118 #endif 120 #endif
119 121
120 #ifdef PNG_READ_BACKGROUND_SUPPORTED 122 #ifdef PNG_READ_BACKGROUND_SUPPORTED
121 /* Handle alpha and tRNS via a background color */ 123 /* Handle alpha and tRNS via a background color */
122 void PNGFAPI 124 void PNGFAPI
123 png_set_background_fixed(png_structrp png_ptr, 125 png_set_background_fixed(png_structrp png_ptr,
124 png_const_color_16p background_color, int background_gamma_code, 126 png_const_color_16p background_color, int background_gamma_code,
125 int need_expand, png_fixed_point background_gamma) 127 int need_expand, png_fixed_point background_gamma)
126 { 128 {
127 png_debug(1, "in png_set_background_fixed"); 129 png_debug(1, "in png_set_background_fixed");
128 130
129 if (!png_rtran_ok(png_ptr, 0) || background_color == NULL) 131 if (png_rtran_ok(png_ptr, 0) == 0 || background_color == NULL)
130 return; 132 return;
131 133
132 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) 134 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
133 { 135 {
134 png_warning(png_ptr, "Application must supply a known background gamma"); 136 png_warning(png_ptr, "Application must supply a known background gamma");
135 return; 137 return;
136 } 138 }
137 139
138 png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA; 140 png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA;
139 png_ptr->transformations &= ~PNG_ENCODE_ALPHA; 141 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
140 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; 142 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
141 143
142 png_ptr->background = *background_color; 144 png_ptr->background = *background_color;
143 png_ptr->background_gamma = background_gamma; 145 png_ptr->background_gamma = background_gamma;
144 png_ptr->background_gamma_type = (png_byte)(background_gamma_code); 146 png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
145 if (need_expand) 147 if (need_expand != 0)
146 png_ptr->transformations |= PNG_BACKGROUND_EXPAND; 148 png_ptr->transformations |= PNG_BACKGROUND_EXPAND;
147 else 149 else
148 png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; 150 png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
149 } 151 }
150 152
151 # ifdef PNG_FLOATING_POINT_SUPPORTED 153 # ifdef PNG_FLOATING_POINT_SUPPORTED
152 void PNGAPI 154 void PNGAPI
153 png_set_background(png_structrp png_ptr, 155 png_set_background(png_structrp png_ptr,
154 png_const_color_16p background_color, int background_gamma_code, 156 png_const_color_16p background_color, int background_gamma_code,
155 int need_expand, double background_gamma) 157 int need_expand, double background_gamma)
156 { 158 {
157 png_set_background_fixed(png_ptr, background_color, background_gamma_code, 159 png_set_background_fixed(png_ptr, background_color, background_gamma_code,
158 need_expand, png_fixed(png_ptr, background_gamma, "png_set_background")); 160 need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
159 } 161 }
160 # endif /* FLOATING_POINT */ 162 # endif /* FLOATING_POINT */
161 #endif /* READ_BACKGROUND */ 163 #endif /* READ_BACKGROUND */
162 164
163 /* Scale 16-bit depth files to 8-bit depth. If both of these are set then the 165 /* Scale 16-bit depth files to 8-bit depth. If both of these are set then the
164 * one that pngrtran does first (scale) happens. This is necessary to allow the 166 * one that pngrtran does first (scale) happens. This is necessary to allow the
165 * TRANSFORM and API behavior to be somewhat consistent, and it's simpler. 167 * TRANSFORM and API behavior to be somewhat consistent, and it's simpler.
166 */ 168 */
167 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED 169 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
168 void PNGAPI 170 void PNGAPI
169 png_set_scale_16(png_structrp png_ptr) 171 png_set_scale_16(png_structrp png_ptr)
170 { 172 {
171 png_debug(1, "in png_set_scale_16"); 173 png_debug(1, "in png_set_scale_16");
172 174
173 if (!png_rtran_ok(png_ptr, 0)) 175 if (png_rtran_ok(png_ptr, 0) == 0)
174 return; 176 return;
175 177
176 png_ptr->transformations |= PNG_SCALE_16_TO_8; 178 png_ptr->transformations |= PNG_SCALE_16_TO_8;
177 } 179 }
178 #endif 180 #endif
179 181
180 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED 182 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
181 /* Chop 16-bit depth files to 8-bit depth */ 183 /* Chop 16-bit depth files to 8-bit depth */
182 void PNGAPI 184 void PNGAPI
183 png_set_strip_16(png_structrp png_ptr) 185 png_set_strip_16(png_structrp png_ptr)
184 { 186 {
185 png_debug(1, "in png_set_strip_16"); 187 png_debug(1, "in png_set_strip_16");
186 188
187 if (!png_rtran_ok(png_ptr, 0)) 189 if (png_rtran_ok(png_ptr, 0) == 0)
188 return; 190 return;
189 191
190 png_ptr->transformations |= PNG_16_TO_8; 192 png_ptr->transformations |= PNG_16_TO_8;
191 } 193 }
192 #endif 194 #endif
193 195
194 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED 196 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
195 void PNGAPI 197 void PNGAPI
196 png_set_strip_alpha(png_structrp png_ptr) 198 png_set_strip_alpha(png_structrp png_ptr)
197 { 199 {
198 png_debug(1, "in png_set_strip_alpha"); 200 png_debug(1, "in png_set_strip_alpha");
199 201
200 if (!png_rtran_ok(png_ptr, 0)) 202 if (png_rtran_ok(png_ptr, 0) == 0)
201 return; 203 return;
202 204
203 png_ptr->transformations |= PNG_STRIP_ALPHA; 205 png_ptr->transformations |= PNG_STRIP_ALPHA;
204 } 206 }
205 #endif 207 #endif
206 208
207 #if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED) 209 #if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED)
208 static png_fixed_point 210 static png_fixed_point
209 translate_gamma_flags(png_structrp png_ptr, png_fixed_point output_gamma, 211 translate_gamma_flags(png_structrp png_ptr, png_fixed_point output_gamma,
210 int is_screen) 212 int is_screen)
211 { 213 {
212 /* Check for flag values. The main reason for having the old Mac value as a 214 /* Check for flag values. The main reason for having the old Mac value as a
213 * flag is that it is pretty near impossible to work out what the correct 215 * flag is that it is pretty near impossible to work out what the correct
214 * value is from Apple documentation - a working Mac system is needed to 216 * value is from Apple documentation - a working Mac system is needed to
215 * discover the value! 217 * discover the value!
216 */ 218 */
217 if (output_gamma == PNG_DEFAULT_sRGB || 219 if (output_gamma == PNG_DEFAULT_sRGB ||
218 output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB) 220 output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB)
219 { 221 {
220 /* If there is no sRGB support this just sets the gamma to the standard 222 /* If there is no sRGB support this just sets the gamma to the standard
221 * sRGB value. (This is a side effect of using this function!) 223 * sRGB value. (This is a side effect of using this function!)
222 */ 224 */
223 # ifdef PNG_READ_sRGB_SUPPORTED 225 # ifdef PNG_READ_sRGB_SUPPORTED
224 png_ptr->flags |= PNG_FLAG_ASSUME_sRGB; 226 png_ptr->flags |= PNG_FLAG_ASSUME_sRGB;
225 # else 227 # else
226 PNG_UNUSED(png_ptr) 228 PNG_UNUSED(png_ptr)
227 # endif 229 # endif
228 if (is_screen) 230 if (is_screen != 0)
229 output_gamma = PNG_GAMMA_sRGB; 231 output_gamma = PNG_GAMMA_sRGB;
230 else 232 else
231 output_gamma = PNG_GAMMA_sRGB_INVERSE; 233 output_gamma = PNG_GAMMA_sRGB_INVERSE;
232 } 234 }
233 235
234 else if (output_gamma == PNG_GAMMA_MAC_18 || 236 else if (output_gamma == PNG_GAMMA_MAC_18 ||
235 output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18) 237 output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18)
236 { 238 {
237 if (is_screen) 239 if (is_screen != 0)
238 output_gamma = PNG_GAMMA_MAC_OLD; 240 output_gamma = PNG_GAMMA_MAC_OLD;
239 else 241 else
240 output_gamma = PNG_GAMMA_MAC_INVERSE; 242 output_gamma = PNG_GAMMA_MAC_INVERSE;
241 } 243 }
242 244
243 return output_gamma; 245 return output_gamma;
244 } 246 }
245 247
246 # ifdef PNG_FLOATING_POINT_SUPPORTED 248 # ifdef PNG_FLOATING_POINT_SUPPORTED
247 static png_fixed_point 249 static png_fixed_point
(...skipping 23 matching lines...) Expand all
271 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED 273 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
272 void PNGFAPI 274 void PNGFAPI
273 png_set_alpha_mode_fixed(png_structrp png_ptr, int mode, 275 png_set_alpha_mode_fixed(png_structrp png_ptr, int mode,
274 png_fixed_point output_gamma) 276 png_fixed_point output_gamma)
275 { 277 {
276 int compose = 0; 278 int compose = 0;
277 png_fixed_point file_gamma; 279 png_fixed_point file_gamma;
278 280
279 png_debug(1, "in png_set_alpha_mode"); 281 png_debug(1, "in png_set_alpha_mode");
280 282
281 if (!png_rtran_ok(png_ptr, 0)) 283 if (png_rtran_ok(png_ptr, 0) == 0)
282 return; 284 return;
283 285
284 output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/); 286 output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/);
285 287
286 /* Validate the value to ensure it is in a reasonable range. The value 288 /* Validate the value to ensure it is in a reasonable range. The value
287 * is expected to be 1 or greater, but this range test allows for some 289 * is expected to be 1 or greater, but this range test allows for some
288 * viewing correction values. The intent is to weed out users of this API 290 * viewing correction values. The intent is to weed out users of this API
289 * who use the inverse of the gamma value accidentally! Since some of these 291 * who use the inverse of the gamma value accidentally! Since some of these
290 * values are reasonable this may have to be changed. 292 * values are reasonable this may have to be changed.
291 */ 293 */
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 png_ptr->colorspace.gamma = file_gamma; 357 png_ptr->colorspace.gamma = file_gamma;
356 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; 358 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
357 } 359 }
358 360
359 /* But always set the output gamma: */ 361 /* But always set the output gamma: */
360 png_ptr->screen_gamma = output_gamma; 362 png_ptr->screen_gamma = output_gamma;
361 363
362 /* Finally, if pre-multiplying, set the background fields to achieve the 364 /* Finally, if pre-multiplying, set the background fields to achieve the
363 * desired result. 365 * desired result.
364 */ 366 */
365 if (compose) 367 if (compose != 0)
366 { 368 {
367 /* And obtain alpha pre-multiplication by composing on black: */ 369 /* And obtain alpha pre-multiplication by composing on black: */
368 memset(&png_ptr->background, 0, (sizeof png_ptr->background)); 370 memset(&png_ptr->background, 0, (sizeof png_ptr->background));
369 png_ptr->background_gamma = png_ptr->colorspace.gamma; /* just in case */ 371 png_ptr->background_gamma = png_ptr->colorspace.gamma; /* just in case */
370 png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE; 372 png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE;
371 png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; 373 png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
372 374
373 if (png_ptr->transformations & PNG_COMPOSE) 375 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
374 png_error(png_ptr, 376 png_error(png_ptr,
375 "conflicting calls to set alpha mode and background"); 377 "conflicting calls to set alpha mode and background");
376 378
377 png_ptr->transformations |= PNG_COMPOSE; 379 png_ptr->transformations |= PNG_COMPOSE;
378 } 380 }
379 } 381 }
380 382
381 # ifdef PNG_FLOATING_POINT_SUPPORTED 383 # ifdef PNG_FLOATING_POINT_SUPPORTED
382 void PNGAPI 384 void PNGAPI
383 png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma) 385 png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma)
384 { 386 {
385 png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr, 387 png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr,
386 output_gamma)); 388 output_gamma));
387 } 389 }
388 # endif 390 # endif
389 #endif 391 #endif
390 392
391 #ifdef PNG_READ_QUANTIZE_SUPPORTED 393 #ifdef PNG_READ_QUANTIZE_SUPPORTED
392 /* Dither file to 8-bit. Supply a palette, the current number 394 /* Dither file to 8-bit. Supply a palette, the current number
393 * of elements in the palette, the maximum number of elements 395 * of elements in the palette, the maximum number of elements
394 * allowed, and a histogram if possible. If the current number 396 * allowed, and a histogram if possible. If the current number
395 * of colors is greater then the maximum number, the palette will be 397 * of colors is greater than the maximum number, the palette will be
396 * modified to fit in the maximum number. "full_quantize" indicates 398 * modified to fit in the maximum number. "full_quantize" indicates
397 * whether we need a quantizing cube set up for RGB images, or if we 399 * whether we need a quantizing cube set up for RGB images, or if we
398 * simply are reducing the number of colors in a paletted image. 400 * simply are reducing the number of colors in a paletted image.
399 */ 401 */
400 402
401 typedef struct png_dsort_struct 403 typedef struct png_dsort_struct
402 { 404 {
403 struct png_dsort_struct * next; 405 struct png_dsort_struct * next;
404 png_byte left; 406 png_byte left;
405 png_byte right; 407 png_byte right;
406 } png_dsort; 408 } png_dsort;
407 typedef png_dsort * png_dsortp; 409 typedef png_dsort * png_dsortp;
408 typedef png_dsort * * png_dsortpp; 410 typedef png_dsort * * png_dsortpp;
409 411
410 void PNGAPI 412 void PNGAPI
411 png_set_quantize(png_structrp png_ptr, png_colorp palette, 413 png_set_quantize(png_structrp png_ptr, png_colorp palette,
412 int num_palette, int maximum_colors, png_const_uint_16p histogram, 414 int num_palette, int maximum_colors, png_const_uint_16p histogram,
413 int full_quantize) 415 int full_quantize)
414 { 416 {
415 png_debug(1, "in png_set_quantize"); 417 png_debug(1, "in png_set_quantize");
416 418
417 if (!png_rtran_ok(png_ptr, 0)) 419 if (png_rtran_ok(png_ptr, 0) == 0)
418 return; 420 return;
419 421
420 png_ptr->transformations |= PNG_QUANTIZE; 422 png_ptr->transformations |= PNG_QUANTIZE;
421 423
422 if (!full_quantize) 424 if (full_quantize == 0)
423 { 425 {
424 int i; 426 int i;
425 427
426 png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr, 428 png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,
427 (png_uint_32)(num_palette * (sizeof (png_byte)))); 429 (png_uint_32)(num_palette * (sizeof (png_byte))));
428 for (i = 0; i < num_palette; i++) 430 for (i = 0; i < num_palette; i++)
429 png_ptr->quantize_index[i] = (png_byte)i; 431 png_ptr->quantize_index[i] = (png_byte)i;
430 } 432 }
431 433
432 if (num_palette > maximum_colors) 434 if (num_palette > maximum_colors)
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 { 469 {
468 png_byte t; 470 png_byte t;
469 471
470 t = png_ptr->quantize_sort[j]; 472 t = png_ptr->quantize_sort[j];
471 png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1]; 473 png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];
472 png_ptr->quantize_sort[j + 1] = t; 474 png_ptr->quantize_sort[j + 1] = t;
473 done = 0; 475 done = 0;
474 } 476 }
475 } 477 }
476 478
477 if (done) 479 if (done != 0)
478 break; 480 break;
479 } 481 }
480 482
481 /* Swap the palette around, and set up a table, if necessary */ 483 /* Swap the palette around, and set up a table, if necessary */
482 if (full_quantize) 484 if (full_quantize != 0)
483 { 485 {
484 int j = num_palette; 486 int j = num_palette;
485 487
486 /* Put all the useful colors within the max, but don't 488 /* Put all the useful colors within the max, but don't
487 * move the others. 489 * move the others.
488 */ 490 */
489 for (i = 0; i < maximum_colors; i++) 491 for (i = 0; i < maximum_colors; i++)
490 { 492 {
491 if ((int)png_ptr->quantize_sort[i] >= maximum_colors) 493 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
492 { 494 {
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
655 } 657 }
656 else 658 else
657 { 659 {
658 j = p->right; 660 j = p->right;
659 next_j = p->left; 661 next_j = p->left;
660 } 662 }
661 663
662 num_new_palette--; 664 num_new_palette--;
663 palette[png_ptr->index_to_palette[j]] 665 palette[png_ptr->index_to_palette[j]]
664 = palette[num_new_palette]; 666 = palette[num_new_palette];
665 if (!full_quantize) 667 if (full_quantize == 0)
666 { 668 {
667 int k; 669 int k;
668 670
669 for (k = 0; k < num_palette; k++) 671 for (k = 0; k < num_palette; k++)
670 { 672 {
671 if (png_ptr->quantize_index[k] == 673 if (png_ptr->quantize_index[k] ==
672 png_ptr->index_to_palette[j]) 674 png_ptr->index_to_palette[j])
673 png_ptr->quantize_index[k] = 675 png_ptr->quantize_index[k] =
674 png_ptr->index_to_palette[next_j]; 676 png_ptr->index_to_palette[next_j];
675 677
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
723 png_ptr->index_to_palette = NULL; 725 png_ptr->index_to_palette = NULL;
724 } 726 }
725 num_palette = maximum_colors; 727 num_palette = maximum_colors;
726 } 728 }
727 if (png_ptr->palette == NULL) 729 if (png_ptr->palette == NULL)
728 { 730 {
729 png_ptr->palette = palette; 731 png_ptr->palette = palette;
730 } 732 }
731 png_ptr->num_palette = (png_uint_16)num_palette; 733 png_ptr->num_palette = (png_uint_16)num_palette;
732 734
733 if (full_quantize) 735 if (full_quantize != 0)
734 { 736 {
735 int i; 737 int i;
736 png_bytep distance; 738 png_bytep distance;
737 int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS + 739 int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +
738 PNG_QUANTIZE_BLUE_BITS; 740 PNG_QUANTIZE_BLUE_BITS;
739 int num_red = (1 << PNG_QUANTIZE_RED_BITS); 741 int num_red = (1 << PNG_QUANTIZE_RED_BITS);
740 int num_green = (1 << PNG_QUANTIZE_GREEN_BITS); 742 int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);
741 int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS); 743 int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
742 png_size_t num_entries = ((png_size_t)1 << total_bits); 744 png_size_t num_entries = ((png_size_t)1 << total_bits);
743 745
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 png_ptr->palette_lookup[d_index] = (png_byte)i; 787 png_ptr->palette_lookup[d_index] = (png_byte)i;
786 } 788 }
787 } 789 }
788 } 790 }
789 } 791 }
790 } 792 }
791 793
792 png_free(png_ptr, distance); 794 png_free(png_ptr, distance);
793 } 795 }
794 } 796 }
795 #endif /* PNG_READ_QUANTIZE_SUPPORTED */ 797 #endif /* READ_QUANTIZE */
796 798
797 #ifdef PNG_READ_GAMMA_SUPPORTED 799 #ifdef PNG_READ_GAMMA_SUPPORTED
798 void PNGFAPI 800 void PNGFAPI
799 png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma, 801 png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma,
800 png_fixed_point file_gamma) 802 png_fixed_point file_gamma)
801 { 803 {
802 png_debug(1, "in png_set_gamma_fixed"); 804 png_debug(1, "in png_set_gamma_fixed");
803 805
804 if (!png_rtran_ok(png_ptr, 0)) 806 if (png_rtran_ok(png_ptr, 0) == 0)
805 return; 807 return;
806 808
807 /* New in libpng-1.5.4 - reserve particular negative values as flags. */ 809 /* New in libpng-1.5.4 - reserve particular negative values as flags. */
808 scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/); 810 scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/);
809 file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/); 811 file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/);
810 812
811 /* Checking the gamma values for being >0 was added in 1.5.4 along with the 813 /* Checking the gamma values for being >0 was added in 1.5.4 along with the
812 * premultiplied alpha support; this actually hides an undocumented feature 814 * premultiplied alpha support; this actually hides an undocumented feature
813 * of the previous implementation which allowed gamma processing to be 815 * of the previous implementation which allowed gamma processing to be
814 * disabled in background handling. There is no evidence (so far) that this 816 * disabled in background handling. There is no evidence (so far) that this
(...skipping 19 matching lines...) Expand all
834 png_ptr->screen_gamma = scrn_gamma; 836 png_ptr->screen_gamma = scrn_gamma;
835 } 837 }
836 838
837 # ifdef PNG_FLOATING_POINT_SUPPORTED 839 # ifdef PNG_FLOATING_POINT_SUPPORTED
838 void PNGAPI 840 void PNGAPI
839 png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma) 841 png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma)
840 { 842 {
841 png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma), 843 png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma),
842 convert_gamma_value(png_ptr, file_gamma)); 844 convert_gamma_value(png_ptr, file_gamma));
843 } 845 }
844 # endif /* FLOATING_POINT_SUPPORTED */ 846 # endif /* FLOATING_POINT */
845 #endif /* READ_GAMMA */ 847 #endif /* READ_GAMMA */
846 848
847 #ifdef PNG_READ_EXPAND_SUPPORTED 849 #ifdef PNG_READ_EXPAND_SUPPORTED
848 /* Expand paletted images to RGB, expand grayscale images of 850 /* Expand paletted images to RGB, expand grayscale images of
849 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks 851 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
850 * to alpha channels. 852 * to alpha channels.
851 */ 853 */
852 void PNGAPI 854 void PNGAPI
853 png_set_expand(png_structrp png_ptr) 855 png_set_expand(png_structrp png_ptr)
854 { 856 {
855 png_debug(1, "in png_set_expand"); 857 png_debug(1, "in png_set_expand");
856 858
857 if (!png_rtran_ok(png_ptr, 0)) 859 if (png_rtran_ok(png_ptr, 0) == 0)
858 return; 860 return;
859 861
860 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); 862 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
861 } 863 }
862 864
863 /* GRR 19990627: the following three functions currently are identical 865 /* GRR 19990627: the following three functions currently are identical
864 * to png_set_expand(). However, it is entirely reasonable that someone 866 * to png_set_expand(). However, it is entirely reasonable that someone
865 * might wish to expand an indexed image to RGB but *not* expand a single, 867 * might wish to expand an indexed image to RGB but *not* expand a single,
866 * fully transparent palette entry to a full alpha channel--perhaps instead 868 * fully transparent palette entry to a full alpha channel--perhaps instead
867 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace 869 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
868 * the transparent color with a particular RGB value, or drop tRNS entirely. 870 * the transparent color with a particular RGB value, or drop tRNS entirely.
869 * IOW, a future version of the library may make the transformations flag 871 * IOW, a future version of the library may make the transformations flag
870 * a bit more fine-grained, with separate bits for each of these three 872 * a bit more fine-grained, with separate bits for each of these three
871 * functions. 873 * functions.
872 * 874 *
873 * More to the point, these functions make it obvious what libpng will be 875 * More to the point, these functions make it obvious what libpng will be
874 * doing, whereas "expand" can (and does) mean any number of things. 876 * doing, whereas "expand" can (and does) mean any number of things.
875 * 877 *
876 * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified 878 * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
877 * to expand only the sample depth but not to expand the tRNS to alpha 879 * to expand only the sample depth but not to expand the tRNS to alpha
878 * and its name was changed to png_set_expand_gray_1_2_4_to_8(). 880 * and its name was changed to png_set_expand_gray_1_2_4_to_8().
879 */ 881 */
880 882
881 /* Expand paletted images to RGB. */ 883 /* Expand paletted images to RGB. */
882 void PNGAPI 884 void PNGAPI
883 png_set_palette_to_rgb(png_structrp png_ptr) 885 png_set_palette_to_rgb(png_structrp png_ptr)
884 { 886 {
885 png_debug(1, "in png_set_palette_to_rgb"); 887 png_debug(1, "in png_set_palette_to_rgb");
886 888
887 if (!png_rtran_ok(png_ptr, 0)) 889 if (png_rtran_ok(png_ptr, 0) == 0)
888 return; 890 return;
889 891
890 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); 892 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
891 } 893 }
892 894
893 /* Expand grayscale images of less than 8-bit depth to 8 bits. */ 895 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
894 void PNGAPI 896 void PNGAPI
895 png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr) 897 png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr)
896 { 898 {
897 png_debug(1, "in png_set_expand_gray_1_2_4_to_8"); 899 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
898 900
899 if (!png_rtran_ok(png_ptr, 0)) 901 if (png_rtran_ok(png_ptr, 0) == 0)
900 return; 902 return;
901 903
902 png_ptr->transformations |= PNG_EXPAND; 904 png_ptr->transformations |= PNG_EXPAND;
903 } 905 }
904 906
905 /* Expand tRNS chunks to alpha channels. */ 907 /* Expand tRNS chunks to alpha channels. */
906 void PNGAPI 908 void PNGAPI
907 png_set_tRNS_to_alpha(png_structrp png_ptr) 909 png_set_tRNS_to_alpha(png_structrp png_ptr)
908 { 910 {
909 png_debug(1, "in png_set_tRNS_to_alpha"); 911 png_debug(1, "in png_set_tRNS_to_alpha");
910 912
911 if (!png_rtran_ok(png_ptr, 0)) 913 if (png_rtran_ok(png_ptr, 0) == 0)
912 return; 914 return;
913 915
914 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); 916 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
915 } 917 }
916 #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */ 918 #endif /* READ_EXPAND */
917 919
918 #ifdef PNG_READ_EXPAND_16_SUPPORTED 920 #ifdef PNG_READ_EXPAND_16_SUPPORTED
919 /* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise 921 /* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise
920 * it may not work correctly.) 922 * it may not work correctly.)
921 */ 923 */
922 void PNGAPI 924 void PNGAPI
923 png_set_expand_16(png_structrp png_ptr) 925 png_set_expand_16(png_structrp png_ptr)
924 { 926 {
925 png_debug(1, "in png_set_expand_16"); 927 png_debug(1, "in png_set_expand_16");
926 928
927 if (!png_rtran_ok(png_ptr, 0)) 929 if (png_rtran_ok(png_ptr, 0) == 0)
928 return; 930 return;
929 931
930 png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS); 932 png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
931 } 933 }
932 #endif 934 #endif
933 935
934 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED 936 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
935 void PNGAPI 937 void PNGAPI
936 png_set_gray_to_rgb(png_structrp png_ptr) 938 png_set_gray_to_rgb(png_structrp png_ptr)
937 { 939 {
938 png_debug(1, "in png_set_gray_to_rgb"); 940 png_debug(1, "in png_set_gray_to_rgb");
939 941
940 if (!png_rtran_ok(png_ptr, 0)) 942 if (png_rtran_ok(png_ptr, 0) == 0)
941 return; 943 return;
942 944
943 /* Because rgb must be 8 bits or more: */ 945 /* Because rgb must be 8 bits or more: */
944 png_set_expand_gray_1_2_4_to_8(png_ptr); 946 png_set_expand_gray_1_2_4_to_8(png_ptr);
945 png_ptr->transformations |= PNG_GRAY_TO_RGB; 947 png_ptr->transformations |= PNG_GRAY_TO_RGB;
946 } 948 }
947 #endif 949 #endif
948 950
949 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED 951 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
950 void PNGFAPI 952 void PNGFAPI
951 png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action, 953 png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action,
952 png_fixed_point red, png_fixed_point green) 954 png_fixed_point red, png_fixed_point green)
953 { 955 {
954 png_debug(1, "in png_set_rgb_to_gray"); 956 png_debug(1, "in png_set_rgb_to_gray");
955 957
956 /* Need the IHDR here because of the check on color_type below. */ 958 /* Need the IHDR here because of the check on color_type below. */
957 /* TODO: fix this */ 959 /* TODO: fix this */
958 if (!png_rtran_ok(png_ptr, 1)) 960 if (png_rtran_ok(png_ptr, 1) == 0)
959 return; 961 return;
960 962
961 switch(error_action) 963 switch (error_action)
962 { 964 {
963 case PNG_ERROR_ACTION_NONE: 965 case PNG_ERROR_ACTION_NONE:
964 png_ptr->transformations |= PNG_RGB_TO_GRAY; 966 png_ptr->transformations |= PNG_RGB_TO_GRAY;
965 break; 967 break;
966 968
967 case PNG_ERROR_ACTION_WARN: 969 case PNG_ERROR_ACTION_WARN:
968 png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN; 970 png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
969 break; 971 break;
970 972
971 case PNG_ERROR_ACTION_ERROR: 973 case PNG_ERROR_ACTION_ERROR:
972 png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR; 974 png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
973 break; 975 break;
974 976
975 default: 977 default:
976 png_error(png_ptr, "invalid error action to rgb_to_gray"); 978 png_error(png_ptr, "invalid error action to rgb_to_gray");
977 break;
978 } 979 }
979 980
980 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 981 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
981 #ifdef PNG_READ_EXPAND_SUPPORTED 982 #ifdef PNG_READ_EXPAND_SUPPORTED
982 png_ptr->transformations |= PNG_EXPAND; 983 png_ptr->transformations |= PNG_EXPAND;
983 #else 984 #else
984 { 985 {
985 /* Make this an error in 1.6 because otherwise the application may assume 986 /* Make this an error in 1.6 because otherwise the application may assume
986 * that it just worked and get a memory overwrite. 987 * that it just worked and get a memory overwrite.
987 */ 988 */
988 png_error(png_ptr, 989 png_error(png_ptr,
989 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED"); 990 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
990 991
991 /* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */ 992 /* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */
992 } 993 }
993 #endif 994 #endif
994 { 995 {
995 if (red >= 0 && green >= 0 && red + green <= PNG_FP_1) 996 if (red >= 0 && green >= 0 && red + green <= PNG_FP_1)
996 { 997 {
997 png_uint_16 red_int, green_int; 998 png_uint_16 red_int, green_int;
998 999
999 /* NOTE: this calculation does not round, but this behavior is retained 1000 /* NOTE: this calculation does not round, but this behavior is retained
1000 * for consistency, the inaccuracy is very small. The code here always 1001 * for consistency; the inaccuracy is very small. The code here always
1001 * overwrites the coefficients, regardless of whether they have been 1002 * overwrites the coefficients, regardless of whether they have been
1002 * defaulted or set already. 1003 * defaulted or set already.
1003 */ 1004 */
1004 red_int = (png_uint_16)(((png_uint_32)red*32768)/100000); 1005 red_int = (png_uint_16)(((png_uint_32)red*32768)/100000);
1005 green_int = (png_uint_16)(((png_uint_32)green*32768)/100000); 1006 green_int = (png_uint_16)(((png_uint_32)green*32768)/100000);
1006 1007
1007 png_ptr->rgb_to_gray_red_coeff = red_int; 1008 png_ptr->rgb_to_gray_red_coeff = red_int;
1008 png_ptr->rgb_to_gray_green_coeff = green_int; 1009 png_ptr->rgb_to_gray_green_coeff = green_int;
1009 png_ptr->rgb_to_gray_coefficients_set = 1; 1010 png_ptr->rgb_to_gray_coefficients_set = 1;
1010 } 1011 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1083 png_fixed_point gtest; 1084 png_fixed_point gtest;
1084 return !png_muldiv(&gtest, screen_gamma, file_gamma, PNG_FP_1) || 1085 return !png_muldiv(&gtest, screen_gamma, file_gamma, PNG_FP_1) ||
1085 png_gamma_significant(gtest); 1086 png_gamma_significant(gtest);
1086 } 1087 }
1087 #endif 1088 #endif
1088 1089
1089 /* Initialize everything needed for the read. This includes modifying 1090 /* Initialize everything needed for the read. This includes modifying
1090 * the palette. 1091 * the palette.
1091 */ 1092 */
1092 1093
1093 /*For the moment 'png_init_palette_transformations' and 1094 /* For the moment 'png_init_palette_transformations' and
1094 * 'png_init_rgb_transformations' only do some flag canceling optimizations. 1095 * 'png_init_rgb_transformations' only do some flag canceling optimizations.
1095 * The intent is that these two routines should have palette or rgb operations 1096 * The intent is that these two routines should have palette or rgb operations
1096 * extracted from 'png_init_read_transformations'. 1097 * extracted from 'png_init_read_transformations'.
1097 */ 1098 */
1098 static void /* PRIVATE */ 1099 static void /* PRIVATE */
1099 png_init_palette_transformations(png_structrp png_ptr) 1100 png_init_palette_transformations(png_structrp png_ptr)
1100 { 1101 {
1101 /* Called to handle the (input) palette case. In png_do_read_transformations 1102 /* Called to handle the (input) palette case. In png_do_read_transformations
1102 * the first step is to expand the palette if requested, so this code must 1103 * the first step is to expand the palette if requested, so this code must
1103 * take care to only make changes that are invariant with respect to the 1104 * take care to only make changes that are invariant with respect to the
1104 * palette expansion, or only do them if there is no expansion. 1105 * palette expansion, or only do them if there is no expansion.
1105 * 1106 *
1106 * STRIP_ALPHA has already been handled in the caller (by setting num_trans 1107 * STRIP_ALPHA has already been handled in the caller (by setting num_trans
1107 * to 0.) 1108 * to 0.)
1108 */ 1109 */
1109 int input_has_alpha = 0; 1110 int input_has_alpha = 0;
1110 int input_has_transparency = 0; 1111 int input_has_transparency = 0;
1111 1112
1112 if (png_ptr->num_trans > 0) 1113 if (png_ptr->num_trans > 0)
1113 { 1114 {
1114 int i; 1115 int i;
1115 1116
1116 /* Ignore if all the entries are opaque (unlikely!) */ 1117 /* Ignore if all the entries are opaque (unlikely!) */
1117 for (i=0; i<png_ptr->num_trans; ++i) 1118 for (i=0; i<png_ptr->num_trans; ++i)
1119 {
1118 if (png_ptr->trans_alpha[i] == 255) 1120 if (png_ptr->trans_alpha[i] == 255)
1119 continue; 1121 continue;
1120 else if (png_ptr->trans_alpha[i] == 0) 1122 else if (png_ptr->trans_alpha[i] == 0)
1121 input_has_transparency = 1; 1123 input_has_transparency = 1;
1122 else 1124 else
1125 {
1126 input_has_transparency = 1;
1123 input_has_alpha = 1; 1127 input_has_alpha = 1;
1128 break;
1129 }
1130 }
1124 } 1131 }
1125 1132
1126 /* If no alpha we can optimize. */ 1133 /* If no alpha we can optimize. */
1127 if (!input_has_alpha) 1134 if (input_has_alpha == 0)
1128 { 1135 {
1129 /* Any alpha means background and associative alpha processing is 1136 /* Any alpha means background and associative alpha processing is
1130 * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA 1137 * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA
1131 * and ENCODE_ALPHA are irrelevant. 1138 * and ENCODE_ALPHA are irrelevant.
1132 */ 1139 */
1133 png_ptr->transformations &= ~PNG_ENCODE_ALPHA; 1140 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1134 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; 1141 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1135 1142
1136 if (!input_has_transparency) 1143 if (input_has_transparency == 0)
1137 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); 1144 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
1138 } 1145 }
1139 1146
1140 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) 1147 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1141 /* png_set_background handling - deals with the complexity of whether the 1148 /* png_set_background handling - deals with the complexity of whether the
1142 * background color is in the file format or the screen format in the case 1149 * background color is in the file format or the screen format in the case
1143 * where an 'expand' will happen. 1150 * where an 'expand' will happen.
1144 */ 1151 */
1145 1152
1146 /* The following code cannot be entered in the alpha pre-multiplication case 1153 /* The following code cannot be entered in the alpha pre-multiplication case
1147 * because PNG_BACKGROUND_EXPAND is cancelled below. 1154 * because PNG_BACKGROUND_EXPAND is cancelled below.
1148 */ 1155 */
1149 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && 1156 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 &&
1150 (png_ptr->transformations & PNG_EXPAND)) 1157 (png_ptr->transformations & PNG_EXPAND) != 0)
1151 { 1158 {
1152 { 1159 {
1153 png_ptr->background.red = 1160 png_ptr->background.red =
1154 png_ptr->palette[png_ptr->background.index].red; 1161 png_ptr->palette[png_ptr->background.index].red;
1155 png_ptr->background.green = 1162 png_ptr->background.green =
1156 png_ptr->palette[png_ptr->background.index].green; 1163 png_ptr->palette[png_ptr->background.index].green;
1157 png_ptr->background.blue = 1164 png_ptr->background.blue =
1158 png_ptr->palette[png_ptr->background.index].blue; 1165 png_ptr->palette[png_ptr->background.index].blue;
1159 1166
1160 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED 1167 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1161 if (png_ptr->transformations & PNG_INVERT_ALPHA) 1168 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
1162 { 1169 {
1163 if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) 1170 if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0)
1164 { 1171 {
1165 /* Invert the alpha channel (in tRNS) unless the pixels are 1172 /* Invert the alpha channel (in tRNS) unless the pixels are
1166 * going to be expanded, in which case leave it for later 1173 * going to be expanded, in which case leave it for later
1167 */ 1174 */
1168 int i, istop = png_ptr->num_trans; 1175 int i, istop = png_ptr->num_trans;
1169 1176
1170 for (i=0; i<istop; i++) 1177 for (i=0; i<istop; i++)
1171 png_ptr->trans_alpha[i] = (png_byte)(255 - 1178 png_ptr->trans_alpha[i] = (png_byte)(255 -
1172 png_ptr->trans_alpha[i]); 1179 png_ptr->trans_alpha[i]);
1173 } 1180 }
1174 } 1181 }
1175 #endif /* PNG_READ_INVERT_ALPHA_SUPPORTED */ 1182 #endif /* READ_INVERT_ALPHA */
1176 } 1183 }
1177 } /* background expand and (therefore) no alpha association. */ 1184 } /* background expand and (therefore) no alpha association. */
1178 #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */ 1185 #endif /* READ_EXPAND && READ_BACKGROUND */
1179 } 1186 }
1180 1187
1181 static void /* PRIVATE */ 1188 static void /* PRIVATE */
1182 png_init_rgb_transformations(png_structrp png_ptr) 1189 png_init_rgb_transformations(png_structrp png_ptr)
1183 { 1190 {
1184 /* Added to libpng-1.5.4: check the color type to determine whether there 1191 /* Added to libpng-1.5.4: check the color type to determine whether there
1185 * is any alpha or transparency in the image and simply cancel the 1192 * is any alpha or transparency in the image and simply cancel the
1186 * background and alpha mode stuff if there isn't. 1193 * background and alpha mode stuff if there isn't.
1187 */ 1194 */
1188 int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0; 1195 int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0;
1189 int input_has_transparency = png_ptr->num_trans > 0; 1196 int input_has_transparency = png_ptr->num_trans > 0;
1190 1197
1191 /* If no alpha we can optimize. */ 1198 /* If no alpha we can optimize. */
1192 if (!input_has_alpha) 1199 if (input_has_alpha == 0)
1193 { 1200 {
1194 /* Any alpha means background and associative alpha processing is 1201 /* Any alpha means background and associative alpha processing is
1195 * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA 1202 * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA
1196 * and ENCODE_ALPHA are irrelevant. 1203 * and ENCODE_ALPHA are irrelevant.
1197 */ 1204 */
1198 # ifdef PNG_READ_ALPHA_MODE_SUPPORTED 1205 # ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1199 png_ptr->transformations &= ~PNG_ENCODE_ALPHA; 1206 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1200 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; 1207 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1201 # endif 1208 # endif
1202 1209
1203 if (!input_has_transparency) 1210 if (input_has_transparency == 0)
1204 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); 1211 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
1205 } 1212 }
1206 1213
1207 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) 1214 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1208 /* png_set_background handling - deals with the complexity of whether the 1215 /* png_set_background handling - deals with the complexity of whether the
1209 * background color is in the file format or the screen format in the case 1216 * background color is in the file format or the screen format in the case
1210 * where an 'expand' will happen. 1217 * where an 'expand' will happen.
1211 */ 1218 */
1212 1219
1213 /* The following code cannot be entered in the alpha pre-multiplication case 1220 /* The following code cannot be entered in the alpha pre-multiplication case
1214 * because PNG_BACKGROUND_EXPAND is cancelled below. 1221 * because PNG_BACKGROUND_EXPAND is cancelled below.
1215 */ 1222 */
1216 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && 1223 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 &&
1217 (png_ptr->transformations & PNG_EXPAND) && 1224 (png_ptr->transformations & PNG_EXPAND) != 0 &&
1218 !(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) 1225 (png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
1219 /* i.e., GRAY or GRAY_ALPHA */ 1226 /* i.e., GRAY or GRAY_ALPHA */
1220 { 1227 {
1221 { 1228 {
1222 /* Expand background and tRNS chunks */ 1229 /* Expand background and tRNS chunks */
1223 int gray = png_ptr->background.gray; 1230 int gray = png_ptr->background.gray;
1224 int trans_gray = png_ptr->trans_color.gray; 1231 int trans_gray = png_ptr->trans_color.gray;
1225 1232
1226 switch (png_ptr->bit_depth) 1233 switch (png_ptr->bit_depth)
1227 { 1234 {
1228 case 1: 1235 case 1:
(...skipping 17 matching lines...) Expand all
1246 /* FALL THROUGH (Already 8 bits) */ 1253 /* FALL THROUGH (Already 8 bits) */
1247 1254
1248 case 16: 1255 case 16:
1249 /* Already a full 16 bits */ 1256 /* Already a full 16 bits */
1250 break; 1257 break;
1251 } 1258 }
1252 1259
1253 png_ptr->background.red = png_ptr->background.green = 1260 png_ptr->background.red = png_ptr->background.green =
1254 png_ptr->background.blue = (png_uint_16)gray; 1261 png_ptr->background.blue = (png_uint_16)gray;
1255 1262
1256 if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) 1263 if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0)
1257 { 1264 {
1258 png_ptr->trans_color.red = png_ptr->trans_color.green = 1265 png_ptr->trans_color.red = png_ptr->trans_color.green =
1259 png_ptr->trans_color.blue = (png_uint_16)trans_gray; 1266 png_ptr->trans_color.blue = (png_uint_16)trans_gray;
1260 } 1267 }
1261 } 1268 }
1262 } /* background expand and (therefore) no alpha association. */ 1269 } /* background expand and (therefore) no alpha association. */
1263 #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */ 1270 #endif /* READ_EXPAND && READ_BACKGROUND */
1264 } 1271 }
1265 1272
1266 void /* PRIVATE */ 1273 void /* PRIVATE */
1267 png_init_read_transformations(png_structrp png_ptr) 1274 png_init_read_transformations(png_structrp png_ptr)
1268 { 1275 {
1269 png_debug(1, "in png_init_read_transformations"); 1276 png_debug(1, "in png_init_read_transformations");
1270 1277
1271 /* This internal function is called from png_read_start_row in pngrutil.c 1278 /* This internal function is called from png_read_start_row in pngrutil.c
1272 * and it is called before the 'rowbytes' calculation is done, so the code 1279 * and it is called before the 'rowbytes' calculation is done, so the code
1273 * in here can change or update the transformations flags. 1280 * in here can change or update the transformations flags.
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1322 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; 1329 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
1323 1330
1324 /* Now turn the gamma transformation on or off as appropriate. Notice 1331 /* Now turn the gamma transformation on or off as appropriate. Notice
1325 * that PNG_GAMMA just refers to the file->screen correction. Alpha 1332 * that PNG_GAMMA just refers to the file->screen correction. Alpha
1326 * composition may independently cause gamma correction because it needs 1333 * composition may independently cause gamma correction because it needs
1327 * linear data (e.g. if the file has a gAMA chunk but the screen gamma 1334 * linear data (e.g. if the file has a gAMA chunk but the screen gamma
1328 * hasn't been specified.) In any case this flag may get turned off in 1335 * hasn't been specified.) In any case this flag may get turned off in
1329 * the code immediately below if the transform can be handled outside the 1336 * the code immediately below if the transform can be handled outside the
1330 * row loop. 1337 * row loop.
1331 */ 1338 */
1332 if (gamma_correction) 1339 if (gamma_correction != 0)
1333 png_ptr->transformations |= PNG_GAMMA; 1340 png_ptr->transformations |= PNG_GAMMA;
1334 1341
1335 else 1342 else
1336 png_ptr->transformations &= ~PNG_GAMMA; 1343 png_ptr->transformations &= ~PNG_GAMMA;
1337 } 1344 }
1338 #endif 1345 #endif
1339 1346
1340 /* Certain transformations have the effect of preventing other 1347 /* Certain transformations have the effect of preventing other
1341 * transformations that happen afterward in png_do_read_transformations, 1348 * transformations that happen afterward in png_do_read_transformations;
1342 * resolve the interdependencies here. From the code of 1349 * resolve the interdependencies here. From the code of
1343 * png_do_read_transformations the order is: 1350 * png_do_read_transformations the order is:
1344 * 1351 *
1345 * 1) PNG_EXPAND (including PNG_EXPAND_tRNS) 1352 * 1) PNG_EXPAND (including PNG_EXPAND_tRNS)
1346 * 2) PNG_STRIP_ALPHA (if no compose) 1353 * 2) PNG_STRIP_ALPHA (if no compose)
1347 * 3) PNG_RGB_TO_GRAY 1354 * 3) PNG_RGB_TO_GRAY
1348 * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY 1355 * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY
1349 * 5) PNG_COMPOSE 1356 * 5) PNG_COMPOSE
1350 * 6) PNG_GAMMA 1357 * 6) PNG_GAMMA
1351 * 7) PNG_STRIP_ALPHA (if compose) 1358 * 7) PNG_STRIP_ALPHA (if compose)
1352 * 8) PNG_ENCODE_ALPHA 1359 * 8) PNG_ENCODE_ALPHA
1353 * 9) PNG_SCALE_16_TO_8 1360 * 9) PNG_SCALE_16_TO_8
1354 * 10) PNG_16_TO_8 1361 * 10) PNG_16_TO_8
1355 * 11) PNG_QUANTIZE (converts to palette) 1362 * 11) PNG_QUANTIZE (converts to palette)
1356 * 12) PNG_EXPAND_16 1363 * 12) PNG_EXPAND_16
1357 * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY 1364 * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY
1358 * 14) PNG_INVERT_MONO 1365 * 14) PNG_INVERT_MONO
1359 * 15) PNG_SHIFT 1366 * 15) PNG_INVERT_ALPHA
1360 * 16) PNG_PACK 1367 * 16) PNG_SHIFT
1361 * 17) PNG_BGR 1368 * 17) PNG_PACK
1362 * 18) PNG_PACKSWAP 1369 * 18) PNG_BGR
1363 * 19) PNG_FILLER (includes PNG_ADD_ALPHA) 1370 * 19) PNG_PACKSWAP
1364 * 20) PNG_INVERT_ALPHA 1371 * 20) PNG_FILLER (includes PNG_ADD_ALPHA)
1365 * 21) PNG_SWAP_ALPHA 1372 * 21) PNG_SWAP_ALPHA
1366 * 22) PNG_SWAP_BYTES 1373 * 22) PNG_SWAP_BYTES
1367 * 23) PNG_USER_TRANSFORM [must be last] 1374 * 23) PNG_USER_TRANSFORM [must be last]
1368 */ 1375 */
1369 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED 1376 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1370 if ((png_ptr->transformations & PNG_STRIP_ALPHA) && 1377 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
1371 !(png_ptr->transformations & PNG_COMPOSE)) 1378 (png_ptr->transformations & PNG_COMPOSE) == 0)
1372 { 1379 {
1373 /* Stripping the alpha channel happens immediately after the 'expand' 1380 /* Stripping the alpha channel happens immediately after the 'expand'
1374 * transformations, before all other transformation, so it cancels out 1381 * transformations, before all other transformation, so it cancels out
1375 * the alpha handling. It has the side effect negating the effect of 1382 * the alpha handling. It has the side effect negating the effect of
1376 * PNG_EXPAND_tRNS too: 1383 * PNG_EXPAND_tRNS too:
1377 */ 1384 */
1378 png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA | 1385 png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA |
1379 PNG_EXPAND_tRNS); 1386 PNG_EXPAND_tRNS);
1380 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; 1387 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1381 1388
1382 /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen 1389 /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen
1383 * so transparency information would remain just so long as it wasn't 1390 * so transparency information would remain just so long as it wasn't
1384 * expanded. This produces unexpected API changes if the set of things 1391 * expanded. This produces unexpected API changes if the set of things
1385 * that do PNG_EXPAND_tRNS changes (perfectly possible given the 1392 * that do PNG_EXPAND_tRNS changes (perfectly possible given the
1386 * documentation - which says ask for what you want, accept what you 1393 * documentation - which says ask for what you want, accept what you
1387 * get.) This makes the behavior consistent from 1.5.4: 1394 * get.) This makes the behavior consistent from 1.5.4:
1388 */ 1395 */
1389 png_ptr->num_trans = 0; 1396 png_ptr->num_trans = 0;
1390 } 1397 }
1391 #endif /* STRIP_ALPHA supported, no COMPOSE */ 1398 #endif /* STRIP_ALPHA supported, no COMPOSE */
1392 1399
1393 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED 1400 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1394 /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA 1401 /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA
1395 * settings will have no effect. 1402 * settings will have no effect.
1396 */ 1403 */
1397 if (!png_gamma_significant(png_ptr->screen_gamma)) 1404 if (png_gamma_significant(png_ptr->screen_gamma) == 0)
1398 { 1405 {
1399 png_ptr->transformations &= ~PNG_ENCODE_ALPHA; 1406 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1400 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; 1407 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1401 } 1408 }
1402 #endif 1409 #endif
1403 1410
1404 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED 1411 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1405 /* Make sure the coefficients for the rgb to gray conversion are set 1412 /* Make sure the coefficients for the rgb to gray conversion are set
1406 * appropriately. 1413 * appropriately.
1407 */ 1414 */
1408 if (png_ptr->transformations & PNG_RGB_TO_GRAY) 1415 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
1409 png_colorspace_set_rgb_coefficients(png_ptr); 1416 png_colorspace_set_rgb_coefficients(png_ptr);
1410 #endif 1417 #endif
1411 1418
1412 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED 1419 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1413 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) 1420 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1414 /* Detect gray background and attempt to enable optimization for 1421 /* Detect gray background and attempt to enable optimization for
1415 * gray --> RGB case. 1422 * gray --> RGB case.
1416 * 1423 *
1417 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or 1424 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
1418 * RGB_ALPHA (in which case need_expand is superfluous anyway), the 1425 * RGB_ALPHA (in which case need_expand is superfluous anyway), the
1419 * background color might actually be gray yet not be flagged as such. 1426 * background color might actually be gray yet not be flagged as such.
1420 * This is not a problem for the current code, which uses 1427 * This is not a problem for the current code, which uses
1421 * PNG_BACKGROUND_IS_GRAY only to decide when to do the 1428 * PNG_BACKGROUND_IS_GRAY only to decide when to do the
1422 * png_do_gray_to_rgb() transformation. 1429 * png_do_gray_to_rgb() transformation.
1423 * 1430 *
1424 * TODO: this code needs to be revised to avoid the complexity and 1431 * TODO: this code needs to be revised to avoid the complexity and
1425 * interdependencies. The color type of the background should be recorded in 1432 * interdependencies. The color type of the background should be recorded in
1426 * png_set_background, along with the bit depth, then the code has a record 1433 * png_set_background, along with the bit depth, then the code has a record
1427 * of exactly what color space the background is currently in. 1434 * of exactly what color space the background is currently in.
1428 */ 1435 */
1429 if (png_ptr->transformations & PNG_BACKGROUND_EXPAND) 1436 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0)
1430 { 1437 {
1431 /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if 1438 /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if
1432 * the file was grayscale the background value is gray. 1439 * the file was grayscale the background value is gray.
1433 */ 1440 */
1434 if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) 1441 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
1435 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; 1442 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1436 } 1443 }
1437 1444
1438 else if (png_ptr->transformations & PNG_COMPOSE) 1445 else if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1439 { 1446 {
1440 /* PNG_COMPOSE: png_set_background was called with need_expand false, 1447 /* PNG_COMPOSE: png_set_background was called with need_expand false,
1441 * so the color is in the color space of the output or png_set_alpha_mode 1448 * so the color is in the color space of the output or png_set_alpha_mode
1442 * was called and the color is black. Ignore RGB_TO_GRAY because that 1449 * was called and the color is black. Ignore RGB_TO_GRAY because that
1443 * happens before GRAY_TO_RGB. 1450 * happens before GRAY_TO_RGB.
1444 */ 1451 */
1445 if (png_ptr->transformations & PNG_GRAY_TO_RGB) 1452 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
1446 { 1453 {
1447 if (png_ptr->background.red == png_ptr->background.green && 1454 if (png_ptr->background.red == png_ptr->background.green &&
1448 png_ptr->background.red == png_ptr->background.blue) 1455 png_ptr->background.red == png_ptr->background.blue)
1449 { 1456 {
1450 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; 1457 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1451 png_ptr->background.gray = png_ptr->background.red; 1458 png_ptr->background.gray = png_ptr->background.red;
1452 } 1459 }
1453 } 1460 }
1454 } 1461 }
1455 #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */ 1462 #endif /* READ_EXPAND && READ_BACKGROUND */
1456 #endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED */ 1463 #endif /* READ_GRAY_TO_RGB */
1457 1464
1458 /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations 1465 /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations
1459 * can be performed directly on the palette, and some (such as rgb to gray) 1466 * can be performed directly on the palette, and some (such as rgb to gray)
1460 * can be optimized inside the palette. This is particularly true of the 1467 * can be optimized inside the palette. This is particularly true of the
1461 * composite (background and alpha) stuff, which can be pretty much all done 1468 * composite (background and alpha) stuff, which can be pretty much all done
1462 * in the palette even if the result is expanded to RGB or gray afterward. 1469 * in the palette even if the result is expanded to RGB or gray afterward.
1463 * 1470 *
1464 * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and 1471 * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and
1465 * earlier and the palette stuff is actually handled on the first row. This 1472 * earlier and the palette stuff is actually handled on the first row. This
1466 * leads to the reported bug that the palette returned by png_get_PLTE is not 1473 * leads to the reported bug that the palette returned by png_get_PLTE is not
1467 * updated. 1474 * updated.
1468 */ 1475 */
1469 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 1476 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1470 png_init_palette_transformations(png_ptr); 1477 png_init_palette_transformations(png_ptr);
1471 1478
1472 else 1479 else
1473 png_init_rgb_transformations(png_ptr); 1480 png_init_rgb_transformations(png_ptr);
1474 1481
1475 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ 1482 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1476 defined(PNG_READ_EXPAND_16_SUPPORTED) 1483 defined(PNG_READ_EXPAND_16_SUPPORTED)
1477 if ((png_ptr->transformations & PNG_EXPAND_16) && 1484 if ((png_ptr->transformations & PNG_EXPAND_16) != 0 &&
1478 (png_ptr->transformations & PNG_COMPOSE) && 1485 (png_ptr->transformations & PNG_COMPOSE) != 0 &&
1479 !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) && 1486 (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 &&
1480 png_ptr->bit_depth != 16) 1487 png_ptr->bit_depth != 16)
1481 { 1488 {
1482 /* TODO: fix this. Because the expand_16 operation is after the compose 1489 /* TODO: fix this. Because the expand_16 operation is after the compose
1483 * handling the background color must be 8, not 16, bits deep, but the 1490 * handling the background color must be 8, not 16, bits deep, but the
1484 * application will supply a 16-bit value so reduce it here. 1491 * application will supply a 16-bit value so reduce it here.
1485 * 1492 *
1486 * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at 1493 * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at
1487 * present, so that case is ok (until do_expand_16 is moved.) 1494 * present, so that case is ok (until do_expand_16 is moved.)
1488 * 1495 *
1489 * NOTE: this discards the low 16 bits of the user supplied background 1496 * NOTE: this discards the low 16 bits of the user supplied background
1490 * color, but until expand_16 works properly there is no choice! 1497 * color, but until expand_16 works properly there is no choice!
1491 */ 1498 */
1492 # define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x)) 1499 # define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x))
1493 CHOP(png_ptr->background.red); 1500 CHOP(png_ptr->background.red);
1494 CHOP(png_ptr->background.green); 1501 CHOP(png_ptr->background.green);
1495 CHOP(png_ptr->background.blue); 1502 CHOP(png_ptr->background.blue);
1496 CHOP(png_ptr->background.gray); 1503 CHOP(png_ptr->background.gray);
1497 # undef CHOP 1504 # undef CHOP
1498 } 1505 }
1499 #endif /* PNG_READ_BACKGROUND_SUPPORTED && PNG_READ_EXPAND_16_SUPPORTED */ 1506 #endif /* READ_BACKGROUND && READ_EXPAND_16 */
1500 1507
1501 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ 1508 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1502 (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \ 1509 (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \
1503 defined(PNG_READ_STRIP_16_TO_8_SUPPORTED)) 1510 defined(PNG_READ_STRIP_16_TO_8_SUPPORTED))
1504 if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) && 1511 if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) != 0 &&
1505 (png_ptr->transformations & PNG_COMPOSE) && 1512 (png_ptr->transformations & PNG_COMPOSE) != 0 &&
1506 !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) && 1513 (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 &&
1507 png_ptr->bit_depth == 16) 1514 png_ptr->bit_depth == 16)
1508 { 1515 {
1509 /* On the other hand, if a 16-bit file is to be reduced to 8-bits per 1516 /* On the other hand, if a 16-bit file is to be reduced to 8-bits per
1510 * component this will also happen after PNG_COMPOSE and so the background 1517 * component this will also happen after PNG_COMPOSE and so the background
1511 * color must be pre-expanded here. 1518 * color must be pre-expanded here.
1512 * 1519 *
1513 * TODO: fix this too. 1520 * TODO: fix this too.
1514 */ 1521 */
1515 png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257); 1522 png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257);
1516 png_ptr->background.green = 1523 png_ptr->background.green =
1517 (png_uint_16)(png_ptr->background.green * 257); 1524 (png_uint_16)(png_ptr->background.green * 257);
(...skipping 22 matching lines...) Expand all
1540 * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that 1547 * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that
1541 * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the 1548 * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the
1542 * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction 1549 * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction
1543 * the gamma tables will not be built even if composition is required on a 1550 * the gamma tables will not be built even if composition is required on a
1544 * gamma encoded value. 1551 * gamma encoded value.
1545 * 1552 *
1546 * In 1.5.4 this is addressed below by an additional check on the individual 1553 * In 1.5.4 this is addressed below by an additional check on the individual
1547 * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the 1554 * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the
1548 * tables. 1555 * tables.
1549 */ 1556 */
1550 if ((png_ptr->transformations & PNG_GAMMA) 1557 if ((png_ptr->transformations & PNG_GAMMA) != 0 ||
1551 || ((png_ptr->transformations & PNG_RGB_TO_GRAY) 1558 ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0 &&
1552 && (png_gamma_significant(png_ptr->colorspace.gamma) || 1559 (png_gamma_significant(png_ptr->colorspace.gamma) != 0 ||
1553 png_gamma_significant(png_ptr->screen_gamma))) 1560 png_gamma_significant(png_ptr->screen_gamma) != 0)) ||
1554 || ((png_ptr->transformations & PNG_COMPOSE) 1561 ((png_ptr->transformations & PNG_COMPOSE) != 0 &&
1555 && (png_gamma_significant(png_ptr->colorspace.gamma) 1562 (png_gamma_significant(png_ptr->colorspace.gamma) != 0 ||
1556 || png_gamma_significant(png_ptr->screen_gamma) 1563 png_gamma_significant(png_ptr->screen_gamma) != 0
1557 # ifdef PNG_READ_BACKGROUND_SUPPORTED 1564 # ifdef PNG_READ_BACKGROUND_SUPPORTED
1558 || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE 1565 || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE &&
1559 && png_gamma_significant(png_ptr->background_gamma)) 1566 png_gamma_significant(png_ptr->background_gamma) != 0)
1560 # endif 1567 # endif
1561 )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) 1568 )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 &&
1562 && png_gamma_significant(png_ptr->screen_gamma)) 1569 png_gamma_significant(png_ptr->screen_gamma) != 0))
1563 )
1564 { 1570 {
1565 png_build_gamma_table(png_ptr, png_ptr->bit_depth); 1571 png_build_gamma_table(png_ptr, png_ptr->bit_depth);
1566 1572
1567 #ifdef PNG_READ_BACKGROUND_SUPPORTED 1573 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1568 if (png_ptr->transformations & PNG_COMPOSE) 1574 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1569 { 1575 {
1570 /* Issue a warning about this combination: because RGB_TO_GRAY is 1576 /* Issue a warning about this combination: because RGB_TO_GRAY is
1571 * optimized to do the gamma transform if present yet do_background has 1577 * optimized to do the gamma transform if present yet do_background has
1572 * to do the same thing if both options are set a 1578 * to do the same thing if both options are set a
1573 * double-gamma-correction happens. This is true in all versions of 1579 * double-gamma-correction happens. This is true in all versions of
1574 * libpng to date. 1580 * libpng to date.
1575 */ 1581 */
1576 if (png_ptr->transformations & PNG_RGB_TO_GRAY) 1582 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
1577 png_warning(png_ptr, 1583 png_warning(png_ptr,
1578 "libpng does not support gamma+background+rgb_to_gray"); 1584 "libpng does not support gamma+background+rgb_to_gray");
1579 1585
1580 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 1586 if ((png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) != 0)
1581 { 1587 {
1582 /* We don't get to here unless there is a tRNS chunk with non-opaque 1588 /* We don't get to here unless there is a tRNS chunk with non-opaque
1583 * entries - see the checking code at the start of this function. 1589 * entries - see the checking code at the start of this function.
1584 */ 1590 */
1585 png_color back, back_1; 1591 png_color back, back_1;
1586 png_colorp palette = png_ptr->palette; 1592 png_colorp palette = png_ptr->palette;
1587 int num_palette = png_ptr->num_palette; 1593 int num_palette = png_ptr->num_palette;
1588 int i; 1594 int i;
1589 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) 1595 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
1590 { 1596 {
(...skipping 27 matching lines...) Expand all
1618 g = png_reciprocal(png_ptr->background_gamma); 1624 g = png_reciprocal(png_ptr->background_gamma);
1619 gs = png_reciprocal2(png_ptr->background_gamma, 1625 gs = png_reciprocal2(png_ptr->background_gamma,
1620 png_ptr->screen_gamma); 1626 png_ptr->screen_gamma);
1621 break; 1627 break;
1622 default: 1628 default:
1623 g = PNG_FP_1; /* back_1 */ 1629 g = PNG_FP_1; /* back_1 */
1624 gs = PNG_FP_1; /* back */ 1630 gs = PNG_FP_1; /* back */
1625 break; 1631 break;
1626 } 1632 }
1627 1633
1628 if (png_gamma_significant(gs)) 1634 if (png_gamma_significant(gs) != 0)
1629 { 1635 {
1630 back.red = png_gamma_8bit_correct(png_ptr->background.red, 1636 back.red = png_gamma_8bit_correct(png_ptr->background.red,
1631 gs); 1637 gs);
1632 back.green = png_gamma_8bit_correct(png_ptr->background.green, 1638 back.green = png_gamma_8bit_correct(png_ptr->background.green,
1633 gs); 1639 gs);
1634 back.blue = png_gamma_8bit_correct(png_ptr->background.blue, 1640 back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1635 gs); 1641 gs);
1636 } 1642 }
1637 1643
1638 else 1644 else
1639 { 1645 {
1640 back.red = (png_byte)png_ptr->background.red; 1646 back.red = (png_byte)png_ptr->background.red;
1641 back.green = (png_byte)png_ptr->background.green; 1647 back.green = (png_byte)png_ptr->background.green;
1642 back.blue = (png_byte)png_ptr->background.blue; 1648 back.blue = (png_byte)png_ptr->background.blue;
1643 } 1649 }
1644 1650
1645 if (png_gamma_significant(g)) 1651 if (png_gamma_significant(g) != 0)
1646 { 1652 {
1647 back_1.red = png_gamma_8bit_correct(png_ptr->background.red, 1653 back_1.red = png_gamma_8bit_correct(png_ptr->background.red,
1648 g); 1654 g);
1649 back_1.green = png_gamma_8bit_correct( 1655 back_1.green = png_gamma_8bit_correct(
1650 png_ptr->background.green, g); 1656 png_ptr->background.green, g);
1651 back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue, 1657 back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1652 g); 1658 g);
1653 } 1659 }
1654 1660
1655 else 1661 else
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
1729 png_ptr->screen_gamma); 1735 png_ptr->screen_gamma);
1730 break; 1736 break;
1731 1737
1732 default: 1738 default:
1733 png_error(png_ptr, "invalid background gamma type"); 1739 png_error(png_ptr, "invalid background gamma type");
1734 } 1740 }
1735 1741
1736 g_sig = png_gamma_significant(g); 1742 g_sig = png_gamma_significant(g);
1737 gs_sig = png_gamma_significant(gs); 1743 gs_sig = png_gamma_significant(gs);
1738 1744
1739 if (g_sig) 1745 if (g_sig != 0)
1740 png_ptr->background_1.gray = png_gamma_correct(png_ptr, 1746 png_ptr->background_1.gray = png_gamma_correct(png_ptr,
1741 png_ptr->background.gray, g); 1747 png_ptr->background.gray, g);
1742 1748
1743 if (gs_sig) 1749 if (gs_sig != 0)
1744 png_ptr->background.gray = png_gamma_correct(png_ptr, 1750 png_ptr->background.gray = png_gamma_correct(png_ptr,
1745 png_ptr->background.gray, gs); 1751 png_ptr->background.gray, gs);
1746 1752
1747 if ((png_ptr->background.red != png_ptr->background.green) || 1753 if ((png_ptr->background.red != png_ptr->background.green) ||
1748 (png_ptr->background.red != png_ptr->background.blue) || 1754 (png_ptr->background.red != png_ptr->background.blue) ||
1749 (png_ptr->background.red != png_ptr->background.gray)) 1755 (png_ptr->background.red != png_ptr->background.gray))
1750 { 1756 {
1751 /* RGB or RGBA with color background */ 1757 /* RGB or RGBA with color background */
1752 if (g_sig) 1758 if (g_sig != 0)
1753 { 1759 {
1754 png_ptr->background_1.red = png_gamma_correct(png_ptr, 1760 png_ptr->background_1.red = png_gamma_correct(png_ptr,
1755 png_ptr->background.red, g); 1761 png_ptr->background.red, g);
1756 1762
1757 png_ptr->background_1.green = png_gamma_correct(png_ptr, 1763 png_ptr->background_1.green = png_gamma_correct(png_ptr,
1758 png_ptr->background.green, g); 1764 png_ptr->background.green, g);
1759 1765
1760 png_ptr->background_1.blue = png_gamma_correct(png_ptr, 1766 png_ptr->background_1.blue = png_gamma_correct(png_ptr,
1761 png_ptr->background.blue, g); 1767 png_ptr->background.blue, g);
1762 } 1768 }
1763 1769
1764 if (gs_sig) 1770 if (gs_sig != 0)
1765 { 1771 {
1766 png_ptr->background.red = png_gamma_correct(png_ptr, 1772 png_ptr->background.red = png_gamma_correct(png_ptr,
1767 png_ptr->background.red, gs); 1773 png_ptr->background.red, gs);
1768 1774
1769 png_ptr->background.green = png_gamma_correct(png_ptr, 1775 png_ptr->background.green = png_gamma_correct(png_ptr,
1770 png_ptr->background.green, gs); 1776 png_ptr->background.green, gs);
1771 1777
1772 png_ptr->background.blue = png_gamma_correct(png_ptr, 1778 png_ptr->background.blue = png_gamma_correct(png_ptr,
1773 png_ptr->background.blue, gs); 1779 png_ptr->background.blue, gs);
1774 } 1780 }
1775 } 1781 }
1776 1782
1777 else 1783 else
1778 { 1784 {
1779 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */ 1785 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1780 png_ptr->background_1.red = png_ptr->background_1.green 1786 png_ptr->background_1.red = png_ptr->background_1.green
1781 = png_ptr->background_1.blue = png_ptr->background_1.gray; 1787 = png_ptr->background_1.blue = png_ptr->background_1.gray;
1782 1788
1783 png_ptr->background.red = png_ptr->background.green 1789 png_ptr->background.red = png_ptr->background.green
1784 = png_ptr->background.blue = png_ptr->background.gray; 1790 = png_ptr->background.blue = png_ptr->background.gray;
1785 } 1791 }
1786 1792
1787 /* The background is now in screen gamma: */ 1793 /* The background is now in screen gamma: */
1788 png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN; 1794 png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN;
1789 } /* color_type != PNG_COLOR_TYPE_PALETTE */ 1795 } /* color_type != PNG_COLOR_TYPE_PALETTE */
1790 }/* png_ptr->transformations & PNG_BACKGROUND */ 1796 }/* png_ptr->transformations & PNG_BACKGROUND */
1791 1797
1792 else 1798 else
1793 /* Transformation does not include PNG_BACKGROUND */ 1799 /* Transformation does not include PNG_BACKGROUND */
1794 #endif /* PNG_READ_BACKGROUND_SUPPORTED */ 1800 #endif /* READ_BACKGROUND */
1795 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE 1801 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE
1796 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED 1802 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1797 /* RGB_TO_GRAY needs to have non-gamma-corrected values! */ 1803 /* RGB_TO_GRAY needs to have non-gamma-corrected values! */
1798 && ((png_ptr->transformations & PNG_EXPAND) == 0 || 1804 && ((png_ptr->transformations & PNG_EXPAND) == 0 ||
1799 (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) 1805 (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
1800 #endif 1806 #endif
1801 ) 1807 )
1802 { 1808 {
1803 png_colorp palette = png_ptr->palette; 1809 png_colorp palette = png_ptr->palette;
1804 int num_palette = png_ptr->num_palette; 1810 int num_palette = png_ptr->num_palette;
1805 int i; 1811 int i;
1806 1812
1807 /* NOTE: there are other transformations that should probably be in 1813 /* NOTE: there are other transformations that should probably be in
1808 * here too. 1814 * here too.
1809 */ 1815 */
1810 for (i = 0; i < num_palette; i++) 1816 for (i = 0; i < num_palette; i++)
1811 { 1817 {
1812 palette[i].red = png_ptr->gamma_table[palette[i].red]; 1818 palette[i].red = png_ptr->gamma_table[palette[i].red];
1813 palette[i].green = png_ptr->gamma_table[palette[i].green]; 1819 palette[i].green = png_ptr->gamma_table[palette[i].green];
1814 palette[i].blue = png_ptr->gamma_table[palette[i].blue]; 1820 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1815 } 1821 }
1816 1822
1817 /* Done the gamma correction. */ 1823 /* Done the gamma correction. */
1818 png_ptr->transformations &= ~PNG_GAMMA; 1824 png_ptr->transformations &= ~PNG_GAMMA;
1819 } /* color_type == PALETTE && !PNG_BACKGROUND transformation */ 1825 } /* color_type == PALETTE && !PNG_BACKGROUND transformation */
1820 } 1826 }
1821 #ifdef PNG_READ_BACKGROUND_SUPPORTED 1827 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1822 else 1828 else
1823 #endif 1829 #endif
1824 #endif /* PNG_READ_GAMMA_SUPPORTED */ 1830 #endif /* READ_GAMMA */
1825 1831
1826 #ifdef PNG_READ_BACKGROUND_SUPPORTED 1832 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1827 /* No GAMMA transformation (see the hanging else 4 lines above) */ 1833 /* No GAMMA transformation (see the hanging else 4 lines above) */
1828 if ((png_ptr->transformations & PNG_COMPOSE) && 1834 if ((png_ptr->transformations & PNG_COMPOSE) != 0 &&
1829 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) 1835 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
1830 { 1836 {
1831 int i; 1837 int i;
1832 int istop = (int)png_ptr->num_trans; 1838 int istop = (int)png_ptr->num_trans;
1833 png_color back; 1839 png_color back;
1834 png_colorp palette = png_ptr->palette; 1840 png_colorp palette = png_ptr->palette;
1835 1841
1836 back.red = (png_byte)png_ptr->background.red; 1842 back.red = (png_byte)png_ptr->background.red;
1837 back.green = (png_byte)png_ptr->background.green; 1843 back.green = (png_byte)png_ptr->background.green;
1838 back.blue = (png_byte)png_ptr->background.blue; 1844 back.blue = (png_byte)png_ptr->background.blue;
(...skipping 13 matching lines...) Expand all
1852 1858
1853 png_composite(palette[i].green, palette[i].green, 1859 png_composite(palette[i].green, palette[i].green,
1854 png_ptr->trans_alpha[i], back.green); 1860 png_ptr->trans_alpha[i], back.green);
1855 1861
1856 png_composite(palette[i].blue, palette[i].blue, 1862 png_composite(palette[i].blue, palette[i].blue,
1857 png_ptr->trans_alpha[i], back.blue); 1863 png_ptr->trans_alpha[i], back.blue);
1858 } 1864 }
1859 } 1865 }
1860 1866
1861 png_ptr->transformations &= ~PNG_COMPOSE; 1867 png_ptr->transformations &= ~PNG_COMPOSE;
1862 » 1868 }
1863 » //png_ptr->flags |= PNG_FLAG_STRIP_ALPHA; /* Add by Sunliang.Liu */ 1869 #endif /* READ_BACKGROUND */
1864 }
1865 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1866 1870
1867 #ifdef PNG_READ_SHIFT_SUPPORTED 1871 #ifdef PNG_READ_SHIFT_SUPPORTED
1868 if ((png_ptr->transformations & PNG_SHIFT) && 1872 if ((png_ptr->transformations & PNG_SHIFT) != 0 &&
1869 !(png_ptr->transformations & PNG_EXPAND) && 1873 (png_ptr->transformations & PNG_EXPAND) == 0 &&
1870 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) 1874 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
1871 { 1875 {
1872 int i; 1876 int i;
1873 int istop = png_ptr->num_palette; 1877 int istop = png_ptr->num_palette;
1874 int shift = 8 - png_ptr->sig_bit.red; 1878 int shift = 8 - png_ptr->sig_bit.red;
1875 1879
1876 png_ptr->transformations &= ~PNG_SHIFT; 1880 png_ptr->transformations &= ~PNG_SHIFT;
1877 1881
1878 /* significant bits can be in the range 1 to 7 for a meaninful result, if 1882 /* significant bits can be in the range 1 to 7 for a meaninful result, if
1879 * the number of significant bits is 0 then no shift is done (this is an 1883 * the number of significant bits is 0 then no shift is done (this is an
1880 * error condition which is silently ignored.) 1884 * error condition which is silently ignored.)
1881 */ 1885 */
1882 if (shift > 0 && shift < 8) for (i=0; i<istop; ++i) 1886 if (shift > 0 && shift < 8)
1883 { 1887 for (i=0; i<istop; ++i)
1884 int component = png_ptr->palette[i].red; 1888 {
1885 1889 int component = png_ptr->palette[i].red;
1886 component >>= shift; 1890
1887 png_ptr->palette[i].red = (png_byte)component; 1891 component >>= shift;
1888 } 1892 png_ptr->palette[i].red = (png_byte)component;
1893 }
1889 1894
1890 shift = 8 - png_ptr->sig_bit.green; 1895 shift = 8 - png_ptr->sig_bit.green;
1891 if (shift > 0 && shift < 8) for (i=0; i<istop; ++i) 1896 if (shift > 0 && shift < 8)
1892 { 1897 for (i=0; i<istop; ++i)
1893 int component = png_ptr->palette[i].green; 1898 {
1894 1899 int component = png_ptr->palette[i].green;
1895 component >>= shift; 1900
1896 png_ptr->palette[i].green = (png_byte)component; 1901 component >>= shift;
1897 } 1902 png_ptr->palette[i].green = (png_byte)component;
1903 }
1898 1904
1899 shift = 8 - png_ptr->sig_bit.blue; 1905 shift = 8 - png_ptr->sig_bit.blue;
1900 if (shift > 0 && shift < 8) for (i=0; i<istop; ++i) 1906 if (shift > 0 && shift < 8)
1901 { 1907 for (i=0; i<istop; ++i)
1902 int component = png_ptr->palette[i].blue; 1908 {
1903 1909 int component = png_ptr->palette[i].blue;
1904 component >>= shift; 1910
1905 png_ptr->palette[i].blue = (png_byte)component; 1911 component >>= shift;
1906 } 1912 png_ptr->palette[i].blue = (png_byte)component;
1907 } 1913 }
1908 #endif /* PNG_READ_SHIFT_SUPPORTED */ 1914 }
1915 #endif /* READ_SHIFT */
1909 } 1916 }
1910 1917
1911 /* Modify the info structure to reflect the transformations. The 1918 /* Modify the info structure to reflect the transformations. The
1912 * info should be updated so a PNG file could be written with it, 1919 * info should be updated so a PNG file could be written with it,
1913 * assuming the transformations result in valid PNG data. 1920 * assuming the transformations result in valid PNG data.
1914 */ 1921 */
1915 void /* PRIVATE */ 1922 void /* PRIVATE */
1916 png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr) 1923 png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr)
1917 { 1924 {
1918 png_debug(1, "in png_read_transform_info"); 1925 png_debug(1, "in png_read_transform_info");
1919 1926
1920 #ifdef PNG_READ_EXPAND_SUPPORTED 1927 #ifdef PNG_READ_EXPAND_SUPPORTED
1921 if (png_ptr->transformations & PNG_EXPAND) 1928 if ((png_ptr->transformations & PNG_EXPAND) != 0)
1922 { 1929 {
1923 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 1930 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1924 { 1931 {
1925 /* This check must match what actually happens in 1932 /* This check must match what actually happens in
1926 * png_do_expand_palette; if it ever checks the tRNS chunk to see if 1933 * png_do_expand_palette; if it ever checks the tRNS chunk to see if
1927 * it is all opaque we must do the same (at present it does not.) 1934 * it is all opaque we must do the same (at present it does not.)
1928 */ 1935 */
1929 if (png_ptr->num_trans > 0) 1936 if (png_ptr->num_trans > 0)
1930 info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; 1937 info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1931 1938
1932 else 1939 else
1933 info_ptr->color_type = PNG_COLOR_TYPE_RGB; 1940 info_ptr->color_type = PNG_COLOR_TYPE_RGB;
1934 1941
1935 info_ptr->bit_depth = 8; 1942 info_ptr->bit_depth = 8;
1936 info_ptr->num_trans = 0; 1943 info_ptr->num_trans = 0;
1944
1945 if (png_ptr->palette == NULL)
1946 png_error (png_ptr, "Palette is NULL in indexed image");
1937 } 1947 }
1938 else 1948 else
1939 { 1949 {
1940 if (png_ptr->num_trans) 1950 if (png_ptr->num_trans != 0)
1941 { 1951 {
1942 if (png_ptr->transformations & PNG_EXPAND_tRNS) 1952 if ((png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
1943 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; 1953 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1944 } 1954 }
1945 if (info_ptr->bit_depth < 8) 1955 if (info_ptr->bit_depth < 8)
1946 info_ptr->bit_depth = 8; 1956 info_ptr->bit_depth = 8;
1947 1957
1948 info_ptr->num_trans = 0; 1958 info_ptr->num_trans = 0;
1949 } 1959 }
1950 } 1960 }
1951 #endif 1961 #endif
1952 1962
1953 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ 1963 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
1954 defined(PNG_READ_ALPHA_MODE_SUPPORTED) 1964 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
1955 /* The following is almost certainly wrong unless the background value is in 1965 /* The following is almost certainly wrong unless the background value is in
1956 * the screen space! 1966 * the screen space!
1957 */ 1967 */
1958 if (png_ptr->transformations & PNG_COMPOSE) 1968 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1959 info_ptr->background = png_ptr->background; 1969 info_ptr->background = png_ptr->background;
1960 #endif 1970 #endif
1961 1971
1962 #ifdef PNG_READ_GAMMA_SUPPORTED 1972 #ifdef PNG_READ_GAMMA_SUPPORTED
1963 /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4), 1973 /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4),
1964 * however it seems that the code in png_init_read_transformations, which has 1974 * however it seems that the code in png_init_read_transformations, which has
1965 * been called before this from png_read_update_info->png_read_start_row 1975 * been called before this from png_read_update_info->png_read_start_row
1966 * sometimes does the gamma transform and cancels the flag. 1976 * sometimes does the gamma transform and cancels the flag.
1967 * 1977 *
1968 * TODO: this looks wrong; the info_ptr should end up with a gamma equal to 1978 * TODO: this looks wrong; the info_ptr should end up with a gamma equal to
1969 * the screen_gamma value. The following probably results in weirdness if 1979 * the screen_gamma value. The following probably results in weirdness if
1970 * the info_ptr is used by the app after the rows have been read. 1980 * the info_ptr is used by the app after the rows have been read.
1971 */ 1981 */
1972 info_ptr->colorspace.gamma = png_ptr->colorspace.gamma; 1982 info_ptr->colorspace.gamma = png_ptr->colorspace.gamma;
1973 #endif 1983 #endif
1974 1984
1975 if (info_ptr->bit_depth == 16) 1985 if (info_ptr->bit_depth == 16)
1976 { 1986 {
1977 # ifdef PNG_READ_16BIT_SUPPORTED 1987 # ifdef PNG_READ_16BIT_SUPPORTED
1978 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED 1988 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
1979 if (png_ptr->transformations & PNG_SCALE_16_TO_8) 1989 if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
1980 info_ptr->bit_depth = 8; 1990 info_ptr->bit_depth = 8;
1981 # endif 1991 # endif
1982 1992
1983 # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED 1993 # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1984 if (png_ptr->transformations & PNG_16_TO_8) 1994 if ((png_ptr->transformations & PNG_16_TO_8) != 0)
1985 info_ptr->bit_depth = 8; 1995 info_ptr->bit_depth = 8;
1986 # endif 1996 # endif
1987 1997
1988 # else 1998 # else
1989 /* No 16 bit support: force chopping 16-bit input down to 8, in this case 1999 /* No 16-bit support: force chopping 16-bit input down to 8, in this case
1990 * the app program can chose if both APIs are available by setting the 2000 * the app program can chose if both APIs are available by setting the
1991 * correct scaling to use. 2001 * correct scaling to use.
1992 */ 2002 */
1993 # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED 2003 # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1994 /* For compatibility with previous versions use the strip method by 2004 /* For compatibility with previous versions use the strip method by
1995 * default. This code works because if PNG_SCALE_16_TO_8 is already 2005 * default. This code works because if PNG_SCALE_16_TO_8 is already
1996 * set the code below will do that in preference to the chop. 2006 * set the code below will do that in preference to the chop.
1997 */ 2007 */
1998 png_ptr->transformations |= PNG_16_TO_8; 2008 png_ptr->transformations |= PNG_16_TO_8;
1999 info_ptr->bit_depth = 8; 2009 info_ptr->bit_depth = 8;
2000 # else 2010 # else
2001 2011
2002 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED 2012 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2003 png_ptr->transformations |= PNG_SCALE_16_TO_8; 2013 png_ptr->transformations |= PNG_SCALE_16_TO_8;
2004 info_ptr->bit_depth = 8; 2014 info_ptr->bit_depth = 8;
2005 # else 2015 # else
2006 2016
2007 CONFIGURATION ERROR: you must enable at least one 16 to 8 method 2017 CONFIGURATION ERROR: you must enable at least one 16 to 8 method
2008 # endif 2018 # endif
2009 # endif 2019 # endif
2010 #endif /* !READ_16BIT_SUPPORTED */ 2020 #endif /* !READ_16BIT */
2011 } 2021 }
2012 2022
2013 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED 2023 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2014 if (png_ptr->transformations & PNG_GRAY_TO_RGB) 2024 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
2015 info_ptr->color_type = (png_byte)(info_ptr->color_type | 2025 info_ptr->color_type = (png_byte)(info_ptr->color_type |
2016 PNG_COLOR_MASK_COLOR); 2026 PNG_COLOR_MASK_COLOR);
2017 #endif 2027 #endif
2018 2028
2019 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED 2029 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2020 if (png_ptr->transformations & PNG_RGB_TO_GRAY) 2030 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
2021 info_ptr->color_type = (png_byte)(info_ptr->color_type & 2031 info_ptr->color_type = (png_byte)(info_ptr->color_type &
2022 ~PNG_COLOR_MASK_COLOR); 2032 ~PNG_COLOR_MASK_COLOR);
2023 #endif 2033 #endif
2024 2034
2025 #ifdef PNG_READ_QUANTIZE_SUPPORTED 2035 #ifdef PNG_READ_QUANTIZE_SUPPORTED
2026 if (png_ptr->transformations & PNG_QUANTIZE) 2036 if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
2027 { 2037 {
2028 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || 2038 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
2029 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && 2039 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
2030 png_ptr->palette_lookup && info_ptr->bit_depth == 8) 2040 png_ptr->palette_lookup != 0 && info_ptr->bit_depth == 8)
2031 { 2041 {
2032 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; 2042 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
2033 } 2043 }
2034 } 2044 }
2035 #endif 2045 #endif
2036 2046
2037 #ifdef PNG_READ_EXPAND_16_SUPPORTED 2047 #ifdef PNG_READ_EXPAND_16_SUPPORTED
2038 if (png_ptr->transformations & PNG_EXPAND_16 && info_ptr->bit_depth == 8 && 2048 if ((png_ptr->transformations & PNG_EXPAND_16) != 0 &&
2039 info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) 2049 info_ptr->bit_depth == 8 &&
2050 info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
2040 { 2051 {
2041 info_ptr->bit_depth = 16; 2052 info_ptr->bit_depth = 16;
2042 } 2053 }
2043 #endif 2054 #endif
2044 2055
2045 #ifdef PNG_READ_PACK_SUPPORTED 2056 #ifdef PNG_READ_PACK_SUPPORTED
2046 if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8)) 2057 if ((png_ptr->transformations & PNG_PACK) != 0 &&
2058 (info_ptr->bit_depth < 8))
2047 info_ptr->bit_depth = 8; 2059 info_ptr->bit_depth = 8;
2048 #endif 2060 #endif
2049 2061
2050 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 2062 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
2051 info_ptr->channels = 1; 2063 info_ptr->channels = 1;
2052 2064
2053 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) 2065 else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
2054 info_ptr->channels = 3; 2066 info_ptr->channels = 3;
2055 2067
2056 else 2068 else
2057 info_ptr->channels = 1; 2069 info_ptr->channels = 1;
2058 2070
2059 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED 2071 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2060 if (png_ptr->transformations & PNG_STRIP_ALPHA) 2072 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0)
2061 { 2073 {
2062 info_ptr->color_type = (png_byte)(info_ptr->color_type & 2074 info_ptr->color_type = (png_byte)(info_ptr->color_type &
2063 ~PNG_COLOR_MASK_ALPHA); 2075 ~PNG_COLOR_MASK_ALPHA);
2064 info_ptr->num_trans = 0; 2076 info_ptr->num_trans = 0;
2065 } 2077 }
2066 #endif 2078 #endif
2067 2079
2068 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) 2080 if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
2069 info_ptr->channels++; 2081 info_ptr->channels++;
2070 2082
2071 #ifdef PNG_READ_FILLER_SUPPORTED 2083 #ifdef PNG_READ_FILLER_SUPPORTED
2072 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */ 2084 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
2073 if ((png_ptr->transformations & PNG_FILLER) && 2085 if ((png_ptr->transformations & PNG_FILLER) != 0 &&
2074 ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || 2086 (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
2075 (info_ptr->color_type == PNG_COLOR_TYPE_GRAY))) 2087 info_ptr->color_type == PNG_COLOR_TYPE_GRAY))
2076 { 2088 {
2077 info_ptr->channels++; 2089 info_ptr->channels++;
2078 /* If adding a true alpha channel not just filler */ 2090 /* If adding a true alpha channel not just filler */
2079 if (png_ptr->transformations & PNG_ADD_ALPHA) 2091 if ((png_ptr->transformations & PNG_ADD_ALPHA) != 0)
2080 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; 2092 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
2081 } 2093 }
2082 #endif 2094 #endif
2083 2095
2084 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ 2096 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
2085 defined(PNG_READ_USER_TRANSFORM_SUPPORTED) 2097 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
2086 if (png_ptr->transformations & PNG_USER_TRANSFORM) 2098 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
2087 { 2099 {
2088 if (info_ptr->bit_depth < png_ptr->user_transform_depth) 2100 if (png_ptr->user_transform_depth != 0)
2089 info_ptr->bit_depth = png_ptr->user_transform_depth; 2101 info_ptr->bit_depth = png_ptr->user_transform_depth;
2090 2102
2091 if (info_ptr->channels < png_ptr->user_transform_channels) 2103 if (png_ptr->user_transform_channels != 0)
2092 info_ptr->channels = png_ptr->user_transform_channels; 2104 info_ptr->channels = png_ptr->user_transform_channels;
2093 } 2105 }
2094 #endif 2106 #endif
2095 2107
2096 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * 2108 info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
2097 info_ptr->bit_depth); 2109 info_ptr->bit_depth);
2098 2110
2099 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width); 2111 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
2100 2112
2101 /* Adding in 1.5.4: cache the above value in png_struct so that we can later 2113 /* Adding in 1.5.4: cache the above value in png_struct so that we can later
2102 * check in png_rowbytes that the user buffer won't get overwritten. Note 2114 * check in png_rowbytes that the user buffer won't get overwritten. Note
2103 * that the field is not always set - if png_read_update_info isn't called 2115 * that the field is not always set - if png_read_update_info isn't called
2104 * the application has to either not do any transforms or get the calculation 2116 * the application has to either not do any transforms or get the calculation
2105 * right itself. 2117 * right itself.
2106 */ 2118 */
2107 png_ptr->info_rowbytes = info_ptr->rowbytes; 2119 png_ptr->info_rowbytes = info_ptr->rowbytes;
2108 2120
2109 #ifndef PNG_READ_EXPAND_SUPPORTED 2121 #ifndef PNG_READ_EXPAND_SUPPORTED
2110 if (png_ptr) 2122 if (png_ptr != NULL)
2111 return; 2123 return;
2112 #endif 2124 #endif
2113 } 2125 }
2114 2126
2115 /* Transform the row. The order of transformations is significant,
2116 * and is very touchy. If you add a transformation, take care to
2117 * decide how it fits in with the other transformations here.
2118 */
2119 void /* PRIVATE */
2120 png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info)
2121 {
2122 png_debug(1, "in png_do_read_transformations");
2123
2124 if (png_ptr->row_buf == NULL)
2125 {
2126 /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this
2127 * error is incredibly rare and incredibly easy to debug without this
2128 * information.
2129 */
2130 png_error(png_ptr, "NULL row buffer");
2131 }
2132
2133 /* The following is debugging; prior to 1.5.4 the code was never compiled in;
2134 * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
2135 * PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for
2136 * all transformations, however in practice the ROW_INIT always gets done on
2137 * demand, if necessary.
2138 */
2139 if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
2140 !(png_ptr->flags & PNG_FLAG_ROW_INIT))
2141 {
2142 /* Application has failed to call either png_read_start_image() or
2143 * png_read_update_info() after setting transforms that expand pixels.
2144 * This check added to libpng-1.2.19 (but not enabled until 1.5.4).
2145 */
2146 png_error(png_ptr, "Uninitialized row");
2147 }
2148
2149 #ifdef PNG_READ_EXPAND_SUPPORTED
2150 if (png_ptr->transformations & PNG_EXPAND)
2151 {
2152 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
2153 {
2154 png_do_expand_palette(row_info, png_ptr->row_buf + 1,
2155 png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
2156 }
2157
2158 else
2159 {
2160 if (png_ptr->num_trans &&
2161 (png_ptr->transformations & PNG_EXPAND_tRNS))
2162 png_do_expand(row_info, png_ptr->row_buf + 1,
2163 &(png_ptr->trans_color));
2164
2165 else
2166 png_do_expand(row_info, png_ptr->row_buf + 1,
2167 NULL);
2168 }
2169 }
2170 #endif
2171
2172 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2173 if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
2174 !(png_ptr->transformations & PNG_COMPOSE) &&
2175 (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2176 row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
2177 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
2178 0 /* at_start == false, because SWAP_ALPHA happens later */);
2179 #endif
2180
2181 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2182 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
2183 {
2184 int rgb_error =
2185 png_do_rgb_to_gray(png_ptr, row_info,
2186 png_ptr->row_buf + 1);
2187
2188 if (rgb_error)
2189 {
2190 png_ptr->rgb_to_gray_status=1;
2191 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
2192 PNG_RGB_TO_GRAY_WARN)
2193 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
2194
2195 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
2196 PNG_RGB_TO_GRAY_ERR)
2197 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
2198 }
2199 }
2200 #endif
2201
2202 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
2203 *
2204 * In most cases, the "simple transparency" should be done prior to doing
2205 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
2206 * pixel is transparent. You would also need to make sure that the
2207 * transparency information is upgraded to RGB.
2208 *
2209 * To summarize, the current flow is:
2210 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
2211 * with background "in place" if transparent,
2212 * convert to RGB if necessary
2213 * - Gray + alpha -> composite with gray background and remove alpha bytes,
2214 * convert to RGB if necessary
2215 *
2216 * To support RGB backgrounds for gray images we need:
2217 * - Gray + simple transparency -> convert to RGB + simple transparency,
2218 * compare 3 or 6 bytes and composite with
2219 * background "in place" if transparent
2220 * (3x compare/pixel compared to doing
2221 * composite with gray bkgrnd)
2222 * - Gray + alpha -> convert to RGB + alpha, composite with background and
2223 * remove alpha bytes (3x float
2224 * operations/pixel compared with composite
2225 * on gray background)
2226 *
2227 * Greg's change will do this. The reason it wasn't done before is for
2228 * performance, as this increases the per-pixel operations. If we would check
2229 * in advance if the background was gray or RGB, and position the gray-to-RGB
2230 * transform appropriately, then it would save a lot of work/time.
2231 */
2232
2233 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2234 /* If gray -> RGB, do so now only if background is non-gray; else do later
2235 * for performance reasons
2236 */
2237 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
2238 !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
2239 png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
2240 #endif
2241
2242 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
2243 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
2244 if (png_ptr->transformations & PNG_COMPOSE)
2245 png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr);
2246 #endif
2247
2248 #ifdef PNG_READ_GAMMA_SUPPORTED
2249 if ((png_ptr->transformations & PNG_GAMMA) &&
2250 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2251 /* Because RGB_TO_GRAY does the gamma transform. */
2252 !(png_ptr->transformations & PNG_RGB_TO_GRAY) &&
2253 #endif
2254 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
2255 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
2256 /* Because PNG_COMPOSE does the gamma transform if there is something to
2257 * do (if there is an alpha channel or transparency.)
2258 */
2259 !((png_ptr->transformations & PNG_COMPOSE) &&
2260 ((png_ptr->num_trans != 0) ||
2261 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
2262 #endif
2263 /* Because png_init_read_transformations transforms the palette, unless
2264 * RGB_TO_GRAY will do the transform.
2265 */
2266 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
2267 png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr);
2268 #endif
2269
2270 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2271 if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
2272 (png_ptr->transformations & PNG_COMPOSE) &&
2273 (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2274 row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
2275 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
2276 0 /* at_start == false, because SWAP_ALPHA happens later */);
2277 #endif
2278
2279 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
2280 if ((png_ptr->transformations & PNG_ENCODE_ALPHA) &&
2281 (row_info->color_type & PNG_COLOR_MASK_ALPHA))
2282 png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr);
2283 #endif
2284
2285 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2286 if (png_ptr->transformations & PNG_SCALE_16_TO_8)
2287 png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1);
2288 #endif
2289
2290 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2291 /* There is no harm in doing both of these because only one has any effect,
2292 * by putting the 'scale' option first if the app asks for scale (either by
2293 * calling the API or in a TRANSFORM flag) this is what happens.
2294 */
2295 if (png_ptr->transformations & PNG_16_TO_8)
2296 png_do_chop(row_info, png_ptr->row_buf + 1);
2297 #endif
2298
2299 #ifdef PNG_READ_QUANTIZE_SUPPORTED
2300 if (png_ptr->transformations & PNG_QUANTIZE)
2301 {
2302 png_do_quantize(row_info, png_ptr->row_buf + 1,
2303 png_ptr->palette_lookup, png_ptr->quantize_index);
2304
2305 if (row_info->rowbytes == 0)
2306 png_error(png_ptr, "png_do_quantize returned rowbytes=0");
2307 }
2308 #endif /* PNG_READ_QUANTIZE_SUPPORTED */
2309
2310 #ifdef PNG_READ_EXPAND_16_SUPPORTED
2311 /* Do the expansion now, after all the arithmetic has been done. Notice
2312 * that previous transformations can handle the PNG_EXPAND_16 flag if this
2313 * is efficient (particularly true in the case of gamma correction, where
2314 * better accuracy results faster!)
2315 */
2316 if (png_ptr->transformations & PNG_EXPAND_16)
2317 png_do_expand_16(row_info, png_ptr->row_buf + 1);
2318 #endif
2319
2320 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2321 /* NOTE: moved here in 1.5.4 (from much later in this list.) */
2322 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
2323 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
2324 png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
2325 #endif
2326
2327 #ifdef PNG_READ_INVERT_SUPPORTED
2328 if (png_ptr->transformations & PNG_INVERT_MONO)
2329 png_do_invert(row_info, png_ptr->row_buf + 1);
2330 #endif
2331
2332 #ifdef PNG_READ_SHIFT_SUPPORTED
2333 if (png_ptr->transformations & PNG_SHIFT)
2334 png_do_unshift(row_info, png_ptr->row_buf + 1,
2335 &(png_ptr->shift));
2336 #endif
2337
2338 #ifdef PNG_READ_PACK_SUPPORTED
2339 if (png_ptr->transformations & PNG_PACK)
2340 png_do_unpack(row_info, png_ptr->row_buf + 1);
2341 #endif
2342
2343 #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
2344 /* Added at libpng-1.5.10 */
2345 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
2346 png_ptr->num_palette_max >= 0)
2347 png_do_check_palette_indexes(png_ptr, row_info);
2348 #endif
2349
2350 #ifdef PNG_READ_BGR_SUPPORTED
2351 if (png_ptr->transformations & PNG_BGR)
2352 png_do_bgr(row_info, png_ptr->row_buf + 1);
2353 #endif
2354
2355 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2356 if (png_ptr->transformations & PNG_PACKSWAP)
2357 png_do_packswap(row_info, png_ptr->row_buf + 1);
2358 #endif
2359
2360 #ifdef PNG_READ_FILLER_SUPPORTED
2361 if (png_ptr->transformations & PNG_FILLER)
2362 png_do_read_filler(row_info, png_ptr->row_buf + 1,
2363 (png_uint_32)png_ptr->filler, png_ptr->flags);
2364 #endif
2365
2366 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
2367 if (png_ptr->transformations & PNG_INVERT_ALPHA)
2368 png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1);
2369 #endif
2370
2371 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
2372 if (png_ptr->transformations & PNG_SWAP_ALPHA)
2373 png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1);
2374 #endif
2375
2376 #ifdef PNG_READ_16BIT_SUPPORTED
2377 #ifdef PNG_READ_SWAP_SUPPORTED
2378 if (png_ptr->transformations & PNG_SWAP_BYTES)
2379 png_do_swap(row_info, png_ptr->row_buf + 1);
2380 #endif
2381 #endif
2382
2383 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
2384 if (png_ptr->transformations & PNG_USER_TRANSFORM)
2385 {
2386 if (png_ptr->read_user_transform_fn != NULL)
2387 (*(png_ptr->read_user_transform_fn)) /* User read transform function */
2388 (png_ptr, /* png_ptr */
2389 row_info, /* row_info: */
2390 /* png_uint_32 width; width of row */
2391 /* png_size_t rowbytes; number of bytes in row */
2392 /* png_byte color_type; color type of pixels */
2393 /* png_byte bit_depth; bit depth of samples */
2394 /* png_byte channels; number of channels (1-4) */
2395 /* png_byte pixel_depth; bits per pixel (depth*channels) */
2396 png_ptr->row_buf + 1); /* start of pixel data for row */
2397 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
2398 if (png_ptr->user_transform_depth)
2399 row_info->bit_depth = png_ptr->user_transform_depth;
2400
2401 if (png_ptr->user_transform_channels)
2402 row_info->channels = png_ptr->user_transform_channels;
2403 #endif
2404 row_info->pixel_depth = (png_byte)(row_info->bit_depth *
2405 row_info->channels);
2406
2407 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width);
2408 }
2409 #endif
2410 }
2411
2412 #ifdef PNG_READ_PACK_SUPPORTED 2127 #ifdef PNG_READ_PACK_SUPPORTED
2413 /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel, 2128 /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
2414 * without changing the actual values. Thus, if you had a row with 2129 * without changing the actual values. Thus, if you had a row with
2415 * a bit depth of 1, you would end up with bytes that only contained 2130 * a bit depth of 1, you would end up with bytes that only contained
2416 * the numbers 0 or 1. If you would rather they contain 0 and 255, use 2131 * the numbers 0 or 1. If you would rather they contain 0 and 255, use
2417 * png_do_shift() after this. 2132 * png_do_shift() after this.
2418 */ 2133 */
2419 void /* PRIVATE */ 2134 static void
2420 png_do_unpack(png_row_infop row_info, png_bytep row) 2135 png_do_unpack(png_row_infop row_info, png_bytep row)
2421 { 2136 {
2422 png_debug(1, "in png_do_unpack"); 2137 png_debug(1, "in png_do_unpack");
2423 2138
2424 if (row_info->bit_depth < 8) 2139 if (row_info->bit_depth < 8)
2425 { 2140 {
2426 png_uint_32 i; 2141 png_uint_32 i;
2427 png_uint_32 row_width=row_info->width; 2142 png_uint_32 row_width=row_info->width;
2428 2143
2429 switch (row_info->bit_depth) 2144 switch (row_info->bit_depth)
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
2507 } 2222 }
2508 } 2223 }
2509 #endif 2224 #endif
2510 2225
2511 #ifdef PNG_READ_SHIFT_SUPPORTED 2226 #ifdef PNG_READ_SHIFT_SUPPORTED
2512 /* Reverse the effects of png_do_shift. This routine merely shifts the 2227 /* Reverse the effects of png_do_shift. This routine merely shifts the
2513 * pixels back to their significant bits values. Thus, if you have 2228 * pixels back to their significant bits values. Thus, if you have
2514 * a row of bit depth 8, but only 5 are significant, this will shift 2229 * a row of bit depth 8, but only 5 are significant, this will shift
2515 * the values back to 0 through 31. 2230 * the values back to 0 through 31.
2516 */ 2231 */
2517 void /* PRIVATE */ 2232 static void
2518 png_do_unshift(png_row_infop row_info, png_bytep row, 2233 png_do_unshift(png_row_infop row_info, png_bytep row,
2519 png_const_color_8p sig_bits) 2234 png_const_color_8p sig_bits)
2520 { 2235 {
2521 int color_type; 2236 int color_type;
2522 2237
2523 png_debug(1, "in png_do_unshift"); 2238 png_debug(1, "in png_do_unshift");
2524 2239
2525 /* The palette case has already been handled in the _init routine. */ 2240 /* The palette case has already been handled in the _init routine. */
2526 color_type = row_info->color_type; 2241 color_type = row_info->color_type;
2527 2242
2528 if (color_type != PNG_COLOR_TYPE_PALETTE) 2243 if (color_type != PNG_COLOR_TYPE_PALETTE)
2529 { 2244 {
2530 int shift[4]; 2245 int shift[4];
2531 int channels = 0; 2246 int channels = 0;
2532 int bit_depth = row_info->bit_depth; 2247 int bit_depth = row_info->bit_depth;
2533 2248
2534 if (color_type & PNG_COLOR_MASK_COLOR) 2249 if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
2535 { 2250 {
2536 shift[channels++] = bit_depth - sig_bits->red; 2251 shift[channels++] = bit_depth - sig_bits->red;
2537 shift[channels++] = bit_depth - sig_bits->green; 2252 shift[channels++] = bit_depth - sig_bits->green;
2538 shift[channels++] = bit_depth - sig_bits->blue; 2253 shift[channels++] = bit_depth - sig_bits->blue;
2539 } 2254 }
2540 2255
2541 else 2256 else
2542 { 2257 {
2543 shift[channels++] = bit_depth - sig_bits->gray; 2258 shift[channels++] = bit_depth - sig_bits->gray;
2544 } 2259 }
2545 2260
2546 if (color_type & PNG_COLOR_MASK_ALPHA) 2261 if ((color_type & PNG_COLOR_MASK_ALPHA) != 0)
2547 { 2262 {
2548 shift[channels++] = bit_depth - sig_bits->alpha; 2263 shift[channels++] = bit_depth - sig_bits->alpha;
2549 } 2264 }
2550 2265
2551 { 2266 {
2552 int c, have_shift; 2267 int c, have_shift;
2553 2268
2554 for (c = have_shift = 0; c < channels; ++c) 2269 for (c = have_shift = 0; c < channels; ++c)
2555 { 2270 {
2556 /* A shift of more than the bit depth is an error condition but it 2271 /* A shift of more than the bit depth is an error condition but it
2557 * gets ignored here. 2272 * gets ignored here.
2558 */ 2273 */
2559 if (shift[c] <= 0 || shift[c] >= bit_depth) 2274 if (shift[c] <= 0 || shift[c] >= bit_depth)
2560 shift[c] = 0; 2275 shift[c] = 0;
2561 2276
2562 else 2277 else
2563 have_shift = 1; 2278 have_shift = 1;
2564 } 2279 }
2565 2280
2566 if (!have_shift) 2281 if (have_shift == 0)
2567 return; 2282 return;
2568 } 2283 }
2569 2284
2570 switch (bit_depth) 2285 switch (bit_depth)
2571 { 2286 {
2572 default: 2287 default:
2573 /* Must be 1bpp gray: should not be here! */ 2288 /* Must be 1bpp gray: should not be here! */
2574 /* NOTREACHED */ 2289 /* NOTREACHED */
2575 break; 2290 break;
2576 2291
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
2634 int channel = 0; 2349 int channel = 0;
2635 2350
2636 while (bp < bp_end) 2351 while (bp < bp_end)
2637 { 2352 {
2638 int value = (bp[0] << 8) + bp[1]; 2353 int value = (bp[0] << 8) + bp[1];
2639 2354
2640 value >>= shift[channel]; 2355 value >>= shift[channel];
2641 if (++channel >= channels) 2356 if (++channel >= channels)
2642 channel = 0; 2357 channel = 0;
2643 *bp++ = (png_byte)(value >> 8); 2358 *bp++ = (png_byte)(value >> 8);
2644 *bp++ = (png_byte)(value & 0xff); 2359 *bp++ = (png_byte)value;
2645 } 2360 }
2646 break; 2361 break;
2647 } 2362 }
2648 #endif 2363 #endif
2649 } 2364 }
2650 } 2365 }
2651 } 2366 }
2652 #endif 2367 #endif
2653 2368
2654 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED 2369 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2655 /* Scale rows of bit depth 16 down to 8 accurately */ 2370 /* Scale rows of bit depth 16 down to 8 accurately */
2656 void /* PRIVATE */ 2371 static void
2657 png_do_scale_16_to_8(png_row_infop row_info, png_bytep row) 2372 png_do_scale_16_to_8(png_row_infop row_info, png_bytep row)
2658 { 2373 {
2659 png_debug(1, "in png_do_scale_16_to_8"); 2374 png_debug(1, "in png_do_scale_16_to_8");
2660 2375
2661 if (row_info->bit_depth == 16) 2376 if (row_info->bit_depth == 16)
2662 { 2377 {
2663 png_bytep sp = row; /* source */ 2378 png_bytep sp = row; /* source */
2664 png_bytep dp = row; /* destination */ 2379 png_bytep dp = row; /* destination */
2665 png_bytep ep = sp + row_info->rowbytes; /* end+1 */ 2380 png_bytep ep = sp + row_info->rowbytes; /* end+1 */
2666 2381
2667 while (sp < ep) 2382 while (sp < ep)
2668 { 2383 {
2669 /* The input is an array of 16 bit components, these must be scaled to 2384 /* The input is an array of 16-bit components, these must be scaled to
2670 * 8 bits each. For a 16 bit value V the required value (from the PNG 2385 * 8 bits each. For a 16-bit value V the required value (from the PNG
2671 * specification) is: 2386 * specification) is:
2672 * 2387 *
2673 * (V * 255) / 65535 2388 * (V * 255) / 65535
2674 * 2389 *
2675 * This reduces to round(V / 257), or floor((V + 128.5)/257) 2390 * This reduces to round(V / 257), or floor((V + 128.5)/257)
2676 * 2391 *
2677 * Represent V as the two byte value vhi.vlo. Make a guess that the 2392 * Represent V as the two byte value vhi.vlo. Make a guess that the
2678 * result is the top byte of V, vhi, then the correction to this value 2393 * result is the top byte of V, vhi, then the correction to this value
2679 * is: 2394 * is:
2680 * 2395 *
2681 * error = floor(((V-vhi.vhi) + 128.5) / 257) 2396 * error = floor(((V-vhi.vhi) + 128.5) / 257)
2682 * = floor(((vlo-vhi) + 128.5) / 257) 2397 * = floor(((vlo-vhi) + 128.5) / 257)
2683 * 2398 *
2684 * This can be approximated using integer arithmetic (and a signed 2399 * This can be approximated using integer arithmetic (and a signed
2685 * shift): 2400 * shift):
2686 * 2401 *
2687 * error = (vlo-vhi+128) >> 8; 2402 * error = (vlo-vhi+128) >> 8;
2688 * 2403 *
2689 * The approximate differs from the exact answer only when (vlo-vhi) is 2404 * The approximate differs from the exact answer only when (vlo-vhi) is
2690 * 128; it then gives a correction of +1 when the exact correction is 2405 * 128; it then gives a correction of +1 when the exact correction is
2691 * 0. This gives 128 errors. The exact answer (correct for all 16 bit 2406 * 0. This gives 128 errors. The exact answer (correct for all 16-bit
2692 * input values) is: 2407 * input values) is:
2693 * 2408 *
2694 * error = (vlo-vhi+128)*65535 >> 24; 2409 * error = (vlo-vhi+128)*65535 >> 24;
2695 * 2410 *
2696 * An alternative arithmetic calculation which also gives no errors is: 2411 * An alternative arithmetic calculation which also gives no errors is:
2697 * 2412 *
2698 * (V * 255 + 32895) >> 16 2413 * (V * 255 + 32895) >> 16
2699 */ 2414 */
2700 2415
2701 png_int_32 tmp = *sp++; /* must be signed! */ 2416 png_int_32 tmp = *sp++; /* must be signed! */
2702 tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24; 2417 tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24;
2703 *dp++ = (png_byte)tmp; 2418 *dp++ = (png_byte)tmp;
2704 } 2419 }
2705 2420
2706 row_info->bit_depth = 8; 2421 row_info->bit_depth = 8;
2707 row_info->pixel_depth = (png_byte)(8 * row_info->channels); 2422 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2708 row_info->rowbytes = row_info->width * row_info->channels; 2423 row_info->rowbytes = row_info->width * row_info->channels;
2709 } 2424 }
2710 } 2425 }
2711 #endif 2426 #endif
2712 2427
2713 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED 2428 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2714 void /* PRIVATE */ 2429 static void
2715 /* Simply discard the low byte. This was the default behavior prior 2430 /* Simply discard the low byte. This was the default behavior prior
2716 * to libpng-1.5.4. 2431 * to libpng-1.5.4.
2717 */ 2432 */
2718 png_do_chop(png_row_infop row_info, png_bytep row) 2433 png_do_chop(png_row_infop row_info, png_bytep row)
2719 { 2434 {
2720 png_debug(1, "in png_do_chop"); 2435 png_debug(1, "in png_do_chop");
2721 2436
2722 if (row_info->bit_depth == 16) 2437 if (row_info->bit_depth == 16)
2723 { 2438 {
2724 png_bytep sp = row; /* source */ 2439 png_bytep sp = row; /* source */
2725 png_bytep dp = row; /* destination */ 2440 png_bytep dp = row; /* destination */
2726 png_bytep ep = sp + row_info->rowbytes; /* end+1 */ 2441 png_bytep ep = sp + row_info->rowbytes; /* end+1 */
2727 2442
2728 while (sp < ep) 2443 while (sp < ep)
2729 { 2444 {
2730 *dp++ = *sp; 2445 *dp++ = *sp;
2731 sp += 2; /* skip low byte */ 2446 sp += 2; /* skip low byte */
2732 } 2447 }
2733 2448
2734 row_info->bit_depth = 8; 2449 row_info->bit_depth = 8;
2735 row_info->pixel_depth = (png_byte)(8 * row_info->channels); 2450 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2736 row_info->rowbytes = row_info->width * row_info->channels; 2451 row_info->rowbytes = row_info->width * row_info->channels;
2737 } 2452 }
2738 } 2453 }
2739 #endif 2454 #endif
2740 2455
2741 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED 2456 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
2742 void /* PRIVATE */ 2457 static void
2743 png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) 2458 png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
2744 { 2459 {
2745 png_debug(1, "in png_do_read_swap_alpha"); 2460 png_debug(1, "in png_do_read_swap_alpha");
2746 2461
2747 { 2462 {
2748 png_uint_32 row_width = row_info->width; 2463 png_uint_32 row_width = row_info->width;
2749 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) 2464 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2750 { 2465 {
2751 /* This converts from RGBA to ARGB */ 2466 /* This converts from RGBA to ARGB */
2752 if (row_info->bit_depth == 8) 2467 if (row_info->bit_depth == 8)
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
2829 *(--dp) = save[1]; 2544 *(--dp) = save[1];
2830 } 2545 }
2831 } 2546 }
2832 #endif 2547 #endif
2833 } 2548 }
2834 } 2549 }
2835 } 2550 }
2836 #endif 2551 #endif
2837 2552
2838 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED 2553 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
2839 void /* PRIVATE */ 2554 static void
2840 png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) 2555 png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
2841 { 2556 {
2842 png_uint_32 row_width; 2557 png_uint_32 row_width;
2843 png_debug(1, "in png_do_read_invert_alpha"); 2558 png_debug(1, "in png_do_read_invert_alpha");
2844 2559
2845 row_width = row_info->width; 2560 row_width = row_info->width;
2846 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) 2561 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2847 { 2562 {
2848 if (row_info->bit_depth == 8) 2563 if (row_info->bit_depth == 8)
2849 { 2564 {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
2931 dp=sp; 2646 dp=sp;
2932 } 2647 }
2933 } 2648 }
2934 #endif 2649 #endif
2935 } 2650 }
2936 } 2651 }
2937 #endif 2652 #endif
2938 2653
2939 #ifdef PNG_READ_FILLER_SUPPORTED 2654 #ifdef PNG_READ_FILLER_SUPPORTED
2940 /* Add filler channel if we have RGB color */ 2655 /* Add filler channel if we have RGB color */
2941 void /* PRIVATE */ 2656 static void
2942 png_do_read_filler(png_row_infop row_info, png_bytep row, 2657 png_do_read_filler(png_row_infop row_info, png_bytep row,
2943 png_uint_32 filler, png_uint_32 flags) 2658 png_uint_32 filler, png_uint_32 flags)
2944 { 2659 {
2945 png_uint_32 i; 2660 png_uint_32 i;
2946 png_uint_32 row_width = row_info->width; 2661 png_uint_32 row_width = row_info->width;
2947 2662
2948 #ifdef PNG_READ_16BIT_SUPPORTED 2663 #ifdef PNG_READ_16BIT_SUPPORTED
2949 png_byte hi_filler = (png_byte)((filler>>8) & 0xff); 2664 png_byte hi_filler = (png_byte)(filler>>8);
2950 #endif 2665 #endif
2951 png_byte lo_filler = (png_byte)(filler & 0xff); 2666 png_byte lo_filler = (png_byte)filler;
2952 2667
2953 png_debug(1, "in png_do_read_filler"); 2668 png_debug(1, "in png_do_read_filler");
2954 2669
2955 if ( 2670 if (
2956 row_info->color_type == PNG_COLOR_TYPE_GRAY) 2671 row_info->color_type == PNG_COLOR_TYPE_GRAY)
2957 { 2672 {
2958 if (row_info->bit_depth == 8) 2673 if (row_info->bit_depth == 8)
2959 { 2674 {
2960 if (flags & PNG_FLAG_FILLER_AFTER) 2675 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
2961 { 2676 {
2962 /* This changes the data from G to GX */ 2677 /* This changes the data from G to GX */
2963 png_bytep sp = row + (png_size_t)row_width; 2678 png_bytep sp = row + (png_size_t)row_width;
2964 png_bytep dp = sp + (png_size_t)row_width; 2679 png_bytep dp = sp + (png_size_t)row_width;
2965 for (i = 1; i < row_width; i++) 2680 for (i = 1; i < row_width; i++)
2966 { 2681 {
2967 *(--dp) = lo_filler; 2682 *(--dp) = lo_filler;
2968 *(--dp) = *(--sp); 2683 *(--dp) = *(--sp);
2969 } 2684 }
2970 *(--dp) = lo_filler; 2685 *(--dp) = lo_filler;
(...skipping 14 matching lines...) Expand all
2985 } 2700 }
2986 row_info->channels = 2; 2701 row_info->channels = 2;
2987 row_info->pixel_depth = 16; 2702 row_info->pixel_depth = 16;
2988 row_info->rowbytes = row_width * 2; 2703 row_info->rowbytes = row_width * 2;
2989 } 2704 }
2990 } 2705 }
2991 2706
2992 #ifdef PNG_READ_16BIT_SUPPORTED 2707 #ifdef PNG_READ_16BIT_SUPPORTED
2993 else if (row_info->bit_depth == 16) 2708 else if (row_info->bit_depth == 16)
2994 { 2709 {
2995 if (flags & PNG_FLAG_FILLER_AFTER) 2710 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
2996 { 2711 {
2997 /* This changes the data from GG to GGXX */ 2712 /* This changes the data from GG to GGXX */
2998 png_bytep sp = row + (png_size_t)row_width * 2; 2713 png_bytep sp = row + (png_size_t)row_width * 2;
2999 png_bytep dp = sp + (png_size_t)row_width * 2; 2714 png_bytep dp = sp + (png_size_t)row_width * 2;
3000 for (i = 1; i < row_width; i++) 2715 for (i = 1; i < row_width; i++)
3001 { 2716 {
2717 *(--dp) = lo_filler;
3002 *(--dp) = hi_filler; 2718 *(--dp) = hi_filler;
3003 *(--dp) = lo_filler;
3004 *(--dp) = *(--sp); 2719 *(--dp) = *(--sp);
3005 *(--dp) = *(--sp); 2720 *(--dp) = *(--sp);
3006 } 2721 }
2722 *(--dp) = lo_filler;
3007 *(--dp) = hi_filler; 2723 *(--dp) = hi_filler;
3008 *(--dp) = lo_filler;
3009 row_info->channels = 2; 2724 row_info->channels = 2;
3010 row_info->pixel_depth = 32; 2725 row_info->pixel_depth = 32;
3011 row_info->rowbytes = row_width * 4; 2726 row_info->rowbytes = row_width * 4;
3012 } 2727 }
3013 2728
3014 else 2729 else
3015 { 2730 {
3016 /* This changes the data from GG to XXGG */ 2731 /* This changes the data from GG to XXGG */
3017 png_bytep sp = row + (png_size_t)row_width * 2; 2732 png_bytep sp = row + (png_size_t)row_width * 2;
3018 png_bytep dp = sp + (png_size_t)row_width * 2; 2733 png_bytep dp = sp + (png_size_t)row_width * 2;
3019 for (i = 0; i < row_width; i++) 2734 for (i = 0; i < row_width; i++)
3020 { 2735 {
3021 *(--dp) = *(--sp); 2736 *(--dp) = *(--sp);
3022 *(--dp) = *(--sp); 2737 *(--dp) = *(--sp);
2738 *(--dp) = lo_filler;
3023 *(--dp) = hi_filler; 2739 *(--dp) = hi_filler;
3024 *(--dp) = lo_filler;
3025 } 2740 }
3026 row_info->channels = 2; 2741 row_info->channels = 2;
3027 row_info->pixel_depth = 32; 2742 row_info->pixel_depth = 32;
3028 row_info->rowbytes = row_width * 4; 2743 row_info->rowbytes = row_width * 4;
3029 } 2744 }
3030 } 2745 }
3031 #endif 2746 #endif
3032 } /* COLOR_TYPE == GRAY */ 2747 } /* COLOR_TYPE == GRAY */
3033 else if (row_info->color_type == PNG_COLOR_TYPE_RGB) 2748 else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
3034 { 2749 {
3035 if (row_info->bit_depth == 8) 2750 if (row_info->bit_depth == 8)
3036 { 2751 {
3037 if (flags & PNG_FLAG_FILLER_AFTER) 2752 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
3038 { 2753 {
3039 /* This changes the data from RGB to RGBX */ 2754 /* This changes the data from RGB to RGBX */
3040 png_bytep sp = row + (png_size_t)row_width * 3; 2755 png_bytep sp = row + (png_size_t)row_width * 3;
3041 png_bytep dp = sp + (png_size_t)row_width; 2756 png_bytep dp = sp + (png_size_t)row_width;
3042 for (i = 1; i < row_width; i++) 2757 for (i = 1; i < row_width; i++)
3043 { 2758 {
3044 *(--dp) = lo_filler; 2759 *(--dp) = lo_filler;
3045 *(--dp) = *(--sp); 2760 *(--dp) = *(--sp);
3046 *(--dp) = *(--sp); 2761 *(--dp) = *(--sp);
3047 *(--dp) = *(--sp); 2762 *(--dp) = *(--sp);
(...skipping 18 matching lines...) Expand all
3066 } 2781 }
3067 row_info->channels = 4; 2782 row_info->channels = 4;
3068 row_info->pixel_depth = 32; 2783 row_info->pixel_depth = 32;
3069 row_info->rowbytes = row_width * 4; 2784 row_info->rowbytes = row_width * 4;
3070 } 2785 }
3071 } 2786 }
3072 2787
3073 #ifdef PNG_READ_16BIT_SUPPORTED 2788 #ifdef PNG_READ_16BIT_SUPPORTED
3074 else if (row_info->bit_depth == 16) 2789 else if (row_info->bit_depth == 16)
3075 { 2790 {
3076 if (flags & PNG_FLAG_FILLER_AFTER) 2791 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
3077 { 2792 {
3078 /* This changes the data from RRGGBB to RRGGBBXX */ 2793 /* This changes the data from RRGGBB to RRGGBBXX */
3079 png_bytep sp = row + (png_size_t)row_width * 6; 2794 png_bytep sp = row + (png_size_t)row_width * 6;
3080 png_bytep dp = sp + (png_size_t)row_width * 2; 2795 png_bytep dp = sp + (png_size_t)row_width * 2;
3081 for (i = 1; i < row_width; i++) 2796 for (i = 1; i < row_width; i++)
3082 { 2797 {
2798 *(--dp) = lo_filler;
3083 *(--dp) = hi_filler; 2799 *(--dp) = hi_filler;
3084 *(--dp) = lo_filler;
3085 *(--dp) = *(--sp); 2800 *(--dp) = *(--sp);
3086 *(--dp) = *(--sp); 2801 *(--dp) = *(--sp);
3087 *(--dp) = *(--sp); 2802 *(--dp) = *(--sp);
3088 *(--dp) = *(--sp); 2803 *(--dp) = *(--sp);
3089 *(--dp) = *(--sp); 2804 *(--dp) = *(--sp);
3090 *(--dp) = *(--sp); 2805 *(--dp) = *(--sp);
3091 } 2806 }
2807 *(--dp) = lo_filler;
3092 *(--dp) = hi_filler; 2808 *(--dp) = hi_filler;
3093 *(--dp) = lo_filler;
3094 row_info->channels = 4; 2809 row_info->channels = 4;
3095 row_info->pixel_depth = 64; 2810 row_info->pixel_depth = 64;
3096 row_info->rowbytes = row_width * 8; 2811 row_info->rowbytes = row_width * 8;
3097 } 2812 }
3098 2813
3099 else 2814 else
3100 { 2815 {
3101 /* This changes the data from RRGGBB to XXRRGGBB */ 2816 /* This changes the data from RRGGBB to XXRRGGBB */
3102 png_bytep sp = row + (png_size_t)row_width * 6; 2817 png_bytep sp = row + (png_size_t)row_width * 6;
3103 png_bytep dp = sp + (png_size_t)row_width * 2; 2818 png_bytep dp = sp + (png_size_t)row_width * 2;
3104 for (i = 0; i < row_width; i++) 2819 for (i = 0; i < row_width; i++)
3105 { 2820 {
3106 *(--dp) = *(--sp); 2821 *(--dp) = *(--sp);
3107 *(--dp) = *(--sp); 2822 *(--dp) = *(--sp);
3108 *(--dp) = *(--sp); 2823 *(--dp) = *(--sp);
3109 *(--dp) = *(--sp); 2824 *(--dp) = *(--sp);
3110 *(--dp) = *(--sp); 2825 *(--dp) = *(--sp);
3111 *(--dp) = *(--sp); 2826 *(--dp) = *(--sp);
2827 *(--dp) = lo_filler;
3112 *(--dp) = hi_filler; 2828 *(--dp) = hi_filler;
3113 *(--dp) = lo_filler;
3114 } 2829 }
3115 2830
3116 row_info->channels = 4; 2831 row_info->channels = 4;
3117 row_info->pixel_depth = 64; 2832 row_info->pixel_depth = 64;
3118 row_info->rowbytes = row_width * 8; 2833 row_info->rowbytes = row_width * 8;
3119 } 2834 }
3120 } 2835 }
3121 #endif 2836 #endif
3122 } /* COLOR_TYPE == RGB */ 2837 } /* COLOR_TYPE == RGB */
3123 } 2838 }
3124 #endif 2839 #endif
3125 2840
3126 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED 2841 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
3127 /* Expand grayscale files to RGB, with or without alpha */ 2842 /* Expand grayscale files to RGB, with or without alpha */
3128 void /* PRIVATE */ 2843 static void
3129 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) 2844 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
3130 { 2845 {
3131 png_uint_32 i; 2846 png_uint_32 i;
3132 png_uint_32 row_width = row_info->width; 2847 png_uint_32 row_width = row_info->width;
3133 2848
3134 png_debug(1, "in png_do_gray_to_rgb"); 2849 png_debug(1, "in png_do_gray_to_rgb");
3135 2850
3136 if (row_info->bit_depth >= 8 && 2851 if (row_info->bit_depth >= 8 &&
3137 !(row_info->color_type & PNG_COLOR_MASK_COLOR)) 2852 (row_info->color_type & PNG_COLOR_MASK_COLOR) == 0)
3138 { 2853 {
3139 if (row_info->color_type == PNG_COLOR_TYPE_GRAY) 2854 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
3140 { 2855 {
3141 if (row_info->bit_depth == 8) 2856 if (row_info->bit_depth == 8)
3142 { 2857 {
3143 /* This changes G to RGB */ 2858 /* This changes G to RGB */
3144 png_bytep sp = row + (png_size_t)row_width - 1; 2859 png_bytep sp = row + (png_size_t)row_width - 1;
3145 png_bytep dp = sp + (png_size_t)row_width * 2; 2860 png_bytep dp = sp + (png_size_t)row_width * 2;
3146 for (i = 0; i < row_width; i++) 2861 for (i = 0; i < row_width; i++)
3147 { 2862 {
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
3257 * In all cases the calculation is to be done in a linear colorspace. If no 2972 * In all cases the calculation is to be done in a linear colorspace. If no
3258 * gamma information is available to correct the encoding of the original RGB 2973 * gamma information is available to correct the encoding of the original RGB
3259 * values this results in an implicit assumption that the original PNG RGB 2974 * values this results in an implicit assumption that the original PNG RGB
3260 * values were linear. 2975 * values were linear.
3261 * 2976 *
3262 * Other integer coefficents can be used via png_set_rgb_to_gray(). Because 2977 * Other integer coefficents can be used via png_set_rgb_to_gray(). Because
3263 * the API takes just red and green coefficients the blue coefficient is 2978 * the API takes just red and green coefficients the blue coefficient is
3264 * calculated to make the sum 32768. This will result in different rounding 2979 * calculated to make the sum 32768. This will result in different rounding
3265 * to that used above. 2980 * to that used above.
3266 */ 2981 */
3267 int /* PRIVATE */ 2982 static int
3268 png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row) 2983 png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row)
3269 2984
3270 { 2985 {
3271 int rgb_error = 0; 2986 int rgb_error = 0;
3272 2987
3273 png_debug(1, "in png_do_rgb_to_gray"); 2988 png_debug(1, "in png_do_rgb_to_gray");
3274 2989
3275 if (!(row_info->color_type & PNG_COLOR_MASK_PALETTE) && 2990 if ((row_info->color_type & PNG_COLOR_MASK_PALETTE) == 0 &&
3276 (row_info->color_type & PNG_COLOR_MASK_COLOR)) 2991 (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
3277 { 2992 {
3278 PNG_CONST png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff; 2993 PNG_CONST png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
3279 PNG_CONST png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff; 2994 PNG_CONST png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
3280 PNG_CONST png_uint_32 bc = 32768 - rc - gc; 2995 PNG_CONST png_uint_32 bc = 32768 - rc - gc;
3281 PNG_CONST png_uint_32 row_width = row_info->width; 2996 PNG_CONST png_uint_32 row_width = row_info->width;
3282 PNG_CONST int have_alpha = 2997 PNG_CONST int have_alpha =
3283 (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0; 2998 (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0;
3284 2999
3285 if (row_info->bit_depth == 8) 3000 if (row_info->bit_depth == 8)
3286 { 3001 {
(...skipping 30 matching lines...) Expand all
3317 { 3032 {
3318 /* If there is no overall correction the table will not be 3033 /* If there is no overall correction the table will not be
3319 * set. 3034 * set.
3320 */ 3035 */
3321 if (png_ptr->gamma_table != NULL) 3036 if (png_ptr->gamma_table != NULL)
3322 red = png_ptr->gamma_table[red]; 3037 red = png_ptr->gamma_table[red];
3323 3038
3324 *(dp++) = red; 3039 *(dp++) = red;
3325 } 3040 }
3326 3041
3327 if (have_alpha) 3042 if (have_alpha != 0)
3328 *(dp++) = *(sp++); 3043 *(dp++) = *(sp++);
3329 } 3044 }
3330 } 3045 }
3331 else 3046 else
3332 #endif 3047 #endif
3333 { 3048 {
3334 png_bytep sp = row; 3049 png_bytep sp = row;
3335 png_bytep dp = row; 3050 png_bytep dp = row;
3336 png_uint_32 i; 3051 png_uint_32 i;
3337 3052
3338 for (i = 0; i < row_width; i++) 3053 for (i = 0; i < row_width; i++)
3339 { 3054 {
3340 png_byte red = *(sp++); 3055 png_byte red = *(sp++);
3341 png_byte green = *(sp++); 3056 png_byte green = *(sp++);
3342 png_byte blue = *(sp++); 3057 png_byte blue = *(sp++);
3343 3058
3344 if (red != green || red != blue) 3059 if (red != green || red != blue)
3345 { 3060 {
3346 rgb_error |= 1; 3061 rgb_error |= 1;
3347 /* NOTE: this is the historical approach which simply 3062 /* NOTE: this is the historical approach which simply
3348 * truncates the results. 3063 * truncates the results.
3349 */ 3064 */
3350 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); 3065 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
3351 } 3066 }
3352 3067
3353 else 3068 else
3354 *(dp++) = red; 3069 *(dp++) = red;
3355 3070
3356 if (have_alpha) 3071 if (have_alpha != 0)
3357 *(dp++) = *(sp++); 3072 *(dp++) = *(sp++);
3358 } 3073 }
3359 } 3074 }
3360 } 3075 }
3361 3076
3362 else /* RGB bit_depth == 16 */ 3077 else /* RGB bit_depth == 16 */
3363 { 3078 {
3364 #ifdef PNG_READ_GAMMA_SUPPORTED 3079 #ifdef PNG_READ_GAMMA_SUPPORTED
3365 if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL) 3080 if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL)
3366 { 3081 {
3367 png_bytep sp = row; 3082 png_bytep sp = row;
3368 png_bytep dp = row; 3083 png_bytep dp = row;
3369 png_uint_32 i; 3084 png_uint_32 i;
3370 3085
3371 for (i = 0; i < row_width; i++) 3086 for (i = 0; i < row_width; i++)
3372 { 3087 {
3373 png_uint_16 red, green, blue, w; 3088 png_uint_16 red, green, blue, w;
3374 3089 png_byte hi,lo;
3375 red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; 3090
3376 green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; 3091 hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo));
3377 blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; 3092 hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo));
3093 hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo));
3378 3094
3379 if (red == green && red == blue) 3095 if (red == green && red == blue)
3380 { 3096 {
3381 if (png_ptr->gamma_16_table != NULL) 3097 if (png_ptr->gamma_16_table != NULL)
3382 w = png_ptr->gamma_16_table[(red&0xff) 3098 w = png_ptr->gamma_16_table[(red & 0xff)
3383 >> png_ptr->gamma_shift][red>>8]; 3099 >> png_ptr->gamma_shift][red >> 8];
3384 3100
3385 else 3101 else
3386 w = red; 3102 w = red;
3387 } 3103 }
3388 3104
3389 else 3105 else
3390 { 3106 {
3391 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) 3107 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red & 0xff)
3392 >> png_ptr->gamma_shift][red>>8]; 3108 >> png_ptr->gamma_shift][red>>8];
3393 png_uint_16 green_1 = 3109 png_uint_16 green_1 =
3394 png_ptr->gamma_16_to_1[(green&0xff) >> 3110 png_ptr->gamma_16_to_1[(green & 0xff) >>
3395 png_ptr->gamma_shift][green>>8]; 3111 png_ptr->gamma_shift][green>>8];
3396 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) 3112 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue & 0xff)
3397 >> png_ptr->gamma_shift][blue>>8]; 3113 >> png_ptr->gamma_shift][blue>>8];
3398 png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1 3114 png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1
3399 + bc*blue_1 + 16384)>>15); 3115 + bc*blue_1 + 16384)>>15);
3400 w = png_ptr->gamma_16_from_1[(gray16&0xff) >> 3116 w = png_ptr->gamma_16_from_1[(gray16 & 0xff) >>
3401 png_ptr->gamma_shift][gray16 >> 8]; 3117 png_ptr->gamma_shift][gray16 >> 8];
3402 rgb_error |= 1; 3118 rgb_error |= 1;
3403 } 3119 }
3404 3120
3405 *(dp++) = (png_byte)((w>>8) & 0xff); 3121 *(dp++) = (png_byte)((w>>8) & 0xff);
3406 *(dp++) = (png_byte)(w & 0xff); 3122 *(dp++) = (png_byte)(w & 0xff);
3407 3123
3408 if (have_alpha) 3124 if (have_alpha != 0)
3409 { 3125 {
3410 *(dp++) = *(sp++); 3126 *(dp++) = *(sp++);
3411 *(dp++) = *(sp++); 3127 *(dp++) = *(sp++);
3412 } 3128 }
3413 } 3129 }
3414 } 3130 }
3415 else 3131 else
3416 #endif 3132 #endif
3417 { 3133 {
3418 png_bytep sp = row; 3134 png_bytep sp = row;
3419 png_bytep dp = row; 3135 png_bytep dp = row;
3420 png_uint_32 i; 3136 png_uint_32 i;
3421 3137
3422 for (i = 0; i < row_width; i++) 3138 for (i = 0; i < row_width; i++)
3423 { 3139 {
3424 png_uint_16 red, green, blue, gray16; 3140 png_uint_16 red, green, blue, gray16;
3425 3141 png_byte hi,lo;
3426 red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; 3142
3427 green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; 3143 hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo));
3428 blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; 3144 hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo));
3145 hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo));
3429 3146
3430 if (red != green || red != blue) 3147 if (red != green || red != blue)
3431 rgb_error |= 1; 3148 rgb_error |= 1;
3432 3149
3433 /* From 1.5.5 in the 16 bit case do the accurate conversion even 3150 /* From 1.5.5 in the 16-bit case do the accurate conversion even
3434 * in the 'fast' case - this is because this is where the code 3151 * in the 'fast' case - this is because this is where the code
3435 * ends up when handling linear 16 bit data. 3152 * ends up when handling linear 16-bit data.
3436 */ 3153 */
3437 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >> 3154 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >>
3438 15); 3155 15);
3439 *(dp++) = (png_byte)((gray16>>8) & 0xff); 3156 *(dp++) = (png_byte)((gray16 >> 8) & 0xff);
3440 *(dp++) = (png_byte)(gray16 & 0xff); 3157 *(dp++) = (png_byte)(gray16 & 0xff);
3441 3158
3442 if (have_alpha) 3159 if (have_alpha != 0)
3443 { 3160 {
3444 *(dp++) = *(sp++); 3161 *(dp++) = *(sp++);
3445 *(dp++) = *(sp++); 3162 *(dp++) = *(sp++);
3446 } 3163 }
3447 } 3164 }
3448 } 3165 }
3449 } 3166 }
3450 3167
3451 row_info->channels = (png_byte)(row_info->channels - 2); 3168 row_info->channels = (png_byte)(row_info->channels - 2);
3452 row_info->color_type = (png_byte)(row_info->color_type & 3169 row_info->color_type = (png_byte)(row_info->color_type &
3453 ~PNG_COLOR_MASK_COLOR); 3170 ~PNG_COLOR_MASK_COLOR);
3454 row_info->pixel_depth = (png_byte)(row_info->channels * 3171 row_info->pixel_depth = (png_byte)(row_info->channels *
3455 row_info->bit_depth); 3172 row_info->bit_depth);
3456 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); 3173 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3457 } 3174 }
3458 return rgb_error; 3175 return rgb_error;
3459 } 3176 }
3460 #endif 3177 #endif
3461 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */ 3178
3462
3463 #ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
3464 /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
3465 * large of png_color. This lets grayscale images be treated as
3466 * paletted. Most useful for gamma correction and simplification
3467 * of code. This API is not used internally.
3468 */
3469 void PNGAPI
3470 png_build_grayscale_palette(int bit_depth, png_colorp palette)
3471 {
3472 int num_palette;
3473 int color_inc;
3474 int i;
3475 int v;
3476
3477 png_debug(1, "in png_do_build_grayscale_palette");
3478
3479 if (palette == NULL)
3480 return;
3481
3482 switch (bit_depth)
3483 {
3484 case 1:
3485 num_palette = 2;
3486 color_inc = 0xff;
3487 break;
3488
3489 case 2:
3490 num_palette = 4;
3491 color_inc = 0x55;
3492 break;
3493
3494 case 4:
3495 num_palette = 16;
3496 color_inc = 0x11;
3497 break;
3498
3499 case 8:
3500 num_palette = 256;
3501 color_inc = 1;
3502 break;
3503
3504 default:
3505 num_palette = 0;
3506 color_inc = 0;
3507 break;
3508 }
3509
3510 for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
3511 {
3512 palette[i].red = (png_byte)v;
3513 palette[i].green = (png_byte)v;
3514 palette[i].blue = (png_byte)v;
3515 }
3516 }
3517 #endif
3518
3519
3520 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
3521 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ 3179 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
3522 defined(PNG_READ_ALPHA_MODE_SUPPORTED) 3180 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
3523 /* Replace any alpha or transparency with the supplied background color. 3181 /* Replace any alpha or transparency with the supplied background color.
3524 * "background" is already in the screen gamma, while "background_1" is 3182 * "background" is already in the screen gamma, while "background_1" is
3525 * at a gamma of 1.0. Paletted files have already been taken care of. 3183 * at a gamma of 1.0. Paletted files have already been taken care of.
3526 */ 3184 */
3527 void /* PRIVATE */ 3185 static void
3528 png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr) 3186 png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
3529 { 3187 {
3530 #ifdef PNG_READ_GAMMA_SUPPORTED 3188 #ifdef PNG_READ_GAMMA_SUPPORTED
3531 png_const_bytep gamma_table = png_ptr->gamma_table; 3189 png_const_bytep gamma_table = png_ptr->gamma_table;
3532 png_const_bytep gamma_from_1 = png_ptr->gamma_from_1; 3190 png_const_bytep gamma_from_1 = png_ptr->gamma_from_1;
3533 png_const_bytep gamma_to_1 = png_ptr->gamma_to_1; 3191 png_const_bytep gamma_to_1 = png_ptr->gamma_to_1;
3534 png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table; 3192 png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table;
3535 png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1; 3193 png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1;
3536 png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1; 3194 png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1;
3537 int gamma_shift = png_ptr->gamma_shift; 3195 int gamma_shift = png_ptr->gamma_shift;
(...skipping 21 matching lines...) Expand all
3559 for (i = 0; i < row_width; i++) 3217 for (i = 0; i < row_width; i++)
3560 { 3218 {
3561 if ((png_uint_16)((*sp >> shift) & 0x01) 3219 if ((png_uint_16)((*sp >> shift) & 0x01)
3562 == png_ptr->trans_color.gray) 3220 == png_ptr->trans_color.gray)
3563 { 3221 {
3564 unsigned int tmp = *sp & (0x7f7f >> (7 - shift)); 3222 unsigned int tmp = *sp & (0x7f7f >> (7 - shift));
3565 tmp |= png_ptr->background.gray << shift; 3223 tmp |= png_ptr->background.gray << shift;
3566 *sp = (png_byte)(tmp & 0xff); 3224 *sp = (png_byte)(tmp & 0xff);
3567 } 3225 }
3568 3226
3569 if (!shift) 3227 if (shift == 0)
3570 { 3228 {
3571 shift = 7; 3229 shift = 7;
3572 sp++; 3230 sp++;
3573 } 3231 }
3574 3232
3575 else 3233 else
3576 shift--; 3234 shift--;
3577 } 3235 }
3578 break; 3236 break;
3579 } 3237 }
(...skipping 18 matching lines...) Expand all
3598 else 3256 else
3599 { 3257 {
3600 unsigned int p = (*sp >> shift) & 0x03; 3258 unsigned int p = (*sp >> shift) & 0x03;
3601 unsigned int g = (gamma_table [p | (p << 2) | 3259 unsigned int g = (gamma_table [p | (p << 2) |
3602 (p << 4) | (p << 6)] >> 6) & 0x03; 3260 (p << 4) | (p << 6)] >> 6) & 0x03;
3603 unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); 3261 unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
3604 tmp |= g << shift; 3262 tmp |= g << shift;
3605 *sp = (png_byte)(tmp & 0xff); 3263 *sp = (png_byte)(tmp & 0xff);
3606 } 3264 }
3607 3265
3608 if (!shift) 3266 if (shift == 0)
3609 { 3267 {
3610 shift = 6; 3268 shift = 6;
3611 sp++; 3269 sp++;
3612 } 3270 }
3613 3271
3614 else 3272 else
3615 shift -= 2; 3273 shift -= 2;
3616 } 3274 }
3617 } 3275 }
3618 3276
3619 else 3277 else
3620 #endif 3278 #endif
3621 { 3279 {
3622 sp = row; 3280 sp = row;
3623 shift = 6; 3281 shift = 6;
3624 for (i = 0; i < row_width; i++) 3282 for (i = 0; i < row_width; i++)
3625 { 3283 {
3626 if ((png_uint_16)((*sp >> shift) & 0x03) 3284 if ((png_uint_16)((*sp >> shift) & 0x03)
3627 == png_ptr->trans_color.gray) 3285 == png_ptr->trans_color.gray)
3628 { 3286 {
3629 unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); 3287 unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
3630 tmp |= png_ptr->background.gray << shift; 3288 tmp |= png_ptr->background.gray << shift;
3631 *sp = (png_byte)(tmp & 0xff); 3289 *sp = (png_byte)(tmp & 0xff);
3632 } 3290 }
3633 3291
3634 if (!shift) 3292 if (shift == 0)
3635 { 3293 {
3636 shift = 6; 3294 shift = 6;
3637 sp++; 3295 sp++;
3638 } 3296 }
3639 3297
3640 else 3298 else
3641 shift -= 2; 3299 shift -= 2;
3642 } 3300 }
3643 } 3301 }
3644 break; 3302 break;
3645 } 3303 }
3646 3304
3647 case 4: 3305 case 4:
3648 { 3306 {
3649 #ifdef PNG_READ_GAMMA_SUPPORTED 3307 #ifdef PNG_READ_GAMMA_SUPPORTED
3650 if (gamma_table != NULL) 3308 if (gamma_table != NULL)
3651 { 3309 {
3652 sp = row; 3310 sp = row;
3653 shift = 4; 3311 shift = 4;
3654 for (i = 0; i < row_width; i++) 3312 for (i = 0; i < row_width; i++)
3655 { 3313 {
3656 if ((png_uint_16)((*sp >> shift) & 0x0f) 3314 if ((png_uint_16)((*sp >> shift) & 0x0f)
3657 == png_ptr->trans_color.gray) 3315 == png_ptr->trans_color.gray)
3658 { 3316 {
3659 unsigned int tmp = *sp & (0xf0f >> (4 - shift)); 3317 unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
3660 tmp |= png_ptr->background.gray << shift; 3318 tmp |= png_ptr->background.gray << shift;
3661 *sp = (png_byte)(tmp & 0xff); 3319 *sp = (png_byte)(tmp & 0xff);
3662 } 3320 }
3663 3321
3664 else 3322 else
3665 { 3323 {
3666 unsigned int p = (*sp >> shift) & 0x0f; 3324 unsigned int p = (*sp >> shift) & 0x0f;
3667 unsigned int g = (gamma_table[p | (p << 4)] >> 4) & 3325 unsigned int g = (gamma_table[p | (p << 4)] >> 4) &
3668 0x0f; 3326 0x0f;
3669 unsigned int tmp = *sp & (0xf0f >> (4 - shift)); 3327 unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
3670 tmp |= g << shift; 3328 tmp |= g << shift;
3671 *sp = (png_byte)(tmp & 0xff); 3329 *sp = (png_byte)(tmp & 0xff);
3672 } 3330 }
3673 3331
3674 if (!shift) 3332 if (shift == 0)
3675 { 3333 {
3676 shift = 4; 3334 shift = 4;
3677 sp++; 3335 sp++;
3678 } 3336 }
3679 3337
3680 else 3338 else
3681 shift -= 4; 3339 shift -= 4;
3682 } 3340 }
3683 } 3341 }
3684 3342
3685 else 3343 else
3686 #endif 3344 #endif
3687 { 3345 {
3688 sp = row; 3346 sp = row;
3689 shift = 4; 3347 shift = 4;
3690 for (i = 0; i < row_width; i++) 3348 for (i = 0; i < row_width; i++)
3691 { 3349 {
3692 if ((png_uint_16)((*sp >> shift) & 0x0f) 3350 if ((png_uint_16)((*sp >> shift) & 0x0f)
3693 == png_ptr->trans_color.gray) 3351 == png_ptr->trans_color.gray)
3694 { 3352 {
3695 unsigned int tmp = *sp & (0xf0f >> (4 - shift)); 3353 unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
3696 tmp |= png_ptr->background.gray << shift; 3354 tmp |= png_ptr->background.gray << shift;
3697 *sp = (png_byte)(tmp & 0xff); 3355 *sp = (png_byte)(tmp & 0xff);
3698 } 3356 }
3699 3357
3700 if (!shift) 3358 if (shift == 0)
3701 { 3359 {
3702 shift = 4; 3360 shift = 4;
3703 sp++; 3361 sp++;
3704 } 3362 }
3705 3363
3706 else 3364 else
3707 shift -= 4; 3365 shift -= 4;
3708 } 3366 }
3709 } 3367 }
3710 break; 3368 break;
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
3943 /* Background is already in screen gamma */ 3601 /* Background is already in screen gamma */
3944 *sp = (png_byte)png_ptr->background.gray; 3602 *sp = (png_byte)png_ptr->background.gray;
3945 } 3603 }
3946 3604
3947 else 3605 else
3948 { 3606 {
3949 png_byte v, w; 3607 png_byte v, w;
3950 3608
3951 v = gamma_to_1[*sp]; 3609 v = gamma_to_1[*sp];
3952 png_composite(w, v, a, png_ptr->background_1.gray); 3610 png_composite(w, v, a, png_ptr->background_1.gray);
3953 if (!optimize) 3611 if (optimize == 0)
3954 w = gamma_from_1[w]; 3612 w = gamma_from_1[w];
3955 *sp = w; 3613 *sp = w;
3956 } 3614 }
3957 } 3615 }
3958 } 3616 }
3959 else 3617 else
3960 #endif 3618 #endif
3961 { 3619 {
3962 sp = row; 3620 sp = row;
3963 for (i = 0; i < row_width; i++, sp += 2) 3621 for (i = 0; i < row_width; i++, sp += 2)
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
4000 & 0xff); 3658 & 0xff);
4001 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); 3659 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
4002 } 3660 }
4003 3661
4004 else 3662 else
4005 { 3663 {
4006 png_uint_16 g, v, w; 3664 png_uint_16 g, v, w;
4007 3665
4008 g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; 3666 g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
4009 png_composite_16(v, g, a, png_ptr->background_1.gray); 3667 png_composite_16(v, g, a, png_ptr->background_1.gray);
4010 if (optimize) 3668 if (optimize != 0)
4011 w = v; 3669 w = v;
4012 else 3670 else
4013 w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8]; 3671 w = gamma_16_from_1[(v & 0xff) >>
3672 gamma_shift][v >> 8];
4014 *sp = (png_byte)((w >> 8) & 0xff); 3673 *sp = (png_byte)((w >> 8) & 0xff);
4015 *(sp + 1) = (png_byte)(w & 0xff); 3674 *(sp + 1) = (png_byte)(w & 0xff);
4016 } 3675 }
4017 } 3676 }
4018 } 3677 }
4019 else 3678 else
4020 #endif 3679 #endif
4021 { 3680 {
4022 sp = row; 3681 sp = row;
4023 for (i = 0; i < row_width; i++, sp += 4) 3682 for (i = 0; i < row_width; i++, sp += 4)
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
4074 *(sp + 1) = (png_byte)png_ptr->background.green; 3733 *(sp + 1) = (png_byte)png_ptr->background.green;
4075 *(sp + 2) = (png_byte)png_ptr->background.blue; 3734 *(sp + 2) = (png_byte)png_ptr->background.blue;
4076 } 3735 }
4077 3736
4078 else 3737 else
4079 { 3738 {
4080 png_byte v, w; 3739 png_byte v, w;
4081 3740
4082 v = gamma_to_1[*sp]; 3741 v = gamma_to_1[*sp];
4083 png_composite(w, v, a, png_ptr->background_1.red); 3742 png_composite(w, v, a, png_ptr->background_1.red);
4084 if (!optimize) w = gamma_from_1[w]; 3743 if (optimize == 0) w = gamma_from_1[w];
4085 *sp = w; 3744 *sp = w;
4086 3745
4087 v = gamma_to_1[*(sp + 1)]; 3746 v = gamma_to_1[*(sp + 1)];
4088 png_composite(w, v, a, png_ptr->background_1.green); 3747 png_composite(w, v, a, png_ptr->background_1.green);
4089 if (!optimize) w = gamma_from_1[w]; 3748 if (optimize == 0) w = gamma_from_1[w];
4090 *(sp + 1) = w; 3749 *(sp + 1) = w;
4091 3750
4092 v = gamma_to_1[*(sp + 2)]; 3751 v = gamma_to_1[*(sp + 2)];
4093 png_composite(w, v, a, png_ptr->background_1.blue); 3752 png_composite(w, v, a, png_ptr->background_1.blue);
4094 if (!optimize) w = gamma_from_1[w]; 3753 if (optimize == 0) w = gamma_from_1[w];
4095 *(sp + 2) = w; 3754 *(sp + 2) = w;
4096 } 3755 }
4097 } 3756 }
4098 } 3757 }
4099 else 3758 else
4100 #endif 3759 #endif
4101 { 3760 {
4102 sp = row; 3761 sp = row;
4103 for (i = 0; i < row_width; i++, sp += 4) 3762 for (i = 0; i < row_width; i++, sp += 4)
4104 { 3763 {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
4166 & 0xff); 3825 & 0xff);
4167 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); 3826 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
4168 } 3827 }
4169 3828
4170 else 3829 else
4171 { 3830 {
4172 png_uint_16 v, w; 3831 png_uint_16 v, w;
4173 3832
4174 v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; 3833 v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
4175 png_composite_16(w, v, a, png_ptr->background_1.red); 3834 png_composite_16(w, v, a, png_ptr->background_1.red);
4176 if (!optimize) 3835 if (optimize == 0)
4177 w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 3836 w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
4178 8]; 3837 8];
4179 *sp = (png_byte)((w >> 8) & 0xff); 3838 *sp = (png_byte)((w >> 8) & 0xff);
4180 *(sp + 1) = (png_byte)(w & 0xff); 3839 *(sp + 1) = (png_byte)(w & 0xff);
4181 3840
4182 v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)]; 3841 v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
4183 png_composite_16(w, v, a, png_ptr->background_1.green); 3842 png_composite_16(w, v, a, png_ptr->background_1.green);
4184 if (!optimize) 3843 if (optimize == 0)
4185 w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 3844 w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
4186 8]; 3845 8];
4187 3846
4188 *(sp + 2) = (png_byte)((w >> 8) & 0xff); 3847 *(sp + 2) = (png_byte)((w >> 8) & 0xff);
4189 *(sp + 3) = (png_byte)(w & 0xff); 3848 *(sp + 3) = (png_byte)(w & 0xff);
4190 3849
4191 v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)]; 3850 v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
4192 png_composite_16(w, v, a, png_ptr->background_1.blue); 3851 png_composite_16(w, v, a, png_ptr->background_1.blue);
4193 if (!optimize) 3852 if (optimize == 0)
4194 w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 3853 w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
4195 8]; 3854 8];
4196 3855
4197 *(sp + 4) = (png_byte)((w >> 8) & 0xff); 3856 *(sp + 4) = (png_byte)((w >> 8) & 0xff);
4198 *(sp + 5) = (png_byte)(w & 0xff); 3857 *(sp + 5) = (png_byte)(w & 0xff);
4199 } 3858 }
4200 } 3859 }
4201 } 3860 }
4202 3861
4203 else 3862 else
4204 #endif 3863 #endif
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
4248 } 3907 }
4249 } 3908 }
4250 break; 3909 break;
4251 } 3910 }
4252 3911
4253 default: 3912 default:
4254 break; 3913 break;
4255 } 3914 }
4256 } 3915 }
4257 } 3916 }
4258 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_READ_ALPHA_MODE_SUPPORTED */ 3917 #endif /* READ_BACKGROUND || READ_ALPHA_MODE */
4259 3918
4260 #ifdef PNG_READ_GAMMA_SUPPORTED 3919 #ifdef PNG_READ_GAMMA_SUPPORTED
4261 /* Gamma correct the image, avoiding the alpha channel. Make sure 3920 /* Gamma correct the image, avoiding the alpha channel. Make sure
4262 * you do this after you deal with the transparency issue on grayscale 3921 * you do this after you deal with the transparency issue on grayscale
4263 * or RGB images. If your bit depth is 8, use gamma_table, if it 3922 * or RGB images. If your bit depth is 8, use gamma_table, if it
4264 * is 16, use gamma_16_table and gamma_shift. Build these with 3923 * is 16, use gamma_16_table and gamma_shift. Build these with
4265 * build_gamma_table(). 3924 * build_gamma_table().
4266 */ 3925 */
4267 void /* PRIVATE */ 3926 static void
4268 png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr) 3927 png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
4269 { 3928 {
4270 png_const_bytep gamma_table = png_ptr->gamma_table; 3929 png_const_bytep gamma_table = png_ptr->gamma_table;
4271 png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table; 3930 png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table;
4272 int gamma_shift = png_ptr->gamma_shift; 3931 int gamma_shift = png_ptr->gamma_shift;
4273 3932
4274 png_bytep sp; 3933 png_bytep sp;
4275 png_uint_32 i; 3934 png_uint_32 i;
4276 png_uint_32 row_width=row_info->width; 3935 png_uint_32 row_width=row_info->width;
4277 3936
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
4458 } 4117 }
4459 } 4118 }
4460 } 4119 }
4461 #endif 4120 #endif
4462 4121
4463 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED 4122 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
4464 /* Encode the alpha channel to the output gamma (the input channel is always 4123 /* Encode the alpha channel to the output gamma (the input channel is always
4465 * linear.) Called only with color types that have an alpha channel. Needs the 4124 * linear.) Called only with color types that have an alpha channel. Needs the
4466 * from_1 tables. 4125 * from_1 tables.
4467 */ 4126 */
4468 void /* PRIVATE */ 4127 static void
4469 png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr) 4128 png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
4470 { 4129 {
4471 png_uint_32 row_width = row_info->width; 4130 png_uint_32 row_width = row_info->width;
4472 4131
4473 png_debug(1, "in png_do_encode_alpha"); 4132 png_debug(1, "in png_do_encode_alpha");
4474 4133
4475 if (row_info->color_type & PNG_COLOR_MASK_ALPHA) 4134 if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
4476 { 4135 {
4477 if (row_info->bit_depth == 8) 4136 if (row_info->bit_depth == 8)
4478 { 4137 {
4479 PNG_CONST png_bytep table = png_ptr->gamma_from_1; 4138 PNG_CONST png_bytep table = png_ptr->gamma_from_1;
4480 4139
4481 if (table != NULL) 4140 if (table != NULL)
4482 { 4141 {
4483 PNG_CONST int step = 4142 PNG_CONST int step =
4484 (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2; 4143 (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2;
4485 4144
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
4524 * so just issue a warning. 4183 * so just issue a warning.
4525 */ 4184 */
4526 png_warning(png_ptr, "png_do_encode_alpha: unexpected call"); 4185 png_warning(png_ptr, "png_do_encode_alpha: unexpected call");
4527 } 4186 }
4528 #endif 4187 #endif
4529 4188
4530 #ifdef PNG_READ_EXPAND_SUPPORTED 4189 #ifdef PNG_READ_EXPAND_SUPPORTED
4531 /* Expands a palette row to an RGB or RGBA row depending 4190 /* Expands a palette row to an RGB or RGBA row depending
4532 * upon whether you supply trans and num_trans. 4191 * upon whether you supply trans and num_trans.
4533 */ 4192 */
4534 void /* PRIVATE */ 4193 static void
4535 png_do_expand_palette(png_row_infop row_info, png_bytep row, 4194 png_do_expand_palette(png_row_infop row_info, png_bytep row,
4536 png_const_colorp palette, png_const_bytep trans_alpha, int num_trans) 4195 png_const_colorp palette, png_const_bytep trans_alpha, int num_trans)
4537 { 4196 {
4538 int shift, value; 4197 int shift, value;
4539 png_bytep sp, dp; 4198 png_bytep sp, dp;
4540 png_uint_32 i; 4199 png_uint_32 i;
4541 png_uint_32 row_width=row_info->width; 4200 png_uint_32 row_width=row_info->width;
4542 4201
4543 png_debug(1, "in png_do_expand_palette"); 4202 png_debug(1, "in png_do_expand_palette");
4544 4203
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
4677 row_info->channels = 3; 4336 row_info->channels = 3;
4678 } 4337 }
4679 } 4338 }
4680 } 4339 }
4681 } 4340 }
4682 } 4341 }
4683 4342
4684 /* If the bit depth < 8, it is expanded to 8. Also, if the already 4343 /* If the bit depth < 8, it is expanded to 8. Also, if the already
4685 * expanded transparency value is supplied, an alpha channel is built. 4344 * expanded transparency value is supplied, an alpha channel is built.
4686 */ 4345 */
4687 void /* PRIVATE */ 4346 static void
4688 png_do_expand(png_row_infop row_info, png_bytep row, 4347 png_do_expand(png_row_infop row_info, png_bytep row,
4689 png_const_color_16p trans_color) 4348 png_const_color_16p trans_color)
4690 { 4349 {
4691 int shift, value; 4350 int shift, value;
4692 png_bytep sp, dp; 4351 png_bytep sp, dp;
4693 png_uint_32 i; 4352 png_uint_32 i;
4694 png_uint_32 row_width=row_info->width; 4353 png_uint_32 row_width=row_info->width;
4695 4354
4696 png_debug(1, "in png_do_expand"); 4355 png_debug(1, "in png_do_expand");
4697 4356
4698 { 4357 {
4699 if (row_info->color_type == PNG_COLOR_TYPE_GRAY) 4358 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
4700 { 4359 {
4701 unsigned int gray = trans_color ? trans_color->gray : 0; 4360 unsigned int gray = trans_color != NULL ? trans_color->gray : 0;
4702 4361
4703 if (row_info->bit_depth < 8) 4362 if (row_info->bit_depth < 8)
4704 { 4363 {
4705 switch (row_info->bit_depth) 4364 switch (row_info->bit_depth)
4706 { 4365 {
4707 case 1: 4366 case 1:
4708 { 4367 {
4709 gray = (gray & 0x01) * 0xff; 4368 gray = (gray & 0x01) * 0xff;
4710 sp = row + (png_size_t)((row_width - 1) >> 3); 4369 sp = row + (png_size_t)((row_width - 1) >> 3);
4711 dp = row + (png_size_t)row_width - 1; 4370 dp = row + (png_size_t)row_width - 1;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
4793 if (trans_color != NULL) 4452 if (trans_color != NULL)
4794 { 4453 {
4795 if (row_info->bit_depth == 8) 4454 if (row_info->bit_depth == 8)
4796 { 4455 {
4797 gray = gray & 0xff; 4456 gray = gray & 0xff;
4798 sp = row + (png_size_t)row_width - 1; 4457 sp = row + (png_size_t)row_width - 1;
4799 dp = row + (png_size_t)(row_width << 1) - 1; 4458 dp = row + (png_size_t)(row_width << 1) - 1;
4800 4459
4801 for (i = 0; i < row_width; i++) 4460 for (i = 0; i < row_width; i++)
4802 { 4461 {
4803 if (*sp == gray) 4462 if ((*sp & 0xffU) == gray)
4804 *dp-- = 0; 4463 *dp-- = 0;
4805 4464
4806 else 4465 else
4807 *dp-- = 0xff; 4466 *dp-- = 0xff;
4808 4467
4809 *dp-- = *sp--; 4468 *dp-- = *sp--;
4810 } 4469 }
4811 } 4470 }
4812 4471
4813 else if (row_info->bit_depth == 16) 4472 else if (row_info->bit_depth == 16)
4814 { 4473 {
4815 unsigned int gray_high = (gray >> 8) & 0xff; 4474 unsigned int gray_high = (gray >> 8) & 0xff;
4816 unsigned int gray_low = gray & 0xff; 4475 unsigned int gray_low = gray & 0xff;
4817 sp = row + row_info->rowbytes - 1; 4476 sp = row + row_info->rowbytes - 1;
4818 dp = row + (row_info->rowbytes << 1) - 1; 4477 dp = row + (row_info->rowbytes << 1) - 1;
4819 for (i = 0; i < row_width; i++) 4478 for (i = 0; i < row_width; i++)
4820 { 4479 {
4821 if (*(sp - 1) == gray_high && *(sp) == gray_low) 4480 if ((*(sp - 1) & 0xffU) == gray_high &&
4481 (*(sp) & 0xffU) == gray_low)
4822 { 4482 {
4823 *dp-- = 0; 4483 *dp-- = 0;
4824 *dp-- = 0; 4484 *dp-- = 0;
4825 } 4485 }
4826 4486
4827 else 4487 else
4828 { 4488 {
4829 *dp-- = 0xff; 4489 *dp-- = 0xff;
4830 *dp-- = 0xff; 4490 *dp-- = 0xff;
4831 } 4491 }
4832 4492
4833 *dp-- = *sp--; 4493 *dp-- = *sp--;
4834 *dp-- = *sp--; 4494 *dp-- = *sp--;
4835 } 4495 }
4836 } 4496 }
4837 4497
4838 row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; 4498 row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
4839 row_info->channels = 2; 4499 row_info->channels = 2;
4840 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); 4500 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
4841 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, 4501 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
4842 row_width); 4502 row_width);
4843 } 4503 }
4844 } 4504 }
4845 else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_color) 4505 else if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
4506 trans_color != NULL)
4846 { 4507 {
4847 if (row_info->bit_depth == 8) 4508 if (row_info->bit_depth == 8)
4848 { 4509 {
4849 png_byte red = (png_byte)(trans_color->red & 0xff); 4510 png_byte red = (png_byte)(trans_color->red & 0xff);
4850 png_byte green = (png_byte)(trans_color->green & 0xff); 4511 png_byte green = (png_byte)(trans_color->green & 0xff);
4851 png_byte blue = (png_byte)(trans_color->blue & 0xff); 4512 png_byte blue = (png_byte)(trans_color->blue & 0xff);
4852 sp = row + (png_size_t)row_info->rowbytes - 1; 4513 sp = row + (png_size_t)row_info->rowbytes - 1;
4853 dp = row + (png_size_t)(row_width << 2) - 1; 4514 dp = row + (png_size_t)(row_width << 2) - 1;
4854 for (i = 0; i < row_width; i++) 4515 for (i = 0; i < row_width; i++)
4855 { 4516 {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
4907 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); 4568 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4908 } 4569 }
4909 } 4570 }
4910 } 4571 }
4911 #endif 4572 #endif
4912 4573
4913 #ifdef PNG_READ_EXPAND_16_SUPPORTED 4574 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4914 /* If the bit depth is 8 and the color type is not a palette type expand the 4575 /* If the bit depth is 8 and the color type is not a palette type expand the
4915 * whole row to 16 bits. Has no effect otherwise. 4576 * whole row to 16 bits. Has no effect otherwise.
4916 */ 4577 */
4917 void /* PRIVATE */ 4578 static void
4918 png_do_expand_16(png_row_infop row_info, png_bytep row) 4579 png_do_expand_16(png_row_infop row_info, png_bytep row)
4919 { 4580 {
4920 if (row_info->bit_depth == 8 && 4581 if (row_info->bit_depth == 8 &&
4921 row_info->color_type != PNG_COLOR_TYPE_PALETTE) 4582 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
4922 { 4583 {
4923 /* The row have a sequence of bytes containing [0..255] and we need 4584 /* The row have a sequence of bytes containing [0..255] and we need
4924 * to turn it into another row containing [0..65535], to do this we 4585 * to turn it into another row containing [0..65535], to do this we
4925 * calculate: 4586 * calculate:
4926 * 4587 *
4927 * (input / 255) * 65535 4588 * (input / 255) * 65535
4928 * 4589 *
4929 * Which happens to be exactly input * 257 and this can be achieved 4590 * Which happens to be exactly input * 257 and this can be achieved
4930 * simply by byte replication in place (copying backwards). 4591 * simply by byte replication in place (copying backwards).
4931 */ 4592 */
4932 png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */ 4593 png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
4933 png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */ 4594 png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */
4934 while (dp > sp) 4595 while (dp > sp)
4935 dp[-2] = dp[-1] = *--sp, dp -= 2; 4596 dp[-2] = dp[-1] = *--sp, dp -= 2;
4936 4597
4937 row_info->rowbytes *= 2; 4598 row_info->rowbytes *= 2;
4938 row_info->bit_depth = 16; 4599 row_info->bit_depth = 16;
4939 row_info->pixel_depth = (png_byte)(row_info->channels * 16); 4600 row_info->pixel_depth = (png_byte)(row_info->channels * 16);
4940 } 4601 }
4941 } 4602 }
4942 #endif 4603 #endif
4943 4604
4944 #ifdef PNG_READ_QUANTIZE_SUPPORTED 4605 #ifdef PNG_READ_QUANTIZE_SUPPORTED
4945 void /* PRIVATE */ 4606 static void
4946 png_do_quantize(png_row_infop row_info, png_bytep row, 4607 png_do_quantize(png_row_infop row_info, png_bytep row,
4947 png_const_bytep palette_lookup, png_const_bytep quantize_lookup) 4608 png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
4948 { 4609 {
4949 png_bytep sp, dp; 4610 png_bytep sp, dp;
4950 png_uint_32 i; 4611 png_uint_32 i;
4951 png_uint_32 row_width=row_info->width; 4612 png_uint_32 row_width=row_info->width;
4952 4613
4953 png_debug(1, "in png_do_quantize"); 4614 png_debug(1, "in png_do_quantize");
4954 4615
4955 if (row_info->bit_depth == 8) 4616 if (row_info->bit_depth == 8)
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
5026 { 4687 {
5027 sp = row; 4688 sp = row;
5028 4689
5029 for (i = 0; i < row_width; i++, sp++) 4690 for (i = 0; i < row_width; i++, sp++)
5030 { 4691 {
5031 *sp = quantize_lookup[*sp]; 4692 *sp = quantize_lookup[*sp];
5032 } 4693 }
5033 } 4694 }
5034 } 4695 }
5035 } 4696 }
5036 #endif /* PNG_READ_QUANTIZE_SUPPORTED */ 4697 #endif /* READ_QUANTIZE */
5037 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */ 4698
5038 4699 /* Transform the row. The order of transformations is significant,
5039 #ifdef PNG_MNG_FEATURES_SUPPORTED 4700 * and is very touchy. If you add a transformation, take care to
5040 /* Undoes intrapixel differencing */ 4701 * decide how it fits in with the other transformations here.
4702 */
5041 void /* PRIVATE */ 4703 void /* PRIVATE */
5042 png_do_read_intrapixel(png_row_infop row_info, png_bytep row) 4704 png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info)
5043 { 4705 {
5044 png_debug(1, "in png_do_read_intrapixel"); 4706 png_debug(1, "in png_do_read_transformations");
5045 4707
5046 if ( 4708 if (png_ptr->row_buf == NULL)
5047 (row_info->color_type & PNG_COLOR_MASK_COLOR)) 4709 {
5048 { 4710 /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this
5049 int bytes_per_pixel; 4711 * error is incredibly rare and incredibly easy to debug without this
5050 png_uint_32 row_width = row_info->width; 4712 * information.
5051 4713 */
5052 if (row_info->bit_depth == 8) 4714 png_error(png_ptr, "NULL row buffer");
5053 { 4715 }
5054 png_bytep rp; 4716
5055 png_uint_32 i; 4717 /* The following is debugging; prior to 1.5.4 the code was never compiled in;
5056 4718 * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
5057 if (row_info->color_type == PNG_COLOR_TYPE_RGB) 4719 * PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for
5058 bytes_per_pixel = 3; 4720 * all transformations, however in practice the ROW_INIT always gets done on
5059 4721 * demand, if necessary.
5060 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) 4722 */
5061 bytes_per_pixel = 4; 4723 if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
4724 (png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
4725 {
4726 /* Application has failed to call either png_read_start_image() or
4727 * png_read_update_info() after setting transforms that expand pixels.
4728 * This check added to libpng-1.2.19 (but not enabled until 1.5.4).
4729 */
4730 png_error(png_ptr, "Uninitialized row");
4731 }
4732
4733 #ifdef PNG_READ_EXPAND_SUPPORTED
4734 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4735 {
4736 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
4737 {
4738 png_do_expand_palette(row_info, png_ptr->row_buf + 1,
4739 png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
4740 }
4741
4742 else
4743 {
4744 if (png_ptr->num_trans != 0 &&
4745 (png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
4746 png_do_expand(row_info, png_ptr->row_buf + 1,
4747 &(png_ptr->trans_color));
5062 4748
5063 else 4749 else
5064 return; 4750 png_do_expand(row_info, png_ptr->row_buf + 1,
5065 4751 NULL);
5066 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) 4752 }
5067 { 4753 }
5068 *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff); 4754 #endif
5069 *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff); 4755
5070 } 4756 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
5071 } 4757 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
5072 else if (row_info->bit_depth == 16) 4758 (png_ptr->transformations & PNG_COMPOSE) == 0 &&
5073 { 4759 (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
5074 png_bytep rp; 4760 row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
5075 png_uint_32 i; 4761 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
5076 4762 0 /* at_start == false, because SWAP_ALPHA happens later */);
5077 if (row_info->color_type == PNG_COLOR_TYPE_RGB) 4763 #endif
5078 bytes_per_pixel = 6; 4764
5079 4765 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
5080 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) 4766 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
5081 bytes_per_pixel = 8; 4767 {
5082 4768 int rgb_error =
5083 else 4769 png_do_rgb_to_gray(png_ptr, row_info,
5084 return; 4770 png_ptr->row_buf + 1);
5085 4771
5086 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) 4772 if (rgb_error != 0)
5087 { 4773 {
5088 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); 4774 png_ptr->rgb_to_gray_status=1;
5089 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); 4775 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
5090 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); 4776 PNG_RGB_TO_GRAY_WARN)
5091 png_uint_32 red = (s0 + s1 + 65536) & 0xffff; 4777 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
5092 png_uint_32 blue = (s2 + s1 + 65536) & 0xffff; 4778
5093 *(rp ) = (png_byte)((red >> 8) & 0xff); 4779 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
5094 *(rp + 1) = (png_byte)(red & 0xff); 4780 PNG_RGB_TO_GRAY_ERR)
5095 *(rp + 4) = (png_byte)((blue >> 8) & 0xff); 4781 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
5096 *(rp + 5) = (png_byte)(blue & 0xff); 4782 }
5097 } 4783 }
5098 } 4784 #endif
5099 } 4785
4786 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
4787 *
4788 * In most cases, the "simple transparency" should be done prior to doing
4789 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
4790 * pixel is transparent. You would also need to make sure that the
4791 * transparency information is upgraded to RGB.
4792 *
4793 * To summarize, the current flow is:
4794 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
4795 * with background "in place" if transparent,
4796 * convert to RGB if necessary
4797 * - Gray + alpha -> composite with gray background and remove alpha bytes,
4798 * convert to RGB if necessary
4799 *
4800 * To support RGB backgrounds for gray images we need:
4801 * - Gray + simple transparency -> convert to RGB + simple transparency,
4802 * compare 3 or 6 bytes and composite with
4803 * background "in place" if transparent
4804 * (3x compare/pixel compared to doing
4805 * composite with gray bkgrnd)
4806 * - Gray + alpha -> convert to RGB + alpha, composite with background and
4807 * remove alpha bytes (3x float
4808 * operations/pixel compared with composite
4809 * on gray background)
4810 *
4811 * Greg's change will do this. The reason it wasn't done before is for
4812 * performance, as this increases the per-pixel operations. If we would check
4813 * in advance if the background was gray or RGB, and position the gray-to-RGB
4814 * transform appropriately, then it would save a lot of work/time.
4815 */
4816
4817 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4818 /* If gray -> RGB, do so now only if background is non-gray; else do later
4819 * for performance reasons
4820 */
4821 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
4822 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) == 0)
4823 png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
4824 #endif
4825
4826 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
4827 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
4828 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
4829 png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr);
4830 #endif
4831
4832 #ifdef PNG_READ_GAMMA_SUPPORTED
4833 if ((png_ptr->transformations & PNG_GAMMA) != 0 &&
4834 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
4835 /* Because RGB_TO_GRAY does the gamma transform. */
4836 (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0 &&
4837 #endif
4838 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
4839 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
4840 /* Because PNG_COMPOSE does the gamma transform if there is something to
4841 * do (if there is an alpha channel or transparency.)
4842 */
4843 !((png_ptr->transformations & PNG_COMPOSE) != 0 &&
4844 ((png_ptr->num_trans != 0) ||
4845 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)) &&
4846 #endif
4847 /* Because png_init_read_transformations transforms the palette, unless
4848 * RGB_TO_GRAY will do the transform.
4849 */
4850 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
4851 png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr);
4852 #endif
4853
4854 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
4855 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
4856 (png_ptr->transformations & PNG_COMPOSE) != 0 &&
4857 (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
4858 row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
4859 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
4860 0 /* at_start == false, because SWAP_ALPHA happens later */);
4861 #endif
4862
4863 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
4864 if ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 &&
4865 (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
4866 png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr);
4867 #endif
4868
4869 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
4870 if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
4871 png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1);
4872 #endif
4873
4874 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
4875 /* There is no harm in doing both of these because only one has any effect,
4876 * by putting the 'scale' option first if the app asks for scale (either by
4877 * calling the API or in a TRANSFORM flag) this is what happens.
4878 */
4879 if ((png_ptr->transformations & PNG_16_TO_8) != 0)
4880 png_do_chop(row_info, png_ptr->row_buf + 1);
4881 #endif
4882
4883 #ifdef PNG_READ_QUANTIZE_SUPPORTED
4884 if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
4885 {
4886 png_do_quantize(row_info, png_ptr->row_buf + 1,
4887 png_ptr->palette_lookup, png_ptr->quantize_index);
4888
4889 if (row_info->rowbytes == 0)
4890 png_error(png_ptr, "png_do_quantize returned rowbytes=0");
4891 }
4892 #endif /* READ_QUANTIZE */
4893
4894 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4895 /* Do the expansion now, after all the arithmetic has been done. Notice
4896 * that previous transformations can handle the PNG_EXPAND_16 flag if this
4897 * is efficient (particularly true in the case of gamma correction, where
4898 * better accuracy results faster!)
4899 */
4900 if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
4901 png_do_expand_16(row_info, png_ptr->row_buf + 1);
4902 #endif
4903
4904 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4905 /* NOTE: moved here in 1.5.4 (from much later in this list.) */
4906 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
4907 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) != 0)
4908 png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
4909 #endif
4910
4911 #ifdef PNG_READ_INVERT_SUPPORTED
4912 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
4913 png_do_invert(row_info, png_ptr->row_buf + 1);
4914 #endif
4915
4916 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
4917 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
4918 png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1);
4919 #endif
4920
4921 #ifdef PNG_READ_SHIFT_SUPPORTED
4922 if ((png_ptr->transformations & PNG_SHIFT) != 0)
4923 png_do_unshift(row_info, png_ptr->row_buf + 1,
4924 &(png_ptr->shift));
4925 #endif
4926
4927 #ifdef PNG_READ_PACK_SUPPORTED
4928 if ((png_ptr->transformations & PNG_PACK) != 0)
4929 png_do_unpack(row_info, png_ptr->row_buf + 1);
4930 #endif
4931
4932 #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
4933 /* Added at libpng-1.5.10 */
4934 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
4935 png_ptr->num_palette_max >= 0)
4936 png_do_check_palette_indexes(png_ptr, row_info);
4937 #endif
4938
4939 #ifdef PNG_READ_BGR_SUPPORTED
4940 if ((png_ptr->transformations & PNG_BGR) != 0)
4941 png_do_bgr(row_info, png_ptr->row_buf + 1);
4942 #endif
4943
4944 #ifdef PNG_READ_PACKSWAP_SUPPORTED
4945 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
4946 png_do_packswap(row_info, png_ptr->row_buf + 1);
4947 #endif
4948
4949 #ifdef PNG_READ_FILLER_SUPPORTED
4950 if ((png_ptr->transformations & PNG_FILLER) != 0)
4951 png_do_read_filler(row_info, png_ptr->row_buf + 1,
4952 (png_uint_32)png_ptr->filler, png_ptr->flags);
4953 #endif
4954
4955 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
4956 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
4957 png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1);
4958 #endif
4959
4960 #ifdef PNG_READ_16BIT_SUPPORTED
4961 #ifdef PNG_READ_SWAP_SUPPORTED
4962 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
4963 png_do_swap(row_info, png_ptr->row_buf + 1);
4964 #endif
4965 #endif
4966
4967 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
4968 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
4969 {
4970 if (png_ptr->read_user_transform_fn != NULL)
4971 (*(png_ptr->read_user_transform_fn)) /* User read transform function */
4972 (png_ptr, /* png_ptr */
4973 row_info, /* row_info: */
4974 /* png_uint_32 width; width of row */
4975 /* png_size_t rowbytes; number of bytes in row */
4976 /* png_byte color_type; color type of pixels */
4977 /* png_byte bit_depth; bit depth of samples */
4978 /* png_byte channels; number of channels (1-4) */
4979 /* png_byte pixel_depth; bits per pixel (depth*channels) */
4980 png_ptr->row_buf + 1); /* start of pixel data for row */
4981 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
4982 if (png_ptr->user_transform_depth != 0)
4983 row_info->bit_depth = png_ptr->user_transform_depth;
4984
4985 if (png_ptr->user_transform_channels != 0)
4986 row_info->channels = png_ptr->user_transform_channels;
4987 #endif
4988 row_info->pixel_depth = (png_byte)(row_info->bit_depth *
4989 row_info->channels);
4990
4991 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width);
4992 }
4993 #endif
5100 } 4994 }
5101 #endif /* PNG_MNG_FEATURES_SUPPORTED */ 4995
5102 #endif /* PNG_READ_SUPPORTED */ 4996 #endif /* READ_TRANSFORMS */
4997 #endif /* READ */
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