--- rxvt-unicode/src/rxvtimg.C 2012/06/04 16:12:55 1.16 +++ rxvt-unicode/src/rxvtimg.C 2012/06/07 11:27:08 1.40 @@ -4,17 +4,58 @@ #if HAVE_IMG -#define float_to_component(d) ((d) * 65535.99) +rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height) +: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(RepeatNormal), + pm(0), ref(0) +{ +} -rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) -: s(screen), w(width), h(height), format(format), shared(false) +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) { - pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); + ++ref->cnt; } +#if 0 rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap) -: s(screen), pm(pixmap), w(width), h(height), format(format), shared(false) +: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap) +{ +} +#endif + +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)), + 0, + 0, + root_pm_w, + root_pm_h + ); + + img->pm = root_pixmap; + img->ref = new pixref (root_pm_w, root_pm_h); + img->ref->ours = false; + + return img; } rxvt_img * @@ -29,33 +70,71 @@ rxvt_img *img = new rxvt_img ( s, XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24), + 0, + 0, gdk_pixbuf_get_width (pb), gdk_pixbuf_get_height (pb) ); - + img->alloc (); img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0); + g_object_unref (pb); + return img; } -rxvt_img::~rxvt_img () +void +rxvt_img::destroy () { - if (!shared) + if (--ref->cnt) + return; + + if (pm && ref->ours) XFreePixmap (s->display->dpy, pm); + + delete ref; +} + +rxvt_img::~rxvt_img () +{ + destroy (); +} + +void +rxvt_img::alloc () +{ + pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); + ref = new pixref (w, h); +} + +Picture +rxvt_img::src_picture () +{ + Display *dpy = s->display->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 (); + //TODO: maybe should reify instead + Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth); + GC gc = XCreateGC (s->display->dpy, pm, 0, 0); + XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); + XFreeGC (s->display->dpy, gc); - ::swap (pm , img->pm); - ::swap (shared, img->shared); + destroy (); - delete img; + pm = pm2; + ref = new pixref (ref->w, ref->h); } void @@ -89,24 +168,28 @@ 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, x, y, w, h); + img->alloc (); - XRenderPictureAttributes pa; + Picture src = src_picture (); + 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 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 +201,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 +214,7 @@ XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); XRenderComposite (dpy, PictOpSrc, - src, + tmp, None, dst, 0, 0, @@ -146,6 +227,9 @@ free (params); XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); + XRenderFreePicture (dpy, tmp); + + return img; } static Picture @@ -164,24 +248,27 @@ } void -rxvt_img::brightness (double r, double g, double b, double a) +rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) { Display *dpy = s->display->dpy; Picture src = create_xrender_mask (dpy, pm, True); 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); + mask_c.red = r; + mask_c.green = g; + mask_c.blue = b; + mask_c.alpha = a; 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 -rxvt_img::contrast (double r, double g, double b, double a) +rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) { if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) return; @@ -191,20 +278,21 @@ 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); + mask_c.red = r; + mask_c.green = g; + mask_c.blue = b; + mask_c.alpha = a; 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 +300,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 +321,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,18 +342,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 = 0xffff; c.g = 0xc0c0; c.b = 0xcbcb;//D - c.r >>= 8; - c.g >>= 8; - c.b >>= 8; - } - for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) @@ -287,9 +353,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 { @@ -324,23 +390,65 @@ rxvt_img * rxvt_img::clone () { - 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); - XFreeGC (s->display->dpy, gc); - return new rxvt_img (s, format, w, h, pm2); + return new rxvt_img (*this); } rxvt_img * -rxvt_img::transform (int new_width, int new_height, int repeat, double matrix[9]) +rxvt_img::reify () { - rxvt_img *img = new rxvt_img (s, format, new_width, new_height); + if (x == 0 && y == 0 && w == ref->w && h == ref->h) + return clone (); 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); + + bool alpha = !format->direct.alphaMask + && (x || y) + && repeat == RepeatNone; + + rxvt_img *img = new rxvt_img (s, alpha ? XRenderFindStandardFormat (dpy, PictStandardARGB32) : format, 0, 0, w, h); + img->alloc (); + + Picture src = src_picture (); + Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); + + XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); + + XRenderFreePicture (dpy, src); + XRenderFreePicture (dpy, dst); + + return img; +} + +rxvt_img * +rxvt_img::sub_rect (int x, int y, int width, int height) +{ + rxvt_img *img = clone (); + + img->x += x; + img->y += y; + + if (w != width || h != height) + { + img->w = width; + img->h = height; + + rxvt_img *img2 = img->reify (); + delete img; + img = img2; + } + + return img; +} + +rxvt_img * +rxvt_img::transform (int new_width, int new_height, double matrix[9]) +{ + rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height); + img->alloc (); + + Display *dpy = s->display->dpy; + Picture src = src_picture (); + Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); XTransform xfrm; @@ -348,6 +456,10 @@ 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 [0][3] += XDoubleToFixed (y); + + 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); @@ -366,19 +478,51 @@ 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) +{ + 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); } rxvt_img * -rxvt_img::convert_to (XRenderPictFormat *new_format) +rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) { - rxvt_img *img = new rxvt_img (s, new_format, w, h); + if (new_format == format) + return clone (); + + rxvt_img *img = new rxvt_img (s, new_format, 0, 0, w, h); + img->alloc (); Display *dpy = s->display->dpy; - Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0); + Picture src = 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); - XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); + XRenderColor rc = { c.r, c.g, c.b, 0xffff }; + XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); + + op = PictOpOver; + } + + XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); @@ -386,5 +530,31 @@ return img; } +rxvt_img * +rxvt_img::blend (rxvt_img *img, double factor) +{ + rxvt_img *img2 = clone (); + Display *dpy = s->display->dpy; + Picture src = img->src_picture (); + 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