typedef struct	Cachefont Cachefont;
typedef struct	Cacheinfo Cacheinfo;
typedef struct	Cachesubf Cachesubf;
typedef struct	Display Display;
typedef struct	Font Font;
typedef struct	Fontchar Fontchar;
typedef struct	Image Image;
typedef struct	Mouse Mouse;
typedef struct	Point Point;
typedef struct	Rectangle Rectangle;
typedef struct	RGB RGB;
typedef struct	Screen Screen;
typedef struct	Subfont Subfont;

extern	int	Rconv(va_list*, Fconv*);
extern	int	Pconv(va_list*, Fconv*);

enum
{
	DOpaque		= 0xFFFFFFFF,
	DTransparent	= 0x00000000,		/* only useful for allocimage, memfillcolor */
	DBlack		= 0x000000FF,
	DWhite		= 0xFFFFFFFF,
	DRed		= 0xFF0000FF,
	DGreen		= 0x00FF00FF,
	DBlue		= 0x0000FFFF,
	DCyan		= 0x00FFFFFF,
	DMagenta		= 0xFF00FFFF,
	DYellow		= 0xFFFF00FF,
	DPaleyellow	= 0xFFFFAAFF,
	DDarkyellow	= 0xEEEE9EFF,
	DDarkgreen	= 0x448844FF,
	DPalegreen	= 0xAAFFAAFF,
	DMedgreen	= 0x88CC88FF,
	DDarkblue	= 0x000055FF,
	DPalebluegreen= 0xAAFFFFFF,
	DPaleblue		= 0x0000BBFF,
	DBluegreen	= 0x008888FF,
	DGreygreen	= 0x55AAAAFF,
	DPalegreygreen	= 0x9EEEEEFF,
	DYellowgreen	= 0x99994CFF,
	DMedblue		= 0x000099FF,
	DGreyblue	= 0x005DBBFF,
	DPalegreyblue	= 0x4993DDFF,
	DPurpleblue	= 0x8888CCFF,

	DNotacolor	= 0xFFFFFF00,
	DNofill		= DNotacolor,
	
};

enum
{
	Displaybufsize	= 8000,
	ICOSSCALE	= 1024,
	Borderwidth =	4,
};

enum
{
	/* refresh methods */
	Refbackup	= 0,
	Refnone		= 1,
	Refmesg		= 2
};
#define	NOREFRESH	((void*)-1)

enum
{
	/* line ends */
	Endsquare	= 0,
	Enddisc		= 1,
	Endarrow	= 2,
	Endmask		= 0x1F
};

#define	ARROW(a, b, c)	(Endarrow|((a)<<5)|((b)<<14)|((c)<<23))

/*
 * image channel descriptors 
 */
enum {
	CRed = 0,
	CGreen,
	CBlue,
	CGrey,
	CAlpha,
	CMap,
	CIgnore,
	NChan,
};

#define __DC(type, nbits)	((((type)&15)<<4)|((nbits)&15))
#define CHAN1(a,b)	__DC(a,b)
#define CHAN2(a,b,c,d)	(CHAN1((a),(b))<<8|__DC((c),(d)))
#define CHAN3(a,b,c,d,e,f)	(CHAN2((a),(b),(c),(d))<<8|__DC((e),(f)))
#define CHAN4(a,b,c,d,e,f,g,h)	(CHAN3((a),(b),(c),(d),(e),(f))<<8|__DC((g),(h)))

#define NBITS(c) ((c)&15)
#define TYPE(c) (((c)>>4)&15)

enum {
	GREY1	= CHAN1(CGrey, 1),
	GREY2	= CHAN1(CGrey, 2),
	GREY4	= CHAN1(CGrey, 4),
	GREY8	= CHAN1(CGrey, 8),
	CMAP8	= CHAN1(CMap, 8),
	RGB15	= CHAN4(CIgnore, 1, CRed, 5, CGreen, 5, CBlue, 5),
	RGB16	= CHAN3(CRed, 5, CGreen, 6, CBlue, 5),
	RGB24	= CHAN3(CRed, 8, CGreen, 8, CBlue, 8),
	RGBA32	= CHAN4(CRed, 8, CGreen, 8, CBlue, 8, CAlpha, 8),
	ARGB32	= CHAN4(CAlpha, 8, CRed, 8, CGreen, 8, CBlue, 8),	/* stupid VGAs */
	XRGB32  = CHAN4(CIgnore, 8, CRed, 8, CGreen, 8, CBlue, 8),
};

