pngset.c
上传用户:sesekoo
上传日期:2020-07-18
资源大小:21543k
文件大小:40k
源码类别:

界面编程

开发平台:

Visual C++

  1. /* pngset.c - storage of image information into info struct
  2.  *
  3.  * Last changed in libpng 1.2.34 [December 18, 2008]
  4.  * For conditions of distribution and use, see copyright notice in png.h
  5.  * Copyright (c) 1998-2008 Glenn Randers-Pehrson
  6.  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  7.  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  8.  *
  9.  * The functions here are used during reads to store data from the file
  10.  * into the info struct, and during writes to store application data
  11.  * into the info struct for writing into the file.  This abstracts the
  12.  * info struct and allows us to change the structure in the future.
  13.  */
  14. #define PNG_INTERNAL
  15. #include "png.h"
  16. #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  17. #if defined(PNG_bKGD_SUPPORTED)
  18. void PNGAPI
  19. png_set_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p background)
  20. {
  21.    png_debug1(1, "in %s storage function", "bKGD");
  22.    if (png_ptr == NULL || info_ptr == NULL)
  23.       return;
  24.    png_memcpy(&(info_ptr->background), background, png_sizeof(png_color_16));
  25.    info_ptr->valid |= PNG_INFO_bKGD;
  26. }
  27. #endif
  28. #if defined(PNG_cHRM_SUPPORTED)
  29. #ifdef PNG_FLOATING_POINT_SUPPORTED
  30. void PNGAPI
  31. png_set_cHRM(png_structp png_ptr, png_infop info_ptr,
  32.    double white_x, double white_y, double red_x, double red_y,
  33.    double green_x, double green_y, double blue_x, double blue_y)
  34. {
  35.    png_debug1(1, "in %s storage function", "cHRM");
  36.    if (png_ptr == NULL || info_ptr == NULL)
  37.       return;
  38.    info_ptr->x_white = (float)white_x;
  39.    info_ptr->y_white = (float)white_y;
  40.    info_ptr->x_red   = (float)red_x;
  41.    info_ptr->y_red   = (float)red_y;
  42.    info_ptr->x_green = (float)green_x;
  43.    info_ptr->y_green = (float)green_y;
  44.    info_ptr->x_blue  = (float)blue_x;
  45.    info_ptr->y_blue  = (float)blue_y;
  46. #ifdef PNG_FIXED_POINT_SUPPORTED
  47.    info_ptr->int_x_white = (png_fixed_point)(white_x*100000.+0.5);
  48.    info_ptr->int_y_white = (png_fixed_point)(white_y*100000.+0.5);
  49.    info_ptr->int_x_red   = (png_fixed_point)(  red_x*100000.+0.5);
  50.    info_ptr->int_y_red   = (png_fixed_point)(  red_y*100000.+0.5);
  51.    info_ptr->int_x_green = (png_fixed_point)(green_x*100000.+0.5);
  52.    info_ptr->int_y_green = (png_fixed_point)(green_y*100000.+0.5);
  53.    info_ptr->int_x_blue  = (png_fixed_point)( blue_x*100000.+0.5);
  54.    info_ptr->int_y_blue  = (png_fixed_point)( blue_y*100000.+0.5);
  55. #endif
  56.    info_ptr->valid |= PNG_INFO_cHRM;
  57. }
  58. #endif
  59. #ifdef PNG_FIXED_POINT_SUPPORTED
  60. void PNGAPI
  61. png_set_cHRM_fixed(png_structp png_ptr, png_infop info_ptr,
  62.    png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
  63.    png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
  64.    png_fixed_point blue_x, png_fixed_point blue_y)
  65. {
  66.    png_debug1(1, "in %s storage function", "cHRM fixed");
  67.    if (png_ptr == NULL || info_ptr == NULL)
  68.       return;
  69. #if !defined(PNG_NO_CHECK_cHRM)
  70.    if (png_check_cHRM_fixed(png_ptr,
  71.       white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y))
  72. #endif
  73.    {
  74.      info_ptr->int_x_white = white_x;
  75.      info_ptr->int_y_white = white_y;
  76.      info_ptr->int_x_red   = red_x;
  77.      info_ptr->int_y_red   = red_y;
  78.      info_ptr->int_x_green = green_x;
  79.      info_ptr->int_y_green = green_y;
  80.      info_ptr->int_x_blue  = blue_x;
  81.      info_ptr->int_y_blue  = blue_y;
  82. #ifdef PNG_FLOATING_POINT_SUPPORTED
  83.      info_ptr->x_white = (float)(white_x/100000.);
  84.      info_ptr->y_white = (float)(white_y/100000.);
  85.      info_ptr->x_red   = (float)(  red_x/100000.);
  86.      info_ptr->y_red   = (float)(  red_y/100000.);
  87.      info_ptr->x_green = (float)(green_x/100000.);
  88.      info_ptr->y_green = (float)(green_y/100000.);
  89.      info_ptr->x_blue  = (float)( blue_x/100000.);
  90.      info_ptr->y_blue  = (float)( blue_y/100000.);
  91. #endif
  92.      info_ptr->valid |= PNG_INFO_cHRM;
  93.    }
  94. }
  95. #endif /* PNG_FIXED_POINT_SUPPORTED */
  96. #endif /* PNG_cHRM_SUPPORTED */
  97. #if defined(PNG_gAMA_SUPPORTED)
  98. #ifdef PNG_FLOATING_POINT_SUPPORTED
  99. void PNGAPI
  100. png_set_gAMA(png_structp png_ptr, png_infop info_ptr, double file_gamma)
  101. {
  102.    double gamma;
  103.    png_debug1(1, "in %s storage function", "gAMA");
  104.    if (png_ptr == NULL || info_ptr == NULL)
  105.       return;
  106.    /* Check for overflow */
  107.    if (file_gamma > 21474.83)
  108.    {
  109.       png_warning(png_ptr, "Limiting gamma to 21474.83");
  110.       gamma=21474.83;
  111.    }
  112.    else
  113.       gamma = file_gamma;
  114.    info_ptr->gamma = (float)gamma;
  115. #ifdef PNG_FIXED_POINT_SUPPORTED
  116.    info_ptr->int_gamma = (int)(gamma*100000.+.5);
  117. #endif
  118.    info_ptr->valid |= PNG_INFO_gAMA;
  119.    if (gamma == 0.0)
  120.       png_warning(png_ptr, "Setting gamma=0");
  121. }
  122. #endif
  123. void PNGAPI
  124. png_set_gAMA_fixed(png_structp png_ptr, png_infop info_ptr, png_fixed_point
  125.    int_gamma)
  126. {
  127.    png_fixed_point gamma;
  128.    png_debug1(1, "in %s storage function", "gAMA");
  129.    if (png_ptr == NULL || info_ptr == NULL)
  130.       return;
  131.    if (int_gamma > (png_fixed_point) PNG_UINT_31_MAX)
  132.    {
  133.      png_warning(png_ptr, "Limiting gamma to 21474.83");
  134.      gamma=PNG_UINT_31_MAX;
  135.    }
  136.    else
  137.    {
  138.      if (int_gamma < 0)
  139.      {
  140.        png_warning(png_ptr, "Setting negative gamma to zero");
  141.        gamma = 0;
  142.      }
  143.      else
  144.        gamma = int_gamma;
  145.    }
  146. #ifdef PNG_FLOATING_POINT_SUPPORTED
  147.    info_ptr->gamma = (float)(gamma/100000.);
  148. #endif
  149. #ifdef PNG_FIXED_POINT_SUPPORTED
  150.    info_ptr->int_gamma = gamma;
  151. #endif
  152.    info_ptr->valid |= PNG_INFO_gAMA;
  153.    if (gamma == 0)
  154.       png_warning(png_ptr, "Setting gamma=0");
  155. }
  156. #endif
  157. #if defined(PNG_hIST_SUPPORTED)
  158. void PNGAPI
  159. png_set_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)
  160. {
  161.    int i;
  162.    png_debug1(1, "in %s storage function", "hIST");
  163.    if (png_ptr == NULL || info_ptr == NULL)
  164.       return;
  165.    if (info_ptr->num_palette == 0 || info_ptr->num_palette
  166.        > PNG_MAX_PALETTE_LENGTH)
  167.    {
  168.        png_warning(png_ptr,
  169.           "Invalid palette size, hIST allocation skipped.");
  170.        return;
  171.    }
  172. #ifdef PNG_FREE_ME_SUPPORTED
  173.    png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
  174. #endif
  175.    /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in version
  176.       1.2.1 */
  177.    png_ptr->hist = (png_uint_16p)png_malloc_warn(png_ptr,
  178.       (png_uint_32)(PNG_MAX_PALETTE_LENGTH * png_sizeof(png_uint_16)));
  179.    if (png_ptr->hist == NULL)
  180.      {
  181.        png_warning(png_ptr, "Insufficient memory for hIST chunk data.");
  182.        return;
  183.      }
  184.    for (i = 0; i < info_ptr->num_palette; i++)
  185.        png_ptr->hist[i] = hist[i];
  186.    info_ptr->hist = png_ptr->hist;
  187.    info_ptr->valid |= PNG_INFO_hIST;
  188. #ifdef PNG_FREE_ME_SUPPORTED
  189.    info_ptr->free_me |= PNG_FREE_HIST;
  190. #else
  191.    png_ptr->flags |= PNG_FLAG_FREE_HIST;
  192. #endif
  193. }
  194. #endif
  195. void PNGAPI
  196. png_set_IHDR(png_structp png_ptr, png_infop info_ptr,
  197.    png_uint_32 width, png_uint_32 height, int bit_depth,
  198.    int color_type, int interlace_type, int compression_type,
  199.    int filter_type)
  200. {
  201.    png_debug1(1, "in %s storage function", "IHDR");
  202.    if (png_ptr == NULL || info_ptr == NULL)
  203.       return;
  204.    /* check for width and height valid values */
  205.    if (width == 0 || height == 0)
  206.       png_error(png_ptr, "Image width or height is zero in IHDR");
  207. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  208.    if (width > png_ptr->user_width_max || height > png_ptr->user_height_max)
  209.       png_error(png_ptr, "image size exceeds user limits in IHDR");
  210. #else
  211.    if (width > PNG_USER_WIDTH_MAX || height > PNG_USER_HEIGHT_MAX)
  212.       png_error(png_ptr, "image size exceeds user limits in IHDR");
  213. #endif
  214.    if (width > PNG_UINT_31_MAX || height > PNG_UINT_31_MAX)
  215.       png_error(png_ptr, "Invalid image size in IHDR");
  216.    if ( width > (PNG_UINT_32_MAX
  217.                  >> 3)      /* 8-byte RGBA pixels */
  218.                  - 64       /* bigrowbuf hack */
  219.                  - 1        /* filter byte */
  220.                  - 7*8      /* rounding of width to multiple of 8 pixels */
  221.                  - 8)       /* extra max_pixel_depth pad */
  222.       png_warning(png_ptr, "Width is too large for libpng to process pixels");
  223.    /* check other values */
  224.    if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
  225.       bit_depth != 8 && bit_depth != 16)
  226.       png_error(png_ptr, "Invalid bit depth in IHDR");
  227.    if (color_type < 0 || color_type == 1 ||
  228.       color_type == 5 || color_type > 6)
  229.       png_error(png_ptr, "Invalid color type in IHDR");
  230.    if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
  231.        ((color_type == PNG_COLOR_TYPE_RGB ||
  232.          color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
  233.          color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
  234.       png_error(png_ptr, "Invalid color type/bit depth combination in IHDR");
  235.    if (interlace_type >= PNG_INTERLACE_LAST)
  236.       png_error(png_ptr, "Unknown interlace method in IHDR");
  237.    if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  238.       png_error(png_ptr, "Unknown compression method in IHDR");
  239. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  240.    /* Accept filter_method 64 (intrapixel differencing) only if
  241.     * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  242.     * 2. Libpng did not read a PNG signature (this filter_method is only
  243.     *    used in PNG datastreams that are embedded in MNG datastreams) and
  244.     * 3. The application called png_permit_mng_features with a mask that
  245.     *    included PNG_FLAG_MNG_FILTER_64 and
  246.     * 4. The filter_method is 64 and
  247.     * 5. The color_type is RGB or RGBA
  248.     */
  249.    if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&png_ptr->mng_features_permitted)
  250.       png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
  251.    if (filter_type != PNG_FILTER_TYPE_BASE)
  252.    {
  253.      if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  254.         (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
  255.         ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
  256.         (color_type == PNG_COLOR_TYPE_RGB ||
  257.          color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
  258.         png_error(png_ptr, "Unknown filter method in IHDR");
  259.      if (png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)
  260.         png_warning(png_ptr, "Invalid filter method in IHDR");
  261.    }
  262. #else
  263.    if (filter_type != PNG_FILTER_TYPE_BASE)
  264.       png_error(png_ptr, "Unknown filter method in IHDR");
  265. #endif
  266.    info_ptr->width = width;
  267.    info_ptr->height = height;
  268.    info_ptr->bit_depth = (png_byte)bit_depth;
  269.    info_ptr->color_type =(png_byte) color_type;
  270.    info_ptr->compression_type = (png_byte)compression_type;
  271.    info_ptr->filter_type = (png_byte)filter_type;
  272.    info_ptr->interlace_type = (png_byte)interlace_type;
  273.    if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  274.       info_ptr->channels = 1;
  275.    else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  276.       info_ptr->channels = 3;
  277.    else
  278.       info_ptr->channels = 1;
  279.    if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  280.       info_ptr->channels++;
  281.    info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
  282.    /* check for potential overflow */
  283.    if (width > (PNG_UINT_32_MAX
  284.                  >> 3)      /* 8-byte RGBA pixels */
  285.                  - 64       /* bigrowbuf hack */
  286.                  - 1        /* filter byte */
  287.                  - 7*8      /* rounding of width to multiple of 8 pixels */
  288.                  - 8)       /* extra max_pixel_depth pad */
  289.       info_ptr->rowbytes = (png_size_t)0;
  290.    else
  291.       info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
  292. }
  293. #if defined(PNG_oFFs_SUPPORTED)
  294. void PNGAPI
  295. png_set_oFFs(png_structp png_ptr, png_infop info_ptr,
  296.    png_int_32 offset_x, png_int_32 offset_y, int unit_type)
  297. {
  298.    png_debug1(1, "in %s storage function", "oFFs");
  299.    if (png_ptr == NULL || info_ptr == NULL)
  300.       return;
  301.    info_ptr->x_offset = offset_x;
  302.    info_ptr->y_offset = offset_y;
  303.    info_ptr->offset_unit_type = (png_byte)unit_type;
  304.    info_ptr->valid |= PNG_INFO_oFFs;
  305. }
  306. #endif
  307. #if defined(PNG_pCAL_SUPPORTED)
  308. void PNGAPI
  309. png_set_pCAL(png_structp png_ptr, png_infop info_ptr,
  310.    png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams,
  311.    png_charp units, png_charpp params)
  312. {
  313.    png_uint_32 length;
  314.    int i;
  315.    png_debug1(1, "in %s storage function", "pCAL");
  316.    if (png_ptr == NULL || info_ptr == NULL)
  317.       return;
  318.    length = (png_uint_32)png_strlen(purpose) + 1;
  319.    png_debug1(3, "allocating purpose for info (%lu bytes)",
  320.      (unsigned long)length);
  321.    info_ptr->pcal_purpose = (png_charp)png_malloc_warn(png_ptr, length);
  322.    if (info_ptr->pcal_purpose == NULL)
  323.    {
  324.        png_warning(png_ptr, "Insufficient memory for pCAL purpose.");
  325.       return;
  326.    }
  327.    png_memcpy(info_ptr->pcal_purpose, purpose, (png_size_t)length);
  328.    png_debug(3, "storing X0, X1, type, and nparams in info");
  329.    info_ptr->pcal_X0 = X0;
  330.    info_ptr->pcal_X1 = X1;
  331.    info_ptr->pcal_type = (png_byte)type;
  332.    info_ptr->pcal_nparams = (png_byte)nparams;
  333.    length = (png_uint_32)png_strlen(units) + 1;
  334.    png_debug1(3, "allocating units for info (%lu bytes)",
  335.      (unsigned long)length);
  336.    info_ptr->pcal_units = (png_charp)png_malloc_warn(png_ptr, length);
  337.    if (info_ptr->pcal_units == NULL)
  338.    {
  339.        png_warning(png_ptr, "Insufficient memory for pCAL units.");
  340.       return;
  341.    }
  342.    png_memcpy(info_ptr->pcal_units, units, (png_size_t)length);
  343.    info_ptr->pcal_params = (png_charpp)png_malloc_warn(png_ptr,
  344.       (png_uint_32)((nparams + 1) * png_sizeof(png_charp)));
  345.    if (info_ptr->pcal_params == NULL)
  346.    {
  347.        png_warning(png_ptr, "Insufficient memory for pCAL params.");
  348.       return;
  349.    }
  350.    info_ptr->pcal_params[nparams] = NULL;
  351.    for (i = 0; i < nparams; i++)
  352.    {
  353.       length = (png_uint_32)png_strlen(params[i]) + 1;
  354.       png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
  355.         (unsigned long)length);
  356.       info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
  357.       if (info_ptr->pcal_params[i] == NULL)
  358.       {
  359.           png_warning(png_ptr, "Insufficient memory for pCAL parameter.");
  360.           return;
  361.       }
  362.       png_memcpy(info_ptr->pcal_params[i], params[i], (png_size_t)length);
  363.    }
  364.    info_ptr->valid |= PNG_INFO_pCAL;
  365. #ifdef PNG_FREE_ME_SUPPORTED
  366.    info_ptr->free_me |= PNG_FREE_PCAL;
  367. #endif
  368. }
  369. #endif
  370. #if defined(PNG_READ_sCAL_SUPPORTED) || defined(PNG_WRITE_sCAL_SUPPORTED)
  371. #ifdef PNG_FLOATING_POINT_SUPPORTED
  372. void PNGAPI
  373. png_set_sCAL(png_structp png_ptr, png_infop info_ptr,
  374.              int unit, double width, double height)
  375. {
  376.    png_debug1(1, "in %s storage function", "sCAL");
  377.    if (png_ptr == NULL || info_ptr == NULL)
  378.       return;
  379.    info_ptr->scal_unit = (png_byte)unit;
  380.    info_ptr->scal_pixel_width = width;
  381.    info_ptr->scal_pixel_height = height;
  382.    info_ptr->valid |= PNG_INFO_sCAL;
  383. }
  384. #else
  385. #ifdef PNG_FIXED_POINT_SUPPORTED
  386. void PNGAPI
  387. png_set_sCAL_s(png_structp png_ptr, png_infop info_ptr,
  388.              int unit, png_charp swidth, png_charp sheight)
  389. {
  390.    png_uint_32 length;
  391.    png_debug1(1, "in %s storage function", "sCAL");
  392.    if (png_ptr == NULL || info_ptr == NULL)
  393.       return;
  394.    info_ptr->scal_unit = (png_byte)unit;
  395.    length = png_strlen(swidth) + 1;
  396.    png_debug1(3, "allocating unit for info (%u bytes)",
  397.       (unsigned int)length);
  398.    info_ptr->scal_s_width = (png_charp)png_malloc_warn(png_ptr, length);
  399.    if (info_ptr->scal_s_width == NULL)
  400.    {
  401.       png_warning(png_ptr,
  402.        "Memory allocation failed while processing sCAL.");
  403.       return;
  404.    }
  405.    png_memcpy(info_ptr->scal_s_width, swidth, (png_size_t)length);
  406.    length = png_strlen(sheight) + 1;
  407.    png_debug1(3, "allocating unit for info (%u bytes)",
  408.       (unsigned int)length);
  409.    info_ptr->scal_s_height = (png_charp)png_malloc_warn(png_ptr, length);
  410.    if (info_ptr->scal_s_height == NULL)
  411.    {
  412.       png_free (png_ptr, info_ptr->scal_s_width);
  413.       info_ptr->scal_s_width = NULL;
  414.       png_warning(png_ptr,
  415.        "Memory allocation failed while processing sCAL.");
  416.       return;
  417.    }
  418.    png_memcpy(info_ptr->scal_s_height, sheight, (png_size_t)length);
  419.    info_ptr->valid |= PNG_INFO_sCAL;
  420. #ifdef PNG_FREE_ME_SUPPORTED
  421.    info_ptr->free_me |= PNG_FREE_SCAL;
  422. #endif
  423. }
  424. #endif
  425. #endif
  426. #endif
  427. #if defined(PNG_pHYs_SUPPORTED)
  428. void PNGAPI
  429. png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
  430.    png_uint_32 res_x, png_uint_32 res_y, int unit_type)
  431. {
  432.    png_debug1(1, "in %s storage function", "pHYs");
  433.    if (png_ptr == NULL || info_ptr == NULL)
  434.       return;
  435.    info_ptr->x_pixels_per_unit = res_x;
  436.    info_ptr->y_pixels_per_unit = res_y;
  437.    info_ptr->phys_unit_type = (png_byte)unit_type;
  438.    info_ptr->valid |= PNG_INFO_pHYs;
  439. }
  440. #endif
  441. void PNGAPI
  442. png_set_PLTE(png_structp png_ptr, png_infop info_ptr,
  443.    png_colorp palette, int num_palette)
  444. {
  445.    png_debug1(1, "in %s storage function", "PLTE");
  446.    if (png_ptr == NULL || info_ptr == NULL)
  447.       return;
  448.    if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
  449.      {
  450.        if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  451.          png_error(png_ptr, "Invalid palette length");
  452.        else
  453.        {
  454.          png_warning(png_ptr, "Invalid palette length");
  455.          return;
  456.        }
  457.      }
  458.    /*
  459.     * It may not actually be necessary to set png_ptr->palette here;
  460.     * we do it for backward compatibility with the way the png_handle_tRNS
  461.     * function used to do the allocation.
  462.     */
  463. #ifdef PNG_FREE_ME_SUPPORTED
  464.    png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
  465. #endif
  466.    /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
  467.       of num_palette entries,
  468.       in case of an invalid PNG file that has too-large sample values. */
  469.    png_ptr->palette = (png_colorp)png_malloc(png_ptr,
  470.       PNG_MAX_PALETTE_LENGTH * png_sizeof(png_color));
  471.    png_memset(png_ptr->palette, 0, PNG_MAX_PALETTE_LENGTH *
  472.       png_sizeof(png_color));
  473.    png_memcpy(png_ptr->palette, palette, num_palette * png_sizeof(png_color));
  474.    info_ptr->palette = png_ptr->palette;
  475.    info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
  476. #ifdef PNG_FREE_ME_SUPPORTED
  477.    info_ptr->free_me |= PNG_FREE_PLTE;
  478. #else
  479.    png_ptr->flags |= PNG_FLAG_FREE_PLTE;
  480. #endif
  481.    info_ptr->valid |= PNG_INFO_PLTE;
  482. }
  483. #if defined(PNG_sBIT_SUPPORTED)
  484. void PNGAPI
  485. png_set_sBIT(png_structp png_ptr, png_infop info_ptr,
  486.    png_color_8p sig_bit)
  487. {
  488.    png_debug1(1, "in %s storage function", "sBIT");
  489.    if (png_ptr == NULL || info_ptr == NULL)
  490.       return;
  491.    png_memcpy(&(info_ptr->sig_bit), sig_bit, png_sizeof(png_color_8));
  492.    info_ptr->valid |= PNG_INFO_sBIT;
  493. }
  494. #endif
  495. #if defined(PNG_sRGB_SUPPORTED)
  496. void PNGAPI
  497. png_set_sRGB(png_structp png_ptr, png_infop info_ptr, int intent)
  498. {
  499.    png_debug1(1, "in %s storage function", "sRGB");
  500.    if (png_ptr == NULL || info_ptr == NULL)
  501.       return;
  502.    info_ptr->srgb_intent = (png_byte)intent;
  503.    info_ptr->valid |= PNG_INFO_sRGB;
  504. }
  505. void PNGAPI
  506. png_set_sRGB_gAMA_and_cHRM(png_structp png_ptr, png_infop info_ptr,
  507.    int intent)
  508. {
  509. #if defined(PNG_gAMA_SUPPORTED)
  510. #ifdef PNG_FLOATING_POINT_SUPPORTED
  511.    float file_gamma;
  512. #endif
  513. #ifdef PNG_FIXED_POINT_SUPPORTED
  514.    png_fixed_point int_file_gamma;
  515. #endif
  516. #endif
  517. #if defined(PNG_cHRM_SUPPORTED)
  518. #ifdef PNG_FLOATING_POINT_SUPPORTED
  519.    float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  520. #endif
  521.    png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
  522.       int_green_y, int_blue_x, int_blue_y;
  523. #endif
  524.    png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
  525.    if (png_ptr == NULL || info_ptr == NULL)
  526.       return;
  527.    png_set_sRGB(png_ptr, info_ptr, intent);
  528. #if defined(PNG_gAMA_SUPPORTED)
  529. #ifdef PNG_FLOATING_POINT_SUPPORTED
  530.    file_gamma = (float).45455;
  531.    png_set_gAMA(png_ptr, info_ptr, file_gamma);
  532. #endif
  533. #ifdef PNG_FIXED_POINT_SUPPORTED
  534.    int_file_gamma = 45455L;
  535.    png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma);
  536. #endif
  537. #endif
  538. #if defined(PNG_cHRM_SUPPORTED)
  539.    int_white_x = 31270L;
  540.    int_white_y = 32900L;
  541.    int_red_x   = 64000L;
  542.    int_red_y   = 33000L;
  543.    int_green_x = 30000L;
  544.    int_green_y = 60000L;
  545.    int_blue_x  = 15000L;
  546.    int_blue_y  =  6000L;
  547. #ifdef PNG_FLOATING_POINT_SUPPORTED
  548.    white_x = (float).3127;
  549.    white_y = (float).3290;
  550.    red_x   = (float).64;
  551.    red_y   = (float).33;
  552.    green_x = (float).30;
  553.    green_y = (float).60;
  554.    blue_x  = (float).15;
  555.    blue_y  = (float).06;
  556. #endif
  557. #if !defined(PNG_NO_CHECK_cHRM)
  558.    if (png_check_cHRM_fixed(png_ptr,
  559.       int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
  560.       int_green_y, int_blue_x, int_blue_y))
  561. #endif
  562.    {
  563. #ifdef PNG_FIXED_POINT_SUPPORTED
  564.      png_set_cHRM_fixed(png_ptr, info_ptr,
  565.         int_white_x, int_white_y, int_red_x, int_red_y, int_green_x,
  566.         int_green_y, int_blue_x, int_blue_y);
  567. #endif
  568. #ifdef PNG_FLOATING_POINT_SUPPORTED
  569.       png_set_cHRM(png_ptr, info_ptr,
  570.          white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  571. #endif
  572.    }
  573. #endif /* cHRM */
  574. }
  575. #endif
  576. #if defined(PNG_iCCP_SUPPORTED)
  577. void PNGAPI
  578. png_set_iCCP(png_structp png_ptr, png_infop info_ptr,
  579.              png_charp name, int compression_type,
  580.              png_charp profile, png_uint_32 proflen)
  581. {
  582.    png_charp new_iccp_name;
  583.    png_charp new_iccp_profile;
  584.    png_uint_32 length;
  585.    png_debug1(1, "in %s storage function", "iCCP");
  586.    if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
  587.       return;
  588.    length = (png_uint_32)png_strlen(name)+1;
  589.    new_iccp_name = (png_charp)png_malloc_warn(png_ptr, length);
  590.    if (new_iccp_name == NULL)
  591.    {
  592.       png_warning(png_ptr, "Insufficient memory to process iCCP chunk.");
  593.       return;
  594.    }
  595.    png_memcpy(new_iccp_name, name, length);
  596.    new_iccp_profile = (png_charp)png_malloc_warn(png_ptr, proflen);
  597.    if (new_iccp_profile == NULL)
  598.    {
  599.       png_free (png_ptr, new_iccp_name);
  600.       png_warning(png_ptr,
  601.       "Insufficient memory to process iCCP profile.");
  602.       return;
  603.    }
  604.    png_memcpy(new_iccp_profile, profile, (png_size_t)proflen);
  605.    png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
  606.    info_ptr->iccp_proflen = proflen;
  607.    info_ptr->iccp_name = new_iccp_name;
  608.    info_ptr->iccp_profile = new_iccp_profile;
  609.    /* Compression is always zero but is here so the API and info structure
  610.     * does not have to change if we introduce multiple compression types */
  611.    info_ptr->iccp_compression = (png_byte)compression_type;
  612. #ifdef PNG_FREE_ME_SUPPORTED
  613.    info_ptr->free_me |= PNG_FREE_ICCP;
  614. #endif
  615.    info_ptr->valid |= PNG_INFO_iCCP;
  616. }
  617. #endif
  618. #if defined(PNG_TEXT_SUPPORTED)
  619. void PNGAPI
  620. png_set_text(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
  621.    int num_text)
  622. {
  623.    int ret;
  624.    ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
  625.    if (ret)
  626.      png_error(png_ptr, "Insufficient memory to store text");
  627. }
  628. int /* PRIVATE */
  629. png_set_text_2(png_structp png_ptr, png_infop info_ptr, png_textp text_ptr,
  630.    int num_text)
  631. {
  632.    int i;
  633.    png_debug1(1, "in %s storage function", (png_ptr->chunk_name[0] == '' ?
  634.       "text" : (png_const_charp)png_ptr->chunk_name));
  635.    if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
  636.       return(0);
  637.    /* Make sure we have enough space in the "text" array in info_struct
  638.     * to hold all of the incoming text_ptr objects.
  639.     */
  640.    if (info_ptr->num_text + num_text > info_ptr->max_text)
  641.    {
  642.       if (info_ptr->text != NULL)
  643.       {
  644.          png_textp old_text;
  645.          int old_max;
  646.          old_max = info_ptr->max_text;
  647.          info_ptr->max_text = info_ptr->num_text + num_text + 8;
  648.          old_text = info_ptr->text;
  649.          info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  650.             (png_uint_32)(info_ptr->max_text * png_sizeof(png_text)));
  651.          if (info_ptr->text == NULL)
  652.            {
  653.              png_free(png_ptr, old_text);
  654.              return(1);
  655.            }
  656.          png_memcpy(info_ptr->text, old_text, (png_size_t)(old_max *
  657.             png_sizeof(png_text)));
  658.          png_free(png_ptr, old_text);
  659.       }
  660.       else
  661.       {
  662.          info_ptr->max_text = num_text + 8;
  663.          info_ptr->num_text = 0;
  664.          info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
  665.             (png_uint_32)(info_ptr->max_text * png_sizeof(png_text)));
  666.          if (info_ptr->text == NULL)
  667.            return(1);
  668. #ifdef PNG_FREE_ME_SUPPORTED
  669.          info_ptr->free_me |= PNG_FREE_TEXT;
  670. #endif
  671.       }
  672.       png_debug1(3, "allocated %d entries for info_ptr->text",
  673.          info_ptr->max_text);
  674.    }
  675.    for (i = 0; i < num_text; i++)
  676.    {
  677.       png_size_t text_length, key_len;
  678.       png_size_t lang_len, lang_key_len;
  679.       png_textp textp = &(info_ptr->text[info_ptr->num_text]);
  680.       if (text_ptr[i].key == NULL)
  681.           continue;
  682.       key_len = png_strlen(text_ptr[i].key);
  683.       if (text_ptr[i].compression <= 0)
  684.       {
  685.         lang_len = 0;
  686.         lang_key_len = 0;
  687.       }
  688.       else
  689. #ifdef PNG_iTXt_SUPPORTED
  690.       {
  691.         /* set iTXt data */
  692.         if (text_ptr[i].lang != NULL)
  693.           lang_len = png_strlen(text_ptr[i].lang);
  694.         else
  695.           lang_len = 0;
  696.         if (text_ptr[i].lang_key != NULL)
  697.           lang_key_len = png_strlen(text_ptr[i].lang_key);
  698.         else
  699.           lang_key_len = 0;
  700.       }
  701. #else
  702.       {
  703.         png_warning(png_ptr, "iTXt chunk not supported.");
  704.         continue;
  705.       }
  706. #endif
  707.       if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '')
  708.       {
  709.          text_length = 0;
  710. #ifdef PNG_iTXt_SUPPORTED
  711.          if (text_ptr[i].compression > 0)
  712.             textp->compression = PNG_ITXT_COMPRESSION_NONE;
  713.          else
  714. #endif
  715.             textp->compression = PNG_TEXT_COMPRESSION_NONE;
  716.       }
  717.       else
  718.       {
  719.          text_length = png_strlen(text_ptr[i].text);
  720.          textp->compression = text_ptr[i].compression;
  721.       }
  722.       textp->key = (png_charp)png_malloc_warn(png_ptr,
  723.          (png_uint_32)
  724.          (key_len + text_length + lang_len + lang_key_len + 4));
  725.       if (textp->key == NULL)
  726.         return(1);
  727.       png_debug2(2, "Allocated %lu bytes at %x in png_set_text",
  728.          (png_uint_32)
  729.          (key_len + lang_len + lang_key_len + text_length + 4),
  730.          (int)textp->key);
  731.       png_memcpy(textp->key, text_ptr[i].key,
  732.          (png_size_t)(key_len));
  733.       *(textp->key + key_len) = '';
  734. #ifdef PNG_iTXt_SUPPORTED
  735.       if (text_ptr[i].compression > 0)
  736.       {
  737.          textp->lang = textp->key + key_len + 1;
  738.          png_memcpy(textp->lang, text_ptr[i].lang, lang_len);
  739.          *(textp->lang + lang_len) = '';
  740.          textp->lang_key = textp->lang + lang_len + 1;
  741.          png_memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
  742.          *(textp->lang_key + lang_key_len) = '';
  743.          textp->text = textp->lang_key + lang_key_len + 1;
  744.       }
  745.       else
  746. #endif
  747.       {
  748. #ifdef PNG_iTXt_SUPPORTED
  749.          textp->lang=NULL;
  750.          textp->lang_key=NULL;
  751. #endif
  752.          textp->text = textp->key + key_len + 1;
  753.       }
  754.       if (text_length)
  755.          png_memcpy(textp->text, text_ptr[i].text,
  756.             (png_size_t)(text_length));
  757.       *(textp->text + text_length) = '';
  758. #ifdef PNG_iTXt_SUPPORTED
  759.       if (textp->compression > 0)
  760.       {
  761.          textp->text_length = 0;
  762.          textp->itxt_length = text_length;
  763.       }
  764.       else
  765. #endif
  766.       {
  767.          textp->text_length = text_length;
  768. #ifdef PNG_iTXt_SUPPORTED
  769.          textp->itxt_length = 0;
  770. #endif
  771.       }
  772.       info_ptr->num_text++;
  773.       png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
  774.    }
  775.    return(0);
  776. }
  777. #endif
  778. #if defined(PNG_tIME_SUPPORTED)
  779. void PNGAPI
  780. png_set_tIME(png_structp png_ptr, png_infop info_ptr, png_timep mod_time)
  781. {
  782.    png_debug1(1, "in %s storage function", "tIME");
  783.    if (png_ptr == NULL || info_ptr == NULL ||
  784.        (png_ptr->mode & PNG_WROTE_tIME))
  785.       return;
  786.    png_memcpy(&(info_ptr->mod_time), mod_time, png_sizeof(png_time));
  787.    info_ptr->valid |= PNG_INFO_tIME;
  788. }
  789. #endif
  790. #if defined(PNG_tRNS_SUPPORTED)
  791. void PNGAPI
  792. png_set_tRNS(png_structp png_ptr, png_infop info_ptr,
  793.    png_bytep trans, int num_trans, png_color_16p trans_values)
  794. {
  795.    png_debug1(1, "in %s storage function", "tRNS");
  796.    if (png_ptr == NULL || info_ptr == NULL)
  797.       return;
  798.    if (trans != NULL)
  799.    {
  800.        /*
  801.         * It may not actually be necessary to set png_ptr->trans here;
  802.         * we do it for backward compatibility with the way the png_handle_tRNS
  803.         * function used to do the allocation.
  804.         */
  805. #ifdef PNG_FREE_ME_SUPPORTED
  806.        png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
  807. #endif
  808.        /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
  809.        png_ptr->trans = info_ptr->trans = (png_bytep)png_malloc(png_ptr,
  810.            (png_uint_32)PNG_MAX_PALETTE_LENGTH);
  811.        if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
  812.          png_memcpy(info_ptr->trans, trans, (png_size_t)num_trans);
  813.    }
  814.    if (trans_values != NULL)
  815.    {
  816.       int sample_max = (1 << info_ptr->bit_depth);
  817.       if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
  818.           (int)trans_values->gray > sample_max) ||
  819.           (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  820.           ((int)trans_values->red > sample_max ||
  821.           (int)trans_values->green > sample_max ||
  822.           (int)trans_values->blue > sample_max)))
  823.         png_warning(png_ptr,
  824.            "tRNS chunk has out-of-range samples for bit_depth");
  825.       png_memcpy(&(info_ptr->trans_values), trans_values,
  826.          png_sizeof(png_color_16));
  827.       if (num_trans == 0)
  828.         num_trans = 1;
  829.    }
  830.    info_ptr->num_trans = (png_uint_16)num_trans;
  831.    if (num_trans != 0)
  832.    {
  833.       info_ptr->valid |= PNG_INFO_tRNS;
  834. #ifdef PNG_FREE_ME_SUPPORTED
  835.       info_ptr->free_me |= PNG_FREE_TRNS;
  836. #else
  837.       png_ptr->flags |= PNG_FLAG_FREE_TRNS;
  838. #endif
  839.    }
  840. }
  841. #endif
  842. #if defined(PNG_sPLT_SUPPORTED)
  843. void PNGAPI
  844. png_set_sPLT(png_structp png_ptr,
  845.              png_infop info_ptr, png_sPLT_tp entries, int nentries)
  846. /*
  847.  *  entries        - array of png_sPLT_t structures
  848.  *                   to be added to the list of palettes
  849.  *                   in the info structure.
  850.  *  nentries       - number of palette structures to be
  851.  *                   added.
  852.  */
  853. {
  854.     png_sPLT_tp np;
  855.     int i;
  856.     if (png_ptr == NULL || info_ptr == NULL)
  857.        return;
  858.     np = (png_sPLT_tp)png_malloc_warn(png_ptr,
  859.         (info_ptr->splt_palettes_num + nentries) *
  860.         (png_uint_32)png_sizeof(png_sPLT_t));
  861.     if (np == NULL)
  862.     {
  863.       png_warning(png_ptr, "No memory for sPLT palettes.");
  864.       return;
  865.     }
  866.     png_memcpy(np, info_ptr->splt_palettes,
  867.            info_ptr->splt_palettes_num * png_sizeof(png_sPLT_t));
  868.     png_free(png_ptr, info_ptr->splt_palettes);
  869.     info_ptr->splt_palettes=NULL;
  870.     for (i = 0; i < nentries; i++)
  871.     {
  872.         png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
  873.         png_sPLT_tp from = entries + i;
  874.         png_uint_32 length;
  875.         length = (png_uint_32)png_strlen(from->name) + 1;
  876.         to->name = (png_charp)png_malloc_warn(png_ptr, length);
  877.         if (to->name == NULL)
  878.         {
  879.            png_warning(png_ptr,
  880.              "Out of memory while processing sPLT chunk");
  881.            continue;
  882.         }
  883.         png_memcpy(to->name, from->name, length);
  884.         to->entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
  885.             (png_uint_32)(from->nentries * png_sizeof(png_sPLT_entry)));
  886.         if (to->entries == NULL)
  887.         {
  888.            png_warning(png_ptr,
  889.              "Out of memory while processing sPLT chunk");
  890.            png_free(png_ptr, to->name);
  891.            to->name = NULL;
  892.            continue;
  893.         }
  894.         png_memcpy(to->entries, from->entries,
  895.             from->nentries * png_sizeof(png_sPLT_entry));
  896.         to->nentries = from->nentries;
  897.         to->depth = from->depth;
  898.     }
  899.     info_ptr->splt_palettes = np;
  900.     info_ptr->splt_palettes_num += nentries;
  901.     info_ptr->valid |= PNG_INFO_sPLT;
  902. #ifdef PNG_FREE_ME_SUPPORTED
  903.     info_ptr->free_me |= PNG_FREE_SPLT;
  904. #endif
  905. }
  906. #endif /* PNG_sPLT_SUPPORTED */
  907. #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  908. void PNGAPI
  909. png_set_unknown_chunks(png_structp png_ptr,
  910.    png_infop info_ptr, png_unknown_chunkp unknowns, int num_unknowns)
  911. {
  912.     png_unknown_chunkp np;
  913.     int i;
  914.     if (png_ptr == NULL || info_ptr == NULL || num_unknowns == 0)
  915.         return;
  916.     np = (png_unknown_chunkp)png_malloc_warn(png_ptr,
  917.         (png_uint_32)((info_ptr->unknown_chunks_num + num_unknowns) *
  918.         png_sizeof(png_unknown_chunk)));
  919.     if (np == NULL)
  920.     {
  921.        png_warning(png_ptr,
  922.           "Out of memory while processing unknown chunk.");
  923.        return;
  924.     }
  925.     png_memcpy(np, info_ptr->unknown_chunks,
  926.            info_ptr->unknown_chunks_num * png_sizeof(png_unknown_chunk));
  927.     png_free(png_ptr, info_ptr->unknown_chunks);
  928.     info_ptr->unknown_chunks=NULL;
  929.     for (i = 0; i < num_unknowns; i++)
  930.     {
  931.        png_unknown_chunkp to = np + info_ptr->unknown_chunks_num + i;
  932.        png_unknown_chunkp from = unknowns + i;
  933.        png_memcpy((png_charp)to->name, 
  934.                   (png_charp)from->name, 
  935.                   png_sizeof(from->name));
  936.        to->name[png_sizeof(to->name)-1] = '';
  937.        to->size = from->size;
  938.        /* note our location in the read or write sequence */
  939.        to->location = (png_byte)(png_ptr->mode & 0xff);
  940.        if (from->size == 0)
  941.           to->data=NULL;
  942.        else
  943.        {
  944.           to->data = (png_bytep)png_malloc_warn(png_ptr,
  945.             (png_uint_32)from->size);
  946.           if (to->data == NULL)
  947.           {
  948.              png_warning(png_ptr,
  949.               "Out of memory while processing unknown chunk.");
  950.              to->size = 0;
  951.           }
  952.           else
  953.              png_memcpy(to->data, from->data, from->size);
  954.        }
  955.     }
  956.     info_ptr->unknown_chunks = np;
  957.     info_ptr->unknown_chunks_num += num_unknowns;
  958. #ifdef PNG_FREE_ME_SUPPORTED
  959.     info_ptr->free_me |= PNG_FREE_UNKN;
  960. #endif
  961. }
  962. void PNGAPI
  963. png_set_unknown_chunk_location(png_structp png_ptr, png_infop info_ptr,
  964.    int chunk, int location)
  965. {
  966.    if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && chunk <
  967.          (int)info_ptr->unknown_chunks_num)
  968.       info_ptr->unknown_chunks[chunk].location = (png_byte)location;
  969. }
  970. #endif
  971. #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  972. #if defined(PNG_READ_EMPTY_PLTE_SUPPORTED) || 
  973.     defined(PNG_WRITE_EMPTY_PLTE_SUPPORTED)
  974. void PNGAPI
  975. png_permit_empty_plte (png_structp png_ptr, int empty_plte_permitted)
  976. {
  977.    /* This function is deprecated in favor of png_permit_mng_features()
  978.       and will be removed from libpng-1.3.0 */
  979.    png_debug(1, "in png_permit_empty_plte, DEPRECATED.");
  980.    if (png_ptr == NULL)
  981.       return;
  982.    png_ptr->mng_features_permitted = (png_byte)
  983.      ((png_ptr->mng_features_permitted & (~PNG_FLAG_MNG_EMPTY_PLTE)) |
  984.      ((empty_plte_permitted & PNG_FLAG_MNG_EMPTY_PLTE)));
  985. }
  986. #endif
  987. #endif
  988. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  989. png_uint_32 PNGAPI
  990. png_permit_mng_features (png_structp png_ptr, png_uint_32 mng_features)
  991. {
  992.    png_debug(1, "in png_permit_mng_features");
  993.    if (png_ptr == NULL)
  994.       return (png_uint_32)0;
  995.    png_ptr->mng_features_permitted =
  996.      (png_byte)(mng_features & PNG_ALL_MNG_FEATURES);
  997.    return (png_uint_32)png_ptr->mng_features_permitted;
  998. }
  999. #endif
  1000. #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  1001. void PNGAPI
  1002. png_set_keep_unknown_chunks(png_structp png_ptr, int keep, png_bytep
  1003.    chunk_list, int num_chunks)
  1004. {
  1005.     png_bytep new_list, p;
  1006.     int i, old_num_chunks;
  1007.     if (png_ptr == NULL)
  1008.        return;
  1009.     if (num_chunks == 0)
  1010.     {
  1011.       if (keep == PNG_HANDLE_CHUNK_ALWAYS || keep == PNG_HANDLE_CHUNK_IF_SAFE)
  1012.         png_ptr->flags |= PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
  1013.       else
  1014.         png_ptr->flags &= ~PNG_FLAG_KEEP_UNKNOWN_CHUNKS;
  1015.       if (keep == PNG_HANDLE_CHUNK_ALWAYS)
  1016.         png_ptr->flags |= PNG_FLAG_KEEP_UNSAFE_CHUNKS;
  1017.       else
  1018.         png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
  1019.       return;
  1020.     }
  1021.     if (chunk_list == NULL)
  1022.       return;
  1023.     old_num_chunks = png_ptr->num_chunk_list;
  1024.     new_list=(png_bytep)png_malloc(png_ptr,
  1025.        (png_uint_32)
  1026.        (5*(num_chunks + old_num_chunks)));
  1027.     if (png_ptr->chunk_list != NULL)
  1028.     {
  1029.        png_memcpy(new_list, png_ptr->chunk_list,
  1030.           (png_size_t)(5*old_num_chunks));
  1031.        png_free(png_ptr, png_ptr->chunk_list);
  1032.        png_ptr->chunk_list=NULL;
  1033.     }
  1034.     png_memcpy(new_list + 5*old_num_chunks, chunk_list,
  1035.        (png_size_t)(5*num_chunks));
  1036.     for (p = new_list + 5*old_num_chunks + 4, i = 0; i<num_chunks; i++, p += 5)
  1037.        *p=(png_byte)keep;
  1038.     png_ptr->num_chunk_list = old_num_chunks + num_chunks;
  1039.     png_ptr->chunk_list = new_list;
  1040. #ifdef PNG_FREE_ME_SUPPORTED
  1041.     png_ptr->free_me |= PNG_FREE_LIST;
  1042. #endif
  1043. }
  1044. #endif
  1045. #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  1046. void PNGAPI
  1047. png_set_read_user_chunk_fn(png_structp png_ptr, png_voidp user_chunk_ptr,
  1048.    png_user_chunk_ptr read_user_chunk_fn)
  1049. {
  1050.    png_debug(1, "in png_set_read_user_chunk_fn");
  1051.    if (png_ptr == NULL)
  1052.       return;
  1053.    png_ptr->read_user_chunk_fn = read_user_chunk_fn;
  1054.    png_ptr->user_chunk_ptr = user_chunk_ptr;
  1055. }
  1056. #endif
  1057. #if defined(PNG_INFO_IMAGE_SUPPORTED)
  1058. void PNGAPI
  1059. png_set_rows(png_structp png_ptr, png_infop info_ptr, png_bytepp row_pointers)
  1060. {
  1061.    png_debug1(1, "in %s storage function", "rows");
  1062.    if (png_ptr == NULL || info_ptr == NULL)
  1063.       return;
  1064.    if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
  1065.       png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  1066.    info_ptr->row_pointers = row_pointers;
  1067.    if (row_pointers)
  1068.       info_ptr->valid |= PNG_INFO_IDAT;
  1069. }
  1070. #endif
  1071. #ifdef PNG_WRITE_SUPPORTED
  1072. void PNGAPI
  1073. png_set_compression_buffer_size(png_structp png_ptr,
  1074.     png_uint_32 size)
  1075. {
  1076.     if (png_ptr == NULL)
  1077.        return;
  1078.     png_free(png_ptr, png_ptr->zbuf);
  1079.     png_ptr->zbuf_size = (png_size_t)size;
  1080.     png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, size);
  1081.     png_ptr->zstream.next_out = png_ptr->zbuf;
  1082.     png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  1083. }
  1084. #endif
  1085. void PNGAPI
  1086. png_set_invalid(png_structp png_ptr, png_infop info_ptr, int mask)
  1087. {
  1088.    if (png_ptr && info_ptr)
  1089.       info_ptr->valid &= ~mask;
  1090. }
  1091. #ifndef PNG_1_0_X
  1092. #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
  1093. /* function was added to libpng 1.2.0 and should always exist by default */
  1094. void PNGAPI
  1095. png_set_asm_flags (png_structp png_ptr, png_uint_32 asm_flags)
  1096. {
  1097. /* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
  1098.     if (png_ptr != NULL)
  1099.     png_ptr->asm_flags = 0;
  1100.     asm_flags = asm_flags; /* Quiet the compiler */
  1101. }
  1102. /* this function was added to libpng 1.2.0 */
  1103. void PNGAPI
  1104. png_set_mmx_thresholds (png_structp png_ptr,
  1105.                         png_byte mmx_bitdepth_threshold,
  1106.                         png_uint_32 mmx_rowbytes_threshold)
  1107. {
  1108. /* Obsolete as of libpng-1.2.20 and will be removed from libpng-1.4.0 */
  1109.     if (png_ptr == NULL)
  1110.        return;
  1111.     /* Quiet the compiler */
  1112.     mmx_bitdepth_threshold = mmx_bitdepth_threshold;
  1113.     mmx_rowbytes_threshold = mmx_rowbytes_threshold;
  1114. }
  1115. #endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
  1116. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  1117. /* this function was added to libpng 1.2.6 */
  1118. void PNGAPI
  1119. png_set_user_limits (png_structp png_ptr, png_uint_32 user_width_max,
  1120.     png_uint_32 user_height_max)
  1121. {
  1122.     /* Images with dimensions larger than these limits will be
  1123.      * rejected by png_set_IHDR().  To accept any PNG datastream
  1124.      * regardless of dimensions, set both limits to 0x7ffffffL.
  1125.      */
  1126.     if (png_ptr == NULL) return;
  1127.     png_ptr->user_width_max = user_width_max;
  1128.     png_ptr->user_height_max = user_height_max;
  1129. }
  1130. #endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
  1131. #endif /* ?PNG_1_0_X */
  1132. #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */