//   Image Base class for CS4731: COMPUTER GRAPHICS
//   Copyright (C) 1999,2000  Mark R. Stevens
//
//   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, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

// header file
#include "Image.h"

// openGL
#ifdef WIN32
#include <windows.h>
#endif

#ifdef __APPLE_CC__
#include <gl.h>
#else
#include <GL/gl.h>
#endif

//--------------------------------------------------------------------------------  
Image::Image (int w, int h) :
  w(w), 
  h(h) {
  pixels  = new unsigned char[w * h * 3];
  zbuffer = new float        [w * h];
}
//--------------------------------------------------------------------------------  

//--------------------------------------------------------------------------------  
int Image::width() const {

  // return the requested data
  return w;

}
//--------------------------------------------------------------------------------  

//--------------------------------------------------------------------------------  
int Image::height() const {

  // return the requested data
  return h;

}
//--------------------------------------------------------------------------------  

//--------------------------------------------------------------------------------  
void Image::clear(unsigned char r, unsigned char g, unsigned char b) {

  // zip over the image and set each color to the clear color
  for (int i = 0; i < w * h; i++) {
    pixels[i * 3 + 0] = r;
    pixels[i * 3 + 1] = g;
    pixels[i * 3 + 2] = b;
    zbuffer[i]        = 1;
  }

}
//--------------------------------------------------------------------------------  

//--------------------------------------------------------------------------------  
void Image::writePixel (int           x,
			int           y, 
			float         z,
			unsigned char r,
			unsigned char g,
			unsigned char b) {

  // make sure the pixel is on the image
  if ((x < 0) || (y < 0) || (x >= w) || (y >= h)) return;

  // pixel index
  int idx = y * w + x;

  // make sure the depth is correct
  if ((z < 0) || (z > 1) || (z > zbuffer[idx])) return;

  // store the pixel color 
  pixels[idx * 3 + 0] = r;
  pixels[idx * 3 + 1] = g;
  pixels[idx * 3 + 2] = b;

  // store the new pixel depth buffer
  zbuffer[idx]        = z;

}
//--------------------------------------------------------------------------------  

//--------------------------------------------------------------------------------  
void Image::draw () const {

  // make sure we are packed properly
  glPixelStorei    (GL_UNPACK_ALIGNMENT, sizeof(unsigned char));    

  // draw the pixels using the openGL command
  glDrawPixels     (w,
		    h,
		    GL_RGB,
		    GL_UNSIGNED_BYTE,
		    static_cast<void *>(pixels));                
}
//--------------------------------------------------------------------------------  




