Blob


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