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

  1.  
  2. /* pngread.c - read a PNG file
  3.  *
  4.  * libpng 1.0.8 - July 24, 2000
  5.  * For conditions of distribution and use, see copyright notice in png.h
  6.  * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson
  7.  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  8.  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  9.  *
  10.  * This file contains routines that an application calls directly to
  11.  * read a PNG file or stream.
  12.  */
  13.  
  14. /* $Id: pngread.c,v 1.5 2001/03/21 16:12:07 tm Exp $ */
  15.  
  16. #define PNG_INTERNAL
  17. #include "png.h"
  18.  
  19. /* Create a PNG structure for reading, and allocate any memory needed. */
  20. png_structp PNGAPI
  21. png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
  22.    png_error_ptr error_fn, png_error_ptr warn_fn)
  23. {
  24.  
  25. #ifdef PNG_USER_MEM_SUPPORTED
  26.    return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
  27.       warn_fn, NULL, NULL, NULL));
  28. }
  29.  
  30. /* Alternate create PNG structure for reading, and allocate any memory needed.*/
  31. png_structp PNGAPI
  32. png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
  33.    png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  34.    png_malloc_ptr malloc_fn, png_free_ptr free_fn)
  35. {
  36. #endif /* PNG_USER_MEM_SUPPORTED */
  37.  
  38.    png_structp png_ptr;
  39.  
  40. #ifdef PNG_SETJMP_SUPPORTED
  41. #ifdef USE_FAR_KEYWORD
  42.    jmp_buf jmpbuf;
  43. #endif
  44. #endif
  45.  
  46.    int i;
  47.  
  48.    png_debug(1, "in png_create_read_struct\n");
  49. #ifdef PNG_USER_MEM_SUPPORTED
  50. # ifdef PNG_PDFLIB_MEM_SUPPORTED /* PDFlib GmbH: */
  51.    if ((png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
  52.       (png_malloc_ptr)malloc_fn, mem_ptr)) == NULL)
  53. # else
  54.    if ((png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
  55.       (png_malloc_ptr)malloc_fn)) == NULL)
  56. # endif
  57. #else
  58.    if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
  59. #endif
  60.    {
  61.       return (png_structp)NULL;
  62.    }
  63.  
  64. #ifdef PNG_SETJMP_SUPPORTED
  65. #ifdef USE_FAR_KEYWORD
  66.    if (setjmp(jmpbuf))
  67. #else
  68.    if (setjmp(png_ptr->jmpbuf))
  69. #endif
  70.    {
  71.       png_free(png_ptr, png_ptr->zbuf);
  72.       png_ptr->zbuf=NULL;
  73.       png_destroy_struct(png_ptr);
  74.       return (png_structp)NULL;
  75.    }
  76. #ifdef USE_FAR_KEYWORD
  77.    png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
  78. #endif
  79. #endif
  80.  
  81. #ifdef PNG_USER_MEM_SUPPORTED
  82.    png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
  83. #endif
  84.  
  85.    png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
  86.  
  87.    i=0;
  88.    do
  89.    {
  90.      if(user_png_ver[i] != png_libpng_ver[i])
  91.         png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  92.    } while (png_libpng_ver[i++]);
  93.  
  94.    if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
  95.    {
  96.      /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
  97.       * we must recompile any applications that use any older library version.
  98.       * For versions after libpng 1.0, we will be compatible, so we need
  99.       * only check the first digit.
  100.       */
  101.      if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
  102.          (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
  103.      {
  104.         png_error(png_ptr,
  105.            "Incompatible libpng version in application and library");
  106.      }
  107.  
  108.      /* Libpng 1.0.6 was not binary compatible, due to insertion of the
  109.         info_ptr->free_me member.  Note to maintainer: this test can be
  110.         removed from version 2.0.0 and beyond because the previous test
  111.         would have already rejected it. */
  112.  
  113.      if (user_png_ver[4] == '6' && user_png_ver[2] == '0' && 
  114.          user_png_ver[0] == '1' && user_png_ver[5] == '\0')
  115.      {
  116.         png_error(png_ptr,
  117.            "Application must be recompiled; version 1.0.6 was incompatible");
  118.      }
  119.    }
  120.  
  121.    /* initialize zbuf - compression buffer */
  122.    png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  123.    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  124.      (png_uint_32)png_ptr->zbuf_size);
  125.    png_ptr->zstream.zalloc = png_zalloc;
  126.    png_ptr->zstream.zfree = png_zfree;
  127.    png_ptr->zstream.opaque = (voidpf)png_ptr;
  128.  
  129.    switch (inflateInit(&png_ptr->zstream))
  130.    {
  131.      case Z_OK: /* Do nothing */ break;
  132.      case Z_MEM_ERROR:
  133.      case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
  134.      case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
  135.      default: png_error(png_ptr, "Unknown zlib error");
  136.    }
  137.  
  138.    png_ptr->zstream.next_out = png_ptr->zbuf;
  139.    png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  140.  
  141.    png_set_read_fn(png_ptr, NULL, NULL);
  142.  
  143.    return (png_ptr);
  144. }
  145.  
  146. /* Initialize PNG structure for reading, and allocate any memory needed.
  147.    This interface is deprecated in favour of the png_create_read_struct(),
  148.    and it will eventually disappear. */
  149. /* PDFlib GmbH: the messy hack below would fool our pdf_ prefixes. */
  150. #if 0
  151. #undef png_read_init
  152. void PNGAPI
  153. png_read_init(png_structp png_ptr)
  154. {
  155.    /* We only come here via pre-1.0.7-compiled applications */
  156.    png_read_init_2(png_ptr, "1.0.0", 10000, 10000);
  157. }
  158. #endif
  159.  
  160. void PNGAPI
  161. png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
  162.    png_size_t png_struct_size, png_size_t png_info_size)
  163. {
  164. #ifdef PNG_SETJMP_SUPPORTED
  165.    jmp_buf tmp_jmp;  /* to save current jump buffer */
  166. #endif
  167.  
  168.    int i=0;
  169.    do
  170.    {
  171.      if(user_png_ver[i] != png_libpng_ver[i])
  172.      {
  173. #ifdef PNG_LEGACY_SUPPORTED
  174.        png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  175. #else
  176.        png_ptr->error_fn=(png_error_ptr)NULL;
  177.        png_error(png_ptr,
  178.         "Application uses deprecated png_read_init() and must be recompiled.");
  179. #endif
  180.      }
  181.    } while (png_libpng_ver[i++]);
  182.  
  183.    if(sizeof(png_struct) > png_struct_size ||
  184.       sizeof(png_info) > png_info_size)
  185.      {
  186.        png_ptr->error_fn=(png_error_ptr)NULL;
  187.        png_error(png_ptr,
  188.   "Application and library have different sized structs. Please recompile.");
  189.      }
  190.  
  191.    png_debug(1, "in png_read_init_2\n");
  192.  
  193. #ifdef PNG_SETJMP_SUPPORTED
  194.    /* save jump buffer and error functions */
  195.    png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
  196. #endif
  197.  
  198.    /* reset all variables to 0 */
  199.    png_memset(png_ptr, 0, sizeof (png_struct));
  200.  
  201. #ifdef PNG_SETJMP_SUPPORTED
  202.    /* restore jump buffer */
  203.    png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
  204. #endif
  205.  
  206.    /* initialize zbuf - compression buffer */
  207.    png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  208.    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  209.      (png_uint_32)png_ptr->zbuf_size);
  210.    png_ptr->zstream.zalloc = png_zalloc;
  211.    png_ptr->zstream.zfree = png_zfree;
  212.    png_ptr->zstream.opaque = (voidpf)png_ptr;
  213.  
  214.    switch (inflateInit(&png_ptr->zstream))
  215.    {
  216.      case Z_OK: /* Do nothing */ break;
  217.      case Z_MEM_ERROR:
  218.      case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
  219.      case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
  220.      default: png_error(png_ptr, "Unknown zlib error");
  221.    }
  222.  
  223.    png_ptr->zstream.next_out = png_ptr->zbuf;
  224.    png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  225.  
  226.    png_set_read_fn(png_ptr, NULL, NULL);
  227. }
  228.  
  229. /* Read the information before the actual image data.  This has been
  230.  * changed in v0.90 to allow reading a file that already has the magic
  231.  * bytes read from the stream.  You can tell libpng how many bytes have
  232.  * been read from the beginning of the stream (up to the maximum of 8)
  233.  * via png_set_sig_bytes(), and we will only check the remaining bytes
  234.  * here.  The application can then have access to the signature bytes we
  235.  * read if it is determined that this isn't a valid PNG file.
  236.  */
  237. void PNGAPI
  238. png_read_info(png_structp png_ptr, png_infop info_ptr)
  239. {
  240.    png_debug(1, "in png_read_info\n");
  241.    /* save jump buffer and error functions */
  242.    /* If we haven't checked all of the PNG signature bytes, do so now. */
  243.    if (png_ptr->sig_bytes < 8)
  244.    {
  245.       png_size_t num_checked = png_ptr->sig_bytes,
  246.                  num_to_check = 8 - num_checked;
  247.  
  248.       png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
  249.       png_ptr->sig_bytes = 8;
  250.  
  251.       if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
  252.       {
  253.          if (num_checked < 4 &&
  254.              png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
  255.             png_error(png_ptr, "Not a PNG file");
  256.          else
  257.             png_error(png_ptr, "PNG file corrupted by ASCII conversion");
  258.       }
  259.    }
  260.  
  261.    for(;;)
  262.    {
  263. #ifdef PNG_USE_LOCAL_ARRAYS
  264.       PNG_IHDR;
  265.       PNG_IDAT;
  266.       PNG_IEND;
  267.       PNG_PLTE;
  268. #if defined(PNG_READ_bKGD_SUPPORTED)
  269.       PNG_bKGD;
  270. #endif
  271. #if defined(PNG_READ_cHRM_SUPPORTED)
  272.       PNG_cHRM;
  273. #endif
  274. #if defined(PNG_READ_gAMA_SUPPORTED)
  275.       PNG_gAMA;
  276. #endif
  277. #if defined(PNG_READ_hIST_SUPPORTED)
  278.       PNG_hIST;
  279. #endif
  280. #if defined(PNG_READ_iCCP_SUPPORTED)
  281.       PNG_iCCP;
  282. #endif
  283. #if defined(PNG_READ_iTXt_SUPPORTED)
  284.       PNG_iTXt;
  285. #endif
  286. #if defined(PNG_READ_oFFs_SUPPORTED)
  287.       PNG_oFFs;
  288. #endif
  289. #if defined(PNG_READ_pCAL_SUPPORTED)
  290.       PNG_pCAL;
  291. #endif
  292. #if defined(PNG_READ_pHYs_SUPPORTED)
  293.       PNG_pHYs;
  294. #endif
  295. #if defined(PNG_READ_sBIT_SUPPORTED)
  296.       PNG_sBIT;
  297. #endif
  298. #if defined(PNG_READ_sCAL_SUPPORTED)
  299.       PNG_sCAL;
  300. #endif
  301. #if defined(PNG_READ_sPLT_SUPPORTED)
  302.       PNG_sPLT;
  303. #endif
  304. #if defined(PNG_READ_sRGB_SUPPORTED)
  305.       PNG_sRGB;
  306. #endif
  307. #if defined(PNG_READ_tEXt_SUPPORTED)
  308.       PNG_tEXt;
  309. #endif
  310. #if defined(PNG_READ_tIME_SUPPORTED)
  311.       PNG_tIME;
  312. #endif
  313. #if defined(PNG_READ_tRNS_SUPPORTED)
  314.       PNG_tRNS;
  315. #endif
  316. #if defined(PNG_READ_zTXt_SUPPORTED)
  317.       PNG_zTXt;
  318. #endif
  319. #endif /* PNG_GLOBAL_ARRAYS */
  320.       png_byte chunk_length[4];
  321.       png_uint_32 length;
  322.  
  323.       png_read_data(png_ptr, chunk_length, 4);
  324.       length = png_get_uint_32(chunk_length);
  325.  
  326.       png_reset_crc(png_ptr);
  327.       png_crc_read(png_ptr, png_ptr->chunk_name, 4);
  328.  
  329.       png_debug2(0, "Reading %s chunk, length=%lu.\n", png_ptr->chunk_name,
  330.          length);
  331.  
  332.       /* This should be a binary subdivision search or a hash for
  333.        * matching the chunk name rather than a linear search.
  334.        */
  335.       if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
  336.          png_handle_IHDR(png_ptr, info_ptr, length);
  337.       else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
  338.          png_handle_IEND(png_ptr, info_ptr, length);
  339. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  340.       else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
  341.       {
  342.          if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  343.             png_ptr->mode |= PNG_HAVE_IDAT;
  344.          png_handle_unknown(png_ptr, info_ptr, length);
  345.          if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
  346.             png_ptr->mode |= PNG_HAVE_PLTE;
  347.          else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  348.          {
  349.             if (!(png_ptr->mode & PNG_HAVE_IHDR))
  350.                png_error(png_ptr, "Missing IHDR before IDAT");
  351.             else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  352.                      !(png_ptr->mode & PNG_HAVE_PLTE))
  353.                png_error(png_ptr, "Missing PLTE before IDAT");
  354.             break;
  355.          }
  356.       }
  357. #endif
  358.       else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
  359.          png_handle_PLTE(png_ptr, info_ptr, length);
  360.       else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  361.       {
  362.          if (!(png_ptr->mode & PNG_HAVE_IHDR))
  363.             png_error(png_ptr, "Missing IHDR before IDAT");
  364.          else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  365.                   !(png_ptr->mode & PNG_HAVE_PLTE))
  366.             png_error(png_ptr, "Missing PLTE before IDAT");
  367.  
  368.          png_ptr->idat_size = length;
  369.          png_ptr->mode |= PNG_HAVE_IDAT;
  370.          break;
  371.       }
  372. #if defined(PNG_READ_bKGD_SUPPORTED)
  373.       else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
  374.          png_handle_bKGD(png_ptr, info_ptr, length);
  375. #endif
  376. #if defined(PNG_READ_cHRM_SUPPORTED)
  377.       else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
  378.          png_handle_cHRM(png_ptr, info_ptr, length);
  379. #endif
  380. #if defined(PNG_READ_gAMA_SUPPORTED)
  381.       else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
  382.          png_handle_gAMA(png_ptr, info_ptr, length);
  383. #endif
  384. #if defined(PNG_READ_hIST_SUPPORTED)
  385.       else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
  386.          png_handle_hIST(png_ptr, info_ptr, length);
  387. #endif
  388. #if defined(PNG_READ_oFFs_SUPPORTED)
  389.       else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
  390.          png_handle_oFFs(png_ptr, info_ptr, length);
  391. #endif
  392. #if defined(PNG_READ_pCAL_SUPPORTED)
  393.       else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
  394.          png_handle_pCAL(png_ptr, info_ptr, length);
  395. #endif
  396. #if defined(PNG_READ_sCAL_SUPPORTED)
  397.       else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
  398.          png_handle_sCAL(png_ptr, info_ptr, length);
  399. #endif
  400. #if defined(PNG_READ_pHYs_SUPPORTED)
  401.       else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
  402.          png_handle_pHYs(png_ptr, info_ptr, length);
  403. #endif
  404. #if defined(PNG_READ_sBIT_SUPPORTED)
  405.       else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
  406.          png_handle_sBIT(png_ptr, info_ptr, length);
  407. #endif
  408. #if defined(PNG_READ_sRGB_SUPPORTED)
  409.       else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
  410.          png_handle_sRGB(png_ptr, info_ptr, length);
  411. #endif
  412. #if defined(PNG_READ_iCCP_SUPPORTED)
  413.       else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
  414.          png_handle_iCCP(png_ptr, info_ptr, length);
  415. #endif
  416. #if defined(PNG_READ_sPLT_SUPPORTED)
  417.       else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
  418.          png_handle_sPLT(png_ptr, info_ptr, length);
  419. #endif
  420. #if defined(PNG_READ_tEXt_SUPPORTED)
  421.       else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
  422.          png_handle_tEXt(png_ptr, info_ptr, length);
  423. #endif
  424. #if defined(PNG_READ_tIME_SUPPORTED)
  425.       else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
  426.          png_handle_tIME(png_ptr, info_ptr, length);
  427. #endif
  428. #if defined(PNG_READ_tRNS_SUPPORTED)
  429.       else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
  430.          png_handle_tRNS(png_ptr, info_ptr, length);
  431. #endif
  432. #if defined(PNG_READ_zTXt_SUPPORTED)
  433.       else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
  434.          png_handle_zTXt(png_ptr, info_ptr, length);
  435. #endif
  436. #if defined(PNG_READ_iTXt_SUPPORTED)
  437.       else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
  438.          png_handle_iTXt(png_ptr, info_ptr, length);
  439. #endif
  440.       else
  441.          png_handle_unknown(png_ptr, info_ptr, length);
  442.    }
  443. }
  444.  
  445. /* optional call to update the users info_ptr structure */
  446. void PNGAPI
  447. png_read_update_info(png_structp png_ptr, png_infop info_ptr)
  448. {
  449.    png_debug(1, "in png_read_update_info\n");
  450.    /* save jump buffer and error functions */
  451.    if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  452.       png_read_start_row(png_ptr);
  453.    png_read_transform_info(png_ptr, info_ptr);
  454. }
  455.  
  456. /* Initialize palette, background, etc, after transformations
  457.  * are set, but before any reading takes place.  This allows
  458.  * the user to obtain a gamma-corrected palette, for example.
  459.  * If the user doesn't call this, we will do it ourselves.
  460.  */
  461. void PNGAPI
  462. png_start_read_image(png_structp png_ptr)
  463. {
  464.    png_debug(1, "in png_start_read_image\n");
  465.    /* save jump buffer and error functions */
  466.    if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  467.       png_read_start_row(png_ptr);
  468. }
  469.  
  470. void PNGAPI
  471. png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
  472. {
  473. #ifdef PNG_USE_LOCAL_ARRAYS
  474.    PNG_IDAT;
  475.    const int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
  476.    const int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  477. #endif
  478.    int ret;
  479.    png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
  480.       png_ptr->row_number, png_ptr->pass);
  481.    /* save jump buffer and error functions */
  482.    if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  483.       png_read_start_row(png_ptr);
  484.    if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  485.    {
  486.    /* check for transforms that have been set but were defined out */
  487. #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
  488.    if (png_ptr->transformations & PNG_INVERT_MONO)
  489.       png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
  490. #endif
  491. #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
  492.    if (png_ptr->transformations & PNG_FILLER)
  493.       png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
  494. #endif
  495. #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
  496.     !defined(PNG_READ_PACKSWAP_SUPPORTED)
  497.    if (png_ptr->transformations & PNG_PACKSWAP)
  498.       png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
  499. #endif
  500. #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
  501.    if (png_ptr->transformations & PNG_PACK)
  502.       png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
  503. #endif
  504. #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
  505.    if (png_ptr->transformations & PNG_SHIFT)
  506.       png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
  507. #endif
  508. #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
  509.    if (png_ptr->transformations & PNG_BGR)
  510.       png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
  511. #endif
  512. #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
  513.    if (png_ptr->transformations & PNG_SWAP_BYTES)
  514.       png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
  515. #endif
  516.    }
  517.  
  518. #if defined(PNG_READ_INTERLACING_SUPPORTED)
  519.    /* if interlaced and we do not need a new row, combine row and return */
  520.    if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  521.    {
  522.       switch (png_ptr->pass)
  523.       {
  524.          case 0:
  525.             if (png_ptr->row_number & 0x07)
  526.             {
  527.                if (dsp_row != NULL)
  528.                   png_combine_row(png_ptr, dsp_row,
  529.                      png_pass_dsp_mask[png_ptr->pass]);
  530.                png_read_finish_row(png_ptr);
  531.                return;
  532.             }
  533.             break;
  534.          case 1:
  535.             if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  536.             {
  537.                if (dsp_row != NULL)
  538.                   png_combine_row(png_ptr, dsp_row,
  539.                      png_pass_dsp_mask[png_ptr->pass]);
  540.                png_read_finish_row(png_ptr);
  541.                return;
  542.             }
  543.             break;
  544.          case 2:
  545.             if ((png_ptr->row_number & 0x07) != 4)
  546.             {
  547.                if (dsp_row != NULL && (png_ptr->row_number & 4))
  548.                   png_combine_row(png_ptr, dsp_row,
  549.                      png_pass_dsp_mask[png_ptr->pass]);
  550.                png_read_finish_row(png_ptr);
  551.                return;
  552.             }
  553.             break;
  554.          case 3:
  555.             if ((png_ptr->row_number & 3) || png_ptr->width < 3)
  556.             {
  557.                if (dsp_row != NULL)
  558.                   png_combine_row(png_ptr, dsp_row,
  559.                      png_pass_dsp_mask[png_ptr->pass]);
  560.                png_read_finish_row(png_ptr);
  561.                return;
  562.             }
  563.             break;
  564.          case 4:
  565.             if ((png_ptr->row_number & 3) != 2)
  566.             {
  567.                if (dsp_row != NULL && (png_ptr->row_number & 2))
  568.                   png_combine_row(png_ptr, dsp_row,
  569.                      png_pass_dsp_mask[png_ptr->pass]);
  570.                png_read_finish_row(png_ptr);
  571.                return;
  572.             }
  573.             break;
  574.          case 5:
  575.             if ((png_ptr->row_number & 1) || png_ptr->width < 2)
  576.             {
  577.                if (dsp_row != NULL)
  578.                   png_combine_row(png_ptr, dsp_row,
  579.                      png_pass_dsp_mask[png_ptr->pass]);
  580.                png_read_finish_row(png_ptr);
  581.                return;
  582.             }
  583.             break;
  584.          case 6:
  585.             if (!(png_ptr->row_number & 1))
  586.             {
  587.                png_read_finish_row(png_ptr);
  588.                return;
  589.             }
  590.             break;
  591.       }
  592.    }
  593. #endif
  594.  
  595.    if (!(png_ptr->mode & PNG_HAVE_IDAT))
  596.       png_error(png_ptr, "Invalid attempt to read row data");
  597.  
  598.    png_ptr->zstream.next_out = png_ptr->row_buf;
  599.    png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
  600.    do
  601.    {
  602.       if (!(png_ptr->zstream.avail_in))
  603.       {
  604.          while (!png_ptr->idat_size)
  605.          {
  606.             png_byte chunk_length[4];
  607.  
  608.             png_crc_finish(png_ptr, 0);
  609.  
  610.             png_read_data(png_ptr, chunk_length, 4);
  611.             png_ptr->idat_size = png_get_uint_32(chunk_length);
  612.  
  613.             png_reset_crc(png_ptr);
  614.             png_crc_read(png_ptr, png_ptr->chunk_name, 4);
  615.             if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  616.                png_error(png_ptr, "Not enough image data");
  617.          }
  618.          png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
  619.          png_ptr->zstream.next_in = png_ptr->zbuf;
  620.          if (png_ptr->zbuf_size > png_ptr->idat_size)
  621.             png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
  622.          png_crc_read(png_ptr, png_ptr->zbuf,
  623.             (png_size_t)png_ptr->zstream.avail_in);
  624.          png_ptr->idat_size -= png_ptr->zstream.avail_in;
  625.       }
  626.       ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
  627.       if (ret == Z_STREAM_END)
  628.       {
  629.          if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
  630.             png_ptr->idat_size)
  631.             png_error(png_ptr, "Extra compressed data");
  632.          png_ptr->mode |= PNG_AFTER_IDAT;
  633.          png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
  634.          break;
  635.       }
  636.       if (ret != Z_OK)
  637.          png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
  638.                    "Decompression error");
  639.  
  640.    } while (png_ptr->zstream.avail_out);
  641.  
  642.    png_ptr->row_info.color_type = png_ptr->color_type;
  643.    png_ptr->row_info.width = png_ptr->iwidth;
  644.    png_ptr->row_info.channels = png_ptr->channels;
  645.    png_ptr->row_info.bit_depth = png_ptr->bit_depth;
  646.    png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
  647.    png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
  648.       (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
  649.  
  650.    if(png_ptr->row_buf[0])
  651.    png_read_filter_row(png_ptr, &(png_ptr->row_info),
  652.       png_ptr->row_buf + 1, png_ptr->prev_row + 1,
  653.       (int)(png_ptr->row_buf[0]));
  654.  
  655.    png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
  656.       png_ptr->rowbytes + 1);
  657.  
  658.    if (png_ptr->transformations)
  659.       png_do_read_transformations(png_ptr);
  660.  
  661. #if defined(PNG_READ_INTERLACING_SUPPORTED)
  662.    /* blow up interlaced rows to full size */
  663.    if (png_ptr->interlaced &&
  664.       (png_ptr->transformations & PNG_INTERLACE))
  665.    {
  666.       if (png_ptr->pass < 6)
  667.          png_do_read_interlace(&(png_ptr->row_info),
  668.             png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
  669.  
  670.       if (dsp_row != NULL)
  671.          png_combine_row(png_ptr, dsp_row,
  672.             png_pass_dsp_mask[png_ptr->pass]);
  673.       if (row != NULL)
  674.          png_combine_row(png_ptr, row,
  675.             png_pass_mask[png_ptr->pass]);
  676.    }
  677.    else
  678. #endif
  679.    {
  680.       if (row != NULL)
  681.          png_combine_row(png_ptr, row, 0xff);
  682.       if (dsp_row != NULL)
  683.          png_combine_row(png_ptr, dsp_row, 0xff);
  684.    }
  685.    png_read_finish_row(png_ptr);
  686.  
  687.    if (png_ptr->read_row_fn != NULL)
  688.       (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  689. }
  690.  
  691. /* Read one or more rows of image data.  If the image is interlaced,
  692.  * and png_set_interlace_handling() has been called, the rows need to
  693.  * contain the contents of the rows from the previous pass.  If the
  694.  * image has alpha or transparency, and png_handle_alpha()[*] has been
  695.  * called, the rows contents must be initialized to the contents of the
  696.  * screen.
  697.  *
  698.  * "row" holds the actual image, and pixels are placed in it
  699.  * as they arrive.  If the image is displayed after each pass, it will
  700.  * appear to "sparkle" in.  "display_row" can be used to display a
  701.  * "chunky" progressive image, with finer detail added as it becomes
  702.  * available.  If you do not want this "chunky" display, you may pass
  703.  * NULL for display_row.  If you do not want the sparkle display, and
  704.  * you have not called png_handle_alpha(), you may pass NULL for rows.
  705.  * If you have called png_handle_alpha(), and the image has either an
  706.  * alpha channel or a transparency chunk, you must provide a buffer for
  707.  * rows.  In this case, you do not have to provide a display_row buffer
  708.  * also, but you may.  If the image is not interlaced, or if you have
  709.  * not called png_set_interlace_handling(), the display_row buffer will
  710.  * be ignored, so pass NULL to it.
  711.  *
  712.  * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.8
  713.  */
  714.  
  715. void PNGAPI
  716. png_read_rows(png_structp png_ptr, png_bytepp row,
  717.    png_bytepp display_row, png_uint_32 num_rows)
  718. {
  719.    png_uint_32 i;
  720.    png_bytepp rp;
  721.    png_bytepp dp;
  722.  
  723.    png_debug(1, "in png_read_rows\n");
  724.    /* save jump buffer and error functions */
  725.    rp = row;
  726.    dp = display_row;
  727.    if (rp != NULL && dp != NULL)
  728.       for (i = 0; i < num_rows; i++)
  729.       {
  730.          png_bytep rptr = *rp++;
  731.          png_bytep dptr = *dp++;
  732.  
  733.          png_read_row(png_ptr, rptr, dptr);
  734.       }
  735.    else if(rp != NULL)
  736.       for (i = 0; i < num_rows; i++)
  737.       {
  738.          png_bytep rptr = *rp;
  739.          png_read_row(png_ptr, rptr, NULL);
  740.          rp++;
  741.       }
  742.    else if(dp != NULL)
  743.       for (i = 0; i < num_rows; i++)
  744.       {
  745.          png_bytep dptr = *dp;
  746.          png_read_row(png_ptr, NULL, dptr);
  747.          dp++;
  748.       }
  749. }
  750.  
  751. /* Read the entire image.  If the image has an alpha channel or a tRNS
  752.  * chunk, and you have called png_handle_alpha()[*], you will need to
  753.  * initialize the image to the current image that PNG will be overlaying.
  754.  * We set the num_rows again here, in case it was incorrectly set in
  755.  * png_read_start_row() by a call to png_read_update_info() or
  756.  * png_start_read_image() if png_set_interlace_handling() wasn't called
  757.  * prior to either of these functions like it should have been.  You can
  758.  * only call this function once.  If you desire to have an image for
  759.  * each pass of a interlaced image, use png_read_rows() instead.
  760.  *
  761.  * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.8
  762.  */
  763. void PNGAPI
  764. png_read_image(png_structp png_ptr, png_bytepp image)
  765. {
  766.    png_uint_32 i,image_height;
  767.    int pass, j;
  768.    png_bytepp rp;
  769.  
  770.    png_debug(1, "in png_read_image\n");
  771.    /* save jump buffer and error functions */
  772.  
  773. #ifdef PNG_READ_INTERLACING_SUPPORTED
  774.    pass = png_set_interlace_handling(png_ptr);
  775. #else
  776.    if (png_ptr->interlaced)
  777.       png_error(png_ptr,
  778.         "Cannot read interlaced image -- interlace handler disabled.");
  779.    pass = 1;
  780. #endif
  781.  
  782.  
  783.    image_height=png_ptr->height;
  784.    png_ptr->num_rows = image_height; /* Make sure this is set correctly */
  785.  
  786.    for (j = 0; j < pass; j++)
  787.    {
  788.       rp = image;
  789.       for (i = 0; i < image_height; i++)
  790.       {
  791.          png_read_row(png_ptr, *rp, NULL);
  792.          rp++;
  793.       }
  794.    }
  795. }
  796.  
  797. /* Read the end of the PNG file.  Will not read past the end of the
  798.  * file, will verify the end is accurate, and will read any comments
  799.  * or time information at the end of the file, if info is not NULL.
  800.  */
  801. void PNGAPI
  802. png_read_end(png_structp png_ptr, png_infop info_ptr)
  803. {
  804.    png_byte chunk_length[4];
  805.    png_uint_32 length;
  806.  
  807.    png_debug(1, "in png_read_end\n");
  808.    /* save jump buffer and error functions */
  809.    png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
  810.  
  811.    do
  812.    {
  813. #ifdef PNG_USE_LOCAL_ARRAYS
  814.       PNG_IHDR;
  815.       PNG_IDAT;
  816.       PNG_IEND;
  817.       PNG_PLTE;
  818. #if defined(PNG_READ_bKGD_SUPPORTED)
  819.       PNG_bKGD;
  820. #endif
  821. #if defined(PNG_READ_cHRM_SUPPORTED)
  822.       PNG_cHRM;
  823. #endif
  824. #if defined(PNG_READ_gAMA_SUPPORTED)
  825.       PNG_gAMA;
  826. #endif
  827. #if defined(PNG_READ_hIST_SUPPORTED)
  828.       PNG_hIST;
  829. #endif
  830. #if defined(PNG_READ_iCCP_SUPPORTED)
  831.       PNG_iCCP;
  832. #endif
  833. #if defined(PNG_READ_iTXt_SUPPORTED)
  834.       PNG_iTXt;
  835. #endif
  836. #if defined(PNG_READ_oFFs_SUPPORTED)
  837.       PNG_oFFs;
  838. #endif
  839. #if defined(PNG_READ_pCAL_SUPPORTED)
  840.       PNG_pCAL;
  841. #endif
  842. #if defined(PNG_READ_pHYs_SUPPORTED)
  843.       PNG_pHYs;
  844. #endif
  845. #if defined(PNG_READ_sBIT_SUPPORTED)
  846.       PNG_sBIT;
  847. #endif
  848. #if defined(PNG_READ_sCAL_SUPPORTED)
  849.       PNG_sCAL;
  850. #endif
  851. #if defined(PNG_READ_sPLT_SUPPORTED)
  852.       PNG_sPLT;
  853. #endif
  854. #if defined(PNG_READ_sRGB_SUPPORTED)
  855.       PNG_sRGB;
  856. #endif
  857. #if defined(PNG_READ_tEXt_SUPPORTED)
  858.       PNG_tEXt;
  859. #endif
  860. #if defined(PNG_READ_tIME_SUPPORTED)
  861.       PNG_tIME;
  862. #endif
  863. #if defined(PNG_READ_tRNS_SUPPORTED)
  864.       PNG_tRNS;
  865. #endif
  866. #if defined(PNG_READ_zTXt_SUPPORTED)
  867.       PNG_zTXt;
  868. #endif
  869. #endif /* PNG_GLOBAL_ARRAYS */
  870.  
  871.       png_read_data(png_ptr, chunk_length, 4);
  872.       length = png_get_uint_32(chunk_length);
  873.  
  874.       png_reset_crc(png_ptr);
  875.       png_crc_read(png_ptr, png_ptr->chunk_name, 4);
  876.  
  877.       png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
  878.  
  879.       if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
  880.          png_handle_IHDR(png_ptr, info_ptr, length);
  881.       else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
  882.          png_handle_IEND(png_ptr, info_ptr, length);
  883. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  884.       else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
  885.       {
  886.          if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  887.          {
  888.             if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
  889.                png_error(png_ptr, "Too many IDAT's found");
  890.          }
  891.          else
  892.             png_ptr->mode |= PNG_AFTER_IDAT;
  893.          png_handle_unknown(png_ptr, info_ptr, length);
  894.          if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
  895.             png_ptr->mode |= PNG_HAVE_PLTE;
  896.       }
  897. #endif
  898.       else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
  899.       {
  900.          /* Zero length IDATs are legal after the last IDAT has been
  901.           * read, but not after other chunks have been read.
  902.           */
  903.          if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
  904.             png_error(png_ptr, "Too many IDAT's found");
  905.          png_crc_finish(png_ptr, length);
  906.       }
  907.       else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
  908.          png_handle_PLTE(png_ptr, info_ptr, length);
  909. #if defined(PNG_READ_bKGD_SUPPORTED)
  910.       else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
  911.          png_handle_bKGD(png_ptr, info_ptr, length);
  912. #endif
  913. #if defined(PNG_READ_cHRM_SUPPORTED)
  914.       else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
  915.          png_handle_cHRM(png_ptr, info_ptr, length);
  916. #endif
  917. #if defined(PNG_READ_gAMA_SUPPORTED)
  918.       else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
  919.          png_handle_gAMA(png_ptr, info_ptr, length);
  920. #endif
  921. #if defined(PNG_READ_hIST_SUPPORTED)
  922.       else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
  923.          png_handle_hIST(png_ptr, info_ptr, length);
  924. #endif
  925. #if defined(PNG_READ_oFFs_SUPPORTED)
  926.       else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
  927.          png_handle_oFFs(png_ptr, info_ptr, length);
  928. #endif
  929. #if defined(PNG_READ_pCAL_SUPPORTED)
  930.       else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
  931.          png_handle_pCAL(png_ptr, info_ptr, length);
  932. #endif
  933. #if defined(PNG_READ_sCAL_SUPPORTED)
  934.       else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
  935.          png_handle_sCAL(png_ptr, info_ptr, length);
  936. #endif
  937. #if defined(PNG_READ_pHYs_SUPPORTED)
  938.       else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
  939.          png_handle_pHYs(png_ptr, info_ptr, length);
  940. #endif
  941. #if defined(PNG_READ_sBIT_SUPPORTED)
  942.       else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
  943.          png_handle_sBIT(png_ptr, info_ptr, length);
  944. #endif
  945. #if defined(PNG_READ_sRGB_SUPPORTED)
  946.       else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
  947.          png_handle_sRGB(png_ptr, info_ptr, length);
  948. #endif
  949. #if defined(PNG_READ_iCCP_SUPPORTED)
  950.       else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
  951.          png_handle_iCCP(png_ptr, info_ptr, length);
  952. #endif
  953. #if defined(PNG_READ_sPLT_SUPPORTED)
  954.       else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
  955.          png_handle_sPLT(png_ptr, info_ptr, length);
  956. #endif
  957. #if defined(PNG_READ_tEXt_SUPPORTED)
  958.       else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
  959.          png_handle_tEXt(png_ptr, info_ptr, length);
  960. #endif
  961. #if defined(PNG_READ_tIME_SUPPORTED)
  962.       else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
  963.          png_handle_tIME(png_ptr, info_ptr, length);
  964. #endif
  965. #if defined(PNG_READ_tRNS_SUPPORTED)
  966.       else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
  967.          png_handle_tRNS(png_ptr, info_ptr, length);
  968. #endif
  969. #if defined(PNG_READ_zTXt_SUPPORTED)
  970.       else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
  971.          png_handle_zTXt(png_ptr, info_ptr, length);
  972. #endif
  973. #if defined(PNG_READ_iTXt_SUPPORTED)
  974.       else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
  975.          png_handle_iTXt(png_ptr, info_ptr, length);
  976. #endif
  977.       else
  978.          png_handle_unknown(png_ptr, info_ptr, length);
  979.    } while (!(png_ptr->mode & PNG_HAVE_IEND));
  980. }
  981.  
  982. /* free all memory used by the read */
  983. void PNGAPI
  984. png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
  985.    png_infopp end_info_ptr_ptr)
  986. {
  987.    png_structp png_ptr = NULL;
  988.    png_infop info_ptr = NULL, end_info_ptr = NULL;
  989. #ifdef PNG_USER_MEM_SUPPORTED
  990.    png_free_ptr free_fn = NULL;
  991. # ifdef PNG_PDFLIB_MEM_SUPPORTED /* PDFlib GmbH: */
  992.    png_voidp mem_ptr = NULL;
  993. # endif
  994. #endif
  995.  
  996.    png_debug(1, "in png_destroy_read_struct\n");
  997.    /* save jump buffer and error functions */
  998.    if (png_ptr_ptr != NULL)
  999.       png_ptr = *png_ptr_ptr;
  1000.  
  1001.    if (info_ptr_ptr != NULL)
  1002.       info_ptr = *info_ptr_ptr;
  1003.  
  1004.    if (end_info_ptr_ptr != NULL)
  1005.       end_info_ptr = *end_info_ptr_ptr;
  1006.  
  1007. #ifdef PNG_USER_MEM_SUPPORTED
  1008.    free_fn = png_ptr->free_fn;
  1009. # ifdef PNG_PDFLIB_MEM_SUPPORTED /* PDFlib GmbH: */
  1010.    mem_ptr = png_ptr->mem_ptr;
  1011. # endif
  1012. #endif
  1013.  
  1014.    png_read_destroy(png_ptr, info_ptr, end_info_ptr);
  1015.  
  1016.    if (info_ptr != NULL)
  1017.    {
  1018. #if defined(PNG_TEXT_SUPPORTED)
  1019.       png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
  1020. #endif
  1021.  
  1022. #ifdef PNG_USER_MEM_SUPPORTED
  1023. # ifdef PNG_PDFLIB_MEM_SUPPORTED /* PDFlib GmbH: */
  1024.       png_destroy_struct_2((png_voidp)info_ptr, free_fn, mem_ptr);
  1025. # else
  1026.       png_destroy_struct_2((png_voidp)info_ptr, free_fn);
  1027. # endif
  1028. #else
  1029.       png_destroy_struct((png_voidp)info_ptr);
  1030. #endif
  1031.       *info_ptr_ptr = (png_infop)NULL;
  1032.    }
  1033.  
  1034.    if (end_info_ptr != NULL)
  1035.    {
  1036. #if defined(PNG_READ_TEXT_SUPPORTED)
  1037.       png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
  1038. #endif
  1039. #ifdef PNG_USER_MEM_SUPPORTED
  1040. # ifdef PNG_PDFLIB_MEM_SUPPORTED /* PDFlib GmbH: */
  1041.       png_destroy_struct_2((png_voidp)end_info_ptr, free_fn, mem_ptr);
  1042. # else
  1043.       png_destroy_struct_2((png_voidp)end_info_ptr, free_fn);
  1044. # endif
  1045. #else
  1046.       png_destroy_struct((png_voidp)end_info_ptr);
  1047. #endif
  1048.       *end_info_ptr_ptr = (png_infop)NULL;
  1049.    }
  1050.  
  1051.    if (png_ptr != NULL)
  1052.    {
  1053. #ifdef PNG_USER_MEM_SUPPORTED
  1054. # ifdef PNG_PDFLIB_MEM_SUPPORTED /* PDFlib GmbH: */
  1055.       png_destroy_struct_2((png_voidp)png_ptr, free_fn, mem_ptr);
  1056. # else
  1057.       png_destroy_struct_2((png_voidp)png_ptr, free_fn);
  1058. # endif
  1059. #else
  1060.       png_destroy_struct((png_voidp)png_ptr);
  1061. #endif
  1062.       *png_ptr_ptr = (png_structp)NULL;
  1063.    }
  1064. }
  1065.  
  1066. /* free all memory used by the read (old method) */
  1067. void PNGAPI
  1068. png_read_destroy(png_structp png_ptr, png_infop info_ptr,png_infop end_info_ptr)
  1069. {
  1070. #ifdef PNG_SETJMP_SUPPORTED
  1071.    jmp_buf tmp_jmp;
  1072. #endif
  1073.    png_error_ptr error_fn;
  1074.    png_error_ptr warning_fn;
  1075.    png_voidp error_ptr;
  1076. #ifdef PNG_USER_MEM_SUPPORTED
  1077.    png_free_ptr free_fn;
  1078. # ifdef PNG_PDFLIB_MEM_SUPPORTED /* PDFlib GmbH: */
  1079.    png_voidp mem_ptr = NULL;
  1080. # endif
  1081. #endif
  1082.  
  1083.    png_debug(1, "in png_read_destroy\n");
  1084.    /* save jump buffer and error functions */
  1085.    if (info_ptr != NULL)
  1086.       png_info_destroy(png_ptr, info_ptr);
  1087.  
  1088.    if (end_info_ptr != NULL)
  1089.       png_info_destroy(png_ptr, end_info_ptr);
  1090.  
  1091.    png_free(png_ptr, png_ptr->zbuf);
  1092.    png_free(png_ptr, png_ptr->row_buf);
  1093.    png_free(png_ptr, png_ptr->prev_row);
  1094. #if defined(PNG_READ_DITHER_SUPPORTED)
  1095.    png_free(png_ptr, png_ptr->palette_lookup);
  1096.    png_free(png_ptr, png_ptr->dither_index);
  1097. #endif
  1098. #if defined(PNG_READ_GAMMA_SUPPORTED)
  1099.    png_free(png_ptr, png_ptr->gamma_table);
  1100. #endif
  1101. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  1102.    png_free(png_ptr, png_ptr->gamma_from_1);
  1103.    png_free(png_ptr, png_ptr->gamma_to_1);
  1104. #endif
  1105. #ifdef PNG_FREE_ME_SUPPORTED
  1106.    if (png_ptr->free_me & PNG_FREE_PLTE)
  1107.       png_zfree(png_ptr, png_ptr->palette);
  1108.    png_ptr->free_me &= ~PNG_FREE_PLTE;
  1109. #else
  1110.    if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
  1111.       png_zfree(png_ptr, png_ptr->palette);
  1112.    png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
  1113. #endif
  1114. #if defined(PNG_tRNS_SUPPORTED) || \
  1115.     defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  1116. #ifdef PNG_FREE_ME_SUPPORTED
  1117.    if (png_ptr->free_me & PNG_FREE_TRNS)
  1118.       png_free(png_ptr, png_ptr->trans);
  1119.    png_ptr->free_me &= ~PNG_FREE_TRNS;
  1120. #else
  1121.    if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
  1122.       png_free(png_ptr, png_ptr->trans);
  1123.    png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
  1124. #endif
  1125. #endif
  1126. #if defined(PNG_READ_hIST_SUPPORTED)
  1127. #ifdef PNG_FREE_ME_SUPPORTED
  1128.    if (png_ptr->free_me & PNG_FREE_HIST)
  1129.       png_free(png_ptr, png_ptr->hist);
  1130.    png_ptr->free_me &= ~PNG_FREE_HIST;
  1131. #else
  1132.    if (png_ptr->flags & PNG_FLAG_FREE_HIST)
  1133.       png_free(png_ptr, png_ptr->hist);
  1134.    png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
  1135. #endif
  1136. #endif
  1137. #if defined(PNG_READ_GAMMA_SUPPORTED)
  1138.    if (png_ptr->gamma_16_table != NULL)
  1139.    {
  1140.       int i;
  1141.       int istop = (1 << (8 - png_ptr->gamma_shift));
  1142.       for (i = 0; i < istop; i++)
  1143.       {
  1144.          png_free(png_ptr, png_ptr->gamma_16_table[i]);
  1145.       }
  1146.    png_free(png_ptr, png_ptr->gamma_16_table);
  1147.    }
  1148. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  1149.    if (png_ptr->gamma_16_from_1 != NULL)
  1150.    {
  1151.       int i;
  1152.       int istop = (1 << (8 - png_ptr->gamma_shift));
  1153.       for (i = 0; i < istop; i++)
  1154.       {
  1155.          png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
  1156.       }
  1157.    png_free(png_ptr, png_ptr->gamma_16_from_1);
  1158.    }
  1159.    if (png_ptr->gamma_16_to_1 != NULL)
  1160.    {
  1161.       int i;
  1162.       int istop = (1 << (8 - png_ptr->gamma_shift));
  1163.       for (i = 0; i < istop; i++)
  1164.       {
  1165.          png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
  1166.       }
  1167.    png_free(png_ptr, png_ptr->gamma_16_to_1);
  1168.    }
  1169. #endif
  1170. #endif
  1171. #if defined(PNG_TIME_RFC1123_SUPPORTED)
  1172.    png_free(png_ptr, png_ptr->time_buffer);
  1173. #endif
  1174.  
  1175.    inflateEnd(&png_ptr->zstream);
  1176. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  1177.    png_free(png_ptr, png_ptr->save_buffer);
  1178. #endif
  1179.  
  1180.    /* Save the important info out of the png_struct, in case it is
  1181.     * being used again.
  1182.     */
  1183. #ifdef PNG_SETJMP_SUPPORTED
  1184.    png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
  1185. #endif
  1186.  
  1187.    error_fn = png_ptr->error_fn;
  1188.    warning_fn = png_ptr->warning_fn;
  1189.    error_ptr = png_ptr->error_ptr;
  1190. #ifdef PNG_USER_MEM_SUPPORTED
  1191.    free_fn = png_ptr->free_fn;
  1192. # ifdef PNG_PDFLIB_MEM_SUPPORTED /* PDFlib GmbH: */
  1193.    mem_ptr = png_ptr->mem_ptr;
  1194. # endif
  1195. #endif
  1196.  
  1197.    png_memset(png_ptr, 0, sizeof (png_struct));
  1198.  
  1199.    png_ptr->error_fn = error_fn;
  1200.    png_ptr->warning_fn = warning_fn;
  1201.    png_ptr->error_ptr = error_ptr;
  1202. #ifdef PNG_USER_MEM_SUPPORTED
  1203.    png_ptr->free_fn = free_fn;
  1204. # ifdef PNG_PDFLIB_MEM_SUPPORTED /* PDFlib GmbH: */
  1205.    png_ptr->mem_ptr = mem_ptr;
  1206. # endif
  1207. #endif
  1208.  
  1209. #ifdef PNG_SETJMP_SUPPORTED
  1210.    png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
  1211. #endif
  1212.  
  1213. }
  1214.  
  1215. void PNGAPI
  1216. png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
  1217. {
  1218.    png_ptr->read_row_fn = read_row_fn;
  1219. }
  1220.  
  1221. #if defined(PNG_INFO_IMAGE_SUPPORTED)
  1222. void PNGAPI
  1223. png_read_png(png_structp png_ptr, png_infop info_ptr,
  1224.                            int transforms,
  1225.                            voidp params)
  1226. {
  1227.    int row;
  1228.  
  1229. #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
  1230.    /* invert the alpha channel from opacity to transparency */
  1231.    if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
  1232.        png_set_invert_alpha(png_ptr);
  1233. #endif
  1234.  
  1235.    /* The call to png_read_info() gives us all of the information from the
  1236.     * PNG file before the first IDAT (image data chunk).
  1237.     */
  1238.    png_read_info(png_ptr, info_ptr);
  1239.  
  1240.    /* -------------- image transformations start here ------------------- */
  1241.  
  1242. #if defined(PNG_READ_16_TO_8_SUPPORTED)
  1243.    /* tell libpng to strip 16 bit/color files down to 8 bits/color */
  1244.    if (transforms & PNG_TRANSFORM_STRIP_16)
  1245.        png_set_strip_16(png_ptr);
  1246. #endif
  1247.  
  1248. #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
  1249.    /* Strip alpha bytes from the input data without combining with the
  1250.     * background (not recommended).
  1251.     */
  1252.    if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
  1253.        png_set_strip_alpha(png_ptr);
  1254. #endif
  1255.  
  1256. #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
  1257.    /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
  1258.     * byte into separate bytes (useful for paletted and grayscale images).
  1259.     */
  1260.    if (transforms & PNG_TRANSFORM_PACKING)
  1261.        png_set_packing(png_ptr);
  1262. #endif
  1263.  
  1264. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  1265.    /* Change the order of packed pixels to least significant bit first
  1266.     * (not useful if you are using png_set_packing). */
  1267.    if (transforms & PNG_TRANSFORM_PACKSWAP)
  1268.        png_set_packswap(png_ptr);
  1269. #endif
  1270.  
  1271. #if defined(PNG_READ_EXPAND_SUPPORTED)
  1272.    /* Expand paletted colors into true RGB triplets
  1273.     * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
  1274.     * Expand paletted or RGB images with transparency to full alpha
  1275.     * channels so the data will be available as RGBA quartets.
  1276.     */
  1277.    if (transforms & PNG_TRANSFORM_EXPAND)
  1278.        if ((png_ptr->bit_depth < 8) ||
  1279.            (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
  1280.            (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
  1281.          png_set_expand(png_ptr);
  1282. #endif
  1283.  
  1284.    /* We don't handle background color or gamma transformation or dithering. */
  1285.  
  1286. #if defined(PNG_READ_INVERT_SUPPORTED)
  1287.    /* invert monochrome files to have 0 as white and 1 as black */
  1288.    if (transforms & PNG_TRANSFORM_INVERT_MONO)
  1289.        png_set_invert_mono(png_ptr);
  1290. #endif
  1291.  
  1292. #if defined(PNG_READ_SHIFT_SUPPORTED)
  1293.    /* If you want to shift the pixel values from the range [0,255] or
  1294.     * [0,65535] to the original [0,7] or [0,31], or whatever range the
  1295.     * colors were originally in:
  1296.     */
  1297.    if ((transforms & PNG_TRANSFORM_SHIFT)
  1298.        && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
  1299.    {
  1300.       png_color_8p sig_bit;
  1301.  
  1302.       png_get_sBIT(png_ptr, info_ptr, &sig_bit);
  1303.       png_set_shift(png_ptr, sig_bit);
  1304.    }
  1305. #endif
  1306.  
  1307. #if defined(PNG_READ_BGR_SUPPORTED)
  1308.    /* flip the RGB pixels to BGR (or RGBA to BGRA) */
  1309.    if (transforms & PNG_TRANSFORM_BGR)
  1310.        png_set_bgr(png_ptr);
  1311. #endif
  1312.  
  1313. #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
  1314.    /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
  1315.    if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
  1316.        png_set_swap_alpha(png_ptr);
  1317. #endif
  1318.  
  1319. #if defined(PNG_READ_SWAP_SUPPORTED)
  1320.    /* swap bytes of 16 bit files to least significant byte first */
  1321.    if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
  1322.        png_set_swap(png_ptr);
  1323. #endif
  1324.  
  1325.    /* We don't handle adding filler bytes */
  1326.  
  1327.    /* Optional call to gamma correct and add the background to the palette
  1328.     * and update info structure.  REQUIRED if you are expecting libpng to
  1329.     * update the palette for you (ie you selected such a transform above).
  1330.     */
  1331.    png_read_update_info(png_ptr, info_ptr);
  1332.  
  1333.    /* -------------- image transformations end here ------------------- */
  1334.  
  1335. #ifdef PNG_FREE_ME_SUPPORTED
  1336.    png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  1337. #endif
  1338.    if(info_ptr->row_pointers == NULL)
  1339.    {
  1340.       info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
  1341.                                          info_ptr->height * sizeof(png_bytep));
  1342. #ifdef PNG_FREE_ME_SUPPORTED
  1343.       info_ptr->free_me |= PNG_FREE_ROWS;
  1344. #endif
  1345.       for (row = 0; row < (int)info_ptr->height; row++)
  1346.          info_ptr->row_pointers[row] = png_malloc(png_ptr,
  1347.             png_get_rowbytes(png_ptr, info_ptr));
  1348.    }
  1349.  
  1350.    png_read_image(png_ptr, info_ptr->row_pointers);
  1351.    info_ptr->valid |= PNG_INFO_IDAT;
  1352.  
  1353.    /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
  1354.    png_read_end(png_ptr, info_ptr);
  1355.  
  1356.    if(transforms == 0 || params == (voidp)NULL)
  1357.       /* quiet compiler warnings */ return;
  1358.  
  1359. }
  1360. #endif
  1361.