8 #define RGB2K(r,g,b) ((299*((u32int)(r))+587*((u32int)(g))+114*((u32int)(b)))/1000)
11 * This program tests the 'memimagedraw' primitive stochastically.
12 * It tests the combination aspects of it thoroughly, but since the
13 * three images it uses are disjoint, it makes no check of the
14 * correct behavior when images overlap. That is, however, much
15 * easier to get right and to test.
18 void drawonepixel(Memimage*, Point, Memimage*, Point, Memimage*, Point);
20 void verifyline(void);
21 void verifyrect(void);
22 void verifyrectrepl(int, int);
23 void putpixel(Memimage *img, Point pt, u32int nv);
24 u32int rgbatopix(uchar, uchar, uchar, uchar);
26 char *dchan, *schan, *mchan;
32 int dbpp; /* bits per pixel in destination */
33 int sbpp; /* bits per pixel in src */
34 int mbpp; /* bits per pixel in mask */
35 int dpm; /* pixel mask at high part of byte, in destination */
36 int nbytes; /* in destination */
58 iprint(char *fmt, ...)
65 n = doprint(buf, buf+sizeof buf, fmt, va) - buf;
73 main(int argc, char *argv[])
80 Xrange = atoi(ARGF());
83 Yrange = atoi(ARGF());
86 niters = atoi(ARGF());
97 case 3: mchan = argv[2];
98 case 2: schan = argv[1];
99 case 1: dchan = argv[0];
103 fprint(2, "usage: dtest [dchan [schan [mchan]]]\n");
107 fmtinstall('b', numbconv); /* binary! */
109 fprint(2, "%s -x %d -y %d -s 0x%x %s %s %s\n", argv0, Xrange, Yrange, seed, dchan, schan, mchan);
112 dst = allocmemimage(Rect(0, 0, Xrange, Yrange), strtochan(dchan));
113 src = allocmemimage(Rect(0, 0, Xrange, Yrange), strtochan(schan));
114 mask = allocmemimage(Rect(0, 0, Xrange, Yrange), strtochan(mchan));
115 stmp = allocmemimage(Rect(0, 0, Xrange, Yrange), strtochan(schan));
116 mtmp = allocmemimage(Rect(0, 0, Xrange, Yrange), strtochan(mchan));
117 ones = allocmemimage(Rect(0, 0, Xrange, Yrange), strtochan(mchan));
118 /* print("chan %lux %lux %lux %lux %lux %lux\n", dst->chan, src->chan, mask->chan, stmp->chan, mtmp->chan, ones->chan); */
119 if(dst==0 || src==0 || mask==0 || mtmp==0 || ones==0) {
121 fprint(2, "dtest: allocation failed: %r\n");
124 nbytes = (4*Xrange+4)*Yrange;
125 srcbits = malloc(nbytes);
126 dstbits = malloc(nbytes);
127 maskbits = malloc(nbytes);
128 savedstbits = malloc(nbytes);
129 if(dstbits==0 || srcbits==0 || maskbits==0 || savedstbits==0)
134 dpm = 0xFF ^ (0xFF>>dbpp);
135 memset(ones->data->bdata, 0xFF, ones->width*sizeof(u32int)*Yrange);
138 fprint(2, "dtest: verify single pixel operation\n");
141 fprint(2, "dtest: verify full line non-replicated\n");
144 fprint(2, "dtest: verify full rectangle non-replicated\n");
147 fprint(2, "dtest: verify full rectangle source replicated\n");
148 verifyrectrepl(1, 0);
150 fprint(2, "dtest: verify full rectangle mask replicated\n");
151 verifyrectrepl(0, 1);
153 fprint(2, "dtest: verify full rectangle source and mask replicated\n");
154 verifyrectrepl(1, 1);
160 * Dump out an ASCII representation of an image. The label specifies
161 * a list of characters to put at various points in the picture.
164 Bprintr5g6b5(Biobuf *bio, char*, u32int v)
170 Bprint(bio, "%.2x%.2x%.2x", r,g,b);
174 Bprintr5g5b5a1(Biobuf *bio, char*, u32int v)
181 Bprint(bio, "%.2x%.2x%.2x%.2x", r,g,b,a);
185 dumpimage(char *name, Memimage *img, void *vdata, Point labelpt)
191 void (*fmt)(Biobuf*, char*, u32int);
192 int npr, x, y, nb, bpp;
202 fmt = (void(*)(Biobuf*,char*,u32int))Bprint;
206 fmt = (void(*)(Biobuf*,char*,u32int))Bprint;
211 if(img->chan == RGB16)
214 fmt = (void(*)(Biobuf*,char*,u32int))Bprint;
219 fmt = (void(*)(Biobuf*,char*,u32int))Bprint;
223 fmt = (void(*)(Biobuf*,char*,u32int))Bprint;
228 fprint(2, "bad format\n");
233 Binit(&b, 2, OWRITE);
236 Bprint(&b, "%s\t%d\tr %R clipr %R repl %d data %p *%P\n", name, r.min.x, r, img->clipr, (img->flags&Frepl) ? 1 : 0, vdata, labelpt);
237 mask = (1ULL<<bpp)-1;
238 /* for(y=r.min.y; y<r.max.y; y++){ */
239 for(y=0; y<Yrange; y++){
242 p = data+(byteaddr(img, Pt(0,y))-(uchar*)img->data->bdata);
243 Bprint(&b, "%-4d\t", y);
244 /* for(x=r.min.x; x<r.max.x; x++){ */
245 for(x=0; x<Xrange; x++){
249 if(x != 0 && (x%8)==0)
253 if(x==labelpt.x && y==labelpt.y){
262 v |= (u32int)(*p++) << nb;
266 /* print("bpp %d v %.8lux mask %.8lux nb %d\n", bpp, v, mask, nb); */
267 fmt(&b, arg, (v>>nb)&mask);
275 * Verify that the destination pixel has the specified value.
276 * The value is in the high bits of v, suitably masked, but must
277 * be extracted from the destination Memimage.
280 checkone(Point p, Point sp, Point mp)
285 delta = (uchar*)byteaddr(dst, p)-(uchar*)dst->data->bdata;
286 dp = (uchar*)dst->data->bdata+delta;
287 sdp = (uchar*)savedstbits+delta;
289 if(memcmp(dp, sdp, (dst->depth+7)/8) != 0) {
290 fprint(2, "dtest: one bad pixel drawing at dst %P from source %P mask %P\n", p, sp, mp);
291 fprint(2, " %.2ux %.2ux %.2ux %.2ux should be %.2ux %.2ux %.2ux %.2ux\n",
292 dp[0], dp[1], dp[2], dp[3], sdp[0], sdp[1], sdp[2], sdp[3]);
293 fprint(2, "addresses dst %p src %p mask %p\n", dp, byteaddr(src, sp), byteaddr(mask, mp));
294 dumpimage("src", src, src->data->bdata, sp);
295 dumpimage("mask", mask, mask->data->bdata, mp);
296 dumpimage("origdst", dst, dstbits, p);
297 dumpimage("dst", dst, dst->data->bdata, p);
298 dumpimage("gooddst", dst, savedstbits, p);
304 * Verify that the destination line has the same value as the saved line.
306 #define RECTPTS(r) (r).min.x, (r).min.y, (r).max.x, (r).max.y
308 checkline(Rectangle r, Point sp, Point mp, int y, Memimage *stmp, Memimage *mtmp)
314 dp = wordaddr(dst, Pt(0, y));
315 saved = savedstbits + y*dst->width;
317 nb = Xrange/(8/dst->depth);
319 nb = Xrange*(dst->depth/8);
320 if(memcmp(dp, saved, nb) != 0){
321 fprint(2, "dtest: bad line at y=%d; saved %p dp %p\n", y, saved, dp);
322 fprint(2, "draw dst %R src %P mask %P\n", r, sp, mp);
323 dumpimage("src", src, src->data->bdata, sp);
324 if(stmp) dumpimage("stmp", stmp, stmp->data->bdata, sp);
325 dumpimage("mask", mask, mask->data->bdata, mp);
326 if(mtmp) dumpimage("mtmp", mtmp, mtmp->data->bdata, mp);
327 dumpimage("origdst", dst, dstbits, r.min);
328 dumpimage("dst", dst, dst->data->bdata, r.min);
329 dumpimage("gooddst", dst, savedstbits, r.min);
335 * Fill the bits of an image with random data.
336 * The Memimage parameter is used only to make sure
337 * the data is well formatted: only ucbits is written.
340 fill(Memimage *img, uchar *ucbits)
344 uchar alpha, r, g, b;
347 if((img->flags&Falpha) == 0){
348 up = (ushort*)ucbits;
349 for(i=0; i<nbytes/2; i++)
350 *up++ = lrand() >> 7;
352 *(uchar*)up = lrand() >> 7;
354 data = img->data->bdata;
355 img->data->bdata = ucbits;
357 for(x=img->r.min.x; x<img->r.max.x; x++)
358 for(y=img->r.min.y; y<img->r.max.y; y++){
360 r = rand()%(alpha+1);
361 g = rand()%(alpha+1);
362 b = rand()%(alpha+1);
363 putpixel(img, Pt(x,y), rgbatopix(r,g,b,alpha));
365 img->data->bdata = data;
371 * Mask is preset; do the rest
380 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
381 memmove(src->data->bdata, srcbits, src->width*sizeof(u32int)*Yrange);
382 memmove(mask->data->bdata, maskbits, mask->width*sizeof(u32int)*Yrange);
384 dp.x = nrand(Xrange);
385 dp.y = nrand(Yrange);
387 sp.x = nrand(Xrange);
388 sp.y = nrand(Yrange);
390 mp.x = nrand(Xrange);
391 mp.y = nrand(Yrange);
393 drawonepixel(dst, dp, src, sp, mask, mp);
394 memmove(mask->data->bdata, maskbits, mask->width*sizeof(u32int)*Yrange);
395 memmove(savedstbits, dst->data->bdata, dst->width*sizeof(u32int)*Yrange);
397 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
398 memimagedraw(dst, Rect(dp.x, dp.y, dp.x+1, dp.y+1), src, sp, mask, mp, SoverD);
399 memmove(mask->data->bdata, maskbits, mask->width*sizeof(u32int)*Yrange);
401 checkone(dp, sp, mp);
410 memset(maskbits, 0, nbytes);
411 for(i=0; i<niters; i++)
415 memset(maskbits, 0xFF, nbytes);
416 for(i=0; i<niters; i++)
420 for(i=0; i<niters; i++){
421 fill(mask, maskbits);
427 * Mask is preset; do the rest
432 Point sp, mp, tp, up;
438 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
439 memmove(src->data->bdata, srcbits, src->width*sizeof(u32int)*Yrange);
440 memmove(mask->data->bdata, maskbits, mask->width*sizeof(u32int)*Yrange);
442 dr.min.x = nrand(Xrange-1);
443 dr.min.y = nrand(Yrange-1);
444 dr.max.x = dr.min.x + 1 + nrand(Xrange-1-dr.min.x);
445 dr.max.y = dr.min.y + 1;
447 sp.x = nrand(Xrange);
448 sp.y = nrand(Yrange);
450 mp.x = nrand(Xrange);
451 mp.y = nrand(Yrange);
455 for(x=dr.min.x; x<dr.max.x && tp.x<Xrange && up.x<Xrange; x++,tp.x++,up.x++)
456 memimagedraw(dst, Rect(x, dr.min.y, x+1, dr.min.y+1), src, tp, mask, up, SoverD);
457 memmove(savedstbits, dst->data->bdata, dst->width*sizeof(u32int)*Yrange);
459 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
461 memimagedraw(dst, dr, src, sp, mask, mp, SoverD);
462 checkline(dr, drawrepl(src->r, sp), drawrepl(mask->r, mp), dr.min.y, nil, nil);
471 memset(maskbits, 0xFF, nbytes);
472 for(i=0; i<niters; i++)
476 memset(maskbits, 0, nbytes);
477 for(i=0; i<niters; i++)
481 for(i=0; i<niters; i++){
482 fill(mask, maskbits);
488 * Mask is preset; do the rest
493 Point sp, mp, tp, up;
499 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
500 memmove(src->data->bdata, srcbits, src->width*sizeof(u32int)*Yrange);
501 memmove(mask->data->bdata, maskbits, mask->width*sizeof(u32int)*Yrange);
503 dr.min.x = nrand(Xrange-1);
504 dr.min.y = nrand(Yrange-1);
505 dr.max.x = dr.min.x + 1 + nrand(Xrange-1-dr.min.x);
506 dr.max.y = dr.min.y + 1 + nrand(Yrange-1-dr.min.y);
508 sp.x = nrand(Xrange);
509 sp.y = nrand(Yrange);
511 mp.x = nrand(Xrange);
512 mp.y = nrand(Yrange);
516 for(y=dr.min.y; y<dr.max.y && tp.y<Yrange && up.y<Yrange; y++,tp.y++,up.y++){
517 for(x=dr.min.x; x<dr.max.x && tp.x<Xrange && up.x<Xrange; x++,tp.x++,up.x++)
518 memimagedraw(dst, Rect(x, y, x+1, y+1), src, tp, mask, up, SoverD);
522 memmove(savedstbits, dst->data->bdata, dst->width*sizeof(u32int)*Yrange);
524 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
526 memimagedraw(dst, dr, src, sp, mask, mp, SoverD);
527 for(y=0; y<Yrange; y++)
528 checkline(dr, drawrepl(src->r, sp), drawrepl(mask->r, mp), y, nil, nil);
537 memset(maskbits, 0, nbytes);
538 for(i=0; i<niters; i++)
542 memset(maskbits, 0xFF, nbytes);
543 for(i=0; i<niters; i++)
547 for(i=0; i<niters; i++){
548 fill(mask, maskbits);
558 r.min.x = nrand(Xrange-1);
559 r.min.y = nrand(Yrange-1);
560 r.max.x = r.min.x + 1 + nrand(Xrange-1-r.min.x);
561 r.max.y = r.min.y + 1 + nrand(Yrange-1-r.min.y);
566 * Return coordinate corresponding to x withing range [minx, maxx)
569 tilexy(int minx, int maxx, int x)
573 sx = (x-minx) % (maxx-minx);
580 replicate(Memimage *i, Memimage *tmp)
585 /* choose the replication window (i->r) */
586 r.min.x = nrand(Xrange-1);
587 r.min.y = nrand(Yrange-1);
588 /* make it trivial more often than pure chance allows */
591 r.max.x = r.min.x + 2;
592 r.max.y = r.min.y + 2;
593 if(r.max.x < Xrange && r.max.y < Yrange)
597 r.max.x = r.min.x + 1;
598 r.max.y = r.min.y + 1;
601 if(r.min.x+3 >= Xrange)
604 r.max.x = r.min.x+3 + nrand(Xrange-(r.min.x+3));
606 if(r.min.y+3 >= Yrange)
609 r.max.y = r.min.y+3 + nrand(Yrange-(r.min.y+3));
611 assert(r.min.x >= 0);
612 assert(r.max.x <= Xrange);
613 assert(r.min.y >= 0);
614 assert(r.max.y <= Yrange);
615 /* copy from i to tmp so we have just the replicated bits */
616 nb = tmp->width*sizeof(u32int)*Yrange;
617 memset(tmp->data->bdata, 0, nb);
618 memimagedraw(tmp, r, i, r.min, ones, r.min, SoverD);
619 memmove(i->data->bdata, tmp->data->bdata, nb);
620 /* i is now a non-replicated instance of the replication */
621 /* replicate it by hand through tmp */
622 memset(tmp->data->bdata, 0, nb);
623 x = -(tilexy(r.min.x, r.max.x, 0)-r.min.x);
624 for(; x<Xrange; x+=Dx(r)){
625 y = -(tilexy(r.min.y, r.max.y, 0)-r.min.y);
626 for(; y<Yrange; y+=Dy(r)){
627 /* set r1 to instance of tile by translation */
630 r1.max.x = r1.min.x+Dx(r);
631 r1.max.y = r1.min.y+Dy(r);
632 memimagedraw(tmp, r1, i, r.min, ones, r.min, SoverD);
637 i->clipr = randrect();
638 /* fprint(2, "replicate [[%d %d] [%d %d]] [[%d %d][%d %d]]\n", r.min.x, r.min.y, r.max.x, r.max.y, */
639 /* i->clipr.min.x, i->clipr.min.y, i->clipr.max.x, i->clipr.max.y); */
640 tmp->clipr = i->clipr;
644 * Mask is preset; do the rest
647 verifyrectmaskrepl(int srcrepl, int maskrepl)
649 Point sp, mp, tp, up;
654 /* print("verfrect %d %d\n", srcrepl, maskrepl); */
655 src->flags &= ~Frepl;
656 src->r = Rect(0, 0, Xrange, Yrange);
658 stmp->flags &= ~Frepl;
659 stmp->r = Rect(0, 0, Xrange, Yrange);
660 stmp->clipr = src->r;
661 mask->flags &= ~Frepl;
662 mask->r = Rect(0, 0, Xrange, Yrange);
663 mask->clipr = mask->r;
664 mtmp->flags &= ~Frepl;
665 mtmp->r = Rect(0, 0, Xrange, Yrange);
666 mtmp->clipr = mask->r;
671 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
672 memmove(src->data->bdata, srcbits, src->width*sizeof(u32int)*Yrange);
673 memmove(mask->data->bdata, maskbits, mask->width*sizeof(u32int)*Yrange);
676 replicate(src, stmp);
681 replicate(mask, mtmp);
688 sp.x = nrand(Xrange);
689 sp.y = nrand(Yrange);
691 mp.x = nrand(Xrange);
692 mp.y = nrand(Yrange);
694 DBG print("smalldraws\n");
695 for(tp.y=sp.y,up.y=mp.y,y=dr.min.y; y<dr.max.y && tp.y<Yrange && up.y<Yrange; y++,tp.y++,up.y++)
696 for(tp.x=sp.x,up.x=mp.x,x=dr.min.x; x<dr.max.x && tp.x<Xrange && up.x<Xrange; x++,tp.x++,up.x++)
697 memimagedraw(dst, Rect(x, y, x+1, y+1), s, tp, m, up, SoverD);
698 memmove(savedstbits, dst->data->bdata, dst->width*sizeof(u32int)*Yrange);
700 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
702 DBG print("bigdraw\n");
703 memimagedraw(dst, dr, src, sp, mask, mp, SoverD);
704 for(y=0; y<Yrange; y++)
705 checkline(dr, drawrepl(src->r, sp), drawrepl(mask->r, mp), y, srcrepl?stmp:nil, maskrepl?mtmp:nil);
709 verifyrectrepl(int srcrepl, int maskrepl)
714 memset(maskbits, 0xFF, nbytes);
715 for(i=0; i<niters; i++)
716 verifyrectmaskrepl(srcrepl, maskrepl);
719 memset(maskbits, 0, nbytes);
720 for(i=0; i<niters; i++)
721 verifyrectmaskrepl(srcrepl, maskrepl);
724 for(i=0; i<niters; i++){
725 fill(mask, maskbits);
726 verifyrectmaskrepl(srcrepl, maskrepl);
731 * Trivial draw implementation.
732 * Color values are passed around as u32ints containing ααRRGGBB
736 * Convert v, which is nhave bits wide, into its nwant bits wide equivalent.
737 * Replicates to widen the value, truncates to narrow it.
740 replbits(u32int v, int nhave, int nwant)
743 for(; nhave<nwant; nhave*=2)
746 return v & ((1<<nwant)-1);
750 * Decode a pixel into the uchar* values.
753 pixtorgba(u32int v, uchar *r, uchar *g, uchar *b, uchar *a)
762 * Convert uchar channels into u32int pixel.
765 rgbatopix(uchar r, uchar g, uchar b, uchar a)
767 return (a<<24)|(r<<16)|(g<<8)|b;
771 * Retrieve the pixel value at pt in the image.
774 getpixel(Memimage *img, Point pt)
776 uchar r, g, b, a, *p;
777 int nbits, npack, bpp;
778 u32int v, c, rbits, bits;
781 a = ~0; /* default alpha is full */
783 p = byteaddr(img, pt);
784 v = p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24);
788 * Sub-byte greyscale pixels.
790 * We want to throw away the top pt.x%npack pixels and then use the next bpp bits
791 * in the bottom byte of v. This madness is due to having big endian bits
792 * but little endian bytes.
795 v >>= 8 - bpp*(pt.x%npack+1);
797 r = g = b = replbits(v, bpp, 8);
800 * General case. We need to parse the channel descriptor and do what it says.
801 * In all channels but the color map, we replicate to 8 bits because that's the
802 * precision that all calculations are done at.
804 * In the case of the color map, we leave the bits alone, in case a color map
805 * with less than 8 bits of index is used. This is currently disallowed, so it's
809 for(c=img->chan; c; c>>=8){
811 bits = v & ((1<<nbits)-1);
812 rbits = replbits(bits, nbits, 8);
831 p = img->cmap->cmap2rgb + 3*bits;
839 fprint(2, "unknown channel type %lud\n", TYPE(c));
844 return rgbatopix(r, g, b, a);
848 * Return the greyscale equivalent of a pixel.
851 getgrey(Memimage *img, Point pt)
854 pixtorgba(getpixel(img, pt), &r, &g, &b, &a);
855 return RGB2K(r, g, b);
859 * Return the value at pt in image, if image is interpreted
860 * as a mask. This means the alpha channel if present, else
861 * the greyscale or its computed equivalent.
864 getmask(Memimage *img, Point pt)
866 if(img->flags&Falpha)
867 return getpixel(img, pt)>>24;
869 return getgrey(img, pt);
875 * Write a pixel to img at point pt.
877 * We do this by reading a 32-bit little endian
878 * value from p and then writing it back
879 * after tweaking the appropriate bits. Because
880 * the data is little endian, we don't have to worry
881 * about what the actual depth is, as long as it is
885 putpixel(Memimage *img, Point pt, u32int nv)
887 uchar r, g, b, a, *p, *q;
888 u32int c, mask, bits, v;
889 int bpp, sh, npack, nbits;
891 pixtorgba(nv, &r, &g, &b, &a);
893 p = byteaddr(img, pt);
894 v = p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24);
896 DBG print("v %.8lux...", v);
899 * Sub-byte greyscale pixels. We need to skip the leftmost pt.x%npack pixels,
900 * which is equivalent to skipping the rightmost npack - pt.x%npack - 1 pixels.
903 sh = bpp*(npack - pt.x%npack - 1);
905 DBG print("repl %lux 8 %d = %lux...", bits, bpp, replbits(bits, 8, bpp));
906 bits = replbits(bits, 8, bpp);
908 DBG print("bits %lux mask %lux sh %d...", bits, mask, sh);
911 DBG print("(%lux & %lux) | (%lux & %lux)", v, ~mask, bits, mask);
912 v = (v & ~mask) | (bits & mask);
915 * General case. We need to parse the channel descriptor again.
918 for(c=img->chan; c; c>>=8){
931 bits = RGB2K(r, g, b);
940 q = img->cmap->rgb2cmap;
941 bits = q[(r>>4)*16*16+(g>>4)*16+(b>>4)];
945 fprint(2, "unknown channel type %lud\n", TYPE(c));
949 DBG print("repl %lux 8 %d = %lux...", bits, nbits, replbits(bits, 8, nbits));
951 bits = replbits(bits, 8, nbits);
953 DBG print("bits %lux mask %lux sh %d...", bits, mask, sh);
956 v = (v & ~mask) | (bits & mask);
960 DBG print("v %.8lux\n", v);
970 drawonepixel(Memimage *dst, Point dp, Memimage *src, Point sp, Memimage *mask, Point mp)
972 uchar m, M, sr, sg, sb, sa, sk, dr, dg, db, da, dk;
974 pixtorgba(getpixel(dst, dp), &dr, &dg, &db, &da);
975 pixtorgba(getpixel(src, sp), &sr, &sg, &sb, &sa);
976 m = getmask(mask, mp);
979 DBG print("dst %x %x %x %x src %x %x %x %x m %x = ", dr,dg,db,da, sr,sg,sb,sa, m);
980 if(dst->flags&Fgrey){
982 * We need to do the conversion to grey before the alpha calculation
983 * because the draw operator does this, and we need to be operating
984 * at the same precision so we get exactly the same answers.
986 sk = RGB2K(sr, sg, sb);
987 dk = RGB2K(dr, dg, db);
988 dk = (sk*m + dk*M)/255;
990 da = (sa*m + da*M)/255;
993 * True color alpha calculation treats all channels (including alpha)
994 * the same. It might have been nice to use an array, but oh well.
996 dr = (sr*m + dr*M)/255;
997 dg = (sg*m + dg*M)/255;
998 db = (sb*m + db*M)/255;
999 da = (sa*m + da*M)/255;
1002 DBG print("%x %x %x %x\n", dr,dg,db,da);
1003 putpixel(dst, dp, rgbatopix(dr, dg, db, da));