#include "image2d.h"
#include <tclap/CmdLine.h>
#include <string>

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

      TCLAP::ValueArg< int >
        ratio("r", "ratio", "ratio", true, 0, "ratio");

      TCLAP::ValueArg<std::string> inim("i", "image", "input image", true, "", "image");

      cmd.add( ratio );
      cmd.add( inim );

      cmd.parse(argc, argv);

      Magick::Image mask( inim.getValue() );
      image2d immask, lowres;

      immask.load( mask, CH_GRAY );
      lowres.loadlowres( &immask, ratio.getValue() );

      for( int i = 0; i < immask.getheight(); i++ )
        {
          for( int j = 0; j < immask.getwidth(); j++ )
            {
              double di, dj;
              di = ( ( (double)i )/( (double)immask.getheight() ) ) * ( (double)lowres.getheight() );
              dj = ( ( (double)j )/( (double)immask.getwidth() ) ) * ( (double)lowres.getwidth() );
              double v;
              v = lowres.getpixelb( di, dj );
              immask.setpixel( i, j ,v );
            }
        }

      immask.getimage( mask );
      mask.write( "lowresitr.png" );
    }
  catch (TCLAP::ArgException &e)
    {
      std::cout << "ERROR: " << e.error() 
                << " for arg " << e.argId() << "\n";
      return 0;
    }
  return 0;
}
