blob: e9da53a51f05f22557499d5af08ef29f9fae7f1c [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH ALLOCIMAGE 3
2.SH NAME
3allocimage, allocimagemix, freeimage, nameimage, namedimage, setalpha, loadimage, cloadimage, unloadimage, readimage, writeimage, bytesperline, wordsperline \- allocating, freeing, reading, writing images
4.SH SYNOPSIS
5.nf
6.PP
7.B
8#include <u.h>
9.B
10#include <libc.h>
11.B
12#include <draw.h>
13.PP
14.ta \w'\fLImage 'u
15.B
16Image *allocimage(Display *d, Rectangle r,
17.br
18.B
19 ulong chan, int repl, int col)
20.PP
21.B
22Image *allocimagemix(Display *d, ulong one, ulong three)
23.PP
24.B
25void freeimage(Image *i)
26.PP
27.B
28int nameimage(Image *i, char *name, int in)
29.PP
30.B
31Image *namedimage(Display *d, char *name)
32.PP
33.B
34ulong setalpha(ulong color, uchar alpha)
35.PP
36.B
37int loadimage(Image *i, Rectangle r, uchar *data, int ndata)
38.PP
39.B
40int cloadimage(Image *i, Rectangle r, uchar *data, int ndata)
41.PP
42.B
43int unloadimage(Image *i, Rectangle r, uchar *data, int ndata)
44.PP
45.B
46Image *readimage(Display *d, int fd, int dolock)
47.PP
48.B
49int writeimage(int fd, Image *i, int dolock)
50.PP
51.B
52int bytesperline(Rectangle r, int d)
53.PP
54.B
55int wordsperline(Rectangle r, int d)
56.PP
57.nf
58.B
59enum
60.nf
61.ft L
62.ta +4n +20
63{
64 DOpaque = 0xFFFFFFFF,
65 DTransparent = 0x00000000,
66 DBlack = 0x000000FF,
67 DWhite = 0xFFFFFFFF,
68 DRed = 0xFF0000FF,
69 DGreen = 0x00FF00FF,
70 DBlue = 0x0000FFFF,
71 DCyan = 0x00FFFFFF,
72 DMagenta = 0xFF00FFFF,
73 DYellow = 0xFFFF00FF,
74 DPaleyellow = 0xFFFFAAFF,
75 DDarkyellow = 0xEEEE9EFF,
76 DDarkgreen = 0x448844FF,
77 DPalegreen = 0xAAFFAAFF,
78 DMedgreen = 0x88CC88FF,
79 DDarkblue = 0x000055FF,
80 DPalebluegreen = 0xAAFFFFFF,
81 DPaleblue = 0x0000BBFF,
82 DBluegreen = 0x008888FF,
83 DGreygreen = 0x55AAAAFF,
84 DPalegreygreen = 0x9EEEEEFF,
85 DYellowgreen = 0x99994CFF,
86 DMedblue = 0x000099FF,
87 DGreyblue = 0x005DBBFF,
88 DPalegreyblue = 0x4993DDFF,
89 DPurpleblue = 0x8888CCFF,
90
91 DNotacolor = 0xFFFFFF00,
92 DNofill = DNotacolor,
93
94};
95.fi
96.SH DESCRIPTION
97A new
98.B Image
99on
100.B Display
101.B d
102is allocated with
103.BR allocimage ;
104it will have the rectangle, pixel channel format,
105and replication flag
106given by its arguments.
107Convenient pixel channels like
108.BR GREY1 ,
109.BR GREY2 ,
110.BR CMAP8 ,
111.BR RGB16 ,
112.BR RGB24 ,
113and
114.BR RGBA32
115are predefined.
116All the new image's pixels will have initial value
117.IR col .
118If
119.I col
120is
121.BR DNofill ,
122no initialization is done.
123Representative useful values of color are predefined:
124.BR DBlack ,
125.BR DWhite ,
126.BR DRed ,
127and so on.
128Colors are specified by 32-bit numbers comprising,
129from most to least significant byte,
1308-bit values for red, green, blue, and alpha.
131The values correspond to illumination, so 0 is black and 255 is white.
132Similarly, for alpha 0 is transparent and 255 is opaque.
133The
134.I id
135field will have been set to the identifying number used by
136.B /dev/draw
137(see
138.IR draw (3)),
139and the
140.I cache
141field will be zero.
142If
143.I repl
144is true, the clip rectangle is set to a very large region; if false, it is set to
145.IR r .
146The
147.I depth
148field will be set to the number of bits per pixel specified
149by the channel descriptor
150(see
rsc058b0112005-01-03 06:40:20 +0000151.IR image (7)).
rsccfa37a72004-04-10 18:53:55 +0000152.I Allocimage
153returns 0 if the server has run out of image memory.
154.PP
155.I Allocimagemix
156is used to allocate background colors.
157On 8-bit color-mapped displays, it
158returns a 2×2 replicated image with one pixel colored
159the color
160.I one
161and the other three with
162.IR three .
163(This simulates a wider range of tones than can be represented by a single pixel
164value on a color-mapped display.)
165On true color displays, it returns a 1×1 replicated image
166whose pixel is the result of mixing the two colors in
167a one to three ratio.
168.PP
169.I Freeimage
170frees the resources used by its argument image.
171.PP
172.I Nameimage
173publishes in the server the image
174.I i
175under the given
176.IR name .
177If
178.I in
179is non-zero, the image is published; otherwise
180.I i
181must be already named
182.I name
183and it is withdrawn from publication.
184.I Namedimage
185returns a reference to the image published under the given
186.I name
187on
188.B Display
189.IR d .
190These routines permit unrelated applications sharing a display to share an image;
191for example they provide the mechanism behind
192.B getwindow
193(see
rscbf8a59f2004-04-11 03:42:27 +0000194.IR graphics (3)).
rsccfa37a72004-04-10 18:53:55 +0000195.PP
196The RGB values in a color are
197.I premultiplied
198by the alpha value; for example, a 50% red is
199.B 0x7F00007F
200not
201.BR 0xFF00007F .
202The function
203.I setalpha
204performs the alpha computation on a given
205.BR color ,
206ignoring its initial alpha value, multiplying the components by the supplied
207.BR alpha .
208For example, to make a 50% red color value, one could execute
209.B setalpha(DRed,
210.BR 0x7F) .
211.PP
212The remaining functions deal with moving groups of pixel
213values between image and user space or external files.
214There is a fixed format for the exchange and storage of
215image data
216(see
rsc058b0112005-01-03 06:40:20 +0000217.IR image (7)).
rsccfa37a72004-04-10 18:53:55 +0000218.PP
219.I Unloadimage
220reads a rectangle of pixels from image
221.I i
222into
223.IR data ,
224whose length is specified by
225.IR ndata .
226It is an error if
227.I ndata
228is too small to accommodate the pixels.
229.PP
230.I Loadimage
231replaces the specified rectangle in image
232.I i
233with the
234.I ndata
235bytes of
236.IR data .
237.PP
238The pixels are presented one horizontal line at a time,
239starting with the top-left pixel of
240.IR r .
241In the data processed by these routines, each scan line starts with a new byte in the array,
242leaving the last byte of the previous line partially empty, if necessary.
243Pixels are packed as tightly as possible within
244.IR data ,
245regardless of the rectangle being extracted.
246Bytes are filled from most to least significant bit order,
247as the
248.I x
249coordinate increases, aligned so
250.IR x =0
251would appear as the leftmost pixel of its byte.
252Thus, for
253.B depth
2541, the pixel at
255.I x
256offset 165 within the rectangle will be in a
257.I data
258byte at bit-position
259.B 0x04
260regardless of the overall
261rectangle: 165 mod 8 equals 5, and
262.B "0x80\ >>\ 5"
263equals
264.BR 0x04 .
265.PP
266.B Cloadimage
267does the same as
268.IR loadimage ,
269but for
270.I ndata
271bytes of compressed image
272.I data
273(see
rsc058b0112005-01-03 06:40:20 +0000274.IR image (7)).
rsccfa37a72004-04-10 18:53:55 +0000275On each call to
276.IR cloadimage,
277the
278.I data
279must be at the beginning of a compressed data block, in particular,
280it should start with the
281.B y
282coordinate and data length for the block.
283.PP
284.IR Loadimage ,
285.IR cloadimage ,
286and
287.I unloadimage
288return the number of bytes copied.
289.PP
290.I Readimage
291creates an image from data contained in an external file (see
rsc058b0112005-01-03 06:40:20 +0000292.IR image (7)
rsccfa37a72004-04-10 18:53:55 +0000293for the file format);
294.I fd
295is a file descriptor obtained by opening such a file for reading.
296The returned image is allocated using
297.IR allocimage .
298The
299.I dolock
300flag specifies whether the
301.B Display
302should be synchronized for multithreaded access; single-threaded
303programs can leave it zero.
304.PP
305.I Writeimage
306writes image
307.I i
308onto file descriptor
309.IR fd ,
310which should be open for writing.
311The format is as described for
312.IR readimage .
313.PP
314.I Readimage
315and
316.I writeimage
317do not close
318.IR fd .
319.PP
320.I Bytesperline
321and
322.I wordsperline
323return the number of bytes or words occupied in memory by one scan line of rectangle
324.I r
325in an image with
326.I d
327bits per pixel.
328.SH EXAMPLE
329To allocate a single-pixel replicated image that may be used to paint a region red,
330.EX
331 red = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, DRed);
332.EE
333.SH SOURCE
rscb5fdffe2004-04-19 19:22:56 +0000334.B /usr/local/plan9/src/libdraw
rsccfa37a72004-04-10 18:53:55 +0000335.SH "SEE ALSO"
rscbf8a59f2004-04-11 03:42:27 +0000336.IR graphics (3),
337.IR draw (3),
rsccfa37a72004-04-10 18:53:55 +0000338.IR draw (3),
rsc058b0112005-01-03 06:40:20 +0000339.IR image (7)
rsccfa37a72004-04-10 18:53:55 +0000340.SH DIAGNOSTICS
341These functions return pointer 0 or integer \-1 on failure, usually due to insufficient
342memory.
343.PP
344May set
345.IR errstr .
346.SH BUGS
347.B Depth
348must be a divisor or multiple of 8.