home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Library / jpeg / encoder.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-17  |  2.1 KB  |  104 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.   cerr << "        -p          Progressive Output" << endl ;
  31.  
  32.   exit (1) ;
  33. }
  34.  
  35. int main (int argc, char *argv [])
  36. {
  37.   if (argc < 3)
  38.     Usage (argc, argv) ;
  39.  
  40.   JpegEncoder encoder ;
  41.   encoder.SetProgressFunction (Progress, NULL) ;
  42.  
  43.   // Change the default. Place each component in a separate scan.
  44.   encoder.SetScanAttributes (0, 2, 0, 0) ;
  45.   encoder.SetScanAttributes (1, 4, 0, 0) ;
  46.   encoder.SetScanAttributes (2, 8, 0, 0) ;
  47.  
  48.   for (int ii = 1 ; ii < argc - 2 ; ++ ii)
  49.   {
  50.     if (argv [ii][0] != '-')
  51.       Usage (argc, argv) ;
  52.  
  53.     switch (argv [ii][1])
  54.     {
  55.     case 'g':
  56.       encoder.SetGrayscale (true) ;
  57.       break ;
  58.     case 'p':
  59.       encoder.SetProgressive (true) ;
  60.       break ;
  61.     default:
  62.       Usage (argc, argv) ;
  63.     }
  64.   }
  65.  
  66.  
  67.   ifstream input (argv [argc-2], ios::binary) ;
  68.   if (! input)
  69.   {
  70.     cerr << "Can't open input file " << argv [argc-2] << endl ;
  71.     return 1 ;
  72.   }
  73.  
  74.   BmpDecoder decoder ;
  75.   BitmapImage image ;
  76.   try
  77.   {
  78.     decoder.ReadImage (input, image) ;
  79.   }
  80.   catch (exception &ee)
  81.   {
  82.     cerr << ee.what () << endl ;
  83.     return 1 ;
  84.   }
  85.  
  86.   ofstream output (argv [argc-1], ios::binary|ios::trunc) ;
  87.   if (! input)
  88.   {
  89.     cerr << "Can't open output file " << argv [argc-1] << endl ;
  90.     return 1 ;
  91.   }
  92.  
  93.   try
  94.   {
  95.     encoder.WriteImage (output, image) ;
  96.   }
  97.   catch (exception &ee)
  98.   {
  99.     cerr << ee.what () << endl ;
  100.     return 1 ;
  101.   }
  102.   return 0 ;
  103. }
  104.