Back to index

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