Blame pngrtran.c

Packit 0ba690
Packit 0ba690
/* pngrtran.c - transforms the data in a row for PNG readers
Packit 0ba690
 *
Packit 0ba690
 * Last changed in libpng 1.2.53 [February 26, 2015]
Packit 0ba690
 * Copyright (c) 1998-2002,2004,2006-2015 Glenn Randers-Pehrson
Packit 0ba690
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
Packit 0ba690
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Packit 0ba690
 *
Packit 0ba690
 * This code is released under the libpng license.
Packit 0ba690
 * For conditions of distribution and use, see the disclaimer
Packit 0ba690
 * and license in png.h
Packit 0ba690
 *
Packit 0ba690
 * This file contains functions optionally called by an application
Packit 0ba690
 * in order to tell libpng how to handle data when reading a PNG.
Packit 0ba690
 * Transformations that are used in both reading and writing are
Packit 0ba690
 * in pngtrans.c.
Packit 0ba690
 */
Packit 0ba690
Packit 0ba690
#define PNG_INTERNAL
Packit 0ba690
#define PNG_NO_PEDANTIC_WARNINGS
Packit 0ba690
#include "png.h"
Packit 0ba690
#ifdef PNG_READ_SUPPORTED
Packit 0ba690
Packit 0ba690
/* Set the action on getting a CRC error for an ancillary or critical chunk. */
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_crc_action");
Packit 0ba690
 
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
Packit 0ba690
   /* Tell libpng how we react to CRC errors in critical chunks */
Packit 0ba690
   switch (crit_action)
