Esempio n. 1
0
/*
 * Reduce the image (bit depth + color type + palette) without
 * losing any information. The palette (if applicable) and the
 * image data must be present (e.g. by calling png_set_rows(),
 * or by loading IDAT).
 * The parameter reductions indicates the intended reductions.
 * The function returns the successful reductions.
 */
png_uint_32 PNGAPI
opng_reduce_image(png_structp png_ptr, png_infop info_ptr,
   png_uint_32 reductions)
{
   unsigned int color_type;
   png_uint_32 result;

   png_debug(1, "in opng_reduce_image_type\n");

   if (!opng_validate_image(png_ptr, info_ptr))
   {
      png_warning(png_ptr,
         "Image reduction requires the presence of the critical info");
      return OPNG_REDUCE_NONE;
   }

#if 0  /* PNG_INTERLACE must be recognized! */
   if (png_ptr->transformations)
   {
      png_warning(png_ptr,
         "Image reduction cannot be applied "
         "under the presence of transformations");
      return OPNG_REDUCE_NONE;
   }
#endif

   color_type = info_ptr->color_type;

   /* The reductions below must be applied in the given order. */

   /* Try to reduce the high bits and color/alpha channels. */
   result = opng_reduce_bits(png_ptr, info_ptr, reductions);

   /* Try to reduce the palette image. */
   if (color_type == PNG_COLOR_TYPE_PALETTE &&
       (reductions & (OPNG_REDUCE_PALETTE_TO_GRAY |
                      OPNG_REDUCE_PALETTE_FAST |
                      OPNG_REDUCE_8_TO_4_2_1)))
      result |= opng_reduce_palette(png_ptr, info_ptr, reductions);

   /* Try to reduce RGB to palette or grayscale to palette. */
   if (((color_type & ~PNG_COLOR_MASK_ALPHA) == PNG_COLOR_TYPE_GRAY &&
        (reductions & OPNG_REDUCE_GRAY_TO_PALETTE)) ||
       ((color_type & ~PNG_COLOR_MASK_ALPHA) == PNG_COLOR_TYPE_RGB &&
        (reductions & OPNG_REDUCE_RGB_TO_PALETTE)))
   {
      if (!(result & OPNG_REDUCE_PALETTE_TO_GRAY))
         result |= opng_reduce_to_palette(png_ptr, info_ptr, reductions);
   }

   return result;
}
/*
 * Reduce the image (bit depth + color type + palette) without
 * losing any information. The palette (if applicable) and the
 * image data must be present, e.g., by calling png_set_rows(),
 * or by loading IDAT.
 * The parameter reductions indicates the intended reductions.
 * The function returns the successful reductions.
 */
png_uint_32 PNGAPI opng_reduce_image(png_structp png_ptr, png_infop info_ptr, png_uint_32 reductions)
{
   if (!opng_validate_image(png_ptr, info_ptr))
   {
      png_warning(png_ptr, "Image reduction requires the presence of all critical information");
      return OPNG_REDUCE_NONE;
   }

   int color_type = png_get_color_type(png_ptr, info_ptr);


   /* The reductions below must be applied in this particular order. */

  png_uint_32 result = 0;
     if ((color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
                   color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
                 && (reductions & OPNG_REDUCE_DIRTY_ALPHA)
                )
     {result |= opng_reduce_dirty_alpha(png_ptr, info_ptr);}


   /* Try to reduce the high bits and color/alpha channels. */
   result |= opng_reduce_bits(png_ptr, info_ptr, reductions);

   /* Try to reduce the palette image. */
   if (color_type == PNG_COLOR_TYPE_PALETTE &&
       (reductions &
        (OPNG_REDUCE_PALETTE_TO_GRAY |
         OPNG_REDUCE_PALETTE_FAST |
         OPNG_REDUCE_8_TO_4_2_1)))
      result |= opng_reduce_palette(png_ptr, info_ptr, reductions);

   /* Try to reduce RGB to palette or grayscale to palette. */
   if (((color_type & ~PNG_COLOR_MASK_ALPHA) == PNG_COLOR_TYPE_GRAY &&
        (reductions & OPNG_REDUCE_GRAY_TO_PALETTE)) ||
       ((color_type & ~PNG_COLOR_MASK_ALPHA) == PNG_COLOR_TYPE_RGB &&
        (reductions & OPNG_REDUCE_RGB_TO_PALETTE)))
   {
      if (!(result & OPNG_REDUCE_PALETTE_TO_GRAY))
         result |= opng_reduce_to_palette(png_ptr, info_ptr, reductions);
   }

   return result;
}