Back to index

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