#include <stdlib.h> 
#include <stdio.h>
#include <cmath>
#include <iostream>
#include <fstream>
#include "vision_utilities.h"

using namespace std;

int myround(double x);

int p1(char *fname_input, char *fname_output)
{
	Image *im_input = new Image;//input image
	readImage(im_input, fname_input);
	int NRows = getNRows(im_input);//# of rows
	int NCols = getNCols(im_input);//# of cols
	
	/* Step 1. Compute the coordinates of centroid, area and radius */
	int area = 0;
	int centroid_x = 0;
	int centroid_y = 0;
	int sum_x = 0;
	int sum_y = 0;

	for (int i = 0; i < NRows; i++)
	{
		for (int j = 0; j < NCols; j++)
		{
			if (getPixel(im_input,i,j)>0)
			{
				area++;
				sum_x += j;
				sum_y += i;
			}
		}
	}
	
	centroid_x = myround((double)sum_x/area);
	centroid_y = myround((double)sum_y/area);

	double radius = sqrt(double(area)/PI);

	/*Step 2. File output */
	ofstream fParameters(fname_output);
	fParameters<<centroid_x<<" "<<centroid_y<<" "<<radius;
	fParameters.close();
	printf("U got it\n");
	return 0;
}

int myround(double x)
{
	if (x<floor(x)+0.50) return floor(x);
	else return (floor(x)+1);
}

int main(int argc, char **argv) 
{ 
	if (argc <= 2){ 
		printf("Usage: %s <input original image> <output parameters file>\n", argv[0]); 
		return 0; 
	} 
	char *inputFilename = argv[1];  
	char *outputFilename = argv[2]; 
	p1(inputFilename,outputFilename); 
	return 0; 
}
