--- rxvt-unicode/src/rxvtimg.C 2012/06/12 19:00:57 1.81 +++ rxvt-unicode/src/rxvtimg.C 2012/06/14 18:59:39 1.85 @@ -4,6 +4,58 @@ #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) +{ + if (format->direct.alphaMask) + return format; // already has alpha + + // try to find a suitable alpha format, one bit alpha is enough for our purposes + if (format->type == PictTypeDirect) + for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) + if (f->direct.alphaMask + && f->type == PictTypeDirect + && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) + && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) + && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) + return f; + + // should be a very good fallback + return XRenderFindStandardFormat (dpy, PictStandardARGB32); +} + rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), pm(0), ref(0) @@ -197,7 +249,7 @@ } Picture -rxvt_img::src_picture () +rxvt_img::picture () { Display *dpy = s->display->dpy; @@ -226,28 +278,51 @@ } void -rxvt_img::fill (const rxvt_color &c) +rxvt_img::fill (const rgba &c) { - rgba cc; - c.get (cc); - XRenderColor rc = { cc.r, cc.g, cc.b, cc.a }; + 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); } +void +rxvt_img::add_alpha () +{ + if (format->direct.alphaMask) + return; + + Display *dpy = s->display->dpy; + + rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat); + img->alloc (); + + 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); + + XRenderFreePicture (dpy, src); + XRenderFreePicture (dpy, dst); + + ::swap (img->ref, ref); + ::swap (img->pm , pm ); + + delete img; +} + static void -get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) +get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params) { - double sigma = radius / 2.0; - double scale = sqrt (2.0 * M_PI) * sigma; - double sum = 0.0; + rxvt_img::nv sigma = radius / 2.0; + rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma; + rxvt_img::nv sum = 0.0; for (int i = 0; i < width; i++) { - double x = i - width / 2; + rxvt_img::nv x = i - width / 2; kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; sum += kernel[i]; } @@ -267,7 +342,7 @@ Display *dpy = s->display->dpy; int size = max (rh, rv) * 2 + 1; - double *kernel = (double *)malloc (size * sizeof (double)); + nv *kernel = (nv *)malloc (size * sizeof (nv)); XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); img->alloc (); @@ -399,12 +474,7 @@ rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); img->alloc (); - - { - rxvt_color empty; - empty.set (s, rgba (0, 0, 0, 0)); - img->fill (empty); - } + img->fill (rgba (0, 0, 0, 0)); // premultiply (yeah, these are not exact, sue me or fix it) r = (r * (a >> 8)) >> 8; @@ -413,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); @@ -440,30 +510,36 @@ delete img; } -rxvt_img * -rxvt_img::clone () +void +rxvt_img::draw (rxvt_img *img, int op, nv mask) { - return new rxvt_img (*this); -} + unshare (); -static XRenderPictFormat * -find_alpha_format_for (Display *dpy, XRenderPictFormat *format) -{ - if (format->direct.alphaMask) - return format; // already has alpha + 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); + } - // try to find a suitable alpha format, one bit alpha is enough for our purposes - if (format->type == PictTypeDirect) - for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) - if (f->direct.alphaMask - && f->type == PictTypeDirect - && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) - && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) - && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) - return f; + XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h); - // should be a very good fallback - return XRenderFindStandardFormat (dpy, PictStandardARGB32); + XRenderFreePicture (dpy, src); + XRenderFreePicture (dpy, dst); + + if (mask) + XRenderFreePicture (dpy, mask_p); +} + +rxvt_img * +rxvt_img::clone () +{ + return new rxvt_img (*this); } rxvt_img * @@ -482,7 +558,7 @@ 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) @@ -522,13 +598,13 @@ } static void -mat_invert (double mat[3][3], double (&inv)[3][3]) +mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3]) { - double s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2]; - double s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1]; - double s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2]; + 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]; + rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2]; - double invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2); + rxvt_img::nv invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2); inv [0][0] = invdet * s0; inv [0][1] = invdet * s1; @@ -543,28 +619,28 @@ inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]); } -static double -mat_apply (double mat[3][3], int i, double x, double y) +static rxvt_img::nv +mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y) { - double v = mat [i][0] * x + mat [i][1] * y + mat [i][2]; - double w = mat [2][0] * x + mat [2][1] * y + mat [2][2]; + 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]; return v * (1. / w); } rxvt_img * -rxvt_img::transform (double matrix[3][3]) +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 - double d [2], rmin[2], rmax[2]; + nv d [2], rmin[2], rmax[2]; for (int i = 0; i < 2; ++i) { - double v; + 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); @@ -577,14 +653,14 @@ int new_width = ceil (rmax [0] - dx); int new_height = ceil (rmax [1] - dy); - double inv[3][3]; + nv inv[3][3]; 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); 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; @@ -609,10 +685,10 @@ if (w == new_width && h == new_height) return clone (); - double matrix[3][3] = { - { new_width / (double)w, 0, 0 }, - { 0, new_height / (double)h, 0 }, - { 0, 0, 1 } + nv matrix[3][3] = { + { new_width / (nv)w, 0, 0 }, + { 0, new_height / (nv)h, 0 }, + { 0, 0, 1 } }; int old_repeat_mode = repeat; @@ -627,12 +703,12 @@ } rxvt_img * -rxvt_img::rotate (int cx, int cy, double phi) +rxvt_img::rotate (int cx, int cy, nv phi) { - double s = sin (phi); - double c = cos (phi); + nv s = sin (phi); + nv c = cos (phi); - double matrix[3][3] = { + nv matrix[3][3] = { { c, -s, cx - c * cx + s * cy }, { s, c, cy - s * cx - c * cy }, { 0, 0, 1 } @@ -650,7 +726,7 @@ } rxvt_img * -rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) +rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) { if (new_format == format) return clone (); @@ -659,17 +735,14 @@ 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; if (format->direct.alphaMask && !new_format->direct.alphaMask) { // does it have to be that complicated - rgba c; - bg.get (c); - - XRenderColor rc = { c.r, c.g, c.b, 0xffff }; + XRenderColor rc = { bg.r, bg.g, bg.b, bg.a }; XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); op = PictOpOver; @@ -684,11 +757,11 @@ } rxvt_img * -rxvt_img::blend (rxvt_img *img, double factor) +rxvt_img::blend (rxvt_img *img, nv factor) { 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);