#include "menu.h"
#include "Memoire1.h"
#include "Gris.h"
#include "Bin.h"
#include "Convo.h"
#include "Dil.h"
#include "Ero.h"
#include "FermOuv.h"
#include "Niveaugris.h"


//Main
int main(int argc, char *argv[]){
	system("clear");
	option(argc,argv);
	return 0;
}

//Fonction de debug
void debugArg(int argc, char *argv[]){
int i;
			printf("\nNombre d'arguments passes au programme : %d\n",argc);
				for(i = 0; i< argc ; i ++)
					{
						printf(" argv[%d] : %s\n", i, argv[i]);
					}
}

//Message d'aide pour l'utilisateur
void help(){
printf("\n\n---------------------------Help------------------------\n\nSYNOPSIS:\n	panorama [option]...\n\nDESCRIPTION:\n	Image manipulation helper to creat a panorama\n\nUSAGE:\n	Usage:\n\n		panorama [option]...\n\n	Help option:\n\n		-?, --help	Show this help message\n\n	General Options:\n\n		-g Convert input file(s) to greyscale\n		-h Extrat the histogram from the input file(s)\n		-e Erode the inut file(s)\n		-d Dilate the inout file(s)\n		-b threshold Render a black-white image using the given threshold\n		-c file Apply a convolution on the input file(s) using the given file as the convolution matrix\n		-p Render the panorama from the input file(s)\n\n	Working on single file:\n\n		-i file Define the input file\n		-o file Define the output file\n\n	Using multiple file:\n\n		-li file1,file2,...,fileN Define a set of files as the input\n		-lo file1,file2,...,fileN Define a set of files as the output     \n\n");
}

//Remplit le tableau avec les noms des fichiers
void tabParameters(int ind, int cpt, char *argv[], char *opt[])
{
int j;
int i=0;
	for(j = 0; j < cpt;j++)
	{
		ind = ind + 1;
		*(opt+j) = argv[ind];
	}
	for(i=0; i< cpt;i++)
		{
			printf("%s\n",opt[i]);
		}
}

