/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* pngrtran.c - transforms the data in a row for PNG readers
*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file and, per its terms, should not be removed:
*
* Last changed in libpng 1.5.4 [July 7, 2011]
* Copyright (c) 1998-2011 Glenn Randers-Pehrson
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
*
* This code is released under the libpng license.
* For conditions of distribution and use, see the disclaimer
* and license in png.h
*
* This file contains functions optionally called by an application
* in order to tell libpng how to handle data when reading a PNG.
* Transformations that are used in both reading and writing are
* in pngtrans.c.
*/
#include "pngpriv.h"
#ifdef PNG_READ_SUPPORTED
/* Set the action on getting a CRC error for an ancillary or critical chunk. */
void PNGAPI
{
return;
/* Tell libpng how we react to CRC errors in critical chunks */
switch (crit_action)
{
case PNG_CRC_NO_CHANGE: /* Leave setting as is */
break;
case PNG_CRC_WARN_USE: /* Warn/use data */
break;
case PNG_CRC_QUIET_USE: /* Quiet/use data */
break;
case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
"Can't discard critical data on CRC error");
case PNG_CRC_ERROR_QUIT: /* Error/quit */
case PNG_CRC_DEFAULT:
default:
break;
}
/* Tell libpng how we react to CRC errors in ancillary chunks */
switch (ancil_action)
{
case PNG_CRC_NO_CHANGE: /* Leave setting as is */
break;
case PNG_CRC_WARN_USE: /* Warn/use data */
break;
case PNG_CRC_QUIET_USE: /* Quiet/use data */
break;
case PNG_CRC_ERROR_QUIT: /* Error/quit */
break;
case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
case PNG_CRC_DEFAULT:
default:
break;
}
}
/* Handle alpha and tRNS via a background color */
void PNGFAPI
{
return;
{
return;
}
if (need_expand)
else
}
# ifdef PNG_FLOATING_POINT_SUPPORTED
void PNGAPI
int need_expand, double background_gamma)
{
}
# endif /* FLOATING_POINT */
#endif /* READ_BACKGROUND */
/* Scale 16-bit depth files to 8-bit depth. If both of these are set then the
* one that pngrtran does first (scale) happens. This is necessary to allow the
* TRANSFORM and API behavior to be somewhat consistent, and it's simpler.
*/
void PNGAPI
{
return;
}
#endif
/* Chop 16-bit depth files to 8-bit depth */
void PNGAPI
{
return;
}
#endif
void PNGAPI
{
return;
}
#endif
#if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED)
static png_fixed_point
int is_screen)
{
/* Check for flag values. The main reason for having the old Mac value as a
* flag is that it is pretty near impossible to work out what the correct
* value is from Apple documentation - a working Mac system is needed to
* discover the value!
*/
if (output_gamma == PNG_DEFAULT_sRGB ||
{
/* If there is no sRGB support this just sets the gamma to the standard
* sRGB value. (This is a side effect of using this function!)
*/
# ifdef PNG_READ_sRGB_SUPPORTED
# endif
if (is_screen)
else
}
else if (output_gamma == PNG_GAMMA_MAC_18 ||
{
if (is_screen)
else
}
return output_gamma;
}
# ifdef PNG_FLOATING_POINT_SUPPORTED
static png_fixed_point
{
/* The following silently ignores cases where fixed point (times 100,000)
* gamma values are passed to the floating point API. This is safe and it
* means the fixed point constants work just fine with the floating point
* API. The alternative would just lead to undetected errors and spurious
* bug reports. Negative values fail inside the _fixed API unless they
* correspond to the flag values.
*/
output_gamma *= PNG_FP_1;
/* This preserves -1 and -2 exactly: */
return (png_fixed_point)output_gamma;
}
# endif
#endif /* READ_ALPHA_MODE || READ_GAMMA */
void PNGFAPI
{
int compose = 0;
return;
/* Validate the value to ensure it is in a reasonable range. The value
* is expected to be 1 or greater, but this range test allows for some
* viewing correction values. The intent is to weed out users of this API
* who use the inverse of the gamma value accidentally! Since some of these
* values are reasonable this may have to be changed.
*/
/* The default file gamma is the inverse of the output gamma; the output
* gamma may be changed below so get the file value first:
*/
/* There are really 8 possibilities here, composed of any combination
* of:
*
* premultiply the color channels
* do not encode non-opaque pixels
* encode the alpha as well as the color channels
*
* because then the encoding is a no-op and there is only the choice of
* premultiplying the color channels or not.
*
* png_set_alpha_mode and png_set_background interact because both use
* png_compose to do the work. Calling both is only useful when
* png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along
* with a default gamma value. Otherwise PNG_COMPOSE must not be set.
*/
switch (mode)
{
case PNG_ALPHA_PNG: /* default: png standard */
/* No compose, but it may be set by png_set_background! */
break;
case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */
compose = 1;
/* The output is linear: */
break;
case PNG_ALPHA_OPTIMIZED: /* associated, non-opaque pixels linear */
compose = 1;
/* output_gamma records the encoding of opaque pixels! */
break;
case PNG_ALPHA_BROKEN: /* associated, non-linear, alpha encoded */
compose = 1;
break;
default:
}
/* Only set the default gamma if the file gamma has not been set (this has
* the side effect that the gamma in a second call to png_set_alpha_mode will
* be ignored.)
*/
/* But always set the output gamma: */
/* Finally, if pre-multiplying, set the background fields to achieve the
* desired result.
*/
if (compose)
{
/* And obtain alpha pre-multiplication by composing on black: */
"conflicting calls to set alpha mode and background");
}
/* New API, make sure apps call the correct initializers: */
}
# ifdef PNG_FLOATING_POINT_SUPPORTED
void PNGAPI
{
output_gamma));
}
# endif
#endif
#ifdef PNG_READ_QUANTIZE_SUPPORTED
/* Dither file to 8-bit. Supply a palette, the current number
* of elements in the palette, the maximum number of elements
* allowed, and a histogram if possible. If the current number
* of colors is greater then the maximum number, the palette will be
* modified to fit in the maximum number. "full_quantize" indicates
* whether we need a quantizing cube set up for RGB images, or if we
* simply are reducing the number of colors in a paletted image.
*/
typedef struct png_dsort_struct
{
} png_dsort;
void PNGAPI
int full_quantize)
{
return;
if (!full_quantize)
{
int i;
for (i = 0; i < num_palette; i++)
}
if (num_palette > maximum_colors)
{
{
/* This is easy enough, just throw out the least used colors.
* Perhaps not the best solution, but good enough.
*/
int i;
/* Initialize an array to sort colors */
/* Initialize the quantize_sort array */
for (i = 0; i < num_palette; i++)
/* Find the least used palette entries by starting a
* bubble sort, and running it until we have sorted
* out enough colors. Note that we don't care about
* sorting all the colors, just finding which are
* least used.
*/
{
int j;
done = 1;
for (j = 0; j < i; j++)
{
{
png_byte t;
t = png_ptr->quantize_sort[j];
done = 0;
}
}
if (done)
break;
}
/* Swap the palette around, and set up a table, if necessary */
if (full_quantize)
{
int j = num_palette;
/* Put all the useful colors within the max, but don't
* move the others.
*/
for (i = 0; i < maximum_colors; i++)
{
{
do
j--;
}
}
}
else
{
int j = num_palette;
/* Move all the used colors inside the max limit, and
* develop a translation table.
*/
for (i = 0; i < maximum_colors; i++)
{
/* Only move the colors we need to */
{
do
j--;
/* Indicate where the color went */
}
}
/* Find closest color for those colors we are not using */
for (i = 0; i < num_palette; i++)
{
{
/* Find the closest color to one we threw out */
{
int d;
if (d < min_d)
{
min_d = d;
min_k = k;
}
}
/* Point to closest color */
}
}
}
}
else
{
/* This is much harder to do simply (and quickly). Perhaps
* we need to go through a median cut routine, but those
* don't always behave themselves with only a few colors
* as input. So we will just find the closest two colors,
* and throw out one of them (chosen somewhat randomly).
* [We don't understand this at all, so if someone wants to
* work on improving it, be our guest - AED, GRP]
*/
int i;
int max_d;
int num_new_palette;
png_dsortp t;
t = NULL;
/* Initialize palette index arrays */
/* Initialize the sort array */
for (i = 0; i < num_palette; i++)
{
}
png_sizeof(png_dsortp)));
/* Initial wild guess at how far apart the farthest pixel
* pair we will be eliminating will be. Larger
* numbers mean more areas will be allocated, Smaller
* numbers run the risk of not saving enough data, and
* having to do this all over again.
*
* I have not done extensive checking on this number.
*/
max_d = 96;
while (num_new_palette > maximum_colors)
{
for (i = 0; i < num_new_palette - 1; i++)
{
int j;
for (j = i + 1; j < num_new_palette; j++)
{
int d;
if (d <= max_d)
{
if (t == NULL)
break;
hash[d] = t;
}
}
if (t == NULL)
break;
}
if (t != NULL)
for (i = 0; i <= max_d; i++)
{
{
png_dsortp p;
{
< num_new_palette &&
{
int j, next_j;
if (num_new_palette & 0x01)
{
j = p->left;
}
else
{
j = p->right;
}
if (!full_quantize)
{
int k;
for (k = 0; k < num_palette; k++)
{
if (png_ptr->quantize_index[k] ==
png_ptr->index_to_palette[j])
png_ptr->quantize_index[k] =
if ((int)png_ptr->quantize_index[k] ==
png_ptr->quantize_index[k] =
png_ptr->index_to_palette[j];
}
}
png_ptr->index_to_palette[j] =
(png_byte)j;
}
if (num_new_palette <= maximum_colors)
break;
}
if (num_new_palette <= maximum_colors)
break;
}
}
for (i = 0; i < 769; i++)
{
{
png_dsortp p = hash[i];
while (p)
{
t = p->next;
p = t;
}
}
hash[i] = 0;
}
max_d += 96;
}
}
}
{
}
if (full_quantize)
{
int i;
png_sizeof(png_byte)));
for (i = 0; i < num_palette; i++)
{
{
/* int dr = abs(ir - r); */
{
/* int dg = abs(ig - g); */
{
/* int db = abs(ib - b); */
{
}
}
}
}
}
}
}
#endif /* PNG_READ_QUANTIZE_SUPPORTED */
#ifdef PNG_READ_GAMMA_SUPPORTED
void PNGFAPI
{
return;
/* New in libpng-1.5.4 - reserve particular negative values as flags. */
#if PNG_LIBPNG_VER >= 10600
/* Checking the gamma values for being >0 was added in 1.5.4 along with the
* premultiplied alpha support; this actually hides an undocumented feature
* of the previous implementation which allowed gamma processing to be
* disabled in background handling. There is no evidence (so far) that this
* was being used; however, png_set_background itself accepted and must still
* accept '0' for the gamma value it takes, because it isn't always used.
*
* Since this is an API change (albeit a very minor one that removes an
* undocumented API feature) it will not be made until libpng-1.6.0.
*/
if (file_gamma <= 0)
if (scrn_gamma <= 0)
#endif
/* Set the gamma values unconditionally - this overrides the value in the PNG
* file if a gAMA chunk was present. png_set_alpha_mode provides a
* different, easier, way to default the file gamma.
*/
}
# ifdef PNG_FLOATING_POINT_SUPPORTED
void PNGAPI
{
}
# endif /* FLOATING_POINT_SUPPORTED */
#endif /* READ_GAMMA */
#ifdef PNG_READ_EXPAND_SUPPORTED
/* Expand paletted images to RGB, expand grayscale images of
* less than 8-bit depth to 8-bit depth, and expand tRNS chunks
* to alpha channels.
*/
void PNGAPI
{
return;
}
/* GRR 19990627: the following three functions currently are identical
* to png_set_expand(). However, it is entirely reasonable that someone
* might wish to expand an indexed image to RGB but *not* expand a single,
* fully transparent palette entry to a full alpha channel--perhaps instead
* the transparent color with a particular RGB value, or drop tRNS entirely.
* IOW, a future version of the library may make the transformations flag
* a bit more fine-grained, with separate bits for each of these three
* functions.
*
* More to the point, these functions make it obvious what libpng will be
* doing, whereas "expand" can (and does) mean any number of things.
*
* GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
* to expand only the sample depth but not to expand the tRNS to alpha
* and its name was changed to png_set_expand_gray_1_2_4_to_8().
*/
/* Expand paletted images to RGB. */
void PNGAPI
{
return;
}
/* Expand grayscale images of less than 8-bit depth to 8 bits. */
void PNGAPI
{
return;
}
/* Expand tRNS chunks to alpha channels. */
void PNGAPI
{
}
#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
#ifdef PNG_READ_EXPAND_16_SUPPORTED
/* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise
* it may not work correctly.)
*/
void PNGAPI
{
return;
/* New API, make sure apps call the correct initializers: */
}
#endif
void PNGAPI
{
{
/* Because rgb must be 8 bits or more: */
}
}
#endif
void PNGFAPI
{
return;
switch(error_action)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
#ifdef PNG_READ_EXPAND_SUPPORTED
#else
{
"Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
}
#endif
{
{
}
else
{
"ignoring out of range rgb_to_gray coefficients");
/* Use the defaults, from the cHRM chunk if set, else the built in Rec
* 709 values (which correspond to sRGB, so we don't have to worry
* about the sRGB chunk!)
*/
if (png_ptr->rgb_to_gray_red_coeff == 0 &&
png_ptr->rgb_to_gray_green_coeff == 0 &&
png_ptr->rgb_to_gray_blue_coeff == 0)
{
}
}
}
}
#ifdef PNG_FLOATING_POINT_SUPPORTED
/* Convert a RGB image to a grayscale of the same width. This allows us,
* for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
*/
void PNGAPI
double green)
{
return;
}
#endif /* FLOATING POINT */
#endif
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
void PNGAPI
{
return;
#endif
}
#endif
#ifdef PNG_READ_GAMMA_SUPPORTED
/* In the case of gamma transformations only do transformations on images where
* the [file] gamma and screen_gamma are not close reciprocals, otherwise it
* slows things down slightly, and also needlessly introduces small errors.
*/
static int /* PRIVATE */
{
/* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
* correction as a difference of the overall transform from 1.0
*
* We want to compare the threshold with s*f - 1, if we get
* overflow here it is because of wacky gamma values so we
* turn on processing anyway.
*/
}
#endif
/* Initialize everything needed for the read. This includes modifying
* the palette.
*/
/*For the moment 'png_init_palette_transformations' and
* 'png_init_rgb_transformations' only do some flag canceling optimizations.
* The intent is that these two routines should have palette or rgb operations
* extracted from 'png_init_read_transformations'.
*/
static void /* PRIVATE */
{
/* Called to handle the (input) palette case. In png_do_read_transformations
* the first step is to expand the palette if requested, so this code must
* take care to only make changes that are invariant with respect to the
* palette expansion, or only do them if there is no expansion.
*
* STRIP_ALPHA has already been handled in the caller (by setting num_trans
* to 0.)
*/
int input_has_alpha = 0;
int input_has_transparency = 0;
{
int i;
/* Ignore if all the entries are opaque (unlikely!) */
continue;
else if (png_ptr->trans_alpha[i] == 0)
else
input_has_alpha = 1;
}
/* If no alpha we can optimize. */
if (!input_has_alpha)
{
/* Any alpha means background and associative alpha processing is
* required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA
* and ENCODE_ALPHA are irrelevant.
*/
if (!input_has_transparency)
}
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
/* png_set_background handling - deals with the complexity of whether the
* background color is in the file format or the screen format in the case
* where an 'expand' will happen.
*/
/* The following code cannot be entered in the alpha pre-multiplication case
* because PNG_BACKGROUND_EXPAND is cancelled below.
*/
{
{
{
{
/* Invert the alpha channel (in tRNS) unless the pixels are
* going to be expanded, in which case leave it for later
*/
for (i=0; i<istop; i++)
png_ptr->trans_alpha[i]);
}
}
#endif /* PNG_READ_INVERT_ALPHA_SUPPORTED */
}
} /* background expand and (therefore) no alpha association. */
#endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
}
static void /* PRIVATE */
{
/* Added to libpng-1.5.4: check the color type to determine whether there
* is any alpha or transparency in the image and simply cancel the
* background and alpha mode stuff if there isn't.
*/
/* If no alpha we can optimize. */
if (!input_has_alpha)
{
/* Any alpha means background and associative alpha processing is
* required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA
* and ENCODE_ALPHA are irrelevant.
*/
# ifdef PNG_READ_ALPHA_MODE_SUPPORTED
# endif
if (!input_has_transparency)
}
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
/* png_set_background handling - deals with the complexity of whether the
* background color is in the file format or the screen format in the case
* where an 'expand' will happen.
*/
/* The following code cannot be entered in the alpha pre-multiplication case
* because PNG_BACKGROUND_EXPAND is cancelled below.
*/
/* i.e., GRAY or GRAY_ALPHA */
{
{
/* Expand background and tRNS chunks */
{
case 1:
{
}
break;
case 2:
{
}
break;
case 4:
{
}
break;
default:
case 8:
case 16:
break;
}
}
} /* background expand and (therefore) no alpha association. */
#endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
}
void /* PRIVATE */
{
/* This internal function is called from png_read_start_row in pngrutil.c
* and it is called before the 'rowbytes' calculation is done, so the code
* in here can change or update the transformations flags.
*
* First do updates that do not depend on the details of the PNG image data
* being processed.
*/
#ifdef PNG_READ_GAMMA_SUPPORTED
/* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds
* png_set_alpha_mode and this is another source for a default file gamma so
* the test needs to be performed later - here. In addition prior to 1.5.4
* the tests were repeated for the PALETTE color type here - this is no
* longer necessary (and doesn't seem to have been necessary before.)
*/
{
/* The following temporary indicates if overall gamma correction is
* required.
*/
int gamma_correction = 0;
{
else
/* Assume the output matches the input; a long time default behavior
* of libpng, although the standard has nothing to say about this.
*/
}
else if (png_ptr->screen_gamma != 0)
/* The converse - assume the file matches the screen, note that this
* perhaps undesireable default can (from 1.5.4) be changed by calling
* png_set_alpha_mode (even if the alpha handling mode isn't required
* or isn't changed from the default.)
*/
else /* neither are set */
/* Just in case the following prevents any processing - file and screen
* are both assumed to be linear and there is no way to introduce a
* third gamma value other than png_set_background with 'UNIQUE', and,
* prior to 1.5.4
*/
/* Now turn the gamma transformation on or off as appropriate. Notice
* that PNG_GAMMA just refers to the file->screen correction. Alpha
* composition may independently cause gamma correction because it needs
* linear data (e.g. if the file has a gAMA chunk but the screen gamma
* hasn't been specified.) In any case this flag may get turned off in
* the code immediately below if the transform can be handled outside the
* row loop.
*/
if (gamma_correction)
else
}
#endif
/* Certain transformations have the effect of preventing other
* transformations that happen afterward in png_do_read_transformations,
* resolve the interdependencies here. From the code of
* png_do_read_transformations the order is:
*
* 1) PNG_EXPAND (including PNG_EXPAND_tRNS)
* 2) PNG_STRIP_ALPHA (if no compose)
* 3) PNG_RGB_TO_GRAY
* 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY
* 5) PNG_COMPOSE
* 6) PNG_GAMMA
* 7) PNG_STRIP_ALPHA (if compose)
* 8) PNG_ENCODE_ALPHA
* 9) PNG_SCALE_16_TO_8
* 10) PNG_16_TO_8
* 11) PNG_QUANTIZE (converts to palette)
* 12) PNG_EXPAND_16
* 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY
* 14) PNG_INVERT_MONO
* 15) PNG_SHIFT
* 16) PNG_PACK
* 17) PNG_BGR
* 18) PNG_PACKSWAP
* 19) PNG_FILLER (includes PNG_ADD_ALPHA)
* 20) PNG_INVERT_ALPHA
* 21) PNG_SWAP_ALPHA
* 22) PNG_SWAP_BYTES
* 23) PNG_USER_TRANSFORM [must be last]
*/
{
/* Stripping the alpha channel happens immediately after the 'expand'
* transformations, before all other transformation, so it cancels out
* the alpha handling. It has the side effect negating the effect of
* PNG_EXPAND_tRNS too:
*/
/* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen
* so transparency information would remain just so long as it wasn't
* expanded. This produces unexpected API changes if the set of things
* that do PNG_EXPAND_tRNS changes (perfectly possible given the
* documentation - which says ask for what you want, accept what you
* get.) This makes the behavior consistent from 1.5.4:
*/
}
#endif /* STRIP_ALPHA supported, no COMPOSE */
/* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA
* settings will have no effect.
*/
{
}
#endif
#if defined(PNG_READ_EXPAND_SUPPORTED) && \
defined(PNG_READ_BACKGROUND_SUPPORTED) && \
defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
/* Detect gray background and attempt to enable optimization for
* gray --> RGB case.
*
* Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
* RGB_ALPHA (in which case need_expand is superfluous anyway), the
* background color might actually be gray yet not be flagged as such.
* This is not a problem for the current code, which uses
* PNG_BACKGROUND_IS_GRAY only to decide when to do the
* png_do_gray_to_rgb() transformation.
*
* TODO: this code needs to be revised to avoid the complexity and
* interdependencies. The color type of the background should be recorded in
* png_set_background, along with the bit depth, then the code has a record
* of exactly what color space the background is currently in.
*/
{
/* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if
* the file was greyscale the background value is gray.
*/
}
{
/* PNG_COMPOSE: png_set_background was called with need_expand false,
* so the color is in the color space of the output or png_set_alpha_mode
* was called and the color is black. Ignore RGB_TO_GRAY because that
* happens before GRAY_TO_RGB.
*/
{
{
}
}
}
#endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED (etc) */
/* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations
* can be performed directly on the palette, and some (such as rgb to gray)
* can be optimized inside the palette. This is particularly true of the
* composite (background and alpha) stuff, which can be pretty much all done
* in the palette even if the result is expanded to RGB or gray afterward.
*
* NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and
* earlier and the palette stuff is actually handled on the first row. This
* leads to the reported bug that the palette returned by png_get_PLTE is not
* updated.
*/
else
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
defined(PNG_READ_EXPAND_16_SUPPORTED)
{
/* TODO: fix this. Because the expand_16 operation is after the compose
* handling the background color must be 8, not 16, bits deep, but the
* application will supply a 16-bit value so reduce it here.
*
* The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at
* present, so that case is ok (until do_expand_16 is moved.)
*
* NOTE: this discards the low 16 bits of the user supplied background
* color, but until expand_16 works properly there is no choice!
*/
}
#endif /* PNG_READ_BACKGROUND_SUPPORTED && PNG_READ_EXPAND_16_SUPPORTED */
/* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the
* background support (see the comments in scripts/pnglibconf.dfa), this
* allows pre-multiplication of the alpha channel to be implemented as
* compositing on black. This is probably sub-optimal and has been done in
* 1.5.4 betas simply to enable external critique and testing (i.e. to
* implement the new API quickly, without lots of internal changes.)
*/
#ifdef PNG_READ_GAMMA_SUPPORTED
# ifdef PNG_READ_BACKGROUND_SUPPORTED
/* Includes ALPHA_MODE */
# endif
/* This needs to change - in the palette image case a whole set of tables are
* built when it would be quicker to just calculate the correct value for
* each palette entry directly. Also, the test is too tricky - why check
* PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that
* PNG_GAMMA is cancelled even if the gamma is known? The test excludes the
* PNG_COMPOSE case, so apparently if there is no *overall* gamma correction
* the gamma tables will not be built even if composition is required on a
* gamma encoded value.
*
* In 1.5.4 this is addressed below by an additional check on the individual
* file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the
* tables.
*/
# ifdef PNG_READ_BACKGROUND_SUPPORTED
# endif
)
{
{
{
/* We don't get to here unless there is a tRNS chunk with non-opaque
* entries - see the checking code at the start of this function.
*/
int i;
{
}
else
{
switch (png_ptr->background_gamma_type)
{
g = (png_ptr->screen_gamma);
break;
break;
break;
default:
g = PNG_FP_1; /* back_1 */
break;
}
if (png_gamma_significant(gs))
{
gs);
gs);
gs);
}
else
{
}
if (png_gamma_significant(g))
{
g);
g);
}
else
{
}
}
for (i = 0; i < num_palette; i++)
{
{
if (png_ptr->trans_alpha[i] == 0)
{
}
else /* if (png_ptr->trans_alpha[i] != 0xff) */
{
png_byte v, w;
}
}
else
{
}
}
/* Prevent the transformations being done again.
*
* NOTE: this is highly dubious; it zaps the transformations in
* place. This seems inconsistent with the general treatment of the
* transformations elsewhere.
*/
} /* color_type == PNG_COLOR_TYPE_PALETTE */
/* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
else /* color_type != PNG_COLOR_TYPE_PALETTE */
{
png_fixed_point g = PNG_FP_1;
switch (png_ptr->background_gamma_type)
{
g = png_ptr->screen_gamma;
/* gs = PNG_FP_1; */
break;
break;
break;
default:
}
{
/* RGB or RGBA with color background */
}
else
{
/* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
}
} /* color_type != PNG_COLOR_TYPE_PALETTE */
}/* png_ptr->transformations & PNG_BACKGROUND */
else
/* Transformation does not include PNG_BACKGROUND */
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
{
int i;
/*NOTE: there are other transformations that should probably be in here
* too.
*/
for (i = 0; i < num_palette; i++)
{
}
/* Done the gamma correction. */
} /* color_type == PALETTE && !PNG_BACKGROUND transformation */
}
else
#endif
#endif /* PNG_READ_GAMMA_SUPPORTED */
/* No GAMMA transformation (see the hanging else 4 lines above) */
{
int i;
for (i = 0; i < istop; i++)
{
if (png_ptr->trans_alpha[i] == 0)
{
}
{
/* The png_composite() macro is defined in png.h */
}
}
}
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
#ifdef PNG_READ_SHIFT_SUPPORTED
{
png_uint_16 i;
sr = 0;
sg = 0;
sb = 0;
for (i = 0; i < istop; i++)
{
}
}
#endif /* PNG_READ_SHIFT_SUPPORTED */
}
/* Modify the info structure to reflect the transformations. The
* info should be updated so a PNG file could be written with it,
* assuming the transformations result in valid PNG data.
*/
void /* PRIVATE */
{
#ifdef PNG_READ_EXPAND_SUPPORTED
{
{
/* This check must match what actually happens in
* png_do_expand_palette; if it ever checks the tRNS chunk to see if
* it is all opaque we must do the same (at present it does not.)
*/
else
}
else
{
{
}
}
}
#endif
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
defined(PNG_READ_ALPHA_MODE_SUPPORTED)
/* The following is almost certainly wrong unless the background value is in
* the screen space!
*/
#endif
#ifdef PNG_READ_GAMMA_SUPPORTED
/* The following used to be conditional on PNG_GAMMA (prior to 1.5.4),
* however it seems that the code in png_init_read_transformations, which has
* been called before this from png_read_update_info->png_read_start_row
* sometimes does the gamma transform and cancels the flag.
*/
#endif
{
# ifdef PNG_READ_16BIT_SUPPORTED
# ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
# endif
# ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
# endif
# else
/* No 16 bit support: force chopping 16-bit input down to 8, in this case
* the app program can chose if both APIs are available by setting the
* correct scaling to use.
*/
# ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
/* For compatibility with previous versions use the strip method by
* default. This code works because if PNG_SCALE_16_TO_8 is already
* set the code below will do that in preference to the chop.
*/
# else
# else
# endif
# endif
#endif /* !READ_16BIT_SUPPORTED */
}
#endif
#endif
#ifdef PNG_READ_QUANTIZE_SUPPORTED
{
{
}
}
#endif
#ifdef PNG_READ_EXPAND_16_SUPPORTED
{
}
#endif
#ifdef PNG_READ_PACK_SUPPORTED
#endif
else
{
}
#endif
#ifdef PNG_READ_FILLER_SUPPORTED
/* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
{
/* If adding a true alpha channel not just filler */
}
#endif
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
{
}
#endif
/* Adding in 1.5.4: cache the above value in png_struct so that we can later
* check in png_rowbytes that the user buffer won't get overwritten. Note
* that the field is not always set - if png_read_update_info isn't called
* the application has to either not do any transforms or get the calculation
* right itself.
*/
#ifndef PNG_READ_EXPAND_SUPPORTED
if (png_ptr)
return;
#endif
}
/* Transform the row. The order of transformations is significant,
* and is very touchy. If you add a transformation, take care to
* decide how it fits in with the other transformations here.
*/
void /* PRIVATE */
{
{
* error is incredibly rare and incredibly easy to debug without this
* information.
*/
}
/* The following is debugging; prior to 1.5.4 the code was never compiled in;
* in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
* PNG_WARN_UNINITIALIZED_ROW removed. In 1.5 the new flag is set only for
* selected new APIs to ensure that there is no API change.
*/
{
/* Application has failed to call either png_read_start_image() or
* png_read_update_info() after setting transforms that expand pixels.
* This check added to libpng-1.2.19 (but not enabled until 1.5.4).
*/
}
#ifdef PNG_READ_EXPAND_SUPPORTED
{
{
}
else
{
&(png_ptr->trans_color));
else
NULL);
}
}
#endif
0 /* at_start == false, because SWAP_ALPHA happens later */);
#endif
{
int rgb_error =
if (rgb_error)
{
}
}
#endif
/* From Andreas Dilger e-mail to png-implement, 26 March 1998:
*
* In most cases, the "simple transparency" should be done prior to doing
* gray-to-RGB, or you will have to test 3x as many bytes to check if a
* pixel is transparent. You would also need to make sure that the
* transparency information is upgraded to RGB.
*
* To summarize, the current flow is:
* - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
* with background "in place" if transparent,
* convert to RGB if necessary
* - Gray + alpha -> composite with gray background and remove alpha bytes,
* convert to RGB if necessary
*
* To support RGB backgrounds for gray images we need:
* - Gray + simple transparency -> convert to RGB + simple transparency,
* compare 3 or 6 bytes and composite with
* background "in place" if transparent
* composite with gray bkgrnd)
* - Gray + alpha -> convert to RGB + alpha, composite with background and
* remove alpha bytes (3x float
* operations/pixel compared with composite
* on gray background)
*
* Greg's change will do this. The reason it wasn't done before is for
* performance, as this increases the per-pixel operations. If we would check
* in advance if the background was gray or RGB, and position the gray-to-RGB
*/
/* If gray -> RGB, do so now only if background is non-gray; else do later
* for performance reasons
*/
#endif
#if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\
(defined PNG_READ_ALPHA_MODE_SUPPORTED)
#endif
#ifdef PNG_READ_GAMMA_SUPPORTED
#if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\
(defined PNG_READ_ALPHA_MODE_SUPPORTED)
#endif
#endif
0 /* at_start == false, because SWAP_ALPHA happens later */);
#endif
#endif
#endif
/* There is no harm in doing both of these because only one has any effect,
* by putting the 'scale' option first if the app asks for scale (either by
* calling the API or in a TRANSFORM flag) this is what happens.
*/
#endif
#ifdef PNG_READ_QUANTIZE_SUPPORTED
{
}
#endif /* PNG_READ_QUANTIZE_SUPPORTED */
#ifdef PNG_READ_EXPAND_16_SUPPORTED
/* Do the expansion now, after all the arithmetic has been done. Notice
* that previous transformations can handle the PNG_EXPAND_16 flag if this
* is efficient (particularly true in the case of gamma correction, where
* better accuracy results faster!)
*/
#endif
/*NOTE: moved here in 1.5.4 (from much later in this list.) */
#endif
#ifdef PNG_READ_INVERT_SUPPORTED
#endif
#ifdef PNG_READ_SHIFT_SUPPORTED
#endif
#ifdef PNG_READ_PACK_SUPPORTED
#endif
#ifdef PNG_READ_BGR_SUPPORTED
#endif
#ifdef PNG_READ_PACKSWAP_SUPPORTED
#endif
#ifdef PNG_READ_FILLER_SUPPORTED
#endif
#endif
#endif
#ifdef PNG_READ_16BIT_SUPPORTED
#ifdef PNG_READ_SWAP_SUPPORTED
#endif
#endif
{
(png_ptr, /* png_ptr */
/* png_uint_32 width; width of row */
/* png_size_t rowbytes; number of bytes in row */
/* png_byte color_type; color type of pixels */
/* png_byte bit_depth; bit depth of samples */
/* png_byte channels; number of channels (1-4) */
/* png_byte pixel_depth; bits per pixel (depth*channels) */
if (png_ptr->user_transform_depth)
#endif
}
#endif
}
#ifdef PNG_READ_PACK_SUPPORTED
/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
* without changing the actual values. Thus, if you had a row with
* a bit depth of 1, you would end up with bytes that only contained
* the numbers 0 or 1. If you would rather they contain 0 and 255, use
* png_do_shift() after this.
*/
void /* PRIVATE */
{
{
png_uint_32 i;
{
case 1:
{
for (i = 0; i < row_width; i++)
{
if (shift == 7)
{
shift = 0;
sp--;
}
else
shift++;
dp--;
}
break;
}
case 2:
{
for (i = 0; i < row_width; i++)
{
if (shift == 6)
{
shift = 0;
sp--;
}
else
shift += 2;
dp--;
}
break;
}
case 4:
{
for (i = 0; i < row_width; i++)
{
if (shift == 4)
{
shift = 0;
sp--;
}
else
shift = 4;
dp--;
}
break;
}
default:
break;
}
}
}
#endif
#ifdef PNG_READ_SHIFT_SUPPORTED
/* Reverse the effects of png_do_shift. This routine merely shifts the
* pixels back to their significant bits values. Thus, if you have
* a row of bit depth 8, but only 5 are significant, this will shift
* the values back to 0 through 31.
*/
void /* PRIVATE */
{
if (
{
int channels = 0;
int c;
{
}
else
{
}
{
}
for (c = 0; c < channels; c++)
{
if (shift[c] <= 0)
shift[c] = 0;
else
value = 1;
}
if (!value)
return;
{
default:
break;
case 2:
{
png_size_t i;
{
*bp >>= 1;
*bp++ &= 0x55;
}
break;
}
case 4:
{
png_size_t i;
for (i = 0; i < istop; i++)
{
}
break;
}
case 8:
{
png_uint_32 i;
for (i = 0; i < istop; i++)
{
}
break;
}
#ifdef PNG_READ_16BIT_SUPPORTED
case 16:
{
png_uint_32 i;
for (i = 0; i < istop; i++)
{
}
break;
}
#endif
}
}
}
#endif
/* Scale rows of bit depth 16 down to 8 accurately */
void /* PRIVATE */
{
{
{
/* The input is an array of 16 bit components, these must be scaled to
* 8 bits each. For a 16 bit value V the required value (from the PNG
* specification) is:
*
* (V * 255) / 65535
*
* This reduces to round(V / 257), or floor((V + 128.5)/257)
*
* Represent V as the two byte value vhi.vlo. Make a guess that the
* result is the top byte of V, vhi, then the correction to this value
* is:
*
* error = floor(((V-vhi.vhi) + 128.5) / 257)
* = floor(((vlo-vhi) + 128.5) / 257)
*
* This can be approximated using integer arithmetic (and a signed
* shift):
*
* error = (vlo-vhi+128) >> 8;
*
* The approximate differs from the exact answer only when (vlo-vhi) is
* 128; it then gives a correction of +1 when the exact correction is
* 0. This gives 128 errors. The exact answer (correct for all 16 bit
* input values) is:
*
* error = (vlo-vhi+128)*65535 >> 24;
*
* An alternative arithmetic calculation which also gives no errors is:
*
* (V * 255 + 32895) >> 16
*/
}
}
}
#endif
void /* PRIVATE */
/* Simply discard the low byte. This was the default behavior prior
* to libpng-1.5.4.
*/
{
{
{
}
}
}
#endif
void /* PRIVATE */
{
{
{
/* This converts from RGBA to ARGB */
{
png_uint_32 i;
for (i = 0; i < row_width; i++)
{
}
}
#ifdef PNG_READ_16BIT_SUPPORTED
/* This converts from RRGGBBAA to AARRGGBB */
else
{
png_uint_32 i;
for (i = 0; i < row_width; i++)
{
}
}
#endif
}
{
/* This converts from GA to AG */
{
png_uint_32 i;
for (i = 0; i < row_width; i++)
{
}
}
#ifdef PNG_READ_16BIT_SUPPORTED
/* This converts from GGAA to AAGG */
else
{
png_uint_32 i;
for (i = 0; i < row_width; i++)
{
}
}
#endif
}
}
}
#endif
void /* PRIVATE */
{
{
{
/* This inverts the alpha channel in RGBA */
png_uint_32 i;
for (i = 0; i < row_width; i++)
{
/* This does nothing:
*(--dp) = *(--sp);
*(--dp) = *(--sp);
*(--dp) = *(--sp);
We can replace it with:
*/
sp-=3;
}
}
#ifdef PNG_READ_16BIT_SUPPORTED
/* This inverts the alpha channel in RRGGBBAA */
else
{
png_uint_32 i;
for (i = 0; i < row_width; i++)
{
/* This does nothing:
*(--dp) = *(--sp);
*(--dp) = *(--sp);
*(--dp) = *(--sp);
*(--dp) = *(--sp);
*(--dp) = *(--sp);
*(--dp) = *(--sp);
We can replace it with:
*/
sp-=6;
}
}
#endif
}
{
{
/* This inverts the alpha channel in GA */
png_uint_32 i;
for (i = 0; i < row_width; i++)
{
}
}
#ifdef PNG_READ_16BIT_SUPPORTED
else
{
/* This inverts the alpha channel in GGAA */
png_uint_32 i;
for (i = 0; i < row_width; i++)
{
/*
*(--dp) = *(--sp);
*(--dp) = *(--sp);
*/
sp-=2;
}
}
#endif
}
}
#endif
#ifdef PNG_READ_FILLER_SUPPORTED
/* Add filler channel if we have RGB color */
void /* PRIVATE */
{
png_uint_32 i;
#ifdef PNG_READ_16BIT_SUPPORTED
#endif
if (
{
{
if (flags & PNG_FLAG_FILLER_AFTER)
{
/* This changes the data from G to GX */
for (i = 1; i < row_width; i++)
{
}
}
else
{
/* This changes the data from G to XG */
for (i = 0; i < row_width; i++)
{
}
}
}
#ifdef PNG_READ_16BIT_SUPPORTED
{
if (flags & PNG_FLAG_FILLER_AFTER)
{
/* This changes the data from GG to GGXX */
for (i = 1; i < row_width; i++)
{
}
}
else
{
/* This changes the data from GG to XXGG */
for (i = 0; i < row_width; i++)
{
}
}
}
#endif
} /* COLOR_TYPE == GRAY */
{
{
if (flags & PNG_FLAG_FILLER_AFTER)
{
/* This changes the data from RGB to RGBX */
for (i = 1; i < row_width; i++)
{
}
}
else
{
/* This changes the data from RGB to XRGB */
for (i = 0; i < row_width; i++)
{
}
}
}
#ifdef PNG_READ_16BIT_SUPPORTED
{
if (flags & PNG_FLAG_FILLER_AFTER)
{
/* This changes the data from RRGGBB to RRGGBBXX */
for (i = 1; i < row_width; i++)
{
}
}
else
{
/* This changes the data from RRGGBB to XXRRGGBB */
for (i = 0; i < row_width; i++)
{
}
}
}
#endif
} /* COLOR_TYPE == RGB */
}
#endif
/* Expand grayscale files to RGB, with or without alpha */
void /* PRIVATE */
{
png_uint_32 i;
{
{
{
/* This changes G to RGB */
for (i = 0; i < row_width; i++)
{
}
}
else
{
/* This changes GG to RRGGBB */
for (i = 0; i < row_width; i++)
{
}
}
}
{
{
/* This changes GA to RGBA */
for (i = 0; i < row_width; i++)
{
}
}
else
{
/* This changes GGAA to RRGGBBAA */
for (i = 0; i < row_width; i++)
{
}
}
}
}
}
#endif
/* Reduce RGB files to grayscale, with or without alpha
* using the equation given in Poynton's ColorFAQ at
* <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008)
* New link:
* Charles Poynton poynton at poynton.com
*
* Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
*
* We approximate this with
*
* Y = 0.21268 * R + 0.7151 * G + 0.07217 * B
*
* which can be expressed with integers as
*
* Y = (6969 * R + 23434 * G + 2365 * B)/32768
*
* The calculation is to be done in a linear colorspace.
*
* Other integer coefficents can be used via png_set_rgb_to_gray().
*/
int /* PRIVATE */
{
png_uint_32 i;
int rgb_error = 0;
{
{
{
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
{
for (i = 0; i < row_width; i++)
{
{
rgb_error |= 1;
}
else
}
}
else
#endif
{
for (i = 0; i < row_width; i++)
{
{
rgb_error |= 1;
}
else
}
}
}
else /* RGB bit_depth == 16 */
{
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
{
for (i = 0; i < row_width; i++)
{
w = red;
else
{
rgb_error |= 1;
}
}
}
else
#endif
{
for (i = 0; i < row_width; i++)
{
rgb_error |= 1;
}
}
}
}
{
{
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
{
for (i = 0; i < row_width; i++)
{
rgb_error |= 1;
}
}
else
#endif
{
for (i = 0; i < row_width; i++)
{
rgb_error |= 1;
}
}
}
else /* RGBA bit_depth == 16 */
{
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
{
for (i = 0; i < row_width; i++)
{
w = red;
else
{
rgb_error |= 1;
}
}
}
else
#endif
{
for (i = 0; i < row_width; i++)
{
rgb_error |= 1;
}
}
}
}
}
return rgb_error;
}
#endif
#endif /* PNG_READ_TRANSFORMS_SUPPORTED */
/* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
* large of png_color. This lets grayscale images be treated as
* paletted. Most useful for gamma correction and simplification
* of code. This API is not used internally.
*/
void PNGAPI
{
int num_palette;
int color_inc;
int i;
int v;
return;
switch (bit_depth)
{
case 1:
num_palette = 2;
color_inc = 0xff;
break;
case 2:
num_palette = 4;
color_inc = 0x55;
break;
case 4:
num_palette = 16;
color_inc = 0x11;
break;
case 8:
num_palette = 256;
color_inc = 1;
break;
default:
num_palette = 0;
color_inc = 0;
break;
}
for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
{
}
}
#endif
/* Replace any alpha or transparency with the supplied background color.
* "background" is already in the screen gamma, while "background_1" is
* at a gamma of 1.0. Paletted files have already been taken care of.
*/
void /* PRIVATE */
{
#ifdef PNG_READ_GAMMA_SUPPORTED
#endif
png_uint_32 i;
int shift;
{
switch (row_info->color_type)
{
case PNG_COLOR_TYPE_GRAY:
{
{
case 1:
{
shift = 7;
for (i = 0; i < row_width; i++)
{
{
}
if (!shift)
{
shift = 7;
sp++;
}
else
shift--;
}
break;
}
case 2:
{
#ifdef PNG_READ_GAMMA_SUPPORTED
if (gamma_table != NULL)
{
shift = 6;
for (i = 0; i < row_width; i++)
{
{
}
else
{
(p << 4) | (p << 6)] >> 6) & 0x03);
}
if (!shift)
{
shift = 6;
sp++;
}
else
shift -= 2;
}
}
else
#endif
{
shift = 6;
for (i = 0; i < row_width; i++)
{
{
}
if (!shift)
{
shift = 6;
sp++;
}
else
shift -= 2;
}
}
break;
}
case 4:
{
#ifdef PNG_READ_GAMMA_SUPPORTED
if (gamma_table != NULL)
{
shift = 4;
for (i = 0; i < row_width; i++)
{
{
}
else
{
(p << 4)] >> 4) & 0x0f);
}
if (!shift)
{
shift = 4;
sp++;
}
else
shift -= 4;
}
}
else
#endif
{
shift = 4;
for (i = 0; i < row_width; i++)
{
{
}
if (!shift)
{
shift = 4;
sp++;
}
else
shift -= 4;
}
}
break;
}
case 8:
{
#ifdef PNG_READ_GAMMA_SUPPORTED
if (gamma_table != NULL)
{
{
else
}
}
else
#endif
{
{
}
}
break;
}
case 16:
{
#ifdef PNG_READ_GAMMA_SUPPORTED
{
{
png_uint_16 v;
{
/* Background is already in screen gamma */
}
else
{
}
}
}
else
#endif
{
{
png_uint_16 v;
{
}
}
}
break;
}
default:
break;
}
break;
}
case PNG_COLOR_TYPE_RGB:
{
{
#ifdef PNG_READ_GAMMA_SUPPORTED
if (gamma_table != NULL)
{
{
{
}
else
{
}
}
}
else
#endif
{
{
{
}
}
}
}
else /* if (row_info->bit_depth == 16) */
{
#ifdef PNG_READ_GAMMA_SUPPORTED
{
{
+ *(sp + 3));
+ *(sp + 5));
{
/* Background is already in screen gamma */
}
else
{
}
}
}
else
#endif
{
{
+ *(sp + 3));
+ *(sp + 5));
{
}
}
}
}
break;
}
{
{
#ifdef PNG_READ_GAMMA_SUPPORTED
gamma_table != NULL)
{
{
if (a == 0xff)
else if (a == 0)
{
/* Background is already in screen gamma */
}
else
{
png_byte v, w;
v = gamma_to_1[*sp];
if (!optimize)
w = gamma_from_1[w];
*sp = w;
}
}
}
else
#endif
{
{
if (a == 0)
else if (a < 0xff)
}
}
}
else /* if (png_ptr->bit_depth == 16) */
{
#ifdef PNG_READ_GAMMA_SUPPORTED
gamma_16_to_1 != NULL)
{
{
+ *(sp + 3));
if (a == (png_uint_16)0xffff)
{
png_uint_16 v;
}
else if (a == 0)
{
/* Background is already in screen gamma */
}
else
{
png_uint_16 g, v, w;
if (optimize)
w = v;
else
}
}
}
else
#endif
{
{
+ *(sp + 3));
if (a == 0)
{
}
else if (a < 0xffff)
{
png_uint_16 g, v;
}
}
}
}
break;
}
case PNG_COLOR_TYPE_RGB_ALPHA:
{
{
#ifdef PNG_READ_GAMMA_SUPPORTED
gamma_table != NULL)
{
{
if (a == 0xff)
{
}
else if (a == 0)
{
/* Background is already in screen gamma */
}
else
{
png_byte v, w;
v = gamma_to_1[*sp];
if (!optimize) w = gamma_from_1[w];
*sp = w;
if (!optimize) w = gamma_from_1[w];
*(sp + 1) = w;
if (!optimize) w = gamma_from_1[w];
*(sp + 2) = w;
}
}
}
else
#endif
{
{
if (a == 0)
{
}
else if (a < 0xff)
{
}
}
}
}
else /* if (row_info->bit_depth == 16) */
{
#ifdef PNG_READ_GAMMA_SUPPORTED
gamma_16_to_1 != NULL)
{
{
if (a == (png_uint_16)0xffff)
{
png_uint_16 v;
}
else if (a == 0)
{
/* Background is already in screen gamma */
}
else
{
png_uint_16 v, w;
if (!optimize)
if (!optimize)
if (!optimize)
}
}
}
else
#endif
{
{
if (a == 0)
{
}
else if (a < 0xffff)
{
png_uint_16 v;
+ *(sp + 3));
+ *(sp + 5));
}
}
}
}
break;
}
default:
break;
}
}
}
#endif
#ifdef PNG_READ_GAMMA_SUPPORTED
/* Gamma correct the image, avoiding the alpha channel. Make sure
* you do this after you deal with the transparency issue on grayscale
* or RGB images. If your bit depth is 8, use gamma_table, if it
* is 16, use gamma_16_table and gamma_shift. Build these with
* build_gamma_table().
*/
void /* PRIVATE */
{
png_uint_32 i;
{
switch (row_info->color_type)
{
case PNG_COLOR_TYPE_RGB:
{
{
for (i = 0; i < row_width; i++)
{
sp++;
sp++;
sp++;
}
}
else /* if (row_info->bit_depth == 16) */
{
for (i = 0; i < row_width; i++)
{
png_uint_16 v;
sp += 2;
sp += 2;
sp += 2;
}
}
break;
}
case PNG_COLOR_TYPE_RGB_ALPHA:
{
{
for (i = 0; i < row_width; i++)
{
sp++;
sp++;
sp++;
sp++;
}
}
else /* if (row_info->bit_depth == 16) */
{
for (i = 0; i < row_width; i++)
{
sp += 2;
sp += 2;
sp += 4;
}
}
break;
}
{
{
for (i = 0; i < row_width; i++)
{
sp += 2;
}
}
else /* if (row_info->bit_depth == 16) */
{
for (i = 0; i < row_width; i++)
{
sp += 4;
}
}
break;
}
case PNG_COLOR_TYPE_GRAY:
{
{
for (i = 0; i < row_width; i += 4)
{
int a = *sp & 0xc0;
int b = *sp & 0x30;
int c = *sp & 0x0c;
int d = *sp & 0x03;
sp++;
}
}
{
for (i = 0; i < row_width; i += 2)
{
sp++;
}
}
{
for (i = 0; i < row_width; i++)
{
sp++;
}
}
{
for (i = 0; i < row_width; i++)
{
sp += 2;
}
}
break;
}
default:
break;
}
}
}
#endif
/* Encode the alpha channel to the output gamma (the input channel is always
* linear.) Called only with color types that have an alpha channel. Needs the
* from_1 tables.
*/
void /* PRIVATE */
{
{
{
{
/* The alpha channel is the last component: */
return;
}
}
{
{
/* The alpha channel is the last component: */
{
png_uint_16 v;
}
return;
}
}
}
/* Only get to here if called with a weird row_info; no harm has been done,
* so just issue a warning.
*/
}
#endif
#ifdef PNG_READ_EXPAND_SUPPORTED
/* Expands a palette row to an RGB or RGBA row depending
* upon whether you supply trans and num_trans.
*/
void /* PRIVATE */
{
png_uint_32 i;
{
{
{
case 1:
{
for (i = 0; i < row_width; i++)
{
*dp = 1;
else
*dp = 0;
if (shift == 7)
{
shift = 0;
sp--;
}
else
shift++;
dp--;
}
break;
}
case 2:
{
for (i = 0; i < row_width; i++)
{
if (shift == 6)
{
shift = 0;
sp--;
}
else
shift += 2;
dp--;
}
break;
}
case 4:
{
for (i = 0; i < row_width; i++)
{
if (shift == 4)
{
shift = 0;
sp--;
}
else
shift += 4;
dp--;
}
break;
}
default:
break;
}
}
{
{
if (num_trans > 0)
{
for (i = 0; i < row_width; i++)
{
*dp-- = 0xff;
else
sp--;
}
}
else
{
for (i = 0; i < row_width; i++)
{
sp--;
}
}
}
}
}
}
/* If the bit depth < 8, it is expanded to 8. Also, if the already
* expanded transparency value is supplied, an alpha channel is built.
*/
void /* PRIVATE */
{
png_uint_32 i;
{
{
{
{
case 1:
{
for (i = 0; i < row_width; i++)
{
*dp = 0xff;
else
*dp = 0;
if (shift == 7)
{
shift = 0;
sp--;
}
else
shift++;
dp--;
}
break;
}
case 2:
{
for (i = 0; i < row_width; i++)
{
(value << 6));
if (shift == 6)
{
shift = 0;
sp--;
}
else
shift += 2;
dp--;
}
break;
}
case 4:
{
for (i = 0; i < row_width; i++)
{
if (shift == 4)
{
shift = 0;
sp--;
}
else
shift = 4;
dp--;
}
break;
}
default:
break;
}
}
if (trans_color != NULL)
{
{
for (i = 0; i < row_width; i++)
{
*dp-- = 0;
else
*dp-- = 0xff;
}
}
{
for (i = 0; i < row_width; i++)
{
{
*dp-- = 0;
*dp-- = 0;
}
else
{
*dp-- = 0xff;
*dp-- = 0xff;
}
}
}
}
}
{
{
for (i = 0; i < row_width; i++)
{
*dp-- = 0;
else
*dp-- = 0xff;
}
}
{
for (i = 0; i < row_width; i++)
{
{
*dp-- = 0;
*dp-- = 0;
}
else
{
*dp-- = 0xff;
*dp-- = 0xff;
}
}
}
}
}
}
#endif
#ifdef PNG_READ_EXPAND_16_SUPPORTED
/* If the bit depth is 8 and the colour type is not a palette type expand the
* whole row to 16 bits. Has no effect otherwise.
*/
void /* PRIVATE */
{
{
/* The row have a sequence of bytes containing [0..255] and we need
* to turn it into another row containing [0..65535], to do this we
* calculate:
*
* (input / 255) * 65535
*
* Which happens to be exactly input * 257 and this can be achieved
* simply by byte replication in place (copying backwards).
*/
}
}
#endif
#ifdef PNG_READ_QUANTIZE_SUPPORTED
void /* PRIVATE */
{
png_uint_32 i;
{
{
int r, g, b, p;
for (i = 0; i < row_width; i++)
{
r = *sp++;
g = *sp++;
b = *sp++;
/* This looks real messy, but the compiler will reduce
* it down to a reasonable formula. For example, with
* 5 bits per color, we get:
* p = (((r >> 3) & 0x1f) << 10) |
* (((g >> 3) & 0x1f) << 5) |
* ((b >> 3) & 0x1f);
*/
p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
(((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
*dp++ = palette_lookup[p];
}
}
palette_lookup != NULL)
{
int r, g, b, p;
for (i = 0; i < row_width; i++)
{
r = *sp++;
g = *sp++;
b = *sp++;
sp++;
p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
(((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
*dp++ = palette_lookup[p];
}
}
{
{
}
}
}
}
#endif /* PNG_READ_QUANTIZE_SUPPORTED */
#endif /* PNG_READ_TRANSFORMS_SUPPORTED */
#ifdef PNG_MNG_FEATURES_SUPPORTED
/* Undoes intrapixel differencing */
void /* PRIVATE */
{
if (
{
int bytes_per_pixel;
{
png_uint_32 i;
bytes_per_pixel = 3;
bytes_per_pixel = 4;
else
return;
{
}
}
{
png_uint_32 i;
bytes_per_pixel = 6;
bytes_per_pixel = 8;
else
return;
{
}
}
}
}
#endif /* PNG_MNG_FEATURES_SUPPORTED */
#endif /* PNG_READ_SUPPORTED */