#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gd.h>
#include <math.h>


struct center{
	int x;
	int y;
	double r;
	double g;
	double b;
};

struct center **coords;


void Calc_Coords(double hex_side_len, double img_w, double img_h, char *infile, char *type);


int main(int argc, char **argv){
	FILE *fp;
	FILE *in;
	gdImagePtr im;
	int c;
	//in = fopen("namerica_lg.jpg", "rb");
	//im = gdImageCreateFromPng(in);
	//im = gdImageCreateFromJpeg(in);
	//fclose(in);
	//c = gdImageGetPixel(im, gdImageSX(im) / 2, gdImageSY(im) / 2);
	int map_width, map_height;
	double pixel_ratio_w, pixel_ratio_h;
	double hex_side_len;
	int max_x;
	int max_y;
	double w = 50;
	double h;
	double img_w, img_h;
	char *map_line;
	int count1, count2, count3, counter4;
	int X, Y;
	int sensitivity=210;
	char infile[50];
	char type[6];	

	if(argc == 5){
        	//for(i=0;i<argc;i++)
        	//{                                        
                //	if(i==1){
                //        	strcpy (option, argv[i]);
                //        	if (option[0] == 's'){

					strcpy(infile, argv[1]);
					sensitivity=atoi(argv[2]);
					strcpy(type, argv[3]);
					printf("s: %d\n", sensitivity);
					w=atoi(argv[4]);
					//exit(0);
		//		}
		//	}
		//}

	}else{
		printf("wrong usage\n");
		exit(0);
	}

	in = fopen(infile, "rb");

	if(strcmp(type, "jpg") == 0){
        	im = gdImageCreateFromJpeg(in);
	}else if(strcmp(type, "png") == 0){
		im = gdImageCreateFromPng(in);
	}else if(strcmp(type, "gif") == 0){
		im = gdImageCreateFromGif(in);
	}else{
		printf("wrong option\n");
		fclose(in);
		exit (0);
	}

        fclose(in);




	//measure 6x6 regions on image 
	//if area is mostly white then draw water to map

//hexagon breaks into 6 triangles, or 1 rect 2 tri
//if on map check before get pixel data

//arguments:  input.jpg -w35 -h55 output.map
//maintain aspect ratio /w only -w input ?

	hex_side_len = gdImageSX(im)/(w/2);
	img_w = gdImageSX(im);
	img_h = gdImageSY(im);


	h = (img_h * w) / img_w;	
	//h = h);

printf("x: %d y: %d\n", gdImageSX(im), gdImageSY(im));
printf("hex_side_len: %lf w: %lf h: %lf\n", hex_side_len, w, h);
	//pixel_ratio_h = floor(gdImageSY(im)/h);


X = img_w/(hex_side_len*1.5);
Y = img_h/(hex_side_len*2);

printf("X: %d Y: %d\n", X, Y );
//X: 2114969600 Y: 2125576512



coords = (struct center **)malloc( sizeof(struct center *) * (X+2) );
	for( counter4=0; counter4<X+2; counter4++ ){
        	coords[ counter4 ] = ( struct center *)malloc((Y+2) * sizeof(struct center) );
        }


map_line = (char *)malloc(sizeof(char)*15*w+1);

printf("here 1\n");
Calc_Coords(hex_side_len, img_w, img_h, infile, type);
printf("out\n");


//max 12 faces per hex, min 6 in bounds
//calc face dimensions
//calculate array of center coordinates
//function to determine faces within img bounds for given coordinate
//function to retrieve all rgb values for points within a face
// -total number of points within a face
// -average of rgb values
// -return average
//function to calculate average of all returned rgb values for all faces assoc with coord
// -store in array of structs

//output map file from data



/*        if( ( fp = fopen( "/proc/cpuinfo", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening /proc/cpuinfo file.\n" );
                exit( 1 );
        }
*/
//gdImageGetPixel(gdImagePtr im, int x, int y)
//c = gdImageGetPixel(im, gdImageSX(im) / 2, gdImageSY(im) / 2);
//c, im->red[c], im->green[c], im->blue[c]);



	memset ((void*) map_line, '\0', sizeof (map_line));

		strcat(map_line, "border_size=1\nusage=map\n\n");
                if (fp = fopen("test.map", "a")) {
                 
                        fprintf(fp, "%s", map_line );

                }
        
                fclose( fp );







/*        for(x=0; x <= img_w; x += hex_side_len*1.5) {
                if(stagger == 0) {
                        y = hex_side_len;
                        stagger = 1;
                }else{
                        y = 0;
                        stagger = 0;
                }
                while(y <= img_h) {
*/







/*	//loop the height
	for(count1 = 0; count1 < h; count1++){
		memset ((void*) map_line, '\0', sizeof (map_line));
		for(count2 = 0; count2 < w; count2++){
			count3 = count2 + count1*h;
			//printf("count3: %d average: %d\n", count3, coords[count3].r+coords[count3].g+coords[count3].b);
*/

	for(count1 = 0; count1 < Y; count1++) {
		memset ((void*) map_line, '\0', sizeof (map_line));
		for(count2 = 0; count2 < X; count2++) {
			if(coords[count2][count1].r+coords[count2][count1].g+coords[count2][count1].b < sensitivity*3){
				//empty = sprintf( map_line, " %d", );
				strcat(map_line, "Gg");
			}else{
				strcat(map_line, "Wo");
			}

			if(count2 < X-1 ){
				strcat(map_line, "          , ");
			}else{
				strcat(map_line, "\n");
			}


		}

		if (fp = fopen("test.map", "a")) {
	
			fprintf(fp, "%s", map_line );

		}

		fclose( fp );
	}



gdImageDestroy(im);
return 0;

}

