[USB]
[reactos.git] / reactos / dll / 3rdparty / libpng / pngrtran.c
1
2 /* pngrtran.c - transforms the data in a row for PNG readers
3 *
4 * Last changed in libpng 1.5.5 [September 22, 2011]
5 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8 *
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
12 *
13 * This file contains functions optionally called by an application
14 * in order to tell libpng how to handle data when reading a PNG.
15 * Transformations that are used in both reading and writing are
16 * in pngtrans.c.
17 */
18
19 #include "pngpriv.h"
20
21 #ifdef PNG_READ_SUPPORTED
22
23 /* Set the action on getting a CRC error for an ancillary or critical chunk. */
24 void PNGAPI
25 png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
26 {
27 png_debug(1, "in png_set_crc_action");
28
29 if (png_ptr == NULL)
30 return;
31
32 /* Tell libpng how we react to CRC errors in critical chunks */
33 switch (crit_action)
34 {
35 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
36 break;
37
38 case PNG_CRC_WARN_USE: /* Warn/use data */
39 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
40 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
41 break;
42
43 case PNG_CRC_QUIET_USE: /* Quiet/use data */
44 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
45 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
46 PNG_FLAG_CRC_CRITICAL_IGNORE;
47 break;
48
49 case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
50 png_warning(png_ptr,
51 "Can't discard critical data on CRC error");
52 case PNG_CRC_ERROR_QUIT: /* Error/quit */
53
54 case PNG_CRC_DEFAULT:
55 default:
56 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
57 break;
58 }
59
60 /* Tell libpng how we react to CRC errors in ancillary chunks */
61 switch (ancil_action)
62 {
63 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
64 break;
65
66 case PNG_CRC_WARN_USE: /* Warn/use data */
67 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
68 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
69 break;
70
71 case PNG_CRC_QUIET_USE: /* Quiet/use data */
72 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
73 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
74 PNG_FLAG_CRC_ANCILLARY_NOWARN;
75 break;
76
77 case PNG_CRC_ERROR_QUIT: /* Error/quit */
78 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
79 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
80 break;
81
82 case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
83
84 case PNG_CRC_DEFAULT:
85 default:
86 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
87 break;
88 }
89 }
90
91 #ifdef PNG_READ_BACKGROUND_SUPPORTED
92 /* Handle alpha and tRNS via a background color */
93 void PNGFAPI
94 png_set_background_fixed(png_structp png_ptr,
95 png_const_color_16p background_color, int background_gamma_code,
96 int need_expand, png_fixed_point background_gamma)
97 {
98 png_debug(1, "in png_set_background_fixed");
99
100 if (png_ptr == NULL)
101 return;
102
103 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
104 {
105 png_warning(png_ptr, "Application must supply a known background gamma");
106 return;
107 }
108
109 png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA;
110 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
111 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
112
113 png_memcpy(&(png_ptr->background), background_color,
114 png_sizeof(png_color_16));
115 png_ptr->background_gamma = background_gamma;
116 png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
117 if (need_expand)
118 png_ptr->transformations |= PNG_BACKGROUND_EXPAND;
119 else
120 png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
121 }
122
123 # ifdef PNG_FLOATING_POINT_SUPPORTED
124 void PNGAPI
125 png_set_background(png_structp png_ptr,
126 png_const_color_16p background_color, int background_gamma_code,
127 int need_expand, double background_gamma)
128 {
129 png_set_background_fixed(png_ptr, background_color, background_gamma_code,
130 need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
131 }
132 # endif /* FLOATING_POINT */
133 #endif /* READ_BACKGROUND */
134
135 /* Scale 16-bit depth files to 8-bit depth. If both of these are set then the
136 * one that pngrtran does first (scale) happens. This is necessary to allow the
137 * TRANSFORM and API behavior to be somewhat consistent, and it's simpler.
138 */
139 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
140 void PNGAPI
141 png_set_scale_16(png_structp png_ptr)
142 {
143 png_debug(1, "in png_set_scale_16");
144
145 if (png_ptr == NULL)
146 return;
147
148 png_ptr->transformations |= PNG_SCALE_16_TO_8;
149 }
150 #endif
151
152 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
153 /* Chop 16-bit depth files to 8-bit depth */
154 void PNGAPI
155 png_set_strip_16(png_structp png_ptr)
156 {
157 png_debug(1, "in png_set_strip_16");
158
159 if (png_ptr == NULL)
160 return;
161
162 png_ptr->transformations |= PNG_16_TO_8;
163 }
164 #endif
165
166 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
167 void PNGAPI
168 png_set_strip_alpha(png_structp png_ptr)
169 {
170 png_debug(1, "in png_set_strip_alpha");
171
172 if (png_ptr == NULL)
173 return;
174
175 png_ptr->transformations |= PNG_STRIP_ALPHA;
176 }
177 #endif
178
179 #if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED)
180 static png_fixed_point
181 translate_gamma_flags(png_structp png_ptr, png_fixed_point output_gamma,
182 int is_screen)
183 {
184 /* Check for flag values. The main reason for having the old Mac value as a
185 * flag is that it is pretty near impossible to work out what the correct
186 * value is from Apple documentation - a working Mac system is needed to
187 * discover the value!
188 */
189 if (output_gamma == PNG_DEFAULT_sRGB ||
190 output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB)
191 {
192 /* If there is no sRGB support this just sets the gamma to the standard
193 * sRGB value. (This is a side effect of using this function!)
194 */
195 # ifdef PNG_READ_sRGB_SUPPORTED
196 png_ptr->flags |= PNG_FLAG_ASSUME_sRGB;
197 # endif
198 if (is_screen)
199 output_gamma = PNG_GAMMA_sRGB;
200 else
201 output_gamma = PNG_GAMMA_sRGB_INVERSE;
202 }
203
204 else if (output_gamma == PNG_GAMMA_MAC_18 ||
205 output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18)
206 {
207 if (is_screen)
208 output_gamma = PNG_GAMMA_MAC_OLD;
209 else
210 output_gamma = PNG_GAMMA_MAC_INVERSE;
211 }
212
213 return output_gamma;
214 }
215
216 # ifdef PNG_FLOATING_POINT_SUPPORTED
217 static png_fixed_point
218 convert_gamma_value(png_structp png_ptr, double output_gamma)
219 {
220 /* The following silently ignores cases where fixed point (times 100,000)
221 * gamma values are passed to the floating point API. This is safe and it
222 * means the fixed point constants work just fine with the floating point
223 * API. The alternative would just lead to undetected errors and spurious
224 * bug reports. Negative values fail inside the _fixed API unless they
225 * correspond to the flag values.
226 */
227 if (output_gamma > 0 && output_gamma < 128)
228 output_gamma *= PNG_FP_1;
229
230 /* This preserves -1 and -2 exactly: */
231 output_gamma = floor(output_gamma + .5);
232
233 if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN)
234 png_fixed_error(png_ptr, "gamma value");
235
236 return (png_fixed_point)output_gamma;
237 }
238 # endif
239 #endif /* READ_ALPHA_MODE || READ_GAMMA */
240
241 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
242 void PNGFAPI
243 png_set_alpha_mode_fixed(png_structp png_ptr, int mode,
244 png_fixed_point output_gamma)
245 {
246 int compose = 0;
247 png_fixed_point file_gamma;
248
249 png_debug(1, "in png_set_alpha_mode");
250
251 if (png_ptr == NULL)
252 return;
253
254 output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/);
255
256 /* Validate the value to ensure it is in a reasonable range. The value
257 * is expected to be 1 or greater, but this range test allows for some
258 * viewing correction values. The intent is to weed out users of this API
259 * who use the inverse of the gamma value accidentally! Since some of these
260 * values are reasonable this may have to be changed.
261 */
262 if (output_gamma < 70000 || output_gamma > 300000)
263 png_error(png_ptr, "output gamma out of expected range");
264
265 /* The default file gamma is the inverse of the output gamma; the output
266 * gamma may be changed below so get the file value first:
267 */
268 file_gamma = png_reciprocal(output_gamma);
269
270 /* There are really 8 possibilities here, composed of any combination
271 * of:
272 *
273 * premultiply the color channels
274 * do not encode non-opaque pixels
275 * encode the alpha as well as the color channels
276 *
277 * The differences disappear if the input/output ('screen') gamma is 1.0,
278 * because then the encoding is a no-op and there is only the choice of
279 * premultiplying the color channels or not.
280 *
281 * png_set_alpha_mode and png_set_background interact because both use
282 * png_compose to do the work. Calling both is only useful when
283 * png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along
284 * with a default gamma value. Otherwise PNG_COMPOSE must not be set.
285 */
286 switch (mode)
287 {
288 case PNG_ALPHA_PNG: /* default: png standard */
289 /* No compose, but it may be set by png_set_background! */
290 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
291 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
292 break;
293
294 case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */
295 compose = 1;
296 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
297 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
298 /* The output is linear: */
299 output_gamma = PNG_FP_1;
300 break;
301
302 case PNG_ALPHA_OPTIMIZED: /* associated, non-opaque pixels linear */
303 compose = 1;
304 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
305 png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA;
306 /* output_gamma records the encoding of opaque pixels! */
307 break;
308
309 case PNG_ALPHA_BROKEN: /* associated, non-linear, alpha encoded */
310 compose = 1;
311 png_ptr->transformations |= PNG_ENCODE_ALPHA;
312 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
313 break;
314
315 default:
316 png_error(png_ptr, "invalid alpha mode");
317 }
318
319 /* Only set the default gamma if the file gamma has not been set (this has
320 * the side effect that the gamma in a second call to png_set_alpha_mode will
321 * be ignored.)
322 */
323 if (png_ptr->gamma == 0)
324 png_ptr->gamma = file_gamma;
325
326 /* But always set the output gamma: */
327 png_ptr->screen_gamma = output_gamma;
328
329 /* Finally, if pre-multiplying, set the background fields to achieve the
330 * desired result.
331 */
332 if (compose)
333 {
334 /* And obtain alpha pre-multiplication by composing on black: */
335 png_memset(&png_ptr->background, 0, sizeof png_ptr->background);
336 png_ptr->background_gamma = png_ptr->gamma; /* just in case */
337 png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE;
338 png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
339
340 if (png_ptr->transformations & PNG_COMPOSE)
341 png_error(png_ptr,
342 "conflicting calls to set alpha mode and background");
343
344 png_ptr->transformations |= PNG_COMPOSE;
345 }
346
347 /* New API, make sure apps call the correct initializers: */
348 png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
349 }
350
351 # ifdef PNG_FLOATING_POINT_SUPPORTED
352 void PNGAPI
353 png_set_alpha_mode(png_structp png_ptr, int mode, double output_gamma)
354 {
355 png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr,
356 output_gamma));
357 }
358 # endif
359 #endif
360
361 #ifdef PNG_READ_QUANTIZE_SUPPORTED
362 /* Dither file to 8-bit. Supply a palette, the current number
363 * of elements in the palette, the maximum number of elements
364 * allowed, and a histogram if possible. If the current number
365 * of colors is greater then the maximum number, the palette will be
366 * modified to fit in the maximum number. "full_quantize" indicates
367 * whether we need a quantizing cube set up for RGB images, or if we
368 * simply are reducing the number of colors in a paletted image.
369 */
370
371 typedef struct png_dsort_struct
372 {
373 struct png_dsort_struct FAR * next;
374 png_byte left;
375 png_byte right;
376 } png_dsort;
377 typedef png_dsort FAR * png_dsortp;
378 typedef png_dsort FAR * FAR * png_dsortpp;
379
380 void PNGAPI
381 png_set_quantize(png_structp png_ptr, png_colorp palette,
382 int num_palette, int maximum_colors, png_const_uint_16p histogram,
383 int full_quantize)
384 {
385 png_debug(1, "in png_set_quantize");
386
387 if (png_ptr == NULL)
388 return;
389
390 png_ptr->transformations |= PNG_QUANTIZE;
391
392 if (!full_quantize)
393 {
394 int i;
395
396 png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,
397 (png_uint_32)(num_palette * png_sizeof(png_byte)));
398 for (i = 0; i < num_palette; i++)
399 png_ptr->quantize_index[i] = (png_byte)i;
400 }
401
402 if (num_palette > maximum_colors)
403 {
404 if (histogram != NULL)
405 {
406 /* This is easy enough, just throw out the least used colors.
407 * Perhaps not the best solution, but good enough.
408 */
409
410 int i;
411
412 /* Initialize an array to sort colors */
413 png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
414 (png_uint_32)(num_palette * png_sizeof(png_byte)));
415
416 /* Initialize the quantize_sort array */
417 for (i = 0; i < num_palette; i++)
418 png_ptr->quantize_sort[i] = (png_byte)i;
419
420 /* Find the least used palette entries by starting a
421 * bubble sort, and running it until we have sorted
422 * out enough colors. Note that we don't care about
423 * sorting all the colors, just finding which are
424 * least used.
425 */
426
427 for (i = num_palette - 1; i >= maximum_colors; i--)
428 {
429 int done; /* To stop early if the list is pre-sorted */
430 int j;
431
432 done = 1;
433 for (j = 0; j < i; j++)
434 {
435 if (histogram[png_ptr->quantize_sort[j]]
436 < histogram[png_ptr->quantize_sort[j + 1]])
437 {
438 png_byte t;
439
440 t = png_ptr->quantize_sort[j];
441 png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];
442 png_ptr->quantize_sort[j + 1] = t;
443 done = 0;
444 }
445 }
446
447 if (done)
448 break;
449 }
450
451 /* Swap the palette around, and set up a table, if necessary */
452 if (full_quantize)
453 {
454 int j = num_palette;
455
456 /* Put all the useful colors within the max, but don't
457 * move the others.
458 */
459 for (i = 0; i < maximum_colors; i++)
460 {
461 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
462 {
463 do
464 j--;
465 while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
466
467 palette[i] = palette[j];
468 }
469 }
470 }
471 else
472 {
473 int j = num_palette;
474
475 /* Move all the used colors inside the max limit, and
476 * develop a translation table.
477 */
478 for (i = 0; i < maximum_colors; i++)
479 {
480 /* Only move the colors we need to */
481 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
482 {
483 png_color tmp_color;
484
485 do
486 j--;
487 while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
488
489 tmp_color = palette[j];
490 palette[j] = palette[i];
491 palette[i] = tmp_color;
492 /* Indicate where the color went */
493 png_ptr->quantize_index[j] = (png_byte)i;
494 png_ptr->quantize_index[i] = (png_byte)j;
495 }
496 }
497
498 /* Find closest color for those colors we are not using */
499 for (i = 0; i < num_palette; i++)
500 {
501 if ((int)png_ptr->quantize_index[i] >= maximum_colors)
502 {
503 int min_d, k, min_k, d_index;
504
505 /* Find the closest color to one we threw out */
506 d_index = png_ptr->quantize_index[i];
507 min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
508 for (k = 1, min_k = 0; k < maximum_colors; k++)
509 {
510 int d;
511
512 d = PNG_COLOR_DIST(palette[d_index], palette[k]);
513
514 if (d < min_d)
515 {
516 min_d = d;
517 min_k = k;
518 }
519 }
520 /* Point to closest color */
521 png_ptr->quantize_index[i] = (png_byte)min_k;
522 }
523 }
524 }
525 png_free(png_ptr, png_ptr->quantize_sort);
526 png_ptr->quantize_sort = NULL;
527 }
528 else
529 {
530 /* This is much harder to do simply (and quickly). Perhaps
531 * we need to go through a median cut routine, but those
532 * don't always behave themselves with only a few colors
533 * as input. So we will just find the closest two colors,
534 * and throw out one of them (chosen somewhat randomly).
535 * [We don't understand this at all, so if someone wants to
536 * work on improving it, be our guest - AED, GRP]
537 */
538 int i;
539 int max_d;
540 int num_new_palette;
541 png_dsortp t;
542 png_dsortpp hash;
543
544 t = NULL;
545
546 /* Initialize palette index arrays */
547 png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
548 (png_uint_32)(num_palette * png_sizeof(png_byte)));
549 png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
550 (png_uint_32)(num_palette * png_sizeof(png_byte)));
551
552 /* Initialize the sort array */
553 for (i = 0; i < num_palette; i++)
554 {
555 png_ptr->index_to_palette[i] = (png_byte)i;
556 png_ptr->palette_to_index[i] = (png_byte)i;
557 }
558
559 hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
560 png_sizeof(png_dsortp)));
561
562 num_new_palette = num_palette;
563
564 /* Initial wild guess at how far apart the farthest pixel
565 * pair we will be eliminating will be. Larger
566 * numbers mean more areas will be allocated, Smaller
567 * numbers run the risk of not saving enough data, and
568 * having to do this all over again.
569 *
570 * I have not done extensive checking on this number.
571 */
572 max_d = 96;
573
574 while (num_new_palette > maximum_colors)
575 {
576 for (i = 0; i < num_new_palette - 1; i++)
577 {
578 int j;
579
580 for (j = i + 1; j < num_new_palette; j++)
581 {
582 int d;
583
584 d = PNG_COLOR_DIST(palette[i], palette[j]);
585
586 if (d <= max_d)
587 {
588
589 t = (png_dsortp)png_malloc_warn(png_ptr,
590 (png_uint_32)(png_sizeof(png_dsort)));
591
592 if (t == NULL)
593 break;
594
595 t->next = hash[d];
596 t->left = (png_byte)i;
597 t->right = (png_byte)j;
598 hash[d] = t;
599 }
600 }
601 if (t == NULL)
602 break;
603 }
604
605 if (t != NULL)
606 for (i = 0; i <= max_d; i++)
607 {
608 if (hash[i] != NULL)
609 {
610 png_dsortp p;
611
612 for (p = hash[i]; p; p = p->next)
613 {
614 if ((int)png_ptr->index_to_palette[p->left]
615 < num_new_palette &&
616 (int)png_ptr->index_to_palette[p->right]
617 < num_new_palette)
618 {
619 int j, next_j;
620
621 if (num_new_palette & 0x01)
622 {
623 j = p->left;
624 next_j = p->right;
625 }
626 else
627 {
628 j = p->right;
629 next_j = p->left;
630 }
631
632 num_new_palette--;
633 palette[png_ptr->index_to_palette[j]]
634 = palette[num_new_palette];
635 if (!full_quantize)
636 {
637 int k;
638
639 for (k = 0; k < num_palette; k++)
640 {
641 if (png_ptr->quantize_index[k] ==
642 png_ptr->index_to_palette[j])
643 png_ptr->quantize_index[k] =
644 png_ptr->index_to_palette[next_j];
645
646 if ((int)png_ptr->quantize_index[k] ==
647 num_new_palette)
648 png_ptr->quantize_index[k] =
649 png_ptr->index_to_palette[j];
650 }
651 }
652
653 png_ptr->index_to_palette[png_ptr->palette_to_index
654 [num_new_palette]] = png_ptr->index_to_palette[j];
655
656 png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
657 = png_ptr->palette_to_index[num_new_palette];
658
659 png_ptr->index_to_palette[j] =
660 (png_byte)num_new_palette;
661
662 png_ptr->palette_to_index[num_new_palette] =
663 (png_byte)j;
664 }
665 if (num_new_palette <= maximum_colors)
666 break;
667 }
668 if (num_new_palette <= maximum_colors)
669 break;
670 }
671 }
672
673 for (i = 0; i < 769; i++)
674 {
675 if (hash[i] != NULL)
676 {
677 png_dsortp p = hash[i];
678 while (p)
679 {
680 t = p->next;
681 png_free(png_ptr, p);
682 p = t;
683 }
684 }
685 hash[i] = 0;
686 }
687 max_d += 96;
688 }
689 png_free(png_ptr, hash);
690 png_free(png_ptr, png_ptr->palette_to_index);
691 png_free(png_ptr, png_ptr->index_to_palette);
692 png_ptr->palette_to_index = NULL;
693 png_ptr->index_to_palette = NULL;
694 }
695 num_palette = maximum_colors;
696 }
697 if (png_ptr->palette == NULL)
698 {
699 png_ptr->palette = palette;
700 }
701 png_ptr->num_palette = (png_uint_16)num_palette;
702
703 if (full_quantize)
704 {
705 int i;
706 png_bytep distance;
707 int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +
708 PNG_QUANTIZE_BLUE_BITS;
709 int num_red = (1 << PNG_QUANTIZE_RED_BITS);
710 int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);
711 int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
712 png_size_t num_entries = ((png_size_t)1 << total_bits);
713
714 png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr,
715 (png_uint_32)(num_entries * png_sizeof(png_byte)));
716
717 distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
718 png_sizeof(png_byte)));
719
720 png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
721
722 for (i = 0; i < num_palette; i++)
723 {
724 int ir, ig, ib;
725 int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS));
726 int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS));
727 int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS));
728
729 for (ir = 0; ir < num_red; ir++)
730 {
731 /* int dr = abs(ir - r); */
732 int dr = ((ir > r) ? ir - r : r - ir);
733 int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS +
734 PNG_QUANTIZE_GREEN_BITS));
735
736 for (ig = 0; ig < num_green; ig++)
737 {
738 /* int dg = abs(ig - g); */
739 int dg = ((ig > g) ? ig - g : g - ig);
740 int dt = dr + dg;
741 int dm = ((dr > dg) ? dr : dg);
742 int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS);
743
744 for (ib = 0; ib < num_blue; ib++)
745 {
746 int d_index = index_g | ib;
747 /* int db = abs(ib - b); */
748 int db = ((ib > b) ? ib - b : b - ib);
749 int dmax = ((dm > db) ? dm : db);
750 int d = dmax + dt + db;
751
752 if (d < (int)distance[d_index])
753 {
754 distance[d_index] = (png_byte)d;
755 png_ptr->palette_lookup[d_index] = (png_byte)i;
756 }
757 }
758 }
759 }
760 }
761
762 png_free(png_ptr, distance);
763 }
764 }
765 #endif /* PNG_READ_QUANTIZE_SUPPORTED */
766
767 #ifdef PNG_READ_GAMMA_SUPPORTED
768 void PNGFAPI
769 png_set_gamma_fixed(png_structp png_ptr, png_fixed_point scrn_gamma,
770 png_fixed_point file_gamma)
771 {
772 png_debug(1, "in png_set_gamma_fixed");
773
774 if (png_ptr == NULL)
775 return;
776
777 /* New in libpng-1.5.4 - reserve particular negative values as flags. */
778 scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/);
779 file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/);
780
781 #if PNG_LIBPNG_VER >= 10600
782 /* Checking the gamma values for being >0 was added in 1.5.4 along with the
783 * premultiplied alpha support; this actually hides an undocumented feature
784 * of the previous implementation which allowed gamma processing to be
785 * disabled in background handling. There is no evidence (so far) that this
786 * was being used; however, png_set_background itself accepted and must still
787 * accept '0' for the gamma value it takes, because it isn't always used.
788 *
789 * Since this is an API change (albeit a very minor one that removes an
790 * undocumented API feature) it will not be made until libpng-1.6.0.
791 */
792 if (file_gamma <= 0)
793 png_error(png_ptr, "invalid file gamma in png_set_gamma");
794
795 if (scrn_gamma <= 0)
796 png_error(png_ptr, "invalid screen gamma in png_set_gamma");
797 #endif
798
799 /* Set the gamma values unconditionally - this overrides the value in the PNG
800 * file if a gAMA chunk was present. png_set_alpha_mode provides a
801 * different, easier, way to default the file gamma.
802 */
803 png_ptr->gamma = file_gamma;
804 png_ptr->screen_gamma = scrn_gamma;
805 }
806
807 # ifdef PNG_FLOATING_POINT_SUPPORTED
808 void PNGAPI
809 png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
810 {
811 png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma),
812 convert_gamma_value(png_ptr, file_gamma));
813 }
814 # endif /* FLOATING_POINT_SUPPORTED */
815 #endif /* READ_GAMMA */
816
817 #ifdef PNG_READ_EXPAND_SUPPORTED
818 /* Expand paletted images to RGB, expand grayscale images of
819 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
820 * to alpha channels.
821 */
822 void PNGAPI
823 png_set_expand(png_structp png_ptr)
824 {
825 png_debug(1, "in png_set_expand");
826
827 if (png_ptr == NULL)
828 return;
829
830 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
831 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
832 }
833
834 /* GRR 19990627: the following three functions currently are identical
835 * to png_set_expand(). However, it is entirely reasonable that someone
836 * might wish to expand an indexed image to RGB but *not* expand a single,
837 * fully transparent palette entry to a full alpha channel--perhaps instead
838 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
839 * the transparent color with a particular RGB value, or drop tRNS entirely.
840 * IOW, a future version of the library may make the transformations flag
841 * a bit more fine-grained, with separate bits for each of these three
842 * functions.
843 *
844 * More to the point, these functions make it obvious what libpng will be
845 * doing, whereas "expand" can (and does) mean any number of things.
846 *
847 * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
848 * to expand only the sample depth but not to expand the tRNS to alpha
849 * and its name was changed to png_set_expand_gray_1_2_4_to_8().
850 */
851
852 /* Expand paletted images to RGB. */
853 void PNGAPI
854 png_set_palette_to_rgb(png_structp png_ptr)
855 {
856 png_debug(1, "in png_set_palette_to_rgb");
857
858 if (png_ptr == NULL)
859 return;
860
861 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
862 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
863 }
864
865 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
866 void PNGAPI
867 png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
868 {
869 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
870
871 if (png_ptr == NULL)
872 return;
873
874 png_ptr->transformations |= PNG_EXPAND;
875 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
876 }
877
878
879
880 /* Expand tRNS chunks to alpha channels. */
881 void PNGAPI
882 png_set_tRNS_to_alpha(png_structp png_ptr)
883 {
884 png_debug(1, "in png_set_tRNS_to_alpha");
885
886 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
887 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
888 }
889 #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
890
891 #ifdef PNG_READ_EXPAND_16_SUPPORTED
892 /* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise
893 * it may not work correctly.)
894 */
895 void PNGAPI
896 png_set_expand_16(png_structp png_ptr)
897 {
898 png_debug(1, "in png_set_expand_16");
899
900 if (png_ptr == NULL)
901 return;
902
903 png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
904 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
905
906 /* New API, make sure apps call the correct initializers: */
907 png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
908 }
909 #endif
910
911 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
912 void PNGAPI
913 png_set_gray_to_rgb(png_structp png_ptr)
914 {
915 png_debug(1, "in png_set_gray_to_rgb");
916
917 if (png_ptr != NULL)
918 {
919 /* Because rgb must be 8 bits or more: */
920 png_set_expand_gray_1_2_4_to_8(png_ptr);
921 png_ptr->transformations |= PNG_GRAY_TO_RGB;
922 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
923 }
924 }
925 #endif
926
927 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
928 void PNGFAPI
929 png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
930 png_fixed_point red, png_fixed_point green)
931 {
932 png_debug(1, "in png_set_rgb_to_gray");
933
934 if (png_ptr == NULL)
935 return;
936
937 switch(error_action)
938 {
939 case 1:
940 png_ptr->transformations |= PNG_RGB_TO_GRAY;
941 break;
942
943 case 2:
944 png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
945 break;
946
947 case 3:
948 png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
949 break;
950
951 default:
952 png_error(png_ptr, "invalid error action to rgb_to_gray");
953 break;
954 }
955 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
956 #ifdef PNG_READ_EXPAND_SUPPORTED
957 png_ptr->transformations |= PNG_EXPAND;
958 #else
959 {
960 png_warning(png_ptr,
961 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
962
963 png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
964 }
965 #endif
966 {
967 if (red >= 0 && green >= 0 && red + green <= PNG_FP_1)
968 {
969 png_uint_16 red_int, green_int;
970
971 /* NOTE: this calculation does not round, but this behavior is retained
972 * for consistency, the inaccuracy is very small. The code here always
973 * overwrites the coefficients, regardless of whether they have been
974 * defaulted or set already.
975 */
976 red_int = (png_uint_16)(((png_uint_32)red*32768)/100000);
977 green_int = (png_uint_16)(((png_uint_32)green*32768)/100000);
978
979 png_ptr->rgb_to_gray_red_coeff = red_int;
980 png_ptr->rgb_to_gray_green_coeff = green_int;
981 png_ptr->rgb_to_gray_coefficients_set = 1;
982 }
983
984 else
985 {
986 if (red >= 0 && green >= 0)
987 png_warning(png_ptr,
988 "ignoring out of range rgb_to_gray coefficients");
989
990 /* Use the defaults, from the cHRM chunk if set, else the historical
991 * values which are close to the sRGB/HDTV/ITU-Rec 709 values. See
992 * png_do_rgb_to_gray for more discussion of the values. In this case
993 * the coefficients are not marked as 'set' and are not overwritten if
994 * something has already provided a default.
995 */
996 if (png_ptr->rgb_to_gray_red_coeff == 0 &&
997 png_ptr->rgb_to_gray_green_coeff == 0)
998 {
999 png_ptr->rgb_to_gray_red_coeff = 6968;
1000 png_ptr->rgb_to_gray_green_coeff = 23434;
1001 /* png_ptr->rgb_to_gray_blue_coeff = 2366; */
1002 }
1003 }
1004 }
1005 }
1006
1007 #ifdef PNG_FLOATING_POINT_SUPPORTED
1008 /* Convert a RGB image to a grayscale of the same width. This allows us,
1009 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
1010 */
1011
1012 void PNGAPI
1013 png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
1014 double green)
1015 {
1016 if (png_ptr == NULL)
1017 return;
1018
1019 png_set_rgb_to_gray_fixed(png_ptr, error_action,
1020 png_fixed(png_ptr, red, "rgb to gray red coefficient"),
1021 png_fixed(png_ptr, green, "rgb to gray green coefficient"));
1022 }
1023 #endif /* FLOATING POINT */
1024
1025 #endif
1026
1027 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
1028 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1029 void PNGAPI
1030 png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1031 read_user_transform_fn)
1032 {
1033 png_debug(1, "in png_set_read_user_transform_fn");
1034
1035 if (png_ptr == NULL)
1036 return;
1037
1038 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1039 png_ptr->transformations |= PNG_USER_TRANSFORM;
1040 png_ptr->read_user_transform_fn = read_user_transform_fn;
1041 #endif
1042 }
1043 #endif
1044
1045 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
1046 #ifdef PNG_READ_GAMMA_SUPPORTED
1047 /* In the case of gamma transformations only do transformations on images where
1048 * the [file] gamma and screen_gamma are not close reciprocals, otherwise it
1049 * slows things down slightly, and also needlessly introduces small errors.
1050 */
1051 static int /* PRIVATE */
1052 png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma)
1053 {
1054 /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
1055 * correction as a difference of the overall transform from 1.0
1056 *
1057 * We want to compare the threshold with s*f - 1, if we get
1058 * overflow here it is because of wacky gamma values so we
1059 * turn on processing anyway.
1060 */
1061 png_fixed_point gtest;
1062 return !png_muldiv(&gtest, screen_gamma, file_gamma, PNG_FP_1) ||
1063 png_gamma_significant(gtest);
1064 }
1065 #endif
1066
1067 /* Initialize everything needed for the read. This includes modifying
1068 * the palette.
1069 */
1070
1071 /*For the moment 'png_init_palette_transformations' and
1072 * 'png_init_rgb_transformations' only do some flag canceling optimizations.
1073 * The intent is that these two routines should have palette or rgb operations
1074 * extracted from 'png_init_read_transformations'.
1075 */
1076 static void /* PRIVATE */
1077 png_init_palette_transformations(png_structp png_ptr)
1078 {
1079 /* Called to handle the (input) palette case. In png_do_read_transformations
1080 * the first step is to expand the palette if requested, so this code must
1081 * take care to only make changes that are invariant with respect to the
1082 * palette expansion, or only do them if there is no expansion.
1083 *
1084 * STRIP_ALPHA has already been handled in the caller (by setting num_trans
1085 * to 0.)
1086 */
1087 int input_has_alpha = 0;
1088 int input_has_transparency = 0;
1089
1090 if (png_ptr->num_trans > 0)
1091 {
1092 int i;
1093
1094 /* Ignore if all the entries are opaque (unlikely!) */
1095 for (i=0; i<png_ptr->num_trans; ++i)
1096 if (png_ptr->trans_alpha[i] == 255)
1097 continue;
1098 else if (png_ptr->trans_alpha[i] == 0)
1099 input_has_transparency = 1;
1100 else
1101 input_has_alpha = 1;
1102 }
1103
1104 /* If no alpha we can optimize. */
1105 if (!input_has_alpha)
1106 {
1107 /* Any alpha means background and associative alpha processing is
1108 * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA
1109 * and ENCODE_ALPHA are irrelevant.
1110 */
1111 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1112 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1113
1114 if (!input_has_transparency)
1115 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
1116 }
1117
1118 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1119 /* png_set_background handling - deals with the complexity of whether the
1120 * background color is in the file format or the screen format in the case
1121 * where an 'expand' will happen.
1122 */
1123
1124 /* The following code cannot be entered in the alpha pre-multiplication case
1125 * because PNG_BACKGROUND_EXPAND is cancelled below.
1126 */
1127 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
1128 (png_ptr->transformations & PNG_EXPAND))
1129 {
1130 {
1131 png_ptr->background.red =
1132 png_ptr->palette[png_ptr->background.index].red;
1133 png_ptr->background.green =
1134 png_ptr->palette[png_ptr->background.index].green;
1135 png_ptr->background.blue =
1136 png_ptr->palette[png_ptr->background.index].blue;
1137
1138 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1139 if (png_ptr->transformations & PNG_INVERT_ALPHA)
1140 {
1141 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
1142 {
1143 /* Invert the alpha channel (in tRNS) unless the pixels are
1144 * going to be expanded, in which case leave it for later
1145 */
1146 int i, istop = png_ptr->num_trans;
1147
1148 for (i=0; i<istop; i++)
1149 png_ptr->trans_alpha[i] = (png_byte)(255 -
1150 png_ptr->trans_alpha[i]);
1151 }
1152 }
1153 #endif /* PNG_READ_INVERT_ALPHA_SUPPORTED */
1154 }
1155 } /* background expand and (therefore) no alpha association. */
1156 #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
1157 }
1158
1159 static void /* PRIVATE */
1160 png_init_rgb_transformations(png_structp png_ptr)
1161 {
1162 /* Added to libpng-1.5.4: check the color type to determine whether there
1163 * is any alpha or transparency in the image and simply cancel the
1164 * background and alpha mode stuff if there isn't.
1165 */
1166 int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0;
1167 int input_has_transparency = png_ptr->num_trans > 0;
1168
1169 /* If no alpha we can optimize. */
1170 if (!input_has_alpha)
1171 {
1172 /* Any alpha means background and associative alpha processing is
1173 * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA
1174 * and ENCODE_ALPHA are irrelevant.
1175 */
1176 # ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1177 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1178 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1179 # endif
1180
1181 if (!input_has_transparency)
1182 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
1183 }
1184
1185 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1186 /* png_set_background handling - deals with the complexity of whether the
1187 * background color is in the file format or the screen format in the case
1188 * where an 'expand' will happen.
1189 */
1190
1191 /* The following code cannot be entered in the alpha pre-multiplication case
1192 * because PNG_BACKGROUND_EXPAND is cancelled below.
1193 */
1194 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
1195 (png_ptr->transformations & PNG_EXPAND) &&
1196 !(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
1197 /* i.e., GRAY or GRAY_ALPHA */
1198 {
1199 {
1200 /* Expand background and tRNS chunks */
1201 switch (png_ptr->bit_depth)
1202 {
1203 case 1:
1204 png_ptr->background.gray *= (png_uint_16)0xff;
1205 png_ptr->background.red = png_ptr->background.green
1206 = png_ptr->background.blue = png_ptr->background.gray;
1207 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
1208 {
1209 png_ptr->trans_color.gray *= (png_uint_16)0xff;
1210 png_ptr->trans_color.red = png_ptr->trans_color.green
1211 = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
1212 }
1213 break;
1214
1215 case 2:
1216 png_ptr->background.gray *= (png_uint_16)0x55;
1217 png_ptr->background.red = png_ptr->background.green
1218 = png_ptr->background.blue = png_ptr->background.gray;
1219 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
1220 {
1221 png_ptr->trans_color.gray *= (png_uint_16)0x55;
1222 png_ptr->trans_color.red = png_ptr->trans_color.green
1223 = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
1224 }
1225 break;
1226
1227 case 4:
1228 png_ptr->background.gray *= (png_uint_16)0x11;
1229 png_ptr->background.red = png_ptr->background.green
1230 = png_ptr->background.blue = png_ptr->background.gray;
1231 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
1232 {
1233 png_ptr->trans_color.gray *= (png_uint_16)0x11;
1234 png_ptr->trans_color.red = png_ptr->trans_color.green
1235 = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
1236 }
1237 break;
1238
1239 default:
1240
1241 case 8:
1242
1243 case 16:
1244 png_ptr->background.red = png_ptr->background.green
1245 = png_ptr->background.blue = png_ptr->background.gray;
1246 break;
1247 }
1248 }
1249 } /* background expand and (therefore) no alpha association. */
1250 #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
1251 }
1252
1253 void /* PRIVATE */
1254 png_init_read_transformations(png_structp png_ptr)
1255 {
1256 png_debug(1, "in png_init_read_transformations");
1257
1258 /* This internal function is called from png_read_start_row in pngrutil.c
1259 * and it is called before the 'rowbytes' calculation is done, so the code
1260 * in here can change or update the transformations flags.
1261 *
1262 * First do updates that do not depend on the details of the PNG image data
1263 * being processed.
1264 */
1265
1266 #ifdef PNG_READ_GAMMA_SUPPORTED
1267 /* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds
1268 * png_set_alpha_mode and this is another source for a default file gamma so
1269 * the test needs to be performed later - here. In addition prior to 1.5.4
1270 * the tests were repeated for the PALETTE color type here - this is no
1271 * longer necessary (and doesn't seem to have been necessary before.)
1272 */
1273 {
1274 /* The following temporary indicates if overall gamma correction is
1275 * required.
1276 */
1277 int gamma_correction = 0;
1278
1279 if (png_ptr->gamma != 0) /* has been set */
1280 {
1281 if (png_ptr->screen_gamma != 0) /* screen set too */
1282 gamma_correction = png_gamma_threshold(png_ptr->gamma,
1283 png_ptr->screen_gamma);
1284
1285 else
1286 /* Assume the output matches the input; a long time default behavior
1287 * of libpng, although the standard has nothing to say about this.
1288 */
1289 png_ptr->screen_gamma = png_reciprocal(png_ptr->gamma);
1290 }
1291
1292 else if (png_ptr->screen_gamma != 0)
1293 /* The converse - assume the file matches the screen, note that this
1294 * perhaps undesireable default can (from 1.5.4) be changed by calling
1295 * png_set_alpha_mode (even if the alpha handling mode isn't required
1296 * or isn't changed from the default.)
1297 */
1298 png_ptr->gamma = png_reciprocal(png_ptr->screen_gamma);
1299
1300 else /* neither are set */
1301 /* Just in case the following prevents any processing - file and screen
1302 * are both assumed to be linear and there is no way to introduce a
1303 * third gamma value other than png_set_background with 'UNIQUE', and,
1304 * prior to 1.5.4
1305 */
1306 png_ptr->screen_gamma = png_ptr->gamma = PNG_FP_1;
1307
1308 /* Now turn the gamma transformation on or off as appropriate. Notice
1309 * that PNG_GAMMA just refers to the file->screen correction. Alpha
1310 * composition may independently cause gamma correction because it needs
1311 * linear data (e.g. if the file has a gAMA chunk but the screen gamma
1312 * hasn't been specified.) In any case this flag may get turned off in
1313 * the code immediately below if the transform can be handled outside the
1314 * row loop.
1315 */
1316 if (gamma_correction)
1317 png_ptr->transformations |= PNG_GAMMA;
1318
1319 else
1320 png_ptr->transformations &= ~PNG_GAMMA;
1321 }
1322 #endif
1323
1324 /* Certain transformations have the effect of preventing other
1325 * transformations that happen afterward in png_do_read_transformations,
1326 * resolve the interdependencies here. From the code of
1327 * png_do_read_transformations the order is:
1328 *
1329 * 1) PNG_EXPAND (including PNG_EXPAND_tRNS)
1330 * 2) PNG_STRIP_ALPHA (if no compose)
1331 * 3) PNG_RGB_TO_GRAY
1332 * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY
1333 * 5) PNG_COMPOSE
1334 * 6) PNG_GAMMA
1335 * 7) PNG_STRIP_ALPHA (if compose)
1336 * 8) PNG_ENCODE_ALPHA
1337 * 9) PNG_SCALE_16_TO_8
1338 * 10) PNG_16_TO_8
1339 * 11) PNG_QUANTIZE (converts to palette)
1340 * 12) PNG_EXPAND_16
1341 * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY
1342 * 14) PNG_INVERT_MONO
1343 * 15) PNG_SHIFT
1344 * 16) PNG_PACK
1345 * 17) PNG_BGR
1346 * 18) PNG_PACKSWAP
1347 * 19) PNG_FILLER (includes PNG_ADD_ALPHA)
1348 * 20) PNG_INVERT_ALPHA
1349 * 21) PNG_SWAP_ALPHA
1350 * 22) PNG_SWAP_BYTES
1351 * 23) PNG_USER_TRANSFORM [must be last]
1352 */
1353 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1354 if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
1355 !(png_ptr->transformations & PNG_COMPOSE))
1356 {
1357 /* Stripping the alpha channel happens immediately after the 'expand'
1358 * transformations, before all other transformation, so it cancels out
1359 * the alpha handling. It has the side effect negating the effect of
1360 * PNG_EXPAND_tRNS too:
1361 */
1362 png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA |
1363 PNG_EXPAND_tRNS);
1364 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1365
1366 /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen
1367 * so transparency information would remain just so long as it wasn't
1368 * expanded. This produces unexpected API changes if the set of things
1369 * that do PNG_EXPAND_tRNS changes (perfectly possible given the
1370 * documentation - which says ask for what you want, accept what you
1371 * get.) This makes the behavior consistent from 1.5.4:
1372 */
1373 png_ptr->num_trans = 0;
1374 }
1375 #endif /* STRIP_ALPHA supported, no COMPOSE */
1376
1377 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1378 /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA
1379 * settings will have no effect.
1380 */
1381 if (!png_gamma_significant(png_ptr->screen_gamma))
1382 {
1383 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1384 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1385 }
1386 #endif
1387
1388 #if defined(PNG_READ_EXPAND_SUPPORTED) && \
1389 defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1390 defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
1391 /* Detect gray background and attempt to enable optimization for
1392 * gray --> RGB case.
1393 *
1394 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
1395 * RGB_ALPHA (in which case need_expand is superfluous anyway), the
1396 * background color might actually be gray yet not be flagged as such.
1397 * This is not a problem for the current code, which uses
1398 * PNG_BACKGROUND_IS_GRAY only to decide when to do the
1399 * png_do_gray_to_rgb() transformation.
1400 *
1401 * TODO: this code needs to be revised to avoid the complexity and
1402 * interdependencies. The color type of the background should be recorded in
1403 * png_set_background, along with the bit depth, then the code has a record
1404 * of exactly what color space the background is currently in.
1405 */
1406 if (png_ptr->transformations & PNG_BACKGROUND_EXPAND)
1407 {
1408 /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if
1409 * the file was grayscale the background value is gray.
1410 */
1411 if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
1412 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1413 }
1414
1415 else if (png_ptr->transformations & PNG_COMPOSE)
1416 {
1417 /* PNG_COMPOSE: png_set_background was called with need_expand false,
1418 * so the color is in the color space of the output or png_set_alpha_mode
1419 * was called and the color is black. Ignore RGB_TO_GRAY because that
1420 * happens before GRAY_TO_RGB.
1421 */
1422 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
1423 {
1424 if (png_ptr->background.red == png_ptr->background.green &&
1425 png_ptr->background.red == png_ptr->background.blue)
1426 {
1427 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1428 png_ptr->background.gray = png_ptr->background.red;
1429 }
1430 }
1431 }
1432 #endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED (etc) */
1433
1434 /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations
1435 * can be performed directly on the palette, and some (such as rgb to gray)
1436 * can be optimized inside the palette. This is particularly true of the
1437 * composite (background and alpha) stuff, which can be pretty much all done
1438 * in the palette even if the result is expanded to RGB or gray afterward.
1439 *
1440 * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and
1441 * earlier and the palette stuff is actually handled on the first row. This
1442 * leads to the reported bug that the palette returned by png_get_PLTE is not
1443 * updated.
1444 */
1445 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1446 png_init_palette_transformations(png_ptr);
1447
1448 else
1449 png_init_rgb_transformations(png_ptr);
1450
1451 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1452 defined(PNG_READ_EXPAND_16_SUPPORTED)
1453 if ((png_ptr->transformations & PNG_EXPAND_16) &&
1454 (png_ptr->transformations & PNG_COMPOSE) &&
1455 !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
1456 png_ptr->bit_depth != 16)
1457 {
1458 /* TODO: fix this. Because the expand_16 operation is after the compose
1459 * handling the background color must be 8, not 16, bits deep, but the
1460 * application will supply a 16-bit value so reduce it here.
1461 *
1462 * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at
1463 * present, so that case is ok (until do_expand_16 is moved.)
1464 *
1465 * NOTE: this discards the low 16 bits of the user supplied background
1466 * color, but until expand_16 works properly there is no choice!
1467 */
1468 # define CHOP(x) (x)=((png_uint_16)(((png_uint_32)(x)*255+32895) >> 16))
1469 CHOP(png_ptr->background.red);
1470 CHOP(png_ptr->background.green);
1471 CHOP(png_ptr->background.blue);
1472 CHOP(png_ptr->background.gray);
1473 # undef CHOP
1474 }
1475 #endif /* PNG_READ_BACKGROUND_SUPPORTED && PNG_READ_EXPAND_16_SUPPORTED */
1476
1477 /* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the
1478 * background support (see the comments in scripts/pnglibconf.dfa), this
1479 * allows pre-multiplication of the alpha channel to be implemented as
1480 * compositing on black. This is probably sub-optimal and has been done in
1481 * 1.5.4 betas simply to enable external critique and testing (i.e. to
1482 * implement the new API quickly, without lots of internal changes.)
1483 */
1484
1485 #ifdef PNG_READ_GAMMA_SUPPORTED
1486 # ifdef PNG_READ_BACKGROUND_SUPPORTED
1487 /* Includes ALPHA_MODE */
1488 png_ptr->background_1 = png_ptr->background;
1489 # endif
1490
1491 /* This needs to change - in the palette image case a whole set of tables are
1492 * built when it would be quicker to just calculate the correct value for
1493 * each palette entry directly. Also, the test is too tricky - why check
1494 * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that
1495 * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the
1496 * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction
1497 * the gamma tables will not be built even if composition is required on a
1498 * gamma encoded value.
1499 *
1500 * In 1.5.4 this is addressed below by an additional check on the individual
1501 * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the
1502 * tables.
1503 */
1504 if ((png_ptr->transformations & PNG_GAMMA)
1505 || ((png_ptr->transformations & PNG_RGB_TO_GRAY)
1506 && (png_gamma_significant(png_ptr->gamma) ||
1507 png_gamma_significant(png_ptr->screen_gamma)))
1508 || ((png_ptr->transformations & PNG_COMPOSE)
1509 && (png_gamma_significant(png_ptr->gamma)
1510 || png_gamma_significant(png_ptr->screen_gamma)
1511 # ifdef PNG_READ_BACKGROUND_SUPPORTED
1512 || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE
1513 && png_gamma_significant(png_ptr->background_gamma))
1514 # endif
1515 )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA)
1516 && png_gamma_significant(png_ptr->screen_gamma))
1517 )
1518 {
1519 png_build_gamma_table(png_ptr, png_ptr->bit_depth);
1520
1521 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1522 if (png_ptr->transformations & PNG_COMPOSE)
1523 {
1524 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1525 {
1526 /* We don't get to here unless there is a tRNS chunk with non-opaque
1527 * entries - see the checking code at the start of this function.
1528 */
1529 png_color back, back_1;
1530 png_colorp palette = png_ptr->palette;
1531 int num_palette = png_ptr->num_palette;
1532 int i;
1533 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
1534 {
1535
1536 back.red = png_ptr->gamma_table[png_ptr->background.red];
1537 back.green = png_ptr->gamma_table[png_ptr->background.green];
1538 back.blue = png_ptr->gamma_table[png_ptr->background.blue];
1539
1540 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
1541 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
1542 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
1543 }
1544 else
1545 {
1546 png_fixed_point g, gs;
1547
1548 switch (png_ptr->background_gamma_type)
1549 {
1550 case PNG_BACKGROUND_GAMMA_SCREEN:
1551 g = (png_ptr->screen_gamma);
1552 gs = PNG_FP_1;
1553 break;
1554
1555 case PNG_BACKGROUND_GAMMA_FILE:
1556 g = png_reciprocal(png_ptr->gamma);
1557 gs = png_reciprocal2(png_ptr->gamma,
1558 png_ptr->screen_gamma);
1559 break;
1560
1561 case PNG_BACKGROUND_GAMMA_UNIQUE:
1562 g = png_reciprocal(png_ptr->background_gamma);
1563 gs = png_reciprocal2(png_ptr->background_gamma,
1564 png_ptr->screen_gamma);
1565 break;
1566 default:
1567 g = PNG_FP_1; /* back_1 */
1568 gs = PNG_FP_1; /* back */
1569 break;
1570 }
1571
1572 if (png_gamma_significant(gs))
1573 {
1574 back.red = png_gamma_8bit_correct(png_ptr->background.red,
1575 gs);
1576 back.green = png_gamma_8bit_correct(png_ptr->background.green,
1577 gs);
1578 back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1579 gs);
1580 }
1581
1582 else
1583 {
1584 back.red = (png_byte)png_ptr->background.red;
1585 back.green = (png_byte)png_ptr->background.green;
1586 back.blue = (png_byte)png_ptr->background.blue;
1587 }
1588
1589 if (png_gamma_significant(g))
1590 {
1591 back_1.red = png_gamma_8bit_correct(png_ptr->background.red,
1592 g);
1593 back_1.green = png_gamma_8bit_correct(
1594 png_ptr->background.green, g);
1595 back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1596 g);
1597 }
1598
1599 else
1600 {
1601 back_1.red = (png_byte)png_ptr->background.red;
1602 back_1.green = (png_byte)png_ptr->background.green;
1603 back_1.blue = (png_byte)png_ptr->background.blue;
1604 }
1605 }
1606
1607 for (i = 0; i < num_palette; i++)
1608 {
1609 if (i < (int)png_ptr->num_trans &&
1610 png_ptr->trans_alpha[i] != 0xff)
1611 {
1612 if (png_ptr->trans_alpha[i] == 0)
1613 {
1614 palette[i] = back;
1615 }
1616 else /* if (png_ptr->trans_alpha[i] != 0xff) */
1617 {
1618 png_byte v, w;
1619
1620 v = png_ptr->gamma_to_1[palette[i].red];
1621 png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);
1622 palette[i].red = png_ptr->gamma_from_1[w];
1623
1624 v = png_ptr->gamma_to_1[palette[i].green];
1625 png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);
1626 palette[i].green = png_ptr->gamma_from_1[w];
1627
1628 v = png_ptr->gamma_to_1[palette[i].blue];
1629 png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);
1630 palette[i].blue = png_ptr->gamma_from_1[w];
1631 }
1632 }
1633 else
1634 {
1635 palette[i].red = png_ptr->gamma_table[palette[i].red];
1636 palette[i].green = png_ptr->gamma_table[palette[i].green];
1637 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1638 }
1639 }
1640
1641 /* Prevent the transformations being done again.
1642 *
1643 * NOTE: this is highly dubious; it removes the transformations in
1644 * place. This seems inconsistent with the general treatment of the
1645 * transformations elsewhere.
1646 */
1647 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA);
1648 } /* color_type == PNG_COLOR_TYPE_PALETTE */
1649
1650 /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1651 else /* color_type != PNG_COLOR_TYPE_PALETTE */
1652 {
1653 png_fixed_point g = PNG_FP_1;
1654 png_fixed_point gs = PNG_FP_1;
1655
1656 switch (png_ptr->background_gamma_type)
1657 {
1658 case PNG_BACKGROUND_GAMMA_SCREEN:
1659 g = png_ptr->screen_gamma;
1660 /* gs = PNG_FP_1; */
1661 break;
1662
1663 case PNG_BACKGROUND_GAMMA_FILE:
1664 g = png_reciprocal(png_ptr->gamma);
1665 gs = png_reciprocal2(png_ptr->gamma, png_ptr->screen_gamma);
1666 break;
1667
1668 case PNG_BACKGROUND_GAMMA_UNIQUE:
1669 g = png_reciprocal(png_ptr->background_gamma);
1670 gs = png_reciprocal2(png_ptr->background_gamma,
1671 png_ptr->screen_gamma);
1672 break;
1673
1674 default:
1675 png_error(png_ptr, "invalid background gamma type");
1676 }
1677
1678 png_ptr->background_1.gray = png_gamma_correct(png_ptr,
1679 png_ptr->background.gray, g);
1680
1681 png_ptr->background.gray = png_gamma_correct(png_ptr,
1682 png_ptr->background.gray, gs);
1683
1684 if ((png_ptr->background.red != png_ptr->background.green) ||
1685 (png_ptr->background.red != png_ptr->background.blue) ||
1686 (png_ptr->background.red != png_ptr->background.gray))
1687 {
1688 /* RGB or RGBA with color background */
1689 png_ptr->background_1.red = png_gamma_correct(png_ptr,
1690 png_ptr->background.red, g);
1691
1692 png_ptr->background_1.green = png_gamma_correct(png_ptr,
1693 png_ptr->background.green, g);
1694
1695 png_ptr->background_1.blue = png_gamma_correct(png_ptr,
1696 png_ptr->background.blue, g);
1697
1698 png_ptr->background.red = png_gamma_correct(png_ptr,
1699 png_ptr->background.red, gs);
1700
1701 png_ptr->background.green = png_gamma_correct(png_ptr,
1702 png_ptr->background.green, gs);
1703
1704 png_ptr->background.blue = png_gamma_correct(png_ptr,
1705 png_ptr->background.blue, gs);
1706 }
1707
1708 else
1709 {
1710 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1711 png_ptr->background_1.red = png_ptr->background_1.green
1712 = png_ptr->background_1.blue = png_ptr->background_1.gray;
1713
1714 png_ptr->background.red = png_ptr->background.green
1715 = png_ptr->background.blue = png_ptr->background.gray;
1716 }
1717 } /* color_type != PNG_COLOR_TYPE_PALETTE */
1718 }/* png_ptr->transformations & PNG_BACKGROUND */
1719
1720 else
1721 /* Transformation does not include PNG_BACKGROUND */
1722 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1723 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1724 {
1725 png_colorp palette = png_ptr->palette;
1726 int num_palette = png_ptr->num_palette;
1727 int i;
1728
1729 /*NOTE: there are other transformations that should probably be in here
1730 * too.
1731 */
1732 for (i = 0; i < num_palette; i++)
1733 {
1734 palette[i].red = png_ptr->gamma_table[palette[i].red];
1735 palette[i].green = png_ptr->gamma_table[palette[i].green];
1736 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1737 }
1738
1739 /* Done the gamma correction. */
1740 png_ptr->transformations &= ~PNG_GAMMA;
1741 } /* color_type == PALETTE && !PNG_BACKGROUND transformation */
1742 }
1743 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1744 else
1745 #endif
1746 #endif /* PNG_READ_GAMMA_SUPPORTED */
1747
1748 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1749 /* No GAMMA transformation (see the hanging else 4 lines above) */
1750 if ((png_ptr->transformations & PNG_COMPOSE) &&
1751 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
1752 {
1753 int i;
1754 int istop = (int)png_ptr->num_trans;
1755 png_color back;
1756 png_colorp palette = png_ptr->palette;
1757
1758 back.red = (png_byte)png_ptr->background.red;
1759 back.green = (png_byte)png_ptr->background.green;
1760 back.blue = (png_byte)png_ptr->background.blue;
1761
1762 for (i = 0; i < istop; i++)
1763 {
1764 if (png_ptr->trans_alpha[i] == 0)
1765 {
1766 palette[i] = back;
1767 }
1768
1769 else if (png_ptr->trans_alpha[i] != 0xff)
1770 {
1771 /* The png_composite() macro is defined in png.h */
1772 png_composite(palette[i].red, palette[i].red,
1773 png_ptr->trans_alpha[i], back.red);
1774
1775 png_composite(palette[i].green, palette[i].green,
1776 png_ptr->trans_alpha[i], back.green);
1777
1778 png_composite(palette[i].blue, palette[i].blue,
1779 png_ptr->trans_alpha[i], back.blue);
1780 }
1781 }
1782
1783 png_ptr->transformations &= ~PNG_COMPOSE;
1784 }
1785 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1786
1787 #ifdef PNG_READ_SHIFT_SUPPORTED
1788 if ((png_ptr->transformations & PNG_SHIFT) &&
1789 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
1790 {
1791 png_uint_16 i;
1792 png_uint_16 istop = png_ptr->num_palette;
1793 int sr = 8 - png_ptr->sig_bit.red;
1794 int sg = 8 - png_ptr->sig_bit.green;
1795 int sb = 8 - png_ptr->sig_bit.blue;
1796
1797 if (sr < 0 || sr > 8)
1798 sr = 0;
1799
1800 if (sg < 0 || sg > 8)
1801 sg = 0;
1802
1803 if (sb < 0 || sb > 8)
1804 sb = 0;
1805
1806 for (i = 0; i < istop; i++)
1807 {
1808 png_ptr->palette[i].red >>= sr;
1809 png_ptr->palette[i].green >>= sg;
1810 png_ptr->palette[i].blue >>= sb;
1811 }
1812 }
1813 #endif /* PNG_READ_SHIFT_SUPPORTED */
1814 }
1815
1816 /* Modify the info structure to reflect the transformations. The
1817 * info should be updated so a PNG file could be written with it,
1818 * assuming the transformations result in valid PNG data.
1819 */
1820 void /* PRIVATE */
1821 png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
1822 {
1823 png_debug(1, "in png_read_transform_info");
1824
1825 #ifdef PNG_READ_EXPAND_SUPPORTED
1826 if (png_ptr->transformations & PNG_EXPAND)
1827 {
1828 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1829 {
1830 /* This check must match what actually happens in
1831 * png_do_expand_palette; if it ever checks the tRNS chunk to see if
1832 * it is all opaque we must do the same (at present it does not.)
1833 */
1834 if (png_ptr->num_trans > 0)
1835 info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1836
1837 else
1838 info_ptr->color_type = PNG_COLOR_TYPE_RGB;
1839
1840 info_ptr->bit_depth = 8;
1841 info_ptr->num_trans = 0;
1842 }
1843 else
1844 {
1845 if (png_ptr->num_trans)
1846 {
1847 if (png_ptr->transformations & PNG_EXPAND_tRNS)
1848 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1849 }
1850 if (info_ptr->bit_depth < 8)
1851 info_ptr->bit_depth = 8;
1852
1853 info_ptr->num_trans = 0;
1854 }
1855 }
1856 #endif
1857
1858 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
1859 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
1860 /* The following is almost certainly wrong unless the background value is in
1861 * the screen space!
1862 */
1863 if (png_ptr->transformations & PNG_COMPOSE)
1864 info_ptr->background = png_ptr->background;
1865 #endif
1866
1867 #ifdef PNG_READ_GAMMA_SUPPORTED
1868 /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4),
1869 * however it seems that the code in png_init_read_transformations, which has
1870 * been called before this from png_read_update_info->png_read_start_row
1871 * sometimes does the gamma transform and cancels the flag.
1872 */
1873 info_ptr->gamma = png_ptr->gamma;
1874 #endif
1875
1876 if (info_ptr->bit_depth == 16)
1877 {
1878 # ifdef PNG_READ_16BIT_SUPPORTED
1879 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
1880 if (png_ptr->transformations & PNG_SCALE_16_TO_8)
1881 info_ptr->bit_depth = 8;
1882 # endif
1883
1884 # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1885 if (png_ptr->transformations & PNG_16_TO_8)
1886 info_ptr->bit_depth = 8;
1887 # endif
1888
1889 # else
1890 /* No 16 bit support: force chopping 16-bit input down to 8, in this case
1891 * the app program can chose if both APIs are available by setting the
1892 * correct scaling to use.
1893 */
1894 # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1895 /* For compatibility with previous versions use the strip method by
1896 * default. This code works because if PNG_SCALE_16_TO_8 is already
1897 * set the code below will do that in preference to the chop.
1898 */
1899 png_ptr->transformations |= PNG_16_TO_8;
1900 info_ptr->bit_depth = 8;
1901 # else
1902
1903 # if PNG_READ_SCALE_16_TO_8_SUPPORTED
1904 png_ptr->transformations |= PNG_SCALE_16_TO_8;
1905 info_ptr->bit_depth = 8;
1906 # else
1907
1908 CONFIGURATION ERROR: you must enable at least one 16 to 8 method
1909 # endif
1910 # endif
1911 #endif /* !READ_16BIT_SUPPORTED */
1912 }
1913
1914 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1915 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
1916 info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
1917 #endif
1918
1919 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1920 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1921 info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
1922 #endif
1923
1924 #ifdef PNG_READ_QUANTIZE_SUPPORTED
1925 if (png_ptr->transformations & PNG_QUANTIZE)
1926 {
1927 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1928 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
1929 png_ptr->palette_lookup && info_ptr->bit_depth == 8)
1930 {
1931 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
1932 }
1933 }
1934 #endif
1935
1936 #ifdef PNG_READ_EXPAND_16_SUPPORTED
1937 if (png_ptr->transformations & PNG_EXPAND_16 && info_ptr->bit_depth == 8 &&
1938 info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
1939 {
1940 info_ptr->bit_depth = 16;
1941 }
1942 #endif
1943
1944 #ifdef PNG_READ_PACK_SUPPORTED
1945 if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
1946 info_ptr->bit_depth = 8;
1947 #endif
1948
1949 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1950 info_ptr->channels = 1;
1951
1952 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
1953 info_ptr->channels = 3;
1954
1955 else
1956 info_ptr->channels = 1;
1957
1958 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1959 if (png_ptr->transformations & PNG_STRIP_ALPHA)
1960 {
1961 info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
1962 info_ptr->num_trans = 0;
1963 }
1964 #endif
1965
1966 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1967 info_ptr->channels++;
1968
1969 #ifdef PNG_READ_FILLER_SUPPORTED
1970 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
1971 if ((png_ptr->transformations & PNG_FILLER) &&
1972 ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1973 (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
1974 {
1975 info_ptr->channels++;
1976 /* If adding a true alpha channel not just filler */
1977 if (png_ptr->transformations & PNG_ADD_ALPHA)
1978 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1979 }
1980 #endif
1981
1982 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
1983 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1984 if (png_ptr->transformations & PNG_USER_TRANSFORM)
1985 {
1986 if (info_ptr->bit_depth < png_ptr->user_transform_depth)
1987 info_ptr->bit_depth = png_ptr->user_transform_depth;
1988
1989 if (info_ptr->channels < png_ptr->user_transform_channels)
1990 info_ptr->channels = png_ptr->user_transform_channels;
1991 }
1992 #endif
1993
1994 info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
1995 info_ptr->bit_depth);
1996
1997 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
1998
1999 /* Adding in 1.5.4: cache the above value in png_struct so that we can later
2000 * check in png_rowbytes that the user buffer won't get overwritten. Note
2001 * that the field is not always set - if png_read_update_info isn't called
2002 * the application has to either not do any transforms or get the calculation
2003 * right itself.
2004 */
2005 png_ptr->info_rowbytes = info_ptr->rowbytes;
2006
2007 #ifndef PNG_READ_EXPAND_SUPPORTED
2008 if (png_ptr)
2009 return;
2010 #endif
2011 }
2012
2013 /* Transform the row. The order of transformations is significant,
2014 * and is very touchy. If you add a transformation, take care to
2015 * decide how it fits in with the other transformations here.
2016 */
2017 void /* PRIVATE */
2018 png_do_read_transformations(png_structp png_ptr)
2019 {
2020 png_debug(1, "in png_do_read_transformations");
2021
2022 if (png_ptr->row_buf == NULL)
2023 {
2024 /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this
2025 * error is incredibly rare and incredibly easy to debug without this
2026 * information.
2027 */
2028 png_error(png_ptr, "NULL row buffer");
2029 }
2030
2031 /* The following is debugging; prior to 1.5.4 the code was never compiled in;
2032 * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
2033 * PNG_WARN_UNINITIALIZED_ROW removed. In 1.5 the new flag is set only for
2034 * selected new APIs to ensure that there is no API change.
2035 */
2036 if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
2037 !(png_ptr->flags & PNG_FLAG_ROW_INIT))
2038 {
2039 /* Application has failed to call either png_read_start_image() or
2040 * png_read_update_info() after setting transforms that expand pixels.
2041 * This check added to libpng-1.2.19 (but not enabled until 1.5.4).
2042 */
2043 png_error(png_ptr, "Uninitialized row");
2044 }
2045
2046 #ifdef PNG_READ_EXPAND_SUPPORTED
2047 if (png_ptr->transformations & PNG_EXPAND)
2048 {
2049 if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
2050 {
2051 png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
2052 png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
2053 }
2054
2055 else
2056 {
2057 if (png_ptr->num_trans &&
2058 (png_ptr->transformations & PNG_EXPAND_tRNS))
2059 png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
2060 &(png_ptr->trans_color));
2061
2062 else
2063 png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
2064 NULL);
2065 }
2066 }
2067 #endif
2068
2069 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2070 if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
2071 !(png_ptr->transformations & PNG_COMPOSE) &&
2072 (png_ptr->row_info.color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2073 png_ptr->row_info.color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
2074 png_do_strip_channel(&(png_ptr->row_info), png_ptr->row_buf + 1,
2075 0 /* at_start == false, because SWAP_ALPHA happens later */);
2076 #endif
2077
2078 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2079 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
2080 {
2081 int rgb_error =
2082 png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info),
2083 png_ptr->row_buf + 1);
2084
2085 if (rgb_error)
2086 {
2087 png_ptr->rgb_to_gray_status=1;
2088 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
2089 PNG_RGB_TO_GRAY_WARN)
2090 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
2091
2092 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
2093 PNG_RGB_TO_GRAY_ERR)
2094 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
2095 }
2096 }
2097 #endif
2098
2099 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
2100 *
2101 * In most cases, the "simple transparency" should be done prior to doing
2102 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
2103 * pixel is transparent. You would also need to make sure that the
2104 * transparency information is upgraded to RGB.
2105 *
2106 * To summarize, the current flow is:
2107 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
2108 * with background "in place" if transparent,
2109 * convert to RGB if necessary
2110 * - Gray + alpha -> composite with gray background and remove alpha bytes,
2111 * convert to RGB if necessary
2112 *
2113 * To support RGB backgrounds for gray images we need:
2114 * - Gray + simple transparency -> convert to RGB + simple transparency,
2115 * compare 3 or 6 bytes and composite with
2116 * background "in place" if transparent
2117 * (3x compare/pixel compared to doing
2118 * composite with gray bkgrnd)
2119 * - Gray + alpha -> convert to RGB + alpha, composite with background and
2120 * remove alpha bytes (3x float
2121 * operations/pixel compared with composite
2122 * on gray background)
2123 *
2124 * Greg's change will do this. The reason it wasn't done before is for
2125 * performance, as this increases the per-pixel operations. If we would check
2126 * in advance if the background was gray or RGB, and position the gray-to-RGB
2127 * transform appropriately, then it would save a lot of work/time.
2128 */
2129
2130 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2131 /* If gray -> RGB, do so now only if background is non-gray; else do later
2132 * for performance reasons
2133 */
2134 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
2135 !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
2136 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
2137 #endif
2138
2139 #if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\
2140 (defined PNG_READ_ALPHA_MODE_SUPPORTED)
2141 if (png_ptr->transformations & PNG_COMPOSE)
2142 png_do_compose(&(png_ptr->row_info), png_ptr->row_buf + 1, png_ptr);
2143 #endif
2144
2145 #ifdef PNG_READ_GAMMA_SUPPORTED
2146 if ((png_ptr->transformations & PNG_GAMMA) &&
2147 #if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\
2148 (defined PNG_READ_ALPHA_MODE_SUPPORTED)
2149 !((png_ptr->transformations & PNG_COMPOSE) &&
2150 ((png_ptr->num_trans != 0) ||
2151 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
2152 #endif
2153 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
2154 png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1, png_ptr);
2155 #endif
2156
2157 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2158 if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
2159 (png_ptr->transformations & PNG_COMPOSE) &&
2160 (png_ptr->row_info.color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2161 png_ptr->row_info.color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
2162 png_do_strip_channel(&(png_ptr->row_info), png_ptr->row_buf + 1,
2163 0 /* at_start == false, because SWAP_ALPHA happens later */);
2164 #endif
2165
2166 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
2167 if ((png_ptr->transformations & PNG_ENCODE_ALPHA) &&
2168 (png_ptr->row_info.color_type & PNG_COLOR_MASK_ALPHA))
2169 png_do_encode_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1, png_ptr);
2170 #endif
2171
2172 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2173 if (png_ptr->transformations & PNG_SCALE_16_TO_8)
2174 png_do_scale_16_to_8(&(png_ptr->row_info), png_ptr->row_buf + 1);
2175 #endif
2176
2177 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2178 /* There is no harm in doing both of these because only one has any effect,
2179 * by putting the 'scale' option first if the app asks for scale (either by
2180 * calling the API or in a TRANSFORM flag) this is what happens.
2181 */
2182 if (png_ptr->transformations & PNG_16_TO_8)
2183 png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
2184 #endif
2185
2186 #ifdef PNG_READ_QUANTIZE_SUPPORTED
2187 if (png_ptr->transformations & PNG_QUANTIZE)
2188 {
2189 png_do_quantize(&(png_ptr->row_info), png_ptr->row_buf + 1,
2190 png_ptr->palette_lookup, png_ptr->quantize_index);
2191
2192 if (png_ptr->row_info.rowbytes == 0)
2193 png_error(png_ptr, "png_do_quantize returned rowbytes=0");
2194 }
2195 #endif /* PNG_READ_QUANTIZE_SUPPORTED */
2196
2197 #ifdef PNG_READ_EXPAND_16_SUPPORTED
2198 /* Do the expansion now, after all the arithmetic has been done. Notice
2199 * that previous transformations can handle the PNG_EXPAND_16 flag if this
2200 * is efficient (particularly true in the case of gamma correction, where
2201 * better accuracy results faster!)
2202 */
2203 if (png_ptr->transformations & PNG_EXPAND_16)
2204 png_do_expand_16(&png_ptr->row_info, png_ptr->row_buf + 1);
2205 #endif
2206
2207 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2208 /*NOTE: moved here in 1.5.4 (from much later in this list.) */
2209 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
2210 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
2211 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
2212 #endif
2213
2214 #ifdef PNG_READ_INVERT_SUPPORTED
2215 if (png_ptr->transformations & PNG_INVERT_MONO)
2216 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
2217 #endif
2218
2219 #ifdef PNG_READ_SHIFT_SUPPORTED
2220 if (png_ptr->transformations & PNG_SHIFT)
2221 png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
2222 &(png_ptr->shift));
2223 #endif
2224
2225 #ifdef PNG_READ_PACK_SUPPORTED
2226 if (png_ptr->transformations & PNG_PACK)
2227 png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
2228 #endif
2229
2230 #ifdef PNG_READ_BGR_SUPPORTED
2231 if (png_ptr->transformations & PNG_BGR)
2232 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
2233 #endif
2234
2235 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2236 if (png_ptr->transformations & PNG_PACKSWAP)
2237 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
2238 #endif
2239
2240 #ifdef PNG_READ_FILLER_SUPPORTED
2241 if (png_ptr->transformations & PNG_FILLER)
2242 png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
2243 (png_uint_32)png_ptr->filler, png_ptr->flags);
2244 #endif
2245
2246 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
2247 if (png_ptr->transformations & PNG_INVERT_ALPHA)
2248 png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
2249 #endif
2250
2251 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
2252 if (png_ptr->transformations & PNG_SWAP_ALPHA)
2253 png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
2254 #endif
2255
2256 #ifdef PNG_READ_16BIT_SUPPORTED
2257 #ifdef PNG_READ_SWAP_SUPPORTED
2258 if (png_ptr->transformations & PNG_SWAP_BYTES)
2259 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
2260 #endif
2261 #endif
2262
2263 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
2264 if (png_ptr->transformations & PNG_USER_TRANSFORM)
2265 {
2266 if (png_ptr->read_user_transform_fn != NULL)
2267 (*(png_ptr->read_user_transform_fn)) /* User read transform function */
2268 (png_ptr, /* png_ptr */
2269 &(png_ptr->row_info), /* row_info: */
2270 /* png_uint_32 width; width of row */
2271 /* png_size_t rowbytes; number of bytes in row */
2272 /* png_byte color_type; color type of pixels */
2273 /* png_byte bit_depth; bit depth of samples */
2274 /* png_byte channels; number of channels (1-4) */
2275 /* png_byte pixel_depth; bits per pixel (depth*channels) */
2276 png_ptr->row_buf + 1); /* start of pixel data for row */
2277 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
2278 if (png_ptr->user_transform_depth)
2279 png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
2280
2281 if (png_ptr->user_transform_channels)
2282 png_ptr->row_info.channels = png_ptr->user_transform_channels;
2283 #endif
2284 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
2285 png_ptr->row_info.channels);
2286
2287 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
2288 png_ptr->row_info.width);
2289 }
2290 #endif
2291 }
2292
2293 #ifdef PNG_READ_PACK_SUPPORTED
2294 /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
2295 * without changing the actual values. Thus, if you had a row with
2296 * a bit depth of 1, you would end up with bytes that only contained
2297 * the numbers 0 or 1. If you would rather they contain 0 and 255, use
2298 * png_do_shift() after this.
2299 */
2300 void /* PRIVATE */
2301 png_do_unpack(png_row_infop row_info, png_bytep row)
2302 {
2303 png_debug(1, "in png_do_unpack");
2304
2305 if (row_info->bit_depth < 8)
2306 {
2307 png_uint_32 i;
2308 png_uint_32 row_width=row_info->width;
2309
2310 switch (row_info->bit_depth)
2311 {
2312 case 1:
2313 {
2314 png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
2315 png_bytep dp = row + (png_size_t)row_width - 1;
2316 png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
2317 for (i = 0; i < row_width; i++)
2318 {
2319 *dp = (png_byte)((*sp >> shift) & 0x01);
2320
2321 if (shift == 7)
2322 {
2323 shift = 0;
2324 sp--;
2325 }
2326
2327 else
2328 shift++;
2329
2330 dp--;
2331 }
2332 break;
2333 }
2334
2335 case 2:
2336 {
2337
2338 png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
2339 png_bytep dp = row + (png_size_t)row_width - 1;
2340 png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
2341 for (i = 0; i < row_width; i++)
2342 {
2343 *dp = (png_byte)((*sp >> shift) & 0x03);
2344
2345 if (shift == 6)
2346 {
2347 shift = 0;
2348 sp--;
2349 }
2350
2351 else
2352 shift += 2;
2353
2354 dp--;
2355 }
2356 break;
2357 }
2358
2359 case 4:
2360 {
2361 png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
2362 png_bytep dp = row + (png_size_t)row_width - 1;
2363 png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
2364 for (i = 0; i < row_width; i++)
2365 {
2366 *dp = (png_byte)((*sp >> shift) & 0x0f);
2367
2368 if (shift == 4)
2369 {
2370 shift = 0;
2371 sp--;
2372 }
2373
2374 else
2375 shift = 4;
2376
2377 dp--;
2378 }
2379 break;
2380 }
2381
2382 default:
2383 break;
2384 }
2385 row_info->bit_depth = 8;
2386 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2387 row_info->rowbytes = row_width * row_info->channels;
2388 }
2389 }
2390 #endif
2391
2392 #ifdef PNG_READ_SHIFT_SUPPORTED
2393 /* Reverse the effects of png_do_shift. This routine merely shifts the
2394 * pixels back to their significant bits values. Thus, if you have
2395 * a row of bit depth 8, but only 5 are significant, this will shift
2396 * the values back to 0 through 31.
2397 */
2398 void /* PRIVATE */
2399 png_do_unshift(png_row_infop row_info, png_bytep row,
2400 png_const_color_8p sig_bits)
2401 {
2402 png_debug(1, "in png_do_unshift");
2403
2404 if (
2405 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
2406 {
2407 int shift[4];
2408 int channels = 0;
2409 int c;
2410 png_uint_16 value = 0;
2411 png_uint_32 row_width = row_info->width;
2412
2413 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
2414 {
2415 shift[channels++] = row_info->bit_depth - sig_bits->red;
2416 shift[channels++] = row_info->bit_depth - sig_bits->green;
2417 shift[channels++] = row_info->bit_depth - sig_bits->blue;
2418 }
2419
2420 else
2421 {
2422 shift[channels++] = row_info->bit_depth - sig_bits->gray;
2423 }
2424
2425 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
2426 {
2427 shift[channels++] = row_info->bit_depth - sig_bits->alpha;
2428 }
2429
2430 for (c = 0; c < channels; c++)
2431 {
2432 if (shift[c] <= 0)
2433 shift[c] = 0;
2434
2435 else
2436 value = 1;
2437 }
2438
2439 if (!value)
2440 return;
2441
2442 switch (row_info->bit_depth)
2443 {
2444 default:
2445 break;
2446
2447 case 2:
2448 {
2449 png_bytep bp;
2450 png_size_t i;
2451 png_size_t istop = row_info->rowbytes;
2452
2453 for (bp = row, i = 0; i < istop; i++)
2454 {
2455 *bp >>= 1;
2456 *bp++ &= 0x55;
2457 }
2458 break;
2459 }
2460
2461 case 4:
2462 {
2463 png_bytep bp = row;
2464 png_size_t i;
2465 png_size_t istop = row_info->rowbytes;
2466 png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
2467 (png_byte)((int)0xf >> shift[0]));
2468
2469 for (i = 0; i < istop; i++)
2470 {
2471 *bp >>= shift[0];
2472 *bp++ &= mask;
2473 }
2474 break;
2475 }
2476
2477 case 8:
2478 {
2479 png_bytep bp = row;
2480 png_uint_32 i;
2481 png_uint_32 istop = row_width * channels;
2482
2483 for (i = 0; i < istop; i++)
2484 {
2485 *bp++ >>= shift[i%channels];
2486 }
2487 break;
2488 }
2489
2490 #ifdef PNG_READ_16BIT_SUPPORTED
2491 case 16:
2492 {
2493 png_bytep bp = row;
2494 png_uint_32 i;
2495 png_uint_32 istop = channels * row_width;
2496
2497 for (i = 0; i < istop; i++)
2498 {
2499 value = (png_uint_16)((*bp << 8) + *(bp + 1));
2500 value >>= shift[i%channels];
2501 *bp++ = (png_byte)(value >> 8);
2502 *bp++ = (png_byte)(value & 0xff);
2503 }
2504 break;
2505 }
2506 #endif
2507 }
2508 }
2509 }
2510 #endif
2511
2512 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2513 /* Scale rows of bit depth 16 down to 8 accurately */
2514 void /* PRIVATE */
2515 png_do_scale_16_to_8(png_row_infop row_info, png_bytep row)
2516 {
2517 png_debug(1, "in png_do_scale_16_to_8");
2518
2519 if (row_info->bit_depth == 16)
2520 {
2521 png_bytep sp = row; /* source */
2522 png_bytep dp = row; /* destinaton */
2523 png_bytep ep = sp + row_info->rowbytes; /* end+1 */
2524
2525 while (sp < ep)
2526 {
2527 /* The input is an array of 16 bit components, these must be scaled to
2528 * 8 bits each. For a 16 bit value V the required value (from the PNG
2529 * specification) is:
2530 *
2531 * (V * 255) / 65535
2532 *
2533 * This reduces to round(V / 257), or floor((V + 128.5)/257)
2534 *
2535 * Represent V as the two byte value vhi.vlo. Make a guess that the
2536 * result is the top byte of V, vhi, then the correction to this value
2537 * is:
2538 *
2539 * error = floor(((V-vhi.vhi) + 128.5) / 257)
2540 * = floor(((vlo-vhi) + 128.5) / 257)
2541 *
2542 * This can be approximated using integer arithmetic (and a signed
2543 * shift):
2544 *
2545 * error = (vlo-vhi+128) >> 8;
2546 *
2547 * The approximate differs from the exact answer only when (vlo-vhi) is
2548 * 128; it then gives a correction of +1 when the exact correction is
2549 * 0. This gives 128 errors. The exact answer (correct for all 16 bit
2550 * input values) is:
2551 *
2552 * error = (vlo-vhi+128)*65535 >> 24;
2553 *
2554 * An alternative arithmetic calculation which also gives no errors is:
2555 *
2556 * (V * 255 + 32895) >> 16
2557 */
2558
2559 png_int_32 tmp = *sp++; /* must be signed! */
2560 tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24;
2561 *dp++ = (png_byte)tmp;
2562 }
2563
2564 row_info->bit_depth = 8;
2565 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2566 row_info->rowbytes = row_info->width * row_info->channels;
2567 }
2568 }
2569 #endif
2570
2571 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2572 void /* PRIVATE */
2573 /* Simply discard the low byte. This was the default behavior prior
2574 * to libpng-1.5.4.
2575 */
2576 png_do_chop(png_row_infop row_info, png_bytep row)
2577 {
2578 png_debug(1, "in png_do_chop");
2579
2580 if (row_info->bit_depth == 16)
2581 {
2582 png_bytep sp = row; /* source */
2583 png_bytep dp = row; /* destinaton */
2584 png_bytep ep = sp + row_info->rowbytes; /* end+1 */
2585
2586 while (sp < ep)
2587 {
2588 *dp++ = *sp;
2589 sp += 2; /* skip low byte */
2590 }
2591
2592 row_info->bit_depth = 8;
2593 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2594 row_info->rowbytes = row_info->width * row_info->channels;
2595 }
2596 }
2597 #endif
2598
2599 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
2600 void /* PRIVATE */
2601 png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
2602 {
2603 png_debug(1, "in png_do_read_swap_alpha");
2604
2605 {
2606 png_uint_32 row_width = row_info->width;
2607 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2608 {
2609 /* This converts from RGBA to ARGB */
2610 if (row_info->bit_depth == 8)
2611 {
2612 png_bytep sp = row + row_info->rowbytes;
2613 png_bytep dp = sp;
2614 png_byte save;
2615 png_uint_32 i;
2616
2617 for (i = 0; i < row_width; i++)
2618 {
2619 save = *(--sp);
2620 *(--dp) = *(--sp);
2621 *(--dp) = *(--sp);
2622 *(--dp) = *(--sp);
2623 *(--dp) = save;
2624 }
2625 }
2626
2627 #ifdef PNG_READ_16BIT_SUPPORTED
2628 /* This converts from RRGGBBAA to AARRGGBB */
2629 else
2630 {
2631 png_bytep sp = row + row_info->rowbytes;
2632 png_bytep dp = sp;
2633 png_byte save[2];
2634 png_uint_32 i;
2635
2636 for (i = 0; i < row_width; i++)
2637 {
2638 save[0] = *(--sp);
2639 save[1] = *(--sp);
2640 *(--dp) = *(--sp);
2641 *(--dp) = *(--sp);
2642 *(--dp) = *(--sp);
2643 *(--dp) = *(--sp);
2644 *(--dp) = *(--sp);
2645 *(--dp) = *(--sp);
2646 *(--dp) = save[0];
2647 *(--dp) = save[1];
2648 }
2649 }
2650 #endif
2651 }
2652
2653 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2654 {
2655 /* This converts from GA to AG */
2656 if (row_info->bit_depth == 8)
2657 {
2658 png_bytep sp = row + row_info->rowbytes;
2659 png_bytep dp = sp;
2660 png_byte save;
2661 png_uint_32 i;
2662
2663 for (i = 0; i < row_width; i++)
2664 {
2665 save = *(--sp);
2666 *(--dp) = *(--sp);
2667 *(--dp) = save;
2668 }
2669 }
2670
2671 #ifdef PNG_READ_16BIT_SUPPORTED
2672 /* This converts from GGAA to AAGG */
2673 else
2674 {
2675 png_bytep sp = row + row_info->rowbytes;
2676 png_bytep dp = sp;
2677 png_byte save[2];
2678 png_uint_32 i;
2679
2680 for (i = 0; i < row_width; i++)
2681 {
2682 save[0] = *(--sp);
2683 save[1] = *(--sp);
2684 *(--dp) = *(--sp);
2685 *(--dp) = *(--sp);
2686 *(--dp) = save[0];
2687 *(--dp) = save[1];
2688 }
2689 }
2690 #endif
2691 }
2692 }
2693 }
2694 #endif
2695
2696 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
2697 void /* PRIVATE */
2698 png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
2699 {
2700 png_uint_32 row_width;
2701 png_debug(1, "in png_do_read_invert_alpha");
2702
2703 row_width = row_info->width;
2704 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2705 {
2706 if (row_info->bit_depth == 8)
2707 {
2708 /* This inverts the alpha channel in RGBA */
2709 png_bytep sp = row + row_info->rowbytes;
2710 png_bytep dp = sp;
2711 png_uint_32 i;
2712
2713 for (i = 0; i < row_width; i++)
2714 {
2715 *(--dp) = (png_byte)(255 - *(--sp));
2716
2717 /* This does nothing:
2718 *(--dp) = *(--sp);
2719 *(--dp) = *(--sp);
2720 *(--dp) = *(--sp);
2721 We can replace it with:
2722 */
2723 sp-=3;
2724 dp=sp;
2725 }
2726 }
2727
2728 #ifdef PNG_READ_16BIT_SUPPORTED
2729 /* This inverts the alpha channel in RRGGBBAA */
2730 else
2731 {
2732 png_bytep sp = row + row_info->rowbytes;
2733 png_bytep dp = sp;
2734 png_uint_32 i;
2735
2736 for (i = 0; i < row_width; i++)
2737 {
2738 *(--dp) = (png_byte)(255 - *(--sp));
2739 *(--dp) = (png_byte)(255 - *(--sp));
2740
2741 /* This does nothing:
2742 *(--dp) = *(--sp);
2743 *(--dp) = *(--sp);
2744 *(--dp) = *(--sp);
2745 *(--dp) = *(--sp);
2746 *(--dp) = *(--sp);
2747 *(--dp) = *(--sp);
2748 We can replace it with:
2749 */
2750 sp-=6;
2751 dp=sp;
2752 }
2753 }
2754 #endif
2755 }
2756 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2757 {
2758 if (row_info->bit_depth == 8)
2759 {
2760 /* This inverts the alpha channel in GA */
2761 png_bytep sp = row + row_info->rowbytes;
2762 png_bytep dp = sp;
2763 png_uint_32 i;
2764
2765 for (i = 0; i < row_width; i++)
2766 {
2767 *(--dp) = (png_byte)(255 - *(--sp));
2768 *(--dp) = *(--sp);
2769 }
2770 }
2771
2772 #ifdef PNG_READ_16BIT_SUPPORTED
2773 else
2774 {
2775 /* This inverts the alpha channel in GGAA */
2776 png_bytep sp = row + row_info->rowbytes;
2777 png_bytep dp = sp;
2778 png_uint_32 i;
2779
2780 for (i = 0; i < row_width; i++)
2781 {
2782 *(--dp) = (png_byte)(255 - *(--sp));
2783 *(--dp) = (png_byte)(255 - *(--sp));
2784 /*
2785 *(--dp) = *(--sp);
2786 *(--dp) = *(--sp);
2787 */
2788 sp-=2;
2789 dp=sp;
2790 }
2791 }
2792 #endif
2793 }
2794 }
2795 #endif
2796
2797 #ifdef PNG_READ_FILLER_SUPPORTED
2798 /* Add filler channel if we have RGB color */
2799 void /* PRIVATE */
2800 png_do_read_filler(png_row_infop row_info, png_bytep row,
2801 png_uint_32 filler, png_uint_32 flags)
2802 {
2803 png_uint_32 i;
2804 png_uint_32 row_width = row_info->width;
2805
2806 #ifdef PNG_READ_16BIT_SUPPORTED
2807 png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
2808 #endif
2809 png_byte lo_filler = (png_byte)(filler & 0xff);
2810
2811 png_debug(1, "in png_do_read_filler");
2812
2813 if (
2814 row_info->color_type == PNG_COLOR_TYPE_GRAY)
2815 {
2816 if (row_info->bit_depth == 8)
2817 {
2818 if (flags & PNG_FLAG_FILLER_AFTER)
2819 {
2820 /* This changes the data from G to GX */
2821 png_bytep sp = row + (png_size_t)row_width;
2822 png_bytep dp = sp + (png_size_t)row_width;
2823 for (i = 1; i < row_width; i++)
2824 {
2825 *(--dp) = lo_filler;
2826 *(--dp) = *(--sp);
2827 }
2828 *(--dp) = lo_filler;
2829 row_info->channels = 2;
2830 row_info->pixel_depth = 16;
2831 row_info->rowbytes = row_width * 2;
2832 }
2833
2834 else
2835 {
2836 /* This changes the data from G to XG */
2837 png_bytep sp = row + (png_size_t)row_width;
2838 png_bytep dp = sp + (png_size_t)row_width;
2839 for (i = 0; i < row_width; i++)
2840 {
2841 *(--dp) = *(--sp);
2842 *(--dp) = lo_filler;
2843 }
2844 row_info->channels = 2;
2845 row_info->pixel_depth = 16;
2846 row_info->rowbytes = row_width * 2;
2847 }
2848 }
2849
2850 #ifdef PNG_READ_16BIT_SUPPORTED
2851 else if (row_info->bit_depth == 16)
2852 {
2853 if (flags & PNG_FLAG_FILLER_AFTER)
2854 {
2855 /* This changes the data from GG to GGXX */
2856 png_bytep sp = row + (png_size_t)row_width * 2;
2857 png_bytep dp = sp + (png_size_t)row_width * 2;
2858 for (i = 1; i < row_width; i++)
2859 {
2860 *(--dp) = hi_filler;
2861 *(--dp) = lo_filler;
2862 *(--dp) = *(--sp);
2863 *(--dp) = *(--sp);
2864 }
2865 *(--dp) = hi_filler;
2866 *(--dp) = lo_filler;
2867 row_info->channels = 2;
2868 row_info->pixel_depth = 32;
2869 row_info->rowbytes = row_width * 4;
2870 }
2871
2872 else
2873 {
2874 /* This changes the data from GG to XXGG */
2875 png_bytep sp = row + (png_size_t)row_width * 2;
2876 png_bytep dp = sp + (png_size_t)row_width * 2;
2877 for (i = 0; i < row_width; i++)
2878 {
2879 *(--dp) = *(--sp);
2880 *(--dp) = *(--sp);
2881 *(--dp) = hi_filler;
2882 *(--dp) = lo_filler;
2883 }
2884 row_info->channels = 2;
2885 row_info->pixel_depth = 32;
2886 row_info->rowbytes = row_width * 4;
2887 }
2888 }
2889 #endif
2890 } /* COLOR_TYPE == GRAY */
2891 else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2892 {
2893 if (row_info->bit_depth == 8)
2894 {
2895 if (flags & PNG_FLAG_FILLER_AFTER)
2896 {
2897 /* This changes the data from RGB to RGBX */
2898 png_bytep sp = row + (png_size_t)row_width * 3;
2899 png_bytep dp = sp + (png_size_t)row_width;
2900 for (i = 1; i < row_width; i++)
2901 {
2902 *(--dp) = lo_filler;
2903 *(--dp) = *(--sp);
2904 *(--dp) = *(--sp);
2905 *(--dp) = *(--sp);
2906 }
2907 *(--dp) = lo_filler;
2908 row_info->channels = 4;
2909 row_info->pixel_depth = 32;
2910 row_info->rowbytes = row_width * 4;
2911 }
2912
2913 else
2914 {
2915 /* This changes the data from RGB to XRGB */
2916 png_bytep sp = row + (png_size_t)row_width * 3;
2917 png_bytep dp = sp + (png_size_t)row_width;
2918 for (i = 0; i < row_width; i++)
2919 {
2920 *(--dp) = *(--sp);
2921 *(--dp) = *(--sp);
2922 *(--dp) = *(--sp);
2923 *(--dp) = lo_filler;
2924 }
2925 row_info->channels = 4;
2926 row_info->pixel_depth = 32;
2927 row_info->rowbytes = row_width * 4;
2928 }
2929 }
2930
2931 #ifdef PNG_READ_16BIT_SUPPORTED
2932 else if (row_info->bit_depth == 16)
2933 {
2934 if (flags & PNG_FLAG_FILLER_AFTER)
2935 {
2936 /* This changes the data from RRGGBB to RRGGBBXX */
2937 png_bytep sp = row + (png_size_t)row_width * 6;
2938 png_bytep dp = sp + (png_size_t)row_width * 2;
2939 for (i = 1; i < row_width; i++)
2940 {
2941 *(--dp) = hi_filler;
2942 *(--dp) = lo_filler;
2943 *(--dp) = *(--sp);
2944 *(--dp) = *(--sp);
2945 *(--dp) = *(--sp);
2946 *(--dp) = *(--sp);
2947 *(--dp) = *(--sp);
2948 *(--dp) = *(--sp);
2949 }
2950 *(--dp) = hi_filler;
2951 *(--dp) = lo_filler;
2952 row_info->channels = 4;
2953 row_info->pixel_depth = 64;
2954 row_info->rowbytes = row_width * 8;
2955 }
2956
2957 else
2958 {
2959 /* This changes the data from RRGGBB to XXRRGGBB */
2960 png_bytep sp = row + (png_size_t)row_width * 6;
2961 png_bytep dp = sp + (png_size_t)row_width * 2;
2962 for (i = 0; i < row_width; i++)
2963 {
2964 *(--dp) = *(--sp);
2965 *(--dp) = *(--sp);
2966 *(--dp) = *(--sp);
2967 *(--dp) = *(--sp);
2968 *(--dp) = *(--sp);
2969 *(--dp) = *(--sp);
2970 *(--dp) = hi_filler;
2971 *(--dp) = lo_filler;
2972 }
2973
2974 row_info->channels = 4;
2975 row_info->pixel_depth = 64;
2976 row_info->rowbytes = row_width * 8;
2977 }
2978 }
2979 #endif
2980 } /* COLOR_TYPE == RGB */
2981 }
2982 #endif
2983
2984 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2985 /* Expand grayscale files to RGB, with or without alpha */
2986 void /* PRIVATE */
2987 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
2988 {
2989 png_uint_32 i;
2990 png_uint_32 row_width = row_info->width;
2991
2992 png_debug(1, "in png_do_gray_to_rgb");
2993
2994 if (row_info->bit_depth >= 8 &&
2995 !(row_info->color_type & PNG_COLOR_MASK_COLOR))
2996 {
2997 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
2998 {
2999 if (row_info->bit_depth == 8)
3000 {
3001 /* This changes G to RGB */
3002 png_bytep sp = row + (png_size_t)row_width - 1;
3003 png_bytep dp = sp + (png_size_t)row_width * 2;
3004 for (i = 0; i < row_width; i++)
3005 {
3006 *(dp--) = *sp;
3007 *(dp--) = *sp;
3008 *(dp--) = *(sp--);
3009 }
3010 }
3011
3012 else
3013 {
3014 /* This changes GG to RRGGBB */
3015 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
3016 png_bytep dp = sp + (png_size_t)row_width * 4;
3017 for (i = 0; i < row_width; i++)
3018 {
3019 *(dp--) = *sp;
3020 *(dp--) = *(sp - 1);
3021 *(dp--) = *sp;
3022 *(dp--) = *(sp - 1);
3023 *(dp--) = *(sp--);
3024 *(dp--) = *(sp--);
3025 }
3026 }
3027 }
3028
3029 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3030 {
3031 if (row_info->bit_depth == 8)
3032 {
3033 /* This changes GA to RGBA */
3034 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
3035 png_bytep dp = sp + (png_size_t)row_width * 2;
3036 for (i = 0; i < row_width; i++)
3037 {
3038 *(dp--) = *(sp--);
3039 *(dp--) = *sp;
3040 *(dp--) = *sp;
3041 *(dp--) = *(sp--);
3042 }
3043 }
3044
3045 else
3046 {
3047 /* This changes GGAA to RRGGBBAA */
3048 png_bytep sp = row + (png_size_t)row_width * 4 - 1;
3049 png_bytep dp = sp + (png_size_t)row_width * 4;
3050 for (i = 0; i < row_width; i++)
3051 {
3052 *(dp--) = *(sp--);
3053 *(dp--) = *(sp--);
3054 *(dp--) = *sp;
3055 *(dp--) = *(sp - 1);
3056 *(dp--) = *sp;
3057 *(dp--) = *(sp - 1);
3058 *(dp--) = *(sp--);
3059 *(dp--) = *(sp--);
3060 }
3061 }
3062 }
3063 row_info->channels += (png_byte)2;
3064 row_info->color_type |= PNG_COLOR_MASK_COLOR;
3065 row_info->pixel_depth = (png_byte)(row_info->channels *
3066 row_info->bit_depth);
3067 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3068 }
3069 }
3070 #endif
3071
3072 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
3073 /* Reduce RGB files to grayscale, with or without alpha
3074 * using the equation given in Poynton's ColorFAQ of 1998-01-04 at
3075 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008 but
3076 * versions dated 1998 through November 2002 have been archived at
3077 * http://web.archive.org/web/20000816232553/http://www.inforamp.net/
3078 * ~poynton/notes/colour_and_gamma/ColorFAQ.txt )
3079 * Charles Poynton poynton at poynton.com
3080 *
3081 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
3082 *
3083 * which can be expressed with integers as
3084 *
3085 * Y = (6969 * R + 23434 * G + 2365 * B)/32768
3086 *
3087 * Poynton's current link (as of January 2003 through July 2011):
3088 * <http://www.poynton.com/notes/colour_and_gamma/>
3089 * has changed the numbers slightly:
3090 *
3091 * Y = 0.2126*R + 0.7152*G + 0.0722*B
3092 *
3093 * which can be expressed with integers as
3094 *
3095 * Y = (6966 * R + 23436 * G + 2366 * B)/32768
3096 *
3097 * Historically, however, libpng uses numbers derived from the ITU-R Rec 709
3098 * end point chromaticities and the D65 white point. Depending on the
3099 * precision used for the D65 white point this produces a variety of different
3100 * numbers, however if the four decimal place value used in ITU-R Rec 709 is
3101 * used (0.3127,0.3290) the Y calculation would be:
3102 *
3103 * Y = (6968 * R + 23435 * G + 2366 * B)/32768
3104 *
3105 * While this is correct the rounding results in an overflow for white, because
3106 * the sum of the rounded coefficients is 32769, not 32768. Consequently
3107 * libpng uses, instead, the closest non-overflowing approximation:
3108 *
3109 * Y = (6968 * R + 23434 * G + 2366 * B)/32768
3110 *
3111 * Starting with libpng-1.5.5, if the image being converted has a cHRM chunk
3112 * (including an sRGB chunk) then the chromaticities are used to calculate the
3113 * coefficients. See the chunk handling in pngrutil.c for more information.
3114 *
3115 * In all cases the calculation is to be done in a linear colorspace. If no
3116 * gamma information is available to correct the encoding of the original RGB
3117 * values this results in an implicit assumption that the original PNG RGB
3118 * values were linear.
3119 *
3120 * Other integer coefficents can be used via png_set_rgb_to_gray(). Because
3121 * the API takes just red and green coefficients the blue coefficient is
3122 * calculated to make the sum 32768. This will result in different rounding
3123 * to that used above.
3124 */
3125 int /* PRIVATE */
3126 png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
3127
3128 {
3129 int rgb_error = 0;
3130
3131 png_debug(1, "in png_do_rgb_to_gray");
3132
3133 if (!(row_info->color_type & PNG_COLOR_MASK_PALETTE) &&
3134 (row_info->color_type & PNG_COLOR_MASK_COLOR))
3135 {
3136 PNG_CONST png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
3137 PNG_CONST png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
3138 PNG_CONST png_uint_32 bc = 32768 - rc - gc;
3139 PNG_CONST png_uint_32 row_width = row_info->width;
3140 PNG_CONST int have_alpha =
3141 (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0;
3142
3143 if (row_info->bit_depth == 8)
3144 {
3145 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
3146 /* Notice that gamma to/from 1 are not necessarily inverses (if
3147 * there is an overall gamma correction). Prior to 1.5.5 this code
3148 * checked the linearized values for equality; this doesn't match
3149 * the documentation, the original values must be checked.
3150 */
3151 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
3152 {
3153 png_bytep sp = row;
3154 png_bytep dp = row;
3155 png_uint_32 i;
3156
3157 for (i = 0; i < row_width; i++)
3158 {
3159 png_byte red = *(sp++);
3160 png_byte green = *(sp++);
3161 png_byte blue = *(sp++);
3162
3163 if (red != green || red != blue)
3164 {
3165 red = png_ptr->gamma_to_1[red];
3166 green = png_ptr->gamma_to_1[green];
3167 blue = png_ptr->gamma_to_1[blue];
3168
3169 rgb_error |= 1;
3170 *(dp++) = png_ptr->gamma_from_1[
3171 (rc*red + gc*green + bc*blue + 16384)>>15];
3172 }
3173
3174 else
3175 {
3176 /* If there is no overall correction the table will not be
3177 * set.
3178 */
3179 if (png_ptr->gamma_table != NULL)
3180 red = png_ptr->gamma_table[red];
3181
3182 *(dp++) = red;
3183 }
3184
3185 if (have_alpha)
3186 *(dp++) = *(sp++);
3187 }
3188 }
3189 else
3190 #endif
3191 {
3192 png_bytep sp = row;
3193 png_bytep dp = row;
3194 png_uint_32 i;
3195
3196 for (i = 0; i < row_width; i++)
3197 {
3198 png_byte red = *(sp++);
3199 png_byte green = *(sp++);
3200 png_byte blue = *(sp++);
3201
3202 if (red != green || red != blue)
3203 {
3204 rgb_error |= 1;
3205 /*NOTE: this is the historical approach which simply
3206 * truncates the results.
3207 */
3208 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
3209 }
3210
3211 else
3212 *(dp++) = red;
3213
3214 if (have_alpha)
3215 *(dp++) = *(sp++);
3216 }
3217 }
3218 }
3219
3220 else /* RGB bit_depth == 16 */
3221 {
3222 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
3223 if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL)
3224 {
3225 png_bytep sp = row;
3226 png_bytep dp = row;
3227 png_uint_32 i;
3228
3229 for (i = 0; i < row_width; i++)
3230 {
3231 png_uint_16 red, green, blue, w;
3232
3233 red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
3234 green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
3235 blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
3236
3237 if (red == green && red == blue)
3238 {
3239 if (png_ptr->gamma_16_table != NULL)
3240 w = png_ptr->gamma_16_table[(red&0xff)
3241 >> png_ptr->gamma_shift][red>>8];
3242
3243 else
3244 w = red;
3245 }
3246
3247 else
3248 {
3249 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff)
3250 >> png_ptr->gamma_shift][red>>8];
3251 png_uint_16 green_1 =
3252 png_ptr->gamma_16_to_1[(green&0xff) >>
3253 png_ptr->gamma_shift][green>>8];
3254 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff)
3255 >> png_ptr->gamma_shift][blue>>8];
3256 png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1
3257 + bc*blue_1 + 16384)>>15);
3258 w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
3259 png_ptr->gamma_shift][gray16 >> 8];
3260 rgb_error |= 1;
3261 }
3262
3263 *(dp++) = (png_byte)((w>>8) & 0xff);
3264 *(dp++) = (png_byte)(w & 0xff);
3265
3266 if (have_alpha)
3267 {
3268 *(dp++) = *(sp++);
3269 *(dp++) = *(sp++);
3270 }
3271 }
3272 }
3273 else
3274 #endif
3275 {
3276 png_bytep sp = row;
3277 png_bytep dp = row;
3278 png_uint_32 i;
3279
3280 for (i = 0; i < row_width; i++)
3281 {
3282 png_uint_16 red, green, blue, gray16;
3283
3284 red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
3285 green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
3286 blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
3287
3288 if (red != green || red != blue)
3289 rgb_error |= 1;
3290
3291 /* From 1.5.5 in the 16 bit case do the accurate convertion even
3292 * in the 'fast' case - this is because this is where the code
3293 * ends up when handling linear 16 bit data.
3294 */
3295 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >>
3296 15);
3297 *(dp++) = (png_byte)((gray16>>8) & 0xff);
3298 *(dp++) = (png_byte)(gray16 & 0xff);
3299
3300 if (have_alpha)
3301 {
3302 *(dp++) = *(sp++);
3303 *(dp++) = *(sp++);
3304 }
3305 }
3306 }
3307 }
3308
3309 row_info->channels -= 2;
3310 row_info->color_type = (png_byte)(row_info->color_type &
3311 ~PNG_COLOR_MASK_COLOR);
3312 row_info->pixel_depth = (png_byte)(row_info->channels *
3313 row_info->bit_depth);
3314 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3315 }
3316 return rgb_error;
3317 }
3318 #endif
3319 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
3320
3321 #ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
3322 /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
3323 * large of png_color. This lets grayscale images be treated as
3324 * paletted. Most useful for gamma correction and simplification
3325 * of code. This API is not used internally.
3326 */
3327 void PNGAPI
3328 png_build_grayscale_palette(int bit_depth, png_colorp palette)
3329 {
3330 int num_palette;
3331 int color_inc;
3332 int i;
3333 int v;
3334
3335 png_debug(1, "in png_do_build_grayscale_palette");
3336
3337 if (palette == NULL)
3338 return;
3339
3340 switch (bit_depth)
3341 {
3342 case 1:
3343 num_palette = 2;
3344 color_inc = 0xff;
3345 break;
3346
3347 case 2:
3348 num_palette = 4;
3349 color_inc = 0x55;
3350 break;
3351
3352 case 4:
3353 num_palette = 16;
3354 color_inc = 0x11;
3355 break;
3356
3357 case 8:
3358 num_palette = 256;
3359 color_inc = 1;
3360 break;
3361
3362 default:
3363 num_palette = 0;
3364 color_inc = 0;
3365 break;
3366 }
3367
3368 for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
3369 {
3370 palette[i].red = (png_byte)v;
3371 palette[i].green = (png_byte)v;
3372 palette[i].blue = (png_byte)v;
3373 }
3374 }
3375 #endif
3376
3377
3378 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
3379 #ifdef PNG_READ_BACKGROUND_SUPPORTED
3380 /* Replace any alpha or transparency with the supplied background color.
3381 * "background" is already in the screen gamma, while "background_1" is
3382 * at a gamma of 1.0. Paletted files have already been taken care of.
3383 */
3384 void /* PRIVATE */
3385 png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr)
3386 {
3387 #ifdef PNG_READ_GAMMA_SUPPORTED
3388 png_const_bytep gamma_table = png_ptr->gamma_table;
3389 png_const_bytep gamma_from_1 = png_ptr->gamma_from_1;
3390 png_const_bytep gamma_to_1 = png_ptr->gamma_to_1;
3391 png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table;
3392 png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1;
3393 png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1;
3394 int gamma_shift = png_ptr->gamma_shift;
3395 #endif
3396
3397 png_bytep sp;
3398 png_uint_32 i;
3399 png_uint_32 row_width = row_info->width;
3400 int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0;
3401 int shift;
3402
3403 png_debug(1, "in png_do_compose");
3404
3405 {
3406 switch (row_info->color_type)
3407 {
3408 case PNG_COLOR_TYPE_GRAY:
3409 {
3410 switch (row_info->bit_depth)
3411 {
3412 case 1:
3413 {
3414 sp = row;
3415 shift = 7;
3416 for (i = 0; i < row_width; i++)
3417 {
3418 if ((png_uint_16)((*sp >> shift) & 0x01)
3419 == png_ptr->trans_color.gray)
3420 {
3421 *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
3422 *sp |= (png_byte)(png_ptr->background.gray << shift);
3423 }
3424
3425 if (!shift)
3426 {
3427 shift = 7;
3428 sp++;
3429 }
3430
3431 else
3432 shift--;
3433 }
3434 break;
3435 }
3436
3437 case 2:
3438 {
3439 #ifdef PNG_READ_GAMMA_SUPPORTED
3440 if (gamma_table != NULL)
3441 {
3442 sp = row;
3443 shift = 6;
3444 for (i = 0; i < row_width; i++)
3445 {
3446 if ((png_uint_16)((*sp >> shift) & 0x03)
3447 == png_ptr->trans_color.gray)
3448 {
3449 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
3450 *sp |= (png_byte)(png_ptr->background.gray << shift);
3451 }
3452
3453 else
3454 {
3455 png_byte p = (png_byte)((*sp >> shift) & 0x03);
3456 png_byte g = (png_byte)((gamma_table [p | (p << 2) |
3457 (p << 4) | (p << 6)] >> 6) & 0x03);
3458 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
3459 *sp |= (png_byte)(g << shift);
3460 }
3461
3462 if (!shift)
3463 {
3464 shift = 6;
3465 sp++;
3466 }
3467
3468 else
3469 shift -= 2;
3470 }
3471 }
3472
3473 else
3474 #endif
3475 {
3476 sp = row;
3477 shift = 6;
3478 for (i = 0; i < row_width; i++)
3479 {
3480 if ((png_uint_16)((*sp >> shift) & 0x03)
3481 == png_ptr->trans_color.gray)
3482 {
3483 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
3484 *sp |= (png_byte)(png_ptr->background.gray << shift);
3485 }
3486
3487 if (!shift)
3488 {
3489 shift = 6;
3490 sp++;
3491 }
3492
3493 else
3494 shift -= 2;
3495 }
3496 }
3497 break;
3498 }
3499
3500 case 4:
3501 {
3502 #ifdef PNG_READ_GAMMA_SUPPORTED
3503 if (gamma_table != NULL)
3504 {
3505 sp = row;
3506 shift = 4;
3507 for (i = 0; i < row_width; i++)
3508 {
3509 if ((png_uint_16)((*sp >> shift) & 0x0f)
3510 == png_ptr->trans_color.gray)
3511 {
3512 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
3513 *sp |= (png_byte)(png_ptr->background.gray << shift);
3514 }
3515
3516 else
3517 {
3518 png_byte p = (png_byte)((*sp >> shift) & 0x0f);
3519 png_byte g = (png_byte)((gamma_table[p |
3520 (p << 4)] >> 4) & 0x0f);
3521 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
3522 *sp |= (png_byte)(g << shift);
3523 }
3524
3525 if (!shift)
3526 {
3527 shift = 4;
3528 sp++;
3529 }
3530
3531 else
3532 shift -= 4;
3533 }
3534 }
3535
3536 else
3537 #endif
3538 {
3539 sp = row;
3540 shift = 4;
3541 for (i = 0; i < row_width; i++)
3542 {
3543 if ((png_uint_16)((*sp >> shift) & 0x0f)
3544 == png_ptr->trans_color.gray)
3545 {
3546 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
3547 *sp |= (png_byte)(png_ptr->background.gray << shift);
3548 }
3549
3550 if (!shift)
3551 {
3552 shift = 4;
3553 sp++;
3554 }
3555
3556 else
3557 shift -= 4;
3558 }
3559 }
3560 break;
3561 }
3562
3563 case 8:
3564 {
3565 #ifdef PNG_READ_GAMMA_SUPPORTED
3566 if (gamma_table != NULL)
3567 {
3568 sp = row;
3569 for (i = 0; i < row_width; i++, sp++)
3570 {
3571 if (*sp == png_ptr->trans_color.gray)
3572 *sp = (png_byte)png_ptr->background.gray;
3573
3574 else
3575 *sp = gamma_table[*sp];
3576 }
3577 }
3578 else
3579 #endif
3580 {
3581 sp = row;
3582 for (i = 0; i < row_width; i++, sp++)
3583 {
3584 if (*sp == png_ptr->trans_color.gray)
3585 *sp = (png_byte)png_ptr->background.gray;
3586 }
3587 }
3588 break;
3589 }
3590
3591 case 16:
3592 {
3593 #ifdef PNG_READ_GAMMA_SUPPORTED
3594 if (gamma_16 != NULL)
3595 {
3596 sp = row;
3597 for (i = 0; i < row_width; i++, sp += 2)
3598 {
3599 png_uint_16 v;
3600
3601 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3602
3603 if (v == png_ptr->trans_color.gray)
3604 {
3605 /* Background is already in screen gamma */
3606 *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff);
3607 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
3608 }
3609
3610 else
3611 {
3612 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3613 *sp = (png_byte)((v >> 8) & 0xff);
3614 *(sp + 1) = (png_byte)(v & 0xff);
3615 }
3616 }
3617 }
3618 else
3619 #endif
3620 {
3621 sp = row;
3622 for (i = 0; i < row_width; i++, sp += 2)
3623 {
3624 png_uint_16 v;
3625
3626 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3627
3628 if (v == png_ptr->trans_color.gray)
3629 {
3630 *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff);
3631 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
3632 }
3633 }
3634 }
3635 break;
3636 }
3637
3638 default:
3639 break;
3640 }
3641 break;
3642 }
3643
3644 case PNG_COLOR_TYPE_RGB:
3645 {
3646 if (row_info->bit_depth == 8)
3647 {
3648 #ifdef PNG_READ_GAMMA_SUPPORTED
3649 if (gamma_table != NULL)
3650 {
3651 sp = row;
3652 for (i = 0; i < row_width; i++, sp += 3)
3653 {
3654 if (*sp == png_ptr->trans_color.red &&
3655 *(sp + 1) == png_ptr->trans_color.green &&
3656 *(sp + 2) == png_ptr->trans_color.blue)
3657 {
3658 *sp = (png_byte)png_ptr->background.red;
3659 *(sp + 1) = (png_byte)png_ptr->background.green;
3660 *(sp + 2) = (png_byte)png_ptr->background.blue;
3661 }
3662
3663 else
3664 {
3665 *sp = gamma_table[*sp];
3666 *(sp + 1) = gamma_table[*(sp + 1)];
3667 *(sp + 2) = gamma_table[*(sp + 2)];
3668 }
3669 }
3670 }
3671 else
3672 #endif
3673 {
3674 sp = row;
3675 for (i = 0; i < row_width; i++, sp += 3)
3676 {
3677 if (*sp == png_ptr->trans_color.red &&
3678 *(sp + 1) == png_ptr->trans_color.green &&
3679 *(sp + 2) == png_ptr->trans_color.blue)
3680 {
3681 *sp = (png_byte)png_ptr->background.red;
3682 *(sp + 1) = (png_byte)png_ptr->background.green;
3683 *(sp + 2) = (png_byte)png_ptr->background.blue;
3684 }
3685 }
3686 }
3687 }
3688 else /* if (row_info->bit_depth == 16) */
3689 {
3690 #ifdef PNG_READ_GAMMA_SUPPORTED
3691 if (gamma_16 != NULL)
3692 {
3693 sp = row;
3694 for (i = 0; i < row_width; i++, sp += 6)
3695 {
3696