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

  1. #ifndef __JPEGENCODER
  2. #define __JPEGENCODER
  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. //  JPEG Encoder Library.
  17. //
  18. //  Title: JpegEncoder class definition.
  19. //
  20. //  Author:  John M. Miano  miano@colosseumbuilders.com
  21. //
  22. //  Description:
  23. //
  24. //    This class is an encoder for JPEG image. The process for using this
  25. //    class is to call the property functions to set the attributes for the
  26. //    stream then call the WriteImage function to create a JPEG stream with
  27. //    those attributes.
  28. //
  29.  
  30. #include "bitimage.h"
  31. #include "jpencomp.h"
  32. #include "jpgexcep.h"
  33.  
  34. class JpegEncoder : public BitmapImageEncoder
  35. {
  36. public:
  37.   JpegEncoder () ;
  38.   virtual ~JpegEncoder () ;
  39.   // Required Member Functions
  40.   JpegEncoder (const JpegEncoder &) ;
  41.   JpegEncoder &operator=(const JpegEncoder &) ;
  42.  
  43.   virtual void WriteImage (std::ostream &strm,
  44.                            BitmapImage &image) ;
  45.  
  46.   //************************
  47.   //** Property Functions **
  48.   //************************
  49.  
  50.   // Image Quality (1-100)
  51.   unsigned int GetQuality () const ;
  52.   void SetQuality (unsigned int) ;
  53.  
  54.   // Grayscale Mode (True=Gray Scale, False=Color)
  55.   bool GetGrayscale () const ;
  56.   void SetGrayscale (bool) ;
  57.  
  58.   // Progressive Mode (True=Progressive, False=Sequential)
  59.   bool GetProgressive () const ;
  60.   void SetProgressive (bool) ;
  61.  
  62.   // Number of rows between restart markers (0=> No restart markers)
  63.   unsigned int GetRowsPerRestart () const ;
  64.   void SetRowsPerRestart (unsigned int) ;
  65.  
  66.   // Comment String
  67.   std::string GetComment () const ;
  68.   void SetComment (const std::string &) ;
  69.  
  70.   // Component Sampling Frequencies (1-4)
  71.   void SetSamplingFrequency (unsigned int component, unsigned int hf, unsigned int vf) ;
  72.   void GetSamplingFrequency (unsigned int component, unsigned int &hf, unsigned int &vf) ;
  73.  
  74.   // Scan Attributes {scan number, component bit mask,
  75.   // spectral selection end (0-63), successive approximation (0-13) }
  76.   void SetScanAttributes (unsigned int scan, unsigned long components, unsigned int sse, unsigned int ssa) ;
  77.   void GetScanAttributes (unsigned int scan, unsigned long &components, unsigned int &sse, unsigned int &ssa) ;
  78.  
  79.   // Maximum number of scans
  80.   enum { MaxScans = 256, } ;
  81.  
  82.   // Component Identifiers
  83.   enum { YComponent = 1, CbComponent = 2, CrComponent = 3, } ;
  84.  
  85. protected:
  86.   // Output Functions used by other classes to write data to the output stream.
  87.   void OutputBits (int bits, unsigned int count) ;
  88.   void OutputByte (UBYTE1) ;
  89.   void OutputWord (UBYTE2) ;
  90.  
  91.   void CallProgressFunction (unsigned int) ;
  92.  
  93. private:
  94.   void DoCopy (const JpegEncoder &) ;
  95.   void Initialize () ;
  96.  
  97.   // This structure is used to represent a scan.
  98.   struct Scan
  99.   {
  100.     // Bitmap of components in the scan.
  101.     unsigned long component_mask ;
  102.     // Spectral Selection for the scan.
  103.     unsigned int spectral_selection_start ;
  104.     unsigned int spectral_selection_end ;
  105.     // Successive Approximation for the first iteration of the scan
  106.     unsigned int successive_approximation ;
  107.     // Successive Approximation Progress. Updated as scans are output.
  108.     int successive_approximation_high ;
  109.     int successive_approximation_low ;
  110.   } ;
  111.  
  112.   // This function determines if the output scan parameters are valid. It
  113.   // throwse the EJpegError exception an inconsistency is found.
  114.   void ValidateParameters () ;
  115.  
  116.   // Basic Output Methods
  117.   void FlushBitBuffer () ;
  118.  
  119.   void OutputMarker (UBYTE1) ;
  120.  
  121.   // Block Output Methods
  122.   void PrintQuantizationTables () ;
  123.   void PrintSequentialFrame (BitmapImage &image) ;
  124.   void PrintProgressiveFrame (BitmapImage &image) ;
  125.   void PrintComment (const std::string &) ;
  126.   void OutputJfifHeader () ;
  127.  
  128.   void OutputRestartInterval (unsigned int restartinterval) ;
  129.  
  130.   void PrintHuffmanTables (const Scan &scan, bool usedc, bool useac) ;
  131.  
  132.   // Sequential Scan Output
  133.   void PrintSequentialScan (const Scan &scan) ;
  134.  
  135.   // Progressive Output Functions
  136.   void PrintProgressiveScan (const Scan &scan) ;
  137.   void PrintDcFirst (const Scan &scan) ;
  138.   void PrintDcRefine (const Scan &scan) ;
  139.   void PrintAcFirst (const Scan &scan) ;
  140.   void PrintAcRefine (const Scan &scan) ;
  141.   void FirstAcData (const Scan &scan, bool outputrestarts, JpegEncoderComponent::ACOUTPUTFUNCTION acfunction) ;
  142.   void RefineAcData (const Scan &scan, bool outputrestarts, JpegEncoderComponent::ACOUTPUTFUNCTION acfunction) ;
  143.  
  144.  
  145.   void InterleavedPass (
  146.                       bool writedata,
  147.                       JpegEncoderComponent::COMPONENTPASSFUNCTION passfunction,
  148.                       JpegEncoderComponent::DCOUTPUTFUNCTION dcfunction,
  149.                       JpegEncoderComponent::ACOUTPUTFUNCTION acfunction,
  150.                       unsigned int sss,
  151.                       unsigned int sse,
  152.                       unsigned int ssa) ;
  153.   void NoninterleavedPass (
  154.                       bool writedata,
  155.                       JpegEncoderComponent::COMPONENTPASSFUNCTION passfunction,
  156.                       JpegEncoderComponent::DCOUTPUTFUNCTION dcfunction,
  157.                       JpegEncoderComponent::ACOUTPUTFUNCTION acfunction,
  158.                       unsigned int sss,
  159.                       unsigned int sse,
  160.                       unsigned int ssa) ;
  161.  
  162.   void ResetDcValues () ;
  163.   void CalculateMcuDimensions () ;
  164.   void CountPassesForProgressReporting () ;
  165.  
  166.   void FindComponentsInScan (Scan &scan) ;
  167.   void CreateQuantizationTables (unsigned int quality) ;
  168.  
  169.   // Data used for bit I/O
  170.   unsigned int bit_count ;  // Number of bits that habe been buffered
  171.   UBYTE1 bit_buffer ;
  172.  
  173.   // Quantization Tables
  174.   JpegEncoderQuantizationTable *chrominance_quanttbl ;  // For Y
  175.   JpegEncoderQuantizationTable *luminance_quanttbl ;    // For Cb and Cr
  176.  
  177.   // Huffman Tables
  178.   JpegEncoderHuffmanTable *ac_tables ;
  179.   JpegEncoderHuffmanTable *dc_tables ;
  180.  
  181.   // Properties
  182.   bool gray_scale ;
  183.   unsigned int rows_per_restart ;
  184.   unsigned int restart_interval ;
  185.   unsigned int image_quality ;
  186.   std::string comment_string ;
  187.   bool progressive_mode ;
  188.  
  189.   unsigned int total_passes ;
  190.   unsigned int current_pass ;
  191.  
  192.   // Image Size
  193.   unsigned int frame_width ;
  194.   unsigned int frame_height ;
  195.   // Maximum Frequencies in all components
  196.   unsigned int max_horizontal_frequency ;
  197.   unsigned int max_vertical_frequency ;
  198.   // MCU Dimensions
  199.   unsigned int  mcu_rows ;
  200.   unsigned int  mcu_cols ;
  201.  
  202.   BitmapImage *current_image ;
  203.   std::ostream *output_stream ;
  204.  
  205.   unsigned int scan_count ;
  206.  
  207.   // Scan Descriptors
  208.   Scan image_scans [MaxScans] ;
  209.   // Components
  210.   enum { MaxComponents = 4, } ;
  211.   JpegEncoderComponent image_components [MaxComponents] ;
  212.   // Components used in the scan being processed.
  213.   unsigned int scan_component_count ;
  214.   JpegEncoderComponent *scan_components [JpegMaxComponentsPerScan] ;
  215.  
  216.   friend class JpegEncoderHuffmanTable ;
  217.   friend class JpegEncoderComponent ;
  218. } ;
  219.  
  220. //
  221. //  The RowsPerRestart parameter specifies the number of MCU rows
  222. //  output between restart markers. A value of zero means no restart
  223. //  markers are used.
  224. //
  225. inline unsigned int JpegEncoder::GetRowsPerRestart () const
  226. {
  227.   return rows_per_restart ;
  228. }
  229.  
  230. inline void JpegEncoder::SetRowsPerRestart (unsigned int rows)
  231. {
  232.   rows_per_restart = rows ;
  233.   return ;
  234. }
  235.  
  236. inline void JpegEncoder::SetGrayscale (bool value)
  237. {
  238.   gray_scale = value ;
  239.   return ;
  240. }
  241.  
  242. inline bool JpegEncoder::GetGrayscale () const
  243. {
  244.   return gray_scale ;
  245. }
  246.  
  247. //
  248. //  This function calls the progress function if one has been specified.
  249. //
  250. //  Parameters:
  251. //      progress: Percent completed (0-100)
  252. //
  253. inline void JpegEncoder::CallProgressFunction (unsigned int progress)
  254. {
  255.   if (progress_function == NULL)
  256.     return ;
  257.  
  258.   bool abort = false ;
  259.   bool update = false;
  260.   unsigned int percent = progress ;
  261.   if (percent > 100)
  262.     percent = 100 ;
  263.   progress_function (*this,
  264.                      progress_data,
  265.                      current_pass,
  266.                      total_passes,
  267.                      percent,
  268.                      abort) ;
  269.  
  270.   // See if the user wants to stop.
  271.   if (abort)
  272.     throw EJpegAbort () ;
  273.   return ;
  274. }
  275.  
  276.  
  277. #endif
  278.  
  279.