#ifndef _gdiIMAGE_
#define _gdiIMAGE_
/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#Title: gdiImage
#Project: xdcGdi
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<15/09/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/

#ifndef _gdiITF_
#	include "gdiItf.h"
#endif

class __declspec( uuid("{895DDEFB-D3F0-476b-8D64-9B6E1DFD1181}"))
	gdiImage:public xbObject,public IgdiImage
{
public:
	gdiImage()
	{
	}

	virtual ~gdiImage()
	{
	}
	virtual void setParams(LPCSTR pType,LPCSTR pLabel);

	xbImplObject();

	virtual bool GetInterface(REFIID riid, void ** ppInterf)
	{
		xbImplInterface(IgdiImage);
		xbImplInterface(gdiImage);
		return xbObject::GetInterface(riid,ppInterf);
	}

	//IgdiImage methods
	virtual void LoadFromStream(LPCSTR pFormat,IxbInputStream *pStm);
	virtual void SaveToStream(LPCSTR pFormat,int compressLevel,IxbOutputStream *pStm) const;

//	virtual bool GetDC(int bmpId,LPCSTR pDCtype,xbRefPtr<IgdiDC> *pprDC,xbOpMode reqMode = xbOp_Required);
	virtual bool GetBitmap(int bmpId,xbRefPtr<IgdiBitmap> *pprBmp,xbOpMode reqMode = xbOp_Required);

	virtual gdiSizeLow GetPixelSize(int bmpId = -1) const;
	virtual gdiSizeU GetMetricSize(int bmpId = -1) const;

	virtual bool IsAnimated() const;
	virtual bool IsPaginated() const;
	virtual bool HasMultipleResolution() const;

	virtual int GetAnimCount() const;
	virtual int GetAnimDelay(int bmpId) const;
	virtual int GetAnimNext(int bmpId) const;

	virtual int GetPageCount() const;
	virtual int GetBitmapCount() const;

	virtual int GetBitmapInfo(xbArray<gdiBmpInfo> *pArray) const;
	virtual int GetBitmapInfo(int bmpId,gdiBmpInfo *pInfo) const;

	virtual int GetBitmapId(int bmpAnimNo,int bmpPage,gdiSizeU *pSz) const;

	virtual int SetBitmapInfo(xbArray<gdiBmpInfo> *pArray);
	virtual int SetBitmapInfo(int bmpId,gdiBmpInfo *pInfo,IgdiBitmap *pBmp);
	virtual int CreateBitmap(int bmpId,gdiBmpInfo *pInfo,xbRefPtr<IgdiBitmap> *pprBmp);

	virtual void Empty();
	virtual bool IsEmpty() const;
	virtual bool IsValid() const;
	virtual void SetBitmapCount(int bmpCnt);

	virtual void SetPixelSize(const gdiSizeLow &szWanted) const;
	virtual void SetMetricSize(const gdiSizeU &szWanted) const;

	virtual void askClose(LPCSTR pReason);
	virtual xbString getCloseReason() const;

//OS dependant
	virtual void msAlphaUndo(int bmpId = -1);
	virtual void msAlphaCorrect(int bmpId = -1);
	virtual HBITMAP getHandle(xbLock *pLck = NULL,int bmpId = -1);
	virtual void Draw(int bmpId,IgdiDC &dcDest,int x,	int y, int width, int height, int xSrc = 0, int ySrc = 0, 
		int widthSrc = -1, int heightSrc = -1, int alpha = -1);
	virtual void DrawGrayed(int bmpId,IgdiDC &dcDest,int x,	int y, int width, int height, int xSrc = 0, int ySrc = 0, 
		int widthSrc = -1, int heightSrc = -1, int alpha = -1);
	virtual bool HasAlpha(int bmpId=-1);
protected:
	xbString	m_type,m_label;

	struct	BmpItem
	{
		gdiBmpInfo				m_info;
		xbRefPtr<IgdiBitmap>	m_prBmp;
	};

	xbArray<BmpItem>	m_arrBmp;

	virtual void onDestruction(bool bInDestroy);
	xbString		m_closeReason;
};


