8 memimagemove(void *from, void *to)
14 print("compacted data not right: #%p\n", md->base);
19 /* if allocmemimage changes this must change too */
20 md->bdata = (uchar*)md->base+sizeof(Memdata*)+sizeof(ulong);
24 allocmemimaged(Rectangle r, ulong chan, Memdata *md)
30 if((d = chantodepth(chan)) == 0) {
31 werrstr("bad channel descriptor %.8lux", chan);
35 werrstr("bad rectangle %R", r);
39 l = wordsperline(r, d);
41 i = mallocz(sizeof(Memimage), 1);
46 i->zero = sizeof(ulong)*l*r.min.y;
49 i->zero += (r.min.x*d)/8;
51 i->zero -= (-r.min.x*d+7)/8;
59 if(memsetchan(i, chan) < 0){
67 allocmemimage(Rectangle r, ulong chan)
75 if((d = chantodepth(chan)) == 0) {
76 werrstr("bad channel descriptor %.8lux", chan);
80 werrstr("bad rectangle %R", r);
84 l = wordsperline(r, d);
87 md = malloc(sizeof(Memdata));
92 md->base = poolalloc(imagmem, sizeof(Memdata*)+(1+nw)*sizeof(ulong));
100 p += sizeof(Memdata*);
102 *(ulong*)p = getcallerpc(&r);
105 /* if this changes, memimagemove must change too */
109 i = allocmemimaged(r, chan, md);
111 poolfree(imagmem, md->base);
120 freememimage(Memimage *i)
124 if(--i->data->ref == 0 && i->data->allocd){
126 poolfree(imagmem, i->data->base);
133 * Wordaddr is deprecated.
136 wordaddr(Memimage *i, Point p)
138 return (ulong*) ((uintptr)byteaddr(i, p) & ~(sizeof(ulong)-1));
142 byteaddr(Memimage *i, Point p)
146 a = i->data->bdata+i->zero+(int)(sizeof(ulong)*p.y*i->width);
149 * We need to always round down,
150 * but C rounds toward zero.
155 return a+(p.x-np+1)/np;
160 return a+p.x*(i->depth/8);
164 memsetchan(Memimage *i, ulong chan)
171 if((d = chantodepth(chan)) == 0) {
172 werrstr("bad channel descriptor");
178 i->flags &= ~(Fgrey|Falpha|Fcmap|Fbytes);
180 for(cc=chan, j=0, k=0; cc; j+=NBITS(cc), cc>>=8, k++){
182 if(t < 0 || t >= NChan){
183 werrstr("bad channel string");
190 if(t == CMap && i->cmap == nil){
191 i->cmap = memdefcmap;
196 i->mask[t] = (1<<NBITS(cc))-1;
197 i->nbits[t] = NBITS(cc);