--- rxvt-unicode/src/rxvtimg.C 2012/06/04 15:53:12 1.14 +++ rxvt-unicode/src/rxvtimg.C 2012/06/06 18:13:03 1.26 @@ -18,6 +18,38 @@ } rxvt_img * +rxvt_img::new_from_root (rxvt_screen *s) +{ + Display *dpy = s->display->dpy; + unsigned int root_pm_w, root_pm_h; + 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]); + + if (root_pixmap == None) + return 0; + + Window wdummy; + int idummy; + unsigned int udummy; + + if (!XGetGeometry (dpy, root_pixmap, &wdummy, &idummy, &idummy, &root_pm_w, &root_pm_h, &udummy, &udummy)) + return 0; + + rxvt_img *img = new rxvt_img ( + s, + XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), + root_pm_w, + root_pm_h, + root_pixmap + ); + + img->shared = true; + + return img; +} + +rxvt_img * rxvt_img::new_from_file (rxvt_screen *s, const char *filename) { GError *err = 0; @@ -35,6 +67,8 @@ img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); + g_object_unref (pb); + return img; } @@ -89,24 +123,27 @@ params[i+2] = XDoubleToFixed (kernel[i] / sum); } -void +rxvt_img * rxvt_img::blur (int rh, int rv) { if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) - return; + return clone (); Display *dpy = s->display->dpy; int size = max (rh, rv) * 2 + 1; double *kernel = (double *)malloc (size * sizeof (double)); XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); + rxvt_img *img = new rxvt_img (s, format, w, h); XRenderPictureAttributes pa; pa.repeat = RepeatPad; - Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa); - Pixmap tmp = XCreatePixmap (dpy, pm, w, h, format->depth); - Picture dst = XRenderCreatePicture (dpy, tmp, format, CPRepeat, &pa); - XFreePixmap (dpy, tmp); + Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); + Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); + + Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); + Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); + XFreePixmap (dpy, tmp_pm); if (kernel && params) { @@ -118,14 +155,12 @@ PictOpSrc, src, None, - dst, + tmp, 0, 0, 0, 0, 0, 0, w, h); - ::swap (src, dst); - size = rv * 2 + 1; get_gaussian_kernel (rv, size, kernel, params); ::swap (params[0], params[1]); @@ -133,7 +168,7 @@ XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); XRenderComposite (dpy, PictOpSrc, - src, + tmp, None, dst, 0, 0, @@ -146,6 +181,9 @@ free (params); XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); + XRenderFreePicture (dpy, tmp); + + return img; } static Picture @@ -178,6 +216,9 @@ XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); + + XRenderFreePicture (dpy, src); + XRenderFreePicture (dpy, dst); } void @@ -198,13 +239,14 @@ XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); + + XRenderFreePicture (dpy, src); + XRenderFreePicture (dpy, dst); } bool rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) { - bool argb = format->id == PictStandardARGB32; - Display *dpy = s->display->dpy; if (s->visual->c_class != TrueColor) @@ -212,20 +254,10 @@ uint32_t red_mask, green_mask, blue_mask, alpha_mask; - if (argb) - { - red_mask = 0xff << 16; - green_mask = 0xff << 8; - blue_mask = 0xff; - alpha_mask = 0xff << 24; - } - else - { - red_mask = s->visual->red_mask; - green_mask = s->visual->green_mask; - blue_mask = s->visual->blue_mask; - alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha; - } + 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; int width_r = ecb_popcount32 (red_mask); int width_g = ecb_popcount32 (green_mask); @@ -243,7 +275,7 @@ if (width > 32767 || height > 32767) return false; - XImage *ximage = XCreateImage (dpy, s->visual, argb ? 32 : format->depth, ZPixmap, 0, 0, + XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0, width, height, 32, 0); if (!ximage) return false; @@ -264,17 +296,6 @@ unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; char *line = ximage->data; - rgba c (0, 0, 0); - - if (channels == 4 && alpha_mask == 0) - { - //pix_colors[Color_bg].get (c); - //TODO - c.r >>= 8; - c.g >>= 8; - c.b >>= 8; - } - for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) @@ -286,9 +307,9 @@ if (channels == 4) { a = pixel[3]; - r = (pixel[0] * a + c.r * (0xff - a)) / 0xff; - g = (pixel[1] * a + c.g * (0xff - a)) / 0xff; - b = (pixel[2] * a + c.b * (0xff - a)) / 0xff; + r = pixel[0] * a / 0xff; + g = pixel[1] * a / 0xff; + b = pixel[2] * a / 0xff; } else { @@ -323,15 +344,36 @@ rxvt_img * rxvt_img::clone () { + rxvt_img *img = new rxvt_img (s, format, w, h); + GC gc = XCreateGC (s->display->dpy, pm, 0, 0); - Pixmap pm2 = XCreatePixmap (s->display->dpy, pm, w, h, format->depth); - XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); + XCopyArea (s->display->dpy, pm, img->pm, gc, 0, 0, w, h, 0, 0); XFreeGC (s->display->dpy, gc); - return new rxvt_img (s, format, w, h, pm2); + + return img; +} + +rxvt_img * +rxvt_img::sub_rect (int x, int y, int width, int height, int repeat) +{ + rxvt_img *img = new rxvt_img (s, format, width, height); + + 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); + + XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); + + XRenderFreePicture (dpy, src); + XRenderFreePicture (dpy, dst); + + return img; } rxvt_img * -rxvt_img::transform (int new_width, int new_height, int repeat, double matrix[9]) +rxvt_img::transform (int new_width, int new_height, double matrix[9], int repeat) { rxvt_img *img = new rxvt_img (s, format, new_width, new_height); @@ -347,6 +389,7 @@ for (int j = 0; j < 3; ++j) xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + 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); @@ -360,12 +403,27 @@ rxvt_img::scale (int new_width, int new_height) { double matrix[9] = { - new_width / (double)w, 0, 0, - 0, new_height / (double)h, 0, + w / (double)new_width, 0, 0, + 0, h / (double)new_height, 0, 0, 0, 1 }; - return transform (new_width, new_height, RepeatNormal, matrix); + return transform (new_width, new_height, matrix); +} + +rxvt_img * +rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi, int repeat) +{ + 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 + }; + + return transform (new_width, new_height, matrix, repeat); } rxvt_img * @@ -385,5 +443,31 @@ return img; } +rxvt_img * +rxvt_img::blend (rxvt_img *img, double factor) +{ + 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); + + XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, w, h); + + XRenderFreePicture (dpy, src); + XRenderFreePicture (dpy, dst); + XRenderFreePicture (dpy, mask); + + return img2; +} + #endif