/*
 * Simple speed benchmark
 *
 * $Date$
 * $Revision$
 *
 */
#include <iostream>
#include <stdexcept>
#include <time.h>

#include <Wave.hh>

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

void doit (Image *, Image *, FilterSet*, int);

int 
main (void)
{
  cout << "benchmark version " << WAVE_VERSION << " (c) 2001-2003 by Herbert"
#ifdef PFI
       << " (pfi support enabled)"
#endif
       << endl;

  FilterSet *asymmetric[] = { 
    &Haar, &Daub4, &Daub6, &Daub8
  };
  FilterSet *symmetric[] = { 
    &Antonini, &Brislawn, &Villa1, 
    &Villa2, &Villa3, &Villa4, &Villa5, &Villa6, &Odegard
  };
  Image *image = NEW (StillImage ());
  int steps = 8;
  int k = 0;
  time_t start, stop;
  
  try 
    {
      image->read ("../images/lena512.pgm");
    }
  catch (const exception &error)
    {
      cerr << "Reading ../images/lena512.pgm: " << error.what () <<endl;
      return 1;
    }

  Image *buffer = image->clone ();

  cout << "Starting transforms...\n";

  start = time (NULL);
  
  for (int i = 0; (unsigned)i < sizeof asymmetric / sizeof (FilterSet*); 
       i++, k++)
    {
      cout << "\rNo " << k << endl;
      doit (image, buffer, asymmetric[i], steps);
    }
  for (int i = 0; (unsigned)i < sizeof symmetric / sizeof (FilterSet*); 
       i++, k++)
    {
      cout << "\rNo " << k << endl;
      doit (image, buffer, symmetric[i], steps);
    }

  
  stop = time (NULL);

  try 
    {
      image->write ("out.pgm");
    }
  catch (const exception &error)
    {
      cerr << "Writing out.pgm: " << error.what () <<endl;
      return 1;
    }
  DELETE (image);

  cout << "That was " << stop - start << " seconds.\n";

  return 0;
}

void doit (Image *image, Image *buffer, FilterSet *filter, int steps)
{
  WaveletTransform *transform = NEW (PyramidTransform (*image, *filter));
  transform->analysis (steps);
  transform->synthesis (steps);
  DELETE (transform);
}

void doit2 (Image *image, Image *buffer, FilterSet *filter, int steps)
{
  FullVector &fv1 = image->full (0);
  FullVector &fv2 = buffer->full (0);
  Wavelet *wavelet = NEW (Wavelet (*filter));
  wavelet->analysis (fv2, fv1, steps);
  wavelet->synthesis (fv1, fv2, steps);
  DELETE (wavelet);
}