void Calc_Coords(double hex_side_len, double img_w, double img_h, char *infile, char *type){
        FILE *in;
        gdImagePtr im;
        //int c;   
        in = fopen(infile, "rb");
        //im = gdImageCreateFromPng(in);
        //im = gdImageCreateFromJpeg(in);
        //fclose(in);


        if(strcmp(type, "jpg") == 0){
                im = gdImageCreateFromJpeg(in);
        }else if(strcmp(type, "png") == 0){
                im = gdImageCreateFromPng(in);
        }else if(strcmp(type, "gif") == 0){
                im = gdImageCreateFromGif(in);
        }else{
                printf("wrong option\n");
                fclose(in);
                exit (0);
        }
	fclose(in);


	double x;
	double y;
	int stagger=0;
	double sum_r, sum_g, sum_b;
	int c;
	int count1, count2; 
	double points;
	int count3 = 0;	
	int xcoord=0; 
	int ycoord=0;


//c = gdImageGetPixel(im, 10, 10 );

//printf("The value of the center pixel is %d; RGB values are %d,%d,%d\n", c, gdImageRed(im,c), gdImageGreen(im,c),gdImageBlue(im,c));


printf("hex_side_len: %lf\n", hex_side_len);

	for(x=0; x <= img_w; x += hex_side_len*1.5) {
		if(stagger == 0) {
			y = hex_side_len;
			stagger = 1;
		}else{
			y = 0;
			stagger = 0;
		}
		ycoord=0;
		while(y <= img_h) {
			sum_r = 0;
			sum_g = 0;
			sum_b = 0;
			points = 0;
			//count1 = x-hex_side_len;
			//count2 = y;
			/*
			for(count1=x-hex_side_len+1; count1<x+hex_side_len-1; count1++){
				if(count1>0 && count1<img_w){
					//printf("here 2\n");
					c = gdImageGetPixel(im, count1, count2);
					//printf("count1: %d count2: %d\n",count1, count2);
					
					sum_r += gdImageRed(im,c);
					sum_g += gdImageGreen(im,c);
					sum_b += gdImageBlue(im,c);
					points++;
					//printf("X: %lf Y: %lf rgb: %d %d %d\n", x, y, gdImageRed(im,c), gdImageGreen(im,c), gdImageBlue(im,c));
				}
			}

			count1 = x;
                        for(count2=y-hex_side_len+1; count2<y+hex_side_len-1; count2++){
                                if(count2>0 && count2<img_h){
                                        c = gdImageGetPixel(im, count1, count2);
                                        sum_r += gdImageRed(im,c);
                                        sum_g += gdImageGreen(im,c);
                                        sum_b += gdImageBlue(im,c);
					points++;
                                }   
                        }
			*/
	
			for(count2 = y-hex_side_len+1; count2<y+hex_side_len-1; count2++){
				for(count1 = x-hex_side_len+1; count1<x+hex_side_len-1; count1++){
					c = gdImageGetPixel(im, count1, count2);	
					sum_r += gdImageRed(im,c);
					sum_g += gdImageGreen(im,c);
					sum_b += gdImageBlue(im,c);
					points++;
				}
			}





printf("x: %d y: %d\n", xcoord, ycoord);

			coords[xcoord][ycoord].r = sum_r/(points);
			coords[xcoord][ycoord].g = sum_g/(points);			
			coords[xcoord][ycoord].b = sum_b/(points);
			//coords[xcoord][ycoord].x = xcoord;
			//coords[xcoord][ycoord].y = ycoord;
//printf("here 3\n");
			ycoord++;
		//printf("count3: %d x: %lf y: %lf rgb: %lf %lf %lf coords: %d %d\n", count3, x, y, coords.r, coords[count3].g, coords[count3].b, coords[count3].x, coords[count3].y);
			count3++;
/*
			//face 1 
			if(x>=0 && y>=hex_side_len && x <= img_w-hex_side_len/2) {
				//get sum of all rgb within face
				//all whole num coords within bounds of 3 vertices
				
			}
			//face 2
                        if(x>=0 && y>=hex_side_len && x <= img_w-hex_side_len/2) {
                            
                        }  
			//face 3
                        if(x>=0 && y>=hex_side_len && x <= img_w-hex_side_len) {
                            
                        }  
			//face 4
                        if(x>=0 && y>=0 && x<= img_w-hex_side_len/2 && y<= img_h-hex_side_len ) {
                            
                        }  
			//face 5
                        if(x>=0 && y>=0 && x<= img_w-hex_side_len && y<= img_h-hex_side_len) {
                            
                        }  
			//face 6
                        if(x>=0 && y>=0 && x<= img_w-hex_side_len/2 && y<= img_h-hex_side_len) {
                            
                        }  
			//face 7
                        if(x>=img_w+hex_side_len/2 && y>=0 && x<= img_w && y<= img_h-hex_side_len) {
                            
                        }  
			//face 8
                        if(x>=img_w+hex_side_len/2 && y>=0 && x<= img_w && y<= img_h-hex_side_len) {
                            
                        }  
			//face 9
                        if(x>=img_w+hex_side_len && y>=0 && x<= img_w+hex_side_len/2 && y<= img_h-hex_side_len) {
                            
                        }  
			//face 10
                        if(x>=img_w+hex_side_len && y>=0 && x<= img_w+hex_side_len/2 && y<= img_h-hex_side_len) {
                            
                        }  
			//face 11
                        if(x>=0 && y>=hex_side_len) {
                            
                        }  
			//face 12
                        if(x>=0 && y>=hex_side_len) {
                            
                        } 
*/
 
			//Write averages to global struct

			//reset sum vars

			//increment y
			y+=hex_side_len*2;

		}
		xcoord++;

	}

gdImageDestroy(im);
}
