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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines