--- rxvt-unicode/src/rxvtimg.C 2012/06/10 07:51:45 1.75 +++ rxvt-unicode/src/rxvtimg.C 2012/06/15 18:05:15 1.92 @@ -1,9 +1,154 @@ +#include #include #include "../config.h" #include "rxvt.h" #if HAVE_IMG +typedef rxvt_img::nv nv; + +struct mat3x3 +{ + nv v[3][3]; + + mat3x3 () + { + } + + mat3x3 (nv matrix[3][3]) + { + 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 invert (); + + nv *operator [](int i) { return &v[i][0]; } + const nv *operator [](int i) const { return &v[i][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); +}; + +mat3x3 +mat3x3::invert () +{ + 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 + ); +} + +#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) @@ -52,6 +197,7 @@ } # if HAVE_PIXBUF + rxvt_img * rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) { @@ -65,7 +211,6 @@ // 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 depth = gdk_pixbuf_get_has_alpha (pb) ? 32 : 24; int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; @@ -79,7 +224,7 @@ xi.bitmap_unit = 0; //XY only, unused xi.bitmap_bit_order = 0; //XY only, unused xi.bitmap_pad = BitmapPad (dpy); - xi.depth = depth; + xi.depth = 32; xi.bytes_per_line = 0; xi.bits_per_pixel = 32; //Z only xi.red_mask = 0x00000000; //Z only, unused @@ -98,9 +243,9 @@ xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); int rowstride = gdk_pixbuf_get_rowstride (pb); - - assert (3 + (depth == 32) == gdk_pixbuf_get_n_channels (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++) @@ -108,14 +253,14 @@ unsigned char *src = row; uint32_t *dst = (uint32_t *)line; - if (depth == 24) + 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 = (r << 16) | (g << 8) | b; + uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b; if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) v = ecb_bswap32 (v); @@ -142,7 +287,7 @@ line += xi.bytes_per_line; } - rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, depth == 24 ? PictStandardRGB24 : PictStandardARGB32), 0, 0, width, height); + rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height); img->alloc (); GC gc = XCreateGC (dpy, img->pm, 0, 0); @@ -169,6 +314,7 @@ return img; } + # endif void @@ -196,7 +342,7 @@ } Picture -rxvt_img::src_picture () +rxvt_img::picture () { Display *dpy = s->display->dpy; @@ -225,25 +371,51 @@ } void -rxvt_img::fill (const rxvt_color &c) +rxvt_img::fill (const rgba &c) { - 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); + XRenderColor rc = { c.r, c.g, c.b, c.a }; + + Display *dpy = s->display->dpy; + 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, 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]; } @@ -263,7 +435,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 (); @@ -311,6 +483,7 @@ free (kernel); free (params); + XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); XRenderFreePicture (dpy, tmp); @@ -394,12 +567,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; @@ -408,7 +576,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); @@ -435,30 +603,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_p) + XRenderFreePicture (dpy, mask_p); +} + +rxvt_img * +rxvt_img::clone () +{ + return new rxvt_img (*this); } rxvt_img * @@ -477,17 +651,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); @@ -500,8 +674,8 @@ { rxvt_img *img = clone (); - img->x += x; - img->y += y; + img->x -= x; + img->y -= y; if (w != width || h != height) { @@ -517,27 +691,63 @@ } rxvt_img * -rxvt_img::transform (int new_width, int new_height, double matrix[9]) +rxvt_img::transform (nv matrix[3][3]) { - rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); + // calculate new pixel bounding box coordinates + nv r[2], rmin[2], rmax[2]; + + mat3x3 m (matrix); + + for (int i = 0; i < 2; ++i) + { + nv v; + + v = m.apply1 (i, 0+x, 0+y); rmin [i] = rmax [i] = v; r [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); + } + + float sx = rmin [0] - x; + float sy = rmin [1] - y; + + // 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]); + + m = mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y); + + mat3x3 inv = m.invert (); + + 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; 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 [0][2] -= XDoubleToFixed (x);//TODO - xfrm.matrix [1][2] -= XDoubleToFixed (y); + 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); + 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); @@ -551,16 +761,16 @@ if (w == new_width && h == new_height) return clone (); - double matrix[9] = { - w / (double)new_width, 0, 0, - 0, h / (double)new_height, 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; - repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems + repeat = RepeatPad; // not right, but xrender can't properly scale it seems - rxvt_img *img = transform (new_width, new_height, matrix); + rxvt_img *img = transform (matrix); repeat = old_repeat_mode; img->repeat = repeat; @@ -569,22 +779,33 @@ } 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); + nv s = sin (phi); + nv c = cos (phi); - double matrix[9] = { - c, -s, -c * x + s * y + x, - s, c, -s * x - c * y + y, - 0, 0, 1 + nv matrix[3][3] = { +#if 0 + { c, -s, cx - c * cx + s * cy }, + { s, c, cy - s * cx - c * cy }, + { 0, 0, 1 } +#else + { c, -s, 0 }, + { s, c, 0 }, + { 0, 0, 1 } +#endif }; - return transform (new_width, new_height, matrix); + move (-cx, -cy); + rxvt_img *img = transform (matrix); + move ( cx, cy); + img->move (cx, cy); + + return img; } 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 (); @@ -593,17 +814,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; @@ -618,11 +836,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);