--- rxvt-unicode/src/rxvtimg.C 2012/06/09 17:28:03 1.73 +++ rxvt-unicode/src/rxvtimg.C 2012/06/10 10:35:03 1.77 @@ -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); @@ -318,6 +323,22 @@ return img; } +static Picture +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; + pa.component_alpha = component_alpha; + Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa); + + XFreePixmap (dpy, pixmap); + + return mask; +} + static void extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) { @@ -367,32 +388,56 @@ } } - XRenderFreePicture (dpy, dst); } void rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) { - if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) - { - rxvt_warn ("rxvt_img::contrast operation not supported on this display, RENDER extension too old.\n"); - return; - } + if (r < 0 || g < 0 || b < 0 || a < 0) + rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); - unshare (); + 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 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, PictOpMultiply, dst, &mask_c, 0, 0, w, h); + Picture src = src_picture (); + Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); + Picture mul = create_xrender_mask (dpy, pm, True, True); + + //TODO: this operator does not yet implement some useful contrast + while (r | g | b | a) + { + unsigned short xr, xg, xb, xa; + XRenderColor mask_c; + 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 * @@ -584,13 +629,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;