#ifndef _gdiITF_
#define _gdiITF_
/*<<<***************************************************************************
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: gdiItf
#Project: xdcGdi
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<15/09/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/

#ifndef _gdiBASE_
#	include "gdiBase.h"
#endif

/*
#ifndef _gdiFONT_
#	include "gdiFont.h"
#endif
*/
#ifndef _xbSTREAMITF_
#	include <xbStreamItf.h>
#endif

#ifndef _xbEXCEPTION_
#	include <xbException.h>
#endif

#ifndef _xbHOTPLUG_
#	include <xbHotPlug.h>
#endif

/*enum xbOpMode
{
	gdiOptional = 0,
	xbOp_Required = 1
};
enum xbOpMode
{
	xbOp_Optional,
	xbOp_Required
};*/

enum gdiCompressLevel
{
	gdiCompressDefault = -1,
	gdiCompressHigh = -2,
	gdiCompressMid = -3,
	gdiCompressLow = -4
};

struct gdiEXPORT gdiSettingInfo
{
	ulong				cbStruct;
	LCID				lcid;
	ulong				cp;
	ulong				infoReqFlags;
	xbString			name,label,help,unit;
	xbArray<xbString>	arrChoiceName,arrChoiceLabel,arrChoiceHelp;
	int					minVal,maxVal,dfltVal,curVal;
	int64				unitCoeffMul,unitCoeffDiv;
	gdiSettingInfo()
	{
		cbStruct = sizeof(gdiSettingInfo);
		lcid = xbLCID_NEUTRAL;
		cp = CP_ACP;
		infoReqFlags = 0;
		Empty();
	}
	void Empty()
	{
		name.Empty();
		label.Empty();
		help.Empty();
		unit.Empty();
		arrChoiceName.Empty();
		arrChoiceLabel.Empty();
		arrChoiceHelp.Empty();
		minVal = maxVal = dfltVal = curVal = 0;
		unitCoeffMul = unitCoeffDiv = 0;
	}
};

interface __declspec(uuid("{E68F6C27-C4D1-4958-950A-A1E91FB40B16}")) IgdiDecompressor;
interface __declspec(uuid("{E798BE00-1308-41e2-8849-E45B2B6F6685}")) IgdiCompressor;
interface __declspec(uuid("{899F89F2-B700-42d1-AD8C-CCACB1DFA6D9}")) IgdiImage;
interface __declspec(uuid("{DA1064E7-1254-42af-A158-A218394BF0F5}")) IgdiBitmap;
interface __declspec(uuid("{47446313-C473-4892-A34B-720DBD20A51D}")) IgdiDecompressorCallBack;

enum gdiCompressCaps
{
	gdiCompressCaps_None =						0x00000000,
	gdiCompressCaps_Read =						0x00000001,
	gdiCompressCaps_Write =						0x00000002,
	gdiCompressCaps_ReadWrite =					0x00000003,
	gdiCompressCaps_CompressWithQualityLost =	0x00000004,
	gdiCompressCaps_CompressWithNoQualityLost = 0x00000008,
	gdiCompressCaps_AnimationSupport =			0x00000010,
	gdiCompressCaps_PagesSupport =				0x00000020,
	gdiCompressCaps_AlphaOpacity =				0x00000040,
	gdiCompressCaps_TransparentColor =			0x00000080
};

interface __declspec(uuid("{97404814-01A8-4daf-96D2-2AF5FA771EB4}")) 
	gdiEXPORT IgdiCompressPlug:public IxbObject
{
	//Auto detection functions
	virtual bool CanWorkWithType(LPCSTR pszType, IxbInputStream *pStm, xbRefPtr<IgdiDecompressor> *pprDecomp = NULL, IgdiImage *pImg = NULL) const = 0;
	virtual bool CanWorkWithType(LPCSTR pszType, const uchar *pStmData, int lgData, int *pLgDataMin, xbRefPtr<IgdiDecompressor> *pprDecomp = NULL, IgdiImage *pImg = NULL) const = 0;

	virtual bool Decompress(ulong caps, xbRefPtr<IgdiDecompressor> *pprUncomp,xbOpMode reqMode = xbOp_Required) = 0;
	virtual bool Compress(ulong caps, xbRefPtr<IgdiCompressor> *pprComp,xbOpMode reqMode = xbOp_Required) = 0;

	virtual void askClose(LPCSTR pReason) = 0;

	virtual bool EnumDecompressSetting(int &no, gdiSettingInfo *pInfo) const = 0;

	virtual xbString GetCompressLabel(bool bShort = true,LCID lcid = xbLCID_NEUTRAL) const = 0;
	virtual xbString GetCompressVersion(bool bShort = true,LCID lcid = xbLCID_NEUTRAL) const = 0;
	virtual xbString GetDefaultExtension() const = 0;

	virtual bool GetCompressLevelInfo(gdiSettingInfo *pInfo) const = 0;
	virtual bool EnumCompressSetting(int &no, gdiSettingInfo *pInfo) const = 0;

	virtual gdiCompressCaps GetCaps() const = 0;
};

