ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
Revision: 1.94
Committed: Fri Jun 15 18:10:40 2012 UTC (11 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.93: +0 -10 lines
Log Message:
*** empty log message ***

File Contents

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