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


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:56