png.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:6k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /* png.c - location for general purpose png functions
  2. libpng 1.0 beta 2 - version 0.87
  3.    For conditions of distribution and use, see copyright notice in png.h
  4. Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  5.    January 15, 1996
  6.    */
  7. #define PNG_INTERNAL
  8. #define PNG_NO_EXTERN
  9. #include "png.h"
  10. /* version information for c files.  This better match the version
  11.    string defined in png.h */
  12. char png_libpng_ver[] = "0.87";
  13. /* place to hold the signiture string for a png file. */
  14. png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  15. /* constant strings for known chunk types.  If you need to add a chunk,
  16.    add a string holding the name here.  If you want to make the code
  17.    portable to EBCDIC machines, use ASCII numbers, not characters. */
  18. png_byte FARDATA png_IHDR[4] = { 73,  72,  68,  82};
  19. png_byte FARDATA png_IDAT[4] = { 73,  68,  65,  84};
  20. png_byte FARDATA png_IEND[4] = { 73,  69,  78,  68};
  21. png_byte FARDATA png_PLTE[4] = { 80,  76,  84,  69};
  22. #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
  23. png_byte FARDATA png_gAMA[4] = {103,  65,  77,  65};
  24. #endif
  25. #if defined(PNG_READ_sBIT_SUPPORTED) || defined(PNG_WRITE_sBIT_SUPPORTED)
  26. png_byte FARDATA png_sBIT[4] = {115,  66,  73,  84};
  27. #endif
  28. #if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
  29. png_byte FARDATA png_cHRM[4] = { 99,  72,  82,  77};
  30. #endif
  31. #if defined(PNG_READ_tRNS_SUPPORTED) || defined(PNG_WRITE_tRNS_SUPPORTED)
  32. png_byte FARDATA png_tRNS[4] = {116,  82,  78,  83};
  33. #endif
  34. #if defined(PNG_READ_bKGD_SUPPORTED) || defined(PNG_WRITE_bKGD_SUPPORTED)
  35. png_byte FARDATA png_bKGD[4] = { 98,  75,  71,  68};
  36. #endif
  37. #if defined(PNG_READ_hIST_SUPPORTED) || defined(PNG_WRITE_hIST_SUPPORTED)
  38. png_byte FARDATA png_hIST[4] = {104,  73,  83,  84};
  39. #endif
  40. #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED)
  41. png_byte FARDATA png_tEXt[4] = {116,  69,  88, 116};
  42. #endif
  43. #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
  44. png_byte FARDATA png_zTXt[4] = {122,  84,  88, 116};
  45. #endif
  46. #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
  47. png_byte FARDATA png_pHYs[4] = {112,  72,  89, 115};
  48. #endif
  49. #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
  50. png_byte FARDATA png_oFFs[4] = {111,  70,  70, 115};
  51. #endif
  52. #if defined(PNG_READ_tIME_SUPPORTED) || defined(PNG_WRITE_tIME_SUPPORTED)
  53. png_byte FARDATA png_tIME[4] = {116,  73,  77,  69};
  54. #endif
  55. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  56. /* start of interlace block */
  57. int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  58. /* offset to next interlace block */
  59. int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  60. /* start of interlace block in the y direction */
  61. int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  62. /* offset to next interlace block in the y direction */
  63. int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  64. /* width of interlace block */
  65. /* this is not currently used - if you need it, uncomment it here and
  66.    in png.h
  67. int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
  68. */
  69. /* height of interlace block */
  70. /* this is not currently used - if you need it, uncomment it here and
  71.    in png.h
  72. int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
  73. */
  74. /* mask to determine which pixels are valid in a pass */
  75. int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  76. /* mask to determine which pixels to overwrite while displaying */
  77. int FARDATA png_pass_dsp_mask[] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
  78. int
  79. png_check_sig(png_bytep sig, int num)
  80. {
  81. if (num > 8)
  82.       num = 8;
  83.    if (num < 1)
  84.       return 0;
  85.    return (!png_memcmp(sig, png_sig, num));
  86. }
  87. /* Function to allocate memory for zlib. */
  88. voidpf
  89. png_zalloc(voidpf png_ptr, uInt items, uInt size)
  90. {
  91. png_voidp ptr;
  92. png_uint_32 num_bytes;
  93. ptr = png_large_malloc((png_structp)png_ptr,
  94. (png_uint_32)items * (png_uint_32)size);
  95. num_bytes = (png_uint_32)items * (png_uint_32)size;
  96. if (num_bytes > (png_uint_32)0x7fff)
  97. {
  98. png_memset(ptr, 0, (png_size_t)0x8000L);
  99. png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
  100. (png_size_t)(num_bytes - (png_uint_32)0x8000L));
  101. }
  102. else
  103. {
  104. png_memset(ptr, 0, (png_size_t)num_bytes);
  105. }
  106.    return (voidpf)(ptr);
  107. }
  108. /* function to free memory for zlib */
  109. void
  110. png_zfree(voidpf png_ptr, voidpf ptr)
  111. {
  112. png_large_free((png_structp)png_ptr, (png_voidp)ptr);
  113. }
  114. /* reset the crc variable to 32 bits of 1's.  Care must be taken
  115.    in case crc is > 32 bits to leave the top bits 0 */
  116. void
  117. png_reset_crc(png_structp png_ptr)
  118. {
  119.    /* set crc to all 1's */
  120.    png_ptr->crc = 0xffffffffL;
  121. }
  122. /* Note: the crc code below was copied from the sample code in the
  123. PNG spec, with appropriate modifications made to ensure the
  124. variables are large enough */
  125. /* table of crc's of all 8-bit messages.  If you wish to png_malloc this
  126.    table, turn this into a pointer, and png_malloc it in make_crc_table().
  127.    You may then want to hook it into png_struct and free it with the
  128.    destroy functions. */
  129. static png_uint_32 crc_table[256];
  130. /* Flag: has the table been computed? Initially false. */
  131. static int crc_table_computed = 0;
  132. /* make the table for a fast crc */
  133. static void
  134. make_crc_table(void)
  135. {
  136.   png_uint_32 c;
  137.   int n, k;
  138.   for (n = 0; n < 256; n++)
  139.   {
  140.    c = (png_uint_32)n;
  141.    for (k = 0; k < 8; k++)
  142.      c = c & 1 ? 0xedb88320L ^ (c >> 1) : c >> 1;
  143.    crc_table[n] = c;
  144.   }
  145.   crc_table_computed = 1;
  146. }
  147. /* update a running crc with the bytes buf[0..len-1]--the crc should be
  148.    initialized to all 1's, and the transmitted value is the 1's complement
  149.    of the final running crc. */
  150. static png_uint_32
  151. update_crc(png_uint_32 crc, png_bytep buf, png_uint_32 len)
  152. {
  153.   png_uint_32 c;
  154.   png_bytep p;
  155.   png_uint_32 n;
  156.   c = crc;
  157.   p = buf;
  158.   n = len;
  159.   if (!crc_table_computed)
  160.   {
  161.    make_crc_table();
  162.   }
  163.   if (n > 0) do
  164.   {
  165. c = crc_table[(png_byte)((c ^ (*p++)) & 0xff)] ^ (c >> 8);
  166.   } while (--n);
  167.   return c;
  168. }
  169. /* calculate the crc over a section of data.  Note that while we
  170.    are passing in a 32 bit value for length, on 16 bit machines, you
  171.    would need to use huge pointers to access all that data.  If you
  172.    need this, put huge here and above. */
  173. void
  174. png_calculate_crc(png_structp png_ptr, png_bytep ptr,
  175.    png_uint_32 length)
  176. {
  177.    png_ptr->crc = update_crc(png_ptr->crc, ptr, length);
  178. }
  179. void
  180. png_info_init(png_infop info)
  181. {
  182.    /* set everything to 0 */
  183. png_memset(info, 0, sizeof (png_info));
  184. }