6 #define poolalloc(a, b) malloc(b)
7 #define poolfree(a, b) free(b)
10 memimagemove(void *from, void *to)
16 print("compacted data not right: #%p\n", md->base);
21 /* if allocmemimage changes this must change too */
22 md->bdata = (uchar*)&md->base[2];
26 allocmemimaged(Rectangle r, u32int chan, Memdata *md, void *X)
32 if(Dx(r) <= 0 || Dy(r) <= 0){
33 werrstr("bad rectangle %R", r);
36 if((d = chantodepth(chan)) == 0) {
37 werrstr("bad channel descriptor %.8lux", chan);
41 l = wordsperline(r, d);
43 i = mallocz(sizeof(Memimage), 1);
49 i->zero = sizeof(u32int)*l*r.min.y;
52 i->zero += (r.min.x*d)/8;
54 i->zero -= (-r.min.x*d+7)/8;
62 if(memsetchan(i, chan) < 0){
70 _allocmemimage(Rectangle r, u32int chan)
77 if((d = chantodepth(chan)) == 0) {
78 werrstr("bad channel descriptor %.8lux", chan);
82 l = wordsperline(r, d);
84 md = malloc(sizeof(Memdata));
90 * The first two words are the md and the callerpc.
91 * Then nw words of data.
92 * The final word lets the drawing routines be a little
93 * sloppy about reading past the end of the block.
95 md->base = poolalloc(imagmem, (2+nw+1)*sizeof(u32int));
101 md->base[0] = (u32int)md;
102 md->base[1] = getcallerpc(&r);
104 /* if this changes, memimagemove must change too */
105 md->bdata = (uchar*)&md->base[2];
109 i = allocmemimaged(r, chan, md, nil);
111 poolfree(imagmem, md->base);
120 _freememimage(Memimage *i)
124 if(i->data->ref-- == 1 && i->data->allocd){
126 poolfree(imagmem, i->data->base);
133 * Wordaddr is deprecated.
136 wordaddr(Memimage *i, Point p)
138 return (u32int*) ((u32int)byteaddr(i, p) & ~(sizeof(u32int)-1));
142 byteaddr(Memimage *i, Point p)
146 a = i->data->bdata+i->zero+sizeof(u32int)*p.y*i->width;
150 * We need to always round down,
151 * but C rounds toward zero.
156 return a+(p.x-np+1)/np;
161 return a+p.x*(i->depth/8);
165 memsetchan(Memimage *i, u32int chan)
172 if((d = chantodepth(chan)) == 0) {
173 werrstr("bad channel descriptor");
179 i->flags &= ~(Fgrey|Falpha|Fcmap|Fbytes);
181 for(cc=chan, j=0, k=0; cc; j+=NBITS(cc), cc>>=8, k++){
183 if(t < 0 || t >= NChan){
184 werrstr("bad channel string");
191 if(t == CMap && i->cmap == nil){
192 i->cmap = memdefcmap;
197 i->mask[t] = (1<<NBITS(cc))-1;
198 i->nbits[t] = NBITS(cc);