#ifndef _TGAREADER_H_
#define _TGAREADER_H_

/* TGA Image File Formats */
typedef struct tagTGA_HEAD
{
	BYTE	Id;
	BYTE	Cmt;
	BYTE	It;
	WORD	Cms1;
	WORD	Cms2;
	BYTE	Cms3;
	WORD	XOrigin;
	WORD	YOrigin;
	WORD	TgaXSize;
	WORD	TgaYSize;
	BYTE	PixelDepth;
	BYTE	ImgDescriptor;
}TGA_HEADER;

/* Define */
#define BOTTOM_LEFT  0
#define BOTTOM_RIGHT 1
#define TOP_LEFT     2
#define TOP_RIGHT    3

/* Return codes */
#define LOAD_FAILURE				-4
#define LOAD_BADDATA				-2
#define LOAD_BADFORMAT				-1
#define LOAD_BADFILE				0
#define LOAD_SUCCESS				1

#define SAVE_FAILURE				-4
#define SAVE_FILE_WRITE_ERR			-1
#define SAVE_FILE_OPEN_ERR			0
#define SAVE_SUCCESS				1

#define VIDEOWIDTH                  720
#define VIDEOHEIGHT                 576
#define BUF_SIZE                    1024*4
#define ON                          0
#define OFF                         1
#define YES                         0
#define NO                          1
#define OK                          0
#define FAIL                        1
#define _32BIT                      32
#define _16BIT                      16
#define _24BIT                      24
#define VARY_COLOR_PIXEL            0
#define MONO_COLOR_PIXEL            1
#define _PX                         1
#define _NX                         -1
#define _PY                         1
#define _NY                         -1
#define SUCCESS                     0
#define FORMAT_ERROR                0XFF

class CTgaReader
{
public:
	HBITMAP m_hBitmap;

public:
	void ReadCelFIle(HFILE handle, TGA_HEADER* head, char* matxbuf, void * pp,short xsize, short ysize, short scrnx, short protflag,long offset, short mflag, short pixelflag, short visibleflag);
	void ReadTgaCompressLine(HFILE handle, void * data1, short xsize, short pixelsize, short* total);
	LPVOID winalloc(long size);
	void winfree(LPVOID ptr);
	void WriteLine(TGA_HEADER* head, unsigned char* data,void* tdata, char* matxbuf, short* xtable,short xsize, short protflag, short matxflag, short xdirection,short pixelflag, short visibleflag);
	void GetALLScrTable(short xsize, short ysize, short scrx, short scry,short* xtab, short* ytab );
	CTgaReader();
	~CTgaReader();

	void TransData24To16(void* data1, void* data, short size);
	void TransData24To32(void* data1, void* data, short size);
	void TransData32To16(void* data1, void* data, short size);
	void Trans16To32(unsigned short* color16, unsigned long* color32p);
	void Trans32To16(unsigned long* color32, unsigned short* color16p);
	void TransData16To32(void* data1, void* data, short size);


	int MakeRunLengthBits(COLORREF* cBits, UCHAR* bits, long total);

	int	SaveToTGA(CString filename, CDC* pDC, CRect rect, UINT uFormat = 2);
	int	LoadImage(CString filename, CBitmap* pBitmap);

private:
	int  BufPoint;
	char*  DataBuf;

};

#endif //_TGAREADER_H_