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

  1. #include <fstream>
  2. #include "jpegdeco.h"
  3. #include "bitimage.h"
  4. #include "bmpencod.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.  
  21. void Usage (int argc, char *argv [])
  22. {
  23.   char *program ;
  24.   if (argc != 0)
  25.     program = argv [0] ;
  26.   else
  27.     program = "DECODER" ;
  28.  
  29.   cerr << "Usage: " << program << " [-v] input.jpg output.bmp" << endl ;
  30.   exit (1) ;
  31. }
  32.  
  33. int main(int argc, char *argv [])
  34. {
  35.   if (argc < 3)
  36.   {
  37.     Usage (argc, argv) ;
  38.     return 1 ;
  39.   }
  40.  
  41.   JpegDecoder decoder ;
  42.  
  43.   for (unsigned int ii = 1 ; ii < argc - 2 ; ++ ii)
  44.   {
  45.     if (argv [ii][0] != '-')
  46.       Usage (argc, argv) ;
  47.  
  48.     switch (argv [ii][1])
  49.     {
  50.     case 'v':
  51.       decoder.SetVerbose (true) ;
  52.       break ;
  53.     default:
  54.       Usage (argc, argv) ;
  55.     }
  56.   }
  57.  
  58.   decoder.SetProgressFunction (Progress, NULL) ;
  59.  
  60.   ifstream is (argv [argc - 2], ios::binary) ;
  61.   if (! is)
  62.   {
  63.     cerr << "Can't open input file " << argv [argc-2] << endl ;
  64.     return 1 ;
  65.   }
  66.   BitmapImage image ;
  67.   cout << "Reading Image..." << endl ;
  68.   try
  69.   {
  70.     decoder.ReadImage (is, image) ;
  71.   }
  72.   catch (EGraphicsException &ee)
  73.   {
  74.     cerr << ee.what () << endl << flush ;
  75.     return 1 ;
  76.   }
  77.  
  78.   cout << "Writing Output..." << endl ;
  79.   ofstream os (argv [argc-1], ios::binary|ios::trunc) ;
  80.   if (! os)
  81.   {
  82.     cerr << "Can't open output file " << argv [argc-1] << endl ;
  83.     return 1 ;
  84.   }
  85.  
  86.   BmpEncoder bs ;
  87.   try
  88.   {
  89.           bs.WriteImage (os, image) ;
  90.   }
  91.   catch (EGraphicsException &ee)
  92.   {
  93.     cerr << ee.what () << endl << flush ;
  94.     return 1 ;
  95.   }
  96.   cout << "Done..." << endl << flush ;
  97.  
  98.   return 0;
  99. }
  100.