//Parseur
void option(int argc, char *argv[])
{
int i,indF;
//int k,l;
int j=argc;

	if ((argc == 1) || ( (argc > 1) && ((!strcmp(argv[1],"-?")) || (!strcmp(argv[1],"--help")) || (!strcmp(argv[1],"-g")==0 && !strcmp(argv[1],"-h")==0 && !strcmp(argv[1],"-b")==0 && !strcmp(argv[1],"-c")==0 && !strcmp(argv[1],"-d")==0 && !strcmp(argv[1],"-e")==0 && !strcmp(argv[1],"-i")==0 && !strcmp(argv[1],"-li")==0 && !strcmp(argv[1],"-lo")==0))))
		{
			help();
		}
		else 
			while (j != 1)
			{
				for (i = 1; i < argc; i++)
				{
					if ((argc > 1) && !strcmp(argv[i],"-g"))
						{
						printf("Convert the input file(s) in grayscale\n");
						toto();								
						j = j - 1;
						}
						else if ((argc > 1) && !strcmp(argv[i],"-h"))
							{
								printf("Extract the histogramm from the input file(s)\n");
								indF = indFinH(argc,argv);
								char *opth[indF];
								tabParameters(i,indF,argv,opth);
								toto();
								j = j - 1;
							}
						else if ((argc > 1) && !strcmp(argv[i],"-e"))
							{
								printf("Erode the input file(s)\n");
								indF = indFinE(argc,argv);
								char *opte[indF];
								tabParameters(i,indF,argv,opte);
								toto();
								j = j - 1;
							}
						else if ((argc > 1) && !strcmp(argv[i],"-d"))
							{
								printf("Dilate the input file(s)\n");
								indF = indFinD(argc,argv);
								char *optd[indF];
								tabParameters(i,indF,argv,optd);
								toto();
								j = j - 1;
							}
						else if ((argc > 1) && !strcmp(argv[i],"-b"))
							{
								printf("Render a black-white image using the given threshold\n");
								indF = indFinB(argc,argv);
								char *optb[indF];
								tabParameters(i,indF,argv,optb);
								toto();
								j = j - 1;
							}
						else if ((argc > 1) && !strcmp(argv[i],"-c"))
							{
								printf("Apply a convolution on the input file(s) using the given file as the convolution matrix\n");
								indF = indFinC(argc,argv);
								char *optc[indF];
								tabParameters(i,indF,argv,optc);
								toto();
								j = j - 1;
							}
						else if ((argc > 1) && !strcmp(argv[i],"-p"))
							{
								printf("Render the panorama from the input file(s)\n");
								indF = indFinP(argc,argv);
								char *optp[indF];
								tabParameters(i,indF,argv,optp);
								toto();
								j = j - 1;
							}
						else if ((argc > 1) && !strcmp(argv[i],"-i"))
							{
								printf("Define the input file\n");
								indF = indFinI(argc,argv);
								char *opti[indF];
								tabParameters(i,indF,argv,opti);
								//toto();
								char *entree = (char*) malloc( 100 );
								entree = opti[0];
									for (k = i; k < argc; k++)
									{
										if ((argc > 1) && !strcmp(argv[k],"-o"))
											{
												printf("Define the output file\n");
												indF = indFinO(argc,argv);
												char *opto[indF];
												tabParameters(k,indF,argv,opto);
												char *sortie = (char*) malloc( 100 );
												sortie = opto[0];
												TablToppm(bina(entree,Taille0(entree),Taille1(entree)),sortie,Taille0(entree),Taille1(entree));
												free(sortie);
												for (l = k; l < argc; l++)
													{
														if ((argc > 1) && !strcmp(argv[l],"-g"))
														{
															printf("Convert the input file(s) in grayscale\n");
															char *sortie1 = (char*) malloc( 100 );
															sortie1 = opto[0];
															TablToGris(bina(entree,Taille0(entree),Taille1(entree)),sortie1,Taille0(entree),Taille1(entree));
															free(sortie1);
															j = j - 1;
														}
														else if ((argc > 1) && !strcmp(argv[l],"-d"))
															{
																printf("Dilate the input file(s)\n");
																indF = indFinD(argc,argv);
																char *optd[indF];
																tabParameters(l,indF,argv,optd);
																char *sortie6 = (char*) malloc( 100 );
																sortie6 = optd[0];
																DilToPPM(bina(entree,Taille0(entree),Taille1(entree)),sortie6,Taille0(entree),Taille1(entree));
																free(sortie6);
																j = j - 1;
															}
														else if ((argc > 1) && !strcmp(argv[l],"-e"))
															{
																printf("Erode the input file(s)\n");
																indF = indFinE(argc,argv);
																char *opte[indF];
																tabParameters(l,indF,argv,opte);
																char *sortie5 = (char*) malloc( 100 );
																printf("Entrez le nom de la nouvelle image après érosion \n");
																sortie5 = opte[0];
																EroToPPM(bina(entree,Taille0(entree),Taille1(entree)),sortie5,Taille0(entree),Taille1(entree));
																free(sortie5);
																j = j - 1;
															}
														else if ((argc > 1) && !strcmp(argv[l],"-c"))
															{
																printf("Apply a convolution on the input file(s) using the given file as the convolution matrix\n");
																indF = indFinC(argc,argv);
																char *optc[indF];
																tabParameters(l,indF,argv,optc);
																char *sortie4 = (char*) malloc( 100 );
																printf("Entrez le nom de la nouvelle image après convolution \n");
																sortie4 = optc[0];
																Convo(bina(entree,Taille0(entree),Taille1(entree)),sortie4,Taille0(entree),Taille1(entree));
																free(sortie4);
																j = j - 1;
															}
														else if ((argc > 1) && !strcmp(argv[i],"-b"))
															{
																printf("Render a black-white image using the given threshold\n");
																indF = indFinB(argc,argv);
																char *optb[indF];
																tabParameters(i,indF,argv,optb);
																char *sortie2 = (char*) malloc( 100 );
																char *seuil = (char*) malloc( 100 );
																printf("Entrez le nom de l'image binarisée (avec extension) \n");
																sortie2 = "bina.ppm";
																printf("Entrez un seuil de binarisation \n");
																scanf("%s", seuil);
																TablToBina(bina(entree,Taille0(entree),Taille1(entree)),sortie2,Taille0(entree),Taille1(entree), atoi(seuil));
																free(sortie2);
																free(seuil);
																j = j - 1;
															}
													}										
												j = j - 1;
											}
								
									}
								
								j = j - 1;
							}
							
						else if ((argc > 1) && !strcmp(argv[i],"-o"))
							{
								printf("Define the output file\n");
								indF = indFinO(argc,argv);
								char *opto[indF];
								tabParameters(i,indF,argv,opto);
								toto();
								j = j - 1;
							}	
						else if ((argc > 1) && !strcmp(argv[i],"-li"))
							{
								printf("Define a set of files as the input\n");
								indF = indFinLI(argc,argv);
								char *optli[indF];
								tabParameters(i,indF,argv,optli);
								toto();
								j = j - 1;
							}
						else if ((argc > 1) && !strcmp(argv[i],"-lo"))
							{
								printf("Define a set of files as the output\n");
								indF = indFinLO(argc,argv);
								char *optlo[indF];
								tabParameters(i,indF,argv,optlo);
								toto();
								j = j - 1;
							}
					else
					{
						j = j - 1;
					};
				}
			}
}

