home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c15 / inc / pngencod.h next >
Encoding:
C/C++ Source or Header  |  1998-12-17  |  7.3 KB  |  315 lines

  1. #ifndef __PNGENCOD_H
  2. #define __PNGENCOD_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 Class Definition
  17. //
  18. //  Author:  John M. Miano  miano@colosseumbuilders.com
  19. //
  20. //  Description:
  21. //
  22. //    This class implements PNG encoding.
  23. //
  24.  
  25.  
  26.  
  27. #include "png.h"
  28. #include "bitimage.h"
  29.  
  30. class PngHuffmanEncoder ;
  31.  
  32. class PngEncoder : public BitmapImageEncoder
  33. {
  34. public:
  35.   PngEncoder () ;
  36.   PngEncoder (const PngEncoder &) ;
  37.   virtual ~PngEncoder () ;
  38.   PngEncoder &operator=(const PngEncoder &) ;
  39.  
  40.   void WriteImage (std::ostream &, BitmapImage &) ;
  41.  
  42.   // Property functions for predefined tEXt strings.
  43.   void SetTitle (const std::string &) ;
  44.   std::string GetTitle () const ;
  45.  
  46.   void SetAuthor (const std::string &) ;
  47.   std::string GetAuthor () const ;
  48.  
  49.   void SetDescription (const std::string &) ;
  50.   std::string GetDescription () const ;
  51.  
  52.   void SetCopyright (const std::string &) ;
  53.   std::string GetCopyright () const ;
  54.  
  55.   void SetSoftware (const std::string &) ;
  56.   std::string GetSoftware () const ;
  57.  
  58.   void SetDisclaimer (const std::string &) ;
  59.   std::string GetDisclaimer () const ;
  60.  
  61.   void SetWarning (const std::string &) ;
  62.   std::string GetWarning () const ;
  63.  
  64.   void SetSource (const std::string &) ;
  65.   std::string GetSource () const ;
  66.  
  67.   void SetComment (const std::string &) ;
  68.   std::string GetComment () const ;
  69.  
  70.   enum CompressionLevel
  71.   {
  72.     FastestCompression = 0,
  73.     FastCompression = 1,
  74.     DefaultCompression = 2,
  75.     MaximumCompression = 3,
  76.   } ;
  77.  
  78.   void SetCompressionLevel (CompressionLevel) ;
  79.   CompressionLevel GetCompressionLevel () const ;
  80.     
  81.   void SetUseFilters (bool) ;
  82.   bool GetUseFilters () const ;
  83.  
  84.   unsigned long GetBlockSize () const ;
  85.   void SetBlockSize (unsigned long) ;
  86.  
  87. protected:
  88.   void Initialize () ;
  89.   void DoCopy (const PngEncoder &) ;
  90.  
  91. private:
  92.   struct HashEntry
  93.   {
  94.     UBYTE2 index ;      // Index into the LZ Window
  95.     HashEntry *next ;   // Next collision entry
  96.     HashEntry *previous ;
  97.   } ;
  98.                    
  99.   void LongestMatch (unsigned int &length,
  100.                      unsigned int &offset) ;
  101.   unsigned int HashValue (unsigned int) ;
  102.  
  103.   void MoveHashEntry (unsigned int entry, unsigned int hashvalue) ;
  104.  
  105.   HashEntry *hash_values ;
  106.   HashEntry *hash_table ;
  107.  
  108.   BitmapImage *current_image ;
  109.   std::ostream *output_stream ;
  110.  
  111.   unsigned int search_limit ;
  112.  
  113.   PngHuffmanEncoder *distance_table ;
  114.   PngHuffmanEncoder *length_table ;
  115.   PngHuffmanEncoder *length_length_table ;
  116.  
  117.   typedef void (PngEncoder::*LENGTHFUNCTION) (unsigned int index,
  118.                                               unsigned int code,
  119.                                               unsigned int length,
  120.                                               unsigned int extra) ;
  121.   void GatherLengthCounts (unsigned int,
  122.                            unsigned int code,
  123.                            unsigned int,
  124.                            unsigned int) ;
  125.   void OutputLengthCounts (unsigned int index,
  126.                            unsigned int code,
  127.                            unsigned int extra,
  128.                            unsigned int value) ;
  129.  
  130.   void FindLengthCodes (LENGTHFUNCTION, UBYTE1 lengths [], unsigned int count) ;
  131.  
  132.   bool ProcessImageData () ;
  133.  
  134.   UBYTE1 *output_buffer ;
  135.   UBYTE4 chunk_type ;
  136.   UBYTE4 byte_position ;
  137.   unsigned int bit_position ;
  138.   void StartChunk (const char type [5]) ;
  139.   void WriteCurrentChunk () ;
  140.   void OutputBlock (const void *data, unsigned int length) ;
  141.   void OutputByte (UBYTE1) ;
  142.   void OutputDataBits (unsigned int data, unsigned int length) ;
  143.   void FlushBitBuffer () ;
  144.   void OutputAdler () ;
  145.  
  146.   void FreeBuffers () ;
  147.   void FillBuffer (unsigned int count) ;
  148.  
  149.   unsigned int row_width ;
  150.   void WriteText (const std::string &keyword, const std::string &value) ;
  151.   void WriteTextBlocks () ;
  152.  
  153.   void CallProgressFunction (unsigned int) ;
  154.  
  155.   std::string title_string ;
  156.   std::string author_string ;
  157.   std::string description_string ;
  158.   std::string copyright_string ;
  159.   std::string software_string ;
  160.   std::string disclaimer_string ;
  161.   std::string warning_string ;
  162.   std::string source_string ;
  163.   std::string comment_string ;
  164.  
  165.   enum { FilterBufferCount = 5, } ;
  166.   UBYTE1 *filter_buffers [FilterBufferCount] ;
  167.   unsigned int current_filter ;
  168.   unsigned int filter_mask ;
  169.   unsigned int filter_width ;
  170.   void FilterRow (unsigned int row) ;
  171.  
  172.   void DoWrite () ;
  173.  
  174.   CompressionLevel compression_level ;
  175.  
  176.   void InitializeHashTable () ;
  177.   void InitializeLZWindow () ;
  178.   void OutputDeflateHeader (bool lastblock) ;
  179.   void OutputZLibHeader () ;
  180.   void WriteImageData () ;
  181.  
  182.   UBYTE4 adler_value ;
  183.   UBYTE1 *lz_window ;
  184.   UBYTE2 *lookahead_buffer ;
  185.   unsigned int lookahead_position  ;
  186.   unsigned int lz_position ;
  187.   int image_row ; // Must be Signed
  188.   unsigned int image_col ;
  189.   bool image_end ;
  190.  
  191.   UBYTE2 *block_buffer ;
  192.   unsigned int block_buffer_size ;
  193.   unsigned int block_buffer_count ;
  194.  
  195.   void OutputBlockData () ;
  196. } ;
  197.  
  198. inline void PngEncoder::SetTitle (const std::string &value)
  199. {
  200.   title_string = value ;
  201.   return ;
  202. }
  203.  
  204. inline std::string PngEncoder::GetTitle () const
  205. {
  206.   return title_string ;
  207. }
  208.  
  209. inline void PngEncoder::SetAuthor (const std::string &value)
  210. {
  211.   author_string = value ;
  212.   return ;
  213. }
  214.  
  215. inline std::string PngEncoder::GetAuthor () const
  216. {
  217.   return author_string ;
  218. }
  219.  
  220. inline void PngEncoder::SetDescription (const std::string &value)
  221. {
  222.   description_string = value ;
  223.   return ;
  224. }
  225.  
  226. inline std::string PngEncoder::GetDescription () const
  227. {
  228.   return description_string ;
  229. }
  230.  
  231. inline void PngEncoder::SetCopyright (const std::string &value)
  232. {
  233.   copyright_string = value ;
  234.   return ;
  235. }
  236.  
  237. inline std::string PngEncoder::GetCopyright () const
  238. {
  239.   return copyright_string ;
  240. }
  241.  
  242. inline void PngEncoder::SetSoftware (const std::string &value)
  243. {
  244.   software_string = value ;
  245.   return ;
  246. }
  247.  
  248. inline std::string PngEncoder::GetSoftware () const
  249. {
  250.   return software_string ;
  251. }
  252.  
  253. inline void PngEncoder::SetDisclaimer (const std::string &value)
  254. {
  255.   disclaimer_string = value ;
  256.   return ;
  257. }
  258.  
  259. inline std::string PngEncoder::GetDisclaimer () const
  260. {
  261.   return disclaimer_string ;
  262. }
  263.  
  264. inline void PngEncoder::SetWarning (const std::string &value)
  265. {
  266.   warning_string = value ;
  267.   return ;
  268. }
  269.  
  270. inline std::string PngEncoder::GetWarning () const
  271. {
  272.   return warning_string ;
  273. }
  274.  
  275. inline void PngEncoder::SetSource (const std::string &value)
  276. {
  277.   source_string = value ;
  278.   return ;
  279. }
  280.  
  281. inline std::string PngEncoder::GetSource () const 
  282. {
  283.   return source_string ;
  284. }
  285.  
  286. inline void PngEncoder::SetComment (const std::string &value)
  287. {
  288.   comment_string = value ;
  289.   return ;
  290. }
  291.  
  292. inline std::string PngEncoder::GetComment () const
  293. {
  294.   return comment_string ;
  295. }
  296.  
  297. inline unsigned long PngEncoder::GetBlockSize () const
  298. {
  299.   return block_buffer_size ;
  300. }
  301.  
  302. inline void PngEncoder::SetBlockSize (unsigned long value)
  303. {
  304.   if (value < 500)
  305.     block_buffer_size = 500 ;
  306.   else
  307.     block_buffer_size = value ;
  308.   return ;
  309. }
  310.  
  311.  
  312. #endif
  313.  
  314.  
  315.