#include "c4.h"
#include <string.h>
#include <errno.h>

void c4_io_out(struct Image *img,
               char *filename)
{
  FILE *file;
  int x,y;

  if(!(file = fopen(filename,"w"))) {
    fprintf(stderr,
            "c4_io_out: unable to open '%s' for writing: %s\n",
            filename,
            strerror(errno));
    return;
  }

  fprintf(file,
          "P6\n"
          "%d %d 255\n",
          img->width,img->height);

  for(y=0;y<img->height;y++)
    for(x=0;x<img->width;x++)
      fprintf(file,
              "%c%c%c",
              ImagePixel(img,x,y).r,
              ImagePixel(img,x,y).g,
              ImagePixel(img,x,y).b);

  fclose(file);
}
