home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / flash078.zip / flashsource-r0_7_8.zip / libpng / pngset.c < prev    next >
C/C++ Source or Header  |  2001-04-27  |  31KB  |  971 lines

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