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

using namespace std;

int p3(char *im1fname, char *im2fname, char *im3fname, char *im4fname, char *im5fname, char *outputFilename)
{
	Image **im = new Image*[5];
	for (int k = 0; k < 5; k++) 
		im[k] = new Image;//DO NOT FORGET THIS!!!OR U WILL GET SEGMENTATION FAULT
	readImage(im[0], im1fname);
	readImage(im[1], im2fname);
	readImage(im[2], im3fname);
	readImage(im[3], im4fname);
	readImage(im[4], im5fname);
	int NRows = getNRows(im[0]);//# of rows
	int NCols = getNCols(im[0]);//# of cols

	Image *mask = new Image;
	setSize(mask,NRows,NCols);
	setColors(mask,1);

	for (int i = 0; i < NRows; i++)
	{
		for (int j = 0; j < NCols; j++)
		{
			int count = 0;
			if (getPixel(im[0],i,j)>0) count++;
			if (getPixel(im[1],i,j)>0) count++;
			if (getPixel(im[2],i,j)>0) count++;
			if (getPixel(im[3],i,j)>0) count++;
			if (getPixel(im[4],i,j)>0) count++;
			if (count>2) setPixel(mask,i,j,1);
/*			setPixel(mask,i,j,getPixel(im[0],i,j)
					||getPixel(im[1],i,j)
					||getPixel(im[2],i,j)
					||getPixel(im[3],i,j)
					||getPixel(im[4],i,j));		
*/		
		}
	}

	writeImage(mask,outputFilename);

	printf("U got it\n");
	return 0;
}

int main(int argc, char **argv) 
{ 
	if (argc <= 6){ 
		printf("Usage: %s <image1> <image2> <image3> <image4> <image5> <output directions file>\n", argv[0]); 
		return 0; 
	}  
	char *im1fname = argv[1];
	char *im2fname = argv[2];
	char *im3fname = argv[3];
	char *im4fname = argv[4];
	char *im5fname = argv[5];
	char *outputFilename = argv[6];
	p3(im1fname,im2fname,im3fname,im4fname,im5fname,outputFilename);
	return 0; 
}
