pngread.c

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

Generated on Tue Jul 5 14:13:31 2011 for ROOT_528-00b_version by  doxygen 1.5.1