home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c06 / src / huffcomp.cpp next >
Encoding:
C/C++ Source or Header  |  1998-12-24  |  1.5 KB  |  76 lines

  1. #include <fstream>
  2. #include <stdexcept>
  3.  
  4. using namespace std ;
  5.  
  6. #include "jpegenco.h"
  7. #include "jpenhuff.h"
  8.  
  9. JpegEncoderHuffmanTable table ;
  10.  
  11. main (int argc, char *argv [])
  12. {
  13.   unsigned long bytes ;
  14.  
  15.   if (argc != 3)
  16.   {
  17.     cerr << "Usage: " << argv [0] << " input-file output-file" << endl ;
  18.     return 1 ;
  19.   }
  20.  
  21.   ifstream input ;
  22.   input.open (argv [1]) ;
  23.   if (! input)
  24.   {
  25.     cerr << "Can't open input file" << endl ;
  26.     return 1 ;
  27.   }
  28.  
  29.   unsigned char data ;
  30.   input.read ((char*) &data, 1) ;
  31.   bytes = 0 ;
  32.   while (! input.eof ())
  33.   {
  34.     ++ bytes ;
  35.     if (bytes % 1000 == 0)
  36.       cout << bytes << '\r' ; 
  37.     table.IncrementFrequency (data) ;
  38.     input.read ((char*) &data, 1) ;
  39.   }
  40.   cout << "Pass One: " << bytes << " bytes" << endl ;
  41.   table.BuildTable () ;
  42.   input.clear () ;
  43.   input.seekg (0) ;
  44.  
  45.   ofstream output ;
  46.   output.open (argv [2], ios::binary) ;
  47.   if (! output)
  48.   {
  49.     cerr << "Can't open output file" << endl ;
  50.     return 1 ;
  51.   }
  52.  
  53.   JpegEncoder encoder (output) ;
  54.  
  55.   table.PrintTable (encoder) ;
  56.   input.read ((char*) &data, 1) ;
  57.   bytes = 0 ;
  58.   while (! input.eof ())
  59.   {
  60.     ++ bytes ;
  61.     if (bytes % 1000 == 0)
  62.       cout << bytes << '\r' ; 
  63.     UBYTE1 length ;
  64.     UBYTE2 code ;
  65.     table.Encode (data, code, length) ;
  66.     encoder.OutputBits (code, length) ;
  67.     input.read ((char*) &data, 1) ;
  68.   }
  69.   cout << "Pass Two: " << bytes << " bytes" << endl ;
  70.   encoder.FlushBitBuffer () ;
  71.  
  72.   input.close () ;
  73.   output.close () ;
  74.   return 0 ;
  75. }
  76.