#include <floatfann.h>
#include "hmm.h"
#include "GLCP.h"
#include "GLCPimage.h"
#include "dbimage.h"
#include "bdbenv.h"
#include "dbalnimage.h"
#include "dbtmpfeature.h"
#include <tclap/CmdLine.h>
#include "dbhmm.h"
#include <string>

int main( int argc, char *argv[] )
{

#ifndef _NOMAGICK_

  try
    {
      ///set up the command line parsing
      TCLAP::CmdLine cmd("Image alignment program", ' ', "0.001");

      TCLAP::ValueArg<std::string> inputimage("i", "image", "image", 
                                              true, "", "0" );

      TCLAP::ValueArg< bool > fmean("", "mean", "mean flag", false,
                                    true, "true/false" );
      TCLAP::ValueArg< bool > fentropy("", "entropy", "entropy flag", false,
                                       true, "true/false" );
      TCLAP::ValueArg< bool > fcontrast("", "contrast", "contrast flag", false,
                                        true, "true/false" );
      TCLAP::ValueArg< bool > fcorrelation("", "correlation", "correlation flag",
                                           false, true, "true/false" );
      TCLAP::ValueArg< bool > fpower("", "power", "power flag", false,
                                     true, "true/false" );


      cmd.add(inputimage);
      cmd.add(fmean);
      cmd.add(fentropy);
      cmd.add(fcontrast);
      cmd.add(fcorrelation);
      cmd.add(fpower);
      cmd.parse(argc, argv);

      GLCP::mask mymask;
      if( fmean.getValue() == true )
        mymask.mean = 1;
      else
        mymask.mean = 0;

      if( fentropy.getValue() == true )
        mymask.entropy = 1;
      else
        mymask.entropy = 0;

      if( fcontrast.getValue() == true )
        mymask.contrast = 1;
      else
        mymask.contrast = 0;

      if( fcorrelation.getValue() == true )
        mymask.correlation = 1;
      else
        mymask.correlation = 0;

      if( fpower.getValue() == true )
        mymask.power = 1;
      else
        mymask.power = 0;

      image2d im;
      Magick::Image mim1;
      mim1.read( inputimage.getValue().c_str() );
      im.load( mim1, CH_GRAY );
      mim1.type( Magick::TrueColorType );
      
      fann *net;
      net = fann_create_from_file( "myann.conf" );
      int numfeature = 0;
      fann_type *annin, *annout;
      int w, h;
      h = im.getheight();
      w = im.getwidth();
      Magick::PixelPacket *pixel_cache = mim1.getPixels(0,0,w,h);
      Magick::ColorRGB red( "red" );
      for( int i = 0; i < im.getheight(); i = i + 32 )
        {
          for( int j = 0; j < im.getwidth(); j = j + 32 )
            {
              GLCP myglcp;
              int nf;
              std::vector< double > s;
              s.clear();
              myglcp.init( &im, i, j, 14 );
              myglcp.meanthetadelta( mymask, s );
              nf = (int)s.size();
              if( numfeature > 0 && nf != numfeature )
                {
                  std::cout << "ERROR number of features inconsistant\n";
                  exit(0);
                }
              else
                annin = new fann_type[nf];
              numfeature = nf;
              for( int z = 0; z < numfeature; z++ )
                annin[z] = s[z];
              annout = fann_run( net, annin );
              if( (float)(annout[0]) > 0.0 )
                {
                  pixel_cache[i*w+j] = red;
                }
            }
          std::cout << "." << std::flush;
        }
      delete [] annin;
      mim1.syncPixels(); 
      mim1.write( "blsh.png" );
      std::cout << "\n";
      
    }
  catch (TCLAP::ArgException &e)
    {
      std::cout << "ERROR: " << e.error() 
                << " for arg " << e.argId() << "\n";
      return 0;
    }
  
#else
  std::cout << "compiled with no image magick\n";
#endif
  return 0;
}
