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
20 #define PNG_NO_PEDANTIC_WARNINGS
22 #ifdef PNG_READ_SUPPORTED
24 /* Set the action on getting a CRC error for an ancillary or critical chunk. */
26 png_set_crc_action(png_structp png_ptr
, int crit_action
, int ancil_action
)
28 png_debug(1, "in png_set_crc_action");
33 /* Tell libpng how we react to CRC errors in critical chunks */
36 case PNG_CRC_NO_CHANGE
: /* Leave setting as is */
39 case PNG_CRC_WARN_USE
: /* Warn/use data */
40 png_ptr
->flags
&= ~PNG_FLAG_CRC_CRITICAL_MASK
;
41 png_ptr
->flags
|= PNG_FLAG_CRC_CRITICAL_USE
;
44 case PNG_CRC_QUIET_USE
: /* Quiet/use data */
45 png_ptr
->flags
&= ~PNG_FLAG_CRC_CRITICAL_MASK
;
46 png_ptr
->flags
|= PNG_FLAG_CRC_CRITICAL_USE
|
47 PNG_FLAG_CRC_CRITICAL_IGNORE
;
50 case PNG_CRC_WARN_DISCARD
: /* Not a valid action for critical data */
52 "Can't discard critical data on CRC error.");
53 case PNG_CRC_ERROR_QUIT
: /* Error/quit */
57 png_ptr
->flags
&= ~PNG_FLAG_CRC_CRITICAL_MASK
;
61 /* Tell libpng how we react to CRC errors in ancillary chunks */
64 case PNG_CRC_NO_CHANGE
: /* Leave setting as is */
67 case PNG_CRC_WARN_USE
: /* Warn/use data */
68 png_ptr
->flags
&= ~PNG_FLAG_CRC_ANCILLARY_MASK
;
69 png_ptr
->flags
|= PNG_FLAG_CRC_ANCILLARY_USE
;
72 case PNG_CRC_QUIET_USE
: /* Quiet/use data */
73 png_ptr
->flags
&= ~PNG_FLAG_CRC_ANCILLARY_MASK
;
74 png_ptr
->flags
|= PNG_FLAG_CRC_ANCILLARY_USE
|
75 PNG_FLAG_CRC_ANCILLARY_NOWARN
;
78 case PNG_CRC_ERROR_QUIT
: /* Error/quit */
79 png_ptr
->flags
&= ~PNG_FLAG_CRC_ANCILLARY_MASK
;
80 png_ptr
->flags
|= PNG_FLAG_CRC_ANCILLARY_NOWARN
;
83 case PNG_CRC_WARN_DISCARD
: /* Warn/discard data */
87 png_ptr
->flags
&= ~PNG_FLAG_CRC_ANCILLARY_MASK
;
92 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
93 defined(PNG_FLOATING_POINT_SUPPORTED)
94 /* Handle alpha and tRNS via a background color */
96 png_set_background(png_structp png_ptr
,
97 png_color_16p background_color
, int background_gamma_code
,
98 int need_expand
, double background_gamma
)
100 png_debug(1, "in png_set_background");
104 if (background_gamma_code
== PNG_BACKGROUND_GAMMA_UNKNOWN
)
106 png_warning(png_ptr
, "Application must supply a known background gamma");
110 png_ptr
->transformations
|= PNG_BACKGROUND
;
111 png_memcpy(&(png_ptr
->background
), background_color
,
112 png_sizeof(png_color_16
));
113 png_ptr
->background_gamma
= (float)background_gamma
;
114 png_ptr
->background_gamma_type
= (png_byte
)(background_gamma_code
);
115 png_ptr
->transformations
|= (need_expand
? PNG_BACKGROUND_EXPAND
: 0);
119 #ifdef PNG_READ_16_TO_8_SUPPORTED
120 /* Strip 16 bit depth files to 8 bit depth */
122 png_set_strip_16(png_structp png_ptr
)
124 png_debug(1, "in png_set_strip_16");
128 png_ptr
->transformations
|= PNG_16_TO_8
;
132 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
134 png_set_strip_alpha(png_structp png_ptr
)
136 png_debug(1, "in png_set_strip_alpha");
140 png_ptr
->flags
|= PNG_FLAG_STRIP_ALPHA
;
144 #ifdef PNG_READ_DITHER_SUPPORTED
145 /* Dither file to 8 bit. Supply a palette, the current number
146 * of elements in the palette, the maximum number of elements
147 * allowed, and a histogram if possible. If the current number
148 * of colors is greater then the maximum number, the palette will be
149 * modified to fit in the maximum number. "full_dither" indicates
150 * whether we need a dithering cube set up for RGB images, or if we
151 * simply are reducing the number of colors in a paletted image.
154 typedef struct png_dsort_struct
156 struct png_dsort_struct FAR
* next
;
160 typedef png_dsort FAR
* png_dsortp
;
161 typedef png_dsort FAR
* FAR
* png_dsortpp
;
164 png_set_dither(png_structp png_ptr
, png_colorp palette
,
165 int num_palette
, int maximum_colors
, png_uint_16p histogram
,
168 png_debug(1, "in png_set_dither");
172 png_ptr
->transformations
|= PNG_DITHER
;
178 png_ptr
->dither_index
= (png_bytep
)png_malloc(png_ptr
,
179 (png_uint_32
)(num_palette
* png_sizeof(png_byte
)));
180 for (i
= 0; i
< num_palette
; i
++)
181 png_ptr
->dither_index
[i
] = (png_byte
)i
;
184 if (num_palette
> maximum_colors
)
186 if (histogram
!= NULL
)
188 /* This is easy enough, just throw out the least used colors.
189 * Perhaps not the best solution, but good enough.
194 /* Initialize an array to sort colors */
195 png_ptr
->dither_sort
= (png_bytep
)png_malloc(png_ptr
,
196 (png_uint_32
)(num_palette
* png_sizeof(png_byte
)));
198 /* Initialize the dither_sort array */
199 for (i
= 0; i
< num_palette
; i
++)
200 png_ptr
->dither_sort
[i
] = (png_byte
)i
;
202 /* Find the least used palette entries by starting a
203 * bubble sort, and running it until we have sorted
204 * out enough colors. Note that we don't care about
205 * sorting all the colors, just finding which are
209 for (i
= num_palette
- 1; i
>= maximum_colors
; i
--)
211 int done
; /* To stop early if the list is pre-sorted */
215 for (j
= 0; j
< i
; j
++)
217 if (histogram
[png_ptr
->dither_sort
[j
]]
218 < histogram
[png_ptr
->dither_sort
[j
+ 1]])
222 t
= png_ptr
->dither_sort
[j
];
223 png_ptr
->dither_sort
[j
] = png_ptr
->dither_sort
[j
+ 1];
224 png_ptr
->dither_sort
[j
+ 1] = t
;
232 /* Swap the palette around, and set up a table, if necessary */
237 /* Put all the useful colors within the max, but don't
240 for (i
= 0; i
< maximum_colors
; i
++)
242 if ((int)png_ptr
->dither_sort
[i
] >= maximum_colors
)
246 while ((int)png_ptr
->dither_sort
[j
] >= maximum_colors
);
247 palette
[i
] = palette
[j
];
255 /* Move all the used colors inside the max limit, and
256 * develop a translation table.
258 for (i
= 0; i
< maximum_colors
; i
++)
260 /* Only move the colors we need to */
261 if ((int)png_ptr
->dither_sort
[i
] >= maximum_colors
)
267 while ((int)png_ptr
->dither_sort
[j
] >= maximum_colors
);
269 tmp_color
= palette
[j
];
270 palette
[j
] = palette
[i
];
271 palette
[i
] = tmp_color
;
272 /* Indicate where the color went */
273 png_ptr
->dither_index
[j
] = (png_byte
)i
;
274 png_ptr
->dither_index
[i
] = (png_byte
)j
;
278 /* Find closest color for those colors we are not using */
279 for (i
= 0; i
< num_palette
; i
++)
281 if ((int)png_ptr
->dither_index
[i
] >= maximum_colors
)
283 int min_d
, k
, min_k
, d_index
;
285 /* Find the closest color to one we threw out */
286 d_index
= png_ptr
->dither_index
[i
];
287 min_d
= PNG_COLOR_DIST(palette
[d_index
], palette
[0]);
288 for (k
= 1, min_k
= 0; k
< maximum_colors
; k
++)
292 d
= PNG_COLOR_DIST(palette
[d_index
], palette
[k
]);
300 /* Point to closest color */
301 png_ptr
->dither_index
[i
] = (png_byte
)min_k
;
305 png_free(png_ptr
, png_ptr
->dither_sort
);
306 png_ptr
->dither_sort
= NULL
;
310 /* This is much harder to do simply (and quickly). Perhaps
311 * we need to go through a median cut routine, but those
312 * don't always behave themselves with only a few colors
313 * as input. So we will just find the closest two colors,
314 * and throw out one of them (chosen somewhat randomly).
315 * [We don't understand this at all, so if someone wants to
316 * work on improving it, be our guest - AED, GRP]
326 /* Initialize palette index arrays */
327 png_ptr
->index_to_palette
= (png_bytep
)png_malloc(png_ptr
,
328 (png_uint_32
)(num_palette
* png_sizeof(png_byte
)));
329 png_ptr
->palette_to_index
= (png_bytep
)png_malloc(png_ptr
,
330 (png_uint_32
)(num_palette
* png_sizeof(png_byte
)));
332 /* Initialize the sort array */
333 for (i
= 0; i
< num_palette
; i
++)
335 png_ptr
->index_to_palette
[i
] = (png_byte
)i
;
336 png_ptr
->palette_to_index
[i
] = (png_byte
)i
;
339 hash
= (png_dsortpp
)png_calloc(png_ptr
, (png_uint_32
)(769 *
340 png_sizeof(png_dsortp
)));
342 num_new_palette
= num_palette
;
344 /* Initial wild guess at how far apart the farthest pixel
345 * pair we will be eliminating will be. Larger
346 * numbers mean more areas will be allocated, Smaller
347 * numbers run the risk of not saving enough data, and
348 * having to do this all over again.
350 * I have not done extensive checking on this number.
354 while (num_new_palette
> maximum_colors
)
356 for (i
= 0; i
< num_new_palette
- 1; i
++)
360 for (j
= i
+ 1; j
< num_new_palette
; j
++)
364 d
= PNG_COLOR_DIST(palette
[i
], palette
[j
]);
369 t
= (png_dsortp
)png_malloc_warn(png_ptr
,
370 (png_uint_32
)(png_sizeof(png_dsort
)));
374 t
->left
= (png_byte
)i
;
375 t
->right
= (png_byte
)j
;
384 for (i
= 0; i
<= max_d
; i
++)
390 for (p
= hash
[i
]; p
; p
= p
->next
)
392 if ((int)png_ptr
->index_to_palette
[p
->left
]
394 (int)png_ptr
->index_to_palette
[p
->right
]
399 if (num_new_palette
& 0x01)
411 palette
[png_ptr
->index_to_palette
[j
]]
412 = palette
[num_new_palette
];
417 for (k
= 0; k
< num_palette
; k
++)
419 if (png_ptr
->dither_index
[k
] ==
420 png_ptr
->index_to_palette
[j
])
421 png_ptr
->dither_index
[k
] =
422 png_ptr
->index_to_palette
[next_j
];
423 if ((int)png_ptr
->dither_index
[k
] ==
425 png_ptr
->dither_index
[k
] =
426 png_ptr
->index_to_palette
[j
];
430 png_ptr
->index_to_palette
[png_ptr
->palette_to_index
431 [num_new_palette
]] = png_ptr
->index_to_palette
[j
];
432 png_ptr
->palette_to_index
[png_ptr
->index_to_palette
[j
]]
433 = png_ptr
->palette_to_index
[num_new_palette
];
435 png_ptr
->index_to_palette
[j
] =
436 (png_byte
)num_new_palette
;
437 png_ptr
->palette_to_index
[num_new_palette
] =
440 if (num_new_palette
<= maximum_colors
)
443 if (num_new_palette
<= maximum_colors
)
448 for (i
= 0; i
< 769; i
++)
452 png_dsortp p
= hash
[i
];
456 png_free(png_ptr
, p
);
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
;
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
++)
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
);
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
);
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.
552 png_set_gamma(png_structp png_ptr
, double scrn_gamma
, double file_gamma
)
554 png_debug(1, "in png_set_gamma");
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
;
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
574 png_set_expand(png_structp png_ptr
)
576 png_debug(1, "in png_set_expand");
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
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. */
605 png_set_palette_to_rgb(png_structp png_ptr
)
607 png_debug(1, "in png_set_palette_to_rgb");
612 png_ptr
->transformations
|= (PNG_EXPAND
| PNG_EXPAND_tRNS
);
613 png_ptr
->flags
&= ~PNG_FLAG_ROW_INIT
;
617 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
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");
626 png_ptr
->transformations
|= PNG_EXPAND
;
627 png_ptr
->flags
&= ~PNG_FLAG_ROW_INIT
;
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 */
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");
642 png_ptr
->transformations
|= (PNG_EXPAND
| PNG_EXPAND_tRNS
);
647 /* Expand tRNS chunks to alpha channels. */
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
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
;
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.
676 png_set_rgb_to_gray(png_structp png_ptr
, int error_action
, double red
,
679 int red_fixed
= (int)((float)red
*100000.0 + 0.5);
680 int green_fixed
= (int)((float)green
*100000.0 + 0.5);
683 png_set_rgb_to_gray_fixed(png_ptr
, error_action
, red_fixed
, green_fixed
);
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");
698 case 1: png_ptr
->transformations
|= PNG_RGB_TO_GRAY
;
701 case 2: png_ptr
->transformations
|= PNG_RGB_TO_GRAY_WARN
;
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
;
712 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED.");
713 png_ptr
->transformations
&= ~PNG_RGB_TO_GRAY
;
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);
730 png_warning(png_ptr
, "ignoring out of range rgb_to_gray coefficients");
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
);
742 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
743 defined(PNG_LEGACY_SUPPORTED) || \
744 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
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");
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
;
758 #ifdef PNG_LEGACY_SUPPORTED
759 if (read_user_transform_fn
)
761 "This version of libpng does not support user transforms");
766 /* Initialize everything needed for the read. This includes modifying
770 png_init_read_transformations(png_structp png_ptr
)
772 png_debug(1, "in png_init_read_transformations");
774 #ifdef PNG_USELESS_TESTS_SUPPORTED
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
;
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
;
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
)
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
;
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
;
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
;
859 png_ptr
->background
.red
= png_ptr
->background
.green
860 = png_ptr
->background
.blue
= png_ptr
->background
.gray
;
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
))
880 /* Invert the alpha channel (in tRNS) unless the pixels are
881 * going to be expanded, in which case leave it for later
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
]);
895 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
896 png_ptr
->background_1
= png_ptr
->background
;
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
))
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 */
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
;
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
];
944 switch (png_ptr
->background_gamma_type
)
946 case PNG_BACKGROUND_GAMMA_SCREEN
:
947 g
= (png_ptr
->screen_gamma
);
951 case PNG_BACKGROUND_GAMMA_FILE
:
952 g
= 1.0 / (png_ptr
->gamma
);
953 gs
= 1.0 / (png_ptr
->gamma
* png_ptr
->screen_gamma
);
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
);
962 g
= 1.0; /* back_1 */
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
;
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
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)
998 else /* if (png_ptr->trans[i] != 0xff) */
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
];
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) */
1033 /* color_type != PNG_COLOR_TYPE_PALETTE */
1035 double m
= (double)(((png_uint_32
)1 << png_ptr
->bit_depth
) - 1);
1039 switch (png_ptr
->background_gamma_type
)
1041 case PNG_BACKGROUND_GAMMA_SCREEN
:
1042 g
= (png_ptr
->screen_gamma
);
1046 case PNG_BACKGROUND_GAMMA_FILE
:
1047 g
= 1.0 / (png_ptr
->gamma
);
1048 gs
= 1.0 / (png_ptr
->gamma
* png_ptr
->screen_gamma
);
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
);
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);
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
;
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
;
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
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
))
1121 int istop
= (int)png_ptr
->num_trans
;
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)
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
))
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)
1165 if (sg
< 0 || sg
> 8)
1167 if (sb
< 0 || sb
> 8)
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)
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.
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
;
1203 info_ptr
->color_type
= PNG_COLOR_TYPE_RGB
;
1204 info_ptr
->bit_depth
= 8;
1205 info_ptr
->num_trans
= 0;
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;
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
;
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
;
1236 #ifdef PNG_FIXED_POINT_SUPPORTED
1237 info_ptr
->int_gamma
= png_ptr
->int_gamma
;
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;
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
;
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
;
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
;
1269 #ifdef PNG_READ_PACK_SUPPORTED
1270 if ((png_ptr
->transformations
& PNG_PACK
) && (info_ptr
->bit_depth
< 8))
1271 info_ptr
->bit_depth
= 8;
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;
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
;
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 */
1298 if (png_ptr
->transformations
& PNG_ADD_ALPHA
)
1299 info_ptr
->color_type
|= PNG_COLOR_MASK_ALPHA
;
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
;
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
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.
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)
1340 png_snprintf2(msg
, 50,
1341 "NULL row buffer for row %ld, pass %d", (long)png_ptr
->row_number
,
1343 png_error(png_ptr
, msg
);
1345 png_error(png_ptr
, "NULL row buffer");
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");
1357 png_warning(png_ptr
, "Uninitialized row");
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
);
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
));
1376 png_do_expand(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
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
));
1388 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1389 if (png_ptr
->transformations
& PNG_RGB_TO_GRAY
)
1392 png_do_rgb_to_gray(png_ptr
, &(png_ptr
->row_info
),
1393 png_ptr
->row_buf
+ 1);
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");
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);
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
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
))) &&
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
);
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);
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");
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);
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,
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);
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);
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);
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);
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
);
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);
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);
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);
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
;
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
);
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.
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)
1589 if (row_info
->bit_depth
< 8)
1593 png_uint_32 row_width
=row_info
->width
;
1595 switch (row_info
->bit_depth
)
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);
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);
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);
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
;
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.
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");
1680 #ifdef PNG_USELESS_TESTS_SUPPORTED
1681 row
!= NULL
&& row_info
!= NULL
&& sig_bits
!= NULL
&&
1683 row_info
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
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
;
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
++)
1717 switch (row_info
->bit_depth
)
1723 png_uint_32 istop
= row_info
->rowbytes
;
1725 for (bp
= row
, i
= 0; i
< istop
; 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
++)
1753 png_uint_32 istop
= row_width
* channels
;
1755 for (i
= 0; i
< istop
; i
++)
1757 *bp
++ >>= shift
[i
%channels
];
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);
1782 #ifdef PNG_READ_16_TO_8_SUPPORTED
1783 /* Chop rows of bit depth 16 down to 8 */
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)
1792 if (row_info
->bit_depth
== 16)
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);
1830 /* Simply discard the low order byte */
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
;
1841 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
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
)
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
;
1862 for (i
= 0; i
< row_width
; i
++)
1871 /* This converts from RRGGBBAA to AARRGGBB */
1874 png_bytep sp
= row
+ row_info
->rowbytes
;
1879 for (i
= 0; i
< row_width
; i
++)
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
;
1904 for (i
= 0; i
< row_width
; i
++)
1911 /* This converts from GGAA to AAGG */
1914 png_bytep sp
= row
+ row_info
->rowbytes
;
1919 for (i
= 0; i
< row_width
; i
++)
1934 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
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
)
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
;
1954 for (i
= 0; i
< row_width
; i
++)
1956 *(--dp
) = (png_byte
)(255 - *(--sp
));
1958 /* This does nothing:
1962 We can replace it with:
1968 /* This inverts the alpha channel in RRGGBBAA */
1971 png_bytep sp
= row
+ row_info
->rowbytes
;
1975 for (i
= 0; i
< row_width
; i
++)
1977 *(--dp
) = (png_byte
)(255 - *(--sp
));
1978 *(--dp
) = (png_byte
)(255 - *(--sp
));
1980 /* This does nothing:
1987 We can replace it with:
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
;
2003 for (i
= 0; i
< row_width
; i
++)
2005 *(--dp
) = (png_byte
)(255 - *(--sp
));
2009 /* This inverts the alpha channel in GGAA */
2012 png_bytep sp
= row
+ row_info
->rowbytes
;
2016 for (i
= 0; i
< row_width
; i
++)
2018 *(--dp
) = (png_byte
)(255 - *(--sp
));
2019 *(--dp
) = (png_byte
)(255 - *(--sp
));
2033 #ifdef PNG_READ_FILLER_SUPPORTED
2034 /* Add filler channel if we have RGB color */
2036 png_do_read_filler(png_row_infop row_info
, png_bytep row
,
2037 png_uint_32 filler
, png_uint_32 flags
)
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");
2048 #ifdef PNG_USELESS_TESTS_SUPPORTED
2049 row
!= NULL
&& row_info
!= NULL
&&
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
;
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 */
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
++)
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
;
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 */
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
++)
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
;
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 */
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
++)
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
;
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 */
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
++)
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 */
2210 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2211 /* Expand grayscale files to RGB, with or without alpha */
2213 png_do_gray_to_rgb(png_row_infop row_info
, png_bytep row
)
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
&&
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
++)
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
++)
2246 *(dp
--) = *(sp
- 1);
2248 *(dp
--) = *(sp
- 1);
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
++)
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
++)
2277 *(dp
--) = *(sp
- 1);
2279 *(dp
--) = *(sp
- 1);
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
);
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)
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().
2317 png_do_rgb_to_gray(png_structp png_ptr
, png_row_infop row_info
, png_bytep row
)
2322 png_uint_32 row_width
= row_info
->width
;
2325 png_debug(1, "in png_do_rgb_to_gray");
2328 #ifdef PNG_USELESS_TESTS_SUPPORTED
2329 row
!= NULL
&& row_info
!= NULL
&&
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
)
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
)
2355 *(dp
++) = png_ptr
->gamma_from_1
[
2356 (rc
*red
+ gc
*green
+ bc
*blue
)>>15];
2359 *(dp
++) = *(sp
- 1);
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
)
2375 *(dp
++) = (png_byte
)((rc
*red
+ gc
*green
+ bc
*blue
)>>15);
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
)
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
)
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
2412 w
= png_ptr
->gamma_16_from_1
[(gray16
&0xff) >>
2413 png_ptr
->gamma_shift
][gray16
>> 8];
2417 *(dp
++) = (png_byte
)((w
>>8) & 0xff);
2418 *(dp
++) = (png_byte
)(w
& 0xff);
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
)
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
)
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
)
2459 *(dp
++) = png_ptr
->gamma_from_1
2460 [(rc
*red
+ gc
*green
+ bc
*blue
)>>15];
2461 *(dp
++) = *(sp
++); /* alpha */
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
)
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
)
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
)
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];
2515 *(dp
++) = (png_byte
)((w
>>8) & 0xff);
2516 *(dp
++) = (png_byte
)(w
& 0xff);
2517 *(dp
++) = *(sp
++); /* alpha */
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
)
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 */
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
);
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
2559 png_build_grayscale_palette(int bit_depth
, png_colorp palette
)
2566 png_debug(1, "in png_do_build_grayscale_palette");
2568 if (palette
== NULL
)
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)
2611 png_correct_palette(png_structp png_ptr
, png_colorp 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
];
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
;
2649 (png_byte
)(pow((double)png_ptr
->background
.red
/255, g
) *
2652 (png_byte
)(pow((double)png_ptr
->background
.green
/255, g
) *
2655 (png_byte
)(pow((double)png_ptr
->background
.blue
/255, g
) *
2659 g
= 1.0 / png_ptr
->background_gamma
;
2662 (png_byte
)(pow((double)png_ptr
->background
.red
/255, g
) *
2665 (png_byte
)(pow((double)png_ptr
->background
.green
/255, g
) *
2668 (png_byte
)(pow((double)png_ptr
->background
.blue
/255, g
) *
2672 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
2676 for (i
= 0; i
< (png_uint_32
)num_palette
; i
++)
2678 if (i
< png_ptr
->num_trans
&& png_ptr
->trans
[i
] == 0)
2682 else if (i
< png_ptr
->num_trans
&& png_ptr
->trans
[i
] != 0xff)
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
];
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
];
2710 for (i
= 0; i
< num_palette
; i
++)
2712 if (palette
[i
].red
== (png_byte
)png_ptr
->trans_values
.gray
)
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
];
2727 #ifdef PNG_READ_GAMMA_SUPPORTED
2728 if (png_ptr
->transformations
& PNG_GAMMA
)
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
2743 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2744 if (png_ptr
->transformations
& PNG_BACKGROUND
)
2746 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
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?) */
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
;
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.
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
2810 png_uint_32 row_width
=row_info
->width
;
2813 png_debug(1, "in png_do_background");
2815 if (background
!= NULL
&&
2816 #ifdef PNG_USELESS_TESTS_SUPPORTED
2817 row
!= NULL
&& row_info
!= NULL
&&
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
)
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
);
2853 #ifdef PNG_READ_GAMMA_SUPPORTED
2854 if (gamma_table
!= NULL
)
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
);
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
);
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
);
2910 #ifdef PNG_READ_GAMMA_SUPPORTED
2911 if (gamma_table
!= NULL
)
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
);
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
);
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
);
2967 #ifdef PNG_READ_GAMMA_SUPPORTED
2968 if (gamma_table
!= NULL
)
2971 for (i
= 0; i
< row_width
; i
++, sp
++)
2973 if (*sp
== trans_values
->gray
)
2975 *sp
= (png_byte
)background
->gray
;
2979 *sp
= gamma_table
[*sp
];
2987 for (i
= 0; i
< row_width
; i
++, sp
++)
2989 if (*sp
== trans_values
->gray
)
2991 *sp
= (png_byte
)background
->gray
;
3000 #ifdef PNG_READ_GAMMA_SUPPORTED
3001 if (gamma_16
!= NULL
)
3004 for (i
= 0; i
< row_width
; i
++, sp
+= 2)
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);
3017 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
3018 *sp
= (png_byte
)((v
>> 8) & 0xff);
3019 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3027 for (i
= 0; i
< row_width
; i
++, sp
+= 2)
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);
3045 case PNG_COLOR_TYPE_RGB
:
3047 if (row_info
->bit_depth
== 8)
3049 #ifdef PNG_READ_GAMMA_SUPPORTED
3050 if (gamma_table
!= NULL
)
3053 for (i
= 0; i
< row_width
; i
++, sp
+= 3)
3055 if (*sp
== trans_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
;
3065 *sp
= gamma_table
[*sp
];
3066 *(sp
+ 1) = gamma_table
[*(sp
+ 1)];
3067 *(sp
+ 2) = gamma_table
[*(sp
+ 2)];
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
)
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);
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);
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);
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
)
3160 for (i
= 0; i
< row_width
; i
++, sp
+= 2, dp
++)
3162 png_uint_16 a
= *(sp
+ 1);
3166 *dp
= gamma_table
[*sp
];
3170 /* Background is already in screen gamma */
3171 *dp
= (png_byte
)background
->gray
;
3177 v
= gamma_to_1
[*sp
];
3178 png_composite(w
, v
, a
, background_1
->gray
);
3179 *dp
= gamma_from_1
[w
];
3188 for (i
= 0; i
< row_width
; i
++, sp
+= 2, dp
++)
3190 png_byte a
= *(sp
+ 1);
3196 #ifdef PNG_READ_GAMMA_SUPPORTED
3199 *dp
= (png_byte
)background
->gray
;
3203 png_composite(*dp
, *sp
, a
, background_1
->gray
);
3206 *dp
= (png_byte
)background
->gray
;
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
)
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)
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
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
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);
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
3273 *dp
= (png_byte
)((background
->gray
>> 8) & 0xff);
3274 *(dp
+ 1) = (png_byte
)(background
->gray
& 0xff);
3276 #ifdef PNG_READ_GAMMA_SUPPORTED
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);
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
)
3303 for (i
= 0; i
< row_width
; i
++, sp
+= 4, dp
+= 3)
3305 png_byte a
= *(sp
+ 3);
3309 *dp
= gamma_table
[*sp
];
3310 *(dp
+ 1) = gamma_table
[*(sp
+ 1)];
3311 *(dp
+ 2) = gamma_table
[*(sp
+ 2)];
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
;
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
];
3341 for (i
= 0; i
< row_width
; i
++, sp
+= 4, dp
+= 3)
3343 png_byte a
= *(sp
+ 3);
3348 *(dp
+ 1) = *(sp
+ 1);
3349 *(dp
+ 2) = *(sp
+ 2);
3353 *dp
= (png_byte
)background
->red
;
3354 *(dp
+ 1) = (png_byte
)background
->green
;
3355 *(dp
+ 2) = (png_byte
)background
->blue
;
3359 png_composite(*dp
, *sp
, a
, background
->red
);
3360 png_composite(*(dp
+ 1), *(sp
+ 1), a
,
3362 png_composite(*(dp
+ 2), *(sp
+ 2), a
,
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
)
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)
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);
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);
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);
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);
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);
3452 png_uint_16 r
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3453 png_uint_16 g
= (png_uint_16
)(((*(sp
+ 2)) << 8)
3455 png_uint_16 b
= (png_uint_16
)(((*(sp
+ 4)) << 8)
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);
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
);
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().
3495 png_do_gamma(png_row_infop row_info
, png_bytep row
,
3496 png_bytep gamma_table
, png_uint_16pp gamma_16_table
,
3501 png_uint_32 row_width
=row_info
->width
;
3503 png_debug(1, "in png_do_gamma");
3506 #ifdef PNG_USELESS_TESTS_SUPPORTED
3507 row
!= NULL
&& row_info
!= NULL
&&
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)
3519 for (i
= 0; i
< row_width
; i
++)
3521 *sp
= gamma_table
[*sp
];
3523 *sp
= gamma_table
[*sp
];
3525 *sp
= gamma_table
[*sp
];
3529 else /* if (row_info->bit_depth == 16) */
3532 for (i
= 0; i
< row_width
; i
++)
3536 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3537 *sp
= (png_byte
)((v
>> 8) & 0xff);
3538 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3540 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3541 *sp
= (png_byte
)((v
>> 8) & 0xff);
3542 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3544 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3545 *sp
= (png_byte
)((v
>> 8) & 0xff);
3546 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3553 case PNG_COLOR_TYPE_RGB_ALPHA
:
3555 if (row_info
->bit_depth
== 8)
3558 for (i
= 0; i
< row_width
; i
++)
3560 *sp
= gamma_table
[*sp
];
3562 *sp
= gamma_table
[*sp
];
3564 *sp
= gamma_table
[*sp
];
3569 else /* if (row_info->bit_depth == 16) */
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);
3578 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3579 *sp
= (png_byte
)((v
>> 8) & 0xff);
3580 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3582 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3583 *sp
= (png_byte
)((v
>> 8) & 0xff);
3584 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3591 case PNG_COLOR_TYPE_GRAY_ALPHA
:
3593 if (row_info
->bit_depth
== 8)
3596 for (i
= 0; i
< row_width
; i
++)
3598 *sp
= gamma_table
[*sp
];
3602 else /* if (row_info->bit_depth == 16) */
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);
3616 case PNG_COLOR_TYPE_GRAY
:
3618 if (row_info
->bit_depth
== 2)
3621 for (i
= 0; i
< row_width
; i
+= 4)
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) ));
3637 if (row_info
->bit_depth
== 4)
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));
3651 else if (row_info
->bit_depth
== 8)
3654 for (i
= 0; i
< row_width
; i
++)
3656 *sp
= gamma_table
[*sp
];
3661 else if (row_info
->bit_depth
== 16)
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);
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.
3684 png_do_expand_palette(png_row_infop row_info
, png_bytep row
,
3685 png_colorp palette
, png_bytep trans
, int num_trans
)
3690 png_uint_32 row_width
=row_info
->width
;
3692 png_debug(1, "in png_do_expand_palette");
3695 #ifdef PNG_USELESS_TESTS_SUPPORTED
3696 row
!= NULL
&& row_info
!= NULL
&&
3698 row_info
->color_type
== PNG_COLOR_TYPE_PALETTE
)
3700 if (row_info
->bit_depth
< 8)
3702 switch (row_info
->bit_depth
)
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)
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
;
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
;
3772 row_info
->bit_depth
= 8;
3773 row_info
->pixel_depth
= 8;
3774 row_info
->rowbytes
= row_width
;
3776 switch (row_info
->bit_depth
)
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
)
3791 *dp
-- = palette
[*sp
].blue
;
3792 *dp
-- = palette
[*sp
].green
;
3793 *dp
-- = palette
[*sp
].red
;
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;
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
;
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;
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.
3831 png_do_expand(png_row_infop row_info
, png_bytep row
,
3832 png_color_16p trans_value
)
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
)
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
)
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)
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) |
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));
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)
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
++)
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
)
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
,
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
)
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
&&
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
);
4042 #ifdef PNG_READ_DITHER_SUPPORTED
4044 png_do_dither(png_row_infop row_info
, png_bytep row
,
4045 png_bytep palette_lookup
, png_bytep dither_lookup
)
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
)
4057 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
&&
4058 palette_lookup
&& row_info
->bit_depth
== 8)
4063 for (i
= 0; i
< row_width
; i
++)
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)
4098 for (i
= 0; i
< row_width
; i
++)
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)
4125 for (i
= 0; i
< row_width
; i
++, sp
++)
4127 *sp
= dither_lookup
[*sp
];
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)
4154 * the screen_gamma is known
4157 * RGB_to_gray transformation is being performed
4162 * the screen_gamma is different from the reciprocal of the
4163 * file_gamma by more than the specified threshold
4167 * a background color has been specified and the file_gamma
4168 * and screen_gamma are not 1.0, within the specified threshold.
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)
4182 if (png_ptr
->screen_gamma
> .000001)
4183 g
= 1.0 / (png_ptr
->gamma
* png_ptr
->screen_gamma
);
4188 png_ptr
->gamma_table
= (png_bytep
)png_malloc(png_ptr
,
4191 for (i
= 0; i
< 256; i
++)
4193 png_ptr
->gamma_table
[i
] = (png_byte
)(pow((double)i
/ 255.0,
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
,
4207 for (i
= 0; i
< 256; i
++)
4209 png_ptr
->gamma_to_1
[i
] = (png_byte
)(pow((double)i
/ 255.0,
4214 png_ptr
->gamma_from_1
= (png_bytep
)png_malloc(png_ptr
,
4217 if (png_ptr
->screen_gamma
> 0.000001)
4218 g
= 1.0 / png_ptr
->screen_gamma
;
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,
4230 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
4235 int i
, j
, shift
, num
;
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
;
4251 sig_bit
= (int)png_ptr
->sig_bit
.gray
;
4255 shift
= 16 - sig_bit
;
4260 if (png_ptr
->transformations
& PNG_16_TO_8
)
4262 if (shift
< (16 - PNG_MAX_GAMMA_8
))
4263 shift
= (16 - PNG_MAX_GAMMA_8
);
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
);
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
))
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
)));
4297 for (i
= 0; i
< 256; i
++)
4299 fout
= ((double)i
+ 0.5) / 256.0;
4301 max
= (png_uint_32
)(fin
* (double)((png_uint_32
)num
<< 8));
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));
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;
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
;
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 */
4389 /* To do: install integer version of png_build_gamma_table here */
4392 #ifdef PNG_MNG_FEATURES_SUPPORTED
4393 /* Undoes intrapixel differencing */
4395 png_do_read_intrapixel(png_row_infop row_info
, png_bytep row
)
4397 png_debug(1, "in png_do_read_intrapixel");
4400 #ifdef PNG_USELESS_TESTS_SUPPORTED
4401 row
!= NULL
&& row_info
!= NULL
&&
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)
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;
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)
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;
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 */