// Image base class - borrowed from Mark Stevens

// header file
#include "Image.h"

// openGL
#include <GL/gl.h>

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

//--------------------------------------------------------------------------------  
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;
  }

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

//--------------------------------------------------------------------------------  
void Image::writePixel (int           x,
			int           y, 
			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;

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

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

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

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

  // make sure the corner is set up correctly
//  glWindowPos2sMESA(0, 0);    

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