extern	char*	chantostr(char*, ulong);
extern	ulong	strtochan(char*);
extern	int		chantodepth(ulong);

struct	Point
{
	int	x;
	int	y;
};

struct Rectangle
{
	Point	min;
	Point	max;
};

typedef void	(*Reffn)(Image*, Rectangle, void*);

struct Screen
{
	Display	*idisplay;	/* display holding data */
	int	id;		/* id of system-held Screen */
	Image	*image;		/* unused; for reference only */
	Image	*fill;		/* color to paint behind windows */
};

struct Display
{
	QLock	qlock;
	int		locking;	/*program is using lockdisplay */
	int		dirno;
	int		fd;
	int		reffd;
	int		ctlfd;
	int		imageid;
	int		local;
	int		depth;
	ulong	chan;
	void		(*error)(Display*, char*);
	char		*devdir;
	char		*windir;
	char		oldlabel[64];
	ulong		dataqid;
	Image		*white;
	Image		*black;
	Image		*image;
	Image		*opaque;
	Image		*transparent;
	uchar		*buf;
	int			bufsize;
	uchar		*bufp;
	Font		*defaultfont;
	Subfont		*defaultsubfont;
	Image		*windows;
};

struct Image
{
	Display		*idisplay;	/* display holding data */
	int		id;		/* id of system-held Image */
	Rectangle	r;		/* rectangle in data area, local coords */
	Rectangle 	clipr;		/* clipping region */
	int		depth;		/* number of bits per pixel */
	ulong	chan;
	int		repl;		/* flag: data replicates to tile clipr */
	Screen		*iscreen;	/* 0 if not a window */
	Image		*next;	/* next in list of windows */
};

struct RGB
{
	ulong	red;
	ulong	green;
	ulong	blue;
};

/*
 * Subfonts
 *
 * given char c, Subfont *f, Fontchar *i, and Point p, one says
 *	i = f->info+c;
 *	draw(b, Rect(p.x+i->left, p.y+i->top,
 *		p.x+i->left+((i+1)->x-i->x), p.y+i->bottom),
 *		color, f->bits, Pt(i->x, i->top));
 *	p.x += i->width;
 * to draw characters in the specified color (itself an Image) in Image b.
 */

struct	Fontchar
{
	int		x;		/* left edge of bits */
	uchar		top;		/* first non-zero scan-line */
	uchar		bottom;		/* last non-zero scan-line + 1 */
	char		left;		/* offset of baseline */
	uchar		width;		/* width of baseline */
};

struct	Subfont
{
	char		*name;
	short		n;		/* number of chars in font */
	uchar		height;		/* height of image */
	char		ascent;		/* top of image to baseline */
	Fontchar 	*info;		/* n+1 character descriptors */
	Image		*bits;		/* of font */
	int		ref;
};

enum
{
	/* starting values */
	LOG2NFCACHE =	6,
	NFCACHE =	(1<<LOG2NFCACHE),	/* #chars cached */
	NFLOOK =	5,			/* #chars to scan in cache */
	NFSUBF =	2,			/* #subfonts to cache */
	/* max value */
	MAXFCACHE =	1024+NFLOOK,		/* upper limit */
	MAXSUBF =	50,			/* generous upper limit */
	/* deltas */
	DSUBF = 	4,
	/* expiry ages */
	SUBFAGE	=	10000,
	CACHEAGE =	10000
};

struct Cachefont
{
	Rune		min;	/* lowest rune value to be taken from subfont */
	Rune		max;	/* highest rune value+1 to be taken from subfont */
	int		offset;	/* position in subfont of character at min */
	char		*name;			/* stored in font */
	char		*isubfontname;		/* to access subfont */
};

struct Cacheinfo
{
	ushort		x;		/* left edge of bits */
	uchar		width;		/* width of baseline */
	schar		left;		/* offset of baseline */
	Rune		value;	/* value of character at this slot in cache */
	ushort		age;
};

