/**
* @file LFColorSande.cpp
* @brief interface to ColorSande implementation
* @author Erik Rodner, Alexander Freytag
* @date 11/19/2007
*/

// STL includes
#include <errno.h>
#include <iostream>
#include <sstream>

// nice-core includes
#include <core/basics/StringTools.h>
#include <core/basics/FileMgt.h>
// 
#include <core/image/Convert.h>

// nice-vislearning includes
#include <vislearning/baselib/Globals.h>
// 
#include "LFColorSande.h"


#define DESCSIZE_DUMMY "/home/bachi/HiWi/nice/nice/objrec-froehlichexp/progs/IMG_8762.JPG"



using namespace OBJREC;
using namespace std;
using namespace NICE;

///////////////////// ///////////////////// /////////////////////
//                   CONSTRUCTORS / DESTRUCTORS
///////////////////// ///////////////////// /////////////////

LFColorSande::LFColorSande() : LocalFeatureRepresentation () 
{
  this->c_binaryExecutable = "";
  this->c_params           = "--descriptor opponentsift";
  this->scales             = "1+1.5+3.0+4.5+6";
  this->descriptor_size    = -1;
  this->usegrid            = false;
  
  int gridsizeInt             = 5;
  std::ostringstream temp;
  temp << gridsizeInt;
  this->gridsize = temp.str(); 
}

LFColorSande::LFColorSande ( const NICE::Config * _conf, std::string _confSection )
{
 
  this->initFromConfig( _conf );
}

LFColorSande::~LFColorSande()
{
}

void LFColorSande::initFromConfig(const NICE::Config * _conf, const std::string & _confSection)
{
  try
  {
    //a possible location on dbv could be: "/home/bachi/libs/van_de_sande/x86_64-linux-gcc/colorDescriptor" );
    this->c_binaryExecutable = _conf->gS ( _confSection, "binaryExecutable" /*we do not add a default here, this has to adapted to your system!!!*/); 
  }
  catch (NICE::Exception exception )
  {
    std::cerr << "\nWARNING --- Add the location where the colorDescriptor-binary is located. \n  A possible location on dbv could be: \"/home/bachi/libs/van_de_sande/x86_64-linux-gcc/colorDescriptor\" \n Source code be obtained at http://staff.science.uva.nl/~ksande/research/colordescriptors/ \n" << std::endl;
    throw exception;
  }

  this->c_params        = _conf->gS ( _confSection, "params", "--descriptor opponentsift" );
  this->scales          = _conf->gS ( _confSection, "scales", "1+1.5+3.0+4.5+6" );
  this->descriptor_size = _conf->gI ( _confSection, "descriptor_size", -1 );
  this->usegrid         = _conf->gB ( _confSection, "usegrid", false );

  int gridsizeInt = _conf->gI ( _confSection, "grid", 5 );
  std::ostringstream temp;
  temp << gridsizeInt;
  this->gridsize        = temp.str();
  
  if ( descriptor_size <= 0 )
  {
    fprintf ( stderr, "LFColorSande::LFColorSande: No descriptor size found in config -> self test\n" );
    
    /** get feature dimension **/
    NICE::Image testimg ( DESCSIZE_DUMMY );
    NICE::VVector features;
    NICE::VVector positions;
    
    this->extractFeatures ( testimg, features, positions );
    
    if ( features.size() <= 0 )
      fthrow ( Exception, "No features found in " << DESCSIZE_DUMMY << " picture." );
    this->descriptor_size = features[0].size();

    fprintf ( stderr, "LFColorSande::LFColorSande Self Test features:%d dimension:%d\n", ( int ) features.size(), descriptor_size );
  }
  
  if ( descriptor_size != _conf->gI ( "features", "descriptor_size", descriptor_size ) )
  {
    cerr << "Warning: LFColorSande: descriptor sizes do not match !!!" << endl;
  }  
}


///////////////////// ///////////////////// /////////////////////
//                      FEATURE STUFF
///////////////////// ///////////////////// ////////////////// 

int LFColorSande::getDescSize () const
{
  return descriptor_size;
}

int LFColorSande::extractFeatures ( const NICE::Image & img, VVector & features,
                                    VVector & positions ) const
{
  cerr << "Warning: LFColorSande is a color local feature implementation, but you are calling the gray-image version of extractFeatures" << endl;
  NICE::ColorImage colorimg;
  NICE::grayToRGB ( img, &colorimg );
  extractFeatures ( colorimg, features, positions );

  return 0;
}

