#ifndef __IMG__
#define __IMG__

#include <afxtempl.h>


class CIMG : public CObject
{
public:
	CIMG(HANDLE hIMG=NULL,int nBits=8);
	virtual ~CIMG();
	CIMG& operator=(CIMG& IMG);
	BOOL IsValid() { return (m_pVoid && Width() && Height());};
	void UseGamma(float fg,BOOL bUse=TRUE);
	BOOL CreateFromHandle(HANDLE hIMG,int nBits);
	BOOL Create(int width,int height,int bits=24);
	BOOL Create(int width,int height,COLORREF color,int bits=24);
	BOOL Create(BITMAPINFOHEADER& bmInfo);
	BOOL CopyIMG(CIMG& IMG);
	BOOL SaveIMG(CString& fileName);
	BOOL OpenIMG(CString& fileName);
	void ReplaceColor(unsigned char oldColor,unsigned char newColor);
	HANDLE GetIMGits(int nStartX=-1,int nStartY=-1,int nCx=-1,int nCy=-1);
	CBitmap *GetBitmap(CDC& dc);
	CBitmap *GetTempBitmap(CDC& dc);
	DWORD GetIMGSize();
	int GetPaletteSize(BITMAPINFOHEADER& bmInfo);
	int GetPaletteSize();
	int CountColors();
	int EnumColors(BYTE *colors);
	void InitIMG(COLORREF color);
	void CopyLine(int source,int dest);
	void DestroyIMG();
	void SetPalette(unsigned char *palette);
	void SetPalette(RGBQUAD *pRGB);
	COLORREF PaletteColor(int index);
	void SetPixel32(int x, int y, unsigned char R, unsigned char G, unsigned char B, unsigned char A);
	void SetPixel(int x,int y,COLORREF color);
	void SetPixel8(int x,int y,unsigned char color);
	COLORREF GetPixel(int x,int y);
	void GetPixel32(int x,int y,unsigned char &R, unsigned char &G, unsigned char &B, unsigned char &A);
	void GetPixel(UINT x,UINT y,int& pixel);
	void BitBlt(HDC hDest,int nXDest,int nYDest,int nWidth,int nHeight,int xSrc,int ySrc);
	void BitBlt(int nXDest,int nYDest,int nWidth,int nHeight,CIMG& IMGSrc,int nSrcX,int nSrcY,BYTE *colors=NULL);
	void StretchBlt(HDC hDest,int nXDest,int nYDest,int nDWidth,int nDHeight,int xSrc,int ySrc,int  nSWidth,int nSHeight);
	void StretchBlt(int nXDest,int nYDest,int nDWidth,int nDHeight,CIMG& IMGSrc,int xSrc,int ySrc,int  nSWidth,int nSHeight);
	void ExpandBlt(int nXDest,int nYDest,int xRatio,int yRatio,CIMG& IMGSrc,int xSrc,int ySrc,int  nSWidth,int nSHeight);
	void SetFlags(int flag) { m_nFlags = flag; };
	int Height() { return height ; };
	int Width() { return width ; };
	unsigned char *GetLinePtr(int line);	
	inline int GetBitCount() {return m_pInfo->bmiHeader.biBitCount;};
	BOOL Make8Bit(CIMG& IMG);
	BOOL SwitchFromOne(CIMG& IMG);
	BOOL SwitchFromFour(CIMG& IMG);
	BOOL SwitchFrom24(CIMG& IMG);
	BOOL SwitchPalette(CIMG& IMG);
	int  ClosestColor(RGBQUAD *pRgb);
	LPBITMAPINFO GetBitmapInfo() { return m_pInfo;};
	static unsigned int Distance(RGBQUAD& rgb1,RGBQUAD& rgb2);
	BOOL Draw(HDC hDC, int dX, int dY, int dWidth, int dHeight, int sX, int sY, int sWidth, int sHeight);
	
	unsigned char *GetBuffer() { return m_pBits; };
	int	GetEffWidth() { return (width * m_nBits/8 + 3)&~3; } ;
	int GetPlane() { return m_nBits/8; };

protected :
	HANDLE IMGHandle();
	BOOL SaveBMP(CString& csFileName);
	BOOL OpenBMP(CString& csFileName);
	void CreateGammaCurve();
	void Expand(int nXDest,int nYDest,int xRatio,int yRatio,CIMG& IMGSrc,int xSrc,int ySrc,int  nSWidth,int nSHeight);

public:
	unsigned char *m_pBits;

protected:
	PBITMAPINFO m_pInfo;
	RGBQUAD *m_pRGB;
	void *m_pVoid; 
	BYTE **m_pLinePtr;
	int height,bytes,width,m_nBits;
	int m_nFlags;
	BOOL m_bUseGamma;
	float m_fGamma;
	float m_fOldGamma;
	unsigned char Gamma[256];
	RGBQUAD CacheQuad[256];
	char CachePtr[256];
};


#endif