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)
111 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);
113 if(drawclip(dst, &r, src, &p0, mask, &p1, &par.sr, &par.mr) == 0){
115 // iprint("empty clipped rectangle\n");
119 if(op < Clear || op > SoverD){
121 // iprint("op out of range: %d\n", op);
129 /* par.sr set by drawclip */
131 /* par.mr set by drawclip */
134 if(src->flags&Frepl){
135 par.state |= Replsrc;
136 if(Dx(src->r)==1 && Dy(src->r)==1){
137 par.sval = pixelbits(src, src->r.min);
138 par.state |= Simplesrc;
139 par.srgba = imgtorgba(src, par.sval);
140 par.sdval = rgbatoimg(dst, par.srgba);
141 if((par.srgba&0xFF) == 0 && (op&DoutS)){
142 // if (drawdebug) iprint("fill with transparent source\n");
143 return; /* no-op successfully handled */
148 if(mask->flags & Frepl){
149 par.state |= Replmask;
150 if(Dx(mask->r)==1 && Dy(mask->r)==1){
151 par.mval = pixelbits(mask, mask->r.min);
152 if(par.mval == 0 && (op&DoutS)){
153 // if(drawdebug) iprint("fill with zero mask\n");
154 return; /* no-op successfully handled */
156 par.state |= Simplemask;
158 par.state |= Fullmask;
159 par.mrgba = imgtorgba(mask, par.mval);
164 // iprint("dr %R sr %R mr %R...", r, par.sr, par.mr);
165 DBG print("draw dr %R sr %R mr %R %lux\n", r, par.sr, par.mr, par.state);
168 * Now that we've clipped the parameters down to be consistent, we
169 * simply try sub-drawing routines in order until we find one that was able
170 * to handle us. If the sub-drawing routine returns zero, it means it was
171 * unable to satisfy the request, so we do not return.
175 * Hardware support. Each video driver provides this function,
176 * which checks to see if there is anything it can help with.
177 * There could be an if around this checking to see if dst is in video memory.
179 DBG print("test hwdraw\n");
181 //if(drawdebug) iprint("hw handled\n");
182 DBG print("hwdraw handled\n");
186 * Optimizations using memmove and memset.
188 DBG print("test memoptdraw\n");
189 if(memoptdraw(&par)){
190 //if(drawdebug) iprint("memopt handled\n");
191 DBG print("memopt handled\n");
197 * Solid source color being painted through a boolean mask onto a high res image.
199 DBG print("test chardraw\n");
201 //if(drawdebug) iprint("chardraw handled\n");
202 DBG print("chardraw handled\n");
207 * General calculation-laden case that does alpha for each pixel.
209 DBG print("do alphadraw\n");
211 //if(drawdebug) iprint("alphadraw handled\n");
212 DBG print("alphadraw handled\n");
218 * Clip the destination rectangle further based on the properties of the
219 * source and mask rectangles. Once the destination rectangle is properly
220 * clipped, adjust the source and mask rectangles to be the same size.
222 * Return zero if the final rectangle is null.
225 drawclipnorepl(Memimage *dst, Rectangle *r, Memimage *src, Point *p0, Memimage *mask, Point *p1, Rectangle *sr, Rectangle *mr)
233 splitcoords = (p0->x!=p1->x) || (p0->y!=p1->y);
234 /* clip to destination */
236 if(!rectclip(r, dst->r) || !rectclip(r, dst->clipr))
238 /* move mask point */
239 p1->x += r->min.x-rmin.x;
240 p1->y += r->min.y-rmin.y;
241 /* move source point */
242 p0->x += r->min.x-rmin.x;
243 p0->y += r->min.y-rmin.y;
244 /* map destination rectangle into source */
246 sr->max.x = p0->x+Dx(*r);
247 sr->max.y = p0->y+Dy(*r);
248 /* sr is r in source coordinates; clip to source */
249 if(!(src->flags&Frepl) && !rectclip(sr, src->r))
251 if(!rectclip(sr, src->clipr))
253 /* compute and clip rectangle in mask */
255 /* move mask point with source */
256 p1->x += sr->min.x-p0->x;
257 p1->y += sr->min.y-p0->y;
259 mr->max.x = p1->x+Dx(*sr);
260 mr->max.y = p1->y+Dy(*sr);
262 /* mr is now rectangle in mask; clip it */
263 if(!(mask->flags&Frepl) && !rectclip(mr, mask->r))
265 if(!rectclip(mr, mask->clipr))
267 /* reflect any clips back to source */
268 sr->min.x += mr->min.x-omr.min.x;
269 sr->min.y += mr->min.y-omr.min.y;
270 sr->max.x += mr->max.x-omr.max.x;
271 sr->max.y += mr->max.y-omr.max.y;
273 if(!(mask->flags&Frepl) && !rectclip(sr, mask->r))
275 if(!rectclip(sr, mask->clipr))
279 /* move source clipping back to destination */
280 delta.x = r->min.x - p0->x;
281 delta.y = r->min.y - p0->y;
282 r->min.x = sr->min.x + delta.x;
283 r->min.y = sr->min.y + delta.y;
284 r->max.x = sr->max.x + delta.x;
285 r->max.y = sr->max.y + delta.y;
289 assert(Dx(*sr) == Dx(*mr) && Dx(*mr) == Dx(*r));
290 assert(Dy(*sr) == Dy(*mr) && Dy(*mr) == Dy(*r));
291 assert(ptinrect(r->min, dst->r));
297 * like drawclipnorepl() above, but if source or mask is replicated,
298 * move its clipped rectangle so that its minimum point falls within
299 * the repl rectangle.
301 * Return zero if the final rectangle is null.
304 drawclip(Memimage *dst, Rectangle *r, Memimage *src, Point *p0, Memimage *mask, Point *p1, Rectangle *sr, Rectangle *mr)
308 if(!drawclipnorepl(dst, r, src, p0, mask, p1, sr, mr))
311 /* move source rectangle so sr->min is in src->r */
312 if(src->flags&Frepl) {
313 delta.x = drawreplxy(src->r.min.x, src->r.max.x, sr->min.x) - sr->min.x;
314 delta.y = drawreplxy(src->r.min.y, src->r.max.y, sr->min.y) - sr->min.y;
315 sr->min.x += delta.x;
316 sr->min.y += delta.y;
317 sr->max.x += delta.x;
318 sr->max.y += delta.y;
322 /* move mask point so it is in mask->r */
323 *p1 = drawrepl(mask->r, *p1);
325 mr->max.x = p1->x+Dx(*sr);
326 mr->max.y = p1->y+Dy(*sr);
328 assert(ptinrect(*p0, src->r));
329 assert(ptinrect(*p1, mask->r));
337 static uchar replbit[1+8][256]; /* replbit[x][y] is the replication of the x-bit quantity y to 8-bit depth */
338 static uchar conv18[256][8]; /* conv18[x][y] is the yth pixel in the depth-1 pixel x */
339 static uchar conv28[256][4]; /* ... */
340 static uchar conv48[256][2];
343 * bitmap of how to replicate n bits to fill 8, for 1 ≤ n ≤ 8.
344 * the X's are where to put the bottom (ones) bit of the n-bit pattern.
345 * only the top 8 bits of the result are actually used.
346 * (the lower 8 bits are needed to get bits in the right place
347 * when n is not a divisor of 8.)
349 * Should check to see if its easier to just refer to replmul than
350 * use the precomputed values in replbit. On PCs it may well
351 * be; on machines with slow multiply instructions it probably isn't.
353 #define a ((((((((((((((((0
356 static int replmul[1+8] = {
358 a X X X X X X X X X X X X X X X X,
359 a _ X _ X _ X _ X _ X _ X _ X _ X,
360 a _ _ X _ _ X _ _ X _ _ X _ _ X _,
361 a _ _ _ X _ _ _ X _ _ _ X _ _ _ X,
362 a _ _ _ _ X _ _ _ _ X _ _ _ _ X _,
363 a _ _ _ _ _ X _ _ _ _ _ X _ _ _ _,
364 a _ _ _ _ _ _ X _ _ _ _ _ _ X _ _,
365 a _ _ _ _ _ _ _ X _ _ _ _ _ _ _ X,
374 int i, j, mask, sh, small;
376 /* bit replication up to 8 bits */
377 for(i=0; i<256; i++){
378 for(j=0; j<=8; j++){ /* j <= 8 [sic] */
379 small = i & ((1<<j)-1);
380 replbit[j][i] = (small*replmul[j])>>8;
384 /* bit unpacking up to 8 bits, only powers of 2 */
385 for(i=0; i<256; i++){
386 for(j=0, sh=7, mask=1; j<8; j++, sh--)
387 conv18[i][j] = replbit[1][(i>>sh)&mask];
389 for(j=0, sh=6, mask=3; j<4; j++, sh-=2)
390 conv28[i][j] = replbit[2][(i>>sh)&mask];
392 for(j=0, sh=4, mask=15; j<2; j++, sh-=4)
393 conv48[i][j] = replbit[4][(i>>sh)&mask];
397 static uchar ones = 0xff;
400 * General alpha drawing case. Can handle anything.
402 typedef struct Buffer Buffer;
404 /* used by most routines */
408 uchar *alpha; /* is &ones when unused, never nil */
411 int delta; /* number of bytes to add to pointer to get next pixel to the right */
413 /* used by boolcalc* for mask data */
414 uchar *m; /* ptr to mask data r.min byte; like p->bytermin */
415 int mskip; /* no. of left bits to skip in *m */
416 uchar *bm; /* ptr to mask data img->r.min byte; like p->bytey0s */
417 int bmskip; /* no. of left bits to skip in *bm */
418 uchar *em; /* ptr to mask data img->r.max.x byte; like p->bytey0e */
419 int emskip; /* no. of right bits to skip in *em */
422 typedef struct Param Param;
423 typedef Buffer Readfn(Param*, uchar*, int);
424 typedef void Writefn(Param*, uchar*, Buffer);
425 typedef Buffer Calcfn(Buffer, Buffer, Buffer, int, int, int);
431 /* giant rathole to customize functions with */
434 Readfn *greymaskcall;
435 Readfn *convreadcall;
436 Writefn *convwritecall;
445 uchar *bytey0s; /* byteaddr(Pt(img->r.min.x, img->r.min.y)) */
446 uchar *bytermin; /* byteaddr(Pt(r.min.x, img->r.min.y)) */
447 uchar *bytey0e; /* byteaddr(Pt(img->r.max.x, img->r.min.y)) */
450 int replcache; /* if set, cache buffers */
451 Buffer bcache[MAXBCACHE];
465 static uchar *drawbuf;
468 static Readfn greymaskread, replread, readptr;
469 static Writefn nullwrite;
470 static Calcfn alphacalc0, alphacalc14, alphacalc2810, alphacalc3679, alphacalc5, alphacalc11, alphacalcS;
471 static Calcfn boolcalc14, boolcalc236789, boolcalc1011;
473 static Readfn* readfn(Memimage*);
474 static Readfn* readalphafn(Memimage*);
475 static Writefn* writefn(Memimage*);
477 static Calcfn* boolcopyfn(Memimage*, Memimage*);
478 static Readfn* convfn(Memimage*, Param*, Memimage*, Param*, int*);
479 static Readfn* ptrfn(Memimage*);
481 static Calcfn *alphacalc[Ncomp] =
483 alphacalc0, /* Clear */
484 alphacalc14, /* DoutS */
485 alphacalc2810, /* SoutD */
486 alphacalc3679, /* DxorS */
487 alphacalc14, /* DinS */
489 alphacalc3679, /* DatopS */
490 alphacalc3679, /* DoverS */
491 alphacalc2810, /* SinD */
492 alphacalc3679, /* SatopD */
493 alphacalc2810, /* S */
494 alphacalc11, /* SoverD */
497 static Calcfn *boolcalc[Ncomp] =
499 alphacalc0, /* Clear */
500 boolcalc14, /* DoutS */
501 boolcalc236789, /* SoutD */
502 boolcalc236789, /* DxorS */
503 boolcalc14, /* DinS */
505 boolcalc236789, /* DatopS */
506 boolcalc236789, /* DoverS */
507 boolcalc236789, /* SinD */
508 boolcalc236789, /* SatopD */
509 boolcalc1011, /* S */
510 boolcalc1011, /* SoverD */
514 * Avoid standard Lock, QLock so that can be used in kernel.
516 typedef struct Dbuf Dbuf;
521 Param spar, mpar, dpar;
524 static Dbuf dbuf[10];
531 for(i=0; i<nelem(dbuf); i++){
534 if(!_tas(&dbuf[i].inuse))
541 getparam(Param *p, Memimage *img, Rectangle r, int convgrey, int needbuf, int *ndrawbuf)
545 memset(p, 0, sizeof *p);
550 p->needbuf = needbuf;
551 p->convgrey = convgrey;
553 assert(img->r.min.x <= r.min.x && r.min.x < img->r.max.x);
555 p->bytey0s = byteaddr(img, Pt(img->r.min.x, img->r.min.y));
556 p->bytermin = byteaddr(img, Pt(r.min.x, img->r.min.y));
557 p->bytey0e = byteaddr(img, Pt(img->r.max.x, img->r.min.y));
558 p->bwidth = sizeof(ulong)*img->width;
560 assert(p->bytey0s <= p->bytermin && p->bytermin <= p->bytey0e);
562 if(p->r.min.x == p->img->r.min.x)
563 assert(p->bytermin == p->bytey0s);
566 if((img->flags&Frepl) && Dy(img->r) <= MAXBCACHE && Dy(img->r) < Dy(r)){
570 p->bufdelta = 4*p->dx;
571 p->bufoff = *ndrawbuf;
572 *ndrawbuf += p->bufdelta*nbuf;
576 clipy(Memimage *img, int *y)
585 assert(0 <= *y && *y < dy);
589 dumpbuf(char *s, Buffer b, int n)
598 print(" k%.2uX", *p);
602 print(" r%.2uX", *p);
606 print(" g%.2uX", *p);
610 print(" b%.2uX", *p);
614 if((p=b.alpha) != &ones){
615 print(" α%.2uX", *p);
623 * For each scan line, we expand the pixels from source, mask, and destination
624 * into byte-aligned red, green, blue, alpha, and grey channels. If buffering is not
625 * needed and the channels were already byte-aligned (grey8, rgb24, rgba32, rgb32),
626 * the readers need not copy the data: they can simply return pointers to the data.
627 * If the destination image is grey and the source is not, it is converted using the NTSC
630 * Once we have all the channels, we call either rgbcalc or greycalc, depending on
631 * whether the destination image is color. This is allowed to overwrite the dst buffer (perhaps
632 * the actual data, perhaps a copy) with its result. It should only overwrite the dst buffer
633 * with the same format (i.e. red bytes with red bytes, etc.) A new buffer is returned from
634 * the calculator, and that buffer is passed to a function to write it to the destination.
635 * If the buffer is already pointing at the destination, the writing function is a no-op.
639 alphadraw(Memdrawparam *par)
641 int isgrey, starty, endy, op;
642 int needbuf, dsty, srcy, masky;
643 int y, dir, dx, dy, ndrawbuf;
645 Buffer bsrc, bdst, bmask;
646 Readfn *rdsrc, *rdmask, *rddst;
649 Memimage *src, *mask, *dst;
668 isgrey = dst->flags&Fgrey;
671 * Buffering when src and dst are the same bitmap is sufficient but not
672 * necessary. There are stronger conditions we could use. We could
673 * check to see if the rectangles intersect, and if simply moving in the
674 * correct y direction can avoid the need to buffer.
676 needbuf = (src->data == dst->data);
679 getparam(&z->spar, src, sr, isgrey, needbuf, &ndrawbuf);
680 getparam(&z->dpar, dst, r, isgrey, needbuf, &ndrawbuf);
681 getparam(&z->mpar, mask, mr, 0, needbuf, &ndrawbuf);
683 dir = (needbuf && byteaddr(dst, r.min) > byteaddr(src, sr.min)) ? -1 : 1;
684 z->spar.dir = z->mpar.dir = z->dpar.dir = dir;
687 * If the mask is purely boolean, we can convert from src to dst format
688 * when we read src, and then just copy it to dst where the mask tells us to.
689 * This requires a boolean (1-bit grey) mask and lack of a source alpha channel.
691 * The computation is accomplished by assigning the function pointers as follows:
692 * rdsrc - read and convert source into dst format in a buffer
693 * rdmask - convert mask to bytes, set pointer to it
694 * rddst - fill with pointer to real dst data, but do no reads
695 * calc - copy src onto dst when mask says to.
697 * This is slightly sleazy, since things aren't doing exactly what their names say,
698 * but it avoids a fair amount of code duplication to make this a case here
699 * rather than have a separate booldraw.
701 //if(drawdebug) iprint("flag %lud mchan %lux=?%x dd %d\n", src->flags&Falpha, mask->chan, GREY1, dst->depth);
702 if(!(src->flags&Falpha) && mask->chan == GREY1 && dst->depth >= 8 && op == SoverD){
703 //if(drawdebug) iprint("boolcopy...");
704 rdsrc = convfn(dst, &z->dpar, src, &z->spar, &ndrawbuf);
706 rdmask = readfn(mask);
707 calc = boolcopyfn(dst, mask);
710 /* usual alphadraw parameter fetching */
713 wrdst = writefn(dst);
714 calc = alphacalc[op];
717 * If there is no alpha channel, we'll ask for a grey channel
718 * and pretend it is the alpha.
720 if(mask->flags&Falpha){
721 rdmask = readalphafn(mask);
722 z->mpar.alphaonly = 1;
724 z->mpar.greymaskcall = readfn(mask);
725 z->mpar.convgrey = 1;
726 rdmask = greymaskread;
729 * Should really be above, but then boolcopyfns would have
730 * to deal with bit alignment, and I haven't written that.
732 * This is a common case for things like ellipse drawing.
733 * When there's no alpha involved and the mask is boolean,
734 * we can avoid all the division and multiplication.
736 if(mask->chan == GREY1 && !(src->flags&Falpha))
738 else if(op == SoverD && !(src->flags&Falpha))
744 * If the image has a small enough repl rectangle,
745 * we can just read each line once and cache them.
747 if(z->spar.replcache){
748 z->spar.replcall = rdsrc;
751 if(z->mpar.replcache){
752 z->mpar.replcall = rdmask;
758 if((z->p = mallocz(ndrawbuf, 0)) == nil){
767 * Before we were saving only offsets from drawbuf in the parameter
768 * structures; now that drawbuf has been grown to accomodate us,
769 * we can fill in the pointers.
771 z->spar.bufbase = drawbuf+z->spar.bufoff;
772 z->mpar.bufbase = drawbuf+z->mpar.bufoff;
773 z->dpar.bufbase = drawbuf+z->dpar.bufoff;
774 z->spar.convbuf = drawbuf+z->spar.convbufoff;
785 * srcy, masky, and dsty are offsets from the top of their
786 * respective Rectangles. they need to be contained within
787 * the rectangles, so clipy can keep them there without division.
789 srcy = (starty + sr.min.y - src->r.min.y)%Dy(src->r);
790 masky = (starty + mr.min.y - mask->r.min.y)%Dy(mask->r);
791 dsty = starty + r.min.y - dst->r.min.y;
793 assert(0 <= srcy && srcy < Dy(src->r));
794 assert(0 <= masky && masky < Dy(mask->r));
795 assert(0 <= dsty && dsty < Dy(dst->r));
797 for(y=starty; y!=endy; y+=dir, srcy+=dir, masky+=dir, dsty+=dir){
802 bsrc = rdsrc(&z->spar, z->spar.bufbase, srcy);
804 bmask = rdmask(&z->mpar, z->mpar.bufbase, masky);
806 bdst = rddst(&z->dpar, z->dpar.bufbase, dsty);
807 DBG dumpbuf("src", bsrc, dx);
808 DBG dumpbuf("mask", bmask, dx);
809 DBG dumpbuf("dst", bdst, dx);
810 bdst = calc(bdst, bsrc, bmask, dx, isgrey, op);
811 wrdst(&z->dpar, z->dpar.bytermin+dsty*z->dpar.bwidth, bdst);
820 alphacalc0(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
826 memset(bdst.rgba, 0, dx*bdst.delta);
831 * Do the channels in the buffers match enough
832 * that we can do word-at-a-time operations
836 chanmatch(Buffer *bdst, Buffer *bsrc)
841 * first, r, g, b must be in the same place
844 drgb = (uchar*)bdst->rgba;
845 srgb = (uchar*)bsrc->rgba;
846 if(bdst->red - drgb != bsrc->red - srgb
847 || bdst->blu - drgb != bsrc->blu - srgb
848 || bdst->grn - drgb != bsrc->grn - srgb)
852 * that implies alpha is in the same place,
853 * if it is there at all (it might be == &ones).
854 * if the destination is &ones, we can scribble
855 * over the rgba slot just fine.
857 if(bdst->alpha == &ones)
861 * if the destination is not ones but the src is,
862 * then the simultaneous calculation will use
863 * bogus bytes from the src's rgba. no good.
865 if(bsrc->alpha == &ones)
869 * otherwise, alphas are in the same place.
875 alphacalc14(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
883 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
884 q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
889 fd = CALC11(sa, ma, t);
894 *bdst.grey = CALC11(fd, *bdst.grey, t);
895 bsrc.grey += bsrc.delta;
896 bdst.grey += bdst.delta;
899 *bdst.rgba = CALC41(fd, *bdst.rgba, t, t1);
902 bsrc.alpha += sadelta;
903 bmask.alpha += bmask.delta;
906 *bdst.red = CALC11(fd, *bdst.red, t);
907 *bdst.grn = CALC11(fd, *bdst.grn, t);
908 *bdst.blu = CALC11(fd, *bdst.blu, t);
909 bsrc.red += bsrc.delta;
910 bsrc.blu += bsrc.delta;
911 bsrc.grn += bsrc.delta;
912 bdst.red += bdst.delta;
913 bdst.blu += bdst.delta;
914 bdst.grn += bdst.delta;
916 if(bdst.alpha != &ones){
917 *bdst.alpha = CALC11(fd, *bdst.alpha, t);
918 bdst.alpha += bdst.delta;
920 bmask.alpha += bmask.delta;
921 bsrc.alpha += sadelta;
927 alphacalc2810(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
935 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
936 q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
945 fs = CALC11(fs, da, t);
948 *bdst.grey = CALC11(fs, *bsrc.grey, t);
949 bsrc.grey += bsrc.delta;
950 bdst.grey += bdst.delta;
953 *bdst.rgba = CALC41(fs, *bsrc.rgba, t, t1);
956 bmask.alpha += bmask.delta;
957 bdst.alpha += bdst.delta;
960 *bdst.red = CALC11(fs, *bsrc.red, t);
961 *bdst.grn = CALC11(fs, *bsrc.grn, t);
962 *bdst.blu = CALC11(fs, *bsrc.blu, t);
963 bsrc.red += bsrc.delta;
964 bsrc.blu += bsrc.delta;
965 bsrc.grn += bsrc.delta;
966 bdst.red += bdst.delta;
967 bdst.blu += bdst.delta;
968 bdst.grn += bdst.delta;
970 if(bdst.alpha != &ones){
971 *bdst.alpha = CALC11(fs, *bsrc.alpha, t);
972 bdst.alpha += bdst.delta;
974 bmask.alpha += bmask.delta;
975 bsrc.alpha += sadelta;
981 alphacalc3679(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
985 int i, sa, ma, da, q;
989 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
990 q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
997 fs = CALC11(ma, da, t);
999 fs = CALC11(ma, 255-da, t);
1003 fd = CALC11(sa, ma, t);
1009 *bdst.grey = CALC12(fs, *bsrc.grey, fd, *bdst.grey, t);
1010 bsrc.grey += bsrc.delta;
1011 bdst.grey += bdst.delta;
1014 *bdst.rgba = CALC42(fs, *bsrc.rgba, fd, *bdst.rgba, t, t1);
1017 bsrc.alpha += sadelta;
1018 bmask.alpha += bmask.delta;
1019 bdst.alpha += bdst.delta;
1022 *bdst.red = CALC12(fs, *bsrc.red, fd, *bdst.red, t);
1023 *bdst.grn = CALC12(fs, *bsrc.grn, fd, *bdst.grn, t);
1024 *bdst.blu = CALC12(fs, *bsrc.blu, fd, *bdst.blu, t);
1025 bsrc.red += bsrc.delta;
1026 bsrc.blu += bsrc.delta;
1027 bsrc.grn += bsrc.delta;
1028 bdst.red += bdst.delta;
1029 bdst.blu += bdst.delta;
1030 bdst.grn += bdst.delta;
1032 if(bdst.alpha != &ones){
1033 *bdst.alpha = CALC12(fs, sa, fd, da, t);
1034 bdst.alpha += bdst.delta;
1036 bmask.alpha += bmask.delta;
1037 bsrc.alpha += sadelta;
1043 alphacalc5(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
1054 alphacalc11(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1063 sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
1064 q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
1066 for(i=0; i<dx; i++){
1069 fd = 255-CALC11(sa, ma, t);
1072 *bdst.grey = CALC12(ma, *bsrc.grey, fd, *bdst.grey, t);
1073 bsrc.grey += bsrc.delta;
1074 bdst.grey += bdst.delta;
1077 *bdst.rgba = CALC42(ma, *bsrc.rgba, fd, *bdst.rgba, t, t1);
1080 bsrc.alpha += sadelta;
1081 bmask.alpha += bmask.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 = CALC12(ma, sa, fd, *bdst.alpha, t);
1096 bdst.alpha += bdst.delta;
1098 bmask.alpha += bmask.delta;
1099 bsrc.alpha += sadelta;
1106 source and mask alpha 1
1108 alphacalcS0(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1115 if(bsrc.delta == bdst.delta){
1116 memmove(bdst.rgba, bsrc.rgba, dx*bdst.delta);
1119 for(i=0; i<dx; i++){
1121 *bdst.grey = *bsrc.grey;
1122 bsrc.grey += bsrc.delta;
1123 bdst.grey += bdst.delta;
1125 *bdst.red = *bsrc.red;
1126 *bdst.grn = *bsrc.grn;
1127 *bdst.blu = *bsrc.blu;
1128 bsrc.red += bsrc.delta;
1129 bsrc.blu += bsrc.delta;
1130 bsrc.grn += bsrc.delta;
1131 bdst.red += bdst.delta;
1132 bdst.blu += bdst.delta;
1133 bdst.grn += bdst.delta;
1135 if(bdst.alpha != &ones){
1137 bdst.alpha += bdst.delta;
1144 /* source alpha 1 */
1146 alphacalcS(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1156 for(i=0; i<dx; i++){
1161 *bdst.grey = CALC12(ma, *bsrc.grey, fd, *bdst.grey, t);
1162 bsrc.grey += bsrc.delta;
1163 bdst.grey += bdst.delta;
1165 *bdst.red = CALC12(ma, *bsrc.red, fd, *bdst.red, t);
1166 *bdst.grn = CALC12(ma, *bsrc.grn, fd, *bdst.grn, t);
1167 *bdst.blu = CALC12(ma, *bsrc.blu, fd, *bdst.blu, t);
1168 bsrc.red += bsrc.delta;
1169 bsrc.blu += bsrc.delta;
1170 bsrc.grn += bsrc.delta;
1171 bdst.red += bdst.delta;
1172 bdst.blu += bdst.delta;
1173 bdst.grn += bdst.delta;
1175 if(bdst.alpha != &ones){
1176 *bdst.alpha = ma+CALC11(fd, *bdst.alpha, t);
1177 bdst.alpha += bdst.delta;
1179 bmask.alpha += bmask.delta;
1185 boolcalc14(Buffer bdst, Buffer b1, Buffer bmask, int dx, int grey, int op)
1194 for(i=0; i<dx; i++){
1196 zero = ma ? op == DoutS : op == DinS;
1201 bdst.grey += bdst.delta;
1204 *bdst.red = *bdst.grn = *bdst.blu = 0;
1205 bdst.red += bdst.delta;
1206 bdst.blu += bdst.delta;
1207 bdst.grn += bdst.delta;
1209 bmask.alpha += bmask.delta;
1210 if(bdst.alpha != &ones){
1213 bdst.alpha += bdst.delta;
1220 boolcalc236789(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1224 int i, ma, da, zero;
1230 for(i=0; i<dx; i++){
1242 *bdst.grey = CALC12(fs, *bsrc.grey, fd, *bdst.grey, t);
1245 bsrc.grey += bsrc.delta;
1246 bdst.grey += bdst.delta;
1249 *bdst.red = CALC12(fs, *bsrc.red, fd, *bdst.red, t);
1250 *bdst.grn = CALC12(fs, *bsrc.grn, fd, *bdst.grn, t);
1251 *bdst.blu = CALC12(fs, *bsrc.blu, fd, *bdst.blu, t);
1254 *bdst.red = *bdst.grn = *bdst.blu = 0;
1255 bsrc.red += bsrc.delta;
1256 bsrc.blu += bsrc.delta;
1257 bsrc.grn += bsrc.delta;
1258 bdst.red += bdst.delta;
1259 bdst.blu += bdst.delta;
1260 bdst.grn += bdst.delta;
1262 bmask.alpha += bmask.delta;
1263 if(bdst.alpha != &ones){
1265 *bdst.alpha = fs+CALC11(fd, da, t);
1268 bdst.alpha += bdst.delta;
1275 boolcalc1011(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
1283 for(i=0; i<dx; i++){
1288 *bdst.grey = *bsrc.grey;
1291 bsrc.grey += bsrc.delta;
1292 bdst.grey += bdst.delta;
1295 *bdst.red = *bsrc.red;
1296 *bdst.grn = *bsrc.grn;
1297 *bdst.blu = *bsrc.blu;
1300 *bdst.red = *bdst.grn = *bdst.blu = 0;
1301 bsrc.red += bsrc.delta;
1302 bsrc.blu += bsrc.delta;
1303 bsrc.grn += bsrc.delta;
1304 bdst.red += bdst.delta;
1305 bdst.blu += bdst.delta;
1306 bdst.grn += bdst.delta;
1308 bmask.alpha += bmask.delta;
1309 if(bdst.alpha != &ones){
1314 bdst.alpha += bdst.delta;
1320 * Replicated cached scan line read. Call the function listed in the Param,
1321 * but cache the result so that for replicated images we only do the work once.
1324 replread(Param *p, uchar *s, int y)
1330 if((p->bfilled & (1<<y)) == 0){
1332 *b = p->replcall(p, p->bufbase+y*p->bufdelta, y);
1338 * Alpha reading function that simply relabels the grey pointer.
1341 greymaskread(Param *p, uchar *buf, int y)
1345 b = p->greymaskcall(p, buf, y);
1352 readnbit(Param *p, uchar *buf, int y)
1356 uchar *repl, *r, *w, *ow, bits;
1357 int i, n, sh, depth, x, dx, npack, nbits;
1359 b.rgba = (ulong*)buf;
1361 b.red = b.blu = b.grn = w;
1368 repl = &replbit[depth][0];
1372 /* copy from p->r.min.x until end of repl rectangle */
1375 if(n > p->img->r.max.x - x)
1376 n = p->img->r.max.x - x;
1378 r = p->bytermin + y*p->bwidth;
1379 DBG print("readnbit dx %d %p=%p+%d*%d, *r=%d fetch %d ", dx, r, p->bytermin, y, p->bwidth, *r, n);
1383 DBG print("throwaway %d...", i);
1389 DBG print("(%.2ux)...", *r);
1393 *w++ = repl[bits>>sh];
1394 DBG print("bit %x...", repl[bits>>sh]);
1402 assert(x+i == p->img->r.max.x);
1404 /* copy from beginning of repl rectangle until where we were before. */
1405 x = p->img->r.min.x;
1407 if(n > p->r.min.x - x)
1410 r = p->bytey0s + y*p->bwidth;
1411 DBG print("x=%d r=%p...", x, r);
1418 DBG print("nbits=%d...", nbits);
1424 *w++ = repl[bits>>sh];
1425 DBG print("bit %x...", repl[bits>>sh]);
1428 DBG print("bits %x nbits %d...", bits, nbits);
1435 /* now we have exactly one full scan line: just replicate the buffer itself until we are done */
1446 writenbit(Param *p, uchar *w, Buffer src)
1450 int i, sh, depth, npack, nbits, x, ex;
1452 assert(src.grey != nil && src.delta == 1);
1456 depth = p->img->depth;
1460 bits = i ? (*w >> (8-depth*i)) : 0;
1467 DBG print(" %x", *r);
1468 bits |= (*r++ >> sh);
1479 bits |= *w & ((1<<sh)-1);
1488 readcmap(Param *p, uchar *buf, int y)
1491 int a, convgrey, copyalpha, dx, i, m;
1492 uchar *q, *cmap, *begin, *end, *r, *w;
1494 begin = p->bytey0s + y*p->bwidth;
1495 r = p->bytermin + y*p->bwidth;
1496 end = p->bytey0e + y*p->bwidth;
1497 cmap = p->img->cmap->cmap2rgb;
1498 convgrey = p->convgrey;
1499 copyalpha = (p->img->flags&Falpha) != 0;
1505 a = p->img->shift[CAlpha]/8;
1506 m = p->img->shift[CMap]/8;
1507 for(i=0; i<dx; i++){
1514 *w++ = RGB2K(q[0], q[1], q[2]);
1516 *w++ = q[2]; /* blue */
1517 *w++ = q[1]; /* green */
1518 *w++ = q[0]; /* red */
1523 for(i=0; i<dx; i++){
1528 *w++ = RGB2K(q[0], q[1], q[2]);
1530 *w++ = q[2]; /* blue */
1531 *w++ = q[1]; /* green */
1532 *w++ = q[0]; /* red */
1537 b.rgba = (ulong*)(buf-copyalpha);
1541 b.red = b.blu = b.grn = buf;
1542 b.delta = 1+copyalpha;
1548 b.delta = 3+copyalpha;
1554 writecmap(Param *p, uchar *w, Buffer src)
1556 uchar *cmap, *red, *grn, *blu, *alpha;
1557 int i, dx, delta, a, m;
1559 cmap = p->img->cmap->rgb2cmap;
1567 if(p->img->flags&Falpha){
1569 m = p->img->shift[CMap]/8;
1570 a = p->img->shift[CAlpha]/8;
1571 for(i=0; i<dx; i++, red+=delta, grn+=delta, blu+=delta, w+=2){
1575 w[m] = cmap[(*red>>4)*256+(*grn>>4)*16+(*blu>>4)];
1578 for(i=0; i<dx; i++, red+=delta, grn+=delta, blu+=delta)
1579 *w++ = cmap[(*red>>4)*256+(*grn>>4)*16+(*blu>>4)];
1585 readbyte(Param *p, uchar *buf, int y)
1589 int dx, isgrey, convgrey, alphaonly, copyalpha, i, nb;
1590 uchar *begin, *end, *r, *w, *rrepl, *grepl, *brepl, *arepl, *krepl;
1591 uchar ured, ugrn, ublu;
1595 begin = p->bytey0s + y*p->bwidth;
1596 r = p->bytermin + y*p->bwidth;
1597 end = p->bytey0e + y*p->bwidth;
1603 convgrey = p->convgrey; /* convert rgb to grey */
1604 isgrey = img->flags&Fgrey;
1605 alphaonly = p->alphaonly;
1606 copyalpha = (img->flags&Falpha) != 0;
1608 DBG print("copyalpha %d alphaonly %d convgrey %d isgrey %d\n", copyalpha, alphaonly, convgrey, isgrey);
1609 /* if we can, avoid processing everything */
1610 if(!(img->flags&Frepl) && !convgrey && (img->flags&Fbytes)){
1611 memset(&b, 0, sizeof b);
1613 memmove(buf, r, dx*nb);
1618 b.alpha = r+img->shift[CAlpha]/8;
1622 b.grey = r+img->shift[CGrey]/8;
1623 b.red = b.grn = b.blu = b.grey;
1625 b.red = r+img->shift[CRed]/8;
1626 b.grn = r+img->shift[CGreen]/8;
1627 b.blu = r+img->shift[CBlue]/8;
1634 rrepl = replbit[img->nbits[CRed]];
1635 grepl = replbit[img->nbits[CGreen]];
1636 brepl = replbit[img->nbits[CBlue]];
1637 arepl = replbit[img->nbits[CAlpha]];
1638 krepl = replbit[img->nbits[CGrey]];
1640 for(i=0; i<dx; i++){
1641 u = r[0] | (r[1]<<8) | (r[2]<<16) | (r[3]<<24);
1643 *w++ = arepl[(u>>img->shift[CAlpha]) & img->mask[CAlpha]];
1644 DBG print("a %x\n", w[-1]);
1648 *w++ = krepl[(u >> img->shift[CGrey]) & img->mask[CGrey]];
1649 else if(!alphaonly){
1650 ured = rrepl[(u >> img->shift[CRed]) & img->mask[CRed]];
1651 ugrn = grepl[(u >> img->shift[CGreen]) & img->mask[CGreen]];
1652 ublu = brepl[(u >> img->shift[CBlue]) & img->mask[CBlue]];
1654 DBG print("g %x %x %x\n", ured, ugrn, ublu);
1655 *w++ = RGB2K(ured, ugrn, ublu);
1656 DBG print("%x\n", w[-1]);
1669 b.alpha = copyalpha ? buf : &ones;
1670 b.rgba = (ulong*)buf;
1672 b.red = b.grn = b.blu = b.grey = nil;
1676 }else if(isgrey || convgrey){
1677 b.grey = buf+copyalpha;
1678 b.red = b.grn = b.blu = buf+copyalpha;
1679 b.delta = copyalpha+1;
1680 DBG print("alpha %x grey %x\n", *b.alpha, *b.grey);
1682 b.blu = buf+copyalpha;
1683 b.grn = buf+copyalpha+1;
1685 b.red = buf+copyalpha+2;
1686 b.delta = copyalpha+3;
1694 writebyte(Param *p, uchar *w, Buffer src)
1697 int i, isalpha, isgrey, nb, delta, dx, adelta;
1698 uchar *red, *grn, *blu, *grey, *alpha;
1713 isalpha = img->flags&Falpha;
1714 isgrey = img->flags&Fgrey;
1717 if(isalpha && alpha == &ones)
1720 if((img->flags&Fbytes) != 0){
1721 int ogry, ored, ogrn, oblu, oalp;
1723 ogry = img->shift[CGrey]/8;
1724 ored = img->shift[CRed]/8;
1725 ogrn = img->shift[CGreen]/8;
1726 oblu = img->shift[CBlue]/8;
1727 oalp = img->shift[CAlpha]/8;
1729 for(i=0; i<dx; i++){
1750 mask = (nb==4) ? 0 : ~((1<<img->depth)-1);
1751 for(i=0; i<dx; i++){
1752 u = w[0] | (w[1]<<8) | (w[2]<<16) | (w[3]<<24);
1753 DBG print("u %.8lux...", u);
1755 DBG print("&mask %.8lux...", u);
1757 u |= ((*grey >> (8-img->nbits[CGrey])) & img->mask[CGrey]) << img->shift[CGrey];
1758 DBG print("|grey %.8lux...", u);
1761 u |= ((*red >> (8-img->nbits[CRed])) & img->mask[CRed]) << img->shift[CRed];
1762 u |= ((*grn >> (8-img->nbits[CGreen])) & img->mask[CGreen]) << img->shift[CGreen];
1763 u |= ((*blu >> (8-img->nbits[CBlue])) & img->mask[CBlue]) << img->shift[CBlue];
1767 DBG print("|rgb %.8lux...", u);
1771 u |= ((*alpha >> (8-img->nbits[CAlpha])) & img->mask[CAlpha]) << img->shift[CAlpha];
1773 DBG print("|alpha %.8lux...", u);
1786 readfn(Memimage *img)
1790 if(img->nbits[CMap] == 8)
1796 readalphafn(Memimage *m)
1803 writefn(Memimage *img)
1807 if(img->nbits[CMap] == 8)
1813 nullwrite(Param *p, uchar *s, Buffer b)
1821 readptr(Param *p, uchar *s, int y)
1827 q = p->bytermin + y*p->bwidth;
1828 b.red = q; /* ptr to data */
1829 b.grn = b.blu = b.grey = nil;
1832 b.delta = p->img->depth/8;
1837 boolmemmove(Buffer bdst, Buffer bsrc, Buffer b1, int dx, int i, int o)
1843 memmove(bdst.red, bsrc.red, dx*bdst.delta);
1848 boolcopy8(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1850 uchar *m, *r, *w, *ew;
1858 for(; w < ew; w++,r++)
1861 return bdst; /* not used */
1865 boolcopy16(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1873 w = (ushort*)bdst.red;
1874 r = (ushort*)bsrc.red;
1876 for(; w < ew; w++,r++)
1879 return bdst; /* not used */
1883 boolcopy24(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1904 return bdst; /* not used */
1908 boolcopy32(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
1916 w = (ulong*)bdst.red;
1917 r = (ulong*)bsrc.red;
1919 for(; w < ew; w++,r++)
1922 return bdst; /* not used */
1926 genconv(Param *p, uchar *buf, int y)
1932 /* read from source into RGB format in convbuf */
1933 b = p->convreadcall(p, p->convbuf, y);
1935 /* write RGB format into dst format in buf */
1936 p->convwritecall(p->convdpar, buf, b);
1939 nb = p->convdpar->img->depth/8;
1942 ew = buf+nb*p->convdx;
1948 b.blu = b.grn = b.grey = nil;
1950 b.rgba = (ulong*)buf;
1957 convfn(Memimage *dst, Param *dpar, Memimage *src, Param *spar, int *ndrawbuf)
1959 if(dst->chan == src->chan && !(src->flags&Frepl)){
1960 //if(drawdebug) iprint("readptr...");
1964 if(dst->chan==CMAP8 && (src->chan==GREY1||src->chan==GREY2||src->chan==GREY4)){
1965 /* cheat because we know the replicated value is exactly the color map entry. */
1966 //if(drawdebug) iprint("Readnbit...");
1970 spar->convreadcall = readfn(src);
1971 spar->convwritecall = writefn(dst);
1972 spar->convdpar = dpar;
1974 /* allocate a conversion buffer */
1975 spar->convbufoff = *ndrawbuf;
1976 *ndrawbuf += spar->dx*4;
1978 if(spar->dx > Dx(spar->img->r)){
1979 spar->convdx = spar->dx;
1980 spar->dx = Dx(spar->img->r);
1983 //if(drawdebug) iprint("genconv...");
1988 pixelbits(Memimage *i, Point pt)
1992 int off, bpp, npack;
1995 p = byteaddr(i, pt);
1996 switch(bpp=i->depth){
2002 val = p[0] >> bpp*(npack-1-off);
2009 val = p[0]|(p[1]<<8);
2012 val = p[0]|(p[1]<<8)|(p[2]<<16);
2015 val = p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24);
2026 boolcopyfn(Memimage *img, Memimage *mask)
2028 if(mask->flags&Frepl && Dx(mask->r)==1 && Dy(mask->r)==1 && pixelbits(mask, mask->r.min)==~0)
2041 assert(0 /* boolcopyfn */);
2047 * Optimized draw for filling and scrolling; uses memset and memmove.
2050 memsets(void *vp, ushort val, int n)
2055 /* make little endian */
2067 memsetl(void *vp, ulong val, int n)
2072 /* make little endian */
2086 memset24(void *vp, ulong val, int n)
2106 imgtorgba(Memimage *img, ulong val)
2114 r = g = b = 0xAA; /* garbage */
2115 for(chan=img->chan; chan; chan>>=8){
2117 ov = v = val&((1<<nb)-1);
2143 p = img->cmap->cmap2rgb+3*ov;
2150 return (r<<24)|(g<<16)|(b<<8)|a;
2154 rgbatoimg(Memimage *img, ulong rgba)
2159 uchar *p, r, g, b, a, m;
2167 for(chan=img->chan; chan; chan>>=8){
2171 v |= (r>>(8-nb))<<d;
2174 v |= (g>>(8-nb))<<d;
2177 v |= (b>>(8-nb))<<d;
2180 v |= (a>>(8-nb))<<d;
2183 p = img->cmap->rgb2cmap;
2184 m = p[(r>>4)*256+(g>>4)*16+(b>>4)];
2185 v |= (m>>(8-nb))<<d;
2189 v |= (m>>(8-nb))<<d;
2194 // print("rgba2img %.8lux = %.*lux\n", rgba, 2*d/8, v);
2200 memoptdraw(Memdrawparam *par)
2202 int m, y, dy, dx, op;
2213 DBG print("state %lux mval %lux dd %d\n", par->state, par->mval, dst->depth);
2215 * If we have an opaque mask and source is one opaque pixel we can convert to the
2216 * destination format and just replicate with memset.
2218 m = Simplesrc|Simplemask|Fullmask;
2219 if((par->state&m)==m && (par->srgba&0xFF) == 0xFF && (op ==S || op == SoverD)){
2220 int d, dwid, ppb, np, nb;
2223 DBG print("memopt, dst %p, dst->data->bdata %p\n", dst, dst->data->bdata);
2224 dwid = dst->width*sizeof(ulong);
2225 dp = byteaddr(dst, par->r.min);
2227 DBG print("sdval %lud, depth %d\n", v, dst->depth);
2232 for(d=dst->depth; d<8; d*=2)
2234 ppb = 8/dst->depth; /* pixels per byte */
2237 np = par->r.min.x&m; /* no. pixels unused on left side of word */
2239 nb = 8 - np * dst->depth; /* no. bits used on right side of word */
2241 DBG print("np %d x %d nb %d lm %ux ppb %d m %ux\n", np, par->r.min.x, nb, lm, ppb, m);
2244 np = par->r.max.x&m; /* no. pixels used on left side of word */
2246 nb = 8 - np * dst->depth; /* no. bits unused on right side of word */
2248 DBG print("np %d x %d nb %d rm %ux ppb %d m %ux\n", np, par->r.max.x, nb, rm, ppb, m);
2250 DBG print("dx %d Dx %d\n", dx, Dx(par->r));
2251 /* lm, rm are masks that are 1 where we should touch the bits */
2252 if(dx < 0){ /* just one byte */
2254 for(y=0; y<dy; y++, dp+=dwid)
2255 *dp ^= (v ^ *dp) & lm;
2256 }else if(dx == 0){ /* no full bytes */
2260 for(y=0; y<dy; y++, dp+=dwid){
2262 DBG print("dp %p v %lux lm %ux (v ^ *dp) & lm %lux\n", dp, v, lm, (v^*dp)&lm);
2263 *dp ^= (v ^ *dp) & lm;
2266 *dp ^= (v ^ *dp) & rm;
2268 }else{ /* full bytes in middle */
2274 for(y=0; y<dy; y++, dp+=dwid){
2276 *dp ^= (v ^ *dp) & lm;
2281 *dp ^= (v ^ *dp) & rm;
2286 for(y=0; y<dy; y++, dp+=dwid)
2290 for(y=0; y<dy; y++, dp+=dwid)
2294 for(y=0; y<dy; y++, dp+=dwid)
2295 memset24(dp, v, dx);
2298 for(y=0; y<dy; y++, dp+=dwid)
2302 assert(0 /* bad dest depth in memoptdraw */);
2307 * If no source alpha, an opaque mask, we can just copy the
2308 * source onto the destination. If the channels are the same and
2309 * the source is not replicated, memmove suffices.
2311 m = Simplemask|Fullmask;
2312 if((par->state&(m|Replsrc))==m && src->depth >= 8
2313 && src->chan == dst->chan && !(src->flags&Falpha) && (op == S || op == SoverD)){
2315 long swid, dwid, nb;
2318 if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min))
2323 swid = src->width*sizeof(ulong);
2324 dwid = dst->width*sizeof(ulong);
2325 sp = byteaddr(src, par->sr.min);
2326 dp = byteaddr(dst, par->r.min);
2333 nb = (dx*src->depth)/8;
2334 for(y=0; y<dy; y++, sp+=swid, dp+=dwid)
2335 memmove(dp, sp, nb);
2340 * If we have a 1-bit mask, 1-bit source, and 1-bit destination, and
2341 * they're all bit aligned, we can just use bit operators. This happens
2342 * when we're manipulating boolean masks, e.g. in the arc code.
2344 if((par->state&(Simplemask|Simplesrc|Replmask|Replsrc))==0
2345 && dst->chan==GREY1 && src->chan==GREY1 && par->mask->chan==GREY1
2346 && (par->r.min.x&7)==(par->sr.min.x&7) && (par->r.min.x&7)==(par->mr.min.x&7)){
2347 uchar *sp, *dp, *mp;
2349 long swid, dwid, mwid;
2352 sp = byteaddr(src, par->sr.min);
2353 dp = byteaddr(dst, par->r.min);
2354 mp = byteaddr(par->mask, par->mr.min);
2355 swid = src->width*sizeof(ulong);
2356 dwid = dst->width*sizeof(ulong);
2357 mwid = par->mask->width*sizeof(ulong);
2359 if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min)){
2364 lm = 0xFF>>(par->r.min.x&7);
2365 rm = 0xFF<<(8-(par->r.max.x&7));
2366 dx -= (8-(par->r.min.x&7)) + (par->r.max.x&7);
2368 if(dx < 0){ /* one byte wide */
2378 for(y=0; y<dy; y++){
2379 *dp ^= (*dp ^ *sp) & *mp & lm;
2389 i = (lm!=0)+dx+(rm!=0);
2393 for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
2395 *dp ^= (*dp ^ *sp++) & *mp++ & lm;
2398 for(x=0; x<dx; x++){
2399 *dp ^= (*dp ^ *sp++) & *mp++;
2403 *dp ^= (*dp ^ *sp++) & *mp++ & rm;
2410 i = (lm!=0)+dx+(rm!=0);
2411 dp += dwid*(dy-1)+i-1;
2412 sp += swid*(dy-1)+i-1;
2413 mp += mwid*(dy-1)+i-1;
2417 for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
2419 *dp ^= (*dp ^ *sp--) & *mp-- & rm;
2422 for(x=0; x<dx; x++){
2423 *dp ^= (*dp ^ *sp--) & *mp--;
2427 *dp ^= (*dp ^ *sp--) & *mp-- & lm;
2439 * Boolean character drawing.
2440 * Solid opaque color through a 1-bit greyscale mask.
2444 chardraw(Memdrawparam *par)
2446 int i, ddepth, dy, dx, x, bx, ex, y, npack, bsh, depth, op;
2447 ulong bits, v, maskwid, dstwid;
2448 uchar *wp, *rp, *q, *wc;
2453 Memimage *mask, *src, *dst;
2455 if(0) if(drawdebug) iprint("chardraw? mf %lux md %d sf %lux dxs %d dys %d dd %d ddat %p sdat %p\n",
2456 par->mask->flags, par->mask->depth, par->src->flags,
2457 Dx(par->src->r), Dy(par->src->r), par->dst->depth, par->dst->data, par->src->data);
2466 if((par->state&(Replsrc|Simplesrc|Replmask)) != (Replsrc|Simplesrc)
2467 || mask->depth != 1 || src->flags&Falpha || dst->depth<8 || dst->data==src->data
2471 //if(drawdebug) iprint("chardraw...");
2473 depth = mask->depth;
2474 maskwid = mask->width*sizeof(ulong);
2475 rp = byteaddr(mask, mr.min);
2477 bsh = (mr.min.x % npack) * depth;
2479 wp = byteaddr(dst, r.min);
2480 dstwid = dst->width*sizeof(ulong);
2481 DBG print("bsh %d\n", bsh);
2485 ddepth = dst->depth;
2488 * for loop counts from bsh to bsh+dx
2490 * we want the bottom bits to be the amount
2491 * to shift the pixels down, so for n≡0 (mod 8) we want
2492 * bottom bits 7. for n≡1, 6, etc.
2493 * the bits come from -n-1.
2501 /* make little endian */
2507 //print("sp %x %x %x %x\n", sp[0], sp[1], sp[2], sp[3]);
2508 for(y=0; y<dy; y++, rp+=maskwid, wp+=dstwid){
2514 //if(drawdebug) iprint("8loop...");
2516 for(x=bx; x>ex; x--, wc++){
2520 DBG print("bits %lux sh %d...", bits, i);
2528 for(x=bx; x>ex; x--, ws++){
2532 DBG print("bits %lux sh %d...", bits, i);
2539 for(x=bx; x>ex; x--, wc+=3){
2543 DBG print("bits %lux sh %d...", bits, i);
2554 for(x=bx; x>ex; x--, wl++){
2558 DBG iprint("bits %lux sh %d...", bits, i);
2573 * Fill entire byte with replicated (if necessary) copy of source pixel,
2574 * assuming destination ldepth is >= source ldepth.
2576 * This code is just plain wrong for >8bpp.
2579 membyteval(Memimage *src)
2584 unloadmemimage(src, src->r, &uc, 1);
2586 uc <<= (src->r.min.x&(7/src->depth))*src->depth;
2588 /* pixel value is now in high part of byte. repeat throughout byte
2590 for(i=bpp; i<8; i<<=1)
2598 memfillcolor(Memimage *i, ulong val)
2606 bits = rgbatoimg(i, val);
2608 case 24: /* 24-bit images suck */
2609 for(y=i->r.min.y; y<i->r.max.y; y++)
2610 memset24(byteaddr(i, Pt(i->r.min.x, y)), bits, Dx(i->r));
2612 default: /* 1, 2, 4, 8, 16, 32 */
2613 for(d=i->depth; d<32; d*=2)
2614 bits = (bits << d) | bits;
2615 memsetl(wordaddr(i, i->r.min), bits, i->width*Dy(i->r));