home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c10 / inc / jpegenco.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-06  |  7.7 KB  |  264 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 PrintComment (const std::string &) ;
  121.   void OutputJfifHeader () ;
  122.  
  123.   void OutputRestartInterval (unsigned int restartinterval) ;
  124.  
  125.   void PrintHuffmanTables (const Scan &scan, bool usedc, bool useac) ;
  126.  
  127.   // Sequential Scan Output
  128.   void PrintSequentialScan (const Scan &scan) ;
  129.  
  130.  
  131.   void InterleavedPass (
  132.                     bool writedata,
  133.                     JpegEncoderComponent::COMPONENTPASSFUNCTION passfunction,
  134.                     JpegEncoderComponent::DCOUTPUTFUNCTION dcfunction,
  135.                     JpegEncoderComponent::ACOUTPUTFUNCTION acfunction,
  136.                     unsigned int sss,
  137.                     unsigned int sse,
  138.                     unsigned int ssa) ;
  139.   void NoninterleavedPass (
  140.                     bool writedata,
  141.                     JpegEncoderComponent::COMPONENTPASSFUNCTION passfunction,
  142.                     JpegEncoderComponent::DCOUTPUTFUNCTION dcfunction,
  143.                     JpegEncoderComponent::ACOUTPUTFUNCTION acfunction,
  144.                     unsigned int sss,
  145.                     unsigned int sse,
  146.                     unsigned int ssa) ;
  147.  
  148.   void ResetDcValues () ;
  149.   void CalculateMcuDimensions () ;
  150.   void CountPassesForProgressReporting () ;
  151.  
  152.   void FindComponentsInScan (Scan &scan) ;
  153.   void CreateQuantizationTables (unsigned int quality) ;
  154.  
  155.   // Data used for bit I/O
  156.   unsigned int bit_count ;  // Number of bits that habe been buffered
  157.   UBYTE1 bit_buffer ;
  158.  
  159.   // Quantization Tables
  160.   JpegEncoderQuantizationTable *chrominance_quanttbl ;  // For Y
  161.   JpegEncoderQuantizationTable *luminance_quanttbl ;    // For Cb and Cr
  162.  
  163.   // Huffman Tables
  164.   JpegEncoderHuffmanTable *ac_tables ;
  165.   JpegEncoderHuffmanTable *dc_tables ;
  166.  
  167.   // Properties
  168.   bool gray_scale ;
  169.   unsigned int rows_per_restart ;
  170.   unsigned int restart_interval ;
  171.   unsigned int image_quality ;
  172.   std::string comment_string ;
  173.  
  174.   unsigned int total_passes ;
  175.   unsigned int current_pass ;
  176.  
  177.   // Image Size
  178.   unsigned int frame_width ;
  179.   unsigned int frame_height ;
  180.   // Maximum Frequencies in all components
  181.   unsigned int max_horizontal_frequency ;
  182.   unsigned int max_vertical_frequency ;
  183.   // MCU Dimensions
  184.   unsigned int  mcu_rows ;
  185.   unsigned int  mcu_cols ;
  186.  
  187.   BitmapImage *current_image ;
  188.   std::ostream *output_stream ;
  189.  
  190.   unsigned int scan_count ;
  191.  
  192.   // Scan Descriptors
  193.   Scan image_scans [MaxScans] ;
  194.   // Components
  195.   enum { MaxComponents = 4, } ;
  196.   JpegEncoderComponent image_components [MaxComponents] ;
  197.   // Components used in the scan being processed.
  198.   unsigned int scan_component_count ;
  199.   JpegEncoderComponent *scan_components [JpegMaxComponentsPerScan] ;
  200.  
  201.   friend class JpegEncoderHuffmanTable ;
  202.   friend class JpegEncoderComponent ;
  203. } ;
  204.  
  205. //
  206. //  The RowsPerRestart parameter specifies the number of MCU rows
  207. //  output between restart markers. A value of zero means no restart
  208. //  markers are used.
  209. //
  210. inline unsigned int JpegEncoder::GetRowsPerRestart () const
  211. {
  212.   return rows_per_restart ;
  213. }
  214.  
  215. inline void JpegEncoder::SetRowsPerRestart (unsigned int rows)
  216. {
  217.   rows_per_restart = rows ;
  218.   return ;
  219. }
  220.  
  221. inline void JpegEncoder::SetGrayscale (bool value)
  222. {
  223.   gray_scale = value ;
  224.   return ;
  225. }
  226.  
  227. inline bool JpegEncoder::GetGrayscale () const
  228. {
  229.   return gray_scale ;
  230. }
  231.  
  232. //
  233. //  This function calls the progress function if one has been specified.
  234. //
  235. //  Parameters:
  236. //      progress: Percent completed (0-100)
  237. //
  238. inline void JpegEncoder::CallProgressFunction (unsigned int progress)
  239. {
  240.   if (progress_function == NULL)
  241.     return ;
  242.  
  243.   bool abort = false ;
  244.   bool update = false;
  245.   unsigned int percent = progress ;
  246.   if (percent > 100)
  247.     percent = 100 ;
  248.   progress_function (*this,
  249.                      progress_data,
  250.                      current_pass,
  251.                      total_passes,
  252.                      percent,
  253.                      abort) ;
  254.  
  255.   // See if the user wants to stop.
  256.   if (abort)
  257.     throw EJpegAbort () ;
  258.   return ;
  259. }
  260.  
  261.  
  262. #endif
  263.  
  264.