/* Surface
 * (C) Copyright 1995 by Ashton Mason (amason@cs.uct.ac.za)
 *
 * Permission to use, modify, copy and distribute this source code for
 * any purpose and without fee is granted, provided that this copyright
 * notice appears in all copies and supporting documentation, and that
 * credit is given where due. This source code is provided "as is" with
 * no express or implied warranty.
 */

#include "headers.h"

	// constructor


image::image(void)
{
  data = NULL;
  xsize = ysize = 0;
}


	// deconstructor


image::~image(void)
{
  if (data) delete data;
}


	// destoy old image, if any
        // create new 8 bit image of specified size


int image::create8Bit(int xdim, int ydim)
{
  if (data) delete data;
  xsize = 0;
  ysize = 0;

  data = new unsigned char[xdim * ydim];
  if (!data)
  {
    error("not enough memory for image buffer");
    return 0;
  }

  xsize = xdim;
  ysize = ydim;

  return 1;
}


	// destoy old image, if any
        // create new 24 bit image of specified size


int image::create24Bit(int xdim, int ydim)
{
  if (data) delete data;
  xsize = 0;
  ysize = 0;

  data = new unsigned char[xdim * ydim * 3];
  if (!data)
  {
    error("not enough memory for image buffer");
    return 0;
  }

  xsize = xdim;
  ysize = ydim;

  return 1;
}


	// destroy the current image, releasing memory


int image::destroy()
{
  xsize = 0;
  ysize = 0;

  if (data)
  {
    delete data;
    return 1;
  }

  return 0;
}


	// writes the current (8 bit) image to a PGM file


int image::writePGM(char *name)
{
  FILE *output;
  char header[20];

  if (!(output = fopen(name, "wb")))
  {
    error("error opening PGM file");
    return 0;
  }

  sprintf(header, "P5\n%d %d\n255\n", imagex, imagey);
  fwrite(header, 1, strlen(header), output);  		// header

	// copy from buffer into frame, converting to ints

  if (fwrite(data, sizeof(unsigned char), imagex * imagey, output) != imagex * imagey)
  {
    error("error writing to PGM file");
    return 0;
  }

  fclose(output);
  return 1;
}


	// writes the current (24 bit) image to a PPM file


int image::writePPM(char *name)
{
  FILE *output;
  char header[20];

  if (!(output = fopen(name, "wb")))
  {
    error("error opening PPM file");
    return 0;
  }

  sprintf(header, "P6\n%d %d\n255\n", imagex, imagey);
  fwrite(header, 1, strlen(header), output);  		// header

	// copy from buffer into frame, converting to ints

  if (fwrite(data, imagex * 3, imagey, output) != imagey)
  {
    error("error writing to PPM file");
    return 0;
  }

  fclose(output);
  return 1;
}



	// write the current (24 bit) image to a TGA file


int image::writeTGA(char *name)
{
  tga targa;

  if (!data)
  {
    error("no image to write to file - program error");
    return 0;
  }

  targa.data = data;
  targa.xsize = imagex;
  targa.ysize = imagey;

  if (!targa.writeTGA(name))
  {
    targa.data = NULL;
    return 0;
  }

  targa.data = NULL;
  return 1;
}


