// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

#ifndef IMAGE_H
#define IMAGE_H

#include <cstdlib>

#ifdef WIN32
// Apparently Windows lacks stdint.h
typedef unsigned int uint32_t;
#else
#include <stdint.h>
#endif

#ifdef USE_SDL
typedef struct SDL_Surface SDL_Surface;
#endif

struct ImageTexture {
	unsigned int glTexNum;
	unsigned int pow2Width;
	unsigned int pow2Height;
	unsigned int origWidth;
	unsigned int origHeight;
};

// 32 Bit RGBA pixel.
union Pixel {
	uint32_t value;
	struct {
		unsigned char r;
		unsigned char g;
		unsigned char b;
		unsigned char a;
	} rgba;
	unsigned char colorsAsArray[4];
};

// Integer rectangle.
class Rect {
public:
	
	// Create a Rect between the specified points.
	inline Rect(int x1, int y1, int x2, int y2) {
		this->x1 = x1;
		this->y1 = y1;
		this->x2 = x2;
		this->y2 = y2;
	}

	// Get the width of the rectangle.
	inline int getWidth(void) const {
		return std::abs(x2 - x1);
	}

	// Get the height of the rectangle.
	inline int getHeight(void) const {
		return std::abs(y2 - y1);
	}

	int x1, y1;
	int x2, y2;

private:
};

// A basic 32 bit (RGBA) image buffer.
class Image {
public:

	// Create a 0x0 image.
	Image(void);

	// Create a new image with specified width and height.
	Image(int width, int height);

	// Destructor.
	~Image(void);

	// Set a pixel.
	inline void setPixel(const Pixel &p, int x, int y) {
		setPixel(p, x + y * width);
	}

	// Set a pixel based on linear index.
	inline void setPixel(const Pixel &p, int index) {
		if(index < 0 || index > width * height) return;
		pixels[index].value = p.value;
	}

	// Get a pointer to a pixel.
	inline Pixel *getPixel(int x, int y) {
        if(x >= width || x < 0 || y >= height || y < 0) return NULL;
		return getPixel(x + y * width);
	}

	// Get a pointer to a pixel based on linear index.
	inline Pixel *getPixel(int index) {
		if(index < 0 || index > width * height) return NULL;
		return &(pixels[index]);
	}

	// Get the width.
	inline int getWidth(void) const {
		return width;
	}

	// Get the height.
	inline int getHeight(void) const {
		return height;
	}

	// Save as a TGA to a new buffer.
	unsigned char *saveTGA(int *length);
	

#	ifdef USE_SDL
	// Copy to an SDL_Surface
	SDL_Surface *createSurface(void);
#	endif

	void createGLTexture(ImageTexture *imgTex);

private:
	Pixel *pixels;
	int width;
	int height;
};

// Load an image from a TGA file in a buffer.
Image *loadTGA(void *tgaData, int length, bool convertPink = false);

// Get the size of a TGA from just the header data.
void loadTGAHeaderGetSize(void *tgaData, int *width, int *height);

#endif // IMAGE_H

