/** 
* @file FCGreyValues.cpp
* @brief extract simple greyvalue features
* @author Erik Rodner
* @date 11/15/2007

*/
#include <iostream>

#include "vislearning/features/simplefeatures/FCGreyValues.h"

using namespace OBJREC;

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



FCGreyValues::FCGreyValues( const Config * conf, int _xsize, int _ysize ) : 
    FeatureFactory ( conf ), xsize(_xsize), ysize(_ysize)
{
    xsize = conf->gI("FCGreyValues", "xsize", _xsize <= 0 ? 20 : _xsize  );
    ysize = conf->gI("FCGreyValues", "ysize", _ysize <= 0 ? 50 : _ysize );

    // refactor-nice.pl: check this substitution
    // old: string normalization_s = conf->gS("FCGreyValues", "normalization", "n01");
    std::string normalization_s = conf->gS("FCGreyValues", "normalization", "n01");

    if ( normalization_s == "n01" )
	normalization = NORMALIZE_N01;
    else if ( normalization_s == "stddev" )
	normalization = NORMALIZE_STDDEV;
    else if ( normalization_s == "mean" )
	normalization = NORMALIZE_MEAN;
    else if ( normalization_s == "none" )
	normalization = NORMALIZE_NONE;
    else {
	fprintf (stderr, "FCGreyValues::FCGreyValues: unknown normalization method\n");
	exit(-1);
    }
   
    // refactor-nice.pl: check this substitution
    // old: tmp = NewImg(xsize, ysize, 255);
    tmp.resize(xsize, ysize);

}

FCGreyValues::~FCGreyValues()
{
}

// refactor-nice.pl: check this substitution
// old: int FCGreyValues::convert ( const Image & img, Vector & vec )
int FCGreyValues::convert ( const NICE::Image & img, NICE::Vector & vec )
{
    tmp.set(0);
    NICE::scale ( img, &tmp );
    vec.resize(xsize*ysize);
    
    double mean = 0.0;
    double stddev = 0.0;

    if ( normalization != NORMALIZE_NONE )
    {
	// compute mean
	for ( int yi = 0 ; yi < ysize ; yi++ )
	    for ( int xi = 0 ; xi < xsize ; xi++ )
		// refactor-nice.pl: check this substitution
		// old: mean += GetVal(tmp, xi, yi);
		mean += tmp.getPixel(xi,yi);
	
	mean /= (xsize*ysize);

	if ( normalization != NORMALIZE_MEAN )
	{
	    // compute stddev
	    stddev = 0.0;
	    for ( int yi = 0 ; yi < ysize ; yi++ )
		for ( int xi = 0 ; xi < xsize ; xi++ )
		{
		    // refactor-nice.pl: check this substitution
		    // old: double d = GetVal(tmp,xi,yi) - mean;
		    double d = tmp.getPixel(xi,yi) - mean;
		    stddev += d*d;
		}

	    if ( stddev < 10e-5 ) return -1;

	    stddev /= xsize*ysize;
	    stddev = sqrt(stddev);
	}
    }

    if ( normalization == NORMALIZE_STDDEV )
    {
	// normalize pixel values
	int k = 0;
	for ( int yi = 0; yi < ysize ; yi++ )
	    for ( int xi = 0; xi < xsize ; xi++,k++ )
		// refactor-nice.pl: check this substitution
		// old: vec[k] = (GetVal(tmp,xi,yi) - mean) / stddev + mean;
		vec[k] = (tmp.getPixel(xi,yi) - mean) / stddev + mean;
    } else if ( normalization == NORMALIZE_N01 ) {
	// normalize pixel values
	int k = 0;
	for ( int yi = 0 ; yi < ysize ; yi++ )
	    for ( int xi = 0 ; xi < xsize ; xi++,k++ )
		// refactor-nice.pl: check this substitution
		// old: vec[k] = (GetVal(tmp,xi,yi) - mean) / stddev;
		vec[k] = (tmp.getPixel(xi,yi) - mean) / stddev;
    } else if ( normalization == NORMALIZE_MEAN ) {
	// normalize pixel values
	int k = 0;
	for ( int yi = 0 ; yi < ysize ; yi++ )
	    for ( int xi = 0 ; xi < xsize ; xi++,k++ )
		// refactor-nice.pl: check this substitution
		// old: vec[k] = (GetVal(tmp,xi,yi) - mean);
		vec[k] = (tmp.getPixel(xi,yi) - mean);

    } else {
	int k = 0;
	for ( int yi = 0 ; yi < ysize ; yi++ )
	    for ( int xi = 0 ; xi < xsize ; xi++,k++ )
		// refactor-nice.pl: check this substitution
		// old: vec[k] = GetVal(tmp,xi,yi);
		vec[k] = tmp.getPixel(xi,yi);
    }

    return 0;
}