//Calcul le nombre d'argument de chaque option
int indFinG(int argc, char *argv[])
{
int i,k;
int indF =0;
	for (i = 1; i < argc; i++)
			{
			if ((argc > 1) && !strcmp(argv[i],"-g"))
			for (k = i+1; k < argc; k++)
				{
				if ((!strcmp(argv[k],"-o")==0) && (!strcmp(argv[k],"-h")==0) && (!strcmp(argv[k],"-e")==0) && (!strcmp(argv[k],"-d")==0) && (!strcmp(argv[k],"-b")==0) && (!strcmp(argv[k],"-c")==0) && (!strcmp(argv[k],"-p")==0) && (!strcmp(argv[k],"-i")==0) && (!strcmp(argv[k],"-li")==0) && (!strcmp(argv[k],"-lo")==0))
					{
						indF = indF + 1;
					}
				else 
					{
						return indF;
					}
				}
			}
return indF;
}


int indFinH(int argc, char *argv[])
{
int i,k;
int indF =0;
	for (i = 1; i < argc; i++)
			{
			if ((argc > 1) && !strcmp(argv[i],"-h"))
			for (k = i+1; k < argc; k++)
				{
				if ((!strcmp(argv[k],"-o")==0) && (!strcmp(argv[k],"-g")==0) && (!strcmp(argv[k],"-e")==0) && (!strcmp(argv[k],"-d")==0) && (!strcmp(argv[k],"-b")==0) && (!strcmp(argv[k],"-c")==0) && (!strcmp(argv[k],"-p")==0) && (!strcmp(argv[k],"-i")==0) && (!strcmp(argv[k],"-li")==0) && (!strcmp(argv[k],"-lo")==0))
					{
						indF = indF + 1;
					}
				else 
					{
						return indF;
					}
				}
			}
return indF;
}

int indFinE(int argc, char *argv[])
{
int i,k;
int indF =0;
	for (i = 1; i < argc; i++)
			{
			if ((argc > 1) && !strcmp(argv[i],"-e"))
			for (k = i+1; k < argc; k++)
				{
				if ((!strcmp(argv[k],"-o")==0) && (!strcmp(argv[k],"-g")==0) && (!strcmp(argv[k],"-h")==0) && (!strcmp(argv[k],"-d")==0) && (!strcmp(argv[k],"-b")==0) && (!strcmp(argv[k],"-c")==0) && (!strcmp(argv[k],"-p")==0) && (!strcmp(argv[k],"-i")==0) && (!strcmp(argv[k],"-li")==0) && (!strcmp(argv[k],"-lo")==0))
					{
						indF = indF + 1;
					}
				else 
					{
						return indF;
					}
				}
			}
return indF;
}

int indFinD(int argc, char *argv[])
{
int i,k;
int indF =0;
	for (i = 1; i < argc; i++)
			{
			if ((argc > 1) && !strcmp(argv[i],"-d"))
			for (k = i+1; k < argc; k++)
				{
				if ((!strcmp(argv[k],"-o")==0) && (!strcmp(argv[k],"-g")==0) && (!strcmp(argv[k],"-e")==0) && (!strcmp(argv[k],"-h")==0) && (!strcmp(argv[k],"-b")==0) && (!strcmp(argv[k],"-c")==0) && (!strcmp(argv[k],"-p")==0) && (!strcmp(argv[k],"-i")==0) && (!strcmp(argv[k],"-li")==0) && (!strcmp(argv[k],"-lo")==0))
					{
						indF = indF + 1;
					}
				else 
					{
						return indF;
					}
				}
			}
return indF;
}

int indFinB(int argc, char *argv[])
{
int i,k;
int indF =0;
	for (i = 1; i < argc; i++)
			{
			if ((argc > 1) && !strcmp(argv[i],"-h"))
			for (k = i+1; k < argc; k++)
				{
				if ((!strcmp(argv[k],"-o")==0) && (!strcmp(argv[k],"-g")==0) && (!strcmp(argv[k],"-e")==0) && (!strcmp(argv[k],"-d")==0) && (!strcmp(argv[k],"-h")==0) && (!strcmp(argv[k],"-c")==0) && (!strcmp(argv[k],"-p")==0) && (!strcmp(argv[k],"-i")==0) && (!strcmp(argv[k],"-li")==0) && (!strcmp(argv[k],"-lo")==0))
					{
						indF = indF + 1;
					}
				else 
					{
						return indF;
					}
				}
			}
return indF;
}

int indFinC(int argc, char *argv[])
{
int i,k;
int indF =0;
	for (i = 1; i < argc; i++)
			{
			if ((argc > 1) && !strcmp(argv[i],"-c"))
			for (k = i+1; k < argc; k++)
				{
				if ((!strcmp(argv[k],"-o")==0) && (!strcmp(argv[k],"-g")==0) && (!strcmp(argv[k],"-e")==0) && (!strcmp(argv[k],"-d")==0) && (!strcmp(argv[k],"-b")==0) && (!strcmp(argv[k],"-h")==0) && (!strcmp(argv[k],"-p")==0) && (!strcmp(argv[k],"-i")==0) && (!strcmp(argv[k],"-li")==0) && (!strcmp(argv[k],"-lo")==0))
					{
						indF = indF + 1;
					}
				else 
					{
						return indF;
					}
				}
			}
return indF;
}

