#include <iostream>
#include <string>
#include <fstream>
#include <math.h>
#include "imagealign.h"
#include "image2d.h"
#include <fftw3.h>
#include <tclap/CmdLine.h>
#include "bdbenv.h"
#include "dbimage.h"
#include "pso.h"
#include "histequalize.h"
#include "tile.h"
#include "tileimage.h"

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

      cmd.add(dbdir);

      cmd.parse(argc, argv);

      bdbenv bdb;
      dbimage imstore;

      bdb.open( dbdir.getValue() );
      imstore.init( bdb.envptr() );
      bdb.adddb( &imstore );
      bdb.cleanlog();

      std::vector< int > rows;
      std::vector< int > cols;
      std::vector< int > channels;
      imstore.rows( rows );
      for( unsigned int i = 0; i < rows.size(); i++ )
        {
          std::cout << rows[i] << "\t| ";
          imstore.rowstacks( rows[i], cols );
          for( unsigned j = 0; j < cols.size(); j++)
            {
              std::cout << cols[j] << " ";
              image2d myim;
              char buff[128];
              imstore.getimage( rows[i], cols[j], 0, 3, myim ); 
              Magick::Image im;
              myim.getimage( im );
              im.equalize();
              sprintf( buff, "%i_%i.png", rows[i], cols[j] );
              im.write( buff );
              myim.del();
            }
          std::cout << "\n";
        }
      imstore.getchannels( channels );
      std::cout << "channels: ";
      for( unsigned i = 0; i < channels.size(); i++ )
        std::cout << channels[i] << " ";
      std::cout << "\n";
      imstore.getslices( channels );
      std::cout << "slices: ";
      for( unsigned i = 0; i < channels.size(); i++ )
        std::cout << channels[i] << " ";
      std::cout << "\n";

      imstore.close();
      bdb.close();
    }
  catch (TCLAP::ArgException &e)
    {
      std::cout << "ERROR: " << e.error() 
                << " for arg " << e.argId() << "\n";
      return 0;
    }

  return 0;
}
