/** 
* @file PSSBackgroundModel.cpp
* @brief simple background models
* @author Erik Rodner
* @date 03/19/2009

*/
#ifdef NOVISUAL
#include <objrec/nice_nonvis.h>
#else
#include <objrec/nice.h>
#endif

#include <iostream>

#include "PSSBackgroundModel.h"

using namespace OBJREC;

using namespace std;
// refactor-nice.pl: check this substitution
// old: using namespace ice;
using namespace NICE;



PSSBackgroundModel::PSSBackgroundModel( int backgroundModelType, double threshold, int backgroundClass )
{
    this->backgroundModelType = backgroundModelType;
    this->threshold = threshold;
    this->backgroundClass = backgroundClass;
}

PSSBackgroundModel::~PSSBackgroundModel()
{
}

	
// refactor-nice.pl: check this substitution
// old: void PSSBackgroundModel::postprocess ( Image & result, GenericImage<double> & probabilities )
void PSSBackgroundModel::postprocess ( NICE::Image & result, GenericImage<double> & probabilities )
{
    if ( backgroundModelType == BGM_FIXED_ENTROPY_THRESHOLD )
    {
	if ( threshold >= 1.0 ) return;

	int numClasses = probabilities.numChannels;
	double t = log(numClasses)*threshold;
	int xsize = probabilities.xsize;
	int ysize = probabilities.ysize;
	int offset_s = 0;
	for (  int ys = 0 ; ys < ysize ; ys ++ ) 
	    for ( int xs = 0 ; xs < xsize ; xs++,offset_s++ )  
	    {
		double entropy = 0.0;
		double sum = 0.0;
		for ( int i = 0 ; i < numClasses ; i++ )
		{
		    double val = probabilities.data[i][offset_s];
		    if ( val <= 0.0 ) continue;
		    entropy -= val*log(val);
		    sum += val;
		}
		entropy /= sum;
		entropy += log(sum);
		
		if ( entropy > t )
		    result.setPixel(xs,ys,backgroundClass);
	    }
    } else if ( backgroundModelType == BGM_ADAPTIVE_ENTROPY_THRESHOLD ) {
	fprintf (stderr, "not yet implemented !!\n");
	exit(-1);
    }

}