home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c13 / inc / pngpvt.h
Encoding:
C/C++ Source or Header  |  1999-04-01  |  4.1 KB  |  185 lines

  1. #ifndef __PNGPVT_H
  2. #define __PNGPVT_H
  3. //
  4. // Copyright (c) 1997,1998 Colosseum Builders, Inc.
  5. // All rights reserved.
  6. //
  7. // Colosseum Builders, Inc. makes no warranty, expressed or implied
  8. // with regards to this software. It is provided as is.
  9. //
  10. // See the README.TXT file that came with this software for restrictions
  11. // on the use and redistribution of this file or send E-mail to
  12. // info@colosseumbuilders.com
  13. //
  14.  
  15. //
  16. //  Title:  PNG Encoder/Decoder private definitions
  17. //
  18. //  Author:  John M. Miano  miano@colosseumbuilders.com
  19. //
  20.  
  21.  
  22. #include <string>
  23.  
  24. #include "datatype.h"
  25.  
  26. // Length of the PNG signature.
  27. const int PngSignatureSize = 8 ;
  28. extern const UBYTE1 PngSignature [PngSignatureSize] ;
  29.  
  30. const unsigned int PngLengthSize = 19 ;
  31. extern const UBYTE1 PngLengthOrder [PngLengthSize] ;
  32.  
  33. const unsigned int PngMaxLengthCodes = 288 ;
  34. const unsigned int PngMaxDistanceCodes = 30 ;
  35. const unsigned int PngMaxNumberOfHuffmanCodes = PngMaxLengthCodes ;
  36. const unsigned int PngFirstLengthCode = 257 ;
  37. const unsigned int PngEndCode = 256 ;
  38.  
  39. // Maximum Huffman Code Sizes
  40. const unsigned int PngMaxLengthLengthCodeSize = 7 ;
  41. const unsigned int PngMaxDistanceCodeSize = 15 ;
  42. const unsigned int PngMaxLengthCodeSize = 15 ;
  43. enum PngColorType
  44. {
  45.   Grayscale = 0,
  46.   RGB = 2,
  47.   Palette = 3,
  48.   GrayscaleAlpha = 4,
  49.   RGBAlpha = 6,
  50. } ;
  51.  
  52.   // Filter type defintiions
  53. enum  PngFilterType
  54. {
  55.   FilterNone = 0,
  56.   FilterSub = 1,
  57.   FilterUp = 2,
  58.   FilterAverage  = 3,
  59.   FilterPaeth = 4,
  60. } ;
  61.  
  62.  
  63.  
  64. // Physical layout of the IDAT header.
  65. #pragma pack (1)
  66. #pragma option -a1
  67. struct PngImageHeader
  68. {
  69.   UBYTE4 width ;
  70.   UBYTE4 height ;
  71.   UBYTE1 bitdepth ;
  72.   UBYTE1 colortype ;
  73.   UBYTE1 compressionmethod ;
  74.   UBYTE1 filtermethod ;
  75.   UBYTE1 interlacemethod ;
  76. } ;
  77. #pragma option -a.
  78.  
  79. // Physical Layout of a cHRM chunk
  80. #pragma pack (1)
  81. #pragma option -a1
  82. struct PngChromaticitiesData
  83. {
  84.   UBYTE4 whitepointx ;
  85.   UBYTE4 whitepointy ;
  86.   UBYTE4 redx ;
  87.   UBYTE4 redy ;
  88.   UBYTE4 greenx ;
  89.   UBYTE4 greeny ;
  90.   UBYTE4 bluex ;
  91.   UBYTE4 bluey ;
  92. } ;
  93. #pragma option -a.
  94.  
  95. // Physical layout of a pPYs chucnk
  96. #pragma pack (1)
  97. #pragma option -a1
  98. struct PngPixelDimensions
  99. {
  100.   UBYTE4 pixelsx ;
  101.   UBYTE4 pixelsy ;
  102.   UBYTE1 unit ;
  103. } ;
  104. #pragma option -a.
  105.  
  106. // Physical layout of a tIME chunk
  107. #pragma pack (1)
  108. #pragma option -a1
  109. struct PngTimeData
  110. {
  111.   UBYTE2 year ;
  112.   UBYTE1 month ;
  113.   UBYTE1 day ;
  114.   UBYTE1 hour ;
  115.   UBYTE1 minute ;
  116.   UBYTE1 second ;
  117. } ;
  118. #pragma option -a.
  119.  
  120. const unsigned int PngWindowSize = (1 << 15) ;
  121. const unsigned int PngWindowMask = PngWindowSize - 1 ;
  122.  
  123. const unsigned int PngLongestLength = 258 ;
  124.  
  125.  
  126. static inline const char *Binary (int vv, int ll)
  127. {
  128.   static const char digit [2] = { '0', '1' } ;
  129.   static char value [33] ;
  130.   value [0] = '\000' ;
  131.   for (unsigned int ii = ll , jj = 0 ; ii > 0 ; -- ii, ++ jj)
  132.   {
  133.     value [jj] = digit [(vv & (1 << (ii-1))) != 0 ] ;
  134.     value [jj+1] = 0 ;
  135.   }
  136.   return value ;
  137. }
  138.  
  139. //
  140. //  Description:
  141. //    Path predictor function defined in section 6.6 of the PNG standard.
  142. //
  143. //  Parameters:
  144. //    left:  The pixel value for the value to the left of the current pixel.
  145. //    above: The value for the pixel above the current pixel.
  146. //    upperleft: The value for the pixel diagonally above and to the right
  147. //                of the current pixel.
  148. //
  149. static inline unsigned int  PaethPredictor (UBYTE1 left,
  150.                                             UBYTE1 above,
  151.                                             UBYTE1 upperleft)
  152. {
  153.   int pp = left + above - upperleft ;
  154.   int pa, pb, pc ;
  155.   if (pp > left)
  156.     pa = pp - left ;
  157.   else
  158.     pa = left - pp ;
  159.   if (pp > above)
  160.     pb = pp - above ;
  161.   else
  162.     pb = above - pp ;
  163.   if (pp > upperleft)
  164.     pc = pp - upperleft ;
  165.   else
  166.     pc = upperleft - pp ;
  167.  
  168.   if (pa <= pb && pa <= pc)
  169.     return left ;
  170.   else if (pb <= pc)
  171.     return above ;
  172.   else
  173.     return upperleft ;
  174. }
  175.  
  176.  
  177. inline UBYTE4 ChunkType (const BYTE1 type [5]) 
  178. {
  179.   UBYTE4 value = type [0] | (type [1] << 8) 
  180.                | (type [2] << 16) | (type [3] << 24) ;
  181.   return value ;
  182. }
  183.  
  184. #endif
  185.