/** 
* @file ImagePyramid.cpp
* @brief gauss image pyramid
* @author Erik Rodner
* @date 02/06/2008

*/
#include <iostream>

#include "vislearning/image/ImagePyramid.h"

using namespace OBJREC;

using namespace std;

using namespace NICE;




ImagePyramid::ImagePyramid( const NICE::Image & img, 
			    int maxLevels,
			    double scaleSpacing,
			    int max_xsize,
			    int max_ysize )
{
    pyramid.push_back ( NICE::Image(img) );
    for ( int i = 1 ; i < maxLevels ; i++ )
    {

	int old_xsize = pyramid[i-1].width();

	int old_ysize = pyramid[i-1].height();
	double new_xsize = old_xsize / scaleSpacing; 
	double new_ysize = old_ysize / scaleSpacing; 

	if ( (new_xsize < max_xsize) || (new_ysize < max_ysize) )
	    break;


	NICE::Image gauss (old_xsize, old_ysize);

	filterGauss(pyramid[i-1], 2, &gauss);


	NICE::Image newLevel ((int)new_xsize, (int)new_ysize);


	// Trafo trafo;
	// trafo.Scale(0, 0, (new_xsize-1)/(old_xsize-1),
	//		  (new_ysize-1)/(old_ysize-1));
	// Transform(trafo, gauss, newLevel);
	NICE::scale ( gauss, &newLevel );

		NICE::scale ( gauss, &newLevel );

		pyramid.push_back(newLevel);
    }
}

ImagePyramid::~ImagePyramid()
{
}

const NICE::Image & ImagePyramid::getLevel ( int i ) const
{
    return pyramid[i];
}

int ImagePyramid::getNumLevels () const
{
    return pyramid.size();
}

void ImagePyramid::getOriginalCoordinates ( int x, int y, int level, double & xo, double & yo ) const
{
    int xsize_orig = pyramid[0].width();
    int ysize_orig = pyramid[0].height();
    int xsize = pyramid[level].width();
    int ysize = pyramid[level].height();

    xo = x*(double)(xsize_orig)/xsize;
    yo = y*(double)(ysize_orig)/ysize;
}

void ImagePyramid::getLevelCoordinates ( double xo, double yo, int level, double & xl, double & yl ) const
{
    int xsize_orig = pyramid[0].width();
    int ysize_orig = pyramid[0].height();
    int xsize = pyramid[level].width();
    int ysize = pyramid[level].height();

    xl = xo*(double)(xsize)/xsize_orig;
    yl = yo*(double)(ysize)/ysize_orig;
}

void ImagePyramid::show() const
{
#ifndef NOVISUAL
    for ( size_t i = 0 ; i < pyramid.size() ; i++ )
	showImage(pyramid[i]);

    /* skipped because of new visualization functions
    GetChar();
    for ( size_t i = 0 ; i < pyramid.size() ; i++ )
	Show(OFF, pyramid[i]);
    */
#else
    fprintf (stderr, "ImagePyramid::show(): visualization disabled !\n");
#endif
}
