/**
* @file LFWriteCache.cpp
* @brief read local features from file
* @author Erik Rodner
* @date 02/14/2008

*/

#include "core/vector/VectorT.h"
#include "core/vector/MatrixT.h"
#include "core/image/ImageT.h"

#include <iostream>
#include <fstream>

#include <sys/stat.h>
#include <sys/types.h>

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

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

using namespace OBJREC;

using namespace std;
using namespace NICE;



LFWriteCache::LFWriteCache ( const Config *conf,
                             const LocalFeatureRepresentation *_lfrep,
                             const std::string & _section ) : lfrep ( _lfrep )
{
  cachedir = conf->gS ( _section, "root" );
  cachemode = Globals::getCacheMode ( conf->gS ( _section, "mode", "cat" ) );

  std::string descFormat_s = conf->gS ( _section, "descriptor_format", "binary_double" );
  if ( descFormat_s == "binary_double" )
    descFormat = VVector::FILEFORMAT_BINARY_DOUBLE;
  else if ( descFormat_s == "binary_uchar" )
    descFormat = VVector::FILEFORMAT_BINARY_CHAR;
  else if ( descFormat_s == "text_line" )
    descFormat = VVector::FILEFORMAT_LINE;
  else if ( descFormat_s == "text_ice" )
    descFormat = VVector::FILEFORMAT_NICE;

}

LFWriteCache::~LFWriteCache()
{
}

int LFWriteCache::getDescSize () const
{
  return lfrep->getDescSize();
}

int LFWriteCache::extractFeatures ( const NICE::Image & img,
                                    VVector & features,
                                    VVector & positions ) const
{
  std::string filename = Globals::getCacheFilename ( cachedir, cachemode );

  int ret = lfrep->extractFeatures ( img, features, positions );
  std::string filename_desc = filename + ".desc";
  std::string filename_pos = filename + ".key";

  struct stat dummy;
  if ( stat ( filename_desc.c_str(), &dummy ) < 0 )
  {
    fprintf ( stderr, "features: %d, positions: %d\n", ( int ) features.size(),
              ( int ) positions.size() );
    if ( features.size() > 0 )
      fprintf ( stderr, "feature dimension: %d %d\n", features[0].size(), lfrep->getDescSize() );
    features.save ( filename_desc, descFormat );
    positions.save ( filename_pos, VVector::FILEFORMAT_LINE );
  } else {
    fprintf ( stderr, "description file %s exists !\n", filename_desc.c_str() );
  }


  return ret;
}

int LFWriteCache::extractFeatures ( const NICE::ColorImage & img,
                                    VVector & features,
                                    VVector & positions ) const
{
  std::string filename = Globals::getCacheFilename ( cachedir, cachemode );

  int ret = lfrep->extractFeatures ( img, features, positions );
  std::string filename_desc = filename + ".desc";
  std::string filename_pos = filename + ".key";

  struct stat dummy;

  if ( stat ( filename_desc.c_str(), &dummy ) < 0 )
  {
    fprintf ( stderr, "features: %d, positions: %d\n", ( int ) features.size(),
              ( int ) positions.size() );
    if ( features.size() > 0 )
      fprintf ( stderr, "feature dimension: %d %d\n", features[0].size(), lfrep->getDescSize() );
    features.save ( filename_desc, descFormat );
    positions.save ( filename_pos, VVector::FILEFORMAT_LINE );
  }
  else
  {
    fprintf ( stderr, "description file %s exists !\n", filename_desc.c_str() );
  }


  return ret;
}

void LFWriteCache::visualize ( NICE::Image & img,
                               const NICE::Vector & feature ) const
{
  lfrep->visualize ( img, feature );
}

void LFWriteCache::visualizeFeatures ( NICE::Image & mark,
                                       const VVector & positions,
                                       size_t color ) const
{
  lfrep->visualizeFeatures ( mark, positions, color );
}

