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)
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.
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;
38 Memimage *memtransparent;
46 static int didinit = 0;
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 */
76 memtransparent = memzeros;
79 u32int _imgtorgba(Memimage*, u32int);
80 u32int _rgbatoimg(Memimage*, u32int);
81 u32int _pixelbits(Memimage*, Point);
83 #define DBG if(drawdebug)
84 static Memdrawparam par;
87 _memimagedrawsetup(Memimage *dst, Rectangle r, Memimage *src, Point p0, Memimage *mask, Point p1, int op)
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){
96 /* iprint("empty clipped rectangle\n"); */
100 if(op < Clear || op > SoverD){
102 /* iprint("op out of range: %d\n", op); */
110 /* par.sr set by drawclip */
112 /* par.mr set by drawclip */
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;
141 par.state |= Fullmask;
142 par.mrgba = _imgtorgba(mask, par.mval);
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);
154 _memimagedraw(Memdrawparam *par)
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.
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.
168 DBG print("test hwdraw\n");
170 /*if(drawdebug) iprint("hw handled\n"); */
171 DBG print("hwdraw handled\n");
175 * Optimizations using memmove and memset.
177 DBG print("test memoptdraw\n");
179 /*if(drawdebug) iprint("memopt handled\n"); */
180 DBG print("memopt handled\n");
186 * Solid source color being painted through a boolean mask onto a high res image.
188 DBG print("test chardraw\n");
190 /*if(drawdebug) iprint("chardraw handled\n"); */
191 DBG print("chardraw handled\n");
196 * General calculation-laden case that does alpha for each pixel.
198 DBG print("do alphadraw\n");
200 /*if(drawdebug) iprint("alphadraw handled\n"); */
201 DBG print("alphadraw handled\n");
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.
215 drawclip(Memimage *dst, Rectangle *r, Memimage *src, Point *p0, Memimage *mask, Point *p1, Rectangle *sr, Rectangle *mr)
221 if(r->min.x>=r->max.x || r->min.y>=r->max.y)
223 splitcoords = (p0->x!=p1->x) || (p0->y!=p1->y);
224 /* clip to destination */
226 if(!rectclip(r, dst->r) || !rectclip(r, dst->clipr))
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 */
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))
241 if(!rectclip(sr, src->clipr))
243 /* compute and clip rectangle in mask */
245 /* move mask point with source */
246 p1->x += sr->min.x-p0->x;
247 p1->y += sr->min.y-p0->y;
249 mr->max.x = p1->x+Dx(*sr);
250 mr->max.y = p1->y+Dy(*sr);
252 /* mr is now rectangle in mask; clip it */
253 if(!(mask->flags&Frepl) && !rectclip(mr, mask->r))
255 if(!rectclip(mr, mask->clipr))
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;
264 if(!(mask->flags&Frepl) && !rectclip(sr, mask->r))
266 if(!rectclip(sr, mask->clipr))
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;
290 /* move mask point so it is in mask->r */
291 *p1 = drawrepl(mask->r, *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));
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];
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.
324 #define a ((((((((((((((((0
327 static int replmul[1+8] = {
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,
345 int i, j, mask, sh, small;
350 fmtinstall('R', Rfmt);
351 fmtinstall('P', Pfmt);
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;
378 * General alpha drawing case. Can handle anything.
380 typedef struct Buffer Buffer;
382 /* used by most routines */
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 */
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);
409 /* giant rathole to customize functions with */
412 Readfn *greymaskcall;
413 Readfn *convreadcall;
414 Writefn *convwritecall;
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)) */
428 int replcache; /* if set, cache buffers */
429 Buffer bcache[MAXBCACHE];
443 static uchar *drawbuf;
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 */
467 alphacalc3679, /* DatopS */
468 alphacalc3679, /* DoverS */
469 alphacalc2810, /* SinD */
470 alphacalc3679, /* SatopD */
471 alphacalc2810, /* S */
472 alphacalc11, /* SoverD */
475 static Calcfn *boolcalc[Ncomp] =
477 alphacalc0, /* Clear */
478 boolcalc14, /* DoutS */
479 boolcalc236789, /* SoutD */
480 boolcalc236789, /* DxorS */
481 boolcalc14, /* DinS */
483 boolcalc236789, /* DatopS */
484 boolcalc236789, /* DoverS */
485 boolcalc236789, /* SinD */
486 boolcalc236789, /* SatopD */
487 boolcalc1011, /* S */
488 boolcalc1011, /* SoverD */
496 if(ndrawbuf > mdrawbuf){
497 p = realloc(drawbuf, ndrawbuf);
499 werrstr("memimagedraw out of memory");
509 getparam(Param *p, Memimage *img, Rectangle r, int convgrey, int needbuf)
513 memset(p, 0, sizeof *p);
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);
534 if((img->flags&Frepl) && Dy(img->r) <= MAXBCACHE && Dy(img->r) < Dy(r)){
538 p->bufdelta = 4*p->dx;
539 p->bufoff = ndrawbuf;
540 ndrawbuf += p->bufdelta*nbuf;
544 clipy(Memimage *img, int *y)
553 assert(0 <= *y && *y < dy);
557 dumpbuf(char *s, Buffer b, int n)
566 print(" k%.2uX", *p);
570 print(" r%.2uX", *p);
574 print(" g%.2uX", *p);
578 print(" b%.2uX", *p);
582 if((p=b.alpha) != &ones){
583 print(" α%.2uX", *p);
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
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.
605 #define DBG if(drawdebug)
607 alphadraw(Memdrawparam *par)
609 int isgrey, starty, endy, op;
610 int needbuf, dsty, srcy, masky;
612 Buffer bsrc, bdst, bmask;
613 Readfn *rdsrc, *rdmask, *rddst;
616 Memimage *src, *mask, *dst;
620 print("alphadraw %R\n", par->r);
634 isgrey = dst->flags&Fgrey;
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.
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;
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.
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.
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);
671 rdmask = readfn(mask);
672 calc = boolcopyfn(dst, mask);
675 /* usual alphadraw parameter fetching */
678 wrdst = writefn(dst);
679 calc = alphacalc[op];
682 * If there is no alpha channel, we'll ask for a grey channel
683 * and pretend it is the alpha.
685 if(mask->flags&Falpha){
686 rdmask = readalphafn(mask);
689 mpar.greymaskcall = readfn(mask);
691 rdmask = greymaskread;
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.
701 if(mask->chan == GREY1 && !(src->flags&Falpha))
703 else if(op == SoverD && !(src->flags&Falpha))
709 * If the image has a small enough repl rectangle,
710 * we can just read each line once and cache them.
713 spar.replcall = rdsrc;
717 mpar.replcall = rdmask;
721 if(allocdrawbuf() < 0)
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.
729 spar.bufbase = drawbuf+spar.bufoff;
730 mpar.bufbase = drawbuf+mpar.bufoff;
731 dpar.bufbase = drawbuf+dpar.bufoff;
732 spar.convbuf = drawbuf+spar.convbufoff;
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.
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));
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){
763 bsrc = rdsrc(&spar, spar.bufbase, srcy);
765 bmask = rdmask(&mpar, mpar.bufbase, masky);
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);
781 alphacalc0(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
785 memset(bdst.rgba, 0, dx*bdst.delta);
790 alphacalc14(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
798 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
799 q = bsrc.delta == 4 && bdst.delta == 4;
809 *bdst.grey = MUL(fd, *bdst.grey, t);
810 bsrc.grey += bsrc.delta;
811 bdst.grey += bdst.delta;
814 *bdst.rgba = MUL0123(fd, *bdst.rgba, s, t);
817 bsrc.alpha += sadelta;
818 bmask.alpha += bmask.delta;
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;
842 alphacalc2810(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
850 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
851 q = bsrc.delta == 4 && bdst.delta == 4;
863 *bdst.grey = MUL(fs, *bsrc.grey, t);
864 bsrc.grey += bsrc.delta;
865 bdst.grey += bdst.delta;
868 *bdst.rgba = MUL0123(fs, *bsrc.rgba, s, t);
871 bmask.alpha += bmask.delta;
872 bdst.alpha += bdst.delta;
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;
896 alphacalc3679(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
900 int i, sa, ma, da, q;
904 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
905 q = bsrc.delta == 4 && bdst.delta == 4;
914 fs = MUL(ma, 255-da, t);
924 *bdst.grey = MUL(fs, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
925 bsrc.grey += bsrc.delta;
926 bdst.grey += bdst.delta;
929 *bdst.rgba = MUL0123(fs, *bsrc.rgba, s, t)+MUL0123(fd, *bdst.rgba, u, v);
932 bsrc.alpha += sadelta;
933 bmask.alpha += bmask.delta;
934 bdst.alpha += bdst.delta;
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;
958 alphacalc5(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
967 alphacalc11(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
976 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
977 q = bsrc.delta == 4 && bdst.delta == 4;
982 fd = 255-MUL(sa, ma, t);
985 *bdst.grey = MUL(ma, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
986 bsrc.grey += bsrc.delta;
987 bdst.grey += bdst.delta;
990 *bdst.rgba = MUL0123(ma, *bsrc.rgba, s, t)+MUL0123(fd, *bdst.rgba, u, v);
993 bsrc.alpha += sadelta;
994 bmask.alpha += bmask.delta;
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;
1019 source and mask alpha 1
1021 alphacalcS0(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1028 if(bsrc.delta == bdst.delta){
1029 memmove(bdst.rgba, bsrc.rgba, dx*bdst.delta);
1032 for(i=0; i<dx; i++){
1034 *bdst.grey = *bsrc.grey;
1035 bsrc.grey += bsrc.delta;
1036 bdst.grey += bdst.delta;
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){
1050 bdst.alpha += bdst.delta;
1057 /* source alpha 1 */
1059 alphacalcS(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1069 for(i=0; i<dx; i++){
1074 *bdst.grey = MUL(ma, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
1075 bsrc.grey += bsrc.delta;
1076 bdst.grey += bdst.delta;
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;
1098 boolcalc14(Buffer bdst, Buffer b1, Buffer bmask, int dx, int grey, int op)
1105 for(i=0; i<dx; i++){
1107 zero = ma ? op == DoutS : op == DinS;
1112 bdst.grey += bdst.delta;
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){
1124 bdst.alpha += bdst.delta;
1131 boolcalc236789(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1135 int i, ma, da, zero;
1141 for(i=0; i<dx; i++){
1153 *bdst.grey = MUL(fs, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
1156 bsrc.grey += bsrc.delta;
1157 bdst.grey += bdst.delta;
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);
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){
1176 *bdst.alpha = fs+MUL(fd, da, t);
1179 bdst.alpha += bdst.delta;
1186 boolcalc1011(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1194 for(i=0; i<dx; i++){
1199 *bdst.grey = *bsrc.grey;
1202 bsrc.grey += bsrc.delta;
1203 bdst.grey += bdst.delta;
1206 *bdst.red = *bsrc.red;
1207 *bdst.grn = *bsrc.grn;
1208 *bdst.blu = *bsrc.blu;
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){
1225 bdst.alpha += bdst.delta;
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.
1235 replread(Param *p, uchar *s, int y)
1241 if((p->bfilled & (1<<y)) == 0){
1243 *b = p->replcall(p, p->bufbase+y*p->bufdelta, y);
1249 * Alpha reading function that simply relabels the grey pointer.
1252 greymaskread(Param *p, uchar *buf, int y)
1256 b = p->greymaskcall(p, buf, y);
1263 readnbit(Param *p, uchar *buf, int y)
1267 uchar *repl, *r, *w, *ow, bits;
1268 int i, n, sh, depth, x, dx, npack, nbits;
1270 memset(&b, 0, sizeof b);
1271 b.rgba = (u32int*)buf;
1273 b.red = b.blu = b.grn = w;
1280 repl = &replbit[depth][0];
1284 /* copy from p->r.min.x until end of repl rectangle */
1287 if(n > p->img->r.max.x - x)
1288 n = p->img->r.max.x - x;
1290 r = p->bytermin + y*p->bwidth;
1291 DBG print("readnbit dx %d %p=%p+%d*%d, *r=%d fetch %d ", dx, r, p->bytermin, y, p->bwidth, *r, n);
1295 DBG print("throwaway %d...", i);
1301 DBG print("(%.2ux)...", *r);
1305 *w++ = repl[bits>>sh];
1306 DBG print("bit %x...", repl[bits>>sh]);
1314 assert(x+i == p->img->r.max.x);
1316 /* copy from beginning of repl rectangle until where we were before. */
1317 x = p->img->r.min.x;
1319 if(n > p->r.min.x - x)
1322 r = p->bytey0s + y*p->bwidth;
1323 DBG print("x=%d r=%p...", x, r);
1330 DBG print("nbits=%d...", nbits);
1336 *w++ = repl[bits>>sh];
1337 DBG print("bit %x...", repl[bits>>sh]);
1340 DBG print("bits %x nbits %d...", bits, nbits);
1347 /* now we have exactly one full scan line: just replicate the buffer itself until we are done */
1358 writenbit(Param *p, uchar *w, Buffer src)
1362 int i, sh, depth, npack, nbits, x, ex;
1364 assert(src.grey != nil && src.delta == 1);
1368 depth = p->img->depth;
1372 bits = i ? (*w >> (8-depth*i)) : 0;
1379 DBG print(" %x", *r);
1380 bits |= (*r++ >> sh);
1391 bits |= *w & ((1<<sh)-1);
1400 readcmap(Param *p, uchar *buf, int y)
1403 int a, convgrey, copyalpha, dx, i, m;
1404 uchar *q, *cmap, *begin, *end, *r, *w;
1406 memset(&b, 0, sizeof b);
1407 begin = p->bytey0s + y*p->bwidth;
1408 r = p->bytermin + y*p->bwidth;
1409 end = p->bytey0e + y*p->bwidth;
1410 cmap = p->img->cmap->cmap2rgb;
1411 convgrey = p->convgrey;
1412 copyalpha = (p->img->flags&Falpha) ? 1 : 0;
1418 a = p->img->shift[CAlpha]/8;
1419 m = p->img->shift[CMap]/8;
1420 for(i=0; i<dx; i++){
1427 *w++ = RGB2K(q[0], q[1], q[2]);
1429 *w++ = q[2]; /* blue */
1430 *w++ = q[1]; /* green */
1431 *w++ = q[0]; /* red */
1436 for(i=0; i<dx; i++){
1441 *w++ = RGB2K(q[0], q[1], q[2]);
1443 *w++ = q[2]; /* blue */
1444 *w++ = q[1]; /* green */
1445 *w++ = q[0]; /* red */
1450 b.rgba = (u32int*)(buf-copyalpha);
1454 b.red = b.blu = b.grn = buf;
1455 b.delta = 1+copyalpha;
1461 b.delta = 3+copyalpha;
1467 writecmap(Param *p, uchar *w, Buffer src)
1469 uchar *cmap, *red, *grn, *blu;
1472 cmap = p->img->cmap->rgb2cmap;
1480 for(i=0; i<dx; i++, red+=delta, grn+=delta, blu+=delta)
1481 *w++ = cmap[(*red>>4)*256+(*grn>>4)*16+(*blu>>4)];
1484 #define DBG if(drawdebug)
1486 readbyte(Param *p, uchar *buf, int y)
1490 int dx, isgrey, convgrey, alphaonly, copyalpha, i, nb;
1491 uchar *begin, *end, *r, *w, *rrepl, *grepl, *brepl, *arepl, *krepl;
1492 uchar ured, ugrn, ublu;
1496 begin = p->bytey0s + y*p->bwidth;
1497 r = p->bytermin + y*p->bwidth;
1498 end = p->bytey0e + y*p->bwidth;
1504 convgrey = p->convgrey; /* convert rgb to grey */
1505 isgrey = img->flags&Fgrey;
1506 alphaonly = p->alphaonly;
1507 copyalpha = (img->flags&Falpha) ? 1 : 0;
1509 /* if we can, avoid processing everything */
1510 if(!(img->flags&Frepl) && !convgrey && (img->flags&Fbytes)){
1511 memset(&b, 0, sizeof b);
1513 memmove(buf, r, dx*nb);
1516 b.rgba = (u32int*)r;
1518 b.alpha = r+img->shift[CAlpha]/8;
1522 b.grey = r+img->shift[CGrey]/8;
1523 b.red = b.grn = b.blu = b.grey;
1525 b.red = r+img->shift[CRed]/8;
1526 b.grn = r+img->shift[CGreen]/8;
1527 b.blu = r+img->shift[CBlue]/8;
1533 rrepl = replbit[img->nbits[CRed]];
1534 grepl = replbit[img->nbits[CGreen]];
1535 brepl = replbit[img->nbits[CBlue]];
1536 arepl = replbit[img->nbits[CAlpha]];
1537 krepl = replbit[img->nbits[CGrey]];
1539 for(i=0; i<dx; i++){
1540 u = r[0] | (r[1]<<8) | (r[2]<<16) | (r[3]<<24);
1542 *w++ = arepl[(u>>img->shift[CAlpha]) & img->mask[CAlpha]];
1545 *w++ = krepl[(u >> img->shift[CGrey]) & img->mask[CGrey]];
1546 else if(!alphaonly){
1547 ured = rrepl[(u >> img->shift[CRed]) & img->mask[CRed]];
1548 ugrn = grepl[(u >> img->shift[CGreen]) & img->mask[CGreen]];
1549 ublu = brepl[(u >> img->shift[CBlue]) & img->mask[CBlue]];
1551 *w++ = RGB2K(ured, ugrn, ublu);
1553 *w++ = brepl[(u >> img->shift[CBlue]) & img->mask[CBlue]];
1554 *w++ = grepl[(u >> img->shift[CGreen]) & img->mask[CGreen]];
1555 *w++ = rrepl[(u >> img->shift[CRed]) & img->mask[CRed]];
1563 b.alpha = copyalpha ? buf : &ones;
1564 b.rgba = (u32int*)buf;
1566 b.red = b.grn = b.blu = b.grey = nil;
1570 }else if(isgrey || convgrey){
1571 b.grey = buf+copyalpha;
1572 b.red = b.grn = b.blu = buf+copyalpha;
1573 b.delta = copyalpha+1;
1575 b.blu = buf+copyalpha;
1576 b.grn = buf+copyalpha+1;
1578 b.red = buf+copyalpha+2;
1579 b.delta = copyalpha+3;
1585 #define DBG if(drawdebug)
1587 writebyte(Param *p, uchar *w, Buffer src)
1590 int i, isalpha, isgrey, nb, delta, dx, adelta;
1591 uchar ff, *red, *grn, *blu, *grey, *alpha;
1605 mask = (nb==4) ? 0 : ~((1<<img->depth)-1);
1607 isalpha = img->flags&Falpha;
1608 isgrey = img->flags&Fgrey;
1611 if(isalpha && (alpha == nil || alpha == &ones)){
1617 for(i=0; i<dx; i++){
1618 u = w[0] | (w[1]<<8) | (w[2]<<16) | (w[3]<<24);
1619 DBG print("u %.8lux...", u);
1621 DBG print("&mask %.8lux...", u);
1623 u |= ((*grey >> (8-img->nbits[CGrey])) & img->mask[CGrey]) << img->shift[CGrey];
1624 DBG print("|grey %.8lux...", u);
1627 u |= ((*red >> (8-img->nbits[CRed])) & img->mask[CRed]) << img->shift[CRed];
1628 u |= ((*grn >> (8-img->nbits[CGreen])) & img->mask[CGreen]) << img->shift[CGreen];
1629 u |= ((*blu >> (8-img->nbits[CBlue])) & img->mask[CBlue]) << img->shift[CBlue];
1633 DBG print("|rgb %.8lux...", u);
1637 u |= ((*alpha >> (8-img->nbits[CAlpha])) & img->mask[CAlpha]) << img->shift[CAlpha];
1639 DBG print("|alpha %.8lux...", u);
1646 DBG print("write back %.8lux...", u);
1653 readfn(Memimage *img)
1657 if(img->nbits[CMap] == 8)
1663 readalphafn(Memimage *m)
1670 writefn(Memimage *img)
1674 if(img->chan == CMAP8)
1680 nullwrite(Param *p, uchar *s, Buffer b)
1687 readptr(Param *p, uchar *s, int y)
1693 memset(&b, 0, sizeof b);
1694 q = p->bytermin + y*p->bwidth;
1695 b.red = q; /* ptr to data */
1696 b.grn = b.blu = b.grey = b.alpha = nil;
1697 b.rgba = (u32int*)q;
1698 b.delta = p->img->depth/8;
1703 boolmemmove(Buffer bdst, Buffer bsrc, Buffer b1, int dx, int i, int o)
1707 memmove(bdst.red, bsrc.red, dx*bdst.delta);
1712 boolcopy8(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1714 uchar *m, *r, *w, *ew;
1722 for(; w < ew; w++,r++)
1725 return bdst; /* not used */
1729 boolcopy16(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1737 w = (ushort*)bdst.red;
1738 r = (ushort*)bsrc.red;
1740 for(; w < ew; w++,r++)
1743 return bdst; /* not used */
1747 boolcopy24(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1768 return bdst; /* not used */
1772 boolcopy32(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1780 w = (u32int*)bdst.red;
1781 r = (u32int*)bsrc.red;
1783 for(; w < ew; w++,r++)
1786 return bdst; /* not used */
1790 genconv(Param *p, uchar *buf, int y)
1796 /* read from source into RGB format in convbuf */
1797 b = p->convreadcall(p, p->convbuf, y);
1799 /* write RGB format into dst format in buf */
1800 p->convwritecall(p->convdpar, buf, b);
1803 nb = p->convdpar->img->depth/8;
1806 ew = buf+nb*p->convdx;
1812 b.blu = b.grn = b.grey = b.alpha = nil;
1813 b.rgba = (u32int*)buf;
1820 convfn(Memimage *dst, Param *dpar, Memimage *src, Param *spar)
1822 if(dst->chan == src->chan && !(src->flags&Frepl)){
1823 /*if(drawdebug) iprint("readptr..."); */
1827 if(dst->chan==CMAP8 && (src->chan==GREY1||src->chan==GREY2||src->chan==GREY4)){
1828 /* cheat because we know the replicated value is exactly the color map entry. */
1829 /*if(drawdebug) iprint("Readnbit..."); */
1833 spar->convreadcall = readfn(src);
1834 spar->convwritecall = writefn(dst);
1835 spar->convdpar = dpar;
1837 /* allocate a conversion buffer */
1838 spar->convbufoff = ndrawbuf;
1839 ndrawbuf += spar->dx*4;
1841 if(spar->dx > Dx(spar->img->r)){
1842 spar->convdx = spar->dx;
1843 spar->dx = Dx(spar->img->r);
1846 /*if(drawdebug) iprint("genconv..."); */
1851 * Do NOT call this directly. pixelbits is a wrapper
1852 * around this that fetches the bits from the X server
1856 _pixelbits(Memimage *i, Point pt)
1860 int off, bpp, npack;
1863 p = byteaddr(i, pt);
1864 switch(bpp=i->depth){
1870 val = p[0] >> bpp*(npack-1-off);
1877 val = p[0]|(p[1]<<8);
1880 val = p[0]|(p[1]<<8)|(p[2]<<16);
1883 val = p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24);
1894 boolcopyfn(Memimage *img, Memimage *mask)
1896 if(mask->flags&Frepl && Dx(mask->r)==1 && Dy(mask->r)==1 && pixelbits(mask, mask->r.min)==~0)
1909 assert(0 /* boolcopyfn */);
1915 * Optimized draw for filling and scrolling; uses memset and memmove.
1918 memsets(void *vp, ushort val, int n)
1929 memsetl(void *vp, u32int val, int n)
1940 memset24(void *vp, u32int val, int n)
1958 _imgtorgba(Memimage *img, u32int val)
1966 r = g = b = 0xAA; /* garbage */
1967 for(chan=img->chan; chan; chan>>=8){
1969 ov = v = val&((1<<nb)-1);
1995 p = img->cmap->cmap2rgb+3*ov;
2002 return (r<<24)|(g<<16)|(b<<8)|a;
2006 _rgbatoimg(Memimage *img, u32int rgba)
2011 uchar *p, r, g, b, a, m;
2019 for(chan=img->chan; chan; chan>>=8){
2023 v |= (r>>(8-nb))<<d;
2026 v |= (g>>(8-nb))<<d;
2029 v |= (b>>(8-nb))<<d;
2032 v |= (a>>(8-nb))<<d;
2035 p = img->cmap->rgb2cmap;
2036 m = p[(r>>4)*256+(g>>4)*16+(b>>4)];
2037 v |= (m>>(8-nb))<<d;
2041 v |= (m>>(8-nb))<<d;
2046 /* print("rgba2img %.8lux = %.*lux\n", rgba, 2*d/8, v); */
2052 memoptdraw(Memdrawparam *par)
2054 int m, y, dy, dx, op;
2065 DBG print("state %lux mval %lux dd %d\n", par->state, par->mval, dst->depth);
2067 * If we have an opaque mask and source is one opaque pixel we can convert to the
2068 * destination format and just replicate with memset.
2070 m = Simplesrc|Simplemask|Fullmask;
2071 if((par->state&m)==m && (par->srgba&0xFF) == 0xFF && (op ==S || op == SoverD)){
2073 int d, dwid, ppb, np, nb;
2076 DBG print("memopt, dst %p, dst->data->bdata %p\n", dst, dst->data->bdata);
2077 dwid = dst->width*sizeof(u32int);
2078 dp = byteaddr(dst, par->r.min);
2080 DBG print("sdval %lud, depth %d\n", v, dst->depth);
2085 for(d=dst->depth; d<8; d*=2)
2087 ppb = 8/dst->depth; /* pixels per byte */
2090 np = par->r.min.x&m; /* no. pixels unused on left side of word */
2092 nb = 8 - np * dst->depth; /* no. bits used on right side of word */
2094 DBG print("np %d x %d nb %d lm %ux ppb %d m %ux\n", np, par->r.min.x, nb, lm, ppb, m);
2097 np = par->r.max.x&m; /* no. pixels used on left side of word */
2099 nb = 8 - np * dst->depth; /* no. bits unused on right side of word */
2101 DBG print("np %d x %d nb %d rm %ux ppb %d m %ux\n", np, par->r.max.x, nb, rm, ppb, m);
2103 DBG print("dx %d Dx %d\n", dx, Dx(par->r));
2104 /* lm, rm are masks that are 1 where we should touch the bits */
2105 if(dx < 0){ /* just one byte */
2107 for(y=0; y<dy; y++, dp+=dwid)
2108 *dp ^= (v ^ *dp) & lm;
2109 }else if(dx == 0){ /* no full bytes */
2113 for(y=0; y<dy; y++, dp+=dwid){
2115 DBG print("dp %p v %lux lm %ux (v ^ *dp) & lm %lux\n", dp, v, lm, (v^*dp)&lm);
2116 *dp ^= (v ^ *dp) & lm;
2119 *dp ^= (v ^ *dp) & rm;
2121 }else{ /* full bytes in middle */
2127 for(y=0; y<dy; y++, dp+=dwid){
2129 *dp ^= (v ^ *dp) & lm;
2134 *dp ^= (v ^ *dp) & rm;
2139 for(y=0; y<dy; y++, dp+=dwid)
2143 p[0] = v; /* make little endian */
2146 DBG print("dp=%p; dx=%d; for(y=0; y<%d; y++, dp+=%d)\nmemsets(dp, v, dx);\n",
2148 for(y=0; y<dy; y++, dp+=dwid)
2152 for(y=0; y<dy; y++, dp+=dwid)
2153 memset24(dp, v, dx);
2156 p[0] = v; /* make little endian */
2161 for(y=0; y<dy; y++, dp+=dwid)
2165 assert(0 /* bad dest depth in memoptdraw */);
2170 * If no source alpha, an opaque mask, we can just copy the
2171 * source onto the destination. If the channels are the same and
2172 * the source is not replicated, memmove suffices.
2174 m = Simplemask|Fullmask;
2175 if((par->state&(m|Replsrc))==m && src->depth >= 8
2176 && src->chan == dst->chan && !(src->flags&Falpha) && (op == S || op == SoverD)){
2178 long swid, dwid, nb;
2181 if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min))
2186 swid = src->width*sizeof(u32int);
2187 dwid = dst->width*sizeof(u32int);
2188 sp = byteaddr(src, par->sr.min);
2189 dp = byteaddr(dst, par->r.min);
2196 nb = (dx*src->depth)/8;
2197 for(y=0; y<dy; y++, sp+=swid, dp+=dwid)
2198 memmove(dp, sp, nb);
2203 * If we have a 1-bit mask, 1-bit source, and 1-bit destination, and
2204 * they're all bit aligned, we can just use bit operators. This happens
2205 * when we're manipulating boolean masks, e.g. in the arc code.
2207 if((par->state&(Simplemask|Simplesrc|Replmask|Replsrc))==0
2208 && dst->chan==GREY1 && src->chan==GREY1 && par->mask->chan==GREY1
2209 && (par->r.min.x&7)==(par->sr.min.x&7) && (par->r.min.x&7)==(par->mr.min.x&7)){
2210 uchar *sp, *dp, *mp;
2212 long swid, dwid, mwid;
2215 sp = byteaddr(src, par->sr.min);
2216 dp = byteaddr(dst, par->r.min);
2217 mp = byteaddr(par->mask, par->mr.min);
2218 swid = src->width*sizeof(u32int);
2219 dwid = dst->width*sizeof(u32int);
2220 mwid = par->mask->width*sizeof(u32int);
2222 if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min)){
2227 lm = 0xFF>>(par->r.min.x&7);
2228 rm = 0xFF<<(8-(par->r.max.x&7));
2229 dx -= (8-(par->r.min.x&7)) + (par->r.max.x&7);
2231 if(dx < 0){ /* one byte wide */
2241 for(y=0; y<dy; y++){
2242 *dp ^= (*dp ^ *sp) & *mp & lm;
2252 i = (lm!=0)+dx+(rm!=0);
2256 for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
2258 *dp ^= (*dp ^ *sp++) & *mp++ & lm;
2261 for(x=0; x<dx; x++){
2262 *dp ^= (*dp ^ *sp++) & *mp++;
2266 *dp ^= (*dp ^ *sp++) & *mp++ & rm;
2273 i = (lm!=0)+dx+(rm!=0);
2274 dp += dwid*(dy-1)+i-1;
2275 sp += swid*(dy-1)+i-1;
2276 mp += mwid*(dy-1)+i-1;
2280 for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
2282 *dp ^= (*dp ^ *sp--) & *mp-- & rm;
2285 for(x=0; x<dx; x++){
2286 *dp ^= (*dp ^ *sp--) & *mp--;
2290 *dp ^= (*dp ^ *sp--) & *mp-- & lm;
2302 * Boolean character drawing.
2303 * Solid opaque color through a 1-bit greyscale mask.
2307 chardraw(Memdrawparam *par)
2310 int i, ddepth, dy, dx, x, bx, ex, y, npack, bsh, depth, op;
2311 u32int v, maskwid, dstwid;
2312 uchar *wp, *rp, *q, *wc;
2317 Memimage *mask, *src, *dst;
2319 if(0) if(drawdebug) iprint("chardraw? mf %lux md %d sf %lux dxs %d dys %d dd %d ddat %p sdat %p\n",
2320 par->mask->flags, par->mask->depth, par->src->flags,
2321 Dx(par->src->r), Dy(par->src->r), par->dst->depth, par->dst->data, par->src->data);
2330 if((par->state&(Replsrc|Simplesrc|Fullsrc|Replmask)) != (Replsrc|Simplesrc|Fullsrc)
2331 || mask->depth != 1 || dst->depth<8 || dst->data==src->data
2335 /*if(drawdebug) iprint("chardraw..."); */
2337 depth = mask->depth;
2338 maskwid = mask->width*sizeof(u32int);
2339 rp = byteaddr(mask, mr.min);
2341 bsh = (mr.min.x % npack) * depth;
2343 wp = byteaddr(dst, r.min);
2344 dstwid = dst->width*sizeof(u32int);
2345 DBG print("bsh %d\n", bsh);
2349 ddepth = dst->depth;
2352 * for loop counts from bsh to bsh+dx
2354 * we want the bottom bits to be the amount
2355 * to shift the pixels down, so for n≡0 (mod 8) we want
2356 * bottom bits 7. for n≡1, 6, etc.
2357 * the bits come from -n-1.
2365 /* make little endian */
2371 /*print("sp %x %x %x %x\n", sp[0], sp[1], sp[2], sp[3]); */
2372 for(y=0; y<dy; y++, rp+=maskwid, wp+=dstwid){
2378 /*if(drawdebug) iprint("8loop..."); */
2380 for(x=bx; x>ex; x--, wc++){
2384 DBG print("bits %lux sh %d...", bits, i);
2392 for(x=bx; x>ex; x--, ws++){
2396 DBG print("bits %lux sh %d...", bits, i);
2403 for(x=bx; x>ex; x--, wc+=3){
2407 DBG print("bits %lux sh %d...", bits, i);
2418 for(x=bx; x>ex; x--, wl++){
2422 DBG iprint("bits %lux sh %d...", bits, i);
2437 * Fill entire byte with replicated (if necessary) copy of source pixel,
2438 * assuming destination ldepth is >= source ldepth.
2440 * This code is just plain wrong for >8bpp.
2443 membyteval(Memimage *src)
2448 unloadmemimage(src, src->r, &uc, 1);
2450 uc <<= (src->r.min.x&(7/src->depth))*src->depth;
2452 * pixel value is now in high part of byte. repeat throughout byte
2454 for(i=bpp; i<8; i<<=1)
2462 _memfillcolor(Memimage *i, u32int val)
2471 bits = _rgbatoimg(i, val);
2473 case 24: /* 24-bit images suck */
2474 for(y=i->r.min.y; y<i->r.max.y; y++)
2475 memset24(byteaddr(i, Pt(i->r.min.x, y)), bits, Dx(i->r));
2477 default: /* 1, 2, 4, 8, 16, 32 */
2478 for(d=i->depth; d<32; d*=2)
2479 bits = (bits << d) | bits;
2480 p[0] = bits; /* make little endian */
2485 memsetl(wordaddr(i, i->r.min), bits, i->width*Dy(i->r));