int LFColorSande::extractFeatures ( const NICE::ColorImage & img, VVector & features, VVector & positions ) const
{
  if ( features.size() != positions.size() )
  {
    positions.clear();
  }

  bool delete_imgfile = false;
  std::string imgfile = Globals::getCurrentImgFN();

  fprintf ( stderr, "imgfile: %s\n", imgfile.c_str() );
  if ( ( imgfile.size() <= 0 ) || ( ( !StringTools::regexMatch ( imgfile, ".[Jj][pP][Gg]$" ) )
                                    && ( !StringTools::regexMatch ( imgfile, ".[Pp][Nn][Gg]$" ) ) ) )
  {

    if ( imgfile.size() <= 0 )
    {
      imgfile = FileMgt::createTempFile ( "/tmp/osl_lfColorSande_input_%s.png" );
      fprintf ( stderr, "LFColorSande: write image to %s (write image)\n", imgfile.c_str() );
      ImageFile imgf ( imgfile );
      imgf.writer ( &img );
    } else {
      std::string tmpfile = FileMgt::createTempFile ( "/tmp/osl_lfColorSande_input_%s.png" );
      fprintf ( stderr, "LFColorSande: write image to %s (convert)\n", tmpfile.c_str() );

      std::string convertcall = "convert " + imgfile + " " + tmpfile;
      cerr << "convert call: " << convertcall << endl;
      system ( convertcall.c_str() );

      imgfile = tmpfile;
    }

    delete_imgfile = true;
  }

  std::string outputfn  = FileMgt::createTempFile ( "/tmp/osl_lfColorSande_output_%s" ) ;

  std::string gridparams = "";

  if ( usegrid )
    gridparams = " --detector densesampling --ds_spacing " + gridsize + " --ds_scales " + scales;

  std::string call = c_binaryExecutable + " " +
                     imgfile            + " " +
                     c_params           +
                     gridparams         +
                     " -output "        + outputfn;

  cerr << "LFColorSande: parameters: <" << c_params + gridparams << ">" << endl;
  clog << "Systemcall: " << call << endl;


  const int buffersize = 65536;
  char *buffer = new char [buffersize];
  FILE *f = popen ( call.c_str(), "r" );
  if ( f == NULL )
  {
    fthrow ( Exception, "Unable to run the implementation of van de Sande: " << call << endl << strerror ( errno ) );
  }

  while ( ! feof ( f ) )
  {
    if ( fgets ( buffer, buffersize, f ) <= 0 )
    {
      break;
    } else {
      fprintf ( stderr, "LFColorSande::extractFeatures: [INFO] %s", buffer );
    }
  }


  pclose ( f );

  f = fopen ( outputfn.c_str(), "r" );
  if ( f == NULL )
  {
    fthrow ( Exception, "Unable to read output of van de Sande implementation\n" );
  }

  if ( fgets ( buffer, buffersize, f ) <= 0 )
  {
    fprintf ( stderr, "LFColorSande::extractFeatures: output is empty !\n" );
    fprintf ( stderr, "img %s out %s\n", imgfile.c_str(), outputfn.c_str() );
    fprintf ( stderr, "call %s\n", call.c_str() );

    exit ( -1 );
  }

  if ( ! strcmp ( buffer, "KOEN1" ) )
  {
    fprintf ( stderr, "LFColorSande::extractFeatures: wrong file format\n" );
    fprintf ( stderr, "img %s out %s\n", imgfile.c_str(), outputfn.c_str() );
    fprintf ( stderr, "call %s\n", call.c_str() );

    exit ( -1 );
  }


  fgets ( buffer, buffersize, f );
  int dimension = atoi ( buffer );
  fprintf ( stderr, "LFColorSande: descriptor dimension = %d\n", dimension );

  if ( ( descriptor_size > 0 ) && ( dimension != descriptor_size ) )
  {
    fprintf ( stderr, "LFColorSande::extractFeatures: dimensions do not match %d -> %d!\n",
              dimension, descriptor_size );
    fprintf ( stderr, "img %s out %s\n", imgfile.c_str(), outputfn.c_str() );
    fprintf ( stderr, "call %s\n", call.c_str() );
    fprintf ( stderr, "dimension std::string buffer: \"%s\"", buffer );
    exit ( -1 );
  }

  fgets ( buffer, buffersize, f );
  int noDesc = atoi ( buffer );

  fprintf ( stderr, "LFColorSande::extractFeatures: no. of descriptors = %d\n", noDesc );

  NICE::Vector x;
  while ( ! feof ( f ) )
  {
    // <CIRCLE 119 307 1.26134 0 0.00014763>; 0 0 6 2 0 6 25 7 9 4 4 0 0 4 20 36 78 4 5 0 0
    //<CIRCLE x y scale orientation cornerness>
    if ( fgets ( buffer, buffersize, f ) == NULL )
      break;

    const char *buffer_data = strchr ( buffer, ';' );
    if ( strlen ( buffer_data ) <= 0 )
    {
      fprintf ( stderr, "LFColorSande::extractFeatures: it seems you forget to specify a descriptor\n" );
      exit ( -1 );
    }

    const char *buffer_key_start = strchr ( buffer, ' ' );
    const char *buffer_key_end = strchr ( buffer, '>' );
    buffer_key_start++;
    int keylen = buffer_key_end - buffer_key_start;
    char *key = new char [keylen+1];
    strncpy ( key, buffer_key_start, keylen );
    key[keylen] = '\0';

    buffer_data += 2;

    std::string buffer_data_s ( buffer_data );

    //clog << "[log] buffer = " << buffer_data_s << endl;

    StringTools::splitVector ( buffer_data_s, ' ', x );
    if ( ( int ) x.size() != dimension )
    {
      cerr << "Line = " << buffer_data_s << endl;
      cerr << "Vector = " << x << endl;
      cerr << "dimension = " << dimension << endl;
      cerr << "x.size() = " << x.size() << endl;
      fprintf ( stderr, "LFColorSande::extractFeatures: error parsing output !!\n" );
      exit ( -1 );
    } else {
      NICE::Vector pos;

      StringTools::splitVector ( key, ' ', pos );

      if ( pos.size() != 5 ) {
        fprintf ( stderr, "LFColorSande::extractFeatures: dimension mismatch (position information)\n" );
        exit ( -1 );
      }
      // Van De Sande verwendet MatLab-Darstellung der Koordinaten. Diese fangen bei 1 an und nicht bei 0.
      pos[0] --;
      pos[1] --;
      positions.push_back ( pos );
      features.push_back ( x );
    }
  }

  fclose ( f );

  if ( delete_imgfile )
    FileMgt::deleteTempFile ( imgfile );

  FileMgt::deleteTempFile ( outputfn );

  delete [] buffer;

  return 0;
}