struct Cachesubf
{
	ulong		age;	/* for replacement */
	Cachefont	*cf;	/* font info that owns us */
	Subfont		*f;	/* attached subfont */
};

struct Font
{
	char		*name;
	Display		*idisplay;
	short		height;	/* max height of image, interline spacing */
	short		ascent;	/* top of image to baseline */
	short		width;	/* widest so far; used in caching only */	
	short		nsub;	/* number of subfonts */
	ulong		age;	/* increasing counter; used for LRU */
	int		maxdepth;	/* maximum depth of all loaded subfonts */
	int		ncache;	/* size of cache */
	int		nsubf;	/* size of subfont list */
	Cacheinfo	*cache;
	Cachesubf	*subf;
	Cachefont	**sub;	/* as read from file */
	Image		*cacheimage;
};

#define	Dx(r)	((r).max.x-(r).min.x)
#define	Dy(r)	((r).max.y-(r).min.y)

/*
 * Image management
 */
extern Image*	pm__allocimage(Image*, Display*, Rectangle, ulong, int, ulong, int, int);
extern Image*	pm_allocimage(Display*, Rectangle, ulong, int, ulong);
extern uchar*	pm_bufimage(Display*, int);
extern int	pm_bytesperline(Rectangle, int);
extern void	pm_closedisplay(Display*);
extern void	pm_drawerror(Display*, char*);
extern int	pm__drawprint(int, char*, ...);
extern int	pm_flushimage(Display*, int);
extern int	pm_freeimage(Image*);
extern int	pm__freeimage1(Image*);
extern int	pm_geninitdraw(char*, void(*)(Display*, char*), char*, char*, char*, int);
extern int	pm_initdraw(void(*)(Display*, char*), char*, char*);
extern Display*	pm_initdisplay(char*, char*, void(*)(Display*, char*));
extern int	pm_loadimage(Image*, Rectangle, uchar*, int);
extern int	pm_cloadimage(Image*, Rectangle, uchar*, int);
extern int	pm_getwindow(Display*, int);
extern int	pm_gengetwindow(Display*, char*, Image**, Screen**, int);
extern Image* pm_readimage(Display*, int, int);
extern Image* pm_creadimage(Display*, int, int);
extern int	pm_unloadimage(Image*, Rectangle, uchar*, int);
extern int	pm_wordsperline(Rectangle, int);
extern int	pm_writeimage(int, Image*, int);
extern Image*	pm_namedimage(Display*, char*);
extern int	pm_nameimage(Image*, char*, int);
extern Image* pm_allocimagemix(Display*, ulong, ulong);

/*
 * Colors
 */
extern	void	pm_readcolmap(Display*, RGB*);
extern	void	pm_writecolmap(Display*, RGB*);
extern	ulong	pm_setalpha(ulong, uchar);

/*
 * Windows
 */
extern Screen*	pm_allocscreen(Image*, Image*, int);
extern Image*	pm__allocwindow(Image*, Screen*, Rectangle, int, ulong);
extern Image*	pm_allocwindow(Screen*, Rectangle, int, ulong);
extern void	pm_bottomnwindows(Image**, int);
extern void	pm_bottomwindow(Image*);
extern int	pm_freescreen(Screen*);
extern Screen*	pm_publicscreen(Display*, int, ulong);
extern void	pm_topnwindows(Image**, int);
extern void	pm_topwindow(Image*);
extern int	pm_originwindow(Image*, Point, Point);

/*
 * Geometry
 */
