--- rxvt-unicode/src/rxvtimg.C 2012/06/07 19:42:09 1.50 +++ rxvt-unicode/src/rxvtimg.C 2012/06/09 19:20:04 1.74 @@ -16,13 +16,6 @@ ++ref->cnt; } -#if 0 -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) -{ -} -#endif - rxvt_img * rxvt_img::new_from_root (rxvt_screen *s) { @@ -58,6 +51,7 @@ return img; } +# if HAVE_PIXBUF rxvt_img * rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) { @@ -73,22 +67,27 @@ // 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; + XImage xi; xi.width = width; xi.height = height; xi.xoffset = 0; xi.format = ZPixmap; - xi.byte_order = LSBFirst; // maybe go for host byte order, because servers are usually local? - xi.bitmap_unit = 32; - xi.bitmap_bit_order = LSBFirst; + 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 = depth; xi.bytes_per_line = 0; - xi.bits_per_pixel = 32; - xi.red_mask = 0x000000ff; - xi.green_mask = 0x0000ff00; - xi.blue_mask = 0x00ff0000; + 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"); @@ -116,19 +115,27 @@ uint8_t g = *src++; uint8_t b = *src++; - uint32_t v = r | (g << 8) | (b << 16); + uint32_t v = (r << 16) | (g << 8) | b; - if (ecb_big_endian ()) + if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) v = ecb_bswap32 (v); - *dst++ = x; + *dst++ = v; } else for (int x = 0; x < width; x++) { uint32_t v = *(uint32_t *)src; src += 4; - v = ecb_big_endian () ? ecb_rotr32 (v, 8) : ecb_rotl32 (v, 8); - *dst++ = ecb_bswap32 (v); + + 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; @@ -162,6 +169,7 @@ return img; } +# endif void rxvt_img::destroy () @@ -205,7 +213,6 @@ if (ref->cnt == 1 && ref->ours) return; - //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); @@ -261,11 +268,10 @@ rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); img->alloc (); - Picture src = src_picture (); - XRenderPictureAttributes pa; pa.repeat = RepeatPad; - 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); @@ -291,7 +297,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, @@ -319,7 +325,7 @@ XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); XRenderPictureAttributes pa; - pa.repeat = True; + pa.repeat = RepeatNormal; Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); XFreePixmap (dpy, pixmap); @@ -327,47 +333,109 @@ return mask; } +static void +extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) +{ + int32_t x = clamp (c, cl0, cl1); + c -= x; + xc = x; +} + +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 xr | xg | xb | xa; +} + void -rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) +rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a) { + unshare (); + 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 = r; - mask_c.green = g; - mask_c.blue = b; - mask_c.alpha = 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 (unsigned short r, unsigned short g, unsigned short b, unsigned short 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"); + + 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); + } + + // 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; 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 = r; - mask_c.green = g; - mask_c.blue = b; - mask_c.alpha = a; - XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); + 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); - XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); + //TODO: this operator does not yet implement some useful contrast + while (r | g | b | a) + { + unsigned short xr, xg, xb, xa; + XRenderColor mask_c; - XRenderFreePicture (dpy, src); + if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) + { + XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1); + XRenderComposite (dpy, PictOpAdd, src, mul, dst, 0, 0, 0, 0, 0, 0, w, h); + } + } + + XRenderFreePicture (dpy, mul); XRenderFreePicture (dpy, dst); + XRenderFreePicture (dpy, src); + + ::swap (img->ref, ref); + ::swap (img->pm , pm ); + + delete img; } rxvt_img * @@ -404,9 +472,10 @@ Display *dpy = s->display->dpy; - bool alpha = !format->direct.alphaMask - && (x || y) - && repeat == RepeatNone; + // 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 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); img->alloc (); @@ -466,10 +535,8 @@ for (int j = 0; j < 3; ++j) xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); -#if 0 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO xfrm.matrix [1][2] -= XDoubleToFixed (y); -#endif XRenderSetPictureFilter (dpy, src, "good", 0, 0); XRenderSetPictureTransform (dpy, src, &xfrm); @@ -560,7 +627,13 @@ 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); + + 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); XRenderColor mask_c;