/** 
* @file ImageFeatures.cpp
* @brief create feature pool and additional information for ImageFeatures
* @author Erik Rodner
* @date 07/24/2008

*/
#include <iostream>

#include "EOHFeature.h"
#include "HOGFeature.h"
#include "PixelPairFeature.h"
#include "HaarFeature.h"
#include "ColorHistogramFeature.h"
#include "HistFeature.h"

#include "FIGradients.h"
#include "FIHistograms.h"

#include "ImageFeatures.h"

using namespace OBJREC;

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



// refactor-nice.pl: check this substitution
// old: ImageFeatures::ImageFeatures( const Config *_conf, const string & section ) : conf(_conf)
ImageFeatures::ImageFeatures( const Config *_conf, const std::string & section ) : conf(_conf)
{
    use_eoh_features = conf->gB(section, "use_eoh_features", false);
    use_pixelpair_features = conf->gB(section, "use_pixelpair_features", false);
    use_hog_features = conf->gB(section, "use_hog_features", false);
    use_haar_features = conf->gB(section, "use_haar_features", false);
    use_colorhistogram_features = conf->gB(section, "use_colorhistogram_features", false);

    fprintf (stderr, "ImageFeatures: section = %s\n", section.c_str() );

    if ( use_eoh_features )
	features.push_back ( new EOHFeature ( conf ) );
    if ( use_pixelpair_features )
	features.push_back ( new PixelPairFeature ( conf ) );
    if ( use_hog_features )
	features.push_back ( new HOGFeature ( conf ) );	
    if ( use_haar_features )
	features.push_back ( new HaarFeature ( conf ) );
    if ( use_colorhistogram_features )
	features.push_back ( new ColorHistogramFeature ( conf ) );
//	features.push_back ( new HistFeature ( conf, "ColorHistogramFeature", CachedExample::D_INTEGRALCOLOR ) );

}

ImageFeatures::~ImageFeatures()
{
    for ( vector<Feature *>::iterator i = features.begin();
			     	       i != features.end();
				       i++ )
    {
	Feature *f = *i;
	delete f;
    }
}

void ImageFeatures::fillFeaturePool ( FeaturePool & fp, bool variableWindow )
{
    for ( vector<Feature *>::const_iterator i = features.begin();
					    i != features.end();
					    i++ )
	(*i)->explode ( fp, variableWindow );

    if ( fp.empty() )
    {
	fprintf (stderr, "ImageFeatures::fillFeaturePool: No features selected !!\n");
	exit(-1);
    }
}

void ImageFeatures::fillExample ( CachedExample *ce )
{
    int xsize, ysize;
    ce->getImageSize( xsize, ysize );

    if ( use_eoh_features || use_hog_features )
    {
	// refactor-nice.pl: check this substitution
	// old: string subsection;
	std::string subsection;
	if ( use_eoh_features ) 
	    subsection = "EOHFeature";
	if ( use_hog_features )
	    subsection = "HOGFeature";

	int subsamplex = conf->gI(subsection, "subsamplex", 1);
	int subsampley = conf->gI(subsection, "subsampley", 1);
	int numBins = conf->gI(subsection, "num_bins", 9);
	bool usesigned = conf->gB(subsection, "use_signed", true);

	FIGradients::buildEOHMap ( ce, subsamplex, subsampley, numBins, usesigned );
    }

    if ( use_colorhistogram_features ) 
    {
	// refactor-nice.pl: check this substitution
	// old: string subsection = "ColorHistogramFeature";
	std::string subsection = "ColorHistogramFeature";
	int numBinsH = conf->gI(subsection, "num_bins_h", 4);
	int numBinsS = conf->gI(subsection, "num_bins_s", 2);
	int numBinsV = conf->gI(subsection, "num_bins_v", 2);
	int subsamplex = conf->gI(subsection, "subsamplex", 1);
	int subsampley = conf->gI(subsection, "subsampley", 1);

	FIHistograms::buildHSVMap ( ce, subsamplex, subsampley, 
				    numBinsH, numBinsS, numBinsV );
    }
}