Packit 0ba690
   {
Packit 0ba690
      case PNG_CRC_NO_CHANGE:                        /* Leave setting as is */
Packit 0ba690
         break;
Packit 0ba690
Packit 0ba690
      case PNG_CRC_WARN_USE:                               /* Warn/use data */
Packit 0ba690
         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
Packit 0ba690
         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
Packit 0ba690
         break;
Packit 0ba690
Packit 0ba690
      case PNG_CRC_QUIET_USE:                             /* Quiet/use data */
Packit 0ba690
         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
Packit 0ba690
         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
Packit 0ba690
                           PNG_FLAG_CRC_CRITICAL_IGNORE;
Packit 0ba690
         break;
Packit 0ba690
Packit 0ba690
      case PNG_CRC_WARN_DISCARD:    /* Not a valid action for critical data */
Packit 0ba690
         png_warning(png_ptr,
Packit 0ba690
            "Can't discard critical data on CRC error.");
Packit 0ba690
      case PNG_CRC_ERROR_QUIT:                                /* Error/quit */
Packit 0ba690
Packit 0ba690
      case PNG_CRC_DEFAULT:
Packit 0ba690
      default:
Packit 0ba690
         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
Packit 0ba690
         break;
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* Tell libpng how we react to CRC errors in ancillary chunks */
Packit 0ba690
   switch (ancil_action)
Packit 0ba690
   {
Packit 0ba690
      case PNG_CRC_NO_CHANGE:                       /* Leave setting as is */
Packit 0ba690
         break;
Packit 0ba690
Packit 0ba690
      case PNG_CRC_WARN_USE:                              /* Warn/use data */
Packit 0ba690
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
Packit 0ba690
         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
Packit 0ba690
         break;
Packit 0ba690
Packit 0ba690
      case PNG_CRC_QUIET_USE:                            /* Quiet/use data */
Packit 0ba690
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
Packit 0ba690
         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
Packit 0ba690
                           PNG_FLAG_CRC_ANCILLARY_NOWARN;
Packit 0ba690
         break;
Packit 0ba690
Packit 0ba690
      case PNG_CRC_ERROR_QUIT:                               /* Error/quit */
Packit 0ba690
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
Packit 0ba690
         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
Packit 0ba690
         break;
Packit 0ba690
Packit 0ba690
      case PNG_CRC_WARN_DISCARD:                      /* Warn/discard data */
Packit 0ba690
Packit 0ba690
      case PNG_CRC_DEFAULT:
Packit 0ba690
      default:
Packit 0ba690
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
Packit 0ba690
         break;
Packit 0ba690
   }
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
Packit 0ba690
    defined(PNG_FLOATING_POINT_SUPPORTED)
Packit 0ba690
/* Handle alpha and tRNS via a background color */
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_background(png_structp png_ptr,
Packit 0ba690
   png_color_16p background_color, int background_gamma_code,
Packit 0ba690
   int need_expand, double background_gamma)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_background");
Packit 0ba690
 
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
   if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
Packit 0ba690
   {
Packit 0ba690
      png_warning(png_ptr, "Application must supply a known background gamma");
Packit 0ba690
      return;
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   png_ptr->transformations |= PNG_BACKGROUND;
Packit 0ba690
   png_memcpy(&(png_ptr->background), background_color,
Packit 0ba690
      png_sizeof(png_color_16));
Packit 0ba690
   png_ptr->background_gamma = (float)background_gamma;
Packit 0ba690
   png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
Packit 0ba690
   png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_16_TO_8_SUPPORTED
Packit 0ba690
/* Strip 16 bit depth files to 8 bit depth */
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_strip_16(png_structp png_ptr)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_strip_16");
Packit 0ba690
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
   png_ptr->transformations |= PNG_16_TO_8;
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_strip_alpha(png_structp png_ptr)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_strip_alpha");
Packit 0ba690
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
   png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_DITHER_SUPPORTED
Packit 0ba690
/* Dither file to 8 bit.  Supply a palette, the current number
Packit 0ba690
 * of elements in the palette, the maximum number of elements
Packit 0ba690
 * allowed, and a histogram if possible.  If the current number
Packit 0ba690
 * of colors is greater then the maximum number, the palette will be
Packit 0ba690
 * modified to fit in the maximum number.  "full_dither" indicates
Packit 0ba690
 * whether we need a dithering cube set up for RGB images, or if we
Packit 0ba690
 * simply are reducing the number of colors in a paletted image.
Packit 0ba690
 */
Packit 0ba690
Packit 0ba690
typedef struct png_dsort_struct
Packit 0ba690
{
Packit 0ba690
   struct png_dsort_struct FAR * next;
Packit 0ba690
   png_byte left;
Packit 0ba690
   png_byte right;
Packit 0ba690
} png_dsort;
Packit 0ba690
typedef png_dsort FAR *       png_dsortp;
Packit 0ba690
typedef png_dsort FAR * FAR * png_dsortpp;
Packit 0ba690
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_dither(png_structp png_ptr, png_colorp palette,
Packit 0ba690
   int num_palette, int maximum_colors, png_uint_16p histogram,
Packit 0ba690
   int full_dither)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_dither");
Packit 0ba690
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
   png_ptr->transformations |= PNG_DITHER;
Packit 0ba690
Packit 0ba690
   if (!full_dither)
Packit 0ba690
   {
Packit 0ba690
      int i;
Packit 0ba690
Packit 0ba690
      png_ptr->dither_index = (png_bytep)png_malloc(png_ptr,
Packit 0ba690
         (png_uint_32)(num_palette * png_sizeof(png_byte)));
Packit 0ba690
      for (i = 0; i < num_palette; i++)
Packit 0ba690
         png_ptr->dither_index[i] = (png_byte)i;
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   if (num_palette > maximum_colors)
Packit 0ba690
   {
Packit 0ba690
      if (histogram != NULL)
Packit 0ba690
      {
Packit 0ba690
         /* This is easy enough, just throw out the least used colors.
Packit 0ba690
          * Perhaps not the best solution, but good enough.
Packit 0ba690
          */
Packit 0ba690
Packit 0ba690
         int i;
Packit 0ba690
Packit 0ba690
         /* Initialize an array to sort colors */
Packit 0ba690
         png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr,
Packit 0ba690
            (png_uint_32)(num_palette * png_sizeof(png_byte)));
Packit 0ba690
Packit 0ba690
         /* Initialize the dither_sort array */
Packit 0ba690
         for (i = 0; i < num_palette; i++)
Packit 0ba690
            png_ptr->dither_sort[i] = (png_byte)i;
Packit 0ba690
Packit 0ba690
         /* Find the least used palette entries by starting a
Packit 0ba690
          * bubble sort, and running it until we have sorted
Packit 0ba690
          * out enough colors.  Note that we don't care about
Packit 0ba690
          * sorting all the colors, just finding which are
Packit 0ba690
          * least used.
Packit 0ba690
          */
Packit 0ba690
Packit 0ba690
         for (i = num_palette - 1; i >= maximum_colors; i--)
Packit 0ba690
         {
Packit 0ba690
            int done; /* To stop early if the list is pre-sorted */
Packit 0ba690
            int j;
Packit 0ba690
Packit 0ba690
            done = 1;
Packit 0ba690
            for (j = 0; j < i; j++)
Packit 0ba690
            {
Packit 0ba690
               if (histogram[png_ptr->dither_sort[j]]
Packit 0ba690
                   < histogram[png_ptr->dither_sort[j + 1]])
Packit 0ba690
               {
Packit 0ba690
                  png_byte t;
Packit 0ba690
Packit 0ba690
                  t = png_ptr->dither_sort[j];
Packit 0ba690
                  png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1];
Packit 0ba690
                  png_ptr->dither_sort[j + 1] = t;
Packit 0ba690
                  done = 0;
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
            if (done)
Packit 0ba690
               break;
Packit 0ba690
         }
Packit 0ba690
Packit 0ba690
         /* Swap the palette around, and set up a table, if necessary */
Packit 0ba690
         if (full_dither)
Packit 0ba690
         {
Packit 0ba690
            int j = num_palette;
Packit 0ba690
Packit 0ba690
            /* Put all the useful colors within the max, but don't
Packit 0ba690
             * move the others.
Packit 0ba690
             */
Packit 0ba690
            for (i = 0; i < maximum_colors; i++)
Packit 0ba690
            {
Packit 0ba690
               if ((int)png_ptr->dither_sort[i] >= maximum_colors)
Packit 0ba690
               {
Packit 0ba690
                  do
Packit 0ba690
                     j--;
Packit 0ba690
                  while ((int)png_ptr->dither_sort[j] >= maximum_colors);
Packit 0ba690
                  palette[i] = palette[j];
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
         else
Packit 0ba690
         {
Packit 0ba690
            int j = num_palette;
Packit 0ba690
Packit 0ba690
            /* Move all the used colors inside the max limit, and
Packit 0ba690
             * develop a translation table.
Packit 0ba690
             */
Packit 0ba690
            for (i = 0; i < maximum_colors; i++)
Packit 0ba690
            {
Packit 0ba690
               /* Only move the colors we need to */
Packit 0ba690
               if ((int)png_ptr->dither_sort[i] >= maximum_colors)
Packit 0ba690
               {
Packit 0ba690
                  png_color tmp_color;
Packit 0ba690
Packit 0ba690
                  do
Packit 0ba690
                     j--;
Packit 0ba690
                  while ((int)png_ptr->dither_sort[j] >= maximum_colors);
Packit 0ba690
Packit 0ba690
                  tmp_color = palette[j];
Packit 0ba690
                  palette[j] = palette[i];
Packit 0ba690
                  palette[i] = tmp_color;
Packit 0ba690
                  /* Indicate where the color went */
Packit 0ba690
                  png_ptr->dither_index[j] = (png_byte)i;
Packit 0ba690
                  png_ptr->dither_index[i] = (png_byte)j;
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
Packit 0ba690
            /* Find closest color for those colors we are not using */
Packit 0ba690
            for (i = 0; i < num_palette; i++)
Packit 0ba690
            {
Packit 0ba690
               if ((int)png_ptr->dither_index[i] >= maximum_colors)
Packit 0ba690
               {
Packit 0ba690
                  int min_d, k, min_k, d_index;
Packit 0ba690
Packit 0ba690
                  /* Find the closest color to one we threw out */
Packit 0ba690
                  d_index = png_ptr->dither_index[i];
Packit 0ba690
                  min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
Packit 0ba690
                  for (k = 1, min_k = 0; k < maximum_colors; k++)
Packit 0ba690
                  {
Packit 0ba690
                     int d;
Packit 0ba690
Packit 0ba690
                     d = PNG_COLOR_DIST(palette[d_index], palette[k]);
Packit 0ba690
Packit 0ba690
                     if (d < min_d)
Packit 0ba690
                     {
Packit 0ba690
                        min_d = d;
Packit 0ba690
                        min_k = k;
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
                  /* Point to closest color */
Packit 0ba690
                  png_ptr->dither_index[i] = (png_byte)min_k;
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
         png_free(png_ptr, png_ptr->dither_sort);
Packit 0ba690
         png_ptr->dither_sort = NULL;
Packit 0ba690
      }
Packit 0ba690
      else
Packit 0ba690
      {
Packit 0ba690
         /* This is much harder to do simply (and quickly).  Perhaps
Packit 0ba690
          * we need to go through a median cut routine, but those
Packit 0ba690
          * don't always behave themselves with only a few colors
Packit 0ba690
          * as input.  So we will just find the closest two colors,
Packit 0ba690
          * and throw out one of them (chosen somewhat randomly).
Packit 0ba690
          * [We don't understand this at all, so if someone wants to
Packit 0ba690
          *  work on improving it, be our guest - AED, GRP]
Packit 0ba690
          */
Packit 0ba690
         int i;
Packit 0ba690
         int max_d;
Packit 0ba690
         int num_new_palette;
Packit 0ba690
         png_dsortp t;
Packit 0ba690
         png_dsortpp hash;
Packit 0ba690
Packit 0ba690
         t = NULL;
Packit 0ba690
Packit 0ba690
         /* Initialize palette index arrays */
Packit 0ba690
         png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
Packit 0ba690
            (png_uint_32)(num_palette * png_sizeof(png_byte)));
Packit 0ba690
         png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
Packit 0ba690
            (png_uint_32)(num_palette * png_sizeof(png_byte)));
Packit 0ba690
Packit 0ba690
         /* Initialize the sort array */
Packit 0ba690
         for (i = 0; i < num_palette; i++)
Packit 0ba690
         {
Packit 0ba690
            png_ptr->index_to_palette[i] = (png_byte)i;
Packit 0ba690
            png_ptr->palette_to_index[i] = (png_byte)i;
Packit 0ba690
         }
Packit 0ba690
Packit 0ba690
         hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
Packit 0ba690
            png_sizeof(png_dsortp)));
Packit 0ba690
Packit 0ba690
         num_new_palette = num_palette;
Packit 0ba690
Packit 0ba690
         /* Initial wild guess at how far apart the farthest pixel
Packit 0ba690
          * pair we will be eliminating will be.  Larger
Packit 0ba690
          * numbers mean more areas will be allocated, Smaller
Packit 0ba690
          * numbers run the risk of not saving enough data, and
Packit 0ba690
          * having to do this all over again.
Packit 0ba690
          *
Packit 0ba690
          * I have not done extensive checking on this number.
Packit 0ba690
          */
Packit 0ba690
         max_d = 96;
Packit 0ba690
Packit 0ba690
         while (num_new_palette > maximum_colors)
Packit 0ba690
         {
Packit 0ba690
            for (i = 0; i < num_new_palette - 1; i++)
Packit 0ba690
            {
Packit 0ba690
               int j;
Packit 0ba690
Packit 0ba690
               for (j = i + 1; j < num_new_palette; j++)
Packit 0ba690
               {
Packit 0ba690
                  int d;
Packit 0ba690
Packit 0ba690
                  d = PNG_COLOR_DIST(palette[i], palette[j]);
Packit 0ba690
Packit 0ba690
                  if (d <= max_d)
Packit 0ba690
                  {
Packit 0ba690
Packit 0ba690
                     t = (png_dsortp)png_malloc_warn(png_ptr,
Packit 0ba690
                         (png_uint_32)(png_sizeof(png_dsort)));
Packit 0ba690
                     if (t == NULL)
Packit 0ba690
                         break;
Packit 0ba690
                     t->next = hash[d];
Packit 0ba690
                     t->left = (png_byte)i;
Packit 0ba690
                     t->right = (png_byte)j;
Packit 0ba690
                     hash[d] = t;
Packit 0ba690
                  }
Packit 0ba690
               }
Packit 0ba690
               if (t == NULL)
Packit 0ba690
                  break;
Packit 0ba690
            }
Packit 0ba690
Packit 0ba690
            if (t != NULL)
Packit 0ba690
            for (i = 0; i <= max_d; i++)
Packit 0ba690
            {
Packit 0ba690
               if (hash[i] != NULL)
Packit 0ba690
               {
Packit 0ba690
                  png_dsortp p;
Packit 0ba690
Packit 0ba690
                  for (p = hash[i]; p; p = p->next)
Packit 0ba690
                  {
Packit 0ba690
                     if ((int)png_ptr->index_to_palette[p->left]
Packit 0ba690
                        < num_new_palette &&
Packit 0ba690
                        (int)png_ptr->index_to_palette[p->right]
Packit 0ba690
                        < num_new_palette)
Packit 0ba690
                     {
Packit 0ba690
                        int j, next_j;
Packit 0ba690
Packit 0ba690
                        if (num_new_palette & 0x01)
Packit 0ba690
                        {
Packit 0ba690
                           j = p->left;
Packit 0ba690
                           next_j = p->right;
Packit 0ba690
                        }
Packit 0ba690
                        else
Packit 0ba690
                        {
Packit 0ba690
                           j = p->right;
Packit 0ba690
                           next_j = p->left;
Packit 0ba690
                        }
Packit 0ba690
Packit 0ba690
                        num_new_palette--;
Packit 0ba690
                        palette[png_ptr->index_to_palette[j]]
Packit 0ba690
                          = palette[num_new_palette];
Packit 0ba690
                        if (!full_dither)
Packit 0ba690
                        {
Packit 0ba690
                           int k;
Packit 0ba690
Packit 0ba690
                           for (k = 0; k < num_palette; k++)
Packit 0ba690
                           {
Packit 0ba690
                              if (png_ptr->dither_index[k] ==
Packit 0ba690
                                 png_ptr->index_to_palette[j])
Packit 0ba690
                                 png_ptr->dither_index[k] =
Packit 0ba690
                                    png_ptr->index_to_palette[next_j];
Packit 0ba690
                              if ((int)png_ptr->dither_index[k] ==
Packit 0ba690
                                 num_new_palette)
Packit 0ba690
                                 png_ptr->dither_index[k] =
Packit 0ba690
                                    png_ptr->index_to_palette[j];
Packit 0ba690
                           }
Packit 0ba690
                        }
Packit 0ba690
Packit 0ba690
                        png_ptr->index_to_palette[png_ptr->palette_to_index
Packit 0ba690
                           [num_new_palette]] = png_ptr->index_to_palette[j];
Packit 0ba690
                        png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
Packit 0ba690
                           = png_ptr->palette_to_index[num_new_palette];
Packit 0ba690
Packit 0ba690
                        png_ptr->index_to_palette[j] =
Packit 0ba690
                            (png_byte)num_new_palette;
Packit 0ba690
                        png_ptr->palette_to_index[num_new_palette] =
Packit 0ba690
                            (png_byte)j;
Packit 0ba690
                     }
Packit 0ba690
                     if (num_new_palette <= maximum_colors)
Packit 0ba690
                        break;
Packit 0ba690
                  }
Packit 0ba690
                  if (num_new_palette <= maximum_colors)
Packit 0ba690
                     break;
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
Packit 0ba690
            for (i = 0; i < 769; i++)
Packit 0ba690
            {
Packit 0ba690
               if (hash[i] != NULL)
Packit 0ba690
               {
Packit 0ba690
                  png_dsortp p = hash[i];
Packit 0ba690
                  while (p)
Packit 0ba690
                  {
Packit 0ba690
                     t = p->next;
Packit 0ba690
                     png_free(png_ptr, p);
Packit 0ba690
                     p = t;
Packit 0ba690
                  }
Packit 0ba690
               }
Packit 0ba690
               hash[i] = 0;
Packit 0ba690
            }
Packit 0ba690
            max_d += 96;
Packit 0ba690
         }
Packit 0ba690
         png_free(png_ptr, hash);
Packit 0ba690
         png_free(png_ptr, png_ptr->palette_to_index);
Packit 0ba690
         png_free(png_ptr, png_ptr->index_to_palette);
Packit 0ba690
         png_ptr->palette_to_index = NULL;
Packit 0ba690
         png_ptr->index_to_palette = NULL;
Packit 0ba690
      }
Packit 0ba690
      num_palette = maximum_colors;
Packit 0ba690
   }
Packit 0ba690
   if (png_ptr->palette == NULL)
Packit 0ba690
   {
Packit 0ba690
      png_ptr->palette = palette;
Packit 0ba690
   }
Packit 0ba690
   png_ptr->num_palette = (png_uint_16)num_palette;
Packit 0ba690
Packit 0ba690
   if (full_dither)
Packit 0ba690
   {
Packit 0ba690
      int i;
Packit 0ba690
      png_bytep distance;
Packit 0ba690
      int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS +
Packit 0ba690
         PNG_DITHER_BLUE_BITS;
Packit 0ba690
      int num_red = (1 << PNG_DITHER_RED_BITS);
Packit 0ba690
      int num_green = (1 << PNG_DITHER_GREEN_BITS);
Packit 0ba690
      int num_blue = (1 << PNG_DITHER_BLUE_BITS);
Packit 0ba690
      png_size_t num_entries = ((png_size_t)1 << total_bits);
Packit 0ba690
Packit 0ba690
      png_ptr->palette_lookup = (png_bytep )png_calloc(png_ptr,
Packit 0ba690
         (png_uint_32)(num_entries * png_sizeof(png_byte)));
Packit 0ba690
Packit 0ba690
      distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
Packit 0ba690
         png_sizeof(png_byte)));
Packit 0ba690
      png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
Packit 0ba690
Packit 0ba690
      for (i = 0; i < num_palette; i++)
Packit 0ba690
      {
Packit 0ba690
         int ir, ig, ib;
Packit 0ba690
         int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS));
Packit 0ba690
         int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS));
Packit 0ba690
         int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS));
Packit 0ba690
Packit 0ba690
         for (ir = 0; ir < num_red; ir++)
Packit 0ba690
         {
Packit 0ba690
            /* int dr = abs(ir - r); */
Packit 0ba690
            int dr = ((ir > r) ? ir - r : r - ir);
Packit 0ba690
            int index_r = (ir << (PNG_DITHER_BLUE_BITS +
Packit 0ba690
                PNG_DITHER_GREEN_BITS));
Packit 0ba690
Packit 0ba690
            for (ig = 0; ig < num_green; ig++)
Packit 0ba690
            {
Packit 0ba690
               /* int dg = abs(ig - g); */
Packit 0ba690
               int dg = ((ig > g) ? ig - g : g - ig);
Packit 0ba690
               int dt = dr + dg;
Packit 0ba690
               int dm = ((dr > dg) ? dr : dg);
Packit 0ba690
               int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS);
Packit 0ba690
Packit 0ba690
               for (ib = 0; ib < num_blue; ib++)
Packit 0ba690
               {
Packit 0ba690
                  int d_index = index_g | ib;
Packit 0ba690
                  /* int db = abs(ib - b); */
Packit 0ba690
                  int db = ((ib > b) ? ib - b : b - ib);
Packit 0ba690
                  int dmax = ((dm > db) ? dm : db);
Packit 0ba690
                  int d = dmax + dt + db;
Packit 0ba690
Packit 0ba690
                  if (d < (int)distance[d_index])
Packit 0ba690
                  {
Packit 0ba690
                     distance[d_index] = (png_byte)d;
Packit 0ba690
                     png_ptr->palette_lookup[d_index] = (png_byte)i;
Packit 0ba690
                  }
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
Packit 0ba690
      png_free(png_ptr, distance);
Packit 0ba690
   }
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
Packit 0ba690
/* Transform the image from the file_gamma to the screen_gamma.  We
Packit 0ba690
 * only do transformations on images where the file_gamma and screen_gamma
Packit 0ba690
 * are not close reciprocals, otherwise it slows things down slightly, and
Packit 0ba690
 * also needlessly introduces small errors.
Packit 0ba690
 *
Packit 0ba690
 * We will turn off gamma transformation later if no semitransparent entries
Packit 0ba690
 * are present in the tRNS array for palette images.  We can't do it here
Packit 0ba690
 * because we don't necessarily have the tRNS chunk yet.
Packit 0ba690
 */
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_gamma");
Packit 0ba690
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
Packit 0ba690
   if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) ||
Packit 0ba690
       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
Packit 0ba690
       (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
Packit 0ba690
     png_ptr->transformations |= PNG_GAMMA;
Packit 0ba690
   png_ptr->gamma = (float)file_gamma;
Packit 0ba690
   png_ptr->screen_gamma = (float)scrn_gamma;
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_EXPAND_SUPPORTED
Packit 0ba690
/* Expand paletted images to RGB, expand grayscale images of
Packit 0ba690
 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
Packit 0ba690
 * to alpha channels.
Packit 0ba690
 */
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_expand(png_structp png_ptr)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_expand");
Packit 0ba690
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
Packit 0ba690
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
Packit 0ba690
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
/* GRR 19990627:  the following three functions currently are identical
Packit 0ba690
 *  to png_set_expand().  However, it is entirely reasonable that someone
Packit 0ba690
 *  might wish to expand an indexed image to RGB but *not* expand a single,
Packit 0ba690
 *  fully transparent palette entry to a full alpha channel--perhaps instead
Packit 0ba690
 *  convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
Packit 0ba690
 *  the transparent color with a particular RGB value, or drop tRNS entirely.
Packit 0ba690
 *  IOW, a future version of the library may make the transformations flag
Packit 0ba690
 *  a bit more fine-grained, with separate bits for each of these three
Packit 0ba690
 *  functions.
Packit 0ba690
 *
Packit 0ba690
 *  More to the point, these functions make it obvious what libpng will be
Packit 0ba690
 *  doing, whereas "expand" can (and does) mean any number of things.
Packit 0ba690
 *
Packit 0ba690
 *  GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
Packit 0ba690
 *  to expand only the sample depth but not to expand the tRNS to alpha
Packit 0ba690
 *  and its name was changed to png_set_expand_gray_1_2_4_to_8().
Packit 0ba690
 */
Packit 0ba690
Packit 0ba690
/* Expand paletted images to RGB. */
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_palette_to_rgb(png_structp png_ptr)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_palette_to_rgb");
Packit 0ba690
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
Packit 0ba690
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
Packit 0ba690
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
#ifndef PNG_1_0_X
Packit 0ba690
/* Expand grayscale images of less than 8-bit depth to 8 bits. */
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
Packit 0ba690
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
Packit 0ba690
   png_ptr->transformations |= PNG_EXPAND;
Packit 0ba690
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
Packit 0ba690
/* Expand grayscale images of less than 8-bit depth to 8 bits. */
Packit 0ba690
/* Deprecated as of libpng-1.2.9 */
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_gray_1_2_4_to_8(png_structp png_ptr)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_gray_1_2_4_to_8");
Packit 0ba690
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
Packit 0ba690
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
Packit 0ba690
/* Expand tRNS chunks to alpha channels. */
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_tRNS_to_alpha(png_structp png_ptr)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_tRNS_to_alpha");
Packit 0ba690
Packit 0ba690
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
Packit 0ba690
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
Packit 0ba690
}
Packit 0ba690
#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_gray_to_rgb(png_structp png_ptr)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_gray_to_rgb");
Packit 0ba690
Packit 0ba690
   png_ptr->transformations |= PNG_GRAY_TO_RGB;
Packit 0ba690
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Packit 0ba690
#ifdef PNG_FLOATING_POINT_SUPPORTED
Packit 0ba690
/* Convert a RGB image to a grayscale of the same width.  This allows us,
Packit 0ba690
 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
Packit 0ba690
 */
Packit 0ba690
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
Packit 0ba690
   double green)
Packit 0ba690
{
Packit 0ba690
   int red_fixed, green_fixed;
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
   if (red > 21474.83647 || red < -21474.83648 ||
Packit 0ba690
       green > 21474.83647 || green < -21474.83648)
Packit 0ba690
   {
Packit 0ba690
      png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
Packit 0ba690
      red_fixed = -1;
Packit 0ba690
      green_fixed = -1;
Packit 0ba690
   }
Packit 0ba690
   else
Packit 0ba690
   {
Packit 0ba690
      red_fixed = (int)((float)red*100000.0 + 0.5);
Packit 0ba690
      green_fixed = (int)((float)green*100000.0 + 0.5);
Packit 0ba690
   }
Packit 0ba690
   png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed);
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
Packit 0ba690
   png_fixed_point red, png_fixed_point green)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_rgb_to_gray");
Packit 0ba690
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
Packit 0ba690
   switch(error_action)
Packit 0ba690
   {
Packit 0ba690
      case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;
Packit 0ba690
              break;
Packit 0ba690
Packit 0ba690
      case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
Packit 0ba690
              break;
Packit 0ba690
Packit 0ba690
      case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
Packit 0ba690
   }
Packit 0ba690
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Packit 0ba690
#ifdef PNG_READ_EXPAND_SUPPORTED
Packit 0ba690
      png_ptr->transformations |= PNG_EXPAND;
Packit 0ba690
#else
Packit 0ba690
   {
Packit 0ba690
      png_warning(png_ptr,
Packit 0ba690
        "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED.");
Packit 0ba690
      png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
   {
Packit 0ba690
      png_uint_16 red_int, green_int;
Packit 0ba690
      if (red < 0 || green < 0)
Packit 0ba690
      {
Packit 0ba690
         red_int   =  6968; /* .212671 * 32768 + .5 */
Packit 0ba690
         green_int = 23434; /* .715160 * 32768 + .5 */
Packit 0ba690
      }
Packit 0ba690
      else if (red + green < 100000L)
Packit 0ba690
      {
Packit 0ba690
         red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
Packit 0ba690
         green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
Packit 0ba690
      }
Packit 0ba690
      else
Packit 0ba690
      {
Packit 0ba690
         png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
Packit 0ba690
         red_int   =  6968;
Packit 0ba690
         green_int = 23434;
Packit 0ba690
      }
Packit 0ba690
      png_ptr->rgb_to_gray_red_coeff   = red_int;
Packit 0ba690
      png_ptr->rgb_to_gray_green_coeff = green_int;
Packit 0ba690
      png_ptr->rgb_to_gray_blue_coeff  =
Packit 0ba690
         (png_uint_16)(32768 - red_int - green_int);
Packit 0ba690
   }
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
Packit 0ba690
    defined(PNG_LEGACY_SUPPORTED) || \
Packit 0ba690
    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Packit 0ba690
void PNGAPI
Packit 0ba690
png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
Packit 0ba690
   read_user_transform_fn)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_set_read_user_transform_fn");
Packit 0ba690
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
      return;
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Packit 0ba690
   png_ptr->transformations |= PNG_USER_TRANSFORM;
Packit 0ba690
   png_ptr->read_user_transform_fn = read_user_transform_fn;
Packit 0ba690
#endif
Packit 0ba690
#ifdef PNG_LEGACY_SUPPORTED
Packit 0ba690
   if (read_user_transform_fn)
Packit 0ba690
      png_warning(png_ptr,
Packit 0ba690
        "This version of libpng does not support user transforms");
Packit 0ba690
#endif
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
/* Initialize everything needed for the read.  This includes modifying
Packit 0ba690
 * the palette.
Packit 0ba690
 */
Packit 0ba690
void /* PRIVATE */
Packit 0ba690
png_init_read_transformations(png_structp png_ptr)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_init_read_transformations");
Packit 0ba690
Packit 0ba690
#ifdef PNG_USELESS_TESTS_SUPPORTED
Packit 0ba690
  if (png_ptr != NULL)
Packit 0ba690
#endif
Packit 0ba690
  {
Packit 0ba690
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
Packit 0ba690
    defined(PNG_READ_SHIFT_SUPPORTED) || \
Packit 0ba690
    defined(PNG_READ_GAMMA_SUPPORTED)
Packit 0ba690
   int color_type = png_ptr->color_type;
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Packit 0ba690
   /* Detect gray background and attempt to enable optimization
Packit 0ba690
    * for gray --> RGB case
Packit 0ba690
    *
Packit 0ba690
    * Note:  if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
Packit 0ba690
    * RGB_ALPHA (in which case need_expand is superfluous anyway), the
Packit 0ba690
    * background color might actually be gray yet not be flagged as such.
Packit 0ba690
    * This is not a problem for the current code, which uses
Packit 0ba690
    * PNG_BACKGROUND_IS_GRAY only to decide when to do the
Packit 0ba690
    * png_do_gray_to_rgb() transformation.
Packit 0ba690
    */
Packit 0ba690
   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
Packit 0ba690
       !(color_type & PNG_COLOR_MASK_COLOR))
Packit 0ba690
   {
Packit 0ba690
          png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
Packit 0ba690
   } else if ((png_ptr->transformations & PNG_BACKGROUND) &&
Packit 0ba690
              !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
Packit 0ba690
              (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
Packit 0ba690
              png_ptr->background.red == png_ptr->background.green &&
Packit 0ba690
              png_ptr->background.red == png_ptr->background.blue)
Packit 0ba690
   {
Packit 0ba690
          png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
Packit 0ba690
          png_ptr->background.gray = png_ptr->background.red;
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
Packit 0ba690
       (png_ptr->transformations & PNG_EXPAND))
Packit 0ba690
   {
Packit 0ba690
      if (!(color_type & PNG_COLOR_MASK_COLOR))  /* i.e., GRAY or GRAY_ALPHA */
Packit 0ba690
      {
Packit 0ba690
         /* Expand background and tRNS chunks */
Packit 0ba690
         switch (png_ptr->bit_depth)
Packit 0ba690
         {
Packit 0ba690
            case 1:
Packit 0ba690
               png_ptr->background.gray *= (png_uint_16)0xff;
Packit 0ba690
               png_ptr->background.red = png_ptr->background.green
Packit 0ba690
                 =  png_ptr->background.blue = png_ptr->background.gray;
Packit 0ba690
               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
Packit 0ba690
               {
Packit 0ba690
                 png_ptr->trans_values.gray *= (png_uint_16)0xff;
Packit 0ba690
                 png_ptr->trans_values.red = png_ptr->trans_values.green
Packit 0ba690
                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
Packit 0ba690
               }
Packit 0ba690
               break;
Packit 0ba690
Packit 0ba690
            case 2:
Packit 0ba690
               png_ptr->background.gray *= (png_uint_16)0x55;
Packit 0ba690
               png_ptr->background.red = png_ptr->background.green
Packit 0ba690
                 = png_ptr->background.blue = png_ptr->background.gray;
Packit 0ba690
               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
Packit 0ba690
               {
Packit 0ba690
                 png_ptr->trans_values.gray *= (png_uint_16)0x55;
Packit 0ba690
                 png_ptr->trans_values.red = png_ptr->trans_values.green
Packit 0ba690
                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
Packit 0ba690
               }
Packit 0ba690
               break;
Packit 0ba690
Packit 0ba690
            case 4:
Packit 0ba690
               png_ptr->background.gray *= (png_uint_16)0x11;
Packit 0ba690
               png_ptr->background.red = png_ptr->background.green
Packit 0ba690
                 = png_ptr->background.blue = png_ptr->background.gray;
Packit 0ba690
               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
Packit 0ba690
               {
Packit 0ba690
                 png_ptr->trans_values.gray *= (png_uint_16)0x11;
Packit 0ba690
                 png_ptr->trans_values.red = png_ptr->trans_values.green
Packit 0ba690
                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
Packit 0ba690
               }
Packit 0ba690
               break;
Packit 0ba690
Packit 0ba690
            case 8:
Packit 0ba690
Packit 0ba690
            case 16:
Packit 0ba690
               png_ptr->background.red = png_ptr->background.green
Packit 0ba690
                 = png_ptr->background.blue = png_ptr->background.gray;
Packit 0ba690
               break;
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
      else if (color_type == PNG_COLOR_TYPE_PALETTE)
Packit 0ba690
      {
Packit 0ba690
         png_ptr->background.red   =
Packit 0ba690
            png_ptr->palette[png_ptr->background.index].red;
Packit 0ba690
         png_ptr->background.green =
Packit 0ba690
            png_ptr->palette[png_ptr->background.index].green;
Packit 0ba690
         png_ptr->background.blue  =
Packit 0ba690
            png_ptr->palette[png_ptr->background.index].blue;
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Packit 0ba690
        if (png_ptr->transformations & PNG_INVERT_ALPHA)
Packit 0ba690
        {
Packit 0ba690
#ifdef PNG_READ_EXPAND_SUPPORTED
Packit 0ba690
           if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
Packit 0ba690
#endif
Packit 0ba690
           {
Packit 0ba690
           /* Invert the alpha channel (in tRNS) unless the pixels are
Packit 0ba690
            * going to be expanded, in which case leave it for later
Packit 0ba690
            */
Packit 0ba690
              int i, istop;
Packit 0ba690
              istop=(int)png_ptr->num_trans;
Packit 0ba690
              for (i=0; i
Packit 0ba690
                 png_ptr->trans[i] = (png_byte)(255 - png_ptr->trans[i]);
Packit 0ba690
           }
Packit 0ba690
        }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
Packit 0ba690
   png_ptr->background_1 = png_ptr->background;
Packit 0ba690
#endif
Packit 0ba690
#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
Packit 0ba690
Packit 0ba690
   if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
Packit 0ba690
       && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0)
Packit 0ba690
         < PNG_GAMMA_THRESHOLD))
Packit 0ba690
   {
Packit 0ba690
    int i, k;
Packit 0ba690
    k=0;
Packit 0ba690
    for (i=0; i<png_ptr->num_trans; i++)
Packit 0ba690
    {
Packit 0ba690
      if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff)
Packit 0ba690
      {
Packit 0ba690
        k=1; /* Partial transparency is present */
Packit 0ba690
        break;
Packit 0ba690
      }
Packit 0ba690
    }
Packit 0ba690
    if (k == 0)
Packit 0ba690
      png_ptr->transformations &= ~PNG_GAMMA;
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
Packit 0ba690
        png_ptr->gamma != 0.0)
Packit 0ba690
   {
Packit 0ba690
      png_build_gamma_table(png_ptr);
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_BACKGROUND_SUPPORTED
Packit 0ba690
      if (png_ptr->transformations & PNG_BACKGROUND)
Packit 0ba690
      {
Packit 0ba690
         if (color_type == PNG_COLOR_TYPE_PALETTE)
Packit 0ba690
         {
Packit 0ba690
           /* Could skip if no transparency */
Packit 0ba690
            png_color back, back_1;
Packit 0ba690
            png_colorp palette = png_ptr->palette;
Packit 0ba690
            int num_palette = png_ptr->num_palette;
Packit 0ba690
            int i;
Packit 0ba690
            if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
Packit 0ba690
            {
Packit 0ba690
               back.red = png_ptr->gamma_table[png_ptr->background.red];
Packit 0ba690
               back.green = png_ptr->gamma_table[png_ptr->background.green];
Packit 0ba690
               back.blue = png_ptr->gamma_table[png_ptr->background.blue];
Packit 0ba690
Packit 0ba690
               back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
Packit 0ba690
               back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
Packit 0ba690
               back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
Packit 0ba690
            }
Packit 0ba690
            else
Packit 0ba690
            {
Packit 0ba690
               double g, gs;
Packit 0ba690
Packit 0ba690
               switch (png_ptr->background_gamma_type)
Packit 0ba690
               {
Packit 0ba690
                  case PNG_BACKGROUND_GAMMA_SCREEN:
Packit 0ba690
                     g = (png_ptr->screen_gamma);
Packit 0ba690
                     gs = 1.0;
Packit 0ba690
                     break;
Packit 0ba690
Packit 0ba690
                  case PNG_BACKGROUND_GAMMA_FILE:
Packit 0ba690
                     g = 1.0 / (png_ptr->gamma);
Packit 0ba690
                     gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
Packit 0ba690
                     break;
Packit 0ba690
Packit 0ba690
                  case PNG_BACKGROUND_GAMMA_UNIQUE:
Packit 0ba690
                     g = 1.0 / (png_ptr->background_gamma);
Packit 0ba690
                     gs = 1.0 / (png_ptr->background_gamma *
Packit 0ba690
                                 png_ptr->screen_gamma);
Packit 0ba690
                     break;
Packit 0ba690
                  default:
Packit 0ba690
                     g = 1.0;    /* back_1 */
Packit 0ba690
                     gs = 1.0;   /* back */
Packit 0ba690
               }
Packit 0ba690
Packit 0ba690
               if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD)
Packit 0ba690
               {
Packit 0ba690
                  back.red   = (png_byte)png_ptr->background.red;
Packit 0ba690
                  back.green = (png_byte)png_ptr->background.green;
Packit 0ba690
                  back.blue  = (png_byte)png_ptr->background.blue;
Packit 0ba690
               }
Packit 0ba690
               else
Packit 0ba690
               {
Packit 0ba690
                  back.red = (png_byte)(pow(
Packit 0ba690
                     (double)png_ptr->background.red/255, gs) * 255.0 + .5);
Packit 0ba690
                  back.green = (png_byte)(pow(
Packit 0ba690
                     (double)png_ptr->background.green/255, gs) * 255.0
Packit 0ba690
                         + .5);
Packit 0ba690
                  back.blue = (png_byte)(pow(
Packit 0ba690
                     (double)png_ptr->background.blue/255, gs) * 255.0 + .5);
Packit 0ba690
               }
Packit 0ba690
Packit 0ba690
               back_1.red = (png_byte)(pow(
Packit 0ba690
                  (double)png_ptr->background.red/255, g) * 255.0 + .5);
Packit 0ba690
               back_1.green = (png_byte)(pow(
Packit 0ba690
                  (double)png_ptr->background.green/255, g) * 255.0 + .5);
Packit 0ba690
               back_1.blue = (png_byte)(pow(
Packit 0ba690
                  (double)png_ptr->background.blue/255, g) * 255.0 + .5);
Packit 0ba690
            }
Packit 0ba690
            for (i = 0; i < num_palette; i++)
Packit 0ba690
            {
Packit 0ba690
               if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff)
Packit 0ba690
               {
Packit 0ba690
                  if (png_ptr->trans[i] == 0)
Packit 0ba690
                  {
Packit 0ba690
                     palette[i] = back;
Packit 0ba690
                  }
Packit 0ba690
                  else /* if (png_ptr->trans[i] != 0xff) */
Packit 0ba690
                  {
Packit 0ba690
                     png_byte v, w;
Packit 0ba690
Packit 0ba690
                     v = png_ptr->gamma_to_1[palette[i].red];
Packit 0ba690
                     png_composite(w, v, png_ptr->trans[i], back_1.red);
Packit 0ba690
                     palette[i].red = png_ptr->gamma_from_1[w];
Packit 0ba690
Packit 0ba690
                     v = png_ptr->gamma_to_1[palette[i].green];
Packit 0ba690
                     png_composite(w, v, png_ptr->trans[i], back_1.green);
Packit 0ba690
                     palette[i].green = png_ptr->gamma_from_1[w];
Packit 0ba690
Packit 0ba690
                     v = png_ptr->gamma_to_1[palette[i].blue];
Packit 0ba690
                     png_composite(w, v, png_ptr->trans[i], back_1.blue);
Packit 0ba690
                     palette[i].blue = png_ptr->gamma_from_1[w];
Packit 0ba690
                  }
Packit 0ba690
               }
Packit 0ba690
               else
Packit 0ba690
               {
Packit 0ba690
                  palette[i].red = png_ptr->gamma_table[palette[i].red];
Packit 0ba690
                  palette[i].green = png_ptr->gamma_table[palette[i].green];
Packit 0ba690
                  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
            /* Prevent the transformations being done again, and make sure
Packit 0ba690
             * that the now spurious alpha channel is stripped - the code
Packit 0ba690
             * has just reduced background composition and gamma correction
Packit 0ba690
             * to a simple alpha channel strip.
Packit 0ba690
             */
Packit 0ba690
            png_ptr->transformations &= ~PNG_BACKGROUND;
Packit 0ba690
            png_ptr->transformations &= ~PNG_GAMMA;
Packit 0ba690
            png_ptr->transformations |= PNG_STRIP_ALPHA;
Packit 0ba690
         }
Packit 0ba690
         /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
Packit 0ba690
         else
Packit 0ba690
         /* color_type != PNG_COLOR_TYPE_PALETTE */
Packit 0ba690
         {
Packit 0ba690
            double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);
Packit 0ba690
            double g = 1.0;
Packit 0ba690
            double gs = 1.0;
Packit 0ba690
Packit 0ba690
            switch (png_ptr->background_gamma_type)
Packit 0ba690
            {
Packit 0ba690
               case PNG_BACKGROUND_GAMMA_SCREEN:
Packit 0ba690
                  g = (png_ptr->screen_gamma);
Packit 0ba690
                  gs = 1.0;
Packit 0ba690
                  break;
Packit 0ba690
Packit 0ba690
               case PNG_BACKGROUND_GAMMA_FILE:
Packit 0ba690
                  g = 1.0 / (png_ptr->gamma);
Packit 0ba690
                  gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
Packit 0ba690
                  break;
Packit 0ba690
Packit 0ba690
               case PNG_BACKGROUND_GAMMA_UNIQUE:
Packit 0ba690
                  g = 1.0 / (png_ptr->background_gamma);
Packit 0ba690
                  gs = 1.0 / (png_ptr->background_gamma *
Packit 0ba690
                     png_ptr->screen_gamma);
Packit 0ba690
                  break;
Packit 0ba690
            }
Packit 0ba690
Packit 0ba690
            png_ptr->background_1.gray = (png_uint_16)(pow(
Packit 0ba690
               (double)png_ptr->background.gray / m, g) * m + .5);
Packit 0ba690
            png_ptr->background.gray = (png_uint_16)(pow(
Packit 0ba690
               (double)png_ptr->background.gray / m, gs) * m + .5);
Packit 0ba690
Packit 0ba690
            if ((png_ptr->background.red != png_ptr->background.green) ||
Packit 0ba690
                (png_ptr->background.red != png_ptr->background.blue) ||
Packit 0ba690
                (png_ptr->background.red != png_ptr->background.gray))
Packit 0ba690
            {
Packit 0ba690
               /* RGB or RGBA with color background */
Packit 0ba690
               png_ptr->background_1.red = (png_uint_16)(pow(
Packit 0ba690
                  (double)png_ptr->background.red / m, g) * m + .5);
Packit 0ba690
               png_ptr->background_1.green = (png_uint_16)(pow(
Packit 0ba690
                  (double)png_ptr->background.green / m, g) * m + .5);
Packit 0ba690
               png_ptr->background_1.blue = (png_uint_16)(pow(
Packit 0ba690
                  (double)png_ptr->background.blue / m, g) * m + .5);
Packit 0ba690
               png_ptr->background.red = (png_uint_16)(pow(
Packit 0ba690
                  (double)png_ptr->background.red / m, gs) * m + .5);
Packit 0ba690
               png_ptr->background.green = (png_uint_16)(pow(
Packit 0ba690
                  (double)png_ptr->background.green / m, gs) * m + .5);
Packit 0ba690
               png_ptr->background.blue = (png_uint_16)(pow(
Packit 0ba690
                  (double)png_ptr->background.blue / m, gs) * m + .5);
Packit 0ba690
            }
Packit 0ba690
            else
Packit 0ba690
            {
Packit 0ba690
               /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
Packit 0ba690
               png_ptr->background_1.red = png_ptr->background_1.green
Packit 0ba690
                 = png_ptr->background_1.blue = png_ptr->background_1.gray;
Packit 0ba690
               png_ptr->background.red = png_ptr->background.green
Packit 0ba690
                 = png_ptr->background.blue = png_ptr->background.gray;
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
      else
Packit 0ba690
      /* Transformation does not include PNG_BACKGROUND */
Packit 0ba690
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
Packit 0ba690
      if (color_type == PNG_COLOR_TYPE_PALETTE)
Packit 0ba690
      {
Packit 0ba690
         png_colorp palette = png_ptr->palette;
Packit 0ba690
         int num_palette = png_ptr->num_palette;
Packit 0ba690
         int i;
Packit 0ba690
Packit 0ba690
         for (i = 0; i < num_palette; i++)
Packit 0ba690
         {
Packit 0ba690
            palette[i].red = png_ptr->gamma_table[palette[i].red];
Packit 0ba690
            palette[i].green = png_ptr->gamma_table[palette[i].green];
Packit 0ba690
            palette[i].blue = png_ptr->gamma_table[palette[i].blue];
Packit 0ba690
         }
Packit 0ba690
Packit 0ba690
         /* Done the gamma correction. */
Packit 0ba690
         png_ptr->transformations &= ~PNG_GAMMA;
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
#ifdef PNG_READ_BACKGROUND_SUPPORTED
Packit 0ba690
   else
Packit 0ba690
#endif
Packit 0ba690
#endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
Packit 0ba690
#ifdef PNG_READ_BACKGROUND_SUPPORTED
Packit 0ba690
   /* No GAMMA transformation */
Packit 0ba690
   if ((png_ptr->transformations & PNG_BACKGROUND) &&
Packit 0ba690
       (color_type == PNG_COLOR_TYPE_PALETTE))
Packit 0ba690
   {
Packit 0ba690
      int i;
Packit 0ba690
      int istop = (int)png_ptr->num_trans;
Packit 0ba690
      png_color back;
Packit 0ba690
      png_colorp palette = png_ptr->palette;
Packit 0ba690
Packit 0ba690
      back.red   = (png_byte)png_ptr->background.red;
Packit 0ba690
      back.green = (png_byte)png_ptr->background.green;
Packit 0ba690
      back.blue  = (png_byte)png_ptr->background.blue;
Packit 0ba690
Packit 0ba690
      for (i = 0; i < istop; i++)
Packit 0ba690
      {
Packit 0ba690
         if (png_ptr->trans[i] == 0)
Packit 0ba690
         {
Packit 0ba690
            palette[i] = back;
Packit 0ba690
         }
Packit 0ba690
         else if (png_ptr->trans[i] != 0xff)
Packit 0ba690
         {
Packit 0ba690
            /* The png_composite() macro is defined in png.h */
Packit 0ba690
            png_composite(palette[i].red, palette[i].red,
Packit 0ba690
               png_ptr->trans[i], back.red);
Packit 0ba690
            png_composite(palette[i].green, palette[i].green,
Packit 0ba690
               png_ptr->trans[i], back.green);
Packit 0ba690
            png_composite(palette[i].blue, palette[i].blue,
Packit 0ba690
               png_ptr->trans[i], back.blue);
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
Packit 0ba690
      /* Handled alpha, still need to strip the channel. */
Packit 0ba690
      png_ptr->transformations &= ~PNG_BACKGROUND;
Packit 0ba690
      png_ptr->transformations |= PNG_STRIP_ALPHA;
Packit 0ba690
   }
Packit 0ba690
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_SHIFT_SUPPORTED
Packit 0ba690
   if ((png_ptr->transformations & PNG_SHIFT) &&
Packit 0ba690
      !(png_ptr->transformations & PNG_EXPAND) &&
Packit 0ba690
      (color_type == PNG_COLOR_TYPE_PALETTE))
Packit 0ba690
   {
Packit 0ba690
      png_uint_16 i;
Packit 0ba690
      png_uint_16 istop = png_ptr->num_palette;
Packit 0ba690
      int sr = 8 - png_ptr->sig_bit.red;
Packit 0ba690
      int sg = 8 - png_ptr->sig_bit.green;
Packit 0ba690
      int sb = 8 - png_ptr->sig_bit.blue;
Packit 0ba690
Packit 0ba690
      if (sr < 0 || sr > 8)
Packit 0ba690
         sr = 0;
Packit 0ba690
      if (sg < 0 || sg > 8)
Packit 0ba690
         sg = 0;
Packit 0ba690
      if (sb < 0 || sb > 8)
Packit 0ba690
         sb = 0;
Packit 0ba690
      for (i = 0; i < istop; i++)
Packit 0ba690
      {
Packit 0ba690
         png_ptr->palette[i].red >>= sr;
Packit 0ba690
         png_ptr->palette[i].green >>= sg;
Packit 0ba690
         png_ptr->palette[i].blue >>= sb;
Packit 0ba690
      }
Packit 0ba690
Packit 0ba690
      png_ptr->transformations &= ~PNG_SHIFT;
Packit 0ba690
   }
Packit 0ba690
#endif  /* PNG_READ_SHIFT_SUPPORTED */
Packit 0ba690
 }
Packit 0ba690
#if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
Packit 0ba690
 && !defined(PNG_READ_BACKGROUND_SUPPORTED)
Packit 0ba690
   if (png_ptr)
Packit 0ba690
      return;
Packit 0ba690
#endif
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
/* Modify the info structure to reflect the transformations.  The
Packit 0ba690
 * info should be updated so a PNG file could be written with it,
Packit 0ba690
 * assuming the transformations result in valid PNG data.
Packit 0ba690
 */
Packit 0ba690
void /* PRIVATE */
Packit 0ba690
png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_read_transform_info");
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_EXPAND_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_EXPAND)
Packit 0ba690
   {
Packit 0ba690
      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Packit 0ba690
      {
Packit 0ba690
         if (png_ptr->num_trans)
Packit 0ba690
            info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
Packit 0ba690
         else
Packit 0ba690
            info_ptr->color_type = PNG_COLOR_TYPE_RGB;
Packit 0ba690
         info_ptr->bit_depth = 8;
Packit 0ba690
         info_ptr->num_trans = 0;
Packit 0ba690
      }
Packit 0ba690
      else
Packit 0ba690
      {
Packit 0ba690
         if (png_ptr->num_trans)
Packit 0ba690
         {
Packit 0ba690
            if (png_ptr->transformations & PNG_EXPAND_tRNS)
Packit 0ba690
              info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
Packit 0ba690
         }
Packit 0ba690
         if (info_ptr->bit_depth < 8)
Packit 0ba690
            info_ptr->bit_depth = 8;
Packit 0ba690
         info_ptr->num_trans = 0;
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_BACKGROUND_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_BACKGROUND)
Packit 0ba690
   {
Packit 0ba690
      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
Packit 0ba690
      info_ptr->num_trans = 0;
Packit 0ba690
      info_ptr->background = png_ptr->background;
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_GAMMA)
Packit 0ba690
   {
Packit 0ba690
#ifdef PNG_FLOATING_POINT_SUPPORTED
Packit 0ba690
      info_ptr->gamma = png_ptr->gamma;
Packit 0ba690
#endif
Packit 0ba690
#ifdef PNG_FIXED_POINT_SUPPORTED
Packit 0ba690
      info_ptr->int_gamma = png_ptr->int_gamma;
Packit 0ba690
#endif
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_16_TO_8_SUPPORTED
Packit 0ba690
   if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
Packit 0ba690
      info_ptr->bit_depth = 8;
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
Packit 0ba690
      info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
Packit 0ba690
      info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_DITHER_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_DITHER)
Packit 0ba690
   {
Packit 0ba690
      if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
Packit 0ba690
          (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
Packit 0ba690
          png_ptr->palette_lookup && info_ptr->bit_depth == 8)
Packit 0ba690
      {
Packit 0ba690
         info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_PACK_SUPPORTED
Packit 0ba690
   if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
Packit 0ba690
      info_ptr->bit_depth = 8;
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Packit 0ba690
      info_ptr->channels = 1;
Packit 0ba690
   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
Packit 0ba690
      info_ptr->channels = 3;
Packit 0ba690
   else
Packit 0ba690
      info_ptr->channels = 1;
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Packit 0ba690
   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
Packit 0ba690
      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
Packit 0ba690
      info_ptr->channels++;
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_FILLER_SUPPORTED
Packit 0ba690
   /* STRIP_ALPHA and FILLER allowed:  MASK_ALPHA bit stripped above */
Packit 0ba690
   if ((png_ptr->transformations & PNG_FILLER) &&
Packit 0ba690
       ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
Packit 0ba690
       (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
Packit 0ba690
   {
Packit 0ba690
      info_ptr->channels++;
Packit 0ba690
      /* If adding a true alpha channel not just filler */
Packit 0ba690
#ifndef PNG_1_0_X
Packit 0ba690
      if (png_ptr->transformations & PNG_ADD_ALPHA)
Packit 0ba690
        info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
Packit 0ba690
#endif
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
Packit 0ba690
defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
Packit 0ba690
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
Packit 0ba690
     {
Packit 0ba690
       if (info_ptr->bit_depth < png_ptr->user_transform_depth)
Packit 0ba690
         info_ptr->bit_depth = png_ptr->user_transform_depth;
Packit 0ba690
       if (info_ptr->channels < png_ptr->user_transform_channels)
Packit 0ba690
         info_ptr->channels = png_ptr->user_transform_channels;
Packit 0ba690
     }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
   info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
Packit 0ba690
      info_ptr->bit_depth);
Packit 0ba690
Packit 0ba690
   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
Packit 0ba690
Packit 0ba690
#ifndef PNG_READ_EXPAND_SUPPORTED
Packit 0ba690
   if (png_ptr)
Packit 0ba690
      return;
Packit 0ba690
#endif
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
/* Transform the row.  The order of transformations is significant,
Packit 0ba690
 * and is very touchy.  If you add a transformation, take care to
Packit 0ba690
 * decide how it fits in with the other transformations here.
Packit 0ba690
 */
Packit 0ba690
void /* PRIVATE */
Packit 0ba690
png_do_read_transformations(png_structp png_ptr)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_do_read_transformations");
Packit 0ba690
Packit 0ba690
   if (png_ptr->row_buf == NULL)
Packit 0ba690
   {
Packit 0ba690
#if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
Packit 0ba690
      char msg[50];
Packit 0ba690
Packit 0ba690
      png_snprintf2(msg, 50,
Packit 0ba690
         "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,
Packit 0ba690
         png_ptr->pass);
Packit 0ba690
      png_error(png_ptr, msg);
Packit 0ba690
#else
Packit 0ba690
      png_error(png_ptr, "NULL row buffer");
Packit 0ba690
#endif
Packit 0ba690
   }
Packit 0ba690
#ifdef PNG_WARN_UNINITIALIZED_ROW
Packit 0ba690
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Packit 0ba690
      /* Application has failed to call either png_read_start_image()
Packit 0ba690
       * or png_read_update_info() after setting transforms that expand
Packit 0ba690
       * pixels.  This check added to libpng-1.2.19
Packit 0ba690
       */
Packit 0ba690
#if (PNG_WARN_UNINITIALIZED_ROW==1)
Packit 0ba690
      png_error(png_ptr, "Uninitialized row");
Packit 0ba690
#else
Packit 0ba690
      png_warning(png_ptr, "Uninitialized row");
Packit 0ba690
#endif
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_EXPAND_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_EXPAND)
Packit 0ba690
   {
Packit 0ba690
      if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
Packit 0ba690
      {
Packit 0ba690
         if (png_ptr->palette == NULL)
Packit 0ba690
            png_error (png_ptr, "Palette is NULL in indexed image");
Packit 0ba690
Packit 0ba690
         png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
Packit 0ba690
            png_ptr->palette, png_ptr->trans, png_ptr->num_trans);
Packit 0ba690
      }
Packit 0ba690
      else
Packit 0ba690
      {
Packit 0ba690
         if (png_ptr->num_trans &&
Packit 0ba690
             (png_ptr->transformations & PNG_EXPAND_tRNS))
Packit 0ba690
            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
Packit 0ba690
               &(png_ptr->trans_values));
Packit 0ba690
         else
Packit 0ba690
            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
Packit 0ba690
               NULL);
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Packit 0ba690
   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
Packit 0ba690
      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
Packit 0ba690
         PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
Packit 0ba690
   {
Packit 0ba690
      int rgb_error =
Packit 0ba690
         png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info),
Packit 0ba690
             png_ptr->row_buf + 1);
Packit 0ba690
      if (rgb_error)
Packit 0ba690
      {
Packit 0ba690
         png_ptr->rgb_to_gray_status=1;
Packit 0ba690
         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
Packit 0ba690
             PNG_RGB_TO_GRAY_WARN)
Packit 0ba690
            png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
Packit 0ba690
         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
Packit 0ba690
             PNG_RGB_TO_GRAY_ERR)
Packit 0ba690
            png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
/* From Andreas Dilger e-mail to png-implement, 26 March 1998:
Packit 0ba690
 *
Packit 0ba690
 *   In most cases, the "simple transparency" should be done prior to doing
Packit 0ba690
 *   gray-to-RGB, or you will have to test 3x as many bytes to check if a
Packit 0ba690
 *   pixel is transparent.  You would also need to make sure that the
Packit 0ba690
 *   transparency information is upgraded to RGB.
Packit 0ba690
 *
Packit 0ba690
 *   To summarize, the current flow is:
Packit 0ba690
 *   - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
Packit 0ba690
 *                                   with background "in place" if transparent,
Packit 0ba690
 *                                   convert to RGB if necessary
Packit 0ba690
 *   - Gray + alpha -> composite with gray background and remove alpha bytes,
Packit 0ba690
 *                                   convert to RGB if necessary
Packit 0ba690
 *
Packit 0ba690
 *   To support RGB backgrounds for gray images we need:
Packit 0ba690
 *   - Gray + simple transparency -> convert to RGB + simple transparency,
Packit 0ba690
 *                                   compare 3 or 6 bytes and composite with
Packit 0ba690
 *                                   background "in place" if transparent
Packit 0ba690
 *                                   (3x compare/pixel compared to doing
Packit 0ba690
 *                                   composite with gray bkgrnd)
Packit 0ba690
 *   - Gray + alpha -> convert to RGB + alpha, composite with background and
Packit 0ba690
 *                                   remove alpha bytes (3x float
Packit 0ba690
 *                                   operations/pixel compared with composite
Packit 0ba690
 *                                   on gray background)
Packit 0ba690
 *
Packit 0ba690
 *  Greg's change will do this.  The reason it wasn't done before is for
Packit 0ba690
 *  performance, as this increases the per-pixel operations.  If we would check
Packit 0ba690
 *  in advance if the background was gray or RGB, and position the gray-to-RGB
Packit 0ba690
 *  transform appropriately, then it would save a lot of work/time.
Packit 0ba690
 */
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Packit 0ba690
   /* If gray -> RGB, do so now only if background is non-gray; else do later
Packit 0ba690
    * for performance reasons
Packit 0ba690
    */
Packit 0ba690
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
Packit 0ba690
       !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
Packit 0ba690
      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_BACKGROUND_SUPPORTED
Packit 0ba690
   if ((png_ptr->transformations & PNG_BACKGROUND) &&
Packit 0ba690
      ((png_ptr->num_trans != 0 ) ||
Packit 0ba690
      (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
Packit 0ba690
      png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
Packit 0ba690
         &(png_ptr->trans_values), &(png_ptr->background)
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
         , &(png_ptr->background_1),
Packit 0ba690
         png_ptr->gamma_table, png_ptr->gamma_from_1,
Packit 0ba690
         png_ptr->gamma_to_1, png_ptr->gamma_16_table,
Packit 0ba690
         png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
Packit 0ba690
         png_ptr->gamma_shift
Packit 0ba690
#endif
Packit 0ba690
);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
   if ((png_ptr->transformations & PNG_GAMMA) &&
Packit 0ba690
#ifdef PNG_READ_BACKGROUND_SUPPORTED
Packit 0ba690
       !((png_ptr->transformations & PNG_BACKGROUND) &&
Packit 0ba690
       ((png_ptr->num_trans != 0) ||
Packit 0ba690
       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
Packit 0ba690
#endif
Packit 0ba690
       (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
Packit 0ba690
      png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
Packit 0ba690
          png_ptr->gamma_table, png_ptr->gamma_16_table,
Packit 0ba690
          png_ptr->gamma_shift);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_16_TO_8_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_16_TO_8)
Packit 0ba690
      png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_DITHER_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_DITHER)
Packit 0ba690
   {
Packit 0ba690
      png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
Packit 0ba690
         png_ptr->palette_lookup, png_ptr->dither_index);
Packit 0ba690
      if (png_ptr->row_info.rowbytes == (png_uint_32)0)
Packit 0ba690
         png_error(png_ptr, "png_do_dither returned rowbytes=0");
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_INVERT_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_INVERT_MONO)
Packit 0ba690
      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_SHIFT_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_SHIFT)
Packit 0ba690
      png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
Packit 0ba690
         &(png_ptr->shift));
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_PACK_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_PACK)
Packit 0ba690
      png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_BGR_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_BGR)
Packit 0ba690
      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_PACKSWAP_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_PACKSWAP)
Packit 0ba690
      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Packit 0ba690
   /* If gray -> RGB, do so now only if we did not do so above */
Packit 0ba690
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
Packit 0ba690
       (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
Packit 0ba690
      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_FILLER_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_FILLER)
Packit 0ba690
      png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
Packit 0ba690
         (png_uint_32)png_ptr->filler, png_ptr->flags);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_INVERT_ALPHA)
Packit 0ba690
      png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_SWAP_ALPHA)
Packit 0ba690
      png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_SWAP_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_SWAP_BYTES)
Packit 0ba690
      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
Packit 0ba690
    {
Packit 0ba690
      if (png_ptr->read_user_transform_fn != NULL)
Packit 0ba690
         (*(png_ptr->read_user_transform_fn)) /* User read transform function */
Packit 0ba690
            (png_ptr,                    /* png_ptr */
Packit 0ba690
               &(png_ptr->row_info),     /* row_info: */
Packit 0ba690
               /*  png_uint_32 width;       width of row */
Packit 0ba690
               /*  png_uint_32 rowbytes;    number of bytes in row */
Packit 0ba690
               /*  png_byte color_type;     color type of pixels */
Packit 0ba690
               /*  png_byte bit_depth;      bit depth of samples */
Packit 0ba690
               /*  png_byte channels;       number of channels (1-4) */
Packit 0ba690
               /*  png_byte pixel_depth;    bits per pixel (depth*channels) */
Packit 0ba690
               png_ptr->row_buf + 1);    /* start of pixel data for row */
Packit 0ba690
#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
Packit 0ba690
      if (png_ptr->user_transform_depth)
Packit 0ba690
         png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
Packit 0ba690
      if (png_ptr->user_transform_channels)
Packit 0ba690
         png_ptr->row_info.channels = png_ptr->user_transform_channels;
Packit 0ba690
#endif
Packit 0ba690
      png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
Packit 0ba690
         png_ptr->row_info.channels);
Packit 0ba690
      png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
Packit 0ba690
         png_ptr->row_info.width);
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_PACK_SUPPORTED
Packit 0ba690
/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
Packit 0ba690
 * without changing the actual values.  Thus, if you had a row with
Packit 0ba690
 * a bit depth of 1, you would end up with bytes that only contained
Packit 0ba690
 * the numbers 0 or 1.  If you would rather they contain 0 and 255, use
Packit 0ba690
 * png_do_shift() after this.
Packit 0ba690
 */
Packit 0ba690
void /* PRIVATE */
Packit 0ba690
png_do_unpack(png_row_infop row_info, png_bytep row)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_do_unpack");
Packit 0ba690
Packit 0ba690
#ifdef PNG_USELESS_TESTS_SUPPORTED
Packit 0ba690
   if (row != NULL && row_info != NULL && row_info->bit_depth < 8)
Packit 0ba690
#else
Packit 0ba690
   if (row_info->bit_depth < 8)
Packit 0ba690
#endif
Packit 0ba690
   {
Packit 0ba690
      png_uint_32 i;
Packit 0ba690
      png_uint_32 row_width=row_info->width;
Packit 0ba690
Packit 0ba690
      switch (row_info->bit_depth)
Packit 0ba690
      {
Packit 0ba690
         case 1:
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
Packit 0ba690
            png_bytep dp = row + (png_size_t)row_width - 1;
Packit 0ba690
            png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *dp = (png_byte)((*sp >> shift) & 0x01);
Packit 0ba690
               if (shift == 7)
Packit 0ba690
               {
Packit 0ba690
                  shift = 0;
Packit 0ba690
                  sp--;
Packit 0ba690
               }
Packit 0ba690
               else
Packit 0ba690
                  shift++;
Packit 0ba690
Packit 0ba690
               dp--;
Packit 0ba690
            }
Packit 0ba690
            break;
Packit 0ba690
         }
Packit 0ba690
Packit 0ba690
         case 2:
Packit 0ba690
         {
Packit 0ba690
Packit 0ba690
            png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
Packit 0ba690
            png_bytep dp = row + (png_size_t)row_width - 1;
Packit 0ba690
            png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *dp = (png_byte)((*sp >> shift) & 0x03);
Packit 0ba690
               if (shift == 6)
Packit 0ba690
               {
Packit 0ba690
                  shift = 0;
Packit 0ba690
                  sp--;
Packit 0ba690
               }
Packit 0ba690
               else
Packit 0ba690
                  shift += 2;
Packit 0ba690
Packit 0ba690
               dp--;
Packit 0ba690
            }
Packit 0ba690
            break;
Packit 0ba690
         }
Packit 0ba690
Packit 0ba690
         case 4:
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
Packit 0ba690
            png_bytep dp = row + (png_size_t)row_width - 1;
Packit 0ba690
            png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *dp = (png_byte)((*sp >> shift) & 0x0f);
Packit 0ba690
               if (shift == 4)
Packit 0ba690
               {
Packit 0ba690
                  shift = 0;
Packit 0ba690
                  sp--;
Packit 0ba690
               }
Packit 0ba690
               else
Packit 0ba690
                  shift = 4;
Packit 0ba690
Packit 0ba690
               dp--;
Packit 0ba690
            }