///////////////////// INTERFACE PERSISTENT /////////////////////
// interface specific methods for store and restore
///////////////////// INTERFACE PERSISTENT ///////////////////// 

void LFColorSande::restore ( std::istream & is, int format )
{
  //delete everything we knew so far...
  this->clear();
  
  
  if ( is.good() )
  {
    
    std::string tmp;
    is >> tmp; //class name 
    
    if ( ! this->isStartTag( tmp, "LFColorSande" ) )
    {
      std::cerr << " WARNING - attempt to restore LFColorSande, but start flag " << tmp << " does not match! Aborting... " << std::endl;
      throw;
    }   
    
    bool b_endOfBlock ( false ) ;
    
    while ( !b_endOfBlock )
    {
      is >> tmp; // start of block 
      
      if ( this->isEndTag( tmp, "LFColorSande" ) )
      {
        b_endOfBlock = true;
        continue;
      }      
      
      tmp = this->removeStartTag ( tmp );     
      
      if ( tmp.compare("c_binaryExecutable") == 0 )
      {
        is >> this->c_binaryExecutable;
        is >> tmp; // end of block 
        tmp = this->removeEndTag ( tmp );
      }
      else if ( tmp.compare("c_params") == 0 )
      {
        is >> this->c_params;
        is >> tmp; // end of block 
        tmp = this->removeEndTag ( tmp );
      }      
      else if ( tmp.compare("scales") == 0 )
      {
        is >> this->scales;
        is >> tmp; // end of block 
        tmp = this->removeEndTag ( tmp );
      }
      else if ( tmp.compare("descriptor_size") == 0 )
      {
        is >> this->descriptor_size;
        is >> tmp; // end of block 
        tmp = this->removeEndTag ( tmp );
      }      
      else if ( tmp.compare("usegrid") == 0 )
      {
        is >> this->usegrid;
        is >> tmp; // end of block 
        tmp = this->removeEndTag ( tmp );
      }
      else if ( tmp.compare("gridsize") == 0 )
      {
        is >> this->gridsize;
        is >> tmp; // end of block 
        tmp = this->removeEndTag ( tmp );
      }     
      else
      {
      std::cerr << "WARNING -- unexpected LFColorSande object -- " << tmp << " -- for restoration... aborting" << std::endl;
      throw;
      }
    }
  }
  else
  {
    std::cerr << "LFColorSande::restore -- InStream not initialized - restoring not possible!" << std::endl;
    throw;
  }
}

void LFColorSande::store ( std::ostream & os, int format ) const
{ 
  if (os.good())
  {
    // show starting point
    os << this->createStartTag( "LFColorSande" ) << std::endl; 

    
    os << this->createStartTag( "c_binaryExecutable" ) << std::endl;
    os << this->c_binaryExecutable << std::endl;
    os << this->createEndTag( "c_binaryExecutable" ) << std::endl;  

    os << this->createStartTag( "c_params" ) << std::endl;
    os << this->c_params << std::endl;
    os << this->createEndTag( "c_params" ) << std::endl;
    
    os << this->createStartTag( "scales" ) << std::endl;
    os << this->scales << std::endl;
    os << this->createEndTag( "scales" ) << std::endl;  

    os << this->createStartTag( "descriptor_size" ) << std::endl;
    os << this->descriptor_size << std::endl;
    os << this->createEndTag( "descriptor_size" ) << std::endl; 
    
    os << this->createStartTag( "usegrid" ) << std::endl;
    os << this->usegrid << std::endl;
    os << this->createEndTag( "usegrid" ) << std::endl;
    
    os << this->createStartTag( "gridsize" ) << std::endl;
    os << this->gridsize << std::endl;
    os << this->createEndTag( "gridsize" ) << std::endl;     
    
    // done
    os << this->createEndTag( "LFColorSande" ) << std::endl;    
  }
  else
  {
    std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  }
}

void LFColorSande::clear ()
{
}