/** 
* @file FeatureFactory.cpp
* @brief Abstract class for image classification
* @author Erik Rodner
* @date 07.09.2007

*/
#include <iostream>

#include "vislearning/features/fbase/FeatureFactory.h"

#include "vislearning/baselib/ProgressBar.h"
#include "vislearning/baselib/Preprocess.h"
#include "vislearning/baselib/Globals.h"

using namespace OBJREC;

using namespace std;
using namespace NICE;


FeatureFactory::FeatureFactory( const Config *_conf )
{
    Preprocess::Init ( _conf );
}

int FeatureFactory::convert ( const NICE::Image & img, NICE::Vector & vec )
{
    fprintf (stderr, "FeatureFactory::convert: not yet implemented !\n");
    exit(-1);
}

int FeatureFactory::convertRGB ( const NICE::ColorImage & img, NICE::Vector & vec )
{
     NICE::Vector tmp,vecr,vecg,vecb;
     NICE::Image *r = img.getChannel (0);
     NICE::Image *g = img.getChannel (1);
     NICE::Image *b = img.getChannel (2);

     //create feature vectors
     int ret;
     if(convert(*r,vecr)>=0 && convert(*g,vecg)>=0 && convert(*b,vecb)>=0)
     {
	  vec.resize( vecr.size() + vecg.size() + vecb.size() );
	  for ( unsigned int i = 0 ; i < vecr.size() ; i++ )
	      vec[i] = vecr[i];
	  for ( unsigned int i = 0 ; i < vecg.size() ; i++ )
	      vec[i+vecr.size()] = vecg[i];
	  for ( unsigned int i = 0 ; i < vecb.size() ; i++ )
	      vec[i+vecr.size()+vecb.size()] = vecb[i];
          ret = 0;
     } else {
          ret = -1;
     }

     delete r;
     delete g;
     delete b;

     return ret;
}

FeatureFactory::~FeatureFactory()
{
}

int FeatureFactory::convertSet ( const LabeledSet *filelist, 
				 LabeledSetVector & storage )
{
    ProgressBar pb ("Feature Conversion");
    pb.show();

    LOOP_ALL_S(*filelist)
    {
	  EACH_S(classno,fn);

	  pb.update ( filelist->count() );
	  NICE::Image img = Preprocess::ReadImgAdv ( fn );
	  Globals::setCurrentImgFN(fn);

	  NICE::Vector x;
	  if ( convert ( img, x ) < 0 ) 
	  {
	      fprintf (stderr, "FeatureFactory: error building feature vector: %s !\n", fn.c_str() );
	      continue;
	  }
	     
	  storage.add (classno,x);
    }
    
    return 0;
}
	
int FeatureFactory::convertSetRGB ( const LabeledSet *filelist, 
				    LabeledSetVector & storage )
{
    LOOP_ALL_S(*filelist)
    {
	  EACH_S(classno,fn);
	  NICE::ColorImage img = Preprocess::ReadImgAdvRGB ( fn );
	  Globals::setCurrentImgFN(fn);

	  NICE::Vector x;
	  if ( convertRGB ( img, x ) < 0 ) 
	  {
	      fprintf (stderr, "FeatureFactoy: error building feature vector: %s !\n", fn.c_str() );
	      continue;
	  }
	     
	  storage.add (classno,x);
    }

    return 0;
}