Packit 0ba690
            break;
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
      row_info->bit_depth = 8;
Packit 0ba690
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
Packit 0ba690
      row_info->rowbytes = row_width * row_info->channels;
Packit 0ba690
   }
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_SHIFT_SUPPORTED
Packit 0ba690
/* Reverse the effects of png_do_shift.  This routine merely shifts the
Packit 0ba690
 * pixels back to their significant bits values.  Thus, if you have
Packit 0ba690
 * a row of bit depth 8, but only 5 are significant, this will shift
Packit 0ba690
 * the values back to 0 through 31.
Packit 0ba690
 */
Packit 0ba690
void /* PRIVATE */
Packit 0ba690
png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_do_unshift");
Packit 0ba690
Packit 0ba690
   if (
Packit 0ba690
#ifdef PNG_USELESS_TESTS_SUPPORTED
Packit 0ba690
       row != NULL && row_info != NULL && sig_bits != NULL &&
Packit 0ba690
#endif
Packit 0ba690
       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
Packit 0ba690
   {
Packit 0ba690
      int shift[4];
Packit 0ba690
      int channels = 0;
Packit 0ba690
      int c;
Packit 0ba690
      png_uint_16 value = 0;
Packit 0ba690
      png_uint_32 row_width = row_info->width;
Packit 0ba690
Packit 0ba690
      if (row_info->color_type & PNG_COLOR_MASK_COLOR)
Packit 0ba690
      {
Packit 0ba690
         shift[channels++] = row_info->bit_depth - sig_bits->red;
Packit 0ba690
         shift[channels++] = row_info->bit_depth - sig_bits->green;
Packit 0ba690
         shift[channels++] = row_info->bit_depth - sig_bits->blue;
Packit 0ba690
      }
Packit 0ba690
      else
Packit 0ba690
      {
Packit 0ba690
         shift[channels++] = row_info->bit_depth - sig_bits->gray;
Packit 0ba690
      }
Packit 0ba690
      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
Packit 0ba690
      {
Packit 0ba690
         shift[channels++] = row_info->bit_depth - sig_bits->alpha;
Packit 0ba690
      }
Packit 0ba690
Packit 0ba690
      for (c = 0; c < channels; c++)
Packit 0ba690
      {
Packit 0ba690
         if (shift[c] <= 0)
Packit 0ba690
            shift[c] = 0;
Packit 0ba690
         else
Packit 0ba690
            value = 1;
Packit 0ba690
      }
Packit 0ba690
Packit 0ba690
      if (!value)
Packit 0ba690
         return;
Packit 0ba690
Packit 0ba690
      switch (row_info->bit_depth)
Packit 0ba690
      {
Packit 0ba690
         case 2:
Packit 0ba690
         {
Packit 0ba690
            png_bytep bp;
Packit 0ba690
            png_uint_32 i;
Packit 0ba690
            png_uint_32 istop = row_info->rowbytes;
Packit 0ba690
Packit 0ba690
            for (bp = row, i = 0; i < istop; i++)
Packit 0ba690
            {
Packit 0ba690
               *bp >>= 1;
Packit 0ba690
               *bp++ &= 0x55;
Packit 0ba690
            }
Packit 0ba690
            break;
Packit 0ba690
         }
Packit 0ba690
Packit 0ba690
         case 4:
Packit 0ba690
         {
Packit 0ba690
            png_bytep bp = row;
Packit 0ba690
            png_uint_32 i;
Packit 0ba690
            png_uint_32 istop = row_info->rowbytes;
Packit 0ba690
            png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
Packit 0ba690
               (png_byte)((int)0xf >> shift[0]));
Packit 0ba690
Packit 0ba690
            for (i = 0; i < istop; i++)
Packit 0ba690
            {
Packit 0ba690
               *bp >>= shift[0];
Packit 0ba690
               *bp++ &= mask;
Packit 0ba690
            }
Packit 0ba690
            break;
Packit 0ba690
         }
Packit 0ba690
Packit 0ba690
         case 8:
Packit 0ba690
         {
Packit 0ba690
            png_bytep bp = row;
Packit 0ba690
            png_uint_32 i;
Packit 0ba690
            png_uint_32 istop = row_width * channels;
Packit 0ba690
Packit 0ba690
            for (i = 0; i < istop; i++)
Packit 0ba690
            {
Packit 0ba690
               *bp++ >>= shift[i%channels];
Packit 0ba690
            }
Packit 0ba690
            break;
Packit 0ba690
         }
Packit 0ba690
Packit 0ba690
         case 16:
Packit 0ba690
         {
Packit 0ba690
            png_bytep bp = row;
Packit 0ba690
            png_uint_32 i;
Packit 0ba690
            png_uint_32 istop = channels * row_width;
Packit 0ba690
Packit 0ba690
            for (i = 0; i < istop; i++)
Packit 0ba690
            {
Packit 0ba690
               value = (png_uint_16)((*bp << 8) + *(bp + 1));
Packit 0ba690
               value >>= shift[i%channels];
Packit 0ba690
               *bp++ = (png_byte)(value >> 8);
Packit 0ba690
               *bp++ = (png_byte)(value & 0xff);
Packit 0ba690
            }
Packit 0ba690
            break;
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_16_TO_8_SUPPORTED
Packit 0ba690
/* Chop rows of bit depth 16 down to 8 */
Packit 0ba690
void /* PRIVATE */
Packit 0ba690
png_do_chop(png_row_infop row_info, png_bytep row)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_do_chop");
Packit 0ba690
Packit 0ba690
#ifdef PNG_USELESS_TESTS_SUPPORTED
Packit 0ba690
   if (row != NULL && row_info != NULL && row_info->bit_depth == 16)
Packit 0ba690
#else
Packit 0ba690
   if (row_info->bit_depth == 16)
Packit 0ba690
#endif
Packit 0ba690
   {
Packit 0ba690
      png_bytep sp = row;
Packit 0ba690
      png_bytep dp = row;
Packit 0ba690
      png_uint_32 i;
Packit 0ba690
      png_uint_32 istop = row_info->width * row_info->channels;
Packit 0ba690
Packit 0ba690
      for (i = 0; i
Packit 0ba690
      {
Packit 0ba690
#ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
Packit 0ba690
      /* This does a more accurate scaling of the 16-bit color
Packit 0ba690
       * value, rather than a simple low-byte truncation.
Packit 0ba690
       *
Packit 0ba690
       * What the ideal calculation should be:
Packit 0ba690
       *   *dp = (((((png_uint_32)(*sp) << 8) |
Packit 0ba690
       *          (png_uint_32)(*(sp + 1))) * 255 + 127)
Packit 0ba690
       *          / (png_uint_32)65535L;
Packit 0ba690
       *
Packit 0ba690
       * GRR: no, I think this is what it really should be:
Packit 0ba690
       *   *dp = (((((png_uint_32)(*sp) << 8) |
Packit 0ba690
       *           (png_uint_32)(*(sp + 1))) + 128L)
Packit 0ba690
       *           / (png_uint_32)257L;
Packit 0ba690
       *
Packit 0ba690
       * GRR: here's the exact calculation with shifts:
Packit 0ba690
       *   temp = (((png_uint_32)(*sp) << 8) |
Packit 0ba690
       *           (png_uint_32)(*(sp + 1))) + 128L;
Packit 0ba690
       *   *dp = (temp - (temp >> 8)) >> 8;
Packit 0ba690
       *
Packit 0ba690
       * Approximate calculation with shift/add instead of multiply/divide:
Packit 0ba690
       *   *dp = ((((png_uint_32)(*sp) << 8) |
Packit 0ba690
       *          (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
Packit 0ba690
       *
Packit 0ba690
       * What we actually do to avoid extra shifting and conversion:
Packit 0ba690
       */
Packit 0ba690
Packit 0ba690
         *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
Packit 0ba690
#else
Packit 0ba690
       /* Simply discard the low order byte */
Packit 0ba690
         *dp = *sp;
Packit 0ba690
#endif
Packit 0ba690
      }
Packit 0ba690
      row_info->bit_depth = 8;
Packit 0ba690
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
Packit 0ba690
      row_info->rowbytes = row_info->width * row_info->channels;
Packit 0ba690
   }
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Packit 0ba690
void /* PRIVATE */
Packit 0ba690
png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_do_read_swap_alpha");
Packit 0ba690
Packit 0ba690
#ifdef PNG_USELESS_TESTS_SUPPORTED
Packit 0ba690
   if (row != NULL && row_info != NULL)
Packit 0ba690
#endif
Packit 0ba690
   {
Packit 0ba690
      png_uint_32 row_width = row_info->width;
Packit 0ba690
      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Packit 0ba690
      {
Packit 0ba690
         /* This converts from RGBA to ARGB */
Packit 0ba690
         if (row_info->bit_depth == 8)
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + row_info->rowbytes;
Packit 0ba690
            png_bytep dp = sp;
Packit 0ba690
            png_byte save;
Packit 0ba690
            png_uint_32 i;
Packit 0ba690
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               save = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = save;
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
         /* This converts from RRGGBBAA to AARRGGBB */
Packit 0ba690
         else
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + row_info->rowbytes;
Packit 0ba690
            png_bytep dp = sp;
Packit 0ba690
            png_byte save[2];
Packit 0ba690
            png_uint_32 i;
Packit 0ba690
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               save[0] = *(--sp);
Packit 0ba690
               save[1] = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = save[0];
Packit 0ba690
               *(--dp) = save[1];
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Packit 0ba690
      {
Packit 0ba690
         /* This converts from GA to AG */
Packit 0ba690
         if (row_info->bit_depth == 8)
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + row_info->rowbytes;
Packit 0ba690
            png_bytep dp = sp;
Packit 0ba690
            png_byte save;
Packit 0ba690
            png_uint_32 i;
Packit 0ba690
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               save = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = save;
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
         /* This converts from GGAA to AAGG */
Packit 0ba690
         else
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + row_info->rowbytes;
Packit 0ba690
            png_bytep dp = sp;
Packit 0ba690
            png_byte save[2];
Packit 0ba690
            png_uint_32 i;
Packit 0ba690
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               save[0] = *(--sp);
Packit 0ba690
               save[1] = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = save[0];
Packit 0ba690
               *(--dp) = save[1];
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Packit 0ba690
void /* PRIVATE */
Packit 0ba690
png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_do_read_invert_alpha");
Packit 0ba690
Packit 0ba690
#ifdef PNG_USELESS_TESTS_SUPPORTED
Packit 0ba690
   if (row != NULL && row_info != NULL)
Packit 0ba690
#endif
Packit 0ba690
   {
Packit 0ba690
      png_uint_32 row_width = row_info->width;
Packit 0ba690
      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Packit 0ba690
      {
Packit 0ba690
         /* This inverts the alpha channel in RGBA */
Packit 0ba690
         if (row_info->bit_depth == 8)
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + row_info->rowbytes;
Packit 0ba690
            png_bytep dp = sp;
Packit 0ba690
            png_uint_32 i;
Packit 0ba690
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(--dp) = (png_byte)(255 - *(--sp));
Packit 0ba690
Packit 0ba690
/*             This does nothing:
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               We can replace it with:
Packit 0ba690
*/
Packit 0ba690
               sp-=3;
Packit 0ba690
               dp=sp;
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
         /* This inverts the alpha channel in RRGGBBAA */
Packit 0ba690
         else
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + row_info->rowbytes;
Packit 0ba690
            png_bytep dp = sp;
Packit 0ba690
            png_uint_32 i;
Packit 0ba690
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(--dp) = (png_byte)(255 - *(--sp));
Packit 0ba690
               *(--dp) = (png_byte)(255 - *(--sp));
Packit 0ba690
Packit 0ba690
/*             This does nothing:
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               We can replace it with:
Packit 0ba690
*/
Packit 0ba690
               sp-=6;
Packit 0ba690
               dp=sp;
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Packit 0ba690
      {
Packit 0ba690
         /* This inverts the alpha channel in GA */
Packit 0ba690
         if (row_info->bit_depth == 8)
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + row_info->rowbytes;
Packit 0ba690
            png_bytep dp = sp;
Packit 0ba690
            png_uint_32 i;
Packit 0ba690
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(--dp) = (png_byte)(255 - *(--sp));
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
         /* This inverts the alpha channel in GGAA */
Packit 0ba690
         else
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp  = row + row_info->rowbytes;
Packit 0ba690
            png_bytep dp = sp;
Packit 0ba690
            png_uint_32 i;
Packit 0ba690
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(--dp) = (png_byte)(255 - *(--sp));
Packit 0ba690
               *(--dp) = (png_byte)(255 - *(--sp));
Packit 0ba690
/*
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
*/
Packit 0ba690
               sp-=2;
Packit 0ba690
               dp=sp;
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_FILLER_SUPPORTED
Packit 0ba690
/* Add filler channel if we have RGB color */
Packit 0ba690
void /* PRIVATE */
Packit 0ba690
png_do_read_filler(png_row_infop row_info, png_bytep row,
Packit 0ba690
   png_uint_32 filler, png_uint_32 flags)
Packit 0ba690
{
Packit 0ba690
   png_uint_32 i;
Packit 0ba690
   png_uint_32 row_width = row_info->width;
Packit 0ba690
Packit 0ba690
   png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
Packit 0ba690
   png_byte lo_filler = (png_byte)(filler & 0xff);
Packit 0ba690
Packit 0ba690
   png_debug(1, "in png_do_read_filler");
Packit 0ba690
Packit 0ba690
   if (
Packit 0ba690
#ifdef PNG_USELESS_TESTS_SUPPORTED
Packit 0ba690
       row != NULL  && row_info != NULL &&
Packit 0ba690
#endif
Packit 0ba690
       row_info->color_type == PNG_COLOR_TYPE_GRAY)
Packit 0ba690
   {
Packit 0ba690
      if (row_info->bit_depth == 8)
Packit 0ba690
      {
Packit 0ba690
         /* This changes the data from G to GX */
Packit 0ba690
         if (flags & PNG_FLAG_FILLER_AFTER)
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)row_width;
Packit 0ba690
            png_bytep dp =  sp + (png_size_t)row_width;
Packit 0ba690
            for (i = 1; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(--dp) = lo_filler;
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
            }
Packit 0ba690
            *(--dp) = lo_filler;
Packit 0ba690
            row_info->channels = 2;
Packit 0ba690
            row_info->pixel_depth = 16;
Packit 0ba690
            row_info->rowbytes = row_width * 2;
Packit 0ba690
         }
Packit 0ba690
      /* This changes the data from G to XG */
Packit 0ba690
         else
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)row_width;
Packit 0ba690
            png_bytep dp = sp  + (png_size_t)row_width;
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = lo_filler;
Packit 0ba690
            }
Packit 0ba690
            row_info->channels = 2;
Packit 0ba690
            row_info->pixel_depth = 16;
Packit 0ba690
            row_info->rowbytes = row_width * 2;
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
      else if (row_info->bit_depth == 16)
Packit 0ba690
      {
Packit 0ba690
         /* This changes the data from GG to GGXX */
Packit 0ba690
         if (flags & PNG_FLAG_FILLER_AFTER)
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)row_width * 2;
Packit 0ba690
            png_bytep dp = sp  + (png_size_t)row_width * 2;
Packit 0ba690
            for (i = 1; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(--dp) = hi_filler;
Packit 0ba690
               *(--dp) = lo_filler;
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
            }
Packit 0ba690
            *(--dp) = hi_filler;
Packit 0ba690
            *(--dp) = lo_filler;
Packit 0ba690
            row_info->channels = 2;
Packit 0ba690
            row_info->pixel_depth = 32;
Packit 0ba690
            row_info->rowbytes = row_width * 4;
Packit 0ba690
         }
Packit 0ba690
         /* This changes the data from GG to XXGG */
Packit 0ba690
         else
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)row_width * 2;
Packit 0ba690
            png_bytep dp = sp  + (png_size_t)row_width * 2;
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = hi_filler;
Packit 0ba690
               *(--dp) = lo_filler;
Packit 0ba690
            }
Packit 0ba690
            row_info->channels = 2;
Packit 0ba690
            row_info->pixel_depth = 32;
Packit 0ba690
            row_info->rowbytes = row_width * 4;
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
   } /* COLOR_TYPE == GRAY */
Packit 0ba690
   else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
Packit 0ba690
   {
Packit 0ba690
      if (row_info->bit_depth == 8)
Packit 0ba690
      {
Packit 0ba690
         /* This changes the data from RGB to RGBX */
Packit 0ba690
         if (flags & PNG_FLAG_FILLER_AFTER)
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)row_width * 3;
Packit 0ba690
            png_bytep dp = sp  + (png_size_t)row_width;
Packit 0ba690
            for (i = 1; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(--dp) = lo_filler;
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
            }
Packit 0ba690
            *(--dp) = lo_filler;
Packit 0ba690
            row_info->channels = 4;
Packit 0ba690
            row_info->pixel_depth = 32;
Packit 0ba690
            row_info->rowbytes = row_width * 4;
Packit 0ba690
         }
Packit 0ba690
      /* This changes the data from RGB to XRGB */
Packit 0ba690
         else
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)row_width * 3;
Packit 0ba690
            png_bytep dp = sp + (png_size_t)row_width;
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = lo_filler;
Packit 0ba690
            }
Packit 0ba690
            row_info->channels = 4;
Packit 0ba690
            row_info->pixel_depth = 32;
Packit 0ba690
            row_info->rowbytes = row_width * 4;
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
      else if (row_info->bit_depth == 16)
Packit 0ba690
      {
Packit 0ba690
         /* This changes the data from RRGGBB to RRGGBBXX */
Packit 0ba690
         if (flags & PNG_FLAG_FILLER_AFTER)
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)row_width * 6;
Packit 0ba690
            png_bytep dp = sp  + (png_size_t)row_width * 2;
Packit 0ba690
            for (i = 1; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(--dp) = hi_filler;
Packit 0ba690
               *(--dp) = lo_filler;
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
            }
Packit 0ba690
            *(--dp) = hi_filler;
Packit 0ba690
            *(--dp) = lo_filler;
Packit 0ba690
            row_info->channels = 4;
Packit 0ba690
            row_info->pixel_depth = 64;
Packit 0ba690
            row_info->rowbytes = row_width * 8;
Packit 0ba690
         }
Packit 0ba690
         /* This changes the data from RRGGBB to XXRRGGBB */
Packit 0ba690
         else
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)row_width * 6;
Packit 0ba690
            png_bytep dp = sp  + (png_size_t)row_width * 2;
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = *(--sp);
Packit 0ba690
               *(--dp) = hi_filler;
Packit 0ba690
               *(--dp) = lo_filler;
Packit 0ba690
            }
