blob: 3f6c483ef8fd252c956ff822c21f8884449b6790 [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH MEMDRAW 3
2.SH NAME
3Memimage,
4Memdata,
5Memdrawparam,
6memimageinit,
7wordaddr,
8byteaddr,
9memimagemove,
10allocmemimage,
11allocmemimaged,
12readmemimage,
13creadmemimage,
14writememimage,
15freememimage,
16memsetchan,
17loadmemimage,
18cloadmemimage,
19unloadmemimage,
20memfillcolor,
21memarc,
22mempoly,
23memellipse,
24memfillpoly,
25memimageline,
26memimagedraw,
27drawclip,
28memlinebbox,
29memlineendsize,
30allocmemsubfont,
31openmemsubfont,
32freememsubfont,
33memsubfontwidth,
34getmemdefont,
35memimagestring,
36iprint,
37hwdraw \- drawing routines for memory-resident images
38.SH SYNOPSIS
39.nf
40.B #include <u.h>
41.nf
42.B #include <u.h>
43.B #include <libc.h>
44.B #include <draw.h>
45.B #include <memdraw.h>
46.PP
47.ft L
48typedef struct Memdata
49{
50 ulong *base; /* allocated data pointer */
51 uchar *bdata; /* first byte of actual data; word-aligned */
52 int ref; /* number of Memimages using this data */
53 void* imref; /* last image that pointed at this */
54 int allocd; /* is this malloc'd? */
55} Memdata;
56
57enum {
58 Frepl = 1<<0, /* is replicated */
59 Fsimple = 1<<1, /* is 1x1 */
60 Fgrey = 1<<2, /* is grey */
61 Falpha = 1<<3, /* has explicit alpha */
62 Fcmap = 1<<4, /* has cmap channel */
63 Fbytes = 1<<5, /* has only 8-bit channels */
64};
65
66typedef struct Memimage
67{
68 Rectangle r; /* rectangle in data area, local coords */
69 Rectangle clipr; /* clipping region */
70 int depth; /* number of bits of storage per pixel */
71 int nchan; /* number of channels */
72 ulong chan; /* channel descriptions */
73
74 Memdata *data; /* pointer to data */
75 int zero; /* data->bdata+zero==&byte containing (0,0) */
76 ulong width; /* width in words of a single scan line */
77 Memlayer *layer; /* nil if not a layer*/
78 ulong flags;
79 \fI...\fP
80} Memimage;
81
82typedef struct Memdrawparam
83{
84 Memimage *dst;
85 Rectangle r;
86 Memimage *src;
87 Rectangle sr;
88 Memimage *mask;
89 Rectangle mr;
90 \fI...\fP
91} Memdrawparam;
92
93.ta \w'\fLMemsubfont* 'u
94int drawdebug;
95.ft
96.PP
97.ft L
98.nf
99void memimageinit(void)
100ulong* wordaddr(Memimage *i, Point p)
101uchar* byteaddr(Memimage *i, Point p)
102void memimagemove(void *from, void *to)
103.PP
104.ft L
105.nf
106Memimage* allocmemimage(Rectangle r, ulong chan)
107Memimage* allocmemimaged(Rectangle r, ulong chan, Memdata *data)
108Memimage* readmemimage(int fd)
109Memimage* creadmemimage(int fd)
110int writememimage(int fd, Memimage *i)
111void freememimage(Memimage *i)
112int memsetchan(Memimage*, ulong)
113.PP
114.ft L
115.nf
116int loadmemimage(Memimage *i, Rectangle r,
117 uchar *buf, int nbuf)
118int cloadmemimage(Memimage *i, Rectangle r,
119 uchar *buf, int nbuf)
120int unloadmemimage(Memimage *i, Rectangle r,
121 uchar *buf, int nbuf)
122void memfillcolor(Memimage *i, ulong color)
123.PP
124.ft L
125.nf
126void memarc(Memimage *dst, Point c, int a, int b, int thick,
127 Memimage *src, Point sp, int alpha, int phi, Drawop op)
128void mempoly(Memimage *dst, Point *p, int np, int end0,
129 int end1, int radius, Memimage *src, Point sp, Drawop op)
130void memellipse(Memimage *dst, Point c, int a, int b,
131 int thick, Memimage *src, Point sp, Drawop op)
132void memfillpoly(Memimage *dst, Point *p, int np, int wind,
133 Memimage *src, Point sp, Drawop op)
134void memimageline(Memimage *dst, Point p0, Point p1, int end0,
135 int end1, int radius, Memimage *src, Point sp, Drawop op)
136void memimagedraw(Memimage *dst, Rectangle r, Memimage *src,
137 Point sp, Memimage *mask, Point mp, Drawop op)
138.PP
139.ft L
140.nf
141int drawclip(Memimage *dst, Rectangle *dr, Memimage *src,
142 Point *sp, Memimage *mask, Point *mp,
143 Rectangle *sr, Rectangle *mr)
144Rectangle memlinebbox(Point p0, Point p1, int end0, int end1,
145 int radius)
146int memlineendsize(int end)
147.PP
148.ft L
149.nf
150Memsubfont* allocmemsubfont(char *name, int n, int height,
151 int ascent, Fontchar *info, Memimage *i)
152Memsubfont* openmemsubfont(char *name)
153void freememsubfont(Memsubfont *f)
154Point memsubfontwidth(Memsubfont *f, char *s)
155Memsubfont* getmemdefont(void)
156Point memimagestring(Memimage *dst, Point p, Memimage *color,
157 Point cp, Memsubfont *f, char *cs, Drawop op)
158.PP
159.ft L
160.nf
161int iprint(char *fmt, ...)
162int hwdraw(Memdrawparam *param)
163.ft R
164.SH DESCRIPTION
165The
166.B Memimage
167type defines memory-resident rectangular pictures and the methods to draw upon them;
168.BR Memimage s
169differ from
170.BR Image s
171(see
rscbf8a59f2004-04-11 03:42:27 +0000172.IR draw (3))
rsccfa37a72004-04-10 18:53:55 +0000173in that they are manipulated directly in user memory rather than by
174RPCs to the
175.B /dev/draw
176hierarchy.
177The
178.Bmemdraw
179library is the basis for the kernel
180.IR draw (3)
181driver and also used by a number of programs that must manipulate
182images without a display.
183.PP
184The
185.BR r,
186.BR clipr ,
187.BR depth ,
188.BR nchan ,
189and
190.BR chan
191structure elements are identical to
192the ones of the same name
193in the
194.B Image
195structure.
196.PP
197The
198.B flags
199element of the
200.B Memimage
201structure holds a number of bits of information about the image.
202In particular, it subsumes the
203purpose of the
204.B repl
205element of
206.B Image
207structures.
208.PP
209.I Memimageinit
210initializes various static data that the library depends on,
211as well as the replicated solid color images
212.BR memopaque ,
213.BR memtransparent ,
214.BR memblack ,
215and
216.BR memwhite .
217It should be called before referring to any of these images
218and before calling any of the other library functions.
219.PP
220Each
221.B Memimage
222points at a
223.B Memdata
224structure that in turn points at the actual pixel data for the image.
225This allows multiple images to be associated with the same
226.BR Memdata .
227The first word of the data pointed at by
228the
229.B base
230element of
231.B Memdata
232points back at the
233.B Memdata
rsc058b0112005-01-03 06:40:20 +0000234structure, so that in the Plan 9 kernel, the
rsccfa37a72004-04-10 18:53:55 +0000235memory allocator (see
rsc058b0112005-01-03 06:40:20 +0000236Plan 9's \fIpool\fR(3))
rsccfa37a72004-04-10 18:53:55 +0000237can compact image memory
238using
239.IR memimagemove .
240.PP
241Because images can have different coordinate systems,
242the
243.B zero
244element of the
245.B Memimage
246structure contains the offset that must be added
247to the
248.B bdata
249element of the corresponding
250.B Memdata
251structure in order to yield a pointer to the data for the pixel (0,0).
252Adding
253.BR width
254machine words
255to this pointer moves it down one scan line.
256The
257.B depth
258element can be used to determine how to move the
259pointer horizontally.
260Note that this method works even for images whose rectangles
261do not include the origin, although one should only dereference
262pointers corresponding to pixels within the image rectangle.
263.I Wordaddr
264and
265.IR byteaddr
266perform these calculations,
267returning pointers to the word and byte, respectively,
268that contain the beginning of the data for a given pixel.
269.PP
270.I Allocmemimage
271allocages
272images with a given rectangle and channel descriptor
273(see
274.B strtochan
275in
rscbf8a59f2004-04-11 03:42:27 +0000276.IR graphics (3)),
rsccfa37a72004-04-10 18:53:55 +0000277creating a fresh
278.B Memdata
279structure and associated storage.
280.I Allocmemimaged
281is similar but uses the supplied
282.I Memdata
283structure rather than a new one.
284The
285.I readmemimage
286function reads an uncompressed bitmap
287from the given file descriptor,
288while
289.I creadmemimage
290reads a compressed bitmap.
291.I Writememimage
292writes a compressed representation of
293.I i
294to file descriptor
295.IR fd .
296For more on bitmap formats, see
rsc058b0112005-01-03 06:40:20 +0000297.IR image (7).
rsccfa37a72004-04-10 18:53:55 +0000298.I Freememimage
299frees images returned by any of these routines.
300The
301.B Memimage
302structure contains some tables that are used
303to store precomputed values depending on the channel descriptor.
304.I Memsetchan
305updates the
306.B chan
307element of the structure as well as these tables,
308returning \-1 if passed a bad channel descriptor.
309.PP
310.I Loadmemimage
311and
312.I cloadmemimage
313replace the pixel data for a given rectangle of an image
314with the given buffer of uncompressed or compressed
315data, respectively.
316When calling
317.IR cloadmemimage ,
318the buffer must contain an
319integral number of
320compressed chunks of data that exactly cover the rectangle.
321.I Unloadmemimage
322retrieves the uncompressed pixel data for a given rectangle of an image.
323All three return the number of bytes consumed on success,
324and \-1 in case of an error.
325.PP
326.I Memfillcolor
327fills an image with the given color, a 32-bit number as
328described in
rscbf8a59f2004-04-11 03:42:27 +0000329.IR color (3).
rsccfa37a72004-04-10 18:53:55 +0000330.PP
331.IR Memarc ,
332.IR mempoly ,
333.IR memellipse ,
334.IR memfillpoly ,
335.IR memimageline ,
336and
337.I memimagedraw
338are identical to the
339.IR arc ,
340.IR poly ,
341.IR ellipse ,
342.IR fillpoly ,
343.IR line ,
344and
345.IR gendraw ,
346routines described in
rscbf8a59f2004-04-11 03:42:27 +0000347.IR draw (3),
rsccfa37a72004-04-10 18:53:55 +0000348except that they operate on
349.BR Memimage s
350rather than
351.BR Image s.
352Similarly,
353.IR allocmemsubfont ,
354.IR openmemsubfont ,
355.IR freememsubfont ,
356.IR memsubfontwidth ,
357.IR getmemdefont ,
358and
359.I memimagestring
360are the
361.B Memimage
362analogues of
363.IR allocsubfont ,
364.IR openfont ,
365.IR freesubfont ,
366.IR strsubfontwidth ,
367.IR getdefont ,
368and
369.B string
370(see
rscbf8a59f2004-04-11 03:42:27 +0000371.IR subfont (3)
rsccfa37a72004-04-10 18:53:55 +0000372and
rscbf8a59f2004-04-11 03:42:27 +0000373.IR graphics (3)),
rsccfa37a72004-04-10 18:53:55 +0000374except that they operate
375only on
376.BR Memsubfont s
377rather than
378.BR Font s.
379.PP
380.I Drawclip
381takes the images involved in a draw operation,
382together with the destination rectangle
383.B dr
384and source
385and mask alignment points
386.B sp
387and
388.BR mp ,
389and
390clips them according to the clipping rectangles of the images involved.
391It also fills in the rectangles
392.B sr
393and
394.B mr
395with rectangles congruent to the returned destination rectangle
396but translated so the upper left corners are the returned
397.B sp
398and
399.BR mp .
400.I Drawclip
401returns zero when the clipped rectangle is empty.
402.I Memlinebbox
403returns a conservative bounding box containing a line between
404two points
405with given end styles
406and radius.
407.I Memlineendsize
408calculates the extra length added to a line by attaching
409an end of a given style.
410.PP
411The
412.I hwdraw
413and
414.I iprint
415functions are no-op stubs that may be overridden by clients
416of the library.
417.I Hwdraw
418is called at each call to
419.I memimagedraw
420with the current request's parameters.
421If it can satisfy the request, it should do so
422and return 1.
423If it cannot satisfy the request, it should return 0.
424This allows (for instance) the kernel to take advantage
425of hardware acceleration.
426.I Iprint
427should format and print its arguments;
428it is given much debugging output when
429the global integer variable
430.B drawdebug
431is non-zero.
432In the kernel,
433.I iprint
434prints to a serial line rather than the screen, for obvious reasons.
435.SH SOURCE
rsc57a22892006-06-25 21:23:39 +0000436.B \*9/src/libmemdraw
rsccfa37a72004-04-10 18:53:55 +0000437.SH SEE ALSO
rscbf8a59f2004-04-11 03:42:27 +0000438.IR addpt (3),
439.IR color (3),
440.IR draw (3),
441.IR graphics (3),
442.IR memlayer (3),
443.IR stringsize (3),
444.IR subfont (3),
rsc058b0112005-01-03 06:40:20 +0000445.IR color (7),
446.IR utf (7)
rsccfa37a72004-04-10 18:53:55 +0000447.SH BUGS
448.I Memimagestring
449is unusual in using a subfont rather than a font,
450and in having no parameter to align the source.