int indFinP(int argc, char *argv[])
{
int i,k;
int indF =0;
	for (i = 1; i < argc; i++)
			{
			if ((argc > 1) && !strcmp(argv[i],"-p"))
			for (k = i+1; k < argc; k++)
				{
				if ((!strcmp(argv[k],"-o")==0) && (!strcmp(argv[k],"-g")==0) && (!strcmp(argv[k],"-e")==0) && (!strcmp(argv[k],"-d")==0) && (!strcmp(argv[k],"-b")==0) && (!strcmp(argv[k],"-c")==0) && (!strcmp(argv[k],"-h")==0) && (!strcmp(argv[k],"-i")==0) && (!strcmp(argv[k],"-li")==0) && (!strcmp(argv[k],"-lo")==0))
					{
						indF = indF + 1;
					}
				else 
					{
						return indF;
					}
				}
			}
return indF;
}

int indFinI(int argc, char *argv[])
{
int i,k;
int indF =0;
	for (i = 1; i < argc; i++)
			{
			if ((argc > 1) && !strcmp(argv[i],"-i"))
			for (k = i+1; k < argc; k++)
				{
				if ((!strcmp(argv[k],"-o")==0) && (!strcmp(argv[k],"-g")==0) && (!strcmp(argv[k],"-e")==0) && (!strcmp(argv[k],"-d")==0) && (!strcmp(argv[k],"-b")==0) && (!strcmp(argv[k],"-c")==0) && (!strcmp(argv[k],"-p")==0) && (!strcmp(argv[k],"-h")==0) && (!strcmp(argv[k],"-li")==0) && (!strcmp(argv[k],"-lo")==0))
					{
						indF = indF + 1;
					}
				else 
					{
						return indF;
					}
				}
			}
return indF;
}

int indFinO(int argc, char *argv[])
{
int i,k;
int indF =0;
	for (i = 1; i < argc; i++)
			{
			if ((argc > 1) && !strcmp(argv[i],"-o"))
			for (k = i+1; k < argc; k++)
				{
				if ((!strcmp(argv[k],"-i")==0) &&(!strcmp(argv[k],"-g")==0) && (!strcmp(argv[k],"-e")==0) && (!strcmp(argv[k],"-d")==0) && (!strcmp(argv[k],"-b")==0) && (!strcmp(argv[k],"-c")==0) && (!strcmp(argv[k],"-p")==0) && (!strcmp(argv[k],"-h")==0) && (!strcmp(argv[k],"-li")==0) && (!strcmp(argv[k],"-lo")==0))
					{
						indF = indF + 1;
					}
				else 
					{
						return indF;
					}
				}
			}
return indF;
}

int indFinLI(int argc, char *argv[])
{
int i,k;
int indF =0;
	for (i = 1; i < argc; i++)
			{
			if ((argc > 1) && !strcmp(argv[i],"-li"))
			for (k = i+1; k < argc; k++)
				{
				if ((!strcmp(argv[k],"-o")==0) && (!strcmp(argv[k],"-g")==0) && (!strcmp(argv[k],"-e")==0) && (!strcmp(argv[k],"-d")==0) && (!strcmp(argv[k],"-b")==0) && (!strcmp(argv[k],"-c")==0) && (!strcmp(argv[k],"-p")==0) && (!strcmp(argv[k],"-i")==0) && (!strcmp(argv[k],"-h")==0) && (!strcmp(argv[k],"-lo")==0))
					{
						indF = indF + 1;
					}
				else 
					{
						return indF;
					}
				}
			}
return indF;
}

int indFinLO(int argc, char *argv[])
{
int i,k;
int indF =0;
	for (i = 1; i < argc; i++)
			{
			if ((argc > 1) && !strcmp(argv[i],"-lo"))
			for (k = i+1; k < argc; k++)
				{
				if ((!strcmp(argv[k],"-o")==0) && (!strcmp(argv[k],"-g")==0) && (!strcmp(argv[k],"-e")==0) && (!strcmp(argv[k],"-d")==0) && (!strcmp(argv[k],"-b")==0) && (!strcmp(argv[k],"-c")==0) && (!strcmp(argv[k],"-p")==0) && (!strcmp(argv[k],"-i")==0) && (!strcmp(argv[k],"-li")==0) && (!strcmp(argv[k],"-h")==0))
					{
						indF = indF + 1;
					}
				else 
					{
						return indF;
					}
				}
			}
return indF;
}
