/*
 * image.c - read and write images.
 *
 * Version 1.0
 * Copyright (C) 1998 Gustav Taxen.
 * 
 * This is free software with ABSOLUTELY NO WARRANTY.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, write to the Free Software
 * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <assert.h>
#include <malloc.h>
#include <stdio.h>

#include <image.h>


int
readImage (image    *img,
	   char     *filename)
{
  FILE   *in;
  char    str[256];
  int     readMode;
  int     nbytes;
  int     maxColorVal;
  int     ready;

  assert (filename);
  
  in = fopen (filename, "r");
  if (!in)
    {
      return -1;
    }

  readMode = 0;
  nbytes = 0;
  ready = 0;

  while (!ready)
   {
     fscanf (in, "%s", str);
     if (str[0] == '#')         /* start of comment line */
       {
	 /* read until end of line */

	 while (getc (in) != '\n')
	   ;
       }
     else
       {
	 /* not a comment, so read whatever's appropriate */

	 switch (readMode)
	   {
	   case 0:
	     ++readMode;
	     break;
	   case 1:
	     sscanf (str, "%d", &img->width);
	     ++readMode;
	     break;
	   case 2:
	     sscanf (str, "%d", &img->height);
	     img->data = (char *) malloc (sizeof (char)
					  * img->width * img->height * 3);
	     assert (img->data);
	     ++readMode;
	     break;
	   case 3:
	     sscanf (str, "%d", &maxColorVal);
	     ++readMode;
	     ready = 1;
	     break;
	   }
       }
   }

  while (getc (in) != '\n')
    ;

  nbytes = fread (img->data,
		  sizeof (char), 
		  img->width * img->height * 3,
		  in);
  
  fclose (in);
  return nbytes;
}


int
writeImage (char    *filename,
	    image   *img)
{
  FILE    *out;
  int      nbytes;

  out = fopen (filename, "w");
  assert (out);

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

  nbytes = fwrite (img->data,
		   sizeof (char), 
		   img->width * img->height * 3, 
		   out);  
  fclose (out);
  return nbytes;
}


char *
getPixel (image   *img,
	  int      x,
	  int      y)
{
  char    *ptr;
  int      rowSize;

  assert (img->data);

  assert (x >= 0);
  assert (x < img->width);
  assert (y >= 0);
  assert (y < img->height);

  ptr = img->data;

  rowSize = img->width * 3;
  ptr += y * rowSize;
  ptr += x * 3;

  return ptr;
}


void
setPixel (char    *dstPixel,
	  char    *srcPixel)
{
  assert (dstPixel);
  assert (srcPixel);

  *dstPixel = *srcPixel;
  *(dstPixel + 1) = *(srcPixel + 1);
  *(dstPixel + 2) = *(srcPixel + 2);
}
