/*
 * Test program for the color image class
 * 
 * This is relatively short as the ppm filters are already old
 * and well-tested.
 *
 * $Date$
 * $Revision$
 *
 */

#include <cassert>
#include <iostream>
#include <stdexcept>
#include "WImage/ColorImage.hh"

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

int
main (void)
{
  cout << "Testing class ColorImage...\n";

  ColorImage *ppm1 = NEW (ColorImage ());

  cout << "read ppm...\n";

  try
    {
      ppm1->read ("./lena128.ppm");
    }
  catch (const exception &error)
    {
      cout << "Exception caught: " << error.what () <<endl;
      return 1;
    }

  int colors = ppm1->colors ();

  cout << "write ppm...\n";

  try
    {
      ppm1->write ("./out.ppm");
    }
  catch (const exception &error)
    {
      cout << "Exception caught: " << error.what () << endl;
      return 1;
    }

  cout << "read ppm again...\n";
  
  ColorImage *ppm2 = NEW (ColorImage ());
  
  try
    {
      ppm2->read ("./out.ppm");
    }
  catch (const exception &error)
    {
      cout << "Exception caught: " << error.what () <<endl;
      return 1;
    }

  for (int i = 0; i < colors; i++)
    {
      assert ((ppm1->channel (i)).equals (ppm2->channel (i)));
    }
  assert (ppm1->equals (*ppm2));
  assert (ppm1->epsilons (*ppm2, COEFF_EPSILON));

  cout << "convert from rgb to yuv and back...\n";
  
  ppm1->colormodel (cm_yuv);
  ppm1->colormodel (cm_rgb);
  ppm1->write ("out.ppm");
  ppm1->read ("out.ppm");
 
  for (int i = 0; i < colors; i++)
    {
      assert ((ppm1->channel (i)).equals (ppm2->channel (i)));
    }

  cout << "done.\n";

  DELETE (ppm1);
  DELETE (ppm2);

#ifdef JPEG
  cout << "black-white JPEG image...\n";
  ColorImage *jpeg = NEW (ColorImage ());
  try
    {
      jpeg->read ("./bottlebw.jpg");
    }
  catch (const exception &error)
    {
      cout << "Exception caught: " << error.what () <<endl;
      return 1;
    }
  DELETE (jpeg);
#endif

  cout << "Test of class ColorImage completed successfully.\n";

  return 0;
}

