diff -r a18e9872164b src/9vx/libmemdraw/draw.c --- a/src/9vx/libmemdraw/draw.c Wed Dec 10 03:29:15 2008 -0800 +++ b/src/9vx/libmemdraw/draw.c Mon Mar 09 16:29:39 2009 +0100 @@ -10,22 +10,32 @@ #define RGB2K(r,g,b) ((156763*(r)+307758*(g)+59769*(b))>>19) /* - * for 0 ≤ x ≤ 255*255, (x*0x0101+0x100)>>16 is a perfect approximation. - * for 0 ≤ x < (1<<16), x/255 = ((x+1)*0x0101)>>16 is a perfect approximation. - * the last one is perfect for all up to 1<<16, avoids a multiply, but requires a rathole. + * For 16-bit values, x / 255 == (t = x+1, (t+(t>>8)) >> 8). + * We add another 127 to round to the nearest value rather + * than truncate. + * + * CALCxy does x bytewise calculations on y input images (x=1,4; y=1,2). + * CALC2x does two parallel 16-bit calculations on y input images (y=1,2). */ -/* #define DIV255(x) (((x)*257+256)>>16) */ -#define DIV255(x) ((((x)+1)*257)>>16) -/* #define DIV255(x) (tmp=(x)+1, (tmp+(tmp>>8))>>8) */ +#define CALC11(a, v, tmp) \ + (tmp=(a)*(v)+128, (tmp+(tmp>>8))>>8) +#define CALC12(a1, v1, a2, v2, tmp) \ + (tmp=(a1)*(v1)+(a2)*(v2)+128, (tmp+(tmp>>8))>>8) +#define MASK 0xFF00FF -#define MUL(x, y, t) (t = (x)*(y)+128, (t+(t>>8))>>8) -#define MASK13 0xFF00FF00 -#define MASK02 0x00FF00FF -#define MUL13(a, x, t) (t = (a)*(((x)&MASK13)>>8)+128, ((t+((t>>8)&MASK02))>>8)&MASK02) -#define MUL02(a, x, t) (t = (a)*(((x)&MASK02)>>0)+128, ((t+((t>>8)&MASK02))>>8)&MASK02) -#define MUL0123(a, x, s, t) ((MUL13(a, x, s)<<8)|MUL02(a, x, t)) +#define CALC21(a, vvuu, tmp) \ + (tmp=(a)*(vvuu)+0x00800080, ((tmp+((tmp>>8)&MASK))>>8)&MASK) -#define MUL2(u, v, x, y) (t = (u)*(v)+(x)*(y)+256, (t+(t>>8))>>8) +#define CALC41(a, rgba, tmp1, tmp2) \ + (CALC21(a, rgba & MASK, tmp1) | \ + (CALC21(a, (rgba>>8)&MASK, tmp2)<<8)) + +#define CALC22(a1, vvuu1, a2, vvuu2, tmp) \ + (tmp=(a1)*(vvuu1)+(a2)*(vvuu2)+0x00800080, ((tmp+((tmp>>8)&MASK))>>8)&MASK) + +#define CALC42(a1, rgba1, a2, rgba2, tmp1, tmp2) \ + (CALC22(a1, rgba1 & MASK, a2, rgba2 & MASK, tmp1) | \ + (CALC22(a1, (rgba1>>8) & MASK, a2, (rgba2>>8) & MASK, tmp2)<<8)) static void mktables(void); typedef int Subdraw(Memdrawparam*); @@ -803,41 +813,85 @@ return bdst; } +/* + * Do the channels in the buffers match enough + * that we can do word-at-a-time operations + * on the pixels? + */ +static int +chanmatch(Buffer *bdst, Buffer *bsrc) +{ + uchar *drgb, *srgb; + + /* + * first, r, g, b must be in the same place + * in the rgba word. + */ + drgb = (uchar*)bdst->rgba; + srgb = (uchar*)bsrc->rgba; + if(bdst->red - drgb != bsrc->red - srgb + || bdst->blu - drgb != bsrc->blu - srgb + || bdst->grn - drgb != bsrc->grn - srgb) + return 0; + + /* + * that implies alpha is in the same place, + * if it is there at all (it might be == &ones). + * if the destination is &ones, we can scribble + * over the rgba slot just fine. + */ + if(bdst->alpha == &ones) + return 1; + + /* + * if the destination is not ones but the src is, + * then the simultaneous calculation will use + * bogus bytes from the src's rgba. no good. + */ + if(bsrc->alpha == &ones) + return 0; + + /* + * otherwise, alphas are in the same place. + */ + return 1; +} + static Buffer alphacalc14(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op) { Buffer obdst; int fd, sadelta; int i, sa, ma, q; - uint32 s, t; + uint32 t, t1; obdst = bdst; sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta; - q = bsrc.delta == 4 && bdst.delta == 4; + q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc); for(i=0; ibuttons |= 2; if(s & Button3Mask) - m->buttons |= 4; + m->buttons |= s & ShiftMask ? 2 : 4; if(s & Button4Mask) m->buttons |= 8; if(s & Button5Mask)