Add memtest support.
[syslinux-debian/hramrach.git] / com32 / lib / libpng / pngrtran.c
blobaf1aa8e4a8c61f9b47152f06072b2ba1bb5a706f
2 /* pngrtran.c - transforms the data in a row for PNG readers
4 * Last changed in libpng 1.2.43 [February 25, 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
16 * in pngtrans.c.
19 #define PNG_INTERNAL
20 #define PNG_NO_PEDANTIC_WARNINGS
21 #include "png.h"
22 #ifdef PNG_READ_SUPPORTED
24 /* Set the action on getting a CRC error for an ancillary or critical chunk. */
25 void PNGAPI
26 png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
28 png_debug(1, "in png_set_crc_action");
30 if (png_ptr == NULL)
31 return;
33 /* Tell libpng how we react to CRC errors in critical chunks */
34 switch (crit_action)
36 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
37 break;
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;
42 break;
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;
48 break;
50 case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
51 png_warning(png_ptr,
52 "Can't discard critical data on CRC error.");
53 case PNG_CRC_ERROR_QUIT: /* Error/quit */
55 case PNG_CRC_DEFAULT:
56 default:
57 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
58 break;
61 /* Tell libpng how we react to CRC errors in ancillary chunks */
62 switch (ancil_action)
64 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
65 break;
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;
70 break;
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;
76 break;
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;
81 break;
83 case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
85 case PNG_CRC_DEFAULT:
86 default:
87 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
88 break;
92 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
93 defined(PNG_FLOATING_POINT_SUPPORTED)
94 /* Handle alpha and tRNS via a background color */
95 void PNGAPI
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");
102 if (png_ptr == NULL)
103 return;
104 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
106 png_warning(png_ptr, "Application must supply a known background gamma");
107 return;
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);
117 #endif
119 #ifdef PNG_READ_16_TO_8_SUPPORTED
120 /* Strip 16 bit depth files to 8 bit depth */
121 void PNGAPI
122 png_set_strip_16(png_structp png_ptr)
124 png_debug(1, "in png_set_strip_16");
126 if (png_ptr == NULL)
127 return;
128 png_ptr->transformations |= PNG_16_TO_8;
130 #endif
132 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
133 void PNGAPI
134 png_set_strip_alpha(png_structp png_ptr)
136 png_debug(1, "in png_set_strip_alpha");
138 if (png_ptr == NULL)
139 return;
140 png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
142 #endif
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;
157 png_byte left;
158 png_byte right;
159 } png_dsort;
160 typedef png_dsort FAR * png_dsortp;
161 typedef png_dsort FAR * FAR * png_dsortpp;
163 void PNGAPI
164 png_set_dither(png_structp png_ptr, png_colorp palette,
165 int num_palette, int maximum_colors, png_uint_16p histogram,
166 int full_dither)
168 png_debug(1, "in png_set_dither");
170 if (png_ptr == NULL)
171 return;
172 png_ptr->transformations |= PNG_DITHER;
174 if (!full_dither)
176 int i;
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.
192 int i;
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
206 * least used.
209 for (i = num_palette - 1; i >= maximum_colors; i--)
211 int done; /* To stop early if the list is pre-sorted */
212 int j;
214 done = 1;
215 for (j = 0; j < i; j++)
217 if (histogram[png_ptr->dither_sort[j]]
218 < histogram[png_ptr->dither_sort[j + 1]])
220 png_byte t;
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;
225 done = 0;
228 if (done)
229 break;
232 /* Swap the palette around, and set up a table, if necessary */
233 if (full_dither)
235 int j = num_palette;
237 /* Put all the useful colors within the max, but don't
238 * move the others.
240 for (i = 0; i < maximum_colors; i++)
242 if ((int)png_ptr->dither_sort[i] >= maximum_colors)
245 j--;
246 while ((int)png_ptr->dither_sort[j] >= maximum_colors);
247 palette[i] = palette[j];
251 else
253 int j = num_palette;
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)
263 png_color tmp_color;
266 j--;
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++)
290 int d;
292 d = PNG_COLOR_DIST(palette[d_index], palette[k]);
294 if (d < min_d)
296 min_d = d;
297 min_k = 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;
308 else
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]
318 int i;
319 int max_d;
320 int num_new_palette;
321 png_dsortp t;
322 png_dsortpp hash;
324 t = NULL;
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.
352 max_d = 96;
354 while (num_new_palette > maximum_colors)
356 for (i = 0; i < num_new_palette - 1; i++)
358 int j;
360 for (j = i + 1; j < num_new_palette; j++)
362 int d;
364 d = PNG_COLOR_DIST(palette[i], palette[j]);
366 if (d <= max_d)
369 t = (png_dsortp)png_malloc_warn(png_ptr,
370 (png_uint_32)(png_sizeof(png_dsort)));
371 if (t == NULL)
372 break;
373 t->next = hash[d];
374 t->left = (png_byte)i;
375 t->right = (png_byte)j;
376 hash[d] = t;
379 if (t == NULL)
380 break;
383 if (t != NULL)
384 for (i = 0; i <= max_d; i++)
386 if (hash[i] != NULL)
388 png_dsortp p;
390 for (p = hash[i]; p; p = p->next)
392 if ((int)png_ptr->index_to_palette[p->left]
393 < num_new_palette &&
394 (int)png_ptr->index_to_palette[p->right]
395 < num_new_palette)
397 int j, next_j;
399 if (num_new_palette & 0x01)
401 j = p->left;
402 next_j = p->right;
404 else
406 j = p->right;
407 next_j = p->left;
410 num_new_palette--;
411 palette[png_ptr->index_to_palette[j]]
412 = palette[num_new_palette];
413 if (!full_dither)
415 int k;
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] ==
424 num_new_palette)
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] =
436 (png_byte)num_new_palette;
437 png_ptr->palette_to_index[num_new_palette] =
438 (png_byte)j;
440 if (num_new_palette <= maximum_colors)
441 break;
443 if (num_new_palette <= maximum_colors)
444 break;
448 for (i = 0; i < 769; i++)
450 if (hash[i] != NULL)
452 png_dsortp p = hash[i];
453 while (p)
455 t = p->next;
456 png_free(png_ptr, p);
457 p = t;
460 hash[i] = 0;
462 max_d += 96;
464 png_free(png_ptr, hash);
465 png_free(png_ptr, png_ptr->palette_to_index);
466 png_free(png_ptr, png_ptr->index_to_palette);
467 png_ptr->palette_to_index = NULL;
468 png_ptr->index_to_palette = NULL;
470 num_palette = maximum_colors;
472 if (png_ptr->palette == NULL)
474 png_ptr->palette = palette;
476 png_ptr->num_palette = (png_uint_16)num_palette;
478 if (full_dither)
480 int i;
481 png_bytep distance;
482 int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS +
483 PNG_DITHER_BLUE_BITS;
484 int num_red = (1 << PNG_DITHER_RED_BITS);
485 int num_green = (1 << PNG_DITHER_GREEN_BITS);
486 int num_blue = (1 << PNG_DITHER_BLUE_BITS);
487 png_size_t num_entries = ((png_size_t)1 << total_bits);
489 png_ptr->palette_lookup = (png_bytep )png_calloc(png_ptr,
490 (png_uint_32)(num_entries * png_sizeof(png_byte)));
492 distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
493 png_sizeof(png_byte)));
494 png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
496 for (i = 0; i < num_palette; i++)
498 int ir, ig, ib;
499 int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS));
500 int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS));
501 int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS));
503 for (ir = 0; ir < num_red; ir++)
505 /* int dr = abs(ir - r); */
506 int dr = ((ir > r) ? ir - r : r - ir);
507 int index_r = (ir << (PNG_DITHER_BLUE_BITS +
508 PNG_DITHER_GREEN_BITS));
510 for (ig = 0; ig < num_green; ig++)
512 /* int dg = abs(ig - g); */
513 int dg = ((ig > g) ? ig - g : g - ig);
514 int dt = dr + dg;
515 int dm = ((dr > dg) ? dr : dg);
516 int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS);
518 for (ib = 0; ib < num_blue; ib++)
520 int d_index = index_g | ib;
521 /* int db = abs(ib - b); */
522 int db = ((ib > b) ? ib - b : b - ib);
523 int dmax = ((dm > db) ? dm : db);
524 int d = dmax + dt + db;
526 if (d < (int)distance[d_index])
528 distance[d_index] = (png_byte)d;
529 png_ptr->palette_lookup[d_index] = (png_byte)i;
536 png_free(png_ptr, distance);
539 #endif
541 #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
542 /* Transform the image from the file_gamma to the screen_gamma. We
543 * only do transformations on images where the file_gamma and screen_gamma
544 * are not close reciprocals, otherwise it slows things down slightly, and
545 * also needlessly introduces small errors.
547 * We will turn off gamma transformation later if no semitransparent entries
548 * are present in the tRNS array for palette images. We can't do it here
549 * because we don't necessarily have the tRNS chunk yet.
551 void PNGAPI
552 png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
554 png_debug(1, "in png_set_gamma");
556 if (png_ptr == NULL)
557 return;
559 if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) ||
560 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
561 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
562 png_ptr->transformations |= PNG_GAMMA;
563 png_ptr->gamma = (float)file_gamma;
564 png_ptr->screen_gamma = (float)scrn_gamma;
566 #endif
568 #ifdef PNG_READ_EXPAND_SUPPORTED
569 /* Expand paletted images to RGB, expand grayscale images of
570 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
571 * to alpha channels.
573 void PNGAPI
574 png_set_expand(png_structp png_ptr)
576 png_debug(1, "in png_set_expand");
578 if (png_ptr == NULL)
579 return;
581 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
582 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
585 /* GRR 19990627: the following three functions currently are identical
586 * to png_set_expand(). However, it is entirely reasonable that someone
587 * might wish to expand an indexed image to RGB but *not* expand a single,
588 * fully transparent palette entry to a full alpha channel--perhaps instead
589 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
590 * the transparent color with a particular RGB value, or drop tRNS entirely.
591 * IOW, a future version of the library may make the transformations flag
592 * a bit more fine-grained, with separate bits for each of these three
593 * functions.
595 * More to the point, these functions make it obvious what libpng will be
596 * doing, whereas "expand" can (and does) mean any number of things.
598 * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
599 * to expand only the sample depth but not to expand the tRNS to alpha
600 * and its name was changed to png_set_expand_gray_1_2_4_to_8().
603 /* Expand paletted images to RGB. */
604 void PNGAPI
605 png_set_palette_to_rgb(png_structp png_ptr)
607 png_debug(1, "in png_set_palette_to_rgb");
609 if (png_ptr == NULL)
610 return;
612 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
613 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
616 #ifndef PNG_1_0_X
617 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
618 void PNGAPI
619 png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
621 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
623 if (png_ptr == NULL)
624 return;
626 png_ptr->transformations |= PNG_EXPAND;
627 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
629 #endif
631 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
632 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
633 /* Deprecated as of libpng-1.2.9 */
634 void PNGAPI
635 png_set_gray_1_2_4_to_8(png_structp png_ptr)
637 png_debug(1, "in png_set_gray_1_2_4_to_8");
639 if (png_ptr == NULL)
640 return;
642 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
644 #endif
647 /* Expand tRNS chunks to alpha channels. */
648 void PNGAPI
649 png_set_tRNS_to_alpha(png_structp png_ptr)
651 png_debug(1, "in png_set_tRNS_to_alpha");
653 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
654 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
656 #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
658 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
659 void PNGAPI
660 png_set_gray_to_rgb(png_structp png_ptr)
662 png_debug(1, "in png_set_gray_to_rgb");
664 png_ptr->transformations |= PNG_GRAY_TO_RGB;
665 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
667 #endif
669 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
670 #ifdef PNG_FLOATING_POINT_SUPPORTED
671 /* Convert a RGB image to a grayscale of the same width. This allows us,
672 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
675 void PNGAPI
676 png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
677 double green)
679 int red_fixed = (int)((float)red*100000.0 + 0.5);
680 int green_fixed = (int)((float)green*100000.0 + 0.5);
681 if (png_ptr == NULL)
682 return;
683 png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed);
685 #endif
687 void PNGAPI
688 png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
689 png_fixed_point red, png_fixed_point green)
691 png_debug(1, "in png_set_rgb_to_gray");
693 if (png_ptr == NULL)
694 return;
696 switch(error_action)
698 case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;
699 break;
701 case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
702 break;
704 case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
706 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
707 #ifdef PNG_READ_EXPAND_SUPPORTED
708 png_ptr->transformations |= PNG_EXPAND;
709 #else
711 png_warning(png_ptr,
712 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED.");
713 png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
715 #endif
717 png_uint_16 red_int, green_int;
718 if (red < 0 || green < 0)
720 red_int = 6968; /* .212671 * 32768 + .5 */
721 green_int = 23434; /* .715160 * 32768 + .5 */
723 else if (red + green < 100000L)
725 red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
726 green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
728 else
730 png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
731 red_int = 6968;
732 green_int = 23434;
734 png_ptr->rgb_to_gray_red_coeff = red_int;
735 png_ptr->rgb_to_gray_green_coeff = green_int;
736 png_ptr->rgb_to_gray_blue_coeff =
737 (png_uint_16)(32768 - red_int - green_int);
740 #endif
742 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
743 defined(PNG_LEGACY_SUPPORTED) || \
744 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
745 void PNGAPI
746 png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
747 read_user_transform_fn)
749 png_debug(1, "in png_set_read_user_transform_fn");
751 if (png_ptr == NULL)
752 return;
754 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
755 png_ptr->transformations |= PNG_USER_TRANSFORM;
756 png_ptr->read_user_transform_fn = read_user_transform_fn;
757 #endif
758 #ifdef PNG_LEGACY_SUPPORTED
759 if (read_user_transform_fn)
760 png_warning(png_ptr,
761 "This version of libpng does not support user transforms");
762 #endif
764 #endif
766 /* Initialize everything needed for the read. This includes modifying
767 * the palette.
769 void /* PRIVATE */
770 png_init_read_transformations(png_structp png_ptr)
772 png_debug(1, "in png_init_read_transformations");
774 #ifdef PNG_USELESS_TESTS_SUPPORTED
775 if (png_ptr != NULL)
776 #endif
778 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
779 defined(PNG_READ_SHIFT_SUPPORTED) || \
780 defined(PNG_READ_GAMMA_SUPPORTED)
781 int color_type = png_ptr->color_type;
782 #endif
784 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
786 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
787 /* Detect gray background and attempt to enable optimization
788 * for gray --> RGB case
790 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
791 * RGB_ALPHA (in which case need_expand is superfluous anyway), the
792 * background color might actually be gray yet not be flagged as such.
793 * This is not a problem for the current code, which uses
794 * PNG_BACKGROUND_IS_GRAY only to decide when to do the
795 * png_do_gray_to_rgb() transformation.
797 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
798 !(color_type & PNG_COLOR_MASK_COLOR))
800 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
801 } else if ((png_ptr->transformations & PNG_BACKGROUND) &&
802 !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
803 (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
804 png_ptr->background.red == png_ptr->background.green &&
805 png_ptr->background.red == png_ptr->background.blue)
807 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
808 png_ptr->background.gray = png_ptr->background.red;
810 #endif
812 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
813 (png_ptr->transformations & PNG_EXPAND))
815 if (!(color_type & PNG_COLOR_MASK_COLOR)) /* i.e., GRAY or GRAY_ALPHA */
817 /* Expand background and tRNS chunks */
818 switch (png_ptr->bit_depth)
820 case 1:
821 png_ptr->background.gray *= (png_uint_16)0xff;
822 png_ptr->background.red = png_ptr->background.green
823 = png_ptr->background.blue = png_ptr->background.gray;
824 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
826 png_ptr->trans_values.gray *= (png_uint_16)0xff;
827 png_ptr->trans_values.red = png_ptr->trans_values.green
828 = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
830 break;
832 case 2:
833 png_ptr->background.gray *= (png_uint_16)0x55;
834 png_ptr->background.red = png_ptr->background.green
835 = png_ptr->background.blue = png_ptr->background.gray;
836 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
838 png_ptr->trans_values.gray *= (png_uint_16)0x55;
839 png_ptr->trans_values.red = png_ptr->trans_values.green
840 = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
842 break;
844 case 4:
845 png_ptr->background.gray *= (png_uint_16)0x11;
846 png_ptr->background.red = png_ptr->background.green
847 = png_ptr->background.blue = png_ptr->background.gray;
848 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
850 png_ptr->trans_values.gray *= (png_uint_16)0x11;
851 png_ptr->trans_values.red = png_ptr->trans_values.green
852 = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
854 break;
856 case 8:
858 case 16:
859 png_ptr->background.red = png_ptr->background.green
860 = png_ptr->background.blue = png_ptr->background.gray;
861 break;
864 else if (color_type == PNG_COLOR_TYPE_PALETTE)
866 png_ptr->background.red =
867 png_ptr->palette[png_ptr->background.index].red;
868 png_ptr->background.green =
869 png_ptr->palette[png_ptr->background.index].green;
870 png_ptr->background.blue =
871 png_ptr->palette[png_ptr->background.index].blue;
873 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
874 if (png_ptr->transformations & PNG_INVERT_ALPHA)
876 #ifdef PNG_READ_EXPAND_SUPPORTED
877 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
878 #endif
880 /* Invert the alpha channel (in tRNS) unless the pixels are
881 * going to be expanded, in which case leave it for later
883 int i, istop;
884 istop=(int)png_ptr->num_trans;
885 for (i=0; i<istop; i++)
886 png_ptr->trans[i] = (png_byte)(255 - png_ptr->trans[i]);
889 #endif
893 #endif
895 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
896 png_ptr->background_1 = png_ptr->background;
897 #endif
898 #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
900 if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
901 && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0)
902 < PNG_GAMMA_THRESHOLD))
904 int i, k;
905 k=0;
906 for (i=0; i<png_ptr->num_trans; i++)
908 if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff)
909 k=1; /* Partial transparency is present */
911 if (k == 0)
912 png_ptr->transformations &= ~PNG_GAMMA;
915 if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
916 png_ptr->gamma != 0.0)
918 png_build_gamma_table(png_ptr);
920 #ifdef PNG_READ_BACKGROUND_SUPPORTED
921 if (png_ptr->transformations & PNG_BACKGROUND)
923 if (color_type == PNG_COLOR_TYPE_PALETTE)
925 /* Could skip if no transparency */
926 png_color back, back_1;
927 png_colorp palette = png_ptr->palette;
928 int num_palette = png_ptr->num_palette;
929 int i;
930 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
932 back.red = png_ptr->gamma_table[png_ptr->background.red];
933 back.green = png_ptr->gamma_table[png_ptr->background.green];
934 back.blue = png_ptr->gamma_table[png_ptr->background.blue];
936 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
937 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
938 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
940 else
942 double g, gs;
944 switch (png_ptr->background_gamma_type)
946 case PNG_BACKGROUND_GAMMA_SCREEN:
947 g = (png_ptr->screen_gamma);
948 gs = 1.0;
949 break;
951 case PNG_BACKGROUND_GAMMA_FILE:
952 g = 1.0 / (png_ptr->gamma);
953 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
954 break;
956 case PNG_BACKGROUND_GAMMA_UNIQUE:
957 g = 1.0 / (png_ptr->background_gamma);
958 gs = 1.0 / (png_ptr->background_gamma *
959 png_ptr->screen_gamma);
960 break;
961 default:
962 g = 1.0; /* back_1 */
963 gs = 1.0; /* back */
966 if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD)
968 back.red = (png_byte)png_ptr->background.red;
969 back.green = (png_byte)png_ptr->background.green;
970 back.blue = (png_byte)png_ptr->background.blue;
972 else
974 back.red = (png_byte)(pow(
975 (double)png_ptr->background.red/255, gs) * 255.0 + .5);
976 back.green = (png_byte)(pow(
977 (double)png_ptr->background.green/255, gs) * 255.0
978 + .5);
979 back.blue = (png_byte)(pow(
980 (double)png_ptr->background.blue/255, gs) * 255.0 + .5);
983 back_1.red = (png_byte)(pow(
984 (double)png_ptr->background.red/255, g) * 255.0 + .5);
985 back_1.green = (png_byte)(pow(
986 (double)png_ptr->background.green/255, g) * 255.0 + .5);
987 back_1.blue = (png_byte)(pow(
988 (double)png_ptr->background.blue/255, g) * 255.0 + .5);
990 for (i = 0; i < num_palette; i++)
992 if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff)
994 if (png_ptr->trans[i] == 0)
996 palette[i] = back;
998 else /* if (png_ptr->trans[i] != 0xff) */
1000 png_byte v, w;
1002 v = png_ptr->gamma_to_1[palette[i].red];
1003 png_composite(w, v, png_ptr->trans[i], back_1.red);
1004 palette[i].red = png_ptr->gamma_from_1[w];
1006 v = png_ptr->gamma_to_1[palette[i].green];
1007 png_composite(w, v, png_ptr->trans[i], back_1.green);
1008 palette[i].green = png_ptr->gamma_from_1[w];
1010 v = png_ptr->gamma_to_1[palette[i].blue];
1011 png_composite(w, v, png_ptr->trans[i], back_1.blue);
1012 palette[i].blue = png_ptr->gamma_from_1[w];
1015 else
1017 palette[i].red = png_ptr->gamma_table[palette[i].red];
1018 palette[i].green = png_ptr->gamma_table[palette[i].green];
1019 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1022 /* Prevent the transformations being done again, and make sure
1023 * that the now spurious alpha channel is stripped - the code
1024 * has just reduced background composition and gamma correction
1025 * to a simple alpha channel strip.
1027 png_ptr->transformations &= ~PNG_BACKGROUND;
1028 png_ptr->transformations &= ~PNG_GAMMA;
1029 png_ptr->transformations |= PNG_STRIP_ALPHA;
1031 /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1032 else
1033 /* color_type != PNG_COLOR_TYPE_PALETTE */
1035 double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);
1036 double g = 1.0;
1037 double gs = 1.0;
1039 switch (png_ptr->background_gamma_type)
1041 case PNG_BACKGROUND_GAMMA_SCREEN:
1042 g = (png_ptr->screen_gamma);
1043 gs = 1.0;
1044 break;
1046 case PNG_BACKGROUND_GAMMA_FILE:
1047 g = 1.0 / (png_ptr->gamma);
1048 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
1049 break;
1051 case PNG_BACKGROUND_GAMMA_UNIQUE:
1052 g = 1.0 / (png_ptr->background_gamma);
1053 gs = 1.0 / (png_ptr->background_gamma *
1054 png_ptr->screen_gamma);
1055 break;
1058 png_ptr->background_1.gray = (png_uint_16)(pow(
1059 (double)png_ptr->background.gray / m, g) * m + .5);
1060 png_ptr->background.gray = (png_uint_16)(pow(
1061 (double)png_ptr->background.gray / m, gs) * m + .5);
1063 if ((png_ptr->background.red != png_ptr->background.green) ||
1064 (png_ptr->background.red != png_ptr->background.blue) ||
1065 (png_ptr->background.red != png_ptr->background.gray))
1067 /* RGB or RGBA with color background */
1068 png_ptr->background_1.red = (png_uint_16)(pow(
1069 (double)png_ptr->background.red / m, g) * m + .5);
1070 png_ptr->background_1.green = (png_uint_16)(pow(
1071 (double)png_ptr->background.green / m, g) * m + .5);
1072 png_ptr->background_1.blue = (png_uint_16)(pow(
1073 (double)png_ptr->background.blue / m, g) * m + .5);
1074 png_ptr->background.red = (png_uint_16)(pow(
1075 (double)png_ptr->background.red / m, gs) * m + .5);
1076 png_ptr->background.green = (png_uint_16)(pow(
1077 (double)png_ptr->background.green / m, gs) * m + .5);
1078 png_ptr->background.blue = (png_uint_16)(pow(
1079 (double)png_ptr->background.blue / m, gs) * m + .5);
1081 else
1083 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1084 png_ptr->background_1.red = png_ptr->background_1.green
1085 = png_ptr->background_1.blue = png_ptr->background_1.gray;
1086 png_ptr->background.red = png_ptr->background.green
1087 = png_ptr->background.blue = png_ptr->background.gray;
1091 else
1092 /* Transformation does not include PNG_BACKGROUND */
1093 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1094 if (color_type == PNG_COLOR_TYPE_PALETTE)
1096 png_colorp palette = png_ptr->palette;
1097 int num_palette = png_ptr->num_palette;
1098 int i;
1100 for (i = 0; i < num_palette; i++)
1102 palette[i].red = png_ptr->gamma_table[palette[i].red];
1103 palette[i].green = png_ptr->gamma_table[palette[i].green];
1104 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1107 /* Done the gamma correction. */
1108 png_ptr->transformations &= ~PNG_GAMMA;
1111 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1112 else
1113 #endif
1114 #endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
1115 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1116 /* No GAMMA transformation */
1117 if ((png_ptr->transformations & PNG_BACKGROUND) &&
1118 (color_type == PNG_COLOR_TYPE_PALETTE))
1120 int i;
1121 int istop = (int)png_ptr->num_trans;
1122 png_color back;
1123 png_colorp palette = png_ptr->palette;
1125 back.red = (png_byte)png_ptr->background.red;
1126 back.green = (png_byte)png_ptr->background.green;
1127 back.blue = (png_byte)png_ptr->background.blue;
1129 for (i = 0; i < istop; i++)
1131 if (png_ptr->trans[i] == 0)
1133 palette[i] = back;
1135 else if (png_ptr->trans[i] != 0xff)
1137 /* The png_composite() macro is defined in png.h */
1138 png_composite(palette[i].red, palette[i].red,
1139 png_ptr->trans[i], back.red);
1140 png_composite(palette[i].green, palette[i].green,
1141 png_ptr->trans[i], back.green);
1142 png_composite(palette[i].blue, palette[i].blue,
1143 png_ptr->trans[i], back.blue);
1147 /* Handled alpha, still need to strip the channel. */
1148 png_ptr->transformations &= ~PNG_BACKGROUND;
1149 png_ptr->transformations |= PNG_STRIP_ALPHA;
1151 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1153 #ifdef PNG_READ_SHIFT_SUPPORTED
1154 if ((png_ptr->transformations & PNG_SHIFT) &&
1155 (color_type == PNG_COLOR_TYPE_PALETTE))
1157 png_uint_16 i;
1158 png_uint_16 istop = png_ptr->num_palette;
1159 int sr = 8 - png_ptr->sig_bit.red;
1160 int sg = 8 - png_ptr->sig_bit.green;
1161 int sb = 8 - png_ptr->sig_bit.blue;
1163 if (sr < 0 || sr > 8)
1164 sr = 0;
1165 if (sg < 0 || sg > 8)
1166 sg = 0;
1167 if (sb < 0 || sb > 8)
1168 sb = 0;
1169 for (i = 0; i < istop; i++)
1171 png_ptr->palette[i].red >>= sr;
1172 png_ptr->palette[i].green >>= sg;
1173 png_ptr->palette[i].blue >>= sb;
1176 #endif /* PNG_READ_SHIFT_SUPPORTED */
1178 #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
1179 && !defined(PNG_READ_BACKGROUND_SUPPORTED)
1180 if (png_ptr)
1181 return;
1182 #endif
1185 /* Modify the info structure to reflect the transformations. The
1186 * info should be updated so a PNG file could be written with it,
1187 * assuming the transformations result in valid PNG data.
1189 void /* PRIVATE */
1190 png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
1192 png_debug(1, "in png_read_transform_info");
1194 #ifdef PNG_READ_EXPAND_SUPPORTED
1195 if (png_ptr->transformations & PNG_EXPAND)
1197 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1199 if (png_ptr->num_trans &&
1200 (png_ptr->transformations & PNG_EXPAND_tRNS))
1201 info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1202 else
1203 info_ptr->color_type = PNG_COLOR_TYPE_RGB;
1204 info_ptr->bit_depth = 8;
1205 info_ptr->num_trans = 0;
1207 else
1209 if (png_ptr->num_trans)
1211 if (png_ptr->transformations & PNG_EXPAND_tRNS)
1212 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1214 if (info_ptr->bit_depth < 8)
1215 info_ptr->bit_depth = 8;
1216 info_ptr->num_trans = 0;
1219 #endif
1221 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1222 if (png_ptr->transformations & PNG_BACKGROUND)
1224 info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
1225 info_ptr->num_trans = 0;
1226 info_ptr->background = png_ptr->background;
1228 #endif
1230 #ifdef PNG_READ_GAMMA_SUPPORTED
1231 if (png_ptr->transformations & PNG_GAMMA)
1233 #ifdef PNG_FLOATING_POINT_SUPPORTED
1234 info_ptr->gamma = png_ptr->gamma;
1235 #endif
1236 #ifdef PNG_FIXED_POINT_SUPPORTED
1237 info_ptr->int_gamma = png_ptr->int_gamma;
1238 #endif
1240 #endif
1242 #ifdef PNG_READ_16_TO_8_SUPPORTED
1243 if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
1244 info_ptr->bit_depth = 8;
1245 #endif
1247 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1248 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
1249 info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
1250 #endif
1252 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1253 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1254 info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
1255 #endif
1257 #ifdef PNG_READ_DITHER_SUPPORTED
1258 if (png_ptr->transformations & PNG_DITHER)
1260 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1261 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
1262 png_ptr->palette_lookup && info_ptr->bit_depth == 8)
1264 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
1267 #endif
1269 #ifdef PNG_READ_PACK_SUPPORTED
1270 if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
1271 info_ptr->bit_depth = 8;
1272 #endif
1274 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1275 info_ptr->channels = 1;
1276 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
1277 info_ptr->channels = 3;
1278 else
1279 info_ptr->channels = 1;
1281 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1282 if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
1283 info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
1284 #endif
1286 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1287 info_ptr->channels++;
1289 #ifdef PNG_READ_FILLER_SUPPORTED
1290 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
1291 if ((png_ptr->transformations & PNG_FILLER) &&
1292 ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1293 (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
1295 info_ptr->channels++;
1296 /* If adding a true alpha channel not just filler */
1297 #ifndef PNG_1_0_X
1298 if (png_ptr->transformations & PNG_ADD_ALPHA)
1299 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1300 #endif
1302 #endif
1304 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
1305 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1306 if (png_ptr->transformations & PNG_USER_TRANSFORM)
1308 if (info_ptr->bit_depth < png_ptr->user_transform_depth)
1309 info_ptr->bit_depth = png_ptr->user_transform_depth;
1310 if (info_ptr->channels < png_ptr->user_transform_channels)
1311 info_ptr->channels = png_ptr->user_transform_channels;
1313 #endif
1315 info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
1316 info_ptr->bit_depth);
1318 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
1320 #ifndef PNG_READ_EXPAND_SUPPORTED
1321 if (png_ptr)
1322 return;
1323 #endif
1326 /* Transform the row. The order of transformations is significant,
1327 * and is very touchy. If you add a transformation, take care to
1328 * decide how it fits in with the other transformations here.
1330 void /* PRIVATE */
1331 png_do_read_transformations(png_structp png_ptr)
1333 png_debug(1, "in png_do_read_transformations");
1335 if (png_ptr->row_buf == NULL)
1337 #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
1338 char msg[50];
1340 png_snprintf2(msg, 50,
1341 "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,
1342 png_ptr->pass);
1343 png_error(png_ptr, msg);
1344 #else
1345 png_error(png_ptr, "NULL row buffer");
1346 #endif
1348 #ifdef PNG_WARN_UNINITIALIZED_ROW
1349 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
1350 /* Application has failed to call either png_read_start_image()
1351 * or png_read_update_info() after setting transforms that expand
1352 * pixels. This check added to libpng-1.2.19
1354 #if (PNG_WARN_UNINITIALIZED_ROW==1)
1355 png_error(png_ptr, "Uninitialized row");
1356 #else
1357 png_warning(png_ptr, "Uninitialized row");
1358 #endif
1359 #endif
1361 #ifdef PNG_READ_EXPAND_SUPPORTED
1362 if (png_ptr->transformations & PNG_EXPAND)
1364 if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
1366 png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
1367 png_ptr->palette, png_ptr->trans, png_ptr->num_trans);
1369 else
1371 if (png_ptr->num_trans &&
1372 (png_ptr->transformations & PNG_EXPAND_tRNS))
1373 png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
1374 &(png_ptr->trans_values));
1375 else
1376 png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
1377 NULL);
1380 #endif
1382 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1383 if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
1384 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1385 PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));
1386 #endif
1388 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1389 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1391 int rgb_error =
1392 png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info),
1393 png_ptr->row_buf + 1);
1394 if (rgb_error)
1396 png_ptr->rgb_to_gray_status=1;
1397 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1398 PNG_RGB_TO_GRAY_WARN)
1399 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1400 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1401 PNG_RGB_TO_GRAY_ERR)
1402 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1405 #endif
1407 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
1409 * In most cases, the "simple transparency" should be done prior to doing
1410 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
1411 * pixel is transparent. You would also need to make sure that the
1412 * transparency information is upgraded to RGB.
1414 * To summarize, the current flow is:
1415 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
1416 * with background "in place" if transparent,
1417 * convert to RGB if necessary
1418 * - Gray + alpha -> composite with gray background and remove alpha bytes,
1419 * convert to RGB if necessary
1421 * To support RGB backgrounds for gray images we need:
1422 * - Gray + simple transparency -> convert to RGB + simple transparency,
1423 * compare 3 or 6 bytes and composite with
1424 * background "in place" if transparent
1425 * (3x compare/pixel compared to doing
1426 * composite with gray bkgrnd)
1427 * - Gray + alpha -> convert to RGB + alpha, composite with background and
1428 * remove alpha bytes (3x float
1429 * operations/pixel compared with composite
1430 * on gray background)
1432 * Greg's change will do this. The reason it wasn't done before is for
1433 * performance, as this increases the per-pixel operations. If we would check
1434 * in advance if the background was gray or RGB, and position the gray-to-RGB
1435 * transform appropriately, then it would save a lot of work/time.
1438 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1439 /* If gray -> RGB, do so now only if background is non-gray; else do later
1440 * for performance reasons
1442 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1443 !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1444 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1445 #endif
1447 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1448 if ((png_ptr->transformations & PNG_BACKGROUND) &&
1449 ((png_ptr->num_trans != 0 ) ||
1450 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
1451 png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
1452 &(png_ptr->trans_values), &(png_ptr->background)
1453 #ifdef PNG_READ_GAMMA_SUPPORTED
1454 , &(png_ptr->background_1),
1455 png_ptr->gamma_table, png_ptr->gamma_from_1,
1456 png_ptr->gamma_to_1, png_ptr->gamma_16_table,
1457 png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
1458 png_ptr->gamma_shift
1459 #endif
1461 #endif
1463 #ifdef PNG_READ_GAMMA_SUPPORTED
1464 if ((png_ptr->transformations & PNG_GAMMA) &&
1465 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1466 !((png_ptr->transformations & PNG_BACKGROUND) &&
1467 ((png_ptr->num_trans != 0) ||
1468 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
1469 #endif
1470 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
1471 png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
1472 png_ptr->gamma_table, png_ptr->gamma_16_table,
1473 png_ptr->gamma_shift);
1474 #endif
1476 #ifdef PNG_READ_16_TO_8_SUPPORTED
1477 if (png_ptr->transformations & PNG_16_TO_8)
1478 png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
1479 #endif
1481 #ifdef PNG_READ_DITHER_SUPPORTED
1482 if (png_ptr->transformations & PNG_DITHER)
1484 png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
1485 png_ptr->palette_lookup, png_ptr->dither_index);
1486 if (png_ptr->row_info.rowbytes == (png_uint_32)0)
1487 png_error(png_ptr, "png_do_dither returned rowbytes=0");
1489 #endif
1491 #ifdef PNG_READ_INVERT_SUPPORTED
1492 if (png_ptr->transformations & PNG_INVERT_MONO)
1493 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
1494 #endif
1496 #ifdef PNG_READ_SHIFT_SUPPORTED
1497 if (png_ptr->transformations & PNG_SHIFT)
1498 png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
1499 &(png_ptr->shift));
1500 #endif
1502 #ifdef PNG_READ_PACK_SUPPORTED
1503 if (png_ptr->transformations & PNG_PACK)
1504 png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
1505 #endif
1507 #ifdef PNG_READ_BGR_SUPPORTED
1508 if (png_ptr->transformations & PNG_BGR)
1509 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
1510 #endif
1512 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1513 if (png_ptr->transformations & PNG_PACKSWAP)
1514 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1515 #endif
1517 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1518 /* If gray -> RGB, do so now only if we did not do so above */
1519 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1520 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1521 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1522 #endif
1524 #ifdef PNG_READ_FILLER_SUPPORTED
1525 if (png_ptr->transformations & PNG_FILLER)
1526 png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1527 (png_uint_32)png_ptr->filler, png_ptr->flags);
1528 #endif
1530 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1531 if (png_ptr->transformations & PNG_INVERT_ALPHA)
1532 png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1533 #endif
1535 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1536 if (png_ptr->transformations & PNG_SWAP_ALPHA)
1537 png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1538 #endif
1540 #ifdef PNG_READ_SWAP_SUPPORTED
1541 if (png_ptr->transformations & PNG_SWAP_BYTES)
1542 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1543 #endif
1545 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1546 if (png_ptr->transformations & PNG_USER_TRANSFORM)
1548 if (png_ptr->read_user_transform_fn != NULL)
1549 (*(png_ptr->read_user_transform_fn)) /* User read transform function */
1550 (png_ptr, /* png_ptr */
1551 &(png_ptr->row_info), /* row_info: */
1552 /* png_uint_32 width; width of row */
1553 /* png_uint_32 rowbytes; number of bytes in row */
1554 /* png_byte color_type; color type of pixels */
1555 /* png_byte bit_depth; bit depth of samples */
1556 /* png_byte channels; number of channels (1-4) */
1557 /* png_byte pixel_depth; bits per pixel (depth*channels) */
1558 png_ptr->row_buf + 1); /* start of pixel data for row */
1559 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
1560 if (png_ptr->user_transform_depth)
1561 png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
1562 if (png_ptr->user_transform_channels)
1563 png_ptr->row_info.channels = png_ptr->user_transform_channels;
1564 #endif
1565 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
1566 png_ptr->row_info.channels);
1567 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
1568 png_ptr->row_info.width);
1570 #endif
1574 #ifdef PNG_READ_PACK_SUPPORTED
1575 /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
1576 * without changing the actual values. Thus, if you had a row with
1577 * a bit depth of 1, you would end up with bytes that only contained
1578 * the numbers 0 or 1. If you would rather they contain 0 and 255, use
1579 * png_do_shift() after this.
1581 void /* PRIVATE */
1582 png_do_unpack(png_row_infop row_info, png_bytep row)
1584 png_debug(1, "in png_do_unpack");
1586 #ifdef PNG_USELESS_TESTS_SUPPORTED
1587 if (row != NULL && row_info != NULL && row_info->bit_depth < 8)
1588 #else
1589 if (row_info->bit_depth < 8)
1590 #endif
1592 png_uint_32 i;
1593 png_uint_32 row_width=row_info->width;
1595 switch (row_info->bit_depth)
1597 case 1:
1599 png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
1600 png_bytep dp = row + (png_size_t)row_width - 1;
1601 png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
1602 for (i = 0; i < row_width; i++)
1604 *dp = (png_byte)((*sp >> shift) & 0x01);
1605 if (shift == 7)
1607 shift = 0;
1608 sp--;
1610 else
1611 shift++;
1613 dp--;
1615 break;
1618 case 2:
1621 png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
1622 png_bytep dp = row + (png_size_t)row_width - 1;
1623 png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
1624 for (i = 0; i < row_width; i++)
1626 *dp = (png_byte)((*sp >> shift) & 0x03);
1627 if (shift == 6)
1629 shift = 0;
1630 sp--;
1632 else
1633 shift += 2;
1635 dp--;
1637 break;
1640 case 4:
1642 png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
1643 png_bytep dp = row + (png_size_t)row_width - 1;
1644 png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
1645 for (i = 0; i < row_width; i++)
1647 *dp = (png_byte)((*sp >> shift) & 0x0f);
1648 if (shift == 4)
1650 shift = 0;
1651 sp--;
1653 else
1654 shift = 4;
1656 dp--;
1658 break;
1661 row_info->bit_depth = 8;
1662 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1663 row_info->rowbytes = row_width * row_info->channels;
1666 #endif
1668 #ifdef PNG_READ_SHIFT_SUPPORTED
1669 /* Reverse the effects of png_do_shift. This routine merely shifts the
1670 * pixels back to their significant bits values. Thus, if you have
1671 * a row of bit depth 8, but only 5 are significant, this will shift
1672 * the values back to 0 through 31.
1674 void /* PRIVATE */
1675 png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
1677 png_debug(1, "in png_do_unshift");
1679 if (
1680 #ifdef PNG_USELESS_TESTS_SUPPORTED
1681 row != NULL && row_info != NULL && sig_bits != NULL &&
1682 #endif
1683 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
1685 int shift[4];
1686 int channels = 0;
1687 int c;
1688 png_uint_16 value = 0;
1689 png_uint_32 row_width = row_info->width;
1691 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
1693 shift[channels++] = row_info->bit_depth - sig_bits->red;
1694 shift[channels++] = row_info->bit_depth - sig_bits->green;
1695 shift[channels++] = row_info->bit_depth - sig_bits->blue;
1697 else
1699 shift[channels++] = row_info->bit_depth - sig_bits->gray;
1701 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
1703 shift[channels++] = row_info->bit_depth - sig_bits->alpha;
1706 for (c = 0; c < channels; c++)
1708 if (shift[c] <= 0)
1709 shift[c] = 0;
1710 else
1711 value = 1;
1714 if (!value)
1715 return;
1717 switch (row_info->bit_depth)
1719 case 2:
1721 png_bytep bp;
1722 png_uint_32 i;
1723 png_uint_32 istop = row_info->rowbytes;
1725 for (bp = row, i = 0; i < istop; i++)
1727 *bp >>= 1;
1728 *bp++ &= 0x55;
1730 break;
1733 case 4:
1735 png_bytep bp = row;
1736 png_uint_32 i;
1737 png_uint_32 istop = row_info->rowbytes;
1738 png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
1739 (png_byte)((int)0xf >> shift[0]));
1741 for (i = 0; i < istop; i++)
1743 *bp >>= shift[0];
1744 *bp++ &= mask;
1746 break;
1749 case 8:
1751 png_bytep bp = row;
1752 png_uint_32 i;
1753 png_uint_32 istop = row_width * channels;
1755 for (i = 0; i < istop; i++)
1757 *bp++ >>= shift[i%channels];
1759 break;
1762 case 16:
1764 png_bytep bp = row;
1765 png_uint_32 i;
1766 png_uint_32 istop = channels * row_width;
1768 for (i = 0; i < istop; i++)
1770 value = (png_uint_16)((*bp << 8) + *(bp + 1));
1771 value >>= shift[i%channels];
1772 *bp++ = (png_byte)(value >> 8);
1773 *bp++ = (png_byte)(value & 0xff);
1775 break;
1780 #endif
1782 #ifdef PNG_READ_16_TO_8_SUPPORTED
1783 /* Chop rows of bit depth 16 down to 8 */
1784 void /* PRIVATE */
1785 png_do_chop(png_row_infop row_info, png_bytep row)
1787 png_debug(1, "in png_do_chop");
1789 #ifdef PNG_USELESS_TESTS_SUPPORTED
1790 if (row != NULL && row_info != NULL && row_info->bit_depth == 16)
1791 #else
1792 if (row_info->bit_depth == 16)
1793 #endif
1795 png_bytep sp = row;
1796 png_bytep dp = row;
1797 png_uint_32 i;
1798 png_uint_32 istop = row_info->width * row_info->channels;
1800 for (i = 0; i<istop; i++, sp += 2, dp++)
1802 #ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
1803 /* This does a more accurate scaling of the 16-bit color
1804 * value, rather than a simple low-byte truncation.
1806 * What the ideal calculation should be:
1807 * *dp = (((((png_uint_32)(*sp) << 8) |
1808 * (png_uint_32)(*(sp + 1))) * 255 + 127)
1809 * / (png_uint_32)65535L;
1811 * GRR: no, I think this is what it really should be:
1812 * *dp = (((((png_uint_32)(*sp) << 8) |
1813 * (png_uint_32)(*(sp + 1))) + 128L)
1814 * / (png_uint_32)257L;
1816 * GRR: here's the exact calculation with shifts:
1817 * temp = (((png_uint_32)(*sp) << 8) |
1818 * (png_uint_32)(*(sp + 1))) + 128L;
1819 * *dp = (temp - (temp >> 8)) >> 8;
1821 * Approximate calculation with shift/add instead of multiply/divide:
1822 * *dp = ((((png_uint_32)(*sp) << 8) |
1823 * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
1825 * What we actually do to avoid extra shifting and conversion:
1828 *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
1829 #else
1830 /* Simply discard the low order byte */
1831 *dp = *sp;
1832 #endif
1834 row_info->bit_depth = 8;
1835 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1836 row_info->rowbytes = row_info->width * row_info->channels;
1839 #endif
1841 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1842 void /* PRIVATE */
1843 png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
1845 png_debug(1, "in png_do_read_swap_alpha");
1847 #ifdef PNG_USELESS_TESTS_SUPPORTED
1848 if (row != NULL && row_info != NULL)
1849 #endif
1851 png_uint_32 row_width = row_info->width;
1852 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1854 /* This converts from RGBA to ARGB */
1855 if (row_info->bit_depth == 8)
1857 png_bytep sp = row + row_info->rowbytes;
1858 png_bytep dp = sp;
1859 png_byte save;
1860 png_uint_32 i;
1862 for (i = 0; i < row_width; i++)
1864 save = *(--sp);
1865 *(--dp) = *(--sp);
1866 *(--dp) = *(--sp);
1867 *(--dp) = *(--sp);
1868 *(--dp) = save;
1871 /* This converts from RRGGBBAA to AARRGGBB */
1872 else
1874 png_bytep sp = row + row_info->rowbytes;
1875 png_bytep dp = sp;
1876 png_byte save[2];
1877 png_uint_32 i;
1879 for (i = 0; i < row_width; i++)
1881 save[0] = *(--sp);
1882 save[1] = *(--sp);
1883 *(--dp) = *(--sp);
1884 *(--dp) = *(--sp);
1885 *(--dp) = *(--sp);
1886 *(--dp) = *(--sp);
1887 *(--dp) = *(--sp);
1888 *(--dp) = *(--sp);
1889 *(--dp) = save[0];
1890 *(--dp) = save[1];
1894 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1896 /* This converts from GA to AG */
1897 if (row_info->bit_depth == 8)
1899 png_bytep sp = row + row_info->rowbytes;
1900 png_bytep dp = sp;
1901 png_byte save;
1902 png_uint_32 i;
1904 for (i = 0; i < row_width; i++)
1906 save = *(--sp);
1907 *(--dp) = *(--sp);
1908 *(--dp) = save;
1911 /* This converts from GGAA to AAGG */
1912 else
1914 png_bytep sp = row + row_info->rowbytes;
1915 png_bytep dp = sp;
1916 png_byte save[2];
1917 png_uint_32 i;
1919 for (i = 0; i < row_width; i++)
1921 save[0] = *(--sp);
1922 save[1] = *(--sp);
1923 *(--dp) = *(--sp);
1924 *(--dp) = *(--sp);
1925 *(--dp) = save[0];
1926 *(--dp) = save[1];
1932 #endif
1934 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1935 void /* PRIVATE */
1936 png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
1938 png_debug(1, "in png_do_read_invert_alpha");
1940 #ifdef PNG_USELESS_TESTS_SUPPORTED
1941 if (row != NULL && row_info != NULL)
1942 #endif
1944 png_uint_32 row_width = row_info->width;
1945 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1947 /* This inverts the alpha channel in RGBA */
1948 if (row_info->bit_depth == 8)
1950 png_bytep sp = row + row_info->rowbytes;
1951 png_bytep dp = sp;
1952 png_uint_32 i;
1954 for (i = 0; i < row_width; i++)
1956 *(--dp) = (png_byte)(255 - *(--sp));
1958 /* This does nothing:
1959 *(--dp) = *(--sp);
1960 *(--dp) = *(--sp);
1961 *(--dp) = *(--sp);
1962 We can replace it with:
1964 sp-=3;
1965 dp=sp;
1968 /* This inverts the alpha channel in RRGGBBAA */
1969 else
1971 png_bytep sp = row + row_info->rowbytes;
1972 png_bytep dp = sp;
1973 png_uint_32 i;
1975 for (i = 0; i < row_width; i++)
1977 *(--dp) = (png_byte)(255 - *(--sp));
1978 *(--dp) = (png_byte)(255 - *(--sp));
1980 /* This does nothing:
1981 *(--dp) = *(--sp);
1982 *(--dp) = *(--sp);
1983 *(--dp) = *(--sp);
1984 *(--dp) = *(--sp);
1985 *(--dp) = *(--sp);
1986 *(--dp) = *(--sp);
1987 We can replace it with:
1989 sp-=6;
1990 dp=sp;
1994 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1996 /* This inverts the alpha channel in GA */
1997 if (row_info->bit_depth == 8)
1999 png_bytep sp = row + row_info->rowbytes;
2000 png_bytep dp = sp;
2001 png_uint_32 i;
2003 for (i = 0; i < row_width; i++)
2005 *(--dp) = (png_byte)(255 - *(--sp));
2006 *(--dp) = *(--sp);
2009 /* This inverts the alpha channel in GGAA */
2010 else
2012 png_bytep sp = row + row_info->rowbytes;
2013 png_bytep dp = sp;
2014 png_uint_32 i;
2016 for (i = 0; i < row_width; i++)
2018 *(--dp) = (png_byte)(255 - *(--sp));
2019 *(--dp) = (png_byte)(255 - *(--sp));
2021 *(--dp) = *(--sp);
2022 *(--dp) = *(--sp);
2024 sp-=2;
2025 dp=sp;
2031 #endif
2033 #ifdef PNG_READ_FILLER_SUPPORTED
2034 /* Add filler channel if we have RGB color */
2035 void /* PRIVATE */
2036 png_do_read_filler(png_row_infop row_info, png_bytep row,
2037 png_uint_32 filler, png_uint_32 flags)
2039 png_uint_32 i;
2040 png_uint_32 row_width = row_info->width;
2042 png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
2043 png_byte lo_filler = (png_byte)(filler & 0xff);
2045 png_debug(1, "in png_do_read_filler");
2047 if (
2048 #ifdef PNG_USELESS_TESTS_SUPPORTED
2049 row != NULL && row_info != NULL &&
2050 #endif
2051 row_info->color_type == PNG_COLOR_TYPE_GRAY)
2053 if (row_info->bit_depth == 8)
2055 /* This changes the data from G to GX */
2056 if (flags & PNG_FLAG_FILLER_AFTER)
2058 png_bytep sp = row + (png_size_t)row_width;
2059 png_bytep dp = sp + (png_size_t)row_width;
2060 for (i = 1; i < row_width; i++)
2062 *(--dp) = lo_filler;
2063 *(--dp) = *(--sp);
2065 *(--dp) = lo_filler;
2066 row_info->channels = 2;
2067 row_info->pixel_depth = 16;
2068 row_info->rowbytes = row_width * 2;
2070 /* This changes the data from G to XG */
2071 else
2073 png_bytep sp = row + (png_size_t)row_width;
2074 png_bytep dp = sp + (png_size_t)row_width;
2075 for (i = 0; i < row_width; i++)
2077 *(--dp) = *(--sp);
2078 *(--dp) = lo_filler;
2080 row_info->channels = 2;
2081 row_info->pixel_depth = 16;
2082 row_info->rowbytes = row_width * 2;
2085 else if (row_info->bit_depth == 16)
2087 /* This changes the data from GG to GGXX */
2088 if (flags & PNG_FLAG_FILLER_AFTER)
2090 png_bytep sp = row + (png_size_t)row_width * 2;
2091 png_bytep dp = sp + (png_size_t)row_width * 2;
2092 for (i = 1; i < row_width; i++)
2094 *(--dp) = hi_filler;
2095 *(--dp) = lo_filler;
2096 *(--dp) = *(--sp);
2097 *(--dp) = *(--sp);
2099 *(--dp) = hi_filler;
2100 *(--dp) = lo_filler;
2101 row_info->channels = 2;
2102 row_info->pixel_depth = 32;
2103 row_info->rowbytes = row_width * 4;
2105 /* This changes the data from GG to XXGG */
2106 else
2108 png_bytep sp = row + (png_size_t)row_width * 2;
2109 png_bytep dp = sp + (png_size_t)row_width * 2;
2110 for (i = 0; i < row_width; i++)
2112 *(--dp) = *(--sp);
2113 *(--dp) = *(--sp);
2114 *(--dp) = hi_filler;
2115 *(--dp) = lo_filler;
2117 row_info->channels = 2;
2118 row_info->pixel_depth = 32;
2119 row_info->rowbytes = row_width * 4;
2122 } /* COLOR_TYPE == GRAY */
2123 else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2125 if (row_info->bit_depth == 8)
2127 /* This changes the data from RGB to RGBX */
2128 if (flags & PNG_FLAG_FILLER_AFTER)
2130 png_bytep sp = row + (png_size_t)row_width * 3;
2131 png_bytep dp = sp + (png_size_t)row_width;
2132 for (i = 1; i < row_width; i++)
2134 *(--dp) = lo_filler;
2135 *(--dp) = *(--sp);
2136 *(--dp) = *(--sp);
2137 *(--dp) = *(--sp);
2139 *(--dp) = lo_filler;
2140 row_info->channels = 4;
2141 row_info->pixel_depth = 32;
2142 row_info->rowbytes = row_width * 4;
2144 /* This changes the data from RGB to XRGB */
2145 else
2147 png_bytep sp = row + (png_size_t)row_width * 3;
2148 png_bytep dp = sp + (png_size_t)row_width;
2149 for (i = 0; i < row_width; i++)
2151 *(--dp) = *(--sp);
2152 *(--dp) = *(--sp);
2153 *(--dp) = *(--sp);
2154 *(--dp) = lo_filler;
2156 row_info->channels = 4;
2157 row_info->pixel_depth = 32;
2158 row_info->rowbytes = row_width * 4;
2161 else if (row_info->bit_depth == 16)
2163 /* This changes the data from RRGGBB to RRGGBBXX */
2164 if (flags & PNG_FLAG_FILLER_AFTER)
2166 png_bytep sp = row + (png_size_t)row_width * 6;
2167 png_bytep dp = sp + (png_size_t)row_width * 2;
2168 for (i = 1; i < row_width; i++)
2170 *(--dp) = hi_filler;
2171 *(--dp) = lo_filler;
2172 *(--dp) = *(--sp);
2173 *(--dp) = *(--sp);
2174 *(--dp) = *(--sp);
2175 *(--dp) = *(--sp);
2176 *(--dp) = *(--sp);
2177 *(--dp) = *(--sp);
2179 *(--dp) = hi_filler;
2180 *(--dp) = lo_filler;
2181 row_info->channels = 4;
2182 row_info->pixel_depth = 64;
2183 row_info->rowbytes = row_width * 8;
2185 /* This changes the data from RRGGBB to XXRRGGBB */
2186 else
2188 png_bytep sp = row + (png_size_t)row_width * 6;
2189 png_bytep dp = sp + (png_size_t)row_width * 2;
2190 for (i = 0; i < row_width; i++)
2192 *(--dp) = *(--sp);
2193 *(--dp) = *(--sp);
2194 *(--dp) = *(--sp);
2195 *(--dp) = *(--sp);
2196 *(--dp) = *(--sp);
2197 *(--dp) = *(--sp);
2198 *(--dp) = hi_filler;
2199 *(--dp) = lo_filler;
2201 row_info->channels = 4;
2202 row_info->pixel_depth = 64;
2203 row_info->rowbytes = row_width * 8;
2206 } /* COLOR_TYPE == RGB */
2208 #endif
2210 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2211 /* Expand grayscale files to RGB, with or without alpha */
2212 void /* PRIVATE */
2213 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
2215 png_uint_32 i;
2216 png_uint_32 row_width = row_info->width;
2218 png_debug(1, "in png_do_gray_to_rgb");
2220 if (row_info->bit_depth >= 8 &&
2221 #ifdef PNG_USELESS_TESTS_SUPPORTED
2222 row != NULL && row_info != NULL &&
2223 #endif
2224 !(row_info->color_type & PNG_COLOR_MASK_COLOR))
2226 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
2228 if (row_info->bit_depth == 8)
2230 png_bytep sp = row + (png_size_t)row_width - 1;
2231 png_bytep dp = sp + (png_size_t)row_width * 2;
2232 for (i = 0; i < row_width; i++)
2234 *(dp--) = *sp;
2235 *(dp--) = *sp;
2236 *(dp--) = *(sp--);
2239 else
2241 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2242 png_bytep dp = sp + (png_size_t)row_width * 4;
2243 for (i = 0; i < row_width; i++)
2245 *(dp--) = *sp;
2246 *(dp--) = *(sp - 1);
2247 *(dp--) = *sp;
2248 *(dp--) = *(sp - 1);
2249 *(dp--) = *(sp--);
2250 *(dp--) = *(sp--);
2254 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2256 if (row_info->bit_depth == 8)
2258 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2259 png_bytep dp = sp + (png_size_t)row_width * 2;
2260 for (i = 0; i < row_width; i++)
2262 *(dp--) = *(sp--);
2263 *(dp--) = *sp;
2264 *(dp--) = *sp;
2265 *(dp--) = *(sp--);
2268 else
2270 png_bytep sp = row + (png_size_t)row_width * 4 - 1;
2271 png_bytep dp = sp + (png_size_t)row_width * 4;
2272 for (i = 0; i < row_width; i++)
2274 *(dp--) = *(sp--);
2275 *(dp--) = *(sp--);
2276 *(dp--) = *sp;
2277 *(dp--) = *(sp - 1);
2278 *(dp--) = *sp;
2279 *(dp--) = *(sp - 1);
2280 *(dp--) = *(sp--);
2281 *(dp--) = *(sp--);
2285 row_info->channels += (png_byte)2;
2286 row_info->color_type |= PNG_COLOR_MASK_COLOR;
2287 row_info->pixel_depth = (png_byte)(row_info->channels *
2288 row_info->bit_depth);
2289 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2292 #endif
2294 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2295 /* Reduce RGB files to grayscale, with or without alpha
2296 * using the equation given in Poynton's ColorFAQ at
2297 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008)
2298 * New link:
2299 * <http://www.poynton.com/notes/colour_and_gamma/>
2300 * Charles Poynton poynton at poynton.com
2302 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2304 * We approximate this with
2306 * Y = 0.21268 * R + 0.7151 * G + 0.07217 * B
2308 * which can be expressed with integers as
2310 * Y = (6969 * R + 23434 * G + 2365 * B)/32768
2312 * The calculation is to be done in a linear colorspace.
2314 * Other integer coefficents can be used via png_set_rgb_to_gray().
2316 int /* PRIVATE */
2317 png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
2320 png_uint_32 i;
2322 png_uint_32 row_width = row_info->width;
2323 int rgb_error = 0;
2325 png_debug(1, "in png_do_rgb_to_gray");
2327 if (
2328 #ifdef PNG_USELESS_TESTS_SUPPORTED
2329 row != NULL && row_info != NULL &&
2330 #endif
2331 (row_info->color_type & PNG_COLOR_MASK_COLOR))
2333 png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
2334 png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
2335 png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
2337 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2339 if (row_info->bit_depth == 8)
2341 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2342 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
2344 png_bytep sp = row;
2345 png_bytep dp = row;
2347 for (i = 0; i < row_width; i++)
2349 png_byte red = png_ptr->gamma_to_1[*(sp++)];
2350 png_byte green = png_ptr->gamma_to_1[*(sp++)];
2351 png_byte blue = png_ptr->gamma_to_1[*(sp++)];
2352 if (red != green || red != blue)
2354 rgb_error |= 1;
2355 *(dp++) = png_ptr->gamma_from_1[
2356 (rc*red + gc*green + bc*blue)>>15];
2358 else
2359 *(dp++) = *(sp - 1);
2362 else
2363 #endif
2365 png_bytep sp = row;
2366 png_bytep dp = row;
2367 for (i = 0; i < row_width; i++)
2369 png_byte red = *(sp++);
2370 png_byte green = *(sp++);
2371 png_byte blue = *(sp++);
2372 if (red != green || red != blue)
2374 rgb_error |= 1;
2375 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
2377 else
2378 *(dp++) = *(sp - 1);
2383 else /* RGB bit_depth == 16 */
2385 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2386 if (png_ptr->gamma_16_to_1 != NULL &&
2387 png_ptr->gamma_16_from_1 != NULL)
2389 png_bytep sp = row;
2390 png_bytep dp = row;
2391 for (i = 0; i < row_width; i++)
2393 png_uint_16 red, green, blue, w;
2395 red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2396 green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2397 blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2399 if (red == green && red == blue)
2400 w = red;
2401 else
2403 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >>
2404 png_ptr->gamma_shift][red>>8];
2405 png_uint_16 green_1 =
2406 png_ptr->gamma_16_to_1[(green&0xff) >>
2407 png_ptr->gamma_shift][green>>8];
2408 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >>
2409 png_ptr->gamma_shift][blue>>8];
2410 png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1
2411 + bc*blue_1)>>15);
2412 w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
2413 png_ptr->gamma_shift][gray16 >> 8];
2414 rgb_error |= 1;
2417 *(dp++) = (png_byte)((w>>8) & 0xff);
2418 *(dp++) = (png_byte)(w & 0xff);
2421 else
2422 #endif
2424 png_bytep sp = row;
2425 png_bytep dp = row;
2426 for (i = 0; i < row_width; i++)
2428 png_uint_16 red, green, blue, gray16;
2430 red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2431 green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2432 blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2434 if (red != green || red != blue)
2435 rgb_error |= 1;
2436 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2437 *(dp++) = (png_byte)((gray16>>8) & 0xff);
2438 *(dp++) = (png_byte)(gray16 & 0xff);
2443 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2445 if (row_info->bit_depth == 8)
2447 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2448 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
2450 png_bytep sp = row;
2451 png_bytep dp = row;
2452 for (i = 0; i < row_width; i++)
2454 png_byte red = png_ptr->gamma_to_1[*(sp++)];
2455 png_byte green = png_ptr->gamma_to_1[*(sp++)];
2456 png_byte blue = png_ptr->gamma_to_1[*(sp++)];
2457 if (red != green || red != blue)
2458 rgb_error |= 1;
2459 *(dp++) = png_ptr->gamma_from_1
2460 [(rc*red + gc*green + bc*blue)>>15];
2461 *(dp++) = *(sp++); /* alpha */
2464 else
2465 #endif
2467 png_bytep sp = row;
2468 png_bytep dp = row;
2469 for (i = 0; i < row_width; i++)
2471 png_byte red = *(sp++);
2472 png_byte green = *(sp++);
2473 png_byte blue = *(sp++);
2474 if (red != green || red != blue)
2475 rgb_error |= 1;
2476 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
2477 *(dp++) = *(sp++); /* alpha */
2481 else /* RGBA bit_depth == 16 */
2483 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2484 if (png_ptr->gamma_16_to_1 != NULL &&
2485 png_ptr->gamma_16_from_1 != NULL)
2487 png_bytep sp = row;
2488 png_bytep dp = row;
2489 for (i = 0; i < row_width; i++)
2491 png_uint_16 red, green, blue, w;
2493 red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2494 green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2495 blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2497 if (red == green && red == blue)
2498 w = red;
2499 else
2501 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >>
2502 png_ptr->gamma_shift][red>>8];
2503 png_uint_16 green_1 =
2504 png_ptr->gamma_16_to_1[(green&0xff) >>
2505 png_ptr->gamma_shift][green>>8];
2506 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >>
2507 png_ptr->gamma_shift][blue>>8];
2508 png_uint_16 gray16 = (png_uint_16)((rc * red_1
2509 + gc * green_1 + bc * blue_1)>>15);
2510 w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
2511 png_ptr->gamma_shift][gray16 >> 8];
2512 rgb_error |= 1;
2515 *(dp++) = (png_byte)((w>>8) & 0xff);
2516 *(dp++) = (png_byte)(w & 0xff);
2517 *(dp++) = *(sp++); /* alpha */
2518 *(dp++) = *(sp++);
2521 else
2522 #endif
2524 png_bytep sp = row;
2525 png_bytep dp = row;
2526 for (i = 0; i < row_width; i++)
2528 png_uint_16 red, green, blue, gray16;
2529 red = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
2530 green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
2531 blue = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
2532 if (red != green || red != blue)
2533 rgb_error |= 1;
2534 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2535 *(dp++) = (png_byte)((gray16>>8) & 0xff);
2536 *(dp++) = (png_byte)(gray16 & 0xff);
2537 *(dp++) = *(sp++); /* alpha */
2538 *(dp++) = *(sp++);
2543 row_info->channels -= (png_byte)2;
2544 row_info->color_type &= ~PNG_COLOR_MASK_COLOR;
2545 row_info->pixel_depth = (png_byte)(row_info->channels *
2546 row_info->bit_depth);
2547 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2549 return rgb_error;
2551 #endif
2553 /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
2554 * large of png_color. This lets grayscale images be treated as
2555 * paletted. Most useful for gamma correction and simplification
2556 * of code.
2558 void PNGAPI
2559 png_build_grayscale_palette(int bit_depth, png_colorp palette)
2561 int num_palette;
2562 int color_inc;
2563 int i;
2564 int v;
2566 png_debug(1, "in png_do_build_grayscale_palette");
2568 if (palette == NULL)
2569 return;
2571 switch (bit_depth)
2573 case 1:
2574 num_palette = 2;
2575 color_inc = 0xff;
2576 break;
2578 case 2:
2579 num_palette = 4;
2580 color_inc = 0x55;
2581 break;
2583 case 4:
2584 num_palette = 16;
2585 color_inc = 0x11;
2586 break;
2588 case 8:
2589 num_palette = 256;
2590 color_inc = 1;
2591 break;
2593 default:
2594 num_palette = 0;
2595 color_inc = 0;
2596 break;
2599 for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
2601 palette[i].red = (png_byte)v;
2602 palette[i].green = (png_byte)v;
2603 palette[i].blue = (png_byte)v;
2607 /* This function is currently unused. Do we really need it? */
2608 #if defined(PNG_READ_DITHER_SUPPORTED) && \
2609 defined(PNG_CORRECT_PALETTE_SUPPORTED)
2610 void /* PRIVATE */
2611 png_correct_palette(png_structp png_ptr, png_colorp palette,
2612 int num_palette)
2614 png_debug(1, "in png_correct_palette");
2616 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
2617 defined(PNG_READ_GAMMA_SUPPORTED) && \
2618 defined(PNG_FLOATING_POINT_SUPPORTED)
2619 if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND))
2621 png_color back, back_1;
2623 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
2625 back.red = png_ptr->gamma_table[png_ptr->background.red];
2626 back.green = png_ptr->gamma_table[png_ptr->background.green];
2627 back.blue = png_ptr->gamma_table[png_ptr->background.blue];
2629 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
2630 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
2631 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
2633 else
2635 double g;
2637 g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma);
2639 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN
2640 || fabs(g - 1.0) < PNG_GAMMA_THRESHOLD)
2642 back.red = png_ptr->background.red;
2643 back.green = png_ptr->background.green;
2644 back.blue = png_ptr->background.blue;
2646 else
2648 back.red =
2649 (png_byte)(pow((double)png_ptr->background.red/255, g) *
2650 255.0 + 0.5);
2651 back.green =
2652 (png_byte)(pow((double)png_ptr->background.green/255, g) *
2653 255.0 + 0.5);
2654 back.blue =
2655 (png_byte)(pow((double)png_ptr->background.blue/255, g) *
2656 255.0 + 0.5);
2659 g = 1.0 / png_ptr->background_gamma;
2661 back_1.red =
2662 (png_byte)(pow((double)png_ptr->background.red/255, g) *
2663 255.0 + 0.5);
2664 back_1.green =
2665 (png_byte)(pow((double)png_ptr->background.green/255, g) *
2666 255.0 + 0.5);
2667 back_1.blue =
2668 (png_byte)(pow((double)png_ptr->background.blue/255, g) *
2669 255.0 + 0.5);
2672 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
2674 png_uint_32 i;
2676 for (i = 0; i < (png_uint_32)num_palette; i++)
2678 if (i < png_ptr->num_trans && png_ptr->trans[i] == 0)
2680 palette[i] = back;
2682 else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff)
2684 png_byte v, w;
2686 v = png_ptr->gamma_to_1[png_ptr->palette[i].red];
2687 png_composite(w, v, png_ptr->trans[i], back_1.red);
2688 palette[i].red = png_ptr->gamma_from_1[w];
2690 v = png_ptr->gamma_to_1[png_ptr->palette[i].green];
2691 png_composite(w, v, png_ptr->trans[i], back_1.green);
2692 palette[i].green = png_ptr->gamma_from_1[w];
2694 v = png_ptr->gamma_to_1[png_ptr->palette[i].blue];
2695 png_composite(w, v, png_ptr->trans[i], back_1.blue);
2696 palette[i].blue = png_ptr->gamma_from_1[w];
2698 else
2700 palette[i].red = png_ptr->gamma_table[palette[i].red];
2701 palette[i].green = png_ptr->gamma_table[palette[i].green];
2702 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
2706 else
2708 int i;
2710 for (i = 0; i < num_palette; i++)
2712 if (palette[i].red == (png_byte)png_ptr->trans_values.gray)
2714 palette[i] = back;
2716 else
2718 palette[i].red = png_ptr->gamma_table[palette[i].red];
2719 palette[i].green = png_ptr->gamma_table[palette[i].green];
2720 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
2725 else
2726 #endif
2727 #ifdef PNG_READ_GAMMA_SUPPORTED
2728 if (png_ptr->transformations & PNG_GAMMA)
2730 int i;
2732 for (i = 0; i < num_palette; i++)
2734 palette[i].red = png_ptr->gamma_table[palette[i].red];
2735 palette[i].green = png_ptr->gamma_table[palette[i].green];
2736 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
2739 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2740 else
2741 #endif
2742 #endif
2743 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2744 if (png_ptr->transformations & PNG_BACKGROUND)
2746 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
2748 png_color back;
2750 back.red = (png_byte)png_ptr->background.red;
2751 back.green = (png_byte)png_ptr->background.green;
2752 back.blue = (png_byte)png_ptr->background.blue;
2754 for (i = 0; i < (int)png_ptr->num_trans; i++)
2756 if (png_ptr->trans[i] == 0)
2758 palette[i].red = back.red;
2759 palette[i].green = back.green;
2760 palette[i].blue = back.blue;
2762 else if (png_ptr->trans[i] != 0xff)
2764 png_composite(palette[i].red, png_ptr->palette[i].red,
2765 png_ptr->trans[i], back.red);
2766 png_composite(palette[i].green, png_ptr->palette[i].green,
2767 png_ptr->trans[i], back.green);
2768 png_composite(palette[i].blue, png_ptr->palette[i].blue,
2769 png_ptr->trans[i], back.blue);
2773 else /* Assume grayscale palette (what else could it be?) */
2775 int i;
2777 for (i = 0; i < num_palette; i++)
2779 if (i == (png_byte)png_ptr->trans_values.gray)
2781 palette[i].red = (png_byte)png_ptr->background.red;
2782 palette[i].green = (png_byte)png_ptr->background.green;
2783 palette[i].blue = (png_byte)png_ptr->background.blue;
2788 #endif
2790 #endif
2792 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2793 /* Replace any alpha or transparency with the supplied background color.
2794 * "background" is already in the screen gamma, while "background_1" is
2795 * at a gamma of 1.0. Paletted files have already been taken care of.
2797 void /* PRIVATE */
2798 png_do_background(png_row_infop row_info, png_bytep row,
2799 png_color_16p trans_values, png_color_16p background
2800 #ifdef PNG_READ_GAMMA_SUPPORTED
2801 , png_color_16p background_1,
2802 png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
2803 png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
2804 png_uint_16pp gamma_16_to_1, int gamma_shift
2805 #endif
2808 png_bytep sp, dp;
2809 png_uint_32 i;
2810 png_uint_32 row_width=row_info->width;
2811 int shift;
2813 png_debug(1, "in png_do_background");
2815 if (background != NULL &&
2816 #ifdef PNG_USELESS_TESTS_SUPPORTED
2817 row != NULL && row_info != NULL &&
2818 #endif
2819 (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
2820 (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values)))
2822 switch (row_info->color_type)
2824 case PNG_COLOR_TYPE_GRAY:
2826 switch (row_info->bit_depth)
2828 case 1:
2830 sp = row;
2831 shift = 7;
2832 for (i = 0; i < row_width; i++)
2834 if ((png_uint_16)((*sp >> shift) & 0x01)
2835 == trans_values->gray)
2837 *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
2838 *sp |= (png_byte)(background->gray << shift);
2840 if (!shift)
2842 shift = 7;
2843 sp++;
2845 else
2846 shift--;
2848 break;
2851 case 2:
2853 #ifdef PNG_READ_GAMMA_SUPPORTED
2854 if (gamma_table != NULL)
2856 sp = row;
2857 shift = 6;
2858 for (i = 0; i < row_width; i++)
2860 if ((png_uint_16)((*sp >> shift) & 0x03)
2861 == trans_values->gray)
2863 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2864 *sp |= (png_byte)(background->gray << shift);
2866 else
2868 png_byte p = (png_byte)((*sp >> shift) & 0x03);
2869 png_byte g = (png_byte)((gamma_table [p | (p << 2) |
2870 (p << 4) | (p << 6)] >> 6) & 0x03);
2871 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2872 *sp |= (png_byte)(g << shift);
2874 if (!shift)
2876 shift = 6;
2877 sp++;
2879 else
2880 shift -= 2;
2883 else
2884 #endif
2886 sp = row;
2887 shift = 6;
2888 for (i = 0; i < row_width; i++)
2890 if ((png_uint_16)((*sp >> shift) & 0x03)
2891 == trans_values->gray)
2893 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2894 *sp |= (png_byte)(background->gray << shift);
2896 if (!shift)
2898 shift = 6;
2899 sp++;
2901 else
2902 shift -= 2;
2905 break;
2908 case 4:
2910 #ifdef PNG_READ_GAMMA_SUPPORTED
2911 if (gamma_table != NULL)
2913 sp = row;
2914 shift = 4;
2915 for (i = 0; i < row_width; i++)
2917 if ((png_uint_16)((*sp >> shift) & 0x0f)
2918 == trans_values->gray)
2920 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2921 *sp |= (png_byte)(background->gray << shift);
2923 else
2925 png_byte p = (png_byte)((*sp >> shift) & 0x0f);
2926 png_byte g = (png_byte)((gamma_table[p |
2927 (p << 4)] >> 4) & 0x0f);
2928 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2929 *sp |= (png_byte)(g << shift);
2931 if (!shift)
2933 shift = 4;
2934 sp++;
2936 else
2937 shift -= 4;
2940 else
2941 #endif
2943 sp = row;
2944 shift = 4;
2945 for (i = 0; i < row_width; i++)
2947 if ((png_uint_16)((*sp >> shift) & 0x0f)
2948 == trans_values->gray)
2950 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2951 *sp |= (png_byte)(background->gray << shift);
2953 if (!shift)
2955 shift = 4;
2956 sp++;
2958 else
2959 shift -= 4;
2962 break;
2965 case 8:
2967 #ifdef PNG_READ_GAMMA_SUPPORTED
2968 if (gamma_table != NULL)
2970 sp = row;
2971 for (i = 0; i < row_width; i++, sp++)
2973 if (*sp == trans_values->gray)
2975 *sp = (png_byte)background->gray;
2977 else
2979 *sp = gamma_table[*sp];
2983 else
2984 #endif
2986 sp = row;
2987 for (i = 0; i < row_width; i++, sp++)
2989 if (*sp == trans_values->gray)
2991 *sp = (png_byte)background->gray;
2995 break;
2998 case 16:
3000 #ifdef PNG_READ_GAMMA_SUPPORTED
3001 if (gamma_16 != NULL)
3003 sp = row;
3004 for (i = 0; i < row_width; i++, sp += 2)
3006 png_uint_16 v;
3008 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3009 if (v == trans_values->gray)
3011 /* Background is already in screen gamma */
3012 *sp = (png_byte)((background->gray >> 8) & 0xff);
3013 *(sp + 1) = (png_byte)(background->gray & 0xff);
3015 else
3017 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3018 *sp = (png_byte)((v >> 8) & 0xff);
3019 *(sp + 1) = (png_byte)(v & 0xff);
3023 else
3024 #endif
3026 sp = row;
3027 for (i = 0; i < row_width; i++, sp += 2)
3029 png_uint_16 v;
3031 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3032 if (v == trans_values->gray)
3034 *sp = (png_byte)((background->gray >> 8) & 0xff);
3035 *(sp + 1) = (png_byte)(background->gray & 0xff);
3039 break;
3042 break;
3045 case PNG_COLOR_TYPE_RGB:
3047 if (row_info->bit_depth == 8)
3049 #ifdef PNG_READ_GAMMA_SUPPORTED
3050 if (gamma_table != NULL)
3052 sp = row;
3053 for (i = 0; i < row_width; i++, sp += 3)
3055 if (*sp == trans_values->red &&
3056 *(sp + 1) == trans_values->green &&
3057 *(sp + 2) == trans_values->blue)
3059 *sp = (png_byte)background->red;
3060 *(sp + 1) = (png_byte)background->green;
3061 *(sp + 2) = (png_byte)background->blue;
3063 else
3065 *sp = gamma_table[*sp];
3066 *(sp + 1) = gamma_table[*(sp + 1)];
3067 *(sp + 2) = gamma_table[*(sp + 2)];
3071 else
3072 #endif
3074 sp = row;
3075 for (i = 0; i < row_width; i++, sp += 3)
3077 if (*sp == trans_values->red &&
3078 *(sp + 1) == trans_values->green &&
3079 *(sp + 2) == trans_values->blue)
3081 *sp = (png_byte)background->red;
3082 *(sp + 1) = (png_byte)background->green;
3083 *(sp + 2) = (png_byte)background->blue;
3088 else /* if (row_info->bit_depth == 16) */
3090 #ifdef PNG_READ_GAMMA_SUPPORTED
3091 if (gamma_16 != NULL)
3093 sp = row;
3094 for (i = 0; i < row_width; i++, sp += 6)
3096 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3097 png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
3098 png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
3099 if (r == trans_values->red && g == trans_values->green &&
3100 b == trans_values->blue)
3102 /* Background is already in screen gamma */
3103 *sp = (png_byte)((background->red >> 8) & 0xff);
3104 *(sp + 1) = (png_byte)(background->red & 0xff);
3105 *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3106 *(sp + 3) = (png_byte)(background->green & 0xff);
3107 *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3108 *(sp + 5) = (png_byte)(background->blue & 0xff);
3110 else
3112 png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3113 *sp = (png_byte)((v >> 8) & 0xff);
3114 *(sp + 1) = (png_byte)(v & 0xff);
3115 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3116 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3117 *(sp + 3) = (png_byte)(v & 0xff);
3118 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3119 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3120 *(sp + 5) = (png_byte)(v & 0xff);
3124 else
3125 #endif
3127 sp = row;
3128 for (i = 0; i < row_width; i++, sp += 6)
3130 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1));
3131 png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
3132 png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
3134 if (r == trans_values->red && g == trans_values->green &&
3135 b == trans_values->blue)
3137 *sp = (png_byte)((background->red >> 8) & 0xff);
3138 *(sp + 1) = (png_byte)(background->red & 0xff);
3139 *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3140 *(sp + 3) = (png_byte)(background->green & 0xff);
3141 *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3142 *(sp + 5) = (png_byte)(background->blue & 0xff);
3147 break;
3150 case PNG_COLOR_TYPE_GRAY_ALPHA:
3152 if (row_info->bit_depth == 8)
3154 #ifdef PNG_READ_GAMMA_SUPPORTED
3155 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3156 gamma_table != NULL)
3158 sp = row;
3159 dp = row;
3160 for (i = 0; i < row_width; i++, sp += 2, dp++)
3162 png_uint_16 a = *(sp + 1);
3164 if (a == 0xff)
3166 *dp = gamma_table[*sp];
3168 else if (a == 0)
3170 /* Background is already in screen gamma */
3171 *dp = (png_byte)background->gray;
3173 else
3175 png_byte v, w;
3177 v = gamma_to_1[*sp];
3178 png_composite(w, v, a, background_1->gray);
3179 *dp = gamma_from_1[w];
3183 else
3184 #endif
3186 sp = row;
3187 dp = row;
3188 for (i = 0; i < row_width; i++, sp += 2, dp++)
3190 png_byte a = *(sp + 1);
3192 if (a == 0xff)
3194 *dp = *sp;
3196 #ifdef PNG_READ_GAMMA_SUPPORTED
3197 else if (a == 0)
3199 *dp = (png_byte)background->gray;
3201 else
3203 png_composite(*dp, *sp, a, background_1->gray);
3205 #else
3206 *dp = (png_byte)background->gray;
3207 #endif
3211 else /* if (png_ptr->bit_depth == 16) */
3213 #ifdef PNG_READ_GAMMA_SUPPORTED
3214 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3215 gamma_16_to_1 != NULL)
3217 sp = row;
3218 dp = row;
3219 for (i = 0; i < row_width; i++, sp += 4, dp += 2)
3221 png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
3223 if (a == (png_uint_16)0xffff)
3225 png_uint_16 v;
3227 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3228 *dp = (png_byte)((v >> 8) & 0xff);
3229 *(dp + 1) = (png_byte)(v & 0xff);
3231 #ifdef PNG_READ_GAMMA_SUPPORTED
3232 else if (a == 0)
3233 #else
3234 else
3235 #endif
3237 /* Background is already in screen gamma */
3238 *dp = (png_byte)((background->gray >> 8) & 0xff);
3239 *(dp + 1) = (png_byte)(background->gray & 0xff);
3241 #ifdef PNG_READ_GAMMA_SUPPORTED
3242 else
3244 png_uint_16 g, v, w;
3246 g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3247 png_composite_16(v, g, a, background_1->gray);
3248 w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
3249 *dp = (png_byte)((w >> 8) & 0xff);
3250 *(dp + 1) = (png_byte)(w & 0xff);
3252 #endif
3255 else
3256 #endif
3258 sp = row;
3259 dp = row;
3260 for (i = 0; i < row_width; i++, sp += 4, dp += 2)
3262 png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
3263 if (a == (png_uint_16)0xffff)
3265 png_memcpy(dp, sp, 2);
3267 #ifdef PNG_READ_GAMMA_SUPPORTED
3268 else if (a == 0)
3269 #else
3270 else
3271 #endif
3273 *dp = (png_byte)((background->gray >> 8) & 0xff);
3274 *(dp + 1) = (png_byte)(background->gray & 0xff);
3276 #ifdef PNG_READ_GAMMA_SUPPORTED
3277 else
3279 png_uint_16 g, v;
3281 g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3282 png_composite_16(v, g, a, background_1->gray);
3283 *dp = (png_byte)((v >> 8) & 0xff);
3284 *(dp + 1) = (png_byte)(v & 0xff);
3286 #endif
3290 break;
3293 case PNG_COLOR_TYPE_RGB_ALPHA:
3295 if (row_info->bit_depth == 8)
3297 #ifdef PNG_READ_GAMMA_SUPPORTED
3298 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3299 gamma_table != NULL)
3301 sp = row;
3302 dp = row;
3303 for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3305 png_byte a = *(sp + 3);
3307 if (a == 0xff)
3309 *dp = gamma_table[*sp];
3310 *(dp + 1) = gamma_table[*(sp + 1)];
3311 *(dp + 2) = gamma_table[*(sp + 2)];
3313 else if (a == 0)
3315 /* Background is already in screen gamma */
3316 *dp = (png_byte)background->red;
3317 *(dp + 1) = (png_byte)background->green;
3318 *(dp + 2) = (png_byte)background->blue;
3320 else
3322 png_byte v, w;
3324 v = gamma_to_1[*sp];
3325 png_composite(w, v, a, background_1->red);
3326 *dp = gamma_from_1[w];
3327 v = gamma_to_1[*(sp + 1)];
3328 png_composite(w, v, a, background_1->green);
3329 *(dp + 1) = gamma_from_1[w];
3330 v = gamma_to_1[*(sp + 2)];
3331 png_composite(w, v, a, background_1->blue);
3332 *(dp + 2) = gamma_from_1[w];
3336 else
3337 #endif
3339 sp = row;
3340 dp = row;
3341 for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3343 png_byte a = *(sp + 3);
3345 if (a == 0xff)
3347 *dp = *sp;
3348 *(dp + 1) = *(sp + 1);
3349 *(dp + 2) = *(sp + 2);
3351 else if (a == 0)
3353 *dp = (png_byte)background->red;
3354 *(dp + 1) = (png_byte)background->green;
3355 *(dp + 2) = (png_byte)background->blue;
3357 else
3359 png_composite(*dp, *sp, a, background->red);
3360 png_composite(*(dp + 1), *(sp + 1), a,
3361 background->green);
3362 png_composite(*(dp + 2), *(sp + 2), a,
3363 background->blue);
3368 else /* if (row_info->bit_depth == 16) */
3370 #ifdef PNG_READ_GAMMA_SUPPORTED
3371 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3372 gamma_16_to_1 != NULL)
3374 sp = row;
3375 dp = row;
3376 for (i = 0; i < row_width; i++, sp += 8, dp += 6)
3378 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3379 << 8) + (png_uint_16)(*(sp + 7)));
3380 if (a == (png_uint_16)0xffff)
3382 png_uint_16 v;
3384 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3385 *dp = (png_byte)((v >> 8) & 0xff);
3386 *(dp + 1) = (png_byte)(v & 0xff);
3387 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3388 *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3389 *(dp + 3) = (png_byte)(v & 0xff);
3390 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3391 *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3392 *(dp + 5) = (png_byte)(v & 0xff);
3394 else if (a == 0)
3396 /* Background is already in screen gamma */
3397 *dp = (png_byte)((background->red >> 8) & 0xff);
3398 *(dp + 1) = (png_byte)(background->red & 0xff);
3399 *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3400 *(dp + 3) = (png_byte)(background->green & 0xff);
3401 *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3402 *(dp + 5) = (png_byte)(background->blue & 0xff);
3404 else
3406 png_uint_16 v, w, x;
3408 v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3409 png_composite_16(w, v, a, background_1->red);
3410 x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
3411 *dp = (png_byte)((x >> 8) & 0xff);
3412 *(dp + 1) = (png_byte)(x & 0xff);
3413 v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
3414 png_composite_16(w, v, a, background_1->green);
3415 x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
3416 *(dp + 2) = (png_byte)((x >> 8) & 0xff);
3417 *(dp + 3) = (png_byte)(x & 0xff);
3418 v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
3419 png_composite_16(w, v, a, background_1->blue);
3420 x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
3421 *(dp + 4) = (png_byte)((x >> 8) & 0xff);
3422 *(dp + 5) = (png_byte)(x & 0xff);
3426 else
3427 #endif
3429 sp = row;
3430 dp = row;
3431 for (i = 0; i < row_width; i++, sp += 8, dp += 6)
3433 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3434 << 8) + (png_uint_16)(*(sp + 7)));
3435 if (a == (png_uint_16)0xffff)
3437 png_memcpy(dp, sp, 6);
3439 else if (a == 0)
3441 *dp = (png_byte)((background->red >> 8) & 0xff);
3442 *(dp + 1) = (png_byte)(background->red & 0xff);
3443 *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3444 *(dp + 3) = (png_byte)(background->green & 0xff);
3445 *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3446 *(dp + 5) = (png_byte)(background->blue & 0xff);
3448 else
3450 png_uint_16 v;
3452 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3453 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3454 + *(sp + 3));
3455 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3456 + *(sp + 5));
3458 png_composite_16(v, r, a, background->red);
3459 *dp = (png_byte)((v >> 8) & 0xff);
3460 *(dp + 1) = (png_byte)(v & 0xff);
3461 png_composite_16(v, g, a, background->green);
3462 *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3463 *(dp + 3) = (png_byte)(v & 0xff);
3464 png_composite_16(v, b, a, background->blue);
3465 *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3466 *(dp + 5) = (png_byte)(v & 0xff);
3471 break;
3475 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
3477 row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
3478 row_info->channels--;
3479 row_info->pixel_depth = (png_byte)(row_info->channels *
3480 row_info->bit_depth);
3481 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3485 #endif
3487 #ifdef PNG_READ_GAMMA_SUPPORTED
3488 /* Gamma correct the image, avoiding the alpha channel. Make sure
3489 * you do this after you deal with the transparency issue on grayscale
3490 * or RGB images. If your bit depth is 8, use gamma_table, if it
3491 * is 16, use gamma_16_table and gamma_shift. Build these with
3492 * build_gamma_table().
3494 void /* PRIVATE */
3495 png_do_gamma(png_row_infop row_info, png_bytep row,
3496 png_bytep gamma_table, png_uint_16pp gamma_16_table,
3497 int gamma_shift)
3499 png_bytep sp;
3500 png_uint_32 i;
3501 png_uint_32 row_width=row_info->width;
3503 png_debug(1, "in png_do_gamma");
3505 if (
3506 #ifdef PNG_USELESS_TESTS_SUPPORTED
3507 row != NULL && row_info != NULL &&
3508 #endif
3509 ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
3510 (row_info->bit_depth == 16 && gamma_16_table != NULL)))
3512 switch (row_info->color_type)
3514 case PNG_COLOR_TYPE_RGB:
3516 if (row_info->bit_depth == 8)
3518 sp = row;
3519 for (i = 0; i < row_width; i++)
3521 *sp = gamma_table[*sp];
3522 sp++;
3523 *sp = gamma_table[*sp];
3524 sp++;
3525 *sp = gamma_table[*sp];
3526 sp++;
3529 else /* if (row_info->bit_depth == 16) */
3531 sp = row;
3532 for (i = 0; i < row_width; i++)
3534 png_uint_16 v;
3536 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3537 *sp = (png_byte)((v >> 8) & 0xff);
3538 *(sp + 1) = (png_byte)(v & 0xff);
3539 sp += 2;
3540 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3541 *sp = (png_byte)((v >> 8) & 0xff);
3542 *(sp + 1) = (png_byte)(v & 0xff);
3543 sp += 2;
3544 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3545 *sp = (png_byte)((v >> 8) & 0xff);
3546 *(sp + 1) = (png_byte)(v & 0xff);
3547 sp += 2;
3550 break;
3553 case PNG_COLOR_TYPE_RGB_ALPHA:
3555 if (row_info->bit_depth == 8)
3557 sp = row;
3558 for (i = 0; i < row_width; i++)
3560 *sp = gamma_table[*sp];
3561 sp++;
3562 *sp = gamma_table[*sp];
3563 sp++;
3564 *sp = gamma_table[*sp];
3565 sp++;
3566 sp++;
3569 else /* if (row_info->bit_depth == 16) */
3571 sp = row;
3572 for (i = 0; i < row_width; i++)
3574 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3575 *sp = (png_byte)((v >> 8) & 0xff);
3576 *(sp + 1) = (png_byte)(v & 0xff);
3577 sp += 2;
3578 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3579 *sp = (png_byte)((v >> 8) & 0xff);
3580 *(sp + 1) = (png_byte)(v & 0xff);
3581 sp += 2;
3582 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3583 *sp = (png_byte)((v >> 8) & 0xff);
3584 *(sp + 1) = (png_byte)(v & 0xff);
3585 sp += 4;
3588 break;
3591 case PNG_COLOR_TYPE_GRAY_ALPHA:
3593 if (row_info->bit_depth == 8)
3595 sp = row;
3596 for (i = 0; i < row_width; i++)
3598 *sp = gamma_table[*sp];
3599 sp += 2;
3602 else /* if (row_info->bit_depth == 16) */
3604 sp = row;
3605 for (i = 0; i < row_width; i++)
3607 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3608 *sp = (png_byte)((v >> 8) & 0xff);
3609 *(sp + 1) = (png_byte)(v & 0xff);
3610 sp += 4;
3613 break;
3616 case PNG_COLOR_TYPE_GRAY:
3618 if (row_info->bit_depth == 2)
3620 sp = row;
3621 for (i = 0; i < row_width; i += 4)
3623 int a = *sp & 0xc0;
3624 int b = *sp & 0x30;
3625 int c = *sp & 0x0c;
3626 int d = *sp & 0x03;
3628 *sp = (png_byte)(
3629 ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)|
3630 ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
3631 ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
3632 ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
3633 sp++;
3637 if (row_info->bit_depth == 4)
3639 sp = row;
3640 for (i = 0; i < row_width; i += 2)
3642 int msb = *sp & 0xf0;
3643 int lsb = *sp & 0x0f;
3645 *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
3646 | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
3647 sp++;
3651 else if (row_info->bit_depth == 8)
3653 sp = row;
3654 for (i = 0; i < row_width; i++)
3656 *sp = gamma_table[*sp];
3657 sp++;
3661 else if (row_info->bit_depth == 16)
3663 sp = row;
3664 for (i = 0; i < row_width; i++)
3666 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3667 *sp = (png_byte)((v >> 8) & 0xff);
3668 *(sp + 1) = (png_byte)(v & 0xff);
3669 sp += 2;
3672 break;
3677 #endif
3679 #ifdef PNG_READ_EXPAND_SUPPORTED
3680 /* Expands a palette row to an RGB or RGBA row depending
3681 * upon whether you supply trans and num_trans.
3683 void /* PRIVATE */
3684 png_do_expand_palette(png_row_infop row_info, png_bytep row,
3685 png_colorp palette, png_bytep trans, int num_trans)
3687 int shift, value;
3688 png_bytep sp, dp;
3689 png_uint_32 i;
3690 png_uint_32 row_width=row_info->width;
3692 png_debug(1, "in png_do_expand_palette");
3694 if (
3695 #ifdef PNG_USELESS_TESTS_SUPPORTED
3696 row != NULL && row_info != NULL &&
3697 #endif
3698 row_info->color_type == PNG_COLOR_TYPE_PALETTE)
3700 if (row_info->bit_depth < 8)
3702 switch (row_info->bit_depth)
3704 case 1:
3706 sp = row + (png_size_t)((row_width - 1) >> 3);
3707 dp = row + (png_size_t)row_width - 1;
3708 shift = 7 - (int)((row_width + 7) & 0x07);
3709 for (i = 0; i < row_width; i++)
3711 if ((*sp >> shift) & 0x01)
3712 *dp = 1;
3713 else
3714 *dp = 0;
3715 if (shift == 7)
3717 shift = 0;
3718 sp--;
3720 else
3721 shift++;
3723 dp--;
3725 break;
3728 case 2:
3730 sp = row + (png_size_t)((row_width - 1) >> 2);
3731 dp = row + (png_size_t)row_width - 1;
3732 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3733 for (i = 0; i < row_width; i++)
3735 value = (*sp >> shift) & 0x03;
3736 *dp = (png_byte)value;
3737 if (shift == 6)
3739 shift = 0;
3740 sp--;
3742 else
3743 shift += 2;
3745 dp--;
3747 break;
3750 case 4:
3752 sp = row + (png_size_t)((row_width - 1) >> 1);
3753 dp = row + (png_size_t)row_width - 1;
3754 shift = (int)((row_width & 0x01) << 2);
3755 for (i = 0; i < row_width; i++)
3757 value = (*sp >> shift) & 0x0f;
3758 *dp = (png_byte)value;
3759 if (shift == 4)
3761 shift = 0;
3762 sp--;
3764 else
3765 shift += 4;
3767 dp--;
3769 break;
3772 row_info->bit_depth = 8;
3773 row_info->pixel_depth = 8;
3774 row_info->rowbytes = row_width;
3776 switch (row_info->bit_depth)
3778 case 8:
3780 if (trans != NULL)
3782 sp = row + (png_size_t)row_width - 1;
3783 dp = row + (png_size_t)(row_width << 2) - 1;
3785 for (i = 0; i < row_width; i++)
3787 if ((int)(*sp) >= num_trans)
3788 *dp-- = 0xff;
3789 else
3790 *dp-- = trans[*sp];
3791 *dp-- = palette[*sp].blue;
3792 *dp-- = palette[*sp].green;
3793 *dp-- = palette[*sp].red;
3794 sp--;
3796 row_info->bit_depth = 8;
3797 row_info->pixel_depth = 32;
3798 row_info->rowbytes = row_width * 4;
3799 row_info->color_type = 6;
3800 row_info->channels = 4;
3802 else
3804 sp = row + (png_size_t)row_width - 1;
3805 dp = row + (png_size_t)(row_width * 3) - 1;
3807 for (i = 0; i < row_width; i++)
3809 *dp-- = palette[*sp].blue;
3810 *dp-- = palette[*sp].green;
3811 *dp-- = palette[*sp].red;
3812 sp--;
3815 row_info->bit_depth = 8;
3816 row_info->pixel_depth = 24;
3817 row_info->rowbytes = row_width * 3;
3818 row_info->color_type = 2;
3819 row_info->channels = 3;
3821 break;
3827 /* If the bit depth < 8, it is expanded to 8. Also, if the already
3828 * expanded transparency value is supplied, an alpha channel is built.
3830 void /* PRIVATE */
3831 png_do_expand(png_row_infop row_info, png_bytep row,
3832 png_color_16p trans_value)
3834 int shift, value;
3835 png_bytep sp, dp;
3836 png_uint_32 i;
3837 png_uint_32 row_width=row_info->width;
3839 png_debug(1, "in png_do_expand");
3841 #ifdef PNG_USELESS_TESTS_SUPPORTED
3842 if (row != NULL && row_info != NULL)
3843 #endif
3845 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
3847 png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
3849 if (row_info->bit_depth < 8)
3851 switch (row_info->bit_depth)
3853 case 1:
3855 gray = (png_uint_16)((gray&0x01)*0xff);
3856 sp = row + (png_size_t)((row_width - 1) >> 3);
3857 dp = row + (png_size_t)row_width - 1;
3858 shift = 7 - (int)((row_width + 7) & 0x07);
3859 for (i = 0; i < row_width; i++)
3861 if ((*sp >> shift) & 0x01)
3862 *dp = 0xff;
3863 else
3864 *dp = 0;
3865 if (shift == 7)
3867 shift = 0;
3868 sp--;
3870 else
3871 shift++;
3873 dp--;
3875 break;
3878 case 2:
3880 gray = (png_uint_16)((gray&0x03)*0x55);
3881 sp = row + (png_size_t)((row_width - 1) >> 2);
3882 dp = row + (png_size_t)row_width - 1;
3883 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3884 for (i = 0; i < row_width; i++)
3886 value = (*sp >> shift) & 0x03;
3887 *dp = (png_byte)(value | (value << 2) | (value << 4) |
3888 (value << 6));
3889 if (shift == 6)
3891 shift = 0;
3892 sp--;
3894 else
3895 shift += 2;
3897 dp--;
3899 break;
3902 case 4:
3904 gray = (png_uint_16)((gray&0x0f)*0x11);
3905 sp = row + (png_size_t)((row_width - 1) >> 1);
3906 dp = row + (png_size_t)row_width - 1;
3907 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
3908 for (i = 0; i < row_width; i++)
3910 value = (*sp >> shift) & 0x0f;
3911 *dp = (png_byte)(value | (value << 4));
3912 if (shift == 4)
3914 shift = 0;
3915 sp--;
3917 else
3918 shift = 4;
3920 dp--;
3922 break;
3926 row_info->bit_depth = 8;
3927 row_info->pixel_depth = 8;
3928 row_info->rowbytes = row_width;
3931 if (trans_value != NULL)
3933 if (row_info->bit_depth == 8)
3935 gray = gray & 0xff;
3936 sp = row + (png_size_t)row_width - 1;
3937 dp = row + (png_size_t)(row_width << 1) - 1;
3938 for (i = 0; i < row_width; i++)
3940 if (*sp == gray)
3941 *dp-- = 0;
3942 else
3943 *dp-- = 0xff;
3944 *dp-- = *sp--;
3948 else if (row_info->bit_depth == 16)
3950 png_byte gray_high = (gray >> 8) & 0xff;
3951 png_byte gray_low = gray & 0xff;
3952 sp = row + row_info->rowbytes - 1;
3953 dp = row + (row_info->rowbytes << 1) - 1;
3954 for (i = 0; i < row_width; i++)
3956 if (*(sp - 1) == gray_high && *(sp) == gray_low)
3958 *dp-- = 0;
3959 *dp-- = 0;
3961 else
3963 *dp-- = 0xff;
3964 *dp-- = 0xff;
3966 *dp-- = *sp--;
3967 *dp-- = *sp--;
3971 row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3972 row_info->channels = 2;
3973 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
3974 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
3975 row_width);
3978 else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
3980 if (row_info->bit_depth == 8)
3982 png_byte red = trans_value->red & 0xff;
3983 png_byte green = trans_value->green & 0xff;
3984 png_byte blue = trans_value->blue & 0xff;
3985 sp = row + (png_size_t)row_info->rowbytes - 1;
3986 dp = row + (png_size_t)(row_width << 2) - 1;
3987 for (i = 0; i < row_width; i++)
3989 if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
3990 *dp-- = 0;
3991 else
3992 *dp-- = 0xff;
3993 *dp-- = *sp--;
3994 *dp-- = *sp--;
3995 *dp-- = *sp--;
3998 else if (row_info->bit_depth == 16)
4000 png_byte red_high = (trans_value->red >> 8) & 0xff;
4001 png_byte green_high = (trans_value->green >> 8) & 0xff;
4002 png_byte blue_high = (trans_value->blue >> 8) & 0xff;
4003 png_byte red_low = trans_value->red & 0xff;
4004 png_byte green_low = trans_value->green & 0xff;
4005 png_byte blue_low = trans_value->blue & 0xff;
4006 sp = row + row_info->rowbytes - 1;
4007 dp = row + (png_size_t)(row_width << 3) - 1;
4008 for (i = 0; i < row_width; i++)
4010 if (*(sp - 5) == red_high &&
4011 *(sp - 4) == red_low &&
4012 *(sp - 3) == green_high &&
4013 *(sp - 2) == green_low &&
4014 *(sp - 1) == blue_high &&
4015 *(sp ) == blue_low)
4017 *dp-- = 0;
4018 *dp-- = 0;
4020 else
4022 *dp-- = 0xff;
4023 *dp-- = 0xff;
4025 *dp-- = *sp--;
4026 *dp-- = *sp--;
4027 *dp-- = *sp--;
4028 *dp-- = *sp--;
4029 *dp-- = *sp--;
4030 *dp-- = *sp--;
4033 row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
4034 row_info->channels = 4;
4035 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
4036 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4040 #endif
4042 #ifdef PNG_READ_DITHER_SUPPORTED
4043 void /* PRIVATE */
4044 png_do_dither(png_row_infop row_info, png_bytep row,
4045 png_bytep palette_lookup, png_bytep dither_lookup)
4047 png_bytep sp, dp;
4048 png_uint_32 i;
4049 png_uint_32 row_width=row_info->width;
4051 png_debug(1, "in png_do_dither");
4053 #ifdef PNG_USELESS_TESTS_SUPPORTED
4054 if (row != NULL && row_info != NULL)
4055 #endif
4057 if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
4058 palette_lookup && row_info->bit_depth == 8)
4060 int r, g, b, p;
4061 sp = row;
4062 dp = row;
4063 for (i = 0; i < row_width; i++)
4065 r = *sp++;
4066 g = *sp++;
4067 b = *sp++;
4069 /* This looks real messy, but the compiler will reduce
4070 * it down to a reasonable formula. For example, with
4071 * 5 bits per color, we get:
4072 * p = (((r >> 3) & 0x1f) << 10) |
4073 * (((g >> 3) & 0x1f) << 5) |
4074 * ((b >> 3) & 0x1f);
4076 p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
4077 ((1 << PNG_DITHER_RED_BITS) - 1)) <<
4078 (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
4079 (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
4080 ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
4081 (PNG_DITHER_BLUE_BITS)) |
4082 ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
4083 ((1 << PNG_DITHER_BLUE_BITS) - 1));
4085 *dp++ = palette_lookup[p];
4087 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4088 row_info->channels = 1;
4089 row_info->pixel_depth = row_info->bit_depth;
4090 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4092 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
4093 palette_lookup != NULL && row_info->bit_depth == 8)
4095 int r, g, b, p;
4096 sp = row;
4097 dp = row;
4098 for (i = 0; i < row_width; i++)
4100 r = *sp++;
4101 g = *sp++;
4102 b = *sp++;
4103 sp++;
4105 p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
4106 ((1 << PNG_DITHER_RED_BITS) - 1)) <<
4107 (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
4108 (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
4109 ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
4110 (PNG_DITHER_BLUE_BITS)) |
4111 ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
4112 ((1 << PNG_DITHER_BLUE_BITS) - 1));
4114 *dp++ = palette_lookup[p];
4116 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4117 row_info->channels = 1;
4118 row_info->pixel_depth = row_info->bit_depth;
4119 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4121 else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
4122 dither_lookup && row_info->bit_depth == 8)
4124 sp = row;
4125 for (i = 0; i < row_width; i++, sp++)
4127 *sp = dither_lookup[*sp];
4132 #endif
4134 #ifdef PNG_FLOATING_POINT_SUPPORTED
4135 #ifdef PNG_READ_GAMMA_SUPPORTED
4136 static PNG_CONST int png_gamma_shift[] =
4137 {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00};
4139 /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit
4140 * tables, we don't make a full table if we are reducing to 8-bit in
4141 * the future. Note also how the gamma_16 tables are segmented so that
4142 * we don't need to allocate > 64K chunks for a full 16-bit table.
4144 * See the PNG extensions document for an integer algorithm for creating
4145 * the gamma tables. Maybe we will implement that here someday.
4147 * We should only reach this point if
4149 * the file_gamma is known (i.e., the gAMA or sRGB chunk is present,
4150 * or the application has provided a file_gamma)
4152 * AND
4154 * the screen_gamma is known
4155 * OR
4157 * RGB_to_gray transformation is being performed
4160 * AND
4162 * the screen_gamma is different from the reciprocal of the
4163 * file_gamma by more than the specified threshold
4165 * OR
4167 * a background color has been specified and the file_gamma
4168 * and screen_gamma are not 1.0, within the specified threshold.
4172 void /* PRIVATE */
4173 png_build_gamma_table(png_structp png_ptr)
4175 png_debug(1, "in png_build_gamma_table");
4177 if (png_ptr->bit_depth <= 8)
4179 int i;
4180 double g;
4182 if (png_ptr->screen_gamma > .000001)
4183 g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
4185 else
4186 g = 1.0;
4188 png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
4189 (png_uint_32)256);
4191 for (i = 0; i < 256; i++)
4193 png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,
4194 g) * 255.0 + .5);
4197 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4198 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4199 if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY))
4202 g = 1.0 / (png_ptr->gamma);
4204 png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
4205 (png_uint_32)256);
4207 for (i = 0; i < 256; i++)
4209 png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,
4210 g) * 255.0 + .5);
4214 png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
4215 (png_uint_32)256);
4217 if (png_ptr->screen_gamma > 0.000001)
4218 g = 1.0 / png_ptr->screen_gamma;
4220 else
4221 g = png_ptr->gamma; /* Probably doing rgb_to_gray */
4223 for (i = 0; i < 256; i++)
4225 png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
4226 g) * 255.0 + .5);
4230 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
4232 else
4234 double g;
4235 int i, j, shift, num;
4236 int sig_bit;
4237 png_uint_32 ig;
4239 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
4241 sig_bit = (int)png_ptr->sig_bit.red;
4243 if ((int)png_ptr->sig_bit.green > sig_bit)
4244 sig_bit = png_ptr->sig_bit.green;
4246 if ((int)png_ptr->sig_bit.blue > sig_bit)
4247 sig_bit = png_ptr->sig_bit.blue;
4249 else
4251 sig_bit = (int)png_ptr->sig_bit.gray;
4254 if (sig_bit > 0)
4255 shift = 16 - sig_bit;
4257 else
4258 shift = 0;
4260 if (png_ptr->transformations & PNG_16_TO_8)
4262 if (shift < (16 - PNG_MAX_GAMMA_8))
4263 shift = (16 - PNG_MAX_GAMMA_8);
4266 if (shift > 8)
4267 shift = 8;
4269 if (shift < 0)
4270 shift = 0;
4272 png_ptr->gamma_shift = (png_byte)shift;
4274 num = (1 << (8 - shift));
4276 if (png_ptr->screen_gamma > .000001)
4277 g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
4278 else
4279 g = 1.0;
4281 png_ptr->gamma_16_table = (png_uint_16pp)png_calloc(png_ptr,
4282 (png_uint_32)(num * png_sizeof(png_uint_16p)));
4284 if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))
4286 double fin, fout;
4287 png_uint_32 last, max;
4289 for (i = 0; i < num; i++)
4291 png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
4292 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4295 g = 1.0 / g;
4296 last = 0;
4297 for (i = 0; i < 256; i++)
4299 fout = ((double)i + 0.5) / 256.0;
4300 fin = pow(fout, g);
4301 max = (png_uint_32)(fin * (double)((png_uint_32)num << 8));
4302 while (last <= max)
4304 png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
4305 [(int)(last >> (8 - shift))] = (png_uint_16)(
4306 (png_uint_16)i | ((png_uint_16)i << 8));
4307 last++;
4310 while (last < ((png_uint_32)num << 8))
4312 png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
4313 [(int)(last >> (8 - shift))] = (png_uint_16)65535L;
4314 last++;
4317 else
4319 for (i = 0; i < num; i++)
4321 png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
4322 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4324 ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);
4326 for (j = 0; j < 256; j++)
4328 png_ptr->gamma_16_table[i][j] =
4329 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
4330 65535.0, g) * 65535.0 + .5);
4335 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4336 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4337 if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))
4340 g = 1.0 / (png_ptr->gamma);
4342 png_ptr->gamma_16_to_1 = (png_uint_16pp)png_calloc(png_ptr,
4343 (png_uint_32)(num * png_sizeof(png_uint_16p )));
4345 for (i = 0; i < num; i++)
4347 png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,
4348 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4350 ig = (((png_uint_32)i *
4351 (png_uint_32)png_gamma_shift[shift]) >> 4);
4352 for (j = 0; j < 256; j++)
4354 png_ptr->gamma_16_to_1[i][j] =
4355 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
4356 65535.0, g) * 65535.0 + .5);
4360 if (png_ptr->screen_gamma > 0.000001)
4361 g = 1.0 / png_ptr->screen_gamma;
4363 else
4364 g = png_ptr->gamma; /* Probably doing rgb_to_gray */
4366 png_ptr->gamma_16_from_1 = (png_uint_16pp)png_calloc(png_ptr,
4367 (png_uint_32)(num * png_sizeof(png_uint_16p)));
4369 for (i = 0; i < num; i++)
4371 png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
4372 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4374 ig = (((png_uint_32)i *
4375 (png_uint_32)png_gamma_shift[shift]) >> 4);
4377 for (j = 0; j < 256; j++)
4379 png_ptr->gamma_16_from_1[i][j] =
4380 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
4381 65535.0, g) * 65535.0 + .5);
4385 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
4388 #endif
4389 /* To do: install integer version of png_build_gamma_table here */
4390 #endif
4392 #ifdef PNG_MNG_FEATURES_SUPPORTED
4393 /* Undoes intrapixel differencing */
4394 void /* PRIVATE */
4395 png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
4397 png_debug(1, "in png_do_read_intrapixel");
4399 if (
4400 #ifdef PNG_USELESS_TESTS_SUPPORTED
4401 row != NULL && row_info != NULL &&
4402 #endif
4403 (row_info->color_type & PNG_COLOR_MASK_COLOR))
4405 int bytes_per_pixel;
4406 png_uint_32 row_width = row_info->width;
4407 if (row_info->bit_depth == 8)
4409 png_bytep rp;
4410 png_uint_32 i;
4412 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4413 bytes_per_pixel = 3;
4415 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4416 bytes_per_pixel = 4;
4418 else
4419 return;
4421 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4423 *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff);
4424 *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff);
4427 else if (row_info->bit_depth == 16)
4429 png_bytep rp;
4430 png_uint_32 i;
4432 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4433 bytes_per_pixel = 6;
4435 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4436 bytes_per_pixel = 8;
4438 else
4439 return;
4441 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4443 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
4444 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
4445 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
4446 png_uint_32 red = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);
4447 png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);
4448 *(rp ) = (png_byte)((red >> 8) & 0xff);
4449 *(rp+1) = (png_byte)(red & 0xff);
4450 *(rp+4) = (png_byte)((blue >> 8) & 0xff);
4451 *(rp+5) = (png_byte)(blue & 0xff);
4456 #endif /* PNG_MNG_FEATURES_SUPPORTED */
4457 #endif /* PNG_READ_SUPPORTED */