--- rxvt-unicode/src/rxvtimg.C 2012/06/14 18:19:11 1.84 +++ rxvt-unicode/src/rxvtimg.C 2012/06/15 13:21:59 1.91 @@ -4,6 +4,38 @@ #if HAVE_IMG +#if 0 +struct pict +{ + Display *dpy; + Picture pic; + + operator Picture () const + { + return pic; + } + + pict () + : pic (0) + { + } + + pict (rxvt_img *img, XRenderPictFormat *format = 0) + : dpy (img->s->display->dpy) + { + XRenderPictureAttributes pa; + pa.repeat = img->repeat; + pic = XRenderCreatePicture (dpy, img->pm, format ? format : img->format, CPRepeat, &pa); + } + + ~pict () + { + if (pic) + XRenderFreePicture (dpy, pic); + } +}; +#endif + static XRenderPictFormat * find_alpha_format_for (Display *dpy, XRenderPictFormat *format) { @@ -217,7 +249,7 @@ } Picture -rxvt_img::src_picture () +rxvt_img::picture () { Display *dpy = s->display->dpy; @@ -251,7 +283,7 @@ XRenderColor rc = { c.r, c.g, c.b, c.a }; Display *dpy = s->display->dpy; - Picture src = src_picture (); + Picture src = picture (); XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); XRenderFreePicture (dpy, src); } @@ -267,7 +299,7 @@ rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat); img->alloc (); - Picture src = src_picture (); + Picture src = picture (); Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); @@ -451,7 +483,7 @@ Display *dpy = s->display->dpy; - Picture src = src_picture (); + Picture src = picture (); Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); Picture mul = create_xrender_mask (dpy, pm, True, True); @@ -478,6 +510,32 @@ delete img; } +void +rxvt_img::draw (rxvt_img *img, int op, nv mask) +{ + unshare (); + + Display *dpy = s->display->dpy; + Picture src = img->picture (); + Picture dst = picture (); + Picture mask_p = 0; + + if (mask != 1.) + { + mask_p = create_xrender_mask (dpy, img->pm, False, False); + XRenderColor mask_c = { 0, 0, 0, float_to_component (mask) }; + XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); + } + + XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h); + + XRenderFreePicture (dpy, src); + XRenderFreePicture (dpy, dst); + + if (mask_p) + XRenderFreePicture (dpy, mask_p); +} + rxvt_img * rxvt_img::clone () { @@ -500,17 +558,17 @@ rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); img->alloc (); - Picture src = src_picture (); + Picture src = picture (); Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); if (alpha) { XRenderColor rc = { 0, 0, 0, 0 }; XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles - XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); + XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, ref->w, ref->h); } else - XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); + XRenderComposite (dpy, PictOpSrc, src, None, dst, -x, -y, 0, 0, 0, 0, w, h); XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); @@ -523,8 +581,8 @@ { rxvt_img *img = clone (); - img->x += x; - img->y += y; + img->x -= x; + img->y -= y; if (w != width || h != height) { @@ -539,8 +597,10 @@ return img; } +typedef rxvt_img::nv matrix[3][3]; + static void -mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3]) +mat_invert (matrix mat, rxvt_img::nv (&inv)[3][3]) { rxvt_img::nv s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2]; rxvt_img::nv s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1]; @@ -562,7 +622,7 @@ } static rxvt_img::nv -mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y) +mat_apply (matrix mat, int i, rxvt_img::nv x, rxvt_img::nv y) { rxvt_img::nv v = mat [i][0] * x + mat [i][1] * y + mat [i][2]; rxvt_img::nv w = mat [2][0] * x + mat [2][1] * y + mat [2][2]; @@ -570,39 +630,64 @@ return v * (1. / w); } +static void +mat_mult (matrix a, matrix b, matrix r) +{ + for (int i = 0; i < 3; ++i) + for (int j = 0; j < 3; ++j) + r [i][j] = a [i][0] * b [0][j] + + a [i][1] * b [1][j] + + a [i][2] * b [2][j]; +} + +static void +mat_trans (rxvt_img::nv x, rxvt_img::nv y, matrix mat) +{ + mat [0][0] = 1; mat [0][1] = 0; mat [0][2] = x; + mat [1][0] = 0; mat [1][1] = 1; mat [1][2] = y; + mat [2][0] = 0; mat [2][1] = 0; mat [2][2] = 1; +} + rxvt_img * rxvt_img::transform (nv matrix[3][3]) { - // find new offset - int ox = mat_apply (matrix, 0, x, y); - int oy = mat_apply (matrix, 1, x, y); - // calculate new pixel bounding box coordinates - nv d [2], rmin[2], rmax[2]; + nv r[2], rmin[2], rmax[2]; for (int i = 0; i < 2; ++i) { nv v; - v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v; - v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v); - v = mat_apply (matrix, i, 0, h); min_it (rmin [i], v); max_it (rmax [i], v); - v = mat_apply (matrix, i, w, h); min_it (rmin [i], v); max_it (rmax [i], v); + + v = mat_apply (matrix, i, 0+x, 0+y); rmin [i] = rmax [i] = v; r [i] = v; + v = mat_apply (matrix, i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v); + v = mat_apply (matrix, i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); + v = mat_apply (matrix, i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); } - int dx = floor (rmin [0]); - int dy = floor (rmin [1]); + float sx = rmin [0] - x; + float sy = rmin [1] - y; - int new_width = ceil (rmax [0] - dx); - int new_height = ceil (rmax [1] - dy); + // TODO: adjust matrix for subpixel accuracy + int nx = floor (rmin [0]); + int ny = floor (rmin [1]); + + int new_width = ceil (rmax [0] - rmin [0]); + int new_height = ceil (rmax [1] - rmin [1]); + + ::matrix tr, tmp; + mat_trans (x, y, tr); + mat_mult (matrix, tr, tmp); + mat_trans (-x, -y, tr); + mat_mult (tr, tmp, matrix); - nv inv[3][3]; + ::matrix inv; mat_invert (matrix, inv); - rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat); + rxvt_img *img = new rxvt_img (s, format, nx, ny, new_width, new_height, repeat); img->alloc (); Display *dpy = s->display->dpy; - Picture src = src_picture (); + Picture src = picture (); Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); XTransform xfrm; @@ -613,7 +698,17 @@ XRenderSetPictureFilter (dpy, src, "good", 0, 0); XRenderSetPictureTransform (dpy, src, &xfrm); - XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height); + XRenderComposite (dpy, PictOpSrc, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height); +#if 1 + { + XRenderColor rc = { 65535,0,0,65535 }; + XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, new_width, new_height); + }{ + XRenderColor rc = { 0,0,0,65535 }; + XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 1, 1, new_width - 2, new_height - 2); + } + XRenderComposite (dpy, PictOpOver, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height); +#endif XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); @@ -651,18 +746,21 @@ nv c = cos (phi); nv matrix[3][3] = { +#if 0 { c, -s, cx - c * cx + s * cy }, { s, c, cy - s * cx - c * cy }, - { 0, 0, 1 } - //{ c, -s, 0 }, - //{ s, c, 0 }, - //{ 0, 0, 1 } + { 0, 0, 1 } +#else + { c, -s, 0 }, + { s, c, 0 }, + { 0, 0, 1 } +#endif }; - //move (-cx, -cy); + move (-cx, -cy); rxvt_img *img = transform (matrix); - //move ( cx, cy); - //img->move (cx, cy); + move ( cx, cy); + img->move (cx, cy); return img; } @@ -677,7 +775,7 @@ img->alloc (); Display *dpy = s->display->dpy; - Picture src = src_picture (); + Picture src = picture (); Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); int op = PictOpSrc; @@ -703,7 +801,7 @@ { rxvt_img *img2 = clone (); Display *dpy = s->display->dpy; - Picture src = img->src_picture (); + Picture src = img->picture (); Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); Picture mask = create_xrender_mask (dpy, img->pm, False, False);