/* Fall 2022 Lab 8
   * 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.

	Nicholas DiGennaro 
	CPSC 101-2
	October 18th, 2022
	Lab 8 
	This program takes user input to determine the width and height of an 
	image while including structures. Then I redirect the output to a ppm file where an Irish flag 
	will be diplayed. 

*/

#include <stdio.h>
#include <string.h>


typedef struct {

char type[4]; 
int width, height, maxColor; 

}header; 


typedef struct {

unsigned int r, g, b; 

}pixel;

int getWidth();
void writeHeader(header);
void fillFlagArray(pixel flag[],int width,int height); 
void writePixels(pixel flag[],int width,int height); 

int main (void){
	int width, height, i, j, k = 0;  // k is used for the array
	header dimensions; 
	pixel colors; 

	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
	width = getWidth(); 	
	// 2. calculate the height based on the proportion stated on Wikipedia
	
	height =width/2;
	fprintf(stderr, "\n\nMaking the flag with width %d & height %d ...",width,height);
	fprintf(stderr, "\n\n");

	dimensions.width = width; 
	dimensions.height = height;
	dimensions.maxColor = 255;

	// 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
		
	pixel flag[width*height];

	// write to stdout the ppm header values 
	// * with redirection, these outputs will be written to the file

	writeHeader(dimensions);

	// 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:
	fillFlagArray(flag, width, height);
		

	// 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:
	writePixels(flag, width, height);


	return 0;
}

int getWidth(){
	int width;
	scanf("%d", &width); 	
	return width; 
	}

void writeHeader(header dimensions){
	strcpy(dimensions.type, "P3"); 
	fprintf(stdout, "%s\n", dimensions.type);
	fprintf(stdout, "%d %d %d\n", dimensions.width, dimensions.height, 255);

	}

void fillFlagArray(pixel flag[],int width,int height){
	int k=0; 
	pixel colors;
	for (int i=0; i<height; i++ ) {
		int j;
	   for (j=0; j<width/3; j++ ) {
			colors.r= 0;
			colors.g= 128;
			colors.b= 0; 
			flag[k++]= colors;
		}
		for (; j<width*2/3; j++ ) {
			colors.r= 255;
			colors.g= 255;
			colors.b= 255;
			flag[k++]= colors;
		}
		for (; j<width; j++ ) {
			colors.r= 255;
			colors.g= 165;
			colors.b= 0;
			flag[k++]= colors;
		}
	}

}

void writePixels(pixel flag[],int width,int height){
	for (int i=0; i<width*height; i++) {
		fprintf(stdout, "%i %i %i\n", flag[i].r, flag[i].g, flag[i].b); 
	}
} 
