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));
89 md->base = poolalloc(imagmem, (2+nw)*sizeof(u32int));
95 md->base[0] = (u32int)md;
96 md->base[1] = getcallerpc(&r);
98 /* if this changes, memimagemove must change too */
99 md->bdata = (uchar*)&md->base[2];
103 i = allocmemimaged(r, chan, md, nil);
105 poolfree(imagmem, md->base);
114 _freememimage(Memimage *i)
118 if(i->data->ref-- == 1 && i->data->allocd){
120 poolfree(imagmem, i->data->base);
127 * Wordaddr is deprecated.
130 wordaddr(Memimage *i, Point p)
132 return (u32int*) ((u32int)byteaddr(i, p) & ~(sizeof(u32int)-1));
136 byteaddr(Memimage *i, Point p)
140 a = i->data->bdata+i->zero+sizeof(u32int)*p.y*i->width;
144 * We need to always round down,
145 * but C rounds toward zero.
150 return a+(p.x-np+1)/np;
155 return a+p.x*(i->depth/8);
159 memsetchan(Memimage *i, u32int chan)
166 if((d = chantodepth(chan)) == 0) {
167 werrstr("bad channel descriptor");
173 i->flags &= ~(Fgrey|Falpha|Fcmap|Fbytes);
175 for(cc=chan, j=0, k=0; cc; j+=NBITS(cc), cc>>=8, k++){
177 if(t < 0 || t >= NChan){
178 werrstr("bad channel string");
185 if(t == CMap && i->cmap == nil){
186 i->cmap = memdefcmap;
191 i->mask[t] = (1<<NBITS(cc))-1;
192 i->nbits[t] = NBITS(cc);