enum gdiBmpColorType
{
	gdiBmpColor_Error = -1,
	gdiBmpColor_Gray = 0,
	gdiBmpColor_RGB = 1,
//	gdiBmpColor_CMYK = 2,
//	gdiBmpColor_YUV = 3, //luminance(Y) and chrominance(UV = hue(U/color tint of an image) + saturation(V)) 
//	gdiBmpColor_YUVK = 4,
//	gdiBmpColor_YUVA = 5,
//	gdiBmpColor_GrayAlpha = 6
	gdiBmpColor_RGBA = 7,	//A: Alpha
};

#define gdiLoopFlag_LeaveImage			0x00000001
#define gdiLoopFlag_RestorePrevious		0x00000002
#define gdiLoopFlag_RestoreBackground	0x00000004 //default
#define gdiLoopFlag_WaitUserInput		0x00000008
#define gdiLoopFlag_ForceBackground		0x00000010

struct gdiEXPORT gdiBmpInfo
{
	ulong				cbStruct;
	LCID				lcid;
	ulong				cp;
	ulong				infoReqFlags;
	xbString			label,author,comment;
	gdiSizeLow			imgSz;
	gdiSizeLow			bmpSz;
	gdiPointLow			bmpOffset;
	gdiSizeU			bmpMetricSz;
	long				bmpId,bmpPage;
	ulong				bmpLoopTime;	//Time in ms
	long				bmpLoopNext;
	long				bmpLoopCount;
	ulong				bmpLoopFlags;
	gdiBmpColorType		bmpType;
	int					bmpBitsPerPixel;
	gdiColor			bmpBgColor; //use if gdiLoopFlag_ForceBackground set
	gdiBmpInfo()
	{
		cbStruct = sizeof(gdiBmpInfo);
		cp = CP_ACP;
		lcid = xbLCID_NEUTRAL;
		infoReqFlags = 0;
		Empty();
	}
	void Empty()
	{
		label.Empty();
		author.Empty();
		comment.Empty();
		imgSz.Empty();
		bmpSz.Empty();
		bmpOffset.Empty();
		bmpMetricSz.Empty();
		bmpPage=-1;
		bmpId=-1;
		bmpLoopTime = -1;
		bmpLoopNext = -1;
		bmpLoopCount = -1;
		bmpLoopFlags = 0;
		bmpType = gdiBmpColor_Error;
		bmpBitsPerPixel = -1;
	}
	void OwnData()
	{
		label.OwnData();
		author.OwnData();
		comment.OwnData();
	}
};

interface gdiEXPORT IgdiImage:public IxbObject
{
	virtual void LoadFromStream(LPCSTR pFormat,IxbInputStream *pStm) = 0;
	virtual void SaveToStream(LPCSTR pFormat,int compressLevel,IxbOutputStream *pStm) const = 0;

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

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

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

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

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

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

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

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

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

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

	virtual void askClose(LPCSTR pReason) = 0;

//OS dependant
	virtual void msAlphaUndo(int bmpId = -1) = 0;
	virtual void msAlphaCorrect(int bmpId = -1) = 0;
	virtual HBITMAP getHandle(xbLock *pLck = NULL,int bmpId = -1) = 0;

	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) = 0;
	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) = 0;
	virtual bool HasAlpha(int bmpId=-1) = 0;
};

interface gdiEXPORT IgdiBitmap:public IxbObject
{
//	virtual bool GetDC(LPCSTR pDCtype, xbRefPtr<IgdiDC> *pprDC,xbOpMode reqMode = xbOp_Required) = 0;
	virtual void askClose(LPCSTR pReason) = 0;

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

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

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

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

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

	virtual void CopyDataFrom(const void *pData, int cByteData) = 0;
	
//OS dependant
	virtual void msAlphaUndo() = 0;
	virtual void msAlphaCorrect() = 0;
	virtual HBITMAP getHandle(xbLock *pLck = NULL) = 0;

	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) = 0;
	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) = 0;
	virtual bool HasAlpha() = 0;
};

interface gdiEXPORT IgdiDecompressorCallBack:public IxbObject
{
	virtual void onImageHeader(IgdiImage *pImg,int bmpAnimNo,int bmpPage) = 0;
	virtual void onImageEnd(IgdiImage *pImg,int bmpAnimNo,int bmpPage) = 0;
};

interface gdiEXPORT IgdiDecompressor:public IxbOutputStream
{
	virtual void NewOutput(IgdiImage *pImg = NULL,IgdiDecompressorCallBack *pCb = NULL) = 0;
	virtual bool GetOutput(xbRefPtr<IgdiImage> *pprImg,xbOpMode reqMode = xbOp_Required) = 0;

	virtual bool EnumDecompressSetting(int &no, gdiSettingInfo *pInfo) const = 0;

	virtual bool SetDecompressSetting(LPCSTR pszName,int intVal,LPCSTR strVal,xbOpMode reqMode = xbOp_Required) = 0;
	virtual bool GetDecompressSetting(LPCSTR pszName,int *pIntVal,xbString *pStrVal) const = 0;
	virtual void ResetDecompressSettings() = 0;

	virtual void askClose(LPCSTR pReason) = 0;
};

