/** 
* @file PDFImage.cpp
* @brief accumulate two dimensional density
* @author Erik Rodner
* @date 04/16/2008

*/
#include <iostream>
#include <assert.h>

#include "vislearning/math/pdf/PDFImage.h"

using namespace OBJREC;

using namespace std;
using namespace NICE;



PDFImage::PDFImage( const NICE::FloatImage & _imgd )
{
    imgd.resize (_imgd.width(), _imgd.height());
    double sum = 0.0;
    for ( int y = 0 ; y < _imgd.height(); y++ )
	for ( int x = 0 ; x < _imgd.width(); x++ )
	    sum += _imgd.getPixel(x,y);

    for ( int y = 0 ; y < _imgd.height(); y++ )
	for ( int x = 0 ; x < _imgd.width(); x++ )
	    imgd.setPixel( x, y, _imgd.getPixel(x,y) / sum ); 
}

PDFImage::~PDFImage()
{
}

// refactor-nice.pl: check this substitution
// old: double PDFImage::getNLogDensity ( const Vector & x ) const
double PDFImage::getNLogDensity ( const NICE::Vector & x ) const
{
    return - log ( getProb(x) );
}

// refactor-nice.pl: check this substitution
// old: double PDFImage::getProb ( const Vector & x ) const
double PDFImage::getProb ( const NICE::Vector & x ) const
{
    assert (x.size() == 2);
    assert ((x[0] < 1) && (x[1] < 1));
    assert ((x[0]>=0) && (x[1]>=0));
    // refactor-nice.pl: check this substitution
    // old: double val = GetValD(imgd, (int)(x[0]*imgd->xsize), (int)(x[1]*imgd->ysize));
    double val = imgd.getPixel((int)(x[0]*imgd.width()),(int)(x[1]*imgd.height()));
    return val;
}

int PDFImage::getDimension () const
{
    return 2;
}
