10 /* perfect approximation to NTSC = .299r+.587g+.114b when 0 ≤ r,g,b < 256 */
11 #define RGB2K(r,g,b) ((156763*(r)+307758*(g)+59769*(b))>>19)
14 * For 16-bit values, x / 255 == (t = x+1, (t+(t>>8)) >> 8).
15 * We add another 127 to round to the nearest value rather
18 * CALCxy does x bytewise calculations on y input images (x=1,4; y=1,2).
19 * CALC2x does two parallel 16-bit calculations on y input images (y=1,2).
21 #define CALC11(a, v, tmp) \
22 (tmp=(a)*(v)+128, (tmp+(tmp>>8))>>8)
24 #define CALC12(a1, v1, a2, v2, tmp) \
25 (tmp=(a1)*(v1)+(a2)*(v2)+128, (tmp+(tmp>>8))>>8)
29 #define CALC21(a, vvuu, tmp) \
30 (tmp=(a)*(vvuu)+0x00800080, ((tmp+((tmp>>8)&MASK))>>8)&MASK)
32 #define CALC41(a, rgba, tmp1, tmp2) \
33 (CALC21(a, rgba & MASK, tmp1) | \
34 (CALC21(a, (rgba>>8)&MASK, tmp2)<<8))
36 #define CALC22(a1, vvuu1, a2, vvuu2, tmp) \
37 (tmp=(a1)*(vvuu1)+(a2)*(vvuu2)+0x00800080, ((tmp+((tmp>>8)&MASK))>>8)&MASK)
39 #define CALC42(a1, rgba1, a2, rgba2, tmp1, tmp2) \
40 (CALC22(a1, rgba1 & MASK, a2, rgba2 & MASK, tmp1) | \
41 (CALC22(a1, (rgba1>>8) & MASK, a2, (rgba2>>8) & MASK, tmp2)<<8))
43 static void mktables(void);
44 typedef int Subdraw(Memdrawparam*);
45 static Subdraw chardraw, alphadraw, memoptdraw;
47 static Memimage* memones;
48 static Memimage* memzeros;
51 Memimage *memtransparent;
59 static int didinit = 0;
65 if(strcmp(imagmem->name, "Image") == 0 || strcmp(imagmem->name, "image") == 0)
66 imagmem->move = memimagemove;
71 fmtinstall('R', Rfmt);
72 fmtinstall('P', Pfmt);
73 fmtinstall('b', _ifmt);
75 memones = allocmemimage(Rect(0,0,1,1), GREY1);
76 memzeros = allocmemimage(Rect(0,0,1,1), GREY1);
77 if(memones == nil || memzeros == nil)
80 memones->flags |= Frepl;
81 memones->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
82 *byteaddr(memones, ZP) = ~0;
84 memzeros->flags |= Frepl;
85 memzeros->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
86 *byteaddr(memzeros, ZP) = 0;
91 memtransparent = memzeros;
97 static ulong imgtorgba(Memimage*, ulong);
98 static ulong rgbatoimg(Memimage*, ulong);
99 static ulong pixelbits(Memimage*, Point);
103 memimagedraw(Memimage *dst, Rectangle r, Memimage *src, Point p0, Memimage *mask, Point p1, int op)
110 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);
112 if(drawclip(dst, &r, src, &p0, mask, &p1, &par.sr, &par.mr) == 0){
114 // iprint("empty clipped rectangle\n");
118 if(op < Clear || op > SoverD){
120 // iprint("op out of range: %d\n", op);
128 /* par.sr set by drawclip */
130 /* par.mr set by drawclip */
133 if(src->flags&Frepl){
134 par.state |= Replsrc;
135 if(Dx(src->r)==1 && Dy(src->r)==1){
136 par.sval = pixelbits(src, src->r.min);
137 par.state |= Simplesrc;
138 par.srgba = imgtorgba(src, par.sval);
139 par.sdval = rgbatoimg(dst, par.srgba);
140 if((par.srgba&0xFF) == 0 && (op&DoutS)){
141 // if (drawdebug) iprint("fill with transparent source\n");
142 return; /* no-op successfully handled */
147 if(mask->flags & Frepl){
148 par.state |= Replmask;
149 if(Dx(mask->r)==1 && Dy(mask->r)==1){
150 par.mval = pixelbits(mask, mask->r.min);
151 if(par.mval == 0 && (op&DoutS)){
152 // if(drawdebug) iprint("fill with zero mask\n");
153 return; /* no-op successfully handled */
155 par.state |= Simplemask;
157 par.state |= Fullmask;
158 par.mrgba = imgtorgba(mask, par.mval);
163 // iprint("dr %R sr %R mr %R...", r, par.sr, par.mr);
164 DBG print("draw dr %R sr %R mr %R %lux\n", r, par.sr, par.mr, par.state);
167 * Now that we've clipped the parameters down to be consistent, we
168 * simply try sub-drawing routines in order until we find one that was able
169 * to handle us. If the sub-drawing routine returns zero, it means it was
170 * unable to satisfy the request, so we do not return.
174 * Hardware support. Each video driver provides this function,
175 * which checks to see if there is anything it can help with.
176 * There could be an if around this checking to see if dst is in video memory.
178 DBG print("test hwdraw\n");
180 //if(drawdebug) iprint("hw handled\n");
181 DBG print("hwdraw handled\n");
185 * Optimizations using memmove and memset.
187 DBG print("test memoptdraw\n");
188 if(memoptdraw(&par)){
189 //if(drawdebug) iprint("memopt handled\n");
190 DBG print("memopt handled\n");
196 * Solid source color being painted through a boolean mask onto a high res image.
198 DBG print("test chardraw\n");
200 //if(drawdebug) iprint("chardraw handled\n");
201 DBG print("chardraw handled\n");
206 * General calculation-laden case that does alpha for each pixel.
208 DBG print("do alphadraw\n");
210 //if(drawdebug) iprint("alphadraw handled\n");
211 DBG print("alphadraw handled\n");
217 * Clip the destination rectangle further based on the properties of the
218 * source and mask rectangles. Once the destination rectangle is properly
219 * clipped, adjust the source and mask rectangles to be the same size.
221 * Return zero if the final rectangle is null.
224 drawclipnorepl(Memimage *dst, Rectangle *r, Memimage *src, Point *p0, Memimage *mask, Point *p1, Rectangle *sr, Rectangle *mr)
232 splitcoords = (p0->x!=p1->x) || (p0->y!=p1->y);
233 /* clip to destination */
235 if(!rectclip(r, dst->r) || !rectclip(r, dst->clipr))
237 /* move mask point */
238 p1->x += r->min.x-rmin.x;
239 p1->y += r->min.y-rmin.y;
240 /* move source point */
241 p0->x += r->min.x-rmin.x;
242 p0->y += r->min.y-rmin.y;
243 /* map destination rectangle into source */
245 sr->max.x = p0->x+Dx(*r);
246 sr->max.y = p0->y+Dy(*r);
247 /* sr is r in source coordinates; clip to source */
248 if(!(src->flags&Frepl) && !rectclip(sr, src->r))
250 if(!rectclip(sr, src->clipr))
252 /* compute and clip rectangle in mask */
254 /* move mask point with source */
255 p1->x += sr->min.x-p0->x;
256 p1->y += sr->min.y-p0->y;
258 mr->max.x = p1->x+Dx(*sr);
259 mr->max.y = p1->y+Dy(*sr);
261 /* mr is now rectangle in mask; clip it */
262 if(!(mask->flags&Frepl) && !rectclip(mr, mask->r))
264 if(!rectclip(mr, mask->clipr))
266 /* reflect any clips back to source */
267 sr->min.x += mr->min.x-omr.min.x;
268 sr->min.y += mr->min.y-omr.min.y;
269 sr->max.x += mr->max.x-omr.max.x;
270 sr->max.y += mr->max.y-omr.max.y;
272 if(!(mask->flags&Frepl) && !rectclip(sr, mask->r))
274 if(!rectclip(sr, mask->clipr))
278 /* move source clipping back to destination */
279 delta.x = r->min.x - p0->x;
280 delta.y = r->min.y - p0->y;
281 r->min.x = sr->min.x + delta.x;
282 r->min.y = sr->min.y + delta.y;
283 r->max.x = sr->max.x + delta.x;
284 r->max.y = sr->max.y + delta.y;
288 assert(Dx(*sr) == Dx(*mr) && Dx(*mr) == Dx(*r));
289 assert(Dy(*sr) == Dy(*mr) && Dy(*mr) == Dy(*r));
290 assert(ptinrect(r->min, dst->r));
296 * like drawclipnorepl() above, but if source or mask is replicated,
297 * move its clipped rectangle so that its minimum point falls within
298 * the repl rectangle.
300 * Return zero if the final rectangle is null.
303 drawclip(Memimage *dst, Rectangle *r, Memimage *src, Point *p0, Memimage *mask, Point *p1, Rectangle *sr, Rectangle *mr)
307 if(!drawclipnorepl(dst, r, src, p0, mask, p1, sr, mr))
310 /* move source rectangle so sr->min is in src->r */
311 if(src->flags&Frepl) {
312 delta.x = drawreplxy(src->r.min.x, src->r.max.x, sr->min.x) - sr->min.x;
313 delta.y = drawreplxy(src->r.min.y, src->r.max.y, sr->min.y) - sr->min.y;
314 sr->min.x += delta.x;
315 sr->min.y += delta.y;
316 sr->max.x += delta.x;
317 sr->max.y += delta.y;
321 /* move mask point so it is in mask->r */
322 *p1 = drawrepl(mask->r, *p1);
324 mr->max.x = p1->x+Dx(*sr);
325 mr->max.y = p1->y+Dy(*sr);
327 assert(ptinrect(*p0, src->r));
328 assert(ptinrect(*p1, mask->r));
336 static uchar replbit[1+8][256]; /* replbit[x][y] is the replication of the x-bit quantity y to 8-bit depth */
337 static uchar conv18[256][8]; /* conv18[x][y] is the yth pixel in the depth-1 pixel x */
338 static uchar conv28[256][4]; /* ... */
339 static uchar conv48[256][2];
342 * bitmap of how to replicate n bits to fill 8, for 1 ≤ n ≤ 8.
343 * the X's are where to put the bottom (ones) bit of the n-bit pattern.
344 * only the top 8 bits of the result are actually used.
345 * (the lower 8 bits are needed to get bits in the right place
346 * when n is not a divisor of 8.)
348 * Should check to see if its easier to just refer to replmul than
349 * use the precomputed values in replbit. On PCs it may well
350 * be; on machines with slow multiply instructions it probably isn't.
352 #define a ((((((((((((((((0
355 static int replmul[1+8] = {
357 a X X X X X X X X X X X X X X X X,
358 a _ X _ X _ X _ X _ X _ X _ X _ X,
359 a _ _ X _ _ X _ _ X _ _ X _ _ X _,
360 a _ _ _ X _ _ _ X _ _ _ X _ _ _ X,
361 a _ _ _ _ X _ _ _ _ X _ _ _ _ X _,
362 a _ _ _ _ _ X _ _ _ _ _ X _ _ _ _,
363 a _ _ _ _ _ _ X _ _ _ _ _ _ X _ _,
364 a _ _ _ _ _ _ _ X _ _ _ _ _ _ _ X,
373 int i, j, mask, sh, small;
375 /* bit replication up to 8 bits */
376 for(i=0; i<256; i++){
377 for(j=0; j<=8; j++){ /* j <= 8 [sic] */
378 small = i & ((1<<j)-1);
379 replbit[j][i] = (small*replmul[j])>>8;
383 /* bit unpacking up to 8 bits, only powers of 2 */
384 for(i=0; i<256; i++){
385 for(j=0, sh=7, mask=1; j<8; j++, sh--)
386 conv18[i][j] = replbit[1][(i>>sh)&mask];
388 for(j=0, sh=6, mask=3; j<4; j++, sh-=2)
389 conv28[i][j] = replbit[2][(i>>sh)&mask];
391 for(j=0, sh=4, mask=15; j<2; j++, sh-=4)
392 conv48[i][j] = replbit[4][(i>>sh)&mask];
396 static uchar ones = 0xff;
399 * General alpha drawing case. Can handle anything.
401 typedef struct Buffer Buffer;
403 /* used by most routines */
407 uchar *alpha; /* is &ones when unused, never nil */
410 int delta; /* number of bytes to add to pointer to get next pixel to the right */
412 /* used by boolcalc* for mask data */
413 uchar *m; /* ptr to mask data r.min byte; like p->bytermin */
414 int mskip; /* no. of left bits to skip in *m */
415 uchar *bm; /* ptr to mask data img->r.min byte; like p->bytey0s */
416 int bmskip; /* no. of left bits to skip in *bm */
417 uchar *em; /* ptr to mask data img->r.max.x byte; like p->bytey0e */
418 int emskip; /* no. of right bits to skip in *em */
421 typedef struct Param Param;
422 typedef Buffer Readfn(Param*, uchar*, int);
423 typedef void Writefn(Param*, uchar*, Buffer);
424 typedef Buffer Calcfn(Buffer, Buffer, Buffer, int, int, int);
430 /* giant rathole to customize functions with */
433 Readfn *greymaskcall;
434 Readfn *convreadcall;
435 Writefn *convwritecall;
444 uchar *bytey0s; /* byteaddr(Pt(img->r.min.x, img->r.min.y)) */
445 uchar *bytermin; /* byteaddr(Pt(r.min.x, img->r.min.y)) */
446 uchar *bytey0e; /* byteaddr(Pt(img->r.max.x, img->r.min.y)) */
449 int replcache; /* if set, cache buffers */
450 Buffer bcache[MAXBCACHE];
464 static Readfn greymaskread, replread, readptr;
465 static Writefn nullwrite;
466 static Calcfn alphacalc0, alphacalc14, alphacalc2810, alphacalc3679, alphacalc5, alphacalc11, alphacalcS;
467 static Calcfn boolcalc14, boolcalc236789, boolcalc1011;
469 static Readfn* readfn(Memimage*);
470 static Readfn* readalphafn(Memimage*);
471 static Writefn* writefn(Memimage*);
473 static Calcfn* boolcopyfn(Memimage*, Memimage*);
474 static Readfn* convfn(Memimage*, Param*, Memimage*, Param*, int*);
476 static Calcfn *alphacalc[Ncomp] =
478 alphacalc0, /* Clear */
479 alphacalc14, /* DoutS */
480 alphacalc2810, /* SoutD */
481 alphacalc3679, /* DxorS */
482 alphacalc14, /* DinS */
484 alphacalc3679, /* DatopS */
485 alphacalc3679, /* DoverS */
486 alphacalc2810, /* SinD */
487 alphacalc3679, /* SatopD */
488 alphacalc2810, /* S */
489 alphacalc11, /* SoverD */
492 static Calcfn *boolcalc[Ncomp] =
494 alphacalc0, /* Clear */
495 boolcalc14, /* DoutS */
496 boolcalc236789, /* SoutD */
497 boolcalc236789, /* DxorS */
498 boolcalc14, /* DinS */
500 boolcalc236789, /* DatopS */
501 boolcalc236789, /* DoverS */
502 boolcalc236789, /* SinD */
503 boolcalc236789, /* SatopD */
504 boolcalc1011, /* S */
505 boolcalc1011, /* SoverD */
509 * Avoid standard Lock, QLock so that can be used in kernel.
511 typedef struct Dbuf Dbuf;
516 Param spar, mpar, dpar;
519 static Dbuf dbuf[10];
526 for(i=0; i<nelem(dbuf); i++){
529 if(!_tas(&dbuf[i].inuse))
536 getparam(Param *p, Memimage *img, Rectangle r, int convgrey, int needbuf, int *ndrawbuf)
540 memset(p, 0, sizeof *p);
545 p->needbuf = needbuf;
546 p->convgrey = convgrey;
548 assert(img->r.min.x <= r.min.x && r.min.x < img->r.max.x);
550 p->bytey0s = byteaddr(img, Pt(img->r.min.x, img->r.min.y));
551 p->bytermin = byteaddr(img, Pt(r.min.x, img->r.min.y));
552 p->bytey0e = byteaddr(img, Pt(img->r.max.x, img->r.min.y));
553 p->bwidth = sizeof(ulong)*img->width;
555 assert(p->bytey0s <= p->bytermin && p->bytermin <= p->bytey0e);
557 if(p->r.min.x == p->img->r.min.x)
558 assert(p->bytermin == p->bytey0s);
561 if((img->flags&Frepl) && Dy(img->r) <= MAXBCACHE && Dy(img->r) < Dy(r)){
565 p->bufdelta = 4*p->dx;
566 p->bufoff = *ndrawbuf;
567 *ndrawbuf += p->bufdelta*nbuf;
571 clipy(Memimage *img, int *y)
580 assert(0 <= *y && *y < dy);
584 dumpbuf(char *s, Buffer b, int n)
593 print(" k%.2uX", *p);
597 print(" r%.2uX", *p);
601 print(" g%.2uX", *p);
605 print(" b%.2uX", *p);
609 if((p=b.alpha) != &ones){
610 print(" α%.2uX", *p);
618 * For each scan line, we expand the pixels from source, mask, and destination
619 * into byte-aligned red, green, blue, alpha, and grey channels. If buffering is not
620 * needed and the channels were already byte-aligned (grey8, rgb24, rgba32, rgb32),
621 * the readers need not copy the data: they can simply return pointers to the data.
622 * If the destination image is grey and the source is not, it is converted using the NTSC
625 * Once we have all the channels, we call either rgbcalc or greycalc, depending on
626 * whether the destination image is color. This is allowed to overwrite the dst buffer (perhaps
627 * the actual data, perhaps a copy) with its result. It should only overwrite the dst buffer
628 * with the same format (i.e. red bytes with red bytes, etc.) A new buffer is returned from
629 * the calculator, and that buffer is passed to a function to write it to the destination.
630 * If the buffer is already pointing at the destination, the writing function is a no-op.
634 alphadraw(Memdrawparam *par)
636 int isgrey, starty, endy, op;
637 int needbuf, dsty, srcy, masky;
638 int y, dir, dx, dy, ndrawbuf;
640 Buffer bsrc, bdst, bmask;
641 Readfn *rdsrc, *rdmask, *rddst;
644 Memimage *src, *mask, *dst;
663 isgrey = dst->flags&Fgrey;
666 * Buffering when src and dst are the same bitmap is sufficient but not
667 * necessary. There are stronger conditions we could use. We could
668 * check to see if the rectangles intersect, and if simply moving in the
669 * correct y direction can avoid the need to buffer.
671 needbuf = (src->data == dst->data);
674 getparam(&z->spar, src, sr, isgrey, needbuf, &ndrawbuf);
675 getparam(&z->dpar, dst, r, isgrey, needbuf, &ndrawbuf);
676 getparam(&z->mpar, mask, mr, 0, needbuf, &ndrawbuf);
678 dir = (needbuf && byteaddr(dst, r.min) > byteaddr(src, sr.min)) ? -1 : 1;
679 z->spar.dir = z->mpar.dir = z->dpar.dir = dir;
682 * If the mask is purely boolean, we can convert from src to dst format
683 * when we read src, and then just copy it to dst where the mask tells us to.
684 * This requires a boolean (1-bit grey) mask and lack of a source alpha channel.
686 * The computation is accomplished by assigning the function pointers as follows:
687 * rdsrc - read and convert source into dst format in a buffer
688 * rdmask - convert mask to bytes, set pointer to it
689 * rddst - fill with pointer to real dst data, but do no reads
690 * calc - copy src onto dst when mask says to.
692 * This is slightly sleazy, since things aren't doing exactly what their names say,
693 * but it avoids a fair amount of code duplication to make this a case here
694 * rather than have a separate booldraw.
696 //if(drawdebug) iprint("flag %lud mchan %lux=?%x dd %d\n", src->flags&Falpha, mask->chan, GREY1, dst->depth);
697 if(!(src->flags&Falpha) && mask->chan == GREY1 && dst->depth >= 8 && op == SoverD){
698 //if(drawdebug) iprint("boolcopy...");
699 rdsrc = convfn(dst, &z->dpar, src, &z->spar, &ndrawbuf);
701 rdmask = readfn(mask);
702 calc = boolcopyfn(dst, mask);
705 /* usual alphadraw parameter fetching */
708 wrdst = writefn(dst);
709 calc = alphacalc[op];
712 * If there is no alpha channel, we'll ask for a grey channel
713 * and pretend it is the alpha.
715 if(mask->flags&Falpha){
716 rdmask = readalphafn(mask);
717 z->mpar.alphaonly = 1;
719 z->mpar.greymaskcall = readfn(mask);
720 z->mpar.convgrey = 1;
721 rdmask = greymaskread;
724 * Should really be above, but then boolcopyfns would have
725 * to deal with bit alignment, and I haven't written that.
727 * This is a common case for things like ellipse drawing.
728 * When there's no alpha involved and the mask is boolean,
729 * we can avoid all the division and multiplication.
731 if(mask->chan == GREY1 && !(src->flags&Falpha))
733 else if(op == SoverD && !(src->flags&Falpha))
739 * If the image has a small enough repl rectangle,
740 * we can just read each line once and cache them.
742 if(z->spar.replcache){
743 z->spar.replcall = rdsrc;
746 if(z->mpar.replcache){
747 z->mpar.replcall = rdmask;
753 if((z->p = mallocz(ndrawbuf, 0)) == nil){
762 * Before we were saving only offsets from drawbuf in the parameter
763 * structures; now that drawbuf has been grown to accomodate us,
764 * we can fill in the pointers.
766 z->spar.bufbase = drawbuf+z->spar.bufoff;
767 z->mpar.bufbase = drawbuf+z->mpar.bufoff;
768 z->dpar.bufbase = drawbuf+z->dpar.bufoff;
769 z->spar.convbuf = drawbuf+z->spar.convbufoff;
780 * srcy, masky, and dsty are offsets from the top of their
781 * respective Rectangles. they need to be contained within
782 * the rectangles, so clipy can keep them there without division.
784 srcy = (starty + sr.min.y - src->r.min.y)%Dy(src->r);
785 masky = (starty + mr.min.y - mask->r.min.y)%Dy(mask->r);
786 dsty = starty + r.min.y - dst->r.min.y;
788 assert(0 <= srcy && srcy < Dy(src->r));
789 assert(0 <= masky && masky < Dy(mask->r));
790 assert(0 <= dsty && dsty < Dy(dst->r));
792 for(y=starty; y!=endy; y+=dir, srcy+=dir, masky+=dir, dsty+=dir){
797 bsrc = rdsrc(&z->spar, z->spar.bufbase, srcy);
799 bmask = rdmask(&z->mpar, z->mpar.bufbase, masky);
801 bdst = rddst(&z->dpar, z->dpar.bufbase, dsty);
802 DBG dumpbuf("src", bsrc, dx);
803 DBG dumpbuf("mask", bmask, dx);
804 DBG dumpbuf("dst", bdst, dx);
805 bdst = calc(bdst, bsrc, bmask, dx, isgrey, op);
806 wrdst(&z->dpar, z->dpar.bytermin+dsty*z->dpar.bwidth, bdst);
815 alphacalc0(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
821 memset(bdst.rgba, 0, dx*bdst.delta);
826 * Do the channels in the buffers match enough
827 * that we can do word-at-a-time operations
831 chanmatch(Buffer *bdst, Buffer *bsrc)
836 * first, r, g, b must be in the same place
839 drgb = (uchar*)bdst->rgba;
840 srgb = (uchar*)bsrc->rgba;
841 if(bdst->red - drgb != bsrc->red - srgb
842 || bdst->blu - drgb != bsrc->blu - srgb
843 || bdst->grn - drgb != bsrc->grn - srgb)
847 * that implies alpha is in the same place,
848 * if it is there at all (it might be == &ones).
849 * if the destination is &ones, we can scribble
850 * over the rgba slot just fine.
852 if(bdst->alpha == &ones)
856 * if the destination is not ones but the src is,
857 * then the simultaneous calculation will use
858 * bogus bytes from the src's rgba. no good.
860 if(bsrc->alpha == &ones)
864 * otherwise, alphas are in the same place.
870 alphacalc14(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
878 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
879 q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
884 fd = CALC11(sa, ma, t);
889 *bdst.grey = CALC11(fd, *bdst.grey, t);
890 bsrc.grey += bsrc.delta;
891 bdst.grey += bdst.delta;
894 *bdst.rgba = CALC41(fd, *bdst.rgba, t, t1);
897 bsrc.alpha += sadelta;
898 bmask.alpha += bmask.delta;
901 *bdst.red = CALC11(fd, *bdst.red, t);
902 *bdst.grn = CALC11(fd, *bdst.grn, t);
903 *bdst.blu = CALC11(fd, *bdst.blu, t);
904 bsrc.red += bsrc.delta;
905 bsrc.blu += bsrc.delta;
906 bsrc.grn += bsrc.delta;
907 bdst.red += bdst.delta;
908 bdst.blu += bdst.delta;
909 bdst.grn += bdst.delta;
911 if(bdst.alpha != &ones){
912 *bdst.alpha = CALC11(fd, *bdst.alpha, t);
913 bdst.alpha += bdst.delta;
915 bmask.alpha += bmask.delta;
916 bsrc.alpha += sadelta;
922 alphacalc2810(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
930 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
931 q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
940 fs = CALC11(fs, da, t);
943 *bdst.grey = CALC11(fs, *bsrc.grey, t);
944 bsrc.grey += bsrc.delta;
945 bdst.grey += bdst.delta;
948 *bdst.rgba = CALC41(fs, *bsrc.rgba, t, t1);
951 bmask.alpha += bmask.delta;
952 bdst.alpha += bdst.delta;
955 *bdst.red = CALC11(fs, *bsrc.red, t);
956 *bdst.grn = CALC11(fs, *bsrc.grn, t);
957 *bdst.blu = CALC11(fs, *bsrc.blu, t);
958 bsrc.red += bsrc.delta;
959 bsrc.blu += bsrc.delta;
960 bsrc.grn += bsrc.delta;
961 bdst.red += bdst.delta;
962 bdst.blu += bdst.delta;
963 bdst.grn += bdst.delta;
965 if(bdst.alpha != &ones){
966 *bdst.alpha = CALC11(fs, *bsrc.alpha, t);
967 bdst.alpha += bdst.delta;
969 bmask.alpha += bmask.delta;
970 bsrc.alpha += sadelta;
976 alphacalc3679(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
980 int i, sa, ma, da, q;
984 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
985 q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
992 fs = CALC11(ma, da, t);
994 fs = CALC11(ma, 255-da, t);
998 fd = CALC11(sa, ma, t);
1004 *bdst.grey = CALC12(fs, *bsrc.grey, fd, *bdst.grey, t);
1005 bsrc.grey += bsrc.delta;
1006 bdst.grey += bdst.delta;
1009 *bdst.rgba = CALC42(fs, *bsrc.rgba, fd, *bdst.rgba, t, t1);
1012 bsrc.alpha += sadelta;
1013 bmask.alpha += bmask.delta;
1014 bdst.alpha += bdst.delta;
1017 *bdst.red = CALC12(fs, *bsrc.red, fd, *bdst.red, t);
1018 *bdst.grn = CALC12(fs, *bsrc.grn, fd, *bdst.grn, t);
1019 *bdst.blu = CALC12(fs, *bsrc.blu, fd, *bdst.blu, t);
1020 bsrc.red += bsrc.delta;
1021 bsrc.blu += bsrc.delta;
1022 bsrc.grn += bsrc.delta;
1023 bdst.red += bdst.delta;
1024 bdst.blu += bdst.delta;
1025 bdst.grn += bdst.delta;
1027 if(bdst.alpha != &ones){
1028 *bdst.alpha = CALC12(fs, sa, fd, da, t);
1029 bdst.alpha += bdst.delta;
1031 bmask.alpha += bmask.delta;
1032 bsrc.alpha += sadelta;
1038 alphacalc5(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
1049 alphacalc11(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1058 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
1059 q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
1061 for(i=0; i<dx; i++){
1064 fd = 255-CALC11(sa, ma, t);
1067 *bdst.grey = CALC12(ma, *bsrc.grey, fd, *bdst.grey, t);
1068 bsrc.grey += bsrc.delta;
1069 bdst.grey += bdst.delta;
1072 *bdst.rgba = CALC42(ma, *bsrc.rgba, fd, *bdst.rgba, t, t1);
1075 bsrc.alpha += sadelta;
1076 bmask.alpha += bmask.delta;
1079 *bdst.red = CALC12(ma, *bsrc.red, fd, *bdst.red, t);
1080 *bdst.grn = CALC12(ma, *bsrc.grn, fd, *bdst.grn, t);
1081 *bdst.blu = CALC12(ma, *bsrc.blu, fd, *bdst.blu, t);
1082 bsrc.red += bsrc.delta;
1083 bsrc.blu += bsrc.delta;
1084 bsrc.grn += bsrc.delta;
1085 bdst.red += bdst.delta;
1086 bdst.blu += bdst.delta;
1087 bdst.grn += bdst.delta;
1089 if(bdst.alpha != &ones){
1090 *bdst.alpha = CALC12(ma, sa, fd, *bdst.alpha, t);
1091 bdst.alpha += bdst.delta;
1093 bmask.alpha += bmask.delta;
1094 bsrc.alpha += sadelta;
1101 source and mask alpha 1
1103 alphacalcS0(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1110 if(bsrc.delta == bdst.delta){
1111 memmove(bdst.rgba, bsrc.rgba, dx*bdst.delta);
1114 for(i=0; i<dx; i++){
1116 *bdst.grey = *bsrc.grey;
1117 bsrc.grey += bsrc.delta;
1118 bdst.grey += bdst.delta;
1120 *bdst.red = *bsrc.red;
1121 *bdst.grn = *bsrc.grn;
1122 *bdst.blu = *bsrc.blu;
1123 bsrc.red += bsrc.delta;
1124 bsrc.blu += bsrc.delta;
1125 bsrc.grn += bsrc.delta;
1126 bdst.red += bdst.delta;
1127 bdst.blu += bdst.delta;
1128 bdst.grn += bdst.delta;
1130 if(bdst.alpha != &ones){
1132 bdst.alpha += bdst.delta;
1139 /* source alpha 1 */
1141 alphacalcS(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1151 for(i=0; i<dx; i++){
1156 *bdst.grey = CALC12(ma, *bsrc.grey, fd, *bdst.grey, t);
1157 bsrc.grey += bsrc.delta;
1158 bdst.grey += bdst.delta;
1160 *bdst.red = CALC12(ma, *bsrc.red, fd, *bdst.red, t);
1161 *bdst.grn = CALC12(ma, *bsrc.grn, fd, *bdst.grn, t);
1162 *bdst.blu = CALC12(ma, *bsrc.blu, fd, *bdst.blu, t);
1163 bsrc.red += bsrc.delta;
1164 bsrc.blu += bsrc.delta;
1165 bsrc.grn += bsrc.delta;
1166 bdst.red += bdst.delta;
1167 bdst.blu += bdst.delta;
1168 bdst.grn += bdst.delta;
1170 if(bdst.alpha != &ones){
1171 *bdst.alpha = ma+CALC11(fd, *bdst.alpha, t);
1172 bdst.alpha += bdst.delta;
1174 bmask.alpha += bmask.delta;
1180 boolcalc14(Buffer bdst, Buffer b1, Buffer bmask, int dx, int grey, int op)
1189 for(i=0; i<dx; i++){
1191 zero = ma ? op == DoutS : op == DinS;
1196 bdst.grey += bdst.delta;
1199 *bdst.red = *bdst.grn = *bdst.blu = 0;
1200 bdst.red += bdst.delta;
1201 bdst.blu += bdst.delta;
1202 bdst.grn += bdst.delta;
1204 bmask.alpha += bmask.delta;
1205 if(bdst.alpha != &ones){
1208 bdst.alpha += bdst.delta;
1215 boolcalc236789(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1219 int i, ma, da, zero;
1225 for(i=0; i<dx; i++){
1237 *bdst.grey = CALC12(fs, *bsrc.grey, fd, *bdst.grey, t);
1240 bsrc.grey += bsrc.delta;
1241 bdst.grey += bdst.delta;
1244 *bdst.red = CALC12(fs, *bsrc.red, fd, *bdst.red, t);
1245 *bdst.grn = CALC12(fs, *bsrc.grn, fd, *bdst.grn, t);
1246 *bdst.blu = CALC12(fs, *bsrc.blu, fd, *bdst.blu, t);
1249 *bdst.red = *bdst.grn = *bdst.blu = 0;
1250 bsrc.red += bsrc.delta;
1251 bsrc.blu += bsrc.delta;
1252 bsrc.grn += bsrc.delta;
1253 bdst.red += bdst.delta;
1254 bdst.blu += bdst.delta;
1255 bdst.grn += bdst.delta;
1257 bmask.alpha += bmask.delta;
1258 if(bdst.alpha != &ones){
1260 *bdst.alpha = fs+CALC11(fd, da, t);
1263 bdst.alpha += bdst.delta;
1270 boolcalc1011(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1278 for(i=0; i<dx; i++){
1283 *bdst.grey = *bsrc.grey;
1286 bsrc.grey += bsrc.delta;
1287 bdst.grey += bdst.delta;
1290 *bdst.red = *bsrc.red;
1291 *bdst.grn = *bsrc.grn;
1292 *bdst.blu = *bsrc.blu;
1295 *bdst.red = *bdst.grn = *bdst.blu = 0;
1296 bsrc.red += bsrc.delta;
1297 bsrc.blu += bsrc.delta;
1298 bsrc.grn += bsrc.delta;
1299 bdst.red += bdst.delta;
1300 bdst.blu += bdst.delta;
1301 bdst.grn += bdst.delta;
1303 bmask.alpha += bmask.delta;
1304 if(bdst.alpha != &ones){
1309 bdst.alpha += bdst.delta;
1315 * Replicated cached scan line read. Call the function listed in the Param,
1316 * but cache the result so that for replicated images we only do the work once.
1319 replread(Param *p, uchar *s, int y)
1325 if((p->bfilled & (1<<y)) == 0){
1327 *b = p->replcall(p, p->bufbase+y*p->bufdelta, y);
1333 * Alpha reading function that simply relabels the grey pointer.
1336 greymaskread(Param *p, uchar *buf, int y)
1340 b = p->greymaskcall(p, buf, y);
1347 readnbit(Param *p, uchar *buf, int y)
1351 uchar *repl, *r, *w, *ow, bits;
1352 int i, n, sh, depth, x, dx, npack, nbits;
1354 b.rgba = (ulong*)buf;
1356 b.red = b.blu = b.grn = w;
1363 repl = &replbit[depth][0];
1367 /* copy from p->r.min.x until end of repl rectangle */
1370 if(n > p->img->r.max.x - x)
1371 n = p->img->r.max.x - x;
1373 r = p->bytermin + y*p->bwidth;
1374 DBG print("readnbit dx %d %p=%p+%d*%d, *r=%d fetch %d ", dx, r, p->bytermin, y, p->bwidth, *r, n);
1378 DBG print("throwaway %d...", i);
1384 DBG print("(%.2ux)...", *r);
1388 *w++ = repl[bits>>sh];
1389 DBG print("bit %x...", repl[bits>>sh]);
1397 assert(x+i == p->img->r.max.x);
1399 /* copy from beginning of repl rectangle until where we were before. */
1400 x = p->img->r.min.x;
1402 if(n > p->r.min.x - x)
1405 r = p->bytey0s + y*p->bwidth;
1406 DBG print("x=%d r=%p...", x, r);
1413 DBG print("nbits=%d...", nbits);
1419 *w++ = repl[bits>>sh];
1420 DBG print("bit %x...", repl[bits>>sh]);
1423 DBG print("bits %x nbits %d...", bits, nbits);
1430 /* now we have exactly one full scan line: just replicate the buffer itself until we are done */
1441 writenbit(Param *p, uchar *w, Buffer src)
1445 int i, sh, depth, npack, nbits, x, ex;
1447 assert(src.grey != nil && src.delta == 1);
1451 depth = p->img->depth;
1455 bits = i ? (*w >> (8-depth*i)) : 0;
1462 DBG print(" %x", *r);
1463 bits |= (*r++ >> sh);
1474 bits |= *w & ((1<<sh)-1);
1483 readcmap(Param *p, uchar *buf, int y)
1486 int a, convgrey, copyalpha, dx, i, m;
1487 uchar *q, *cmap, *begin, *end, *r, *w;
1489 begin = p->bytey0s + y*p->bwidth;
1490 r = p->bytermin + y*p->bwidth;
1491 end = p->bytey0e + y*p->bwidth;
1492 cmap = p->img->cmap->cmap2rgb;
1493 convgrey = p->convgrey;
1494 copyalpha = (p->img->flags&Falpha) != 0;
1500 a = p->img->shift[CAlpha]/8;
1501 m = p->img->shift[CMap]/8;
1502 for(i=0; i<dx; i++){
1509 *w++ = RGB2K(q[0], q[1], q[2]);
1511 *w++ = q[2]; /* blue */
1512 *w++ = q[1]; /* green */
1513 *w++ = q[0]; /* red */
1518 for(i=0; i<dx; i++){
1523 *w++ = RGB2K(q[0], q[1], q[2]);
1525 *w++ = q[2]; /* blue */
1526 *w++ = q[1]; /* green */
1527 *w++ = q[0]; /* red */
1532 b.rgba = (ulong*)(buf-copyalpha);
1536 b.red = b.blu = b.grn = buf;
1537 b.delta = 1+copyalpha;
1543 b.delta = 3+copyalpha;
1549 writecmap(Param *p, uchar *w, Buffer src)
1551 uchar *cmap, *red, *grn, *blu, *alpha;
1552 int i, dx, delta, a, m;
1554 cmap = p->img->cmap->rgb2cmap;
1562 if(p->img->flags&Falpha){
1564 m = p->img->shift[CMap]/8;
1565 a = p->img->shift[CAlpha]/8;
1566 for(i=0; i<dx; i++, red+=delta, grn+=delta, blu+=delta, w+=2){
1570 w[m] = cmap[(*red>>4)*256+(*grn>>4)*16+(*blu>>4)];
1573 for(i=0; i<dx; i++, red+=delta, grn+=delta, blu+=delta)
1574 *w++ = cmap[(*red>>4)*256+(*grn>>4)*16+(*blu>>4)];
1580 readbyte(Param *p, uchar *buf, int y)
1584 int dx, isgrey, convgrey, alphaonly, copyalpha, i, nb;
1585 uchar *begin, *end, *r, *w, *rrepl, *grepl, *brepl, *arepl, *krepl;
1586 uchar ured, ugrn, ublu;
1590 begin = p->bytey0s + y*p->bwidth;
1591 r = p->bytermin + y*p->bwidth;
1592 end = p->bytey0e + y*p->bwidth;
1598 convgrey = p->convgrey; /* convert rgb to grey */
1599 isgrey = img->flags&Fgrey;
1600 alphaonly = p->alphaonly;
1601 copyalpha = (img->flags&Falpha) != 0;
1603 DBG print("copyalpha %d alphaonly %d convgrey %d isgrey %d\n", copyalpha, alphaonly, convgrey, isgrey);
1604 /* if we can, avoid processing everything */
1605 if(!(img->flags&Frepl) && !convgrey && (img->flags&Fbytes)){
1606 memset(&b, 0, sizeof b);
1608 memmove(buf, r, dx*nb);
1613 b.alpha = r+img->shift[CAlpha]/8;
1617 b.grey = r+img->shift[CGrey]/8;
1618 b.red = b.grn = b.blu = b.grey;
1620 b.red = r+img->shift[CRed]/8;
1621 b.grn = r+img->shift[CGreen]/8;
1622 b.blu = r+img->shift[CBlue]/8;
1629 rrepl = replbit[img->nbits[CRed]];
1630 grepl = replbit[img->nbits[CGreen]];
1631 brepl = replbit[img->nbits[CBlue]];
1632 arepl = replbit[img->nbits[CAlpha]];
1633 krepl = replbit[img->nbits[CGrey]];
1635 for(i=0; i<dx; i++){
1636 u = r[0] | (r[1]<<8) | (r[2]<<16) | (r[3]<<24);
1638 *w++ = arepl[(u>>img->shift[CAlpha]) & img->mask[CAlpha]];
1639 DBG print("a %x\n", w[-1]);
1643 *w++ = krepl[(u >> img->shift[CGrey]) & img->mask[CGrey]];
1644 else if(!alphaonly){
1645 ured = rrepl[(u >> img->shift[CRed]) & img->mask[CRed]];
1646 ugrn = grepl[(u >> img->shift[CGreen]) & img->mask[CGreen]];
1647 ublu = brepl[(u >> img->shift[CBlue]) & img->mask[CBlue]];
1649 DBG print("g %x %x %x\n", ured, ugrn, ublu);
1650 *w++ = RGB2K(ured, ugrn, ublu);
1651 DBG print("%x\n", w[-1]);
1664 b.alpha = copyalpha ? buf : &ones;
1665 b.rgba = (ulong*)buf;
1667 b.red = b.grn = b.blu = b.grey = nil;
1671 }else if(isgrey || convgrey){
1672 b.grey = buf+copyalpha;
1673 b.red = b.grn = b.blu = buf+copyalpha;
1674 b.delta = copyalpha+1;
1675 DBG print("alpha %x grey %x\n", *b.alpha, *b.grey);
1677 b.blu = buf+copyalpha;
1678 b.grn = buf+copyalpha+1;
1680 b.red = buf+copyalpha+2;
1681 b.delta = copyalpha+3;
1689 writebyte(Param *p, uchar *w, Buffer src)
1692 int i, isalpha, isgrey, nb, delta, dx, adelta;
1693 uchar *red, *grn, *blu, *grey, *alpha;
1708 isalpha = img->flags&Falpha;
1709 isgrey = img->flags&Fgrey;
1712 if(isalpha && alpha == &ones)
1715 if((img->flags&Fbytes) != 0){
1716 int ogry, ored, ogrn, oblu, oalp;
1718 ogry = img->shift[CGrey]/8;
1719 ored = img->shift[CRed]/8;
1720 ogrn = img->shift[CGreen]/8;
1721 oblu = img->shift[CBlue]/8;
1722 oalp = img->shift[CAlpha]/8;
1724 for(i=0; i<dx; i++){
1745 mask = (nb==4) ? 0 : ~((1<<img->depth)-1);
1746 for(i=0; i<dx; i++){
1747 u = w[0] | (w[1]<<8) | (w[2]<<16) | (w[3]<<24);
1748 DBG print("u %.8lux...", u);
1750 DBG print("&mask %.8lux...", u);
1752 u |= ((*grey >> (8-img->nbits[CGrey])) & img->mask[CGrey]) << img->shift[CGrey];
1753 DBG print("|grey %.8lux...", u);
1756 u |= ((*red >> (8-img->nbits[CRed])) & img->mask[CRed]) << img->shift[CRed];
1757 u |= ((*grn >> (8-img->nbits[CGreen])) & img->mask[CGreen]) << img->shift[CGreen];
1758 u |= ((*blu >> (8-img->nbits[CBlue])) & img->mask[CBlue]) << img->shift[CBlue];
1762 DBG print("|rgb %.8lux...", u);
1766 u |= ((*alpha >> (8-img->nbits[CAlpha])) & img->mask[CAlpha]) << img->shift[CAlpha];
1768 DBG print("|alpha %.8lux...", u);
1781 readfn(Memimage *img)
1785 if(img->nbits[CMap] == 8)
1791 readalphafn(Memimage *m)
1798 writefn(Memimage *img)
1802 if(img->nbits[CMap] == 8)
1808 nullwrite(Param *p, uchar *s, Buffer b)
1816 readptr(Param *p, uchar *s, int y)
1822 q = p->bytermin + y*p->bwidth;
1823 b.red = q; /* ptr to data */
1824 b.grn = b.blu = b.grey = nil;
1827 b.delta = p->img->depth/8;
1832 boolmemmove(Buffer bdst, Buffer bsrc, Buffer b1, int dx, int i, int o)
1838 memmove(bdst.red, bsrc.red, dx*bdst.delta);
1843 boolcopy8(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1845 uchar *m, *r, *w, *ew;
1853 for(; w < ew; w++,r++)
1856 return bdst; /* not used */
1860 boolcopy16(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1868 w = (ushort*)bdst.red;
1869 r = (ushort*)bsrc.red;
1871 for(; w < ew; w++,r++)
1874 return bdst; /* not used */
1878 boolcopy24(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1899 return bdst; /* not used */
1903 boolcopy32(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1911 w = (ulong*)bdst.red;
1912 r = (ulong*)bsrc.red;
1914 for(; w < ew; w++,r++)
1917 return bdst; /* not used */
1921 genconv(Param *p, uchar *buf, int y)
1927 /* read from source into RGB format in convbuf */
1928 b = p->convreadcall(p, p->convbuf, y);
1930 /* write RGB format into dst format in buf */
1931 p->convwritecall(p->convdpar, buf, b);
1934 nb = p->convdpar->img->depth/8;
1937 ew = buf+nb*p->convdx;
1943 b.blu = b.grn = b.grey = nil;
1945 b.rgba = (ulong*)buf;
1952 convfn(Memimage *dst, Param *dpar, Memimage *src, Param *spar, int *ndrawbuf)
1954 if(dst->chan == src->chan && !(src->flags&Frepl)){
1955 //if(drawdebug) iprint("readptr...");
1959 if(dst->chan==CMAP8 && (src->chan==GREY1||src->chan==GREY2||src->chan==GREY4)){
1960 /* cheat because we know the replicated value is exactly the color map entry. */
1961 //if(drawdebug) iprint("Readnbit...");
1965 spar->convreadcall = readfn(src);
1966 spar->convwritecall = writefn(dst);
1967 spar->convdpar = dpar;
1969 /* allocate a conversion buffer */
1970 spar->convbufoff = *ndrawbuf;
1971 *ndrawbuf += spar->dx*4;
1973 if(spar->dx > Dx(spar->img->r)){
1974 spar->convdx = spar->dx;
1975 spar->dx = Dx(spar->img->r);
1978 //if(drawdebug) iprint("genconv...");
1983 pixelbits(Memimage *i, Point pt)
1987 int off, bpp, npack;
1990 p = byteaddr(i, pt);
1991 switch(bpp=i->depth){
1997 val = p[0] >> bpp*(npack-1-off);
2004 val = p[0]|(p[1]<<8);
2007 val = p[0]|(p[1]<<8)|(p[2]<<16);
2010 val = p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24);
2021 boolcopyfn(Memimage *img, Memimage *mask)
2023 if(mask->flags&Frepl && Dx(mask->r)==1 && Dy(mask->r)==1 && pixelbits(mask, mask->r.min)==~0)
2036 assert(0 /* boolcopyfn */);
2042 * Optimized draw for filling and scrolling; uses memset and memmove.
2045 memsets(void *vp, ushort val, int n)
2050 /* make little endian */
2062 memsetl(void *vp, ulong val, int n)
2067 /* make little endian */
2081 memset24(void *vp, ulong val, int n)
2101 imgtorgba(Memimage *img, ulong val)
2109 r = g = b = 0xAA; /* garbage */
2110 for(chan=img->chan; chan; chan>>=8){
2112 ov = v = val&((1<<nb)-1);
2138 p = img->cmap->cmap2rgb+3*ov;
2145 return (r<<24)|(g<<16)|(b<<8)|a;
2149 rgbatoimg(Memimage *img, ulong rgba)
2154 uchar *p, r, g, b, a, m;
2162 for(chan=img->chan; chan; chan>>=8){
2166 v |= (r>>(8-nb))<<d;
2169 v |= (g>>(8-nb))<<d;
2172 v |= (b>>(8-nb))<<d;
2175 v |= (a>>(8-nb))<<d;
2178 p = img->cmap->rgb2cmap;
2179 m = p[(r>>4)*256+(g>>4)*16+(b>>4)];
2180 v |= (m>>(8-nb))<<d;
2184 v |= (m>>(8-nb))<<d;
2189 // print("rgba2img %.8lux = %.*lux\n", rgba, 2*d/8, v);
2195 memoptdraw(Memdrawparam *par)
2197 int m, y, dy, dx, op;
2208 DBG print("state %lux mval %lux dd %d\n", par->state, par->mval, dst->depth);
2210 * If we have an opaque mask and source is one opaque pixel we can convert to the
2211 * destination format and just replicate with memset.
2213 m = Simplesrc|Simplemask|Fullmask;
2214 if((par->state&m)==m && (par->srgba&0xFF) == 0xFF && (op ==S || op == SoverD)){
2215 int d, dwid, ppb, np, nb;
2218 DBG print("memopt, dst %p, dst->data->bdata %p\n", dst, dst->data->bdata);
2219 dwid = dst->width*sizeof(ulong);
2220 dp = byteaddr(dst, par->r.min);
2222 DBG print("sdval %lud, depth %d\n", v, dst->depth);
2227 for(d=dst->depth; d<8; d*=2)
2229 ppb = 8/dst->depth; /* pixels per byte */
2232 np = par->r.min.x&m; /* no. pixels unused on left side of word */
2234 nb = 8 - np * dst->depth; /* no. bits used on right side of word */
2236 DBG print("np %d x %d nb %d lm %ux ppb %d m %ux\n", np, par->r.min.x, nb, lm, ppb, m);
2239 np = par->r.max.x&m; /* no. pixels used on left side of word */
2241 nb = 8 - np * dst->depth; /* no. bits unused on right side of word */
2243 DBG print("np %d x %d nb %d rm %ux ppb %d m %ux\n", np, par->r.max.x, nb, rm, ppb, m);
2245 DBG print("dx %d Dx %d\n", dx, Dx(par->r));
2246 /* lm, rm are masks that are 1 where we should touch the bits */
2247 if(dx < 0){ /* just one byte */
2249 for(y=0; y<dy; y++, dp+=dwid)
2250 *dp ^= (v ^ *dp) & lm;
2251 }else if(dx == 0){ /* no full bytes */
2255 for(y=0; y<dy; y++, dp+=dwid){
2257 DBG print("dp %p v %lux lm %ux (v ^ *dp) & lm %lux\n", dp, v, lm, (v^*dp)&lm);
2258 *dp ^= (v ^ *dp) & lm;
2261 *dp ^= (v ^ *dp) & rm;
2263 }else{ /* full bytes in middle */
2269 for(y=0; y<dy; y++, dp+=dwid){
2271 *dp ^= (v ^ *dp) & lm;
2276 *dp ^= (v ^ *dp) & rm;
2281 for(y=0; y<dy; y++, dp+=dwid)
2285 for(y=0; y<dy; y++, dp+=dwid)
2289 for(y=0; y<dy; y++, dp+=dwid)
2290 memset24(dp, v, dx);
2293 for(y=0; y<dy; y++, dp+=dwid)
2297 assert(0 /* bad dest depth in memoptdraw */);
2302 * If no source alpha, an opaque mask, we can just copy the
2303 * source onto the destination. If the channels are the same and
2304 * the source is not replicated, memmove suffices.
2306 m = Simplemask|Fullmask;
2307 if((par->state&(m|Replsrc))==m && src->depth >= 8
2308 && src->chan == dst->chan && !(src->flags&Falpha) && (op == S || op == SoverD)){
2310 long swid, dwid, nb;
2313 if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min))
2318 swid = src->width*sizeof(ulong);
2319 dwid = dst->width*sizeof(ulong);
2320 sp = byteaddr(src, par->sr.min);
2321 dp = byteaddr(dst, par->r.min);
2328 nb = (dx*src->depth)/8;
2329 for(y=0; y<dy; y++, sp+=swid, dp+=dwid)
2330 memmove(dp, sp, nb);
2335 * If we have a 1-bit mask, 1-bit source, and 1-bit destination, and
2336 * they're all bit aligned, we can just use bit operators. This happens
2337 * when we're manipulating boolean masks, e.g. in the arc code.
2339 if((par->state&(Simplemask|Simplesrc|Replmask|Replsrc))==0
2340 && dst->chan==GREY1 && src->chan==GREY1 && par->mask->chan==GREY1
2341 && (par->r.min.x&7)==(par->sr.min.x&7) && (par->r.min.x&7)==(par->mr.min.x&7)){
2342 uchar *sp, *dp, *mp;
2344 long swid, dwid, mwid;
2347 sp = byteaddr(src, par->sr.min);
2348 dp = byteaddr(dst, par->r.min);
2349 mp = byteaddr(par->mask, par->mr.min);
2350 swid = src->width*sizeof(ulong);
2351 dwid = dst->width*sizeof(ulong);
2352 mwid = par->mask->width*sizeof(ulong);
2354 if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min)){
2359 lm = 0xFF>>(par->r.min.x&7);
2360 rm = 0xFF<<(8-(par->r.max.x&7));
2361 dx -= (8-(par->r.min.x&7)) + (par->r.max.x&7);
2363 if(dx < 0){ /* one byte wide */
2373 for(y=0; y<dy; y++){
2374 *dp ^= (*dp ^ *sp) & *mp & lm;
2384 i = (lm!=0)+dx+(rm!=0);
2388 for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
2390 *dp ^= (*dp ^ *sp++) & *mp++ & lm;
2393 for(x=0; x<dx; x++){
2394 *dp ^= (*dp ^ *sp++) & *mp++;
2398 *dp ^= (*dp ^ *sp++) & *mp++ & rm;
2405 i = (lm!=0)+dx+(rm!=0);
2406 dp += dwid*(dy-1)+i-1;
2407 sp += swid*(dy-1)+i-1;
2408 mp += mwid*(dy-1)+i-1;
2412 for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
2414 *dp ^= (*dp ^ *sp--) & *mp-- & rm;
2417 for(x=0; x<dx; x++){
2418 *dp ^= (*dp ^ *sp--) & *mp--;
2422 *dp ^= (*dp ^ *sp--) & *mp-- & lm;
2434 * Boolean character drawing.
2435 * Solid opaque color through a 1-bit greyscale mask.
2439 chardraw(Memdrawparam *par)
2441 int i, ddepth, dy, dx, x, bx, ex, y, npack, bsh, depth, op;
2442 ulong bits, v, maskwid, dstwid;
2443 uchar *wp, *rp, *q, *wc;
2448 Memimage *mask, *src, *dst;
2450 if(0) if(drawdebug) iprint("chardraw? mf %lux md %d sf %lux dxs %d dys %d dd %d ddat %p sdat %p\n",
2451 par->mask->flags, par->mask->depth, par->src->flags,
2452 Dx(par->src->r), Dy(par->src->r), par->dst->depth, par->dst->data, par->src->data);
2461 if((par->state&(Replsrc|Simplesrc|Replmask)) != (Replsrc|Simplesrc)
2462 || mask->depth != 1 || src->flags&Falpha || dst->depth<8 || dst->data==src->data
2466 //if(drawdebug) iprint("chardraw...");
2468 depth = mask->depth;
2469 maskwid = mask->width*sizeof(ulong);
2470 rp = byteaddr(mask, mr.min);
2472 bsh = (mr.min.x % npack) * depth;
2474 wp = byteaddr(dst, r.min);
2475 dstwid = dst->width*sizeof(ulong);
2476 DBG print("bsh %d\n", bsh);
2480 ddepth = dst->depth;
2483 * for loop counts from bsh to bsh+dx
2485 * we want the bottom bits to be the amount
2486 * to shift the pixels down, so for n≡0 (mod 8) we want
2487 * bottom bits 7. for n≡1, 6, etc.
2488 * the bits come from -n-1.
2496 /* make little endian */
2502 //print("sp %x %x %x %x\n", sp[0], sp[1], sp[2], sp[3]);
2503 for(y=0; y<dy; y++, rp+=maskwid, wp+=dstwid){
2509 //if(drawdebug) iprint("8loop...");
2511 for(x=bx; x>ex; x--, wc++){
2515 DBG print("bits %lux sh %d...", bits, i);
2523 for(x=bx; x>ex; x--, ws++){
2527 DBG print("bits %lux sh %d...", bits, i);
2534 for(x=bx; x>ex; x--, wc+=3){
2538 DBG print("bits %lux sh %d...", bits, i);
2549 for(x=bx; x>ex; x--, wl++){
2553 DBG iprint("bits %lux sh %d...", bits, i);
2568 memfillcolor(Memimage *i, ulong val)
2576 bits = rgbatoimg(i, val);
2578 case 24: /* 24-bit images suck */
2579 for(y=i->r.min.y; y<i->r.max.y; y++)
2580 memset24(byteaddr(i, Pt(i->r.min.x, y)), bits, Dx(i->r));
2582 default: /* 1, 2, 4, 8, 16, 32 */
2583 for(d=i->depth; d<32; d*=2)
2584 bits = (bits << d) | bits;
2585 memsetl(wordaddr(i, i->r.min), bits, i->width*Dy(i->r));