home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c09 / inc / jpegenco.h next >
Encoding:
C/C++ Source or Header  |  1998-12-23  |  7.8 KB  |  265 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.   // Number of rows between restart markers (0=> No restart markers)
  59.   unsigned int GetRowsPerRestart () const ;
  60.   void SetRowsPerRestart (unsigned int) ;
  61.  
  62.   // Comment String
  63.   std::string GetComment () const ;
  64.   void SetComment (const std::string &) ;
  65.  
  66.   // Component Sampling Frequencies (1-4)
  67.   void SetSamplingFrequency (unsigned int component, unsigned int hf, unsigned int vf) ;
  68.   void GetSamplingFrequency (unsigned int component, unsigned int &hf, unsigned int &vf) ;
  69.  
  70.   // Scan Attributes {scan number, component bit mask,
  71.   // spectral selection end (0-63), successive approximation (0-13) }
  72.   void SetScanAttributes (unsigned int scan, unsigned long components, unsigned int sse, unsigned int ssa) ;
  73.   void GetScanAttributes (unsigned int scan, unsigned long &components, unsigned int &sse, unsigned int &ssa) ;
  74.  
  75.   // Maximum number of scans
  76.   enum { MaxScans = 256, } ;
  77.  
  78.   // Component Identifiers
  79.   enum { YComponent = 1, CbComponent = 2, CrComponent = 3, } ;
  80.  
  81. protected:
  82.   // Output Functions used by other classes to write data to the output stream.
  83.   void OutputBits (int bits, unsigned int count) ;
  84.   void OutputByte (UBYTE1) ;
  85.   void OutputWord (UBYTE2) ;
  86.  
  87.   void CallProgressFunction (unsigned int) ;
  88.  
  89. private:
  90.   void DoCopy (const JpegEncoder &) ;
  91.   void Initialize () ;
  92.  
  93.   // This structure is used to represent a scan.
  94.   struct Scan
  95.   {
  96.     // Bitmap of components in the scan.
  97.     unsigned long component_mask ;
  98.     // Spectral Selection for the scan.
  99.     unsigned int spectral_selection_start ;
  100.     unsigned int spectral_selection_end ;
  101.     // Successive Approximation for the first iteration of the scan
  102.     unsigned int successive_approximation ;
  103.     // Successive Approximation Progress. Updated as scans are output.
  104.     int successive_approximation_high ;
  105.     int successive_approximation_low ;
  106.   } ;
  107.  
  108.   // This function determines if the output scan parameters are valid. It
  109.   // throwse the EJpegError exception an inconsistency is found.
  110.   void ValidateParameters () ;
  111.  
  112.   // Basic Output Methods
  113.   void FlushBitBuffer () ;
  114.  
  115.   void OutputMarker (UBYTE1) ;
  116.  
  117.   // Block Output Methods
  118.   void PrintQuantizationTables () ;
  119.   void PrintSequentialFrame (BitmapImage &image) ;
  120.   void PrintProgressiveFrame (BitmapImage &image) ;
  121.   void PrintComment (const std::string &) ;
  122.   void OutputJfifHeader () ;
  123.  
  124.   void OutputRestartInterval (unsigned int restartinterval) ;
  125.  
  126.   void PrintHuffmanTables (const Scan &scan, bool usedc, bool useac) ;
  127.  
  128.   // Sequential Scan Output
  129.   void PrintSequentialScan (const Scan &scan) ;
  130.  
  131.  
  132.   void InterleavedPass (
  133.                       bool writedata,
  134.                       JpegEncoderComponent::COMPONENTPASSFUNCTION passfunction,
  135.                       JpegEncoderComponent::DCOUTPUTFUNCTION dcfunction,
  136.                       JpegEncoderComponent::ACOUTPUTFUNCTION acfunction,
  137.                       unsigned int sss,
  138.                       unsigned int sse,
  139.                       unsigned int ssa) ;
  140.   void NoninterleavedPass (
  141.                       bool writedata,
  142.                       JpegEncoderComponent::COMPONENTPASSFUNCTION passfunction,
  143.                       JpegEncoderComponent::DCOUTPUTFUNCTION dcfunction,
  144.                       JpegEncoderComponent::ACOUTPUTFUNCTION acfunction,
  145.                       unsigned int sss,
  146.                       unsigned int sse,
  147.                       unsigned int ssa) ;
  148.  
  149.   void ResetDcValues () ;
  150.   void CalculateMcuDimensions () ;
  151.   void CountPassesForProgressReporting () ;
  152.  
  153.   void FindComponentsInScan (Scan &scan) ;
  154.   void CreateQuantizationTables (unsigned int quality) ;
  155.  
  156.   // Data used for bit I/O
  157.   unsigned int bit_count ;  // Number of bits that habe been buffered
  158.   UBYTE1 bit_buffer ;
  159.  
  160.   // Quantization Tables
  161.   JpegEncoderQuantizationTable *chrominance_quanttbl ;  // For Y
  162.   JpegEncoderQuantizationTable *luminance_quanttbl ;    // For Cb and Cr
  163.  
  164.   // Huffman Tables
  165.   JpegEncoderHuffmanTable *ac_tables ;
  166.   JpegEncoderHuffmanTable *dc_tables ;
  167.  
  168.   // Properties
  169.   bool gray_scale ;
  170.   unsigned int rows_per_restart ;
  171.   unsigned int restart_interval ;
  172.   unsigned int image_quality ;
  173.   std::string comment_string ;
  174.  
  175.   unsigned int total_passes ;
  176.   unsigned int current_pass ;
  177.  
  178.   // Image Size
  179.   unsigned int frame_width ;
  180.   unsigned int frame_height ;
  181.   // Maximum Frequencies in all components
  182.   unsigned int max_horizontal_frequency ;
  183.   unsigned int max_vertical_frequency ;
  184.   // MCU Dimensions
  185.   unsigned int  mcu_rows ;
  186.   unsigned int  mcu_cols ;
  187.  
  188.   BitmapImage *current_image ;
  189.   std::ostream *output_stream ;
  190.  
  191.   unsigned int scan_count ;
  192.  
  193.   // Scan Descriptors
  194.   Scan image_scans [MaxScans] ;
  195.   // Components
  196.   enum { MaxComponents = 4, } ;
  197.   JpegEncoderComponent image_components [MaxComponents] ;
  198.   // Components used in the scan being processed.
  199.   unsigned int scan_component_count ;
  200.   JpegEncoderComponent *scan_components [JpegMaxComponentsPerScan] ;
  201.  
  202.   friend class JpegEncoderHuffmanTable ;
  203.   friend class JpegEncoderComponent ;
  204. } ;
  205.  
  206. //
  207. //  The RowsPerRestart parameter specifies the number of MCU rows
  208. //  output between restart markers. A value of zero means no restart
  209. //  markers are used.
  210. //
  211. inline unsigned int JpegEncoder::GetRowsPerRestart () const
  212. {
  213.   return rows_per_restart ;
  214. }
  215.  
  216. inline void JpegEncoder::SetRowsPerRestart (unsigned int rows)
  217. {
  218.   rows_per_restart = rows ;
  219.   return ;
  220. }
  221.  
  222. inline void JpegEncoder::SetGrayscale (bool value)
  223. {
  224.   gray_scale = value ;
  225.   return ;
  226. }
  227.  
  228. inline bool JpegEncoder::GetGrayscale () const
  229. {
  230.   return gray_scale ;
  231. }
  232.  
  233. //
  234. //  This function calls the progress function if one has been specified.
  235. //
  236. //  Parameters:
  237. //      progress: Percent completed (0-100)
  238. //
  239. inline void JpegEncoder::CallProgressFunction (unsigned int progress)
  240. {
  241.   if (progress_function == NULL)
  242.     return ;
  243.  
  244.   bool abort = false ;
  245.   bool update = false;
  246.   unsigned int percent = progress ;
  247.   if (percent > 100)
  248.     percent = 100 ;
  249.   progress_function (*this,
  250.                      progress_data,
  251.                      current_pass,
  252.                      total_passes,
  253.                      percent,
  254.                      abort) ;
  255.  
  256.   // See if the user wants to stop.
  257.   if (abort)
  258.     throw EJpegAbort () ;
  259.   return ;
  260. }
  261.  
  262.  
  263. #endif
  264.  
  265.