#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> hmmdir("s", "sqlite", "hmm sqlitedb",
                                          true, "", "sqlite database");

      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" );

      TCLAP::ValueArg< int > mid( "m", "mid", "model number", true, 1, 
                                  "the model number to use" );

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

      dbhmm dbh;
      classifierdb classdb;

      std::string hmmdbdir;

      hmmdbdir = hmmdir.getValue();

      std::cout << "hmmdbdir = \"" << hmmdbdir << "\"\n";

      classdb.open( hmmdir.getValue() );
      dbh.init( &classdb, mid.getValue(), 0, 0, 0 );

      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;

      GLCPimage gi;

      image2d im;
      Magick::Image mim1;
      mim1.read( inputimage.getValue().c_str() );
      im.load( mim1, CH_GRAY );
      
      Magick::Image mim;
      
      gi.classifyimage( &im, &dbh, mymask, 1 );
      
      im.getimage( mim );
      gi.overlay( mim );
      mim.write( "class.png" );
      im.getimage( mim );
      gi.seperate_histogram( mim );
      mim.write( "seperated.png" );
    
      std::cout << "\n";
      dbh.close();
      classdb.close();
      
    }
  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;
}
