/*
	James Schvaneveldt
	10/11/22
	CPSC 1011 Section 001
	Lab 7
	Program that uses PPM files and structures to loop and print out the Irish flag
*/

#include <stdio.h>


	typedef struct{
		int width, height;
	}header;

	typedef struct {
		int red, green, blue;
	} pixel;

int main (void){
	int i, j = 0, k = 0;  // k is used for the array
	
	header hdr;

	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
	fscanf(stdin, "%d", &hdr.width);	

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

	fprintf(stderr, "\n\nMaking the flag with width %d & height %d ...", hdr.width, hdr.height);
	fprintf(stderr, "\n\n");
	
//	pixel px;
	pixel px[hdr.height*hdr.width];
	
	// 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", hdr.width, hdr.height, 255);



	// 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 < hdr.height; i++ ) {
		for ( j = 0; j < hdr.width/3; j++ ) {
			px[k].red = 0;
			px[k].green = 128;
			px[k].blue = 0;
			k++;
		}
		for ( j = hdr.width/3; j < 2*hdr.width/3; j++ ) {
			px[k].red = 255;
			px[k].green = 255;
			px[k].blue = 255;
			k++;
		}
		for ( j = hdr.width*2/3; j < hdr.width; j++){
			px[k].red = 255;
			px[k].green = 165;
			px[k].blue = 0;
			k++;
		}
	}
		

	// 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 ( k = 0; k < hdr.height*hdr.width; k++ ){ 
		fprintf(stdout, "%d %d %d\n", px[k].red, px[k].green, px[k].blue);
	}



	return 0;
}
