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 16-bit values, x / 255 == (t = x+1, (t+(t>>8)) >> 8).
14 * We add another 127 to round to the nearest value rather
17 * CALCxy does x bytewise calculations on y input images (x=1,4; y=1,2).
18 * CALC2x does two parallel 16-bit calculations on y input images (y=1,2).
20 #define CALC11(a, v, tmp) \
21 (tmp=(a)*(v)+128, (tmp+(tmp>>8))>>8)
23 #define CALC12(a1, v1, a2, v2, tmp) \
24 (tmp=(a1)*(v1)+(a2)*(v2)+128, (tmp+(tmp>>8))>>8)
28 #define CALC21(a, vvuu, tmp) \
29 (tmp=(a)*(vvuu)+0x00800080, ((tmp+((tmp>>8)&MASK))>>8)&MASK)
31 #define CALC41(a, rgba, tmp1, tmp2) \
32 (CALC21(a, rgba & MASK, tmp1) | \
33 (CALC21(a, (rgba>>8)&MASK, tmp2)<<8))
35 #define CALC22(a1, vvuu1, a2, vvuu2, tmp) \
36 (tmp=(a1)*(vvuu1)+(a2)*(vvuu2)+0x00800080, ((tmp+((tmp>>8)&MASK))>>8)&MASK)
38 #define CALC42(a1, rgba1, a2, rgba2, tmp1, tmp2) \
39 (CALC22(a1, rgba1 & MASK, a2, rgba2 & MASK, tmp1) | \
40 (CALC22(a1, (rgba1>>8) & MASK, a2, (rgba2>>8) & MASK, tmp2)<<8))
42 static void mktables(void);
43 typedef int Subdraw(Memdrawparam*);
44 static Subdraw chardraw, alphadraw, memoptdraw;
46 static Memimage* memones;
47 static Memimage* memzeros;
50 Memimage *memtransparent;
58 static int didinit = 0;
64 if(strcmp(imagmem->name, "Image") == 0 || strcmp(imagmem->name, "image") == 0)
65 imagmem->move = memimagemove;
70 fmtinstall('R', Rfmt);
71 fmtinstall('P', Pfmt);
72 fmtinstall('b', _ifmt);
74 memones = allocmemimage(Rect(0,0,1,1), GREY1);
75 memzeros = allocmemimage(Rect(0,0,1,1), GREY1);
76 if(memones == nil || memzeros == nil)
79 memones->flags |= Frepl;
80 memones->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
81 *byteaddr(memones, ZP) = ~0;
83 memzeros->flags |= Frepl;
84 memzeros->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
85 *byteaddr(memzeros, ZP) = 0;
90 memtransparent = memzeros;
96 static ulong imgtorgba(Memimage*, ulong);
97 static ulong rgbatoimg(Memimage*, ulong);
98 static ulong pixelbits(Memimage*, Point);
101 memimagedraw(Memimage *dst, Rectangle r, Memimage *src, Point p0, Memimage *mask, Point p1, int op)
108 if(drawclip(dst, &r, src, &p0, mask, &p1, &par.sr, &par.mr) == 0)
111 if(op < Clear || op > SoverD)
118 /* par.sr set by drawclip */
120 /* par.mr set by drawclip */
123 if(src->flags&Frepl){
124 par.state |= Replsrc;
125 if(Dx(src->r)==1 && Dy(src->r)==1){
126 par.sval = pixelbits(src, src->r.min);
127 par.state |= Simplesrc;
128 par.srgba = imgtorgba(src, par.sval);
129 par.sdval = rgbatoimg(dst, par.srgba);
130 if((par.srgba&0xFF) == 0 && (op&DoutS))
131 return; /* no-op successfully handled */
135 if(mask->flags & Frepl){
136 par.state |= Replmask;
137 if(Dx(mask->r)==1 && Dy(mask->r)==1){
138 par.mval = pixelbits(mask, mask->r.min);
139 if(par.mval == 0 && (op&DoutS))
140 return; /* no-op successfully handled */
141 par.state |= Simplemask;
143 par.state |= Fullmask;
144 par.mrgba = imgtorgba(mask, par.mval);
149 * Now that we've clipped the parameters down to be consistent, we
150 * simply try sub-drawing routines in order until we find one that was able
151 * to handle us. If the sub-drawing routine returns zero, it means it was
152 * unable to satisfy the request, so we do not return.
156 * Hardware support. Each video driver provides this function,
157 * which checks to see if there is anything it can help with.
158 * There could be an if around this checking to see if dst is in video memory.
164 * Optimizations using memmove and memset.
171 * Solid source color being painted through a boolean mask onto a high res image.
177 * General calculation-laden case that does alpha for each pixel.
184 * Clip the destination rectangle further based on the properties of the
185 * source and mask rectangles. Once the destination rectangle is properly
186 * clipped, adjust the source and mask rectangles to be the same size.
188 * Return zero if the final rectangle is null.
191 drawclipnorepl(Memimage *dst, Rectangle *r, Memimage *src, Point *p0, Memimage *mask, Point *p1, Rectangle *sr, Rectangle *mr)
199 splitcoords = (p0->x!=p1->x) || (p0->y!=p1->y);
200 /* clip to destination */
202 if(!rectclip(r, dst->r) || !rectclip(r, dst->clipr))
204 /* move mask point */
205 p1->x += r->min.x-rmin.x;
206 p1->y += r->min.y-rmin.y;
207 /* move source point */
208 p0->x += r->min.x-rmin.x;
209 p0->y += r->min.y-rmin.y;
210 /* map destination rectangle into source */
212 sr->max.x = p0->x+Dx(*r);
213 sr->max.y = p0->y+Dy(*r);
214 /* sr is r in source coordinates; clip to source */
215 if(!(src->flags&Frepl) && !rectclip(sr, src->r))
217 if(!rectclip(sr, src->clipr))
219 /* compute and clip rectangle in mask */
221 /* move mask point with source */
222 p1->x += sr->min.x-p0->x;
223 p1->y += sr->min.y-p0->y;
225 mr->max.x = p1->x+Dx(*sr);
226 mr->max.y = p1->y+Dy(*sr);
228 /* mr is now rectangle in mask; clip it */
229 if(!(mask->flags&Frepl) && !rectclip(mr, mask->r))
231 if(!rectclip(mr, mask->clipr))
233 /* reflect any clips back to source */
234 sr->min.x += mr->min.x-omr.min.x;
235 sr->min.y += mr->min.y-omr.min.y;
236 sr->max.x += mr->max.x-omr.max.x;
237 sr->max.y += mr->max.y-omr.max.y;
239 if(!(mask->flags&Frepl) && !rectclip(sr, mask->r))
241 if(!rectclip(sr, mask->clipr))
245 /* move source clipping back to destination */
246 delta.x = r->min.x - p0->x;
247 delta.y = r->min.y - p0->y;
248 r->min.x = sr->min.x + delta.x;
249 r->min.y = sr->min.y + delta.y;
250 r->max.x = sr->max.x + delta.x;
251 r->max.y = sr->max.y + delta.y;
255 assert(Dx(*sr) == Dx(*mr) && Dx(*mr) == Dx(*r));
256 assert(Dy(*sr) == Dy(*mr) && Dy(*mr) == Dy(*r));
257 assert(ptinrect(r->min, dst->r));
263 * like drawclipnorepl() above, but if source or mask is replicated,
264 * move its clipped rectangle so that its minimum point falls within
265 * the repl rectangle.
267 * Return zero if the final rectangle is null.
270 drawclip(Memimage *dst, Rectangle *r, Memimage *src, Point *p0, Memimage *mask, Point *p1, Rectangle *sr, Rectangle *mr)
274 if(!drawclipnorepl(dst, r, src, p0, mask, p1, sr, mr))
277 /* move source rectangle so sr->min is in src->r */
278 if(src->flags&Frepl) {
279 delta.x = drawreplxy(src->r.min.x, src->r.max.x, sr->min.x) - sr->min.x;
280 delta.y = drawreplxy(src->r.min.y, src->r.max.y, sr->min.y) - sr->min.y;
281 sr->min.x += delta.x;
282 sr->min.y += delta.y;
283 sr->max.x += delta.x;
284 sr->max.y += delta.y;
288 /* move mask point so it is in mask->r */
289 *p1 = drawrepl(mask->r, *p1);
291 mr->max.x = p1->x+Dx(*sr);
292 mr->max.y = p1->y+Dy(*sr);
294 assert(ptinrect(*p0, src->r));
295 assert(ptinrect(*p1, mask->r));
303 static uchar replbit[1+8][256]; /* replbit[x][y] is the replication of the x-bit quantity y to 8-bit depth */
304 static uchar conv18[256][8]; /* conv18[x][y] is the yth pixel in the depth-1 pixel x */
305 static uchar conv28[256][4]; /* ... */
306 static uchar conv48[256][2];
309 * bitmap of how to replicate n bits to fill 8, for 1 ≤ n ≤ 8.
310 * the X's are where to put the bottom (ones) bit of the n-bit pattern.
311 * only the top 8 bits of the result are actually used.
312 * (the lower 8 bits are needed to get bits in the right place
313 * when n is not a divisor of 8.)
315 * Should check to see if its easier to just refer to replmul than
316 * use the precomputed values in replbit. On PCs it may well
317 * be; on machines with slow multiply instructions it probably isn't.
319 #define a ((((((((((((((((0
322 static int replmul[1+8] = {
324 a X X X X X X X X X X X X X X X X,
325 a _ X _ X _ X _ X _ X _ X _ X _ X,
326 a _ _ X _ _ X _ _ X _ _ X _ _ X _,
327 a _ _ _ X _ _ _ X _ _ _ X _ _ _ X,
328 a _ _ _ _ X _ _ _ _ X _ _ _ _ X _,
329 a _ _ _ _ _ X _ _ _ _ _ X _ _ _ _,
330 a _ _ _ _ _ _ X _ _ _ _ _ _ X _ _,
331 a _ _ _ _ _ _ _ X _ _ _ _ _ _ _ X,
340 int i, j, mask, sh, small;
342 /* bit replication up to 8 bits */
343 for(i=0; i<256; i++){
344 for(j=0; j<=8; j++){ /* j <= 8 [sic] */
345 small = i & ((1<<j)-1);
346 replbit[j][i] = (small*replmul[j])>>8;
350 /* bit unpacking up to 8 bits, only powers of 2 */
351 for(i=0; i<256; i++){
352 for(j=0, sh=7, mask=1; j<8; j++, sh--)
353 conv18[i][j] = replbit[1][(i>>sh)&mask];
355 for(j=0, sh=6, mask=3; j<4; j++, sh-=2)
356 conv28[i][j] = replbit[2][(i>>sh)&mask];
358 for(j=0, sh=4, mask=15; j<2; j++, sh-=4)
359 conv48[i][j] = replbit[4][(i>>sh)&mask];
363 static uchar ones = 0xff;
366 * General alpha drawing case. Can handle anything.
368 typedef struct Buffer Buffer;
370 /* used by most routines */
374 uchar *alpha; /* is &ones when unused, never nil */
377 int delta; /* number of bytes to add to pointer to get next pixel to the right */
379 /* used by boolcalc* for mask data */
380 uchar *m; /* ptr to mask data r.min byte; like p->bytermin */
381 int mskip; /* no. of left bits to skip in *m */
382 uchar *bm; /* ptr to mask data img->r.min byte; like p->bytey0s */
383 int bmskip; /* no. of left bits to skip in *bm */
384 uchar *em; /* ptr to mask data img->r.max.x byte; like p->bytey0e */
385 int emskip; /* no. of right bits to skip in *em */
388 typedef struct Param Param;
389 typedef Buffer Readfn(Param*, uchar*, int);
390 typedef void Writefn(Param*, uchar*, Buffer);
391 typedef Buffer Calcfn(Buffer, Buffer, Buffer, int, int, int);
397 /* giant rathole to customize functions with */
400 Readfn *greymaskcall;
401 Readfn *convreadcall;
402 Writefn *convwritecall;
411 uchar *bytey0s; /* byteaddr(Pt(img->r.min.x, img->r.min.y)) */
412 uchar *bytermin; /* byteaddr(Pt(r.min.x, img->r.min.y)) */
413 uchar *bytey0e; /* byteaddr(Pt(img->r.max.x, img->r.min.y)) */
416 int replcache; /* if set, cache buffers */
417 Buffer bcache[MAXBCACHE];
431 static Readfn greymaskread, replread, readptr;
432 static Writefn nullwrite;
433 static Calcfn alphacalc0, alphacalc14, alphacalc2810, alphacalc3679, alphacalc5, alphacalc11, alphacalcS;
434 static Calcfn boolcalc14, boolcalc236789, boolcalc1011;
436 static Readfn* readfn(Memimage*);
437 static Readfn* readalphafn(Memimage*);
438 static Writefn* writefn(Memimage*);
440 static Calcfn* boolcopyfn(Memimage*, Memimage*);
441 static Readfn* convfn(Memimage*, Param*, Memimage*, Param*, int*);
443 static Calcfn *alphacalc[Ncomp] =
445 alphacalc0, /* Clear */
446 alphacalc14, /* DoutS */
447 alphacalc2810, /* SoutD */
448 alphacalc3679, /* DxorS */
449 alphacalc14, /* DinS */
451 alphacalc3679, /* DatopS */
452 alphacalc3679, /* DoverS */
453 alphacalc2810, /* SinD */
454 alphacalc3679, /* SatopD */
455 alphacalc2810, /* S */
456 alphacalc11, /* SoverD */
459 static Calcfn *boolcalc[Ncomp] =
461 alphacalc0, /* Clear */
462 boolcalc14, /* DoutS */
463 boolcalc236789, /* SoutD */
464 boolcalc236789, /* DxorS */
465 boolcalc14, /* DinS */
467 boolcalc236789, /* DatopS */
468 boolcalc236789, /* DoverS */
469 boolcalc236789, /* SinD */
470 boolcalc236789, /* SatopD */
471 boolcalc1011, /* S */
472 boolcalc1011, /* SoverD */
476 * Avoid standard Lock, QLock so that can be used in kernel.
478 typedef struct Dbuf Dbuf;
483 Param spar, mpar, dpar;
486 static Dbuf dbuf[10];
493 for(i=0; i<nelem(dbuf); i++){
496 if(!_tas(&dbuf[i].inuse))
503 getparam(Param *p, Memimage *img, Rectangle r, int convgrey, int needbuf, int *ndrawbuf)
507 memset(p, 0, sizeof *p);
512 p->needbuf = needbuf;
513 p->convgrey = convgrey;
515 assert(img->r.min.x <= r.min.x && r.min.x < img->r.max.x);
517 p->bytey0s = byteaddr(img, Pt(img->r.min.x, img->r.min.y));
518 p->bytermin = byteaddr(img, Pt(r.min.x, img->r.min.y));
519 p->bytey0e = byteaddr(img, Pt(img->r.max.x, img->r.min.y));
520 p->bwidth = sizeof(ulong)*img->width;
522 assert(p->bytey0s <= p->bytermin && p->bytermin <= p->bytey0e);
524 if(p->r.min.x == p->img->r.min.x)
525 assert(p->bytermin == p->bytey0s);
528 if((img->flags&Frepl) && Dy(img->r) <= MAXBCACHE && Dy(img->r) < Dy(r)){
532 p->bufdelta = 4*p->dx;
533 p->bufoff = *ndrawbuf;
534 *ndrawbuf += p->bufdelta*nbuf;
538 clipy(Memimage *img, int *y)
547 assert(0 <= *y && *y < dy);
551 * For each scan line, we expand the pixels from source, mask, and destination
552 * into byte-aligned red, green, blue, alpha, and grey channels. If buffering is not
553 * needed and the channels were already byte-aligned (grey8, rgb24, rgba32, rgb32),
554 * the readers need not copy the data: they can simply return pointers to the data.
555 * If the destination image is grey and the source is not, it is converted using the NTSC
558 * Once we have all the channels, we call either rgbcalc or greycalc, depending on
559 * whether the destination image is color. This is allowed to overwrite the dst buffer (perhaps
560 * the actual data, perhaps a copy) with its result. It should only overwrite the dst buffer
561 * with the same format (i.e. red bytes with red bytes, etc.) A new buffer is returned from
562 * the calculator, and that buffer is passed to a function to write it to the destination.
563 * If the buffer is already pointing at the destination, the writing function is a no-op.
566 alphadraw(Memdrawparam *par)
568 int isgrey, starty, endy, op;
569 int needbuf, dsty, srcy, masky;
570 int y, dir, dx, dy, ndrawbuf;
572 Buffer bsrc, bdst, bmask;
573 Readfn *rdsrc, *rdmask, *rddst;
576 Memimage *src, *mask, *dst;
595 isgrey = dst->flags&Fgrey;
598 * Buffering when src and dst are the same bitmap is sufficient but not
599 * necessary. There are stronger conditions we could use. We could
600 * check to see if the rectangles intersect, and if simply moving in the
601 * correct y direction can avoid the need to buffer.
603 needbuf = (src->data == dst->data);
606 getparam(&z->spar, src, sr, isgrey, needbuf, &ndrawbuf);
607 getparam(&z->dpar, dst, r, isgrey, needbuf, &ndrawbuf);
608 getparam(&z->mpar, mask, mr, 0, needbuf, &ndrawbuf);
610 dir = (needbuf && byteaddr(dst, r.min) > byteaddr(src, sr.min)) ? -1 : 1;
611 z->spar.dir = z->mpar.dir = z->dpar.dir = dir;
614 * If the mask is purely boolean, we can convert from src to dst format
615 * when we read src, and then just copy it to dst where the mask tells us to.
616 * This requires a boolean (1-bit grey) mask and lack of a source alpha channel.
618 * The computation is accomplished by assigning the function pointers as follows:
619 * rdsrc - read and convert source into dst format in a buffer
620 * rdmask - convert mask to bytes, set pointer to it
621 * rddst - fill with pointer to real dst data, but do no reads
622 * calc - copy src onto dst when mask says to.
624 * This is slightly sleazy, since things aren't doing exactly what their names say,
625 * but it avoids a fair amount of code duplication to make this a case here
626 * rather than have a separate booldraw.
628 if(!(src->flags&Falpha) && mask->chan == GREY1 && dst->depth >= 8 && op == SoverD){
629 rdsrc = convfn(dst, &z->dpar, src, &z->spar, &ndrawbuf);
631 rdmask = readfn(mask);
632 calc = boolcopyfn(dst, mask);
635 /* usual alphadraw parameter fetching */
638 wrdst = writefn(dst);
639 calc = alphacalc[op];
642 * If there is no alpha channel, we'll ask for a grey channel
643 * and pretend it is the alpha.
645 if(mask->flags&Falpha){
646 rdmask = readalphafn(mask);
647 z->mpar.alphaonly = 1;
649 z->mpar.greymaskcall = readfn(mask);
650 z->mpar.convgrey = 1;
651 rdmask = greymaskread;
654 * Should really be above, but then boolcopyfns would have
655 * to deal with bit alignment, and I haven't written that.
657 * This is a common case for things like ellipse drawing.
658 * When there's no alpha involved and the mask is boolean,
659 * we can avoid all the division and multiplication.
661 if(mask->chan == GREY1 && !(src->flags&Falpha))
663 else if(op == SoverD && !(src->flags&Falpha))
669 * If the image has a small enough repl rectangle,
670 * we can just read each line once and cache them.
672 if(z->spar.replcache){
673 z->spar.replcall = rdsrc;
676 if(z->mpar.replcache){
677 z->mpar.replcall = rdmask;
683 if((z->p = mallocz(ndrawbuf, 0)) == nil){
692 * Before we were saving only offsets from drawbuf in the parameter
693 * structures; now that drawbuf has been grown to accomodate us,
694 * we can fill in the pointers.
696 z->spar.bufbase = drawbuf+z->spar.bufoff;
697 z->mpar.bufbase = drawbuf+z->mpar.bufoff;
698 z->dpar.bufbase = drawbuf+z->dpar.bufoff;
699 z->spar.convbuf = drawbuf+z->spar.convbufoff;
710 * srcy, masky, and dsty are offsets from the top of their
711 * respective Rectangles. they need to be contained within
712 * the rectangles, so clipy can keep them there without division.
714 srcy = (starty + sr.min.y - src->r.min.y)%Dy(src->r);
715 masky = (starty + mr.min.y - mask->r.min.y)%Dy(mask->r);
716 dsty = starty + r.min.y - dst->r.min.y;
718 assert(0 <= srcy && srcy < Dy(src->r));
719 assert(0 <= masky && masky < Dy(mask->r));
720 assert(0 <= dsty && dsty < Dy(dst->r));
722 for(y=starty; y!=endy; y+=dir, srcy+=dir, masky+=dir, dsty+=dir){
727 bsrc = rdsrc(&z->spar, z->spar.bufbase, srcy);
728 bmask = rdmask(&z->mpar, z->mpar.bufbase, masky);
729 bdst = rddst(&z->dpar, z->dpar.bufbase, dsty);
730 bdst = calc(bdst, bsrc, bmask, dx, isgrey, op);
731 wrdst(&z->dpar, z->dpar.bytermin+dsty*z->dpar.bwidth, bdst);
739 alphacalc0(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
745 memset(bdst.rgba, 0, dx*bdst.delta);
750 * Do the channels in the buffers match enough
751 * that we can do word-at-a-time operations
755 chanmatch(Buffer *bdst, Buffer *bsrc)
760 * first, r, g, b must be in the same place
763 drgb = (uchar*)bdst->rgba;
764 srgb = (uchar*)bsrc->rgba;
765 if(bdst->red - drgb != bsrc->red - srgb
766 || bdst->blu - drgb != bsrc->blu - srgb
767 || bdst->grn - drgb != bsrc->grn - srgb)
771 * that implies alpha is in the same place,
772 * if it is there at all (it might be == &ones).
773 * if the destination is &ones, we can scribble
774 * over the rgba slot just fine.
776 if(bdst->alpha == &ones)
780 * if the destination is not ones but the src is,
781 * then the simultaneous calculation will use
782 * bogus bytes from the src's rgba. no good.
784 if(bsrc->alpha == &ones)
788 * otherwise, alphas are in the same place.
794 alphacalc14(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
802 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
803 q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
808 fd = CALC11(sa, ma, t);
813 *bdst.grey = CALC11(fd, *bdst.grey, t);
814 bsrc.grey += bsrc.delta;
815 bdst.grey += bdst.delta;
818 *bdst.rgba = CALC41(fd, *bdst.rgba, t, t1);
821 bsrc.alpha += sadelta;
822 bmask.alpha += bmask.delta;
825 *bdst.red = CALC11(fd, *bdst.red, t);
826 *bdst.grn = CALC11(fd, *bdst.grn, t);
827 *bdst.blu = CALC11(fd, *bdst.blu, t);
828 bsrc.red += bsrc.delta;
829 bsrc.blu += bsrc.delta;
830 bsrc.grn += bsrc.delta;
831 bdst.red += bdst.delta;
832 bdst.blu += bdst.delta;
833 bdst.grn += bdst.delta;
835 if(bdst.alpha != &ones){
836 *bdst.alpha = CALC11(fd, *bdst.alpha, t);
837 bdst.alpha += bdst.delta;
839 bmask.alpha += bmask.delta;
840 bsrc.alpha += sadelta;
846 alphacalc2810(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
854 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
855 q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
864 fs = CALC11(fs, da, t);
867 *bdst.grey = CALC11(fs, *bsrc.grey, t);
868 bsrc.grey += bsrc.delta;
869 bdst.grey += bdst.delta;
872 *bdst.rgba = CALC41(fs, *bsrc.rgba, t, t1);
875 bmask.alpha += bmask.delta;
876 bdst.alpha += bdst.delta;
879 *bdst.red = CALC11(fs, *bsrc.red, t);
880 *bdst.grn = CALC11(fs, *bsrc.grn, t);
881 *bdst.blu = CALC11(fs, *bsrc.blu, t);
882 bsrc.red += bsrc.delta;
883 bsrc.blu += bsrc.delta;
884 bsrc.grn += bsrc.delta;
885 bdst.red += bdst.delta;
886 bdst.blu += bdst.delta;
887 bdst.grn += bdst.delta;
889 if(bdst.alpha != &ones){
890 *bdst.alpha = CALC11(fs, *bsrc.alpha, t);
891 bdst.alpha += bdst.delta;
893 bmask.alpha += bmask.delta;
894 bsrc.alpha += sadelta;
900 alphacalc3679(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
904 int i, sa, ma, da, q;
908 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
909 q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
916 fs = CALC11(ma, da, t);
918 fs = CALC11(ma, 255-da, t);
922 fd = CALC11(sa, ma, t);
928 *bdst.grey = CALC12(fs, *bsrc.grey, fd, *bdst.grey, t);
929 bsrc.grey += bsrc.delta;
930 bdst.grey += bdst.delta;
933 *bdst.rgba = CALC42(fs, *bsrc.rgba, fd, *bdst.rgba, t, t1);
936 bsrc.alpha += sadelta;
937 bmask.alpha += bmask.delta;
938 bdst.alpha += bdst.delta;
941 *bdst.red = CALC12(fs, *bsrc.red, fd, *bdst.red, t);
942 *bdst.grn = CALC12(fs, *bsrc.grn, fd, *bdst.grn, t);
943 *bdst.blu = CALC12(fs, *bsrc.blu, fd, *bdst.blu, t);
944 bsrc.red += bsrc.delta;
945 bsrc.blu += bsrc.delta;
946 bsrc.grn += bsrc.delta;
947 bdst.red += bdst.delta;
948 bdst.blu += bdst.delta;
949 bdst.grn += bdst.delta;
951 if(bdst.alpha != &ones){
952 *bdst.alpha = CALC12(fs, sa, fd, da, t);
953 bdst.alpha += bdst.delta;
955 bmask.alpha += bmask.delta;
956 bsrc.alpha += sadelta;
962 alphacalc5(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
973 alphacalc11(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
982 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
983 q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
988 fd = 255-CALC11(sa, ma, t);
991 *bdst.grey = CALC12(ma, *bsrc.grey, fd, *bdst.grey, t);
992 bsrc.grey += bsrc.delta;
993 bdst.grey += bdst.delta;
996 *bdst.rgba = CALC42(ma, *bsrc.rgba, fd, *bdst.rgba, t, t1);
999 bsrc.alpha += sadelta;
1000 bmask.alpha += bmask.delta;
1003 *bdst.red = CALC12(ma, *bsrc.red, fd, *bdst.red, t);
1004 *bdst.grn = CALC12(ma, *bsrc.grn, fd, *bdst.grn, t);
1005 *bdst.blu = CALC12(ma, *bsrc.blu, fd, *bdst.blu, t);
1006 bsrc.red += bsrc.delta;
1007 bsrc.blu += bsrc.delta;
1008 bsrc.grn += bsrc.delta;
1009 bdst.red += bdst.delta;
1010 bdst.blu += bdst.delta;
1011 bdst.grn += bdst.delta;
1013 if(bdst.alpha != &ones){
1014 *bdst.alpha = CALC12(ma, sa, fd, *bdst.alpha, t);
1015 bdst.alpha += bdst.delta;
1017 bmask.alpha += bmask.delta;
1018 bsrc.alpha += sadelta;
1025 source and mask alpha 1
1027 alphacalcS0(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1034 if(bsrc.delta == bdst.delta){
1035 memmove(bdst.rgba, bsrc.rgba, dx*bdst.delta);
1038 for(i=0; i<dx; i++){
1040 *bdst.grey = *bsrc.grey;
1041 bsrc.grey += bsrc.delta;
1042 bdst.grey += bdst.delta;
1044 *bdst.red = *bsrc.red;
1045 *bdst.grn = *bsrc.grn;
1046 *bdst.blu = *bsrc.blu;
1047 bsrc.red += bsrc.delta;
1048 bsrc.blu += bsrc.delta;
1049 bsrc.grn += bsrc.delta;
1050 bdst.red += bdst.delta;
1051 bdst.blu += bdst.delta;
1052 bdst.grn += bdst.delta;
1054 if(bdst.alpha != &ones){
1056 bdst.alpha += bdst.delta;
1063 /* source alpha 1 */
1065 alphacalcS(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1075 for(i=0; i<dx; i++){
1080 *bdst.grey = CALC12(ma, *bsrc.grey, fd, *bdst.grey, t);
1081 bsrc.grey += bsrc.delta;
1082 bdst.grey += bdst.delta;
1084 *bdst.red = CALC12(ma, *bsrc.red, fd, *bdst.red, t);
1085 *bdst.grn = CALC12(ma, *bsrc.grn, fd, *bdst.grn, t);
1086 *bdst.blu = CALC12(ma, *bsrc.blu, fd, *bdst.blu, t);
1087 bsrc.red += bsrc.delta;
1088 bsrc.blu += bsrc.delta;
1089 bsrc.grn += bsrc.delta;
1090 bdst.red += bdst.delta;
1091 bdst.blu += bdst.delta;
1092 bdst.grn += bdst.delta;
1094 if(bdst.alpha != &ones){
1095 *bdst.alpha = ma+CALC11(fd, *bdst.alpha, t);
1096 bdst.alpha += bdst.delta;
1098 bmask.alpha += bmask.delta;
1104 boolcalc14(Buffer bdst, Buffer b1, Buffer bmask, int dx, int grey, int op)
1113 for(i=0; i<dx; i++){
1115 zero = ma ? op == DoutS : op == DinS;
1120 bdst.grey += bdst.delta;
1123 *bdst.red = *bdst.grn = *bdst.blu = 0;
1124 bdst.red += bdst.delta;
1125 bdst.blu += bdst.delta;
1126 bdst.grn += bdst.delta;
1128 bmask.alpha += bmask.delta;
1129 if(bdst.alpha != &ones){
1132 bdst.alpha += bdst.delta;
1139 boolcalc236789(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1143 int i, ma, da, zero;
1149 for(i=0; i<dx; i++){
1161 *bdst.grey = CALC12(fs, *bsrc.grey, fd, *bdst.grey, t);
1164 bsrc.grey += bsrc.delta;
1165 bdst.grey += bdst.delta;
1168 *bdst.red = CALC12(fs, *bsrc.red, fd, *bdst.red, t);
1169 *bdst.grn = CALC12(fs, *bsrc.grn, fd, *bdst.grn, t);
1170 *bdst.blu = CALC12(fs, *bsrc.blu, fd, *bdst.blu, t);
1173 *bdst.red = *bdst.grn = *bdst.blu = 0;
1174 bsrc.red += bsrc.delta;
1175 bsrc.blu += bsrc.delta;
1176 bsrc.grn += bsrc.delta;
1177 bdst.red += bdst.delta;
1178 bdst.blu += bdst.delta;
1179 bdst.grn += bdst.delta;
1181 bmask.alpha += bmask.delta;
1182 if(bdst.alpha != &ones){
1184 *bdst.alpha = fs+CALC11(fd, da, t);
1187 bdst.alpha += bdst.delta;
1194 boolcalc1011(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1202 for(i=0; i<dx; i++){
1207 *bdst.grey = *bsrc.grey;
1210 bsrc.grey += bsrc.delta;
1211 bdst.grey += bdst.delta;
1214 *bdst.red = *bsrc.red;
1215 *bdst.grn = *bsrc.grn;
1216 *bdst.blu = *bsrc.blu;
1219 *bdst.red = *bdst.grn = *bdst.blu = 0;
1220 bsrc.red += bsrc.delta;
1221 bsrc.blu += bsrc.delta;
1222 bsrc.grn += bsrc.delta;
1223 bdst.red += bdst.delta;
1224 bdst.blu += bdst.delta;
1225 bdst.grn += bdst.delta;
1227 bmask.alpha += bmask.delta;
1228 if(bdst.alpha != &ones){
1233 bdst.alpha += bdst.delta;
1239 * Replicated cached scan line read. Call the function listed in the Param,
1240 * but cache the result so that for replicated images we only do the work once.
1243 replread(Param *p, uchar *s, int y)
1249 if((p->bfilled & (1<<y)) == 0){
1251 *b = p->replcall(p, p->bufbase+y*p->bufdelta, y);
1257 * Alpha reading function that simply relabels the grey pointer.
1260 greymaskread(Param *p, uchar *buf, int y)
1264 b = p->greymaskcall(p, buf, y);
1270 readnbit(Param *p, uchar *buf, int y)
1274 uchar *repl, *r, *w, *ow, bits;
1275 int i, n, sh, depth, x, dx, npack, nbits;
1277 b.rgba = (ulong*)buf;
1279 b.red = b.blu = b.grn = w;
1286 repl = &replbit[depth][0];
1290 /* copy from p->r.min.x until end of repl rectangle */
1293 if(n > p->img->r.max.x - x)
1294 n = p->img->r.max.x - x;
1296 r = p->bytermin + y*p->bwidth;
1308 *w++ = repl[bits>>sh];
1316 assert(x+i == p->img->r.max.x);
1318 /* copy from beginning of repl rectangle until where we were before. */
1319 x = p->img->r.min.x;
1321 if(n > p->r.min.x - x)
1324 r = p->bytey0s + y*p->bwidth;
1336 *w++ = repl[bits>>sh];
1345 /* now we have exactly one full scan line: just replicate the buffer itself until we are done */
1354 writenbit(Param *p, uchar *w, Buffer src)
1358 int i, sh, depth, npack, nbits, x, ex;
1360 assert(src.grey != nil && src.delta == 1);
1364 depth = p->img->depth;
1368 bits = i ? (*w >> (8-depth*i)) : 0;
1375 bits |= (*r++ >> sh);
1386 bits |= *w & ((1<<sh)-1);
1393 readcmap(Param *p, uchar *buf, int y)
1396 int a, convgrey, copyalpha, dx, i, m;
1397 uchar *q, *cmap, *begin, *end, *r, *w;
1399 begin = p->bytey0s + y*p->bwidth;
1400 r = p->bytermin + y*p->bwidth;
1401 end = p->bytey0e + y*p->bwidth;
1402 cmap = p->img->cmap->cmap2rgb;
1403 convgrey = p->convgrey;
1404 copyalpha = (p->img->flags&Falpha) != 0;
1410 a = p->img->shift[CAlpha]/8;
1411 m = p->img->shift[CMap]/8;
1412 for(i=0; i<dx; i++){
1419 *w++ = RGB2K(q[0], q[1], q[2]);
1421 *w++ = q[2]; /* blue */
1422 *w++ = q[1]; /* green */
1423 *w++ = q[0]; /* red */
1428 for(i=0; i<dx; i++){
1433 *w++ = RGB2K(q[0], q[1], q[2]);
1435 *w++ = q[2]; /* blue */
1436 *w++ = q[1]; /* green */
1437 *w++ = q[0]; /* red */
1442 b.rgba = (ulong*)(buf-copyalpha);
1446 b.red = b.blu = b.grn = buf;
1447 b.delta = 1+copyalpha;
1453 b.delta = 3+copyalpha;
1459 writecmap(Param *p, uchar *w, Buffer src)
1461 uchar *cmap, *red, *grn, *blu, *alpha;
1462 int i, dx, delta, a, m;
1464 cmap = p->img->cmap->rgb2cmap;
1472 if(p->img->flags&Falpha){
1474 m = p->img->shift[CMap]/8;
1475 a = p->img->shift[CAlpha]/8;
1476 for(i=0; i<dx; i++, red+=delta, grn+=delta, blu+=delta, w+=2){
1480 w[m] = cmap[(*red>>4)*256+(*grn>>4)*16+(*blu>>4)];
1483 for(i=0; i<dx; i++, red+=delta, grn+=delta, blu+=delta)
1484 *w++ = cmap[(*red>>4)*256+(*grn>>4)*16+(*blu>>4)];
1489 readbyte(Param *p, uchar *buf, int y)
1493 int dx, isgrey, convgrey, alphaonly, copyalpha, i, nb;
1494 uchar *begin, *end, *r, *w, *rrepl, *grepl, *brepl, *arepl, *krepl;
1495 uchar ured, ugrn, ublu;
1499 begin = p->bytey0s + y*p->bwidth;
1500 r = p->bytermin + y*p->bwidth;
1501 end = p->bytey0e + y*p->bwidth;
1507 convgrey = p->convgrey; /* convert rgb to grey */
1508 isgrey = img->flags&Fgrey;
1509 alphaonly = p->alphaonly;
1510 copyalpha = (img->flags&Falpha) != 0;
1512 /* if we can, avoid processing everything */
1513 if(!(img->flags&Frepl) && !convgrey && (img->flags&Fbytes)){
1514 memset(&b, 0, sizeof b);
1516 memmove(buf, r, dx*nb);
1521 b.alpha = r+img->shift[CAlpha]/8;
1525 b.grey = r+img->shift[CGrey]/8;
1526 b.red = b.grn = b.blu = b.grey;
1528 b.red = r+img->shift[CRed]/8;
1529 b.grn = r+img->shift[CGreen]/8;
1530 b.blu = r+img->shift[CBlue]/8;
1536 rrepl = replbit[img->nbits[CRed]];
1537 grepl = replbit[img->nbits[CGreen]];
1538 brepl = replbit[img->nbits[CBlue]];
1539 arepl = replbit[img->nbits[CAlpha]];
1540 krepl = replbit[img->nbits[CGrey]];
1542 for(i=0; i<dx; i++){
1543 u = r[0] | (r[1]<<8) | (r[2]<<16) | (r[3]<<24);
1545 *w++ = arepl[(u>>img->shift[CAlpha]) & img->mask[CAlpha]];
1549 *w++ = krepl[(u >> img->shift[CGrey]) & img->mask[CGrey]];
1550 else if(!alphaonly){
1551 ured = rrepl[(u >> img->shift[CRed]) & img->mask[CRed]];
1552 ugrn = grepl[(u >> img->shift[CGreen]) & img->mask[CGreen]];
1553 ublu = brepl[(u >> img->shift[CBlue]) & img->mask[CBlue]];
1555 *w++ = RGB2K(ured, ugrn, ublu);
1568 b.alpha = copyalpha ? buf : &ones;
1569 b.rgba = (ulong*)buf;
1571 b.red = b.grn = b.blu = b.grey = nil;
1575 }else if(isgrey || convgrey){
1576 b.grey = buf+copyalpha;
1577 b.red = b.grn = b.blu = buf+copyalpha;
1578 b.delta = copyalpha+1;
1580 b.blu = buf+copyalpha;
1581 b.grn = buf+copyalpha+1;
1583 b.red = buf+copyalpha+2;
1584 b.delta = copyalpha+3;
1590 writebyte(Param *p, uchar *w, Buffer src)
1593 int i, isalpha, isgrey, nb, delta, dx, adelta;
1594 uchar *red, *grn, *blu, *grey, *alpha;
1609 isalpha = img->flags&Falpha;
1610 isgrey = img->flags&Fgrey;
1613 if(isalpha && alpha == &ones)
1616 if((img->flags&Fbytes) != 0){
1617 int ogry, ored, ogrn, oblu, oalp;
1619 ogry = img->shift[CGrey]/8;
1620 ored = img->shift[CRed]/8;
1621 ogrn = img->shift[CGreen]/8;
1622 oblu = img->shift[CBlue]/8;
1623 oalp = img->shift[CAlpha]/8;
1625 for(i=0; i<dx; i++){
1646 mask = (nb==4) ? 0 : ~((1<<img->depth)-1);
1647 for(i=0; i<dx; i++){
1648 u = w[0] | (w[1]<<8) | (w[2]<<16) | (w[3]<<24);
1651 u |= ((*grey >> (8-img->nbits[CGrey])) & img->mask[CGrey]) << img->shift[CGrey];
1654 u |= ((*red >> (8-img->nbits[CRed])) & img->mask[CRed]) << img->shift[CRed];
1655 u |= ((*grn >> (8-img->nbits[CGreen])) & img->mask[CGreen]) << img->shift[CGreen];
1656 u |= ((*blu >> (8-img->nbits[CBlue])) & img->mask[CBlue]) << img->shift[CBlue];
1663 u |= ((*alpha >> (8-img->nbits[CAlpha])) & img->mask[CAlpha]) << img->shift[CAlpha];
1676 readfn(Memimage *img)
1680 if(img->nbits[CMap] == 8)
1686 readalphafn(Memimage *m)
1693 writefn(Memimage *img)
1697 if(img->nbits[CMap] == 8)
1703 nullwrite(Param *p, uchar *s, Buffer b)
1711 readptr(Param *p, uchar *s, int y)
1717 q = p->bytermin + y*p->bwidth;
1718 b.red = q; /* ptr to data */
1719 b.grn = b.blu = b.grey = nil;
1722 b.delta = p->img->depth/8;
1727 boolmemmove(Buffer bdst, Buffer bsrc, Buffer b1, int dx, int i, int o)
1733 memmove(bdst.red, bsrc.red, dx*bdst.delta);
1738 boolcopy8(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1740 uchar *m, *r, *w, *ew;
1748 for(; w < ew; w++,r++)
1751 return bdst; /* not used */
1755 boolcopy16(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1763 w = (ushort*)bdst.red;
1764 r = (ushort*)bsrc.red;
1766 for(; w < ew; w++,r++)
1769 return bdst; /* not used */
1773 boolcopy24(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1794 return bdst; /* not used */
1798 boolcopy32(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1806 w = (ulong*)bdst.red;
1807 r = (ulong*)bsrc.red;
1809 for(; w < ew; w++,r++)
1812 return bdst; /* not used */
1816 genconv(Param *p, uchar *buf, int y)
1822 /* read from source into RGB format in convbuf */
1823 b = p->convreadcall(p, p->convbuf, y);
1825 /* write RGB format into dst format in buf */
1826 p->convwritecall(p->convdpar, buf, b);
1829 nb = p->convdpar->img->depth/8;
1832 ew = buf+nb*p->convdx;
1838 b.blu = b.grn = b.grey = nil;
1840 b.rgba = (ulong*)buf;
1847 convfn(Memimage *dst, Param *dpar, Memimage *src, Param *spar, int *ndrawbuf)
1849 if(dst->chan == src->chan && !(src->flags&Frepl))
1852 if(dst->chan==CMAP8 && (src->chan==GREY1||src->chan==GREY2||src->chan==GREY4)){
1853 /* cheat because we know the replicated value is exactly the color map entry. */
1857 spar->convreadcall = readfn(src);
1858 spar->convwritecall = writefn(dst);
1859 spar->convdpar = dpar;
1861 /* allocate a conversion buffer */
1862 spar->convbufoff = *ndrawbuf;
1863 *ndrawbuf += spar->dx*4;
1865 if(spar->dx > Dx(spar->img->r)){
1866 spar->convdx = spar->dx;
1867 spar->dx = Dx(spar->img->r);
1874 pixelbits(Memimage *i, Point pt)
1878 int off, bpp, npack;
1881 p = byteaddr(i, pt);
1882 switch(bpp=i->depth){
1888 val = p[0] >> bpp*(npack-1-off);
1895 val = p[0]|(p[1]<<8);
1898 val = p[0]|(p[1]<<8)|(p[2]<<16);
1901 val = p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24);
1912 boolcopyfn(Memimage *img, Memimage *mask)
1914 if(mask->flags&Frepl && Dx(mask->r)==1 && Dy(mask->r)==1 && pixelbits(mask, mask->r.min)==~0)
1927 assert(0 /* boolcopyfn */);
1933 * Optimized draw for filling and scrolling; uses memset and memmove.
1936 memsets(void *vp, ushort val, int n)
1941 /* make little endian */
1953 memsetl(void *vp, ulong val, int n)
1958 /* make little endian */
1972 memset24(void *vp, ulong val, int n)
1992 imgtorgba(Memimage *img, ulong val)
2000 r = g = b = 0xAA; /* garbage */
2001 for(chan=img->chan; chan; chan>>=8){
2003 ov = v = val&((1<<nb)-1);
2029 p = img->cmap->cmap2rgb+3*ov;
2036 return (r<<24)|(g<<16)|(b<<8)|a;
2040 rgbatoimg(Memimage *img, ulong rgba)
2045 uchar *p, r, g, b, a, m;
2053 for(chan=img->chan; chan; chan>>=8){
2057 v |= (r>>(8-nb))<<d;
2060 v |= (g>>(8-nb))<<d;
2063 v |= (b>>(8-nb))<<d;
2066 v |= (a>>(8-nb))<<d;
2069 p = img->cmap->rgb2cmap;
2070 m = p[(r>>4)*256+(g>>4)*16+(b>>4)];
2071 v |= (m>>(8-nb))<<d;
2075 v |= (m>>(8-nb))<<d;
2084 memoptdraw(Memdrawparam *par)
2086 int m, y, dy, dx, op;
2098 * If we have an opaque mask and source is one opaque pixel we can convert to the
2099 * destination format and just replicate with memset.
2101 m = Simplesrc|Simplemask|Fullmask;
2102 if((par->state&m)==m && (par->srgba&0xFF) == 0xFF && (op ==S || op == SoverD)){
2103 int d, dwid, ppb, np, nb;
2106 dwid = dst->width*sizeof(ulong);
2107 dp = byteaddr(dst, par->r.min);
2113 for(d=dst->depth; d<8; d*=2)
2115 ppb = 8/dst->depth; /* pixels per byte */
2118 np = par->r.min.x&m; /* no. pixels unused on left side of word */
2120 nb = 8 - np * dst->depth; /* no. bits used on right side of word */
2124 np = par->r.max.x&m; /* no. pixels used on left side of word */
2126 nb = 8 - np * dst->depth; /* no. bits unused on right side of word */
2129 /* lm, rm are masks that are 1 where we should touch the bits */
2130 if(dx < 0){ /* just one byte */
2132 for(y=0; y<dy; y++, dp+=dwid)
2133 *dp ^= (v ^ *dp) & lm;
2134 }else if(dx == 0){ /* no full bytes */
2138 for(y=0; y<dy; y++, dp+=dwid){
2140 *dp ^= (v ^ *dp) & lm;
2143 *dp ^= (v ^ *dp) & rm;
2145 }else{ /* full bytes in middle */
2151 for(y=0; y<dy; y++, dp+=dwid){
2153 *dp ^= (v ^ *dp) & lm;
2158 *dp ^= (v ^ *dp) & rm;
2163 for(y=0; y<dy; y++, dp+=dwid)
2167 for(y=0; y<dy; y++, dp+=dwid)
2171 for(y=0; y<dy; y++, dp+=dwid)
2172 memset24(dp, v, dx);
2175 for(y=0; y<dy; y++, dp+=dwid)
2179 assert(0 /* bad dest depth in memoptdraw */);
2184 * If no source alpha, an opaque mask, we can just copy the
2185 * source onto the destination. If the channels are the same and
2186 * the source is not replicated, memmove suffices.
2188 m = Simplemask|Fullmask;
2189 if((par->state&(m|Replsrc))==m && src->depth >= 8
2190 && src->chan == dst->chan && !(src->flags&Falpha) && (op == S || op == SoverD)){
2192 long swid, dwid, nb;
2195 if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min))
2200 swid = src->width*sizeof(ulong);
2201 dwid = dst->width*sizeof(ulong);
2202 sp = byteaddr(src, par->sr.min);
2203 dp = byteaddr(dst, par->r.min);
2210 nb = (dx*src->depth)/8;
2211 for(y=0; y<dy; y++, sp+=swid, dp+=dwid)
2212 memmove(dp, sp, nb);
2217 * If we have a 1-bit mask, 1-bit source, and 1-bit destination, and
2218 * they're all bit aligned, we can just use bit operators. This happens
2219 * when we're manipulating boolean masks, e.g. in the arc code.
2221 if((par->state&(Simplemask|Simplesrc|Replmask|Replsrc))==0
2222 && dst->chan==GREY1 && src->chan==GREY1 && par->mask->chan==GREY1
2223 && (par->r.min.x&7)==(par->sr.min.x&7) && (par->r.min.x&7)==(par->mr.min.x&7)){
2224 uchar *sp, *dp, *mp;
2226 long swid, dwid, mwid;
2229 sp = byteaddr(src, par->sr.min);
2230 dp = byteaddr(dst, par->r.min);
2231 mp = byteaddr(par->mask, par->mr.min);
2232 swid = src->width*sizeof(ulong);
2233 dwid = dst->width*sizeof(ulong);
2234 mwid = par->mask->width*sizeof(ulong);
2236 if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min)){
2241 lm = 0xFF>>(par->r.min.x&7);
2242 rm = 0xFF<<(8-(par->r.max.x&7));
2243 dx -= (8-(par->r.min.x&7)) + (par->r.max.x&7);
2245 if(dx < 0){ /* one byte wide */
2255 for(y=0; y<dy; y++){
2256 *dp ^= (*dp ^ *sp) & *mp & lm;
2266 i = (lm!=0)+dx+(rm!=0);
2270 for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
2272 *dp ^= (*dp ^ *sp++) & *mp++ & lm;
2275 for(x=0; x<dx; x++){
2276 *dp ^= (*dp ^ *sp++) & *mp++;
2280 *dp ^= (*dp ^ *sp++) & *mp++ & rm;
2287 i = (lm!=0)+dx+(rm!=0);
2288 dp += dwid*(dy-1)+i-1;
2289 sp += swid*(dy-1)+i-1;
2290 mp += mwid*(dy-1)+i-1;
2294 for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
2296 *dp ^= (*dp ^ *sp--) & *mp-- & rm;
2299 for(x=0; x<dx; x++){
2300 *dp ^= (*dp ^ *sp--) & *mp--;
2304 *dp ^= (*dp ^ *sp--) & *mp-- & lm;
2315 * Boolean character drawing.
2316 * Solid opaque color through a 1-bit greyscale mask.
2319 chardraw(Memdrawparam *par)
2321 int i, ddepth, dy, dx, x, bx, ex, y, npack, bsh, depth, op;
2322 ulong bits, v, maskwid, dstwid;
2323 uchar *wp, *rp, *q, *wc;
2328 Memimage *mask, *src, *dst;
2337 if((par->state&(Replsrc|Simplesrc|Replmask)) != (Replsrc|Simplesrc)
2338 || mask->depth != 1 || src->flags&Falpha || dst->depth<8 || dst->data==src->data
2342 depth = mask->depth;
2343 maskwid = mask->width*sizeof(ulong);
2344 rp = byteaddr(mask, mr.min);
2346 bsh = (mr.min.x % npack) * depth;
2348 wp = byteaddr(dst, r.min);
2349 dstwid = dst->width*sizeof(ulong);
2353 ddepth = dst->depth;
2356 * for loop counts from bsh to bsh+dx
2358 * we want the bottom bits to be the amount
2359 * to shift the pixels down, so for n≡0 (mod 8) we want
2360 * bottom bits 7. for n≡1, 6, etc.
2361 * the bits come from -n-1.
2369 /* make little endian */
2375 for(y=0; y<dy; y++, rp+=maskwid, wp+=dstwid){
2382 for(x=bx; x>ex; x--, wc++){
2393 for(x=bx; x>ex; x--, ws++){
2403 for(x=bx; x>ex; x--, wc+=3){
2417 for(x=bx; x>ex; x--, wl++){
2432 memfillcolor(Memimage *i, ulong val)
2440 bits = rgbatoimg(i, val);
2442 case 24: /* 24-bit images suck */
2443 for(y=i->r.min.y; y<i->r.max.y; y++)
2444 memset24(byteaddr(i, Pt(i->r.min.x, y)), bits, Dx(i->r));
2446 default: /* 1, 2, 4, 8, 16, 32 */
2447 for(d=i->depth; d<32; d*=2)
2448 bits = (bits << d) | bits;
2449 memsetl(wordaddr(i, i->r.min), bits, i->width*Dy(i->r));