pngwutil.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:32k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /* pngwutil.c - utilities to write a png file
  2. libpng 1.0 beta 2 - version 0.87
  3.    For conditions of distribution and use, see copyright notice in png.h
  4. Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  5.    January 15, 1996
  6.    */
  7. #define PNG_INTERNAL
  8. #include "png.h"
  9. /* place a 32 bit number into a buffer in png byte order.  We work
  10.    with unsigned numbers for convenience, you may have to cast
  11.    signed numbers (if you use any, most png data is unsigned). */
  12. void
  13. png_save_uint_32(png_bytep buf, png_uint_32 i)
  14. {
  15.    buf[0] = (png_byte)((i >> 24) & 0xff);
  16.    buf[1] = (png_byte)((i >> 16) & 0xff);
  17.    buf[2] = (png_byte)((i >> 8) & 0xff);
  18.    buf[3] = (png_byte)(i & 0xff);
  19. }
  20. /* place a 16 bit number into a buffer in png byte order */
  21. void
  22. png_save_uint_16(png_bytep buf, png_uint_16 i)
  23. {
  24.    buf[0] = (png_byte)((i >> 8) & 0xff);
  25.    buf[1] = (png_byte)(i & 0xff);
  26. }
  27. /* write a 32 bit number */
  28. void
  29. png_write_uint_32(png_structp png_ptr, png_uint_32 i)
  30. {
  31.    png_byte buf[4];
  32.    buf[0] = (png_byte)((i >> 24) & 0xff);
  33.    buf[1] = (png_byte)((i >> 16) & 0xff);
  34.    buf[2] = (png_byte)((i >> 8) & 0xff);
  35.    buf[3] = (png_byte)(i & 0xff);
  36.    png_write_data(png_ptr, buf, 4);
  37. }
  38. /* write a 16 bit number */
  39. void
  40. png_write_uint_16(png_structp png_ptr, png_uint_16 i)
  41. {
  42.    png_byte buf[2];
  43.    buf[0] = (png_byte)((i >> 8) & 0xff);
  44.    buf[1] = (png_byte)(i & 0xff);
  45.    png_write_data(png_ptr, buf, 2);
  46. }
  47. /* Write a png chunk all at once.  The type is an array of ASCII characters
  48.    representing the chunk name.  The array must be at least 4 bytes in
  49.    length, and does not need to be null terminated.  To be safe, pass the
  50.    pre-defined chunk names here, and if you need a new one, define it
  51.    where the others are defined.  The length is the length of the data.
  52.    All the data must be present.  If that is not possible, use the
  53.    png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
  54.    functions instead.  */
  55. void
  56. png_write_chunk(png_structp png_ptr, png_bytep type,
  57.    png_bytep data, png_uint_32 length)
  58. {
  59.    /* write length */
  60.    png_write_uint_32(png_ptr, length);
  61.    /* write chunk name */
  62.    png_write_data(png_ptr, type, (png_uint_32)4);
  63.    /* reset the crc and run the chunk name over it */
  64.    png_reset_crc(png_ptr);
  65.    png_calculate_crc(png_ptr, type, (png_uint_32)4);
  66.    /* write the data and update the crc */
  67.    if (length)
  68.    {
  69.       png_calculate_crc(png_ptr, data, length);
  70.       png_write_data(png_ptr, data, length);
  71.    }
  72.    /* write the crc */
  73.    png_write_uint_32(png_ptr, ~png_ptr->crc);
  74. }
  75. /* Write the start of a png chunk.  The type is the chunk type.
  76.    The total_length is the sum of the lengths of all the data you will be
  77.    passing in png_write_chunk_data() */
  78. void
  79. png_write_chunk_start(png_structp png_ptr, png_bytep type,
  80.    png_uint_32 total_length)
  81. {
  82.    /* write the length */
  83.    png_write_uint_32(png_ptr, total_length);
  84.    /* write the chunk name */
  85.    png_write_data(png_ptr, type, (png_uint_32)4);
  86.    /* reset the crc and run it over the chunk name */
  87.    png_reset_crc(png_ptr);
  88.    png_calculate_crc(png_ptr, type, (png_uint_32)4);
  89. }
  90. /* write the data of a png chunk started with png_write_chunk_start().
  91.    Note that multiple calls to this function are allowed, and that the
  92.    sum of the lengths from these calls *must* add up to the total_length
  93.    given to png_write_chunk_start() */
  94. void
  95. png_write_chunk_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
  96. {
  97.    /* write the data, and run the crc over it */
  98.    if (length)
  99.    {
  100.       png_calculate_crc(png_ptr, data, length);
  101.       png_write_data(png_ptr, data, length);
  102.    }
  103. }
  104. /* finish a chunk started with png_write_chunk_start() */
  105. void
  106. png_write_chunk_end(png_structp png_ptr)
  107. {
  108.    /* write the crc */
  109.    png_write_uint_32(png_ptr, ~png_ptr->crc);
  110. }
  111. /* simple function to write the signature */
  112. void
  113. png_write_sig(png_structp png_ptr)
  114. {
  115.    /* write the 8 byte signature */
  116.    png_write_data(png_ptr, png_sig, (png_uint_32)8);
  117. }
  118. /* Write the IHDR chunk, and update the png_struct with the necessary
  119.    information.  Note that the rest of this code depends upon this
  120.    information being correct.  */
  121. void
  122. png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
  123.    int bit_depth, int color_type, int compression_type, int filter_type,
  124.    int interlace_type)
  125. {
  126.    png_byte buf[13]; /* buffer to store the IHDR info */
  127.    /* pack the header information into the buffer */
  128.    png_save_uint_32(buf, width);
  129.    png_save_uint_32(buf + 4, height);
  130. buf[8] = (png_byte)bit_depth;
  131. buf[9] = (png_byte)color_type;
  132. buf[10] = (png_byte)compression_type;
  133. buf[11] = (png_byte)filter_type;
  134. buf[12] = (png_byte)interlace_type;
  135. /* save off the relevent information */
  136. png_ptr->bit_depth = (png_byte)bit_depth;
  137. png_ptr->color_type = (png_byte)color_type;
  138. png_ptr->interlaced = (png_byte)interlace_type;
  139.    png_ptr->width = width;
  140.    png_ptr->height = height;
  141.    switch (color_type)
  142.    {
  143.       case 0:
  144.       case 3:
  145.          png_ptr->channels = 1;
  146.          break;
  147.       case 2:
  148.          png_ptr->channels = 3;
  149.          break;
  150.       case 4:
  151.          png_ptr->channels = 2;
  152.          break;
  153.       case 6:
  154.          png_ptr->channels = 4;
  155.          break;
  156.    }
  157. png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
  158.    png_ptr->rowbytes = ((width * (png_uint_32)png_ptr->pixel_depth + 7) >> 3);
  159.    /* set the usr info, so any transformations can modify it */
  160.    png_ptr->usr_width = png_ptr->width;
  161.    png_ptr->usr_bit_depth = png_ptr->bit_depth;
  162.     png_ptr->usr_channels = png_ptr->channels;
  163.    /* write the chunk */
  164.     png_write_chunk(png_ptr, png_IHDR, buf, (png_uint_32)13);
  165.    /* initialize zlib with png info */
  166.    png_ptr->zstream = (z_stream *)png_malloc(png_ptr, sizeof (z_stream));
  167.    png_ptr->zstream->zalloc = png_zalloc;
  168.    png_ptr->zstream->zfree = png_zfree;
  169.    png_ptr->zstream->opaque = (voidpf)png_ptr;
  170.    if (!png_ptr->do_custom_filter)
  171.    {
  172.       if (png_ptr->color_type == 3 || png_ptr->bit_depth < 8)
  173.          png_ptr->do_filter = 0;
  174.       else
  175.          png_ptr->do_filter = 1;
  176.    }
  177.    if (!png_ptr->zlib_custom_strategy)
  178.    {
  179.       if (png_ptr->do_filter)
  180.          png_ptr->zlib_strategy = Z_FILTERED;
  181.       else
  182.          png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
  183.    }
  184.    if (!png_ptr->zlib_custom_level)
  185.       png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
  186.    if (!png_ptr->zlib_custom_mem_level)
  187.       png_ptr->zlib_mem_level = 8;
  188.    if (!png_ptr->zlib_custom_window_bits)
  189.       png_ptr->zlib_window_bits = 15;
  190.    if (!png_ptr->zlib_custom_method)
  191.       png_ptr->zlib_method = 8;
  192.    deflateInit2(png_ptr->zstream, png_ptr->zlib_level,
  193.       png_ptr->zlib_method,
  194.       png_ptr->zlib_window_bits,
  195.       png_ptr->zlib_mem_level,
  196.       png_ptr->zlib_strategy);
  197.    png_ptr->zstream->next_out = png_ptr->zbuf;
  198.    png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  199. }
  200. /* write the palette.  We are careful not to trust png_color to be in the
  201.    correct order for PNG, so people can redefine it to any convient
  202.    structure. */
  203. void
  204. png_write_PLTE(png_structp png_ptr, png_colorp palette, int number)
  205. {
  206.    int i;
  207. png_colorp pal_ptr;
  208.    png_byte buf[3];
  209.    png_write_chunk_start(png_ptr, png_PLTE, number * 3);
  210.    for (i = 0, pal_ptr = palette;
  211.       i < number;
  212.       i++, pal_ptr++)
  213.    {
  214.       buf[0] = pal_ptr->red;
  215.       buf[1] = pal_ptr->green;
  216.       buf[2] = pal_ptr->blue;
  217.       png_write_chunk_data(png_ptr, buf, (png_uint_32)3);
  218.    }
  219.    png_write_chunk_end(png_ptr);
  220. }
  221. /* write an IDAT chunk */
  222. void
  223. png_write_IDAT(png_structp png_ptr, png_bytep data, png_uint_32 length)
  224. {
  225.    png_write_chunk(png_ptr, png_IDAT, data, length);
  226. }
  227. /* write an IEND chunk */
  228. void
  229. png_write_IEND(png_structp png_ptr)
  230. {
  231.    png_write_chunk(png_ptr, png_IEND, NULL, (png_uint_32)0);
  232. }
  233. #if defined(PNG_WRITE_gAMA_SUPPORTED)
  234. /* write a gAMA chunk */
  235. void
  236. png_write_gAMA(png_structp png_ptr, double gamma)
  237. {
  238.    png_uint_32 igamma;
  239.    png_byte buf[4];
  240.    /* gamma is saved in 1/100,000ths */
  241.    igamma = (png_uint_32)(gamma * 100000.0 + 0.5);
  242.    png_save_uint_32(buf, igamma);
  243.    png_write_chunk(png_ptr, png_gAMA, buf, (png_uint_32)4);
  244. }
  245. #endif
  246. #if defined(PNG_WRITE_sBIT_SUPPORTED)
  247. /* write the sBIT chunk */
  248. void
  249. png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
  250. {
  251.    png_byte buf[4];
  252.    int size;
  253.    /* make sure we don't depend upon the order of PNG_COLOR_8 */
  254.    if (color_type & PNG_COLOR_MASK_COLOR)
  255.    {
  256.       buf[0] = sbit->red;
  257.       buf[1] = sbit->green;
  258.       buf[2] = sbit->blue;
  259.       size = 3;
  260.    }
  261.    else
  262.    {
  263.       buf[0] = sbit->gray;
  264.       size = 1;
  265.    }
  266.    if (color_type & PNG_COLOR_MASK_ALPHA)
  267.    {
  268.       buf[size++] = sbit->alpha;
  269.    }
  270.    png_write_chunk(png_ptr, png_sBIT, buf, (png_uint_32)size);
  271. }
  272. #endif
  273. #if defined(PNG_WRITE_cHRM_SUPPORTED)
  274. /* write the cHRM chunk */
  275. void
  276. png_write_cHRM ( png_structp png_ptr, double white_x, double white_y,
  277. double red_x, double red_y, double green_x, double green_y,
  278. double blue_x, double blue_y)
  279. {
  280.    png_uint_32 itemp;
  281.    png_byte buf[32];
  282.    /* each value is saved int 1/100,000ths */
  283.    itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
  284.    png_save_uint_32(buf, itemp);
  285.    itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
  286.    png_save_uint_32(buf + 4, itemp);
  287.    itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
  288.    png_save_uint_32(buf + 8, itemp);
  289.    itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
  290.    png_save_uint_32(buf + 12, itemp);
  291.    itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
  292.    png_save_uint_32(buf + 16, itemp);
  293.    itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
  294.    png_save_uint_32(buf + 20, itemp);
  295.    itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
  296.    png_save_uint_32(buf + 24, itemp);
  297.    itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
  298.    png_save_uint_32(buf + 28, itemp);
  299.    png_write_chunk(png_ptr, png_cHRM, buf, (png_uint_32)32);
  300. }
  301. #endif
  302. #if defined(PNG_WRITE_tRNS_SUPPORTED)
  303. /* write the tRNS chunk */
  304. void
  305. png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
  306.    int num_trans, int color_type)
  307. {
  308.    png_byte buf[6];
  309.    if (color_type == PNG_COLOR_TYPE_PALETTE)
  310.    {
  311.       /* write the chunk out as it is */
  312.       png_write_chunk(png_ptr, png_tRNS, trans, (png_uint_32)num_trans);
  313.    }
  314.    else if (color_type == PNG_COLOR_TYPE_GRAY)
  315.    {
  316.       /* one 16 bit value */
  317.       png_save_uint_16(buf, tran->gray);
  318.       png_write_chunk(png_ptr, png_tRNS, buf, (png_uint_32)2);
  319.    }
  320.    else if (color_type == PNG_COLOR_TYPE_RGB)
  321.    {
  322.       /* three 16 bit values */
  323.       png_save_uint_16(buf, tran->red);
  324.       png_save_uint_16(buf + 2, tran->green);
  325.       png_save_uint_16(buf + 4, tran->blue);
  326.       png_write_chunk(png_ptr, png_tRNS, buf, (png_uint_32)6);
  327.    }
  328. }
  329. #endif
  330. #if defined(PNG_WRITE_bKGD_SUPPORTED)
  331. /* write the background chunk */
  332. void
  333. png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
  334. {
  335.    png_byte buf[6];
  336.    if (color_type == PNG_COLOR_TYPE_PALETTE)
  337.    {
  338.       buf[0] = back->index;
  339.       png_write_chunk(png_ptr, png_bKGD, buf, (png_uint_32)1);
  340.    }
  341.    else if (color_type & PNG_COLOR_MASK_COLOR)
  342.    {
  343.       png_save_uint_16(buf, back->red);
  344.       png_save_uint_16(buf + 2, back->green);
  345.       png_save_uint_16(buf + 4, back->blue);
  346.       png_write_chunk(png_ptr, png_bKGD, buf, (png_uint_32)6);
  347.    }
  348.    else
  349. {
  350.       png_save_uint_16(buf, back->gray);
  351.       png_write_chunk(png_ptr, png_bKGD, buf, (png_uint_32)2);
  352.    }
  353. }
  354. #endif
  355. #if defined(PNG_WRITE_hIST_SUPPORTED)
  356. /* write the histogram */
  357. void
  358. png_write_hIST(png_structp png_ptr, png_uint_16p hist, int number)
  359. {
  360.    int i;
  361.    png_byte buf[3];
  362.    png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(number * 2));
  363.    for (i = 0; i < number; i++)
  364.    {
  365. png_save_uint_16(buf, hist[i]);
  366.       png_write_chunk_data(png_ptr, buf, (png_uint_32)2);
  367.    }
  368.    png_write_chunk_end(png_ptr);
  369. }
  370. #endif
  371. #if defined(PNG_WRITE_tEXt_SUPPORTED)
  372. /* write a tEXt chunk */
  373. void
  374. png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
  375.    png_uint_32 text_len)
  376. {
  377.    int key_len;
  378.    key_len = png_strlen(key);
  379.    /* make sure we count the 0 after the key */
  380.    png_write_chunk_start(png_ptr, png_tEXt,
  381. (png_uint_32)(key_len + text_len + 1));
  382.    /* key has an 0 at the end.  How nice */
  383.    png_write_chunk_data(png_ptr, (png_bytep )key, (png_uint_32)(key_len + 1));
  384.    if (text && text_len)
  385.       png_write_chunk_data(png_ptr, (png_bytep )text, (png_uint_32)text_len);
  386.    png_write_chunk_end(png_ptr);
  387. }
  388. #endif
  389. #if defined(PNG_WRITE_zTXt_SUPPORTED)
  390. /* write a compressed chunk */
  391. void
  392. png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
  393.    png_uint_32 text_len, int compression)
  394. {
  395.    int key_len;
  396.    char buf[1];
  397.    int i, ret;
  398. png_charpp output_ptr = NULL; /* array of pointers to output */
  399.    int num_output_ptr = 0; /* number of output pointers used */
  400.    int max_output_ptr = 0; /* size of output_ptr */
  401.    key_len = png_strlen(key);
  402.    /* we can't write the chunk until we find out how much data we have,
  403.       which means we need to run the compresser first, and save the
  404.       output.  This shouldn't be a problem, as the vast majority of
  405.       comments should be reasonable, but we will set up an array of
  406.       malloced pointers to be sure. */
  407.    /* set up the compression buffers */
  408.    png_ptr->zstream->avail_in = (uInt)text_len;
  409.    png_ptr->zstream->next_in = (Bytef *)text;
  410.    png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  411.    png_ptr->zstream->next_out = (Bytef *)png_ptr->zbuf;
  412. /* this is the same compression loop as in png_write_row() */
  413.    do
  414.    {
  415.       /* compress the data */
  416. ret = deflate(png_ptr->zstream, Z_NO_FLUSH);
  417.       if (ret != Z_OK)
  418.       {
  419.          /* error */
  420.          if (png_ptr->zstream->msg)
  421.             png_error(png_ptr, png_ptr->zstream->msg);
  422.          else
  423.             png_error(png_ptr, "zlib error");
  424.       }
  425.       /* check to see if we need more room */
  426.       if (!png_ptr->zstream->avail_out && png_ptr->zstream->avail_in)
  427.       {
  428.          /* make sure the output array has room */
  429.          if (num_output_ptr >= max_output_ptr)
  430. {
  431.             png_uint_32 old_max;
  432.             old_max = max_output_ptr;
  433.             max_output_ptr = num_output_ptr + 4;
  434. if (output_ptr)
  435. {
  436. png_charpp old_ptr;
  437. old_ptr = output_ptr;
  438. output_ptr = (png_charpp)png_large_malloc(png_ptr,
  439. max_output_ptr * sizeof (png_charpp));
  440. png_memcpy(output_ptr, old_ptr,
  441. (png_size_t)(old_max * sizeof (png_charp)));
  442. png_large_free(png_ptr, old_ptr);
  443. }
  444. else
  445. output_ptr = (png_charpp)png_large_malloc(png_ptr,
  446. max_output_ptr * sizeof (png_charp));
  447.          }
  448.          /* save the data */
  449.          output_ptr[num_output_ptr] = png_large_malloc(png_ptr,
  450.             png_ptr->zbuf_size);
  451.          png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
  452.             (png_size_t)png_ptr->zbuf_size);
  453.          num_output_ptr++;
  454.          /* and reset the buffer */
  455.          png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  456.          png_ptr->zstream->next_out = png_ptr->zbuf;
  457.       }
  458.    /* continue until we don't have anymore to compress */
  459.    } while (png_ptr->zstream->avail_in);
  460. /* finish the compression */
  461.    do
  462.    {
  463.       /* tell zlib we are finished */
  464. ret = deflate(png_ptr->zstream, Z_FINISH);
  465.       if (ret != Z_OK && ret != Z_STREAM_END)
  466.       {
  467.          /* we got an error */
  468.          if (png_ptr->zstream->msg)
  469.             png_error(png_ptr, png_ptr->zstream->msg);
  470.          else
  471.             png_error(png_ptr, "zlib error");
  472.       }
  473.       /* check to see if we need more room */
  474.       if (!png_ptr->zstream->avail_out && ret == Z_OK)
  475.       {
  476. /* check to make sure our output array has room */
  477. if (num_output_ptr >= max_output_ptr)
  478.          {
  479.             png_uint_32 old_max;
  480.             old_max = max_output_ptr;
  481.             max_output_ptr = num_output_ptr + 4;
  482.             if (output_ptr)
  483. {
  484. png_charpp old_ptr;
  485. old_ptr = output_ptr;
  486. output_ptr = (png_charpp)png_large_malloc(png_ptr,
  487. max_output_ptr * sizeof (png_charpp));
  488. png_memcpy(output_ptr, old_ptr,
  489. (png_size_t)(old_max * sizeof (png_charp)));
  490. png_large_free(png_ptr, old_ptr);
  491. }
  492. else
  493. output_ptr = (png_charpp)png_large_malloc(png_ptr,
  494. max_output_ptr * sizeof (png_charp));
  495. }
  496. /* save off the data */
  497. output_ptr[num_output_ptr] = png_large_malloc(png_ptr,
  498. png_ptr->zbuf_size);
  499. png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
  500.             (png_size_t)png_ptr->zbuf_size);
  501.          num_output_ptr++;
  502.          /* and reset the buffer pointers */
  503.          png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  504.          png_ptr->zstream->next_out = png_ptr->zbuf;
  505.       }
  506.    } while (ret != Z_STREAM_END);
  507.    /* text length is number of buffers plus last buffer */
  508. text_len = png_ptr->zbuf_size * num_output_ptr;
  509.    if (png_ptr->zstream->avail_out < png_ptr->zbuf_size)
  510.       text_len += (png_uint_32)(png_ptr->zbuf_size -
  511.          png_ptr->zstream->avail_out);
  512.    /* write start of chunk */
  513.    png_write_chunk_start(png_ptr, png_zTXt,
  514.       (png_uint_32)(key_len + text_len + 2));
  515.    /* write key */
  516.    png_write_chunk_data(png_ptr, (png_bytep )key, (png_uint_32)(key_len + 1));
  517. buf[0] = (png_byte)compression;
  518.    /* write compression */
  519.    png_write_chunk_data(png_ptr, (png_bytep )buf, (png_uint_32)1);
  520.    /* write saved output buffers, if any */
  521.    for (i = 0; i < num_output_ptr; i++)
  522.    {
  523.       png_write_chunk_data(png_ptr, (png_bytep )output_ptr[i], png_ptr->zbuf_size);
  524. png_large_free(png_ptr, output_ptr[i]);
  525.    }
  526.    if (max_output_ptr)
  527.       png_large_free(png_ptr, output_ptr);
  528.    /* write anything left in zbuf */
  529.    if (png_ptr->zstream->avail_out < png_ptr->zbuf_size)
  530.       png_write_chunk_data(png_ptr, png_ptr->zbuf,
  531.          png_ptr->zbuf_size - png_ptr->zstream->avail_out);
  532.    /* close the chunk */
  533.    png_write_chunk_end(png_ptr);
  534.    /* reset zlib for another zTXt or the image data */
  535. deflateReset(png_ptr->zstream);
  536. }
  537. #endif
  538. #if defined(PNG_WRITE_pHYs_SUPPORTED)
  539. /* write the pHYs chunk */
  540. void
  541. png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
  542.    png_uint_32 y_pixels_per_unit,
  543.    int unit_type)
  544. {
  545.    png_byte buf[9];
  546.    png_save_uint_32(buf, x_pixels_per_unit);
  547.    png_save_uint_32(buf + 4, y_pixels_per_unit);
  548. buf[8] = (png_byte)unit_type;
  549.    png_write_chunk(png_ptr, png_pHYs, buf, (png_uint_32)9);
  550. }
  551. #endif
  552. #if defined(PNG_WRITE_oFFs_SUPPORTED)
  553. /* write the oFFs chunk */
  554. void
  555. png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
  556.    png_uint_32 y_offset,
  557.    int unit_type)
  558. {
  559.    png_byte buf[9];
  560.    png_save_uint_32(buf, x_offset);
  561.    png_save_uint_32(buf + 4, y_offset);
  562. buf[8] = (png_byte)unit_type;
  563.    png_write_chunk(png_ptr, png_oFFs, buf, (png_uint_32)9);
  564. }
  565. #endif
  566. #if defined(PNG_WRITE_tIME_SUPPORTED)
  567. /* write the tIME chunk.  Use either png_convert_from_struct_tm()
  568.    or png_convert_from_time_t(), or fill in the structure yourself */
  569. void
  570. png_write_tIME(png_structp png_ptr, png_timep mod_time)
  571. {
  572.    png_byte buf[7];
  573.    png_save_uint_16(buf, mod_time->year);
  574.    buf[2] = mod_time->month;
  575.    buf[3] = mod_time->day;
  576.    buf[4] = mod_time->hour;
  577.    buf[5] = mod_time->minute;
  578.    buf[6] = mod_time->second;
  579.    png_write_chunk(png_ptr, png_tIME, buf, (png_uint_32)7);
  580. }
  581. #endif
  582. /* initializes the row writing capability of libpng */
  583. void
  584. png_write_start_row(png_structp png_ptr)
  585. {
  586.    /* set up row buffer */
  587.    png_ptr->row_buf = (png_bytep )png_large_malloc(png_ptr,
  588.       (((png_uint_32)png_ptr->usr_channels *
  589.       (png_uint_32)png_ptr->usr_bit_depth *
  590.       png_ptr->width + 7) >> 3) + 1);
  591.    /* set up filtering buffers, if filtering */
  592.    if (png_ptr->do_filter)
  593.    {
  594.       png_ptr->prev_row = (png_bytep )png_large_malloc(png_ptr,
  595.          png_ptr->rowbytes + 1);
  596.       png_memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  597.       png_ptr->save_row = (png_bytep )png_large_malloc(png_ptr,
  598.          png_ptr->rowbytes + 1);
  599.       png_memset(png_ptr->save_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  600.    }
  601.    /* if interlaced, we need to set up width and height of pass */
  602. if (png_ptr->interlaced)
  603.    {
  604.       if (!(png_ptr->transformations & PNG_INTERLACE))
  605.       {
  606.          png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  607.             png_pass_ystart[0]) / png_pass_yinc[0];
  608.          png_ptr->usr_width = (png_ptr->width +
  609.             png_pass_inc[0] - 1 -
  610.             png_pass_start[0]) /
  611.             png_pass_inc[0];
  612.       }
  613.       else
  614.       {
  615.          png_ptr->num_rows = png_ptr->height;
  616.          png_ptr->usr_width = png_ptr->width;
  617.       }
  618.    }
  619.    else
  620. {
  621.       png_ptr->num_rows = png_ptr->height;
  622.       png_ptr->usr_width = png_ptr->width;
  623.    }
  624.    png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  625.    png_ptr->zstream->next_out = png_ptr->zbuf;
  626. }
  627. /* Internal use only.   Called when finished processing a row of data */
  628. void
  629. png_write_finish_row(png_structp png_ptr)
  630. {
  631.    int ret;
  632.    /* next row */
  633.    png_ptr->row_number++;
  634.    /* see if we are done */
  635.    if (png_ptr->row_number < png_ptr->num_rows)
  636. return;
  637.    /* if interlaced, go to next pass */
  638.    if (png_ptr->interlaced)
  639.    {
  640.       png_ptr->row_number = 0;
  641.       if (png_ptr->transformations & PNG_INTERLACE)
  642.       {
  643.          png_ptr->pass++;
  644.       }
  645.       else
  646.       {
  647.          /* loop until we find a non-zero width or height pass */
  648.          do
  649.          {
  650.             png_ptr->pass++;
  651.             if (png_ptr->pass >= 7)
  652.                break;
  653. png_ptr->usr_width = (png_ptr->width +
  654.                png_pass_inc[png_ptr->pass] - 1 -
  655.                png_pass_start[png_ptr->pass]) /
  656.                png_pass_inc[png_ptr->pass];
  657.             png_ptr->num_rows = (png_ptr->height +
  658.                png_pass_yinc[png_ptr->pass] - 1 -
  659.                png_pass_ystart[png_ptr->pass]) /
  660.                png_pass_yinc[png_ptr->pass];
  661.             if (png_ptr->transformations & PNG_INTERLACE)
  662.                break;
  663.          } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
  664.       }
  665.       /* reset filter row */
  666.       if (png_ptr->prev_row)
  667.          png_memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  668.       /* if we have more data to get, go get it */
  669. if (png_ptr->pass < 7)
  670.          return;
  671.    }
  672.    /* if we get here, we've just written the last row, so we need
  673.       to flush the compressor */
  674.    do
  675.    {
  676.       /* tell the compressor we are done */
  677. ret = deflate(png_ptr->zstream, Z_FINISH);
  678.       /* check for an error */
  679.       if (ret != Z_OK && ret != Z_STREAM_END)
  680.       {
  681.          if (png_ptr->zstream->msg)
  682.             png_error(png_ptr, png_ptr->zstream->msg);
  683.          else
  684.             png_error(png_ptr, "zlib error");
  685.       }
  686. /* check to see if we need more room */
  687.       if (!png_ptr->zstream->avail_out && ret == Z_OK)
  688.       {
  689.          png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  690.          png_ptr->zstream->next_out = png_ptr->zbuf;
  691.          png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  692.       }
  693.    } while (ret != Z_STREAM_END);
  694.    /* write any extra space */
  695.    if (png_ptr->zstream->avail_out < png_ptr->zbuf_size)
  696.    {
  697.       png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
  698.          png_ptr->zstream->avail_out);
  699.    }
  700.    deflateReset(png_ptr->zstream);
  701. }
  702. #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
  703. /* pick out the correct pixels for the interlace pass.
  704.    The basic idea here is to go through the row with a source
  705.    pointer and a destination pointer (sp and dp), and copy the
  706.    correct pixels for the pass.  As the row gets compacted,
  707.    sp will always be >= dp, so we should never overwrite anything.
  708.    See the default: case for the easiest code to understand.
  709.    */
  710. void
  711. png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
  712. {
  713.    /* we don't have to do anything on the last pass (6) */
  714.    if (row && row_info && pass < 6)
  715.    {
  716.       /* each pixel depth is handled seperately */
  717.       switch (row_info->pixel_depth)
  718. {
  719.          case 1:
  720.          {
  721.             png_bytep sp;
  722.             png_bytep dp;
  723.             int shift;
  724.             int d;
  725.             int value;
  726.             png_uint_32 i;
  727.             dp = row;
  728.             d = 0;
  729.             shift = 7;
  730.             for (i = png_pass_start[pass];
  731.                i < row_info->width;
  732.                i += png_pass_inc[pass])
  733.             {
  734.                sp = row + (png_size_t)(i >> 3);
  735. value = (int)(*sp >> (7 - (int)(i & 7))) & 0x1;
  736.                d |= (value << shift);
  737.                if (shift == 0)
  738.                {
  739.                   shift = 7;
  740. *dp++ = (png_byte)d;
  741.                   d = 0;
  742.                }
  743.                else
  744.                   shift--;
  745.             }
  746.             if (shift != 7)
  747. *dp = (png_byte)d;
  748.             break;
  749.          }
  750.          case 2:
  751. {
  752.             png_bytep sp;
  753.             png_bytep dp;
  754.             int shift;
  755.             int d;
  756.             int value;
  757.             png_uint_32 i;
  758.             dp = row;
  759.             shift = 6;
  760.             d = 0;
  761.             for (i = png_pass_start[pass];
  762.                i < row_info->width;
  763.                i += png_pass_inc[pass])
  764.             {
  765.                sp = row + (png_size_t)(i >> 2);
  766.                value = (*sp >> ((3 - (int)(i & 3)) << 1)) & 0x3;
  767.                d |= (value << shift);
  768.                if (shift == 0)
  769.                {
  770.                   shift = 6;
  771. *dp++ = (png_byte)d;
  772.                   d = 0;
  773.                }
  774.                else
  775.                   shift -= 2;
  776.             }
  777.             if (shift != 6)
  778.  *dp = (png_byte)d;
  779.             break;
  780.          }
  781.          case 4:
  782.          {
  783.             png_bytep sp;
  784.             png_bytep dp;
  785. int shift;
  786.             int d;
  787.             int value;
  788.             png_uint_32 i;
  789.             dp = row;
  790.             shift = 4;
  791.             d = 0;
  792.             for (i = png_pass_start[pass];
  793.                i < row_info->width;
  794.                i += png_pass_inc[pass])
  795.             {
  796.                sp = row + (png_size_t)(i >> 1);
  797.                value = (*sp >> ((1 - (int)(i & 1)) << 2)) & 0xf;
  798.                d |= (value << shift);
  799.                if (shift == 0)
  800.                {
  801. shift = 4;
  802. *dp++ = (png_byte)d;
  803.                   d = 0;
  804.                }
  805.                else
  806.                   shift -= 4;
  807.             }
  808.             if (shift != 4)
  809. *dp = (png_byte)d;
  810.             break;
  811.          }
  812.          default:
  813.          {
  814.             png_bytep sp;
  815.             png_bytep dp;
  816.             png_uint_32 i;
  817.             int pixel_bytes;
  818. /* start at the beginning */
  819.             dp = row;
  820.             /* find out how many bytes each pixel takes up */
  821.             pixel_bytes = (row_info->pixel_depth >> 3);
  822.             /* loop through the row, only looking at the pixels that
  823.                matter */
  824.             for (i = png_pass_start[pass];
  825.                i < row_info->width;
  826.                i += png_pass_inc[pass])
  827.             {
  828.                /* find out where the original pixel is */
  829.                sp = row + (png_size_t)(i * pixel_bytes);
  830.                /* move the pixel */
  831.                if (dp != sp)
  832.                   png_memcpy(dp, sp, pixel_bytes);
  833.                /* next pixel */
  834.                dp += pixel_bytes;
  835.             }
  836. break;
  837.          }
  838.       }
  839.       /* set new row width */
  840.       row_info->width = (row_info->width +
  841.          png_pass_inc[pass] - 1 -
  842.          png_pass_start[pass]) /
  843.          png_pass_inc[pass];
  844.       row_info->rowbytes = ((row_info->width *
  845.          row_info->pixel_depth + 7) >> 3);
  846.    }
  847. }
  848. #endif
  849. /* this filters the row.  Both row and prev_row have space at the
  850.    first byte for the filter byte. */
  851. void
  852. png_write_filter_row(png_row_infop row_info, png_bytep row,
  853.    png_bytep prev_row)
  854. {
  855.    int minf, bpp;
  856.    png_uint_32 i, v;
  857.    png_uint_32 s0, s1, s2, s3, s4, mins;
  858. png_bytep rp, pp, cp, lp;
  859.    /* find out how many bytes offset each pixel is */
  860.    bpp = (row_info->pixel_depth + 7) / 8;
  861.    if (bpp < 1)
  862.       bpp = 1;
  863.    /* the prediction method we use is to find which method provides
  864.       the smallest value when summing the abs of the distances from
  865.       zero using anything >= 128 as negitive numbers. */
  866.    s0 = s1 = s2 = s3 = s4 = 0;
  867. for (i = 0, rp = row + 1, pp = prev_row + 1, lp = row + 1 - bpp,
  868.          cp = prev_row + 1 - bpp;
  869.       i < bpp; i++, rp++, pp++, lp++, cp++)
  870.    {
  871.       /* check none filter */
  872.       v = *rp;
  873.       if (v < 128)
  874.          s0 += v;
  875.       else
  876.          s0 += 256 - v;
  877.       /* check up filter */
  878.       v = (png_byte)(((int)*rp - (int)*pp) & 0xff);
  879.       if (v < 128)
  880.          s2 += v;
  881.       else
  882.          s2 += 256 - v;
  883.       /* check avg filter */
  884.       v = (png_byte)(((int)*rp - ((int)*pp / 2)) & 0xff);
  885.       if (v < 128)
  886.          s3 += v;
  887.       else
  888.          s3 += 256 - v;
  889.    }
  890.    /* some filters are same until we get past bpp */
  891.    s1 = s0;
  892.    s4 = s2;
  893.    for (; i < row_info->rowbytes; i++, rp++, pp++, lp++, cp++)
  894.    {
  895.       int a, b, c, pa, pb, pc, p;
  896. /* check none filter */
  897.       v = *rp;
  898.       if (v < 128)
  899.          s0 += v;
  900.       else
  901.          s0 += 256 - v;
  902.       /* check sub filter */
  903.       v = (png_byte)(((int)*rp - (int)*lp) & 0xff);
  904.       if (v < 128)
  905.          s1 += v;
  906.       else
  907.          s1 += 256 - v;
  908.       /* check up filter */
  909.       v = (png_byte)(((int)*rp - (int)*pp) & 0xff);
  910. if (v < 128)
  911.          s2 += v;
  912.       else
  913.          s2 += 256 - v;
  914.       /* check avg filter */
  915.       v = (png_byte)(((int)*rp - (((int)*pp + (int)*lp) / 2)) & 0xff);
  916.       if (v < 128)
  917.          s3 += v;
  918.       else
  919.          s3 += 256 - v;
  920.       /* check paeth filter */
  921.       b = *pp;
  922.       c = *cp;
  923.       a = *lp;
  924.       p = a + b - c;
  925. pa = abs(p - a);
  926.       pb = abs(p - b);
  927.       pc = abs(p - c);
  928.       if (pa <= pb && pa <= pc)
  929.          p = a;
  930.       else if (pb <= pc)
  931.          p = b;
  932.       else
  933.          p = c;
  934.       v = (png_byte)(((int)*rp - p) & 0xff);
  935.       if (v < 128)
  936.          s4 += v;
  937.       else
  938.          s4 += 256 - v;
  939.    }
  940.    mins = s0;
  941.    minf = 0;
  942.    if (s1 < mins)
  943.    {
  944. mins = s1;
  945.       minf = 1;
  946.    }
  947.    if (s2 < mins)
  948.    {
  949.       mins = s2;
  950.       minf = 2;
  951.    }
  952.    if (s3 < mins)
  953.    {
  954. mins = s3;
  955.       minf = 3;
  956.    }
  957.    if (s4 < mins)
  958.    {
  959.       minf = 4;
  960.    }
  961.    /* set filter byte */
  962. row[0] = (png_byte)minf;
  963.    /* do filter */
  964.    switch (minf)
  965.    {
  966.       /* sub filter */
  967.       case 1:
  968. for (i = bpp, rp = row + (png_size_t)row_info->rowbytes,
  969.             lp = row + (png_size_t)row_info->rowbytes - bpp;
  970.             i < row_info->rowbytes; i++, rp--, lp--)
  971.          {
  972.             *rp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
  973.          }
  974. break;
  975.       /* up filter */
  976.       case 2:
  977.          for (i = 0, rp = row + (png_size_t)row_info->rowbytes,
  978.             pp = prev_row + (png_size_t)row_info->rowbytes;
  979.             i < row_info->rowbytes; i++, rp--, pp--)
  980.          {
  981.             *rp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
  982.          }
  983.          break;
  984.       /* avg filter */
  985.       case 3:
  986. for (i = row_info->rowbytes,
  987.             rp = row + (png_size_t)row_info->rowbytes,
  988.             pp = prev_row + (png_size_t)row_info->rowbytes,
  989.             lp = row + (png_size_t)row_info->rowbytes - bpp;
  990.             i > bpp; i--, rp--, lp--, pp--)
  991.          {
  992.             *rp = (png_byte)(((int)*rp - (((int)*lp + (int)*pp) /
  993.                2)) & 0xff);
  994.          }
  995.          for (; i > 0; i--, rp--, pp--)
  996.          {
  997.             *rp = (png_byte)(((int)*rp - ((int)*pp / 2)) & 0xff);
  998.          }
  999.          break;
  1000.       /* paeth filter */
  1001.       case 4:
  1002.          for (i = row_info->rowbytes,
  1003.             rp = row + (png_size_t)row_info->rowbytes,
  1004. pp = prev_row + (png_size_t)row_info->rowbytes,
  1005.             lp = row + (png_size_t)row_info->rowbytes - bpp,
  1006.             cp = prev_row + (png_size_t)row_info->rowbytes - bpp;
  1007.             i > 0; i--, rp--, lp--, pp--, cp--)
  1008.          {
  1009.             int a, b, c, pa, pb, pc, p;
  1010.             b = *pp;
  1011.             if (i > bpp)
  1012.             {
  1013.                c = *cp;
  1014.                a = *lp;
  1015.             }
  1016.             else
  1017.             {
  1018.                a = c = 0;
  1019.             }
  1020.             p = a + b - c;
  1021. pa = abs(p - a);
  1022.             pb = abs(p - b);
  1023.             pc = abs(p - c);
  1024.             if (pa <= pb && pa <= pc)
  1025.                p = a;
  1026.             else if (pb <= pc)
  1027.                p = b;
  1028.             else
  1029.                p = c;
  1030.             *rp = (png_byte)(((int)*rp - p) & 0xff);
  1031.          }
  1032.          break;
  1033.    }
  1034. }