448 lines · 9.9 KB · Text
RawDownload
.TH MEMDRAW 2.SH NAMEMemimage,Memdata,Memdrawparam,memimageinit,wordaddr,byteaddr,memimagemove,allocmemimage,allocmemimaged,readmemimage,creadmemimage,writememimage,freememimage,memsetchan,loadmemimage,cloadmemimage,unloadmemimage,memfillcolor,memarc,mempoly,memellipse,memfillpoly,memimageline,memimagedraw,drawclip,memlinebbox,memlineendsize,allocmemsubfont,openmemsubfont,freememsubfont,memsubfontwidth,getmemdefont,memimagestring,iprint,hwdraw \- drawing routines for memory-resident images.SH SYNOPSIS.nf.B #include <u.h>.B #include <libc.h>.B #include <draw.h>.B #include <memdraw.h>.PP.ft Ltypedef struct Memdata{	ulong	*base;	/* allocated data pointer */	uchar	*bdata;	/* first byte of actual data; word-aligned */	int		ref;		/* number of Memimages using this data */	void*	imref;	/* last image that pointed at this */	int		allocd;	/* is this malloc'd? */} Memdata; enum {	Frepl	= 1<<0,	/* is replicated */	Fsimple	= 1<<1,	/* is 1x1 */	Fgrey	= 1<<2,	/* is grey */	Falpha	= 1<<3,	/* has explicit alpha */	Fcmap	= 1<<4,	/* has cmap channel */	Fbytes	= 1<<5,	/* has only 8-bit channels */}; typedef struct Memimage{	Rectangle	r;		/* rectangle in data area, local coords */	Rectangle	clipr;	/* clipping region */	int		depth;	/* number of bits of storage per pixel */	int		nchan;	/* number of channels */	ulong	chan;	/* channel descriptions */ 	Memdata	*data;	/* pointer to data */	int		zero;	/* data->bdata+zero==&byte containing (0,0) */	ulong	width;	/* width in words of a single scan line */	Memlayer	*layer;	/* nil if not a layer*/	ulong	flags;	\fI...\fP} Memimage; typedef struct Memdrawparam{	Memimage	*dst;	Rectangle	r;	Memimage	*src;	Rectangle	sr;	Memimage	*mask;	Rectangle	mr;	\fI...\fP} Memdrawparam; .ta \w'\fLMemsubfont* 'uint	drawdebug;.ft.PP.ft L.nfvoid	memimageinit(void)ulong*	wordaddr(Memimage *i, Point p)uchar*	byteaddr(Memimage *i, Point p)void	memimagemove(void *from, void *to).PP.ft L.nfMemimage*	allocmemimage(Rectangle r, ulong chan)Memimage*	allocmemimaged(Rectangle r, ulong chan, Memdata *data)Memimage*	readmemimage(int fd)Memimage*	creadmemimage(int fd)int	writememimage(int fd, Memimage *i)void	freememimage(Memimage *i)int	memsetchan(Memimage*, ulong).PP.ft L.nfint	loadmemimage(Memimage *i, Rectangle r,	   uchar *buf, int nbuf)int	cloadmemimage(Memimage *i, Rectangle r,	   uchar *buf, int nbuf)int	unloadmemimage(Memimage *i, Rectangle r,	   uchar *buf, int nbuf)void	memfillcolor(Memimage *i, ulong color).PP.ft L.nfvoid	memarc(Memimage *dst, Point c, int a, int b, int thick,	   Memimage *src, Point sp, int alpha, int phi, Drawop op)void	mempoly(Memimage *dst, Point *p, int np, int end0,	   int end1, int radius, Memimage *src, Point sp, Drawop op)void	memellipse(Memimage *dst, Point c, int a, int b,	   int thick, Memimage *src, Point sp, Drawop op)void	memfillpoly(Memimage *dst, Point *p, int np, int wind,		   Memimage *src, Point sp, Drawop op)void	memimageline(Memimage *dst, Point p0, Point p1, int end0,	   int end1, int radius, Memimage *src, Point sp, Drawop op)void	memimagedraw(Memimage *dst, Rectangle r, Memimage *src,	   Point sp, Memimage *mask, Point mp, Drawop op).PP.ft L.nfint	drawclip(Memimage *dst, Rectangle *dr, Memimage *src,	   Point *sp, Memimage *mask, Point *mp,	   Rectangle *sr, Rectangle *mr)Rectangle	memlinebbox(Point p0, Point p1, int end0, int end1,	   int radius)int	memlineendsize(int end).PP.ft L.nfMemsubfont*	allocmemsubfont(char *name, int n, int height,	   int ascent, Fontchar *info, Memimage *i)Memsubfont*	openmemsubfont(char *name)void	freememsubfont(Memsubfont *f)Point	memsubfontwidth(Memsubfont *f, char *s)Memsubfont*	getmemdefont(void)Point	memimagestring(Memimage *dst, Point p, Memimage *color,	    Point cp, Memsubfont *f, char *cs).PP.ft L.nfint	iprint(char *fmt, ...)int	hwdraw(Memdrawparam *param).ft R.SH DESCRIPTIONThe.B Memimagetype defines memory-resident rectangular pictures and the methods to draw upon them;.BR Memimage sdiffer from.BR Image s(see.IR draw (2))in that they are manipulated directly in user memory rather than byRPCs to the.B /dev/drawhierarchy.The.B memdrawlibrary is the basis for the kernel .IR draw (3)driver and also used by a number of programs that must manipulateimages without a display..PPThe .BR r, .BR clipr ,.BR depth ,.BR nchan ,and.BR chan structure elements are identical tothe ones of the same namein the .B Imagestructure..PPThe.B flagselement of the .B Memimagestructure holds a number of bits of information about the image.In particular, it subsumes thepurpose of the.B replelement of .B Imagestructures..PP.I Memimageinitinitializes various static data that the library depends on,as well as the replicated solid color images .BR memopaque ,.BR memtransparent ,.BR memblack ,and.BR memwhite .It should be called before referring to any of these imagesand before calling any of the other library functions..PPEach .B Memimagepoints at a .B Memdatastructure that in turn points at the actual pixel data for the image.This allows multiple images to be associated with the same .BR Memdata .The first word of the data pointed at bythe .B baseelement of.B Memdatapoints back at the.B Memdatastructure, so that thememory allocator (see.IR pool (2))can compact image memoryusing.IR memimagemove ..PPBecause images can have different coordinate systems,the .B zeroelement of the .B Memimagestructure contains the offset that must be addedto the .B bdataelement of the corresponding.B Memdatastructure in order to yield a pointer to the data for the pixel (0,0).Adding.BR widthmachine wordsto this pointer moves it down one scan line.The.B depthelement can be used to determine how to move thepointer horizontally.Note that this method works even for images whose rectanglesdo not include the origin, although one should only dereferencepointers corresponding to pixels within the image rectangle..I Wordaddrand.IR byteaddr perform these calculations, returning pointers to the word and byte, respectively,that contain the beginning of the data for a given pixel..PP.I Allocmemimageallocages images with a given rectangle and channel descriptor(see .B strtochanin.IR graphics (2)),creating a fresh.B Memdatastructure and associated storage..I Allocmemimagedis similar but uses the supplied.I Memdatastructure rather than a new one.The.I readmemimagefunction reads an uncompressed bitmap from the given file descriptor,while.I creadmemimagereads a compressed bitmap..I Writememimagewrites a compressed representation of.I ito file descriptor.IR fd .For more on bitmap formats, see.IR image (6)..I Freememimagefrees images returned by any of these routines.The.B Memimagestructure contains some tables that are usedto store precomputed values depending on the channel descriptor..I Memsetchanupdates the.B chanelement of the structure as well as these tables,returning \-1 if passed a bad channel descriptor..PP.I Loadmemimageand.I cloadmemimagereplace the pixel data for a given rectangle of an imagewith the given buffer of uncompressed or compresseddata, respectively.When calling.IR cloadmemimage ,the buffer must contain anintegral number of compressed chunks of data that exactly cover the rectangle..I Unloadmemimageretrieves the uncompressed pixel data for a given rectangle of an image.All three return the number of bytes consumed on success,and \-1 in case of an error..PP.I Memfillcolorfills an image with the given color, a 32-bit number asdescribed in .IR color (2)..PP.IR Memarc ,.IR mempoly ,.IR memellipse ,.IR memfillpoly ,.IR memimageline ,and.I memimagedraware identical to the .IR arc ,.IR poly ,.IR ellipse ,.IR fillpoly ,.IR line ,and.IR gendraw ,routines described in.IR draw (2),except that they operate on.BR Memimage srather than .BR Image s.Similarly, .IR allocmemsubfont ,.IR openmemsubfont ,.IR freememsubfont ,.IR memsubfontwidth ,.IR getmemdefont ,and.I memimagestringare the .B Memimageanalogues of .IR allocsubfont ,.IR openfont ,.IR freesubfont ,.IR strsubfontwidth ,.IR getdefont ,and.B string(see .IR subfont (2)and.IR graphics (2)),except that they operateonly on .BR Memsubfont srather than.BR Font s..PP.I Drawcliptakes the images involved in a draw operation,together with the destination rectangle .B drand sourceand mask alignment points.B spand.BR mp ,andclips them according to the clipping rectangles of the images involved.It also fills in the rectangles.B srand.B mrwith rectangles congruent to the returned destination rectanglebut translated so the upper left corners are the returned.B spand.BR mp ..I Drawclipreturns zero when the clipped rectangle is empty..I Memlinebboxreturns a conservative bounding box containing a line betweentwo pointswith given end stylesand radius..I Memlineendsizecalculates the extra length added to a line by attachingan end of a given style..PPThe.I hwdrawand .I iprintfunctions are no-op stubs that may be overridden by clientsof the library..I Hwdrawis called at each call to.I memimagedrawwith the current request's parameters.If it can satisfy the request, it should do so and return 1.If it cannot satisfy the request, it should return 0.This allows (for instance) the kernel to take advantageof hardware acceleration..I Iprintshould format and print its arguments;it is given much debugging output whenthe global integer variable.B drawdebugis non-zero.In the kernel,.I iprintprints to a serial line rather than the screen, for obvious reasons..SH SOURCE.B /sys/src/libmemdraw.SH SEE ALSO.IR addpt (2),.IR color (2),.IR draw (2),.IR graphics (2),.IR memlayer (2),.IR stringsize (2),.IR subfont (2),.IR color (6),.IR utf (6).SH BUGS.I Memimagestringis unusual in using a subfont rather than a font,and in having no parameter to align the source.