/*
 * Test program for the class ImageResizer
 *
 * $Date$
 * $Revision$
 *
 */

#ifdef NDEBUG
#undef NDEBUG
#endif /* NDEBUG */

#include "WImage/StillImage.hh"
#include "WImage/ColorImage.hh"
#include "WTools/ImageResizer.hh"
#include "testing.h"
#include <cassert>
#include <iostream>
#include <iomanip>
#include <stdexcept>

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

static void testColorDumbResizing (void);
static void testGreyscaleDumbResizing (void);

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

  ColorImage *img = NEW (ColorImage ());
  ColorImage * result;
  //TEST_NO_EXCEPTION (img->read ("rita.jpg")); // 768x1024
  //TEST_NO_EXCEPTION (img->read ("christina57.jpg")); // 594x448
  //TEST_NO_EXCEPTION (img->read ("lena128.ppm")); // 128x128
  //int white[3] = { 255, 255, 255 };

  TEST_NO_EXCEPTION (img->read ("prod_18_schuh1.jpg")); // 300x280

  int red[3] = { 255, 0, 0 };

  ImageResizer ir (*img, Haar, red, 0.001, true);

  //TEST_NO_EXCEPTION (result = ir.resize(100, 200));
  //TEST_NO_EXCEPTION (result = ir.resize(340, 300));
  //TEST_NO_EXCEPTION (result = ir.resize(64, 64));

  TEST_NO_EXCEPTION (result = ir.resize(80, 300));
  cout << "score: " << 20000 * ir.getInnerAvgPerSize () << endl;

  TEST_NO_EXCEPTION (result->write("rescaled.jpg"));
  DELETE (result);

  //TEST_NO_EXCEPTION (result = ir.resize(400, 500));
  //cout << "score: " << 20000 * ir.getInnerAvgPerSize () << endl;
  //cout << "Test of class ImageResizer completed successfully.\n";
  //TEST_NO_EXCEPTION (result->write("aus.jpg"));

  TEST_NO_EXCEPTION (result = ir.resize(0, 0));
  cout << "score: " << 20000 * ir.getInnerAvgPerSize () << endl;
  TEST_NO_EXCEPTION (result->write("autocropped.jpg"));
  DELETE (result);

  TEST_NO_EXCEPTION (result = img->fitInto (80, 300, red));
  TEST_NO_EXCEPTION (result->write("simple.jpg"));
  DELETE (result);


  testColorDumbResizing ();
  testGreyscaleDumbResizing ();

  return 0;
}

static void
testGreyscaleDumbResizing (void)
{
  cout << " ... greyscale dumb resizing... " << endl;
  Image *img = NEW(StillImage ());
  TEST_NO_EXCEPTION (img->read ("lena.pgm")); // 512x448
  cout << "     source rows: " << img->rows() << ", cols: " << img->cols() << endl;

  Image *result = NULL;

  TEST_NO_EXCEPTION(result = img->fitInto(480, 420));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("lena-480x420.pgm"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(480, 420, 255));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("lena-480x420-fill.pgm"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(530, 448));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("lena-530x448.pgm"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(530, 448, 255));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("lena-530x448-fill.pgm"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(440, 530));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("lena-440x530.pgm"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(440, 530, 255));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("lena-440x530-fill.pgm"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(530, 420));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("lena-530x420.pgm"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(530, 420, 255));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("lena-530x420-fill.pgm"));
  DELETE (result);

  DELETE(img);
}


static void
testColorDumbResizing (void)
{
  cout << " ... color dumb resizing... " << endl;
  ColorImage *img = NEW (ColorImage ());
  TEST_NO_EXCEPTION (img->read ("christina57.jpg")); // 594x448
  cout << "     source rows: " << img->rows() << ", cols: " << img->cols() << endl;
  cout << "     source colormodel: " << img->colormodel() << endl;

  ColorImage *result = NULL;
  int red[3] = { 255, 0, 0 };
  int blue[3] = { 0, 255, 0 };
  int green[3] = { 0, 0, 255 };
  int white[3] = { 255, 255, 255 };

  TEST_NO_EXCEPTION(result = img->fitInto(550, 420));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("christina-550x420.jpg"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(550, 420, red));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("christina-550x420-fill.jpg"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(600, 580));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("christina-600x580.jpg"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(600, 580, blue));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("christina-600x580-fill.jpg"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(550, 420));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("christina-550x420.jpg"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(550, 420, green));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("christina-550x420-fill.jpg"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(680, 400));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("christina-680x400.jpg"));
  DELETE (result);

  TEST_NO_EXCEPTION(result = img->fitInto(680, 400, white));
  cout << "     rows: " << result->rows() << ", cols: " << result->cols() << endl;
  TEST_NO_EXCEPTION(result->write("christina-680x400-fill.jpg"));
  DELETE (result);

  DELETE(img);
}