extern Point		Pt(int, int);
extern Rectangle	Rect(int, int, int, int);
extern Rectangle	Rpt(Point, Point);
extern Point		addpt(Point, Point);
extern Point		subpt(Point, Point);
extern Point		divpt(Point, int);
extern Point		mulpt(Point, int);
extern int		eqpt(Point, Point);
extern int		eqrect(Rectangle, Rectangle);
extern Rectangle	insetrect(Rectangle, int);
extern Rectangle	rectaddpt(Rectangle, Point);
extern Rectangle	rectsubpt(Rectangle, Point);
extern Rectangle	canonrect(Rectangle);
extern int		rectXrect(Rectangle, Rectangle);
extern int		rectinrect(Rectangle, Rectangle);
extern void		combinerect(Rectangle*, Rectangle);
extern int		rectclip(Rectangle*, Rectangle);
extern int		ptinrect(Point, Rectangle);
extern void		replclipr(Image*, int, Rectangle);
extern int		drawreplxy(int, int, int);	/* used to be drawsetxy */
extern Point	drawrepl(Rectangle, Point);
extern int		rgb2cmap(int, int, int);
extern int		cmap2rgb(int);
extern int		cmap2rgba(int);
extern void		icossin(int, int*, int*);
extern void		icossin2(int, int, int*, int*);

/*
 * Graphics
 */
extern void	pm_draw(Image*, Rectangle, Image*, Image*, Point);
extern void	pm_gendraw(Image*, Rectangle, Image*, Point, Image*, Point);
extern void	pm_line(Image*, Point, Point, int, int, int, Image*, Point);
extern void	pm_poly(Image*, Point*, int, int, int, int, Image*, Point);
extern void	pm_fillpoly(Image*, Point*, int, int, Image*, Point);
extern Point	pm_string(Image*, Point, Image*, Point, Font*, char*);
extern Point	pm_stringn(Image*, Point, Image*, Point, Font*, char*, int);
extern Point	pm_runestring(Image*, Point, Image*, Point, Font*, Rune*);
extern Point	pm_runestringn(Image*, Point, Image*, Point, Font*, Rune*, int);
extern Point	pm_stringbg(Image*, Point, Image*, Point, Font*, char*, Image*, Point);
extern Point	pm_stringnbg(Image*, Point, Image*, Point, Font*, char*, int, Image*, Point);
extern Point	pm_runestringbg(Image*, Point, Image*, Point, Font*, Rune*, Image*, Point);
extern Point	pm_runestringnbg(Image*, Point, Image*, Point, Font*, Rune*, int, Image*, Point);
extern Point	pm__string(Image*, Point, Image*, Point, Font*, char*, Rune*, int, Rectangle, Image*, Point);
extern Point	pm_stringsubfont(Image*, Point, Image*, Subfont*, char*);
extern int		pm_bezier(Image*, Point, Point, Point, Point, int, int, int, Image*, Point);
extern int		pm_bezspline(Image*, Point*, int, int, int, int, Image*, Point);
extern int		pm_fillbezier(Image*, Point, Point, Point, Point, int, Image*, Point);
extern int		pm_fillbezspline(Image*, Point*, int, int, Image*, Point);
extern void	pm_ellipse(Image*, Point, int, int, int, Image*, Point);
extern void	pm_fillellipse(Image*, Point, int, int, Image*, Point);
extern void	pm_arc(Image*, Point, int, int, int, Image*, Point, int, int);
extern void	pm_fillarc(Image*, Point, int, int, Image*, Point, int, int);
extern void	pm_border(Image*, Rectangle, int, Image*, Point);

/*
 * Font management
 */
extern Font*	pm_openfont(Display*, char*);
extern Font*	pm_buildfont(Display*, char*, char*);
extern void	pm_freefont(Font*);
extern Font*	pm_mkfont(Subfont*, Rune);
extern int	pm_cachechars(Font*, char**, Rune**, ushort*, int, int*, char**);
extern void	pm_agefont(Font*);
extern Subfont*	pm_allocsubfont(char*, int, int, int, Fontchar*, Image*);
extern Subfont*	pm_lookupsubfont(Display*, char*);
extern void	pm_installsubfont(char*, Subfont*);
extern void	pm_uninstallsubfont(Subfont*);
extern void	pm_freesubfont(Subfont*);
extern Subfont*	pm_readsubfont(Display*, char*, int, int);
extern Subfont*	pm_readsubfonti(Display*, char*, int, Image*, int);
extern int	pm_writesubfont(int, Subfont*);
extern void	_unpackinfo(Fontchar*, uchar*, int);
extern Point	pm_stringsize(Font*, char*);
extern int	pm_stringwidth(Font*, char*);
extern int	pm_stringnwidth(Font*, char*, int);
extern Point	pm_runestringsize(Font*, Rune*);
extern int	pm_runestringwidth(Font*, Rune*);
extern int	pm_runestringnwidth(Font*, Rune*, int);
extern Point	pm_strsubfontwidth(Subfont*, char*);
extern int	pm_loadchar(Font*, Rune, Cacheinfo*, int, int, char**);
extern char*	pm_subfontname(char*, char*, int);
extern Subfont*	pm__getsubfont(Display*, char*);
extern Subfont*	pm_getdefont(Display*);
extern void		pm_lockdisplay(Display*);
extern void	pm_unlockdisplay(Display*);
extern int		pm_drawlsetrefresh(ulong, int, void*, void*);

