/* Fall 2022 Lab 7
   * intro to PPM with creating a flag of Ireland
			green|white|orange
	* reinforces loops and arrays

	PPM images have 2 parts:
		header information
		pixel information

	An image is height (rows) X width (columns).
	So, a 300 X 600 image is 300 rows high and 600 columns wide.

	All of the image contents (header and pixel info) will be written
	to stdout so when we run the program with redirection, those items
	being sent to stdout will be redirected to a file

	For example, to run this program, you will type:
		./a.out > flag.ppm
	and everything that was sent to stdout will be written to the file
	called  flag.ppm

	All the other statements that you want to go to the screen and not 
	the image will be written to stderr.

*/

/*
Parthiv Patel
14/06/2022
CPSC 1011
Lab 7
Printing the flag of Ireland based on the user input on how wide the want the flag and based on input calculate height and create a PPM image using structs, having individual values of r, g, and b so that each pixel has its own value.
*/


#include <stdio.h>



typedef struct pixel{
	unsigned int r;
	unsigned int g;
	unsigned int b;
}pixel_t;

typedef struct header{
	int width, height, maxColor;
}header_t;


int main (void){
	int width, height, i, j, k = 0;  // k is used for the array
	header_t header;
	fprintf(stderr, "\n\nFlag of Ireland");
	fprintf(stderr, "\n\tWhat width do you want the flag to be? ");
	// 1. get the user's input for width

	scanf(" %d", &header.width);	

	// 2. calculate the height based on the proportion stated on Wikipedia
	header.height = header.width/2;

	fprintf(stderr, "\n\nMaking the flag with width %d & height %d ...",
		header.width, header.height);
	fprintf(stderr, "\n\n");


	// 3. declare an array of type:  unsigned int
	// * it will need to be big enough to hold all the color values:
	// * each pixel is made up of red, green, and blue values
	//   so each pixel has 3 things in it
	// * therefore (HINT):  the width is actually 3 times bigger
	int a = header.width*header.height;	
	pixel_t pixel[a];
	pixel_t flag;

	header.maxColor = 255;

	// write to stdout the ppm header values 
	// * with redirection, these outputs will be written to the file
	fprintf(stdout, "P3\n");
	
	fprintf(stdout, "%d %d %d\n", header.width, header.height, header.maxColor);


	// 4. nested for loops for writing the pixel data:
	// 	a.  outer for loop is for each row
	// 	b.  because the flag of Irelad has 3 equal vertical parts,
	//        there will be 3 equal inner for loops, each one going
	//        1/3 of the way across the row (i.e. 1/3 of the columns)
	// pseudocode:
	 
	for (i=0; i<header.height; i++ ) {
	   for (j=0; j<header.width/3;j++) {
			flag.r = 0;
			flag.g = 128;
			flag.b = 0;
			pixel[k++] = flag;
		//	pixel_t.pixel[k++]= {0,128,0};
		//	pixel[k++]=128;
		//	pixel[k++]=0;
		//	fprintf(stdout, "%i %i %i\n", 0, 128, 0);
		  // fill in array with green 
		//	which is r, g, b of:  0 128 0
		}
		for (j=0; j<header.width/3; j++) {
			flag.r = 255;
			flag.g = 255;
			flag.b = 255;
			pixel[k++] = flag;

		//	pixel_t.pixel[k++] = {255,255,255};
		//	pixel[k++]= 255;
		//	pixel[k++]= 255;
		//	fprintf(stdout, "%i %i %i\n", 255, 225, 225);
		//	fill in array with white 
		//	which is r, g, b of:  255 255 255 
		}
		for (j=0; j< header.width/3; j++) {
			flag.r = 255;
			flag.g = 165;
			flag.b = 0;
			pixel[k++] = flag;
		//	pixel_t.pixel[k++] = {255, 165, 0};
		//	pixel[k++] = 165;
		//	pixel[k++] = 0;
		//	fprintf(stdout, "%i %i %i\n", 255, 165, 0);
		//	fill in array with orange 
		//	which is r, g, b of:  255 165 0 
		}
	}
	
		

	// 5. print the contents of the array to stdout, so with redirection,
	//    the pixels will be written to the .ppm file
	//    * with P3, the unsigned int values must be written so that
	//      the values are 3 to a line (see lab write-up)
	// pseudocode:
	
	for (i=0; i<a; i++) {

		fprintf(stdout, "%i %i %i\n", pixel[i].r, pixel[i].g , pixel[i].b );
	}
	


	return 0;
}