interface gdiEXPORT IgdiCompressor:public IxbInputStream
{
	virtual void NewInput(IgdiImage *pImg) = 0;
	virtual void NewInput(IgdiBitmap *pBmp) = 0;
	virtual bool GetInput(xbRefPtr<IgdiImage> *pprImg,xbOpMode reqMode = xbOp_Required) = 0;
	virtual bool GetInput(xbRefPtr<IgdiBitmap> *pprBmp,xbOpMode reqMode = xbOp_Required) = 0;

	virtual void SetCompressLevel(int compressLevel) = 0;
	virtual int GetCompressLevel() const = 0;

	virtual bool EnumCompressSetting(int &no, gdiSettingInfo *pInfo) const = 0;

	virtual bool SetCompressSetting(LPCSTR pszName,int intVal,LPCSTR strVal,xbOpMode reqMode = xbOp_Required) = 0;
	virtual bool GetCompressSetting(LPCSTR pszName,int *pIntVal,xbString *pStrVal) const = 0;
	virtual void ResetCompressSettings(int compressLevel = gdiCompressDefault) = 0;

	virtual void askClose(LPCSTR pReason) = 0;
};

struct gdiCompressPlugInfo
{
	xbString	extensions;
	xbString	typeLabel,typeVersion;
	xbRefPtr<IgdiCompressPlug> prPlug;
	gdiCompressCaps	caps;
};

class gdiEXPORT  __declspec(uuid("{321A9C42-F414-4f06-92BE-CDEE915B2AD5}"))
	gdiCompressPlugType:public xbHotPlugType
{
public:
	gdiCompressPlugType(const char *pPlugTypeName,const char *pPlugTypeVer,const char *pLabel,const xbEngines &engUsed = xbEngines())
		:xbHotPlugType(pPlugTypeName,pPlugTypeVer,pLabel,engUsed)
	{
	}
	virtual ~gdiCompressPlugType()
	{
		Destroy();
	}
	virtual bool GetInterface(REFIID riid, void ** ppInterf)
	{
		xbImplInterface(gdiCompressPlugType);
		return xbHotPlugType::GetInterface(riid,ppInterf);
	}
	bool	CreatePluggedObject(const xbString &name,xbRefPtr<IgdiCompressPlug> *pprObj,xbRefPtr<IxbHotPlugin> *pprPlugin = NULL);
	int		GetSupportedType(xbArray<xbString> *pArrType);
	int		GetSupportedType(xbArray<gdiCompressPlugInfo> *pArrType,gdiCompressCaps caps = gdiCompressCaps_None,bool bGetPlugRef = false,LCID lcid = xbLCID_NEUTRAL);

	bool	CanWorkWith(IxbHotPlugin *pObj,const xbString &name);
	bool	CanWorkWithType(LPCSTR pszType,IxbInputStream *pStm, xbRefPtr<IgdiDecompressor> *pprDecomp, IgdiImage *pImg);
	bool	CanWorkWithType(LPCSTR pszType,const uchar *pStmData, int lgData, int *pLgDataMin, xbRefPtr<IgdiDecompressor> *pprDecomp, IgdiImage *pImg);

	xbString GetImageFileFilter(xbString *pDfltExt,gdiCompressCaps caps = gdiCompressCaps_None,LCID lcid = xbLCID_NEUTRAL);
protected:
	static int PrioritySort(const Object &obj1,const Object &obj2);
	bool	CreatePluggedObject(sIxbPlugPtr *pPtr,const xbString &name)
	{
		return xbHotPlugType::CreatePluggedObject(pPtr,name);
	}
	bool	CreatePluggedObject(const xbString &name,xbRefPtr<IxbObject> *pprObj,xbRefPtr<IxbHotPlugin> *pprPlugin);
};

//Exception gdiEcompress: used by gdi system to signal image (de)compression errors
xbEdclStart(gdiEcompress,xbEadv,{97D22FF5-AF9A-4f4d-9973-A087C761125C},gdiEXPORT)
	virtual xbMBiconType GetMBoxIcon() const {return xbMBicon_Stop;}
	virtual const char *GetMBoxTitle() const {return "GDI compression system error";} 
xbEdclEnd(gdiEcompress);

xbEdclStart(gdiEcompressUser,gdiEcompress,{97D22FF5-AF9A-4f4d-9973-A087C761125C},gdiEXPORT)
	virtual xbMBiconType GetMBoxIcon() const {return xbMBicon_Exclamation;}
	virtual const char *GetMBoxTitle() const {return "GDI compression error";} 
xbEdclEnd(gdiEcompressUser);

gdiEXPORT extern gdiCompressPlugType	g_gdiCompressPlugType;

gdiEXPORT extern bool gdiCreateImage(xbRefPtr<IgdiImage> *pprImg,LPCSTR pType, LPCSTR pLabel, xbOpMode reqMode = xbOp_Required);
gdiEXPORT extern bool gdiCreateBitmap(xbRefPtr<IgdiBitmap> *pprBmp,const gdiSizeLow &sz, gdiBmpColorType bmpType, int bitsPerPixel, xbOpMode reqMode = xbOp_Required);
#endif
