#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define imagecolumns 82
#define imagerows    62
typedef unsigned char BYTE;
typedef BYTE colimage[imagerows][imagecolumns][3];

extern colimage picture;




FILE *_fopen(char *filename, char *mode)
{
  FILE *stream = fopen(filename, mode);
  if(stream == NULL)
  {
    fprintf(stderr,
	    "Unable to open \"%s\" for writing.\n"
	    "Reported error: %s.\n", filename, strerror(errno));
    exit(EXIT_FAILURE);
  }
  return stream;
}


void _fprintf(FILE *stream, char *format, ...)
{
  va_list ap;
  int result;

  va_start(ap, format);
  result = vfprintf(stream, format, ap);
  va_end(ap);

  if(ferror(stream))
  {
    fprintf(stderr, "Error while writing to output file.\n"
	    "Reported error: %s.\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
}


void WritePPM(colimage *pic, char *filename)
{
  FILE *stream = _fopen(filename, "wb");
  int i, j, k;

  _fprintf(stream, "P3\n%d %d %d\n", imagecolumns, imagerows, 255);
  for(i = 0; i < imagerows; i++)
    for(j = 0; j < imagecolumns; j++)
      for(k = 0; k < 3; k++)
	_fprintf(stream, "  %d,", (*pic)[i][j][k]);
  _fprintf(stream, "};\n");
  fclose(stream);
}



int main(int argc, char* argv[])
{
  /* Filter the image */
  // Filter(picture)

  /* Write the filtered image to a file */
  WritePPM(&picture, argv);
  return 0;
}







void Filter(colimage *cimage)
{
	/* Your code goes here */
	/* To get the value of a component of a pixel, first dereference the array */
	/* For example: (*cimage)[i][j][k]                                         */
}