Packit 0ba690
            row_info->channels = 4;
Packit 0ba690
            row_info->pixel_depth = 64;
Packit 0ba690
            row_info->rowbytes = row_width * 8;
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
   } /* COLOR_TYPE == RGB */
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Packit 0ba690
/* Expand grayscale files to RGB, with or without alpha */
Packit 0ba690
void /* PRIVATE */
Packit 0ba690
png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
Packit 0ba690
{
Packit 0ba690
   png_uint_32 i;
Packit 0ba690
   png_uint_32 row_width = row_info->width;
Packit 0ba690
Packit 0ba690
   png_debug(1, "in png_do_gray_to_rgb");
Packit 0ba690
Packit 0ba690
   if (row_info->bit_depth >= 8 &&
Packit 0ba690
#ifdef PNG_USELESS_TESTS_SUPPORTED
Packit 0ba690
       row != NULL && row_info != NULL &&
Packit 0ba690
#endif
Packit 0ba690
      !(row_info->color_type & PNG_COLOR_MASK_COLOR))
Packit 0ba690
   {
Packit 0ba690
      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
Packit 0ba690
      {
Packit 0ba690
         if (row_info->bit_depth == 8)
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)row_width - 1;
Packit 0ba690
            png_bytep dp = sp  + (png_size_t)row_width * 2;
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(dp--) = *sp;
Packit 0ba690
               *(dp--) = *sp;
Packit 0ba690
               *(dp--) = *(sp--);
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
         else
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
Packit 0ba690
            png_bytep dp = sp  + (png_size_t)row_width * 4;
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(dp--) = *sp;
Packit 0ba690
               *(dp--) = *(sp - 1);
Packit 0ba690
               *(dp--) = *sp;
Packit 0ba690
               *(dp--) = *(sp - 1);
Packit 0ba690
               *(dp--) = *(sp--);
Packit 0ba690
               *(dp--) = *(sp--);
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Packit 0ba690
      {
Packit 0ba690
         if (row_info->bit_depth == 8)
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
Packit 0ba690
            png_bytep dp = sp  + (png_size_t)row_width * 2;
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(dp--) = *(sp--);
Packit 0ba690
               *(dp--) = *sp;
Packit 0ba690
               *(dp--) = *sp;
Packit 0ba690
               *(dp--) = *(sp--);
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
         else
Packit 0ba690
         {
Packit 0ba690
            png_bytep sp = row + (png_size_t)row_width * 4 - 1;
Packit 0ba690
            png_bytep dp = sp  + (png_size_t)row_width * 4;
Packit 0ba690
            for (i = 0; i < row_width; i++)
Packit 0ba690
            {
Packit 0ba690
               *(dp--) = *(sp--);
Packit 0ba690
               *(dp--) = *(sp--);
Packit 0ba690
               *(dp--) = *sp;
Packit 0ba690
               *(dp--) = *(sp - 1);
Packit 0ba690
               *(dp--) = *sp;
Packit 0ba690
               *(dp--) = *(sp - 1);
Packit 0ba690
               *(dp--) = *(sp--);
Packit 0ba690
               *(dp--) = *(sp--);
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
      row_info->channels += (png_byte)2;
Packit 0ba690
      row_info->color_type |= PNG_COLOR_MASK_COLOR;
Packit 0ba690
      row_info->pixel_depth = (png_byte)(row_info->channels *
Packit 0ba690
         row_info->bit_depth);
Packit 0ba690
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
Packit 0ba690
   }
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Packit 0ba690
/* Reduce RGB files to grayscale, with or without alpha
Packit 0ba690
 * using the equation given in Poynton's ColorFAQ at
Packit 0ba690
 * <http://www.inforamp.net/~poynton/>  (THIS LINK IS DEAD June 2008)
Packit 0ba690
 * New link:
Packit 0ba690
 * <http://www.poynton.com/notes/colour_and_gamma/>
Packit 0ba690
 * Charles Poynton poynton at poynton.com
Packit 0ba690
 *
Packit 0ba690
 *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
Packit 0ba690
 *
Packit 0ba690
 *  We approximate this with
Packit 0ba690
 *
Packit 0ba690
 *     Y = 0.21268 * R    + 0.7151 * G    + 0.07217 * B
Packit 0ba690
 *
Packit 0ba690
 *  which can be expressed with integers as
Packit 0ba690
 *
Packit 0ba690
 *     Y = (6969 * R + 23434 * G + 2365 * B)/32768
Packit 0ba690
 *
Packit 0ba690
 *  The calculation is to be done in a linear colorspace.
Packit 0ba690
 *
Packit 0ba690
 *  Other integer coefficents can be used via png_set_rgb_to_gray().
Packit 0ba690
 */
Packit 0ba690
int /* PRIVATE */
Packit 0ba690
png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
Packit 0ba690
Packit 0ba690
{
Packit 0ba690
   png_uint_32 i;
Packit 0ba690
Packit 0ba690
   png_uint_32 row_width = row_info->width;
Packit 0ba690
   int rgb_error = 0;
Packit 0ba690
Packit 0ba690
   png_debug(1, "in png_do_rgb_to_gray");
Packit 0ba690
Packit 0ba690
   if (
Packit 0ba690
#ifdef PNG_USELESS_TESTS_SUPPORTED
Packit 0ba690
       row != NULL && row_info != NULL &&
Packit 0ba690
#endif
Packit 0ba690
      (row_info->color_type & PNG_COLOR_MASK_COLOR))
Packit 0ba690
   {
Packit 0ba690
      png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
Packit 0ba690
      png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
Packit 0ba690
      png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
Packit 0ba690
Packit 0ba690
      if (row_info->color_type == PNG_COLOR_TYPE_RGB)
Packit 0ba690
      {
Packit 0ba690
         if (row_info->bit_depth == 8)
Packit 0ba690
         {
Packit 0ba690
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Packit 0ba690
            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
Packit 0ba690
            {
Packit 0ba690
               png_bytep sp = row;
Packit 0ba690
               png_bytep dp = row;
Packit 0ba690
Packit 0ba690
               for (i = 0; i < row_width; i++)
Packit 0ba690
               {
Packit 0ba690
                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
Packit 0ba690
                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
Packit 0ba690
                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
Packit 0ba690
                  if (red != green || red != blue)
Packit 0ba690
                  {
Packit 0ba690
                     rgb_error |= 1;
Packit 0ba690
                     *(dp++) = png_ptr->gamma_from_1[
Packit 0ba690
                       (rc*red + gc*green + bc*blue)>>15];
Packit 0ba690
                  }
Packit 0ba690
                  else
Packit 0ba690
                     *(dp++) = *(sp - 1);
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
            else
Packit 0ba690
#endif
Packit 0ba690
            {
Packit 0ba690
               png_bytep sp = row;
Packit 0ba690
               png_bytep dp = row;
Packit 0ba690
               for (i = 0; i < row_width; i++)
Packit 0ba690
               {
Packit 0ba690
                  png_byte red   = *(sp++);
Packit 0ba690
                  png_byte green = *(sp++);
Packit 0ba690
                  png_byte blue  = *(sp++);
Packit 0ba690
                  if (red != green || red != blue)
Packit 0ba690
                  {
Packit 0ba690
                     rgb_error |= 1;
Packit 0ba690
                     *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
Packit 0ba690
                  }
Packit 0ba690
                  else
Packit 0ba690
                     *(dp++) = *(sp - 1);
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
Packit 0ba690
         else /* RGB bit_depth == 16 */
Packit 0ba690
         {
Packit 0ba690
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Packit 0ba690
            if (png_ptr->gamma_16_to_1 != NULL &&
Packit 0ba690
                png_ptr->gamma_16_from_1 != NULL)
Packit 0ba690
            {
Packit 0ba690
               png_bytep sp = row;
Packit 0ba690
               png_bytep dp = row;
Packit 0ba690
               for (i = 0; i < row_width; i++)
Packit 0ba690
               {
Packit 0ba690
                  png_uint_16 red, green, blue, w;
Packit 0ba690
                  png_byte hi,lo;
Packit 0ba690
Packit 0ba690
                  hi=*(sp)++; lo=*(sp)++;
Packit 0ba690
                  red   = (png_uint_16)((hi << 8) | (lo));
Packit 0ba690
                  hi=*(sp)++; lo=*(sp)++;
Packit 0ba690
                  green = (png_uint_16)((hi << 8) | (lo));
Packit 0ba690
                  hi=*(sp)++; lo=*(sp)++;
Packit 0ba690
                  blue  = (png_uint_16)((hi << 8) | (lo));
Packit 0ba690
Packit 0ba690
                  if (red == green && red == blue)
Packit 0ba690
                     w = red;
Packit 0ba690
                  else
Packit 0ba690
                  {
Packit 0ba690
                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
Packit 0ba690
                                  png_ptr->gamma_shift][red>>8];
Packit 0ba690
                     png_uint_16 green_1 =
Packit 0ba690
                         png_ptr->gamma_16_to_1[(green&0xff) >>
Packit 0ba690
                                  png_ptr->gamma_shift][green>>8];
Packit 0ba690
                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
Packit 0ba690
                                  png_ptr->gamma_shift][blue>>8];
Packit 0ba690
                     png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
Packit 0ba690
                                  + bc*blue_1)>>15);
Packit 0ba690
                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
Packit 0ba690
                         png_ptr->gamma_shift][gray16 >> 8];
Packit 0ba690
                     rgb_error |= 1;
Packit 0ba690
                  }
Packit 0ba690
Packit 0ba690
                  *(dp++) = (png_byte)((w>>8) & 0xff);
Packit 0ba690
                  *(dp++) = (png_byte)(w & 0xff);
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
            else
Packit 0ba690
#endif
Packit 0ba690
            {
Packit 0ba690
               png_bytep sp = row;
Packit 0ba690
               png_bytep dp = row;
Packit 0ba690
               for (i = 0; i < row_width; i++)
Packit 0ba690
               {
Packit 0ba690
                  png_uint_16 red, green, blue, gray16;
Packit 0ba690
                  png_byte hi,lo;
Packit 0ba690
Packit 0ba690
                  hi=*(sp)++; lo=*(sp)++;
Packit 0ba690
                  red   = (png_uint_16)((hi << 8) | (lo));
Packit 0ba690
                  hi=*(sp)++; lo=*(sp)++;
Packit 0ba690
                  green = (png_uint_16)((hi << 8) | (lo));
Packit 0ba690
                  hi=*(sp)++; lo=*(sp)++;
Packit 0ba690
                  blue  = (png_uint_16)((hi << 8) | (lo));
Packit 0ba690
Packit 0ba690
                  if (red != green || red != blue)
Packit 0ba690
                     rgb_error |= 1;
Packit 0ba690
                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
Packit 0ba690
                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
Packit 0ba690
                  *(dp++) = (png_byte)(gray16 & 0xff);
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Packit 0ba690
      {
Packit 0ba690
         if (row_info->bit_depth == 8)
Packit 0ba690
         {
Packit 0ba690
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Packit 0ba690
            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
Packit 0ba690
            {
Packit 0ba690
               png_bytep sp = row;
Packit 0ba690
               png_bytep dp = row;
Packit 0ba690
               for (i = 0; i < row_width; i++)
Packit 0ba690
               {
Packit 0ba690
                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
Packit 0ba690
                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
Packit 0ba690
                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
Packit 0ba690
                  if (red != green || red != blue)
Packit 0ba690
                     rgb_error |= 1;
Packit 0ba690
                  *(dp++) =  png_ptr->gamma_from_1
Packit 0ba690
                             [(rc*red + gc*green + bc*blue)>>15];
Packit 0ba690
                  *(dp++) = *(sp++);  /* alpha */
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
            else
Packit 0ba690
#endif
Packit 0ba690
            {
Packit 0ba690
               png_bytep sp = row;
Packit 0ba690
               png_bytep dp = row;
Packit 0ba690
               for (i = 0; i < row_width; i++)
Packit 0ba690
               {
Packit 0ba690
                  png_byte red   = *(sp++);
Packit 0ba690
                  png_byte green = *(sp++);
Packit 0ba690
                  png_byte blue  = *(sp++);
Packit 0ba690
                  if (red != green || red != blue)
Packit 0ba690
                     rgb_error |= 1;
Packit 0ba690
                  *(dp++) =  (png_byte)((rc*red + gc*green + bc*blue)>>15);
Packit 0ba690
                  *(dp++) = *(sp++);  /* alpha */
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
         else /* RGBA bit_depth == 16 */
Packit 0ba690
         {
Packit 0ba690
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Packit 0ba690
            if (png_ptr->gamma_16_to_1 != NULL &&
Packit 0ba690
                png_ptr->gamma_16_from_1 != NULL)
Packit 0ba690
            {
Packit 0ba690
               png_bytep sp = row;
Packit 0ba690
               png_bytep dp = row;
Packit 0ba690
               for (i = 0; i < row_width; i++)
Packit 0ba690
               {
Packit 0ba690
                  png_uint_16 red, green, blue, w;
Packit 0ba690
                  png_byte hi,lo;
Packit 0ba690
Packit 0ba690
                  hi=*(sp)++; lo=*(sp)++;
Packit 0ba690
                  red   = (png_uint_16)((hi << 8) | (lo));
Packit 0ba690
                  hi=*(sp)++; lo=*(sp)++;
Packit 0ba690
                  green = (png_uint_16)((hi << 8) | (lo));
Packit 0ba690
                  hi=*(sp)++; lo=*(sp)++;
Packit 0ba690
                  blue  = (png_uint_16)((hi << 8) | (lo));
Packit 0ba690
Packit 0ba690
                  if (red == green && red == blue)
Packit 0ba690
                     w = red;
Packit 0ba690
                  else
Packit 0ba690
                  {
Packit 0ba690
                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
Packit 0ba690
                         png_ptr->gamma_shift][red>>8];
Packit 0ba690
                     png_uint_16 green_1 =
Packit 0ba690
                         png_ptr->gamma_16_to_1[(green&0xff) >>
Packit 0ba690
                         png_ptr->gamma_shift][green>>8];
Packit 0ba690
                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
Packit 0ba690
                         png_ptr->gamma_shift][blue>>8];
Packit 0ba690
                     png_uint_16 gray16  = (png_uint_16)((rc * red_1
Packit 0ba690
                         + gc * green_1 + bc * blue_1)>>15);
Packit 0ba690
                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
Packit 0ba690
                         png_ptr->gamma_shift][gray16 >> 8];
Packit 0ba690
                     rgb_error |= 1;
Packit 0ba690
                  }
Packit 0ba690
Packit 0ba690
                  *(dp++) = (png_byte)((w>>8) & 0xff);
Packit 0ba690
                  *(dp++) = (png_byte)(w & 0xff);
Packit 0ba690
                  *(dp++) = *(sp++);  /* alpha */
Packit 0ba690
                  *(dp++) = *(sp++);
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
            else
Packit 0ba690
#endif
Packit 0ba690
            {
Packit 0ba690
               png_bytep sp = row;
Packit 0ba690
               png_bytep dp = row;
Packit 0ba690
               for (i = 0; i < row_width; i++)
Packit 0ba690
               {
Packit 0ba690
                  png_uint_16 red, green, blue, gray16;
Packit 0ba690
                  red   = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
Packit 0ba690
                  green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
Packit 0ba690
                  blue  = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
Packit 0ba690
                  if (red != green || red != blue)
Packit 0ba690
                     rgb_error |= 1;
Packit 0ba690
                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
Packit 0ba690
                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
Packit 0ba690
                  *(dp++) = (png_byte)(gray16 & 0xff);
Packit 0ba690
                  *(dp++) = *(sp++);  /* alpha */
Packit 0ba690
                  *(dp++) = *(sp++);
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
   row_info->channels -= (png_byte)2;
Packit 0ba690
      row_info->color_type &= ~PNG_COLOR_MASK_COLOR;
Packit 0ba690
      row_info->pixel_depth = (png_byte)(row_info->channels *
Packit 0ba690
         row_info->bit_depth);
Packit 0ba690
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
Packit 0ba690
   }
Packit 0ba690
   return rgb_error;
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
/* Build a grayscale palette.  Palette is assumed to be 1 << bit_depth
Packit 0ba690
 * large of png_color.  This lets grayscale images be treated as
Packit 0ba690
 * paletted.  Most useful for gamma correction and simplification
Packit 0ba690
 * of code.
Packit 0ba690
 */
Packit 0ba690
void PNGAPI
Packit 0ba690
png_build_grayscale_palette(int bit_depth, png_colorp palette)
Packit 0ba690
{
Packit 0ba690
   int num_palette;
Packit 0ba690
   int color_inc;
Packit 0ba690
   int i;
Packit 0ba690
   int v;
Packit 0ba690
Packit 0ba690
   png_debug(1, "in png_do_build_grayscale_palette");
Packit 0ba690
Packit 0ba690
   if (palette == NULL)
Packit 0ba690
      return;
Packit 0ba690
Packit 0ba690
   switch (bit_depth)
Packit 0ba690
   {
Packit 0ba690
      case 1:
Packit 0ba690
         num_palette = 2;
Packit 0ba690
         color_inc = 0xff;
Packit 0ba690
         break;
Packit 0ba690
Packit 0ba690
      case 2:
Packit 0ba690
         num_palette = 4;
Packit 0ba690
         color_inc = 0x55;
Packit 0ba690
         break;
Packit 0ba690
Packit 0ba690
      case 4:
Packit 0ba690
         num_palette = 16;
Packit 0ba690
         color_inc = 0x11;
Packit 0ba690
         break;
Packit 0ba690
Packit 0ba690
      case 8:
Packit 0ba690
         num_palette = 256;
Packit 0ba690
         color_inc = 1;
Packit 0ba690
         break;
Packit 0ba690
Packit 0ba690
      default:
Packit 0ba690
         num_palette = 0;
Packit 0ba690
         color_inc = 0;
Packit 0ba690
         break;
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
Packit 0ba690
   {
Packit 0ba690
      palette[i].red = (png_byte)v;
Packit 0ba690
      palette[i].green = (png_byte)v;
Packit 0ba690
      palette[i].blue = (png_byte)v;
Packit 0ba690
   }
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
/* This function is currently unused.  Do we really need it? */
Packit 0ba690
#if defined(PNG_READ_DITHER_SUPPORTED) && \
Packit 0ba690
  defined(PNG_CORRECT_PALETTE_SUPPORTED)
Packit 0ba690
void /* PRIVATE */
Packit 0ba690
png_correct_palette(png_structp png_ptr, png_colorp palette,
Packit 0ba690
   int num_palette)
Packit 0ba690
{
Packit 0ba690
   png_debug(1, "in png_correct_palette");
Packit 0ba690
Packit 0ba690
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
Packit 0ba690
    defined(PNG_READ_GAMMA_SUPPORTED) && \
Packit 0ba690
  defined(PNG_FLOATING_POINT_SUPPORTED)
Packit 0ba690
   if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND))
Packit 0ba690
   {
Packit 0ba690
      png_color back, back_1;
Packit 0ba690
Packit 0ba690
      if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
Packit 0ba690
      {
Packit 0ba690
         back.red = png_ptr->gamma_table[png_ptr->background.red];
Packit 0ba690
         back.green = png_ptr->gamma_table[png_ptr->background.green];
Packit 0ba690
         back.blue = png_ptr->gamma_table[png_ptr->background.blue];
Packit 0ba690
Packit 0ba690
         back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
Packit 0ba690
         back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
Packit 0ba690
         back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
Packit 0ba690
      }
Packit 0ba690
      else
Packit 0ba690
      {
Packit 0ba690
         double g;
Packit 0ba690
Packit 0ba690
         g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma);
Packit 0ba690
Packit 0ba690
         if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN
Packit 0ba690
             || fabs(g - 1.0) < PNG_GAMMA_THRESHOLD)
Packit 0ba690
         {
Packit 0ba690
            back.red = png_ptr->background.red;
Packit 0ba690
            back.green = png_ptr->background.green;
Packit 0ba690
            back.blue = png_ptr->background.blue;
Packit 0ba690
         }
Packit 0ba690
         else
Packit 0ba690
         {
Packit 0ba690
            back.red =
Packit 0ba690
               (png_byte)(pow((double)png_ptr->background.red/255, g) *
Packit 0ba690
                255.0 + 0.5);
Packit 0ba690
            back.green =
Packit 0ba690
               (png_byte)(pow((double)png_ptr->background.green/255, g) *
Packit 0ba690
                255.0 + 0.5);
Packit 0ba690
            back.blue =
Packit 0ba690
               (png_byte)(pow((double)png_ptr->background.blue/255, g) *
Packit 0ba690
                255.0 + 0.5);
Packit 0ba690
         }
Packit 0ba690
Packit 0ba690
         g = 1.0 / png_ptr->background_gamma;
Packit 0ba690
Packit 0ba690
         back_1.red =
Packit 0ba690
            (png_byte)(pow((double)png_ptr->background.red/255, g) *
Packit 0ba690
             255.0 + 0.5);
Packit 0ba690
         back_1.green =
Packit 0ba690
            (png_byte)(pow((double)png_ptr->background.green/255, g) *
Packit 0ba690
             255.0 + 0.5);
Packit 0ba690
         back_1.blue =
Packit 0ba690
            (png_byte)(pow((double)png_ptr->background.blue/255, g) *
Packit 0ba690
             255.0 + 0.5);
Packit 0ba690
      }
Packit 0ba690
Packit 0ba690
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Packit 0ba690
      {
Packit 0ba690
         png_uint_32 i;
Packit 0ba690
Packit 0ba690
         for (i = 0; i < (png_uint_32)num_palette; i++)
Packit 0ba690
         {
Packit 0ba690
            if (i < png_ptr->num_trans && png_ptr->trans[i] == 0)
Packit 0ba690
            {
Packit 0ba690
               palette[i] = back;
Packit 0ba690
            }
Packit 0ba690
            else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff)
Packit 0ba690
            {
Packit 0ba690
               png_byte v, w;
Packit 0ba690
Packit 0ba690
               v = png_ptr->gamma_to_1[png_ptr->palette[i].red];
Packit 0ba690
               png_composite(w, v, png_ptr->trans[i], back_1.red);
Packit 0ba690
               palette[i].red = png_ptr->gamma_from_1[w];
Packit 0ba690
Packit 0ba690
               v = png_ptr->gamma_to_1[png_ptr->palette[i].green];
Packit 0ba690
               png_composite(w, v, png_ptr->trans[i], back_1.green);
Packit 0ba690
               palette[i].green = png_ptr->gamma_from_1[w];
Packit 0ba690
Packit 0ba690
               v = png_ptr->gamma_to_1[png_ptr->palette[i].blue];
Packit 0ba690
               png_composite(w, v, png_ptr->trans[i], back_1.blue);
Packit 0ba690
               palette[i].blue = png_ptr->gamma_from_1[w];
Packit 0ba690
            }
Packit 0ba690
            else
Packit 0ba690
            {
Packit 0ba690
               palette[i].red = png_ptr->gamma_table[palette[i].red];
Packit 0ba690
               palette[i].green = png_ptr->gamma_table[palette[i].green];
Packit 0ba690
               palette[i].blue = png_ptr->gamma_table[palette[i].blue];
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
      else
Packit 0ba690
      {
Packit 0ba690
         int i;
Packit 0ba690
Packit 0ba690
         for (i = 0; i < num_palette; i++)
Packit 0ba690
         {
Packit 0ba690
            if (palette[i].red == (png_byte)png_ptr->trans_values.gray)
Packit 0ba690
            {
Packit 0ba690
               palette[i] = back;
Packit 0ba690
            }
Packit 0ba690
            else
Packit 0ba690
            {
Packit 0ba690
               palette[i].red = png_ptr->gamma_table[palette[i].red];
Packit 0ba690
               palette[i].green = png_ptr->gamma_table[palette[i].green];
Packit 0ba690
               palette[i].blue = png_ptr->gamma_table[palette[i].blue];
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
   else
Packit 0ba690
#endif
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_GAMMA)
Packit 0ba690
   {
Packit 0ba690
      int i;
Packit 0ba690
Packit 0ba690
      for (i = 0; i < num_palette; i++)
Packit 0ba690
      {
Packit 0ba690
         palette[i].red = png_ptr->gamma_table[palette[i].red];
Packit 0ba690
         palette[i].green = png_ptr->gamma_table[palette[i].green];
Packit 0ba690
         palette[i].blue = png_ptr->gamma_table[palette[i].blue];
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
#ifdef PNG_READ_BACKGROUND_SUPPORTED
Packit 0ba690
   else
Packit 0ba690
#endif
Packit 0ba690
#endif
Packit 0ba690
#ifdef PNG_READ_BACKGROUND_SUPPORTED
Packit 0ba690
   if (png_ptr->transformations & PNG_BACKGROUND)
Packit 0ba690
   {
Packit 0ba690
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Packit 0ba690
      {
Packit 0ba690
         png_color back;
Packit 0ba690
Packit 0ba690
         back.red   = (png_byte)png_ptr->background.red;
Packit 0ba690
         back.green = (png_byte)png_ptr->background.green;
Packit 0ba690
         back.blue  = (png_byte)png_ptr->background.blue;
Packit 0ba690
Packit 0ba690
         for (i = 0; i < (int)png_ptr->num_trans; i++)
Packit 0ba690
         {
Packit 0ba690
            if (png_ptr->trans[i] == 0)
Packit 0ba690
            {
Packit 0ba690
               palette[i].red = back.red;
Packit 0ba690
               palette[i].green = back.green;
Packit 0ba690
               palette[i].blue = back.blue;
Packit 0ba690
            }
Packit 0ba690
            else if (png_ptr->trans[i] != 0xff)
Packit 0ba690
            {
Packit 0ba690
               png_composite(palette[i].red, png_ptr->palette[i].red,
Packit 0ba690
                  png_ptr->trans[i], back.red);
Packit 0ba690
               png_composite(palette[i].green, png_ptr->palette[i].green,
Packit 0ba690
                  png_ptr->trans[i], back.green);
Packit 0ba690
               png_composite(palette[i].blue, png_ptr->palette[i].blue,
Packit 0ba690
                  png_ptr->trans[i], back.blue);
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
      else /* Assume grayscale palette (what else could it be?) */
Packit 0ba690
      {
Packit 0ba690
         int i;
Packit 0ba690
Packit 0ba690
         for (i = 0; i < num_palette; i++)
Packit 0ba690
         {
Packit 0ba690
            if (i == (png_byte)png_ptr->trans_values.gray)
Packit 0ba690
            {
Packit 0ba690
               palette[i].red = (png_byte)png_ptr->background.red;
Packit 0ba690
               palette[i].green = (png_byte)png_ptr->background.green;
Packit 0ba690
               palette[i].blue = (png_byte)png_ptr->background.blue;
Packit 0ba690
            }
Packit 0ba690
         }
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
#endif
Packit 0ba690
}
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
#ifdef PNG_READ_BACKGROUND_SUPPORTED
Packit 0ba690
/* Replace any alpha or transparency with the supplied background color.
Packit 0ba690
 * "background" is already in the screen gamma, while "background_1" is
Packit 0ba690
 * at a gamma of 1.0.  Paletted files have already been taken care of.
Packit 0ba690
 */
Packit 0ba690
void /* PRIVATE */
Packit 0ba690
png_do_background(png_row_infop row_info, png_bytep row,
Packit 0ba690
   png_color_16p trans_values, png_color_16p background
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
   , png_color_16p background_1,
Packit 0ba690
   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
Packit 0ba690
   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
Packit 0ba690
   png_uint_16pp gamma_16_to_1, int gamma_shift
Packit 0ba690
#endif
Packit 0ba690
   )
Packit 0ba690
{
Packit 0ba690
   png_bytep sp, dp;
Packit 0ba690
   png_uint_32 i;
Packit 0ba690
   png_uint_32 row_width=row_info->width;
Packit 0ba690
   int shift;
Packit 0ba690
Packit 0ba690
   png_debug(1, "in png_do_background");
Packit 0ba690
Packit 0ba690
   if (background != NULL &&
Packit 0ba690
#ifdef PNG_USELESS_TESTS_SUPPORTED
Packit 0ba690
       row != NULL && row_info != NULL &&
Packit 0ba690
#endif
Packit 0ba690
      (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
Packit 0ba690
      (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values)))
Packit 0ba690
   {
Packit 0ba690
      switch (row_info->color_type)
Packit 0ba690
      {
Packit 0ba690
         case PNG_COLOR_TYPE_GRAY:
Packit 0ba690
         {
Packit 0ba690
            switch (row_info->bit_depth)
Packit 0ba690
            {
Packit 0ba690
               case 1:
Packit 0ba690
               {
Packit 0ba690
                  sp = row;
Packit 0ba690
                  shift = 7;
Packit 0ba690
                  for (i = 0; i < row_width; i++)
Packit 0ba690
                  {
Packit 0ba690
                     if ((png_uint_16)((*sp >> shift) & 0x01)
Packit 0ba690
                        == trans_values->gray)
Packit 0ba690
                     {
Packit 0ba690
                        *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
Packit 0ba690
                        *sp |= (png_byte)(background->gray << shift);
Packit 0ba690
                     }
Packit 0ba690
                     if (!shift)
Packit 0ba690
                     {
Packit 0ba690
                        shift = 7;
Packit 0ba690
                        sp++;
Packit 0ba690
                     }
Packit 0ba690
                     else
Packit 0ba690
                        shift--;
Packit 0ba690
                  }
Packit 0ba690
                  break;
Packit 0ba690
               }
Packit 0ba690
Packit 0ba690
               case 2:
Packit 0ba690
               {
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
                  if (gamma_table != NULL)
Packit 0ba690
                  {
Packit 0ba690
                     sp = row;
Packit 0ba690
                     shift = 6;
Packit 0ba690
                     for (i = 0; i < row_width; i++)
Packit 0ba690
                     {
Packit 0ba690
                        if ((png_uint_16)((*sp >> shift) & 0x03)
Packit 0ba690
                            == trans_values->gray)
Packit 0ba690
                        {
Packit 0ba690
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
Packit 0ba690
                           *sp |= (png_byte)(background->gray << shift);
Packit 0ba690
                        }
Packit 0ba690
                        else
Packit 0ba690
                        {
Packit 0ba690
                           png_byte p = (png_byte)((*sp >> shift) & 0x03);
Packit 0ba690
                           png_byte g = (png_byte)((gamma_table [p | (p << 2) |
Packit 0ba690
                               (p << 4) | (p << 6)] >> 6) & 0x03);
Packit 0ba690
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
Packit 0ba690
                           *sp |= (png_byte)(g << shift);
Packit 0ba690
                        }
Packit 0ba690
                        if (!shift)
Packit 0ba690
                        {
Packit 0ba690
                           shift = 6;
Packit 0ba690
                           sp++;
Packit 0ba690
                        }
Packit 0ba690
                        else
Packit 0ba690
                           shift -= 2;
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
                  else
Packit 0ba690
#endif
Packit 0ba690
                  {
Packit 0ba690
                     sp = row;
Packit 0ba690
                     shift = 6;
Packit 0ba690
                     for (i = 0; i < row_width; i++)
Packit 0ba690
                     {
Packit 0ba690
                        if ((png_uint_16)((*sp >> shift) & 0x03)
Packit 0ba690
                            == trans_values->gray)
Packit 0ba690
                        {
Packit 0ba690
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
Packit 0ba690
                           *sp |= (png_byte)(background->gray << shift);
Packit 0ba690
                        }
Packit 0ba690
                        if (!shift)
Packit 0ba690
                        {
Packit 0ba690
                           shift = 6;
Packit 0ba690
                           sp++;
Packit 0ba690
                        }
Packit 0ba690
                        else
Packit 0ba690
                           shift -= 2;
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
                  break;
Packit 0ba690
               }
Packit 0ba690
Packit 0ba690
               case 4:
Packit 0ba690
               {
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
                  if (gamma_table != NULL)
Packit 0ba690
                  {
Packit 0ba690
                     sp = row;
Packit 0ba690
                     shift = 4;
Packit 0ba690
                     for (i = 0; i < row_width; i++)
Packit 0ba690
                     {
Packit 0ba690
                        if ((png_uint_16)((*sp >> shift) & 0x0f)
Packit 0ba690
                            == trans_values->gray)
Packit 0ba690
                        {
Packit 0ba690
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
Packit 0ba690
                           *sp |= (png_byte)(background->gray << shift);
Packit 0ba690
                        }
Packit 0ba690
                        else
Packit 0ba690
                        {
Packit 0ba690
                           png_byte p = (png_byte)((*sp >> shift) & 0x0f);
Packit 0ba690
                           png_byte g = (png_byte)((gamma_table[p |
Packit 0ba690
                             (p << 4)] >> 4) & 0x0f);
Packit 0ba690
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
Packit 0ba690
                           *sp |= (png_byte)(g << shift);
Packit 0ba690
                        }
Packit 0ba690
                        if (!shift)
Packit 0ba690
                        {
Packit 0ba690
                           shift = 4;
Packit 0ba690
                           sp++;
Packit 0ba690
                        }
Packit 0ba690
                        else
Packit 0ba690
                           shift -= 4;
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
                  else
Packit 0ba690
#endif
Packit 0ba690
                  {
Packit 0ba690
                     sp = row;
Packit 0ba690
                     shift = 4;
Packit 0ba690
                     for (i = 0; i < row_width; i++)
Packit 0ba690
                     {
Packit 0ba690
                        if ((png_uint_16)((*sp >> shift) & 0x0f)
Packit 0ba690
                            == trans_values->gray)
Packit 0ba690
                        {
Packit 0ba690
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
Packit 0ba690
                           *sp |= (png_byte)(background->gray << shift);
Packit 0ba690
                        }
Packit 0ba690
                        if (!shift)
Packit 0ba690
                        {
Packit 0ba690
                           shift = 4;
Packit 0ba690
                           sp++;
Packit 0ba690
                        }
Packit 0ba690
                        else
Packit 0ba690
                           shift -= 4;
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
                  break;
Packit 0ba690
               }
Packit 0ba690
Packit 0ba690
               case 8:
Packit 0ba690
               {
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
                  if (gamma_table != NULL)
Packit 0ba690
                  {
Packit 0ba690
                     sp = row;
Packit 0ba690
                     for (i = 0; i < row_width; i++, sp++)
Packit 0ba690
                     {
Packit 0ba690
                        if (*sp == trans_values->gray)
Packit 0ba690
                        {
Packit 0ba690
                           *sp = (png_byte)background->gray;
Packit 0ba690
                        }
Packit 0ba690
                        else
Packit 0ba690
                        {
Packit 0ba690
                           *sp = gamma_table[*sp];
Packit 0ba690
                        }
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
                  else
Packit 0ba690
#endif
Packit 0ba690
                  {
Packit 0ba690
                     sp = row;
Packit 0ba690
                     for (i = 0; i < row_width; i++, sp++)
Packit 0ba690
                     {
Packit 0ba690
                        if (*sp == trans_values->gray)
Packit 0ba690
                        {
Packit 0ba690
                           *sp = (png_byte)background->gray;
Packit 0ba690
                        }
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
                  break;
Packit 0ba690
               }
Packit 0ba690
Packit 0ba690
               case 16:
Packit 0ba690
               {
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
                  if (gamma_16 != NULL)
Packit 0ba690
                  {
Packit 0ba690
                     sp = row;
Packit 0ba690
                     for (i = 0; i < row_width; i++, sp += 2)
Packit 0ba690
                     {
Packit 0ba690
                        png_uint_16 v;
Packit 0ba690
Packit 0ba690
                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
Packit 0ba690
                        if (v == trans_values->gray)
Packit 0ba690
                        {
Packit 0ba690
                           /* Background is already in screen gamma */
Packit 0ba690
                           *sp = (png_byte)((background->gray >> 8) & 0xff);
Packit 0ba690
                           *(sp + 1) = (png_byte)(background->gray & 0xff);
Packit 0ba690
                        }
Packit 0ba690
                        else
Packit 0ba690
                        {
Packit 0ba690
                           v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
Packit 0ba690
                           *sp = (png_byte)((v >> 8) & 0xff);
Packit 0ba690
                           *(sp + 1) = (png_byte)(v & 0xff);
Packit 0ba690
                        }
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
                  else
Packit 0ba690
#endif
Packit 0ba690
                  {
Packit 0ba690
                     sp = row;
Packit 0ba690
                     for (i = 0; i < row_width; i++, sp += 2)
Packit 0ba690
                     {
Packit 0ba690
                        png_uint_16 v;
Packit 0ba690
Packit 0ba690
                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
Packit 0ba690
                        if (v == trans_values->gray)
Packit 0ba690
                        {
Packit 0ba690
                           *sp = (png_byte)((background->gray >> 8) & 0xff);
Packit 0ba690
                           *(sp + 1) = (png_byte)(background->gray & 0xff);
Packit 0ba690
                        }
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
                  break;
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
            break;
Packit 0ba690
         }
Packit 0ba690
Packit 0ba690
         case PNG_COLOR_TYPE_RGB:
Packit 0ba690
         {
Packit 0ba690
            if (row_info->bit_depth == 8)
Packit 0ba690
            {
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
               if (gamma_table != NULL)
Packit 0ba690
               {
Packit 0ba690
                  sp = row;
Packit 0ba690
                  for (i = 0; i < row_width; i++, sp += 3)
Packit 0ba690
                  {
Packit 0ba690
                     if (*sp == trans_values->red &&
Packit 0ba690
                        *(sp + 1) == trans_values->green &&
Packit 0ba690
                        *(sp + 2) == trans_values->blue)
Packit 0ba690
                     {
Packit 0ba690
                        *sp = (png_byte)background->red;
Packit 0ba690
                        *(sp + 1) = (png_byte)background->green;
Packit 0ba690
                        *(sp + 2) = (png_byte)background->blue;
Packit 0ba690
                     }
Packit 0ba690
                     else
Packit 0ba690
                     {
Packit 0ba690
                        *sp = gamma_table[*sp];
Packit 0ba690
                        *(sp + 1) = gamma_table[*(sp + 1)];
Packit 0ba690
                        *(sp + 2) = gamma_table[*(sp + 2)];
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
               }
Packit 0ba690
               else
Packit 0ba690
#endif
Packit 0ba690
               {
Packit 0ba690
                  sp = row;
Packit 0ba690
                  for (i = 0; i < row_width; i++, sp += 3)
Packit 0ba690
                  {
Packit 0ba690
                     if (*sp == trans_values->red &&
Packit 0ba690
                        *(sp + 1) == trans_values->green &&
Packit 0ba690
                        *(sp + 2) == trans_values->blue)
Packit 0ba690
                     {
Packit 0ba690
                        *sp = (png_byte)background->red;
Packit 0ba690
                        *(sp + 1) = (png_byte)background->green;
Packit 0ba690
                        *(sp + 2) = (png_byte)background->blue;
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
            else /* if (row_info->bit_depth == 16) */
Packit 0ba690
            {
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
               if (gamma_16 != NULL)
Packit 0ba690
               {
Packit 0ba690
                  sp = row;
Packit 0ba690
                  for (i = 0; i < row_width; i++, sp += 6)
Packit 0ba690
                  {
Packit 0ba690
                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
Packit 0ba690
                     png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
Packit 0ba690
                     png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
Packit 0ba690
                     if (r == trans_values->red && g == trans_values->green &&
Packit 0ba690
                        b == trans_values->blue)
Packit 0ba690
                     {
Packit 0ba690
                        /* Background is already in screen gamma */
Packit 0ba690
                        *sp = (png_byte)((background->red >> 8) & 0xff);
Packit 0ba690
                        *(sp + 1) = (png_byte)(background->red & 0xff);
Packit 0ba690
                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
Packit 0ba690
                        *(sp + 3) = (png_byte)(background->green & 0xff);
Packit 0ba690
                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
Packit 0ba690
                        *(sp + 5) = (png_byte)(background->blue & 0xff);
Packit 0ba690
                     }
Packit 0ba690
                     else
Packit 0ba690
                     {
Packit 0ba690
                        png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
Packit 0ba690
                        *sp = (png_byte)((v >> 8) & 0xff);
Packit 0ba690
                        *(sp + 1) = (png_byte)(v & 0xff);
Packit 0ba690
                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
Packit 0ba690
                        *(sp + 2) = (png_byte)((v >> 8) & 0xff);
Packit 0ba690
                        *(sp + 3) = (png_byte)(v & 0xff);
Packit 0ba690
                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
Packit 0ba690
                        *(sp + 4) = (png_byte)((v >> 8) & 0xff);
Packit 0ba690
                        *(sp + 5) = (png_byte)(v & 0xff);
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
               }
Packit 0ba690
               else
Packit 0ba690
#endif
Packit 0ba690
               {
Packit 0ba690
                  sp = row;
Packit 0ba690
                  for (i = 0; i < row_width; i++, sp += 6)
Packit 0ba690
                  {
Packit 0ba690
                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1));
Packit 0ba690
                     png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
Packit 0ba690
                     png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
Packit 0ba690
Packit 0ba690
                     if (r == trans_values->red && g == trans_values->green &&
Packit 0ba690
                        b == trans_values->blue)
Packit 0ba690
                     {
Packit 0ba690
                        *sp = (png_byte)((background->red >> 8) & 0xff);
Packit 0ba690
                        *(sp + 1) = (png_byte)(background->red & 0xff);
Packit 0ba690
                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
Packit 0ba690
                        *(sp + 3) = (png_byte)(background->green & 0xff);
Packit 0ba690
                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
Packit 0ba690
                        *(sp + 5) = (png_byte)(background->blue & 0xff);
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
            break;
Packit 0ba690
         }
Packit 0ba690
Packit 0ba690
         case PNG_COLOR_TYPE_GRAY_ALPHA:
Packit 0ba690
         {
Packit 0ba690
            if (row_info->bit_depth == 8)
Packit 0ba690
            {
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
Packit 0ba690
                   gamma_table != NULL)
Packit 0ba690
               {
Packit 0ba690
                  sp = row;
Packit 0ba690
                  dp = row;
Packit 0ba690
                  for (i = 0; i < row_width; i++, sp += 2, dp++)
Packit 0ba690
                  {
Packit 0ba690
                     png_uint_16 a = *(sp + 1);
Packit 0ba690
Packit 0ba690
                     if (a == 0xff)
Packit 0ba690
                     {
Packit 0ba690
                        *dp = gamma_table[*sp];
Packit 0ba690
                     }
Packit 0ba690
                     else if (a == 0)
Packit 0ba690
                     {
Packit 0ba690
                        /* Background is already in screen gamma */
Packit 0ba690
                        *dp = (png_byte)background->gray;
Packit 0ba690
                     }
Packit 0ba690
                     else
Packit 0ba690
                     {
Packit 0ba690
                        png_byte v, w;
Packit 0ba690
Packit 0ba690
                        v = gamma_to_1[*sp];
Packit 0ba690
                        png_composite(w, v, a, background_1->gray);
Packit 0ba690
                        *dp = gamma_from_1[w];
Packit 0ba690
                     }
Packit 0ba690
                  }
Packit 0ba690
               }
Packit 0ba690
               else
Packit 0ba690
#endif
Packit 0ba690
               {
Packit 0ba690
                  sp = row;
Packit 0ba690
                  dp = row;
Packit 0ba690
                  for (i = 0; i < row_width; i++, sp += 2, dp++)
Packit 0ba690
                  {
Packit 0ba690
                     png_byte a = *(sp + 1);
Packit 0ba690
Packit 0ba690
                     if (a == 0xff)
Packit 0ba690
                     {
Packit 0ba690
                        *dp = *sp;
Packit 0ba690
                     }
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
                     else if (a == 0)
Packit 0ba690
                     {
Packit 0ba690
                        *dp = (png_byte)background->gray;
Packit 0ba690
                     }
Packit 0ba690
                     else
Packit 0ba690
                     {
Packit 0ba690
                        png_composite(*dp, *sp, a, background_1->gray);
Packit 0ba690
                     }
Packit 0ba690
#else
Packit 0ba690
                     *dp = (png_byte)background->gray;
Packit 0ba690
#endif
Packit 0ba690
                  }
Packit 0ba690
               }
Packit 0ba690
            }
Packit 0ba690
            else /* if (png_ptr->bit_depth == 16) */
Packit 0ba690
            {
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
Packit 0ba690
                   gamma_16_to_1 != NULL)
Packit 0ba690
               {
Packit 0ba690
                  sp = row;
Packit 0ba690
                  dp = row;
Packit 0ba690
                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
Packit 0ba690
                  {
Packit 0ba690
                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
Packit 0ba690
Packit 0ba690
                     if (a == (png_uint_16)0xffff)
Packit 0ba690
                     {
Packit 0ba690
                        png_uint_16 v;
Packit 0ba690
Packit 0ba690
                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
Packit 0ba690
                        *dp = (png_byte)((v >> 8) & 0xff);
Packit 0ba690
                        *(dp + 1) = (png_byte)(v & 0xff);
Packit 0ba690
                     }
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
                     else if (a == 0)
Packit 0ba690
#else
Packit 0ba690
                     else
Packit 0ba690
#endif
Packit 0ba690
                     {
Packit 0ba690
                        /* Background is already in screen gamma */
Packit 0ba690
                        *dp = (png_byte)((background->gray >> 8) & 0xff);
Packit 0ba690
                        *(dp + 1) = (png_byte)(background->gray & 0xff);
Packit 0ba690
                     }
Packit 0ba690
#ifdef PNG_READ_GAMMA_SUPPORTED
Packit 0ba690
                     else
Packit 0ba690
                     {
Packit 0ba690
                        png_uint_16 g, v, w;
Packit 0ba690
Packit 0ba690
                        g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
Packit 0ba690
                        png_composite_16(v, g, a, background_1->gray);
Packit 0ba690
                        w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
Packit 0ba690
                        *dp = (png_byte)((w >> 8) & 0xff);
Packit 0ba690
                        *(dp + 1) = (png_byte)(w & 0xff);
Packit 0ba690
                     }
Packit 0ba690
#endif
Packit 0ba690
                  }
Packit 0ba690
               }
Packit 0ba690
               else
Packit 0ba690
#endif
Packit 0ba690
               {
Packit 0ba690
                  sp = row;
Packit 0ba690