#ifndef VDR_EM84XX_OSD_H
#define VDR_EM84XX_OSD_H

typedef unsigned char *PBYTE;
#include <fmp.h>
#include <rm84mpegcmn.h>
#include <hwiface.h>

#include <vdr/osd.h>

typedef struct _tag_em84_paletteentry
{
	unsigned char a;
	unsigned char r;
	unsigned char g;
	unsigned char b;
} EM84PALENTRY;

//typedef unsigned char *PBYTE;

#define _EM84OSD_HEADERSIZE	1032
#define _EM84OSD_MAXWIDTH	720
#define _EM84OSD_MAXHEIGHT	576
#define _EM84OSD_PALETTESIZE	256
#define _EM84OSD_OSDBUFFERSIZE	(_EM84OSD_MAXWIDTH*_EM84OSD_MAXHEIGHT+_EM84OSD_HEADERSIZE)
#define _EM84OSD_2MB_SIZE	40944

class cOsdEm84xx: public cOsd
{
protected:
	static IOsd *mOsd;
	static unsigned char mOsdBuffer[_EM84OSD_OSDBUFFERSIZE];
	static EM84PALENTRY mPaletteEM84[_EM84OSD_PALETTESIZE];

	static int mCurrentWidth;
	static int mCurrentHeight;
	static int mOffsetX;
	static int mOffsetY;
	static int mUsedColors;
	static int mMaxColors;

	static void fillOsdHeader(unsigned char* iOsdBuffer, unsigned short iOsdWidth, unsigned short iOsdHeight, unsigned char iBpp, bool iRle, bool iHilite, unsigned int iBufSize);
	static void rgbtoyuv(double R,double G,double B,double *y,double *u,double *v);
	static void setOSDPaletteEntry(int iPaletteIndex, unsigned char* iOsdBuffer, unsigned char iRed, unsigned char iGreen, unsigned char iBlue, unsigned char iAlpha);
	int suiteColorToPalette(unsigned int vdrcolor);
	void commitPaletteToOsd();
	void setOSDPixel(int x, int y, unsigned char color);
	void resetOSD();
	void resetPalette();

	virtual void displayOSD(int x, int y) = 0;

public:
#if APIVERSNUM < 10509	
	cOsdEm84xx(int Left, int Top);
#else
	cOsdEm84xx(int Left, int Top, uint Level);
#endif
	virtual ~cOsdEm84xx();

	virtual eOsdError CanHandleAreas(const tArea *Areas, int NumAreas);
	virtual void Flush(void);

	static bool GetOsdBufferRGB(unsigned char *oImageBuffer);
};

class cOsdProviderEm84xx: public cOsdProvider
{
private:
	bool mOsd4mb;
	bool mOsdEnabled;

public:
	cOsdProviderEm84xx(bool iOsdHas4mb);
#if APIVERSNUM < 10509	
	virtual cOsd *CreateOsd(int Left, int Top);
#else	
	virtual cOsd *CreateOsd(int Left, int Top, uint Level);
#endif	

	void Enable(bool Enable);
	bool IsEnabled();
};


#endif // VDR_EM84XX_OSD_H
