
#include <stdlib.h> 
#include <stdio.h> 
#include "vision_utilities.h"

int p1(char *fname_input, int thr, char *fname_output)
{
	Image *im_input = new Image;//input image
	Image *im_output = new Image;//output image
	readImage(im_input, fname_input);
	int NRows = getNRows(im_input);
	int NCols = getNCols(im_input);
	setSize(im_output,NRows,NCols);
	setColors(im_output,1);//output is a binary image

	for (int i = 0; i<NRows; i++)
	{
		for (int j = 0; j<NCols; j++)
		{
			int pixel = getPixel(im_input,i,j);//gray-level of the input image at (i,j)
			if (pixel>thr)
			{
				setPixel(im_output,i,j,1);
			}
			else
				setPixel(im_output,i,j,0);		
		}
	}

	writeImage(im_output, fname_output);
	printf("Threshold = %d. U got it\n", thr);
	return 0;
}

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