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