/*
 * Predefined 
 */
extern	uchar	defontdata[];
extern	int		sizeofdefont;
extern	Point		ZP;
extern	Rectangle	ZR;

/*
 * Set up by initdraw()
 */
extern	Display	*pm_display;
extern	Font		*pm_font;
extern	Image	*pm_screen;
extern	Screen	*pm__screen;
extern	int	pm__cursorfd;
extern	int	pm__drawdebug;	/* set to 1 to see errors from flushimage */

#define	BGSHORT(p)		(((p)[0]<<0) | ((p)[1]<<8))
#define	BGLONG(p)		((BGSHORT(p)<<0) | (BGSHORT(p+2)<<16))
#define	BPSHORT(p, v)		((p)[0]=(v), (p)[1]=((v)>>8))
#define	BPLONG(p, v)		(BPSHORT(p, (v)), BPSHORT(p+2, (v)>>16))

/*
 * Compressed image file parameters and helper routines
 */
#define	NMATCH	3		/* shortest match possible */
#define	NRUN	(NMATCH+31)	/* longest match possible */
#define	NMEM	1024		/* window size */
#define	NDUMP	128		/* maximum length of dump */
#define	NCBLOCK	6000		/* size of compressed blocks */
extern	void	_twiddlecompressed(uchar*, int);
extern	int	_compblocksize(Rectangle, int);

/* XXX backwards helps; should go */
extern	int		log2[];
extern	ulong	drawld2chan[];
extern	void		pm_drawsetdebug(int);

#ifndef NODEFINE

