2 /* pngrtran.c - transforms the data in a row for PNG readers
4 * Last changed in libpng 1.4.0 [January 3, 2010]
5 * Copyright (c) 1998-2010 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
19 #define PNG_NO_PEDANTIC_WARNINGS
21 #ifdef PNG_READ_SUPPORTED
24 /* Set the action on getting a CRC error for an ancillary or critical chunk. */
26 png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
28 png_debug(1, "in png_set_crc_action");
33 /* Tell libpng how we react to CRC errors in critical chunks */
36 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
39 case PNG_CRC_WARN_USE: /* Warn/use data */
40 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
41 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
44 case PNG_CRC_QUIET_USE: /* Quiet/use data */
45 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
46 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
47 PNG_FLAG_CRC_CRITICAL_IGNORE;
50 case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
52 "Can't discard critical data on CRC error");
53 case PNG_CRC_ERROR_QUIT: /* Error/quit */
57 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
61 /* Tell libpng how we react to CRC errors in ancillary chunks */
64 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
67 case PNG_CRC_WARN_USE: /* Warn/use data */
68 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
69 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
72 case PNG_CRC_QUIET_USE: /* Quiet/use data */
73 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
74 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
75 PNG_FLAG_CRC_ANCILLARY_NOWARN;
78 case PNG_CRC_ERROR_QUIT: /* Error/quit */
79 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
80 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
83 case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
87 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
92 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
93 defined(PNG_FLOATING_POINT_SUPPORTED)
94 /* Handle alpha and tRNS via a background color */
96 png_set_background(png_structp png_ptr,
97 png_color_16p background_color, int background_gamma_code,
98 int need_expand, double background_gamma)
100 png_debug(1, "in png_set_background");
104 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
106 png_warning(png_ptr, "Application must supply a known background gamma");
110 png_ptr->transformations |= PNG_BACKGROUND;
111 png_memcpy(&(png_ptr->background), background_color,
112 png_sizeof(png_color_16));
113 png_ptr->background_gamma = (float)background_gamma;
114 png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
115 png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
119 #ifdef PNG_READ_16_TO_8_SUPPORTED
120 /* Strip 16 bit depth files to 8 bit depth */
122 png_set_strip_16(png_structp png_ptr)
124 png_debug(1, "in png_set_strip_16");
128 png_ptr->transformations |= PNG_16_TO_8;
132 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
134 png_set_strip_alpha(png_structp png_ptr)
136 png_debug(1, "in png_set_strip_alpha");
140 png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
144 #ifdef PNG_READ_DITHER_SUPPORTED
145 /* Dither file to 8 bit. Supply a palette, the current number
146 * of elements in the palette, the maximum number of elements
147 * allowed, and a histogram if possible. If the current number
148 * of colors is greater then the maximum number, the palette will be
149 * modified to fit in the maximum number. "full_dither" indicates
150 * whether we need a dithering cube set up for RGB images, or if we
151 * simply are reducing the number of colors in a paletted image.
154 typedef struct png_dsort_struct
156 struct png_dsort_struct FAR * next;
160 typedef png_dsort FAR * png_dsortp;
161 typedef png_dsort FAR * FAR * png_dsortpp;
164 png_set_dither(png_structp png_ptr, png_colorp palette,
165 int num_palette, int maximum_colors, png_uint_16p histogram,
168 png_debug(1, "in png_set_dither");
172 png_ptr->transformations |= PNG_DITHER;
178 png_ptr->dither_index = (png_bytep)png_malloc(png_ptr,
179 (png_uint_32)(num_palette * png_sizeof(png_byte)));
180 for (i = 0; i < num_palette; i++)
181 png_ptr->dither_index[i] = (png_byte)i;
184 if (num_palette > maximum_colors)
186 if (histogram != NULL)
188 /* This is easy enough, just throw out the least used colors.
189 * Perhaps not the best solution, but good enough.
194 /* Initialize an array to sort colors */
195 png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr,
196 (png_uint_32)(num_palette * png_sizeof(png_byte)));
198 /* Initialize the dither_sort array */
199 for (i = 0; i < num_palette; i++)
200 png_ptr->dither_sort[i] = (png_byte)i;
202 /* Find the least used palette entries by starting a
203 * bubble sort, and running it until we have sorted
204 * out enough colors. Note that we don't care about
205 * sorting all the colors, just finding which are
209 for (i = num_palette - 1; i >= maximum_colors; i--)
211 int done; /* To stop early if the list is pre-sorted */
215 for (j = 0; j < i; j++)
217 if (histogram[png_ptr->dither_sort[j]]
218 < histogram[png_ptr->dither_sort[j + 1]])
222 t = png_ptr->dither_sort[j];
223 png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1];
224 png_ptr->dither_sort[j + 1] = t;
232 /* Swap the palette around, and set up a table, if necessary */
237 /* Put all the useful colors within the max, but don't
240 for (i = 0; i < maximum_colors; i++)
242 if ((int)png_ptr->dither_sort[i] >= maximum_colors)
246 while ((int)png_ptr->dither_sort[j] >= maximum_colors);
247 palette[i] = palette[j];
255 /* Move all the used colors inside the max limit, and
256 * develop a translation table.
258 for (i = 0; i < maximum_colors; i++)
260 /* Only move the colors we need to */
261 if ((int)png_ptr->dither_sort[i] >= maximum_colors)
267 while ((int)png_ptr->dither_sort[j] >= maximum_colors);
269 tmp_color = palette[j];
270 palette[j] = palette[i];
271 palette[i] = tmp_color;
272 /* Indicate where the color went */
273 png_ptr->dither_index[j] = (png_byte)i;
274 png_ptr->dither_index[i] = (png_byte)j;
278 /* Find closest color for those colors we are not using */
279 for (i = 0; i < num_palette; i++)
281 if ((int)png_ptr->dither_index[i] >= maximum_colors)
283 int min_d, k, min_k, d_index;
285 /* Find the closest color to one we threw out */
286 d_index = png_ptr->dither_index[i];
287 min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
288 for (k = 1, min_k = 0; k < maximum_colors; k++)
292 d = PNG_COLOR_DIST(palette[d_index], palette[k]);
300 /* Point to closest color */
301 png_ptr->dither_index[i] = (png_byte)min_k;
305 png_free(png_ptr, png_ptr->dither_sort);
306 png_ptr->dither_sort = NULL;
310 /* This is much harder to do simply (and quickly). Perhaps
311 * we need to go through a median cut routine, but those
312 * don't always behave themselves with only a few colors
313 * as input. So we will just find the closest two colors,
314 * and throw out one of them (chosen somewhat randomly).
315 * [We don't understand this at all, so if someone wants to
316 * work on improving it, be our guest - AED, GRP]
326 /* Initialize palette index arrays */
327 png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
328 (png_uint_32)(num_palette * png_sizeof(png_byte)));
329 png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
330 (png_uint_32)(num_palette * png_sizeof(png_byte)));
332 /* Initialize the sort array */
333 for (i = 0; i < num_palette; i++)
335 png_ptr->index_to_palette[i] = (png_byte)i;
336 png_ptr->palette_to_index[i] = (png_byte)i;
339 hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
340 png_sizeof(png_dsortp)));
342 num_new_palette = num_palette;
344 /* Initial wild guess at how far apart the farthest pixel
345 * pair we will be eliminating will be. Larger
346 * numbers mean more areas will be allocated, Smaller
347 * numbers run the risk of not saving enough data, and
348 * having to do this all over again.
350 * I have not done extensive checking on this number.
354 while (num_new_palette > maximum_colors)
356 for (i = 0; i < num_new_palette - 1; i++)
360 for (j = i + 1; j < num_new_palette; j++)
364 d = PNG_COLOR_DIST(palette[i], palette[j]);
369 t = (png_dsortp)png_malloc_warn(png_ptr,
370 (png_uint_32)(png_sizeof(png_dsort)));
374 t->left = (png_byte)i;
375 t->right = (png_byte)j;
384 for (i = 0; i <= max_d; i++)
390 for (p = hash[i]; p; p = p->next)
392 if ((int)png_ptr->index_to_palette[p->left]
394 (int)png_ptr->index_to_palette[p->right]
399 if (num_new_palette & 0x01)
411 palette[png_ptr->index_to_palette[j]]
412 = palette[num_new_palette];
417 for (k = 0; k < num_palette; k++)
419 if (png_ptr->dither_index[k] ==
420 png_ptr->index_to_palette[j])
421 png_ptr->dither_index[k] =
422 png_ptr->index_to_palette[next_j];
423 if ((int)png_ptr->dither_index[k] ==
425 png_ptr->dither_index[k] =
426 png_ptr->index_to_palette[j];
430 png_ptr->index_to_palette[png_ptr->palette_to_index
431 [num_new_palette]] = png_ptr->index_to_palette[j];
432 png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
433 = png_ptr->palette_to_index[num_new_palette];
435 png_ptr->index_to_palette[j] = (png_byte)num_new_palette;
436 png_ptr->palette_to_index[num_new_palette] = (png_byte)j;
438 if (num_new_palette <= maximum_colors)
441 if (num_new_palette <= maximum_colors)
446 for (i = 0; i < 769; i++)
450 png_dsortp p = hash[i];
454 png_free(png_ptr, p);
462 png_free(png_ptr, hash);
463 png_free(png_ptr, png_ptr->palette_to_index);
464 png_free(png_ptr, png_ptr->index_to_palette);
465 png_ptr->palette_to_index = NULL;
466 png_ptr->index_to_palette = NULL;
468 num_palette = maximum_colors;
470 if (png_ptr->palette == NULL)
472 png_ptr->palette = palette;
474 png_ptr->num_palette = (png_uint_16)num_palette;
480 int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS +
481 PNG_DITHER_BLUE_BITS;
482 int num_red = (1 << PNG_DITHER_RED_BITS);
483 int num_green = (1 << PNG_DITHER_GREEN_BITS);
484 int num_blue = (1 << PNG_DITHER_BLUE_BITS);
485 png_size_t num_entries = ((png_size_t)1 << total_bits);
487 png_ptr->palette_lookup = (png_bytep )png_calloc(png_ptr,
488 (png_uint_32)(num_entries * png_sizeof(png_byte)));
490 distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
491 png_sizeof(png_byte)));
492 png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
494 for (i = 0; i < num_palette; i++)
497 int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS));
498 int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS));
499 int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS));
501 for (ir = 0; ir < num_red; ir++)
503 /* int dr = abs(ir - r); */
504 int dr = ((ir > r) ? ir - r : r - ir);
505 int index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS));
507 for (ig = 0; ig < num_green; ig++)
509 /* int dg = abs(ig - g); */
510 int dg = ((ig > g) ? ig - g : g - ig);
512 int dm = ((dr > dg) ? dr : dg);
513 int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS);
515 for (ib = 0; ib < num_blue; ib++)
517 int d_index = index_g | ib;
518 /* int db = abs(ib - b); */
519 int db = ((ib > b) ? ib - b : b - ib);
520 int dmax = ((dm > db) ? dm : db);
521 int d = dmax + dt + db;
523 if (d < (int)distance[d_index])
525 distance[d_index] = (png_byte)d;
526 png_ptr->palette_lookup[d_index] = (png_byte)i;
533 png_free(png_ptr, distance);
538 #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
539 /* Transform the image from the file_gamma to the screen_gamma. We
540 * only do transformations on images where the file_gamma and screen_gamma
541 * are not close reciprocals, otherwise it slows things down slightly, and
542 * also needlessly introduces small errors.
544 * We will turn off gamma transformation later if no semitransparent entries
545 * are present in the tRNS array for palette images. We can't do it here
546 * because we don't necessarily have the tRNS chunk yet.
549 png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
551 png_debug(1, "in png_set_gamma");
556 if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) ||
557 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
558 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
559 png_ptr->transformations |= PNG_GAMMA;
560 png_ptr->gamma = (float)file_gamma;
561 png_ptr->screen_gamma = (float)scrn_gamma;
565 #ifdef PNG_READ_EXPAND_SUPPORTED
566 /* Expand paletted images to RGB, expand grayscale images of
567 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
571 png_set_expand(png_structp png_ptr)
573 png_debug(1, "in png_set_expand");
578 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
579 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
582 /* GRR 19990627: the following three functions currently are identical
583 * to png_set_expand(). However, it is entirely reasonable that someone
584 * might wish to expand an indexed image to RGB but *not* expand a single,
585 * fully transparent palette entry to a full alpha channel--perhaps instead
586 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
587 * the transparent color with a particular RGB value, or drop tRNS entirely.
588 * IOW, a future version of the library may make the transformations flag
589 * a bit more fine-grained, with separate bits for each of these three
592 * More to the point, these functions make it obvious what libpng will be
593 * doing, whereas "expand" can (and does) mean any number of things.
595 * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
596 * to expand only the sample depth but not to expand the tRNS to alpha
597 * and its name was changed to png_set_expand_gray_1_2_4_to_8().
600 /* Expand paletted images to RGB. */
602 png_set_palette_to_rgb(png_structp png_ptr)
604 png_debug(1, "in png_set_palette_to_rgb");
609 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
610 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
613 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
615 png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
617 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
622 png_ptr->transformations |= PNG_EXPAND;
623 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
628 /* Expand tRNS chunks to alpha channels. */
630 png_set_tRNS_to_alpha(png_structp png_ptr)
632 png_debug(1, "in png_set_tRNS_to_alpha");
634 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
635 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
637 #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
639 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
641 png_set_gray_to_rgb(png_structp png_ptr)
643 png_debug(1, "in png_set_gray_to_rgb");
645 png_ptr->transformations |= PNG_GRAY_TO_RGB;
646 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
650 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
651 #ifdef PNG_FLOATING_POINT_SUPPORTED
652 /* Convert a RGB image to a grayscale of the same width. This allows us,
653 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
657 png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
660 int red_fixed = (int)((float)red*100000.0 + 0.5);
661 int green_fixed = (int)((float)green*100000.0 + 0.5);
664 png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed);
669 png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
670 png_fixed_point red, png_fixed_point green)
672 png_debug(1, "in png_set_rgb_to_gray");
679 case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;
682 case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
685 case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
687 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
688 #ifdef PNG_READ_EXPAND_SUPPORTED
689 png_ptr->transformations |= PNG_EXPAND;
693 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
694 png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
698 png_uint_16 red_int, green_int;
699 if (red < 0 || green < 0)
701 red_int = 6968; /* .212671 * 32768 + .5 */
702 green_int = 23434; /* .715160 * 32768 + .5 */
704 else if (red + green < 100000L)
706 red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
707 green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
711 png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
715 png_ptr->rgb_to_gray_red_coeff = red_int;
716 png_ptr->rgb_to_gray_green_coeff = green_int;
717 png_ptr->rgb_to_gray_blue_coeff =
718 (png_uint_16)(32768 - red_int - green_int);
723 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
724 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
726 png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
727 read_user_transform_fn)
729 png_debug(1, "in png_set_read_user_transform_fn");
734 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
735 png_ptr->transformations |= PNG_USER_TRANSFORM;
736 png_ptr->read_user_transform_fn = read_user_transform_fn;
741 /* Initialize everything needed for the read. This includes modifying
745 png_init_read_transformations(png_structp png_ptr)
747 png_debug(1, "in png_init_read_transformations");
750 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \
751 || defined(PNG_READ_GAMMA_SUPPORTED)
752 int color_type = png_ptr->color_type;
755 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
757 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
758 /* Detect gray background and attempt to enable optimization
759 * for gray --> RGB case
761 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
762 * RGB_ALPHA (in which case need_expand is superfluous anyway), the
763 * background color might actually be gray yet not be flagged as such.
764 * This is not a problem for the current code, which uses
765 * PNG_BACKGROUND_IS_GRAY only to decide when to do the
766 * png_do_gray_to_rgb() transformation.
768 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
769 !(color_type & PNG_COLOR_MASK_COLOR))
771 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
772 } else if ((png_ptr->transformations & PNG_BACKGROUND) &&
773 !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
774 (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
775 png_ptr->background.red == png_ptr->background.green &&
776 png_ptr->background.red == png_ptr->background.blue)
778 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
779 png_ptr->background.gray = png_ptr->background.red;
783 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
784 (png_ptr->transformations & PNG_EXPAND))
786 if (!(color_type & PNG_COLOR_MASK_COLOR)) /* i.e., GRAY or GRAY_ALPHA */
788 /* Expand background and tRNS chunks */
789 switch (png_ptr->bit_depth)
792 png_ptr->background.gray *= (png_uint_16)0xff;
793 png_ptr->background.red = png_ptr->background.green
794 = png_ptr->background.blue = png_ptr->background.gray;
795 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
797 png_ptr->trans_color.gray *= (png_uint_16)0xff;
798 png_ptr->trans_color.red = png_ptr->trans_color.green
799 = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
804 png_ptr->background.gray *= (png_uint_16)0x55;
805 png_ptr->background.red = png_ptr->background.green
806 = png_ptr->background.blue = png_ptr->background.gray;
807 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
809 png_ptr->trans_color.gray *= (png_uint_16)0x55;
810 png_ptr->trans_color.red = png_ptr->trans_color.green
811 = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
816 png_ptr->background.gray *= (png_uint_16)0x11;
817 png_ptr->background.red = png_ptr->background.green
818 = png_ptr->background.blue = png_ptr->background.gray;
819 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
821 png_ptr->trans_color.gray *= (png_uint_16)0x11;
822 png_ptr->trans_color.red = png_ptr->trans_color.green
823 = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
830 png_ptr->background.red = png_ptr->background.green
831 = png_ptr->background.blue = png_ptr->background.gray;
835 else if (color_type == PNG_COLOR_TYPE_PALETTE)
837 png_ptr->background.red =
838 png_ptr->palette[png_ptr->background.index].red;
839 png_ptr->background.green =
840 png_ptr->palette[png_ptr->background.index].green;
841 png_ptr->background.blue =
842 png_ptr->palette[png_ptr->background.index].blue;
844 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
845 if (png_ptr->transformations & PNG_INVERT_ALPHA)
847 #ifdef PNG_READ_EXPAND_SUPPORTED
848 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
851 /* Invert the alpha channel (in tRNS) unless the pixels are
852 * going to be expanded, in which case leave it for later
855 istop=(int)png_ptr->num_trans;
856 for (i=0; i<istop; i++)
857 png_ptr->trans_alpha[i] = (png_byte)(255 - png_ptr->trans_alpha[i]);
866 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
867 png_ptr->background_1 = png_ptr->background;
869 #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
871 if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
872 && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0)
873 < PNG_GAMMA_THRESHOLD))
877 for (i=0; i<png_ptr->num_trans; i++)
879 if (png_ptr->trans_alpha[i] != 0 && png_ptr->trans_alpha[i] != 0xff)
880 k=1; /* Partial transparency is present */
883 png_ptr->transformations &= ~PNG_GAMMA;
886 if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
887 png_ptr->gamma != 0.0)
889 png_build_gamma_table(png_ptr, png_ptr->bit_depth);
891 #ifdef PNG_READ_BACKGROUND_SUPPORTED
892 if (png_ptr->transformations & PNG_BACKGROUND)
894 if (color_type == PNG_COLOR_TYPE_PALETTE)
896 /* Could skip if no transparency */
897 png_color back, back_1;
898 png_colorp palette = png_ptr->palette;
899 int num_palette = png_ptr->num_palette;
901 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
903 back.red = png_ptr->gamma_table[png_ptr->background.red];
904 back.green = png_ptr->gamma_table[png_ptr->background.green];
905 back.blue = png_ptr->gamma_table[png_ptr->background.blue];
907 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
908 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
909 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
915 switch (png_ptr->background_gamma_type)
917 case PNG_BACKGROUND_GAMMA_SCREEN:
918 g = (png_ptr->screen_gamma);
922 case PNG_BACKGROUND_GAMMA_FILE:
923 g = 1.0 / (png_ptr->gamma);
924 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
927 case PNG_BACKGROUND_GAMMA_UNIQUE:
928 g = 1.0 / (png_ptr->background_gamma);
929 gs = 1.0 / (png_ptr->background_gamma *
930 png_ptr->screen_gamma);
933 g = 1.0; /* back_1 */
937 if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD)
939 back.red = (png_byte)png_ptr->background.red;
940 back.green = (png_byte)png_ptr->background.green;
941 back.blue = (png_byte)png_ptr->background.blue;
945 back.red = (png_byte)(pow(
946 (double)png_ptr->background.red/255.0, gs) * 255.0 + .5);
947 back.green = (png_byte)(pow(
948 (double)png_ptr->background.green/255.0, gs) * 255.0 + .5);
949 back.blue = (png_byte)(pow(
950 (double)png_ptr->background.blue/255.0, gs) * 255.0 + .5);
953 back_1.red = (png_byte)(pow(
954 (double)png_ptr->background.red/255.0, g) * 255.0 + .5);
955 back_1.green = (png_byte)(pow(
956 (double)png_ptr->background.green/255.0, g) * 255.0 + .5);
957 back_1.blue = (png_byte)(pow(
958 (double)png_ptr->background.blue/255.0, g) * 255.0 + .5);
960 for (i = 0; i < num_palette; i++)
962 if (i < (int)png_ptr->num_trans && png_ptr->trans_alpha[i] != 0xff)
964 if (png_ptr->trans_alpha[i] == 0)
968 else /* if (png_ptr->trans_alpha[i] != 0xff) */
972 v = png_ptr->gamma_to_1[palette[i].red];
973 png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);
974 palette[i].red = png_ptr->gamma_from_1[w];
976 v = png_ptr->gamma_to_1[palette[i].green];
977 png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);
978 palette[i].green = png_ptr->gamma_from_1[w];
980 v = png_ptr->gamma_to_1[palette[i].blue];
981 png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);
982 palette[i].blue = png_ptr->gamma_from_1[w];
987 palette[i].red = png_ptr->gamma_table[palette[i].red];
988 palette[i].green = png_ptr->gamma_table[palette[i].green];
989 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
992 /* Prevent the transformations being done again, and make sure
993 * that the now spurious alpha channel is stripped - the code
994 * has just reduced background composition and gamma correction
995 * to a simple alpha channel strip.
997 png_ptr->transformations &= ~PNG_BACKGROUND;
998 png_ptr->transformations &= ~PNG_GAMMA;
999 png_ptr->transformations |= PNG_STRIP_ALPHA;
1001 /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1003 /* color_type != PNG_COLOR_TYPE_PALETTE */
1005 double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);
1009 switch (png_ptr->background_gamma_type)
1011 case PNG_BACKGROUND_GAMMA_SCREEN:
1012 g = (png_ptr->screen_gamma);
1016 case PNG_BACKGROUND_GAMMA_FILE:
1017 g = 1.0 / (png_ptr->gamma);
1018 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
1021 case PNG_BACKGROUND_GAMMA_UNIQUE:
1022 g = 1.0 / (png_ptr->background_gamma);
1023 gs = 1.0 / (png_ptr->background_gamma *
1024 png_ptr->screen_gamma);
1028 png_ptr->background_1.gray = (png_uint_16)(pow(
1029 (double)png_ptr->background.gray / m, g) * m + .5);
1030 png_ptr->background.gray = (png_uint_16)(pow(
1031 (double)png_ptr->background.gray / m, gs) * m + .5);
1033 if ((png_ptr->background.red != png_ptr->background.green) ||
1034 (png_ptr->background.red != png_ptr->background.blue) ||
1035 (png_ptr->background.red != png_ptr->background.gray))
1037 /* RGB or RGBA with color background */
1038 png_ptr->background_1.red = (png_uint_16)(pow(
1039 (double)png_ptr->background.red / m, g) * m + .5);
1040 png_ptr->background_1.green = (png_uint_16)(pow(
1041 (double)png_ptr->background.green / m, g) * m + .5);
1042 png_ptr->background_1.blue = (png_uint_16)(pow(
1043 (double)png_ptr->background.blue / m, g) * m + .5);
1044 png_ptr->background.red = (png_uint_16)(pow(
1045 (double)png_ptr->background.red / m, gs) * m + .5);
1046 png_ptr->background.green = (png_uint_16)(pow(
1047 (double)png_ptr->background.green / m, gs) * m + .5);
1048 png_ptr->background.blue = (png_uint_16)(pow(
1049 (double)png_ptr->background.blue / m, gs) * m + .5);
1053 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1054 png_ptr->background_1.red = png_ptr->background_1.green
1055 = png_ptr->background_1.blue = png_ptr->background_1.gray;
1056 png_ptr->background.red = png_ptr->background.green
1057 = png_ptr->background.blue = png_ptr->background.gray;
1062 /* Transformation does not include PNG_BACKGROUND */
1063 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1064 if (color_type == PNG_COLOR_TYPE_PALETTE)
1066 png_colorp palette = png_ptr->palette;
1067 int num_palette = png_ptr->num_palette;
1070 for (i = 0; i < num_palette; i++)
1072 palette[i].red = png_ptr->gamma_table[palette[i].red];
1073 palette[i].green = png_ptr->gamma_table[palette[i].green];
1074 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1077 /* Done the gamma correction. */
1078 png_ptr->transformations &= ~PNG_GAMMA;
1081 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1084 #endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
1085 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1086 /* No GAMMA transformation */
1087 if ((png_ptr->transformations & PNG_BACKGROUND) &&
1088 (color_type == PNG_COLOR_TYPE_PALETTE))
1091 int istop = (int)png_ptr->num_trans;
1093 png_colorp palette = png_ptr->palette;
1095 back.red = (png_byte)png_ptr->background.red;
1096 back.green = (png_byte)png_ptr->background.green;
1097 back.blue = (png_byte)png_ptr->background.blue;
1099 for (i = 0; i < istop; i++)
1101 if (png_ptr->trans_alpha[i] == 0)
1105 else if (png_ptr->trans_alpha[i] != 0xff)
1107 /* The png_composite() macro is defined in png.h */
1108 png_composite(palette[i].red, palette[i].red,
1109 png_ptr->trans_alpha[i], back.red);
1110 png_composite(palette[i].green, palette[i].green,
1111 png_ptr->trans_alpha[i], back.green);
1112 png_composite(palette[i].blue, palette[i].blue,
1113 png_ptr->trans_alpha[i], back.blue);
1117 /* Handled alpha, still need to strip the channel. */
1118 png_ptr->transformations &= ~PNG_BACKGROUND;
1119 png_ptr->transformations |= PNG_STRIP_ALPHA;
1121 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1123 #ifdef PNG_READ_SHIFT_SUPPORTED
1124 if ((png_ptr->transformations & PNG_SHIFT) &&
1125 (color_type == PNG_COLOR_TYPE_PALETTE))
1128 png_uint_16 istop = png_ptr->num_palette;
1129 int sr = 8 - png_ptr->sig_bit.red;
1130 int sg = 8 - png_ptr->sig_bit.green;
1131 int sb = 8 - png_ptr->sig_bit.blue;
1133 if (sr < 0 || sr > 8)
1135 if (sg < 0 || sg > 8)
1137 if (sb < 0 || sb > 8)
1139 for (i = 0; i < istop; i++)
1141 png_ptr->palette[i].red >>= sr;
1142 png_ptr->palette[i].green >>= sg;
1143 png_ptr->palette[i].blue >>= sb;
1146 #endif /* PNG_READ_SHIFT_SUPPORTED */
1148 #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
1149 && !defined(PNG_READ_BACKGROUND_SUPPORTED)
1155 /* Modify the info structure to reflect the transformations. The
1156 * info should be updated so a PNG file could be written with it,
1157 * assuming the transformations result in valid PNG data.
1160 png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
1162 png_debug(1, "in png_read_transform_info");
1164 #ifdef PNG_READ_EXPAND_SUPPORTED
1165 if (png_ptr->transformations & PNG_EXPAND)
1167 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1169 if (png_ptr->num_trans &&
1170 (png_ptr->transformations & PNG_EXPAND_tRNS))
1171 info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1173 info_ptr->color_type = PNG_COLOR_TYPE_RGB;
1174 info_ptr->bit_depth = 8;
1175 info_ptr->num_trans = 0;
1179 if (png_ptr->num_trans)
1181 if (png_ptr->transformations & PNG_EXPAND_tRNS)
1182 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1184 if (info_ptr->bit_depth < 8)
1185 info_ptr->bit_depth = 8;
1186 info_ptr->num_trans = 0;
1191 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1192 if (png_ptr->transformations & PNG_BACKGROUND)
1194 info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
1195 info_ptr->num_trans = 0;
1196 info_ptr->background = png_ptr->background;
1200 #ifdef PNG_READ_GAMMA_SUPPORTED
1201 if (png_ptr->transformations & PNG_GAMMA)
1203 #ifdef PNG_FLOATING_POINT_SUPPORTED
1204 info_ptr->gamma = png_ptr->gamma;
1206 #ifdef PNG_FIXED_POINT_SUPPORTED
1207 info_ptr->int_gamma = png_ptr->int_gamma;
1212 #ifdef PNG_READ_16_TO_8_SUPPORTED
1213 if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
1214 info_ptr->bit_depth = 8;
1217 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1218 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
1219 info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
1222 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1223 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1224 info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
1227 #ifdef PNG_READ_DITHER_SUPPORTED
1228 if (png_ptr->transformations & PNG_DITHER)
1230 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1231 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
1232 png_ptr->palette_lookup && info_ptr->bit_depth == 8)
1234 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
1239 #ifdef PNG_READ_PACK_SUPPORTED
1240 if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
1241 info_ptr->bit_depth = 8;
1244 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1245 info_ptr->channels = 1;
1246 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
1247 info_ptr->channels = 3;
1249 info_ptr->channels = 1;
1251 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1252 if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
1253 info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
1256 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1257 info_ptr->channels++;
1259 #ifdef PNG_READ_FILLER_SUPPORTED
1260 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
1261 if ((png_ptr->transformations & PNG_FILLER) &&
1262 ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1263 (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
1265 info_ptr->channels++;
1266 /* If adding a true alpha channel not just filler */
1267 if (png_ptr->transformations & PNG_ADD_ALPHA)
1268 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1272 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
1273 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1274 if (png_ptr->transformations & PNG_USER_TRANSFORM)
1276 if (info_ptr->bit_depth < png_ptr->user_transform_depth)
1277 info_ptr->bit_depth = png_ptr->user_transform_depth;
1278 if (info_ptr->channels < png_ptr->user_transform_channels)
1279 info_ptr->channels = png_ptr->user_transform_channels;
1283 info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
1284 info_ptr->bit_depth);
1286 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
1288 #ifndef PNG_READ_EXPAND_SUPPORTED
1294 /* Transform the row. The order of transformations is significant,
1295 * and is very touchy. If you add a transformation, take care to
1296 * decide how it fits in with the other transformations here.
1299 png_do_read_transformations(png_structp png_ptr)
1301 png_debug(1, "in png_do_read_transformations");
1303 if (png_ptr->row_buf == NULL)
1305 #ifdef PNG_STDIO_SUPPORTED
1308 png_snprintf2(msg, 50,
1309 "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,
1311 png_error(png_ptr, msg);
1313 png_error(png_ptr, "NULL row buffer");
1316 #ifdef PNG_WARN_UNINITIALIZED_ROW
1317 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
1318 /* Application has failed to call either png_read_start_image()
1319 * or png_read_update_info() after setting transforms that expand
1320 * pixels. This check added to libpng-1.2.19
1322 #if (PNG_WARN_UNINITIALIZED_ROW==1)
1323 png_error(png_ptr, "Uninitialized row");
1325 png_warning(png_ptr, "Uninitialized row");
1329 #ifdef PNG_READ_EXPAND_SUPPORTED
1330 if (png_ptr->transformations & PNG_EXPAND)
1332 if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
1334 png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
1335 png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
1339 if (png_ptr->num_trans &&
1340 (png_ptr->transformations & PNG_EXPAND_tRNS))
1341 png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
1342 &(png_ptr->trans_color));
1344 png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
1350 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1351 if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
1352 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1353 PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));
1356 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1357 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1360 png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1);
1363 png_ptr->rgb_to_gray_status=1;
1364 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1365 PNG_RGB_TO_GRAY_WARN)
1366 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1367 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1368 PNG_RGB_TO_GRAY_ERR)
1369 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1374 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
1376 * In most cases, the "simple transparency" should be done prior to doing
1377 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
1378 * pixel is transparent. You would also need to make sure that the
1379 * transparency information is upgraded to RGB.
1381 * To summarize, the current flow is:
1382 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
1383 * with background "in place" if transparent,
1384 * convert to RGB if necessary
1385 * - Gray + alpha -> composite with gray background and remove alpha bytes,
1386 * convert to RGB if necessary
1388 * To support RGB backgrounds for gray images we need:
1389 * - Gray + simple transparency -> convert to RGB + simple transparency,
1390 * compare 3 or 6 bytes and composite with
1391 * background "in place" if transparent
1392 * (3x compare/pixel compared to doing
1393 * composite with gray bkgrnd)
1394 * - Gray + alpha -> convert to RGB + alpha, composite with background and
1395 * remove alpha bytes (3x float
1396 * operations/pixel compared with composite
1397 * on gray background)
1399 * Greg's change will do this. The reason it wasn't done before is for
1400 * performance, as this increases the per-pixel operations. If we would check
1401 * in advance if the background was gray or RGB, and position the gray-to-RGB
1402 * transform appropriately, then it would save a lot of work/time.
1405 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1406 /* If gray -> RGB, do so now only if background is non-gray; else do later
1407 * for performance reasons
1409 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1410 !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1411 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1414 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1415 if ((png_ptr->transformations & PNG_BACKGROUND) &&
1416 ((png_ptr->num_trans != 0 ) ||
1417 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
1418 png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
1419 &(png_ptr->trans_color), &(png_ptr->background)
1420 #ifdef PNG_READ_GAMMA_SUPPORTED
1421 , &(png_ptr->background_1),
1422 png_ptr->gamma_table, png_ptr->gamma_from_1,
1423 png_ptr->gamma_to_1, png_ptr->gamma_16_table,
1424 png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
1425 png_ptr->gamma_shift
1430 #ifdef PNG_READ_GAMMA_SUPPORTED
1431 if ((png_ptr->transformations & PNG_GAMMA) &&
1432 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1433 !((png_ptr->transformations & PNG_BACKGROUND) &&
1434 ((png_ptr->num_trans != 0) ||
1435 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
1437 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
1438 png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
1439 png_ptr->gamma_table, png_ptr->gamma_16_table,
1440 png_ptr->gamma_shift);
1443 #ifdef PNG_READ_16_TO_8_SUPPORTED
1444 if (png_ptr->transformations & PNG_16_TO_8)
1445 png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
1448 #ifdef PNG_READ_DITHER_SUPPORTED
1449 if (png_ptr->transformations & PNG_DITHER)
1451 png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
1452 png_ptr->palette_lookup, png_ptr->dither_index);
1453 if (png_ptr->row_info.rowbytes == (png_uint_32)0)
1454 png_error(png_ptr, "png_do_dither returned rowbytes=0");
1458 #ifdef PNG_READ_INVERT_SUPPORTED
1459 if (png_ptr->transformations & PNG_INVERT_MONO)
1460 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
1463 #ifdef PNG_READ_SHIFT_SUPPORTED
1464 if (png_ptr->transformations & PNG_SHIFT)
1465 png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
1469 #ifdef PNG_READ_PACK_SUPPORTED
1470 if (png_ptr->transformations & PNG_PACK)
1471 png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
1474 #ifdef PNG_READ_BGR_SUPPORTED
1475 if (png_ptr->transformations & PNG_BGR)
1476 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
1479 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1480 if (png_ptr->transformations & PNG_PACKSWAP)
1481 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1484 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1485 /* If gray -> RGB, do so now only if we did not do so above */
1486 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1487 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1488 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1491 #ifdef PNG_READ_FILLER_SUPPORTED
1492 if (png_ptr->transformations & PNG_FILLER)
1493 png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1494 (png_uint_32)png_ptr->filler, png_ptr->flags);
1497 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1498 if (png_ptr->transformations & PNG_INVERT_ALPHA)
1499 png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1502 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1503 if (png_ptr->transformations & PNG_SWAP_ALPHA)
1504 png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1507 #ifdef PNG_READ_SWAP_SUPPORTED
1508 if (png_ptr->transformations & PNG_SWAP_BYTES)
1509 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1512 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1513 if (png_ptr->transformations & PNG_USER_TRANSFORM)
1515 if (png_ptr->read_user_transform_fn != NULL)
1516 (*(png_ptr->read_user_transform_fn)) /* User read transform function */
1517 (png_ptr, /* png_ptr */
1518 &(png_ptr->row_info), /* row_info: */
1519 /* png_uint_32 width; width of row */
1520 /* png_uint_32 rowbytes; number of bytes in row */
1521 /* png_byte color_type; color type of pixels */
1522 /* png_byte bit_depth; bit depth of samples */
1523 /* png_byte channels; number of channels (1-4) */
1524 /* png_byte pixel_depth; bits per pixel (depth*channels) */
1525 png_ptr->row_buf + 1); /* start of pixel data for row */
1526 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
1527 if (png_ptr->user_transform_depth)
1528 png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
1529 if (png_ptr->user_transform_channels)
1530 png_ptr->row_info.channels = png_ptr->user_transform_channels;
1532 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
1533 png_ptr->row_info.channels);
1534 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
1535 png_ptr->row_info.width);
1541 #ifdef PNG_READ_PACK_SUPPORTED
1542 /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
1543 * without changing the actual values. Thus, if you had a row with
1544 * a bit depth of 1, you would end up with bytes that only contained
1545 * the numbers 0 or 1. If you would rather they contain 0 and 255, use
1546 * png_do_shift() after this.
1549 png_do_unpack(png_row_infop row_info, png_bytep row)
1551 png_debug(1, "in png_do_unpack");
1553 if (row_info->bit_depth < 8)
1556 png_uint_32 row_width=row_info->width;
1558 switch (row_info->bit_depth)
1562 png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
1563 png_bytep dp = row + (png_size_t)row_width - 1;
1564 png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
1565 for (i = 0; i < row_width; i++)
1567 *dp = (png_byte)((*sp >> shift) & 0x01);
1584 png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
1585 png_bytep dp = row + (png_size_t)row_width - 1;
1586 png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
1587 for (i = 0; i < row_width; i++)
1589 *dp = (png_byte)((*sp >> shift) & 0x03);
1605 png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
1606 png_bytep dp = row + (png_size_t)row_width - 1;
1607 png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
1608 for (i = 0; i < row_width; i++)
1610 *dp = (png_byte)((*sp >> shift) & 0x0f);
1624 row_info->bit_depth = 8;
1625 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1626 row_info->rowbytes = row_width * row_info->channels;
1631 #ifdef PNG_READ_SHIFT_SUPPORTED
1632 /* Reverse the effects of png_do_shift. This routine merely shifts the
1633 * pixels back to their significant bits values. Thus, if you have
1634 * a row of bit depth 8, but only 5 are significant, this will shift
1635 * the values back to 0 through 31.
1638 png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
1640 png_debug(1, "in png_do_unshift");
1643 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
1648 png_uint_16 value = 0;
1649 png_uint_32 row_width = row_info->width;
1651 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
1653 shift[channels++] = row_info->bit_depth - sig_bits->red;
1654 shift[channels++] = row_info->bit_depth - sig_bits->green;
1655 shift[channels++] = row_info->bit_depth - sig_bits->blue;
1659 shift[channels++] = row_info->bit_depth - sig_bits->gray;
1661 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
1663 shift[channels++] = row_info->bit_depth - sig_bits->alpha;
1666 for (c = 0; c < channels; c++)
1677 switch (row_info->bit_depth)
1683 png_uint_32 istop = row_info->rowbytes;
1685 for (bp = row, i = 0; i < istop; i++)
1697 png_uint_32 istop = row_info->rowbytes;
1698 png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
1699 (png_byte)((int)0xf >> shift[0]));
1701 for (i = 0; i < istop; i++)
1713 png_uint_32 istop = row_width * channels;
1715 for (i = 0; i < istop; i++)
1717 *bp++ >>= shift[i%channels];
1726 png_uint_32 istop = channels * row_width;
1728 for (i = 0; i < istop; i++)
1730 value = (png_uint_16)((*bp << 8) + *(bp + 1));
1731 value >>= shift[i%channels];
1732 *bp++ = (png_byte)(value >> 8);
1733 *bp++ = (png_byte)(value & 0xff);
1742 #ifdef PNG_READ_16_TO_8_SUPPORTED
1743 /* Chop rows of bit depth 16 down to 8 */
1745 png_do_chop(png_row_infop row_info, png_bytep row)
1747 png_debug(1, "in png_do_chop");
1749 if (row_info->bit_depth == 16)
1754 png_uint_32 istop = row_info->width * row_info->channels;
1756 for (i = 0; i<istop; i++, sp += 2, dp++)
1758 #ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
1759 /* This does a more accurate scaling of the 16-bit color
1760 * value, rather than a simple low-byte truncation.
1762 * What the ideal calculation should be:
1763 * *dp = (((((png_uint_32)(*sp) << 8) |
1764 * (png_uint_32)(*(sp + 1))) * 255 + 127)
1765 * / (png_uint_32)65535L;
1767 * GRR: no, I think this is what it really should be:
1768 * *dp = (((((png_uint_32)(*sp) << 8) |
1769 * (png_uint_32)(*(sp + 1))) + 128L)
1770 * / (png_uint_32)257L;
1772 * GRR: here's the exact calculation with shifts:
1773 * temp = (((png_uint_32)(*sp) << 8) |
1774 * (png_uint_32)(*(sp + 1))) + 128L;
1775 * *dp = (temp - (temp >> 8)) >> 8;
1777 * Approximate calculation with shift/add instead of multiply/divide:
1778 * *dp = ((((png_uint_32)(*sp) << 8) |
1779 * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
1781 * What we actually do to avoid extra shifting and conversion:
1784 *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
1786 /* Simply discard the low order byte */
1790 row_info->bit_depth = 8;
1791 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1792 row_info->rowbytes = row_info->width * row_info->channels;
1797 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1799 png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
1801 png_debug(1, "in png_do_read_swap_alpha");
1804 png_uint_32 row_width = row_info->width;
1805 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1807 /* This converts from RGBA to ARGB */
1808 if (row_info->bit_depth == 8)
1810 png_bytep sp = row + row_info->rowbytes;
1815 for (i = 0; i < row_width; i++)
1824 /* This converts from RRGGBBAA to AARRGGBB */
1827 png_bytep sp = row + row_info->rowbytes;
1832 for (i = 0; i < row_width; i++)
1847 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1849 /* This converts from GA to AG */
1850 if (row_info->bit_depth == 8)
1852 png_bytep sp = row + row_info->rowbytes;
1857 for (i = 0; i < row_width; i++)
1864 /* This converts from GGAA to AAGG */
1867 png_bytep sp = row + row_info->rowbytes;
1872 for (i = 0; i < row_width; i++)
1887 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1889 png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
1891 png_debug(1, "in png_do_read_invert_alpha");
1894 png_uint_32 row_width = row_info->width;
1895 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1897 /* This inverts the alpha channel in RGBA */
1898 if (row_info->bit_depth == 8)
1900 png_bytep sp = row + row_info->rowbytes;
1904 for (i = 0; i < row_width; i++)
1906 *(--dp) = (png_byte)(255 - *(--sp));
1908 /* This does nothing:
1912 We can replace it with:
1918 /* This inverts the alpha channel in RRGGBBAA */
1921 png_bytep sp = row + row_info->rowbytes;
1925 for (i = 0; i < row_width; i++)
1927 *(--dp) = (png_byte)(255 - *(--sp));
1928 *(--dp) = (png_byte)(255 - *(--sp));
1930 /* This does nothing:
1937 We can replace it with:
1944 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1946 /* This inverts the alpha channel in GA */
1947 if (row_info->bit_depth == 8)
1949 png_bytep sp = row + row_info->rowbytes;
1953 for (i = 0; i < row_width; i++)
1955 *(--dp) = (png_byte)(255 - *(--sp));
1959 /* This inverts the alpha channel in GGAA */
1962 png_bytep sp = row + row_info->rowbytes;
1966 for (i = 0; i < row_width; i++)
1968 *(--dp) = (png_byte)(255 - *(--sp));
1969 *(--dp) = (png_byte)(255 - *(--sp));
1983 #ifdef PNG_READ_FILLER_SUPPORTED
1984 /* Add filler channel if we have RGB color */
1986 png_do_read_filler(png_row_infop row_info, png_bytep row,
1987 png_uint_32 filler, png_uint_32 flags)
1990 png_uint_32 row_width = row_info->width;
1992 png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
1993 png_byte lo_filler = (png_byte)(filler & 0xff);
1995 png_debug(1, "in png_do_read_filler");
1998 row_info->color_type == PNG_COLOR_TYPE_GRAY)
2000 if (row_info->bit_depth == 8)
2002 /* This changes the data from G to GX */
2003 if (flags & PNG_FLAG_FILLER_AFTER)
2005 png_bytep sp = row + (png_size_t)row_width;
2006 png_bytep dp = sp + (png_size_t)row_width;
2007 for (i = 1; i < row_width; i++)
2009 *(--dp) = lo_filler;
2012 *(--dp) = lo_filler;
2013 row_info->channels = 2;
2014 row_info->pixel_depth = 16;
2015 row_info->rowbytes = row_width * 2;
2017 /* This changes the data from G to XG */
2020 png_bytep sp = row + (png_size_t)row_width;
2021 png_bytep dp = sp + (png_size_t)row_width;
2022 for (i = 0; i < row_width; i++)
2025 *(--dp) = lo_filler;
2027 row_info->channels = 2;
2028 row_info->pixel_depth = 16;
2029 row_info->rowbytes = row_width * 2;
2032 else if (row_info->bit_depth == 16)
2034 /* This changes the data from GG to GGXX */
2035 if (flags & PNG_FLAG_FILLER_AFTER)
2037 png_bytep sp = row + (png_size_t)row_width * 2;
2038 png_bytep dp = sp + (png_size_t)row_width * 2;
2039 for (i = 1; i < row_width; i++)
2041 *(--dp) = hi_filler;
2042 *(--dp) = lo_filler;
2046 *(--dp) = hi_filler;
2047 *(--dp) = lo_filler;
2048 row_info->channels = 2;
2049 row_info->pixel_depth = 32;
2050 row_info->rowbytes = row_width * 4;
2052 /* This changes the data from GG to XXGG */
2055 png_bytep sp = row + (png_size_t)row_width * 2;
2056 png_bytep dp = sp + (png_size_t)row_width * 2;
2057 for (i = 0; i < row_width; i++)
2061 *(--dp) = hi_filler;
2062 *(--dp) = lo_filler;
2064 row_info->channels = 2;
2065 row_info->pixel_depth = 32;
2066 row_info->rowbytes = row_width * 4;
2069 } /* COLOR_TYPE == GRAY */
2070 else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2072 if (row_info->bit_depth == 8)
2074 /* This changes the data from RGB to RGBX */
2075 if (flags & PNG_FLAG_FILLER_AFTER)
2077 png_bytep sp = row + (png_size_t)row_width * 3;
2078 png_bytep dp = sp + (png_size_t)row_width;
2079 for (i = 1; i < row_width; i++)
2081 *(--dp) = lo_filler;
2086 *(--dp) = lo_filler;
2087 row_info->channels = 4;
2088 row_info->pixel_depth = 32;
2089 row_info->rowbytes = row_width * 4;
2091 /* This changes the data from RGB to XRGB */
2094 png_bytep sp = row + (png_size_t)row_width * 3;
2095 png_bytep dp = sp + (png_size_t)row_width;
2096 for (i = 0; i < row_width; i++)
2101 *(--dp) = lo_filler;
2103 row_info->channels = 4;
2104 row_info->pixel_depth = 32;
2105 row_info->rowbytes = row_width * 4;
2108 else if (row_info->bit_depth == 16)
2110 /* This changes the data from RRGGBB to RRGGBBXX */
2111 if (flags & PNG_FLAG_FILLER_AFTER)
2113 png_bytep sp = row + (png_size_t)row_width * 6;
2114 png_bytep dp = sp + (png_size_t)row_width * 2;
2115 for (i = 1; i < row_width; i++)
2117 *(--dp) = hi_filler;
2118 *(--dp) = lo_filler;
2126 *(--dp) = hi_filler;
2127 *(--dp) = lo_filler;
2128 row_info->channels = 4;
2129 row_info->pixel_depth = 64;
2130 row_info->rowbytes = row_width * 8;
2132 /* This changes the data from RRGGBB to XXRRGGBB */
2135 png_bytep sp = row + (png_size_t)row_width * 6;
2136 png_bytep dp = sp + (png_size_t)row_width * 2;
2137 for (i = 0; i < row_width; i++)
2145 *(--dp) = hi_filler;
2146 *(--dp) = lo_filler;
2148 row_info->channels = 4;
2149 row_info->pixel_depth = 64;
2150 row_info->rowbytes = row_width * 8;
2153 } /* COLOR_TYPE == RGB */
2157 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2158 /* Expand grayscale files to RGB, with or without alpha */
2160 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
2163 png_uint_32 row_width = row_info->width;
2165 png_debug(1, "in png_do_gray_to_rgb");
2167 if (row_info->bit_depth >= 8 &&
2168 !(row_info->color_type & PNG_COLOR_MASK_COLOR))
2170 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
2172 if (row_info->bit_depth == 8)
2174 png_bytep sp = row + (png_size_t)row_width - 1;
2175 png_bytep dp = sp + (png_size_t)row_width * 2;
2176 for (i = 0; i < row_width; i++)
2185 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2186 png_bytep dp = sp + (png_size_t)row_width * 4;
2187 for (i = 0; i < row_width; i++)
2190 *(dp--) = *(sp - 1);
2192 *(dp--) = *(sp - 1);
2198 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2200 if (row_info->bit_depth == 8)
2202 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2203 png_bytep dp = sp + (png_size_t)row_width * 2;
2204 for (i = 0; i < row_width; i++)
2214 png_bytep sp = row + (png_size_t)row_width * 4 - 1;
2215 png_bytep dp = sp + (png_size_t)row_width * 4;
2216 for (i = 0; i < row_width; i++)
2221 *(dp--) = *(sp - 1);
2223 *(dp--) = *(sp - 1);
2229 row_info->channels += (png_byte)2;
2230 row_info->color_type |= PNG_COLOR_MASK_COLOR;
2231 row_info->pixel_depth = (png_byte)(row_info->channels *
2232 row_info->bit_depth);
2233 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2238 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2239 /* Reduce RGB files to grayscale, with or without alpha
2240 * using the equation given in Poynton's ColorFAQ at
2241 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008)
2243 * <http://www.poynton.com/notes/colour_and_gamma/>
2244 * Charles Poynton poynton at poynton.com
2246 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2248 * We approximate this with
2250 * Y = 0.21268 * R + 0.7151 * G + 0.07217 * B
2252 * which can be expressed with integers as
2254 * Y = (6969 * R + 23434 * G + 2365 * B)/32768
2256 * The calculation is to be done in a linear colorspace.
2258 * Other integer coefficents can be used via png_set_rgb_to_gray().
2261 png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
2266 png_uint_32 row_width = row_info->width;
2269 png_debug(1, "in png_do_rgb_to_gray");
2272 (row_info->color_type & PNG_COLOR_MASK_COLOR))
2274 png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
2275 png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
2276 png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
2278 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2280 if (row_info->bit_depth == 8)
2282 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2283 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
2288 for (i = 0; i < row_width; i++)
2290 png_byte red = png_ptr->gamma_to_1[*(sp++)];
2291 png_byte green = png_ptr->gamma_to_1[*(sp++)];
2292 png_byte blue = png_ptr->gamma_to_1[*(sp++)];
2293 if (red != green || red != blue)
2296 *(dp++) = png_ptr->gamma_from_1[
2297 (rc*red + gc*green + bc*blue)>>15];
2300 *(dp++) = *(sp - 1);
2308 for (i = 0; i < row_width; i++)
2310 png_byte red = *(sp++);
2311 png_byte green = *(sp++);
2312 png_byte blue = *(sp++);
2313 if (red != green || red != blue)
2316 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
2319 *(dp++) = *(sp - 1);
2324 else /* RGB bit_depth == 16 */
2326 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2327 if (png_ptr->gamma_16_to_1 != NULL &&
2328 png_ptr->gamma_16_from_1 != NULL)
2332 for (i = 0; i < row_width; i++)
2334 png_uint_16 red, green, blue, w;
2336 red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2337 green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2338 blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2340 if (red == green && red == blue)
2344 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >>
2345 png_ptr->gamma_shift][red>>8];
2346 png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
2347 png_ptr->gamma_shift][green>>8];
2348 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >>
2349 png_ptr->gamma_shift][blue>>8];
2350 png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1
2352 w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
2353 png_ptr->gamma_shift][gray16 >> 8];
2357 *(dp++) = (png_byte)((w>>8) & 0xff);
2358 *(dp++) = (png_byte)(w & 0xff);
2366 for (i = 0; i < row_width; i++)
2368 png_uint_16 red, green, blue, gray16;
2370 red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2371 green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2372 blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2374 if (red != green || red != blue)
2376 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2377 *(dp++) = (png_byte)((gray16>>8) & 0xff);
2378 *(dp++) = (png_byte)(gray16 & 0xff);
2383 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2385 if (row_info->bit_depth == 8)
2387 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2388 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
2392 for (i = 0; i < row_width; i++)
2394 png_byte red = png_ptr->gamma_to_1[*(sp++)];
2395 png_byte green = png_ptr->gamma_to_1[*(sp++)];
2396 png_byte blue = png_ptr->gamma_to_1[*(sp++)];
2397 if (red != green || red != blue)
2399 *(dp++) = png_ptr->gamma_from_1
2400 [(rc*red + gc*green + bc*blue)>>15];
2401 *(dp++) = *(sp++); /* alpha */
2409 for (i = 0; i < row_width; i++)
2411 png_byte red = *(sp++);
2412 png_byte green = *(sp++);
2413 png_byte blue = *(sp++);
2414 if (red != green || red != blue)
2416 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
2417 *(dp++) = *(sp++); /* alpha */
2421 else /* RGBA bit_depth == 16 */
2423 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2424 if (png_ptr->gamma_16_to_1 != NULL &&
2425 png_ptr->gamma_16_from_1 != NULL)
2429 for (i = 0; i < row_width; i++)
2431 png_uint_16 red, green, blue, w;
2433 red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2434 green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2435 blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2437 if (red == green && red == blue)
2441 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >>
2442 png_ptr->gamma_shift][red>>8];
2443 png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
2444 png_ptr->gamma_shift][green>>8];
2445 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >>
2446 png_ptr->gamma_shift][blue>>8];
2447 png_uint_16 gray16 = (png_uint_16)((rc * red_1
2448 + gc * green_1 + bc * blue_1)>>15);
2449 w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
2450 png_ptr->gamma_shift][gray16 >> 8];
2454 *(dp++) = (png_byte)((w>>8) & 0xff);
2455 *(dp++) = (png_byte)(w & 0xff);
2456 *(dp++) = *(sp++); /* alpha */
2465 for (i = 0; i < row_width; i++)
2467 png_uint_16 red, green, blue, gray16;
2468 red = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
2469 green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
2470 blue = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
2471 if (red != green || red != blue)
2473 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2474 *(dp++) = (png_byte)((gray16>>8) & 0xff);
2475 *(dp++) = (png_byte)(gray16 & 0xff);
2476 *(dp++) = *(sp++); /* alpha */
2482 row_info->channels -= (png_byte)2;
2483 row_info->color_type &= ~PNG_COLOR_MASK_COLOR;
2484 row_info->pixel_depth = (png_byte)(row_info->channels *
2485 row_info->bit_depth);
2486 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2492 /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
2493 * large of png_color. This lets grayscale images be treated as
2494 * paletted. Most useful for gamma correction and simplification
2498 png_build_grayscale_palette(int bit_depth, png_colorp palette)
2505 png_debug(1, "in png_do_build_grayscale_palette");
2507 if (palette == NULL)
2538 for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
2540 palette[i].red = (png_byte)v;
2541 palette[i].green = (png_byte)v;
2542 palette[i].blue = (png_byte)v;
2547 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2548 /* Replace any alpha or transparency with the supplied background color.
2549 * "background" is already in the screen gamma, while "background_1" is
2550 * at a gamma of 1.0. Paletted files have already been taken care of.
2553 png_do_background(png_row_infop row_info, png_bytep row,
2554 png_color_16p trans_color, png_color_16p background
2555 #ifdef PNG_READ_GAMMA_SUPPORTED
2556 , png_color_16p background_1,
2557 png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
2558 png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
2559 png_uint_16pp gamma_16_to_1, int gamma_shift
2565 png_uint_32 row_width=row_info->width;
2568 png_debug(1, "in png_do_background");
2570 if (background != NULL &&
2571 (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
2572 (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_color)))
2574 switch (row_info->color_type)
2576 case PNG_COLOR_TYPE_GRAY:
2578 switch (row_info->bit_depth)
2584 for (i = 0; i < row_width; i++)
2586 if ((png_uint_16)((*sp >> shift) & 0x01)
2587 == trans_color->gray)
2589 *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
2590 *sp |= (png_byte)(background->gray << shift);
2605 #ifdef PNG_READ_GAMMA_SUPPORTED
2606 if (gamma_table != NULL)
2610 for (i = 0; i < row_width; i++)
2612 if ((png_uint_16)((*sp >> shift) & 0x03)
2613 == trans_color->gray)
2615 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2616 *sp |= (png_byte)(background->gray << shift);
2620 png_byte p = (png_byte)((*sp >> shift) & 0x03);
2621 png_byte g = (png_byte)((gamma_table [p | (p << 2) |
2622 (p << 4) | (p << 6)] >> 6) & 0x03);
2623 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2624 *sp |= (png_byte)(g << shift);
2640 for (i = 0; i < row_width; i++)
2642 if ((png_uint_16)((*sp >> shift) & 0x03)
2643 == trans_color->gray)
2645 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2646 *sp |= (png_byte)(background->gray << shift);
2662 #ifdef PNG_READ_GAMMA_SUPPORTED
2663 if (gamma_table != NULL)
2667 for (i = 0; i < row_width; i++)
2669 if ((png_uint_16)((*sp >> shift) & 0x0f)
2670 == trans_color->gray)
2672 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2673 *sp |= (png_byte)(background->gray << shift);
2677 png_byte p = (png_byte)((*sp >> shift) & 0x0f);
2678 png_byte g = (png_byte)((gamma_table[p |
2679 (p << 4)] >> 4) & 0x0f);
2680 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2681 *sp |= (png_byte)(g << shift);
2697 for (i = 0; i < row_width; i++)
2699 if ((png_uint_16)((*sp >> shift) & 0x0f)
2700 == trans_color->gray)
2702 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2703 *sp |= (png_byte)(background->gray << shift);
2719 #ifdef PNG_READ_GAMMA_SUPPORTED
2720 if (gamma_table != NULL)
2723 for (i = 0; i < row_width; i++, sp++)
2725 if (*sp == trans_color->gray)
2727 *sp = (png_byte)background->gray;
2731 *sp = gamma_table[*sp];
2739 for (i = 0; i < row_width; i++, sp++)
2741 if (*sp == trans_color->gray)
2743 *sp = (png_byte)background->gray;
2752 #ifdef PNG_READ_GAMMA_SUPPORTED
2753 if (gamma_16 != NULL)
2756 for (i = 0; i < row_width; i++, sp += 2)
2760 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
2761 if (v == trans_color->gray)
2763 /* Background is already in screen gamma */
2764 *sp = (png_byte)((background->gray >> 8) & 0xff);
2765 *(sp + 1) = (png_byte)(background->gray & 0xff);
2769 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
2770 *sp = (png_byte)((v >> 8) & 0xff);
2771 *(sp + 1) = (png_byte)(v & 0xff);
2779 for (i = 0; i < row_width; i++, sp += 2)
2783 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
2784 if (v == trans_color->gray)
2786 *sp = (png_byte)((background->gray >> 8) & 0xff);
2787 *(sp + 1) = (png_byte)(background->gray & 0xff);
2797 case PNG_COLOR_TYPE_RGB:
2799 if (row_info->bit_depth == 8)
2801 #ifdef PNG_READ_GAMMA_SUPPORTED
2802 if (gamma_table != NULL)
2805 for (i = 0; i < row_width; i++, sp += 3)
2807 if (*sp == trans_color->red &&
2808 *(sp + 1) == trans_color->green &&
2809 *(sp + 2) == trans_color->blue)
2811 *sp = (png_byte)background->red;
2812 *(sp + 1) = (png_byte)background->green;
2813 *(sp + 2) = (png_byte)background->blue;
2817 *sp = gamma_table[*sp];
2818 *(sp + 1) = gamma_table[*(sp + 1)];
2819 *(sp + 2) = gamma_table[*(sp + 2)];
2827 for (i = 0; i < row_width; i++, sp += 3)
2829 if (*sp == trans_color->red &&
2830 *(sp + 1) == trans_color->green &&
2831 *(sp + 2) == trans_color->blue)
2833 *sp = (png_byte)background->red;
2834 *(sp + 1) = (png_byte)background->green;
2835 *(sp + 2) = (png_byte)background->blue;
2840 else /* if (row_info->bit_depth == 16) */
2842 #ifdef PNG_READ_GAMMA_SUPPORTED
2843 if (gamma_16 != NULL)
2846 for (i = 0; i < row_width; i++, sp += 6)
2848 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
2849 png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
2850 png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
2851 if (r == trans_color->red && g == trans_color->green &&
2852 b == trans_color->blue)
2854 /* Background is already in screen gamma */
2855 *sp = (png_byte)((background->red >> 8) & 0xff);
2856 *(sp + 1) = (png_byte)(background->red & 0xff);
2857 *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
2858 *(sp + 3) = (png_byte)(background->green & 0xff);
2859 *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
2860 *(sp + 5) = (png_byte)(background->blue & 0xff);
2864 png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
2865 *sp = (png_byte)((v >> 8) & 0xff);
2866 *(sp + 1) = (png_byte)(v & 0xff);
2867 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
2868 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
2869 *(sp + 3) = (png_byte)(v & 0xff);
2870 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
2871 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
2872 *(sp + 5) = (png_byte)(v & 0xff);
2880 for (i = 0; i < row_width; i++, sp += 6)
2882 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1));
2883 png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
2884 png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
2886 if (r == trans_color->red && g == trans_color->green &&
2887 b == trans_color->blue)
2889 *sp = (png_byte)((background->red >> 8) & 0xff);
2890 *(sp + 1) = (png_byte)(background->red & 0xff);
2891 *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
2892 *(sp + 3) = (png_byte)(background->green & 0xff);
2893 *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
2894 *(sp + 5) = (png_byte)(background->blue & 0xff);
2902 case PNG_COLOR_TYPE_GRAY_ALPHA:
2904 if (row_info->bit_depth == 8)
2906 #ifdef PNG_READ_GAMMA_SUPPORTED
2907 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
2908 gamma_table != NULL)
2912 for (i = 0; i < row_width; i++, sp += 2, dp++)
2914 png_uint_16 a = *(sp + 1);
2918 *dp = gamma_table[*sp];
2922 /* Background is already in screen gamma */
2923 *dp = (png_byte)background->gray;
2929 v = gamma_to_1[*sp];
2930 png_composite(w, v, a, background_1->gray);
2931 *dp = gamma_from_1[w];
2940 for (i = 0; i < row_width; i++, sp += 2, dp++)
2942 png_byte a = *(sp + 1);
2948 #ifdef PNG_READ_GAMMA_SUPPORTED
2951 *dp = (png_byte)background->gray;
2955 png_composite(*dp, *sp, a, background_1->gray);
2958 *dp = (png_byte)background->gray;
2963 else /* if (png_ptr->bit_depth == 16) */
2965 #ifdef PNG_READ_GAMMA_SUPPORTED
2966 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
2967 gamma_16_to_1 != NULL)
2971 for (i = 0; i < row_width; i++, sp += 4, dp += 2)
2973 png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
2975 if (a == (png_uint_16)0xffff)
2979 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
2980 *dp = (png_byte)((v >> 8) & 0xff);
2981 *(dp + 1) = (png_byte)(v & 0xff);
2983 #ifdef PNG_READ_GAMMA_SUPPORTED
2989 /* Background is already in screen gamma */
2990 *dp = (png_byte)((background->gray >> 8) & 0xff);
2991 *(dp + 1) = (png_byte)(background->gray & 0xff);
2993 #ifdef PNG_READ_GAMMA_SUPPORTED
2996 png_uint_16 g, v, w;
2998 g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
2999 png_composite_16(v, g, a, background_1->gray);
3000 w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
3001 *dp = (png_byte)((w >> 8) & 0xff);
3002 *(dp + 1) = (png_byte)(w & 0xff);
3012 for (i = 0; i < row_width; i++, sp += 4, dp += 2)
3014 png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
3015 if (a == (png_uint_16)0xffff)
3017 png_memcpy(dp, sp, 2);
3019 #ifdef PNG_READ_GAMMA_SUPPORTED
3025 *dp = (png_byte)((background->gray >> 8) & 0xff);
3026 *(dp + 1) = (png_byte)(background->gray & 0xff);
3028 #ifdef PNG_READ_GAMMA_SUPPORTED
3033 g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3034 png_composite_16(v, g, a, background_1->gray);
3035 *dp = (png_byte)((v >> 8) & 0xff);
3036 *(dp + 1) = (png_byte)(v & 0xff);
3045 case PNG_COLOR_TYPE_RGB_ALPHA:
3047 if (row_info->bit_depth == 8)
3049 #ifdef PNG_READ_GAMMA_SUPPORTED
3050 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3051 gamma_table != NULL)
3055 for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3057 png_byte a = *(sp + 3);
3061 *dp = gamma_table[*sp];
3062 *(dp + 1) = gamma_table[*(sp + 1)];
3063 *(dp + 2) = gamma_table[*(sp + 2)];
3067 /* Background is already in screen gamma */
3068 *dp = (png_byte)background->red;
3069 *(dp + 1) = (png_byte)background->green;
3070 *(dp + 2) = (png_byte)background->blue;
3076 v = gamma_to_1[*sp];
3077 png_composite(w, v, a, background_1->red);
3078 *dp = gamma_from_1[w];
3079 v = gamma_to_1[*(sp + 1)];
3080 png_composite(w, v, a, background_1->green);
3081 *(dp + 1) = gamma_from_1[w];
3082 v = gamma_to_1[*(sp + 2)];
3083 png_composite(w, v, a, background_1->blue);
3084 *(dp + 2) = gamma_from_1[w];
3093 for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3095 png_byte a = *(sp + 3);
3100 *(dp + 1) = *(sp + 1);
3101 *(dp + 2) = *(sp + 2);
3105 *dp = (png_byte)background->red;
3106 *(dp + 1) = (png_byte)background->green;
3107 *(dp + 2) = (png_byte)background->blue;
3111 png_composite(*dp, *sp, a, background->red);
3112 png_composite(*(dp + 1), *(sp + 1), a,
3114 png_composite(*(dp + 2), *(sp + 2), a,
3120 else /* if (row_info->bit_depth == 16) */
3122 #ifdef PNG_READ_GAMMA_SUPPORTED
3123 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3124 gamma_16_to_1 != NULL)
3128 for (i = 0; i < row_width; i++, sp += 8, dp += 6)
3130 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3131 << 8) + (png_uint_16)(*(sp + 7)));
3132 if (a == (png_uint_16)0xffff)
3136 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3137 *dp = (png_byte)((v >> 8) & 0xff);
3138 *(dp + 1) = (png_byte)(v & 0xff);
3139 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3140 *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3141 *(dp + 3) = (png_byte)(v & 0xff);
3142 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3143 *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3144 *(dp + 5) = (png_byte)(v & 0xff);
3148 /* Background is already in screen gamma */
3149 *dp = (png_byte)((background->red >> 8) & 0xff);
3150 *(dp + 1) = (png_byte)(background->red & 0xff);
3151 *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3152 *(dp + 3) = (png_byte)(background->green & 0xff);
3153 *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3154 *(dp + 5) = (png_byte)(background->blue & 0xff);
3158 png_uint_16 v, w, x;
3160 v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3161 png_composite_16(w, v, a, background_1->red);
3162 x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
3163 *dp = (png_byte)((x >> 8) & 0xff);
3164 *(dp + 1) = (png_byte)(x & 0xff);
3165 v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
3166 png_composite_16(w, v, a, background_1->green);
3167 x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
3168 *(dp + 2) = (png_byte)((x >> 8) & 0xff);
3169 *(dp + 3) = (png_byte)(x & 0xff);
3170 v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
3171 png_composite_16(w, v, a, background_1->blue);
3172 x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
3173 *(dp + 4) = (png_byte)((x >> 8) & 0xff);
3174 *(dp + 5) = (png_byte)(x & 0xff);
3183 for (i = 0; i < row_width; i++, sp += 8, dp += 6)
3185 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3186 << 8) + (png_uint_16)(*(sp + 7)));
3187 if (a == (png_uint_16)0xffff)
3189 png_memcpy(dp, sp, 6);
3193 *dp = (png_byte)((background->red >> 8) & 0xff);
3194 *(dp + 1) = (png_byte)(background->red & 0xff);
3195 *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3196 *(dp + 3) = (png_byte)(background->green & 0xff);
3197 *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3198 *(dp + 5) = (png_byte)(background->blue & 0xff);
3204 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3205 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3207 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3210 png_composite_16(v, r, a, background->red);
3211 *dp = (png_byte)((v >> 8) & 0xff);
3212 *(dp + 1) = (png_byte)(v & 0xff);
3213 png_composite_16(v, g, a, background->green);
3214 *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3215 *(dp + 3) = (png_byte)(v & 0xff);
3216 png_composite_16(v, b, a, background->blue);
3217 *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3218 *(dp + 5) = (png_byte)(v & 0xff);
3227 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
3229 row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
3230 row_info->channels--;
3231 row_info->pixel_depth = (png_byte)(row_info->channels *
3232 row_info->bit_depth);
3233 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3239 #ifdef PNG_READ_GAMMA_SUPPORTED
3240 /* Gamma correct the image, avoiding the alpha channel. Make sure
3241 * you do this after you deal with the transparency issue on grayscale
3242 * or RGB images. If your bit depth is 8, use gamma_table, if it
3243 * is 16, use gamma_16_table and gamma_shift. Build these with
3244 * build_gamma_table().
3247 png_do_gamma(png_row_infop row_info, png_bytep row,
3248 png_bytep gamma_table, png_uint_16pp gamma_16_table,
3253 png_uint_32 row_width=row_info->width;
3255 png_debug(1, "in png_do_gamma");
3258 ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
3259 (row_info->bit_depth == 16 && gamma_16_table != NULL)))
3261 switch (row_info->color_type)
3263 case PNG_COLOR_TYPE_RGB:
3265 if (row_info->bit_depth == 8)
3268 for (i = 0; i < row_width; i++)
3270 *sp = gamma_table[*sp];
3272 *sp = gamma_table[*sp];
3274 *sp = gamma_table[*sp];
3278 else /* if (row_info->bit_depth == 16) */
3281 for (i = 0; i < row_width; i++)
3285 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3286 *sp = (png_byte)((v >> 8) & 0xff);
3287 *(sp + 1) = (png_byte)(v & 0xff);
3289 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3290 *sp = (png_byte)((v >> 8) & 0xff);
3291 *(sp + 1) = (png_byte)(v & 0xff);
3293 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3294 *sp = (png_byte)((v >> 8) & 0xff);
3295 *(sp + 1) = (png_byte)(v & 0xff);
3302 case PNG_COLOR_TYPE_RGB_ALPHA:
3304 if (row_info->bit_depth == 8)
3307 for (i = 0; i < row_width; i++)
3309 *sp = gamma_table[*sp];
3311 *sp = gamma_table[*sp];
3313 *sp = gamma_table[*sp];
3318 else /* if (row_info->bit_depth == 16) */
3321 for (i = 0; i < row_width; i++)
3323 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3324 *sp = (png_byte)((v >> 8) & 0xff);
3325 *(sp + 1) = (png_byte)(v & 0xff);
3327 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3328 *sp = (png_byte)((v >> 8) & 0xff);
3329 *(sp + 1) = (png_byte)(v & 0xff);
3331 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3332 *sp = (png_byte)((v >> 8) & 0xff);
3333 *(sp + 1) = (png_byte)(v & 0xff);
3340 case PNG_COLOR_TYPE_GRAY_ALPHA:
3342 if (row_info->bit_depth == 8)
3345 for (i = 0; i < row_width; i++)
3347 *sp = gamma_table[*sp];
3351 else /* if (row_info->bit_depth == 16) */
3354 for (i = 0; i < row_width; i++)
3356 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3357 *sp = (png_byte)((v >> 8) & 0xff);
3358 *(sp + 1) = (png_byte)(v & 0xff);
3365 case PNG_COLOR_TYPE_GRAY:
3367 if (row_info->bit_depth == 2)
3370 for (i = 0; i < row_width; i += 4)
3378 ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)|
3379 ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
3380 ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
3381 ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
3386 if (row_info->bit_depth == 4)
3389 for (i = 0; i < row_width; i += 2)
3391 int msb = *sp & 0xf0;
3392 int lsb = *sp & 0x0f;
3394 *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
3395 | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
3400 else if (row_info->bit_depth == 8)
3403 for (i = 0; i < row_width; i++)
3405 *sp = gamma_table[*sp];
3410 else if (row_info->bit_depth == 16)
3413 for (i = 0; i < row_width; i++)
3415 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3416 *sp = (png_byte)((v >> 8) & 0xff);
3417 *(sp + 1) = (png_byte)(v & 0xff);
3428 #ifdef PNG_READ_EXPAND_SUPPORTED
3429 /* Expands a palette row to an RGB or RGBA row depending
3430 * upon whether you supply trans and num_trans.
3433 png_do_expand_palette(png_row_infop row_info, png_bytep row,
3434 png_colorp palette, png_bytep trans_alpha, int num_trans)
3439 png_uint_32 row_width=row_info->width;
3441 png_debug(1, "in png_do_expand_palette");
3444 row_info->color_type == PNG_COLOR_TYPE_PALETTE)
3446 if (row_info->bit_depth < 8)
3448 switch (row_info->bit_depth)
3452 sp = row + (png_size_t)((row_width - 1) >> 3);
3453 dp = row + (png_size_t)row_width - 1;
3454 shift = 7 - (int)((row_width + 7) & 0x07);
3455 for (i = 0; i < row_width; i++)
3457 if ((*sp >> shift) & 0x01)
3476 sp = row + (png_size_t)((row_width - 1) >> 2);
3477 dp = row + (png_size_t)row_width - 1;
3478 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3479 for (i = 0; i < row_width; i++)
3481 value = (*sp >> shift) & 0x03;
3482 *dp = (png_byte)value;
3498 sp = row + (png_size_t)((row_width - 1) >> 1);
3499 dp = row + (png_size_t)row_width - 1;
3500 shift = (int)((row_width & 0x01) << 2);
3501 for (i = 0; i < row_width; i++)
3503 value = (*sp >> shift) & 0x0f;
3504 *dp = (png_byte)value;
3518 row_info->bit_depth = 8;
3519 row_info->pixel_depth = 8;
3520 row_info->rowbytes = row_width;
3522 switch (row_info->bit_depth)
3526 if (trans_alpha != NULL)
3528 sp = row + (png_size_t)row_width - 1;
3529 dp = row + (png_size_t)(row_width << 2) - 1;
3531 for (i = 0; i < row_width; i++)
3533 if ((int)(*sp) >= num_trans)
3536 *dp-- = trans_alpha[*sp];
3537 *dp-- = palette[*sp].blue;
3538 *dp-- = palette[*sp].green;
3539 *dp-- = palette[*sp].red;
3542 row_info->bit_depth = 8;
3543 row_info->pixel_depth = 32;
3544 row_info->rowbytes = row_width * 4;
3545 row_info->color_type = 6;
3546 row_info->channels = 4;
3550 sp = row + (png_size_t)row_width - 1;
3551 dp = row + (png_size_t)(row_width * 3) - 1;
3553 for (i = 0; i < row_width; i++)
3555 *dp-- = palette[*sp].blue;
3556 *dp-- = palette[*sp].green;
3557 *dp-- = palette[*sp].red;
3561 row_info->bit_depth = 8;
3562 row_info->pixel_depth = 24;
3563 row_info->rowbytes = row_width * 3;
3564 row_info->color_type = 2;
3565 row_info->channels = 3;
3573 /* If the bit depth < 8, it is expanded to 8. Also, if the already
3574 * expanded transparency value is supplied, an alpha channel is built.
3577 png_do_expand(png_row_infop row_info, png_bytep row,
3578 png_color_16p trans_value)
3583 png_uint_32 row_width=row_info->width;
3585 png_debug(1, "in png_do_expand");
3588 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
3590 png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
3592 if (row_info->bit_depth < 8)
3594 switch (row_info->bit_depth)
3598 gray = (png_uint_16)((gray&0x01)*0xff);
3599 sp = row + (png_size_t)((row_width - 1) >> 3);
3600 dp = row + (png_size_t)row_width - 1;
3601 shift = 7 - (int)((row_width + 7) & 0x07);
3602 for (i = 0; i < row_width; i++)
3604 if ((*sp >> shift) & 0x01)
3623 gray = (png_uint_16)((gray&0x03)*0x55);
3624 sp = row + (png_size_t)((row_width - 1) >> 2);
3625 dp = row + (png_size_t)row_width - 1;
3626 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3627 for (i = 0; i < row_width; i++)
3629 value = (*sp >> shift) & 0x03;
3630 *dp = (png_byte)(value | (value << 2) | (value << 4) |
3647 gray = (png_uint_16)((gray&0x0f)*0x11);
3648 sp = row + (png_size_t)((row_width - 1) >> 1);
3649 dp = row + (png_size_t)row_width - 1;
3650 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
3651 for (i = 0; i < row_width; i++)
3653 value = (*sp >> shift) & 0x0f;
3654 *dp = (png_byte)(value | (value << 4));
3669 row_info->bit_depth = 8;
3670 row_info->pixel_depth = 8;
3671 row_info->rowbytes = row_width;
3674 if (trans_value != NULL)
3676 if (row_info->bit_depth == 8)
3679 sp = row + (png_size_t)row_width - 1;
3680 dp = row + (png_size_t)(row_width << 1) - 1;
3681 for (i = 0; i < row_width; i++)
3691 else if (row_info->bit_depth == 16)
3693 png_byte gray_high = (gray >> 8) & 0xff;
3694 png_byte gray_low = gray & 0xff;
3695 sp = row + row_info->rowbytes - 1;
3696 dp = row + (row_info->rowbytes << 1) - 1;
3697 for (i = 0; i < row_width; i++)
3699 if (*(sp - 1) == gray_high && *(sp) == gray_low)
3714 row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3715 row_info->channels = 2;
3716 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
3717 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
3721 else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
3723 if (row_info->bit_depth == 8)
3725 png_byte red = trans_value->red & 0xff;
3726 png_byte green = trans_value->green & 0xff;
3727 png_byte blue = trans_value->blue & 0xff;
3728 sp = row + (png_size_t)row_info->rowbytes - 1;
3729 dp = row + (png_size_t)(row_width << 2) - 1;
3730 for (i = 0; i < row_width; i++)
3732 if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
3741 else if (row_info->bit_depth == 16)
3743 png_byte red_high = (trans_value->red >> 8) & 0xff;
3744 png_byte green_high = (trans_value->green >> 8) & 0xff;
3745 png_byte blue_high = (trans_value->blue >> 8) & 0xff;
3746 png_byte red_low = trans_value->red & 0xff;
3747 png_byte green_low = trans_value->green & 0xff;
3748 png_byte blue_low = trans_value->blue & 0xff;
3749 sp = row + row_info->rowbytes - 1;
3750 dp = row + (png_size_t)(row_width << 3) - 1;
3751 for (i = 0; i < row_width; i++)
3753 if (*(sp - 5) == red_high &&
3754 *(sp - 4) == red_low &&
3755 *(sp - 3) == green_high &&
3756 *(sp - 2) == green_low &&
3757 *(sp - 1) == blue_high &&
3776 row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
3777 row_info->channels = 4;
3778 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
3779 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3785 #ifdef PNG_READ_DITHER_SUPPORTED
3787 png_do_dither(png_row_infop row_info, png_bytep row,
3788 png_bytep palette_lookup, png_bytep dither_lookup)
3792 png_uint_32 row_width=row_info->width;
3794 png_debug(1, "in png_do_dither");
3797 if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
3798 palette_lookup && row_info->bit_depth == 8)
3803 for (i = 0; i < row_width; i++)
3809 /* This looks real messy, but the compiler will reduce
3810 * it down to a reasonable formula. For example, with
3811 * 5 bits per color, we get:
3812 * p = (((r >> 3) & 0x1f) << 10) |
3813 * (((g >> 3) & 0x1f) << 5) |
3814 * ((b >> 3) & 0x1f);
3816 p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
3817 ((1 << PNG_DITHER_RED_BITS) - 1)) <<
3818 (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
3819 (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
3820 ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
3821 (PNG_DITHER_BLUE_BITS)) |
3822 ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
3823 ((1 << PNG_DITHER_BLUE_BITS) - 1));
3825 *dp++ = palette_lookup[p];
3827 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
3828 row_info->channels = 1;
3829 row_info->pixel_depth = row_info->bit_depth;
3830 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3832 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
3833 palette_lookup != NULL && row_info->bit_depth == 8)
3838 for (i = 0; i < row_width; i++)
3845 p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
3846 ((1 << PNG_DITHER_RED_BITS) - 1)) <<
3847 (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
3848 (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
3849 ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
3850 (PNG_DITHER_BLUE_BITS)) |
3851 ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
3852 ((1 << PNG_DITHER_BLUE_BITS) - 1));
3854 *dp++ = palette_lookup[p];
3856 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
3857 row_info->channels = 1;
3858 row_info->pixel_depth = row_info->bit_depth;
3859 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3861 else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
3862 dither_lookup && row_info->bit_depth == 8)
3865 for (i = 0; i < row_width; i++, sp++)
3867 *sp = dither_lookup[*sp];
3874 #ifdef PNG_FLOATING_POINT_SUPPORTED
3875 #ifdef PNG_READ_GAMMA_SUPPORTED
3876 static PNG_CONST int png_gamma_shift[] =
3877 {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00};
3879 /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit
3880 * tables, we don't make a full table if we are reducing to 8-bit in
3881 * the future. Note also how the gamma_16 tables are segmented so that
3882 * we don't need to allocate > 64K chunks for a full 16-bit table.
3884 * See the PNG extensions document for an integer algorithm for creating
3885 * the gamma tables. Maybe we will implement that here someday.
3887 * We should only reach this point if
3889 * the file_gamma is known (i.e., the gAMA or sRGB chunk is present,
3890 * or the application has provided a file_gamma)
3894 * the screen_gamma is known
3898 * RGB_to_gray transformation is being performed
3903 * the screen_gamma is different from the reciprocal of the
3904 * file_gamma by more than the specified threshold
3908 * a background color has been specified and the file_gamma
3909 * and screen_gamma are not 1.0, within the specified threshold.
3914 png_build_gamma_table(png_structp png_ptr, png_byte bit_depth)
3916 png_debug(1, "in png_build_gamma_table");
3923 if (png_ptr->screen_gamma > .000001)
3924 g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
3929 png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
3932 for (i = 0; i < 256; i++)
3934 png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,
3938 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
3939 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
3940 if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY))
3943 g = 1.0 / (png_ptr->gamma);
3945 png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
3948 for (i = 0; i < 256; i++)
3950 png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,
3955 png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
3958 if (png_ptr->screen_gamma > 0.000001)
3959 g = 1.0 / png_ptr->screen_gamma;
3962 g = png_ptr->gamma; /* Probably doing rgb_to_gray */
3964 for (i = 0; i < 256; i++)
3966 png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
3971 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
3976 int i, j, shift, num;
3980 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
3982 sig_bit = (int)png_ptr->sig_bit.red;
3984 if ((int)png_ptr->sig_bit.green > sig_bit)
3985 sig_bit = png_ptr->sig_bit.green;
3987 if ((int)png_ptr->sig_bit.blue > sig_bit)
3988 sig_bit = png_ptr->sig_bit.blue;
3992 sig_bit = (int)png_ptr->sig_bit.gray;
3996 shift = 16 - sig_bit;
4001 if (png_ptr->transformations & PNG_16_TO_8)
4003 if (shift < (16 - PNG_MAX_GAMMA_8))
4004 shift = (16 - PNG_MAX_GAMMA_8);
4013 png_ptr->gamma_shift = (png_byte)shift;
4015 num = (1 << (8 - shift));
4017 if (png_ptr->screen_gamma > .000001)
4018 g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
4022 png_ptr->gamma_16_table = (png_uint_16pp)png_calloc(png_ptr,
4023 (png_uint_32)(num * png_sizeof(png_uint_16p)));
4025 if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))
4028 png_uint_32 last, max;
4030 for (i = 0; i < num; i++)
4032 png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
4033 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4038 for (i = 0; i < 256; i++)
4040 fout = ((double)i + 0.5) / 256.0;
4042 max = (png_uint_32)(fin * (double)((png_uint_32)num << 8));
4045 png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
4046 [(int)(last >> (8 - shift))] = (png_uint_16)(
4047 (png_uint_16)i | ((png_uint_16)i << 8));
4051 while (last < ((png_uint_32)num << 8))
4053 png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
4054 [(int)(last >> (8 - shift))] = (png_uint_16)65535L;
4060 for (i = 0; i < num; i++)
4062 png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
4063 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4065 ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);
4067 for (j = 0; j < 256; j++)
4069 png_ptr->gamma_16_table[i][j] =
4070 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
4071 65535.0, g) * 65535.0 + .5);
4076 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4077 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4078 if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))
4081 g = 1.0 / (png_ptr->gamma);
4083 png_ptr->gamma_16_to_1 = (png_uint_16pp)png_calloc(png_ptr,
4084 (png_uint_32)(num * png_sizeof(png_uint_16p )));
4086 for (i = 0; i < num; i++)
4088 png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,
4089 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4091 ig = (((png_uint_32)i *
4092 (png_uint_32)png_gamma_shift[shift]) >> 4);
4093 for (j = 0; j < 256; j++)
4095 png_ptr->gamma_16_to_1[i][j] =
4096 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
4097 65535.0, g) * 65535.0 + .5);
4101 if (png_ptr->screen_gamma > 0.000001)
4102 g = 1.0 / png_ptr->screen_gamma;
4105 g = png_ptr->gamma; /* Probably doing rgb_to_gray */
4107 png_ptr->gamma_16_from_1 = (png_uint_16pp)png_calloc(png_ptr,
4108 (png_uint_32)(num * png_sizeof(png_uint_16p)));
4110 for (i = 0; i < num; i++)
4112 png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
4113 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4115 ig = (((png_uint_32)i *
4116 (png_uint_32)png_gamma_shift[shift]) >> 4);
4118 for (j = 0; j < 256; j++)
4120 png_ptr->gamma_16_from_1[i][j] =
4121 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
4122 65535.0, g) * 65535.0 + .5);
4126 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
4130 /* To do: install integer version of png_build_gamma_table here */
4133 #ifdef PNG_MNG_FEATURES_SUPPORTED
4134 /* Undoes intrapixel differencing */
4136 png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
4138 png_debug(1, "in png_do_read_intrapixel");
4141 (row_info->color_type & PNG_COLOR_MASK_COLOR))
4143 int bytes_per_pixel;
4144 png_uint_32 row_width = row_info->width;
4145 if (row_info->bit_depth == 8)
4150 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4151 bytes_per_pixel = 3;
4153 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4154 bytes_per_pixel = 4;
4159 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4161 *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff);
4162 *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff);
4165 else if (row_info->bit_depth == 16)
4170 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4171 bytes_per_pixel = 6;
4173 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4174 bytes_per_pixel = 8;
4179 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4181 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
4182 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
4183 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
4184 png_uint_32 red = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);
4185 png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);
4186 *(rp ) = (png_byte)((red >> 8) & 0xff);
4187 *(rp+1) = (png_byte)(red & 0xff);
4188 *(rp+4) = (png_byte)((blue >> 8) & 0xff);
4189 *(rp+5) = (png_byte)(blue & 0xff);
4194 #endif /* PNG_MNG_FEATURES_SUPPORTED */
4195 #endif /* PNG_READ_SUPPORTED */