home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c09 / src / encoder.cpp next >
Encoding:
C/C++ Source or Header  |  1998-12-22  |  2.0 KB  |  100 lines

  1. #include <fstream>
  2.  
  3. #include "jpegenco.h"
  4. #include "bmpdecod.h"
  5.  
  6. using namespace std ;
  7.  
  8. #pragma argsused
  9. void Progress (BitmapImageCoder &coder,
  10.                void *data,
  11.                unsigned int currentpass,
  12.                unsigned int passcount,
  13.                unsigned int progress,
  14.                bool &cancel)
  15. {
  16.   cout << currentpass << " of " << passcount << " " << progress << "%             \r" << flush ;
  17.   return ;
  18. }
  19.  
  20. void Usage (int argc, char *argv [])
  21. {
  22.   char *file ;
  23.   if (argc != 0)
  24.     file = argv [0] ;
  25.   else
  26.     file = argv [0] ;
  27.  
  28.   cerr << "Usage:  " << file << " [-flags] bmp-file jpg-file " << endl ;
  29.   cerr << "Flags:  -g          Grayscale Output" << endl ;
  30.  
  31.   exit (1) ;
  32. }
  33.  
  34. int main (int argc, char *argv [])
  35. {
  36.   if (argc < 3)
  37.     Usage (argc, argv) ;
  38.  
  39.   JpegEncoder encoder ;
  40.   encoder.SetProgressFunction (Progress, NULL) ;
  41.  
  42.   // Change the default. Place each component in a separate scan.
  43.   encoder.SetScanAttributes (0, 2, 0, 0) ;
  44.   encoder.SetScanAttributes (1, 4, 0, 0) ;
  45.   encoder.SetScanAttributes (2, 8, 0, 0) ;
  46.  
  47.   for (int ii = 1 ; ii < argc - 2 ; ++ ii)
  48.   {
  49.     if (argv [ii][0] != '-')
  50.       Usage (argc, argv) ;
  51.  
  52.     switch (argv [ii][1])
  53.     {
  54.     case 'g':
  55.       encoder.SetGrayscale (true) ;
  56.       break ;
  57.     default:
  58.       Usage (argc, argv) ;
  59.     }
  60.   }
  61.  
  62.  
  63.   ifstream input (argv [argc-2], ios::binary) ;
  64.   if (! input)
  65.   {
  66.     cerr << "Can't open input file " << argv [argc-2] << endl ;
  67.     return 1 ;
  68.   }
  69.  
  70.   BmpDecoder decoder ;
  71.   BitmapImage image ;
  72.   try
  73.   {
  74.     decoder.ReadImage (input, image) ;
  75.   }
  76.   catch (exception &ee)
  77.   {
  78.     cerr << ee.what () << endl ;
  79.     return 1 ;
  80.   }
  81.  
  82.   ofstream output (argv [argc-1], ios::binary|ios::trunc) ;
  83.   if (! input)
  84.   {
  85.     cerr << "Can't open output file " << argv [argc-1] << endl ;
  86.     return 1 ;
  87.   }
  88.  
  89.   try
  90.   {
  91.     encoder.WriteImage (output, image) ;
  92.   }
  93.   catch (exception &ee)
  94.   {
  95.     cerr << ee.what () << endl ;
  96.     return 1 ;
  97.   }
  98.   return 0 ;
  99. }
  100.