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

*/
#include <errno.h>
#include <iostream>

#include <core/image/Convert.h>

#include "core/basics/StringTools.h"
#include "core/basics/FileMgt.h"
#include "vislearning/baselib/Globals.h"

#include "vislearning/features/localfeatures/LFColorSande.h"


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

#include <sstream>

using namespace OBJREC;

using namespace std;
using namespace NICE;



LFColorSande::LFColorSande ( const Config *conf, std::string section )
{
  std::cerr << "LF COLOR SANDE SECTION ====================== :" << section << ":"<<std::endl;
  
  try
  {
    //a possible location on dbv could be: "/home/bachi/libs/van_de_sande/x86_64-linux-gcc/colorDescriptor" );
    c_binaryExecutable = conf->gS ( section, "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;
  }

  c_params = conf->gS ( section, "params", "--descriptor opponentsift" );
  scales = conf->gS ( section, "scales", "1+1.5+3.0+4.5+6" );
  std::cerr << "scales: " << scales << std::endl;

  descriptor_size = conf->gI ( section, "descriptor_size", -1 );

  usegrid = conf->gB ( section, "usegrid", false );

  int g = conf->gI ( section, "grid", 5 );
  std::ostringstream temp;
  temp << g;
  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 );
    VVector features;
    VVector positions;
    extractFeatures ( testimg, features, positions );
    if ( features.size() <= 0 )
      fthrow ( Exception, "No features found in " << DESCSIZE_DUMMY << " picture." );
    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;
  }  
}

LFColorSande::~LFColorSande()
{
}

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;
}
