//**************************************************************************
//
//      CSE 585 Project #2
//      By Anirudh Modi (anirudh@bart.aero.psu.edu) on 4/10/2000-Mon
//       & Shin Chin (scc136@psu.edu)
//       & Ming Ni (ni@cse.psu.edu)
//
//**************************************************************************
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>     // for atoi()
#include <string.h>
#include <math.h>
#include <time.h>
#include "utils.h"
#include "image.h"
#include "matrix.h"
#include "edgeflow.h"
#include "edgedetect.h"
#include "region.h"
//**************************************************************************
int debugflag = 0;
int textureflag = 1;
int intensityflag = 1;
//**************************************************************************
int main (int argc, char *argv[])
{
if (argc < 4)
        {
         cerr << endl;
         cerr << "Syntax: " << argv[0] << " <orig-image-file> <real-matrix> <imag-matrix> [opt-args]" << endl;
         cerr << endl;
         cerr << "  Available optional arguments:" << endl;
         cerr << "\t-t <threshold>\t\t\t(Possible values: 1.0e-30 to 1.0e-1)" << endl;
         cerr << "\t-m <min-boundary-size>\t\t(Possible values: 5-20)" << endl;
         cerr << endl;
         exit(-1);
        }
cout << endl << "Progam BEGINS." << endl;

double threshold = 4.0e-3;
int min_region_size = 10;

if (int index = parseArg(argc, argv, "-t"))
        threshold = atof(argv[index]);

if (int index = parseArg(argc, argv, "-m"))
        min_region_size = atoi(argv[index]);

cout << endl;
cout << "Threshold = " << threshold << endl;
cout << "Minimum boundary size (in pixels) = " << min_region_size << endl;
cout << endl;

time_t begin_time, end_time;
time(&begin_time);

Matrix Fr, Fi;
GrayImage orig, iR, iG, iB;
int rgbflag = 0;

if (orig.isRGB(argv[1]))
        {
         rgbflag = 1;
         orig.ReadRGBimageFromFile2(argv[1], &iR, &iG, &iB);
        }
else
        orig.ReadFromFile2(argv[1]);

Fr.ReadFromFile(argv[2]);
Fi.ReadFromFile(argv[3]);

char *name = ExtractName(argv[1]);
char filename[50];

int height = Fr.nx;
int width = Fr.ny;

Matrix Edge(height, width);

DetectEdge(&Edge, Fr, Fi);

sprintf(filename, "%s.tec", name);
Write2DTecplotFile(&Fr, &Fi, filename);

GrayImage edgeImage;

Edge.Threshold(threshold);
edgeImage.ReadFromMatrix(Edge);
edgeImage.RemoveLonelyPixels();
sprintf(filename, "%s.edge", name);
edgeImage.WriteToFile(filename, "gif");

sprintf(filename, "%s.final", name);
if (rgbflag)
        {
         GrayImage fR, fG, fB;
         fR.Init(height, width, 255);
         fG.Init(height, width, 255);
         fB.Init(height, width, 255);
         Combine2Images(iR, edgeImage, &fR);
         Combine2Images(iG, edgeImage, &fG);
         Combine2Images(iB, edgeImage, &fB);
         WriteToRGBFile(filename, "gif", fR, fG, fB);
        }
else
        {
         GrayImage finalImage;
         finalImage.Init(height, width, 255);
         Combine2Images(orig, edgeImage, &finalImage);
         finalImage.WriteToFile(filename, "gif");
        }

//edgeImage.ClosingNxN(5);
//edgeImage.Skeletonization();

makeRegions(edgeImage, min_region_size);

GrayImage dummy;
dummy.Init(height, width, 255);

writeAllRegions(&dummy);
dummy.ThresholdWhite(1);
dummy.DilationNxN(3);
sprintf(filename, "%s0.edge", name);
dummy.WriteToFile(filename, "gif");

refineRegions(min_region_size, 2);
printRegionLengths();
writeAllRegions(&dummy);
dummy.ThresholdWhite(1);
sprintf(filename, "%s1.edge", name);
dummy.WriteToFile(filename, "gif");

time(&end_time);
cout << endl << "Progam ENDS." << endl;

cout << "Total time consumed = " << (double) (end_time - begin_time) 
        << " sec" << endl;
cout.flush();
}
//**************************************************************************