--- rxvt-unicode/src/rxvtimg.C 2012/06/09 19:20:04 1.74 +++ rxvt-unicode/src/rxvtimg.C 2012/06/12 18:25:57 1.80 @@ -52,6 +52,7 @@ } # if HAVE_PIXBUF + rxvt_img * rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) { @@ -65,7 +66,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 +79,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 +98,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 +108,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 +142,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 +169,7 @@ return img; } + # endif void @@ -227,11 +228,14 @@ void rxvt_img::fill (const rxvt_color &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); + rgba cc; + c.get (cc); + XRenderColor rc = { cc.r, cc.g, cc.b, cc.a }; + + Display *dpy = s->display->dpy; + Picture src = src_picture (); + XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); + XRenderFreePicture (dpy, src); } static void @@ -311,6 +315,7 @@ free (kernel); free (params); + XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); XRenderFreePicture (dpy, tmp); @@ -319,14 +324,15 @@ } static Picture -create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) +create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha) { Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); XRenderPictureAttributes pa; pa.repeat = RepeatNormal; - Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); + pa.component_alpha = component_alpha; + Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa); XFreePixmap (dpy, pixmap); @@ -409,11 +415,7 @@ Picture src = src_picture (); Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); - Picture mul = create_xrender_mask (dpy, pm, True); - - XRenderPictureAttributes pa; - pa.component_alpha = 1; - XRenderChangePicture (dpy, mul, CPComponentAlpha, &pa); + Picture mul = create_xrender_mask (dpy, pm, True, True); //TODO: this operator does not yet implement some useful contrast while (r | g | b | a) @@ -519,10 +521,66 @@ return img; } +static void +mat_invert (double mat[3][3], double (&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]; + + double invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2); + + inv [0][0] = invdet * s0; + inv [0][1] = invdet * s1; + inv [0][2] = invdet * s2; + + inv [1][0] = invdet * (mat [2][0] * mat [1][2] - mat [2][2] * mat [1][0]); + inv [1][1] = invdet * (mat [2][2] * mat [0][0] - mat [2][0] * mat [0][2]); + inv [1][2] = invdet * (mat [1][0] * mat [0][2] - mat [1][2] * mat [0][0]); + + inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]); + inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]); + 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) +{ + 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]; + + return v * (1. / w); +} + rxvt_img * -rxvt_img::transform (int new_width, int new_height, double matrix[9]) +rxvt_img::transform (double matrix[3][3]) { - rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); + // 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 rmin[2], rmax[2]; + + for (int i = 0; i < 2; ++i) + { + double v; + v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [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); + } + + int dx = floor (rmin [0]); + int dy = floor (rmin [1]); + + int new_width = ceil (rmax [0] - dx); + int new_height = ceil (rmax [1] - dy); + + double inv[3][3]; + mat_invert (matrix, inv); + + rxvt_img *img = new rxvt_img (s, format, ox - dx, oy - dy, new_width, new_height, repeat); img->alloc (); Display *dpy = s->display->dpy; @@ -533,14 +591,11 @@ 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, dx, dy, 0, 0, 0, 0, new_width, new_height); XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); @@ -554,16 +609,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 + double matrix[3][3] = { + { new_width / (double)w, 0, 0 }, + { 0, new_height / (double)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; @@ -572,18 +627,26 @@ } rxvt_img * -rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) +rxvt_img::rotate (int cx, int cy, double phi) { double s = sin (phi); double c = cos (phi); - double matrix[9] = { - c, -s, -c * x + s * y + x, - s, c, -s * x - c * y + y, - 0, 0, 1 + double matrix[3][3] = { + //{ 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 } }; - 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 * @@ -627,13 +690,7 @@ Display *dpy = s->display->dpy; Picture src = img->src_picture (); Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); - - Pixmap pixmap = XCreatePixmap (dpy, img->pm, 1, 1, 8); - XRenderPictFormat *format = XRenderFindStandardFormat (dpy, PictStandardA8); - XRenderPictureAttributes pa; - pa.repeat = True; - Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); - XFreePixmap (dpy, pixmap); + Picture mask = create_xrender_mask (dpy, img->pm, False, False); XRenderColor mask_c;