/*
 * A simple ppm to greyscale converter.
 * 
 * $Date$
 * $Revision$
 *
 */

#include <iostream>
#include <stdexcept>
#include <cstring>
#include <Wave.hh>

#if !(defined __WATCOMC__ && __WATCOMC__ < 1230)
using namespace std;
#endif

#define NFILES 2
#define COLORS 3

static void usage (void);

int 
main (int argc, char *argv[])
{
  char *filenames[NFILES] = { NULL, NULL };
  int nindex = 0;
  bool avg = false;
  filetype output = fn_unknown;
  
  for (int i = 1; i < argc; i++)
    {
      switch (argv[i][0])
        {
        case '-':
          if (argv[i][1] == '\0')
            {
              /* it's a stdin / stdout filename */
              if (nindex < NFILES)
                {
                  /* leave it at NULL */
                  nindex++;
                }
              else
                {
                  cerr << "Error, too many file names." << endl;
                  return 1;
                }
            }
          else if (strcmp (&argv[i][1], "h") == 0)
            {
              usage ();
              return 0;
            }
          else if (strcmp (&argv[i][1], "a") == 0)
            {
              avg = true;
            }
          else if (strcmp (&argv[i][1], "r") == 0)
            {
              output = fn_raw;
            }
          else if (strcmp (&argv[i][1], "p") == 0)
            {
              output = fn_pgm;
            }
          else
            {
              cerr << "error, invalid argument: \"" 
                   << argv[i] << "\"." << endl;
              return 1;
            }
          break;
          
        default:
          if (nindex < NFILES)
            {
              if (nindex == 1)
                {
                  FileName fn (argv[i]);
                  output = fn.guess ();
                }
              filenames[nindex++] = argv[i];
            }
          else
            {
              cerr << "Error, too many file names." << endl;
              return 1;
            }
          break;
          
        }
    }

  if (output == fn_unknown)
    {
      output = fn_pgm;
    }
  
  ColorImage *ppm = NEW (ColorImage ());

  try
    {
      ppm->read (filenames[0]);
    }
  catch (const exception &error)
    {
      cerr << "Error while reading: " << error.what () <<endl;
      return 1;
    }

  Image *ret;

  if (avg)
    {
      ret = ppm->averageImage ();
    }
  else
    {
      ppm->colormodel (cm_yuv);
      ret = (ppm->channel (0)).clone ();
    }
  
  try
    {
      ret->write (filenames[1], output);
    }
  catch (const exception &error)
    {
      cerr << "Error while writing: " << error.what () << endl;
      return 1;
    }

  DELETE (ppm);
  
  return 0;
}

static void 
usage (void)
{
  cout << "ppm2grey, version " << WAVE_VERSION << ", (c) 2001-2004 by Herbert,"
#ifdef PFI
       << " (pfi support enabled)"
#endif
       << endl;
  cout << "Usage: ppm2grey [<opts>] [infile] [<opts>] [outfile] [<opts>]\n"
       << " Options are:\n"
       << "  -a   : return the average rather than y from yuv.\n"
       << "  -r   : force raw format output.\n"
       << "  -p   : force pgm format output.\n"
       << "A \"-\" instead of a filename stands for stdin / stdout.\n"
       << "If no filenames are given stdin / stdout will be assumed.\n"
       << "If no output file type is given it will be guessed.\n"
       << "If the output file type cannot be guessed, pgm will be used.\n";
}