#undef  _allocimage
#define _allocimage			pm__allocimage
#undef  allocimage
#define allocimage			pm_allocimage
#undef  bufimage
#define bufimage			pm_bufimage
#undef  bytesperline
#define bytesperline			pm_bytesperline
#undef  closedisplay
#define closedisplay			pm_closedisplay
#undef  drawerror
#define drawerror			pm_drawerror
#undef  _drawprint
#define _drawprint			pm__drawprint
#undef  flushimage
#define flushimage			pm_flushimage
#undef  freeimage
#define freeimage			pm_freeimage
#undef  freescreen
#define freescreen			pm_freescreen
#undef  _freeimage1
#define _freeimage1			pm__freeimage1
#undef  geninitdraw
#define geninitdraw			pm_geninitdraw
#undef  initdraw
#define initdraw			pm_initdraw
#undef  initdisplay
#define initdisplay			pm_initdisplay
#undef  loadimage
#define loadimage			pm_loadimage
#undef  cloadimage
#define cloadimage			pm_cloadimage
#undef  getwindow
#define getwindow			pm_getwindow
#undef  gengetwindow
#define gengetwindow			pm_gengetwindow
#undef  readimage
#define readimage			pm_readimage
#undef  creadimage
#define creadimage			pm_creadimage
#undef  unloadimage
#define unloadimage			pm_unloadimage
#undef  wordsperline
#define wordsperline			pm_wordsperline
#undef  writeimage
#define writeimage			pm_writeimage
#undef  namedimage
#define namedimage			pm_namedimage
#undef  nameimage
#define nameimage			pm_nameimage
#undef  allocimagemix
#define allocimagemix			pm_allocimagemix
#undef  readcolmap
#define readcolmap			pm_readcolmap
#undef  writecolmap
#define writecolmap			pm_writecolmap
#undef  setalpha
#define setalpha			pm_setalpha
#undef  allocscreen
#define allocscreen			pm_allocscreen
#undef  _allocwindow
#define _allocwindow			pm__allocwindow
#undef  allocwindow
#define allocwindow			pm_allocwindow
#undef  bottomnwindows
#define bottomnwindows			pm_bottomnwindows
#undef  bottomwindow
#define bottomwindow			pm_bottomwindow
#undef  publicscreen
#define publicscreen			pm_publicscreen
#undef  topnwindows
#define topnwindows			pm_topnwindows
#undef  topwindow
#define topwindow			pm_topwindow
#undef  originwindow
#define originwindow			pm_originwindow
#undef  draw
#define draw			pm_draw
#undef  gendraw
#define gendraw			pm_gendraw
#undef  line
#define line			pm_line
#undef  poly
#define poly			pm_poly
#undef  fillpoly
#define fillpoly			pm_fillpoly
#undef  string
#define string			pm_string
#undef  stringn
#define stringn			pm_stringn
#undef  runestring
#define runestring			pm_runestring
#undef  runestringn
#define runestringn			pm_runestringn
#undef  stringbg
#define stringbg			pm_stringbg
#undef  runestringbg
#define runestringbg			pm_runestringbg
#undef  _string
#define _string			pm__string
#undef  stringsubfont
#define stringsubfont			pm_stringsubfont
#undef  bezier
#define bezier			pm_bezier
#undef  bezspline
#define bezspline			pm_bezspline
#undef  fillbezier
#define fillbezier			pm_fillbezier
#undef  fillbezspline
#define fillbezspline			pm_fillbezspline
#undef  ellipse
#define ellipse			pm_ellipse
#undef  fillellipse
#define fillellipse			pm_fillellipse
#undef  arc
#define arc			pm_arc
#undef  fillarc
#define fillarc			pm_fillarc
#undef  border
#define border			pm_border
#undef  openfont
#define openfont			pm_openfont
#undef  buildfont
#define buildfont			pm_buildfont
#undef  freefont
#define freefont			pm_freefont
#undef  mkfont
#define mkfont			pm_mkfont
#undef  cachechars
#define cachechars			pm_cachechars
#undef  agefont
#define agefont			pm_agefont
#undef  allocsubfont
#define allocsubfont			pm_allocsubfont
#undef  lookupsubfont
#define lookupsubfont			pm_lookupsubfont
#undef  installsubfont
#define installsubfont			pm_installsubfont
#undef  uninstallsubfont
#define uninstallsubfont			pm_uninstallsubfont
#undef  freesubfont
#define freesubfont			pm_freesubfont
#undef  readsubfont
#define readsubfont			pm_readsubfont
#undef  readsubfonti
#define readsubfonti			pm_readsubfonti
#undef  writesubfont
#define writesubfont			pm_writesubfont
#undef  stringsize
#define stringsize			pm_stringsize
#undef  stringwidth
#define stringwidth			pm_stringwidth
#undef  stringnwidth
#define stringnwidth			pm_stringnwidth
#undef  runestringsize
#define runestringsize			pm_runestringsize
#undef  runestringwidth
#define runestringwidth			pm_runestringwidth
#undef  runestringnwidth
#define runestringnwidth			pm_runestringnwidth
#undef  strsubfontwidth
#define strsubfontwidth			pm_strsubfontwidth
#undef  loadchar
#define loadchar			pm_loadchar
#undef  subfontname
#define subfontname			pm_subfontname
#undef  _getsubfont
#define _getsubfont			pm__getsubfont
#undef  getdefont
#define getdefont			pm_getdefont
#undef  lockdisplay
#define lockdisplay			pm_lockdisplay
#undef  unlockdisplay
#define unlockdisplay			pm_unlockdisplay
#undef  drawlsetrefresh
#define drawlsetrefresh			pm_drawlsetrefresh
#undef  display
#define display			pm_display
#undef  font
#define font			pm_font
#undef  screen
#define screen			pm_screen
#undef  _screen
#define _screen			pm__screen
#undef  _cursorfd
#define _cursorfd			pm__cursorfd
#undef  _drawdebug
#define _drawdebug			pm__drawdebug
#undef  _drawsetdebug
#define _drawsetdebug			pm__drawsetdebug

#endif