class __declspec( uuid("{A9B14FC7-9251-499c-BECE-E1BAD6ED07A8}"))
	gdiBitmap:public xbObject,public IgdiBitmap,public sIxbLock
{
public:
	gdiBitmap()
	{
		m_bmpType = gdiBmpColor_Error;
		m_bmpBitsPerPixel = 0;
		m_bmpRowByteLength = 0;
		m_hBitmap = NULL;
		m_bmpDataPtr = NULL;
		m_bMSalphaCorrect = false;
	}

	virtual ~gdiBitmap()
	{
	}
	virtual bool setParams(const gdiSizeLow &sz, gdiBmpColorType bmpType, int bitsPerPixel, xbOpMode reqMode);

	xbImplObject();

	virtual bool GetInterface(REFIID riid, void ** ppInterf)
	{
		xbImplInterface(IgdiBitmap);
		xbImplInterface(gdiBitmap);
		xbImplInterface(sIxbLock);
		return xbObject::GetInterface(riid,ppInterf);
	}

	//IgdiBitmap methods
//	virtual bool GetDC(LPCSTR pDCtype,xbRefPtr<IgdiDC> *pprDC,xbOpMode reqMode = xbOp_Required);
	virtual void askClose(LPCSTR pReason);
	virtual xbString getCloseReason() const;

	virtual void GetBitmapInfo(gdiSizeLow *pSz,gdiBmpColorType *pBmpType,int *pBmpBitsPerPixel) const;
	virtual bool SetBitmapInfo(const gdiSizeLow *pSz,gdiBmpColorType bmpType,int bmpBitsPerPixel,bool bConvert = true,xbOpMode reqMode = xbOp_Required);
	virtual bool Convert(const gdiSizeLow *pSz,gdiBmpColorType bmpType,int bmpBitsPerPixel,xbRefPtr<IgdiBitmap> *pprBmpDest,xbOpMode reqMode = xbOp_Required);
	virtual bool CopyTo(IgdiBitmap *pBmpDest,xbOpMode reqMode = xbOp_Required);
	virtual bool Grayed(xbRefPtr<IgdiBitmap> *pprBmpDest,xbOpMode reqMode = xbOp_Required);

	virtual gdiSizeLow GetPixelSize() const;
	virtual gdiBmpColorType GetColorType() const;
	virtual int GetBitsPerPixel() const;
	virtual int GetRowByteLength() const;

	virtual void SetPixelSize(const gdiSizeLow &sz,bool bConvert = true);
	virtual void SetColorType(gdiBmpColorType colorType,bool bConvert = true);
	virtual void SetBitsPerPixel(int bmpBitsPerPixel,bool bConvert = true);

	virtual int GetBitmapRow(xbLock	*pLck,int rowStart, uchar** rowPointers, int rowCnt, int rowRequiredByteLength);
	virtual uchar *getBitmapData(xbLock	*pLck,int *pRowByteLength);

	virtual void Clear(const gdiColor &clr);
	virtual void Empty();
	virtual bool IsEmpty() const;
	virtual bool IsValid() const;

	virtual void CopyDataFrom(const void *pData, int cByteData);

	//sIxbLock methods
	virtual void				Lock() const
	{
		m_sectPtr.Lock();
	}
	virtual void				Unlock() const
	{
		m_sectPtr.Unlock();
	}

	//OS dependant
	virtual void msAlphaUndo();
	virtual void msAlphaCorrect();
	virtual HBITMAP getHandle(xbLock *pLck = NULL);

	virtual void Draw(IgdiDC &dcDest,int x,	int y, int width, int height, int xSrc = 0, int ySrc = 0, 
		int widthSrc = -1, int heightSrc = -1, int alpha = -1);
	virtual void DrawGrayed(IgdiDC &dcDest,int x,	int y, int width, int height, int xSrc = 0, int ySrc = 0, 
		int widthSrc = -1, int heightSrc = -1, int alpha = -1);
	virtual bool HasAlpha();
protected:

	virtual void onDestruction(bool bInDestroy);

	bool create( const gdiSizeLow &sz, gdiBmpColorType bmpType, int bitsPerPixel, xbOpMode reqMode);
	bool convert( const gdiSizeLow &sz, gdiBmpColorType bmpType, int bitsPerPixel, xbOpMode reqMode);
	void effectGray(IgdiBitmap *pBmp,gdiSizeLow &sz,int bytePerPixel);

	gdiSizeLow			m_bmpSz;
	gdiBmpColorType	m_bmpType;
	long			m_bmpBitsPerPixel,m_bmpRowByteLength;
//	xbData			m_bmpData;

	HBITMAP			m_hBitmap;
	uchar			*m_bmpDataPtr;
	xbString		m_closeReason;

	xbRefPtr<IgdiBitmap> m_prGrayed;
	bool				 m_bMSalphaCorrect;
};
#endif
