3dphotoshoot

diff libs/libpng/pngset.c @ 14:06dc8b9b4f89

added libimago, libjpeg and libpng
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Jun 2015 17:25:49 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libpng/pngset.c	Sun Jun 07 17:25:49 2015 +0300
     1.3 @@ -0,0 +1,1293 @@
     1.4 +
     1.5 +/* pngset.c - storage of image information into info struct
     1.6 + *
     1.7 + * Last changed in libpng 1.2.30 [August 15, 2008]
     1.8 + * For conditions of distribution and use, see copyright notice in png.h
     1.9 + * Copyright (c) 1998-2008 Glenn Randers-Pehrson
    1.10 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
    1.11 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
    1.12 + *
    1.13 + * The functions here are used during reads to store data from the file
    1.14 + * into the info struct, and during writes to store application data
    1.15 + * into the info struct for writing into the file.  This abstracts the
    1.16 + * info struct and allows us to change the structure in the future.
    1.17 + */
    1.18 +
    1.19 +#define PNG_INTERNAL
    1.20 +#include "png.h"
    1.21 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
    1.22 +
    1.23 +#if defined(PNG_bKGD_SUPPORTED)
    1.24 +void PNGAPI
    1.25 +png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
    1.26 +{
    1.27 +   png_debug1(1, "in %s storage function\n", "bKGD");
    1.28 +   if (png_ptr == NULL || info_ptr == NULL)
    1.29 +      return;
    1.30 +
    1.31 +   png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
    1.32 +   info_ptr->valid |= PNG_INFO_bKGD;
    1.33 +}
    1.34 +#endif
    1.35 +
    1.36 +#if defined(PNG_cHRM_SUPPORTED)
    1.37 +#ifdef PNG_FLOATING_POINT_SUPPORTED
    1.38 +void PNGAPI
    1.39 +png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
    1.40 +   double white_x, double white_y, double red_x, double red_y,
    1.41 +   double green_x, double green_y, double blue_x, double blue_y)
    1.42 +{
    1.43 +   png_debug1(1, "in %s storage function\n", "cHRM");
    1.44 +   if (png_ptr == NULL || info_ptr == NULL)
    1.45 +      return;
    1.46 +   if (!(white_x || white_y || red_x || red_y || green_x || green_y ||
    1.47 +       blue_x || blue_y))
    1.48 +   {
    1.49 +      png_warning(png_ptr,
    1.50 +        "Ignoring attempt to set all-zero chromaticity values");
    1.51 +      return;
    1.52 +   }
    1.53 +   if (white_x < 0.0 || white_y < 0.0 ||
    1.54 +         red_x < 0.0 ||   red_y < 0.0 ||
    1.55 +       green_x < 0.0 || green_y < 0.0 ||
    1.56 +        blue_x < 0.0 ||  blue_y < 0.0)
    1.57 +   {
    1.58 +      png_warning(png_ptr,
    1.59 +        "Ignoring attempt to set negative chromaticity value");
    1.60 +      return;
    1.61 +   }
    1.62 +   if (white_x > 21474.83 || white_y > 21474.83 ||
    1.63 +         red_x > 21474.83 ||   red_y > 21474.83 ||
    1.64 +       green_x > 21474.83 || green_y > 21474.83 ||
    1.65 +        blue_x > 21474.83 ||  blue_y > 21474.83)
    1.66 +   {
    1.67 +      png_warning(png_ptr,
    1.68 +        "Ignoring attempt to set chromaticity value exceeding 21474.83");
    1.69 +      return;
    1.70 +   }
    1.71 +
    1.72 +   info_ptr->x_white = (float)white_x;
    1.73 +   info_ptr->y_white = (float)white_y;
    1.74 +   info_ptr->x_red   = (float)red_x;
    1.75 +   info_ptr->y_red   = (float)red_y;
    1.76 +   info_ptr->x_green = (float)green_x;
    1.77 +   info_ptr->y_green = (float)green_y;
    1.78 +   info_ptr->x_blue  = (float)blue_x;
    1.79 +   info_ptr->y_blue  = (float)blue_y;
    1.80 +#ifdef PNG_FIXED_POINT_SUPPORTED
    1.81 +   info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
    1.82 +   info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
    1.83 +   info_ptr->int_x_red   = (png_fixed_point)(  red_x*100000.+0.5);
    1.84 +   info_ptr->int_y_red   = (png_fixed_point)(  red_y*100000.+0.5);
    1.85 +   info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
    1.86 +   info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
    1.87 +   info_ptr->int_x_blue  = (png_fixed_point)( blue_x*100000.+0.5);
    1.88 +   info_ptr->int_y_blue  = (png_fixed_point)( blue_y*100000.+0.5);
    1.89 +#endif
    1.90 +   info_ptr->valid |= PNG_INFO_cHRM;
    1.91 +}
    1.92 +#endif
    1.93 +#ifdef PNG_FIXED_POINT_SUPPORTED
    1.94 +void PNGAPI
    1.95 +png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
    1.96 +   png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
    1.97 +   png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
    1.98 +   png_fixed_point blue_x, png_fixed_point blue_y)
    1.99 +{
   1.100 +   png_debug1(1, "in %s storage function\n", "cHRM");
   1.101 +   if (png_ptr == NULL || info_ptr == NULL)
   1.102 +      return;
   1.103 +
   1.104 +   if (!(white_x || white_y || red_x || red_y || green_x || green_y ||
   1.105 +       blue_x || blue_y))
   1.106 +   {
   1.107 +      png_warning(png_ptr,
   1.108 +        "Ignoring attempt to set all-zero chromaticity values");
   1.109 +      return;
   1.110 +   }
   1.111 +   if (white_x < 0 || white_y < 0 ||
   1.112 +         red_x < 0 ||   red_y < 0 ||
   1.113 +       green_x < 0 || green_y < 0 ||
   1.114 +        blue_x < 0 ||  blue_y < 0)
   1.115 +   {
   1.116 +      png_warning(png_ptr,
   1.117 +        "Ignoring attempt to set negative chromaticity value");
   1.118 +      return;
   1.119 +   }
   1.120 +   if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
   1.121 +       white_y > (png_fixed_point) PNG_UINT_31_MAX ||
   1.122 +         red_x > (png_fixed_point) PNG_UINT_31_MAX ||
   1.123 +         red_y > (png_fixed_point) PNG_UINT_31_MAX ||
   1.124 +       green_x > (png_fixed_point) PNG_UINT_31_MAX ||
   1.125 +       green_y > (png_fixed_point) PNG_UINT_31_MAX ||
   1.126 +        blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
   1.127 +        blue_y > (png_fixed_point) PNG_UINT_31_MAX )
   1.128 +   {
   1.129 +      png_warning(png_ptr,
   1.130 +        "Ignoring attempt to set chromaticity value exceeding 21474.83");
   1.131 +      return;
   1.132 +   }
   1.133 +   info_ptr->int_x_white = white_x;
   1.134 +   info_ptr->int_y_white = white_y;
   1.135 +   info_ptr->int_x_red   = red_x;
   1.136 +   info_ptr->int_y_red   = red_y;
   1.137 +   info_ptr->int_x_green = green_x;
   1.138 +   info_ptr->int_y_green = green_y;
   1.139 +   info_ptr->int_x_blue  = blue_x;
   1.140 +   info_ptr->int_y_blue  = blue_y;
   1.141 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   1.142 +   info_ptr->x_white = (float)(white_x/100000.);
   1.143 +   info_ptr->y_white = (float)(white_y/100000.);
   1.144 +   info_ptr->x_red   = (float)(  red_x/100000.);
   1.145 +   info_ptr->y_red   = (float)(  red_y/100000.);
   1.146 +   info_ptr->x_green = (float)(green_x/100000.);
   1.147 +   info_ptr->y_green = (float)(green_y/100000.);
   1.148 +   info_ptr->x_blue  = (float)( blue_x/100000.);
   1.149 +   info_ptr->y_blue  = (float)( blue_y/100000.);
   1.150 +#endif
   1.151 +   info_ptr->valid |= PNG_INFO_cHRM;
   1.152 +}
   1.153 +#endif
   1.154 +#endif
   1.155 +
   1.156 +#if defined(PNG_gAMA_SUPPORTED)
   1.157 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   1.158 +void PNGAPI
   1.159 +png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
   1.160 +{
   1.161 +   double gamma;
   1.162 +   png_debug1(1, "in %s storage function\n", "gAMA");
   1.163 +   if (png_ptr == NULL || info_ptr == NULL)
   1.164 +      return;
   1.165 +
   1.166 +   /* Check for overflow */
   1.167 +   if (file_gamma > 21474.83)
   1.168 +   {
   1.169 +      png_warning(png_ptr, "Limiting gamma to 21474.83");
   1.170 +      gamma=21474.83;
   1.171 +   }
   1.172 +   else
   1.173 +      gamma = file_gamma;
   1.174 +   info_ptr->gamma = (float)gamma;
   1.175 +#ifdef PNG_FIXED_POINT_SUPPORTED
   1.176 +   info_ptr->int_gamma = (int)(gamma*100000.+.5);
   1.177 +#endif
   1.178 +   info_ptr->valid |= PNG_INFO_gAMA;
   1.179 +   if (gamma == 0.0)
   1.180 +      png_warning(png_ptr, "Setting gamma=0");
   1.181 +}
   1.182 +#endif
   1.183 +void PNGAPI
   1.184 +png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
   1.185 +   int_gamma)
   1.186 +{
   1.187 +   png_fixed_point gamma;
   1.188 +
   1.189 +   png_debug1(1, "in %s storage function\n", "gAMA");
   1.190 +   if (png_ptr == NULL || info_ptr == NULL)
   1.191 +      return;
   1.192 +
   1.193 +   if (int_gamma > (png_fixed_point) PNG_UINT_31_MAX)
   1.194 +   {
   1.195 +     png_warning(png_ptr, "Limiting gamma to 21474.83");
   1.196 +     gamma=PNG_UINT_31_MAX;
   1.197 +   }
   1.198 +   else
   1.199 +   {
   1.200 +     if (int_gamma < 0)
   1.201 +     {
   1.202 +       png_warning(png_ptr, "Setting negative gamma to zero");
   1.203 +       gamma = 0;
   1.204 +     }
   1.205 +     else
   1.206 +       gamma = int_gamma;
   1.207 +   }
   1.208 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   1.209 +   info_ptr->gamma = (float)(gamma/100000.);
   1.210 +#endif
   1.211 +#ifdef PNG_FIXED_POINT_SUPPORTED
   1.212 +   info_ptr->int_gamma = gamma;
   1.213 +#endif
   1.214 +   info_ptr->valid |= PNG_INFO_gAMA;
   1.215 +   if (gamma == 0)
   1.216 +      png_warning(png_ptr, "Setting gamma=0");
   1.217 +}
   1.218 +#endif
   1.219 +
   1.220 +#if defined(PNG_hIST_SUPPORTED)
   1.221 +void PNGAPI
   1.222 +png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
   1.223 +{
   1.224 +   int i;
   1.225 +
   1.226 +   png_debug1(1, "in %s storage function\n", "hIST");
   1.227 +   if (png_ptr == NULL || info_ptr == NULL)
   1.228 +      return;
   1.229 +   if (info_ptr->num_palette == 0 || info_ptr->num_palette
   1.230 +       > PNG_MAX_PALETTE_LENGTH)
   1.231 +   {
   1.232 +       png_warning(png_ptr,
   1.233 +          "Invalid palette size, hIST allocation skipped.");
   1.234 +       return;
   1.235 +   }
   1.236 +
   1.237 +#ifdef PNG_FREE_ME_SUPPORTED
   1.238 +   png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
   1.239 +#endif
   1.240 +   /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in version
   1.241 +      1.2.1 */
   1.242 +   png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
   1.243 +      (png_uint_32)(PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16)));
   1.244 +   if (png_ptr->hist == NULL)
   1.245 +     {
   1.246 +       png_warning(png_ptr, "Insufficient memory for hIST chunk data.");
   1.247 +       return;
   1.248 +     }
   1.249 +
   1.250 +   for (i = 0; i < info_ptr->num_palette; i++)
   1.251 +       png_ptr->hist[i] = hist[i];
   1.252 +   info_ptr->hist = png_ptr->hist;
   1.253 +   info_ptr->valid |= PNG_INFO_hIST;
   1.254 +
   1.255 +#ifdef PNG_FREE_ME_SUPPORTED
   1.256 +   info_ptr->free_me |= PNG_FREE_HIST;
   1.257 +#else
   1.258 +   png_ptr->flags |= PNG_FLAG_FREE_HIST;
   1.259 +#endif
   1.260 +}
   1.261 +#endif
   1.262 +
   1.263 +void PNGAPI
   1.264 +png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
   1.265 +   png_uint_32 width, png_uint_32 height, int bit_depth,
   1.266 +   int color_type, int interlace_type, int compression_type,
   1.267 +   int filter_type)
   1.268 +{
   1.269 +   png_debug1(1, "in %s storage function\n", "IHDR");
   1.270 +   if (png_ptr == NULL || info_ptr == NULL)
   1.271 +      return;
   1.272 +
   1.273 +   /* check for width and height valid values */
   1.274 +   if (width == 0 || height == 0)
   1.275 +      png_error(png_ptr, "Image width or height is zero in IHDR");
   1.276 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
   1.277 +   if (width > png_ptr->user_width_max || height > png_ptr->user_height_max)
   1.278 +      png_error(png_ptr, "image size exceeds user limits in IHDR");
   1.279 +#else
   1.280 +   if (width > PNG_USER_WIDTH_MAX || height > PNG_USER_HEIGHT_MAX)
   1.281 +      png_error(png_ptr, "image size exceeds user limits in IHDR");
   1.282 +#endif
   1.283 +   if (width > PNG_UINT_31_MAX || height > PNG_UINT_31_MAX)
   1.284 +      png_error(png_ptr, "Invalid image size in IHDR");
   1.285 +   if ( width > (PNG_UINT_32_MAX
   1.286 +                 >> 3)      /* 8-byte RGBA pixels */
   1.287 +                 - 64       /* bigrowbuf hack */
   1.288 +                 - 1        /* filter byte */
   1.289 +                 - 7*8      /* rounding of width to multiple of 8 pixels */
   1.290 +                 - 8)       /* extra max_pixel_depth pad */
   1.291 +      png_warning(png_ptr, "Width is too large for libpng to process pixels");
   1.292 +
   1.293 +   /* check other values */
   1.294 +   if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
   1.295 +      bit_depth != 8 && bit_depth != 16)
   1.296 +      png_error(png_ptr, "Invalid bit depth in IHDR");
   1.297 +
   1.298 +   if (color_type < 0 || color_type == 1 ||
   1.299 +      color_type == 5 || color_type > 6)
   1.300 +      png_error(png_ptr, "Invalid color type in IHDR");
   1.301 +
   1.302 +   if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
   1.303 +       ((color_type == PNG_COLOR_TYPE_RGB ||
   1.304 +         color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
   1.305 +         color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
   1.306 +      png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");
   1.307 +
   1.308 +   if (interlace_type >= PNG_INTERLACE_LAST)
   1.309 +      png_error(png_ptr, "Unknown interlace method in IHDR");
   1.310 +
   1.311 +   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
   1.312 +      png_error(png_ptr, "Unknown compression method in IHDR");
   1.313 +
   1.314 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
   1.315 +   /* Accept filter_method 64 (intrapixel differencing) only if
   1.316 +    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
   1.317 +    * 2. Libpng did not read a PNG signature (this filter_method is only
   1.318 +    *    used in PNG datastreams that are embedded in MNG datastreams) and
   1.319 +    * 3. The application called png_permit_mng_features with a mask that
   1.320 +    *    included PNG_FLAG_MNG_FILTER_64 and
   1.321 +    * 4. The filter_method is 64 and
   1.322 +    * 5. The color_type is RGB or RGBA
   1.323 +    */
   1.324 +   if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted)
   1.325 +      png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
   1.326 +   if (filter_type != PNG_FILTER_TYPE_BASE)
   1.327 +   {
   1.328 +     if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
   1.329 +        (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
   1.330 +        ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
   1.331 +        (color_type == PNG_COLOR_TYPE_RGB ||
   1.332 +         color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
   1.333 +        png_error(png_ptr, "Unknown filter method in IHDR");
   1.334 +     if (png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
   1.335 +        png_warning(png_ptr, "Invalid filter method in IHDR");
   1.336 +   }
   1.337 +#else
   1.338 +   if (filter_type != PNG_FILTER_TYPE_BASE)
   1.339 +      png_error(png_ptr, "Unknown filter method in IHDR");
   1.340 +#endif
   1.341 +
   1.342 +   info_ptr->width = width;
   1.343 +   info_ptr->height = height;
   1.344 +   info_ptr->bit_depth = (png_byte)bit_depth;
   1.345 +   info_ptr->color_type =(png_byte) color_type;
   1.346 +   info_ptr->compression_type = (png_byte)compression_type;
   1.347 +   info_ptr->filter_type = (png_byte)filter_type;
   1.348 +   info_ptr->interlace_type = (png_byte)interlace_type;
   1.349 +   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   1.350 +      info_ptr->channels = 1;
   1.351 +   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
   1.352 +      info_ptr->channels = 3;
   1.353 +   else
   1.354 +      info_ptr->channels = 1;
   1.355 +   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
   1.356 +      info_ptr->channels++;
   1.357 +   info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
   1.358 +
   1.359 +   /* check for potential overflow */
   1.360 +   if (width > (PNG_UINT_32_MAX
   1.361 +                 >> 3)      /* 8-byte RGBA pixels */
   1.362 +                 - 64       /* bigrowbuf hack */
   1.363 +                 - 1        /* filter byte */
   1.364 +                 - 7*8      /* rounding of width to multiple of 8 pixels */
   1.365 +                 - 8)       /* extra max_pixel_depth pad */
   1.366 +      info_ptr->rowbytes = (png_size_t)0;
   1.367 +   else
   1.368 +      info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
   1.369 +}
   1.370 +
   1.371 +#if defined(PNG_oFFs_SUPPORTED)
   1.372 +void PNGAPI
   1.373 +png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
   1.374 +   png_int_32 offset_x, png_int_32 offset_y, int unit_type)
   1.375 +{
   1.376 +   png_debug1(1, "in %s storage function\n", "oFFs");
   1.377 +   if (png_ptr == NULL || info_ptr == NULL)
   1.378 +      return;
   1.379 +
   1.380 +   info_ptr->x_offset = offset_x;
   1.381 +   info_ptr->y_offset = offset_y;
   1.382 +   info_ptr->offset_unit_type = (png_byte)unit_type;
   1.383 +   info_ptr->valid |= PNG_INFO_oFFs;
   1.384 +}
   1.385 +#endif
   1.386 +
   1.387 +#if defined(PNG_pCAL_SUPPORTED)
   1.388 +void PNGAPI
   1.389 +png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
   1.390 +   png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
   1.391 +   png_charp units, png_charpp params)
   1.392 +{
   1.393 +   png_uint_32 length;
   1.394 +   int i;
   1.395 +
   1.396 +   png_debug1(1, "in %s storage function\n", "pCAL");
   1.397 +   if (png_ptr == NULL || info_ptr == NULL)
   1.398 +      return;
   1.399 +
   1.400 +   length = png_strlen(purpose) + 1;
   1.401 +   png_debug1(3, "allocating purpose for info (%lu bytes)\n",
   1.402 +     (unsigned long)length);
   1.403 +   info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
   1.404 +   if (info_ptr->pcal_purpose == NULL)
   1.405 +   {
   1.406 +       png_warning(png_ptr, "Insufficient memory for pCAL purpose.");
   1.407 +      return;
   1.408 +   }
   1.409 +   png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);
   1.410 +
   1.411 +   png_debug(3, "storing X0, X1, type, and nparams in info\n");
   1.412 +   info_ptr->pcal_X0 = X0;
   1.413 +   info_ptr->pcal_X1 = X1;
   1.414 +   info_ptr->pcal_type = (png_byte)type;
   1.415 +   info_ptr->pcal_nparams = (png_byte)nparams;
   1.416 +
   1.417 +   length = png_strlen(units) + 1;
   1.418 +   png_debug1(3, "allocating units for info (%lu bytes)\n",
   1.419 +     (unsigned long)length);
   1.420 +   info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
   1.421 +   if (info_ptr->pcal_units == NULL)
   1.422 +   {
   1.423 +       png_warning(png_ptr, "Insufficient memory for pCAL units.");
   1.424 +      return;
   1.425 +   }
   1.426 +   png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);
   1.427 +
   1.428 +   info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
   1.429 +      (png_uint_32)((nparams + 1) * png_sizeof(png_charp)));
   1.430 +   if (info_ptr->pcal_params == NULL)
   1.431 +   {
   1.432 +       png_warning(png_ptr, "Insufficient memory for pCAL params.");
   1.433 +      return;
   1.434 +   }
   1.435 +
   1.436 +   info_ptr->pcal_params[nparams] = NULL;
   1.437 +
   1.438 +   for (i = 0; i < nparams; i++)
   1.439 +   {
   1.440 +      length = png_strlen(params[i]) + 1;
   1.441 +      png_debug2(3, "allocating parameter %d for info (%lu bytes)\n", i,
   1.442 +        (unsigned long)length);
   1.443 +      info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
   1.444 +      if (info_ptr->pcal_params[i] == NULL)
   1.445 +      {
   1.446 +          png_warning(png_ptr, "Insufficient memory for pCAL parameter.");
   1.447 +          return;
   1.448 +      }
   1.449 +      png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
   1.450 +   }
   1.451 +
   1.452 +   info_ptr->valid |= PNG_INFO_pCAL;
   1.453 +#ifdef PNG_FREE_ME_SUPPORTED
   1.454 +   info_ptr->free_me |= PNG_FREE_PCAL;
   1.455 +#endif
   1.456 +}
   1.457 +#endif
   1.458 +
   1.459 +#if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
   1.460 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   1.461 +void PNGAPI
   1.462 +png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
   1.463 +             int unit, double width, double height)
   1.464 +{
   1.465 +   png_debug1(1, "in %s storage function\n", "sCAL");
   1.466 +   if (png_ptr == NULL || info_ptr == NULL)
   1.467 +      return;
   1.468 +
   1.469 +   info_ptr->scal_unit = (png_byte)unit;
   1.470 +   info_ptr->scal_pixel_width = width;
   1.471 +   info_ptr->scal_pixel_height = height;
   1.472 +
   1.473 +   info_ptr->valid |= PNG_INFO_sCAL;
   1.474 +}
   1.475 +#else
   1.476 +#ifdef PNG_FIXED_POINT_SUPPORTED
   1.477 +void PNGAPI
   1.478 +png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
   1.479 +             int unit, png_charp swidth, png_charp sheight)
   1.480 +{
   1.481 +   png_uint_32 length;
   1.482 +
   1.483 +   png_debug1(1, "in %s storage function\n", "sCAL");
   1.484 +   if (png_ptr == NULL || info_ptr == NULL)
   1.485 +      return;
   1.486 +
   1.487 +   info_ptr->scal_unit = (png_byte)unit;
   1.488 +
   1.489 +   length = png_strlen(swidth) + 1;
   1.490 +   png_debug1(3, "allocating unit for info (%u bytes)\n",
   1.491 +      (unsigned int)length);
   1.492 +   info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
   1.493 +   if (info_ptr->scal_s_width == NULL)
   1.494 +   {
   1.495 +      png_warning(png_ptr,
   1.496 +       "Memory allocation failed while processing sCAL.");
   1.497 +      return;
   1.498 +   }
   1.499 +   png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length);
   1.500 +
   1.501 +   length = png_strlen(sheight) + 1;
   1.502 +   png_debug1(3, "allocating unit for info (%u bytes)\n",
   1.503 +      (unsigned int)length);
   1.504 +   info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
   1.505 +   if (info_ptr->scal_s_height == NULL)
   1.506 +   {
   1.507 +      png_free (png_ptr, info_ptr->scal_s_width);
   1.508 +      info_ptr->scal_s_width = NULL;
   1.509 +      png_warning(png_ptr,
   1.510 +       "Memory allocation failed while processing sCAL.");
   1.511 +      return;
   1.512 +   }
   1.513 +   png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length);
   1.514 +   info_ptr->valid |= PNG_INFO_sCAL;
   1.515 +#ifdef PNG_FREE_ME_SUPPORTED
   1.516 +   info_ptr->free_me |= PNG_FREE_SCAL;
   1.517 +#endif
   1.518 +}
   1.519 +#endif
   1.520 +#endif
   1.521 +#endif
   1.522 +
   1.523 +#if defined(PNG_pHYs_SUPPORTED)
   1.524 +void PNGAPI
   1.525 +png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
   1.526 +   png_uint_32 res_x, png_uint_32 res_y, int unit_type)
   1.527 +{
   1.528 +   png_debug1(1, "in %s storage function\n", "pHYs");
   1.529 +   if (png_ptr == NULL || info_ptr == NULL)
   1.530 +      return;
   1.531 +
   1.532 +   info_ptr->x_pixels_per_unit = res_x;
   1.533 +   info_ptr->y_pixels_per_unit = res_y;
   1.534 +   info_ptr->phys_unit_type = (png_byte)unit_type;
   1.535 +   info_ptr->valid |= PNG_INFO_pHYs;
   1.536 +}
   1.537 +#endif
   1.538 +
   1.539 +void PNGAPI
   1.540 +png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
   1.541 +   png_colorp palette, int num_palette)
   1.542 +{
   1.543 +
   1.544 +   png_debug1(1, "in %s storage function\n", "PLTE");
   1.545 +   if (png_ptr == NULL || info_ptr == NULL)
   1.546 +      return;
   1.547 +
   1.548 +   if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
   1.549 +     {
   1.550 +       if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
   1.551 +         png_error(png_ptr, "Invalid palette length");
   1.552 +       else
   1.553 +       {
   1.554 +         png_warning(png_ptr, "Invalid palette length");
   1.555 +         return;
   1.556 +       }
   1.557 +     }
   1.558 +
   1.559 +   /*
   1.560 +    * It may not actually be necessary to set png_ptr->palette here;
   1.561 +    * we do it for backward compatibility with the way the png_handle_tRNS
   1.562 +    * function used to do the allocation.
   1.563 +    */
   1.564 +#ifdef PNG_FREE_ME_SUPPORTED
   1.565 +   png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
   1.566 +#endif
   1.567 +
   1.568 +   /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
   1.569 +      of num_palette entries,
   1.570 +      in case of an invalid PNG file that has too-large sample values. */
   1.571 +   png_ptr->palette = (png_colorp)png_malloc(png_ptr,
   1.572 +      PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
   1.573 +   png_memset(png_ptr->palette, 0, PNG_MAX_PALETTE_LENGTH *
   1.574 +      png_sizeof(png_color));
   1.575 +   png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
   1.576 +   info_ptr->palette = png_ptr->palette;
   1.577 +   info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
   1.578 +
   1.579 +#ifdef PNG_FREE_ME_SUPPORTED
   1.580 +   info_ptr->free_me |= PNG_FREE_PLTE;
   1.581 +#else
   1.582 +   png_ptr->flags |= PNG_FLAG_FREE_PLTE;
   1.583 +#endif
   1.584 +
   1.585 +   info_ptr->valid |= PNG_INFO_PLTE;
   1.586 +}
   1.587 +
   1.588 +#if defined(PNG_sBIT_SUPPORTED)
   1.589 +void PNGAPI
   1.590 +png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
   1.591 +   png_color_8p sig_bit)
   1.592 +{
   1.593 +   png_debug1(1, "in %s storage function\n", "sBIT");
   1.594 +   if (png_ptr == NULL || info_ptr == NULL)
   1.595 +      return;
   1.596 +
   1.597 +   png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
   1.598 +   info_ptr->valid |= PNG_INFO_sBIT;
   1.599 +}
   1.600 +#endif
   1.601 +
   1.602 +#if defined(PNG_sRGB_SUPPORTED)
   1.603 +void PNGAPI
   1.604 +png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
   1.605 +{
   1.606 +   png_debug1(1, "in %s storage function\n", "sRGB");
   1.607 +   if (png_ptr == NULL || info_ptr == NULL)
   1.608 +      return;
   1.609 +
   1.610 +   info_ptr->srgb_intent = (png_byte)intent;
   1.611 +   info_ptr->valid |= PNG_INFO_sRGB;
   1.612 +}
   1.613 +
   1.614 +void PNGAPI
   1.615 +png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
   1.616 +   int intent)
   1.617 +{
   1.618 +#if defined(PNG_gAMA_SUPPORTED)
   1.619 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   1.620 +   float file_gamma;
   1.621 +#endif
   1.622 +#ifdef PNG_FIXED_POINT_SUPPORTED
   1.623 +   png_fixed_point int_file_gamma;
   1.624 +#endif
   1.625 +#endif
   1.626 +#if defined(PNG_cHRM_SUPPORTED)
   1.627 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   1.628 +   float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
   1.629 +#endif
   1.630 +#ifdef PNG_FIXED_POINT_SUPPORTED
   1.631 +   png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
   1.632 +      int_green_y, int_blue_x, int_blue_y;
   1.633 +#endif
   1.634 +#endif
   1.635 +   png_debug1(1, "in %s storage function\n", "sRGB_gAMA_and_cHRM");
   1.636 +   if (png_ptr == NULL || info_ptr == NULL)
   1.637 +      return;
   1.638 +
   1.639 +   png_set_sRGB(png_ptr, info_ptr, intent);
   1.640 +
   1.641 +#if defined(PNG_gAMA_SUPPORTED)
   1.642 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   1.643 +   file_gamma = (float).45455;
   1.644 +   png_set_gAMA(png_ptr, info_ptr, file_gamma);
   1.645 +#endif
   1.646 +#ifdef PNG_FIXED_POINT_SUPPORTED
   1.647 +   int_file_gamma = 45455L;
   1.648 +   png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
   1.649 +#endif
   1.650 +#endif
   1.651 +
   1.652 +#if defined(PNG_cHRM_SUPPORTED)
   1.653 +#ifdef PNG_FIXED_POINT_SUPPORTED
   1.654 +   int_white_x = 31270L;
   1.655 +   int_white_y = 32900L;
   1.656 +   int_red_x   = 64000L;
   1.657 +   int_red_y   = 33000L;
   1.658 +   int_green_x = 30000L;
   1.659 +   int_green_y = 60000L;
   1.660 +   int_blue_x  = 15000L;
   1.661 +   int_blue_y  =  6000L;
   1.662 +
   1.663 +   png_set_cHRM_fixed(png_ptr, info_ptr,
   1.664 +      int_white_x, int_white_y, int_red_x, int_red_y, int_green_x, int_green_y,
   1.665 +      int_blue_x, int_blue_y);
   1.666 +#endif
   1.667 +#ifdef PNG_FLOATING_POINT_SUPPORTED
   1.668 +   white_x = (float).3127;
   1.669 +   white_y = (float).3290;
   1.670 +   red_x   = (float).64;
   1.671 +   red_y   = (float).33;
   1.672 +   green_x = (float).30;
   1.673 +   green_y = (float).60;
   1.674 +   blue_x  = (float).15;
   1.675 +   blue_y  = (float).06;
   1.676 +
   1.677 +   png_set_cHRM(png_ptr, info_ptr,
   1.678 +      white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
   1.679 +#endif
   1.680 +#endif
   1.681 +}
   1.682 +#endif
   1.683 +
   1.684 +
   1.685 +#if defined(PNG_iCCP_SUPPORTED)
   1.686 +void PNGAPI
   1.687 +png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
   1.688 +             png_charp name, int compression_type,
   1.689 +             png_charp profile, png_uint_32 proflen)
   1.690 +{
   1.691 +   png_charp new_iccp_name;
   1.692 +   png_charp new_iccp_profile;
   1.693 +   png_uint_32 length;
   1.694 +
   1.695 +   png_debug1(1, "in %s storage function\n", "iCCP");
   1.696 +   if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
   1.697 +      return;
   1.698 +
   1.699 +   length = png_strlen(name)+1;
   1.700 +   new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
   1.701 +   if (new_iccp_name == NULL)
   1.702 +   {
   1.703 +      png_warning(png_ptr, "Insufficient memory to process iCCP chunk.");
   1.704 +      return;
   1.705 +   }
   1.706 +   png_memcpy(new_iccp_name, name, length);
   1.707 +   new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
   1.708 +   if (new_iccp_profile == NULL)
   1.709 +   {
   1.710 +      png_free (png_ptr, new_iccp_name);
   1.711 +      png_warning(png_ptr,
   1.712 +      "Insufficient memory to process iCCP profile.");
   1.713 +      return;
   1.714 +   }
   1.715 +   png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
   1.716 +
   1.717 +   png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
   1.718 +
   1.719 +   info_ptr->iccp_proflen = proflen;
   1.720 +   info_ptr->iccp_name = new_iccp_name;
   1.721 +   info_ptr->iccp_profile = new_iccp_profile;
   1.722 +   /* Compression is always zero but is here so the API and info structure
   1.723 +    * does not have to change if we introduce multiple compression types */
   1.724 +   info_ptr->iccp_compression = (png_byte)compression_type;
   1.725 +#ifdef PNG_FREE_ME_SUPPORTED
   1.726 +   info_ptr->free_me |= PNG_FREE_ICCP;
   1.727 +#endif
   1.728 +   info_ptr->valid |= PNG_INFO_iCCP;
   1.729 +}
   1.730 +#endif
   1.731 +
   1.732 +#if defined(PNG_TEXT_SUPPORTED)
   1.733 +void PNGAPI
   1.734 +png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
   1.735 +   int num_text)
   1.736 +{
   1.737 +   int ret;
   1.738 +   ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
   1.739 +   if (ret)
   1.740 +     png_error(png_ptr, "Insufficient memory to store text");
   1.741 +}
   1.742 +
   1.743 +int /* PRIVATE */
   1.744 +png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
   1.745 +   int num_text)
   1.746 +{
   1.747 +   int i;
   1.748 +
   1.749 +   png_debug1(1, "in %s storage function\n", (png_ptr->chunk_name[0] == '\0' ?
   1.750 +      "text" : (png_const_charp)png_ptr->chunk_name));
   1.751 +
   1.752 +   if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
   1.753 +      return(0);
   1.754 +
   1.755 +   /* Make sure we have enough space in the "text" array in info_struct
   1.756 +    * to hold all of the incoming text_ptr objects.
   1.757 +    */
   1.758 +   if (info_ptr->num_text + num_text > info_ptr->max_text)
   1.759 +   {
   1.760 +      if (info_ptr->text != NULL)
   1.761 +      {
   1.762 +         png_textp old_text;
   1.763 +         int old_max;
   1.764 +
   1.765 +         old_max = info_ptr->max_text;
   1.766 +         info_ptr->max_text = info_ptr->num_text + num_text + 8;
   1.767 +         old_text = info_ptr->text;
   1.768 +         info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
   1.769 +            (png_uint_32)(info_ptr->max_text * png_sizeof(png_text)));
   1.770 +         if (info_ptr->text == NULL)
   1.771 +           {
   1.772 +             png_free(png_ptr, old_text);
   1.773 +             return(1);
   1.774 +           }
   1.775 +         png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
   1.776 +            png_sizeof(png_text)));
   1.777 +         png_free(png_ptr, old_text);
   1.778 +      }
   1.779 +      else
   1.780 +      {
   1.781 +         info_ptr->max_text = num_text + 8;
   1.782 +         info_ptr->num_text = 0;
   1.783 +         info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
   1.784 +            (png_uint_32)(info_ptr->max_text * png_sizeof(png_text)));
   1.785 +         if (info_ptr->text == NULL)
   1.786 +           return(1);
   1.787 +#ifdef PNG_FREE_ME_SUPPORTED
   1.788 +         info_ptr->free_me |= PNG_FREE_TEXT;
   1.789 +#endif
   1.790 +      }
   1.791 +      png_debug1(3, "allocated %d entries for info_ptr->text\n",
   1.792 +         info_ptr->max_text);
   1.793 +   }
   1.794 +   for (i = 0; i < num_text; i++)
   1.795 +   {
   1.796 +      png_size_t text_length, key_len;
   1.797 +      png_size_t lang_len, lang_key_len;
   1.798 +      png_textp textp = &(info_ptr->text[info_ptr->num_text]);
   1.799 +
   1.800 +      if (text_ptr[i].key == NULL)
   1.801 +          continue;
   1.802 +
   1.803 +      key_len = png_strlen(text_ptr[i].key);
   1.804 +
   1.805 +      if (text_ptr[i].compression <= 0)
   1.806 +      {
   1.807 +        lang_len = 0;
   1.808 +        lang_key_len = 0;
   1.809 +      }
   1.810 +      else
   1.811 +#ifdef PNG_iTXt_SUPPORTED
   1.812 +      {
   1.813 +        /* set iTXt data */
   1.814 +        if (text_ptr[i].lang != NULL)
   1.815 +          lang_len = png_strlen(text_ptr[i].lang);
   1.816 +        else
   1.817 +          lang_len = 0;
   1.818 +        if (text_ptr[i].lang_key != NULL)
   1.819 +          lang_key_len = png_strlen(text_ptr[i].lang_key);
   1.820 +        else
   1.821 +          lang_key_len = 0;
   1.822 +      }
   1.823 +#else
   1.824 +      {
   1.825 +        png_warning(png_ptr, "iTXt chunk not supported.");
   1.826 +        continue;
   1.827 +      }
   1.828 +#endif
   1.829 +
   1.830 +      if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
   1.831 +      {
   1.832 +         text_length = 0;
   1.833 +#ifdef PNG_iTXt_SUPPORTED
   1.834 +         if (text_ptr[i].compression > 0)
   1.835 +            textp->compression = PNG_ITXT_COMPRESSION_NONE;
   1.836 +         else
   1.837 +#endif
   1.838 +            textp->compression = PNG_TEXT_COMPRESSION_NONE;
   1.839 +      }
   1.840 +      else
   1.841 +      {
   1.842 +         text_length = png_strlen(text_ptr[i].text);
   1.843 +         textp->compression = text_ptr[i].compression;
   1.844 +      }
   1.845 +
   1.846 +      textp->key = (png_charp)png_malloc_warn(png_ptr,
   1.847 +         (png_uint_32)
   1.848 +         (key_len + text_length + lang_len + lang_key_len + 4));
   1.849 +      if (textp->key == NULL)
   1.850 +        return(1);
   1.851 +      png_debug2(2, "Allocated %lu bytes at %x in png_set_text\n",
   1.852 +         (png_uint_32)
   1.853 +         (key_len + lang_len + lang_key_len + text_length + 4),
   1.854 +         (int)textp->key);
   1.855 +
   1.856 +      png_memcpy(textp->key, text_ptr[i].key,
   1.857 +         (png_size_t)(key_len));
   1.858 +      *(textp->key + key_len) = '\0';
   1.859 +#ifdef PNG_iTXt_SUPPORTED
   1.860 +      if (text_ptr[i].compression > 0)
   1.861 +      {
   1.862 +         textp->lang = textp->key + key_len + 1;
   1.863 +         png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
   1.864 +         *(textp->lang + lang_len) = '\0';
   1.865 +         textp->lang_key = textp->lang + lang_len + 1;
   1.866 +         png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
   1.867 +         *(textp->lang_key + lang_key_len) = '\0';
   1.868 +         textp->text = textp->lang_key + lang_key_len + 1;
   1.869 +      }
   1.870 +      else
   1.871 +#endif
   1.872 +      {
   1.873 +#ifdef PNG_iTXt_SUPPORTED
   1.874 +         textp->lang=NULL;
   1.875 +         textp->lang_key=NULL;
   1.876 +#endif
   1.877 +         textp->text = textp->key + key_len + 1;
   1.878 +      }
   1.879 +      if (text_length)
   1.880 +         png_memcpy(textp->text, text_ptr[i].text,
   1.881 +            (png_size_t)(text_length));
   1.882 +      *(textp->text + text_length) = '\0';
   1.883 +
   1.884 +#ifdef PNG_iTXt_SUPPORTED
   1.885 +      if (textp->compression > 0)
   1.886 +      {
   1.887 +         textp->text_length = 0;
   1.888 +         textp->itxt_length = text_length;
   1.889 +      }
   1.890 +      else
   1.891 +#endif
   1.892 +      {
   1.893 +         textp->text_length = text_length;
   1.894 +#ifdef PNG_iTXt_SUPPORTED
   1.895 +         textp->itxt_length = 0;
   1.896 +#endif
   1.897 +      }
   1.898 +      info_ptr->num_text++;
   1.899 +      png_debug1(3, "transferred text chunk %d\n", info_ptr->num_text);
   1.900 +   }
   1.901 +   return(0);
   1.902 +}
   1.903 +#endif
   1.904 +
   1.905 +#if defined(PNG_tIME_SUPPORTED)
   1.906 +void PNGAPI
   1.907 +png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
   1.908 +{
   1.909 +   png_debug1(1, "in %s storage function\n", "tIME");
   1.910 +   if (png_ptr == NULL || info_ptr == NULL ||
   1.911 +       (png_ptr->mode & PNG_WROTE_tIME))
   1.912 +      return;
   1.913 +
   1.914 +   png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
   1.915 +   info_ptr->valid |= PNG_INFO_tIME;
   1.916 +}
   1.917 +#endif
   1.918 +
   1.919 +#if defined(PNG_tRNS_SUPPORTED)
   1.920 +void PNGAPI
   1.921 +png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
   1.922 +   png_bytep trans, int num_trans, png_color_16p trans_values)
   1.923 +{
   1.924 +   png_debug1(1, "in %s storage function\n", "tRNS");
   1.925 +   if (png_ptr == NULL || info_ptr == NULL)
   1.926 +      return;
   1.927 +
   1.928 +   if (trans != NULL)
   1.929 +   {
   1.930 +       /*
   1.931 +        * It may not actually be necessary to set png_ptr->trans here;
   1.932 +        * we do it for backward compatibility with the way the png_handle_tRNS
   1.933 +        * function used to do the allocation.
   1.934 +        */
   1.935 +
   1.936 +#ifdef PNG_FREE_ME_SUPPORTED
   1.937 +       png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
   1.938 +#endif
   1.939 +
   1.940 +       /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
   1.941 +       png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr,
   1.942 +           (png_uint_32)PNG_MAX_PALETTE_LENGTH);
   1.943 +       if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
   1.944 +         png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans);
   1.945 +   }
   1.946 +
   1.947 +   if (trans_values != NULL)
   1.948 +   {
   1.949 +      int sample_max = (1 << info_ptr->bit_depth);
   1.950 +      if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
   1.951 +          (int)trans_values->gray > sample_max) ||
   1.952 +          (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
   1.953 +          ((int)trans_values->red > sample_max ||
   1.954 +          (int)trans_values->green > sample_max ||
   1.955 +          (int)trans_values->blue > sample_max)))
   1.956 +        png_warning(png_ptr,
   1.957 +           "tRNS chunk has out-of-range samples for bit_depth");
   1.958 +      png_memcpy(&(info_ptr->trans_values), trans_values,
   1.959 +         png_sizeof(png_color_16));
   1.960 +      if (num_trans == 0)
   1.961 +        num_trans = 1;
   1.962 +   }
   1.963 +
   1.964 +   info_ptr->num_trans = (png_uint_16)num_trans;
   1.965 +   if (num_trans != 0)
   1.966 +   {
   1.967 +      info_ptr->valid |= PNG_INFO_tRNS;
   1.968 +#ifdef PNG_FREE_ME_SUPPORTED
   1.969 +      info_ptr->free_me |= PNG_FREE_TRNS;
   1.970 +#else
   1.971 +      png_ptr->flags |= PNG_FLAG_FREE_TRNS;
   1.972 +#endif
   1.973 +   }
   1.974 +}
   1.975 +#endif
   1.976 +
   1.977 +#if defined(PNG_sPLT_SUPPORTED)
   1.978 +void PNGAPI
   1.979 +png_set_sPLT(png_structp png_ptr,
   1.980 +             png_infop info_ptr, png_sPLT_tp entries, int nentries)
   1.981 +/*
   1.982 + *  entries        - array of png_sPLT_t structures
   1.983 + *                   to be added to the list of palettes
   1.984 + *                   in the info structure.
   1.985 + *  nentries       - number of palette structures to be
   1.986 + *                   added.
   1.987 + */
   1.988 +{
   1.989 +    png_sPLT_tp np;
   1.990 +    int i;
   1.991 +
   1.992 +    if (png_ptr == NULL || info_ptr == NULL)
   1.993 +       return;
   1.994 +
   1.995 +    np = (png_sPLT_tp)png_malloc_warn(png_ptr,
   1.996 +        (info_ptr->splt_palettes_num + nentries) *
   1.997 +        (png_uint_32)png_sizeof(png_sPLT_t));
   1.998 +    if (np == NULL)
   1.999 +    {
  1.1000 +      png_warning(png_ptr, "No memory for sPLT palettes.");
  1.1001 +      return;
  1.1002 +    }
  1.1003 +
  1.1004 +    png_memcpy(np, info_ptr->splt_palettes,
  1.1005 +           info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
  1.1006 +    png_free(png_ptr, info_ptr->splt_palettes);
  1.1007 +    info_ptr->splt_palettes=NULL;
  1.1008 +
  1.1009 +    for (i = 0; i < nentries; i++)
  1.1010 +    {
  1.1011 +        png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
  1.1012 +        png_sPLT_tp from = entries + i;
  1.1013 +        png_uint_32 length;
  1.1014 +
  1.1015 +        length = png_strlen(from->name) + 1;
  1.1016 +        to->name = (png_charp)png_malloc_warn(png_ptr, length);
  1.1017 +        if (to->name == NULL)
  1.1018 +        {
  1.1019 +           png_warning(png_ptr,
  1.1020 +             "Out of memory while processing sPLT chunk");
  1.1021 +           continue;
  1.1022 +        }
  1.1023 +        png_memcpy(to->name, from->name, length);
  1.1024 +        to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
  1.1025 +            (png_uint_32)(from->nentries * png_sizeof(png_sPLT_entry)));
  1.1026 +        if (to->entries == NULL)
  1.1027 +        {
  1.1028 +           png_warning(png_ptr,
  1.1029 +             "Out of memory while processing sPLT chunk");
  1.1030 +           png_free(png_ptr, to->name);
  1.1031 +           to->name = NULL;
  1.1032 +           continue;
  1.1033 +        }
  1.1034 +        png_memcpy(to->entries, from->entries,
  1.1035 +            from->nentries * png_sizeof(png_sPLT_entry));
  1.1036 +        to->nentries = from->nentries;
  1.1037 +        to->depth = from->depth;
  1.1038 +    }
  1.1039 +
  1.1040 +    info_ptr->splt_palettes = np;
  1.1041 +    info_ptr->splt_palettes_num += nentries;
  1.1042 +    info_ptr->valid |= PNG_INFO_sPLT;
  1.1043 +#ifdef PNG_FREE_ME_SUPPORTED
  1.1044 +    info_ptr->free_me |= PNG_FREE_SPLT;
  1.1045 +#endif
  1.1046 +}
  1.1047 +#endif /* PNG_sPLT_SUPPORTED */
  1.1048 +
  1.1049 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  1.1050 +void PNGAPI
  1.1051 +png_set_unknown_chunks(png_structp png_ptr,
  1.1052 +   png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
  1.1053 +{
  1.1054 +    png_unknown_chunkp np;
  1.1055 +    int i;
  1.1056 +
  1.1057 +    if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
  1.1058 +        return;
  1.1059 +
  1.1060 +    np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
  1.1061 +        (png_uint_32)((info_ptr->unknown_chunks_num + num_unknowns) *
  1.1062 +        png_sizeof(png_unknown_chunk)));
  1.1063 +    if (np == NULL)
  1.1064 +    {
  1.1065 +       png_warning(png_ptr,
  1.1066 +          "Out of memory while processing unknown chunk.");
  1.1067 +       return;
  1.1068 +    }
  1.1069 +
  1.1070 +    png_memcpy(np, info_ptr->unknown_chunks,
  1.1071 +           info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
  1.1072 +    png_free(png_ptr, info_ptr->unknown_chunks);
  1.1073 +    info_ptr->unknown_chunks=NULL;
  1.1074 +
  1.1075 +    for (i = 0; i < num_unknowns; i++)
  1.1076 +    {
  1.1077 +       png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
  1.1078 +       png_unknown_chunkp from = unknowns + i;
  1.1079 +
  1.1080 +       png_memcpy((png_charp)to->name, 
  1.1081 +                  (png_charp)from->name, 
  1.1082 +                  png_sizeof(from->name));
  1.1083 +       to->name[png_sizeof(to->name)-1] = '\0';
  1.1084 +       to->size = from->size;
  1.1085 +       /* note our location in the read or write sequence */
  1.1086 +       to->location = (png_byte)(png_ptr->mode & 0xff);
  1.1087 +
  1.1088 +       if (from->size == 0)
  1.1089 +          to->data=NULL;
  1.1090 +       else
  1.1091 +       {
  1.1092 +          to->data = (png_bytep)png_malloc_warn(png_ptr,
  1.1093 +            (png_uint_32)from->size);
  1.1094 +          if (to->data == NULL)
  1.1095 +          {
  1.1096 +             png_warning(png_ptr,
  1.1097 +              "Out of memory while processing unknown chunk.");
  1.1098 +             to->size = 0;
  1.1099 +          }
  1.1100 +          else
  1.1101 +             png_memcpy(to->data, from->data, from->size);
  1.1102 +       }
  1.1103 +    }
  1.1104 +
  1.1105 +    info_ptr->unknown_chunks = np;
  1.1106 +    info_ptr->unknown_chunks_num += num_unknowns;
  1.1107 +#ifdef PNG_FREE_ME_SUPPORTED
  1.1108 +    info_ptr->free_me |= PNG_FREE_UNKN;
  1.1109 +#endif
  1.1110 +}
  1.1111 +void PNGAPI
  1.1112 +png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
  1.1113 +   int chunk, int location)
  1.1114 +{
  1.1115 +   if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
  1.1116 +         (int)info_ptr->unknown_chunks_num)
  1.1117 +      info_ptr->unknown_chunks[chunk].location = (png_byte)location;
  1.1118 +}
  1.1119 +#endif
  1.1120 +
  1.1121 +#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  1.1122 +#if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || \
  1.1123 +    defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
  1.1124 +void PNGAPI
  1.1125 +png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
  1.1126 +{
  1.1127 +   /* This function is deprecated in favor of png_permit_mng_features()
  1.1128 +      and will be removed from libpng-1.3.0 */
  1.1129 +   png_debug(1, "in png_permit_empty_plte, DEPRECATED.\n");
  1.1130 +   if (png_ptr == NULL)
  1.1131 +      return;
  1.1132 +   png_ptr->mng_features_permitted = (png_byte)
  1.1133 +     ((png_ptr->mng_features_permitted & (~PNG_FLAG_MNG_EMPTY_PLTE)) |
  1.1134 +     ((empty_plte_permitted & PNG_FLAG_MNG_EMPTY_PLTE)));
  1.1135 +}
  1.1136 +#endif
  1.1137 +#endif
  1.1138 +
  1.1139 +#if defined(PNG_MNG_FEATURES_SUPPORTED)
  1.1140 +png_uint_32 PNGAPI
  1.1141 +png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
  1.1142 +{
  1.1143 +   png_debug(1, "in png_permit_mng_features\n");
  1.1144 +   if (png_ptr == NULL)
  1.1145 +      return (png_uint_32)0;
  1.1146 +   png_ptr->mng_features_permitted =
  1.1147 +     (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
  1.1148 +   return (png_uint_32)png_ptr->mng_features_permitted;
  1.1149 +}
  1.1150 +#endif
  1.1151 +
  1.1152 +#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  1.1153 +void PNGAPI
  1.1154 +png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
  1.1155 +   chunk_list, int num_chunks)
  1.1156 +{
  1.1157 +    png_bytep new_list, p;
  1.1158 +    int i, old_num_chunks;
  1.1159 +    if (png_ptr == NULL)
  1.1160 +       return;
  1.1161 +    if (num_chunks == 0)
  1.1162 +    {
  1.1163 +      if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
  1.1164 +        png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
  1.1165 +      else
  1.1166 +        png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
  1.1167 +
  1.1168 +      if (keep == PNG_HANDLE_CHUNK_ALWAYS)
  1.1169 +        png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
  1.1170 +      else
  1.1171 +        png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
  1.1172 +      return;
  1.1173 +    }
  1.1174 +    if (chunk_list == NULL)
  1.1175 +      return;
  1.1176 +    old_num_chunks = png_ptr->num_chunk_list;
  1.1177 +    new_list=(png_bytep)png_malloc(png_ptr,
  1.1178 +       (png_uint_32)
  1.1179 +       (5*(num_chunks + old_num_chunks)));
  1.1180 +    if (png_ptr->chunk_list != NULL)
  1.1181 +    {
  1.1182 +       png_memcpy(new_list, png_ptr->chunk_list,
  1.1183 +          (png_size_t)(5*old_num_chunks));
  1.1184 +       png_free(png_ptr, png_ptr->chunk_list);
  1.1185 +       png_ptr->chunk_list=NULL;
  1.1186 +    }
  1.1187 +    png_memcpy(new_list + 5*old_num_chunks, chunk_list,
  1.1188 +       (png_size_t)(5*num_chunks));
  1.1189 +    for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
  1.1190 +       *p=(png_byte)keep;
  1.1191 +    png_ptr->num_chunk_list = old_num_chunks + num_chunks;
  1.1192 +    png_ptr->chunk_list = new_list;
  1.1193 +#ifdef PNG_FREE_ME_SUPPORTED
  1.1194 +    png_ptr->free_me |= PNG_FREE_LIST;
  1.1195 +#endif
  1.1196 +}
  1.1197 +#endif
  1.1198 +
  1.1199 +#if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  1.1200 +void PNGAPI
  1.1201 +png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
  1.1202 +   png_user_chunk_ptr read_user_chunk_fn)
  1.1203 +{
  1.1204 +   png_debug(1, "in png_set_read_user_chunk_fn\n");
  1.1205 +   if (png_ptr == NULL)
  1.1206 +      return;
  1.1207 +   png_ptr->read_user_chunk_fn = read_user_chunk_fn;
  1.1208 +   png_ptr->user_chunk_ptr = user_chunk_ptr;
  1.1209 +}
  1.1210 +#endif
  1.1211 +
  1.1212 +#if defined(PNG_INFO_IMAGE_SUPPORTED)
  1.1213 +void PNGAPI
  1.1214 +png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
  1.1215 +{
  1.1216 +   png_debug1(1, "in %s storage function\n", "rows");
  1.1217 +
  1.1218 +   if (png_ptr == NULL || info_ptr == NULL)
  1.1219 +      return;
  1.1220 +
  1.1221 +   if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
  1.1222 +      png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  1.1223 +   info_ptr->row_pointers = row_pointers;
  1.1224 +   if (row_pointers)
  1.1225 +      info_ptr->valid |= PNG_INFO_IDAT;
  1.1226 +}
  1.1227 +#endif
  1.1228 +
  1.1229 +#ifdef PNG_WRITE_SUPPORTED
  1.1230 +void PNGAPI
  1.1231 +png_set_compression_buffer_size(png_structp png_ptr,
  1.1232 +    png_uint_32 size)
  1.1233 +{
  1.1234 +    if (png_ptr == NULL)
  1.1235 +       return;
  1.1236 +    png_free(png_ptr, png_ptr->zbuf);
  1.1237 +    png_ptr->zbuf_size = (png_size_t)size;
  1.1238 +    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
  1.1239 +    png_ptr->zstream.next_out = png_ptr->zbuf;
  1.1240 +    png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  1.1241 +}
  1.1242 +#endif
  1.1243 +
  1.1244 +void PNGAPI
  1.1245 +png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
  1.1246 +{
  1.1247 +   if (png_ptr && info_ptr)
  1.1248 +      info_ptr->valid &= ~mask;
  1.1249 +}
  1.1250 +
  1.1251 +
  1.1252 +#ifndef PNG_1_0_X
  1.1253 +#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
  1.1254 +/* function was added to libpng 1.2.0 and should always exist by default */
  1.1255 +void PNGAPI
  1.1256 +png_set_asm_flags (png_structp png_ptr, png_uint_32 asm_flags)
  1.1257 +{
  1.1258 +/* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
  1.1259 +    if (png_ptr != NULL)
  1.1260 +    png_ptr->asm_flags = 0;
  1.1261 +    asm_flags = asm_flags; /* Quiet the compiler */
  1.1262 +}
  1.1263 +
  1.1264 +/* this function was added to libpng 1.2.0 */
  1.1265 +void PNGAPI
  1.1266 +png_set_mmx_thresholds (png_structp png_ptr,
  1.1267 +                        png_byte mmx_bitdepth_threshold,
  1.1268 +                        png_uint_32 mmx_rowbytes_threshold)
  1.1269 +{
  1.1270 +/* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
  1.1271 +    if (png_ptr == NULL)
  1.1272 +       return;
  1.1273 +    /* Quiet the compiler */
  1.1274 +    mmx_bitdepth_threshold = mmx_bitdepth_threshold;
  1.1275 +    mmx_rowbytes_threshold = mmx_rowbytes_threshold;
  1.1276 +}
  1.1277 +#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
  1.1278 +
  1.1279 +#ifdef PNG_SET_USER_LIMITS_SUPPORTED
  1.1280 +/* this function was added to libpng 1.2.6 */
  1.1281 +void PNGAPI
  1.1282 +png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
  1.1283 +    png_uint_32 user_height_max)
  1.1284 +{
  1.1285 +    /* Images with dimensions larger than these limits will be
  1.1286 +     * rejected by png_set_IHDR().  To accept any PNG datastream
  1.1287 +     * regardless of dimensions, set both limits to 0x7ffffffL.
  1.1288 +     */
  1.1289 +    if (png_ptr == NULL) return;
  1.1290 +    png_ptr->user_width_max = user_width_max;
  1.1291 +    png_ptr->user_height_max = user_height_max;
  1.1292 +}
  1.1293 +#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
  1.1294 +
  1.1295 +#endif /* ?PNG_1_0_X */
  1.1296 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */