#include "defs.h"
/*
John Severson
11/15/22
CPSC 1011 section 001
Lab 11
This program copies an image using pointers and file pointers.It then shrinks
the image to one half of its original size.
*/



//main function for the program
//command line arguements argc(how many aruments) argv(the "array" of 
//arguements.
int main (int argc, char *argv[]){
	//declaring file pointers
	FILE *inFile;
	FILE *outFile;
	
	inFile = fopen(argv[1], "r");
	if (inFile == NULL) {
		fprintf(outFile, "File open error. Exiting program\n");
		exit(1);
	}

	outFile = fopen(argv[2], "w");
	if (outFile == NULL) {
		fprintf(outFile, "Output failure. Exiting program\n");
		exit(2);
	}

   //call to get the header info from file
  
	header image = getHeader(inFile);

   //creates pointer for pixels array
	
	values *pixelsPtr = NULL;

	//allocates dynamic memory for the image
	
	pixelsPtr = (values *)calloc((image.width * image.height), sizeof(values));

	if(pixelsPtr == NULL) {
		printf("Unable to allocate array. Exiting program. \n");
		exit(1);
	}

	//fills array of pixels
	fillImageArray(pixelsPtr, image.width, image.height, inFile);
	
   //call for write header funciton
   writeHeader(image, outFile);

//	prints the pixel array
//	writePixels(pixelsPtr, image.width, image.height);
// uses pointer as parameter for the allocated memory

	halfSizeWritePixels(pixelsPtr, image.width, image.height, outFile);

   return 0;
}

