/**
* @file testCachedExample.cpp
* @brief test cached example implementation
* @author Erik Rodner
* @date 09/12/2008
*/

#include <core/imagedisplay/ImageDisplay.h>
#include <core/basics/Config.h>
#include <vislearning/baselib/cmdline.h>
#include <vislearning/image/GenericImageTools.h>

#include <vislearning/cbaselib/CachedExample.h>

using namespace OBJREC;

using namespace NICE;
using namespace std;


int main ( int argc, char **argv )
{
  std::set_terminate ( __gnu_cxx::__verbose_terminate_handler );

  char configfile [300];

  struct CmdLineOption options[] = {
    {"config", "use config file", NULL, "%s", configfile},
    {NULL, NULL, NULL, NULL, NULL}
  };
  int ret;
  char *more_options[argc];
  ret = parse_arguments ( argc, ( const char** ) argv, options, more_options );

  if ( ret != 0 )
  {
    if ( ret != 1 ) fprintf ( stderr, "Error parsing command line !\n" );
    exit ( -1 );
  }

  Config conf ( configfile );

  int i = 0;
  while ( more_options[i] != NULL )
  {

    std::string filename ( more_options[i] );
    fprintf ( stderr, "Filename: %s\n", filename.c_str() );
    CachedExample ce ( filename );

    NICE::MultiChannelImageT<int> & img = ce.getIChannel ( CachedExample::I_COLOR );
    NICE::MultiChannelImageT<double> & imgc = ce.getDChannel ( CachedExample::D_INTEGRALCOLOR );

    imgc.reInitFrom ( img );
    for ( uint j = 0 ; j < img.channels(); j++ )
    {
      ImageT<double> tmp = imgc[j];
      GenericImageTools::calcIntegralImage ( tmp, img[j], img.width(), img.height() );
    }

    Image visimg = imgc.getChannel ( 0 );
    showImage ( visimg );

    ce.dropPreCached();

#ifndef NOVISUAL
    getchar();
#endif

    imgc = ce.getDChannel ( CachedExample::D_INTEGRALCOLOR );

    visimg = imgc.getChannel ( 0 );
    showImage ( visimg );


    i++;
  }


  return 0;
}