Back to index

plt-scheme  4.2.1
pngread.c
Go to the documentation of this file.
00001 
00002 /* pngread.c - read a PNG file
00003  *
00004  * libpng 1.2.5 - October 3, 2002
00005  * For conditions of distribution and use, see copyright notice in png.h
00006  * Copyright (c) 1998-2002 Glenn Randers-Pehrson
00007  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
00008  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
00009  *
00010  * This file contains routines that an application calls directly to
00011  * read a PNG file or stream.
00012  */
00013 
00014 #define PNG_INTERNAL
00015 #include "png.h"
00016 
00017 /* Create a PNG structure for reading, and allocate any memory needed. */
00018 png_structp PNGAPI
00019 png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
00020    png_error_ptr error_fn, png_error_ptr warn_fn)
00021 {
00022 
00023 #ifdef PNG_USER_MEM_SUPPORTED
00024    return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
00025       warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
00026 }
00027 
00028 /* Alternate create PNG structure for reading, and allocate any memory needed. */
00029 png_structp PNGAPI
00030 png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
00031    png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
00032    png_malloc_ptr malloc_fn, png_free_ptr free_fn)
00033 {
00034 #endif /* PNG_USER_MEM_SUPPORTED */
00035 
00036    png_structp png_ptr;
00037 
00038 #ifdef PNG_SETJMP_SUPPORTED
00039 #ifdef USE_FAR_KEYWORD
00040    jmp_buf jmpbuf;
00041 #endif
00042 #endif
00043 
00044    int i;
00045 
00046    png_debug(1, "in png_create_read_struct\n");
00047 #ifdef PNG_USER_MEM_SUPPORTED
00048    png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
00049       (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
00050 #else
00051    png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
00052 #endif
00053    if (png_ptr == NULL)
00054       return (NULL);
00055 
00056 #if !defined(PNG_1_0_X)
00057 #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
00058    png_init_mmx_flags(png_ptr);   /* 1.2.0 addition */
00059 #endif
00060 #endif /* PNG_1_0_X */
00061 
00062 #ifdef PNG_SETJMP_SUPPORTED
00063 #ifdef USE_FAR_KEYWORD
00064    if (setjmp(jmpbuf))
00065 #else
00066    if (setjmp(png_ptr->jmpbuf))
00067 #endif
00068    {
00069       png_free(png_ptr, png_ptr->zbuf);
00070       png_ptr->zbuf=NULL;
00071 #ifdef PNG_USER_MEM_SUPPORTED
00072       png_destroy_struct_2((png_voidp)png_ptr, 
00073          (png_free_ptr)free_fn, (png_voidp)mem_ptr);
00074 #else
00075       png_destroy_struct((png_voidp)png_ptr);
00076 #endif
00077       return (NULL);
00078    }
00079 #ifdef USE_FAR_KEYWORD
00080    png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
00081 #endif
00082 #endif
00083 
00084 #ifdef PNG_USER_MEM_SUPPORTED
00085    png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
00086 #endif
00087 
00088    png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
00089 
00090    i=0;
00091    do
00092    {
00093      if(user_png_ver[i] != png_libpng_ver[i])
00094         png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
00095    } while (png_libpng_ver[i++]);
00096 
00097    if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
00098    {
00099      /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
00100       * we must recompile any applications that use any older library version.
00101       * For versions after libpng 1.0, we will be compatible, so we need
00102       * only check the first digit.
00103       */
00104      if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
00105          (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
00106          (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
00107      {
00108 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
00109         char msg[80];
00110         if (user_png_ver)
00111         {
00112           sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
00113              user_png_ver);
00114           png_warning(png_ptr, msg);
00115         }
00116         sprintf(msg, "Application  is  running with png.c from libpng-%.20s",
00117            png_libpng_ver);
00118         png_warning(png_ptr, msg);
00119 #endif
00120 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
00121         png_ptr->flags=0;
00122 #endif
00123         png_error(png_ptr,
00124            "Incompatible libpng version in application and library");
00125      }
00126    }
00127 
00128    /* initialize zbuf - compression buffer */
00129    png_ptr->zbuf_size = PNG_ZBUF_SIZE;
00130    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
00131      (png_uint_32)png_ptr->zbuf_size);
00132    png_ptr->zstream.zalloc = png_zalloc;
00133    png_ptr->zstream.zfree = png_zfree;
00134    png_ptr->zstream.opaque = (voidpf)png_ptr;
00135 
00136    switch (inflateInit(&png_ptr->zstream))
00137    {
00138      case Z_OK: /* Do nothing */ break;
00139      case Z_MEM_ERROR:
00140      case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
00141      case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
00142      default: png_error(png_ptr, "Unknown zlib error");
00143    }
00144 
00145    png_ptr->zstream.next_out = png_ptr->zbuf;
00146    png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
00147 
00148    png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
00149 
00150 #ifdef PNG_SETJMP_SUPPORTED
00151 /* Applications that neglect to set up their own setjmp() and then encounter
00152    a png_error() will longjmp here.  Since the jmpbuf is then meaningless we
00153    abort instead of returning. */
00154 #ifdef USE_FAR_KEYWORD
00155    if (setjmp(jmpbuf))
00156       PNG_ABORT();
00157    png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
00158 #else
00159    if (setjmp(png_ptr->jmpbuf))
00160       PNG_ABORT();
00161 #endif
00162 #endif
00163    return (png_ptr);
00164 }
00165 
00166 /* Initialize PNG structure for reading, and allocate any memory needed.
00167    This interface is deprecated in favour of the png_create_read_struct(),
00168    and it will eventually disappear. */
00169 #undef png_read_init
00170 void PNGAPI
00171 png_read_init(png_structp png_ptr)
00172 {
00173    /* We only come here via pre-1.0.7-compiled applications */
00174    png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
00175 }
00176 
00177 void PNGAPI
00178 png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
00179    png_size_t png_struct_size, png_size_t png_info_size)
00180 {
00181    /* We only come here via pre-1.0.12-compiled applications */
00182 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
00183    if(sizeof(png_struct) > png_struct_size || sizeof(png_info) > png_info_size)
00184    {
00185       char msg[80];
00186       png_ptr->warning_fn=NULL;
00187       if (user_png_ver)
00188       {
00189         sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
00190            user_png_ver);
00191         png_warning(png_ptr, msg);
00192       }
00193       sprintf(msg, "Application  is  running with png.c from libpng-%.20s",
00194          png_libpng_ver);
00195       png_warning(png_ptr, msg);
00196    }
00197 #endif
00198    if(sizeof(png_struct) > png_struct_size)
00199      {
00200        png_ptr->error_fn=NULL;
00201 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
00202        png_ptr->flags=0;
00203 #endif
00204        png_error(png_ptr,
00205        "The png struct allocated by the application for reading is too small.");
00206      }
00207    if(sizeof(png_info) > png_info_size)
00208      {
00209        png_ptr->error_fn=NULL;
00210 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
00211        png_ptr->flags=0;
00212 #endif
00213        png_error(png_ptr,
00214          "The info struct allocated by application for reading is too small.");
00215      }
00216    png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
00217 }
00218 
00219 void PNGAPI
00220 png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
00221    png_size_t png_struct_size)
00222 {
00223 #ifdef PNG_SETJMP_SUPPORTED
00224    jmp_buf tmp_jmp;  /* to save current jump buffer */
00225 #endif
00226 
00227    int i=0;
00228 
00229    png_structp png_ptr=*ptr_ptr;
00230 
00231    do
00232    {
00233      if(user_png_ver[i] != png_libpng_ver[i])
00234      {
00235 #ifdef PNG_LEGACY_SUPPORTED
00236        png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
00237 #else
00238        png_ptr->warning_fn=NULL;
00239        png_warning(png_ptr,
00240         "Application uses deprecated png_read_init() and should be recompiled.");
00241        break;
00242 #endif
00243      }
00244    } while (png_libpng_ver[i++]);
00245 
00246    png_debug(1, "in png_read_init_3\n");
00247 
00248 #ifdef PNG_SETJMP_SUPPORTED
00249    /* save jump buffer and error functions */
00250    png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
00251 #endif
00252 
00253    if(sizeof(png_struct) > png_struct_size)
00254      {
00255        png_destroy_struct(png_ptr);
00256        *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
00257        png_ptr = *ptr_ptr;
00258      }
00259 
00260    /* reset all variables to 0 */
00261    png_memset(png_ptr, 0, sizeof (png_struct));
00262 
00263 #ifdef PNG_SETJMP_SUPPORTED
00264    /* restore jump buffer */
00265    png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
00266 #endif
00267 
00268    /* initialize zbuf - compression buffer */
00269    png_ptr->zbuf_size = PNG_ZBUF_SIZE;
00270    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
00271      (png_uint_32)png_ptr->zbuf_size);
00272    png_ptr->zstream.zalloc = png_zalloc;
00273    png_ptr->zstream.zfree = png_zfree;
00274    png_ptr->zstream.opaque = (voidpf)png_ptr;
00275 
00276    switch (inflateInit(&png_ptr->zstream))
00277    {
00278      case Z_OK: /* Do nothing */ break;
00279      case Z_MEM_ERROR:
00280      case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
00281      case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
00282      default: png_error(png_ptr, "Unknown zlib error");
00283    }
00284 
00285    png_ptr->zstream.next_out = png_ptr->zbuf;
00286    png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
00287 
00288    png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
00289 }
00290 
00291 /* Read the information before the actual image data.  This has been
00292  * changed in v0.90 to allow reading a file that already has the magic
00293  * bytes read from the stream.  You can tell libpng how many bytes have
00294  * been read from the beginning of the stream (up to the maximum of 8)
00295  * via png_set_sig_bytes(), and we will only check the remaining bytes
00296  * here.  The application can then have access to the signature bytes we
00297  * read if it is determined that this isn't a valid PNG file.
00298  */
00299 void PNGAPI
00300 png_read_info(png_structp png_ptr, png_infop info_ptr)
00301 {
00302    png_debug(1, "in png_read_info\n");
00303    /* If we haven't checked all of the PNG signature bytes, do so now. */
00304    if (png_ptr->sig_bytes < 8)
00305    {
00306       png_size_t num_checked = png_ptr->sig_bytes,
00307                  num_to_check = 8 - num_checked;
00308 
00309       png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
00310       png_ptr->sig_bytes = 8;
00311 
00312       if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
00313       {
00314          if (num_checked < 4 &&
00315              png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
00316             png_error(png_ptr, "Not a PNG file");
00317          else
00318             png_error(png_ptr, "PNG file corrupted by ASCII conversion");
00319       }
00320       if (num_checked < 3)
00321          png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
00322    }
00323 
00324    for(;;)
00325    {
00326 #ifdef PNG_USE_LOCAL_ARRAYS
00327       PNG_IHDR;
00328       PNG_IDAT;
00329       PNG_IEND;
00330       PNG_PLTE;
00331 #if defined(PNG_READ_bKGD_SUPPORTED)
00332       PNG_bKGD;
00333 #endif
00334 #if defined(PNG_READ_cHRM_SUPPORTED)
00335       PNG_cHRM;
00336 #endif
00337 #if defined(PNG_READ_gAMA_SUPPORTED)
00338       PNG_gAMA;
00339 #endif
00340 #if defined(PNG_READ_hIST_SUPPORTED)
00341       PNG_hIST;
00342 #endif
00343 #if defined(PNG_READ_iCCP_SUPPORTED)
00344       PNG_iCCP;
00345 #endif
00346 #if defined(PNG_READ_iTXt_SUPPORTED)
00347       PNG_iTXt;
00348 #endif
00349 #if defined(PNG_READ_oFFs_SUPPORTED)
00350       PNG_oFFs;
00351 #endif
00352 #if defined(PNG_READ_pCAL_SUPPORTED)
00353       PNG_pCAL;
00354 #endif
00355 #if defined(PNG_READ_pHYs_SUPPORTED)
00356       PNG_pHYs;
00357 #endif
00358 #if defined(PNG_READ_sBIT_SUPPORTED)
00359       PNG_sBIT;
00360 #endif
00361 #if defined(PNG_READ_sCAL_SUPPORTED)
00362       PNG_sCAL;
00363 #endif
00364 #if defined(PNG_READ_sPLT_SUPPORTED)
00365       PNG_sPLT;
00366 #endif
00367 #if defined(PNG_READ_sRGB_SUPPORTED)
00368       PNG_sRGB;
00369 #endif
00370 #if defined(PNG_READ_tEXt_SUPPORTED)
00371       PNG_tEXt;
00372 #endif
00373 #if defined(PNG_READ_tIME_SUPPORTED)
00374       PNG_tIME;
00375 #endif
00376 #if defined(PNG_READ_tRNS_SUPPORTED)
00377       PNG_tRNS;
00378 #endif
00379 #if defined(PNG_READ_zTXt_SUPPORTED)
00380       PNG_zTXt;
00381 #endif
00382 #endif /* PNG_GLOBAL_ARRAYS */
00383       png_byte chunk_length[4];
00384       png_uint_32 length;
00385 
00386       png_read_data(png_ptr, chunk_length, 4);
00387       length = png_get_uint_32(chunk_length);
00388 
00389       png_reset_crc(png_ptr);
00390       png_crc_read(png_ptr, png_ptr->chunk_name, 4);
00391 
00392       png_debug2(0, "Reading %s chunk, length=%lu.\n", png_ptr->chunk_name,
00393          length);
00394 
00395       if (length > PNG_MAX_UINT)
00396          png_error(png_ptr, "Invalid chunk length.");
00397 
00398       /* This should be a binary subdivision search or a hash for
00399        * matching the chunk name rather than a linear search.
00400        */
00401       if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
00402          png_handle_IHDR(png_ptr, info_ptr, length);
00403       else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
00404          png_handle_IEND(png_ptr, info_ptr, length);
00405 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
00406       else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
00407       {
00408          if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
00409             png_ptr->mode |= PNG_HAVE_IDAT;
00410          png_handle_unknown(png_ptr, info_ptr, length);
00411          if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
00412             png_ptr->mode |= PNG_HAVE_PLTE;
00413          else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
00414          {
00415             if (!(png_ptr->mode & PNG_HAVE_IHDR))
00416                png_error(png_ptr, "Missing IHDR before IDAT");
00417             else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
00418                      !(png_ptr->mode & PNG_HAVE_PLTE))
00419                png_error(png_ptr, "Missing PLTE before IDAT");
00420             break;
00421          }
00422       }
00423 #endif
00424       else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
00425          png_handle_PLTE(png_ptr, info_ptr, length);
00426       else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
00427       {
00428          if (!(png_ptr->mode & PNG_HAVE_IHDR))
00429             png_error(png_ptr, "Missing IHDR before IDAT");
00430          else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
00431                   !(png_ptr->mode & PNG_HAVE_PLTE))
00432             png_error(png_ptr, "Missing PLTE before IDAT");
00433 
00434          png_ptr->idat_size = length;
00435          png_ptr->mode |= PNG_HAVE_IDAT;
00436          break;
00437       }
00438 #if defined(PNG_READ_bKGD_SUPPORTED)
00439       else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
00440          png_handle_bKGD(png_ptr, info_ptr, length);
00441 #endif
00442 #if defined(PNG_READ_cHRM_SUPPORTED)
00443       else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
00444          png_handle_cHRM(png_ptr, info_ptr, length);
00445 #endif
00446 #if defined(PNG_READ_gAMA_SUPPORTED)
00447       else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
00448          png_handle_gAMA(png_ptr, info_ptr, length);
00449 #endif
00450 #if defined(PNG_READ_hIST_SUPPORTED)
00451       else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
00452          png_handle_hIST(png_ptr, info_ptr, length);
00453 #endif
00454 #if defined(PNG_READ_oFFs_SUPPORTED)
00455       else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
00456          png_handle_oFFs(png_ptr, info_ptr, length);
00457 #endif
00458 #if defined(PNG_READ_pCAL_SUPPORTED)
00459       else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
00460          png_handle_pCAL(png_ptr, info_ptr, length);
00461 #endif
00462 #if defined(PNG_READ_sCAL_SUPPORTED)
00463       else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
00464          png_handle_sCAL(png_ptr, info_ptr, length);
00465 #endif
00466 #if defined(PNG_READ_pHYs_SUPPORTED)
00467       else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
00468          png_handle_pHYs(png_ptr, info_ptr, length);
00469 #endif
00470 #if defined(PNG_READ_sBIT_SUPPORTED)
00471       else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
00472          png_handle_sBIT(png_ptr, info_ptr, length);
00473 #endif
00474 #if defined(PNG_READ_sRGB_SUPPORTED)
00475       else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
00476          png_handle_sRGB(png_ptr, info_ptr, length);
00477 #endif
00478 #if defined(PNG_READ_iCCP_SUPPORTED)
00479       else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
00480          png_handle_iCCP(png_ptr, info_ptr, length);
00481 #endif
00482 #if defined(PNG_READ_sPLT_SUPPORTED)
00483       else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
00484          png_handle_sPLT(png_ptr, info_ptr, length);
00485 #endif
00486 #if defined(PNG_READ_tEXt_SUPPORTED)
00487       else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
00488          png_handle_tEXt(png_ptr, info_ptr, length);
00489 #endif
00490 #if defined(PNG_READ_tIME_SUPPORTED)
00491       else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
00492          png_handle_tIME(png_ptr, info_ptr, length);
00493 #endif
00494 #if defined(PNG_READ_tRNS_SUPPORTED)
00495       else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
00496          png_handle_tRNS(png_ptr, info_ptr, length);
00497 #endif
00498 #if defined(PNG_READ_zTXt_SUPPORTED)
00499       else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
00500          png_handle_zTXt(png_ptr, info_ptr, length);
00501 #endif
00502 #if defined(PNG_READ_iTXt_SUPPORTED)
00503       else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
00504          png_handle_iTXt(png_ptr, info_ptr, length);
00505 #endif
00506       else
00507          png_handle_unknown(png_ptr, info_ptr, length);
00508    }
00509 }
00510 
00511 /* optional call to update the users info_ptr structure */
00512 void PNGAPI
00513 png_read_update_info(png_structp png_ptr, png_infop info_ptr)
00514 {
00515    png_debug(1, "in png_read_update_info\n");
00516    if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
00517       png_read_start_row(png_ptr);
00518    else
00519       png_warning(png_ptr,
00520       "Ignoring extra png_read_update_info() call; row buffer not reallocated");
00521    png_read_transform_info(png_ptr, info_ptr);
00522 }
00523 
00524 /* Initialize palette, background, etc, after transformations
00525  * are set, but before any reading takes place.  This allows
00526  * the user to obtain a gamma-corrected palette, for example.
00527  * If the user doesn't call this, we will do it ourselves.
00528  */
00529 void PNGAPI
00530 png_start_read_image(png_structp png_ptr)
00531 {
00532    png_debug(1, "in png_start_read_image\n");
00533    if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
00534       png_read_start_row(png_ptr);
00535 }
00536 
00537 void PNGAPI
00538 png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
00539 {
00540 #ifdef PNG_USE_LOCAL_ARRAYS
00541    PNG_IDAT;
00542    const int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
00543    const int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
00544 #endif
00545    int ret;
00546    png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
00547       png_ptr->row_number, png_ptr->pass);
00548    if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
00549       png_read_start_row(png_ptr);
00550    if (png_ptr->row_number == 0 && png_ptr->pass == 0)
00551    {
00552    /* check for transforms that have been set but were defined out */
00553 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
00554    if (png_ptr->transformations & PNG_INVERT_MONO)
00555       png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
00556 #endif
00557 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
00558    if (png_ptr->transformations & PNG_FILLER)
00559       png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
00560 #endif
00561 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
00562    if (png_ptr->transformations & PNG_PACKSWAP)
00563       png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
00564 #endif
00565 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
00566    if (png_ptr->transformations & PNG_PACK)
00567       png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
00568 #endif
00569 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
00570    if (png_ptr->transformations & PNG_SHIFT)
00571       png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
00572 #endif
00573 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
00574    if (png_ptr->transformations & PNG_BGR)
00575       png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
00576 #endif
00577 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
00578    if (png_ptr->transformations & PNG_SWAP_BYTES)
00579       png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
00580 #endif
00581    }
00582 
00583 #if defined(PNG_READ_INTERLACING_SUPPORTED)
00584    /* if interlaced and we do not need a new row, combine row and return */
00585    if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
00586    {
00587       switch (png_ptr->pass)
00588       {
00589          case 0:
00590             if (png_ptr->row_number & 0x07)
00591             {
00592                if (dsp_row != NULL)
00593                   png_combine_row(png_ptr, dsp_row,
00594                      png_pass_dsp_mask[png_ptr->pass]);
00595                png_read_finish_row(png_ptr);
00596                return;
00597             }
00598             break;
00599          case 1:
00600             if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
00601             {
00602                if (dsp_row != NULL)
00603                   png_combine_row(png_ptr, dsp_row,
00604                      png_pass_dsp_mask[png_ptr->pass]);
00605                png_read_finish_row(png_ptr);
00606                return;
00607             }
00608             break;
00609          case 2:
00610             if ((png_ptr->row_number & 0x07) != 4)
00611             {
00612                if (dsp_row != NULL && (png_ptr->row_number & 4))
00613                   png_combine_row(png_ptr, dsp_row,
00614                      png_pass_dsp_mask[png_ptr->pass]);
00615                png_read_finish_row(png_ptr);
00616                return;
00617             }
00618             break;
00619          case 3:
00620             if ((png_ptr->row_number & 3) || png_ptr->width < 3)
00621             {
00622                if (dsp_row != NULL)
00623                   png_combine_row(png_ptr, dsp_row,
00624                      png_pass_dsp_mask[png_ptr->pass]);
00625                png_read_finish_row(png_ptr);
00626                return;
00627             }
00628             break;
00629          case 4:
00630             if ((png_ptr->row_number & 3) != 2)
00631             {
00632                if (dsp_row != NULL && (png_ptr->row_number & 2))
00633                   png_combine_row(png_ptr, dsp_row,
00634                      png_pass_dsp_mask[png_ptr->pass]);
00635                png_read_finish_row(png_ptr);
00636                return;
00637             }
00638             break;
00639          case 5:
00640             if ((png_ptr->row_number & 1) || png_ptr->width < 2)
00641             {
00642                if (dsp_row != NULL)
00643                   png_combine_row(png_ptr, dsp_row,
00644                      png_pass_dsp_mask[png_ptr->pass]);
00645                png_read_finish_row(png_ptr);
00646                return;
00647             }
00648             break;
00649          case 6:
00650             if (!(png_ptr->row_number & 1))
00651             {
00652                png_read_finish_row(png_ptr);
00653                return;
00654             }
00655             break;
00656       }
00657    }
00658 #endif
00659 
00660    if (!(png_ptr->mode & PNG_HAVE_IDAT))
00661       png_error(png_ptr, "Invalid attempt to read row data");
00662 
00663    png_ptr->zstream.next_out = png_ptr->row_buf;
00664    png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
00665    do
00666    {
00667       if (!(png_ptr->zstream.avail_in))
00668       {
00669          while (!png_ptr->idat_size)
00670          {
00671             png_byte chunk_length[4];
00672 
00673             png_crc_finish(png_ptr, 0);
00674 
00675             png_read_data(png_ptr, chunk_length, 4);
00676             png_ptr->idat_size = png_get_uint_32(chunk_length);
00677 
00678             if (png_ptr->idat_size > PNG_MAX_UINT)
00679               png_error(png_ptr, "Invalid chunk length.");
00680 
00681             png_reset_crc(png_ptr);
00682             png_crc_read(png_ptr, png_ptr->chunk_name, 4);
00683             if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
00684                png_error(png_ptr, "Not enough image data");
00685          }
00686          png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
00687          png_ptr->zstream.next_in = png_ptr->zbuf;
00688          if (png_ptr->zbuf_size > png_ptr->idat_size)
00689             png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
00690          png_crc_read(png_ptr, png_ptr->zbuf,
00691             (png_size_t)png_ptr->zstream.avail_in);
00692          png_ptr->idat_size -= png_ptr->zstream.avail_in;
00693       }
00694       ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
00695       if (ret == Z_STREAM_END)
00696       {
00697          if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
00698             png_ptr->idat_size)
00699             png_error(png_ptr, "Extra compressed data");
00700          png_ptr->mode |= PNG_AFTER_IDAT;
00701          png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
00702          break;
00703       }
00704       if (ret != Z_OK)
00705          png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
00706                    "Decompression error");
00707 
00708    } while (png_ptr->zstream.avail_out);
00709 
00710    png_ptr->row_info.color_type = png_ptr->color_type;
00711    png_ptr->row_info.width = png_ptr->iwidth;
00712    png_ptr->row_info.channels = png_ptr->channels;
00713    png_ptr->row_info.bit_depth = png_ptr->bit_depth;
00714    png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
00715    png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
00716       (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
00717 
00718    if(png_ptr->row_buf[0])
00719    png_read_filter_row(png_ptr, &(png_ptr->row_info),
00720       png_ptr->row_buf + 1, png_ptr->prev_row + 1,
00721       (int)(png_ptr->row_buf[0]));
00722 
00723    png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
00724       png_ptr->rowbytes + 1);
00725    
00726 #if defined(PNG_MNG_FEATURES_SUPPORTED)
00727    if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
00728       (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
00729    {
00730       /* Intrapixel differencing */
00731       png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
00732    }
00733 #endif
00734 
00735    if (png_ptr->transformations)
00736       png_do_read_transformations(png_ptr);
00737 
00738 #if defined(PNG_READ_INTERLACING_SUPPORTED)
00739    /* blow up interlaced rows to full size */
00740    if (png_ptr->interlaced &&
00741       (png_ptr->transformations & PNG_INTERLACE))
00742    {
00743       if (png_ptr->pass < 6)
00744 /*       old interface (pre-1.0.9):
00745          png_do_read_interlace(&(png_ptr->row_info),
00746             png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
00747  */
00748          png_do_read_interlace(png_ptr);
00749 
00750       if (dsp_row != NULL)
00751          png_combine_row(png_ptr, dsp_row,
00752             png_pass_dsp_mask[png_ptr->pass]);
00753       if (row != NULL)
00754          png_combine_row(png_ptr, row,
00755             png_pass_mask[png_ptr->pass]);
00756    }
00757    else
00758 #endif
00759    {
00760       if (row != NULL)
00761          png_combine_row(png_ptr, row, 0xff);
00762       if (dsp_row != NULL)
00763          png_combine_row(png_ptr, dsp_row, 0xff);
00764    }
00765    png_read_finish_row(png_ptr);
00766 
00767    if (png_ptr->read_row_fn != NULL)
00768       (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
00769 }
00770 
00771 /* Read one or more rows of image data.  If the image is interlaced,
00772  * and png_set_interlace_handling() has been called, the rows need to
00773  * contain the contents of the rows from the previous pass.  If the
00774  * image has alpha or transparency, and png_handle_alpha()[*] has been
00775  * called, the rows contents must be initialized to the contents of the
00776  * screen.
00777  *
00778  * "row" holds the actual image, and pixels are placed in it
00779  * as they arrive.  If the image is displayed after each pass, it will
00780  * appear to "sparkle" in.  "display_row" can be used to display a
00781  * "chunky" progressive image, with finer detail added as it becomes
00782  * available.  If you do not want this "chunky" display, you may pass
00783  * NULL for display_row.  If you do not want the sparkle display, and
00784  * you have not called png_handle_alpha(), you may pass NULL for rows.
00785  * If you have called png_handle_alpha(), and the image has either an
00786  * alpha channel or a transparency chunk, you must provide a buffer for
00787  * rows.  In this case, you do not have to provide a display_row buffer
00788  * also, but you may.  If the image is not interlaced, or if you have
00789  * not called png_set_interlace_handling(), the display_row buffer will
00790  * be ignored, so pass NULL to it.
00791  *
00792  * [*] png_handle_alpha() does not exist yet, as of libpng version 1.2.5
00793  */
00794 
00795 void PNGAPI
00796 png_read_rows(png_structp png_ptr, png_bytepp row,
00797    png_bytepp display_row, png_uint_32 num_rows)
00798 {
00799    png_uint_32 i;
00800    png_bytepp rp;
00801    png_bytepp dp;
00802 
00803    png_debug(1, "in png_read_rows\n");
00804    rp = row;
00805    dp = display_row;
00806    if (rp != NULL && dp != NULL)
00807       for (i = 0; i < num_rows; i++)
00808       {
00809          png_bytep rptr = *rp++;
00810          png_bytep dptr = *dp++;
00811 
00812          png_read_row(png_ptr, rptr, dptr);
00813       }
00814    else if(rp != NULL)
00815       for (i = 0; i < num_rows; i++)
00816       {
00817          png_bytep rptr = *rp;
00818          png_read_row(png_ptr, rptr, png_bytep_NULL);
00819          rp++;
00820       }
00821    else if(dp != NULL)
00822       for (i = 0; i < num_rows; i++)
00823       {
00824          png_bytep dptr = *dp;
00825          png_read_row(png_ptr, png_bytep_NULL, dptr);
00826          dp++;
00827       }
00828 }
00829 
00830 /* Read the entire image.  If the image has an alpha channel or a tRNS
00831  * chunk, and you have called png_handle_alpha()[*], you will need to
00832  * initialize the image to the current image that PNG will be overlaying.
00833  * We set the num_rows again here, in case it was incorrectly set in
00834  * png_read_start_row() by a call to png_read_update_info() or
00835  * png_start_read_image() if png_set_interlace_handling() wasn't called
00836  * prior to either of these functions like it should have been.  You can
00837  * only call this function once.  If you desire to have an image for
00838  * each pass of a interlaced image, use png_read_rows() instead.
00839  *
00840  * [*] png_handle_alpha() does not exist yet, as of libpng version 1.2.5
00841  */
00842 void PNGAPI
00843 png_read_image(png_structp png_ptr, png_bytepp image)
00844 {
00845    png_uint_32 i,image_height;
00846    int pass, j;
00847    png_bytepp rp;
00848 
00849    png_debug(1, "in png_read_image\n");
00850 
00851 #ifdef PNG_READ_INTERLACING_SUPPORTED
00852    pass = png_set_interlace_handling(png_ptr);
00853 #else
00854    if (png_ptr->interlaced)
00855       png_error(png_ptr,
00856         "Cannot read interlaced image -- interlace handler disabled.");
00857    pass = 1;
00858 #endif
00859 
00860 
00861    image_height=png_ptr->height;
00862    png_ptr->num_rows = image_height; /* Make sure this is set correctly */
00863 
00864    for (j = 0; j < pass; j++)
00865    {
00866       rp = image;
00867       for (i = 0; i < image_height; i++)
00868       {
00869          png_read_row(png_ptr, *rp, png_bytep_NULL);
00870          rp++;
00871       }
00872    }
00873 }
00874 
00875 /* Read the end of the PNG file.  Will not read past the end of the
00876  * file, will verify the end is accurate, and will read any comments
00877  * or time information at the end of the file, if info is not NULL.
00878  */
00879 void PNGAPI
00880 png_read_end(png_structp png_ptr, png_infop info_ptr)
00881 {
00882    png_byte chunk_length[4];
00883    png_uint_32 length;
00884 
00885    png_debug(1, "in png_read_end\n");
00886    png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
00887 
00888    do
00889    {
00890 #ifdef PNG_USE_LOCAL_ARRAYS
00891       PNG_IHDR;
00892       PNG_IDAT;
00893       PNG_IEND;
00894       PNG_PLTE;
00895 #if defined(PNG_READ_bKGD_SUPPORTED)
00896       PNG_bKGD;
00897 #endif
00898 #if defined(PNG_READ_cHRM_SUPPORTED)
00899       PNG_cHRM;
00900 #endif
00901 #if defined(PNG_READ_gAMA_SUPPORTED)
00902       PNG_gAMA;
00903 #endif
00904 #if defined(PNG_READ_hIST_SUPPORTED)
00905       PNG_hIST;
00906 #endif
00907 #if defined(PNG_READ_iCCP_SUPPORTED)
00908       PNG_iCCP;
00909 #endif
00910 #if defined(PNG_READ_iTXt_SUPPORTED)
00911       PNG_iTXt;
00912 #endif
00913 #if defined(PNG_READ_oFFs_SUPPORTED)
00914       PNG_oFFs;
00915 #endif
00916 #if defined(PNG_READ_pCAL_SUPPORTED)
00917       PNG_pCAL;
00918 #endif
00919 #if defined(PNG_READ_pHYs_SUPPORTED)
00920       PNG_pHYs;
00921 #endif
00922 #if defined(PNG_READ_sBIT_SUPPORTED)
00923       PNG_sBIT;
00924 #endif
00925 #if defined(PNG_READ_sCAL_SUPPORTED)
00926       PNG_sCAL;
00927 #endif
00928 #if defined(PNG_READ_sPLT_SUPPORTED)
00929       PNG_sPLT;
00930 #endif
00931 #if defined(PNG_READ_sRGB_SUPPORTED)
00932       PNG_sRGB;
00933 #endif
00934 #if defined(PNG_READ_tEXt_SUPPORTED)
00935       PNG_tEXt;
00936 #endif
00937 #if defined(PNG_READ_tIME_SUPPORTED)
00938       PNG_tIME;
00939 #endif
00940 #if defined(PNG_READ_tRNS_SUPPORTED)
00941       PNG_tRNS;
00942 #endif
00943 #if defined(PNG_READ_zTXt_SUPPORTED)
00944       PNG_zTXt;
00945 #endif
00946 #endif /* PNG_GLOBAL_ARRAYS */
00947 
00948       png_read_data(png_ptr, chunk_length, 4);
00949       length = png_get_uint_32(chunk_length);
00950 
00951       png_reset_crc(png_ptr);
00952       png_crc_read(png_ptr, png_ptr->chunk_name, 4);
00953 
00954       png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
00955 
00956       if (length > PNG_MAX_UINT)
00957          png_error(png_ptr, "Invalid chunk length.");
00958 
00959       if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
00960          png_handle_IHDR(png_ptr, info_ptr, length);
00961       else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
00962          png_handle_IEND(png_ptr, info_ptr, length);
00963 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
00964       else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
00965       {
00966          if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
00967          {
00968             if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
00969                png_error(png_ptr, "Too many IDAT's found");
00970          }
00971          else
00972             png_ptr->mode |= PNG_AFTER_IDAT;
00973          png_handle_unknown(png_ptr, info_ptr, length);
00974          if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
00975             png_ptr->mode |= PNG_HAVE_PLTE;
00976       }
00977 #endif
00978       else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
00979       {
00980          /* Zero length IDATs are legal after the last IDAT has been
00981           * read, but not after other chunks have been read.
00982           */
00983          if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
00984             png_error(png_ptr, "Too many IDAT's found");
00985          png_crc_finish(png_ptr, length);
00986       }
00987       else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
00988          png_handle_PLTE(png_ptr, info_ptr, length);
00989 #if defined(PNG_READ_bKGD_SUPPORTED)
00990       else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
00991          png_handle_bKGD(png_ptr, info_ptr, length);
00992 #endif
00993 #if defined(PNG_READ_cHRM_SUPPORTED)
00994       else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
00995          png_handle_cHRM(png_ptr, info_ptr, length);
00996 #endif
00997 #if defined(PNG_READ_gAMA_SUPPORTED)
00998       else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
00999          png_handle_gAMA(png_ptr, info_ptr, length);
01000 #endif
01001 #if defined(PNG_READ_hIST_SUPPORTED)
01002       else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
01003          png_handle_hIST(png_ptr, info_ptr, length);
01004 #endif
01005 #if defined(PNG_READ_oFFs_SUPPORTED)
01006       else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
01007          png_handle_oFFs(png_ptr, info_ptr, length);
01008 #endif
01009 #if defined(PNG_READ_pCAL_SUPPORTED)
01010       else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
01011          png_handle_pCAL(png_ptr, info_ptr, length);
01012 #endif
01013 #if defined(PNG_READ_sCAL_SUPPORTED)
01014       else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
01015          png_handle_sCAL(png_ptr, info_ptr, length);
01016 #endif
01017 #if defined(PNG_READ_pHYs_SUPPORTED)
01018       else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
01019          png_handle_pHYs(png_ptr, info_ptr, length);
01020 #endif
01021 #if defined(PNG_READ_sBIT_SUPPORTED)
01022       else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
01023          png_handle_sBIT(png_ptr, info_ptr, length);
01024 #endif
01025 #if defined(PNG_READ_sRGB_SUPPORTED)
01026       else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
01027          png_handle_sRGB(png_ptr, info_ptr, length);
01028 #endif
01029 #if defined(PNG_READ_iCCP_SUPPORTED)
01030       else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
01031          png_handle_iCCP(png_ptr, info_ptr, length);
01032 #endif
01033 #if defined(PNG_READ_sPLT_SUPPORTED)
01034       else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
01035          png_handle_sPLT(png_ptr, info_ptr, length);
01036 #endif
01037 #if defined(PNG_READ_tEXt_SUPPORTED)
01038       else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
01039          png_handle_tEXt(png_ptr, info_ptr, length);
01040 #endif
01041 #if defined(PNG_READ_tIME_SUPPORTED)
01042       else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
01043          png_handle_tIME(png_ptr, info_ptr, length);
01044 #endif
01045 #if defined(PNG_READ_tRNS_SUPPORTED)
01046       else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
01047          png_handle_tRNS(png_ptr, info_ptr, length);
01048 #endif
01049 #if defined(PNG_READ_zTXt_SUPPORTED)
01050       else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
01051          png_handle_zTXt(png_ptr, info_ptr, length);
01052 #endif
01053 #if defined(PNG_READ_iTXt_SUPPORTED)
01054       else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
01055          png_handle_iTXt(png_ptr, info_ptr, length);
01056 #endif
01057       else
01058          png_handle_unknown(png_ptr, info_ptr, length);
01059    } while (!(png_ptr->mode & PNG_HAVE_IEND));
01060 }
01061 
01062 /* free all memory used by the read */
01063 void PNGAPI
01064 png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
01065    png_infopp end_info_ptr_ptr)
01066 {
01067    png_structp png_ptr = NULL;
01068    png_infop info_ptr = NULL, end_info_ptr = NULL;
01069 #ifdef PNG_USER_MEM_SUPPORTED
01070    png_free_ptr free_fn = NULL;
01071    png_voidp mem_ptr = NULL;
01072 #endif
01073 
01074    png_debug(1, "in png_destroy_read_struct\n");
01075    if (png_ptr_ptr != NULL)
01076       png_ptr = *png_ptr_ptr;
01077 
01078    if (info_ptr_ptr != NULL)
01079       info_ptr = *info_ptr_ptr;
01080 
01081    if (end_info_ptr_ptr != NULL)
01082       end_info_ptr = *end_info_ptr_ptr;
01083 
01084 #ifdef PNG_USER_MEM_SUPPORTED
01085    free_fn = png_ptr->free_fn;
01086    mem_ptr = png_ptr->mem_ptr;
01087 #endif
01088 
01089    png_read_destroy(png_ptr, info_ptr, end_info_ptr);
01090 
01091    if (info_ptr != NULL)
01092    {
01093 #if defined(PNG_TEXT_SUPPORTED)
01094       png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
01095 #endif
01096 
01097 #ifdef PNG_USER_MEM_SUPPORTED
01098       png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
01099           (png_voidp)mem_ptr);
01100 #else
01101       png_destroy_struct((png_voidp)info_ptr);
01102 #endif
01103       *info_ptr_ptr = NULL;
01104    }
01105 
01106    if (end_info_ptr != NULL)
01107    {
01108 #if defined(PNG_READ_TEXT_SUPPORTED)
01109       png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
01110 #endif
01111 #ifdef PNG_USER_MEM_SUPPORTED
01112       png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
01113          (png_voidp)mem_ptr);
01114 #else
01115       png_destroy_struct((png_voidp)end_info_ptr);
01116 #endif
01117       *end_info_ptr_ptr = NULL;
01118    }
01119 
01120    if (png_ptr != NULL)
01121    {
01122 #ifdef PNG_USER_MEM_SUPPORTED
01123       png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
01124           (png_voidp)mem_ptr);
01125 #else
01126       png_destroy_struct((png_voidp)png_ptr);
01127 #endif
01128       *png_ptr_ptr = NULL;
01129    }
01130 }
01131 
01132 /* free all memory used by the read (old method) */
01133 void /* PRIVATE */
01134 png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
01135 {
01136 #ifdef PNG_SETJMP_SUPPORTED
01137    jmp_buf tmp_jmp;
01138 #endif
01139    png_error_ptr error_fn;
01140    png_error_ptr warning_fn;
01141    png_voidp error_ptr;
01142 #ifdef PNG_USER_MEM_SUPPORTED
01143    png_free_ptr free_fn;
01144 #endif
01145 
01146    png_debug(1, "in png_read_destroy\n");
01147    if (info_ptr != NULL)
01148       png_info_destroy(png_ptr, info_ptr);
01149 
01150    if (end_info_ptr != NULL)
01151       png_info_destroy(png_ptr, end_info_ptr);
01152 
01153    png_free(png_ptr, png_ptr->zbuf);
01154    png_free(png_ptr, png_ptr->big_row_buf);
01155    png_free(png_ptr, png_ptr->prev_row);
01156 #if defined(PNG_READ_DITHER_SUPPORTED)
01157    png_free(png_ptr, png_ptr->palette_lookup);
01158    png_free(png_ptr, png_ptr->dither_index);
01159 #endif
01160 #if defined(PNG_READ_GAMMA_SUPPORTED)
01161    png_free(png_ptr, png_ptr->gamma_table);
01162 #endif
01163 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
01164    png_free(png_ptr, png_ptr->gamma_from_1);
01165    png_free(png_ptr, png_ptr->gamma_to_1);
01166 #endif
01167 #ifdef PNG_FREE_ME_SUPPORTED
01168    if (png_ptr->free_me & PNG_FREE_PLTE)
01169       png_zfree(png_ptr, png_ptr->palette);
01170    png_ptr->free_me &= ~PNG_FREE_PLTE;
01171 #else
01172    if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
01173       png_zfree(png_ptr, png_ptr->palette);
01174    png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
01175 #endif
01176 #if defined(PNG_tRNS_SUPPORTED) || \
01177     defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
01178 #ifdef PNG_FREE_ME_SUPPORTED
01179    if (png_ptr->free_me & PNG_FREE_TRNS)
01180       png_free(png_ptr, png_ptr->trans);
01181    png_ptr->free_me &= ~PNG_FREE_TRNS;
01182 #else
01183    if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
01184       png_free(png_ptr, png_ptr->trans);
01185    png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
01186 #endif
01187 #endif
01188 #if defined(PNG_READ_hIST_SUPPORTED)
01189 #ifdef PNG_FREE_ME_SUPPORTED
01190    if (png_ptr->free_me & PNG_FREE_HIST)
01191       png_free(png_ptr, png_ptr->hist);
01192    png_ptr->free_me &= ~PNG_FREE_HIST;
01193 #else
01194    if (png_ptr->flags & PNG_FLAG_FREE_HIST)
01195       png_free(png_ptr, png_ptr->hist);
01196    png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
01197 #endif
01198 #endif
01199 #if defined(PNG_READ_GAMMA_SUPPORTED)
01200    if (png_ptr->gamma_16_table != NULL)
01201    {
01202       int i;
01203       int istop = (1 << (8 - png_ptr->gamma_shift));
01204       for (i = 0; i < istop; i++)
01205       {
01206          png_free(png_ptr, png_ptr->gamma_16_table[i]);
01207       }
01208    png_free(png_ptr, png_ptr->gamma_16_table);
01209    }
01210 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
01211    if (png_ptr->gamma_16_from_1 != NULL)
01212    {
01213       int i;
01214       int istop = (1 << (8 - png_ptr->gamma_shift));
01215       for (i = 0; i < istop; i++)
01216       {
01217          png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
01218       }
01219    png_free(png_ptr, png_ptr->gamma_16_from_1);
01220    }
01221    if (png_ptr->gamma_16_to_1 != NULL)
01222    {
01223       int i;
01224       int istop = (1 << (8 - png_ptr->gamma_shift));
01225       for (i = 0; i < istop; i++)
01226       {
01227          png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
01228       }
01229    png_free(png_ptr, png_ptr->gamma_16_to_1);
01230    }
01231 #endif
01232 #endif
01233 #if defined(PNG_TIME_RFC1123_SUPPORTED)
01234    png_free(png_ptr, png_ptr->time_buffer);
01235 #endif
01236 
01237    inflateEnd(&png_ptr->zstream);
01238 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
01239    png_free(png_ptr, png_ptr->save_buffer);
01240 #endif
01241 
01242 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
01243 #ifdef PNG_TEXT_SUPPORTED
01244    png_free(png_ptr, png_ptr->current_text);
01245 #endif /* PNG_TEXT_SUPPORTED */
01246 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
01247 
01248    /* Save the important info out of the png_struct, in case it is
01249     * being used again.
01250     */
01251 #ifdef PNG_SETJMP_SUPPORTED
01252    png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
01253 #endif
01254 
01255    error_fn = png_ptr->error_fn;
01256    warning_fn = png_ptr->warning_fn;
01257    error_ptr = png_ptr->error_ptr;
01258 #ifdef PNG_USER_MEM_SUPPORTED
01259    free_fn = png_ptr->free_fn;
01260 #endif
01261 
01262    png_memset(png_ptr, 0, sizeof (png_struct));
01263 
01264    png_ptr->error_fn = error_fn;
01265    png_ptr->warning_fn = warning_fn;
01266    png_ptr->error_ptr = error_ptr;
01267 #ifdef PNG_USER_MEM_SUPPORTED
01268    png_ptr->free_fn = free_fn;
01269 #endif
01270 
01271 #ifdef PNG_SETJMP_SUPPORTED
01272    png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
01273 #endif
01274 
01275 }
01276 
01277 void PNGAPI
01278 png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
01279 {
01280    png_ptr->read_row_fn = read_row_fn;
01281 }
01282 
01283 #if defined(PNG_INFO_IMAGE_SUPPORTED)
01284 void PNGAPI
01285 png_read_png(png_structp png_ptr, png_infop info_ptr,
01286                            int transforms,
01287                            voidp params)
01288 {
01289    int row;
01290 
01291 #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
01292    /* invert the alpha channel from opacity to transparency */
01293    if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
01294        png_set_invert_alpha(png_ptr);
01295 #endif
01296 
01297    /* The call to png_read_info() gives us all of the information from the
01298     * PNG file before the first IDAT (image data chunk).
01299     */
01300    png_read_info(png_ptr, info_ptr);
01301 
01302    /* -------------- image transformations start here ------------------- */
01303 
01304 #if defined(PNG_READ_16_TO_8_SUPPORTED)
01305    /* tell libpng to strip 16 bit/color files down to 8 bits/color */
01306    if (transforms & PNG_TRANSFORM_STRIP_16)
01307        png_set_strip_16(png_ptr);
01308 #endif
01309 
01310 #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
01311    /* Strip alpha bytes from the input data without combining with the
01312     * background (not recommended).
01313     */
01314    if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
01315        png_set_strip_alpha(png_ptr);
01316 #endif
01317 
01318 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
01319    /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
01320     * byte into separate bytes (useful for paletted and grayscale images).
01321     */
01322    if (transforms & PNG_TRANSFORM_PACKING)
01323        png_set_packing(png_ptr);
01324 #endif
01325 
01326 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
01327    /* Change the order of packed pixels to least significant bit first
01328     * (not useful if you are using png_set_packing). */
01329    if (transforms & PNG_TRANSFORM_PACKSWAP)
01330        png_set_packswap(png_ptr);
01331 #endif
01332 
01333 #if defined(PNG_READ_EXPAND_SUPPORTED)
01334    /* Expand paletted colors into true RGB triplets
01335     * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
01336     * Expand paletted or RGB images with transparency to full alpha
01337     * channels so the data will be available as RGBA quartets.
01338     */
01339    if (transforms & PNG_TRANSFORM_EXPAND)
01340        if ((png_ptr->bit_depth < 8) ||
01341            (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
01342            (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
01343          png_set_expand(png_ptr);
01344 #endif
01345 
01346    /* We don't handle background color or gamma transformation or dithering. */
01347 
01348 #if defined(PNG_READ_INVERT_SUPPORTED)
01349    /* invert monochrome files to have 0 as white and 1 as black */
01350    if (transforms & PNG_TRANSFORM_INVERT_MONO)
01351        png_set_invert_mono(png_ptr);
01352 #endif
01353 
01354 #if defined(PNG_READ_SHIFT_SUPPORTED)
01355    /* If you want to shift the pixel values from the range [0,255] or
01356     * [0,65535] to the original [0,7] or [0,31], or whatever range the
01357     * colors were originally in:
01358     */
01359    if ((transforms & PNG_TRANSFORM_SHIFT)
01360        && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
01361    {
01362       png_color_8p sig_bit;
01363 
01364       png_get_sBIT(png_ptr, info_ptr, &sig_bit);
01365       png_set_shift(png_ptr, sig_bit);
01366    }
01367 #endif
01368 
01369 #if defined(PNG_READ_BGR_SUPPORTED)
01370    /* flip the RGB pixels to BGR (or RGBA to BGRA) */
01371    if (transforms & PNG_TRANSFORM_BGR)
01372        png_set_bgr(png_ptr);
01373 #endif
01374 
01375 #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
01376    /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
01377    if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
01378        png_set_swap_alpha(png_ptr);
01379 #endif
01380 
01381 #if defined(PNG_READ_SWAP_SUPPORTED)
01382    /* swap bytes of 16 bit files to least significant byte first */
01383    if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
01384        png_set_swap(png_ptr);
01385 #endif
01386 
01387    /* We don't handle adding filler bytes */
01388 
01389    /* Optional call to gamma correct and add the background to the palette
01390     * and update info structure.  REQUIRED if you are expecting libpng to
01391     * update the palette for you (i.e., you selected such a transform above).
01392     */
01393    png_read_update_info(png_ptr, info_ptr);
01394 
01395    /* -------------- image transformations end here ------------------- */
01396 
01397 #ifdef PNG_FREE_ME_SUPPORTED
01398    png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
01399 #endif
01400    if(info_ptr->row_pointers == NULL)
01401    {
01402       info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
01403          info_ptr->height * sizeof(png_bytep));
01404 #ifdef PNG_FREE_ME_SUPPORTED
01405       info_ptr->free_me |= PNG_FREE_ROWS;
01406 #endif
01407       for (row = 0; row < (int)info_ptr->height; row++)
01408       {
01409          info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
01410             png_get_rowbytes(png_ptr, info_ptr));
01411       }
01412    }
01413 
01414    png_read_image(png_ptr, info_ptr->row_pointers);
01415    info_ptr->valid |= PNG_INFO_IDAT;
01416 
01417    /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
01418    png_read_end(png_ptr, info_ptr);
01419 
01420    if(transforms == 0 || params == NULL)
01421       /* quiet compiler warnings */ return;
01422 
01423 }
01424 #endif