#ifndef _NOMAGICK_
#include <Magick++.h>
#endif

#include <iostream>
#include <string>
#include <math.h>
#include "imagealign.h"
#include "image2d.h"
#include <fftw3.h>
#include <tclap/CmdLine.h>
#include "bdbenv.h"
#include "texture.h"
#include "dbimage.h"
#include "emcluster.h"
#include "kmeans.h"

#ifndef _NOMAGICK_

int main( int argc, char *argv[] )
{
  try
    {
      ///set up the command line parsing
      TCLAP::CmdLine cmd("Image alignment program", ' ', "0.001");

      TCLAP::ValueArg<std::string> dbdir("d", "db", "database dirctory", true, "", "databaase");

      TCLAP::ValueArg<int> sx("x", "stackx", "Stack x", true, 0, "stack");
      TCLAP::ValueArg<int> sy("y", "stacky", "Stack y", true, 0, "stack");

      TCLAP::ValueArg<int> si("i", "id", "image d", true, 0, "0" );
      TCLAP::ValueArg<int> ch("c", "channel", "channel", true, 0, "0" );

      TCLAP::ValueArg<std::string> output("o", "output", "image output", false, "output.png", "output file name");

      cmd.add(dbdir);
      cmd.add(sx);
      cmd.add(sy);
      cmd.add(ch);
      cmd.add(si);
      cmd.add( output );

      cmd.parse(argc, argv);
      image2d ima;
      bdbenv bdb;
      dbimage imstore;

      bdb.open( dbdir.getValue() );
      imstore.init( bdb.envptr() );
      bdb.adddb( &imstore );
      bdb.cleanlog();
      Magick::Image mim;
      imstore.getimage( sx.getValue(), sy.getValue(),
                        si.getValue(), 
                        ch.getValue(), ima );
      ima.getimage( mim );
      mim.equalize();
      mim.write( output.getValue().c_str() );
      imstore.close();
      bdb.close();
    }
  catch (TCLAP::ArgException &e)
    {
      std::cout << "ERROR: " << e.error() 
                << " for arg " << e.argId() << "\n";
      return 0;
    }
  return 0;
}
#else
int main()
{
  std::cout << "no magick\n";
  return 0;
}

#endif

