ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtimg.C (file contents):
Revision 1.32 by root, Thu Jun 7 07:53:12 2012 UTC vs.
Revision 1.84 by root, Thu Jun 14 18:19:11 2012 UTC

2#include "../config.h" 2#include "../config.h"
3#include "rxvt.h" 3#include "rxvt.h"
4 4
5#if HAVE_IMG 5#if HAVE_IMG
6 6
7static XRenderPictFormat *
8find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
9{
10 if (format->direct.alphaMask)
11 return format; // already has alpha
12
13 // try to find a suitable alpha format, one bit alpha is enough for our purposes
14 if (format->type == PictTypeDirect)
15 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
16 if (f->direct.alphaMask
17 && f->type == PictTypeDirect
18 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
19 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
20 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
21 return f;
22
23 // should be a very good fallback
24 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
25}
26
7rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height) 27rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat)
8: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), 28: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat),
9 pm(0), refcnt(0) 29 pm(0), ref(0)
10{ 30{
11} 31}
12 32
13rxvt_img::rxvt_img (const rxvt_img &img) 33rxvt_img::rxvt_img (const rxvt_img &img)
14: s(img.s), x(img.x), y(img.y), w(img.w), h(img.h), format(img.format), repeat(img.repeat), pm(img.pm), refcnt(img.refcnt) 34: 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)
15{ 35{
16 if (refcnt)
17 ++*refcnt; 36 ++ref->cnt;
18} 37}
19
20#if 0
21rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap)
22: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap)
23{
24}
25#endif
26 38
27rxvt_img * 39rxvt_img *
28rxvt_img::new_from_root (rxvt_screen *s) 40rxvt_img::new_from_root (rxvt_screen *s)
29{ 41{
30 Display *dpy = s->display->dpy; 42 Display *dpy = s->display->dpy;
44 return 0; 56 return 0;
45 57
46 rxvt_img *img = new rxvt_img ( 58 rxvt_img *img = new rxvt_img (
47 s, 59 s,
48 XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)), 60 XRenderFindVisualFormat (dpy, DefaultVisual (dpy, s->display->screen)),
61 0,
62 0,
49 root_pm_w, 63 root_pm_w,
50 root_pm_h 64 root_pm_h
51 ); 65 );
52 66
53 img->pm = root_pixmap; 67 img->pm = root_pixmap;
68 img->ref = new pixref (root_pm_w, root_pm_h);
69 img->ref->ours = false;
70
71 return img;
72}
73
74# if HAVE_PIXBUF
75
76rxvt_img *
77rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb)
78{
79 Display *dpy = s->display->dpy;
80
81 int width = gdk_pixbuf_get_width (pb);
82 int height = gdk_pixbuf_get_height (pb);
83
84 if (width > 32767 || height > 32767) // well, we *could* upload in chunks
85 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n");
86
87 // since we require rgb24/argb32 formats from xrender we assume
88 // that both 24 and 32 bpp MUST be supported by any screen that supports xrender
89
90 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
91
92 XImage xi;
93
94 xi.width = width;
95 xi.height = height;
96 xi.xoffset = 0;
97 xi.format = ZPixmap;
98 xi.byte_order = ImageByteOrder (dpy);
99 xi.bitmap_unit = 0; //XY only, unused
100 xi.bitmap_bit_order = 0; //XY only, unused
101 xi.bitmap_pad = BitmapPad (dpy);
102 xi.depth = 32;
103 xi.bytes_per_line = 0;
104 xi.bits_per_pixel = 32; //Z only
105 xi.red_mask = 0x00000000; //Z only, unused
106 xi.green_mask = 0x00000000; //Z only, unused
107 xi.blue_mask = 0x00000000; //Z only, unused
108 xi.obdata = 0; // probably unused
109
110 bool byte_order_mismatch = byte_order != xi.byte_order;
111
112 if (!XInitImage (&xi))
113 rxvt_fatal ("unable to initialise ximage, please report.\n");
114
115 if (height > INT_MAX / xi.bytes_per_line)
116 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n");
117
118 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line);
119
120 int rowstride = gdk_pixbuf_get_rowstride (pb);
121 bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb);
122 unsigned char *row = gdk_pixbuf_get_pixels (pb);
123
124 char *line = xi.data;
125
126 for (int y = 0; y < height; y++)
127 {
128 unsigned char *src = row;
129 uint32_t *dst = (uint32_t *)line;
130
131 if (!pb_has_alpha)
132 for (int x = 0; x < width; x++)
133 {
134 uint8_t r = *src++;
135 uint8_t g = *src++;
136 uint8_t b = *src++;
137
138 uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b;
139
140 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch)
141 v = ecb_bswap32 (v);
142
143 *dst++ = v;
144 }
145 else
146 for (int x = 0; x < width; x++)
147 {
148 uint32_t v = *(uint32_t *)src; src += 4;
149
150 if (ecb_big_endian ())
151 v = ecb_bswap32 (v);
152
153 v = ecb_rotl32 (v, 8); // abgr to bgra
154
155 if (!byte_order_mismatch)
156 v = ecb_bswap32 (v);
157
158 *dst++ = v;
159 }
160
161 row += rowstride;
162 line += xi.bytes_per_line;
163 }
164
165 rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height);
166 img->alloc ();
167
168 GC gc = XCreateGC (dpy, img->pm, 0, 0);
169 XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height);
170 XFreeGC (dpy, gc);
171
172 free (xi.data);
54 173
55 return img; 174 return img;
56} 175}
57 176
58rxvt_img * 177rxvt_img *
62 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err); 181 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err);
63 182
64 if (!pb) 183 if (!pb)
65 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message); 184 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message);
66 185
67 rxvt_img *img = new rxvt_img ( 186 rxvt_img *img = new_from_pixbuf (s, pb);
68 s,
69 XRenderFindStandardFormat (s->display->dpy, gdk_pixbuf_get_has_alpha (pb) ? PictStandardARGB32 : PictStandardRGB24),
70 gdk_pixbuf_get_width (pb),
71 gdk_pixbuf_get_height (pb)
72 );
73 img->alloc ();
74 img->render_pixbuf (pb, 0, 0, img->w, img->h, 0, 0);
75 187
76 g_object_unref (pb); 188 g_object_unref (pb);
77 189
78 return img; 190 return img;
79} 191}
192
193# endif
80 194
81void 195void
82rxvt_img::destroy () 196rxvt_img::destroy ()
83{ 197{
84 if (!refcnt || --*refcnt) 198 if (--ref->cnt)
85 return; 199 return;
86 200
87 if (pm) 201 if (pm && ref->ours)
88 XFreePixmap (s->display->dpy, pm); 202 XFreePixmap (s->display->dpy, pm);
89 203
90 delete refcnt; 204 delete ref;
91} 205}
92 206
93rxvt_img::~rxvt_img () 207rxvt_img::~rxvt_img ()
94{ 208{
95 destroy (); 209 destroy ();
97 211
98void 212void
99rxvt_img::alloc () 213rxvt_img::alloc ()
100{ 214{
101 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); 215 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
102 refcnt = new int (1); 216 ref = new pixref (w, h);
217}
218
219Picture
220rxvt_img::src_picture ()
221{
222 Display *dpy = s->display->dpy;
223
224 XRenderPictureAttributes pa;
225 pa.repeat = repeat;
226 Picture pic = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
227
228 return pic;
103} 229}
104 230
105void 231void
106rxvt_img::unshare () 232rxvt_img::unshare ()
107{ 233{
108 if (refcnt && *refcnt == 1) 234 if (ref->cnt == 1 && ref->ours)
109 return; 235 return;
110 236
111 Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); 237 Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth);
112 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 238 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
113 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, w, h, 0, 0); 239 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0);
114 XFreeGC (s->display->dpy, gc); 240 XFreeGC (s->display->dpy, gc);
115 241
116 destroy (); 242 destroy ();
117 243
118 pm = pm2; 244 pm = pm2;
119 refcnt = new int (1); 245 ref = new pixref (ref->w, ref->h);
120} 246}
121 247
122void 248void
123rxvt_img::fill (const rxvt_color &c) 249rxvt_img::fill (const rgba &c)
124{ 250{
125 XGCValues gcv; 251 XRenderColor rc = { c.r, c.g, c.b, c.a };
126 gcv.foreground = c; 252
127 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 253 Display *dpy = s->display->dpy;
128 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 254 Picture src = src_picture ();
129 XFreeGC (s->display->dpy, gc); 255 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
256 XRenderFreePicture (dpy, src);
257}
258
259void
260rxvt_img::add_alpha ()
261{
262 if (format->direct.alphaMask)
263 return;
264
265 Display *dpy = s->display->dpy;
266
267 rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat);
268 img->alloc ();
269
270 Picture src = src_picture ();
271 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
272
273 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
274
275 XRenderFreePicture (dpy, src);
276 XRenderFreePicture (dpy, dst);
277
278 ::swap (img->ref, ref);
279 ::swap (img->pm , pm );
280
281 delete img;
130} 282}
131 283
132static void 284static void
133get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 285get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params)
134{ 286{
135 double sigma = radius / 2.0; 287 rxvt_img::nv sigma = radius / 2.0;
136 double scale = sqrt (2.0 * M_PI) * sigma; 288 rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma;
137 double sum = 0.0; 289 rxvt_img::nv sum = 0.0;
138 290
139 for (int i = 0; i < width; i++) 291 for (int i = 0; i < width; i++)
140 { 292 {
141 double x = i - width / 2; 293 rxvt_img::nv x = i - width / 2;
142 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; 294 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
143 sum += kernel[i]; 295 sum += kernel[i];
144 } 296 }
145 297
146 params[0] = XDoubleToFixed (width); 298 params[0] = XDoubleToFixed (width);
156 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 308 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
157 return clone (); 309 return clone ();
158 310
159 Display *dpy = s->display->dpy; 311 Display *dpy = s->display->dpy;
160 int size = max (rh, rv) * 2 + 1; 312 int size = max (rh, rv) * 2 + 1;
161 double *kernel = (double *)malloc (size * sizeof (double)); 313 nv *kernel = (nv *)malloc (size * sizeof (nv));
162 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 314 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
163 rxvt_img *img = new rxvt_img (s, format, w, h); 315 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
164 img->alloc (); 316 img->alloc ();
165 317
166 XRenderPictureAttributes pa; 318 XRenderPictureAttributes pa;
167
168 pa.repeat = RepeatPad; 319 pa.repeat = RepeatPad;
169 Picture src = XRenderCreatePicture (dpy, pm , format, CPRepeat, &pa); 320 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
170 Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); 321 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
171 322
172 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); 323 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
173 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); 324 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
174 XFreePixmap (dpy, tmp_pm); 325 XFreePixmap (dpy, tmp_pm);
175 326
191 342
192 size = rv * 2 + 1; 343 size = rv * 2 + 1;
193 get_gaussian_kernel (rv, size, kernel, params); 344 get_gaussian_kernel (rv, size, kernel, params);
194 ::swap (params[0], params[1]); 345 ::swap (params[0], params[1]);
195 346
196 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 347 XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2);
197 XRenderComposite (dpy, 348 XRenderComposite (dpy,
198 PictOpSrc, 349 PictOpSrc,
199 tmp, 350 tmp,
200 None, 351 None,
201 dst, 352 dst,
205 w, h); 356 w, h);
206 } 357 }
207 358
208 free (kernel); 359 free (kernel);
209 free (params); 360 free (params);
361
210 XRenderFreePicture (dpy, src); 362 XRenderFreePicture (dpy, src);
211 XRenderFreePicture (dpy, dst); 363 XRenderFreePicture (dpy, dst);
212 XRenderFreePicture (dpy, tmp); 364 XRenderFreePicture (dpy, tmp);
213 365
214 return img; 366 return img;
215} 367}
216 368
217static Picture 369static Picture
218create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) 370create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
219{ 371{
220 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); 372 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
221 373
222 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); 374 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
223 XRenderPictureAttributes pa; 375 XRenderPictureAttributes pa;
224 pa.repeat = True; 376 pa.repeat = RepeatNormal;
377 pa.component_alpha = component_alpha;
225 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); 378 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
226 379
227 XFreePixmap (dpy, pixmap); 380 XFreePixmap (dpy, pixmap);
228 381
229 return mask; 382 return mask;
230} 383}
231 384
385static void
386extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc)
387{
388 int32_t x = clamp (c, cl0, cl1);
389 c -= x;
390 xc = x;
391}
392
393static bool
394extract (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)
395{
396 extract (cl0, cl1, r, xr);
397 extract (cl0, cl1, g, xg);
398 extract (cl0, cl1, b, xb);
399 extract (cl0, cl1, a, xa);
400
401 return xr | xg | xb | xa;
402}
403
232void 404void
233rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) 405rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a)
234{ 406{
407 unshare ();
408
235 Display *dpy = s->display->dpy; 409 Display *dpy = s->display->dpy;
236 Picture src = create_xrender_mask (dpy, pm, True);
237 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 410 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
238 411
412 // loop should not be needed for brightness, as only -1..1 makes sense
413 //while (r | g | b | a)
414 {
415 unsigned short xr, xg, xb, xa;
239 XRenderColor mask_c; 416 XRenderColor mask_c;
240 mask_c.red = r; 417
241 mask_c.green = g; 418 if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
242 mask_c.blue = b; 419 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
243 mask_c.alpha = a; 420
421 if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
422 {
423 XRenderColor mask_w = { 65535, 65535, 65535, 65535 };
424 XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h);
425 mask_c.red = -mask_c.red; //TODO: verify that doing clamp, assign, and negation does the right thing
426 mask_c.green = -mask_c.green;
427 mask_c.blue = -mask_c.blue;
428 mask_c.alpha = -mask_c.alpha;
429 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
430 XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h);
431 }
432 }
433
434 XRenderFreePicture (dpy, dst);
435}
436
437void
438rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a)
439{
440 if (r < 0 || g < 0 || b < 0 || a < 0)
441 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
442
443 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
444 img->alloc ();
445 img->fill (rgba (0, 0, 0, 0));
446
447 // premultiply (yeah, these are not exact, sue me or fix it)
448 r = (r * (a >> 8)) >> 8;
449 g = (g * (a >> 8)) >> 8;
450 b = (b * (a >> 8)) >> 8;
451
452 Display *dpy = s->display->dpy;
453
454 Picture src = src_picture ();
455 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
456 Picture mul = create_xrender_mask (dpy, pm, True, True);
457
458 //TODO: this operator does not yet implement some useful contrast
459 while (r | g | b | a)
460 {
461 unsigned short xr, xg, xb, xa;
462 XRenderColor mask_c;
463
464 if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
465 {
244 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 466 XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1);
245
246 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 467 XRenderComposite (dpy, PictOpAdd, src, mul, dst, 0, 0, 0, 0, 0, 0, w, h);
468 }
469 }
470
471 XRenderFreePicture (dpy, mul);
472 XRenderFreePicture (dpy, dst);
473 XRenderFreePicture (dpy, src);
474
475 ::swap (img->ref, ref);
476 ::swap (img->pm , pm );
477
478 delete img;
479}
480
481rxvt_img *
482rxvt_img::clone ()
483{
484 return new rxvt_img (*this);
485}
486
487rxvt_img *
488rxvt_img::reify ()
489{
490 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
491 return clone ();
492
493 Display *dpy = s->display->dpy;
494
495 // add an alpha channel if...
496 bool alpha = !format->direct.alphaMask // pixmap has none yet
497 && (x || y) // we need one because of non-zero offset
498 && repeat == RepeatNone; // and we have no good pixels to fill with
499
500 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat);
501 img->alloc ();
502
503 Picture src = src_picture ();
504 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
505
506 if (alpha)
507 {
508 XRenderColor rc = { 0, 0, 0, 0 };
509 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles
510 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h);
511 }
512 else
513 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h);
247 514
248 XRenderFreePicture (dpy, src); 515 XRenderFreePicture (dpy, src);
249 XRenderFreePicture (dpy, dst); 516 XRenderFreePicture (dpy, dst);
250}
251 517
252void
253rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a)
254{
255 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
256 return; 518 return img;
519}
257 520
521rxvt_img *
522rxvt_img::sub_rect (int x, int y, int width, int height)
523{
524 rxvt_img *img = clone ();
525
526 img->x += x;
527 img->y += y;
528
529 if (w != width || h != height)
530 {
531 img->w = width;
532 img->h = height;
533
534 rxvt_img *img2 = img->reify ();
535 delete img;
536 img = img2;
537 }
538
539 return img;
540}
541
542static void
543mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3])
544{
545 rxvt_img::nv s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2];
546 rxvt_img::nv s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1];
547 rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2];
548
549 rxvt_img::nv invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2);
550
551 inv [0][0] = invdet * s0;
552 inv [0][1] = invdet * s1;
553 inv [0][2] = invdet * s2;
554
555 inv [1][0] = invdet * (mat [2][0] * mat [1][2] - mat [2][2] * mat [1][0]);
556 inv [1][1] = invdet * (mat [2][2] * mat [0][0] - mat [2][0] * mat [0][2]);
557 inv [1][2] = invdet * (mat [1][0] * mat [0][2] - mat [1][2] * mat [0][0]);
558
559 inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]);
560 inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]);
561 inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]);
562}
563
564static rxvt_img::nv
565mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y)
566{
567 rxvt_img::nv v = mat [i][0] * x + mat [i][1] * y + mat [i][2];
568 rxvt_img::nv w = mat [2][0] * x + mat [2][1] * y + mat [2][2];
569
570 return v * (1. / w);
571}
572
573rxvt_img *
574rxvt_img::transform (nv matrix[3][3])
575{
576 // find new offset
577 int ox = mat_apply (matrix, 0, x, y);
578 int oy = mat_apply (matrix, 1, x, y);
579
580 // calculate new pixel bounding box coordinates
581 nv d [2], rmin[2], rmax[2];
582
583 for (int i = 0; i < 2; ++i)
584 {
585 nv v;
586 v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v;
587 v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v);
588 v = mat_apply (matrix, i, 0, h); min_it (rmin [i], v); max_it (rmax [i], v);
589 v = mat_apply (matrix, i, w, h); min_it (rmin [i], v); max_it (rmax [i], v);
590 }
591
592 int dx = floor (rmin [0]);
593 int dy = floor (rmin [1]);
594
595 int new_width = ceil (rmax [0] - dx);
596 int new_height = ceil (rmax [1] - dy);
597
598 nv inv[3][3];
599 mat_invert (matrix, inv);
600
601 rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat);
602 img->alloc ();
603
258 Display *dpy = s->display->dpy; 604 Display *dpy = s->display->dpy;
259 Picture src = create_xrender_mask (dpy, pm, True); 605 Picture src = src_picture ();
260 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 606 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
261 607
262 XRenderColor mask_c; 608 XTransform xfrm;
263 mask_c.red = r;
264 mask_c.green = g;
265 mask_c.blue = b;
266 mask_c.alpha = a;
267 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
268 609
610 for (int i = 0; i < 3; ++i)
611 for (int j = 0; j < 3; ++j)
612 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
613
614 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
615 XRenderSetPictureTransform (dpy, src, &xfrm);
269 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 616 XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height);
270 617
271 XRenderFreePicture (dpy, src); 618 XRenderFreePicture (dpy, src);
272 XRenderFreePicture (dpy, dst); 619 XRenderFreePicture (dpy, dst);
273}
274 620
275bool 621 return img;
276rxvt_img::render_pixbuf (GdkPixbuf *pixbuf, int src_x, int src_y, int width, int height, int dst_x, int dst_y) 622}
277{
278 Display *dpy = s->display->dpy;
279 623
280 if (s->visual->c_class != TrueColor) 624rxvt_img *
625rxvt_img::scale (int new_width, int new_height)
626{
627 if (w == new_width && h == new_height)
281 return false; 628 return clone ();
282 629
283 uint32_t red_mask, green_mask, blue_mask, alpha_mask; 630 nv matrix[3][3] = {
631 { new_width / (nv)w, 0, 0 },
632 { 0, new_height / (nv)h, 0 },
633 { 0, 0, 1 }
634 };
284 635
285 red_mask = (uint32_t)format->direct.redMask << format->direct.red; 636 int old_repeat_mode = repeat;
286 green_mask = (uint32_t)format->direct.greenMask << format->direct.green; 637 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
287 blue_mask = (uint32_t)format->direct.blueMask << format->direct.blue;
288 alpha_mask = (uint32_t)format->direct.alphaMask << format->direct.alpha;
289 638
290 int width_r = ecb_popcount32 (red_mask); 639 rxvt_img *img = transform (matrix);
291 int width_g = ecb_popcount32 (green_mask);
292 int width_b = ecb_popcount32 (blue_mask);
293 int width_a = ecb_popcount32 (alpha_mask);
294 640
295 if (width_r > 8 || width_g > 8 || width_b > 8 || width_a > 8) 641 repeat = old_repeat_mode;
642 img->repeat = repeat;
643
644 return img;
645}
646
647rxvt_img *
648rxvt_img::rotate (int cx, int cy, nv phi)
649{
650 nv s = sin (phi);
651 nv c = cos (phi);
652
653 nv matrix[3][3] = {
654 { c, -s, cx - c * cx + s * cy },
655 { s, c, cy - s * cx - c * cy },
656 { 0, 0, 1 }
657 //{ c, -s, 0 },
658 //{ s, c, 0 },
659 //{ 0, 0, 1 }
660 };
661
662 //move (-cx, -cy);
663 rxvt_img *img = transform (matrix);
664 //move ( cx, cy);
665 //img->move (cx, cy);
666
667 return img;
668}
669
670rxvt_img *
671rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
672{
673 if (new_format == format)
296 return false; 674 return clone ();
297 675
298 int sh_r = ecb_ctz32 (red_mask);
299 int sh_g = ecb_ctz32 (green_mask);
300 int sh_b = ecb_ctz32 (blue_mask);
301 int sh_a = ecb_ctz32 (alpha_mask);
302
303 if (width > 32767 || height > 32767)
304 return false;
305
306 XImage *ximage = XCreateImage (dpy, s->visual, format->depth, ZPixmap, 0, 0,
307 width, height, 32, 0);
308 if (!ximage)
309 return false;
310
311 if (height > INT_MAX / ximage->bytes_per_line
312 || !(ximage->data = (char *)malloc (height * ximage->bytes_per_line)))
313 {
314 XDestroyImage (ximage);
315 return false;
316 }
317
318 GC gc = XCreateGC (dpy, pm, 0, 0);
319
320 ximage->byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
321
322 int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
323 int channels = gdk_pixbuf_get_n_channels (pixbuf);
324 unsigned char *row = gdk_pixbuf_get_pixels (pixbuf) + src_y * rowstride + src_x * channels;
325 char *line = ximage->data;
326
327 for (int y = 0; y < height; y++)
328 {
329 for (int x = 0; x < width; x++)
330 {
331 unsigned char *pixel = row + x * channels;
332 uint32_t value;
333 unsigned char r, g, b, a;
334
335 if (channels == 4)
336 {
337 a = pixel[3];
338 r = pixel[0] * a / 0xff;
339 g = pixel[1] * a / 0xff;
340 b = pixel[2] * a / 0xff;
341 }
342 else
343 {
344 a = 0xff;
345 r = pixel[0];
346 g = pixel[1];
347 b = pixel[2];
348 }
349
350 value = ((r >> (8 - width_r)) << sh_r)
351 | ((g >> (8 - width_g)) << sh_g)
352 | ((b >> (8 - width_b)) << sh_b)
353 | ((a >> (8 - width_a)) << sh_a);
354
355 if (ximage->bits_per_pixel == 32)
356 ((uint32_t *)line)[x] = value;
357 else
358 XPutPixel (ximage, x, y, value);
359 }
360
361 row += rowstride;
362 line += ximage->bytes_per_line;
363 }
364
365 XPutImage (dpy, pm, gc, ximage, 0, 0, dst_x, dst_y, width, height);
366 XDestroyImage (ximage);
367 XFreeGC (dpy, gc);
368
369 return true;
370}
371
372rxvt_img *
373rxvt_img::clone ()
374{
375 return new rxvt_img (*this);
376}
377
378rxvt_img *
379rxvt_img::sub_rect (int x, int y, int width, int height)
380{
381 rxvt_img *img = new rxvt_img (s, format, width, height); 676 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
382 img->alloc (); 677 img->alloc ();
383 678
384 Display *dpy = s->display->dpy; 679 Display *dpy = s->display->dpy;
385 XRenderPictureAttributes pa; 680 Picture src = src_picture ();
386 pa.repeat = repeat;
387 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
388 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 681 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
682 int op = PictOpSrc;
389 683
684 if (format->direct.alphaMask && !new_format->direct.alphaMask)
685 {
686 // does it have to be that complicated
687 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
688 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
689
690 op = PictOpOver;
691 }
692
390 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, width, height); 693 XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
391 694
392 XRenderFreePicture (dpy, src); 695 XRenderFreePicture (dpy, src);
393 XRenderFreePicture (dpy, dst); 696 XRenderFreePicture (dpy, dst);
394 697
395 return img; 698 return img;
396} 699}
397 700
398rxvt_img * 701rxvt_img *
399rxvt_img::transform (int new_width, int new_height, double matrix[9])
400{
401 rxvt_img *img = new rxvt_img (s, format, new_width, new_height);
402 img->alloc ();
403
404 Display *dpy = s->display->dpy;
405 XRenderPictureAttributes pa;
406 pa.repeat = repeat;
407 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
408 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
409
410 XTransform xfrm;
411
412 for (int i = 0; i < 3; ++i)
413 for (int j = 0; j < 3; ++j)
414 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]);
415
416 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
417 XRenderSetPictureTransform (dpy, src, &xfrm);
418 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height);
419
420 XRenderFreePicture (dpy, src);
421 XRenderFreePicture (dpy, dst);
422
423 return img;
424}
425
426rxvt_img *
427rxvt_img::scale (int new_width, int new_height)
428{
429 double matrix[9] = {
430 w / (double)new_width, 0, 0,
431 0, h / (double)new_height, 0,
432 0, 0, 1
433 };
434
435 return transform (new_width, new_height, matrix);
436}
437
438rxvt_img *
439rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi)
440{
441 double s = sin (phi);
442 double c = cos (phi);
443
444 double matrix[9] = {
445 c, -s, -c * x + s * y + x,
446 s, c, -s * x - c * y + y,
447 0, 0, 1
448 };
449
450 return transform (new_width, new_height, matrix);
451}
452
453rxvt_img *
454rxvt_img::convert_to (XRenderPictFormat *new_format, const rxvt_color &bg)
455{
456 if (new_format == format)
457 return clone ();
458
459 rxvt_img *img = new rxvt_img (s, new_format, w, h);
460 img->alloc ();
461
462 Display *dpy = s->display->dpy;
463 Picture src = XRenderCreatePicture (dpy, pm, format, 0, 0);
464 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
465 int op = PictOpSrc;
466
467 if (format->direct.alphaMask && !new_format->direct.alphaMask)
468 {
469 // does it have to be that complicated
470 rgba c;
471 bg.get (c);
472
473 XRenderColor rc = { c.r, c.g, c.b, 0xffff };
474 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
475
476 op = PictOpOver;
477 }
478
479 XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
480
481 XRenderFreePicture (dpy, src);
482 XRenderFreePicture (dpy, dst);
483
484 return img;
485}
486
487rxvt_img *
488rxvt_img::blend (rxvt_img *img, double factor) 702rxvt_img::blend (rxvt_img *img, nv factor)
489{ 703{
490 rxvt_img *img2 = clone (); 704 rxvt_img *img2 = clone ();
491 Display *dpy = s->display->dpy; 705 Display *dpy = s->display->dpy;
492 Picture src = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 706 Picture src = img->src_picture ();
493 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 707 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
494 Picture mask = create_xrender_mask (dpy, img->pm, False); 708 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
495 709
496 XRenderColor mask_c; 710 XRenderColor mask_c;
497 711
498 mask_c.alpha = float_to_component (factor); 712 mask_c.alpha = float_to_component (factor);
499 mask_c.red = 713 mask_c.red =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines