2 /* pngrtran.c - transforms the data in a row for PNG readers
4 * Last changed in libpng 1.5.2 [March 31, 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.)
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
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
21 #ifdef PNG_READ_SUPPORTED
23 /* Set the action on getting a CRC error for an ancillary or critical chunk. */
25 png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
27 png_debug(1, "in png_set_crc_action");
32 /* Tell libpng how we react to CRC errors in critical chunks */
35 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
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;
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;
49 case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
51 "Can't discard critical data on CRC error");
52 case PNG_CRC_ERROR_QUIT: /* Error/quit */
56 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
60 /* Tell libpng how we react to CRC errors in ancillary chunks */
63 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
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;
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;
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;
82 case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
86 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
91 #ifdef PNG_READ_BACKGROUND_SUPPORTED
92 /* Handle alpha and tRNS via a background color */
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)
98 png_debug(1, "in png_set_background_fixed");
103 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
105 png_warning(png_ptr, "Application must supply a known background gamma");
109 png_ptr->transformations |= PNG_BACKGROUND;
110 png_memcpy(&(png_ptr->background), background_color,
111 png_sizeof(png_color_16));
112 png_ptr->background_gamma = background_gamma;
113 png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
114 png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
117 # ifdef PNG_FLOATING_POINT_SUPPORTED
119 png_set_background(png_structp png_ptr,
120 png_const_color_16p background_color, int background_gamma_code,
121 int need_expand, double background_gamma)
123 png_set_background_fixed(png_ptr, background_color, background_gamma_code,
124 need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
126 # endif /* FLOATING_POINT */
127 #endif /* READ_BACKGROUND */
129 #ifdef PNG_READ_16_TO_8_SUPPORTED
130 /* Strip 16 bit depth files to 8 bit depth */
132 png_set_strip_16(png_structp png_ptr)
134 png_debug(1, "in png_set_strip_16");
139 png_ptr->transformations |= PNG_16_TO_8;
140 png_ptr->transformations &= ~PNG_EXPAND_16;
144 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
146 png_set_strip_alpha(png_structp png_ptr)
148 png_debug(1, "in png_set_strip_alpha");
153 png_ptr->transformations |= PNG_STRIP_ALPHA;
157 #ifdef PNG_READ_QUANTIZE_SUPPORTED
158 /* Dither file to 8 bit. Supply a palette, the current number
159 * of elements in the palette, the maximum number of elements
160 * allowed, and a histogram if possible. If the current number
161 * of colors is greater then the maximum number, the palette will be
162 * modified to fit in the maximum number. "full_quantize" indicates
163 * whether we need a quantizing cube set up for RGB images, or if we
164 * simply are reducing the number of colors in a paletted image.
167 typedef struct png_dsort_struct
169 struct png_dsort_struct FAR * next;
173 typedef png_dsort FAR * png_dsortp;
174 typedef png_dsort FAR * FAR * png_dsortpp;
177 png_set_quantize(png_structp png_ptr, png_colorp palette,
178 int num_palette, int maximum_colors, png_const_uint_16p histogram,
181 png_debug(1, "in png_set_quantize");
186 png_ptr->transformations |= PNG_QUANTIZE;
192 png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,
193 (png_uint_32)(num_palette * png_sizeof(png_byte)));
194 for (i = 0; i < num_palette; i++)
195 png_ptr->quantize_index[i] = (png_byte)i;
198 if (num_palette > maximum_colors)
200 if (histogram != NULL)
202 /* This is easy enough, just throw out the least used colors.
203 * Perhaps not the best solution, but good enough.
208 /* Initialize an array to sort colors */
209 png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
210 (png_uint_32)(num_palette * png_sizeof(png_byte)));
212 /* Initialize the quantize_sort array */
213 for (i = 0; i < num_palette; i++)
214 png_ptr->quantize_sort[i] = (png_byte)i;
216 /* Find the least used palette entries by starting a
217 * bubble sort, and running it until we have sorted
218 * out enough colors. Note that we don't care about
219 * sorting all the colors, just finding which are
223 for (i = num_palette - 1; i >= maximum_colors; i--)
225 int done; /* To stop early if the list is pre-sorted */
229 for (j = 0; j < i; j++)
231 if (histogram[png_ptr->quantize_sort[j]]
232 < histogram[png_ptr->quantize_sort[j + 1]])
236 t = png_ptr->quantize_sort[j];
237 png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];
238 png_ptr->quantize_sort[j + 1] = t;
247 /* Swap the palette around, and set up a table, if necessary */
252 /* Put all the useful colors within the max, but don't
255 for (i = 0; i < maximum_colors; i++)
257 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
261 while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
263 palette[i] = palette[j];
271 /* Move all the used colors inside the max limit, and
272 * develop a translation table.
274 for (i = 0; i < maximum_colors; i++)
276 /* Only move the colors we need to */
277 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
283 while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
285 tmp_color = palette[j];
286 palette[j] = palette[i];
287 palette[i] = tmp_color;
288 /* Indicate where the color went */
289 png_ptr->quantize_index[j] = (png_byte)i;
290 png_ptr->quantize_index[i] = (png_byte)j;
294 /* Find closest color for those colors we are not using */
295 for (i = 0; i < num_palette; i++)
297 if ((int)png_ptr->quantize_index[i] >= maximum_colors)
299 int min_d, k, min_k, d_index;
301 /* Find the closest color to one we threw out */
302 d_index = png_ptr->quantize_index[i];
303 min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
304 for (k = 1, min_k = 0; k < maximum_colors; k++)
308 d = PNG_COLOR_DIST(palette[d_index], palette[k]);
316 /* Point to closest color */
317 png_ptr->quantize_index[i] = (png_byte)min_k;
321 png_free(png_ptr, png_ptr->quantize_sort);
322 png_ptr->quantize_sort = NULL;
326 /* This is much harder to do simply (and quickly). Perhaps
327 * we need to go through a median cut routine, but those
328 * don't always behave themselves with only a few colors
329 * as input. So we will just find the closest two colors,
330 * and throw out one of them (chosen somewhat randomly).
331 * [We don't understand this at all, so if someone wants to
332 * work on improving it, be our guest - AED, GRP]
342 /* Initialize palette index arrays */
343 png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
344 (png_uint_32)(num_palette * png_sizeof(png_byte)));
345 png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
346 (png_uint_32)(num_palette * png_sizeof(png_byte)));
348 /* Initialize the sort array */
349 for (i = 0; i < num_palette; i++)
351 png_ptr->index_to_palette[i] = (png_byte)i;
352 png_ptr->palette_to_index[i] = (png_byte)i;
355 hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
356 png_sizeof(png_dsortp)));
358 num_new_palette = num_palette;
360 /* Initial wild guess at how far apart the farthest pixel
361 * pair we will be eliminating will be. Larger
362 * numbers mean more areas will be allocated, Smaller
363 * numbers run the risk of not saving enough data, and
364 * having to do this all over again.
366 * I have not done extensive checking on this number.
370 while (num_new_palette > maximum_colors)
372 for (i = 0; i < num_new_palette - 1; i++)
376 for (j = i + 1; j < num_new_palette; j++)
380 d = PNG_COLOR_DIST(palette[i], palette[j]);
385 t = (png_dsortp)png_malloc_warn(png_ptr,
386 (png_uint_32)(png_sizeof(png_dsort)));
392 t->left = (png_byte)i;
393 t->right = (png_byte)j;
402 for (i = 0; i <= max_d; i++)
408 for (p = hash[i]; p; p = p->next)
410 if ((int)png_ptr->index_to_palette[p->left]
412 (int)png_ptr->index_to_palette[p->right]
417 if (num_new_palette & 0x01)
429 palette[png_ptr->index_to_palette[j]]
430 = palette[num_new_palette];
435 for (k = 0; k < num_palette; k++)
437 if (png_ptr->quantize_index[k] ==
438 png_ptr->index_to_palette[j])
439 png_ptr->quantize_index[k] =
440 png_ptr->index_to_palette[next_j];
442 if ((int)png_ptr->quantize_index[k] ==
444 png_ptr->quantize_index[k] =
445 png_ptr->index_to_palette[j];
449 png_ptr->index_to_palette[png_ptr->palette_to_index
450 [num_new_palette]] = png_ptr->index_to_palette[j];
452 png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
453 = png_ptr->palette_to_index[num_new_palette];
455 png_ptr->index_to_palette[j] =
456 (png_byte)num_new_palette;
458 png_ptr->palette_to_index[num_new_palette] =
461 if (num_new_palette <= maximum_colors)
464 if (num_new_palette <= maximum_colors)
469 for (i = 0; i < 769; i++)
473 png_dsortp p = hash[i];
477 png_free(png_ptr, p);
485 png_free(png_ptr, hash);
486 png_free(png_ptr, png_ptr->palette_to_index);
487 png_free(png_ptr, png_ptr->index_to_palette);
488 png_ptr->palette_to_index = NULL;
489 png_ptr->index_to_palette = NULL;
491 num_palette = maximum_colors;
493 if (png_ptr->palette == NULL)
495 png_ptr->palette = palette;
497 png_ptr->num_palette = (png_uint_16)num_palette;
503 int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +
504 PNG_QUANTIZE_BLUE_BITS;
505 int num_red = (1 << PNG_QUANTIZE_RED_BITS);
506 int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);
507 int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
508 png_size_t num_entries = ((png_size_t)1 << total_bits);
510 png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr,
511 (png_uint_32)(num_entries * png_sizeof(png_byte)));
513 distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
514 png_sizeof(png_byte)));
516 png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
518 for (i = 0; i < num_palette; i++)
521 int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS));
522 int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS));
523 int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS));
525 for (ir = 0; ir < num_red; ir++)
527 /* int dr = abs(ir - r); */
528 int dr = ((ir > r) ? ir - r : r - ir);
529 int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS +
530 PNG_QUANTIZE_GREEN_BITS));
532 for (ig = 0; ig < num_green; ig++)
534 /* int dg = abs(ig - g); */
535 int dg = ((ig > g) ? ig - g : g - ig);
537 int dm = ((dr > dg) ? dr : dg);
538 int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS);
540 for (ib = 0; ib < num_blue; ib++)
542 int d_index = index_g | ib;
543 /* int db = abs(ib - b); */
544 int db = ((ib > b) ? ib - b : b - ib);
545 int dmax = ((dm > db) ? dm : db);
546 int d = dmax + dt + db;
548 if (d < (int)distance[d_index])
550 distance[d_index] = (png_byte)d;
551 png_ptr->palette_lookup[d_index] = (png_byte)i;
558 png_free(png_ptr, distance);
561 #endif /* PNG_READ_QUANTIZE_SUPPORTED */
563 #ifdef PNG_READ_GAMMA_SUPPORTED
564 /* Transform the image from the file_gamma to the screen_gamma. We
565 * only do transformations on images where the file_gamma and screen_gamma
566 * are not close reciprocals, otherwise it slows things down slightly, and
567 * also needlessly introduces small errors.
569 * We will turn off gamma transformation later if no semitransparent entries
570 * are present in the tRNS array for palette images. We can't do it here
571 * because we don't necessarily have the tRNS chunk yet.
573 static int /* PRIVATE */
574 png_gamma_threshold(png_fixed_point scrn_gamma, png_fixed_point file_gamma)
576 /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
577 * correction as a difference of the overall transform from 1.0
579 * We want to compare the threshold with s*f - 1, if we get
580 * overflow here it is because of wacky gamma values so we
581 * turn on processing anyway.
583 png_fixed_point gtest;
584 return !png_muldiv(>est, scrn_gamma, file_gamma, PNG_FP_1) ||
585 png_gamma_significant(gtest);
589 png_set_gamma_fixed(png_structp png_ptr, png_fixed_point scrn_gamma,
590 png_fixed_point file_gamma)
592 png_debug(1, "in png_set_gamma_fixed");
597 if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
598 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
599 png_gamma_threshold(scrn_gamma, file_gamma))
600 png_ptr->transformations |= PNG_GAMMA;
601 png_ptr->gamma = file_gamma;
602 png_ptr->screen_gamma = scrn_gamma;
605 # ifdef PNG_FLOATING_POINT_SUPPORTED
607 png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
609 png_set_gamma_fixed(png_ptr,
610 png_fixed(png_ptr, scrn_gamma, "png_set_gamma screen gamma"),
611 png_fixed(png_ptr, file_gamma, "png_set_gamma file gamma"));
613 # endif /* FLOATING_POINT_SUPPORTED */
614 #endif /* READ_GAMMA */
616 #ifdef PNG_READ_EXPAND_SUPPORTED
617 /* Expand paletted images to RGB, expand grayscale images of
618 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
622 png_set_expand(png_structp png_ptr)
624 png_debug(1, "in png_set_expand");
629 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
630 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
633 /* GRR 19990627: the following three functions currently are identical
634 * to png_set_expand(). However, it is entirely reasonable that someone
635 * might wish to expand an indexed image to RGB but *not* expand a single,
636 * fully transparent palette entry to a full alpha channel--perhaps instead
637 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
638 * the transparent color with a particular RGB value, or drop tRNS entirely.
639 * IOW, a future version of the library may make the transformations flag
640 * a bit more fine-grained, with separate bits for each of these three
643 * More to the point, these functions make it obvious what libpng will be
644 * doing, whereas "expand" can (and does) mean any number of things.
646 * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
647 * to expand only the sample depth but not to expand the tRNS to alpha
648 * and its name was changed to png_set_expand_gray_1_2_4_to_8().
651 /* Expand paletted images to RGB. */
653 png_set_palette_to_rgb(png_structp png_ptr)
655 png_debug(1, "in png_set_palette_to_rgb");
660 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
661 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
664 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
666 png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
668 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
673 png_ptr->transformations |= PNG_EXPAND;
674 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
679 /* Expand tRNS chunks to alpha channels. */
681 png_set_tRNS_to_alpha(png_structp png_ptr)
683 png_debug(1, "in png_set_tRNS_to_alpha");
685 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
686 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
688 #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
690 #ifdef PNG_READ_EXPAND_16_SUPPORTED
691 /* Expand to 16 bit channels, expand the tRNS chunk too (because otherwise
692 * it may not work correctly.)
695 png_set_expand_16(png_structp png_ptr)
697 png_debug(1, "in png_set_expand_16");
702 png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
703 png_ptr->transformations &= ~PNG_16_TO_8;
705 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
709 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
711 png_set_gray_to_rgb(png_structp png_ptr)
713 png_debug(1, "in png_set_gray_to_rgb");
717 /* Because rgb must be 8 bits or more: */
718 png_set_expand_gray_1_2_4_to_8(png_ptr);
719 png_ptr->transformations |= PNG_GRAY_TO_RGB;
720 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
725 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
727 png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
728 png_fixed_point red, png_fixed_point green)
730 png_debug(1, "in png_set_rgb_to_gray");
738 png_ptr->transformations |= PNG_RGB_TO_GRAY;
742 png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
746 png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
750 png_error(png_ptr, "invalid error action to rgb_to_gray");
753 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
754 #ifdef PNG_READ_EXPAND_SUPPORTED
755 png_ptr->transformations |= PNG_EXPAND;
759 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
761 png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
765 png_uint_16 red_int, green_int;
766 if (red < 0 || green < 0)
768 red_int = 6968; /* .212671 * 32768 + .5 */
769 green_int = 23434; /* .715160 * 32768 + .5 */
772 else if (red + green < 100000L)
774 red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
775 green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
780 png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
785 png_ptr->rgb_to_gray_red_coeff = red_int;
786 png_ptr->rgb_to_gray_green_coeff = green_int;
787 png_ptr->rgb_to_gray_blue_coeff =
788 (png_uint_16)(32768 - red_int - green_int);
792 #ifdef PNG_FLOATING_POINT_SUPPORTED
793 /* Convert a RGB image to a grayscale of the same width. This allows us,
794 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
798 png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
804 png_set_rgb_to_gray_fixed(png_ptr, error_action,
805 png_fixed(png_ptr, red, "rgb to gray red coefficient"),
806 png_fixed(png_ptr, green, "rgb to gray green coefficient"));
808 #endif /* FLOATING POINT */
812 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
813 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
815 png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
816 read_user_transform_fn)
818 png_debug(1, "in png_set_read_user_transform_fn");
823 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
824 png_ptr->transformations |= PNG_USER_TRANSFORM;
825 png_ptr->read_user_transform_fn = read_user_transform_fn;
830 /* Initialize everything needed for the read. This includes modifying
834 png_init_read_transformations(png_structp png_ptr)
836 png_debug(1, "in png_init_read_transformations");
839 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
840 defined(PNG_READ_SHIFT_SUPPORTED) || \
841 defined(PNG_READ_GAMMA_SUPPORTED)
842 int color_type = png_ptr->color_type;
845 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
847 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
848 /* Detect gray background and attempt to enable optimization
849 * for gray --> RGB case
851 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
852 * RGB_ALPHA (in which case need_expand is superfluous anyway), the
853 * background color might actually be gray yet not be flagged as such.
854 * This is not a problem for the current code, which uses
855 * PNG_BACKGROUND_IS_GRAY only to decide when to do the
856 * png_do_gray_to_rgb() transformation.
858 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
859 !(color_type & PNG_COLOR_MASK_COLOR))
861 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
864 else if ((png_ptr->transformations & PNG_BACKGROUND) &&
865 !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
866 (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
867 png_ptr->background.red == png_ptr->background.green &&
868 png_ptr->background.red == png_ptr->background.blue)
870 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
871 png_ptr->background.gray = png_ptr->background.red;
875 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
876 (png_ptr->transformations & PNG_EXPAND))
878 if (!(color_type & PNG_COLOR_MASK_COLOR)) /* i.e., GRAY or GRAY_ALPHA */
880 /* Expand background and tRNS chunks */
881 switch (png_ptr->bit_depth)
884 png_ptr->background.gray *= (png_uint_16)0xff;
885 png_ptr->background.red = png_ptr->background.green
886 = png_ptr->background.blue = png_ptr->background.gray;
887 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
889 png_ptr->trans_color.gray *= (png_uint_16)0xff;
890 png_ptr->trans_color.red = png_ptr->trans_color.green
891 = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
896 png_ptr->background.gray *= (png_uint_16)0x55;
897 png_ptr->background.red = png_ptr->background.green
898 = png_ptr->background.blue = png_ptr->background.gray;
899 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
901 png_ptr->trans_color.gray *= (png_uint_16)0x55;
902 png_ptr->trans_color.red = png_ptr->trans_color.green
903 = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
908 png_ptr->background.gray *= (png_uint_16)0x11;
909 png_ptr->background.red = png_ptr->background.green
910 = png_ptr->background.blue = png_ptr->background.gray;
911 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
913 png_ptr->trans_color.gray *= (png_uint_16)0x11;
914 png_ptr->trans_color.red = png_ptr->trans_color.green
915 = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
924 png_ptr->background.red = png_ptr->background.green
925 = png_ptr->background.blue = png_ptr->background.gray;
929 else if (color_type == PNG_COLOR_TYPE_PALETTE)
931 png_ptr->background.red =
932 png_ptr->palette[png_ptr->background.index].red;
933 png_ptr->background.green =
934 png_ptr->palette[png_ptr->background.index].green;
935 png_ptr->background.blue =
936 png_ptr->palette[png_ptr->background.index].blue;
938 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
939 if (png_ptr->transformations & PNG_INVERT_ALPHA)
941 #ifdef PNG_READ_EXPAND_SUPPORTED
942 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
945 /* Invert the alpha channel (in tRNS) unless the pixels are
946 * going to be expanded, in which case leave it for later
949 istop=(int)png_ptr->num_trans;
950 for (i=0; i<istop; i++)
951 png_ptr->trans_alpha[i] = (png_byte)(255 -
952 png_ptr->trans_alpha[i]);
961 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
962 png_ptr->background_1 = png_ptr->background;
964 #ifdef PNG_READ_GAMMA_SUPPORTED
966 if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
967 && png_gamma_threshold(png_ptr->screen_gamma, png_ptr->gamma))
971 for (i=0; i<png_ptr->num_trans; i++)
973 if (png_ptr->trans_alpha[i] != 0 && png_ptr->trans_alpha[i] != 0xff)
974 k=1; /* Partial transparency is present */
977 png_ptr->transformations &= ~PNG_GAMMA;
980 if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
983 png_build_gamma_table(png_ptr, png_ptr->bit_depth);
985 #ifdef PNG_READ_BACKGROUND_SUPPORTED
986 if (png_ptr->transformations & PNG_BACKGROUND)
988 if (color_type == PNG_COLOR_TYPE_PALETTE)
990 /* Could skip if no transparency */
991 png_color back, back_1;
992 png_colorp palette = png_ptr->palette;
993 int num_palette = png_ptr->num_palette;
995 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
998 back.red = png_ptr->gamma_table[png_ptr->background.red];
999 back.green = png_ptr->gamma_table[png_ptr->background.green];
1000 back.blue = png_ptr->gamma_table[png_ptr->background.blue];
1002 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
1003 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
1004 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
1008 png_fixed_point g, gs;
1010 switch (png_ptr->background_gamma_type)
1012 case PNG_BACKGROUND_GAMMA_SCREEN:
1013 g = (png_ptr->screen_gamma);
1017 case PNG_BACKGROUND_GAMMA_FILE:
1018 g = png_reciprocal(png_ptr->gamma);
1019 gs = png_reciprocal2(png_ptr->gamma,
1020 png_ptr->screen_gamma);
1023 case PNG_BACKGROUND_GAMMA_UNIQUE:
1024 g = png_reciprocal(png_ptr->background_gamma);
1025 gs = png_reciprocal2(png_ptr->background_gamma,
1026 png_ptr->screen_gamma);
1029 g = PNG_FP_1; /* back_1 */
1030 gs = PNG_FP_1; /* back */
1034 if (png_gamma_significant(gs))
1036 back.red = (png_byte)png_ptr->background.red;
1037 back.green = (png_byte)png_ptr->background.green;
1038 back.blue = (png_byte)png_ptr->background.blue;
1043 back.red = png_gamma_8bit_correct(png_ptr->background.red,
1045 back.green = png_gamma_8bit_correct(png_ptr->background.green,
1047 back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1050 back_1.red = png_gamma_8bit_correct(png_ptr->background.red, g);
1051 back_1.green = png_gamma_8bit_correct(png_ptr->background.green,
1053 back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1056 for (i = 0; i < num_palette; i++)
1058 if (i < (int)png_ptr->num_trans &&
1059 png_ptr->trans_alpha[i] != 0xff)
1061 if (png_ptr->trans_alpha[i] == 0)
1065 else /* if (png_ptr->trans_alpha[i] != 0xff) */
1069 v = png_ptr->gamma_to_1[palette[i].red];
1070 png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);
1071 palette[i].red = png_ptr->gamma_from_1[w];
1073 v = png_ptr->gamma_to_1[palette[i].green];
1074 png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);
1075 palette[i].green = png_ptr->gamma_from_1[w];
1077 v = png_ptr->gamma_to_1[palette[i].blue];
1078 png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);
1079 palette[i].blue = png_ptr->gamma_from_1[w];
1084 palette[i].red = png_ptr->gamma_table[palette[i].red];
1085 palette[i].green = png_ptr->gamma_table[palette[i].green];
1086 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1089 /* Prevent the transformations being done again, and make sure
1090 * that the now spurious alpha channel is stripped - the code
1091 * has just reduced background composition and gamma correction
1092 * to a simple alpha channel strip.
1094 png_ptr->transformations &= ~PNG_BACKGROUND;
1095 png_ptr->transformations &= ~PNG_GAMMA;
1096 png_ptr->transformations |= PNG_STRIP_ALPHA;
1099 /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1101 /* color_type != PNG_COLOR_TYPE_PALETTE */
1103 png_fixed_point g = PNG_FP_1;
1104 png_fixed_point gs = PNG_FP_1;
1106 switch (png_ptr->background_gamma_type)
1108 case PNG_BACKGROUND_GAMMA_SCREEN:
1109 g = png_ptr->screen_gamma;
1110 /* gs = PNG_FP_1; */
1113 case PNG_BACKGROUND_GAMMA_FILE:
1114 g = png_reciprocal(png_ptr->gamma);
1115 gs = png_reciprocal2(png_ptr->gamma, png_ptr->screen_gamma);
1118 case PNG_BACKGROUND_GAMMA_UNIQUE:
1119 g = png_reciprocal(png_ptr->background_gamma);
1120 gs = png_reciprocal2(png_ptr->background_gamma,
1121 png_ptr->screen_gamma);
1125 png_error(png_ptr, "invalid background gamma type");
1128 png_ptr->background_1.gray = png_gamma_correct(png_ptr,
1129 png_ptr->background.gray, g);
1131 png_ptr->background.gray = png_gamma_correct(png_ptr,
1132 png_ptr->background.gray, gs);
1134 if ((png_ptr->background.red != png_ptr->background.green) ||
1135 (png_ptr->background.red != png_ptr->background.blue) ||
1136 (png_ptr->background.red != png_ptr->background.gray))
1138 /* RGB or RGBA with color background */
1139 png_ptr->background_1.red = png_gamma_correct(png_ptr,
1140 png_ptr->background.red, g);
1142 png_ptr->background_1.green = png_gamma_correct(png_ptr,
1143 png_ptr->background.green, g);
1145 png_ptr->background_1.blue = png_gamma_correct(png_ptr,
1146 png_ptr->background.blue, g);
1148 png_ptr->background.red = png_gamma_correct(png_ptr,
1149 png_ptr->background.red, gs);
1151 png_ptr->background.green = png_gamma_correct(png_ptr,
1152 png_ptr->background.green, gs);
1154 png_ptr->background.blue = png_gamma_correct(png_ptr,
1155 png_ptr->background.blue, gs);
1160 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1161 png_ptr->background_1.red = png_ptr->background_1.green
1162 = png_ptr->background_1.blue = png_ptr->background_1.gray;
1164 png_ptr->background.red = png_ptr->background.green
1165 = png_ptr->background.blue = png_ptr->background.gray;
1170 /* Transformation does not include PNG_BACKGROUND */
1171 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1172 if (color_type == PNG_COLOR_TYPE_PALETTE)
1174 png_colorp palette = png_ptr->palette;
1175 int num_palette = png_ptr->num_palette;
1178 for (i = 0; i < num_palette; i++)
1180 palette[i].red = png_ptr->gamma_table[palette[i].red];
1181 palette[i].green = png_ptr->gamma_table[palette[i].green];
1182 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1185 /* Done the gamma correction. */
1186 png_ptr->transformations &= ~PNG_GAMMA;
1189 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1192 #endif /* PNG_READ_GAMMA_SUPPORTED */
1193 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1194 /* No GAMMA transformation */
1195 if ((png_ptr->transformations & PNG_BACKGROUND) &&
1196 (color_type == PNG_COLOR_TYPE_PALETTE))
1199 int istop = (int)png_ptr->num_trans;
1201 png_colorp palette = png_ptr->palette;
1203 back.red = (png_byte)png_ptr->background.red;
1204 back.green = (png_byte)png_ptr->background.green;
1205 back.blue = (png_byte)png_ptr->background.blue;
1207 for (i = 0; i < istop; i++)
1209 if (png_ptr->trans_alpha[i] == 0)
1214 else if (png_ptr->trans_alpha[i] != 0xff)
1216 /* The png_composite() macro is defined in png.h */
1217 png_composite(palette[i].red, palette[i].red,
1218 png_ptr->trans_alpha[i], back.red);
1220 png_composite(palette[i].green, palette[i].green,
1221 png_ptr->trans_alpha[i], back.green);
1223 png_composite(palette[i].blue, palette[i].blue,
1224 png_ptr->trans_alpha[i], back.blue);
1228 /* Handled alpha, still need to strip the channel. */
1229 png_ptr->transformations &= ~PNG_BACKGROUND;
1230 png_ptr->transformations |= PNG_STRIP_ALPHA;
1232 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1234 #ifdef PNG_READ_SHIFT_SUPPORTED
1235 if ((png_ptr->transformations & PNG_SHIFT) &&
1236 (color_type == PNG_COLOR_TYPE_PALETTE))
1239 png_uint_16 istop = png_ptr->num_palette;
1240 int sr = 8 - png_ptr->sig_bit.red;
1241 int sg = 8 - png_ptr->sig_bit.green;
1242 int sb = 8 - png_ptr->sig_bit.blue;
1244 if (sr < 0 || sr > 8)
1247 if (sg < 0 || sg > 8)
1250 if (sb < 0 || sb > 8)
1253 for (i = 0; i < istop; i++)
1255 png_ptr->palette[i].red >>= sr;
1256 png_ptr->palette[i].green >>= sg;
1257 png_ptr->palette[i].blue >>= sb;
1260 #endif /* PNG_READ_SHIFT_SUPPORTED */
1262 #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
1263 && !defined(PNG_READ_BACKGROUND_SUPPORTED)
1269 /* Modify the info structure to reflect the transformations. The
1270 * info should be updated so a PNG file could be written with it,
1271 * assuming the transformations result in valid PNG data.
1274 png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
1276 png_debug(1, "in png_read_transform_info");
1278 #ifdef PNG_READ_EXPAND_SUPPORTED
1279 if (png_ptr->transformations & PNG_EXPAND)
1281 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1283 if (png_ptr->num_trans &&
1284 (png_ptr->transformations & PNG_EXPAND_tRNS))
1285 info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1288 info_ptr->color_type = PNG_COLOR_TYPE_RGB;
1290 info_ptr->bit_depth = 8;
1291 info_ptr->num_trans = 0;
1295 if (png_ptr->num_trans)
1297 if (png_ptr->transformations & PNG_EXPAND_tRNS)
1298 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1300 if (info_ptr->bit_depth < 8)
1301 info_ptr->bit_depth = 8;
1303 info_ptr->num_trans = 0;
1308 #ifdef PNG_READ_EXPAND_16_SUPPORTED
1309 if (png_ptr->transformations & PNG_EXPAND_16 && info_ptr->bit_depth == 8 &&
1310 info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
1312 info_ptr->bit_depth = 16;
1316 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1317 if (png_ptr->transformations & PNG_BACKGROUND)
1319 info_ptr->color_type = (png_byte)(info_ptr->color_type &
1320 ~PNG_COLOR_MASK_ALPHA);
1321 info_ptr->num_trans = 0;
1322 info_ptr->background = png_ptr->background;
1326 #ifdef PNG_READ_GAMMA_SUPPORTED
1327 if (png_ptr->transformations & PNG_GAMMA)
1329 info_ptr->gamma = png_ptr->gamma;
1333 #ifdef PNG_READ_16_TO_8_SUPPORTED
1334 #ifdef PNG_READ_16BIT_SUPPORTED
1335 if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
1336 info_ptr->bit_depth = 8;
1338 /* Force chopping 16-bit input down to 8 */
1339 if (info_ptr->bit_depth == 16)
1341 png_ptr->transformations |=PNG_16_TO_8;
1342 info_ptr->bit_depth = 8;
1347 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1348 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
1349 info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
1352 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1353 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1354 info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
1357 #ifdef PNG_READ_QUANTIZE_SUPPORTED
1358 if (png_ptr->transformations & PNG_QUANTIZE)
1360 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1361 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
1362 png_ptr->palette_lookup && info_ptr->bit_depth == 8)
1364 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
1369 #ifdef PNG_READ_PACK_SUPPORTED
1370 if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
1371 info_ptr->bit_depth = 8;
1374 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1375 info_ptr->channels = 1;
1377 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
1378 info_ptr->channels = 3;
1381 info_ptr->channels = 1;
1383 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1384 if (png_ptr->transformations & PNG_STRIP_ALPHA)
1385 info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
1388 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1389 info_ptr->channels++;
1391 #ifdef PNG_READ_FILLER_SUPPORTED
1392 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
1393 if ((png_ptr->transformations & PNG_FILLER) &&
1394 ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1395 (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
1397 info_ptr->channels++;
1398 /* If adding a true alpha channel not just filler */
1399 if (png_ptr->transformations & PNG_ADD_ALPHA)
1400 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1404 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
1405 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1406 if (png_ptr->transformations & PNG_USER_TRANSFORM)
1408 if (info_ptr->bit_depth < png_ptr->user_transform_depth)
1409 info_ptr->bit_depth = png_ptr->user_transform_depth;
1411 if (info_ptr->channels < png_ptr->user_transform_channels)
1412 info_ptr->channels = png_ptr->user_transform_channels;
1416 info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
1417 info_ptr->bit_depth);
1419 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
1421 #ifndef PNG_READ_EXPAND_SUPPORTED
1427 /* Transform the row. The order of transformations is significant,
1428 * and is very touchy. If you add a transformation, take care to
1429 * decide how it fits in with the other transformations here.
1432 png_do_read_transformations(png_structp png_ptr)
1434 png_debug(1, "in png_do_read_transformations");
1436 if (png_ptr->row_buf == NULL)
1438 #ifdef PNG_CONSOLE_IO_SUPPORTED
1441 png_snprintf2(msg, 50,
1442 "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,
1444 png_error(png_ptr, msg);
1446 png_error(png_ptr, "NULL row buffer");
1449 #ifdef PNG_WARN_UNINITIALIZED_ROW
1450 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
1451 /* Application has failed to call either png_read_start_image()
1452 * or png_read_update_info() after setting transforms that expand
1453 * pixels. This check added to libpng-1.2.19
1455 #if (PNG_WARN_UNINITIALIZED_ROW==1)
1456 png_error(png_ptr, "Uninitialized row");
1458 png_warning(png_ptr, "Uninitialized row");
1462 #ifdef PNG_READ_EXPAND_SUPPORTED
1463 if (png_ptr->transformations & PNG_EXPAND)
1465 if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
1467 png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
1468 png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
1473 if (png_ptr->num_trans &&
1474 (png_ptr->transformations & PNG_EXPAND_tRNS))
1475 png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
1476 &(png_ptr->trans_color));
1479 png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
1485 /* Delay the 'expand 16' step until later for efficiency, so that the
1486 * intermediate steps work with 8 bit data.
1489 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1490 if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
1491 (png_ptr->row_info.color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
1492 png_ptr->row_info.color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
1493 png_do_strip_channel(&(png_ptr->row_info), png_ptr->row_buf + 1,
1494 0/*!at_start, because SWAP_ALPHA happens later*/);
1497 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1498 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1501 png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info),
1502 png_ptr->row_buf + 1);
1506 png_ptr->rgb_to_gray_status=1;
1507 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1508 PNG_RGB_TO_GRAY_WARN)
1509 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1511 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1512 PNG_RGB_TO_GRAY_ERR)
1513 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1518 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
1520 * In most cases, the "simple transparency" should be done prior to doing
1521 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
1522 * pixel is transparent. You would also need to make sure that the
1523 * transparency information is upgraded to RGB.
1525 * To summarize, the current flow is:
1526 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
1527 * with background "in place" if transparent,
1528 * convert to RGB if necessary
1529 * - Gray + alpha -> composite with gray background and remove alpha bytes,
1530 * convert to RGB if necessary
1532 * To support RGB backgrounds for gray images we need:
1533 * - Gray + simple transparency -> convert to RGB + simple transparency,
1534 * compare 3 or 6 bytes and composite with
1535 * background "in place" if transparent
1536 * (3x compare/pixel compared to doing
1537 * composite with gray bkgrnd)
1538 * - Gray + alpha -> convert to RGB + alpha, composite with background and
1539 * remove alpha bytes (3x float
1540 * operations/pixel compared with composite
1541 * on gray background)
1543 * Greg's change will do this. The reason it wasn't done before is for
1544 * performance, as this increases the per-pixel operations. If we would check
1545 * in advance if the background was gray or RGB, and position the gray-to-RGB
1546 * transform appropriately, then it would save a lot of work/time.
1549 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1550 /* If gray -> RGB, do so now only if background is non-gray; else do later
1551 * for performance reasons
1553 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1554 !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1555 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1558 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1559 if ((png_ptr->transformations & PNG_BACKGROUND) &&
1560 ((png_ptr->num_trans != 0) ||
1561 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
1562 png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
1563 &(png_ptr->trans_color), &(png_ptr->background)
1564 #ifdef PNG_READ_GAMMA_SUPPORTED
1565 , &(png_ptr->background_1),
1566 png_ptr->gamma_table, png_ptr->gamma_from_1,
1567 png_ptr->gamma_to_1, png_ptr->gamma_16_table,
1568 png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
1569 png_ptr->gamma_shift
1574 #ifdef PNG_READ_GAMMA_SUPPORTED
1575 if ((png_ptr->transformations & PNG_GAMMA) &&
1576 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1577 !((png_ptr->transformations & PNG_BACKGROUND) &&
1578 ((png_ptr->num_trans != 0) ||
1579 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
1581 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
1582 png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
1583 png_ptr->gamma_table, png_ptr->gamma_16_table,
1584 png_ptr->gamma_shift);
1587 #ifdef PNG_READ_16_TO_8_SUPPORTED
1588 if (png_ptr->transformations & PNG_16_TO_8)
1589 png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
1592 #ifdef PNG_READ_QUANTIZE_SUPPORTED
1593 if (png_ptr->transformations & PNG_QUANTIZE)
1595 png_do_quantize(&(png_ptr->row_info), png_ptr->row_buf + 1,
1596 png_ptr->palette_lookup, png_ptr->quantize_index);
1598 if (png_ptr->row_info.rowbytes == 0)
1599 png_error(png_ptr, "png_do_quantize returned rowbytes=0");
1601 #endif /* PNG_READ_QUANTIZE_SUPPORTED */
1603 #ifdef PNG_READ_EXPAND_16_SUPPORTED
1604 /* Do the expansion now, after all the arithmetic has been done. Notice
1605 * that previous transformations can handle the PNG_EXPAND_16 flag if this
1606 * is efficient (particularly true in the case of gamma correction, where
1607 * better accuracy results faster!)
1609 if (png_ptr->transformations & PNG_EXPAND_16)
1610 png_do_expand_16(&png_ptr->row_info, png_ptr->row_buf + 1);
1613 #ifdef PNG_READ_INVERT_SUPPORTED
1614 if (png_ptr->transformations & PNG_INVERT_MONO)
1615 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
1618 #ifdef PNG_READ_SHIFT_SUPPORTED
1619 if (png_ptr->transformations & PNG_SHIFT)
1620 png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
1624 #ifdef PNG_READ_PACK_SUPPORTED
1625 if (png_ptr->transformations & PNG_PACK)
1626 png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
1629 #ifdef PNG_READ_BGR_SUPPORTED
1630 if (png_ptr->transformations & PNG_BGR)
1631 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
1634 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1635 if (png_ptr->transformations & PNG_PACKSWAP)
1636 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1639 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1640 /*NOTE: this must be in the wrong place - what happens if BGR is set too?
1641 * Need pngvalid to test this combo.
1643 /* If gray -> RGB, do so now only if we did not do so above */
1644 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1645 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1646 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1649 #ifdef PNG_READ_FILLER_SUPPORTED
1650 if (png_ptr->transformations & PNG_FILLER)
1651 png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1652 (png_uint_32)png_ptr->filler, png_ptr->flags);
1655 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1656 if (png_ptr->transformations & PNG_INVERT_ALPHA)
1657 png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1660 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1661 if (png_ptr->transformations & PNG_SWAP_ALPHA)
1662 png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1665 #ifdef PNG_READ_16BIT_SUPPORTED
1666 #ifdef PNG_READ_SWAP_SUPPORTED
1667 if (png_ptr->transformations & PNG_SWAP_BYTES)
1668 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1672 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1673 if (png_ptr->transformations & PNG_USER_TRANSFORM)
1675 if (png_ptr->read_user_transform_fn != NULL)
1676 (*(png_ptr->read_user_transform_fn)) /* User read transform function */
1677 (png_ptr, /* png_ptr */
1678 &(png_ptr->row_info), /* row_info: */
1679 /* png_uint_32 width; width of row */
1680 /* png_size_t rowbytes; number of bytes in row */
1681 /* png_byte color_type; color type of pixels */
1682 /* png_byte bit_depth; bit depth of samples */
1683 /* png_byte channels; number of channels (1-4) */
1684 /* png_byte pixel_depth; bits per pixel (depth*channels) */
1685 png_ptr->row_buf + 1); /* start of pixel data for row */
1686 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
1687 if (png_ptr->user_transform_depth)
1688 png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
1690 if (png_ptr->user_transform_channels)
1691 png_ptr->row_info.channels = png_ptr->user_transform_channels;
1693 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
1694 png_ptr->row_info.channels);
1696 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
1697 png_ptr->row_info.width);
1703 #ifdef PNG_READ_PACK_SUPPORTED
1704 /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
1705 * without changing the actual values. Thus, if you had a row with
1706 * a bit depth of 1, you would end up with bytes that only contained
1707 * the numbers 0 or 1. If you would rather they contain 0 and 255, use
1708 * png_do_shift() after this.
1711 png_do_unpack(png_row_infop row_info, png_bytep row)
1713 png_debug(1, "in png_do_unpack");
1715 if (row_info->bit_depth < 8)
1718 png_uint_32 row_width=row_info->width;
1720 switch (row_info->bit_depth)
1724 png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
1725 png_bytep dp = row + (png_size_t)row_width - 1;
1726 png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
1727 for (i = 0; i < row_width; i++)
1729 *dp = (png_byte)((*sp >> shift) & 0x01);
1748 png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
1749 png_bytep dp = row + (png_size_t)row_width - 1;
1750 png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
1751 for (i = 0; i < row_width; i++)
1753 *dp = (png_byte)((*sp >> shift) & 0x03);
1771 png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
1772 png_bytep dp = row + (png_size_t)row_width - 1;
1773 png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
1774 for (i = 0; i < row_width; i++)
1776 *dp = (png_byte)((*sp >> shift) & 0x0f);
1795 row_info->bit_depth = 8;
1796 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1797 row_info->rowbytes = row_width * row_info->channels;
1802 #ifdef PNG_READ_SHIFT_SUPPORTED
1803 /* Reverse the effects of png_do_shift. This routine merely shifts the
1804 * pixels back to their significant bits values. Thus, if you have
1805 * a row of bit depth 8, but only 5 are significant, this will shift
1806 * the values back to 0 through 31.
1809 png_do_unshift(png_row_infop row_info, png_bytep row,
1810 png_const_color_8p sig_bits)
1812 png_debug(1, "in png_do_unshift");
1815 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
1820 png_uint_16 value = 0;
1821 png_uint_32 row_width = row_info->width;
1823 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
1825 shift[channels++] = row_info->bit_depth - sig_bits->red;
1826 shift[channels++] = row_info->bit_depth - sig_bits->green;
1827 shift[channels++] = row_info->bit_depth - sig_bits->blue;
1832 shift[channels++] = row_info->bit_depth - sig_bits->gray;
1835 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
1837 shift[channels++] = row_info->bit_depth - sig_bits->alpha;
1840 for (c = 0; c < channels; c++)
1852 switch (row_info->bit_depth)
1861 png_size_t istop = row_info->rowbytes;
1863 for (bp = row, i = 0; i < istop; i++)
1875 png_size_t istop = row_info->rowbytes;
1876 png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
1877 (png_byte)((int)0xf >> shift[0]));
1879 for (i = 0; i < istop; i++)
1891 png_uint_32 istop = row_width * channels;
1893 for (i = 0; i < istop; i++)
1895 *bp++ >>= shift[i%channels];
1900 #ifdef PNG_READ_16BIT_SUPPORTED
1905 png_uint_32 istop = channels * row_width;
1907 for (i = 0; i < istop; i++)
1909 value = (png_uint_16)((*bp << 8) + *(bp + 1));
1910 value >>= shift[i%channels];
1911 *bp++ = (png_byte)(value >> 8);
1912 *bp++ = (png_byte)(value & 0xff);
1922 #ifdef PNG_READ_16_TO_8_SUPPORTED
1923 /* Chop rows of bit depth 16 down to 8 */
1925 png_do_chop(png_row_infop row_info, png_bytep row)
1927 png_debug(1, "in png_do_chop");
1929 if (row_info->bit_depth == 16)
1934 png_uint_32 istop = row_info->width * row_info->channels;
1936 for (i = 0; i<istop; i++, sp += 2, dp++)
1938 #ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
1939 /* This does a more accurate scaling of the 16-bit color
1940 * value, rather than a simple low-byte truncation.
1942 * What the ideal calculation should be:
1943 * *dp = (((((png_uint_32)(*sp) << 8) |
1944 * (png_uint_32)(*(sp + 1))) * 255 + 127)
1945 * / (png_uint_32)65535L;
1947 * GRR: no, I think this is what it really should be:
1948 * *dp = (((((png_uint_32)(*sp) << 8) |
1949 * (png_uint_32)(*(sp + 1))) + 128L)
1950 * / (png_uint_32)257L;
1952 * GRR: here's the exact calculation with shifts:
1953 * temp = (((png_uint_32)(*sp) << 8) |
1954 * (png_uint_32)(*(sp + 1))) + 128L;
1955 * *dp = (temp - (temp >> 8)) >> 8;
1957 * Approximate calculation with shift/add instead of multiply/divide:
1958 * *dp = ((((png_uint_32)(*sp) << 8) |
1959 * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
1961 * What we actually do to avoid extra shifting and conversion:
1964 *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
1966 /* Simply discard the low order byte */
1970 row_info->bit_depth = 8;
1971 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1972 row_info->rowbytes = row_info->width * row_info->channels;
1977 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1979 png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
1981 png_debug(1, "in png_do_read_swap_alpha");
1984 png_uint_32 row_width = row_info->width;
1985 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1987 /* This converts from RGBA to ARGB */
1988 if (row_info->bit_depth == 8)
1990 png_bytep sp = row + row_info->rowbytes;
1995 for (i = 0; i < row_width; i++)
2005 #ifdef PNG_READ_16BIT_SUPPORTED
2006 /* This converts from RRGGBBAA to AARRGGBB */
2009 png_bytep sp = row + row_info->rowbytes;
2014 for (i = 0; i < row_width; i++)
2031 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2033 /* This converts from GA to AG */
2034 if (row_info->bit_depth == 8)
2036 png_bytep sp = row + row_info->rowbytes;
2041 for (i = 0; i < row_width; i++)
2049 #ifdef PNG_READ_16BIT_SUPPORTED
2050 /* This converts from GGAA to AAGG */
2053 png_bytep sp = row + row_info->rowbytes;
2058 for (i = 0; i < row_width; i++)
2074 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
2076 png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
2078 png_uint_32 row_width;
2079 png_debug(1, "in png_do_read_invert_alpha");
2081 row_width = row_info->width;
2082 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2084 if (row_info->bit_depth == 8)
2086 /* This inverts the alpha channel in RGBA */
2087 png_bytep sp = row + row_info->rowbytes;
2091 for (i = 0; i < row_width; i++)
2093 *(--dp) = (png_byte)(255 - *(--sp));
2095 /* This does nothing:
2099 We can replace it with:
2106 #ifdef PNG_READ_16BIT_SUPPORTED
2107 /* This inverts the alpha channel in RRGGBBAA */
2110 png_bytep sp = row + row_info->rowbytes;
2114 for (i = 0; i < row_width; i++)
2116 *(--dp) = (png_byte)(255 - *(--sp));
2117 *(--dp) = (png_byte)(255 - *(--sp));
2119 /* This does nothing:
2126 We can replace it with:
2134 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2136 if (row_info->bit_depth == 8)
2138 /* This inverts the alpha channel in GA */
2139 png_bytep sp = row + row_info->rowbytes;
2143 for (i = 0; i < row_width; i++)
2145 *(--dp) = (png_byte)(255 - *(--sp));
2150 #ifdef PNG_READ_16BIT_SUPPORTED
2153 /* This inverts the alpha channel in GGAA */
2154 png_bytep sp = row + row_info->rowbytes;
2158 for (i = 0; i < row_width; i++)
2160 *(--dp) = (png_byte)(255 - *(--sp));
2161 *(--dp) = (png_byte)(255 - *(--sp));
2175 #ifdef PNG_READ_FILLER_SUPPORTED
2176 /* Add filler channel if we have RGB color */
2178 png_do_read_filler(png_row_infop row_info, png_bytep row,
2179 png_uint_32 filler, png_uint_32 flags)
2182 png_uint_32 row_width = row_info->width;
2184 #ifdef PNG_READ_16BIT_SUPPORTED
2185 png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
2187 png_byte lo_filler = (png_byte)(filler & 0xff);
2189 png_debug(1, "in png_do_read_filler");
2192 row_info->color_type == PNG_COLOR_TYPE_GRAY)
2194 if (row_info->bit_depth == 8)
2196 if (flags & PNG_FLAG_FILLER_AFTER)
2198 /* This changes the data from G to GX */
2199 png_bytep sp = row + (png_size_t)row_width;
2200 png_bytep dp = sp + (png_size_t)row_width;
2201 for (i = 1; i < row_width; i++)
2203 *(--dp) = lo_filler;
2206 *(--dp) = lo_filler;
2207 row_info->channels = 2;
2208 row_info->pixel_depth = 16;
2209 row_info->rowbytes = row_width * 2;
2214 /* This changes the data from G to XG */
2215 png_bytep sp = row + (png_size_t)row_width;
2216 png_bytep dp = sp + (png_size_t)row_width;
2217 for (i = 0; i < row_width; i++)
2220 *(--dp) = lo_filler;
2222 row_info->channels = 2;
2223 row_info->pixel_depth = 16;
2224 row_info->rowbytes = row_width * 2;
2228 #ifdef PNG_READ_16BIT_SUPPORTED
2229 else if (row_info->bit_depth == 16)
2231 if (flags & PNG_FLAG_FILLER_AFTER)
2233 /* This changes the data from GG to GGXX */
2234 png_bytep sp = row + (png_size_t)row_width * 2;
2235 png_bytep dp = sp + (png_size_t)row_width * 2;
2236 for (i = 1; i < row_width; i++)
2238 *(--dp) = hi_filler;
2239 *(--dp) = lo_filler;
2243 *(--dp) = hi_filler;
2244 *(--dp) = lo_filler;
2245 row_info->channels = 2;
2246 row_info->pixel_depth = 32;
2247 row_info->rowbytes = row_width * 4;
2252 /* This changes the data from GG to XXGG */
2253 png_bytep sp = row + (png_size_t)row_width * 2;
2254 png_bytep dp = sp + (png_size_t)row_width * 2;
2255 for (i = 0; i < row_width; i++)
2259 *(--dp) = hi_filler;
2260 *(--dp) = lo_filler;
2262 row_info->channels = 2;
2263 row_info->pixel_depth = 32;
2264 row_info->rowbytes = row_width * 4;
2268 } /* COLOR_TYPE == GRAY */
2269 else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2271 if (row_info->bit_depth == 8)
2273 if (flags & PNG_FLAG_FILLER_AFTER)
2275 /* This changes the data from RGB to RGBX */
2276 png_bytep sp = row + (png_size_t)row_width * 3;
2277 png_bytep dp = sp + (png_size_t)row_width;
2278 for (i = 1; i < row_width; i++)
2280 *(--dp) = lo_filler;
2285 *(--dp) = lo_filler;
2286 row_info->channels = 4;
2287 row_info->pixel_depth = 32;
2288 row_info->rowbytes = row_width * 4;
2293 /* This changes the data from RGB to XRGB */
2294 png_bytep sp = row + (png_size_t)row_width * 3;
2295 png_bytep dp = sp + (png_size_t)row_width;
2296 for (i = 0; i < row_width; i++)
2301 *(--dp) = lo_filler;
2303 row_info->channels = 4;
2304 row_info->pixel_depth = 32;
2305 row_info->rowbytes = row_width * 4;
2309 #ifdef PNG_READ_16BIT_SUPPORTED
2310 else if (row_info->bit_depth == 16)
2312 if (flags & PNG_FLAG_FILLER_AFTER)
2314 /* This changes the data from RRGGBB to RRGGBBXX */
2315 png_bytep sp = row + (png_size_t)row_width * 6;
2316 png_bytep dp = sp + (png_size_t)row_width * 2;
2317 for (i = 1; i < row_width; i++)
2319 *(--dp) = hi_filler;
2320 *(--dp) = lo_filler;
2328 *(--dp) = hi_filler;
2329 *(--dp) = lo_filler;
2330 row_info->channels = 4;
2331 row_info->pixel_depth = 64;
2332 row_info->rowbytes = row_width * 8;
2337 /* This changes the data from RRGGBB to XXRRGGBB */
2338 png_bytep sp = row + (png_size_t)row_width * 6;
2339 png_bytep dp = sp + (png_size_t)row_width * 2;
2340 for (i = 0; i < row_width; i++)
2348 *(--dp) = hi_filler;
2349 *(--dp) = lo_filler;
2352 row_info->channels = 4;
2353 row_info->pixel_depth = 64;
2354 row_info->rowbytes = row_width * 8;
2358 } /* COLOR_TYPE == RGB */
2362 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2363 /* Expand grayscale files to RGB, with or without alpha */
2365 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
2368 png_uint_32 row_width = row_info->width;
2370 png_debug(1, "in png_do_gray_to_rgb");
2372 if (row_info->bit_depth >= 8 &&
2373 !(row_info->color_type & PNG_COLOR_MASK_COLOR))
2375 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
2377 if (row_info->bit_depth == 8)
2379 /* This changes G to RGB */
2380 png_bytep sp = row + (png_size_t)row_width - 1;
2381 png_bytep dp = sp + (png_size_t)row_width * 2;
2382 for (i = 0; i < row_width; i++)
2392 /* This changes GG to RRGGBB */
2393 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2394 png_bytep dp = sp + (png_size_t)row_width * 4;
2395 for (i = 0; i < row_width; i++)
2398 *(dp--) = *(sp - 1);
2400 *(dp--) = *(sp - 1);
2407 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2409 if (row_info->bit_depth == 8)
2411 /* This changes GA to RGBA */
2412 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2413 png_bytep dp = sp + (png_size_t)row_width * 2;
2414 for (i = 0; i < row_width; i++)
2425 /* This changes GGAA to RRGGBBAA */
2426 png_bytep sp = row + (png_size_t)row_width * 4 - 1;
2427 png_bytep dp = sp + (png_size_t)row_width * 4;
2428 for (i = 0; i < row_width; i++)
2433 *(dp--) = *(sp - 1);
2435 *(dp--) = *(sp - 1);
2441 row_info->channels += (png_byte)2;
2442 row_info->color_type |= PNG_COLOR_MASK_COLOR;
2443 row_info->pixel_depth = (png_byte)(row_info->channels *
2444 row_info->bit_depth);
2445 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2450 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2451 /* Reduce RGB files to grayscale, with or without alpha
2452 * using the equation given in Poynton's ColorFAQ at
2453 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008)
2455 * <http://www.poynton.com/notes/colour_and_gamma/>
2456 * Charles Poynton poynton at poynton.com
2458 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2460 * We approximate this with
2462 * Y = 0.21268 * R + 0.7151 * G + 0.07217 * B
2464 * which can be expressed with integers as
2466 * Y = (6969 * R + 23434 * G + 2365 * B)/32768
2468 * The calculation is to be done in a linear colorspace.
2470 * Other integer coefficents can be used via png_set_rgb_to_gray().
2473 png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
2478 png_uint_32 row_width = row_info->width;
2481 png_debug(1, "in png_do_rgb_to_gray");
2483 if (!(row_info->color_type & PNG_COLOR_MASK_PALETTE) &&
2484 (row_info->color_type & PNG_COLOR_MASK_COLOR))
2486 png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
2487 png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
2488 png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
2490 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2492 if (row_info->bit_depth == 8)
2494 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2495 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
2500 for (i = 0; i < row_width; i++)
2502 png_byte red = png_ptr->gamma_to_1[*(sp++)];
2503 png_byte green = png_ptr->gamma_to_1[*(sp++)];
2504 png_byte blue = png_ptr->gamma_to_1[*(sp++)];
2506 if (red != green || red != blue)
2509 *(dp++) = png_ptr->gamma_from_1[
2510 (rc*red + gc*green + bc*blue)>>15];
2514 *(dp++) = *(sp - 1);
2522 for (i = 0; i < row_width; i++)
2524 png_byte red = *(sp++);
2525 png_byte green = *(sp++);
2526 png_byte blue = *(sp++);
2528 if (red != green || red != blue)
2531 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
2535 *(dp++) = *(sp - 1);
2540 else /* RGB bit_depth == 16 */
2542 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2543 if (png_ptr->gamma_16_to_1 != NULL &&
2544 png_ptr->gamma_16_from_1 != NULL)
2548 for (i = 0; i < row_width; i++)
2550 png_uint_16 red, green, blue, w;
2552 red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2553 green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2554 blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2556 if (red == green && red == blue)
2561 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff)
2562 >> png_ptr->gamma_shift][red>>8];
2563 png_uint_16 green_1 =
2564 png_ptr->gamma_16_to_1[(green&0xff) >>
2565 png_ptr->gamma_shift][green>>8];
2566 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff)
2567 >> png_ptr->gamma_shift][blue>>8];
2568 png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1
2570 w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
2571 png_ptr->gamma_shift][gray16 >> 8];
2575 *(dp++) = (png_byte)((w>>8) & 0xff);
2576 *(dp++) = (png_byte)(w & 0xff);
2584 for (i = 0; i < row_width; i++)
2586 png_uint_16 red, green, blue, gray16;
2588 red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2589 green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2590 blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2592 if (red != green || red != blue)
2595 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2596 *(dp++) = (png_byte)((gray16>>8) & 0xff);
2597 *(dp++) = (png_byte)(gray16 & 0xff);
2602 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2604 if (row_info->bit_depth == 8)
2606 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2607 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
2611 for (i = 0; i < row_width; i++)
2613 png_byte red = png_ptr->gamma_to_1[*(sp++)];
2614 png_byte green = png_ptr->gamma_to_1[*(sp++)];
2615 png_byte blue = png_ptr->gamma_to_1[*(sp++)];
2617 if (red != green || red != blue)
2620 *(dp++) = png_ptr->gamma_from_1
2621 [(rc*red + gc*green + bc*blue)>>15];
2623 *(dp++) = *(sp++); /* alpha */
2631 for (i = 0; i < row_width; i++)
2633 png_byte red = *(sp++);
2634 png_byte green = *(sp++);
2635 png_byte blue = *(sp++);
2636 if (red != green || red != blue)
2639 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
2640 *(dp++) = *(sp++); /* alpha */
2644 else /* RGBA bit_depth == 16 */
2646 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2647 if (png_ptr->gamma_16_to_1 != NULL &&
2648 png_ptr->gamma_16_from_1 != NULL)
2652 for (i = 0; i < row_width; i++)
2654 png_uint_16 red, green, blue, w;
2656 red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2657 green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2658 blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2660 if (red == green && red == blue)
2665 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >>
2666 png_ptr->gamma_shift][red>>8];
2668 png_uint_16 green_1 =
2669 png_ptr->gamma_16_to_1[(green&0xff) >>
2670 png_ptr->gamma_shift][green>>8];
2672 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >>
2673 png_ptr->gamma_shift][blue>>8];
2675 png_uint_16 gray16 = (png_uint_16)((rc * red_1
2676 + gc * green_1 + bc * blue_1)>>15);
2678 w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
2679 png_ptr->gamma_shift][gray16 >> 8];
2684 *(dp++) = (png_byte)((w>>8) & 0xff);
2685 *(dp++) = (png_byte)(w & 0xff);
2686 *(dp++) = *(sp++); /* alpha */
2695 for (i = 0; i < row_width; i++)
2697 png_uint_16 red, green, blue, gray16;
2698 red = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
2699 green = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
2700 blue = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
2702 if (red != green || red != blue)
2705 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2706 *(dp++) = (png_byte)((gray16>>8) & 0xff);
2707 *(dp++) = (png_byte)(gray16 & 0xff);
2708 *(dp++) = *(sp++); /* alpha */
2714 row_info->channels -= 2;
2715 row_info->color_type = (png_byte)(row_info->color_type &
2716 ~PNG_COLOR_MASK_COLOR);
2717 row_info->pixel_depth = (png_byte)(row_info->channels *
2718 row_info->bit_depth);
2719 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2725 /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
2726 * large of png_color. This lets grayscale images be treated as
2727 * paletted. Most useful for gamma correction and simplification
2731 png_build_grayscale_palette(int bit_depth, png_colorp palette)
2738 png_debug(1, "in png_do_build_grayscale_palette");
2740 if (palette == NULL)
2771 for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
2773 palette[i].red = (png_byte)v;
2774 palette[i].green = (png_byte)v;
2775 palette[i].blue = (png_byte)v;
2780 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2781 /* Replace any alpha or transparency with the supplied background color.
2782 * "background" is already in the screen gamma, while "background_1" is
2783 * at a gamma of 1.0. Paletted files have already been taken care of.
2786 png_do_background(png_row_infop row_info, png_bytep row,
2787 png_const_color_16p trans_color, png_const_color_16p background
2788 #ifdef PNG_READ_GAMMA_SUPPORTED
2789 , png_const_color_16p background_1, png_const_bytep gamma_table,
2790 png_const_bytep gamma_from_1, png_const_bytep gamma_to_1,
2791 png_const_uint_16pp gamma_16, png_const_uint_16pp gamma_16_from_1,
2792 png_const_uint_16pp gamma_16_to_1, int gamma_shift
2798 png_uint_32 row_width = row_info->width;
2801 png_debug(1, "in png_do_background");
2803 if (background != NULL &&
2804 (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
2805 (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_color)))
2807 switch (row_info->color_type)
2809 case PNG_COLOR_TYPE_GRAY:
2811 switch (row_info->bit_depth)
2817 for (i = 0; i < row_width; i++)
2819 if ((png_uint_16)((*sp >> shift) & 0x01)
2820 == trans_color->gray)
2822 *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
2823 *sp |= (png_byte)(background->gray << shift);
2840 #ifdef PNG_READ_GAMMA_SUPPORTED
2841 if (gamma_table != NULL)
2845 for (i = 0; i < row_width; i++)
2847 if ((png_uint_16)((*sp >> shift) & 0x03)
2848 == trans_color->gray)
2850 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2851 *sp |= (png_byte)(background->gray << shift);
2856 png_byte p = (png_byte)((*sp >> shift) & 0x03);
2857 png_byte g = (png_byte)((gamma_table [p | (p << 2) |
2858 (p << 4) | (p << 6)] >> 6) & 0x03);
2859 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2860 *sp |= (png_byte)(g << shift);
2879 for (i = 0; i < row_width; i++)
2881 if ((png_uint_16)((*sp >> shift) & 0x03)
2882 == trans_color->gray)
2884 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2885 *sp |= (png_byte)(background->gray << shift);
2903 #ifdef PNG_READ_GAMMA_SUPPORTED
2904 if (gamma_table != NULL)
2908 for (i = 0; i < row_width; i++)
2910 if ((png_uint_16)((*sp >> shift) & 0x0f)
2911 == trans_color->gray)
2913 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2914 *sp |= (png_byte)(background->gray << shift);
2919 png_byte p = (png_byte)((*sp >> shift) & 0x0f);
2920 png_byte g = (png_byte)((gamma_table[p |
2921 (p << 4)] >> 4) & 0x0f);
2922 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2923 *sp |= (png_byte)(g << shift);
2942 for (i = 0; i < row_width; i++)
2944 if ((png_uint_16)((*sp >> shift) & 0x0f)
2945 == trans_color->gray)
2947 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2948 *sp |= (png_byte)(background->gray << shift);
2966 #ifdef PNG_READ_GAMMA_SUPPORTED
2967 if (gamma_table != NULL)
2970 for (i = 0; i < row_width; i++, sp++)
2972 if (*sp == trans_color->gray)
2973 *sp = (png_byte)background->gray;
2976 *sp = gamma_table[*sp];
2983 for (i = 0; i < row_width; i++, sp++)
2985 if (*sp == trans_color->gray)
2986 *sp = (png_byte)background->gray;
2994 #ifdef PNG_READ_GAMMA_SUPPORTED
2995 if (gamma_16 != NULL)
2998 for (i = 0; i < row_width; i++, sp += 2)
3002 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3004 if (v == trans_color->gray)
3006 /* Background is already in screen gamma */
3007 *sp = (png_byte)((background->gray >> 8) & 0xff);
3008 *(sp + 1) = (png_byte)(background->gray & 0xff);
3013 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3014 *sp = (png_byte)((v >> 8) & 0xff);
3015 *(sp + 1) = (png_byte)(v & 0xff);
3023 for (i = 0; i < row_width; i++, sp += 2)
3027 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3029 if (v == trans_color->gray)
3031 *sp = (png_byte)((background->gray >> 8) & 0xff);
3032 *(sp + 1) = (png_byte)(background->gray & 0xff);
3045 case PNG_COLOR_TYPE_RGB:
3047 if (row_info->bit_depth == 8)
3049 #ifdef PNG_READ_GAMMA_SUPPORTED
3050 if (gamma_table != NULL)
3053 for (i = 0; i < row_width; i++, sp += 3)
3055 if (*sp == trans_color->red &&
3056 *(sp + 1) == trans_color->green &&
3057 *(sp + 2) == trans_color->blue)
3059 *sp = (png_byte)background->red;
3060 *(sp + 1) = (png_byte)background->green;
3061 *(sp + 2) = (png_byte)background->blue;
3066 *sp = gamma_table[*sp];
3067 *(sp + 1) = gamma_table[*(sp + 1)];
3068 *(sp + 2) = gamma_table[*(sp + 2)];
3076 for (i = 0; i < row_width; i++, sp += 3)
3078 if (*sp == trans_color->red &&
3079 *(sp + 1) == trans_color->green &&
3080 *(sp + 2) == trans_color->blue)
3082 *sp = (png_byte)background->red;
3083 *(sp + 1) = (png_byte)background->green;
3084 *(sp + 2) = (png_byte)background->blue;
3089 else /* if (row_info->bit_depth == 16) */
3091 #ifdef PNG_READ_GAMMA_SUPPORTED
3092 if (gamma_16 != NULL)
3095 for (i = 0; i < row_width; i++, sp += 6)
3097 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3099 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3102 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3105 if (r == trans_color->red && g == trans_color->green &&
3106 b == trans_color->blue)
3108 /* Background is already in screen gamma */
3109 *sp = (png_byte)((background->red >> 8) & 0xff);
3110 *(sp + 1) = (png_byte)(background->red & 0xff);
3111 *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3112 *(sp + 3) = (png_byte)(background->green & 0xff);
3113 *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3114 *(sp + 5) = (png_byte)(background->blue & 0xff);
3119 png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3120 *sp = (png_byte)((v >> 8) & 0xff);
3121 *(sp + 1) = (png_byte)(v & 0xff);
3123 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3124 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3125 *(sp + 3) = (png_byte)(v & 0xff);
3127 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3128 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3129 *(sp + 5) = (png_byte)(v & 0xff);
3138 for (i = 0; i < row_width; i++, sp += 6)
3140 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3142 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3145 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3148 if (r == trans_color->red && g == trans_color->green &&
3149 b == trans_color->blue)
3151 *sp = (png_byte)((background->red >> 8) & 0xff);
3152 *(sp + 1) = (png_byte)(background->red & 0xff);
3153 *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3154 *(sp + 3) = (png_byte)(background->green & 0xff);
3155 *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3156 *(sp + 5) = (png_byte)(background->blue & 0xff);
3164 case PNG_COLOR_TYPE_GRAY_ALPHA:
3166 if (row_info->bit_depth == 8)
3168 #ifdef PNG_READ_GAMMA_SUPPORTED
3169 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3170 gamma_table != NULL)
3174 for (i = 0; i < row_width; i++, sp += 2, dp++)
3176 png_uint_16 a = *(sp + 1);
3179 *dp = gamma_table[*sp];
3183 /* Background is already in screen gamma */
3184 *dp = (png_byte)background->gray;
3191 v = gamma_to_1[*sp];
3192 png_composite(w, v, a, background_1->gray);
3193 *dp = gamma_from_1[w];
3202 for (i = 0; i < row_width; i++, sp += 2, dp++)
3204 png_byte a = *(sp + 1);
3209 #ifdef PNG_READ_GAMMA_SUPPORTED
3211 *dp = (png_byte)background->gray;
3214 png_composite(*dp, *sp, a, background_1->gray);
3217 *dp = (png_byte)background->gray;
3222 else /* if (png_ptr->bit_depth == 16) */
3224 #ifdef PNG_READ_GAMMA_SUPPORTED
3225 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3226 gamma_16_to_1 != NULL)
3230 for (i = 0; i < row_width; i++, sp += 4, dp += 2)
3232 png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3235 if (a == (png_uint_16)0xffff)
3239 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3240 *dp = (png_byte)((v >> 8) & 0xff);
3241 *(dp + 1) = (png_byte)(v & 0xff);
3244 #ifdef PNG_READ_GAMMA_SUPPORTED
3250 /* Background is already in screen gamma */
3251 *dp = (png_byte)((background->gray >> 8) & 0xff);
3252 *(dp + 1) = (png_byte)(background->gray & 0xff);
3255 #ifdef PNG_READ_GAMMA_SUPPORTED
3258 png_uint_16 g, v, w;
3260 g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3261 png_composite_16(v, g, a, background_1->gray);
3262 w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
3263 *dp = (png_byte)((w >> 8) & 0xff);
3264 *(dp + 1) = (png_byte)(w & 0xff);
3274 for (i = 0; i < row_width; i++, sp += 4, dp += 2)
3276 png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3279 if (a == (png_uint_16)0xffff)
3280 png_memcpy(dp, sp, 2);
3282 #ifdef PNG_READ_GAMMA_SUPPORTED
3288 *dp = (png_byte)((background->gray >> 8) & 0xff);
3289 *(dp + 1) = (png_byte)(background->gray & 0xff);
3292 #ifdef PNG_READ_GAMMA_SUPPORTED
3297 g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3298 png_composite_16(v, g, a, background_1->gray);
3299 *dp = (png_byte)((v >> 8) & 0xff);
3300 *(dp + 1) = (png_byte)(v & 0xff);
3309 case PNG_COLOR_TYPE_RGB_ALPHA:
3311 if (row_info->bit_depth == 8)
3313 #ifdef PNG_READ_GAMMA_SUPPORTED
3314 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3315 gamma_table != NULL)
3319 for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3321 png_byte a = *(sp + 3);
3325 *dp = gamma_table[*sp];
3326 *(dp + 1) = gamma_table[*(sp + 1)];
3327 *(dp + 2) = gamma_table[*(sp + 2)];
3332 /* Background is already in screen gamma */
3333 *dp = (png_byte)background->red;
3334 *(dp + 1) = (png_byte)background->green;
3335 *(dp + 2) = (png_byte)background->blue;
3342 v = gamma_to_1[*sp];
3343 png_composite(w, v, a, background_1->red);
3344 *dp = gamma_from_1[w];
3346 v = gamma_to_1[*(sp + 1)];
3347 png_composite(w, v, a, background_1->green);
3348 *(dp + 1) = gamma_from_1[w];
3350 v = gamma_to_1[*(sp + 2)];
3351 png_composite(w, v, a, background_1->blue);
3352 *(dp + 2) = gamma_from_1[w];
3361 for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3363 png_byte a = *(sp + 3);
3368 *(dp + 1) = *(sp + 1);
3369 *(dp + 2) = *(sp + 2);
3374 *dp = (png_byte)background->red;
3375 *(dp + 1) = (png_byte)background->green;
3376 *(dp + 2) = (png_byte)background->blue;
3381 png_composite(*dp, *sp, a, background->red);
3383 png_composite(*(dp + 1), *(sp + 1), a,
3386 png_composite(*(dp + 2), *(sp + 2), a,
3392 else /* if (row_info->bit_depth == 16) */
3394 #ifdef PNG_READ_GAMMA_SUPPORTED
3395 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3396 gamma_16_to_1 != NULL)
3400 for (i = 0; i < row_width; i++, sp += 8, dp += 6)
3402 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3403 << 8) + (png_uint_16)(*(sp + 7)));
3405 if (a == (png_uint_16)0xffff)
3409 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3410 *dp = (png_byte)((v >> 8) & 0xff);
3411 *(dp + 1) = (png_byte)(v & 0xff);
3413 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3414 *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3415 *(dp + 3) = (png_byte)(v & 0xff);
3417 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3418 *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3419 *(dp + 5) = (png_byte)(v & 0xff);
3424 /* Background is already in screen gamma */
3425 *dp = (png_byte)((background->red >> 8) & 0xff);
3426 *(dp + 1) = (png_byte)(background->red & 0xff);
3427 *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3428 *(dp + 3) = (png_byte)(background->green & 0xff);
3429 *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3430 *(dp + 5) = (png_byte)(background->blue & 0xff);
3435 png_uint_16 v, w, x;
3437 v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3438 png_composite_16(w, v, a, background_1->red);
3440 x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
3441 *dp = (png_byte)((x >> 8) & 0xff);
3442 *(dp + 1) = (png_byte)(x & 0xff);
3444 v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
3445 png_composite_16(w, v, a, background_1->green);
3447 x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
3448 *(dp + 2) = (png_byte)((x >> 8) & 0xff);
3449 *(dp + 3) = (png_byte)(x & 0xff);
3451 v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
3452 png_composite_16(w, v, a, background_1->blue);
3454 x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
3455 *(dp + 4) = (png_byte)((x >> 8) & 0xff);
3456 *(dp + 5) = (png_byte)(x & 0xff);
3466 for (i = 0; i < row_width; i++, sp += 8, dp += 6)
3468 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3469 << 8) + (png_uint_16)(*(sp + 7)));
3471 if (a == (png_uint_16)0xffff)
3473 png_memcpy(dp, sp, 6);
3478 *dp = (png_byte)((background->red >> 8) & 0xff);
3479 *(dp + 1) = (png_byte)(background->red & 0xff);
3480 *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3481 *(dp + 3) = (png_byte)(background->green & 0xff);
3482 *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3483 *(dp + 5) = (png_byte)(background->blue & 0xff);
3490 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3491 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3493 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3496 png_composite_16(v, r, a, background->red);
3497 *dp = (png_byte)((v >> 8) & 0xff);
3498 *(dp + 1) = (png_byte)(v & 0xff);
3500 png_composite_16(v, g, a, background->green);
3501 *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3502 *(dp + 3) = (png_byte)(v & 0xff);
3504 png_composite_16(v, b, a, background->blue);
3505 *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3506 *(dp + 5) = (png_byte)(v & 0xff);
3518 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
3520 row_info->color_type = (png_byte)(row_info->color_type &
3521 ~PNG_COLOR_MASK_ALPHA);
3522 row_info->channels--;
3523 row_info->pixel_depth = (png_byte)(row_info->channels *
3524 row_info->bit_depth);
3525 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3531 #ifdef PNG_READ_GAMMA_SUPPORTED
3532 /* Gamma correct the image, avoiding the alpha channel. Make sure
3533 * you do this after you deal with the transparency issue on grayscale
3534 * or RGB images. If your bit depth is 8, use gamma_table, if it
3535 * is 16, use gamma_16_table and gamma_shift. Build these with
3536 * build_gamma_table().
3539 png_do_gamma(png_row_infop row_info, png_bytep row,
3540 png_const_bytep gamma_table, png_const_uint_16pp gamma_16_table,
3545 png_uint_32 row_width=row_info->width;
3547 png_debug(1, "in png_do_gamma");
3549 if (((row_info->bit_depth <= 8 && gamma_table != NULL) ||
3550 (row_info->bit_depth == 16 && gamma_16_table != NULL)))
3552 switch (row_info->color_type)
3554 case PNG_COLOR_TYPE_RGB:
3556 if (row_info->bit_depth == 8)
3559 for (i = 0; i < row_width; i++)
3561 *sp = gamma_table[*sp];
3563 *sp = gamma_table[*sp];
3565 *sp = gamma_table[*sp];
3570 else /* if (row_info->bit_depth == 16) */
3573 for (i = 0; i < row_width; i++)
3577 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3578 *sp = (png_byte)((v >> 8) & 0xff);
3579 *(sp + 1) = (png_byte)(v & 0xff);
3582 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3583 *sp = (png_byte)((v >> 8) & 0xff);
3584 *(sp + 1) = (png_byte)(v & 0xff);
3587 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3588 *sp = (png_byte)((v >> 8) & 0xff);
3589 *(sp + 1) = (png_byte)(v & 0xff);
3596 case PNG_COLOR_TYPE_RGB_ALPHA:
3598 if (row_info->bit_depth == 8)
3601 for (i = 0; i < row_width; i++)
3603 *sp = gamma_table[*sp];
3606 *sp = gamma_table[*sp];
3609 *sp = gamma_table[*sp];
3616 else /* if (row_info->bit_depth == 16) */
3619 for (i = 0; i < row_width; i++)
3621 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3622 *sp = (png_byte)((v >> 8) & 0xff);
3623 *(sp + 1) = (png_byte)(v & 0xff);
3626 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3627 *sp = (png_byte)((v >> 8) & 0xff);
3628 *(sp + 1) = (png_byte)(v & 0xff);
3631 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3632 *sp = (png_byte)((v >> 8) & 0xff);
3633 *(sp + 1) = (png_byte)(v & 0xff);
3640 case PNG_COLOR_TYPE_GRAY_ALPHA:
3642 if (row_info->bit_depth == 8)
3645 for (i = 0; i < row_width; i++)
3647 *sp = gamma_table[*sp];
3652 else /* if (row_info->bit_depth == 16) */
3655 for (i = 0; i < row_width; i++)
3657 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3658 *sp = (png_byte)((v >> 8) & 0xff);
3659 *(sp + 1) = (png_byte)(v & 0xff);
3666 case PNG_COLOR_TYPE_GRAY:
3668 if (row_info->bit_depth == 2)
3671 for (i = 0; i < row_width; i += 4)
3679 ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)|
3680 ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
3681 ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
3682 ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
3687 if (row_info->bit_depth == 4)
3690 for (i = 0; i < row_width; i += 2)
3692 int msb = *sp & 0xf0;
3693 int lsb = *sp & 0x0f;
3695 *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
3696 | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
3701 else if (row_info->bit_depth == 8)
3704 for (i = 0; i < row_width; i++)
3706 *sp = gamma_table[*sp];
3711 else if (row_info->bit_depth == 16)
3714 for (i = 0; i < row_width; i++)
3716 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3717 *sp = (png_byte)((v >> 8) & 0xff);
3718 *(sp + 1) = (png_byte)(v & 0xff);
3732 #ifdef PNG_READ_EXPAND_SUPPORTED
3733 /* Expands a palette row to an RGB or RGBA row depending
3734 * upon whether you supply trans and num_trans.
3737 png_do_expand_palette(png_row_infop row_info, png_bytep row,
3738 png_const_colorp palette, png_const_bytep trans_alpha, int num_trans)
3743 png_uint_32 row_width=row_info->width;
3745 png_debug(1, "in png_do_expand_palette");
3747 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
3749 if (row_info->bit_depth < 8)
3751 switch (row_info->bit_depth)
3755 sp = row + (png_size_t)((row_width - 1) >> 3);
3756 dp = row + (png_size_t)row_width - 1;
3757 shift = 7 - (int)((row_width + 7) & 0x07);
3758 for (i = 0; i < row_width; i++)
3760 if ((*sp >> shift) & 0x01)
3782 sp = row + (png_size_t)((row_width - 1) >> 2);
3783 dp = row + (png_size_t)row_width - 1;
3784 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3785 for (i = 0; i < row_width; i++)
3787 value = (*sp >> shift) & 0x03;
3788 *dp = (png_byte)value;
3805 sp = row + (png_size_t)((row_width - 1) >> 1);
3806 dp = row + (png_size_t)row_width - 1;
3807 shift = (int)((row_width & 0x01) << 2);
3808 for (i = 0; i < row_width; i++)
3810 value = (*sp >> shift) & 0x0f;
3811 *dp = (png_byte)value;
3829 row_info->bit_depth = 8;
3830 row_info->pixel_depth = 8;
3831 row_info->rowbytes = row_width;
3834 if (row_info->bit_depth == 8)
3837 if (trans_alpha != NULL)
3839 sp = row + (png_size_t)row_width - 1;
3840 dp = row + (png_size_t)(row_width << 2) - 1;
3842 for (i = 0; i < row_width; i++)
3844 if ((int)(*sp) >= num_trans)
3848 *dp-- = trans_alpha[*sp];
3850 *dp-- = palette[*sp].blue;
3851 *dp-- = palette[*sp].green;
3852 *dp-- = palette[*sp].red;
3855 row_info->bit_depth = 8;
3856 row_info->pixel_depth = 32;
3857 row_info->rowbytes = row_width * 4;
3858 row_info->color_type = 6;
3859 row_info->channels = 4;
3864 sp = row + (png_size_t)row_width - 1;
3865 dp = row + (png_size_t)(row_width * 3) - 1;
3867 for (i = 0; i < row_width; i++)
3869 *dp-- = palette[*sp].blue;
3870 *dp-- = palette[*sp].green;
3871 *dp-- = palette[*sp].red;
3875 row_info->bit_depth = 8;
3876 row_info->pixel_depth = 24;
3877 row_info->rowbytes = row_width * 3;
3878 row_info->color_type = 2;
3879 row_info->channels = 3;
3886 /* If the bit depth < 8, it is expanded to 8. Also, if the already
3887 * expanded transparency value is supplied, an alpha channel is built.
3890 png_do_expand(png_row_infop row_info, png_bytep row,
3891 png_const_color_16p trans_value)
3896 png_uint_32 row_width=row_info->width;
3898 png_debug(1, "in png_do_expand");
3901 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
3903 png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
3905 if (row_info->bit_depth < 8)
3907 switch (row_info->bit_depth)
3911 gray = (png_uint_16)((gray & 0x01) * 0xff);
3912 sp = row + (png_size_t)((row_width - 1) >> 3);
3913 dp = row + (png_size_t)row_width - 1;
3914 shift = 7 - (int)((row_width + 7) & 0x07);
3915 for (i = 0; i < row_width; i++)
3917 if ((*sp >> shift) & 0x01)
3939 gray = (png_uint_16)((gray & 0x03) * 0x55);
3940 sp = row + (png_size_t)((row_width - 1) >> 2);
3941 dp = row + (png_size_t)row_width - 1;
3942 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3943 for (i = 0; i < row_width; i++)
3945 value = (*sp >> shift) & 0x03;
3946 *dp = (png_byte)(value | (value << 2) | (value << 4) |
3964 gray = (png_uint_16)((gray & 0x0f) * 0x11);
3965 sp = row + (png_size_t)((row_width - 1) >> 1);
3966 dp = row + (png_size_t)row_width - 1;
3967 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
3968 for (i = 0; i < row_width; i++)
3970 value = (*sp >> shift) & 0x0f;
3971 *dp = (png_byte)(value | (value << 4));
3990 row_info->bit_depth = 8;
3991 row_info->pixel_depth = 8;
3992 row_info->rowbytes = row_width;
3995 if (trans_value != NULL)
3997 if (row_info->bit_depth == 8)
4000 sp = row + (png_size_t)row_width - 1;
4001 dp = row + (png_size_t)(row_width << 1) - 1;
4003 for (i = 0; i < row_width; i++)
4015 else if (row_info->bit_depth == 16)
4017 png_byte gray_high = (png_byte)((gray >> 8) & 0xff);
4018 png_byte gray_low = (png_byte)(gray & 0xff);
4019 sp = row + row_info->rowbytes - 1;
4020 dp = row + (row_info->rowbytes << 1) - 1;
4021 for (i = 0; i < row_width; i++)
4023 if (*(sp - 1) == gray_high && *(sp) == gray_low)
4040 row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
4041 row_info->channels = 2;
4042 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
4043 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
4047 else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
4049 if (row_info->bit_depth == 8)
4051 png_byte red = (png_byte)(trans_value->red & 0xff);
4052 png_byte green = (png_byte)(trans_value->green & 0xff);
4053 png_byte blue = (png_byte)(trans_value->blue & 0xff);
4054 sp = row + (png_size_t)row_info->rowbytes - 1;
4055 dp = row + (png_size_t)(row_width << 2) - 1;
4056 for (i = 0; i < row_width; i++)
4058 if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
4069 else if (row_info->bit_depth == 16)
4071 png_byte red_high = (png_byte)((trans_value->red >> 8) & 0xff);
4072 png_byte green_high = (png_byte)((trans_value->green >> 8) & 0xff);
4073 png_byte blue_high = (png_byte)((trans_value->blue >> 8) & 0xff);
4074 png_byte red_low = (png_byte)(trans_value->red & 0xff);
4075 png_byte green_low = (png_byte)(trans_value->green & 0xff);
4076 png_byte blue_low = (png_byte)(trans_value->blue & 0xff);
4077 sp = row + row_info->rowbytes - 1;
4078 dp = row + (png_size_t)(row_width << 3) - 1;
4079 for (i = 0; i < row_width; i++)
4081 if (*(sp - 5) == red_high &&
4082 *(sp - 4) == red_low &&
4083 *(sp - 3) == green_high &&
4084 *(sp - 2) == green_low &&
4085 *(sp - 1) == blue_high &&
4106 row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
4107 row_info->channels = 4;
4108 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
4109 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4115 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4116 /* If the bit depth is 8 and the colour type is not a palette type expand the
4117 * whole row to 16 bits. Has no effect otherwise.
4120 png_do_expand_16(png_row_infop row_info, png_bytep row)
4122 if (row_info->bit_depth == 8 &&
4123 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
4125 /* The row have a sequence of bytes containing [0..255] and we need
4126 * to turn it into another row containing [0..65535], to do this we
4129 * (input / 255) * 65535
4131 * Which happens to be exactly input * 257 and this can be achieved
4132 * simply by byte replication in place (copying backwards).
4134 png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
4135 png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */
4137 dp[-2] = dp[-1] = *--sp, dp -= 2;
4139 row_info->rowbytes *= 2;
4140 row_info->bit_depth = 16;
4141 row_info->pixel_depth = (png_byte)(row_info->channels * 16);
4146 #ifdef PNG_READ_QUANTIZE_SUPPORTED
4148 png_do_quantize(png_row_infop row_info, png_bytep row,
4149 png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
4153 png_uint_32 row_width=row_info->width;
4155 png_debug(1, "in png_do_quantize");
4157 if (row_info->bit_depth == 8)
4159 if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup)
4164 for (i = 0; i < row_width; i++)
4170 /* This looks real messy, but the compiler will reduce
4171 * it down to a reasonable formula. For example, with
4172 * 5 bits per color, we get:
4173 * p = (((r >> 3) & 0x1f) << 10) |
4174 * (((g >> 3) & 0x1f) << 5) |
4175 * ((b >> 3) & 0x1f);
4177 p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
4178 ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
4179 (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
4180 (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
4181 ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
4182 (PNG_QUANTIZE_BLUE_BITS)) |
4183 ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
4184 ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
4186 *dp++ = palette_lookup[p];
4189 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4190 row_info->channels = 1;
4191 row_info->pixel_depth = row_info->bit_depth;
4192 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4195 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
4196 palette_lookup != NULL)
4201 for (i = 0; i < row_width; i++)
4208 p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
4209 ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
4210 (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
4211 (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
4212 ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
4213 (PNG_QUANTIZE_BLUE_BITS)) |
4214 ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
4215 ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
4217 *dp++ = palette_lookup[p];
4220 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4221 row_info->channels = 1;
4222 row_info->pixel_depth = row_info->bit_depth;
4223 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4226 else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
4231 for (i = 0; i < row_width; i++, sp++)
4233 *sp = quantize_lookup[*sp];
4238 #endif /* PNG_READ_QUANTIZE_SUPPORTED */
4240 #ifdef PNG_MNG_FEATURES_SUPPORTED
4241 /* Undoes intrapixel differencing */
4243 png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
4245 png_debug(1, "in png_do_read_intrapixel");
4248 (row_info->color_type & PNG_COLOR_MASK_COLOR))
4250 int bytes_per_pixel;
4251 png_uint_32 row_width = row_info->width;
4253 if (row_info->bit_depth == 8)
4258 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4259 bytes_per_pixel = 3;
4261 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4262 bytes_per_pixel = 4;
4267 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4269 *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
4270 *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
4273 else if (row_info->bit_depth == 16)
4278 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4279 bytes_per_pixel = 6;
4281 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4282 bytes_per_pixel = 8;
4287 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4289 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
4290 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
4291 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
4292 png_uint_32 red = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);
4293 png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);
4294 *(rp ) = (png_byte)((red >> 8) & 0xff);
4295 *(rp + 1) = (png_byte)(red & 0xff);
4296 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
4297 *(rp + 5) = (png_byte)(blue & 0xff);
4302 #endif /* PNG_MNG_FEATURES_SUPPORTED */
4303 #endif /* PNG_READ_SUPPORTED */