#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. The program uses file pointers 
that I made in the main file to take input and write output to different
files.
*/


//prints values of the pixel array(creates flag)
void writePixels(values p1[], int wWP, int hWP) {

   for (int k = 0; k < wWP*hWP; k++) {
      fprintf(stdout, "%i %i %i\n", p1[k].r, p1[k].g, p1[k].b);
   }

}
//prints a shrunken version of the original image one half its size
void halfSizeWritePixels(values p2[], int wHW, int hHW, FILE *out) {
	
	unsigned int avgr, avgg, avgb;
//takes average of 4 pixels at a time and uses that as the color for 
//the new pixel
	for (int k = 0; k < wHW*hHW; (k = k + 2)) {
		avgr = (p2[k].r + p2[k+1].r + p2[k + wHW].r + p2[k + wHW + 1].r)/4;	

		avgg = (p2[k].g + p2[k+1].g + p2[k + wHW].g + p2[k + wHW + 1].g)/4;

      avgb = (p2[k].b + p2[k+1].b + p2[k + wHW].b + p2[k + wHW + 1].b)/4;
		
		if ((k + wHW + 1) != ((wHW*hHW) - 1)) {
		fprintf(out, "%i %i %i\n", avgr, avgg, avgb);
		}
//accounts for any odd widths using a modulus function
		if ((k/wHW) %2 != 0) {
			k = k + wHW;
		}
	}
	
}
