/*
	Ben Nazaruk
	October 4, 2022
	CPSC 1011-005
	Lab 07

	The program will ask the user to input a width dimension. The program
	will then create a structure for a ppm header and array of pixels using
	those dimensions. The program will then fill the rgb values of the pixels
	in the array with values corresponding to the flag of Ireland that will
	be output to stdout preceded by the header data for redirection 
	to a ppm file.
*/

#include <stdio.h>

typedef struct header
{
	char type[3];
	int width, height, maxColor;
} header_t;
typedef struct pixel
{
	unsigned int r, g, b;
} pixel_t;

int main (void)
{
	
	int width, height, i, j, k = 0;
	
	//user input for width
	fprintf(stderr, "\n\nFlag of Ireland");
	fprintf(stderr, "\n\tWhat width do you want the flag to be? ");
	fscanf(stdin, "%i", &width);
	
	height = width/2;

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

	//pxl array and header from dimensions
	pixel_t imgPx[width*height];
	header_t imgHeader = {"P3", width, height, 255};

	//ppm header
	fprintf(stdout, "%s\n%d %d %d\n", imgHeader.type, imgHeader.width, imgHeader.height, imgHeader.maxColor);

	//fill array with pixel rbg values
	for(i = 0; i < height; i++)
	{
		for(j = 0; j < (width/3); j++)
		{
			imgPx[k].r = 0;
			imgPx[k].g = 128;
			imgPx[k].b = 0;
			k++;
		}
		for(j = 0; j < (width/3); j++)
		{
			imgPx[k].r = 255;
			imgPx[k].g = 255;
			imgPx[k].b = 255;
			k++;
		}
		for(j = 0; j < (width/3); j++)
		{
			imgPx[k].r = 255;
			imgPx[k].g = 165;
			imgPx[k].b = 0;
			k++;
		}
	}
		
	//print all rgb values to file
	for(i = 0; i < (width*height); i++)
	{
		fprintf(stdout, "%i %i %i ", imgPx[i].r, imgPx[i].g, imgPx[i].b);
	}


	return 0;
}
