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 = vseprint(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 fprint(2, "%s -x %d -y %d -s 0x%x %s %s %s\n", argv0, Xrange, Yrange, seed, dchan, schan, mchan);
110 dst = allocmemimage(Rect(0, 0, Xrange, Yrange), strtochan(dchan));
111 src = allocmemimage(Rect(0, 0, Xrange, Yrange), strtochan(schan));
112 mask = allocmemimage(Rect(0, 0, Xrange, Yrange), strtochan(mchan));
113 stmp = allocmemimage(Rect(0, 0, Xrange, Yrange), strtochan(schan));
114 mtmp = allocmemimage(Rect(0, 0, Xrange, Yrange), strtochan(mchan));
115 ones = allocmemimage(Rect(0, 0, Xrange, Yrange), strtochan(mchan));
116 /* print("chan %lux %lux %lux %lux %lux %lux\n", dst->chan, src->chan, mask->chan, stmp->chan, mtmp->chan, ones->chan); */
117 if(dst==0 || src==0 || mask==0 || mtmp==0 || ones==0) {
119 fprint(2, "dtest: allocation failed: %r\n");
122 nbytes = (4*Xrange+4)*Yrange;
123 srcbits = malloc(nbytes);
124 dstbits = malloc(nbytes);
125 maskbits = malloc(nbytes);
126 savedstbits = malloc(nbytes);
127 if(dstbits==0 || srcbits==0 || maskbits==0 || savedstbits==0)
132 dpm = 0xFF ^ (0xFF>>dbpp);
133 memset(ones->data->bdata, 0xFF, ones->width*sizeof(u32int)*Yrange);
136 fprint(2, "dtest: verify single pixel operation\n");
139 fprint(2, "dtest: verify full line non-replicated\n");
142 fprint(2, "dtest: verify full rectangle non-replicated\n");
145 fprint(2, "dtest: verify full rectangle source replicated\n");
146 verifyrectrepl(1, 0);
148 fprint(2, "dtest: verify full rectangle mask replicated\n");
149 verifyrectrepl(0, 1);
151 fprint(2, "dtest: verify full rectangle source and mask replicated\n");
152 verifyrectrepl(1, 1);
158 * Dump out an ASCII representation of an image. The label specifies
159 * a list of characters to put at various points in the picture.
162 Bprintr5g6b5(Biobuf *bio, char* _, u32int v)
168 Bprint(bio, "%.2x%.2x%.2x", r,g,b);
172 Bprintr5g5b5a1(Biobuf *bio, char* _, u32int v)
179 Bprint(bio, "%.2x%.2x%.2x%.2x", r,g,b,a);
183 dumpimage(char *name, Memimage *img, void *vdata, Point labelpt)
189 void (*fmt)(Biobuf*, char*, u32int);
190 int npr, x, y, nb, bpp;
200 fmt = (void(*)(Biobuf*,char*,u32int))Bprint;
204 fmt = (void(*)(Biobuf*,char*,u32int))Bprint;
209 if(img->chan == RGB16)
212 fmt = (void(*)(Biobuf*,char*,u32int))Bprint;
217 fmt = (void(*)(Biobuf*,char*,u32int))Bprint;
221 fmt = (void(*)(Biobuf*,char*,u32int))Bprint;
226 fprint(2, "bad format\n");
231 Binit(&b, 2, OWRITE);
234 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);
235 mask = (1ULL<<bpp)-1;
236 /* for(y=r.min.y; y<r.max.y; y++){ */
237 for(y=0; y<Yrange; y++){
240 p = data+(byteaddr(img, Pt(0,y))-(uchar*)img->data->bdata);
241 Bprint(&b, "%-4d\t", y);
242 /* for(x=r.min.x; x<r.max.x; x++){ */
243 for(x=0; x<Xrange; x++){
247 if(x != 0 && (x%8)==0)
251 if(x==labelpt.x && y==labelpt.y){
260 v |= (u32int)(*p++) << nb;
264 /* print("bpp %d v %.8lux mask %.8lux nb %d\n", bpp, v, mask, nb); */
265 fmt(&b, arg, (v>>nb)&mask);
273 * Verify that the destination pixel has the specified value.
274 * The value is in the high bits of v, suitably masked, but must
275 * be extracted from the destination Memimage.
278 checkone(Point p, Point sp, Point mp)
283 delta = (uchar*)byteaddr(dst, p)-(uchar*)dst->data->bdata;
284 dp = (uchar*)dst->data->bdata+delta;
285 sdp = (uchar*)savedstbits+delta;
287 if(memcmp(dp, sdp, (dst->depth+7)/8) != 0) {
288 fprint(2, "dtest: one bad pixel drawing at dst %P from source %P mask %P\n", p, sp, mp);
289 fprint(2, " %.2ux %.2ux %.2ux %.2ux should be %.2ux %.2ux %.2ux %.2ux\n",
290 dp[0], dp[1], dp[2], dp[3], sdp[0], sdp[1], sdp[2], sdp[3]);
291 fprint(2, "addresses dst %p src %p mask %p\n", dp, byteaddr(src, sp), byteaddr(mask, mp));
292 dumpimage("src", src, src->data->bdata, sp);
293 dumpimage("mask", mask, mask->data->bdata, mp);
294 dumpimage("origdst", dst, dstbits, p);
295 dumpimage("dst", dst, dst->data->bdata, p);
296 dumpimage("gooddst", dst, savedstbits, p);
302 * Verify that the destination line has the same value as the saved line.
304 #define RECTPTS(r) (r).min.x, (r).min.y, (r).max.x, (r).max.y
306 checkline(Rectangle r, Point sp, Point mp, int y, Memimage *stmp, Memimage *mtmp)
312 dp = wordaddr(dst, Pt(0, y));
313 saved = savedstbits + y*dst->width;
315 nb = Xrange/(8/dst->depth);
317 nb = Xrange*(dst->depth/8);
318 if(memcmp(dp, saved, nb) != 0){
319 fprint(2, "dtest: bad line at y=%d; saved %p dp %p\n", y, saved, dp);
320 fprint(2, "draw dst %R src %P mask %P\n", r, sp, mp);
321 dumpimage("src", src, src->data->bdata, sp);
322 if(stmp) dumpimage("stmp", stmp, stmp->data->bdata, sp);
323 dumpimage("mask", mask, mask->data->bdata, mp);
324 if(mtmp) dumpimage("mtmp", mtmp, mtmp->data->bdata, mp);
325 dumpimage("origdst", dst, dstbits, r.min);
326 dumpimage("dst", dst, dst->data->bdata, r.min);
327 dumpimage("gooddst", dst, savedstbits, r.min);
333 * Fill the bits of an image with random data.
334 * The Memimage parameter is used only to make sure
335 * the data is well formatted: only ucbits is written.
338 fill(Memimage *img, uchar *ucbits)
342 uchar alpha, r, g, b;
345 if((img->flags&Falpha) == 0){
346 up = (ushort*)ucbits;
347 for(i=0; i<nbytes/2; i++)
348 *up++ = lrand() >> 7;
350 *(uchar*)up = lrand() >> 7;
352 data = img->data->bdata;
353 img->data->bdata = ucbits;
355 for(x=img->r.min.x; x<img->r.max.x; x++)
356 for(y=img->r.min.y; y<img->r.max.y; y++){
358 r = rand()%(alpha+1);
359 g = rand()%(alpha+1);
360 b = rand()%(alpha+1);
361 putpixel(img, Pt(x,y), rgbatopix(r,g,b,alpha));
363 img->data->bdata = data;
369 * Mask is preset; do the rest
378 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
379 memmove(src->data->bdata, srcbits, src->width*sizeof(u32int)*Yrange);
380 memmove(mask->data->bdata, maskbits, mask->width*sizeof(u32int)*Yrange);
382 dp.x = nrand(Xrange);
383 dp.y = nrand(Yrange);
385 sp.x = nrand(Xrange);
386 sp.y = nrand(Yrange);
388 mp.x = nrand(Xrange);
389 mp.y = nrand(Yrange);
391 drawonepixel(dst, dp, src, sp, mask, mp);
392 memmove(mask->data->bdata, maskbits, mask->width*sizeof(u32int)*Yrange);
393 memmove(savedstbits, dst->data->bdata, dst->width*sizeof(u32int)*Yrange);
395 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
396 memimagedraw(dst, Rect(dp.x, dp.y, dp.x+1, dp.y+1), src, sp, mask, mp, SoverD);
397 memmove(mask->data->bdata, maskbits, mask->width*sizeof(u32int)*Yrange);
399 checkone(dp, sp, mp);
408 memset(maskbits, 0, nbytes);
409 for(i=0; i<niters; i++)
413 memset(maskbits, 0xFF, nbytes);
414 for(i=0; i<niters; i++)
418 for(i=0; i<niters; i++){
419 fill(mask, maskbits);
425 * Mask is preset; do the rest
430 Point sp, mp, tp, up;
436 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
437 memmove(src->data->bdata, srcbits, src->width*sizeof(u32int)*Yrange);
438 memmove(mask->data->bdata, maskbits, mask->width*sizeof(u32int)*Yrange);
440 dr.min.x = nrand(Xrange-1);
441 dr.min.y = nrand(Yrange-1);
442 dr.max.x = dr.min.x + 1 + nrand(Xrange-1-dr.min.x);
443 dr.max.y = dr.min.y + 1;
445 sp.x = nrand(Xrange);
446 sp.y = nrand(Yrange);
448 mp.x = nrand(Xrange);
449 mp.y = nrand(Yrange);
453 for(x=dr.min.x; x<dr.max.x && tp.x<Xrange && up.x<Xrange; x++,tp.x++,up.x++)
454 memimagedraw(dst, Rect(x, dr.min.y, x+1, dr.min.y+1), src, tp, mask, up, SoverD);
455 memmove(savedstbits, dst->data->bdata, dst->width*sizeof(u32int)*Yrange);
457 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
459 memimagedraw(dst, dr, src, sp, mask, mp, SoverD);
460 checkline(dr, drawrepl(src->r, sp), drawrepl(mask->r, mp), dr.min.y, nil, nil);
469 memset(maskbits, 0xFF, nbytes);
470 for(i=0; i<niters; i++)
474 memset(maskbits, 0, nbytes);
475 for(i=0; i<niters; i++)
479 for(i=0; i<niters; i++){
480 fill(mask, maskbits);
486 * Mask is preset; do the rest
491 Point sp, mp, tp, up;
497 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
498 memmove(src->data->bdata, srcbits, src->width*sizeof(u32int)*Yrange);
499 memmove(mask->data->bdata, maskbits, mask->width*sizeof(u32int)*Yrange);
501 dr.min.x = nrand(Xrange-1);
502 dr.min.y = nrand(Yrange-1);
503 dr.max.x = dr.min.x + 1 + nrand(Xrange-1-dr.min.x);
504 dr.max.y = dr.min.y + 1 + nrand(Yrange-1-dr.min.y);
506 sp.x = nrand(Xrange);
507 sp.y = nrand(Yrange);
509 mp.x = nrand(Xrange);
510 mp.y = nrand(Yrange);
514 for(y=dr.min.y; y<dr.max.y && tp.y<Yrange && up.y<Yrange; y++,tp.y++,up.y++){
515 for(x=dr.min.x; x<dr.max.x && tp.x<Xrange && up.x<Xrange; x++,tp.x++,up.x++)
516 memimagedraw(dst, Rect(x, y, x+1, y+1), src, tp, mask, up, SoverD);
520 memmove(savedstbits, dst->data->bdata, dst->width*sizeof(u32int)*Yrange);
522 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
524 memimagedraw(dst, dr, src, sp, mask, mp, SoverD);
525 for(y=0; y<Yrange; y++)
526 checkline(dr, drawrepl(src->r, sp), drawrepl(mask->r, mp), y, nil, nil);
535 memset(maskbits, 0, nbytes);
536 for(i=0; i<niters; i++)
540 memset(maskbits, 0xFF, nbytes);
541 for(i=0; i<niters; i++)
545 for(i=0; i<niters; i++){
546 fill(mask, maskbits);
556 r.min.x = nrand(Xrange-1);
557 r.min.y = nrand(Yrange-1);
558 r.max.x = r.min.x + 1 + nrand(Xrange-1-r.min.x);
559 r.max.y = r.min.y + 1 + nrand(Yrange-1-r.min.y);
564 * Return coordinate corresponding to x withing range [minx, maxx)
567 tilexy(int minx, int maxx, int x)
571 sx = (x-minx) % (maxx-minx);
578 replicate(Memimage *i, Memimage *tmp)
583 /* choose the replication window (i->r) */
584 r.min.x = nrand(Xrange-1);
585 r.min.y = nrand(Yrange-1);
586 /* make it trivial more often than pure chance allows */
589 r.max.x = r.min.x + 2;
590 r.max.y = r.min.y + 2;
591 if(r.max.x < Xrange && r.max.y < Yrange)
595 r.max.x = r.min.x + 1;
596 r.max.y = r.min.y + 1;
599 if(r.min.x+3 >= Xrange)
602 r.max.x = r.min.x+3 + nrand(Xrange-(r.min.x+3));
604 if(r.min.y+3 >= Yrange)
607 r.max.y = r.min.y+3 + nrand(Yrange-(r.min.y+3));
609 assert(r.min.x >= 0);
610 assert(r.max.x <= Xrange);
611 assert(r.min.y >= 0);
612 assert(r.max.y <= Yrange);
613 /* copy from i to tmp so we have just the replicated bits */
614 nb = tmp->width*sizeof(u32int)*Yrange;
615 memset(tmp->data->bdata, 0, nb);
616 memimagedraw(tmp, r, i, r.min, ones, r.min, SoverD);
617 memmove(i->data->bdata, tmp->data->bdata, nb);
618 /* i is now a non-replicated instance of the replication */
619 /* replicate it by hand through tmp */
620 memset(tmp->data->bdata, 0, nb);
621 x = -(tilexy(r.min.x, r.max.x, 0)-r.min.x);
622 for(; x<Xrange; x+=Dx(r)){
623 y = -(tilexy(r.min.y, r.max.y, 0)-r.min.y);
624 for(; y<Yrange; y+=Dy(r)){
625 /* set r1 to instance of tile by translation */
628 r1.max.x = r1.min.x+Dx(r);
629 r1.max.y = r1.min.y+Dy(r);
630 memimagedraw(tmp, r1, i, r.min, ones, r.min, SoverD);
635 i->clipr = randrect();
636 /* fprint(2, "replicate [[%d %d] [%d %d]] [[%d %d][%d %d]]\n", r.min.x, r.min.y, r.max.x, r.max.y, */
637 /* i->clipr.min.x, i->clipr.min.y, i->clipr.max.x, i->clipr.max.y); */
638 tmp->clipr = i->clipr;
642 * Mask is preset; do the rest
645 verifyrectmaskrepl(int srcrepl, int maskrepl)
647 Point sp, mp, tp, up;
652 /* print("verfrect %d %d\n", srcrepl, maskrepl); */
653 src->flags &= ~Frepl;
654 src->r = Rect(0, 0, Xrange, Yrange);
656 stmp->flags &= ~Frepl;
657 stmp->r = Rect(0, 0, Xrange, Yrange);
658 stmp->clipr = src->r;
659 mask->flags &= ~Frepl;
660 mask->r = Rect(0, 0, Xrange, Yrange);
661 mask->clipr = mask->r;
662 mtmp->flags &= ~Frepl;
663 mtmp->r = Rect(0, 0, Xrange, Yrange);
664 mtmp->clipr = mask->r;
669 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
670 memmove(src->data->bdata, srcbits, src->width*sizeof(u32int)*Yrange);
671 memmove(mask->data->bdata, maskbits, mask->width*sizeof(u32int)*Yrange);
674 replicate(src, stmp);
679 replicate(mask, mtmp);
686 sp.x = nrand(Xrange);
687 sp.y = nrand(Yrange);
689 mp.x = nrand(Xrange);
690 mp.y = nrand(Yrange);
692 DBG print("smalldraws\n");
693 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++)
694 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++)
695 memimagedraw(dst, Rect(x, y, x+1, y+1), s, tp, m, up, SoverD);
696 memmove(savedstbits, dst->data->bdata, dst->width*sizeof(u32int)*Yrange);
698 memmove(dst->data->bdata, dstbits, dst->width*sizeof(u32int)*Yrange);
700 DBG print("bigdraw\n");
701 memimagedraw(dst, dr, src, sp, mask, mp, SoverD);
702 for(y=0; y<Yrange; y++)
703 checkline(dr, drawrepl(src->r, sp), drawrepl(mask->r, mp), y, srcrepl?stmp:nil, maskrepl?mtmp:nil);
707 verifyrectrepl(int srcrepl, int maskrepl)
712 memset(maskbits, 0xFF, nbytes);
713 for(i=0; i<niters; i++)
714 verifyrectmaskrepl(srcrepl, maskrepl);
717 memset(maskbits, 0, nbytes);
718 for(i=0; i<niters; i++)
719 verifyrectmaskrepl(srcrepl, maskrepl);
722 for(i=0; i<niters; i++){
723 fill(mask, maskbits);
724 verifyrectmaskrepl(srcrepl, maskrepl);
729 * Trivial draw implementation.
730 * Color values are passed around as u32ints containing ααRRGGBB
734 * Convert v, which is nhave bits wide, into its nwant bits wide equivalent.
735 * Replicates to widen the value, truncates to narrow it.
738 replbits(u32int v, int nhave, int nwant)
741 for(; nhave<nwant; nhave*=2)
744 return v & ((1<<nwant)-1);
748 * Decode a pixel into the uchar* values.
751 pixtorgba(u32int v, uchar *r, uchar *g, uchar *b, uchar *a)
760 * Convert uchar channels into u32int pixel.
763 rgbatopix(uchar r, uchar g, uchar b, uchar a)
765 return (a<<24)|(r<<16)|(g<<8)|b;
769 * Retrieve the pixel value at pt in the image.
772 getpixel(Memimage *img, Point pt)
774 uchar r, g, b, a, *p;
775 int nbits, npack, bpp;
776 u32int v, c, rbits, bits;
779 a = ~0; /* default alpha is full */
781 p = byteaddr(img, pt);
782 v = p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24);
786 * Sub-byte greyscale pixels.
788 * We want to throw away the top pt.x%npack pixels and then use the next bpp bits
789 * in the bottom byte of v. This madness is due to having big endian bits
790 * but little endian bytes.
793 v >>= 8 - bpp*(pt.x%npack+1);
795 r = g = b = replbits(v, bpp, 8);
798 * General case. We need to parse the channel descriptor and do what it says.
799 * In all channels but the color map, we replicate to 8 bits because that's the
800 * precision that all calculations are done at.
802 * In the case of the color map, we leave the bits alone, in case a color map
803 * with less than 8 bits of index is used. This is currently disallowed, so it's
807 for(c=img->chan; c; c>>=8){
809 bits = v & ((1<<nbits)-1);
810 rbits = replbits(bits, nbits, 8);
829 p = img->cmap->cmap2rgb + 3*bits;
837 fprint(2, "unknown channel type %lud\n", TYPE(c));
842 return rgbatopix(r, g, b, a);
846 * Return the greyscale equivalent of a pixel.
849 getgrey(Memimage *img, Point pt)
852 pixtorgba(getpixel(img, pt), &r, &g, &b, &a);
853 return RGB2K(r, g, b);
857 * Return the value at pt in image, if image is interpreted
858 * as a mask. This means the alpha channel if present, else
859 * the greyscale or its computed equivalent.
862 getmask(Memimage *img, Point pt)
864 if(img->flags&Falpha)
865 return getpixel(img, pt)>>24;
867 return getgrey(img, pt);
873 * Write a pixel to img at point pt.
875 * We do this by reading a 32-bit little endian
876 * value from p and then writing it back
877 * after tweaking the appropriate bits. Because
878 * the data is little endian, we don't have to worry
879 * about what the actual depth is, as long as it is
883 putpixel(Memimage *img, Point pt, u32int nv)
885 uchar r, g, b, a, *p, *q;
886 u32int c, mask, bits, v;
887 int bpp, sh, npack, nbits;
889 pixtorgba(nv, &r, &g, &b, &a);
891 p = byteaddr(img, pt);
892 v = p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24);
894 DBG print("v %.8lux...", v);
897 * Sub-byte greyscale pixels. We need to skip the leftmost pt.x%npack pixels,
898 * which is equivalent to skipping the rightmost npack - pt.x%npack - 1 pixels.
901 sh = bpp*(npack - pt.x%npack - 1);
903 DBG print("repl %lux 8 %d = %lux...", bits, bpp, replbits(bits, 8, bpp));
904 bits = replbits(bits, 8, bpp);
906 DBG print("bits %lux mask %lux sh %d...", bits, mask, sh);
909 DBG print("(%lux & %lux) | (%lux & %lux)", v, ~mask, bits, mask);
910 v = (v & ~mask) | (bits & mask);
913 * General case. We need to parse the channel descriptor again.
916 for(c=img->chan; c; c>>=8){
929 bits = RGB2K(r, g, b);
938 q = img->cmap->rgb2cmap;
939 bits = q[(r>>4)*16*16+(g>>4)*16+(b>>4)];
943 fprint(2, "unknown channel type %lud\n", TYPE(c));
947 DBG print("repl %lux 8 %d = %lux...", bits, nbits, replbits(bits, 8, nbits));
949 bits = replbits(bits, 8, nbits);
951 DBG print("bits %lux mask %lux sh %d...", bits, mask, sh);
954 v = (v & ~mask) | (bits & mask);
958 DBG print("v %.8lux\n", v);
968 drawonepixel(Memimage *dst, Point dp, Memimage *src, Point sp, Memimage *mask, Point mp)
970 uchar m, M, sr, sg, sb, sa, sk, dr, dg, db, da, dk;
972 pixtorgba(getpixel(dst, dp), &dr, &dg, &db, &da);
973 pixtorgba(getpixel(src, sp), &sr, &sg, &sb, &sa);
974 m = getmask(mask, mp);
975 M = 255-(sa*m + 127)/255;
977 DBG print("dst %x %x %x %x src %x %x %x %x m %x = ", dr,dg,db,da, sr,sg,sb,sa, m);
978 if(dst->flags&Fgrey){
980 * We need to do the conversion to grey before the alpha calculation
981 * because the draw operator does this, and we need to be operating
982 * at the same precision so we get exactly the same answers.
984 sk = RGB2K(sr, sg, sb);
985 dk = RGB2K(dr, dg, db);
986 dk = (sk*m + dk*M + 127)/255;
988 da = (sa*m + da*M + 127)/255;
991 * True color alpha calculation treats all channels (including alpha)
992 * the same. It might have been nice to use an array, but oh well.
994 dr = (sr*m + dr*M + 127)/255;
995 dg = (sg*m + dg*M + 127)/255;
996 db = (sb*m + db*M + 127)/255;
997 da = (sa*m + da*M + 127)/255;
1000 DBG print("%x %x %x %x\n", dr,dg,db,da);
1001 putpixel(dst, dp, rgbatopix(dr, dg, db, da));