--- rxvt-unicode/src/rxvtimg.C 2012/06/06 22:01:44 1.30 +++ rxvt-unicode/src/rxvtimg.C 2012/06/17 21:58:18 1.98 @@ -1,30 +1,272 @@ +/*----------------------------------------------------------------------* + * File: rxvtimg.C + *----------------------------------------------------------------------* + * + * All portions of code are copyright by their respective author/s. + * Copyright (c) 2012 Marc Lehmann + * Copyright (c) 2012 Emanuele Giaquinta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + *---------------------------------------------------------------------*/ + +#include #include #include "../config.h" #include "rxvt.h" #if HAVE_IMG -#define float_to_component(d) ((d) * 65535.99) +typedef rxvt_img::nv nv; + +namespace +{ + struct mat3x3 + { + nv v[3][3]; + + mat3x3 () + { + } + + mat3x3 (const nv *matrix) + { + memcpy (v, matrix, sizeof (v)); + } + + mat3x3 (nv v11, nv v12, nv v13, nv v21, nv v22, nv v23, nv v31, nv v32, nv v33) + { + v[0][0] = v11; v[0][1] = v12; v[0][2] = v13; + v[1][0] = v21; v[1][1] = v22; v[1][2] = v23; + v[2][0] = v31; v[2][1] = v32; v[2][2] = v33; + } + + mat3x3 inverse (); + + nv *operator [](int i) { return &v[i][0]; } + const nv *operator [](int i) const { return &v[i][0]; } + + operator const nv * () const { return &v[0][0]; } + operator nv * () { return &v[0][0]; } + + // quite inefficient, hopefully gcc pulls the w calc out of any loops + nv apply1 (int i, nv x, nv y) + { + mat3x3 &m = *this; + + nv v = m[i][0] * x + m[i][1] * y + m[i][2]; + nv w = m[2][0] * x + m[2][1] * y + m[2][2]; + + return v * (1. / w); + } + + static mat3x3 translate (nv x, nv y); + static mat3x3 scale (nv s, nv t); + static mat3x3 rotate (nv phi); + }; + + mat3x3 + mat3x3::inverse () + { + mat3x3 &m = *this; + mat3x3 inv; + + nv s0 = m[2][2] * m[1][1] - m[2][1] * m[1][2]; + nv s1 = m[2][1] * m[0][2] - m[2][2] * m[0][1]; + nv s2 = m[1][2] * m[0][1] - m[1][1] * m[0][2]; + + nv invdet = 1. / (m[0][0] * s0 + m[1][0] * s1 + m[2][0] * s2); + + inv[0][0] = invdet * s0; + inv[0][1] = invdet * s1; + inv[0][2] = invdet * s2; + + inv[1][0] = invdet * (m[2][0] * m[1][2] - m[2][2] * m[1][0]); + inv[1][1] = invdet * (m[2][2] * m[0][0] - m[2][0] * m[0][2]); + inv[1][2] = invdet * (m[1][0] * m[0][2] - m[1][2] * m[0][0]); + + inv[2][0] = invdet * (m[2][1] * m[1][0] - m[2][0] * m[1][1]); + inv[2][1] = invdet * (m[2][0] * m[0][1] - m[2][1] * m[0][0]); + inv[2][2] = invdet * (m[1][1] * m[0][0] - m[1][0] * m[0][1]); + + return inv; + } + + static mat3x3 + operator *(const mat3x3 &a, const mat3x3 &b) + { + mat3x3 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]; + + return r; + } + + mat3x3 + mat3x3::translate (nv x, nv y) + { + return mat3x3 ( + 1, 0, x, + 0, 1, y, + 0, 0, 1 + ); + } + + mat3x3 + mat3x3::scale (nv s, nv t) + { + return mat3x3 ( + s, 0, 0, + 0, t, 0, + 0, 0, 1 + ); + } + + // clockwise + mat3x3 + mat3x3::rotate (nv phi) + { + nv s = sin (phi); + nv c = cos (phi); + + return mat3x3 ( + c, -s, 0, + s, c, 0, + 0, 0, 1 + ); + } + + struct composer + { + rxvt_img *srcimg, *dstimg; + Picture src, dst, msk; + Display *dpy; + + ecb_noinline + composer (rxvt_img *srcimg, rxvt_img *dstimg = 0) + : srcimg (srcimg), dstimg (dstimg), msk (0) + { + if (!this->dstimg) + this->dstimg = srcimg->new_empty (); + else if (!this->dstimg->pm) // somewhat unsatisfying + this->dstimg->alloc (); + + dpy = srcimg->s->dpy; + src = srcimg->picture (); + dst = this->dstimg->picture (); + } + + ecb_noinline + void mask (bool rgb = true, int x = 1, int y = 1) + { + Pixmap pixmap = XCreatePixmap (dpy, srcimg->pm, x, y, rgb ? 32 : 8); + + XRenderPictFormat *format = XRenderFindStandardFormat (dpy, rgb ? PictStandardARGB32 : PictStandardA8); + XRenderPictureAttributes pa; + pa.repeat = RepeatNormal; + pa.component_alpha = rgb; + msk = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa); -rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) -: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false) + XFreePixmap (dpy, pixmap); + + ecb_assume (msk); + } + + // CreateSolidFill creates a very very very weird picture + void mask (const rgba &c) + { + XRenderColor rc = { + c.r * c.a / 65535, + c.g * c.a / 65535, + c.b * c.a / 65535, + c.a + }; + msk = XRenderCreateSolidFill (dpy, &rc); + ecb_assume (msk); + } + + void fill (const rgba &c) + { + XRenderColor rc = { + c.r * c.a / 65535, + c.g * c.a / 65535, + c.b * c.a / 65535, + c.a + }; + + XRenderFillRectangle (dpy, PictOpSrc, msk, &rc, 0, 0, 1, 1); + } + + operator rxvt_img *() + { + return dstimg; + } + + ecb_noinline + ~composer () + { + XRenderFreePicture (dpy, src); + XRenderFreePicture (dpy, dst); + if (msk) XRenderFreePicture (dpy, msk); + } + }; +} + +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) { - pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); } -rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) -: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap) +rxvt_img::rxvt_img (const rxvt_img &img) +: s(img.s), x(img.x), y(img.y), w(img.w), h(img.h), format(img.format), repeat(img.repeat), pm(img.pm), ref(img.ref) { + ++ref->cnt; } rxvt_img * rxvt_img::new_from_root (rxvt_screen *s) { - Display *dpy = s->display->dpy; + Display *dpy = s->dpy; unsigned int root_pm_w, root_pm_h; - Pixmap root_pixmap = s->display->get_pixmap_property (s->display->xa[XA_XROOTPMAP_ID]); + Pixmap root_pixmap = s->display->get_pixmap_property (s->display->xa [XA_XROOTPMAP_ID]); if (root_pixmap == None) - root_pixmap = s->display->get_pixmap_property (s->display->xa[XA_ESETROOT_PMAP_ID]); + root_pixmap = s->display->get_pixmap_property (s->display->xa [XA_ESETROOT_PMAP_ID]); if (root_pixmap == None) return 0; @@ -39,12 +281,118 @@ rxvt_img *img = new rxvt_img ( s, XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), + 0, + 0, root_pm_w, - root_pm_h, - root_pixmap + root_pm_h ); - img->shared = true; + img->pm = root_pixmap; + img->ref = new pixref (root_pm_w, root_pm_h); + img->ref->ours = false; + + return img; +} + +# if HAVE_PIXBUF + +rxvt_img * +rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) +{ + Display *dpy = s->dpy; + + int width = gdk_pixbuf_get_width (pb); + int height = gdk_pixbuf_get_height (pb); + + if (width > 32767 || height > 32767) // well, we *could* upload in chunks + rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); + + // since we require rgb24/argb32 formats from xrender we assume + // that both 24 and 32 bpp MUST be supported by any screen that supports xrender + + int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; + + XImage xi; + + xi.width = width; + xi.height = height; + xi.xoffset = 0; + xi.format = ZPixmap; + xi.byte_order = ImageByteOrder (dpy); + xi.bitmap_unit = 0; //XY only, unused + xi.bitmap_bit_order = 0; //XY only, unused + xi.bitmap_pad = BitmapPad (dpy); + xi.depth = 32; + xi.bytes_per_line = 0; + xi.bits_per_pixel = 32; //Z only + xi.red_mask = 0x00000000; //Z only, unused + xi.green_mask = 0x00000000; //Z only, unused + xi.blue_mask = 0x00000000; //Z only, unused + xi.obdata = 0; // probably unused + + bool byte_order_mismatch = byte_order != xi.byte_order; + + if (!XInitImage (&xi)) + rxvt_fatal ("unable to initialise ximage, please report.\n"); + + if (height > INT_MAX / xi.bytes_per_line) + rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); + + xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); + + int rowstride = gdk_pixbuf_get_rowstride (pb); + bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb); + unsigned char *row = gdk_pixbuf_get_pixels (pb); + + char *line = xi.data; + + for (int y = 0; y < height; y++) + { + unsigned char *src = row; + uint32_t *dst = (uint32_t *)line; + + if (!pb_has_alpha) + for (int x = 0; x < width; x++) + { + uint8_t r = *src++; + uint8_t g = *src++; + uint8_t b = *src++; + + uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b; + + if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) + v = ecb_bswap32 (v); + + *dst++ = v; + } + else + for (int x = 0; x < width; x++) + { + uint32_t v = *(uint32_t *)src; src += 4; + + if (ecb_big_endian ()) + v = ecb_bswap32 (v); + + v = ecb_rotl32 (v, 8); // abgr to bgra + + if (!byte_order_mismatch) + v = ecb_bswap32 (v); + + *dst++ = v; + } + + row += rowstride; + line += xi.bytes_per_line; + } + + rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height); + img->alloc (); + + GC gc = XCreateGC (dpy, img->pm, 0, 0); + XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); + XFreeGC (dpy, gc); + + free (xi.data); return img; } @@ -58,60 +406,116 @@ if (!pb) rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); - rxvt_img *img = new rxvt_img ( - s, - XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), - gdk_pixbuf_get_width (pb), - gdk_pixbuf_get_height (pb) - ); - - img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); + rxvt_img *img = new_from_pixbuf (s, pb); g_object_unref (pb); return img; } +# endif + +void +rxvt_img::destroy () +{ + if (--ref->cnt) + return; + + if (pm && ref->ours) + XFreePixmap (s->dpy, pm); + + delete ref; +} + rxvt_img::~rxvt_img () { - if (!shared) - XFreePixmap (s->display->dpy, pm); + destroy (); +} + +void +rxvt_img::alloc () +{ + pm = XCreatePixmap (s->dpy, s->display->root, w, h, format->depth); + ref = new pixref (w, h); +} + +rxvt_img * +rxvt_img::new_empty () +{ + rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); + img->alloc (); + + return img; +} + +Picture +rxvt_img::picture () +{ + Display *dpy = s->dpy; + + XRenderPictureAttributes pa; + pa.repeat = repeat; + Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); + + return pic; } void rxvt_img::unshare () { - if (!shared) + if (ref->cnt == 1 && ref->ours) return; - rxvt_img *img = clone (); + Pixmap pm2 = XCreatePixmap (s->dpy, s->display->root, ref->w, ref->h, format->depth); + GC gc = XCreateGC (s->dpy, pm, 0, 0); + XCopyArea (s->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); + XFreeGC (s->dpy, gc); - ::swap (pm , img->pm); - ::swap (shared, img->shared); + destroy (); - delete img; + pm = pm2; + ref = new pixref (ref->w, ref->h); +} + +void +rxvt_img::fill (const rgba &c) +{ + XRenderColor rc = { c.r, c.g, c.b, c.a }; + + Display *dpy = s->dpy; + Picture src = picture (); + XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); + XRenderFreePicture (dpy, src); } void -rxvt_img::fill (const rxvt_color &c) +rxvt_img::add_alpha () { - XGCValues gcv; - gcv.foreground = c; - GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); - XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); - XFreeGC (s->display->dpy, gc); + if (format->direct.alphaMask) + return; + + composer cc (this, new rxvt_img (s, find_alpha_format_for (s->dpy, format), x, y, w, h, repeat)); + + XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w, h); + + rxvt_img *img = cc; + + ::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, nv *kernel, XFixed *params) { - double sigma = radius / 2.0; - double scale = sqrt (2.0 * M_PI) * sigma; - double sum = 0.0; + nv sigma = radius / 2.0; + nv scale = sqrt (2.0 * M_PI) * sigma; + nv sum = 0.0; for (int i = 0; i < width; i++) { - double x = i - width / 2; + nv x = i - width / 2; kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; sum += kernel[i]; } @@ -129,17 +533,16 @@ if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) return clone (); - Display *dpy = s->display->dpy; + Display *dpy = s->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, w, h); + rxvt_img *img = new_empty (); XRenderPictureAttributes pa; - pa.repeat = RepeatPad; - Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); - Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); + Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); + Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); @@ -165,7 +568,7 @@ get_gaussian_kernel (rv, size, kernel, params); ::swap (params[0], params[1]); - XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); + XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2); XRenderComposite (dpy, PictOpSrc, tmp, @@ -179,6 +582,7 @@ free (kernel); free (params); + XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); XRenderFreePicture (dpy, tmp); @@ -186,300 +590,300 @@ return img; } -static Picture -create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) +ecb_noinline static void +extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) { - Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); - - XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); - XRenderPictureAttributes pa; - pa.repeat = True; - Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); + int32_t x = clamp (c, cl0, cl1); + c -= x; + xc = x; +} - XFreePixmap (dpy, pixmap); +ecb_noinline static bool +extract (int32_t cl0, int32_t cl1, int32_t &r, int32_t &g, int32_t &b, int32_t &a, unsigned short &xr, unsigned short &xg, unsigned short &xb, unsigned short &xa) +{ + extract (cl0, cl1, r, xr); + extract (cl0, cl1, g, xg); + extract (cl0, cl1, b, xb); + extract (cl0, cl1, a, xa); - return mask; + return xr | xg | xb | xa; } void -rxvt_img::brightness (double r, double g, double b, double a) +rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a) { - Display *dpy = s->display->dpy; - Picture src = create_xrender_mask (dpy, pm, True); + unshare (); + + Display *dpy = s->dpy; Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); - XRenderColor mask_c; - mask_c.red = float_to_component (r); - mask_c.green = float_to_component (g); - mask_c.blue = float_to_component (b); - mask_c.alpha = float_to_component (a); - XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); + // loop should not be needed for brightness, as only -1..1 makes sense + //while (r | g | b | a) + { + unsigned short xr, xg, xb, xa; + XRenderColor mask_c; - XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); + if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) + XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); + + if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) + { + XRenderColor mask_w = { 65535, 65535, 65535, 65535 }; + XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); + mask_c.red = -mask_c.red; //TODO: verify that doing clamp, assign, and negation does the right thing + mask_c.green = -mask_c.green; + mask_c.blue = -mask_c.blue; + mask_c.alpha = -mask_c.alpha; + XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h); + XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h); + } + } - XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); } void -rxvt_img::contrast (double r, double g, double b, double a) +rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) { - if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) - return; + if (r < 0 || g < 0 || b < 0 || a < 0) + rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); - Display *dpy = s->display->dpy; - Picture src = create_xrender_mask (dpy, pm, True); - Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); + // premultiply (yeah, these are not exact, sue me or fix it) + r = (r * (a >> 8)) >> 8; + g = (g * (a >> 8)) >> 8; + b = (b * (a >> 8)) >> 8; - XRenderColor mask_c; - mask_c.red = float_to_component (r); - mask_c.green = float_to_component (g); - mask_c.blue = float_to_component (b); - mask_c.alpha = float_to_component (a); - XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); + composer cc (this); + rxvt_img *img = cc; + img->fill (rgba (0, 0, 0, 0)); - XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); + cc.mask (true); - XRenderFreePicture (dpy, src); - XRenderFreePicture (dpy, dst); + //TODO: this operator does not yet implement some useful contrast + while (r | g | b | a) + { + unsigned short xr, xg, xb, xa; + XRenderColor mask_c; + + if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) + { + XRenderFillRectangle (cc.dpy, PictOpSrc, cc.msk, &mask_c, 0, 0, 1, 1); + XRenderComposite (cc.dpy, PictOpAdd, cc.src, cc.msk, cc.dst, 0, 0, 0, 0, 0, 0, w, h); + } + } + + ::swap (img->ref, ref); + ::swap (img->pm , pm ); + + delete img; } -bool -rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) +void +rxvt_img::draw (rxvt_img *img, int op, nv mask) { - Display *dpy = s->display->dpy; + unshare (); - if (s->visual->c_class != TrueColor) - return false; + composer cc (img, this); + + if (mask != 1.) + cc.mask (rgba (0, 0, 0, float_to_component (mask))); - uint32_t red_mask, green_mask, blue_mask, alpha_mask; - - red_mask = (uint32_t)format->direct.redMask << format->direct.red; - green_mask = (uint32_t)format->direct.greenMask << format->direct.green; - blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue; - alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; + XRenderComposite (cc.dpy, op, cc.src, cc.msk, cc.dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h); +} - int width_r = ecb_popcount32 (red_mask); - int width_g = ecb_popcount32 (green_mask); - int width_b = ecb_popcount32 (blue_mask); - int width_a = ecb_popcount32 (alpha_mask); +rxvt_img * +rxvt_img::clone () +{ + return new rxvt_img (*this); +} - if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8) - return false; +rxvt_img * +rxvt_img::reify () +{ + if (x == 0 && y == 0 && w == ref->w && h == ref->h) + return clone (); - int sh_r = ecb_ctz32 (red_mask); - int sh_g = ecb_ctz32 (green_mask); - int sh_b = ecb_ctz32 (blue_mask); - int sh_a = ecb_ctz32 (alpha_mask); + Display *dpy = s->dpy; - if (width > 32767 || height > 32767) - return false; + // add an alpha channel if... + bool alpha = !format->direct.alphaMask // pixmap has none yet + && (x || y) // we need one because of non-zero offset + && repeat == RepeatNone; // and we have no good pixels to fill with - XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0, - width, height, 32, 0); - if (!ximage) - return false; + composer cc (this, new rxvt_img (s, alpha ? find_alpha_format_for (s->dpy, format) : format, + 0, 0, w, h, repeat)); - if (height > INT_MAX / ximage->bytes_per_line - || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line))) + if (alpha) { - XDestroyImage (ximage); - return false; + XRenderColor rc = { 0, 0, 0, 0 }; + XRenderFillRectangle (cc.dpy, PictOpSrc, cc.dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles + XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, 0, 0, 0, 0, x, y, ref->w, ref->h); } + else + XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, -x, -y, 0, 0, 0, 0, w, h); - GC gc = XCreateGC (dpy, pm, 0, 0); + return cc; +} - ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; +rxvt_img * +rxvt_img::sub_rect (int x, int y, int width, int height) +{ + rxvt_img *img = clone (); - int rowstride = gdk_pixbuf_get_rowstride (pixbuf); - int channels = gdk_pixbuf_get_n_channels (pixbuf); - unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; - char *line = ximage->data; + img->x -= x; + img->y -= y; - for (int y = 0; y < height; y++) + if (w != width || h != height) { - for (int x = 0; x < width; x++) - { - unsigned char *pixel = row + x * channels; - uint32_t value; - unsigned char r, g, b, a; - - if (channels == 4) - { - a = pixel[3]; - r = pixel[0] * a / 0xff; - g = pixel[1] * a / 0xff; - b = pixel[2] * a / 0xff; - } - else - { - a = 0xff; - r = pixel[0]; - g = pixel[1]; - b = pixel[2]; - } - - value = ((r >> (8 - width_r)) << sh_r) - | ((g >> (8 - width_g)) << sh_g) - | ((b >> (8 - width_b)) << sh_b) - | ((a >> (8 - width_a)) << sh_a); - - if (ximage->bits_per_pixel == 32) - ((uint32_t *)line)[x] = value; - else - XPutPixel (ximage, x, y, value); - } + img->w = width; + img->h = height; - row += rowstride; - line += ximage->bytes_per_line; + rxvt_img *img2 = img->reify (); + delete img; + img = img2; } - XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height); - XDestroyImage (ximage); - XFreeGC (dpy, gc); - - return true; + return img; } rxvt_img * -rxvt_img::clone () +rxvt_img::transform (const nv matrix[3][3]) { - rxvt_img *img = new rxvt_img (s, format, w, h); - - GC gc = XCreateGC (s->display->dpy, pm, 0, 0); - XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); - XFreeGC (s->display->dpy, gc); - - return img; + return transform (mat3x3 (&matrix[0][0])); } rxvt_img * -rxvt_img::sub_rect (int x, int y, int width, int height) +rxvt_img::transform (const nv *matrix) { - rxvt_img *img = new rxvt_img (s, format, width, height); + mat3x3 m (matrix); - Display *dpy = s->display->dpy; - XRenderPictureAttributes pa; - pa.repeat = repeat; - Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); - Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); + // calculate new pixel bounding box coordinates + nv rmin[2], rmax[2]; - XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); + for (int i = 0; i < 2; ++i) + { + nv v; - XRenderFreePicture (dpy, src); - XRenderFreePicture (dpy, dst); + v = m.apply1 (i, 0+x, 0+y); rmin [i] = rmax [i] = v; + v = m.apply1 (i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v); + v = m.apply1 (i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); + v = m.apply1 (i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); + } - return img; -} + float sx = rmin [0] - x; + float sy = rmin [1] - y; -rxvt_img * -rxvt_img::transform (int new_width, int new_height, double matrix[9]) -{ - rxvt_img *img = new rxvt_img (s, format, new_width, new_height); + // TODO: adjust matrix for subpixel accuracy + int nx = floor (rmin [0]); + int ny = floor (rmin [1]); - Display *dpy = s->display->dpy; - XRenderPictureAttributes pa; - pa.repeat = repeat; - Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); - Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); + int new_width = ceil (rmax [0] - rmin [0]); + int new_height = ceil (rmax [1] - rmin [1]); + + mat3x3 inv = (mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y)).inverse (); + + composer cc (this, new rxvt_img (s, format, nx, ny, new_width, new_height, repeat)); XTransform xfrm; for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) - xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); + xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); - XRenderSetPictureFilter (dpy, src, "good", 0, 0); - XRenderSetPictureTransform (dpy, src, &xfrm); - XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); + XRenderSetPictureFilter (cc.dpy, cc.src, "good", 0, 0); + XRenderSetPictureTransform (cc.dpy, cc.src, &xfrm); + XRenderComposite (cc.dpy, PictOpSrc, cc.src, None, cc.dst, sx, sy, 0, 0, 0, 0, new_width, new_height); - XRenderFreePicture (dpy, src); - XRenderFreePicture (dpy, dst); - - return img; + return cc; } rxvt_img * rxvt_img::scale (int new_width, int new_height) { - double matrix[9] = { - w / (double)new_width, 0, 0, - 0, h / (double)new_height, 0, - 0, 0, 1 - }; + if (w == new_width && h == new_height) + return clone (); + + int old_repeat_mode = repeat; + repeat = RepeatPad; // not right, but xrender can't properly scale it seems + + rxvt_img *img = transform (mat3x3::scale (new_width / (nv)w, new_height / (nv)h)); + + repeat = old_repeat_mode; + img->repeat = repeat; - return transform (new_width, new_height, matrix); + return img; } rxvt_img * -rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) +rxvt_img::rotate (int cx, int cy, nv phi) { - double s = sin (phi); - double c = cos (phi); +#if 0 + { c, -s, cx - c * cx + s * cy }, + { s, c, cy - s * cx - c * cy }, + { 0, 0, 1 } +#endif - double matrix[9] = { - c, -s, -c * x + s * y + x, - s, c, -s * x - c * y + y, - 0, 0, 1 - }; + move (-cx, -cy); + rxvt_img *img = transform (mat3x3::rotate (phi)); + move ( cx, cy); + img->move (cx, cy); - return transform (new_width, new_height, matrix); + return img; } rxvt_img * -rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) +rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) { - rxvt_img *img = new rxvt_img (s, new_format, w, h); + if (new_format == format) + return clone (); + + composer cc (this, new rxvt_img (s, new_format, x, y, w, h, repeat)); - Display *dpy = s->display->dpy; - Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); - 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 }; - XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); + XRenderColor rc = { bg.r, bg.g, bg.b, bg.a }; + XRenderFillRectangle (cc.dpy, PictOpSrc, cc.dst, &rc, 0, 0, w, h); op = PictOpOver; } - XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); + XRenderComposite (cc.dpy, op, cc.src, None, cc.dst, 0, 0, 0, 0, 0, 0, w, h); - XRenderFreePicture (dpy, src); - XRenderFreePicture (dpy, dst); + return cc; +} - return img; +rxvt_img * +rxvt_img::tint (const rgba &c) +{ + composer cc (this); + cc.mask (true); + cc.fill (c); + + XRenderComposite (cc.dpy, PictOpSrc, cc.src, cc.msk, cc.dst, 0, 0, 0, 0, 0, 0, w, h); + + return cc; } rxvt_img * -rxvt_img::blend (rxvt_img *img, double factor) +rxvt_img::filter (const char *name, int nparams, nv *params) { - rxvt_img *img2 = clone (); - Display *dpy = s->display->dpy; - Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); - Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); - Picture mask = create_xrender_mask (dpy, img->pm, False); - - XRenderColor mask_c; - - mask_c.alpha = float_to_component (factor); - mask_c.red = - mask_c.green = - mask_c.blue = 0; - XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); + rxvt_img *img = new_empty (); - XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, w, h); + composer cc (img); - XRenderFreePicture (dpy, src); - XRenderFreePicture (dpy, dst); - XRenderFreePicture (dpy, mask); + XFixed *xparams = rxvt_temp_buf (nparams); + + for (int i = 0; i < nparams; ++i) + xparams [i] = XDoubleToFixed (params [i]); + + XRenderSetPictureFilter (cc.dpy, cc.src, name, xparams, nparams); + + XRenderComposite (cc.dpy, PictOpSrc, cc.src, 0, cc.dst, 0, 0, 0, 0, 0, 0, w, h); - return img2; + return cc; } #endif