/*commanlinetool for segmentation */
#include <fstream.h>
#include <stdio.h>
#include <time.h>

#include "../datalib/vector.h"
#include "../datalib/volume.h"
#include "../mathlib/statistic.h"
#include "../mathlib/transform.h"
#include "../mathlib/moments.h"

#include "../errorcode.h"

#include "../datalib/datacube.h"
#include "../datalib/cfddatacube.h"
#include "../datalib/testcubes.h"

#include "../reconstructionlib/reconstruction.h"
#include "../mathlib/nld.h"

#include "command_line_parser.h"

using namespace std;
using namespace pramodx;


int
main(int argc, char **argv)
{
    DataCube convert(3);
	char inputfile[80];
	char outputfile[80];
	Vector<int> size(3);
	Vector<double> d(3);
	size[1]=0;	size[3]=0;	size[3]=0;
	d[1]=1.0;d[2]=1.0;d[3]=1.0;
        int invert=0;
        command_line_parser parser(argc, argv);
    if (argc<2)
    {
        cout << "Usage: rawtonih -sx:(sizex) -sy:(sizey) -sz:(sizez) inputfilename.dat outputfilename" << endl;
        return 0;
    };

    for (int i=0;i<parser.get_n_options();i++)
    {
        option o=parser.get_option(i);
        if (strcmp(o.get_option(),"h")==0 || parser.get_n_filenames()==0)
        {
            cout << "Usage: rawtonih -sx:(sizex) -sy:(sizey) -sz:(sizez) inputfilename.dat outputfilename" << endl;
            cout << "Optional: -dx:(spaceingx) -dy:(spaceingy) -dz:(spaceingz)" << endl;
            cout << "-i Invert data" << endl;
            return 0;
        };

        if (strcmp(o.get_option(),"i")==0)
        {
            invert=1;
            cout << "Invert Data" << endl;
        };
        


        
        if (strcmp(o.get_option(),"dx")==0)
        {
            float help;
            if (sscanf (o.get_value(),"%f",&(help))==EOF) continue;
            d[1]=help;
            cout << "dx=" << d[1] << endl;
        };


        if (strcmp(o.get_option(),"dy")==0)
        {
            float help;
            if (sscanf (o.get_value(),"%f",&(help))==EOF) continue;
            d[2]=help;
            cout << "dy=" << d[2] << endl;
        };

        if (strcmp(o.get_option(),"dz")==0)
        {
            float help;
            if (sscanf (o.get_value(),"%f",&(help))==EOF) continue;
            d[3]=help;
            cout << "dz=" << d[3] << endl;
        };
 


        if (strcmp(o.get_option(),"sx")==0)
        {
            int help;
            if (sscanf (o.get_value(),"%d",&(help))==EOF) continue;
            size[1]=help;
            cout << "sx=" << size[1] << endl;
        };

        if (strcmp(o.get_option(),"sy")==0)
        {
            int help;
            if (sscanf (o.get_value(),"%d",&(help))==EOF) continue;
            size[2]=help;
            cout << "sy=" << size[2] << endl;
        };

        if (strcmp(o.get_option(),"sz")==0)
        {
            int help;
            if (sscanf (o.get_value(),"%d",&(help))==EOF) continue;
            size[3]=help;
            cout << "sz=" << size[3] << endl;
        };
  




    };
if (size[1]*size[2]*size[3]==0)
	{
		cout << "Error Size not set use -h for help." << endl;
		return 1;
		
	}

sprintf(inputfile,parser.get_filename(0));
printf("Inputfile:");
printf(inputfile);
printf("\n");
sprintf(outputfile,parser.get_filename(1));
printf("Outputfile:");
printf(outputfile);
printf("\n");
convert.SetCubeSizeAndSpaceing(size,d);
convert.MallocCube();
if (convert.readRAW(inputfile)) 
{
	cout << "Error durring read!" << endl;
	return 1;
};
if (invert)
    for (int z=0;z<convert.NrOfElements();z++)
        convert(z)=1-convert(z);

convert.WriteToNIH(outputfile);
return 0;
}


