Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <memdraw.h>
6 #define poolalloc(a, b) malloc(b)
7 #define poolfree(a, b) free(b)
9 void
10 memimagemove(void *from, void *to)
11 {
12 Memdata *md;
14 md = *(Memdata**)to;
15 if(md->base != from){
16 print("compacted data not right: #%p\n", md->base);
17 abort();
18 }
19 md->base = to;
21 /* if allocmemimage changes this must change too */
22 md->bdata = (uchar*)((ulong*)md->base+2);
23 }
25 Memimage*
26 allocmemimaged(Rectangle r, u32int chan, Memdata *md, void *X)
27 {
28 int d;
29 u32int l;
30 Memimage *i;
32 if(Dx(r) <= 0 || Dy(r) <= 0){
33 werrstr("bad rectangle %R", r);
34 return nil;
35 }
36 if((d = chantodepth(chan)) == 0) {
37 werrstr("bad channel descriptor %.8lux", chan);
38 return nil;
39 }
41 l = wordsperline(r, d);
43 i = mallocz(sizeof(Memimage), 1);
44 if(i == nil)
45 return nil;
47 i->X = X;
48 i->data = md;
49 i->zero = sizeof(u32int)*l*r.min.y;
51 if(r.min.x >= 0)
52 i->zero += (r.min.x*d)/8;
53 else
54 i->zero -= (-r.min.x*d+7)/8;
55 i->zero = -i->zero;
56 i->width = l;
57 i->r = r;
58 i->clipr = r;
59 i->flags = 0;
60 i->layer = nil;
61 i->cmap = memdefcmap;
62 if(memsetchan(i, chan) < 0){
63 free(i);
64 return nil;
65 }
66 return i;
67 }
69 Memimage*
70 _allocmemimage(Rectangle r, u32int chan)
71 {
72 int d;
73 u32int l, nw;
74 ulong *ul;
75 Memdata *md;
76 Memimage *i;
78 if((d = chantodepth(chan)) == 0) {
79 werrstr("bad channel descriptor %.8lux", chan);
80 return nil;
81 }
83 l = wordsperline(r, d);
84 nw = l*Dy(r);
85 md = malloc(sizeof(Memdata));
86 if(md == nil)
87 return nil;
89 md->ref = 1;
90 /*
91 * The first two ulongs are the md and the callerpc.
92 * Then nw words of data.
93 * The final word lets the drawing routines be a little
94 * sloppy about reading past the end of the block.
95 */
96 md->base = poolalloc(imagmem, 2*sizeof(ulong)+(nw+1)*sizeof(u32int));
97 if(md->base == nil){
98 free(md);
99 return nil;
102 ul = (ulong*)md->base;
103 ul[0] = (ulong)md;
104 ul[1] = getcallerpc(&r);
106 /* if this changes, memimagemove must change too */
107 md->bdata = (uchar*)(ul+2);
109 md->allocd = 1;
111 i = allocmemimaged(r, chan, md, nil);
112 if(i == nil){
113 poolfree(imagmem, md->base);
114 free(md);
115 return nil;
117 md->imref = i;
118 return i;
121 void
122 _freememimage(Memimage *i)
124 if(i == nil)
125 return;
126 if(i->data->ref-- == 1 && i->data->allocd){
127 if(i->data->base)
128 poolfree(imagmem, i->data->base);
129 free(i->data);
131 free(i);
134 /*
135 * Wordaddr is deprecated.
136 */
137 u32int*
138 wordaddr(Memimage *i, Point p)
140 return (u32int*) ((ulong)byteaddr(i, p) & ~(sizeof(u32int)-1));
143 uchar*
144 byteaddr(Memimage *i, Point p)
146 uchar *a;
148 a = i->data->bdata+i->zero+sizeof(u32int)*p.y*i->width;
150 if(i->depth < 8){
151 /*
152 * We need to always round down,
153 * but C rounds toward zero.
154 */
155 int np;
156 np = 8/i->depth;
157 if(p.x < 0)
158 return a+(p.x-np+1)/np;
159 else
160 return a+p.x/np;
162 else
163 return a+p.x*(i->depth/8);
166 int
167 memsetchan(Memimage *i, u32int chan)
169 int d;
170 int t, j, k;
171 u32int cc;
172 int bytes;
174 if((d = chantodepth(chan)) == 0) {
175 werrstr("bad channel descriptor");
176 return -1;
179 i->depth = d;
180 i->chan = chan;
181 i->flags &= ~(Fgrey|Falpha|Fcmap|Fbytes);
182 bytes = 1;
183 for(cc=chan, j=0, k=0; cc; j+=NBITS(cc), cc>>=8, k++){
184 t=TYPE(cc);
185 if(t < 0 || t >= NChan){
186 werrstr("bad channel string");
187 return -1;
189 if(t == CGrey)
190 i->flags |= Fgrey;
191 if(t == CAlpha)
192 i->flags |= Falpha;
193 if(t == CMap && i->cmap == nil){
194 i->cmap = memdefcmap;
195 i->flags |= Fcmap;
198 i->shift[t] = j;
199 i->mask[t] = (1<<NBITS(cc))-1;
200 i->nbits[t] = NBITS(cc);
201 if(NBITS(cc) != 8)
202 bytes = 0;
204 i->nchan = k;
205 if(bytes)
206 i->flags |= Fbytes;
207 return 0;