pngrutil.c
Upload User: zlh9724
Upload Date: 2007-01-04
Package Size: 1991k
Code Size: 34k
Category:

Browser Client

Development Platform:

Unix_Linux

  1. /* pngrutil.c - utilities to read 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. /* grab an uint 32 from a buffer */
  10. png_uint_32
  11. png_get_uint_32(png_bytep buf)
  12. {
  13.    png_uint_32 i;
  14.    i = ((png_uint_32)(*buf) << 24) +
  15.       ((png_uint_32)(*(buf + 1)) << 16) +
  16.       ((png_uint_32)(*(buf + 2)) << 8) +
  17.       (png_uint_32)(*(buf + 3));
  18.    return i;
  19. }
  20. /* grab an uint 16 from a buffer */
  21. png_uint_16
  22. png_get_uint_16(png_bytep buf)
  23. {
  24.    png_uint_16 i;
  25. i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
  26. (png_uint_16)(*(buf + 1)));
  27.    return i;
  28. }
  29. /* read data, and run it through the crc */
  30. void
  31. png_crc_read(png_structp png_ptr, png_bytep buf, png_uint_32 length)
  32. {
  33.    png_read_data(png_ptr, buf, length);
  34.    png_calculate_crc(png_ptr, buf, length);
  35. }
  36. /* skip data, but calcuate the crc anyway */
  37. void
  38. png_crc_skip(png_structp png_ptr, png_uint_32 length)
  39. {
  40.    png_uint_32 i;
  41.    for (i = length; i > png_ptr->zbuf_size; i -= png_ptr->zbuf_size)
  42.    {
  43.       png_read_data(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  44.       png_calculate_crc(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  45.    }
  46.    if (i)
  47.    {
  48.       png_read_data(png_ptr, png_ptr->zbuf, i);
  49.       png_calculate_crc(png_ptr, png_ptr->zbuf, i);
  50.    }
  51. }
  52. /* read and check the IDHR chunk */
  53. void
  54. png_handle_IHDR(png_structp png_ptr, png_infop info, png_uint_32 length)
  55. {
  56.    png_byte buf[13];
  57.    png_uint_32 width, height;
  58.    int bit_depth, color_type, compression_type, filter_type;
  59.    int interlace_type;
  60.    /* check the length */
  61.    if (length != 13)
  62.       png_error(png_ptr, "Invalid IHDR chunk");
  63.    png_crc_read(png_ptr, buf, 13);
  64.    width = png_get_uint_32(buf);
  65.    height = png_get_uint_32(buf + 4);
  66.    bit_depth = buf[8];
  67.    color_type = buf[9];
  68.    compression_type = buf[10];
  69.    filter_type = buf[11];
  70.    interlace_type = buf[12];
  71.    /* check for width and height valid values */
  72.    if (width == 0 || height == 0)
  73.       png_error(png_ptr, "Invalid Width or Height Found");
  74.    /* check other values */
  75.    if (bit_depth != 1 && bit_depth != 2 &&
  76.       bit_depth != 4 && bit_depth != 8 &&
  77.       bit_depth != 16)
  78.       png_error(png_ptr, "Invalid Bit Depth Found");
  79.    if (color_type < 0 || color_type == 1 ||
  80.       color_type == 5 || color_type > 6)
  81.       png_error(png_ptr, "Invalid Color Type Found");
  82. if (color_type == PNG_COLOR_TYPE_PALETTE &&
  83.       bit_depth == 16)
  84.       png_error(png_ptr, "Found Invalid Color Type and Bit Depth Combination");
  85.    if ((color_type == PNG_COLOR_TYPE_RGB ||
  86.       color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
  87.       color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
  88.       bit_depth < 8)
  89.       png_error(png_ptr, "Found Invalid Color Type and Bit Depth Combination");
  90.    if (interlace_type > 1)
  91.       png_error(png_ptr, "Found Invalid Interlace Value");
  92.    if (compression_type > 0)
  93.       png_error(png_ptr, "Found Invalid Compression Value");
  94.    if (filter_type > 0)
  95.       png_error(png_ptr, "Found Invalid Filter Value");
  96.    /* set internal variables */
  97.    png_ptr->width = width;
  98.    png_ptr->height = height;
  99. png_ptr->bit_depth = (png_byte)bit_depth;
  100. png_ptr->interlaced = (png_byte)interlace_type;
  101. png_ptr->color_type = (png_byte)color_type;
  102.    /* find number of channels */
  103.    switch (png_ptr->color_type)
  104.    {
  105.       case 0:
  106.       case 3:
  107.          png_ptr->channels = 1;
  108.          break;
  109.       case 2:
  110.          png_ptr->channels = 3;
  111.          break;
  112.       case 4:
  113.          png_ptr->channels = 2;
  114.          break;
  115.       case 6:
  116.          png_ptr->channels = 4;
  117.          break;
  118.    }
  119.    /* set up other useful info */
  120. png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
  121. png_ptr->channels);
  122.    png_ptr->rowbytes = ((png_ptr->width *
  123.       (png_uint_32)png_ptr->pixel_depth + 7) >> 3);
  124.    /* call the IHDR callback (which should just set up info) */
  125.    png_read_IHDR(png_ptr, info, width, height, bit_depth,
  126.       color_type, compression_type, filter_type, interlace_type);
  127. }
  128. /* read and check the palette */
  129. void
  130. png_handle_PLTE(png_structp png_ptr, png_infop info, png_uint_32 length)
  131. {
  132.    int num, i;
  133.    png_colorp palette;
  134.    if (length % 3)
  135.       png_error(png_ptr, "Invalid Palette Chunk");
  136.    num = (int)length / 3;
  137.    palette = (png_colorp)png_large_malloc(png_ptr, num * sizeof (png_color));
  138.    png_ptr->do_free |= PNG_FREE_PALETTE;
  139. for (i = 0; i < num; i++)
  140.    {
  141.       png_byte buf[3];
  142.       png_crc_read(png_ptr, buf, 3);
  143.       /* don't depend upon png_color being any order */
  144.       palette[i].red = buf[0];
  145.       palette[i].green = buf[1];
  146.       palette[i].blue = buf[2];
  147.    }
  148.    png_ptr->palette = palette;
  149. png_ptr->num_palette = (png_uint_16)num;
  150.    png_read_PLTE(png_ptr, info, palette, num);
  151. }
  152. #if defined(PNG_READ_gAMA_SUPPORTED)
  153. void
  154. png_handle_gAMA(png_structp png_ptr, png_infop info, png_uint_32 length)
  155. {
  156.    png_uint_32 igamma;
  157.    float gamma;
  158.    png_byte buf[4];
  159.    if (length != 4)
  160.    {
  161.       png_warning(png_ptr, "Incorrect gAMA chunk length");
  162. png_crc_skip(png_ptr, length);
  163.       return;
  164.    }
  165.    png_crc_read(png_ptr, buf, 4);
  166.    igamma = png_get_uint_32(buf);
  167.    /* check for zero gamma */
  168.    if (!igamma)
  169. return;
  170.    gamma = (float)igamma / (float)100000.0;
  171.    png_read_gAMA(png_ptr, info, gamma);
  172.    png_ptr->gamma = gamma;
  173. }
  174. #endif
  175. #if defined(PNG_READ_sBIT_SUPPORTED)
  176. void
  177. png_handle_sBIT(png_structp png_ptr, png_infop info, png_uint_32 length)
  178. {
  179.    int slen;
  180. png_byte buf[4];
  181.    buf[0] = buf[1] = buf[2] = buf[3] = 0;
  182.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  183.       slen = 3;
  184.    else
  185. slen = png_ptr->channels;
  186.    if (length != (png_uint_32)slen)
  187.    {
  188.       png_warning(png_ptr, "Incorrect sBIT chunk length");
  189. png_crc_skip(png_ptr, length);
  190. return;
  191.    }
  192.    png_crc_read(png_ptr, buf, length);
  193.    if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  194.    {
  195.       png_ptr->sig_bit.red = buf[0];
  196.       png_ptr->sig_bit.green = buf[1];
  197.       png_ptr->sig_bit.blue = buf[2];
  198.       png_ptr->sig_bit.alpha = buf[3];
  199.    }
  200.    else
  201.    {
  202. png_ptr->sig_bit.gray = buf[0];
  203.       png_ptr->sig_bit.alpha = buf[1];
  204.    }
  205.    png_read_sBIT(png_ptr, info, &(png_ptr->sig_bit));
  206. }
  207. #endif
  208. #if defined(PNG_READ_cHRM_SUPPORTED)
  209. void
  210. png_handle_cHRM(png_structp png_ptr, png_infop info, png_uint_32 length)
  211. {
  212.    png_byte buf[4];
  213.    png_uint_32 v;
  214.    float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  215.    if (length != 32)
  216.    {
  217.       png_warning(png_ptr, "Incorrect cHRM chunk length");
  218. png_crc_skip(png_ptr, length);
  219. return;
  220.    }
  221.    png_crc_read(png_ptr, buf, 4);
  222.    v = png_get_uint_32(buf);
  223.    white_x = (float)v / (float)100000.0;
  224.    png_crc_read(png_ptr, buf, 4);
  225.    v = png_get_uint_32(buf);
  226.    white_y = (float)v / (float)100000.0;
  227.    png_crc_read(png_ptr, buf, 4);
  228.    v = png_get_uint_32(buf);
  229.    red_x = (float)v / (float)100000.0;
  230.    png_crc_read(png_ptr, buf, 4);
  231.    v = png_get_uint_32(buf);
  232.    red_y = (float)v / (float)100000.0;
  233.    png_crc_read(png_ptr, buf, 4);
  234.    v = png_get_uint_32(buf);
  235.    green_x = (float)v / (float)100000.0;
  236.    png_crc_read(png_ptr, buf, 4);
  237.    v = png_get_uint_32(buf);
  238.    green_y = (float)v / (float)100000.0;
  239.    png_crc_read(png_ptr, buf, 4);
  240.    v = png_get_uint_32(buf);
  241.    blue_x = (float)v / (float)100000.0;
  242.    png_crc_read(png_ptr, buf, 4);
  243.    v = png_get_uint_32(buf);
  244.    blue_y = (float)v / (float)100000.0;
  245.    png_read_cHRM(png_ptr, info,
  246. white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  247. }
  248. #endif
  249. #if defined(PNG_READ_tRNS_SUPPORTED)
  250. void
  251. png_handle_tRNS(png_structp png_ptr, png_infop info, png_uint_32 length)
  252. {
  253.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  254.    {
  255.       if (length > png_ptr->num_palette)
  256.       {
  257.          png_warning(png_ptr, "Incorrect tRNS chunk length");
  258. png_crc_skip(png_ptr, length);
  259.          return;
  260.       }
  261.       png_ptr->trans = (png_bytep)png_large_malloc(png_ptr, length);
  262.       png_ptr->do_free |= PNG_FREE_TRANS;
  263. png_crc_read(png_ptr, png_ptr->trans, length);
  264. png_ptr->num_trans = (png_uint_16)length;
  265.    }
  266.    else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  267.    {
  268.       png_byte buf[6];
  269.       if (length != 6)
  270.       {
  271.          png_warning(png_ptr, "Incorrect tRNS chunk length");
  272. png_crc_skip(png_ptr, length);
  273.          return;
  274.       }
  275.       png_crc_read(png_ptr, buf, length);
  276.       png_ptr->num_trans = 3;
  277.       png_ptr->trans_values.red = png_get_uint_16(buf);
  278.       png_ptr->trans_values.green = png_get_uint_16(buf + 2);
  279.       png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
  280.    }
  281.    else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  282.    {
  283.       png_byte buf[6];
  284.       if (length != 2)
  285.       {
  286.          png_warning(png_ptr, "Incorrect tRNS chunk length");
  287. png_crc_skip(png_ptr, length);
  288. return;
  289. }
  290. png_crc_read(png_ptr, buf, 2);
  291. png_ptr->num_trans = 1;
  292. png_ptr->trans_values.gray = png_get_uint_16(buf);
  293. }
  294. else
  295.       png_warning(png_ptr, "Invalid tRNS chunk");
  296.    png_read_tRNS(png_ptr, info, png_ptr->trans, png_ptr->num_trans,
  297.       &(png_ptr->trans_values));
  298. }
  299. #endif
  300. #if defined(PNG_READ_bKGD_SUPPORTED)
  301. void
  302. png_handle_bKGD(png_structp png_ptr, png_infop info, png_uint_32 length)
  303. {
  304.    int truelen;
  305.    png_byte buf[6];
  306.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  307.       truelen = 1;
  308.    else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  309.       truelen = 6;
  310.    else
  311.       truelen = 2;
  312.    if (length != (png_uint_32)truelen)
  313.    {
  314. png_warning(png_ptr, "Incorrect bKGD chunk length");
  315. png_crc_skip(png_ptr, length);
  316.       return;
  317.    }
  318.    png_crc_read(png_ptr, buf, length);
  319.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  320.       png_ptr->background.index = buf[0];
  321.    else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
  322.       png_ptr->background.gray = png_get_uint_16(buf);
  323.    else
  324.    {
  325.       png_ptr->background.red = png_get_uint_16(buf);
  326.       png_ptr->background.green = png_get_uint_16(buf + 2);
  327.       png_ptr->background.blue = png_get_uint_16(buf + 4);
  328.    }
  329.    png_read_bKGD(png_ptr, info, &(png_ptr->background));
  330. }
  331. #endif
  332. #if defined(PNG_READ_hIST_SUPPORTED)
  333. void
  334. png_handle_hIST(png_structp png_ptr, png_infop info, png_uint_32 length)
  335. {
  336. int num, i;
  337. if (length != 2 * png_ptr->num_palette)
  338. {
  339. png_warning(png_ptr, "Incorrect hIST chunk length");
  340. png_crc_skip(png_ptr, length);
  341. return;
  342. }
  343.    num = (int)length / 2;
  344. png_ptr->hist = (png_uint_16p)png_large_malloc(png_ptr,
  345. num * sizeof (png_uint_16));
  346.    png_ptr->do_free |= PNG_FREE_HIST;
  347. for (i = 0; i < num; i++)
  348.    {
  349.       png_byte buf[2];
  350.       png_crc_read(png_ptr, buf, 2);
  351.       png_ptr->hist[i] = png_get_uint_16(buf);
  352.    }
  353.    png_read_hIST(png_ptr, info, png_ptr->hist);
  354. }
  355. #endif
  356. #if defined(PNG_READ_pHYs_SUPPORTED)
  357. void
  358. png_handle_pHYs(png_structp png_ptr, png_infop info, png_uint_32 length)
  359. {
  360.    png_byte buf[9];
  361.    png_uint_32 res_x, res_y;
  362.    int unit_type;
  363.    if (length != 9)
  364.    {
  365. png_warning(png_ptr, "Incorrect pHYs chunk length");
  366. png_crc_skip(png_ptr, length);
  367.       return;
  368. }
  369.    png_crc_read(png_ptr, buf, 9);
  370.    res_x = png_get_uint_32(buf);
  371.    res_y = png_get_uint_32(buf + 4);
  372.    unit_type = buf[8];
  373.    png_read_pHYs(png_ptr, info, res_x, res_y, unit_type);
  374. }
  375. #endif
  376. #if defined(PNG_READ_oFFs_SUPPORTED)
  377. void
  378. png_handle_oFFs(png_structp png_ptr, png_infop info, png_uint_32 length)
  379. {
  380.    png_byte buf[9];
  381.    png_uint_32 offset_x, offset_y;
  382.    int unit_type;
  383.    if (length != 9)
  384.    {
  385. png_warning(png_ptr, "Incorrect oFFs chunk length");
  386. png_crc_skip(png_ptr, length);
  387.       return;
  388.    }
  389. png_crc_read(png_ptr, buf, 9);
  390.    offset_x = png_get_uint_32(buf);
  391.    offset_y = png_get_uint_32(buf + 4);
  392.    unit_type = buf[8];
  393.    png_read_oFFs(png_ptr, info, offset_x, offset_y, unit_type);
  394. }
  395. #endif
  396. #if defined(PNG_READ_tIME_SUPPORTED)
  397. void
  398. png_handle_tIME(png_structp png_ptr, png_infop info, png_uint_32 length)
  399. {
  400.    png_byte buf[7];
  401.    png_time mod_time;
  402.    if (length != 7)
  403.    {
  404. png_warning(png_ptr, "Incorrect tIME chunk length");
  405. png_crc_skip(png_ptr, length);
  406.       return;
  407.    }
  408.    png_crc_read(png_ptr, buf, 7);
  409.    mod_time.second = buf[6];
  410.    mod_time.minute = buf[5];
  411.    mod_time.hour = buf[4];
  412.    mod_time.day = buf[3];
  413.    mod_time.month = buf[2];
  414. mod_time.year = png_get_uint_16(buf);
  415.    png_read_tIME(png_ptr, info, &mod_time);
  416. }
  417. #endif
  418. #if defined(PNG_READ_tEXt_SUPPORTED)
  419. /* note: this does not correctly handle chunks that are > 64K */
  420. void
  421. png_handle_tEXt(png_structp png_ptr, png_infop info, png_uint_32 length)
  422. {
  423. png_charp key;
  424.    png_charp text;
  425. key = (png_charp )png_large_malloc(png_ptr, length + 1);
  426.    png_crc_read(png_ptr, (png_bytep )key, length);
  427. key[(png_size_t)length] = '';
  428.    for (text = key; *text; text++)
  429.       /* empty loop */ ;
  430.    if (text != key + (png_size_t)length)
  431.       text++;
  432.    png_read_tEXt(png_ptr, info, key, text, length - (text - key));
  433. }
  434. #endif
  435. #if defined(PNG_READ_zTXt_SUPPORTED)
  436. /* note: this does not correctly handle chunks that are > 64K compressed */
  437. void
  438. png_handle_zTXt(png_structp png_ptr, png_infop info, png_uint_32 length)
  439. {
  440.    png_charp key;
  441.    png_charp text;
  442.    int ret;
  443.    png_uint_32 text_size, key_size;
  444. key = png_large_malloc(png_ptr, length + 1);
  445. png_crc_read(png_ptr, (png_bytep )key, length);
  446.    key[(png_size_t)length] = '';
  447.    for (text = key; *text; text++)
  448.       /* empty loop */ ;
  449.    /* zTXt can't have zero text */
  450.    if (text == key + (png_size_t)length)
  451.    {
  452.       png_warning(png_ptr, "Zero length zTXt chunk");
  453. png_large_free(png_ptr, key);
  454.       return;
  455.    }
  456.    text++;
  457.    if (*text) /* check compression byte */
  458.    {
  459.       png_large_free(png_ptr, key);
  460.       return;
  461.    }
  462.    text++;
  463.    png_ptr->zstream->next_in = (png_bytep )text;
  464.    png_ptr->zstream->avail_in = (uInt)(length - (text - key));
  465.    png_ptr->zstream->next_out = png_ptr->zbuf;
  466.    png_ptr->zstream->avail_out = (png_size_t)png_ptr->zbuf_size;
  467.    key_size = text - key;
  468.    text_size = 0;
  469. text = NULL;
  470.    ret = Z_STREAM_END;
  471.    while (png_ptr->zstream->avail_in)
  472.    {
  473. ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH);
  474.       if (ret != Z_OK && ret != Z_STREAM_END)
  475.       {
  476. if (png_ptr->zstream->msg)
  477. png_warning(png_ptr, png_ptr->zstream->msg);
  478.          else
  479. png_warning(png_ptr, "zTXt decompression error");
  480. inflateReset(png_ptr->zstream);
  481.          png_ptr->zstream->avail_in = 0;
  482.          png_large_free(png_ptr, key);
  483.          png_large_free(png_ptr, text);
  484.          return;
  485.       }
  486.       if (!png_ptr->zstream->avail_out || ret == Z_STREAM_END)
  487.       {
  488.          if (!text)
  489.          {
  490.             text = (png_charp)png_large_malloc(png_ptr,
  491.                png_ptr->zbuf_size - png_ptr->zstream->avail_out +
  492.                   key_size + 1);
  493.             png_memcpy(text + (png_size_t)key_size, png_ptr->zbuf,
  494.                (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream->avail_out));
  495.             png_memcpy(text, key, (png_size_t)key_size);
  496.             text_size = key_size + (png_size_t)png_ptr->zbuf_size -
  497.                png_ptr->zstream->avail_out;
  498.             *(text + (png_size_t)text_size) = '';
  499.          }
  500.          else
  501.          {
  502. png_charp tmp;
  503.             tmp = text;
  504.             text = png_large_malloc(png_ptr, text_size +
  505.                png_ptr->zbuf_size - png_ptr->zstream->avail_out + 1);
  506.             png_memcpy(text, tmp, (png_size_t)text_size);
  507.             png_large_free(png_ptr, tmp);
  508.             png_memcpy(text + (png_size_t)text_size, png_ptr->zbuf,
  509.                (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream->avail_out));
  510.             text_size += png_ptr->zbuf_size - png_ptr->zstream->avail_out;
  511.             *(text + (png_size_t)text_size) = '';
  512.          }
  513.          if (ret != Z_STREAM_END)
  514.          {
  515.             png_ptr->zstream->next_out = png_ptr->zbuf;
  516.             png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
  517.          }
  518.       }
  519.       else
  520.       {
  521.          break;
  522.       }
  523.       if (ret == Z_STREAM_END)
  524.          break;
  525.    }
  526. inflateReset(png_ptr->zstream);
  527.    png_ptr->zstream->avail_in = 0;
  528.    if (ret != Z_STREAM_END)
  529.    {
  530.       png_large_free(png_ptr, key);
  531.       png_large_free(png_ptr, text);
  532.       return;
  533.    }
  534.    png_large_free(png_ptr, key);
  535.    key = text;
  536.    text += (png_size_t)key_size;
  537.    text_size -= key_size;
  538.    png_read_zTXt(png_ptr, info, key, text, text_size, 0);
  539. }
  540. #endif
  541. /* Combines the row recently read in with the previous row.
  542.    This routine takes care of alpha and transparency if requested.
  543.    This routine also handles the two methods of progressive display
  544.    of interlaced images, depending on the mask value.
  545.    The mask value describes which pixels are to be combined with
  546.    the row.  The pattern always repeats every 8 pixels, so just 8
  547.    bits are needed.  A one indicates the pixels is to be combined,
  548.    a zero indicates the pixel is to be skipped.  This is in addition
  549.    to any alpha or transparency value associated with the pixel.  If
  550.    you want all pixels to be combined, pass 0xff (255) in mask.
  551. */
  552. void
  553. png_combine_row(png_structp png_ptr, png_bytep row,
  554.    int mask)
  555. {
  556.    if (mask == 0xff)
  557.    {
  558.       png_memcpy(row, png_ptr->row_buf + 1,
  559.          (png_size_t)((png_ptr->width *
  560.          png_ptr->row_info.pixel_depth + 7) >> 3));
  561.    }
  562.    else
  563.    {
  564.       switch (png_ptr->row_info.pixel_depth)
  565.       {
  566.          case 1:
  567.          {
  568.             png_bytep sp;
  569.             png_bytep dp;
  570.             int m;
  571.             int shift;
  572.             png_uint_32 i;
  573.             int value;
  574.             sp = png_ptr->row_buf + 1;
  575.             dp = row;
  576.             shift = 7;
  577.             m = 0x80;
  578.             for (i = 0; i < png_ptr->width; i++)
  579.             {
  580.                if (m & mask)
  581.                {
  582.                   value = (*sp >> shift) & 0x1;
  583.                   *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
  584. *dp |= (png_byte)(value << shift);
  585.                }
  586.                if (shift == 0)
  587.                {
  588.                   shift = 7;
  589.                   sp++;
  590.                   dp++;
  591.                }
  592.                else
  593.                   shift--;
  594.                if (m == 1)
  595.                   m = 0x80;
  596.                else
  597.                   m >>= 1;
  598.             }
  599.             break;
  600.          }
  601.          case 2:
  602.          {
  603.             png_bytep sp;
  604.             png_bytep dp;
  605.             int m;
  606.             int shift;
  607.             png_uint_32 i;
  608.             int value;
  609.             sp = png_ptr->row_buf + 1;
  610.             dp = row;
  611.             shift = 6;
  612.             m = 0x80;
  613.             for (i = 0; i < png_ptr->width; i++)
  614.             {
  615.                if (m & mask)
  616.                {
  617.                   value = (*sp >> shift) & 0x3;
  618.                   *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  619. *dp |= (png_byte)(value << shift);
  620.                }
  621.                if (shift == 0)
  622.                {
  623.                   shift = 6;
  624.                   sp++;
  625.                   dp++;
  626.                }
  627.                else
  628.                   shift -= 2;
  629.                if (m == 1)
  630.                   m = 0x80;
  631.                else
  632.                   m >>= 1;
  633.             }
  634.             break;
  635.          }
  636.          case 4:
  637.          {
  638.             png_bytep sp;
  639.             png_bytep dp;
  640.             int m;
  641.             int shift;
  642.             png_uint_32 i;
  643.             int value;
  644.             sp = png_ptr->row_buf + 1;
  645.             dp = row;
  646.             shift = 4;
  647.             m = 0x80;
  648.             for (i = 0; i < png_ptr->width; i++)
  649.             {
  650.                if (m & mask)
  651.                {
  652.                   value = (*sp >> shift) & 0xf;
  653.                   *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  654. *dp |= (png_byte)(value << shift);
  655.                }
  656.                if (shift == 0)
  657.                {
  658.                   shift = 4;
  659.                   sp++;
  660.                   dp++;
  661.                }
  662.                else
  663.                   shift -= 4;
  664.                if (m == 1)
  665.                   m = 0x80;
  666.                else
  667.                   m >>= 1;
  668.             }
  669.             break;
  670.          }
  671.          default:
  672.          {
  673.             png_bytep sp;
  674.             png_bytep dp;
  675.             png_uint_32 i;
  676.             int pixel_bytes, m;
  677.             pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
  678.             sp = png_ptr->row_buf + 1;
  679.             dp = row;
  680.             m = 0x80;
  681.             for (i = 0; i < png_ptr->width; i++)
  682.             {
  683.                if (m & mask)
  684.                {
  685.                   png_memcpy(dp, sp, pixel_bytes);
  686.                }
  687.                sp += pixel_bytes;
  688.                dp += pixel_bytes;
  689.                if (m == 1)
  690.                   m = 0x80;
  691.                else
  692.                   m >>= 1;
  693.             }
  694.             break;
  695.          }
  696.       }
  697.    }
  698. }
  699. #if defined(PNG_READ_INTERLACING_SUPPORTED)
  700. void
  701. png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass)
  702. {
  703.    if (row && row_info)
  704.    {
  705.       png_uint_32 final_width;
  706.       final_width = row_info->width * png_pass_inc[pass];
  707.       switch (row_info->pixel_depth)
  708.       {
  709.          case 1:
  710.          {
  711.             png_bytep sp, dp;
  712.             int sshift, dshift;
  713.             png_byte v;
  714.             png_uint_32 i;
  715.             int j;
  716.             sp = row + (png_size_t)((row_info->width - 1) >> 3);
  717.             sshift = 7 - (int)((row_info->width + 7) & 7);
  718.             dp = row + (png_size_t)((final_width - 1) >> 3);
  719.             dshift = 7 - (int)((final_width + 7) & 7);
  720.             for (i = row_info->width; i; i--)
  721.             {
  722. v = (png_byte)((*sp >> sshift) & 0x1);
  723.                for (j = 0; j < png_pass_inc[pass]; j++)
  724.                {
  725.                   *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
  726.                   *dp |= (png_byte)(v << dshift);
  727.                   if (dshift == 7)
  728.                   {
  729.                      dshift = 0;
  730.                      dp--;
  731.                   }
  732.                   else
  733.                      dshift++;
  734.                }
  735.                if (sshift == 7)
  736.                {
  737.                   sshift = 0;
  738.                   sp--;
  739.                }
  740.                else
  741.                   sshift++;
  742.             }
  743.             break;
  744.          }
  745.          case 2:
  746.          {
  747.             png_bytep sp, dp;
  748.             int sshift, dshift;
  749.             png_byte v;
  750.             png_uint_32 i, j;
  751.             sp = row + (png_size_t)((row_info->width - 1) >> 2);
  752.             sshift = (png_size_t)((3 - ((row_info->width + 3) & 3)) << 1);
  753.             dp = row + (png_size_t)((final_width - 1) >> 2);
  754.             dshift = (png_size_t)((3 - ((final_width + 3) & 3)) << 1);
  755.             for (i = row_info->width; i; i--)
  756.             {
  757. v = (png_byte)((*sp >> sshift) & 0x3);
  758.                for (j = 0; j < png_pass_inc[pass]; j++)
  759.                {
  760.                   *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
  761. *dp |= (png_byte)(v << dshift);
  762.                   if (dshift == 6)
  763.                   {
  764.                      dshift = 0;
  765.                      dp--;
  766.                   }
  767.                   else
  768.                      dshift += 2;
  769.                }
  770.                if (sshift == 6)
  771.                {
  772.                   sshift = 0;
  773.                   sp--;
  774.                }
  775.                else
  776.                   sshift += 2;
  777.             }
  778.             break;
  779.          }
  780.          case 4:
  781.          {
  782.             png_bytep sp, dp;
  783.             int sshift, dshift;
  784.             png_byte v;
  785.             png_uint_32 i;
  786.             int j;
  787.             sp = row + (png_size_t)((row_info->width - 1) >> 1);
  788.             sshift = (png_size_t)((1 - ((row_info->width + 1) & 1)) << 2);
  789.             dp = row + (png_size_t)((final_width - 1) >> 1);
  790.             dshift = (png_size_t)((1 - ((final_width + 1) & 1)) << 2);
  791.             for (i = row_info->width; i; i--)
  792.             {
  793. v = (png_byte)((*sp >> sshift) & 0xf);
  794.                for (j = 0; j < png_pass_inc[pass]; j++)
  795.                {
  796.                   *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
  797.                   *dp |= (png_byte)(v << dshift);
  798.                   if (dshift == 4)
  799.                   {
  800.                      dshift = 0;
  801.                      dp--;
  802.                   }
  803.                   else
  804.                      dshift = 4;
  805.                }
  806.                if (sshift == 4)
  807.                {
  808.                   sshift = 0;
  809.                   sp--;
  810.                }
  811.                else
  812.                   sshift = 4;
  813.             }
  814.             break;
  815.          }
  816.          default:
  817.          {
  818.             png_bytep sp, dp;
  819.             png_byte v[8];
  820.             png_uint_32 i;
  821.             int j;
  822.             int pixel_bytes;
  823.             pixel_bytes = (row_info->pixel_depth >> 3);
  824.             sp = row + (png_size_t)((row_info->width - 1) * pixel_bytes);
  825.             dp = row + (png_size_t)((final_width - 1) * pixel_bytes);
  826.             for (i = row_info->width; i; i--)
  827.             {
  828.                png_memcpy(v, sp, pixel_bytes);
  829.                for (j = 0; j < png_pass_inc[pass]; j++)
  830.                {
  831.                   png_memcpy(dp, v, pixel_bytes);
  832.                   dp -= pixel_bytes;
  833.                }
  834.                sp -= pixel_bytes;
  835.             }
  836.             break;
  837.          }
  838.       }
  839.       row_info->width = final_width;
  840.       row_info->rowbytes = ((final_width *
  841.          (png_uint_32)row_info->pixel_depth + 7) >> 3);
  842.    }
  843. }
  844. #endif
  845. void
  846. png_read_filter_row(png_row_infop row_info, png_bytep row,
  847.    png_bytep prev_row, int filter)
  848. {
  849.    switch (filter)
  850.    {
  851.       case 0:
  852.          break;
  853.       case 1:
  854.       {
  855.          png_uint_32 i;
  856.          int bpp;
  857.          png_bytep rp;
  858.          png_bytep lp;
  859.          bpp = (row_info->pixel_depth + 7) / 8;
  860.          for (i = (png_uint_32)bpp, rp = row + bpp, lp = row;
  861.             i < row_info->rowbytes; i++, rp++, lp++)
  862.          {
  863.             *rp = (png_byte)(((int)(*rp) + (int)(*lp)) & 0xff);
  864.          }
  865.          break;
  866.       }
  867.       case 2:
  868.       {
  869.          png_uint_32 i;
  870.          png_bytep rp;
  871.          png_bytep pp;
  872.          for (i = 0, rp = row, pp = prev_row;
  873.             i < row_info->rowbytes; i++, rp++, pp++)
  874.          {
  875.             *rp = (png_byte)(((int)(*rp) + (int)(*pp)) & 0xff);
  876.          }
  877.          break;
  878.       }
  879.       case 3:
  880.       {
  881.          png_uint_32 i;
  882.          int bpp;
  883.          png_bytep rp;
  884.          png_bytep pp;
  885.          png_bytep lp;
  886.          bpp = (row_info->pixel_depth + 7) / 8;
  887.          for (i = 0, rp = row, pp = prev_row;
  888.             i < (png_uint_32)bpp; i++, rp++, pp++)
  889.          {
  890.             *rp = (png_byte)(((int)(*rp) +
  891.                ((int)(*pp) / 2)) & 0xff);
  892.          }
  893.          for (lp = row; i < row_info->rowbytes; i++, rp++, lp++, pp++)
  894.          {
  895.             *rp = (png_byte)(((int)(*rp) +
  896.                (int)(*pp + *lp) / 2) & 0xff);
  897.          }
  898.          break;
  899.       }
  900.       case 4:
  901.       {
  902.          int bpp;
  903.          png_uint_32 i;
  904.          png_bytep rp;
  905.          png_bytep pp;
  906.          png_bytep lp;
  907.          png_bytep cp;
  908.          bpp = (row_info->pixel_depth + 7) / 8;
  909.          for (i = 0, rp = row, pp = prev_row,
  910.             lp = row - bpp, cp = prev_row - bpp;
  911.             i < row_info->rowbytes; i++, rp++, pp++, lp++, cp++)
  912.          {
  913.             int a, b, c, pa, pb, pc, p;
  914.             b = *pp;
  915.             if (i >= (png_uint_32)bpp)
  916.             {
  917.                c = *cp;
  918.                a = *lp;
  919.             }
  920.             else
  921.             {
  922.                a = c = 0;
  923.             }
  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.             *rp = (png_byte)(((int)(*rp) + p) & 0xff);
  935.          }
  936.          break;
  937.       }
  938.       default:
  939.          break;
  940.    }
  941. }
  942. void
  943. png_read_finish_row(png_structp png_ptr)
  944. {
  945.    png_ptr->row_number++;
  946.    if (png_ptr->row_number < png_ptr->num_rows)
  947.       return;
  948.    if (png_ptr->interlaced)
  949.    {
  950.       png_ptr->row_number = 0;
  951.       png_memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  952.       do
  953.       {
  954.          png_ptr->pass++;
  955.          if (png_ptr->pass >= 7)
  956.             break;
  957.          png_ptr->iwidth = (png_ptr->width +
  958.             png_pass_inc[png_ptr->pass] - 1 -
  959.             png_pass_start[png_ptr->pass]) /
  960.             png_pass_inc[png_ptr->pass];
  961.          png_ptr->irowbytes = ((png_ptr->iwidth *
  962.             png_ptr->pixel_depth + 7) >> 3) + 1;
  963.          if (!(png_ptr->transformations & PNG_INTERLACE))
  964.          {
  965.             png_ptr->num_rows = (png_ptr->height +
  966.                png_pass_yinc[png_ptr->pass] - 1 -
  967.                png_pass_ystart[png_ptr->pass]) /
  968.                png_pass_yinc[png_ptr->pass];
  969.             if (!(png_ptr->num_rows))
  970.                continue;
  971.          }
  972.          if (png_ptr->transformations & PNG_INTERLACE)
  973.             break;
  974.       } while (png_ptr->iwidth == 0);
  975.       if (png_ptr->pass < 7)
  976.          return;
  977.    }
  978.    if (!png_ptr->zlib_finished)
  979.    {
  980.       char extra;
  981.       int ret;
  982.       png_ptr->zstream->next_out = (Byte *)&extra;
  983.       png_ptr->zstream->avail_out = (uInt)1;
  984.       do
  985.       {
  986.          if (!(png_ptr->zstream->avail_in))
  987.          {
  988.             while (!png_ptr->idat_size)
  989.             {
  990.                png_byte buf[4];
  991.                png_uint_32 crc;
  992.                png_read_data(png_ptr, buf, 4);
  993.                crc = png_get_uint_32(buf);
  994.                if (((crc ^ 0xffffffffL) & 0xffffffffL) !=
  995.                   (png_ptr->crc & 0xffffffffL))
  996.                   png_error(png_ptr, "Bad CRC value");
  997.                png_read_data(png_ptr, buf, 4);
  998.                png_ptr->idat_size = png_get_uint_32(buf);
  999.                png_reset_crc(png_ptr);
  1000.                png_crc_read(png_ptr, buf, 4);
  1001.                if (png_memcmp(buf, png_IDAT, 4))
  1002.                   png_error(png_ptr, "Not enough image data");
  1003.             }
  1004.             png_ptr->zstream->avail_in = (uInt)png_ptr->zbuf_size;
  1005.             png_ptr->zstream->next_in = png_ptr->zbuf;
  1006.             if (png_ptr->zbuf_size > png_ptr->idat_size)
  1007.                png_ptr->zstream->avail_in = (uInt)png_ptr->idat_size;
  1008.             png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream->avail_in);
  1009.             png_ptr->idat_size -= png_ptr->zstream->avail_in;
  1010.          }
  1011. ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH);
  1012.          if (ret == Z_STREAM_END)
  1013.          {
  1014.             if (!(png_ptr->zstream->avail_out) || png_ptr->zstream->avail_in ||
  1015.                png_ptr->idat_size)
  1016.                png_error(png_ptr, "Extra compressed data");
  1017.             png_ptr->mode = PNG_AT_LAST_IDAT;
  1018.             break;
  1019.          }
  1020.          if (ret != Z_OK)
  1021.             png_error(png_ptr, "Compression Error");
  1022.          if (!(png_ptr->zstream->avail_out))
  1023.             png_error(png_ptr, "Extra compressed data");
  1024.       } while (1);
  1025.       png_ptr->zstream->avail_out = 0;
  1026.    }
  1027.    if (png_ptr->idat_size || png_ptr->zstream->avail_in)
  1028.       png_error(png_ptr, "Extra compression data");
  1029.    inflateReset(png_ptr->zstream);
  1030.    png_ptr->mode = PNG_AT_LAST_IDAT;
  1031. }
  1032. void
  1033. png_read_start_row(png_structp png_ptr)
  1034. {
  1035.    int max_pixel_depth;
  1036.    png_uint_32 rowbytes;
  1037.    png_ptr->zstream->avail_in = 0;
  1038.    png_init_read_transformations(png_ptr);
  1039.    if (png_ptr->interlaced)
  1040.    {
  1041.       if (!(png_ptr->transformations & PNG_INTERLACE))
  1042.          png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  1043.             png_pass_ystart[0]) / png_pass_yinc[0];
  1044.       else
  1045.          png_ptr->num_rows = png_ptr->height;
  1046.       png_ptr->iwidth = (png_ptr->width +
  1047.          png_pass_inc[png_ptr->pass] - 1 -
  1048.          png_pass_start[png_ptr->pass]) /
  1049.          png_pass_inc[png_ptr->pass];
  1050.       png_ptr->irowbytes = ((png_ptr->iwidth *
  1051.          png_ptr->pixel_depth + 7) >> 3) + 1;
  1052.    }
  1053.    else
  1054.    {
  1055.       png_ptr->num_rows = png_ptr->height;
  1056.       png_ptr->iwidth = png_ptr->width;
  1057.       png_ptr->irowbytes = png_ptr->rowbytes + 1;
  1058.    }
  1059.    max_pixel_depth = png_ptr->pixel_depth;
  1060. #if defined(PNG_READ_PACK_SUPPORTED)
  1061.    if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
  1062.    {
  1063.       max_pixel_depth = 8;
  1064.    }
  1065. #endif
  1066. #if defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_PACK_SUPPORTED)
  1067.    if (png_ptr->transformations & (PNG_EXPAND | PNG_PACK))
  1068.    {
  1069.       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1070.       {
  1071.          if (png_ptr->num_trans)
  1072.             max_pixel_depth = 32;
  1073.          else
  1074.             max_pixel_depth = 24;
  1075.       }
  1076.       else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  1077.       {
  1078.          if (max_pixel_depth < 8)
  1079.             max_pixel_depth = 8;
  1080.          if (png_ptr->num_trans)
  1081.             max_pixel_depth *= 2;
  1082.       }
  1083.       else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1084.       {
  1085.          if (png_ptr->num_trans)
  1086.          {
  1087.             max_pixel_depth *= 4;
  1088.             max_pixel_depth /= 3;
  1089.          }
  1090.       }
  1091.    }
  1092. #endif
  1093. #if defined(PNG_READ_FILLER_SUPPORTED)
  1094.    if (png_ptr->transformations & (PNG_FILLER))
  1095.    {
  1096.       if (max_pixel_depth < 32)
  1097.          max_pixel_depth = 32;
  1098.    }
  1099. #endif
  1100. #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  1101.    if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  1102.    {
  1103.       if ((png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
  1104.          png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  1105.       {
  1106.          if (max_pixel_depth <= 16)
  1107.             max_pixel_depth = 32;
  1108.          else if (max_pixel_depth <= 32)
  1109.             max_pixel_depth = 64;
  1110.       }
  1111.       else
  1112.       {
  1113.          if (max_pixel_depth <= 8)
  1114.             max_pixel_depth = 24;
  1115.          else if (max_pixel_depth <= 16)
  1116.             max_pixel_depth = 48;
  1117.       }
  1118.    }
  1119. #endif
  1120.    /* align the width on the next larger 8 pixels.  Mainly used
  1121.       for interlacing */
  1122.    rowbytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
  1123.    /* calculate the maximum bytes needed, adding a byte and a pixel
  1124.       for safety sake */
  1125.    rowbytes = ((rowbytes * (png_uint_32)max_pixel_depth + 7) >> 3) +
  1126.       1 + ((max_pixel_depth + 7) >> 3);
  1127. #ifdef PNG_MAX_MALLOC_64K
  1128.    if (rowbytes > 65536L)
  1129.       png_error(png_ptr, "This image requires a row greater then 64KB");
  1130. #endif
  1131. png_ptr->row_buf = (png_bytep )png_large_malloc(png_ptr, rowbytes);
  1132. #ifdef PNG_MAX_MALLOC_64K
  1133.    if (png_ptr->rowbytes + 1 > 65536L)
  1134. png_error(png_ptr, "This image requires a row greater then 64KB");
  1135. #endif
  1136.    png_ptr->prev_row = png_large_malloc(png_ptr,
  1137.       png_ptr->rowbytes + 1);
  1138.    png_memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1);
  1139.    png_ptr->row_init = 1;
  1140. }