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

  1.  
  2. /* pngrutil.c - utilities to read a PNG file
  3.  *
  4.  * libpng 1.0.11 - April 27, 2001
  5.  * For conditions of distribution and use, see copyright notice in png.h
  6.  * Copyright (c) 1998-2001 Glenn Randers-Pehrson
  7.  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  8.  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  9.  *
  10.  * This file contains routines that are only called from within
  11.  * libpng itself during the course of reading an image.
  12.  */
  13.  
  14. #define PNG_INTERNAL
  15. #include "png.h"
  16.  
  17. #if defined(_WIN32_WCE)
  18. /* strtod() function is not supported on WindowsCE */
  19. #  ifdef PNG_FLOATING_POINT_SUPPORTED
  20. __inline double strtod(const char *nptr, char **endptr)
  21. {
  22.    double result = 0;
  23.    int len;
  24.    wchar_t *str, *end;
  25.  
  26.    len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
  27.    str = (wchar_t *)malloc(len * sizeof(wchar_t));
  28.    if ( NULL != str )
  29.    {
  30.       MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len);
  31.       result = wcstod(str, &end);
  32.       len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL);
  33.       *endptr = (char *)nptr + (png_strlen(nptr) - len + 1);
  34.       free(str);
  35.    }
  36.    return result;
  37. }
  38. #  endif
  39. #endif
  40.  
  41. #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
  42. /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
  43. png_uint_32 /* PRIVATE */
  44. png_get_uint_32(png_bytep buf)
  45. {
  46.    png_uint_32 i = ((png_uint_32)(*buf) << 24) +
  47.       ((png_uint_32)(*(buf + 1)) << 16) +
  48.       ((png_uint_32)(*(buf + 2)) << 8) +
  49.       (png_uint_32)(*(buf + 3));
  50.  
  51.    return (i);
  52. }
  53.  
  54. #if defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_oFFs_SUPPORTED)
  55. /* Grab a signed 32-bit integer from a buffer in big-endian format.  The
  56.  * data is stored in the PNG file in two's complement format, and it is
  57.  * assumed that the machine format for signed integers is the same. */
  58. png_int_32 /* PRIVATE */
  59. png_get_int_32(png_bytep buf)
  60. {
  61.    png_int_32 i = ((png_int_32)(*buf) << 24) +
  62.       ((png_int_32)(*(buf + 1)) << 16) +
  63.       ((png_int_32)(*(buf + 2)) << 8) +
  64.       (png_int_32)(*(buf + 3));
  65.  
  66.    return (i);
  67. }
  68. #endif /* PNG_READ_pCAL_SUPPORTED */
  69.  
  70. /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
  71. png_uint_16 /* PRIVATE */
  72. png_get_uint_16(png_bytep buf)
  73. {
  74.    png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
  75.       (png_uint_16)(*(buf + 1)));
  76.  
  77.    return (i);
  78. }
  79. #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */
  80.  
  81. /* Read data, and (optionally) run it through the CRC. */
  82. void /* PRIVATE */
  83. png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
  84. {
  85.    png_read_data(png_ptr, buf, length);
  86.    png_calculate_crc(png_ptr, buf, length);
  87. }
  88.  
  89. /* Optionally skip data and then check the CRC.  Depending on whether we
  90.    are reading a ancillary or critical chunk, and how the program has set
  91.    things up, we may calculate the CRC on the data and print a message.
  92.    Returns '1' if there was a CRC error, '0' otherwise. */
  93. int /* PRIVATE */
  94. png_crc_finish(png_structp png_ptr, png_uint_32 skip)
  95. {
  96.    png_size_t i;
  97.    png_size_t istop = png_ptr->zbuf_size;
  98.  
  99.    for (i = (png_size_t)skip; i > istop; i -= istop)
  100.    {
  101.       png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  102.    }
  103.    if (i)
  104.    {
  105.       png_crc_read(png_ptr, png_ptr->zbuf, i);
  106.    }
  107.  
  108.    if (png_crc_error(png_ptr))
  109.    {
  110.       if (((png_ptr->chunk_name[0] & 0x20) &&                /* Ancillary */
  111.            !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
  112.           (!(png_ptr->chunk_name[0] & 0x20) &&             /* Critical  */
  113.           (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
  114.       {
  115.          png_chunk_warning(png_ptr, "CRC error");
  116.       }
  117.       else
  118.       {
  119.          png_chunk_error(png_ptr, "CRC error");
  120.       }
  121.       return (1);
  122.    }
  123.  
  124.    return (0);
  125. }
  126.  
  127. /* Compare the CRC stored in the PNG file with that calculated by libpng from
  128.    the data it has read thus far. */
  129. int /* PRIVATE */
  130. png_crc_error(png_structp png_ptr)
  131. {
  132.    png_byte crc_bytes[4];
  133.    png_uint_32 crc;
  134.    int need_crc = 1;
  135.  
  136.    if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */
  137.    {
  138.       if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
  139.           (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
  140.          need_crc = 0;
  141.    }
  142.    else                                                    /* critical */
  143.    {
  144.       if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
  145.          need_crc = 0;
  146.    }
  147.  
  148.    png_read_data(png_ptr, crc_bytes, 4);
  149.  
  150.    if (need_crc)
  151.    {
  152.       crc = png_get_uint_32(crc_bytes);
  153.       return ((int)(crc != png_ptr->crc));
  154.    }
  155.    else
  156.       return (0);
  157. }
  158.  
  159. #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
  160.     defined(PNG_READ_iCCP_SUPPORTED)
  161. /*
  162.  * Decompress trailing data in a chunk.  The assumption is that chunkdata
  163.  * points at an allocated area holding the contents of a chunk with a
  164.  * trailing compressed part.  What we get back is an allocated area
  165.  * holding the original prefix part and an uncompressed version of the
  166.  * trailing part (the malloc area passed in is freed).
  167.  */
  168. png_charp /* PRIVATE */
  169. png_decompress_chunk(png_structp png_ptr, int comp_type,
  170.                               png_charp chunkdata, png_size_t chunklength,
  171.                               png_size_t prefix_size, png_size_t *newlength)
  172. {
  173.    static char msg[] = "Error decoding compressed text";
  174.    png_charp text = NULL;
  175.    png_size_t text_size;
  176.  
  177.    if (comp_type == PNG_COMPRESSION_TYPE_BASE)
  178.    {
  179.       int ret = Z_OK;
  180.       png_ptr->zstream.next_in = (png_bytep)(chunkdata + prefix_size);
  181.       png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size);
  182.       png_ptr->zstream.next_out = png_ptr->zbuf;
  183.       png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  184.  
  185.       text_size = 0;
  186.       text = NULL;
  187.  
  188.       while (png_ptr->zstream.avail_in)
  189.       {
  190.          ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  191.          if (ret != Z_OK && ret != Z_STREAM_END)
  192.          {
  193.             if (png_ptr->zstream.msg != NULL)
  194.                png_warning(png_ptr, png_ptr->zstream.msg);
  195.             else
  196.                png_warning(png_ptr, msg);
  197.             inflateReset(&png_ptr->zstream);
  198.             png_ptr->zstream.avail_in = 0;
  199.  
  200.             if (text ==  NULL)
  201.             {
  202.                text_size = prefix_size + sizeof(msg) + 1;
  203.                text = (png_charp)png_malloc(png_ptr, text_size);
  204.                png_memcpy(text, chunkdata, prefix_size);
  205.             }
  206.  
  207.             text[text_size - 1] = 0x00;
  208.  
  209.             /* Copy what we can of the error message into the text chunk */
  210.             text_size = (png_size_t)(chunklength - (text - chunkdata) - 1);
  211.             text_size = sizeof(msg) > text_size ? text_size : sizeof(msg);
  212.             png_memcpy(text + prefix_size, msg, text_size + 1);
  213.             break;
  214.          }
  215.          if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END)
  216.          {
  217.             if (text == NULL)
  218.             {
  219.                text_size = prefix_size +
  220.                    png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  221.                text = (png_charp)png_malloc(png_ptr, text_size + 1);
  222.                png_memcpy(text + prefix_size, png_ptr->zbuf,
  223.                     text_size - prefix_size);
  224.                png_memcpy(text, chunkdata, prefix_size);
  225.                *(text + text_size) = 0x00;
  226.             }
  227.             else
  228.             {
  229.                png_charp tmp;
  230.  
  231.                tmp = text;
  232.                text = (png_charp)png_malloc(png_ptr, (png_uint_32)(text_size +
  233.                   png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
  234.                png_memcpy(text, tmp, text_size);
  235.                png_free(png_ptr, tmp);
  236.                png_memcpy(text + text_size, png_ptr->zbuf,
  237.                   (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
  238.                text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
  239.                *(text + text_size) = 0x00;
  240.             }
  241.             if (ret == Z_STREAM_END)
  242.                break;
  243.             else
  244.             {
  245.                png_ptr->zstream.next_out = png_ptr->zbuf;
  246.                png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  247.             }
  248.          }
  249.       }
  250.       if (ret != Z_STREAM_END)
  251.       {
  252. #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  253.          char umsg[50];
  254.  
  255.          if (ret == Z_BUF_ERROR)
  256.             sprintf(umsg,"Buffer error in compressed datastream in %s chunk",
  257.                 png_ptr->chunk_name);
  258.          else if (ret == Z_DATA_ERROR)
  259.             sprintf(umsg,"Data error in compressed datastream in %s chunk",
  260.                 png_ptr->chunk_name);
  261.          else
  262.             sprintf(umsg,"Incomplete compressed datastream in %s chunk",
  263.                 png_ptr->chunk_name);
  264.          png_warning(png_ptr, umsg);
  265. #else
  266.          png_warning(png_ptr,
  267.             "Incomplete compressed datastream in chunk other than IDAT");
  268. #endif
  269.          text_size=prefix_size;
  270.          if (text ==  NULL)
  271.          {
  272.             text = (png_charp)png_malloc(png_ptr, text_size+1);
  273.             png_memcpy(text, chunkdata, prefix_size);
  274.          }
  275.          *(text + text_size) = 0x00;
  276.       }
  277.  
  278.       inflateReset(&png_ptr->zstream);
  279.       png_ptr->zstream.avail_in = 0;
  280.  
  281.       png_free(png_ptr, chunkdata);
  282.       chunkdata = text;
  283.       *newlength=text_size;
  284.    }
  285.    else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
  286.    {
  287. #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  288.       char umsg[50];
  289.  
  290.       sprintf(umsg, "Unknown zTXt compression type %d", comp_type);
  291.       png_warning(png_ptr, umsg);
  292. #else
  293.       png_warning(png_ptr, "Unknown zTXt compression type");
  294. #endif
  295.  
  296.       *(chunkdata + prefix_size) = 0x00;
  297.       *newlength=prefix_size;
  298.    }
  299.  
  300.    return chunkdata;
  301. }
  302. #endif
  303.  
  304. /* read and check the IDHR chunk */
  305. void /* PRIVATE */
  306. png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  307. {
  308.    png_byte buf[13];
  309.    png_uint_32 width, height;
  310.    int bit_depth, color_type, compression_type, filter_type;
  311.    int interlace_type;
  312.  
  313.    png_debug(1, "in png_handle_IHDR\n");
  314.  
  315.    if (png_ptr->mode & PNG_HAVE_IHDR)
  316.       png_error(png_ptr, "Out of place IHDR");
  317.  
  318.    /* check the length */
  319.    if (length != 13)
  320.       png_error(png_ptr, "Invalid IHDR chunk");
  321.  
  322.    png_ptr->mode |= PNG_HAVE_IHDR;
  323.  
  324.    png_crc_read(png_ptr, buf, 13);
  325.    png_crc_finish(png_ptr, 0);
  326.  
  327.    width = png_get_uint_32(buf);
  328.    height = png_get_uint_32(buf + 4);
  329.    bit_depth = buf[8];
  330.    color_type = buf[9];
  331.    compression_type = buf[10];
  332.    filter_type = buf[11];
  333.    interlace_type = buf[12];
  334.  
  335.  
  336.    /* set internal variables */
  337.    png_ptr->width = width;
  338.    png_ptr->height = height;
  339.    png_ptr->bit_depth = (png_byte)bit_depth;
  340.    png_ptr->interlaced = (png_byte)interlace_type;
  341.    png_ptr->color_type = (png_byte)color_type;
  342.    png_ptr->filter_type = (png_byte)filter_type;
  343.  
  344.    /* find number of channels */
  345.    switch (png_ptr->color_type)
  346.    {
  347.       case PNG_COLOR_TYPE_GRAY:
  348.       case PNG_COLOR_TYPE_PALETTE:
  349.          png_ptr->channels = 1;
  350.          break;
  351.       case PNG_COLOR_TYPE_RGB:
  352.          png_ptr->channels = 3;
  353.          break;
  354.       case PNG_COLOR_TYPE_GRAY_ALPHA:
  355.          png_ptr->channels = 2;
  356.          break;
  357.       case PNG_COLOR_TYPE_RGB_ALPHA:
  358.          png_ptr->channels = 4;
  359.          break;
  360.    }
  361.  
  362.    /* set up other useful info */
  363.    png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
  364.    png_ptr->channels);
  365.    png_ptr->rowbytes = ((png_ptr->width *
  366.       (png_uint_32)png_ptr->pixel_depth + 7) >> 3);
  367.    png_debug1(3,"bit_depth = %d\n", png_ptr->bit_depth);
  368.    png_debug1(3,"channels = %d\n", png_ptr->channels);
  369.    png_debug1(3,"rowbytes = %lu\n", png_ptr->rowbytes);
  370.    png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
  371.       color_type, interlace_type, compression_type, filter_type);
  372. }
  373.  
  374. /* read and check the palette */
  375. void /* PRIVATE */
  376. png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  377. {
  378.    png_color palette[PNG_MAX_PALETTE_LENGTH];
  379.    int num, i;
  380. #ifndef PNG_NO_POINTER_INDEXING
  381.    png_colorp pal_ptr;
  382. #endif
  383.  
  384.    png_debug(1, "in png_handle_PLTE\n");
  385.  
  386.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  387.       png_error(png_ptr, "Missing IHDR before PLTE");
  388.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  389.    {
  390.       png_warning(png_ptr, "Invalid PLTE after IDAT");
  391.       png_crc_finish(png_ptr, length);
  392.       return;
  393.    }
  394.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  395.       png_error(png_ptr, "Duplicate PLTE chunk");
  396.  
  397.    png_ptr->mode |= PNG_HAVE_PLTE;
  398.  
  399. #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  400.    if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  401.    {
  402.       png_crc_finish(png_ptr, length);
  403.       return;
  404.    }
  405. #endif
  406.  
  407.    if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
  408.    {
  409.       if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  410.       {
  411.          png_warning(png_ptr, "Invalid palette chunk");
  412.          png_crc_finish(png_ptr, length);
  413.          return;
  414.       }
  415.       else
  416.       {
  417.          png_error(png_ptr, "Invalid palette chunk");
  418.       }
  419.    }
  420.  
  421.    num = (int)length / 3;
  422.  
  423. #ifndef PNG_NO_POINTER_INDEXING
  424.    for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
  425.    {
  426.       png_byte buf[3];
  427.  
  428.       png_crc_read(png_ptr, buf, 3);
  429.       pal_ptr->red = buf[0];
  430.       pal_ptr->green = buf[1];
  431.       pal_ptr->blue = buf[2];
  432.    }
  433. #else
  434.    for (i = 0; i < num; i++)
  435.    {
  436.       png_byte buf[3];
  437.  
  438.       png_crc_read(png_ptr, buf, 3);
  439.       /* don't depend upon png_color being any order */
  440.       palette[i].red = buf[0];
  441.       palette[i].green = buf[1];
  442.       palette[i].blue = buf[2];
  443.    }
  444. #endif
  445.  
  446.    /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
  447.       whatever the normal CRC configuration tells us.  However, if we
  448.       have an RGB image, the PLTE can be considered ancillary, so
  449.       we will act as though it is. */
  450. #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  451.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  452. #endif
  453.    {
  454.       png_crc_finish(png_ptr, 0);
  455.    }
  456. #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
  457.    else if (png_crc_error(png_ptr))  /* Only if we have a CRC error */
  458.    {
  459.       /* If we don't want to use the data from an ancillary chunk,
  460.          we have two options: an error abort, or a warning and we
  461.          ignore the data in this chunk (which should be OK, since
  462.          it's considered ancillary for a RGB or RGBA image). */
  463.       if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
  464.       {
  465.          if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
  466.          {
  467.             png_chunk_error(png_ptr, "CRC error");
  468.          }
  469.          else
  470.          {
  471.             png_chunk_warning(png_ptr, "CRC error");
  472.             return;
  473.          }
  474.       }
  475.       /* Otherwise, we (optionally) emit a warning and use the chunk. */
  476.       else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
  477.       {
  478.          png_chunk_warning(png_ptr, "CRC error");
  479.       }
  480.    }
  481. #endif
  482.  
  483.    png_set_PLTE(png_ptr, info_ptr, palette, num);
  484.  
  485. #if defined(PNG_READ_tRNS_SUPPORTED)
  486.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  487.    {
  488.       if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  489.       {
  490.          if (png_ptr->num_trans > (png_uint_16)num)
  491.          {
  492.             png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
  493.             png_ptr->num_trans = (png_uint_16)num;
  494.          }
  495.          if (info_ptr->num_trans > (png_uint_16)num)
  496.          {
  497.             png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
  498.             info_ptr->num_trans = (png_uint_16)num;
  499.          }
  500.       }
  501.    }
  502. #endif
  503.  
  504. }
  505.  
  506. void /* PRIVATE */
  507. png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  508. {
  509.    png_debug(1, "in png_handle_IEND\n");
  510.  
  511.    if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
  512.    {
  513.       png_error(png_ptr, "No image in file");
  514.  
  515.       /* to quiet compiler warnings about unused info_ptr */
  516.       if (info_ptr == NULL)
  517.          return;
  518.    }
  519.  
  520.    png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
  521.  
  522.    if (length != 0)
  523.    {
  524.       png_warning(png_ptr, "Incorrect IEND chunk length");
  525.    }
  526.    png_crc_finish(png_ptr, length);
  527. }
  528.  
  529. #if defined(PNG_READ_gAMA_SUPPORTED)
  530. void /* PRIVATE */
  531. png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  532. {
  533.    png_fixed_point igamma;
  534. #ifdef PNG_FLOATING_POINT_SUPPORTED
  535.    float file_gamma;
  536. #endif
  537.    png_byte buf[4];
  538.  
  539.    png_debug(1, "in png_handle_gAMA\n");
  540.  
  541.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  542.       png_error(png_ptr, "Missing IHDR before gAMA");
  543.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  544.    {
  545.       png_warning(png_ptr, "Invalid gAMA after IDAT");
  546.       png_crc_finish(png_ptr, length);
  547.       return;
  548.    }
  549.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  550.       /* Should be an error, but we can cope with it */
  551.       png_warning(png_ptr, "Out of place gAMA chunk");
  552.  
  553.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
  554. #if defined(PNG_READ_sRGB_SUPPORTED)
  555.       && !(info_ptr->valid & PNG_INFO_sRGB)
  556. #endif
  557.       )
  558.    {
  559.       png_warning(png_ptr, "Duplicate gAMA chunk");
  560.       png_crc_finish(png_ptr, length);
  561.       return;
  562.    }
  563.  
  564.    if (length != 4)
  565.    {
  566.       png_warning(png_ptr, "Incorrect gAMA chunk length");
  567.       png_crc_finish(png_ptr, length);
  568.       return;
  569.    }
  570.  
  571.    png_crc_read(png_ptr, buf, 4);
  572.    if (png_crc_finish(png_ptr, 0))
  573.       return;
  574.  
  575.    igamma = (png_fixed_point)png_get_uint_32(buf);
  576.    /* check for zero gamma */
  577.    if (igamma == 0)
  578.       {
  579.          png_warning(png_ptr,
  580.            "Ignoring gAMA chunk with gamma=0");
  581.          return;
  582.       }
  583.  
  584. #if defined(PNG_READ_sRGB_SUPPORTED)
  585.    if (info_ptr->valid & PNG_INFO_sRGB)
  586.       if(igamma < 45000L || igamma > 46000L)
  587.       {
  588.          png_warning(png_ptr,
  589.            "Ignoring incorrect gAMA value when sRGB is also present");
  590. #ifndef PNG_NO_CONSOLE_IO
  591.          fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma);
  592. #endif
  593.          return;
  594.       }
  595. #endif /* PNG_READ_sRGB_SUPPORTED */
  596.  
  597. #ifdef PNG_FLOATING_POINT_SUPPORTED
  598.    file_gamma = (float)igamma / (float)100000.0;
  599. #  ifdef PNG_READ_GAMMA_SUPPORTED
  600.      png_ptr->gamma = file_gamma;
  601. #  endif
  602.      png_set_gAMA(png_ptr, info_ptr, file_gamma);
  603. #endif
  604. #ifdef PNG_FIXED_POINT_SUPPORTED
  605.    png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
  606. #endif
  607. }
  608. #endif
  609.  
  610. #if defined(PNG_READ_sBIT_SUPPORTED)
  611. void /* PRIVATE */
  612. png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  613. {
  614.    png_size_t truelen;
  615.    png_byte buf[4];
  616.  
  617.    png_debug(1, "in png_handle_sBIT\n");
  618.  
  619.    buf[0] = buf[1] = buf[2] = buf[3] = 0;
  620.  
  621.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  622.       png_error(png_ptr, "Missing IHDR before sBIT");
  623.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  624.    {
  625.       png_warning(png_ptr, "Invalid sBIT after IDAT");
  626.       png_crc_finish(png_ptr, length);
  627.       return;
  628.    }
  629.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  630.    {
  631.       /* Should be an error, but we can cope with it */
  632.       png_warning(png_ptr, "Out of place sBIT chunk");
  633.    }
  634.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
  635.    {
  636.       png_warning(png_ptr, "Duplicate sBIT chunk");
  637.       png_crc_finish(png_ptr, length);
  638.       return;
  639.    }
  640.  
  641.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  642.       truelen = 3;
  643.    else
  644.       truelen = (png_size_t)png_ptr->channels;
  645.  
  646.    if (length != truelen)
  647.    {
  648.       png_warning(png_ptr, "Incorrect sBIT chunk length");
  649.       png_crc_finish(png_ptr, length);
  650.       return;
  651.    }
  652.  
  653.    png_crc_read(png_ptr, buf, truelen);
  654.    if (png_crc_finish(png_ptr, 0))
  655.       return;
  656.  
  657.    if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  658.    {
  659.       png_ptr->sig_bit.red = buf[0];
  660.       png_ptr->sig_bit.green = buf[1];
  661.       png_ptr->sig_bit.blue = buf[2];
  662.       png_ptr->sig_bit.alpha = buf[3];
  663.    }
  664.    else
  665.    {
  666.       png_ptr->sig_bit.gray = buf[0];
  667.       png_ptr->sig_bit.red = buf[0];
  668.       png_ptr->sig_bit.green = buf[0];
  669.       png_ptr->sig_bit.blue = buf[0];
  670.       png_ptr->sig_bit.alpha = buf[1];
  671.    }
  672.    png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
  673. }
  674. #endif
  675.  
  676. #if defined(PNG_READ_cHRM_SUPPORTED)
  677. void /* PRIVATE */
  678. png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  679. {
  680.    png_byte buf[4];
  681. #ifdef PNG_FLOATING_POINT_SUPPORTED
  682.    float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
  683. #endif
  684.    png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
  685.       int_y_green, int_x_blue, int_y_blue;
  686.  
  687.    png_debug(1, "in png_handle_cHRM\n");
  688.  
  689.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  690.       png_error(png_ptr, "Missing IHDR before cHRM");
  691.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  692.    {
  693.       png_warning(png_ptr, "Invalid cHRM after IDAT");
  694.       png_crc_finish(png_ptr, length);
  695.       return;
  696.    }
  697.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  698.       /* Should be an error, but we can cope with it */
  699.       png_warning(png_ptr, "Missing PLTE before cHRM");
  700.  
  701.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
  702. #if defined(PNG_READ_sRGB_SUPPORTED)
  703.       && !(info_ptr->valid & PNG_INFO_sRGB)
  704. #endif
  705.       )
  706.    {
  707.       png_warning(png_ptr, "Duplicate cHRM chunk");
  708.       png_crc_finish(png_ptr, length);
  709.       return;
  710.    }
  711.  
  712.    if (length != 32)
  713.    {
  714.       png_warning(png_ptr, "Incorrect cHRM chunk length");
  715.       png_crc_finish(png_ptr, length);
  716.       return;
  717.    }
  718.  
  719.    png_crc_read(png_ptr, buf, 4);
  720.    int_x_white = (png_fixed_point)png_get_uint_32(buf);
  721.  
  722.    png_crc_read(png_ptr, buf, 4);
  723.    int_y_white = (png_fixed_point)png_get_uint_32(buf);
  724.  
  725.    if (int_x_white > 80000L || int_y_white > 80000L ||
  726.       int_x_white + int_y_white > 100000L)
  727.    {
  728.       png_warning(png_ptr, "Invalid cHRM white point");
  729.       png_crc_finish(png_ptr, 24);
  730.       return;
  731.    }
  732.  
  733.    png_crc_read(png_ptr, buf, 4);
  734.    int_x_red = (png_fixed_point)png_get_uint_32(buf);
  735.  
  736.    png_crc_read(png_ptr, buf, 4);
  737.    int_y_red = (png_fixed_point)png_get_uint_32(buf);
  738.  
  739.    if (int_x_red > 80000L || int_y_red > 80000L ||
  740.       int_x_red + int_y_red > 100000L)
  741.    {
  742.       png_warning(png_ptr, "Invalid cHRM red point");
  743.       png_crc_finish(png_ptr, 16);
  744.       return;
  745.    }
  746.  
  747.    png_crc_read(png_ptr, buf, 4);
  748.    int_x_green = (png_fixed_point)png_get_uint_32(buf);
  749.  
  750.    png_crc_read(png_ptr, buf, 4);
  751.    int_y_green = (png_fixed_point)png_get_uint_32(buf);
  752.  
  753.    if (int_x_green > 80000L || int_y_green > 80000L ||
  754.       int_x_green + int_y_green > 100000L)
  755.    {
  756.       png_warning(png_ptr, "Invalid cHRM green point");
  757.       png_crc_finish(png_ptr, 8);
  758.       return;
  759.    }
  760.  
  761.    png_crc_read(png_ptr, buf, 4);
  762.    int_x_blue = (png_fixed_point)png_get_uint_32(buf);
  763.  
  764.    png_crc_read(png_ptr, buf, 4);
  765.    int_y_blue = (png_fixed_point)png_get_uint_32(buf);
  766.  
  767.    if (int_x_blue > 80000L || int_y_blue > 80000L ||
  768.       int_x_blue + int_y_blue > 100000L)
  769.    {
  770.       png_warning(png_ptr, "Invalid cHRM blue point");
  771.       png_crc_finish(png_ptr, 0);
  772.       return;
  773.    }
  774. #ifdef PNG_FLOATING_POINT_SUPPORTED
  775.    white_x = (float)int_x_white / (float)100000.0;
  776.    white_y = (float)int_y_white / (float)100000.0;
  777.    red_x   = (float)int_x_red   / (float)100000.0;
  778.    red_y   = (float)int_y_red   / (float)100000.0;
  779.    green_x = (float)int_x_green / (float)100000.0;
  780.    green_y = (float)int_y_green / (float)100000.0;
  781.    blue_x  = (float)int_x_blue  / (float)100000.0;
  782.    blue_y  = (float)int_y_blue  / (float)100000.0;
  783. #endif
  784.  
  785. #if defined(PNG_READ_sRGB_SUPPORTED)
  786.    if (info_ptr->valid & PNG_INFO_sRGB)
  787.       {
  788.       if (abs(int_x_white - 31270L) > 1000 ||
  789.           abs(int_y_white - 32900L) > 1000 ||
  790.           abs(  int_x_red - 64000L) > 1000 ||
  791.           abs(  int_y_red - 33000L) > 1000 ||
  792.           abs(int_x_green - 30000L) > 1000 ||
  793.           abs(int_y_green - 60000L) > 1000 ||
  794.           abs( int_x_blue - 15000L) > 1000 ||
  795.           abs( int_y_blue -  6000L) > 1000)
  796.          {
  797.  
  798.             png_warning(png_ptr,
  799.               "Ignoring incorrect cHRM value when sRGB is also present");
  800. #ifndef PNG_NO_CONSOLE_IO
  801. #ifdef PNG_FLOATING_POINT_SUPPORTED
  802.             fprintf(stderr,"wx=%f, wy=%f, rx=%f, ry=%f\n",
  803.                white_x, white_y, red_x, red_y);
  804.             fprintf(stderr,"gx=%f, gy=%f, bx=%f, by=%f\n",
  805.                green_x, green_y, blue_x, blue_y);
  806. #else
  807.             fprintf(stderr,"wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
  808.                int_x_white, int_y_white, int_x_red, int_y_red);
  809.             fprintf(stderr,"gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
  810.                int_x_green, int_y_green, int_x_blue, int_y_blue);
  811. #endif
  812. #endif /* PNG_NO_CONSOLE_IO */
  813.          }
  814.          png_crc_finish(png_ptr, 0);
  815.          return;
  816.       }
  817. #endif /* PNG_READ_sRGB_SUPPORTED */
  818.  
  819. #ifdef PNG_FLOATING_POINT_SUPPORTED
  820.    png_set_cHRM(png_ptr, info_ptr,
  821.       white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
  822. #endif
  823. #ifdef PNG_FIXED_POINT_SUPPORTED
  824.    png_set_cHRM_fixed(png_ptr, info_ptr,
  825.       int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
  826.       int_y_green, int_x_blue, int_y_blue);
  827. #endif
  828.    if (png_crc_finish(png_ptr, 0))
  829.       return;
  830. }
  831. #endif
  832.  
  833. #if defined(PNG_READ_sRGB_SUPPORTED)
  834. void /* PRIVATE */
  835. png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  836. {
  837.    int intent;
  838.    png_byte buf[1];
  839.  
  840.    png_debug(1, "in png_handle_sRGB\n");
  841.  
  842.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  843.       png_error(png_ptr, "Missing IHDR before sRGB");
  844.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  845.    {
  846.       png_warning(png_ptr, "Invalid sRGB after IDAT");
  847.       png_crc_finish(png_ptr, length);
  848.       return;
  849.    }
  850.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  851.       /* Should be an error, but we can cope with it */
  852.       png_warning(png_ptr, "Out of place sRGB chunk");
  853.  
  854.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
  855.    {
  856.       png_warning(png_ptr, "Duplicate sRGB chunk");
  857.       png_crc_finish(png_ptr, length);
  858.       return;
  859.    }
  860.  
  861.    if (length != 1)
  862.    {
  863.       png_warning(png_ptr, "Incorrect sRGB chunk length");
  864.       png_crc_finish(png_ptr, length);
  865.       return;
  866.    }
  867.  
  868.    png_crc_read(png_ptr, buf, 1);
  869.    if (png_crc_finish(png_ptr, 0))
  870.       return;
  871.  
  872.    intent = buf[0];
  873.    /* check for bad intent */
  874.    if (intent >= PNG_sRGB_INTENT_LAST)
  875.    {
  876.       png_warning(png_ptr, "Unknown sRGB intent");
  877.       return;
  878.    }
  879.  
  880. #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
  881.    if ((info_ptr->valid & PNG_INFO_gAMA))
  882.    {
  883.    int igamma;
  884. #ifdef PNG_FIXED_POINT_SUPPORTED
  885.       igamma=(int)info_ptr->int_gamma;
  886. #else
  887. #  ifdef PNG_FLOATING_POINT_SUPPORTED
  888.       igamma=(int)(info_ptr->gamma * 100000.);
  889. #  endif
  890. #endif
  891. #if 0 && defined(PNG_cHRM_SUPPORTED) && !defined(PNG_FIXED_POINT_SUPPORTED)
  892. /* We need to define these here because they aren't in png.h */
  893.    png_fixed_point int_x_white;
  894.    png_fixed_point int_y_white;
  895.    png_fixed_point int_x_red;
  896.    png_fixed_point int_y_red;
  897.    png_fixed_point int_x_green;
  898.    png_fixed_point int_y_green;
  899.    png_fixed_point int_x_blue;
  900.    png_fixed_point int_y_blue;
  901. #endif
  902.       if(igamma < 45000L || igamma > 46000L)
  903.       {
  904.          png_warning(png_ptr,
  905.            "Ignoring incorrect gAMA value when sRGB is also present");
  906. #ifndef PNG_NO_CONSOLE_IO
  907. #  ifdef PNG_FIXED_POINT_SUPPORTED
  908.          fprintf(stderr,"incorrect gamma=(%d/100000)\n",(int)png_ptr->int_gamma);
  909. #  else
  910. #    ifdef PNG_FLOATING_POINT_SUPPORTED
  911.          fprintf(stderr,"incorrect gamma=%f\n",png_ptr->gamma);
  912. #    endif
  913. #  endif
  914. #endif
  915.       }
  916.    }
  917. #endif /* PNG_READ_gAMA_SUPPORTED */
  918.  
  919. #ifdef PNG_READ_cHRM_SUPPORTED
  920. #ifdef PNG_FIXED_POINT_SUPPORTED
  921.    if (info_ptr->valid & PNG_INFO_cHRM)
  922.       if (abs(info_ptr->int_x_white - 31270L) > 1000 ||
  923.           abs(info_ptr->int_y_white - 32900L) > 1000 ||
  924.           abs(  info_ptr->int_x_red - 64000L) > 1000 ||
  925.           abs(  info_ptr->int_y_red - 33000L) > 1000 ||
  926.           abs(info_ptr->int_x_green - 30000L) > 1000 ||
  927.           abs(info_ptr->int_y_green - 60000L) > 1000 ||
  928.           abs( info_ptr->int_x_blue - 15000L) > 1000 ||
  929.           abs( info_ptr->int_y_blue -  6000L) > 1000)
  930.          {
  931.             png_warning(png_ptr,
  932.               "Ignoring incorrect cHRM value when sRGB is also present");
  933.          }
  934. #endif /* PNG_FIXED_POINT_SUPPORTED */
  935. #endif /* PNG_READ_cHRM_SUPPORTED */
  936.  
  937.    png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
  938. }
  939. #endif /* PNG_READ_sRGB_SUPPORTED */
  940.  
  941. #if defined(PNG_READ_iCCP_SUPPORTED)
  942. void /* PRIVATE */
  943. png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  944. /* Note: this does not properly handle chunks that are > 64K under DOS */
  945. {
  946.    png_charp chunkdata;
  947.    png_byte compression_type;
  948.    png_charp profile;
  949.    png_uint_32 skip = 0;
  950.    png_uint_32 profile_size = 0;
  951.    png_uint_32 profile_length = 0;
  952.    png_size_t slength, prefix_length, data_length;
  953.  
  954.    png_debug(1, "in png_handle_iCCP\n");
  955.  
  956.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  957.       png_error(png_ptr, "Missing IHDR before iCCP");
  958.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  959.    {
  960.       png_warning(png_ptr, "Invalid iCCP after IDAT");
  961.       png_crc_finish(png_ptr, length);
  962.       return;
  963.    }
  964.    else if (png_ptr->mode & PNG_HAVE_PLTE)
  965.       /* Should be an error, but we can cope with it */
  966.       png_warning(png_ptr, "Out of place iCCP chunk");
  967.  
  968.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
  969.    {
  970.       png_warning(png_ptr, "Duplicate iCCP chunk");
  971.       png_crc_finish(png_ptr, length);
  972.       return;
  973.    }
  974.  
  975. #ifdef PNG_MAX_MALLOC_64K
  976.    if (length > (png_uint_32)65535L)
  977.    {
  978.       png_warning(png_ptr, "iCCP chunk too large to fit in memory");
  979.       skip = length - (png_uint_32)65535L;
  980.       length = (png_uint_32)65535L;
  981.    }
  982. #endif
  983.  
  984.    chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
  985.    slength = (png_size_t)length;
  986.    png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
  987.  
  988.    if (png_crc_finish(png_ptr, skip))
  989.    {
  990.       png_free(png_ptr, chunkdata);
  991.       return;
  992.    }
  993.  
  994.    chunkdata[slength] = 0x00;
  995.  
  996.    for (profile = chunkdata; *profile; profile++)
  997.       /* empty loop to find end of name */ ;
  998.  
  999.    ++profile;
  1000.  
  1001.    /* there should be at least one zero (the compression type byte)
  1002.       following the separator, and we should be on it  */
  1003.    if ( profile >= chunkdata + slength)
  1004.    {
  1005.       png_free(png_ptr, chunkdata);
  1006.       png_warning(png_ptr, "Malformed iCCP chunk");
  1007.       return;
  1008.    }
  1009.  
  1010.    /* compression_type should always be zero */
  1011.    compression_type = *profile++;
  1012.    if (compression_type)
  1013.    {
  1014.       png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
  1015.       compression_type=0x00;  /* Reset it to zero (libpng-1.0.6 through 1.0.8
  1016.                                  wrote nonzero) */
  1017.    }
  1018.  
  1019.    prefix_length = profile - chunkdata;
  1020.    chunkdata = png_decompress_chunk(png_ptr, compression_type, chunkdata,
  1021.                                     slength, prefix_length, &data_length);
  1022.  
  1023.    profile_length = data_length - prefix_length;
  1024.  
  1025.    /* Check the profile_size recorded in the first 32 bits of the ICC profile */
  1026.    profile_size = ((*(chunkdata+prefix_length))<<24) |
  1027.                   ((*(chunkdata+prefix_length+1))<<16) |
  1028.                   ((*(chunkdata+prefix_length+2))<< 8) |
  1029.                   ((*(chunkdata+prefix_length+3))    );
  1030.  
  1031.    if(profile_size < profile_length)
  1032.       profile_length = profile_size;
  1033.  
  1034.    if(profile_size > profile_length)
  1035.    {
  1036.       png_warning(png_ptr, "Ignoring truncated iCCP profile.\n");
  1037.       return;
  1038.    }
  1039.  
  1040.    png_set_iCCP(png_ptr, info_ptr, chunkdata, compression_type,
  1041.                 chunkdata + prefix_length, profile_length);
  1042.    png_free(png_ptr, chunkdata);
  1043. }
  1044. #endif /* PNG_READ_iCCP_SUPPORTED */
  1045.  
  1046. #if defined(PNG_READ_sPLT_SUPPORTED)
  1047. void /* PRIVATE */
  1048. png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1049. /* Note: this does not properly handle chunks that are > 64K under DOS */
  1050. {
  1051.    png_bytep chunkdata;
  1052.    png_bytep entry_start;
  1053.    png_sPLT_t new_palette;
  1054. #ifdef PNG_NO_POINTER_INDEXING
  1055.    png_sPLT_entryp pp;
  1056. #endif
  1057.    int data_length, entry_size, i;
  1058.    png_uint_32 skip = 0;
  1059.    png_size_t slength;
  1060.  
  1061.    png_debug(1, "in png_handle_sPLT\n");
  1062.  
  1063.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1064.       png_error(png_ptr, "Missing IHDR before sPLT");
  1065.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  1066.    {
  1067.       png_warning(png_ptr, "Invalid sPLT after IDAT");
  1068.       png_crc_finish(png_ptr, length);
  1069.       return;
  1070.    }
  1071.  
  1072. #ifdef PNG_MAX_MALLOC_64K
  1073.    if (length > (png_uint_32)65535L)
  1074.    {
  1075.       png_warning(png_ptr, "sPLT chunk too large to fit in memory");
  1076.       skip = length - (png_uint_32)65535L;
  1077.       length = (png_uint_32)65535L;
  1078.    }
  1079. #endif
  1080.  
  1081.    chunkdata = (png_bytep)png_malloc(png_ptr, length + 1);
  1082.    slength = (png_size_t)length;
  1083.    png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
  1084.  
  1085.    if (png_crc_finish(png_ptr, skip))
  1086.    {
  1087.       png_free(png_ptr, chunkdata);
  1088.       return;
  1089.    }
  1090.  
  1091.    chunkdata[slength] = 0x00;
  1092.  
  1093.    for (entry_start = chunkdata; *entry_start; entry_start++)
  1094.       /* empty loop to find end of name */ ;
  1095.    ++entry_start;
  1096.  
  1097.    /* a sample depth should follow the separator, and we should be on it  */
  1098.    if (entry_start > chunkdata + slength)
  1099.    {
  1100.       png_free(png_ptr, chunkdata);
  1101.       png_warning(png_ptr, "malformed sPLT chunk");
  1102.       return;
  1103.    }
  1104.  
  1105.    new_palette.depth = *entry_start++;
  1106.    entry_size = (new_palette.depth == 8 ? 6 : 10);
  1107.    data_length = (slength - (entry_start - chunkdata));
  1108.  
  1109.    /* integrity-check the data length */
  1110.    if (data_length % entry_size)
  1111.    {
  1112.       png_free(png_ptr, chunkdata);
  1113.       png_warning(png_ptr, "sPLT chunk has bad length");
  1114.       return;
  1115.    }
  1116.  
  1117.    new_palette.nentries = data_length / entry_size;
  1118.    new_palette.entries = (png_sPLT_entryp)png_malloc(
  1119.        png_ptr, new_palette.nentries * sizeof(png_sPLT_entry));
  1120.  
  1121. #ifndef PNG_NO_POINTER_INDEXING
  1122.    for (i = 0; i < new_palette.nentries; i++)
  1123.    {
  1124.       png_sPLT_entryp pp = new_palette.entries + i;
  1125.  
  1126.       if (new_palette.depth == 8)
  1127.       {
  1128.           pp->red = *entry_start++;
  1129.           pp->green = *entry_start++;
  1130.           pp->blue = *entry_start++;
  1131.           pp->alpha = *entry_start++;
  1132.       }
  1133.       else
  1134.       {
  1135.           pp->red   = png_get_uint_16(entry_start); entry_start += 2;
  1136.           pp->green = png_get_uint_16(entry_start); entry_start += 2;
  1137.           pp->blue  = png_get_uint_16(entry_start); entry_start += 2;
  1138.           pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
  1139.       }
  1140.       pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
  1141.    }
  1142. #else
  1143.    pp = new_palette.entries;
  1144.    for (i = 0; i < new_palette.nentries; i++)
  1145.    {
  1146.  
  1147.       if (new_palette.depth == 8)
  1148.       {
  1149.           pp[i].red   = *entry_start++;
  1150.           pp[i].green = *entry_start++;
  1151.           pp[i].blue  = *entry_start++;
  1152.           pp[i].alpha = *entry_start++;
  1153.       }
  1154.       else
  1155.       {
  1156.           pp[i].red   = png_get_uint_16(entry_start); entry_start += 2;
  1157.           pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
  1158.           pp[i].blue  = png_get_uint_16(entry_start); entry_start += 2;
  1159.           pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
  1160.       }
  1161.       pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
  1162.    }
  1163. #endif
  1164.  
  1165.    /* discard all chunk data except the name and stash that */
  1166.    new_palette.name = (png_charp)chunkdata;
  1167.  
  1168.    png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
  1169.  
  1170.    png_free(png_ptr, chunkdata);
  1171.    png_free(png_ptr, new_palette.entries);
  1172. }
  1173. #endif /* PNG_READ_sPLT_SUPPORTED */
  1174.  
  1175. #if defined(PNG_READ_tRNS_SUPPORTED)
  1176. void /* PRIVATE */
  1177. png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1178. {
  1179.    png_byte    readbuf[PNG_MAX_PALETTE_LENGTH];
  1180.  
  1181.    png_debug(1, "in png_handle_tRNS\n");
  1182.  
  1183.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1184.       png_error(png_ptr, "Missing IHDR before tRNS");
  1185.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  1186.    {
  1187.       png_warning(png_ptr, "Invalid tRNS after IDAT");
  1188.       png_crc_finish(png_ptr, length);
  1189.       return;
  1190.    }
  1191.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
  1192.    {
  1193.       png_warning(png_ptr, "Duplicate tRNS chunk");
  1194.       png_crc_finish(png_ptr, length);
  1195.       return;
  1196.    }
  1197.  
  1198.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1199.    {
  1200.       if (!(png_ptr->mode & PNG_HAVE_PLTE))
  1201.       {
  1202.          /* Should be an error, but we can cope with it */
  1203.          png_warning(png_ptr, "Missing PLTE before tRNS");
  1204.       }
  1205.       else if (length > (png_uint_32)png_ptr->num_palette)
  1206.       {
  1207.          png_warning(png_ptr, "Incorrect tRNS chunk length");
  1208.          png_crc_finish(png_ptr, length);
  1209.          return;
  1210.       }
  1211.       if (length == 0)
  1212.       {
  1213.          png_warning(png_ptr, "Zero length tRNS chunk");
  1214.          png_crc_finish(png_ptr, length);
  1215.          return;
  1216.       }
  1217.  
  1218.       png_crc_read(png_ptr, readbuf, (png_size_t)length);
  1219.       png_ptr->num_trans = (png_uint_16)length;
  1220.    }
  1221.    else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  1222.    {
  1223.       png_byte buf[6];
  1224.  
  1225.       if (length != 6)
  1226.       {
  1227.          png_warning(png_ptr, "Incorrect tRNS chunk length");
  1228.          png_crc_finish(png_ptr, length);
  1229.          return;
  1230.       }
  1231.  
  1232.       png_crc_read(png_ptr, buf, (png_size_t)length);
  1233.       png_ptr->num_trans = 1;
  1234.       png_ptr->trans_values.red = png_get_uint_16(buf);
  1235.       png_ptr->trans_values.green = png_get_uint_16(buf + 2);
  1236.       png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
  1237.    }
  1238.    else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  1239.    {
  1240.       png_byte buf[6];
  1241.  
  1242.       if (length != 2)
  1243.       {
  1244.          png_warning(png_ptr, "Incorrect tRNS chunk length");
  1245.          png_crc_finish(png_ptr, length);
  1246.          return;
  1247.       }
  1248.  
  1249.       png_crc_read(png_ptr, buf, 2);
  1250.       png_ptr->num_trans = 1;
  1251.       png_ptr->trans_values.gray = png_get_uint_16(buf);
  1252.    }
  1253.    else
  1254.    {
  1255.       png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
  1256.       png_crc_finish(png_ptr, length);
  1257.       return;
  1258.    }
  1259.  
  1260.    if (png_crc_finish(png_ptr, 0))
  1261.       return;
  1262.  
  1263.    png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
  1264.       &(png_ptr->trans_values));
  1265. }
  1266. #endif
  1267.  
  1268. #if defined(PNG_READ_bKGD_SUPPORTED)
  1269. void /* PRIVATE */
  1270. png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1271. {
  1272.    png_size_t truelen;
  1273.    png_byte buf[6];
  1274.  
  1275.    png_debug(1, "in png_handle_bKGD\n");
  1276.  
  1277.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1278.       png_error(png_ptr, "Missing IHDR before bKGD");
  1279.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  1280.    {
  1281.       png_warning(png_ptr, "Invalid bKGD after IDAT");
  1282.       png_crc_finish(png_ptr, length);
  1283.       return;
  1284.    }
  1285.    else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  1286.             !(png_ptr->mode & PNG_HAVE_PLTE))
  1287.    {
  1288.       png_warning(png_ptr, "Missing PLTE before bKGD");
  1289.       png_crc_finish(png_ptr, length);
  1290.       return;
  1291.    }
  1292.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
  1293.    {
  1294.       png_warning(png_ptr, "Duplicate bKGD chunk");
  1295.       png_crc_finish(png_ptr, length);
  1296.       return;
  1297.    }
  1298.  
  1299.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1300.       truelen = 1;
  1301.    else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  1302.       truelen = 6;
  1303.    else
  1304.       truelen = 2;
  1305.  
  1306.    if (length != truelen)
  1307.    {
  1308.       png_warning(png_ptr, "Incorrect bKGD chunk length");
  1309.       png_crc_finish(png_ptr, length);
  1310.       return;
  1311.    }
  1312.  
  1313.    png_crc_read(png_ptr, buf, truelen);
  1314.    if (png_crc_finish(png_ptr, 0))
  1315.       return;
  1316.  
  1317.    /* We convert the index value into RGB components so that we can allow
  1318.     * arbitrary RGB values for background when we have transparency, and
  1319.     * so it is easy to determine the RGB values of the background color
  1320.     * from the info_ptr struct. */
  1321.    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1322.    {
  1323.       png_ptr->background.index = buf[0];
  1324.       if(info_ptr->num_palette)
  1325.       {
  1326.           if(buf[0] > info_ptr->num_palette)
  1327.           {
  1328.              png_warning(png_ptr, "Incorrect bKGD chunk index value");
  1329.              return;
  1330.           }
  1331.           png_ptr->background.red =
  1332.              (png_uint_16)png_ptr->palette[buf[0]].red;
  1333.           png_ptr->background.green =
  1334.              (png_uint_16)png_ptr->palette[buf[0]].green;
  1335.           png_ptr->background.blue =
  1336.              (png_uint_16)png_ptr->palette[buf[0]].blue;
  1337.       }
  1338.    }
  1339.    else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
  1340.    {
  1341.       png_ptr->background.red =
  1342.       png_ptr->background.green =
  1343.       png_ptr->background.blue =
  1344.       png_ptr->background.gray = png_get_uint_16(buf);
  1345.    }
  1346.    else
  1347.    {
  1348.       png_ptr->background.red = png_get_uint_16(buf);
  1349.       png_ptr->background.green = png_get_uint_16(buf + 2);
  1350.       png_ptr->background.blue = png_get_uint_16(buf + 4);
  1351.    }
  1352.  
  1353.    png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
  1354. }
  1355. #endif
  1356.  
  1357. #if defined(PNG_READ_hIST_SUPPORTED)
  1358. void /* PRIVATE */
  1359. png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1360. {
  1361.    int num, i;
  1362.    png_uint_16    readbuf[PNG_MAX_PALETTE_LENGTH];
  1363.  
  1364.    png_debug(1, "in png_handle_hIST\n");
  1365.  
  1366.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1367.       png_error(png_ptr, "Missing IHDR before hIST");
  1368.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  1369.    {
  1370.       png_warning(png_ptr, "Invalid hIST after IDAT");
  1371.       png_crc_finish(png_ptr, length);
  1372.       return;
  1373.    }
  1374.    else if (!(png_ptr->mode & PNG_HAVE_PLTE))
  1375.    {
  1376.       png_warning(png_ptr, "Missing PLTE before hIST");
  1377.       png_crc_finish(png_ptr, length);
  1378.       return;
  1379.    }
  1380.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
  1381.    {
  1382.       png_warning(png_ptr, "Duplicate hIST chunk");
  1383.       png_crc_finish(png_ptr, length);
  1384.       return;
  1385.    }
  1386.  
  1387.    num = (int)length / 2 ;
  1388.    if (num != png_ptr->num_palette)
  1389.    {
  1390.       png_warning(png_ptr, "Incorrect hIST chunk length");
  1391.       png_crc_finish(png_ptr, length);
  1392.       return;
  1393.    }
  1394.  
  1395.    for (i = 0; i < num; i++)
  1396.    {
  1397.       png_byte buf[2];
  1398.  
  1399.       png_crc_read(png_ptr, buf, 2);
  1400.       readbuf[i] = png_get_uint_16(buf);
  1401.    }
  1402.  
  1403.    if (png_crc_finish(png_ptr, 0))
  1404.       return;
  1405.  
  1406.    png_set_hIST(png_ptr, info_ptr, readbuf);
  1407. }
  1408. #endif
  1409.  
  1410. #if defined(PNG_READ_pHYs_SUPPORTED)
  1411. void /* PRIVATE */
  1412. png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1413. {
  1414.    png_byte buf[9];
  1415.    png_uint_32 res_x, res_y;
  1416.    int unit_type;
  1417.  
  1418.    png_debug(1, "in png_handle_pHYs\n");
  1419.  
  1420.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1421.       png_error(png_ptr, "Missing IHDR before pHYs");
  1422.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  1423.    {
  1424.       png_warning(png_ptr, "Invalid pHYs after IDAT");
  1425.       png_crc_finish(png_ptr, length);
  1426.       return;
  1427.    }
  1428.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
  1429.    {
  1430.       png_warning(png_ptr, "Duplicate pHYs chunk");
  1431.       png_crc_finish(png_ptr, length);
  1432.       return;
  1433.    }
  1434.  
  1435.    if (length != 9)
  1436.    {
  1437.       png_warning(png_ptr, "Incorrect pHYs chunk length");
  1438.       png_crc_finish(png_ptr, length);
  1439.       return;
  1440.    }
  1441.  
  1442.    png_crc_read(png_ptr, buf, 9);
  1443.    if (png_crc_finish(png_ptr, 0))
  1444.       return;
  1445.  
  1446.    res_x = png_get_uint_32(buf);
  1447.    res_y = png_get_uint_32(buf + 4);
  1448.    unit_type = buf[8];
  1449.    png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
  1450. }
  1451. #endif
  1452.  
  1453. #if defined(PNG_READ_oFFs_SUPPORTED)
  1454. void /* PRIVATE */
  1455. png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1456. {
  1457.    png_byte buf[9];
  1458.    png_int_32 offset_x, offset_y;
  1459.    int unit_type;
  1460.  
  1461.    png_debug(1, "in png_handle_oFFs\n");
  1462.  
  1463.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1464.       png_error(png_ptr, "Missing IHDR before oFFs");
  1465.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  1466.    {
  1467.       png_warning(png_ptr, "Invalid oFFs after IDAT");
  1468.       png_crc_finish(png_ptr, length);
  1469.       return;
  1470.    }
  1471.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
  1472.    {
  1473.       png_warning(png_ptr, "Duplicate oFFs chunk");
  1474.       png_crc_finish(png_ptr, length);
  1475.       return;
  1476.    }
  1477.  
  1478.    if (length != 9)
  1479.    {
  1480.       png_warning(png_ptr, "Incorrect oFFs chunk length");
  1481.       png_crc_finish(png_ptr, length);
  1482.       return;
  1483.    }
  1484.  
  1485.    png_crc_read(png_ptr, buf, 9);
  1486.    if (png_crc_finish(png_ptr, 0))
  1487.       return;
  1488.  
  1489.    offset_x = png_get_int_32(buf);
  1490.    offset_y = png_get_int_32(buf + 4);
  1491.    unit_type = buf[8];
  1492.    png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
  1493. }
  1494. #endif
  1495.  
  1496. #if defined(PNG_READ_pCAL_SUPPORTED)
  1497. /* read the pCAL chunk (described in the PNG Extensions document) */
  1498. void /* PRIVATE */
  1499. png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1500. {
  1501.    png_charp purpose;
  1502.    png_int_32 X0, X1;
  1503.    png_byte type, nparams;
  1504.    png_charp buf, units, endptr;
  1505.    png_charpp params;
  1506.    png_size_t slength;
  1507.    int i;
  1508.  
  1509.    png_debug(1, "in png_handle_pCAL\n");
  1510.  
  1511.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1512.       png_error(png_ptr, "Missing IHDR before pCAL");
  1513.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  1514.    {
  1515.       png_warning(png_ptr, "Invalid pCAL after IDAT");
  1516.       png_crc_finish(png_ptr, length);
  1517.       return;
  1518.    }
  1519.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
  1520.    {
  1521.       png_warning(png_ptr, "Duplicate pCAL chunk");
  1522.       png_crc_finish(png_ptr, length);
  1523.       return;
  1524.    }
  1525.  
  1526.    png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n",
  1527.       length + 1);
  1528.    purpose = (png_charp)png_malloc(png_ptr, length + 1);
  1529.    slength = (png_size_t)length;
  1530.    png_crc_read(png_ptr, (png_bytep)purpose, slength);
  1531.  
  1532.    if (png_crc_finish(png_ptr, 0))
  1533.    {
  1534.       png_free(png_ptr, purpose);
  1535.       return;
  1536.    }
  1537.  
  1538.    purpose[slength] = 0x00; /* null terminate the last string */
  1539.  
  1540.    png_debug(3, "Finding end of pCAL purpose string\n");
  1541.    for (buf = purpose; *buf; buf++)
  1542.       /* empty loop */ ;
  1543.  
  1544.    endptr = purpose + slength;
  1545.  
  1546.    /* We need to have at least 12 bytes after the purpose string
  1547.       in order to get the parameter information. */
  1548.    if (endptr <= buf + 12)
  1549.    {
  1550.       png_warning(png_ptr, "Invalid pCAL data");
  1551.       png_free(png_ptr, purpose);
  1552.       return;
  1553.    }
  1554.  
  1555.    png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n");
  1556.    X0 = png_get_int_32((png_bytep)buf+1);
  1557.    X1 = png_get_int_32((png_bytep)buf+5);
  1558.    type = buf[9];
  1559.    nparams = buf[10];
  1560.    units = buf + 11;
  1561.  
  1562.    png_debug(3, "Checking pCAL equation type and number of parameters\n");
  1563.    /* Check that we have the right number of parameters for known
  1564.       equation types. */
  1565.    if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
  1566.        (type == PNG_EQUATION_BASE_E && nparams != 3) ||
  1567.        (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
  1568.        (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
  1569.    {
  1570.       png_warning(png_ptr, "Invalid pCAL parameters for equation type");
  1571.       png_free(png_ptr, purpose);
  1572.       return;
  1573.    }
  1574.    else if (type >= PNG_EQUATION_LAST)
  1575.    {
  1576.       png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
  1577.    }
  1578.  
  1579.    for (buf = units; *buf; buf++)
  1580.       /* Empty loop to move past the units string. */ ;
  1581.  
  1582.    png_debug(3, "Allocating pCAL parameters array\n");
  1583.    params = (png_charpp)png_malloc(png_ptr, (png_uint_32)(nparams
  1584.       *sizeof(png_charp))) ;
  1585.  
  1586.    /* Get pointers to the start of each parameter string. */
  1587.    for (i = 0; i < (int)nparams; i++)
  1588.    {
  1589.       buf++; /* Skip the null string terminator from previous parameter. */
  1590.  
  1591.       png_debug1(3, "Reading pCAL parameter %d\n", i);
  1592.       for (params[i] = buf; *buf != 0x00 && buf <= endptr; buf++)
  1593.          /* Empty loop to move past each parameter string */ ;
  1594.  
  1595.       /* Make sure we haven't run out of data yet */
  1596.       if (buf > endptr)
  1597.       {
  1598.          png_warning(png_ptr, "Invalid pCAL data");
  1599.          png_free(png_ptr, purpose);
  1600.          png_free(png_ptr, params);
  1601.          return;
  1602.       }
  1603.    }
  1604.  
  1605.    png_set_pCAL(png_ptr, info_ptr, purpose, X0, X1, type, nparams,
  1606.       units, params);
  1607.  
  1608.    png_free(png_ptr, purpose);
  1609.    png_free(png_ptr, params);
  1610. }
  1611. #endif
  1612.  
  1613. #if defined(PNG_READ_sCAL_SUPPORTED)
  1614. /* read the sCAL chunk */
  1615. void /* PRIVATE */
  1616. png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1617. {
  1618.    png_charp buffer, ep;
  1619. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1620.    double width, height;
  1621.    png_charp vp;
  1622. #else
  1623. #ifdef PNG_FIXED_POINT_SUPPORTED
  1624.    png_charp swidth, sheight;
  1625. #endif
  1626. #endif
  1627.    png_size_t slength;
  1628.  
  1629.    png_debug(1, "in png_handle_sCAL\n");
  1630.  
  1631.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1632.       png_error(png_ptr, "Missing IHDR before sCAL");
  1633.    else if (png_ptr->mode & PNG_HAVE_IDAT)
  1634.    {
  1635.       png_warning(png_ptr, "Invalid sCAL after IDAT");
  1636.       png_crc_finish(png_ptr, length);
  1637.       return;
  1638.    }
  1639.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
  1640.    {
  1641.       png_warning(png_ptr, "Duplicate sCAL chunk");
  1642.       png_crc_finish(png_ptr, length);
  1643.       return;
  1644.    }
  1645.  
  1646.    png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n",
  1647.       length + 1);
  1648.    buffer = (png_charp)png_malloc(png_ptr, length + 1);
  1649.    slength = (png_size_t)length;
  1650.    png_crc_read(png_ptr, (png_bytep)buffer, slength);
  1651.  
  1652.    if (png_crc_finish(png_ptr, 0))
  1653.    {
  1654.       png_free(png_ptr, buffer);
  1655.       return;
  1656.    }
  1657.  
  1658.    buffer[slength] = 0x00; /* null terminate the last string */
  1659.  
  1660.    ep = buffer + 1;        /* skip unit byte */
  1661.  
  1662. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1663.    width = strtod(ep, &vp);
  1664.    if (*vp)
  1665.    {
  1666.        png_warning(png_ptr, "malformed width string in sCAL chunk");
  1667.        return;
  1668.    }
  1669. #else
  1670. #ifdef PNG_FIXED_POINT_SUPPORTED
  1671.    swidth = (png_charp)png_malloc(png_ptr, png_strlen(ep) + 1);
  1672.    png_memcpy(swidth, ep, (png_size_t)png_strlen(ep));
  1673. #endif
  1674. #endif
  1675.  
  1676.    for (ep = buffer; *ep; ep++)
  1677.       /* empty loop */ ;
  1678.    ep++;
  1679.  
  1680. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1681.    height = strtod(ep, &vp);
  1682.    if (*vp)
  1683.    {
  1684.        png_warning(png_ptr, "malformed height string in sCAL chunk");
  1685.        return;
  1686.    }
  1687. #else
  1688. #ifdef PNG_FIXED_POINT_SUPPORTED
  1689.    sheight = (png_charp)png_malloc(png_ptr, png_strlen(ep) + 1);
  1690.    png_memcpy(sheight, ep, (png_size_t)png_strlen(ep));
  1691. #endif
  1692. #endif
  1693.  
  1694.    if (buffer + slength < ep
  1695. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1696.       || width <= 0. || height <= 0.
  1697. #endif
  1698.       )
  1699.    {
  1700.       png_warning(png_ptr, "Invalid sCAL data");
  1701.       png_free(png_ptr, buffer);
  1702. #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
  1703.       png_free(png_ptr, swidth);
  1704.       png_free(png_ptr, sheight);
  1705. #endif
  1706.       return;
  1707.    }
  1708.  
  1709.  
  1710. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1711.    png_set_sCAL(png_ptr, info_ptr, buffer[0], width, height);
  1712. #else
  1713. #ifdef PNG_FIXED_POINT_SUPPORTED
  1714.    png_set_sCAL_s(png_ptr, info_ptr, buffer[0], swidth, sheight);
  1715. #endif
  1716. #endif
  1717.  
  1718.    png_free(png_ptr, buffer);
  1719. #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
  1720.    png_free(png_ptr, swidth);
  1721.    png_free(png_ptr, sheight);
  1722. #endif
  1723. }
  1724. #endif
  1725.  
  1726. #if defined(PNG_READ_tIME_SUPPORTED)
  1727. void /* PRIVATE */
  1728. png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1729. {
  1730.    png_byte buf[7];
  1731.    png_time mod_time;
  1732.  
  1733.    png_debug(1, "in png_handle_tIME\n");
  1734.  
  1735.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1736.       png_error(png_ptr, "Out of place tIME chunk");
  1737.    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
  1738.    {
  1739.       png_warning(png_ptr, "Duplicate tIME chunk");
  1740.       png_crc_finish(png_ptr, length);
  1741.       return;
  1742.    }
  1743.  
  1744.    if (png_ptr->mode & PNG_HAVE_IDAT)
  1745.       png_ptr->mode |= PNG_AFTER_IDAT;
  1746.  
  1747.    if (length != 7)
  1748.    {
  1749.       png_warning(png_ptr, "Incorrect tIME chunk length");
  1750.       png_crc_finish(png_ptr, length);
  1751.       return;
  1752.    }
  1753.  
  1754.    png_crc_read(png_ptr, buf, 7);
  1755.    if (png_crc_finish(png_ptr, 0))
  1756.       return;
  1757.  
  1758.    mod_time.second = buf[6];
  1759.    mod_time.minute = buf[5];
  1760.    mod_time.hour = buf[4];
  1761.    mod_time.day = buf[3];
  1762.    mod_time.month = buf[2];
  1763.    mod_time.year = png_get_uint_16(buf);
  1764.  
  1765.    png_set_tIME(png_ptr, info_ptr, &mod_time);
  1766. }
  1767. #endif
  1768.  
  1769. #if defined(PNG_READ_tEXt_SUPPORTED)
  1770. /* Note: this does not properly handle chunks that are > 64K under DOS */
  1771. void /* PRIVATE */
  1772. png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1773. {
  1774.    png_textp text_ptr;
  1775.    png_charp key;
  1776.    png_charp text;
  1777.    png_uint_32 skip = 0;
  1778.    png_size_t slength;
  1779.  
  1780.    png_debug(1, "in png_handle_tEXt\n");
  1781.  
  1782.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1783.       png_error(png_ptr, "Missing IHDR before tEXt");
  1784.  
  1785.    if (png_ptr->mode & PNG_HAVE_IDAT)
  1786.       png_ptr->mode |= PNG_AFTER_IDAT;
  1787.  
  1788. #ifdef PNG_MAX_MALLOC_64K
  1789.    if (length > (png_uint_32)65535L)
  1790.    {
  1791.       png_warning(png_ptr, "tEXt chunk too large to fit in memory");
  1792.       skip = length - (png_uint_32)65535L;
  1793.       length = (png_uint_32)65535L;
  1794.    }
  1795. #endif
  1796.  
  1797.    key = (png_charp)png_malloc(png_ptr, length + 1);
  1798.    slength = (png_size_t)length;
  1799.    png_crc_read(png_ptr, (png_bytep)key, slength);
  1800.  
  1801.    if (png_crc_finish(png_ptr, skip))
  1802.    {
  1803.       png_free(png_ptr, key);
  1804.       return;
  1805.    }
  1806.  
  1807.    key[slength] = 0x00;
  1808.  
  1809.    for (text = key; *text; text++)
  1810.       /* empty loop to find end of key */ ;
  1811.  
  1812.    if (text != key + slength)
  1813.       text++;
  1814.  
  1815.    text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
  1816.    text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
  1817.    text_ptr->key = key;
  1818. #ifdef PNG_iTXt_SUPPORTED
  1819.    text_ptr->lang = NULL;
  1820.    text_ptr->lang_key = NULL;
  1821.    text_ptr->itxt_length = 0;
  1822. #endif
  1823.    text_ptr->text = text;
  1824.    text_ptr->text_length = png_strlen(text);
  1825.  
  1826.    png_set_text(png_ptr, info_ptr, text_ptr, 1);
  1827.  
  1828.    png_free(png_ptr, key);
  1829.    png_free(png_ptr, text_ptr);
  1830. }
  1831. #endif
  1832.  
  1833. #if defined(PNG_READ_zTXt_SUPPORTED)
  1834. /* note: this does not correctly handle chunks that are > 64K under DOS */
  1835. void /* PRIVATE */
  1836. png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1837. {
  1838.    png_textp text_ptr;
  1839.    png_charp chunkdata;
  1840.    png_charp text;
  1841.    int comp_type;
  1842.    png_size_t slength, prefix_len, data_len;
  1843.  
  1844.    png_debug(1, "in png_handle_zTXt\n");
  1845.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1846.       png_error(png_ptr, "Missing IHDR before zTXt");
  1847.  
  1848.    if (png_ptr->mode & PNG_HAVE_IDAT)
  1849.       png_ptr->mode |= PNG_AFTER_IDAT;
  1850.  
  1851. #ifdef PNG_MAX_MALLOC_64K
  1852.    /* We will no doubt have problems with chunks even half this size, but
  1853.       there is no hard and fast rule to tell us where to stop. */
  1854.    if (length > (png_uint_32)65535L)
  1855.    {
  1856.      png_warning(png_ptr,"zTXt chunk too large to fit in memory");
  1857.      png_crc_finish(png_ptr, length);
  1858.      return;
  1859.    }
  1860. #endif
  1861.  
  1862.    chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
  1863.      slength = (png_size_t)length;
  1864.    png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
  1865.    if (png_crc_finish(png_ptr, 0))
  1866.    {
  1867.       png_free(png_ptr, chunkdata);
  1868.       return;
  1869.    }
  1870.  
  1871.    chunkdata[slength] = 0x00;
  1872.  
  1873.    for (text = chunkdata; *text; text++)
  1874.       /* empty loop */ ;
  1875.  
  1876.    /* zTXt must have some text after the chunkdataword */
  1877.    if (text == chunkdata + slength)
  1878.    {
  1879.       comp_type = PNG_TEXT_COMPRESSION_NONE;
  1880.       png_warning(png_ptr, "Zero length zTXt chunk");
  1881.    }
  1882.    else
  1883.    {
  1884.        comp_type = *(++text);
  1885.        if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
  1886.        {
  1887.           png_warning(png_ptr, "Unknown compression type in zTXt chunk");
  1888.           comp_type = PNG_TEXT_COMPRESSION_zTXt;
  1889.        }
  1890.        text++;        /* skip the compression_method byte */
  1891.    }
  1892.    prefix_len = text - chunkdata;
  1893.  
  1894.    chunkdata = (png_charp)png_decompress_chunk(png_ptr, comp_type, chunkdata,
  1895.                                     (png_size_t)length, prefix_len, &data_len);
  1896.  
  1897.    text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
  1898.    text_ptr->compression = comp_type;
  1899.    text_ptr->key = chunkdata;
  1900. #ifdef PNG_iTXt_SUPPORTED
  1901.    text_ptr->lang = NULL;
  1902.    text_ptr->lang_key = NULL;
  1903.    text_ptr->itxt_length = 0;
  1904. #endif
  1905.    text_ptr->text = chunkdata + prefix_len;
  1906.    text_ptr->text_length = data_len;
  1907.  
  1908.    png_set_text(png_ptr, info_ptr, text_ptr, 1);
  1909.  
  1910.    png_free(png_ptr, text_ptr);
  1911.    png_free(png_ptr, chunkdata);
  1912. }
  1913. #endif
  1914.  
  1915. #if defined(PNG_READ_iTXt_SUPPORTED)
  1916. /* note: this does not correctly handle chunks that are > 64K under DOS */
  1917. void /* PRIVATE */
  1918. png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  1919. {
  1920.    png_textp text_ptr;
  1921.    png_charp chunkdata;
  1922.    png_charp key, lang, text, lang_key;
  1923.    int comp_flag;
  1924.    int comp_type = 0;
  1925.    png_size_t slength, prefix_len, data_len;
  1926.  
  1927.    png_debug(1, "in png_handle_iTXt\n");
  1928.  
  1929.    if (!(png_ptr->mode & PNG_HAVE_IHDR))
  1930.       png_error(png_ptr, "Missing IHDR before iTXt");
  1931.  
  1932.    if (png_ptr->mode & PNG_HAVE_IDAT)
  1933.       png_ptr->mode |= PNG_AFTER_IDAT;
  1934.  
  1935. #ifdef PNG_MAX_MALLOC_64K
  1936.    /* We will no doubt have problems with chunks even half this size, but
  1937.       there is no hard and fast rule to tell us where to stop. */
  1938.    if (length > (png_uint_32)65535L)
  1939.    {
  1940.      png_warning(png_ptr,"iTXt chunk too large to fit in memory");
  1941.      png_crc_finish(png_ptr, length);
  1942.      return;
  1943.    }
  1944. #endif
  1945.  
  1946.    chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
  1947.    slength = (png_size_t)length;
  1948.    png_crc_read(png_ptr, (png_bytep)chunkdata, slength);
  1949.    if (png_crc_finish(png_ptr, 0))
  1950.    {
  1951.       png_free(png_ptr, chunkdata);
  1952.       return;
  1953.    }
  1954.  
  1955.    chunkdata[slength] = 0x00;
  1956.  
  1957.    for (lang = chunkdata; *lang; lang++)
  1958.       /* empty loop */ ;
  1959.    lang++;        /* skip NUL separator */
  1960.  
  1961.    /* iTXt must have a language tag (possibly empty), two compression bytes,
  1962.       translated keyword (possibly empty), and possibly some text after the
  1963.       keyword */
  1964.  
  1965.    if (lang >= chunkdata + slength)
  1966.    {
  1967.       comp_flag = PNG_TEXT_COMPRESSION_NONE;
  1968.       png_warning(png_ptr, "Zero length iTXt chunk");
  1969.    }
  1970.    else
  1971.    {
  1972.        comp_flag = *lang++;
  1973.        comp_type = *lang++;
  1974.    }
  1975.  
  1976.    for (lang_key = lang; *lang_key; lang_key++)
  1977.       /* empty loop */ ;
  1978.    lang_key++;        /* skip NUL separator */
  1979.  
  1980.    for (text = lang_key; *text; text++)
  1981.       /* empty loop */ ;
  1982.    text++;        /* skip NUL separator */
  1983.  
  1984.    prefix_len = text - chunkdata;
  1985.  
  1986.    key=chunkdata;
  1987.    if (comp_flag)
  1988.        chunkdata = png_decompress_chunk(png_ptr, comp_type, chunkdata,
  1989.           (size_t)length, prefix_len, &data_len);
  1990.    else
  1991.        data_len=png_strlen(chunkdata + prefix_len);
  1992.    text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
  1993.    text_ptr->compression = (int)comp_flag + 1;
  1994.    text_ptr->lang_key = chunkdata+(lang_key-key);
  1995.    text_ptr->lang = chunkdata+(lang-key);
  1996.    text_ptr->itxt_length = data_len;
  1997.    text_ptr->text_length = 0;
  1998.    text_ptr->key = chunkdata;
  1999.    text_ptr->text = chunkdata + prefix_len;
  2000.  
  2001.    png_set_text(png_ptr, info_ptr, text_ptr, 1);
  2002.  
  2003.    png_free(png_ptr, text_ptr);
  2004.    png_free(png_ptr, chunkdata);
  2005. }
  2006. #endif
  2007.  
  2008. /* This function is called when we haven't found a handler for a
  2009.    chunk.  If there isn't a problem with the chunk itself (ie bad
  2010.    chunk name, CRC, or a critical chunk), the chunk is silently ignored
  2011.    -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
  2012.    case it will be saved away to be written out later. */
  2013. void /* PRIVATE */
  2014. png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
  2015. {
  2016.    png_uint_32 skip = 0;
  2017.  
  2018.    png_debug(1, "in png_handle_unknown\n");
  2019.  
  2020.    if (png_ptr->mode & PNG_HAVE_IDAT)
  2021.    {
  2022. #ifdef PNG_USE_LOCAL_ARRAYS
  2023.       PNG_IDAT;
  2024. #endif
  2025.       if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))  /* not an IDAT */
  2026.          png_ptr->mode |= PNG_AFTER_IDAT;
  2027.    }
  2028.  
  2029.    png_check_chunk_name(png_ptr, png_ptr->chunk_name);
  2030.  
  2031.    if (!(png_ptr->chunk_name[0] & 0x20))
  2032.    {
  2033. #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  2034.       if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
  2035.            HANDLE_CHUNK_ALWAYS
  2036. #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  2037.            && png_ptr->read_user_chunk_fn == NULL
  2038. #endif
  2039.         )
  2040. #endif
  2041.           png_chunk_error(png_ptr, "unknown critical chunk");
  2042.    }
  2043.  
  2044. #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  2045.    if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
  2046.    {
  2047.        png_unknown_chunk chunk;
  2048.  
  2049. #ifdef PNG_MAX_MALLOC_64K
  2050.        if (length > (png_uint_32)65535L)
  2051.        {
  2052.            png_warning(png_ptr, "unknown chunk too large to fit in memory");
  2053.            skip = length - (png_uint_32)65535L;
  2054.            length = (png_uint_32)65535L;
  2055.        }
  2056. #endif
  2057.        png_strcpy((png_charp)chunk.name, (png_charp)png_ptr->chunk_name);
  2058.        chunk.data = (png_bytep)png_malloc(png_ptr, length);
  2059.        chunk.size = (png_size_t)length;
  2060.        png_crc_read(png_ptr, (png_bytep)chunk.data, length);
  2061. #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  2062.        if(png_ptr->read_user_chunk_fn != NULL)
  2063.        {
  2064.           /* callback to user unknown chunk handler */
  2065.           if ((*(png_ptr->read_user_chunk_fn)) (png_ptr, &chunk) <= 0)
  2066.           {
  2067.              if (!(png_ptr->chunk_name[0] & 0x20))
  2068.                 if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
  2069.                      HANDLE_CHUNK_ALWAYS)
  2070.                    png_chunk_error(png_ptr, "unknown critical chunk");
  2071.              png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
  2072.           }
  2073.        }
  2074.        else
  2075. #endif
  2076.           png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
  2077.        png_free(png_ptr, chunk.data);
  2078.    }
  2079.    else
  2080. #endif
  2081.       skip = length;
  2082.  
  2083.    png_crc_finish(png_ptr, skip);
  2084.  
  2085. #if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
  2086.    if (info_ptr == NULL)
  2087.      /* quiet compiler warnings about unused info_ptr */ ;
  2088. #endif
  2089. }
  2090.  
  2091. /* This function is called to verify that a chunk name is valid.
  2092.    This function can't have the "critical chunk check" incorporated
  2093.    into it, since in the future we will need to be able to call user
  2094.    functions to handle unknown critical chunks after we check that
  2095.    the chunk name itself is valid. */
  2096.  
  2097. #define isnonalpha(c) ((c) < 41 || (c) > 122 || ((c) > 90 && (c) < 97))
  2098.  
  2099. void /* PRIVATE */
  2100. png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)
  2101. {
  2102.    png_debug(1, "in png_check_chunk_name\n");
  2103.    if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
  2104.        isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
  2105.    {
  2106.       png_chunk_error(png_ptr, "invalid chunk type");
  2107.    }
  2108. }
  2109.  
  2110. /* Combines the row recently read in with the existing pixels in the
  2111.    row.  This routine takes care of alpha and transparency if requested.
  2112.    This routine also handles the two methods of progressive display
  2113.    of interlaced images, depending on the mask value.
  2114.    The mask value describes which pixels are to be combined with
  2115.    the row.  The pattern always repeats every 8 pixels, so just 8
  2116.    bits are needed.  A one indicates the pixel is to be combined,
  2117.    a zero indicates the pixel is to be skipped.  This is in addition
  2118.    to any alpha or transparency value associated with the pixel.  If
  2119.    you want all pixels to be combined, pass 0xff (255) in mask.  */
  2120. #ifndef PNG_HAVE_ASSEMBLER_COMBINE_ROW
  2121. void /* PRIVATE */
  2122. png_combine_row(png_structp png_ptr, png_bytep row, int mask)
  2123. {
  2124.    png_debug(1,"in png_combine_row\n");
  2125.    if (mask == 0xff)
  2126.    {
  2127.       png_memcpy(row, png_ptr->row_buf + 1,
  2128.          (png_size_t)((png_ptr->width *
  2129.          png_ptr->row_info.pixel_depth + 7) >> 3));
  2130.    }
  2131.    else
  2132.    {
  2133.       switch (png_ptr->row_info.pixel_depth)
  2134.       {
  2135.          case 1:
  2136.          {
  2137.             png_bytep sp = png_ptr->row_buf + 1;
  2138.             png_bytep dp = row;
  2139.             int s_inc, s_start, s_end;
  2140.             int m = 0x80;
  2141.             int shift;
  2142.             png_uint_32 i;
  2143.             png_uint_32 row_width = png_ptr->width;
  2144.  
  2145. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2146.             if (png_ptr->transformations & PNG_PACKSWAP)
  2147.             {
  2148.                 s_start = 0;
  2149.                 s_end = 7;
  2150.                 s_inc = 1;
  2151.             }
  2152.             else
  2153. #endif
  2154.             {
  2155.                 s_start = 7;
  2156.                 s_end = 0;
  2157.                 s_inc = -1;
  2158.             }
  2159.  
  2160.             shift = s_start;
  2161.  
  2162.             for (i = 0; i < row_width; i++)
  2163.             {
  2164.                if (m & mask)
  2165.                {
  2166.                   int value;
  2167.  
  2168.                   value = (*sp >> shift) & 0x01;
  2169.                   *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
  2170.                   *dp |= (png_byte)(value << shift);
  2171.                }
  2172.  
  2173.                if (shift == s_end)
  2174.                {
  2175.                   shift = s_start;
  2176.                   sp++;
  2177.                   dp++;
  2178.                }
  2179.                else
  2180.                   shift += s_inc;
  2181.  
  2182.                if (m == 1)
  2183.                   m = 0x80;
  2184.                else
  2185.                   m >>= 1;
  2186.             }
  2187.             break;
  2188.          }
  2189.          case 2:
  2190.          {
  2191.             png_bytep sp = png_ptr->row_buf + 1;
  2192.             png_bytep dp = row;
  2193.             int s_start, s_end, s_inc;
  2194.             int m = 0x80;
  2195.             int shift;
  2196.             png_uint_32 i;
  2197.             png_uint_32 row_width = png_ptr->width;
  2198.             int value;
  2199.  
  2200. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2201.             if (png_ptr->transformations & PNG_PACKSWAP)
  2202.             {
  2203.                s_start = 0;
  2204.                s_end = 6;
  2205.                s_inc = 2;
  2206.             }
  2207.             else
  2208. #endif
  2209.             {
  2210.                s_start = 6;
  2211.                s_end = 0;
  2212.                s_inc = -2;
  2213.             }
  2214.  
  2215.             shift = s_start;
  2216.  
  2217.             for (i = 0; i < row_width; i++)
  2218.             {
  2219.                if (m & mask)
  2220.                {
  2221.                   value = (*sp >> shift) & 0x03;
  2222.                   *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  2223.                   *dp |= (png_byte)(value << shift);
  2224.                }
  2225.  
  2226.                if (shift == s_end)
  2227.                {
  2228.                   shift = s_start;
  2229.                   sp++;
  2230.                   dp++;
  2231.                }
  2232.                else
  2233.                   shift += s_inc;
  2234.                if (m == 1)
  2235.                   m = 0x80;
  2236.                else
  2237.                   m >>= 1;
  2238.             }
  2239.             break;
  2240.          }
  2241.          case 4:
  2242.          {
  2243.             png_bytep sp = png_ptr->row_buf + 1;
  2244.             png_bytep dp = row;
  2245.             int s_start, s_end, s_inc;
  2246.             int m = 0x80;
  2247.             int shift;
  2248.             png_uint_32 i;
  2249.             png_uint_32 row_width = png_ptr->width;
  2250.             int value;
  2251.  
  2252. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2253.             if (png_ptr->transformations & PNG_PACKSWAP)
  2254.             {
  2255.                s_start = 0;
  2256.                s_end = 4;
  2257.                s_inc = 4;
  2258.             }
  2259.             else
  2260. #endif
  2261.             {
  2262.                s_start = 4;
  2263.                s_end = 0;
  2264.                s_inc = -4;
  2265.             }
  2266.             shift = s_start;
  2267.  
  2268.             for (i = 0; i < row_width; i++)
  2269.             {
  2270.                if (m & mask)
  2271.                {
  2272.                   value = (*sp >> shift) & 0xf;
  2273.                   *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  2274.                   *dp |= (png_byte)(value << shift);
  2275.                }
  2276.  
  2277.                if (shift == s_end)
  2278.                {
  2279.                   shift = s_start;
  2280.                   sp++;
  2281.                   dp++;
  2282.                }
  2283.                else
  2284.                   shift += s_inc;
  2285.                if (m == 1)
  2286.                   m = 0x80;
  2287.                else
  2288.                   m >>= 1;
  2289.             }
  2290.             break;
  2291.          }
  2292.          default:
  2293.          {
  2294.             png_bytep sp = png_ptr->row_buf + 1;
  2295.             png_bytep dp = row;
  2296.             png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
  2297.             png_uint_32 i;
  2298.             png_uint_32 row_width = png_ptr->width;
  2299.             png_byte m = 0x80;
  2300.  
  2301.  
  2302.             for (i = 0; i < row_width; i++)
  2303.             {
  2304.                if (m & mask)
  2305.                {
  2306.                   png_memcpy(dp, sp, pixel_bytes);
  2307.                }
  2308.  
  2309.                sp += pixel_bytes;
  2310.                dp += pixel_bytes;
  2311.  
  2312.                if (m == 1)
  2313.                   m = 0x80;
  2314.                else
  2315.                   m >>= 1;
  2316.             }
  2317.             break;
  2318.          }
  2319.       }
  2320.    }
  2321. }
  2322. #endif /* !PNG_HAVE_ASSEMBLER_COMBINE_ROW */
  2323.  
  2324. #ifdef PNG_READ_INTERLACING_SUPPORTED
  2325. #ifndef PNG_HAVE_ASSEMBLER_READ_INTERLACE   /* else in pngvcrd.c, pnggccrd.c */
  2326. /* OLD pre-1.0.9 interface:
  2327. void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
  2328.    png_uint_32 transformations)
  2329.  */
  2330. void /* PRIVATE */
  2331. png_do_read_interlace(png_structp png_ptr)
  2332. {
  2333.    png_row_infop row_info = &(png_ptr->row_info);
  2334.    png_bytep row = png_ptr->row_buf + 1;
  2335.    int pass = png_ptr->pass;
  2336.    png_uint_32 transformations = png_ptr->transformations;
  2337. #ifdef PNG_USE_LOCAL_ARRAYS
  2338.    /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2339.    /* offset to next interlace block */
  2340.    const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2341. #endif
  2342.  
  2343.    png_debug(1,"in png_do_read_interlace (stock C version)\n");
  2344.    if (row != NULL && row_info != NULL)
  2345.    {
  2346.       png_uint_32 final_width;
  2347.  
  2348.       final_width = row_info->width * png_pass_inc[pass];
  2349.  
  2350.       switch (row_info->pixel_depth)
  2351.       {
  2352.          case 1:
  2353.          {
  2354.             png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
  2355.             png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
  2356.             int sshift, dshift;
  2357.             int s_start, s_end, s_inc;
  2358.             int jstop = png_pass_inc[pass];
  2359.             png_byte v;
  2360.             png_uint_32 i;
  2361.             int j;
  2362.  
  2363. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2364.             if (transformations & PNG_PACKSWAP)
  2365.             {
  2366.                 sshift = (int)((row_info->width + 7) & 0x07);
  2367.                 dshift = (int)((final_width + 7) & 0x07);
  2368.                 s_start = 7;
  2369.                 s_end = 0;
  2370.                 s_inc = -1;
  2371.             }
  2372.             else
  2373. #endif
  2374.             {
  2375.                 sshift = 7 - (int)((row_info->width + 7) & 0x07);
  2376.                 dshift = 7 - (int)((final_width + 7) & 0x07);
  2377.                 s_start = 0;
  2378.                 s_end = 7;
  2379.                 s_inc = 1;
  2380.             }
  2381.  
  2382.             for (i = 0; i < row_info->width; i++)
  2383.             {
  2384.                v = (png_byte)((*sp >> sshift) & 0x01);
  2385.                for (j = 0; j < jstop; j++)
  2386.                {
  2387.                   *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
  2388.                   *dp |= (png_byte)(v << dshift);
  2389.                   if (dshift == s_end)
  2390.                   {
  2391.                      dshift = s_start;
  2392.                      dp--;
  2393.                   }
  2394.                   else
  2395.                      dshift += s_inc;
  2396.                }
  2397.                if (sshift == s_end)
  2398.                {
  2399.                   sshift = s_start;
  2400.                   sp--;
  2401.                }
  2402.                else
  2403.                   sshift += s_inc;
  2404.             }
  2405.             break;
  2406.          }
  2407.          case 2:
  2408.          {
  2409.             png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
  2410.             png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
  2411.             int sshift, dshift;
  2412.             int s_start, s_end, s_inc;
  2413.             int jstop = png_pass_inc[pass];
  2414.             png_uint_32 i;
  2415.  
  2416. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2417.             if (transformations & PNG_PACKSWAP)
  2418.             {
  2419.                sshift = (int)(((row_info->width + 3) & 0x03) << 1);
  2420.                dshift = (int)(((final_width + 3) & 0x03) << 1);
  2421.                s_start = 6;
  2422.                s_end = 0;
  2423.                s_inc = -2;
  2424.             }
  2425.             else
  2426. #endif
  2427.             {
  2428.                sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
  2429.                dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
  2430.                s_start = 0;
  2431.                s_end = 6;
  2432.                s_inc = 2;
  2433.             }
  2434.  
  2435.             for (i = 0; i < row_info->width; i++)
  2436.             {
  2437.                png_byte v;
  2438.                int j;
  2439.  
  2440.                v = (png_byte)((*sp >> sshift) & 0x03);
  2441.                for (j = 0; j < jstop; j++)
  2442.                {
  2443.                   *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
  2444.                   *dp |= (png_byte)(v << dshift);
  2445.                   if (dshift == s_end)
  2446.                   {
  2447.                      dshift = s_start;
  2448.                      dp--;
  2449.                   }
  2450.                   else
  2451.                      dshift += s_inc;
  2452.                }
  2453.                if (sshift == s_end)
  2454.                {
  2455.                   sshift = s_start;
  2456.                   sp--;
  2457.                }
  2458.                else
  2459.                   sshift += s_inc;
  2460.             }
  2461.             break;
  2462.          }
  2463.          case 4:
  2464.          {
  2465.             png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
  2466.             png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
  2467.             int sshift, dshift;
  2468.             int s_start, s_end, s_inc;
  2469.             png_uint_32 i;
  2470.             int jstop = png_pass_inc[pass];
  2471.  
  2472. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  2473.             if (transformations & PNG_PACKSWAP)
  2474.             {
  2475.                sshift = (int)(((row_info->width + 1) & 0x01) << 2);
  2476.                dshift = (int)(((final_width + 1) & 0x01) << 2);
  2477.                s_start = 4;
  2478.                s_end = 0;
  2479.                s_inc = -4;
  2480.             }
  2481.             else
  2482. #endif
  2483.             {
  2484.                sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
  2485.                dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
  2486.                s_start = 0;
  2487.                s_end = 4;
  2488.                s_inc = 4;
  2489.             }
  2490.  
  2491.             for (i = 0; i < row_info->width; i++)
  2492.             {
  2493.                png_byte v = (png_byte)((*sp >> sshift) & 0xf);
  2494.                int j;
  2495.  
  2496.                for (j = 0; j < jstop; j++)
  2497.                {
  2498.                   *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
  2499.                   *dp |= (png_byte)(v << dshift);
  2500.                   if (dshift == s_end)
  2501.                   {
  2502.                      dshift = s_start;
  2503.                      dp--;
  2504.                   }
  2505.                   else
  2506.                      dshift += s_inc;
  2507.                }
  2508.                if (sshift == s_end)
  2509.                {
  2510.                   sshift = s_start;
  2511.                   sp--;
  2512.                }
  2513.                else
  2514.                   sshift += s_inc;
  2515.             }
  2516.             break;
  2517.          }
  2518.          default:
  2519.          {
  2520.             png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
  2521.             png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes;
  2522.             png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
  2523.  
  2524.             int jstop = png_pass_inc[pass];
  2525.             png_uint_32 i;
  2526.  
  2527.             for (i = 0; i < row_info->width; i++)
  2528.             {
  2529.                png_byte v[8];
  2530.                int j;
  2531.  
  2532.                png_memcpy(v, sp, pixel_bytes);
  2533.                for (j = 0; j < jstop; j++)
  2534.                {
  2535.                   png_memcpy(dp, v, pixel_bytes);
  2536.                   dp -= pixel_bytes;
  2537.                }
  2538.                sp -= pixel_bytes;
  2539.             }
  2540.             break;
  2541.          }
  2542.       }
  2543.       row_info->width = final_width;
  2544.       row_info->rowbytes = ((final_width *
  2545.          (png_uint_32)row_info->pixel_depth + 7) >> 3);
  2546.    }
  2547. #if !defined(PNG_READ_PACKSWAP_SUPPORTED)
  2548.    /* silence compiler warning */
  2549.    if (transformations)
  2550.       return;
  2551. #endif
  2552. }
  2553. #endif /* !PNG_HAVE_ASSEMBLER_READ_INTERLACE */
  2554. #endif /* PNG_READ_INTERLACING_SUPPORTED */
  2555.  
  2556. #ifndef PNG_HAVE_ASSEMBLER_READ_FILTER_ROW
  2557. void /* PRIVATE */
  2558. png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
  2559.    png_bytep prev_row, int filter)
  2560. {
  2561.    png_debug(1, "in png_read_filter_row\n");
  2562.    png_debug2(2,"row = %lu, filter = %d\n", png_ptr->row_number, filter);
  2563.    switch (filter)
  2564.    {
  2565.       case PNG_FILTER_VALUE_NONE:
  2566.          break;
  2567.       case PNG_FILTER_VALUE_SUB:
  2568.       {
  2569.          png_uint_32 i;
  2570.          png_uint_32 istop = row_info->rowbytes;
  2571.          png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
  2572.          png_bytep rp = row + bpp;
  2573.          png_bytep lp = row;
  2574.  
  2575.          for (i = bpp; i < istop; i++)
  2576.          {
  2577.             *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
  2578.             rp++;
  2579.          }
  2580.          break;
  2581.       }
  2582.       case PNG_FILTER_VALUE_UP:
  2583.       {
  2584.          png_uint_32 i;
  2585.          png_uint_32 istop = row_info->rowbytes;
  2586.          png_bytep rp = row;
  2587.          png_bytep pp = prev_row;
  2588.  
  2589.          for (i = 0; i < istop; i++)
  2590.          {
  2591.             *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
  2592.             rp++;
  2593.          }
  2594.          break;
  2595.       }
  2596.       case PNG_FILTER_VALUE_AVG:
  2597.       {
  2598.          png_uint_32 i;
  2599.          png_bytep rp = row;
  2600.          png_bytep pp = prev_row;
  2601.          png_bytep lp = row;
  2602.          png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
  2603.          png_uint_32 istop = row_info->rowbytes - bpp;
  2604.  
  2605.          for (i = 0; i < bpp; i++)
  2606.          {
  2607.             *rp = (png_byte)(((int)(*rp) +
  2608.                ((int)(*pp++) / 2 )) & 0xff);
  2609.             rp++;
  2610.          }
  2611.  
  2612.          for (i = 0; i < istop; i++)
  2613.          {
  2614.             *rp = (png_byte)(((int)(*rp) +
  2615.                (int)(*pp++ + *lp++) / 2 ) & 0xff);
  2616.             rp++;
  2617.          }
  2618.          break;
  2619.       }
  2620.       case PNG_FILTER_VALUE_PAETH:
  2621.       {
  2622.          png_uint_32 i;
  2623.          png_bytep rp = row;
  2624.          png_bytep pp = prev_row;
  2625.          png_bytep lp = row;
  2626.          png_bytep cp = prev_row;
  2627.          png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
  2628.          png_uint_32 istop=row_info->rowbytes - bpp;
  2629.  
  2630.          for (i = 0; i < bpp; i++)
  2631.          {
  2632.             *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
  2633.             rp++;
  2634.          }
  2635.  
  2636.          for (i = 0; i < istop; i++)   /* use leftover rp,pp */
  2637.          {
  2638.             int a, b, c, pa, pb, pc, p;
  2639.  
  2640.             a = *lp++;
  2641.             b = *pp++;
  2642.             c = *cp++;
  2643.  
  2644.             p = b - c;
  2645.             pc = a - c;
  2646.  
  2647. #ifdef PNG_USE_ABS
  2648.             pa = abs(p);
  2649.             pb = abs(pc);
  2650.             pc = abs(p + pc);
  2651. #else
  2652.             pa = p < 0 ? -p : p;
  2653.             pb = pc < 0 ? -pc : pc;
  2654.             pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  2655. #endif
  2656.  
  2657.             /*
  2658.                if (pa <= pb && pa <= pc)
  2659.                   p = a;
  2660.                else if (pb <= pc)
  2661.                   p = b;
  2662.                else
  2663.                   p = c;
  2664.              */
  2665.  
  2666.             p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
  2667.  
  2668.             *rp = (png_byte)(((int)(*rp) + p) & 0xff);
  2669.             rp++;
  2670.          }
  2671.          break;
  2672.       }
  2673.       default:
  2674.          png_warning(png_ptr, "Ignoring bad adaptive filter type");
  2675.          *row=0;
  2676.          break;
  2677.    }
  2678. }
  2679. #endif /* !PNG_HAVE_ASSEMBLER_READ_FILTER_ROW */
  2680.  
  2681. void /* PRIVATE */
  2682. png_read_finish_row(png_structp png_ptr)
  2683. {
  2684. #ifdef PNG_USE_LOCAL_ARRAYS
  2685.    /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2686.  
  2687.    /* start of interlace block */
  2688.    const int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  2689.  
  2690.    /* offset to next interlace block */
  2691.    const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2692.  
  2693.    /* start of interlace block in the y direction */
  2694.    const int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  2695.  
  2696.    /* offset to next interlace block in the y direction */
  2697.    const int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  2698. #endif
  2699.  
  2700.    png_debug(1, "in png_read_finish_row\n");
  2701.    png_ptr->row_number++;
  2702.    if (png_ptr->row_number < png_ptr->num_rows)
  2703.       return;
  2704.  
  2705.    if (png_ptr->interlaced)
  2706.    {
  2707.       png_ptr->row_number = 0;
  2708.       png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  2709.       do
  2710.       {
  2711.          png_ptr->pass++;
  2712.          if (png_ptr->pass >= 7)
  2713.             break;
  2714.          png_ptr->iwidth = (png_ptr->width +
  2715.             png_pass_inc[png_ptr->pass] - 1 -
  2716.             png_pass_start[png_ptr->pass]) /
  2717.             png_pass_inc[png_ptr->pass];
  2718.             png_ptr->irowbytes = ((png_ptr->iwidth *
  2719.                (png_uint_32)png_ptr->pixel_depth + 7) >> 3) +1;
  2720.  
  2721.          if (!(png_ptr->transformations & PNG_INTERLACE))
  2722.          {
  2723.             png_ptr->num_rows = (png_ptr->height +
  2724.                png_pass_yinc[png_ptr->pass] - 1 -
  2725.                png_pass_ystart[png_ptr->pass]) /
  2726.                png_pass_yinc[png_ptr->pass];
  2727.             if (!(png_ptr->num_rows))
  2728.                continue;
  2729.          }
  2730.          else  /* if (png_ptr->transformations & PNG_INTERLACE) */
  2731.             break;
  2732.       } while (png_ptr->iwidth == 0);
  2733.  
  2734.       if (png_ptr->pass < 7)
  2735.          return;
  2736.    }
  2737.  
  2738.    if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
  2739.    {
  2740. #ifdef PNG_USE_LOCAL_ARRAYS
  2741.       PNG_IDAT;
  2742. #endif
  2743.       char extra;
  2744.       int ret;
  2745.  
  2746.       png_ptr->zstream.next_out = (Byte *)&extra;
  2747.       png_ptr->zstream.avail_out = (uInt)1;
  2748.       for(;;)
  2749.       {
  2750.          if (!(png_ptr->zstream.avail_in))
  2751.          {
  2752.             while (!png_ptr->idat_size)
  2753.             {
  2754.                png_byte chunk_length[4];
  2755.  
  2756.                png_crc_finish(png_ptr, 0);
  2757.  
  2758.                png_read_data(png_ptr, chunk_length, 4);
  2759.                png_ptr->idat_size = png_get_uint_32(chunk_length);
  2760.  
  2761.                png_reset_crc(png_ptr);
  2762.                png_crc_read(png_ptr, png_ptr->chunk_name, 4);
  2763.                if (png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
  2764.                   png_error(png_ptr, "Not enough image data");
  2765.  
  2766.             }
  2767.             png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
  2768.             png_ptr->zstream.next_in = png_ptr->zbuf;
  2769.             if (png_ptr->zbuf_size > png_ptr->idat_size)
  2770.                png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
  2771.             png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
  2772.             png_ptr->idat_size -= png_ptr->zstream.avail_in;
  2773.          }
  2774.          ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  2775.          if (ret == Z_STREAM_END)
  2776.          {
  2777.             if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
  2778.                png_ptr->idat_size)
  2779.                png_error(png_ptr, "Extra compressed data");
  2780.             png_ptr->mode |= PNG_AFTER_IDAT;
  2781.             png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  2782.             break;
  2783.          }
  2784.          if (ret != Z_OK)
  2785.             png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  2786.                       "Decompression Error");
  2787.  
  2788.          if (!(png_ptr->zstream.avail_out))
  2789.             png_error(png_ptr, "Extra compressed data");
  2790.  
  2791.       }
  2792.       png_ptr->zstream.avail_out = 0;
  2793.    }
  2794.  
  2795.    if (png_ptr->idat_size || png_ptr->zstream.avail_in)
  2796.       png_error(png_ptr, "Extra compression data");
  2797.  
  2798.    inflateReset(&png_ptr->zstream);
  2799.  
  2800.    png_ptr->mode |= PNG_AFTER_IDAT;
  2801. }
  2802.  
  2803. void /* PRIVATE */
  2804. png_read_start_row(png_structp png_ptr)
  2805. {
  2806. #ifdef PNG_USE_LOCAL_ARRAYS
  2807.    /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  2808.  
  2809.    /* start of interlace block */
  2810.    const int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  2811.  
  2812.    /* offset to next interlace block */
  2813.    const int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  2814.  
  2815.    /* start of interlace block in the y direction */
  2816.    const int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  2817.  
  2818.    /* offset to next interlace block in the y direction */
  2819.    const int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  2820. #endif
  2821.  
  2822.    int max_pixel_depth;
  2823.    png_uint_32 row_bytes;
  2824.  
  2825.    png_debug(1, "in png_read_start_row\n");
  2826.    png_ptr->zstream.avail_in = 0;
  2827.    png_init_read_transformations(png_ptr);
  2828.    if (png_ptr->interlaced)
  2829.    {
  2830.       if (!(png_ptr->transformations & PNG_INTERLACE))
  2831.          png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  2832.             png_pass_ystart[0]) / png_pass_yinc[0];
  2833.       else
  2834.          png_ptr->num_rows = png_ptr->height;
  2835.  
  2836.       png_ptr->iwidth = (png_ptr->width +
  2837.          png_pass_inc[png_ptr->pass] - 1 -
  2838.          png_pass_start[png_ptr->pass]) /
  2839.          png_pass_inc[png_ptr->pass];
  2840.  
  2841.          row_bytes = ((png_ptr->iwidth *
  2842.             (png_uint_32)png_ptr->pixel_depth + 7) >> 3) +1;
  2843.          png_ptr->irowbytes = (png_size_t)row_bytes;
  2844.          if((png_uint_32)png_ptr->irowbytes != row_bytes)
  2845.             png_error(png_ptr, "Rowbytes overflow in png_read_start_row");
  2846.    }
  2847.    else
  2848.    {
  2849.       png_ptr->num_rows = png_ptr->height;
  2850.       png_ptr->iwidth = png_ptr->width;
  2851.       png_ptr->irowbytes = png_ptr->rowbytes + 1;
  2852.    }
  2853.    max_pixel_depth = png_ptr->pixel_depth;
  2854.  
  2855. #if defined(PNG_READ_PACK_SUPPORTED)
  2856.    if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
  2857.       max_pixel_depth = 8;
  2858. #endif
  2859.  
  2860. #if defined(PNG_READ_EXPAND_SUPPORTED)
  2861.    if (png_ptr->transformations & PNG_EXPAND)
  2862.    {
  2863.       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  2864.       {
  2865.          if (png_ptr->num_trans)
  2866.             max_pixel_depth = 32;
  2867.          else
  2868.             max_pixel_depth = 24;
  2869.       }
  2870.       else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  2871.       {
  2872.          if (max_pixel_depth < 8)
  2873.             max_pixel_depth = 8;
  2874.          if (png_ptr->num_trans)
  2875.             max_pixel_depth *= 2;
  2876.       }
  2877.       else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  2878.       {
  2879.          if (png_ptr->num_trans)
  2880.          {
  2881.             max_pixel_depth *= 4;
  2882.             max_pixel_depth /= 3;
  2883.          }
  2884.       }
  2885.    }
  2886. #endif
  2887.  
  2888. #if defined(PNG_READ_FILLER_SUPPORTED)
  2889.    if (png_ptr->transformations & (PNG_FILLER))
  2890.    {
  2891.       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  2892.          max_pixel_depth = 32;
  2893.       else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
  2894.       {
  2895.          if (max_pixel_depth <= 8)
  2896.             max_pixel_depth = 16;
  2897.          else
  2898.             max_pixel_depth = 32;
  2899.       }
  2900.       else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
  2901.       {
  2902.          if (max_pixel_depth <= 32)
  2903.             max_pixel_depth = 32;
  2904.          else
  2905.             max_pixel_depth = 64;
  2906.       }
  2907.    }
  2908. #endif
  2909.  
  2910. #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  2911.    if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  2912.    {
  2913.       if (
  2914. #if defined(PNG_READ_EXPAND_SUPPORTED)
  2915.         (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
  2916. #endif
  2917. #if defined(PNG_READ_FILLER_SUPPORTED)
  2918.         (png_ptr->transformations & (PNG_FILLER)) ||
  2919. #endif
  2920.         png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  2921.       {
  2922.          if (max_pixel_depth <= 16)
  2923.             max_pixel_depth = 32;
  2924.          else
  2925.             max_pixel_depth = 64;
  2926.       }
  2927.       else
  2928.       {
  2929.          if (max_pixel_depth <= 8)
  2930.            {
  2931.              if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2932.                max_pixel_depth = 32;
  2933.              else
  2934.                max_pixel_depth = 24;
  2935.            }
  2936.          else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2937.             max_pixel_depth = 64;
  2938.          else
  2939.             max_pixel_depth = 48;
  2940.       }
  2941.    }
  2942. #endif
  2943.  
  2944. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
  2945. defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
  2946.    if(png_ptr->transformations & PNG_USER_TRANSFORM)
  2947.      {
  2948.        int user_pixel_depth=png_ptr->user_transform_depth*
  2949.          png_ptr->user_transform_channels;
  2950.        if(user_pixel_depth > max_pixel_depth)
  2951.          max_pixel_depth=user_pixel_depth;
  2952.      }
  2953. #endif
  2954.  
  2955.    /* align the width on the next larger 8 pixels.  Mainly used
  2956.       for interlacing */
  2957.    row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
  2958.    /* calculate the maximum bytes needed, adding a byte and a pixel
  2959.       for safety's sake */
  2960.    row_bytes = ((row_bytes * (png_uint_32)max_pixel_depth + 7) >> 3) +
  2961.       1 + ((max_pixel_depth + 7) >> 3);
  2962. #ifdef PNG_MAX_MALLOC_64K
  2963.    if (row_bytes > (png_uint_32)65536L)
  2964.       png_error(png_ptr, "This image requires a row greater than 64KB");
  2965. #endif
  2966.    png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, row_bytes);
  2967.    png_ptr->row_buf_size = row_bytes;
  2968.  
  2969. #ifdef PNG_MAX_MALLOC_64K
  2970.    if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
  2971.       png_error(png_ptr, "This image requires a row greater than 64KB");
  2972. #endif
  2973.    png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
  2974.       png_ptr->rowbytes + 1));
  2975.  
  2976.    png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
  2977.  
  2978.    png_debug1(3, "width = %lu,\n", png_ptr->width);
  2979.    png_debug1(3, "height = %lu,\n", png_ptr->height);
  2980.    png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth);
  2981.    png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows);
  2982.    png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes);
  2983.    png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes);
  2984.  
  2985.    png_ptr->flags |= PNG_FLAG_ROW_INIT;
  2986. }
  2987.