fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

png.c

Go to the documentation of this file.
00001 
00002 /* png.c - location for general purpose libpng functions
00003  *
00004  * Last changed in libpng 1.2.39 [August 13, 2009]
00005  * Copyright (c) 1998-2009 Glenn Randers-Pehrson
00006  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
00007  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
00008  *
00009  * This code is released under the libpng license.
00010  * For conditions of distribution and use, see the disclaimer
00011  * and license in png.h
00012  */
00013 
00014 #define PNG_INTERNAL
00015 #define PNG_NO_EXTERN
00016 #include "png.h"
00017 
00018 /* Generate a compiler error if there is an old png.h in the search path. */
00019 typedef version_1_2_40 Your_png_h_is_not_version_1_2_40;
00020 
00021 /* Version information for C files.  This had better match the version
00022  * string defined in png.h.  */
00023 
00024 #ifdef PNG_USE_GLOBAL_ARRAYS
00025 /* png_libpng_ver was changed to a function in version 1.0.5c */
00026 PNG_CONST char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
00027 
00028 #ifdef PNG_READ_SUPPORTED
00029 
00030 /* png_sig was changed to a function in version 1.0.5c */
00031 /* Place to hold the signature string for a PNG file. */
00032 PNG_CONST png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
00033 #endif /* PNG_READ_SUPPORTED */
00034 
00035 /* Invoke global declarations for constant strings for known chunk types */
00036 PNG_IHDR;
00037 PNG_IDAT;
00038 PNG_IEND;
00039 PNG_PLTE;
00040 PNG_bKGD;
00041 PNG_cHRM;
00042 PNG_gAMA;
00043 PNG_hIST;
00044 PNG_iCCP;
00045 PNG_iTXt;
00046 PNG_oFFs;
00047 PNG_pCAL;
00048 PNG_sCAL;
00049 PNG_pHYs;
00050 PNG_sBIT;
00051 PNG_sPLT;
00052 PNG_sRGB;
00053 PNG_tEXt;
00054 PNG_tIME;
00055 PNG_tRNS;
00056 PNG_zTXt;
00057 
00058 #ifdef PNG_READ_SUPPORTED
00059 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
00060 
00061 /* Start of interlace block */
00062 PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
00063 
00064 /* Offset to next interlace block */
00065 PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
00066 
00067 /* Start of interlace block in the y direction */
00068 PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
00069 
00070 /* Offset to next interlace block in the y direction */
00071 PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
00072 
00073 /* Height of interlace block.  This is not currently used - if you need
00074  * it, uncomment it here and in png.h
00075 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
00076 */
00077 
00078 /* Mask to determine which pixels are valid in a pass */
00079 PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
00080 
00081 /* Mask to determine which pixels to overwrite while displaying */
00082 PNG_CONST int FARDATA png_pass_dsp_mask[]
00083    = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
00084 
00085 #endif /* PNG_READ_SUPPORTED */
00086 #endif /* PNG_USE_GLOBAL_ARRAYS */
00087 
00088 /* Tells libpng that we have already handled the first "num_bytes" bytes
00089  * of the PNG file signature.  If the PNG data is embedded into another
00090  * stream we can set num_bytes = 8 so that libpng will not attempt to read
00091  * or write any of the magic bytes before it starts on the IHDR.
00092  */
00093 
00094 #ifdef PNG_READ_SUPPORTED
00095 void PNGAPI
00096 png_set_sig_bytes(png_structp png_ptr, int num_bytes)
00097 {
00098    if (png_ptr == NULL)
00099       return;
00100    png_debug(1, "in png_set_sig_bytes");
00101    if (num_bytes > 8)
00102       png_error(png_ptr, "Too many bytes for PNG signature.");
00103 
00104    png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
00105 }
00106 
00107 /* Checks whether the supplied bytes match the PNG signature.  We allow
00108  * checking less than the full 8-byte signature so that those apps that
00109  * already read the first few bytes of a file to determine the file type
00110  * can simply check the remaining bytes for extra assurance.  Returns
00111  * an integer less than, equal to, or greater than zero if sig is found,
00112  * respectively, to be less than, to match, or be greater than the correct
00113  * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
00114  */
00115 int PNGAPI
00116 png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
00117 {
00118    png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
00119    if (num_to_check > 8)
00120       num_to_check = 8;
00121    else if (num_to_check < 1)
00122       return (-1);
00123 
00124    if (start > 7)
00125       return (-1);
00126 
00127    if (start + num_to_check > 8)
00128       num_to_check = 8 - start;
00129 
00130    return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
00131 }
00132 
00133 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
00134 /* (Obsolete) function to check signature bytes.  It does not allow one
00135  * to check a partial signature.  This function might be removed in the
00136  * future - use png_sig_cmp().  Returns true (nonzero) if the file is PNG.
00137  */
00138 int PNGAPI
00139 png_check_sig(png_bytep sig, int num)
00140 {
00141   return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
00142 }
00143 #endif
00144 #endif /* PNG_READ_SUPPORTED */
00145 
00146 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
00147 /* Function to allocate memory for zlib and clear it to 0. */
00148 #ifdef PNG_1_0_X
00149 voidpf PNGAPI
00150 #else
00151 voidpf /* PRIVATE */
00152 #endif
00153 png_zalloc(voidpf png_ptr, uInt items, uInt size)
00154 {
00155    png_voidp ptr;
00156    png_structp p=(png_structp)png_ptr;
00157    png_uint_32 save_flags=p->flags;
00158    png_uint_32 num_bytes;
00159 
00160    if (png_ptr == NULL)
00161       return (NULL);
00162    if (items > PNG_UINT_32_MAX/size)
00163    {
00164      png_warning (p, "Potential overflow in png_zalloc()");
00165      return (NULL);
00166    }
00167    num_bytes = (png_uint_32)items * size;
00168 
00169    p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
00170    ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
00171    p->flags=save_flags;
00172 
00173 #if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
00174    if (ptr == NULL)
00175        return ((voidpf)ptr);
00176 
00177    if (num_bytes > (png_uint_32)0x8000L)
00178    {
00179       png_memset(ptr, 0, (png_size_t)0x8000L);
00180       png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
00181          (png_size_t)(num_bytes - (png_uint_32)0x8000L));
00182    }
00183    else
00184    {
00185       png_memset(ptr, 0, (png_size_t)num_bytes);
00186    }
00187 #endif
00188    return ((voidpf)ptr);
00189 }
00190 
00191 /* Function to free memory for zlib */
00192 #ifdef PNG_1_0_X
00193 void PNGAPI
00194 #else
00195 void /* PRIVATE */
00196 #endif
00197 png_zfree(voidpf png_ptr, voidpf ptr)
00198 {
00199    png_free((png_structp)png_ptr, (png_voidp)ptr);
00200 }
00201 
00202 /* Reset the CRC variable to 32 bits of 1's.  Care must be taken
00203  * in case CRC is > 32 bits to leave the top bits 0.
00204  */
00205 void /* PRIVATE */
00206 png_reset_crc(png_structp png_ptr)
00207 {
00208    png_ptr->crc = crc32(0, Z_NULL, 0);
00209 }
00210 
00211 /* Calculate the CRC over a section of data.  We can only pass as
00212  * much data to this routine as the largest single buffer size.  We
00213  * also check that this data will actually be used before going to the
00214  * trouble of calculating it.
00215  */
00216 void /* PRIVATE */
00217 png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
00218 {
00219    int need_crc = 1;
00220 
00221    if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */
00222    {
00223       if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
00224           (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
00225          need_crc = 0;
00226    }
00227    else                                                    /* critical */
00228    {
00229       if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
00230          need_crc = 0;
00231    }
00232 
00233    if (need_crc)
00234       png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
00235 }
00236 
00237 /* Allocate the memory for an info_struct for the application.  We don't
00238  * really need the png_ptr, but it could potentially be useful in the
00239  * future.  This should be used in favour of malloc(png_sizeof(png_info))
00240  * and png_info_init() so that applications that want to use a shared
00241  * libpng don't have to be recompiled if png_info changes size.
00242  */
00243 png_infop PNGAPI
00244 png_create_info_struct(png_structp png_ptr)
00245 {
00246    png_infop info_ptr;
00247 
00248    png_debug(1, "in png_create_info_struct");
00249    if (png_ptr == NULL)
00250       return (NULL);
00251 #ifdef PNG_USER_MEM_SUPPORTED
00252    info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
00253       png_ptr->malloc_fn, png_ptr->mem_ptr);
00254 #else
00255    info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
00256 #endif
00257    if (info_ptr != NULL)
00258       png_info_init_3(&info_ptr, png_sizeof(png_info));
00259 
00260    return (info_ptr);
00261 }
00262 
00263 /* This function frees the memory associated with a single info struct.
00264  * Normally, one would use either png_destroy_read_struct() or
00265  * png_destroy_write_struct() to free an info struct, but this may be
00266  * useful for some applications.
00267  */
00268 void PNGAPI
00269 png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
00270 {
00271    png_infop info_ptr = NULL;
00272    if (png_ptr == NULL)
00273       return;
00274 
00275    png_debug(1, "in png_destroy_info_struct");
00276    if (info_ptr_ptr != NULL)
00277       info_ptr = *info_ptr_ptr;
00278 
00279    if (info_ptr != NULL)
00280    {
00281       png_info_destroy(png_ptr, info_ptr);
00282 
00283 #ifdef PNG_USER_MEM_SUPPORTED
00284       png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
00285           png_ptr->mem_ptr);
00286 #else
00287       png_destroy_struct((png_voidp)info_ptr);
00288 #endif
00289       *info_ptr_ptr = NULL;
00290    }
00291 }
00292 
00293 /* Initialize the info structure.  This is now an internal function (0.89)
00294  * and applications using it are urged to use png_create_info_struct()
00295  * instead.
00296  */
00297 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
00298 #undef png_info_init
00299 void PNGAPI
00300 png_info_init(png_infop info_ptr)
00301 {
00302    /* We only come here via pre-1.0.12-compiled applications */
00303    png_info_init_3(&info_ptr, 0);
00304 }
00305 #endif
00306 
00307 void PNGAPI
00308 png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
00309 {
00310    png_infop info_ptr = *ptr_ptr;
00311 
00312    if (info_ptr == NULL)
00313       return;
00314 
00315    png_debug(1, "in png_info_init_3");
00316 
00317    if (png_sizeof(png_info) > png_info_struct_size)
00318    {
00319       png_destroy_struct(info_ptr);
00320       info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
00321       *ptr_ptr = info_ptr;
00322    }
00323 
00324    /* Set everything to 0 */
00325    png_memset(info_ptr, 0, png_sizeof(png_info));
00326 }
00327 
00328 #ifdef PNG_FREE_ME_SUPPORTED
00329 void PNGAPI
00330 png_data_freer(png_structp png_ptr, png_infop info_ptr,
00331    int freer, png_uint_32 mask)
00332 {
00333    png_debug(1, "in png_data_freer");
00334    if (png_ptr == NULL || info_ptr == NULL)
00335       return;
00336    if (freer == PNG_DESTROY_WILL_FREE_DATA)
00337       info_ptr->free_me |= mask;
00338    else if (freer == PNG_USER_WILL_FREE_DATA)
00339       info_ptr->free_me &= ~mask;
00340    else
00341       png_warning(png_ptr,
00342          "Unknown freer parameter in png_data_freer.");
00343 }
00344 #endif
00345 
00346 void PNGAPI
00347 png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
00348    int num)
00349 {
00350    png_debug(1, "in png_free_data");
00351    if (png_ptr == NULL || info_ptr == NULL)
00352       return;
00353 
00354 #if defined(PNG_TEXT_SUPPORTED)
00355    /* Free text item num or (if num == -1) all text items */
00356 #ifdef PNG_FREE_ME_SUPPORTED
00357    if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
00358 #else
00359    if (mask & PNG_FREE_TEXT)
00360 #endif
00361    {
00362       if (num != -1)
00363       {
00364          if (info_ptr->text && info_ptr->text[num].key)
00365          {
00366             png_free(png_ptr, info_ptr->text[num].key);
00367             info_ptr->text[num].key = NULL;
00368          }
00369       }
00370       else
00371       {
00372          int i;
00373          for (i = 0; i < info_ptr->num_text; i++)
00374              png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
00375          png_free(png_ptr, info_ptr->text);
00376          info_ptr->text = NULL;
00377          info_ptr->num_text=0;
00378       }
00379    }
00380 #endif
00381 
00382 #if defined(PNG_tRNS_SUPPORTED)
00383    /* Free any tRNS entry */
00384 #ifdef PNG_FREE_ME_SUPPORTED
00385    if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
00386 #else
00387    if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
00388 #endif
00389    {
00390       png_free(png_ptr, info_ptr->trans);
00391       info_ptr->trans = NULL;
00392       info_ptr->valid &= ~PNG_INFO_tRNS;
00393 #ifndef PNG_FREE_ME_SUPPORTED
00394       png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
00395 #endif
00396    }
00397 #endif
00398 
00399 #if defined(PNG_sCAL_SUPPORTED)
00400    /* Free any sCAL entry */
00401 #ifdef PNG_FREE_ME_SUPPORTED
00402    if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
00403 #else
00404    if (mask & PNG_FREE_SCAL)
00405 #endif
00406    {
00407 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
00408       png_free(png_ptr, info_ptr->scal_s_width);
00409       png_free(png_ptr, info_ptr->scal_s_height);
00410       info_ptr->scal_s_width = NULL;
00411       info_ptr->scal_s_height = NULL;
00412 #endif
00413       info_ptr->valid &= ~PNG_INFO_sCAL;
00414    }
00415 #endif
00416 
00417 #if defined(PNG_pCAL_SUPPORTED)
00418    /* Free any pCAL entry */
00419 #ifdef PNG_FREE_ME_SUPPORTED
00420    if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
00421 #else
00422    if (mask & PNG_FREE_PCAL)
00423 #endif
00424    {
00425       png_free(png_ptr, info_ptr->pcal_purpose);
00426       png_free(png_ptr, info_ptr->pcal_units);
00427       info_ptr->pcal_purpose = NULL;
00428       info_ptr->pcal_units = NULL;
00429       if (info_ptr->pcal_params != NULL)
00430          {
00431             int i;
00432             for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
00433             {
00434                png_free(png_ptr, info_ptr->pcal_params[i]);
00435                info_ptr->pcal_params[i]=NULL;
00436             }
00437             png_free(png_ptr, info_ptr->pcal_params);
00438             info_ptr->pcal_params = NULL;
00439          }
00440       info_ptr->valid &= ~PNG_INFO_pCAL;
00441    }
00442 #endif
00443 
00444 #if defined(PNG_iCCP_SUPPORTED)
00445    /* Free any iCCP entry */
00446 #ifdef PNG_FREE_ME_SUPPORTED
00447    if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
00448 #else
00449    if (mask & PNG_FREE_ICCP)
00450 #endif
00451    {
00452       png_free(png_ptr, info_ptr->iccp_name);
00453       png_free(png_ptr, info_ptr->iccp_profile);
00454       info_ptr->iccp_name = NULL;
00455       info_ptr->iccp_profile = NULL;
00456       info_ptr->valid &= ~PNG_INFO_iCCP;
00457    }
00458 #endif
00459 
00460 #if defined(PNG_sPLT_SUPPORTED)
00461    /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
00462 #ifdef PNG_FREE_ME_SUPPORTED
00463    if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
00464 #else
00465    if (mask & PNG_FREE_SPLT)
00466 #endif
00467    {
00468       if (num != -1)
00469       {
00470          if (info_ptr->splt_palettes)
00471          {
00472             png_free(png_ptr, info_ptr->splt_palettes[num].name);
00473             png_free(png_ptr, info_ptr->splt_palettes[num].entries);
00474             info_ptr->splt_palettes[num].name = NULL;
00475             info_ptr->splt_palettes[num].entries = NULL;
00476          }
00477       }
00478       else
00479       {
00480          if (info_ptr->splt_palettes_num)
00481          {
00482             int i;
00483             for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
00484                png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
00485 
00486             png_free(png_ptr, info_ptr->splt_palettes);
00487             info_ptr->splt_palettes = NULL;
00488             info_ptr->splt_palettes_num = 0;
00489          }
00490          info_ptr->valid &= ~PNG_INFO_sPLT;
00491       }
00492    }
00493 #endif
00494 
00495 #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
00496    if (png_ptr->unknown_chunk.data)
00497    {
00498       png_free(png_ptr, png_ptr->unknown_chunk.data);
00499       png_ptr->unknown_chunk.data = NULL;
00500    }
00501 
00502 #ifdef PNG_FREE_ME_SUPPORTED
00503    if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
00504 #else
00505    if (mask & PNG_FREE_UNKN)
00506 #endif
00507    {
00508       if (num != -1)
00509       {
00510           if (info_ptr->unknown_chunks)
00511           {
00512              png_free(png_ptr, info_ptr->unknown_chunks[num].data);
00513              info_ptr->unknown_chunks[num].data = NULL;
00514           }
00515       }
00516       else
00517       {
00518          int i;
00519 
00520          if (info_ptr->unknown_chunks_num)
00521          {
00522             for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
00523                png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
00524 
00525             png_free(png_ptr, info_ptr->unknown_chunks);
00526             info_ptr->unknown_chunks = NULL;
00527             info_ptr->unknown_chunks_num = 0;
00528          }
00529       }
00530    }
00531 #endif
00532 
00533 #if defined(PNG_hIST_SUPPORTED)
00534    /* Free any hIST entry */
00535 #ifdef PNG_FREE_ME_SUPPORTED
00536    if ((mask & PNG_FREE_HIST)  & info_ptr->free_me)
00537 #else
00538    if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
00539 #endif
00540    {
00541       png_free(png_ptr, info_ptr->hist);
00542       info_ptr->hist = NULL;
00543       info_ptr->valid &= ~PNG_INFO_hIST;
00544 #ifndef PNG_FREE_ME_SUPPORTED
00545       png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
00546 #endif
00547    }
00548 #endif
00549 
00550    /* Free any PLTE entry that was internally allocated */
00551 #ifdef PNG_FREE_ME_SUPPORTED
00552    if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
00553 #else
00554    if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
00555 #endif
00556    {
00557       png_zfree(png_ptr, info_ptr->palette);
00558       info_ptr->palette = NULL;
00559       info_ptr->valid &= ~PNG_INFO_PLTE;
00560 #ifndef PNG_FREE_ME_SUPPORTED
00561       png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
00562 #endif
00563       info_ptr->num_palette = 0;
00564    }
00565 
00566 #if defined(PNG_INFO_IMAGE_SUPPORTED)
00567    /* Free any image bits attached to the info structure */
00568 #ifdef PNG_FREE_ME_SUPPORTED
00569    if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
00570 #else
00571    if (mask & PNG_FREE_ROWS)
00572 #endif
00573    {
00574       if (info_ptr->row_pointers)
00575       {
00576          int row;
00577          for (row = 0; row < (int)info_ptr->height; row++)
00578          {
00579             png_free(png_ptr, info_ptr->row_pointers[row]);
00580             info_ptr->row_pointers[row]=NULL;
00581          }
00582          png_free(png_ptr, info_ptr->row_pointers);
00583          info_ptr->row_pointers=NULL;
00584       }
00585       info_ptr->valid &= ~PNG_INFO_IDAT;
00586    }
00587 #endif
00588 
00589 #ifdef PNG_FREE_ME_SUPPORTED
00590    if (num == -1)
00591       info_ptr->free_me &= ~mask;
00592    else
00593       info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
00594 #endif
00595 }
00596 
00597 /* This is an internal routine to free any memory that the info struct is
00598  * pointing to before re-using it or freeing the struct itself.  Recall
00599  * that png_free() checks for NULL pointers for us.
00600  */
00601 void /* PRIVATE */
00602 png_info_destroy(png_structp png_ptr, png_infop info_ptr)
00603 {
00604    png_debug(1, "in png_info_destroy");
00605 
00606    png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
00607 
00608 #if defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
00609    if (png_ptr->num_chunk_list)
00610    {
00611       png_free(png_ptr, png_ptr->chunk_list);
00612       png_ptr->chunk_list=NULL;
00613       png_ptr->num_chunk_list = 0;
00614    }
00615 #endif
00616 
00617    png_info_init_3(&info_ptr, png_sizeof(png_info));
00618 }
00619 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
00620 
00621 /* This function returns a pointer to the io_ptr associated with the user
00622  * functions.  The application should free any memory associated with this
00623  * pointer before png_write_destroy() or png_read_destroy() are called.
00624  */
00625 png_voidp PNGAPI
00626 png_get_io_ptr(png_structp png_ptr)
00627 {
00628    if (png_ptr == NULL)
00629       return (NULL);
00630    return (png_ptr->io_ptr);
00631 }
00632 
00633 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
00634 #if !defined(PNG_NO_STDIO)
00635 /* Initialize the default input/output functions for the PNG file.  If you
00636  * use your own read or write routines, you can call either png_set_read_fn()
00637  * or png_set_write_fn() instead of png_init_io().  If you have defined
00638  * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
00639  * necessarily available.
00640  */
00641 void PNGAPI
00642 png_init_io(png_structp png_ptr, png_FILE_p fp)
00643 {
00644    png_debug(1, "in png_init_io");
00645    if (png_ptr == NULL)
00646       return;
00647    png_ptr->io_ptr = (png_voidp)fp;
00648 }
00649 #endif
00650 
00651 #if defined(PNG_TIME_RFC1123_SUPPORTED)
00652 /* Convert the supplied time into an RFC 1123 string suitable for use in
00653  * a "Creation Time" or other text-based time string.
00654  */
00655 png_charp PNGAPI
00656 png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
00657 {
00658    static PNG_CONST char short_months[12][4] =
00659         {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
00660          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
00661 
00662    if (png_ptr == NULL)
00663       return (NULL);
00664    if (png_ptr->time_buffer == NULL)
00665    {
00666       png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
00667          png_sizeof(char)));
00668    }
00669 
00670 #if defined(_WIN32_WCE)
00671    {
00672       wchar_t time_buf[29];
00673       wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
00674           ptime->day % 32, short_months[(ptime->month - 1) % 12],
00675         ptime->year, ptime->hour % 24, ptime->minute % 60,
00676           ptime->second % 61);
00677       WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29,
00678           NULL, NULL);
00679    }
00680 #else
00681 #ifdef USE_FAR_KEYWORD
00682    {
00683       char near_time_buf[29];
00684       png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
00685           ptime->day % 32, short_months[(ptime->month - 1) % 12],
00686           ptime->year, ptime->hour % 24, ptime->minute % 60,
00687           ptime->second % 61);
00688       png_memcpy(png_ptr->time_buffer, near_time_buf,
00689           29*png_sizeof(char));
00690    }
00691 #else
00692    png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
00693        ptime->day % 32, short_months[(ptime->month - 1) % 12],
00694        ptime->year, ptime->hour % 24, ptime->minute % 60,
00695        ptime->second % 61);
00696 #endif
00697 #endif /* _WIN32_WCE */
00698    return ((png_charp)png_ptr->time_buffer);
00699 }
00700 #endif /* PNG_TIME_RFC1123_SUPPORTED */
00701 
00702 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
00703 
00704 png_charp PNGAPI
00705 png_get_copyright(png_structp png_ptr)
00706 {
00707    png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */
00708    return ((png_charp) "\n libpng version 1.2.40 - September 10, 2009\n\
00709    Copyright (c) 1998-2009 Glenn Randers-Pehrson\n\
00710    Copyright (c) 1996-1997 Andreas Dilger\n\
00711    Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n");
00712 }
00713 
00714 /* The following return the library version as a short string in the
00715  * format 1.0.0 through 99.99.99zz.  To get the version of *.h files
00716  * used with your application, print out PNG_LIBPNG_VER_STRING, which
00717  * is defined in png.h.
00718  * Note: now there is no difference between png_get_libpng_ver() and
00719  * png_get_header_ver().  Due to the version_nn_nn_nn typedef guard,
00720  * it is guaranteed that png.c uses the correct version of png.h.
00721  */
00722 png_charp PNGAPI
00723 png_get_libpng_ver(png_structp png_ptr)
00724 {
00725    /* Version of *.c files used when building libpng */
00726    png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */
00727    return ((png_charp) PNG_LIBPNG_VER_STRING);
00728 }
00729 
00730 png_charp PNGAPI
00731 png_get_header_ver(png_structp png_ptr)
00732 {
00733    /* Version of *.h files used when building libpng */
00734    png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */
00735    return ((png_charp) PNG_LIBPNG_VER_STRING);
00736 }
00737 
00738 png_charp PNGAPI
00739 png_get_header_version(png_structp png_ptr)
00740 {
00741    /* Returns longer string containing both version and date */
00742    png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */
00743    return ((png_charp) PNG_HEADER_VERSION_STRING
00744 #ifndef PNG_READ_SUPPORTED
00745    "     (NO READ SUPPORT)"
00746 #endif
00747    "\n");
00748 }
00749 
00750 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
00751 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
00752 int PNGAPI
00753 png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
00754 {
00755    /* Check chunk_name and return "keep" value if it's on the list, else 0 */
00756    int i;
00757    png_bytep p;
00758    if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
00759       return 0;
00760    p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
00761    for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
00762       if (!png_memcmp(chunk_name, p, 4))
00763         return ((int)*(p + 4));
00764    return 0;
00765 }
00766 #endif
00767 
00768 /* This function, added to libpng-1.0.6g, is untested. */
00769 int PNGAPI
00770 png_reset_zstream(png_structp png_ptr)
00771 {
00772    if (png_ptr == NULL)
00773       return Z_STREAM_ERROR;
00774    return (inflateReset(&png_ptr->zstream));
00775 }
00776 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
00777 
00778 /* This function was added to libpng-1.0.7 */
00779 png_uint_32 PNGAPI
00780 png_access_version_number(void)
00781 {
00782    /* Version of *.c files used when building libpng */
00783    return((png_uint_32) PNG_LIBPNG_VER);
00784 }
00785 
00786 
00787 #if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
00788 #if !defined(PNG_1_0_X)
00789 /* This function was added to libpng 1.2.0 */
00790 int PNGAPI
00791 png_mmx_support(void)
00792 {
00793    /* Obsolete, to be removed from libpng-1.4.0 */
00794     return -1;
00795 }
00796 #endif /* PNG_1_0_X */
00797 #endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
00798 
00799 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
00800 #ifdef PNG_SIZE_T
00801 /* Added at libpng version 1.2.6 */
00802    PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
00803 png_size_t PNGAPI
00804 png_convert_size(size_t size)
00805 {
00806    if (size > (png_size_t)-1)
00807       PNG_ABORT();  /* We haven't got access to png_ptr, so no png_error() */
00808    return ((png_size_t)size);
00809 }
00810 #endif /* PNG_SIZE_T */
00811 
00812 /* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
00813 #if defined(PNG_cHRM_SUPPORTED)
00814 #if !defined(PNG_NO_CHECK_cHRM)
00815 
00816 /*
00817  *    Multiply two 32-bit numbers, V1 and V2, using 32-bit
00818  *    arithmetic, to produce a 64 bit result in the HI/LO words.
00819  *
00820  *                  A B
00821  *                x C D
00822  *               ------
00823  *              AD || BD
00824  *        AC || CB || 0
00825  *
00826  *    where A and B are the high and low 16-bit words of V1,
00827  *    C and D are the 16-bit words of V2, AD is the product of
00828  *    A and D, and X || Y is (X << 16) + Y.
00829 */
00830 
00831 void /* PRIVATE */
00832 png_64bit_product (long v1, long v2, unsigned long *hi_product,
00833    unsigned long *lo_product)
00834 {
00835    int a, b, c, d;
00836    long lo, hi, x, y;
00837 
00838    a = (v1 >> 16) & 0xffff;
00839    b = v1 & 0xffff;
00840    c = (v2 >> 16) & 0xffff;
00841    d = v2 & 0xffff;
00842 
00843    lo = b * d;                   /* BD */
00844    x = a * d + c * b;            /* AD + CB */
00845    y = ((lo >> 16) & 0xffff) + x;
00846 
00847    lo = (lo & 0xffff) | ((y & 0xffff) << 16);
00848    hi = (y >> 16) & 0xffff;
00849 
00850    hi += a * c;                  /* AC */
00851 
00852    *hi_product = (unsigned long)hi;
00853    *lo_product = (unsigned long)lo;
00854 }
00855 
00856 int /* PRIVATE */
00857 png_check_cHRM_fixed(png_structp png_ptr,
00858    png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
00859    png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
00860    png_fixed_point blue_x, png_fixed_point blue_y)
00861 {
00862    int ret = 1;
00863    unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
00864 
00865    png_debug(1, "in function png_check_cHRM_fixed");
00866    if (png_ptr == NULL)
00867       return 0;
00868 
00869    if (white_x < 0 || white_y <= 0 ||
00870          red_x < 0 ||   red_y <  0 ||
00871        green_x < 0 || green_y <  0 ||
00872         blue_x < 0 ||  blue_y <  0)
00873    {
00874       png_warning(png_ptr,
00875         "Ignoring attempt to set negative chromaticity value");
00876       ret = 0;
00877    }
00878    if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
00879        white_y > (png_fixed_point) PNG_UINT_31_MAX ||
00880          red_x > (png_fixed_point) PNG_UINT_31_MAX ||
00881          red_y > (png_fixed_point) PNG_UINT_31_MAX ||
00882        green_x > (png_fixed_point) PNG_UINT_31_MAX ||
00883        green_y > (png_fixed_point) PNG_UINT_31_MAX ||
00884         blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
00885         blue_y > (png_fixed_point) PNG_UINT_31_MAX )
00886    {
00887       png_warning(png_ptr,
00888         "Ignoring attempt to set chromaticity value exceeding 21474.83");
00889       ret = 0;
00890    }
00891    if (white_x > 100000L - white_y)
00892    {
00893       png_warning(png_ptr, "Invalid cHRM white point");
00894       ret = 0;
00895    }
00896    if (red_x > 100000L - red_y)
00897    {
00898       png_warning(png_ptr, "Invalid cHRM red point");
00899       ret = 0;
00900    }
00901    if (green_x > 100000L - green_y)
00902    {
00903       png_warning(png_ptr, "Invalid cHRM green point");
00904       ret = 0;
00905    }
00906    if (blue_x > 100000L - blue_y)
00907    {
00908       png_warning(png_ptr, "Invalid cHRM blue point");
00909       ret = 0;
00910    }
00911 
00912    png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
00913    png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
00914 
00915    if (xy_hi == yx_hi && xy_lo == yx_lo)
00916    {
00917       png_warning(png_ptr,
00918          "Ignoring attempt to set cHRM RGB triangle with zero area");
00919       ret = 0;
00920    }
00921 
00922    return ret;
00923 }
00924 #endif /* NO_PNG_CHECK_cHRM */
00925 #endif /* PNG_cHRM_SUPPORTED */
00926 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */