#ifndef TGATEXTURE_H
#define TGATEXTURE_H

#include "Texture.h"
#include <GL/glu.h>
#undef __glext_h_
#undef __glxext_h_
#undef __gl_h_
#include <GL/GLee.h>

enum TGATypes
{
	TGA_NODATA = 0,
	TGA_INDEXED = 1,
	TGA_RGB = 2,
	TGA_GRAYSCALE = 3,
	TGA_INDEXED_RLE = 9,
	TGA_RGB_RLE = 10,
	TGA_GRAYSCALE_RLE = 11
};

// Image Data Formats
#define	IMAGE_RGB       0
#define IMAGE_RGBA      1
#define IMAGE_LUMINANCe 2

// Image data types
#define	IMAGE_DATA_UNSIGNED_BYTE 0

// Pixel data transfer from file to screen:
// These masks are AND'd with the imageDesc in the TGA header,
// bit 4 is left-to-right ordering
// bit 5 is top-to-bottom
#define BOTTOM_LEFT  0x00	// first pixel is bottom left corner
#define BOTTOM_RIGHT 0x10	// first pixel is bottom right corner
#define TOP_LEFT     0x20	// first pixel is top left corner
#define TOP_RIGHT    0x30	// first pixel is top right corner

// TGA header
struct tgaheader_t
{
	unsigned char  idLength;		
	unsigned char  colorMapType;	
	unsigned char  imageTypeCode;	
	unsigned char  colorMapSpec[5];	
	unsigned short xOrigin;			
	unsigned short yOrigin;			
	unsigned short width;			
	unsigned short height;			
	unsigned char  bpp;				
	unsigned char  imageDesc;		
};

struct rgba_t
{
	unsigned char r;
	unsigned char g;
	unsigned char b;
	unsigned char a;
};

struct rgb_t
{
	unsigned char r;
	unsigned char g;
	unsigned char b;
};

class TgaTexture : public Texture
{
private:
	unsigned char  m_colorDepth;
	unsigned char  m_imageDataType;
	unsigned char  m_imageDataFormat;
	unsigned char *m_pImageData;
	unsigned short m_width;
	unsigned short m_height;	
	unsigned long  m_imageSize;

	// swap the red and blue components in the image data
	void SwapRedblue();

public:
	TgaTexture();
	virtual ~TgaTexture();

	// loading and unloading
	bool load(const char *sFilename);
	void Release();

	// flips image vertically
	bool FlipVertical();

	// returns the current image data
	unsigned char *GetImage() 
	{ 
		return m_pImageData; 
	}
};

#endif