home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pdflb302.zip / pdf / lpng.1-0-8 / pngread.c < prev    next >
C/C++ Source or Header  |  2000-07-24  |  44KB  |  1,323 lines

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