// DIB.h

/*
	modified by c_gao June 2008

*/

#ifndef __DIB
#define __DIB

#include "../source/platform.h"


enum PIXEL_FORMAT_16BIT//针对16bit颜色的像素格式
{
	DIB_PIXEL_FORMAT16_RGB=0,
	DIB_PIXEL_FORMAT16_555=15,
	DIB_PIXEL_FORMAT16_565=16
};

class CDIB 
{
public:
	enum BitmapType
	{
		BMP,
		GIF,
		TIFF
	};
	CDIB(HANDLE hDib=NULL, int nBits=8);
	virtual ~CDIB();
	CDIB& operator=(CDIB& dib);
	BOOL IsValid() { return (m_pVoid && Width() && Height());}
	void UseGamma(float fg,BOOL bUse=TRUE);
	BOOL CreateFromHandle(HANDLE hDib, int nBits);
	BOOL Create(int width,int height, int bits=24,PIXEL_FORMAT_16BIT format=DIB_PIXEL_FORMAT16_RGB);
	BOOL Create(BITMAPINFOHEADER& bmInfo);
	BOOL CopyDIB(CDIB& dib);
	void ReplaceColor(unsigned char oldColor, unsigned char newColor);
	HANDLE GetDIBits(int nStartX=-1, int nStartY=-1, int nCx=-1, int nCy=-1);
	//CBitmap *GetBitmap(CDC& dc);
	//CBitmap *GetTempBitmap(CDC& dc);
	DWORD GetDIBSize();
	int GetPaletteSize(BITMAPINFOHEADER& bmInfo);
	int GetPaletteSize();
	int CountColors();
	int EnumColors(BYTE *colors);
	void InitDIB(COLORREF color);
	void CopyLine(int source, int dest);
	void DestroyDIB();
	void SetPalette(unsigned char *palette);
	void SetPalette(RGBQUAD *pRGB);
	COLORREF PaletteColor(int index);
	void SetPixel(int x, int y, COLORREF color);
	void SetPixel8(int x, int y, unsigned char color);
	COLORREF GetPixel(int x, int y);
	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, CDIB& dibSrc,
		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, CDIB& dibSrc,
		int xSrc, int ySrc, int  nSWidth, int nSHeight);
	void ExpandBlt(int nXDest, int nYDest, int xRatio, int yRatio, CDIB& dibSrc, 
		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(CDIB& dib);
	BOOL SwitchFromOne(CDIB& dib);
	BOOL SwitchFromFour(CDIB& dib);
	BOOL SwitchFrom24(CDIB& dib);
	BOOL SwitchPalette(CDIB& dib);
	int  ClosestColor(RGBQUAD *pRgb);
	LPBITMAPINFO GetBitmapInfo() { return m_pInfo;}
	static unsigned int Distance(RGBQUAD& rgb1, RGBQUAD& rgb2);
//Get Bits add by c_gao
	unsigned char* GetBits();
protected :
	HANDLE DIBHandle();
	void CreateGammaCurve();
	void Expand(int nXDest, int nYDest, int xRatio, int yRatio, CDIB& dibSrc,
		int xSrc, int ySrc, int  nSWidth, int nSHeight);
	unsigned char *m_pBits;
	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];

	//added by c_gao
	PIXEL_FORMAT_16BIT	m_pixel_format;
};


#endif