home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Library / PNG / encoder.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-17  |  2.0 KB  |  101 lines

  1. #include <fstream>
  2. #include <time.h>
  3. #include "pngencod.h"
  4. #include "bmpdecod.h"
  5.  
  6. using namespace std ;
  7.  
  8. void Progress (BitmapImageCoder &, void*, unsigned int pass, unsigned int passcount,
  9.                unsigned int percent, bool &)
  10. {
  11.   unsigned int value = 100 * (pass - 1) / passcount + percent / passcount ;
  12.   cout << value << "%                \r" << flush ;
  13. }
  14.  
  15. void Usage (int argc, char *argv [])
  16. {
  17.   char *program ;
  18.   if (argc != 0)
  19.     program = argv [0] ;
  20.   else
  21.     program = "ENCODER" ;
  22.  
  23.   cerr << "Usage: " << program << " [-P -F -M -f] input.bmp output.png" << endl ;
  24.  
  25.   exit (1) ;
  26.   return ;
  27. }
  28.  
  29.  
  30. main (int argc, char *argv [])
  31. {
  32.   time_t start = time (NULL) ;
  33.  
  34.   if (argc < 3)
  35.     Usage (argc, argv) ;
  36.  
  37.   PngEncoder encoder ;
  38.  
  39.   for (unsigned int ii = 1 ; ii < argc - 2 ; ++ ii)
  40.   {
  41.     if (argv [ii][0] != '-')
  42.       Usage (argc, argv) ;
  43.  
  44.     switch (argv [ii][1])
  45.     {
  46.     case 'f':
  47.       encoder.SetUseFilters (true) ;
  48.       break ;
  49.     case 'F':
  50.       encoder.SetCompressionLevel (PngEncoder::FastestCompression) ;
  51.       break ;
  52.     case 'M':
  53.       encoder.SetCompressionLevel (PngEncoder::MaximumCompression) ;
  54.       break ;
  55.     case 'P':
  56.       encoder.SetProgressFunction (Progress, NULL) ;
  57.       break ;
  58.     default:
  59.       Usage (argc, argv) ;
  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.     exit (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.     throw ;
  80.   }
  81.  
  82.   ofstream output (argv [argc-1], ios::binary) ;
  83.   if (! output)
  84.   {
  85.     cerr << "Can't open output file " << argv [argc-1] << endl ;
  86.     exit (1) ;
  87.   }
  88.  
  89.   try
  90.   {
  91.     encoder.WriteImage (output, image) ;
  92.   }
  93.   catch (exception &ee)
  94.   {
  95.     cerr << ee.what () << endl ;
  96.   }
  97.   time_t elapsed = time (NULL) - start ;
  98.   cout << endl << elapsed << " Seconds" << endl ;
  99.   return 0 ;
  100. }
  101.