--- rxvt-unicode/src/rxvtimg.C 2012/06/07 11:34:09 1.41 +++ rxvt-unicode/src/rxvtimg.C 2012/06/10 10:35:03 1.77 @@ -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,109 @@ return img; } +# if HAVE_PIXBUF + +rxvt_img * +rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) +{ + Display *dpy = s->display->dpy; + + int width = gdk_pixbuf_get_width (pb); + int height = gdk_pixbuf_get_height (pb); + + if (width > 32767 || height > 32767) // well, we *could* upload in chunks + rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); + + // 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 byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; + + XImage xi; + + xi.width = width; + xi.height = height; + xi.xoffset = 0; + xi.format = ZPixmap; + 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 = 32; + xi.bytes_per_line = 0; + 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"); + + if (height > INT_MAX / xi.bytes_per_line) + rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); + + xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); + + int rowstride = gdk_pixbuf_get_rowstride (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++) + { + unsigned char *src = row; + uint32_t *dst = (uint32_t *)line; + + 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 = (255 << 24) | (r << 16) | (g << 8) | b; + + if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) + v = ecb_bswap32 (v); + + *dst++ = v; + } + else + for (int x = 0; x < width; x++) + { + uint32_t v = *(uint32_t *)src; src += 4; + + 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; + line += xi.bytes_per_line; + } + + rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height); + img->alloc (); + + GC gc = XCreateGC (dpy, img->pm, 0, 0); + XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); + XFreeGC (dpy, gc); + + free (xi.data); + + return img; +} + rxvt_img * rxvt_img::new_from_file (rxvt_screen *s, const char *filename) { @@ -67,22 +163,15 @@ if (!pb) rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); - 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); + rxvt_img *img = new_from_pixbuf (s, pb); g_object_unref (pb); return img; } +# endif + void rxvt_img::destroy () { @@ -125,7 +214,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); @@ -140,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 @@ -181,11 +272,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); @@ -211,7 +301,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, @@ -225,6 +315,7 @@ free (kernel); free (params); + XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); XRenderFreePicture (dpy, tmp); @@ -233,158 +324,120 @@ } 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 = True; - Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); + pa.repeat = RepeatNormal; + pa.component_alpha = component_alpha; + Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa); XFreePixmap (dpy, pixmap); return mask; } -void -rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) +static void +extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc) { - 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); + int32_t x = clamp (c, cl0, cl1); + c -= x; + xc = x; +} - XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); +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); - XRenderFreePicture (dpy, src); - XRenderFreePicture (dpy, dst); + return xr | xg | xb | xa; } void -rxvt_img::contrast (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) { - if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL)) - return; + 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, PictOpMultiply, 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); } -bool -rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) +void +rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a) { - Display *dpy = s->display->dpy; - - if (s->visual->c_class != TrueColor) - return false; - - uint32_t red_mask, green_mask, blue_mask, alpha_mask; + if (r < 0 || g < 0 || b < 0 || a < 0) + rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); - 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); - int width_b = ecb_popcount32 (blue_mask); - int width_a = ecb_popcount32 (alpha_mask); - - if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8) - return false; + rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); + img->alloc (); - int sh_r = ecb_ctz32 (red_mask); - int sh_g = ecb_ctz32 (green_mask); - int sh_b = ecb_ctz32 (blue_mask); - int sh_a = ecb_ctz32 (alpha_mask); + { + 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; - if (width > 32767 || height > 32767) - return false; + Display *dpy = s->display->dpy; - XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0, - width, height, 32, 0); - if (!ximage) - return false; + Picture src = src_picture (); + Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); + Picture mul = create_xrender_mask (dpy, pm, True, True); - if (height > INT_MAX / ximage->bytes_per_line - || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line))) + //TODO: this operator does not yet implement some useful contrast + while (r | g | b | a) { - XDestroyImage (ximage); - return false; - } - - GC gc = XCreateGC (dpy, pm, 0, 0); - - ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; + unsigned short xr, xg, xb, xa; + XRenderColor mask_c; - int rowstride = gdk_pixbuf_get_rowstride (pixbuf); - int channels = gdk_pixbuf_get_n_channels (pixbuf); - unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels; - char *line = ximage->data; - - for (int y = 0; y < height; y++) - { - for (int x = 0; x < width; x++) + if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha)) { - unsigned char *pixel = row + x * channels; - uint32_t value; - unsigned char r, g, b, a; - - if (channels == 4) - { - a = pixel[3]; - r = pixel[0] * a / 0xff; - g = pixel[1] * a / 0xff; - b = pixel[2] * a / 0xff; - } - else - { - a = 0xff; - r = pixel[0]; - g = pixel[1]; - b = pixel[2]; - } - - value = ((r >> (8 - width_r)) << sh_r) - | ((g >> (8 - width_g)) << sh_g) - | ((b >> (8 - width_b)) << sh_b) - | ((a >> (8 - width_a)) << sh_a); - - if (ximage->bits_per_pixel == 32) - ((uint32_t *)line)[x] = value; - else - XPutPixel (ximage, x, y, value); + XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1); + XRenderComposite (dpy, PictOpAdd, src, mul, dst, 0, 0, 0, 0, 0, 0, w, h); } - - row += rowstride; - line += ximage->bytes_per_line; } - XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height); - XDestroyImage (ximage); - XFreeGC (dpy, gc); + XRenderFreePicture (dpy, mul); + XRenderFreePicture (dpy, dst); + XRenderFreePicture (dpy, src); + + ::swap (img->ref, ref); + ::swap (img->pm , pm ); - return true; + delete img; } rxvt_img * @@ -393,6 +446,26 @@ return new rxvt_img (*this); } +static XRenderPictFormat * +find_alpha_format_for (Display *dpy, XRenderPictFormat *format) +{ + if (format->direct.alphaMask) + return format; // already has alpha + + // try to find a suitable alpha format, one bit alpha is enough for our purposes + if (format->type == PictTypeDirect) + for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) + if (f->direct.alphaMask + && f->type == PictTypeDirect + && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) + && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) + && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) + return f; + + // should be a very good fallback + return XRenderFindStandardFormat (dpy, PictStandardARGB32); +} + rxvt_img * rxvt_img::reify () { @@ -401,18 +474,26 @@ 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 ? XRenderFindStandardFormat (dpy, PictStandardARGB32) : format, 0, 0, w, h, repeat); + rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); 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); - + if (alpha) + { + XRenderColor rc = { 0, 0, 0, 0 }; + XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles + XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); + } + else + XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); + XRenderFreePicture (dpy, src); XRenderFreePicture (dpy, dst); @@ -456,8 +537,8 @@ 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); + xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO + xfrm.matrix [1][2] -= XDoubleToFixed (y); XRenderSetPictureFilter (dpy, src, "good", 0, 0); XRenderSetPictureTransform (dpy, src, &xfrm); @@ -472,13 +553,24 @@ rxvt_img * rxvt_img::scale (int new_width, int new_height) { + 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 }; - return transform (new_width, new_height, matrix); + int old_repeat_mode = repeat; + repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems + + rxvt_img *img = transform (new_width, new_height, matrix); + + repeat = old_repeat_mode; + img->repeat = repeat; + + return img; } rxvt_img * @@ -497,12 +589,12 @@ } rxvt_img * -rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg) +rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) { if (new_format == format) return clone (); - rxvt_img *img = new rxvt_img (s, new_format, 0, 0, w, h, repeat); + rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); img->alloc (); Display *dpy = s->display->dpy; @@ -537,7 +629,7 @@ 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); + Picture mask = create_xrender_mask (dpy, img->pm, False, False); XRenderColor mask_c;