Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <memdraw.h>
6 int drawdebug;
7 static int tablesbuilt;
9 /* perfect approximation to NTSC = .299r+.587g+.114b when 0 ≤ r,g,b < 256 */
10 #define RGB2K(r,g,b) ((156763*(r)+307758*(g)+59769*(b))>>19)
12 /*
13 * for 0 ≤ x ≤ 255*255, (x*0x0101+0x100)>>16 is a perfect approximation.
14 * for 0 ≤ x < (1<<16), x/255 = ((x+1)*0x0101)>>16 is a perfect approximation.
15 * the last one is perfect for all up to 1<<16, avoids a multiply, but requires a rathole.
16 */
17 /* #define DIV255(x) (((x)*257+256)>>16) */
18 #define DIV255(x) ((((x)+1)*257)>>16)
19 /* #define DIV255(x) (tmp=(x)+1, (tmp+(tmp>>8))>>8) */
21 #define MUL(x, y, t) (t = (x)*(y)+128, (t+(t>>8))>>8)
22 #define MASK13 0xFF00FF00
23 #define MASK02 0x00FF00FF
24 #define MUL13(a, x, t) (t = (a)*(((x)&MASK13)>>8)+128, ((t+((t>>8)&MASK02))>>8)&MASK02)
25 #define MUL02(a, x, t) (t = (a)*(((x)&MASK02)>>0)+128, ((t+((t>>8)&MASK02))>>8)&MASK02)
26 #define MUL0123(a, x, s, t) ((MUL13(a, x, s)<<8)|MUL02(a, x, t))
28 #define MUL2(u, v, x, y) (t = (u)*(v)+(x)*(y)+256, (t+(t>>8))>>8)
30 static void mktables(void);
31 typedef int Subdraw(Memdrawparam*);
32 static Subdraw chardraw, alphadraw, memoptdraw;
34 static Memimage* memones;
35 static Memimage* memzeros;
36 Memimage *memwhite;
37 Memimage *memblack;
38 Memimage *memtransparent;
39 Memimage *memopaque;
41 int __ifmt(Fmt*);
43 void
44 memimageinit(void)
45 {
46 static int didinit = 0;
48 if(didinit)
49 return;
51 didinit = 1;
53 mktables();
54 _memmkcmap();
56 fmtinstall('R', Rfmt);
57 fmtinstall('P', Pfmt);
58 fmtinstall('b', __ifmt);
60 memones = allocmemimage(Rect(0,0,1,1), GREY1);
61 memones->flags |= Frepl;
62 memones->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
63 *byteaddr(memones, ZP) = ~0;
65 memzeros = allocmemimage(Rect(0,0,1,1), GREY1);
66 memzeros->flags |= Frepl;
67 memzeros->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
68 *byteaddr(memzeros, ZP) = 0;
70 if(memones == nil || memzeros == nil)
71 assert(0 /*cannot initialize memimage library */); /* RSC BUG */
73 memwhite = memones;
74 memblack = memzeros;
75 memopaque = memones;
76 memtransparent = memzeros;
77 }
79 u32int _imgtorgba(Memimage*, u32int);
80 u32int _rgbatoimg(Memimage*, u32int);
81 u32int _pixelbits(Memimage*, Point);
83 #define DBG if(drawdebug)
84 static Memdrawparam par;
86 Memdrawparam*
87 _memimagedrawsetup(Memimage *dst, Rectangle r, Memimage *src, Point p0, Memimage *mask, Point p1, int op)
88 {
89 if(mask == nil)
90 mask = memopaque;
92 DBG print("memimagedraw %p/%luX %R @ %p %p/%luX %P %p/%luX %P... ", dst, dst->chan, r, dst->data->bdata, src, src->chan, p0, mask, mask->chan, p1);
94 if(drawclip(dst, &r, src, &p0, mask, &p1, &par.sr, &par.mr) == 0){
95 /* if(drawdebug) */
96 /* iprint("empty clipped rectangle\n"); */
97 return nil;
98 }
100 if(op < Clear || op > SoverD){
101 /* if(drawdebug) */
102 /* iprint("op out of range: %d\n", op); */
103 return nil;
106 par.op = op;
107 par.dst = dst;
108 par.r = r;
109 par.src = src;
110 /* par.sr set by drawclip */
111 par.mask = mask;
112 /* par.mr set by drawclip */
114 par.state = 0;
115 if(src->flags&Frepl){
116 par.state |= Replsrc;
117 if(Dx(src->r)==1 && Dy(src->r)==1){
118 par.sval = pixelbits(src, src->r.min);
119 par.state |= Simplesrc;
120 par.srgba = _imgtorgba(src, par.sval);
121 par.sdval = _rgbatoimg(dst, par.srgba);
122 if((par.srgba&0xFF) == 0 && (op&DoutS)){
123 /* if (drawdebug) iprint("fill with transparent source\n"); */
124 return nil; /* no-op successfully handled */
126 if((par.srgba&0xFF) == 0xFF)
127 par.state |= Fullsrc;
131 if(mask->flags & Frepl){
132 par.state |= Replmask;
133 if(Dx(mask->r)==1 && Dy(mask->r)==1){
134 par.mval = pixelbits(mask, mask->r.min);
135 if(par.mval == 0 && (op&DoutS)){
136 /* if(drawdebug) iprint("fill with zero mask\n"); */
137 return nil; /* no-op successfully handled */
139 par.state |= Simplemask;
140 if(par.mval == ~0)
141 par.state |= Fullmask;
142 par.mrgba = _imgtorgba(mask, par.mval);
146 /* if(drawdebug) */
147 /* iprint("dr %R sr %R mr %R...", r, par.sr, par.mr); */
148 DBG print("draw dr %R sr %R mr %R %lux\n", r, par.sr, par.mr, par.state);
150 return &par;
153 void
154 _memimagedraw(Memdrawparam *par)
156 /*
157 * Now that we've clipped the parameters down to be consistent, we
158 * simply try sub-drawing routines in order until we find one that was able
159 * to handle us. If the sub-drawing routine returns zero, it means it was
160 * unable to satisfy the request, so we do not return.
161 */
163 /*
164 * Hardware support. Each video driver provides this function,
165 * which checks to see if there is anything it can help with.
166 * There could be an if around this checking to see if dst is in video memory.
167 */
168 DBG print("test hwdraw\n");
169 if(hwdraw(par)){
170 /*if(drawdebug) iprint("hw handled\n"); */
171 DBG print("hwdraw handled\n");
172 return;
174 /*
175 * Optimizations using memmove and memset.
176 */
177 DBG print("test memoptdraw\n");
178 if(memoptdraw(par)){
179 /*if(drawdebug) iprint("memopt handled\n"); */
180 DBG print("memopt handled\n");
181 return;
184 /*
185 * Character drawing.
186 * Solid source color being painted through a boolean mask onto a high res image.
187 */
188 DBG print("test chardraw\n");
189 if(chardraw(par)){
190 /*if(drawdebug) iprint("chardraw handled\n"); */
191 DBG print("chardraw handled\n");
192 return;
195 /*
196 * General calculation-laden case that does alpha for each pixel.
197 */
198 DBG print("do alphadraw\n");
199 alphadraw(par);
200 /*if(drawdebug) iprint("alphadraw handled\n"); */
201 DBG print("alphadraw handled\n");
203 #undef DBG
205 /*
206 * Clip the destination rectangle further based on the properties of the
207 * source and mask rectangles. Once the destination rectangle is properly
208 * clipped, adjust the source and mask rectangles to be the same size.
209 * Then if source or mask is replicated, move its clipped rectangle
210 * so that its minimum point falls within the repl rectangle.
212 * Return zero if the final rectangle is null.
213 */
214 int
215 drawclip(Memimage *dst, Rectangle *r, Memimage *src, Point *p0, Memimage *mask, Point *p1, Rectangle *sr, Rectangle *mr)
217 Point rmin, delta;
218 int splitcoords;
219 Rectangle omr;
221 if(r->min.x>=r->max.x || r->min.y>=r->max.y)
222 return 0;
223 splitcoords = (p0->x!=p1->x) || (p0->y!=p1->y);
224 /* clip to destination */
225 rmin = r->min;
226 if(!rectclip(r, dst->r) || !rectclip(r, dst->clipr))
227 return 0;
228 /* move mask point */
229 p1->x += r->min.x-rmin.x;
230 p1->y += r->min.y-rmin.y;
231 /* move source point */
232 p0->x += r->min.x-rmin.x;
233 p0->y += r->min.y-rmin.y;
234 /* map destination rectangle into source */
235 sr->min = *p0;
236 sr->max.x = p0->x+Dx(*r);
237 sr->max.y = p0->y+Dy(*r);
238 /* sr is r in source coordinates; clip to source */
239 if(!(src->flags&Frepl) && !rectclip(sr, src->r))
240 return 0;
241 if(!rectclip(sr, src->clipr))
242 return 0;
243 /* compute and clip rectangle in mask */
244 if(splitcoords){
245 /* move mask point with source */
246 p1->x += sr->min.x-p0->x;
247 p1->y += sr->min.y-p0->y;
248 mr->min = *p1;
249 mr->max.x = p1->x+Dx(*sr);
250 mr->max.y = p1->y+Dy(*sr);
251 omr = *mr;
252 /* mr is now rectangle in mask; clip it */
253 if(!(mask->flags&Frepl) && !rectclip(mr, mask->r))
254 return 0;
255 if(!rectclip(mr, mask->clipr))
256 return 0;
257 /* reflect any clips back to source */
258 sr->min.x += mr->min.x-omr.min.x;
259 sr->min.y += mr->min.y-omr.min.y;
260 sr->max.x += mr->max.x-omr.max.x;
261 sr->max.y += mr->max.y-omr.max.y;
262 *p1 = mr->min;
263 }else{
264 if(!(mask->flags&Frepl) && !rectclip(sr, mask->r))
265 return 0;
266 if(!rectclip(sr, mask->clipr))
267 return 0;
268 *p1 = sr->min;
271 /* move source clipping back to destination */
272 delta.x = r->min.x - p0->x;
273 delta.y = r->min.y - p0->y;
274 r->min.x = sr->min.x + delta.x;
275 r->min.y = sr->min.y + delta.y;
276 r->max.x = sr->max.x + delta.x;
277 r->max.y = sr->max.y + delta.y;
279 /* move source rectangle so sr->min is in src->r */
280 if(src->flags&Frepl) {
281 delta.x = drawreplxy(src->r.min.x, src->r.max.x, sr->min.x) - sr->min.x;
282 delta.y = drawreplxy(src->r.min.y, src->r.max.y, sr->min.y) - sr->min.y;
283 sr->min.x += delta.x;
284 sr->min.y += delta.y;
285 sr->max.x += delta.x;
286 sr->max.y += delta.y;
288 *p0 = sr->min;
290 /* move mask point so it is in mask->r */
291 *p1 = drawrepl(mask->r, *p1);
292 mr->min = *p1;
293 mr->max.x = p1->x+Dx(*sr);
294 mr->max.y = p1->y+Dy(*sr);
296 assert(Dx(*sr) == Dx(*mr) && Dx(*mr) == Dx(*r));
297 assert(Dy(*sr) == Dy(*mr) && Dy(*mr) == Dy(*r));
298 assert(ptinrect(*p0, src->r));
299 assert(ptinrect(*p1, mask->r));
300 assert(ptinrect(r->min, dst->r));
302 return 1;
305 /*
306 * Conversion tables.
307 */
308 static uchar replbit[1+8][256]; /* replbit[x][y] is the replication of the x-bit quantity y to 8-bit depth */
309 static uchar conv18[256][8]; /* conv18[x][y] is the yth pixel in the depth-1 pixel x */
310 static uchar conv28[256][4]; /* ... */
311 static uchar conv48[256][2];
313 /*
314 * bitmap of how to replicate n bits to fill 8, for 1 ≤ n ≤ 8.
315 * the X's are where to put the bottom (ones) bit of the n-bit pattern.
316 * only the top 8 bits of the result are actually used.
317 * (the lower 8 bits are needed to get bits in the right place
318 * when n is not a divisor of 8.)
320 * Should check to see if its easier to just refer to replmul than
321 * use the precomputed values in replbit. On PCs it may well
322 * be; on machines with slow multiply instructions it probably isn't.
323 */
324 #define a ((((((((((((((((0
325 #define X *2+1)
326 #define _ *2)
327 static int replmul[1+8] = {
328 0,
329 a X X X X X X X X X X X X X X X X,
330 a _ X _ X _ X _ X _ X _ X _ X _ X,
331 a _ _ X _ _ X _ _ X _ _ X _ _ X _,
332 a _ _ _ X _ _ _ X _ _ _ X _ _ _ X,
333 a _ _ _ _ X _ _ _ _ X _ _ _ _ X _,
334 a _ _ _ _ _ X _ _ _ _ _ X _ _ _ _,
335 a _ _ _ _ _ _ X _ _ _ _ _ _ X _ _,
336 a _ _ _ _ _ _ _ X _ _ _ _ _ _ _ X,
337 };
338 #undef a
339 #undef X
340 #undef _
342 static void
343 mktables(void)
345 int i, j, mask, sh, small;
347 if(tablesbuilt)
348 return;
350 fmtinstall('R', Rfmt);
351 fmtinstall('P', Pfmt);
352 tablesbuilt = 1;
354 /* bit replication up to 8 bits */
355 for(i=0; i<256; i++){
356 for(j=0; j<=8; j++){ /* j <= 8 [sic] */
357 small = i & ((1<<j)-1);
358 replbit[j][i] = (small*replmul[j])>>8;
362 /* bit unpacking up to 8 bits, only powers of 2 */
363 for(i=0; i<256; i++){
364 for(j=0, sh=7, mask=1; j<8; j++, sh--)
365 conv18[i][j] = replbit[1][(i>>sh)&mask];
367 for(j=0, sh=6, mask=3; j<4; j++, sh-=2)
368 conv28[i][j] = replbit[2][(i>>sh)&mask];
370 for(j=0, sh=4, mask=15; j<2; j++, sh-=4)
371 conv48[i][j] = replbit[4][(i>>sh)&mask];
375 static uchar ones = 0xff;
377 /*
378 * General alpha drawing case. Can handle anything.
379 */
380 typedef struct Buffer Buffer;
381 struct Buffer {
382 /* used by most routines */
383 uchar *red;
384 uchar *grn;
385 uchar *blu;
386 uchar *alpha;
387 uchar *grey;
388 u32int *rgba;
389 int delta; /* number of bytes to add to pointer to get next pixel to the right */
391 /* used by boolcalc* for mask data */
392 uchar *m; /* ptr to mask data r.min byte; like p->bytermin */
393 int mskip; /* no. of left bits to skip in *m */
394 uchar *bm; /* ptr to mask data img->r.min byte; like p->bytey0s */
395 int bmskip; /* no. of left bits to skip in *bm */
396 uchar *em; /* ptr to mask data img->r.max.x byte; like p->bytey0e */
397 int emskip; /* no. of right bits to skip in *em */
398 };
400 typedef struct Param Param;
401 typedef Buffer Readfn(Param*, uchar*, int);
402 typedef void Writefn(Param*, uchar*, Buffer);
403 typedef Buffer Calcfn(Buffer, Buffer, Buffer, int, int, int);
405 enum {
406 MAXBCACHE = 16
407 };
409 /* giant rathole to customize functions with */
410 struct Param {
411 Readfn *replcall;
412 Readfn *greymaskcall;
413 Readfn *convreadcall;
414 Writefn *convwritecall;
416 Memimage *img;
417 Rectangle r;
418 int dx; /* of r */
419 int needbuf;
420 int convgrey;
421 int alphaonly;
423 uchar *bytey0s; /* byteaddr(Pt(img->r.min.x, img->r.min.y)) */
424 uchar *bytermin; /* byteaddr(Pt(r.min.x, img->r.min.y)) */
425 uchar *bytey0e; /* byteaddr(Pt(img->r.max.x, img->r.min.y)) */
426 int bwidth;
428 int replcache; /* if set, cache buffers */
429 Buffer bcache[MAXBCACHE];
430 u32int bfilled;
431 uchar *bufbase;
432 int bufoff;
433 int bufdelta;
435 int dir;
437 int convbufoff;
438 uchar *convbuf;
439 Param *convdpar;
440 int convdx;
441 };
443 static uchar *drawbuf;
444 static int ndrawbuf;
445 static int mdrawbuf;
446 static Param spar, mpar, dpar; /* easier on the stacks */
447 static Readfn greymaskread, replread, readptr;
448 static Writefn nullwrite;
449 static Calcfn alphacalc0, alphacalc14, alphacalc2810, alphacalc3679, alphacalc5, alphacalc11, alphacalcS;
450 static Calcfn boolcalc14, boolcalc236789, boolcalc1011;
452 static Readfn* readfn(Memimage*);
453 static Readfn* readalphafn(Memimage*);
454 static Writefn* writefn(Memimage*);
456 static Calcfn* boolcopyfn(Memimage*, Memimage*);
457 static Readfn* convfn(Memimage*, Param*, Memimage*, Param*);
459 static Calcfn *alphacalc[Ncomp] =
461 alphacalc0, /* Clear */
462 alphacalc14, /* DoutS */
463 alphacalc2810, /* SoutD */
464 alphacalc3679, /* DxorS */
465 alphacalc14, /* DinS */
466 alphacalc5, /* D */
467 alphacalc3679, /* DatopS */
468 alphacalc3679, /* DoverS */
469 alphacalc2810, /* SinD */
470 alphacalc3679, /* SatopD */
471 alphacalc2810, /* S */
472 alphacalc11, /* SoverD */
473 };
475 static Calcfn *boolcalc[Ncomp] =
477 alphacalc0, /* Clear */
478 boolcalc14, /* DoutS */
479 boolcalc236789, /* SoutD */
480 boolcalc236789, /* DxorS */
481 boolcalc14, /* DinS */
482 alphacalc5, /* D */
483 boolcalc236789, /* DatopS */
484 boolcalc236789, /* DoverS */
485 boolcalc236789, /* SinD */
486 boolcalc236789, /* SatopD */
487 boolcalc1011, /* S */
488 boolcalc1011, /* SoverD */
489 };
491 static int
492 allocdrawbuf(void)
494 uchar *p;
496 if(ndrawbuf > mdrawbuf){
497 p = realloc(drawbuf, ndrawbuf);
498 if(p == nil){
499 werrstr("memimagedraw out of memory");
500 return -1;
502 drawbuf = p;
503 mdrawbuf = ndrawbuf;
505 return 0;
508 static void
509 getparam(Param *p, Memimage *img, Rectangle r, int convgrey, int needbuf)
511 int nbuf;
513 memset(p, 0, sizeof *p);
515 p->img = img;
516 p->r = r;
517 p->dx = Dx(r);
518 p->needbuf = needbuf;
519 p->convgrey = convgrey;
521 assert(img->r.min.x <= r.min.x && r.min.x < img->r.max.x);
523 p->bytey0s = byteaddr(img, Pt(img->r.min.x, img->r.min.y));
524 p->bytermin = byteaddr(img, Pt(r.min.x, img->r.min.y));
525 p->bytey0e = byteaddr(img, Pt(img->r.max.x, img->r.min.y));
526 p->bwidth = sizeof(u32int)*img->width;
528 assert(p->bytey0s <= p->bytermin && p->bytermin <= p->bytey0e);
530 if(p->r.min.x == p->img->r.min.x)
531 assert(p->bytermin == p->bytey0s);
533 nbuf = 1;
534 if((img->flags&Frepl) && Dy(img->r) <= MAXBCACHE && Dy(img->r) < Dy(r)){
535 p->replcache = 1;
536 nbuf = Dy(img->r);
538 p->bufdelta = 4*p->dx;
539 p->bufoff = ndrawbuf;
540 ndrawbuf += p->bufdelta*nbuf;
543 static void
544 clipy(Memimage *img, int *y)
546 int dy;
548 dy = Dy(img->r);
549 if(*y == dy)
550 *y = 0;
551 else if(*y == -1)
552 *y = dy-1;
553 assert(0 <= *y && *y < dy);
556 static void
557 dumpbuf(char *s, Buffer b, int n)
559 int i;
560 uchar *p;
562 print("%s", s);
563 for(i=0; i<n; i++){
564 print(" ");
565 if(p=b.grey){
566 print(" k%.2uX", *p);
567 b.grey += b.delta;
568 }else{
569 if(p=b.red){
570 print(" r%.2uX", *p);
571 b.red += b.delta;
573 if(p=b.grn){
574 print(" g%.2uX", *p);
575 b.grn += b.delta;
577 if(p=b.blu){
578 print(" b%.2uX", *p);
579 b.blu += b.delta;
582 if((p=b.alpha) != &ones){
583 print(" α%.2uX", *p);
584 b.alpha += b.delta;
587 print("\n");
590 /*
591 * For each scan line, we expand the pixels from source, mask, and destination
592 * into byte-aligned red, green, blue, alpha, and grey channels. If buffering is not
593 * needed and the channels were already byte-aligned (grey8, rgb24, rgba32, rgb32),
594 * the readers need not copy the data: they can simply return pointers to the data.
595 * If the destination image is grey and the source is not, it is converted using the NTSC
596 * formula.
598 * Once we have all the channels, we call either rgbcalc or greycalc, depending on
599 * whether the destination image is color. This is allowed to overwrite the dst buffer (perhaps
600 * the actual data, perhaps a copy) with its result. It should only overwrite the dst buffer
601 * with the same format (i.e. red bytes with red bytes, etc.) A new buffer is returned from
602 * the calculator, and that buffer is passed to a function to write it to the destination.
603 * If the buffer is already pointing at the destination, the writing function is a no-op.
604 */
605 #define DBG if(drawdebug)
606 static int
607 alphadraw(Memdrawparam *par)
609 int isgrey, starty, endy, op;
610 int needbuf, dsty, srcy, masky;
611 int y, dir, dx, dy;
612 Buffer bsrc, bdst, bmask;
613 Readfn *rdsrc, *rdmask, *rddst;
614 Calcfn *calc;
615 Writefn *wrdst;
616 Memimage *src, *mask, *dst;
617 Rectangle r, sr, mr;
619 if(drawdebug)
620 print("alphadraw %R\n", par->r);
621 r = par->r;
622 dx = Dx(r);
623 dy = Dy(r);
625 ndrawbuf = 0;
627 src = par->src;
628 mask = par->mask;
629 dst = par->dst;
630 sr = par->sr;
631 mr = par->mr;
632 op = par->op;
634 isgrey = dst->flags&Fgrey;
636 /*
637 * Buffering when src and dst are the same bitmap is sufficient but not
638 * necessary. There are stronger conditions we could use. We could
639 * check to see if the rectangles intersect, and if simply moving in the
640 * correct y direction can avoid the need to buffer.
641 */
642 needbuf = (src->data == dst->data);
644 getparam(&spar, src, sr, isgrey, needbuf);
645 getparam(&dpar, dst, r, isgrey, needbuf);
646 getparam(&mpar, mask, mr, 0, needbuf);
648 dir = (needbuf && byteaddr(dst, r.min) > byteaddr(src, sr.min)) ? -1 : 1;
649 spar.dir = mpar.dir = dpar.dir = dir;
651 /*
652 * If the mask is purely boolean, we can convert from src to dst format
653 * when we read src, and then just copy it to dst where the mask tells us to.
654 * This requires a boolean (1-bit grey) mask and lack of a source alpha channel.
656 * The computation is accomplished by assigning the function pointers as follows:
657 * rdsrc - read and convert source into dst format in a buffer
658 * rdmask - convert mask to bytes, set pointer to it
659 * rddst - fill with pointer to real dst data, but do no reads
660 * calc - copy src onto dst when mask says to.
661 * wrdst - do nothing
662 * This is slightly sleazy, since things aren't doing exactly what their names say,
663 * but it avoids a fair amount of code duplication to make this a case here
664 * rather than have a separate booldraw.
665 */
666 /*if(drawdebug) iprint("flag %lud mchan %lux=?%x dd %d\n", src->flags&Falpha, mask->chan, GREY1, dst->depth); */
667 if(!(src->flags&Falpha) && mask->chan == GREY1 && dst->depth >= 8 && op == SoverD){
668 /*if(drawdebug) iprint("boolcopy..."); */
669 rdsrc = convfn(dst, &dpar, src, &spar);
670 rddst = readptr;
671 rdmask = readfn(mask);
672 calc = boolcopyfn(dst, mask);
673 wrdst = nullwrite;
674 }else{
675 /* usual alphadraw parameter fetching */
676 rdsrc = readfn(src);
677 rddst = readfn(dst);
678 wrdst = writefn(dst);
679 calc = alphacalc[op];
681 /*
682 * If there is no alpha channel, we'll ask for a grey channel
683 * and pretend it is the alpha.
684 */
685 if(mask->flags&Falpha){
686 rdmask = readalphafn(mask);
687 mpar.alphaonly = 1;
688 }else{
689 mpar.greymaskcall = readfn(mask);
690 mpar.convgrey = 1;
691 rdmask = greymaskread;
693 /*
694 * Should really be above, but then boolcopyfns would have
695 * to deal with bit alignment, and I haven't written that.
697 * This is a common case for things like ellipse drawing.
698 * When there's no alpha involved and the mask is boolean,
699 * we can avoid all the division and multiplication.
700 */
701 if(mask->chan == GREY1 && !(src->flags&Falpha))
702 calc = boolcalc[op];
703 else if(op == SoverD && !(src->flags&Falpha))
704 calc = alphacalcS;
708 /*
709 * If the image has a small enough repl rectangle,
710 * we can just read each line once and cache them.
711 */
712 if(spar.replcache){
713 spar.replcall = rdsrc;
714 rdsrc = replread;
716 if(mpar.replcache){
717 mpar.replcall = rdmask;
718 rdmask = replread;
721 if(allocdrawbuf() < 0)
722 return 0;
724 /*
725 * Before we were saving only offsets from drawbuf in the parameter
726 * structures; now that drawbuf has been grown to accomodate us,
727 * we can fill in the pointers.
728 */
729 spar.bufbase = drawbuf+spar.bufoff;
730 mpar.bufbase = drawbuf+mpar.bufoff;
731 dpar.bufbase = drawbuf+dpar.bufoff;
732 spar.convbuf = drawbuf+spar.convbufoff;
734 if(dir == 1){
735 starty = 0;
736 endy = dy;
737 }else{
738 starty = dy-1;
739 endy = -1;
742 /*
743 * srcy, masky, and dsty are offsets from the top of their
744 * respective Rectangles. they need to be contained within
745 * the rectangles, so clipy can keep them there without division.
746 */
747 srcy = (starty + sr.min.y - src->r.min.y)%Dy(src->r);
748 masky = (starty + mr.min.y - mask->r.min.y)%Dy(mask->r);
749 dsty = starty + r.min.y - dst->r.min.y;
751 assert(0 <= srcy && srcy < Dy(src->r));
752 assert(0 <= masky && masky < Dy(mask->r));
753 assert(0 <= dsty && dsty < Dy(dst->r));
755 if(drawdebug)
756 print("alphadraw: rdsrc=%p rdmask=%p rddst=%p calc=%p wrdst=%p\n",
757 rdsrc, rdmask, rddst, calc, wrdst);
758 for(y=starty; y!=endy; y+=dir, srcy+=dir, masky+=dir, dsty+=dir){
759 clipy(src, &srcy);
760 clipy(dst, &dsty);
761 clipy(mask, &masky);
763 bsrc = rdsrc(&spar, spar.bufbase, srcy);
764 DBG print("[");
765 bmask = rdmask(&mpar, mpar.bufbase, masky);
766 DBG print("]\n");
767 bdst = rddst(&dpar, dpar.bufbase, dsty);
768 DBG dumpbuf("src", bsrc, dx);
769 DBG dumpbuf("mask", bmask, dx);
770 DBG dumpbuf("dst", bdst, dx);
771 bdst = calc(bdst, bsrc, bmask, dx, isgrey, op);
772 DBG dumpbuf("bdst", bdst, dx);
773 wrdst(&dpar, dpar.bytermin+dsty*dpar.bwidth, bdst);
776 return 1;
778 #undef DBG
780 static Buffer
781 alphacalc0(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
783 USED(grey);
784 USED(op);
785 memset(bdst.rgba, 0, dx*bdst.delta);
786 return bdst;
789 static Buffer
790 alphacalc14(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
792 Buffer obdst;
793 int fd, sadelta;
794 int i, sa, ma, q;
795 u32int s, t;
797 obdst = bdst;
798 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
799 q = bsrc.delta == 4 && bdst.delta == 4;
801 for(i=0; i<dx; i++){
802 sa = *bsrc.alpha;
803 ma = *bmask.alpha;
804 fd = MUL(sa, ma, t);
805 if(op == DoutS)
806 fd = 255-fd;
808 if(grey){
809 *bdst.grey = MUL(fd, *bdst.grey, t);
810 bsrc.grey += bsrc.delta;
811 bdst.grey += bdst.delta;
812 }else{
813 if(q){
814 *bdst.rgba = MUL0123(fd, *bdst.rgba, s, t);
815 bsrc.rgba++;
816 bdst.rgba++;
817 bsrc.alpha += sadelta;
818 bmask.alpha += bmask.delta;
819 continue;
821 *bdst.red = MUL(fd, *bdst.red, t);
822 *bdst.grn = MUL(fd, *bdst.grn, t);
823 *bdst.blu = MUL(fd, *bdst.blu, t);
824 bsrc.red += bsrc.delta;
825 bsrc.blu += bsrc.delta;
826 bsrc.grn += bsrc.delta;
827 bdst.red += bdst.delta;
828 bdst.blu += bdst.delta;
829 bdst.grn += bdst.delta;
831 if(bdst.alpha != &ones){
832 *bdst.alpha = MUL(fd, *bdst.alpha, t);
833 bdst.alpha += bdst.delta;
835 bmask.alpha += bmask.delta;
836 bsrc.alpha += sadelta;
838 return obdst;
841 static Buffer
842 alphacalc2810(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
844 Buffer obdst;
845 int fs, sadelta;
846 int i, ma, da, q;
847 u32int s, t;
849 obdst = bdst;
850 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
851 q = bsrc.delta == 4 && bdst.delta == 4;
853 for(i=0; i<dx; i++){
854 ma = *bmask.alpha;
855 da = *bdst.alpha;
856 if(op == SoutD)
857 da = 255-da;
858 fs = ma;
859 if(op != S)
860 fs = MUL(fs, da, t);
862 if(grey){
863 *bdst.grey = MUL(fs, *bsrc.grey, t);
864 bsrc.grey += bsrc.delta;
865 bdst.grey += bdst.delta;
866 }else{
867 if(q){
868 *bdst.rgba = MUL0123(fs, *bsrc.rgba, s, t);
869 bsrc.rgba++;
870 bdst.rgba++;
871 bmask.alpha += bmask.delta;
872 bdst.alpha += bdst.delta;
873 continue;
875 *bdst.red = MUL(fs, *bsrc.red, t);
876 *bdst.grn = MUL(fs, *bsrc.grn, t);
877 *bdst.blu = MUL(fs, *bsrc.blu, t);
878 bsrc.red += bsrc.delta;
879 bsrc.blu += bsrc.delta;
880 bsrc.grn += bsrc.delta;
881 bdst.red += bdst.delta;
882 bdst.blu += bdst.delta;
883 bdst.grn += bdst.delta;
885 if(bdst.alpha != &ones){
886 *bdst.alpha = MUL(fs, *bsrc.alpha, t);
887 bdst.alpha += bdst.delta;
889 bmask.alpha += bmask.delta;
890 bsrc.alpha += sadelta;
892 return obdst;
895 static Buffer
896 alphacalc3679(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
898 Buffer obdst;
899 int fs, fd, sadelta;
900 int i, sa, ma, da, q;
901 u32int s, t, u, v;
903 obdst = bdst;
904 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
905 q = bsrc.delta == 4 && bdst.delta == 4;
907 for(i=0; i<dx; i++){
908 sa = *bsrc.alpha;
909 ma = *bmask.alpha;
910 da = *bdst.alpha;
911 if(op == SatopD)
912 fs = MUL(ma, da, t);
913 else
914 fs = MUL(ma, 255-da, t);
915 if(op == DoverS)
916 fd = 255;
917 else{
918 fd = MUL(sa, ma, t);
919 if(op != DatopS)
920 fd = 255-fd;
923 if(grey){
924 *bdst.grey = MUL(fs, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
925 bsrc.grey += bsrc.delta;
926 bdst.grey += bdst.delta;
927 }else{
928 if(q){
929 *bdst.rgba = MUL0123(fs, *bsrc.rgba, s, t)+MUL0123(fd, *bdst.rgba, u, v);
930 bsrc.rgba++;
931 bdst.rgba++;
932 bsrc.alpha += sadelta;
933 bmask.alpha += bmask.delta;
934 bdst.alpha += bdst.delta;
935 continue;
937 *bdst.red = MUL(fs, *bsrc.red, s)+MUL(fd, *bdst.red, t);
938 *bdst.grn = MUL(fs, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
939 *bdst.blu = MUL(fs, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
940 bsrc.red += bsrc.delta;
941 bsrc.blu += bsrc.delta;
942 bsrc.grn += bsrc.delta;
943 bdst.red += bdst.delta;
944 bdst.blu += bdst.delta;
945 bdst.grn += bdst.delta;
947 if(bdst.alpha != &ones){
948 *bdst.alpha = MUL(fs, sa, s)+MUL(fd, da, t);
949 bdst.alpha += bdst.delta;
951 bmask.alpha += bmask.delta;
952 bsrc.alpha += sadelta;
954 return obdst;
957 static Buffer
958 alphacalc5(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
960 USED(dx);
961 USED(grey);
962 USED(op);
963 return bdst;
966 static Buffer
967 alphacalc11(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
969 Buffer obdst;
970 int fd, sadelta;
971 int i, sa, ma, q;
972 u32int s, t, u, v;
974 USED(op);
975 obdst = bdst;
976 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
977 q = bsrc.delta == 4 && bdst.delta == 4;
979 for(i=0; i<dx; i++){
980 sa = *bsrc.alpha;
981 ma = *bmask.alpha;
982 fd = 255-MUL(sa, ma, t);
984 if(grey){
985 *bdst.grey = MUL(ma, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
986 bsrc.grey += bsrc.delta;
987 bdst.grey += bdst.delta;
988 }else{
989 if(q){
990 *bdst.rgba = MUL0123(ma, *bsrc.rgba, s, t)+MUL0123(fd, *bdst.rgba, u, v);
991 bsrc.rgba++;
992 bdst.rgba++;
993 bsrc.alpha += sadelta;
994 bmask.alpha += bmask.delta;
995 continue;
997 *bdst.red = MUL(ma, *bsrc.red, s)+MUL(fd, *bdst.red, t);
998 *bdst.grn = MUL(ma, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
999 *bdst.blu = MUL(ma, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
1000 bsrc.red += bsrc.delta;
1001 bsrc.blu += bsrc.delta;
1002 bsrc.grn += bsrc.delta;
1003 bdst.red += bdst.delta;
1004 bdst.blu += bdst.delta;
1005 bdst.grn += bdst.delta;
1007 if(bdst.alpha != &ones){
1008 *bdst.alpha = MUL(ma, sa, s)+MUL(fd, *bdst.alpha, t);
1009 bdst.alpha += bdst.delta;
1011 bmask.alpha += bmask.delta;
1012 bsrc.alpha += sadelta;
1014 return obdst;
1018 not used yet
1019 source and mask alpha 1
1020 static Buffer
1021 alphacalcS0(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1023 Buffer obdst;
1024 int i;
1026 USED(op);
1027 obdst = bdst;
1028 if(bsrc.delta == bdst.delta){
1029 memmove(bdst.rgba, bsrc.rgba, dx*bdst.delta);
1030 return obdst;
1032 for(i=0; i<dx; i++){
1033 if(grey){
1034 *bdst.grey = *bsrc.grey;
1035 bsrc.grey += bsrc.delta;
1036 bdst.grey += bdst.delta;
1037 }else{
1038 *bdst.red = *bsrc.red;
1039 *bdst.grn = *bsrc.grn;
1040 *bdst.blu = *bsrc.blu;
1041 bsrc.red += bsrc.delta;
1042 bsrc.blu += bsrc.delta;
1043 bsrc.grn += bsrc.delta;
1044 bdst.red += bdst.delta;
1045 bdst.blu += bdst.delta;
1046 bdst.grn += bdst.delta;
1048 if(bdst.alpha != &ones){
1049 *bdst.alpha = 255;
1050 bdst.alpha += bdst.delta;
1053 return obdst;
1057 /* source alpha 1 */
1058 static Buffer
1059 alphacalcS(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1061 Buffer obdst;
1062 int fd;
1063 int i, ma;
1064 u32int s, t;
1066 USED(op);
1067 obdst = bdst;
1069 for(i=0; i<dx; i++){
1070 ma = *bmask.alpha;
1071 fd = 255-ma;
1073 if(grey){
1074 *bdst.grey = MUL(ma, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
1075 bsrc.grey += bsrc.delta;
1076 bdst.grey += bdst.delta;
1077 }else{
1078 *bdst.red = MUL(ma, *bsrc.red, s)+MUL(fd, *bdst.red, t);
1079 *bdst.grn = MUL(ma, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
1080 *bdst.blu = MUL(ma, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
1081 bsrc.red += bsrc.delta;
1082 bsrc.blu += bsrc.delta;
1083 bsrc.grn += bsrc.delta;
1084 bdst.red += bdst.delta;
1085 bdst.blu += bdst.delta;
1086 bdst.grn += bdst.delta;
1088 if(bdst.alpha != &ones){
1089 *bdst.alpha = ma+MUL(fd, *bdst.alpha, t);
1090 bdst.alpha += bdst.delta;
1092 bmask.alpha += bmask.delta;
1094 return obdst;
1097 static Buffer
1098 boolcalc14(Buffer bdst, Buffer b1, Buffer bmask, int dx, int grey, int op)
1100 Buffer obdst;
1101 int i, ma, zero;
1103 obdst = bdst;
1105 for(i=0; i<dx; i++){
1106 ma = *bmask.alpha;
1107 zero = ma ? op == DoutS : op == DinS;
1109 if(grey){
1110 if(zero)
1111 *bdst.grey = 0;
1112 bdst.grey += bdst.delta;
1113 }else{
1114 if(zero)
1115 *bdst.red = *bdst.grn = *bdst.blu = 0;
1116 bdst.red += bdst.delta;
1117 bdst.blu += bdst.delta;
1118 bdst.grn += bdst.delta;
1120 bmask.alpha += bmask.delta;
1121 if(bdst.alpha != &ones){
1122 if(zero)
1123 *bdst.alpha = 0;
1124 bdst.alpha += bdst.delta;
1127 return obdst;
1130 static Buffer
1131 boolcalc236789(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1133 Buffer obdst;
1134 int fs, fd;
1135 int i, ma, da, zero;
1136 u32int s, t;
1138 obdst = bdst;
1139 zero = !(op&1);
1141 for(i=0; i<dx; i++){
1142 ma = *bmask.alpha;
1143 da = *bdst.alpha;
1144 fs = da;
1145 if(op&2)
1146 fs = 255-da;
1147 fd = 0;
1148 if(op&4)
1149 fd = 255;
1151 if(grey){
1152 if(ma)
1153 *bdst.grey = MUL(fs, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
1154 else if(zero)
1155 *bdst.grey = 0;
1156 bsrc.grey += bsrc.delta;
1157 bdst.grey += bdst.delta;
1158 }else{
1159 if(ma){
1160 *bdst.red = MUL(fs, *bsrc.red, s)+MUL(fd, *bdst.red, t);
1161 *bdst.grn = MUL(fs, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
1162 *bdst.blu = MUL(fs, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
1164 else if(zero)
1165 *bdst.red = *bdst.grn = *bdst.blu = 0;
1166 bsrc.red += bsrc.delta;
1167 bsrc.blu += bsrc.delta;
1168 bsrc.grn += bsrc.delta;
1169 bdst.red += bdst.delta;
1170 bdst.blu += bdst.delta;
1171 bdst.grn += bdst.delta;
1173 bmask.alpha += bmask.delta;
1174 if(bdst.alpha != &ones){
1175 if(ma)
1176 *bdst.alpha = fs+MUL(fd, da, t);
1177 else if(zero)
1178 *bdst.alpha = 0;
1179 bdst.alpha += bdst.delta;
1182 return obdst;
1185 static Buffer
1186 boolcalc1011(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1188 Buffer obdst;
1189 int i, ma, zero;
1191 obdst = bdst;
1192 zero = !(op&1);
1194 for(i=0; i<dx; i++){
1195 ma = *bmask.alpha;
1197 if(grey){
1198 if(ma)
1199 *bdst.grey = *bsrc.grey;
1200 else if(zero)
1201 *bdst.grey = 0;
1202 bsrc.grey += bsrc.delta;
1203 bdst.grey += bdst.delta;
1204 }else{
1205 if(ma){
1206 *bdst.red = *bsrc.red;
1207 *bdst.grn = *bsrc.grn;
1208 *bdst.blu = *bsrc.blu;
1210 else if(zero)
1211 *bdst.red = *bdst.grn = *bdst.blu = 0;
1212 bsrc.red += bsrc.delta;
1213 bsrc.blu += bsrc.delta;
1214 bsrc.grn += bsrc.delta;
1215 bdst.red += bdst.delta;
1216 bdst.blu += bdst.delta;
1217 bdst.grn += bdst.delta;
1219 bmask.alpha += bmask.delta;
1220 if(bdst.alpha != &ones){
1221 if(ma)
1222 *bdst.alpha = 255;
1223 else if(zero)
1224 *bdst.alpha = 0;
1225 bdst.alpha += bdst.delta;
1228 return obdst;
1231 * Replicated cached scan line read. Call the function listed in the Param,
1232 * but cache the result so that for replicated images we only do the work once.
1234 static Buffer
1235 replread(Param *p, uchar *s, int y)
1237 Buffer *b;
1239 USED(s);
1240 b = &p->bcache[y];
1241 if((p->bfilled & (1<<y)) == 0){
1242 p->bfilled |= 1<<y;
1243 *b = p->replcall(p, p->bufbase+y*p->bufdelta, y);
1245 return *b;
1249 * Alpha reading function that simply relabels the grey pointer.
1251 static Buffer
1252 greymaskread(Param *p, uchar *buf, int y)
1254 Buffer b;
1256 b = p->greymaskcall(p, buf, y);
1257 b.alpha = b.grey;
1258 return b;
1261 #define DBG if(0)
1262 static Buffer
1263 readnbit(Param *p, uchar *buf, int y)
1265 Buffer b;
1266 Memimage *img;
1267 uchar *repl, *r, *w, *ow, bits;
1268 int i, n, sh, depth, x, dx, npack, nbits;
1270 b.rgba = (u32int*)buf;
1271 b.grey = w = buf;
1272 b.red = b.blu = b.grn = w;
1273 b.alpha = &ones;
1274 b.delta = 1;
1276 dx = p->dx;
1277 img = p->img;
1278 depth = img->depth;
1279 repl = &replbit[depth][0];
1280 npack = 8/depth;
1281 sh = 8-depth;
1283 /* copy from p->r.min.x until end of repl rectangle */
1284 x = p->r.min.x;
1285 n = dx;
1286 if(n > p->img->r.max.x - x)
1287 n = p->img->r.max.x - x;
1289 r = p->bytermin + y*p->bwidth;
1290 DBG print("readnbit dx %d %p=%p+%d*%d, *r=%d fetch %d ", dx, r, p->bytermin, y, p->bwidth, *r, n);
1291 bits = *r++;
1292 nbits = 8;
1293 if(i=x&(npack-1)){
1294 DBG print("throwaway %d...", i);
1295 bits <<= depth*i;
1296 nbits -= depth*i;
1298 for(i=0; i<n; i++){
1299 if(nbits == 0){
1300 DBG print("(%.2ux)...", *r);
1301 bits = *r++;
1302 nbits = 8;
1304 *w++ = repl[bits>>sh];
1305 DBG print("bit %x...", repl[bits>>sh]);
1306 bits <<= depth;
1307 nbits -= depth;
1309 dx -= n;
1310 if(dx == 0)
1311 return b;
1313 assert(x+i == p->img->r.max.x);
1315 /* copy from beginning of repl rectangle until where we were before. */
1316 x = p->img->r.min.x;
1317 n = dx;
1318 if(n > p->r.min.x - x)
1319 n = p->r.min.x - x;
1321 r = p->bytey0s + y*p->bwidth;
1322 DBG print("x=%d r=%p...", x, r);
1323 bits = *r++;
1324 nbits = 8;
1325 if(i=x&(npack-1)){
1326 bits <<= depth*i;
1327 nbits -= depth*i;
1329 DBG print("nbits=%d...", nbits);
1330 for(i=0; i<n; i++){
1331 if(nbits == 0){
1332 bits = *r++;
1333 nbits = 8;
1335 *w++ = repl[bits>>sh];
1336 DBG print("bit %x...", repl[bits>>sh]);
1337 bits <<= depth;
1338 nbits -= depth;
1339 DBG print("bits %x nbits %d...", bits, nbits);
1341 dx -= n;
1342 if(dx == 0)
1343 return b;
1345 assert(dx > 0);
1346 /* now we have exactly one full scan line: just replicate the buffer itself until we are done */
1347 ow = buf;
1348 while(dx--)
1349 *w++ = *ow++;
1351 return b;
1353 #undef DBG
1355 #define DBG if(0)
1356 static void
1357 writenbit(Param *p, uchar *w, Buffer src)
1359 uchar *r;
1360 u32int bits;
1361 int i, sh, depth, npack, nbits, x, ex;
1363 assert(src.grey != nil && src.delta == 1);
1365 x = p->r.min.x;
1366 ex = x+p->dx;
1367 depth = p->img->depth;
1368 npack = 8/depth;
1370 i=x&(npack-1);
1371 bits = i ? (*w >> (8-depth*i)) : 0;
1372 nbits = depth*i;
1373 sh = 8-depth;
1374 r = src.grey;
1376 for(; x<ex; x++){
1377 bits <<= depth;
1378 DBG print(" %x", *r);
1379 bits |= (*r++ >> sh);
1380 nbits += depth;
1381 if(nbits == 8){
1382 *w++ = bits;
1383 nbits = 0;
1387 if(nbits){
1388 sh = 8-nbits;
1389 bits <<= sh;
1390 bits |= *w & ((1<<sh)-1);
1391 *w = bits;
1393 DBG print("\n");
1394 return;
1396 #undef DBG
1398 static Buffer
1399 readcmap(Param *p, uchar *buf, int y)
1401 Buffer b;
1402 int a, convgrey, copyalpha, dx, i, m;
1403 uchar *q, *cmap, *begin, *end, *r, *w;
1405 begin = p->bytey0s + y*p->bwidth;
1406 r = p->bytermin + y*p->bwidth;
1407 end = p->bytey0e + y*p->bwidth;
1408 cmap = p->img->cmap->cmap2rgb;
1409 convgrey = p->convgrey;
1410 copyalpha = (p->img->flags&Falpha) ? 1 : 0;
1412 w = buf;
1413 dx = p->dx;
1414 if(copyalpha){
1415 b.alpha = buf++;
1416 a = p->img->shift[CAlpha]/8;
1417 m = p->img->shift[CMap]/8;
1418 for(i=0; i<dx; i++){
1419 *w++ = r[a];
1420 q = cmap+r[m]*3;
1421 r += 2;
1422 if(r == end)
1423 r = begin;
1424 if(convgrey){
1425 *w++ = RGB2K(q[0], q[1], q[2]);
1426 }else{
1427 *w++ = q[2]; /* blue */
1428 *w++ = q[1]; /* green */
1429 *w++ = q[0]; /* red */
1432 }else{
1433 b.alpha = &ones;
1434 for(i=0; i<dx; i++){
1435 q = cmap+*r++*3;
1436 if(r == end)
1437 r = begin;
1438 if(convgrey){
1439 *w++ = RGB2K(q[0], q[1], q[2]);
1440 }else{
1441 *w++ = q[2]; /* blue */
1442 *w++ = q[1]; /* green */
1443 *w++ = q[0]; /* red */
1448 b.rgba = (u32int*)(buf-copyalpha);
1450 if(convgrey){
1451 b.grey = buf;
1452 b.red = b.blu = b.grn = buf;
1453 b.delta = 1+copyalpha;
1454 }else{
1455 b.blu = buf;
1456 b.grn = buf+1;
1457 b.red = buf+2;
1458 b.grey = nil;
1459 b.delta = 3+copyalpha;
1461 return b;
1464 static void
1465 writecmap(Param *p, uchar *w, Buffer src)
1467 uchar *cmap, *red, *grn, *blu;
1468 int i, dx, delta;
1470 cmap = p->img->cmap->rgb2cmap;
1472 delta = src.delta;
1473 red= src.red;
1474 grn = src.grn;
1475 blu = src.blu;
1477 dx = p->dx;
1478 for(i=0; i<dx; i++, red+=delta, grn+=delta, blu+=delta)
1479 *w++ = cmap[(*red>>4)*256+(*grn>>4)*16+(*blu>>4)];
1482 #define DBG if(drawdebug)
1483 static Buffer
1484 readbyte(Param *p, uchar *buf, int y)
1486 Buffer b;
1487 Memimage *img;
1488 int dx, isgrey, convgrey, alphaonly, copyalpha, i, nb;
1489 uchar *begin, *end, *r, *w, *rrepl, *grepl, *brepl, *arepl, *krepl;
1490 uchar ured, ugrn, ublu;
1491 u32int u;
1493 img = p->img;
1494 begin = p->bytey0s + y*p->bwidth;
1495 r = p->bytermin + y*p->bwidth;
1496 end = p->bytey0e + y*p->bwidth;
1498 w = buf;
1499 dx = p->dx;
1500 nb = img->depth/8;
1502 convgrey = p->convgrey; /* convert rgb to grey */
1503 isgrey = img->flags&Fgrey;
1504 alphaonly = p->alphaonly;
1505 copyalpha = (img->flags&Falpha) ? 1 : 0;
1507 /* if we can, avoid processing everything */
1508 if(!(img->flags&Frepl) && !convgrey && (img->flags&Fbytes)){
1509 memset(&b, 0, sizeof b);
1510 if(p->needbuf){
1511 memmove(buf, r, dx*nb);
1512 r = buf;
1514 b.rgba = (u32int*)r;
1515 if(copyalpha)
1516 b.alpha = r+img->shift[CAlpha]/8;
1517 else
1518 b.alpha = &ones;
1519 if(isgrey){
1520 b.grey = r+img->shift[CGrey]/8;
1521 b.red = b.grn = b.blu = b.grey;
1522 }else{
1523 b.red = r+img->shift[CRed]/8;
1524 b.grn = r+img->shift[CGreen]/8;
1525 b.blu = r+img->shift[CBlue]/8;
1527 b.delta = nb;
1528 return b;
1531 rrepl = replbit[img->nbits[CRed]];
1532 grepl = replbit[img->nbits[CGreen]];
1533 brepl = replbit[img->nbits[CBlue]];
1534 arepl = replbit[img->nbits[CAlpha]];
1535 krepl = replbit[img->nbits[CGrey]];
1537 for(i=0; i<dx; i++){
1538 u = r[0] | (r[1]<<8) | (r[2]<<16) | (r[3]<<24);
1539 if(copyalpha)
1540 *w++ = arepl[(u>>img->shift[CAlpha]) & img->mask[CAlpha]];
1542 if(isgrey)
1543 *w++ = krepl[(u >> img->shift[CGrey]) & img->mask[CGrey]];
1544 else if(!alphaonly){
1545 ured = rrepl[(u >> img->shift[CRed]) & img->mask[CRed]];
1546 ugrn = grepl[(u >> img->shift[CGreen]) & img->mask[CGreen]];
1547 ublu = brepl[(u >> img->shift[CBlue]) & img->mask[CBlue]];
1548 if(convgrey){
1549 *w++ = RGB2K(ured, ugrn, ublu);
1550 }else{
1551 *w++ = brepl[(u >> img->shift[CBlue]) & img->mask[CBlue]];
1552 *w++ = grepl[(u >> img->shift[CGreen]) & img->mask[CGreen]];
1553 *w++ = rrepl[(u >> img->shift[CRed]) & img->mask[CRed]];
1556 r += nb;
1557 if(r == end)
1558 r = begin;
1561 b.alpha = copyalpha ? buf : &ones;
1562 b.rgba = (u32int*)buf;
1563 if(alphaonly){
1564 b.red = b.grn = b.blu = b.grey = nil;
1565 if(!copyalpha)
1566 b.rgba = nil;
1567 b.delta = 1;
1568 }else if(isgrey || convgrey){
1569 b.grey = buf+copyalpha;
1570 b.red = b.grn = b.blu = buf+copyalpha;
1571 b.delta = copyalpha+1;
1572 }else{
1573 b.blu = buf+copyalpha;
1574 b.grn = buf+copyalpha+1;
1575 b.grey = nil;
1576 b.red = buf+copyalpha+2;
1577 b.delta = copyalpha+3;
1579 return b;
1581 #undef DBG
1583 #define DBG if(drawdebug)
1584 static void
1585 writebyte(Param *p, uchar *w, Buffer src)
1587 Memimage *img;
1588 int i, isalpha, isgrey, nb, delta, dx, adelta;
1589 uchar ff, *red, *grn, *blu, *grey, *alpha;
1590 u32int u, mask;
1592 img = p->img;
1594 red = src.red;
1595 grn = src.grn;
1596 blu = src.blu;
1597 alpha = src.alpha;
1598 delta = src.delta;
1599 grey = src.grey;
1600 dx = p->dx;
1602 nb = img->depth/8;
1603 mask = (nb==4) ? 0 : ~((1<<img->depth)-1);
1605 isalpha = img->flags&Falpha;
1606 isgrey = img->flags&Fgrey;
1607 adelta = src.delta;
1609 if(isalpha && (alpha == nil || alpha == &ones)){
1610 ff = 0xFF;
1611 alpha = &ff;
1612 adelta = 0;
1615 for(i=0; i<dx; i++){
1616 u = w[0] | (w[1]<<8) | (w[2]<<16) | (w[3]<<24);
1617 DBG print("u %.8lux...", u);
1618 u &= mask;
1619 DBG print("&mask %.8lux...", u);
1620 if(isgrey){
1621 u |= ((*grey >> (8-img->nbits[CGrey])) & img->mask[CGrey]) << img->shift[CGrey];
1622 DBG print("|grey %.8lux...", u);
1623 grey += delta;
1624 }else{
1625 u |= ((*red >> (8-img->nbits[CRed])) & img->mask[CRed]) << img->shift[CRed];
1626 u |= ((*grn >> (8-img->nbits[CGreen])) & img->mask[CGreen]) << img->shift[CGreen];
1627 u |= ((*blu >> (8-img->nbits[CBlue])) & img->mask[CBlue]) << img->shift[CBlue];
1628 red += delta;
1629 grn += delta;
1630 blu += delta;
1631 DBG print("|rgb %.8lux...", u);
1634 if(isalpha){
1635 u |= ((*alpha >> (8-img->nbits[CAlpha])) & img->mask[CAlpha]) << img->shift[CAlpha];
1636 alpha += adelta;
1637 DBG print("|alpha %.8lux...", u);
1640 w[0] = u;
1641 w[1] = u>>8;
1642 w[2] = u>>16;
1643 w[3] = u>>24;
1644 DBG print("write back %.8lux...", u);
1645 w += nb;
1648 #undef DBG
1650 static Readfn*
1651 readfn(Memimage *img)
1653 if(img->depth < 8)
1654 return readnbit;
1655 if(img->nbits[CMap] == 8)
1656 return readcmap;
1657 return readbyte;
1660 static Readfn*
1661 readalphafn(Memimage *m)
1663 USED(m);
1664 return readbyte;
1667 static Writefn*
1668 writefn(Memimage *img)
1670 if(img->depth < 8)
1671 return writenbit;
1672 if(img->chan == CMAP8)
1673 return writecmap;
1674 return writebyte;
1677 static void
1678 nullwrite(Param *p, uchar *s, Buffer b)
1680 USED(p);
1681 USED(s);
1684 static Buffer
1685 readptr(Param *p, uchar *s, int y)
1687 Buffer b;
1688 uchar *q;
1690 USED(s);
1691 q = p->bytermin + y*p->bwidth;
1692 b.red = q; /* ptr to data */
1693 b.grn = b.blu = b.grey = b.alpha = nil;
1694 b.rgba = (u32int*)q;
1695 b.delta = p->img->depth/8;
1696 return b;
1699 static Buffer
1700 boolmemmove(Buffer bdst, Buffer bsrc, Buffer b1, int dx, int i, int o)
1702 USED(i);
1703 USED(o);
1704 memmove(bdst.red, bsrc.red, dx*bdst.delta);
1705 return bdst;
1708 static Buffer
1709 boolcopy8(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1711 uchar *m, *r, *w, *ew;
1713 USED(i);
1714 USED(o);
1715 m = bmask.grey;
1716 w = bdst.red;
1717 r = bsrc.red;
1718 ew = w+dx;
1719 for(; w < ew; w++,r++)
1720 if(*m++)
1721 *w = *r;
1722 return bdst; /* not used */
1725 static Buffer
1726 boolcopy16(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1728 uchar *m;
1729 ushort *r, *w, *ew;
1731 USED(i);
1732 USED(o);
1733 m = bmask.grey;
1734 w = (ushort*)bdst.red;
1735 r = (ushort*)bsrc.red;
1736 ew = w+dx;
1737 for(; w < ew; w++,r++)
1738 if(*m++)
1739 *w = *r;
1740 return bdst; /* not used */
1743 static Buffer
1744 boolcopy24(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1746 uchar *m;
1747 uchar *r, *w, *ew;
1749 USED(i);
1750 USED(o);
1751 m = bmask.grey;
1752 w = bdst.red;
1753 r = bsrc.red;
1754 ew = w+dx*3;
1755 while(w < ew){
1756 if(*m++){
1757 *w++ = *r++;
1758 *w++ = *r++;
1759 *w++ = *r++;
1760 }else{
1761 w += 3;
1762 r += 3;
1765 return bdst; /* not used */
1768 static Buffer
1769 boolcopy32(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1771 uchar *m;
1772 u32int *r, *w, *ew;
1774 USED(i);
1775 USED(o);
1776 m = bmask.grey;
1777 w = (u32int*)bdst.red;
1778 r = (u32int*)bsrc.red;
1779 ew = w+dx;
1780 for(; w < ew; w++,r++)
1781 if(*m++)
1782 *w = *r;
1783 return bdst; /* not used */
1786 static Buffer
1787 genconv(Param *p, uchar *buf, int y)
1789 Buffer b;
1790 int nb;
1791 uchar *r, *w, *ew;
1793 /* read from source into RGB format in convbuf */
1794 b = p->convreadcall(p, p->convbuf, y);
1796 /* write RGB format into dst format in buf */
1797 p->convwritecall(p->convdpar, buf, b);
1799 if(p->convdx){
1800 nb = p->convdpar->img->depth/8;
1801 r = buf;
1802 w = buf+nb*p->dx;
1803 ew = buf+nb*p->convdx;
1804 while(w<ew)
1805 *w++ = *r++;
1808 b.red = buf;
1809 b.blu = b.grn = b.grey = b.alpha = nil;
1810 b.rgba = (u32int*)buf;
1811 b.delta = 0;
1813 return b;
1816 static Readfn*
1817 convfn(Memimage *dst, Param *dpar, Memimage *src, Param *spar)
1819 if(dst->chan == src->chan && !(src->flags&Frepl)){
1820 /*if(drawdebug) iprint("readptr..."); */
1821 return readptr;
1824 if(dst->chan==CMAP8 && (src->chan==GREY1||src->chan==GREY2||src->chan==GREY4)){
1825 /* cheat because we know the replicated value is exactly the color map entry. */
1826 /*if(drawdebug) iprint("Readnbit..."); */
1827 return readnbit;
1830 spar->convreadcall = readfn(src);
1831 spar->convwritecall = writefn(dst);
1832 spar->convdpar = dpar;
1834 /* allocate a conversion buffer */
1835 spar->convbufoff = ndrawbuf;
1836 ndrawbuf += spar->dx*4;
1838 if(spar->dx > Dx(spar->img->r)){
1839 spar->convdx = spar->dx;
1840 spar->dx = Dx(spar->img->r);
1843 /*if(drawdebug) iprint("genconv..."); */
1844 return genconv;
1848 * Do NOT call this directly. pixelbits is a wrapper
1849 * around this that fetches the bits from the X server
1850 * when necessary.
1852 u32int
1853 _pixelbits(Memimage *i, Point pt)
1855 uchar *p;
1856 u32int val;
1857 int off, bpp, npack;
1859 val = 0;
1860 p = byteaddr(i, pt);
1861 switch(bpp=i->depth){
1862 case 1:
1863 case 2:
1864 case 4:
1865 npack = 8/bpp;
1866 off = pt.x%npack;
1867 val = p[0] >> bpp*(npack-1-off);
1868 val &= (1<<bpp)-1;
1869 break;
1870 case 8:
1871 val = p[0];
1872 break;
1873 case 16:
1874 val = p[0]|(p[1]<<8);
1875 break;
1876 case 24:
1877 val = p[0]|(p[1]<<8)|(p[2]<<16);
1878 break;
1879 case 32:
1880 val = p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24);
1881 break;
1883 while(bpp<32){
1884 val |= val<<bpp;
1885 bpp *= 2;
1887 return val;
1890 static Calcfn*
1891 boolcopyfn(Memimage *img, Memimage *mask)
1893 if(mask->flags&Frepl && Dx(mask->r)==1 && Dy(mask->r)==1 && pixelbits(mask, mask->r.min)==~0)
1894 return boolmemmove;
1896 switch(img->depth){
1897 case 8:
1898 return boolcopy8;
1899 case 16:
1900 return boolcopy16;
1901 case 24:
1902 return boolcopy24;
1903 case 32:
1904 return boolcopy32;
1905 default:
1906 assert(0 /* boolcopyfn */);
1908 return 0;
1912 * Optimized draw for filling and scrolling; uses memset and memmove.
1914 static void
1915 memsets(void *vp, ushort val, int n)
1917 ushort *p, *ep;
1919 p = vp;
1920 ep = p+n;
1921 while(p<ep)
1922 *p++ = val;
1925 static void
1926 memsetl(void *vp, u32int val, int n)
1928 u32int *p, *ep;
1930 p = vp;
1931 ep = p+n;
1932 while(p<ep)
1933 *p++ = val;
1936 static void
1937 memset24(void *vp, u32int val, int n)
1939 uchar *p, *ep;
1940 uchar a,b,c;
1942 p = vp;
1943 ep = p+3*n;
1944 a = val;
1945 b = val>>8;
1946 c = val>>16;
1947 while(p<ep){
1948 *p++ = a;
1949 *p++ = b;
1950 *p++ = c;
1954 u32int
1955 _imgtorgba(Memimage *img, u32int val)
1957 uchar r, g, b, a;
1958 int nb, ov, v;
1959 u32int chan;
1960 uchar *p;
1962 a = 0xFF;
1963 r = g = b = 0xAA; /* garbage */
1964 for(chan=img->chan; chan; chan>>=8){
1965 nb = NBITS(chan);
1966 ov = v = val&((1<<nb)-1);
1967 val >>= nb;
1969 while(nb < 8){
1970 v |= v<<nb;
1971 nb *= 2;
1973 v >>= (nb-8);
1975 switch(TYPE(chan)){
1976 case CRed:
1977 r = v;
1978 break;
1979 case CGreen:
1980 g = v;
1981 break;
1982 case CBlue:
1983 b = v;
1984 break;
1985 case CAlpha:
1986 a = v;
1987 break;
1988 case CGrey:
1989 r = g = b = v;
1990 break;
1991 case CMap:
1992 p = img->cmap->cmap2rgb+3*ov;
1993 r = *p++;
1994 g = *p++;
1995 b = *p;
1996 break;
1999 return (r<<24)|(g<<16)|(b<<8)|a;
2002 u32int
2003 _rgbatoimg(Memimage *img, u32int rgba)
2005 u32int chan;
2006 int d, nb;
2007 u32int v;
2008 uchar *p, r, g, b, a, m;
2010 v = 0;
2011 r = rgba>>24;
2012 g = rgba>>16;
2013 b = rgba>>8;
2014 a = rgba;
2015 d = 0;
2016 for(chan=img->chan; chan; chan>>=8){
2017 nb = NBITS(chan);
2018 switch(TYPE(chan)){
2019 case CRed:
2020 v |= (r>>(8-nb))<<d;
2021 break;
2022 case CGreen:
2023 v |= (g>>(8-nb))<<d;
2024 break;
2025 case CBlue:
2026 v |= (b>>(8-nb))<<d;
2027 break;
2028 case CAlpha:
2029 v |= (a>>(8-nb))<<d;
2030 break;
2031 case CMap:
2032 p = img->cmap->rgb2cmap;
2033 m = p[(r>>4)*256+(g>>4)*16+(b>>4)];
2034 v |= (m>>(8-nb))<<d;
2035 break;
2036 case CGrey:
2037 m = RGB2K(r,g,b);
2038 v |= (m>>(8-nb))<<d;
2039 break;
2041 d += nb;
2043 /* print("rgba2img %.8lux = %.*lux\n", rgba, 2*d/8, v); */
2044 return v;
2047 #define DBG if(0)
2048 static int
2049 memoptdraw(Memdrawparam *par)
2051 int m, y, dy, dx, op;
2052 u32int v;
2053 Memimage *src;
2054 Memimage *dst;
2056 dx = Dx(par->r);
2057 dy = Dy(par->r);
2058 src = par->src;
2059 dst = par->dst;
2060 op = par->op;
2062 DBG print("state %lux mval %lux dd %d\n", par->state, par->mval, dst->depth);
2064 * If we have an opaque mask and source is one opaque pixel we can convert to the
2065 * destination format and just replicate with memset.
2067 m = Simplesrc|Simplemask|Fullmask;
2068 if((par->state&m)==m && (par->srgba&0xFF) == 0xFF && (op ==S || op == SoverD)){
2069 uchar *dp, p[4];
2070 int d, dwid, ppb, np, nb;
2071 uchar lm, rm;
2073 DBG print("memopt, dst %p, dst->data->bdata %p\n", dst, dst->data->bdata);
2074 dwid = dst->width*sizeof(u32int);
2075 dp = byteaddr(dst, par->r.min);
2076 v = par->sdval;
2077 DBG print("sdval %lud, depth %d\n", v, dst->depth);
2078 switch(dst->depth){
2079 case 1:
2080 case 2:
2081 case 4:
2082 for(d=dst->depth; d<8; d*=2)
2083 v |= (v<<d);
2084 ppb = 8/dst->depth; /* pixels per byte */
2085 m = ppb-1;
2086 /* left edge */
2087 np = par->r.min.x&m; /* no. pixels unused on left side of word */
2088 dx -= (ppb-np);
2089 nb = 8 - np * dst->depth; /* no. bits used on right side of word */
2090 lm = (1<<nb)-1;
2091 DBG print("np %d x %d nb %d lm %ux ppb %d m %ux\n", np, par->r.min.x, nb, lm, ppb, m);
2093 /* right edge */
2094 np = par->r.max.x&m; /* no. pixels used on left side of word */
2095 dx -= np;
2096 nb = 8 - np * dst->depth; /* no. bits unused on right side of word */
2097 rm = ~((1<<nb)-1);
2098 DBG print("np %d x %d nb %d rm %ux ppb %d m %ux\n", np, par->r.max.x, nb, rm, ppb, m);
2100 DBG print("dx %d Dx %d\n", dx, Dx(par->r));
2101 /* lm, rm are masks that are 1 where we should touch the bits */
2102 if(dx < 0){ /* just one byte */
2103 lm &= rm;
2104 for(y=0; y<dy; y++, dp+=dwid)
2105 *dp ^= (v ^ *dp) & lm;
2106 }else if(dx == 0){ /* no full bytes */
2107 if(lm)
2108 dwid--;
2110 for(y=0; y<dy; y++, dp+=dwid){
2111 if(lm){
2112 DBG print("dp %p v %lux lm %ux (v ^ *dp) & lm %lux\n", dp, v, lm, (v^*dp)&lm);
2113 *dp ^= (v ^ *dp) & lm;
2114 dp++;
2116 *dp ^= (v ^ *dp) & rm;
2118 }else{ /* full bytes in middle */
2119 dx /= ppb;
2120 if(lm)
2121 dwid--;
2122 dwid -= dx;
2124 for(y=0; y<dy; y++, dp+=dwid){
2125 if(lm){
2126 *dp ^= (v ^ *dp) & lm;
2127 dp++;
2129 memset(dp, v, dx);
2130 dp += dx;
2131 *dp ^= (v ^ *dp) & rm;
2134 return 1;
2135 case 8:
2136 for(y=0; y<dy; y++, dp+=dwid)
2137 memset(dp, v, dx);
2138 return 1;
2139 case 16:
2140 p[0] = v; /* make little endian */
2141 p[1] = v>>8;
2142 v = *(ushort*)p;
2143 DBG print("dp=%p; dx=%d; for(y=0; y<%d; y++, dp+=%d)\nmemsets(dp, v, dx);\n",
2144 dp, dx, dy, dwid);
2145 for(y=0; y<dy; y++, dp+=dwid)
2146 memsets(dp, v, dx);
2147 return 1;
2148 case 24:
2149 for(y=0; y<dy; y++, dp+=dwid)
2150 memset24(dp, v, dx);
2151 return 1;
2152 case 32:
2153 p[0] = v; /* make little endian */
2154 p[1] = v>>8;
2155 p[2] = v>>16;
2156 p[3] = v>>24;
2157 v = *(u32int*)p;
2158 for(y=0; y<dy; y++, dp+=dwid)
2159 memsetl(dp, v, dx);
2160 return 1;
2161 default:
2162 assert(0 /* bad dest depth in memoptdraw */);
2167 * If no source alpha, an opaque mask, we can just copy the
2168 * source onto the destination. If the channels are the same and
2169 * the source is not replicated, memmove suffices.
2171 m = Simplemask|Fullmask;
2172 if((par->state&(m|Replsrc))==m && src->depth >= 8
2173 && src->chan == dst->chan && !(src->flags&Falpha) && (op == S || op == SoverD)){
2174 uchar *sp, *dp;
2175 long swid, dwid, nb;
2176 int dir;
2178 if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min))
2179 dir = -1;
2180 else
2181 dir = 1;
2183 swid = src->width*sizeof(u32int);
2184 dwid = dst->width*sizeof(u32int);
2185 sp = byteaddr(src, par->sr.min);
2186 dp = byteaddr(dst, par->r.min);
2187 if(dir == -1){
2188 sp += (dy-1)*swid;
2189 dp += (dy-1)*dwid;
2190 swid = -swid;
2191 dwid = -dwid;
2193 nb = (dx*src->depth)/8;
2194 for(y=0; y<dy; y++, sp+=swid, dp+=dwid)
2195 memmove(dp, sp, nb);
2196 return 1;
2200 * If we have a 1-bit mask, 1-bit source, and 1-bit destination, and
2201 * they're all bit aligned, we can just use bit operators. This happens
2202 * when we're manipulating boolean masks, e.g. in the arc code.
2204 if((par->state&(Simplemask|Simplesrc|Replmask|Replsrc))==0
2205 && dst->chan==GREY1 && src->chan==GREY1 && par->mask->chan==GREY1
2206 && (par->r.min.x&7)==(par->sr.min.x&7) && (par->r.min.x&7)==(par->mr.min.x&7)){
2207 uchar *sp, *dp, *mp;
2208 uchar lm, rm;
2209 long swid, dwid, mwid;
2210 int i, x, dir;
2212 sp = byteaddr(src, par->sr.min);
2213 dp = byteaddr(dst, par->r.min);
2214 mp = byteaddr(par->mask, par->mr.min);
2215 swid = src->width*sizeof(u32int);
2216 dwid = dst->width*sizeof(u32int);
2217 mwid = par->mask->width*sizeof(u32int);
2219 if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min)){
2220 dir = -1;
2221 }else
2222 dir = 1;
2224 lm = 0xFF>>(par->r.min.x&7);
2225 rm = 0xFF<<(8-(par->r.max.x&7));
2226 dx -= (8-(par->r.min.x&7)) + (par->r.max.x&7);
2228 if(dx < 0){ /* one byte wide */
2229 lm &= rm;
2230 if(dir == -1){
2231 dp += dwid*(dy-1);
2232 sp += swid*(dy-1);
2233 mp += mwid*(dy-1);
2234 dwid = -dwid;
2235 swid = -swid;
2236 mwid = -mwid;
2238 for(y=0; y<dy; y++){
2239 *dp ^= (*dp ^ *sp) & *mp & lm;
2240 dp += dwid;
2241 sp += swid;
2242 mp += mwid;
2244 return 1;
2247 dx /= 8;
2248 if(dir == 1){
2249 i = (lm!=0)+dx+(rm!=0);
2250 mwid -= i;
2251 swid -= i;
2252 dwid -= i;
2253 for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
2254 if(lm){
2255 *dp ^= (*dp ^ *sp++) & *mp++ & lm;
2256 dp++;
2258 for(x=0; x<dx; x++){
2259 *dp ^= (*dp ^ *sp++) & *mp++;
2260 dp++;
2262 if(rm){
2263 *dp ^= (*dp ^ *sp++) & *mp++ & rm;
2264 dp++;
2267 return 1;
2268 }else{
2269 /* dir == -1 */
2270 i = (lm!=0)+dx+(rm!=0);
2271 dp += dwid*(dy-1)+i-1;
2272 sp += swid*(dy-1)+i-1;
2273 mp += mwid*(dy-1)+i-1;
2274 dwid = -dwid+i;
2275 swid = -swid+i;
2276 mwid = -mwid+i;
2277 for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
2278 if(rm){
2279 *dp ^= (*dp ^ *sp--) & *mp-- & rm;
2280 dp--;
2282 for(x=0; x<dx; x++){
2283 *dp ^= (*dp ^ *sp--) & *mp--;
2284 dp--;
2286 if(lm){
2287 *dp ^= (*dp ^ *sp--) & *mp-- & lm;
2288 dp--;
2292 return 1;
2294 return 0;
2296 #undef DBG
2299 * Boolean character drawing.
2300 * Solid opaque color through a 1-bit greyscale mask.
2302 #define DBG if(0)
2303 static int
2304 chardraw(Memdrawparam *par)
2306 u32int bits;
2307 int i, ddepth, dy, dx, x, bx, ex, y, npack, bsh, depth, op;
2308 u32int v, maskwid, dstwid;
2309 uchar *wp, *rp, *q, *wc;
2310 ushort *ws;
2311 u32int *wl;
2312 uchar sp[4];
2313 Rectangle r, mr;
2314 Memimage *mask, *src, *dst;
2316 if(0) if(drawdebug) iprint("chardraw? mf %lux md %d sf %lux dxs %d dys %d dd %d ddat %p sdat %p\n",
2317 par->mask->flags, par->mask->depth, par->src->flags,
2318 Dx(par->src->r), Dy(par->src->r), par->dst->depth, par->dst->data, par->src->data);
2320 mask = par->mask;
2321 src = par->src;
2322 dst = par->dst;
2323 r = par->r;
2324 mr = par->mr;
2325 op = par->op;
2327 if((par->state&(Replsrc|Simplesrc|Fullsrc|Replmask)) != (Replsrc|Simplesrc|Fullsrc)
2328 || mask->depth != 1 || dst->depth<8 || dst->data==src->data
2329 || op != SoverD)
2330 return 0;
2332 /*if(drawdebug) iprint("chardraw..."); */
2334 depth = mask->depth;
2335 maskwid = mask->width*sizeof(u32int);
2336 rp = byteaddr(mask, mr.min);
2337 npack = 8/depth;
2338 bsh = (mr.min.x % npack) * depth;
2340 wp = byteaddr(dst, r.min);
2341 dstwid = dst->width*sizeof(u32int);
2342 DBG print("bsh %d\n", bsh);
2343 dy = Dy(r);
2344 dx = Dx(r);
2346 ddepth = dst->depth;
2349 * for loop counts from bsh to bsh+dx
2351 * we want the bottom bits to be the amount
2352 * to shift the pixels down, so for n≡0 (mod 8) we want
2353 * bottom bits 7. for n≡1, 6, etc.
2354 * the bits come from -n-1.
2357 bx = -bsh-1;
2358 ex = -bsh-1-dx;
2359 SET(bits);
2360 v = par->sdval;
2362 /* make little endian */
2363 sp[0] = v;
2364 sp[1] = v>>8;
2365 sp[2] = v>>16;
2366 sp[3] = v>>24;
2368 /*print("sp %x %x %x %x\n", sp[0], sp[1], sp[2], sp[3]); */
2369 for(y=0; y<dy; y++, rp+=maskwid, wp+=dstwid){
2370 q = rp;
2371 if(bsh)
2372 bits = *q++;
2373 switch(ddepth){
2374 case 8:
2375 /*if(drawdebug) iprint("8loop..."); */
2376 wc = wp;
2377 for(x=bx; x>ex; x--, wc++){
2378 i = x&7;
2379 if(i == 8-1)
2380 bits = *q++;
2381 DBG print("bits %lux sh %d...", bits, i);
2382 if((bits>>i)&1)
2383 *wc = v;
2385 break;
2386 case 16:
2387 ws = (ushort*)wp;
2388 v = *(ushort*)sp;
2389 for(x=bx; x>ex; x--, ws++){
2390 i = x&7;
2391 if(i == 8-1)
2392 bits = *q++;
2393 DBG print("bits %lux sh %d...", bits, i);
2394 if((bits>>i)&1)
2395 *ws = v;
2397 break;
2398 case 24:
2399 wc = wp;
2400 for(x=bx; x>ex; x--, wc+=3){
2401 i = x&7;
2402 if(i == 8-1)
2403 bits = *q++;
2404 DBG print("bits %lux sh %d...", bits, i);
2405 if((bits>>i)&1){
2406 wc[0] = sp[0];
2407 wc[1] = sp[1];
2408 wc[2] = sp[2];
2411 break;
2412 case 32:
2413 wl = (u32int*)wp;
2414 v = *(u32int*)sp;
2415 for(x=bx; x>ex; x--, wl++){
2416 i = x&7;
2417 if(i == 8-1)
2418 bits = *q++;
2419 DBG iprint("bits %lux sh %d...", bits, i);
2420 if((bits>>i)&1)
2421 *wl = v;
2423 break;
2427 DBG print("\n");
2428 return 1;
2430 #undef DBG
2434 * Fill entire byte with replicated (if necessary) copy of source pixel,
2435 * assuming destination ldepth is >= source ldepth.
2437 * This code is just plain wrong for >8bpp.
2439 u32int
2440 membyteval(Memimage *src)
2442 int i, val, bpp;
2443 uchar uc;
2445 unloadmemimage(src, src->r, &uc, 1);
2446 bpp = src->depth;
2447 uc <<= (src->r.min.x&(7/src->depth))*src->depth;
2448 uc &= ~(0xFF>>bpp);
2449 * pixel value is now in high part of byte. repeat throughout byte
2450 val = uc;
2451 for(i=bpp; i<8; i<<=1)
2452 val |= val>>i;
2453 return val;
2458 void
2459 _memfillcolor(Memimage *i, u32int val)
2461 u32int bits;
2462 int d, y;
2463 uchar p[4];
2465 if(val == DNofill)
2466 return;
2468 bits = _rgbatoimg(i, val);
2469 switch(i->depth){
2470 case 24: /* 24-bit images suck */
2471 for(y=i->r.min.y; y<i->r.max.y; y++)
2472 memset24(byteaddr(i, Pt(i->r.min.x, y)), bits, Dx(i->r));
2473 break;
2474 default: /* 1, 2, 4, 8, 16, 32 */
2475 for(d=i->depth; d<32; d*=2)
2476 bits = (bits << d) | bits;
2477 p[0] = bits; /* make little endian */
2478 p[1] = bits>>8;
2479 p[2] = bits>>16;
2480 p[3] = bits>>24;
2481 bits = *(u32int*)p;
2482 memsetl(wordaddr(i, i->r.min), bits, i->width*Dy(i->r));
2483 break;