home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Library / include / pngdecod.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-17  |  4.6 KB  |  175 lines

  1. #ifndef __PNGDECOD_H
  2. #define __PNGDECOD_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 Decoder class definition
  17. //
  18. //  Author:  John M. Miano  miano@colosseumbuilders.com
  19. //
  20. //  Description:
  21. //
  22. //    This class is a decoder for PNG images
  23. //
  24.  
  25. #include "bitimage.h"
  26. #include "grexcept.h"
  27. #include "png.h"
  28.  
  29. class PngHuffmanDecoder ;
  30.  
  31. class PngDecoder : public BitmapImageDecoder
  32. {
  33. public:
  34.   PngDecoder () ;
  35.   PngDecoder (const PngDecoder &) ;
  36.   virtual ~PngDecoder () ;
  37.   PngDecoder &operator=(PngDecoder &) ;
  38.  
  39.   virtual void ReadImage (std::istream &, BitmapImage &) ;
  40.  
  41.   void SetVerbose (bool) ;
  42.   bool GetVerbose () const ;
  43.  
  44. private:
  45.   void Initialize () ;
  46.   void DoCopy (const PngDecoder &) ;
  47.  
  48.   struct InterlaceInfo
  49.   {
  50.     unsigned int row_interval ;
  51.     unsigned int col_interval ;
  52.     unsigned int start_row ;
  53.     unsigned int start_col ;
  54.   } ;
  55.  
  56.   // Function to read the PNG signature.
  57.   void ReadSignature () ;
  58.   // Function to read a single PNG chunk
  59.   void ReadChunk () ;
  60.  
  61.   // Functions for processing chunks
  62.   void ProcessHeader () ;
  63.   void ProcessPalette () ;
  64.   void ProcessBackground () ;
  65.   void ProcessGamma () ;
  66.   void ProcessData () ;
  67.   void ProcessChromaticities () ;
  68.   void ProcessHistogram () ;
  69.   void ProcessPhysicalPixelDimensions () ;
  70.   void ProcessSignificantBits () ;
  71.   void ProcessTextualData () ;
  72.   void ProcessImageTime () ;
  73.   void ProcessTransparency () ;
  74.   void ProcessCompressedText () ;
  75.  
  76.   void CallProgressFunction (unsigned int percent) ;
  77.  
  78.   // Raw Input Functions
  79.   int GetBits (unsigned int count=1) ;
  80.   UBYTE1 GetIDATByte () ;
  81.  
  82.   // Deflate Functions
  83.   UBYTE1 DecodeByte () ;
  84.   void StartNewDataSet () ;
  85.   void CheckAdler () ;
  86.   void ReadLengths (PngHuffmanDecoder &, unsigned int [], unsigned int) ;
  87.   UBYTE1 DecodeLiteralByte () ;
  88.   UBYTE1 DecodeCompressedByte () ;
  89.  
  90.   void FreeData () ;
  91.  
  92.   // Function for reading pixel data for the image.
  93.   void ReadPixelData () ;
  94.   void CopyNoninterlacedRowToImage (unsigned int row);
  95.   void CopyInterlacedRowToImage (unsigned int row, unsigned int width) ;
  96.   void ReadNoninterlaced () ;
  97.   void ReadInterlaced () ;
  98.   void FilterRow (unsigned int filter) ;
  99.  
  100.   // Background Color Values
  101.   UBYTE1 background_red ;
  102.   UBYTE1 background_green ;
  103.   UBYTE1 background_blue ;
  104.   UBYTE1 background_gray ;
  105.  
  106.   // Current chunk information
  107.   UBYTE4 chunk_length ;
  108.   UBYTE4 chunk_type ;
  109.   UBYTE1 *chunk_data ;
  110.   UBYTE4 chunk_data_size ;
  111.   UBYTE4 chunk_crc ;
  112.  
  113.   // Deflate State Variables
  114.   UBYTE4 stream_adler ;
  115.   UBYTE1 *lz_window ;
  116.   unsigned int window_position ;
  117.   unsigned int copy_position ;
  118.   unsigned int copy_count ;
  119.   PngHuffmanDecoder *literal_table ;
  120.   PngHuffmanDecoder *distance_table ;
  121.   bool final_data_set ;        // Final data set read
  122.   bool literal_mode ;          // true => Reading a literal stream
  123.   unsigned int literal_count ; // Number remaining literal bytes
  124.  
  125.   std::istream *input_stream ;
  126.   BitmapImage *current_image ;
  127.   bool verbose_flag ;
  128.   unsigned int palette_size ;
  129.   bool reading_pixel_data ;  // Set to true while reading IDAT blocks
  130.  
  131.   // IDAT read state
  132.   unsigned int bit_offset ;    // Bit offset into current byte.
  133.   unsigned int byte_offset ;   // Byte offset into current IDAT chunk
  134.   UBYTE1 bit_buffer ;          // IDAT read state
  135.  
  136.   // Image information from the header
  137.   UBYTE4 image_height ;
  138.   UBYTE4 image_width ;
  139.   UBYTE1 image_depth ;
  140.   unsigned int image_color_type ;
  141.   UBYTE1 image_compression_method ;
  142.   UBYTE1 image_filter_method ;
  143.   UBYTE1 image_interlace_method ;
  144.  
  145.   // Row input buffers
  146.   UBYTE1 *row_buffers [2] ;
  147.   int current_row_buffer ;
  148.   unsigned int row_buffer_width ;
  149.  
  150.   unsigned int interlace_pass ;  // Current interlace pass
  151.   double file_gamma ;  // Value from a gAMAchunk
  152.  
  153.   bool data_read ; // False until IDAT chunk read.
  154.   bool palette_read ; // False until PLTE chunk read.
  155.   bool header_read ; // False until IHDR chunk read.
  156.   bool end_read ; // False until IEND chunk read.
  157.  
  158.   static const InterlaceInfo interlace_values [] ;
  159.  
  160.   friend class PngHuffmanDecoder ;
  161. } ;
  162.  
  163. inline void PngDecoder::SetVerbose (bool value)
  164. {
  165.   verbose_flag = value ;
  166.   return ;
  167. }
  168.  
  169. inline bool PngDecoder::GetVerbose () const
  170. {
  171.   return verbose_flag ;
  172. }
  173.  
  174. #endif
  175.