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*)((ulong*)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)
78 if((d = chantodepth(chan)) == 0) {
79 werrstr("bad channel descriptor %.8lux", chan);
83 l = wordsperline(r, d);
85 md = malloc(sizeof(Memdata));
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.
96 md->base = poolalloc(imagmem, 2*sizeof(ulong)+(nw+1)*sizeof(u32int));
102 ul = (ulong*)md->base;
104 ul[1] = getcallerpc(&r);
106 /* if this changes, memimagemove must change too */
107 md->bdata = (uchar*)(ul+2);
111 i = allocmemimaged(r, chan, md, nil);
113 poolfree(imagmem, md->base);
122 _freememimage(Memimage *i)
126 if(i->data->ref-- == 1 && i->data->allocd){
128 poolfree(imagmem, i->data->base);
135 * Wordaddr is deprecated.
138 wordaddr(Memimage *i, Point p)
140 return (u32int*) ((ulong)byteaddr(i, p) & ~(sizeof(u32int)-1));
144 byteaddr(Memimage *i, Point p)
148 a = i->data->bdata+i->zero+sizeof(u32int)*p.y*i->width;
152 * We need to always round down,
153 * but C rounds toward zero.
158 return a+(p.x-np+1)/np;
163 return a+p.x*(i->depth/8);
167 memsetchan(Memimage *i, u32int chan)
174 if((d = chantodepth(chan)) == 0) {
175 werrstr("bad channel descriptor");
181 i->flags &= ~(Fgrey|Falpha|Fcmap|Fbytes);
183 for(cc=chan, j=0, k=0; cc; j+=NBITS(cc), cc>>=8, k++){
185 if(t < 0 || t >= NChan){
186 werrstr("bad channel string");
193 if(t == CMap && i->cmap == nil){
194 i->cmap = memdefcmap;
199 i->mask[t] = (1<<NBITS(cc))-1;
200 i->nbits[t] = NBITS(cc);