home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / pdflib / pdflib-4.0.1.sit / pdflib-4.0.1 / png / pngset.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  25.0 KB  |  840 lines  |  [TEXT/CWIE]

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