/**
* @file ICETools.cpp
* @brief simple ICE GUI tools
* @author Erik Rodner
* @date 03/13/2008

*/
#include "core/image/ImageT.h"
#include "core/vector/VectorT.h"
#include "core/vector/MatrixT.h"
#include <core/image/Filter.h>
#include <core/image/Convert.h>

#include <core/imagedisplay/ImageDisplay.h>

#include <iostream>

#include <core/image/RectT.h>

#include "vislearning/baselib/ICETools.h"

#include "core/basics/Exception.h"

using namespace OBJREC;

using namespace std;

using namespace NICE;

ICETools::ICETools()
{
}

ICETools::~ICETools()
{
}

void ICETools::selectRectangles ( const NICE::Image & panel, NICE::Image & overlay, vector<Vector> & x, int color )
{
  fthrow ( Exception, "ICETools::selectRectangles -- not yet implemented due to old ICE version." );
}

void ICETools::selectPoints ( const NICE::Image & panel, NICE::Image & overlay, vector<Vector> & x, int color )
{
  fthrow ( Exception, "ICETools::selectPoints -- not yet implemented due to old ICE version." );
}

void ICETools::convertToRGB ( const NICE::Matrix & m, NICE::ColorImage & img )
{
  matrixToPseudoColor(m, img);
}

void ICETools::convertToRGB ( const NICE::FloatImage & m, NICE::ColorImage & img )
{
  imageToPseudoColor(m, img);
}

void ICETools::calcGrayImage ( const NICE::ColorImage & img, NICE::Image & imgg )
{
  imgg.resize ( img.width(), img.height() );
  for ( int y = 0 ; y < img.height(); y++ )
    for ( int x = 0 ; x < img.width() ; x++ )
    {
      unsigned char g = ( unsigned char ) ( 0.299 * img.getPixel ( x, y, 0 ) +
                                            0.587 * img.getPixel ( x, y, 1 ) + 0.114 * img.getPixel ( x, y, 2 ) );
      imgg.setPixel ( x, y, g );
    }
}

double *ICETools::convertICE2M ( const NICE::Matrix & M )
{
  double *raw = new double [ M.rows() * M.cols() ];
  long index = 0;
  for ( uint i = 0 ; i < M.rows() ; i++ )
    for ( uint j = 0 ; j < M.cols() ; j++, index++ )
      raw[index] = M ( i, j );

  return raw;
}

void ICETools::convertM2ICE ( NICE::Matrix & M, double *raw )
{
  long index = 0;
  for ( int i = 0 ; i < ( int ) M.rows() ; i++ )
    for ( int j = 0 ; j < ( int ) M.cols() ; j++, index++ )
      M ( i, j ) = raw[index];
}

#ifndef NOVISUAL
int ICETools::markImage ( const NICE::Image & img, NICE::Image & mark, int marksize, int color )
{
  fprintf ( stderr, "ICETools::markImage: reimplement this function for NICE\n" );
  exit ( -1 );
  return 0;
}

int ICETools::showImages ( vector<NICE::Image> & imagelist )
{
#ifndef NOVISUAL
  for ( size_t j = 0 ; j < imagelist.size() ; j++ )
  {
    showImage ( imagelist[j] );
  }
#endif

  int xsize = imagelist[0].width();
  int ysize = imagelist[0].height();

  int n = imagelist.size();
  int n1 = ( int ) sqrt ( (double)n );
  int n2 = n / n1 + 1;

  NICE::Image img ( n1*xsize, n2*ysize );
  img.set ( 0 );
  int k = 0;
  for ( int j = 0 ; j < n2 ; j++ )
    for ( int i = 0 ; i < n1 ; i++, k++ )
      if ( k >= n ) break;
      else {
        for ( int y = 0 ; y < imagelist[k].height(); y++ )
          for ( int x = 0 ; x < imagelist[k].width(); x++ )
          {
            img.setPixel ( i*xsize + x, j*ysize + y,
                           imagelist[k].getPixel ( x, y ) );
          }
      }

  img.write ( "display.bmp" );

  getchar();

  return 0;
}

#endif

