ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
Revision: 1.85
Committed: Thu Jun 14 18:59:39 2012 UTC (11 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.84: +66 -8 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 sf-exg 1.6 #include <math.h>
2 root 1.1 #include "../config.h"
3     #include "rxvt.h"
4    
5     #if HAVE_IMG
6    
7 root 1.85 #if 0
8     struct pict
9     {
10     Display *dpy;
11     Picture pic;
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    
39 root 1.83 static XRenderPictFormat *
40     find_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    
59 root 1.41 rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat)
60     : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat),
61 root 1.34 pm(0), ref(0)
62 root 1.1 {
63     }
64    
65 root 1.32 rxvt_img::rxvt_img (const rxvt_img &img)
66 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)
67 root 1.32 {
68 root 1.34 ++ref->cnt;
69 root 1.32 }
70    
71 root 1.4 rxvt_img *
72 sf-exg 1.21 rxvt_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 root 1.33 0,
94     0,
95 sf-exg 1.21 root_pm_w,
96 root 1.32 root_pm_h
97 sf-exg 1.21 );
98    
99 root 1.32 img->pm = root_pixmap;
100 root 1.34 img->ref = new pixref (root_pm_w, root_pm_h);
101     img->ref->ours = false;
102 sf-exg 1.21
103     return img;
104     }
105    
106 sf-exg 1.73 # if HAVE_PIXBUF
107 root 1.77
108 sf-exg 1.21 rxvt_img *
109 root 1.47 rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb)
110     {
111 root 1.48 Display *dpy = s->display->dpy;
112    
113 root 1.47 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 root 1.54 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
123    
124 root 1.47 XImage xi;
125    
126     xi.width = width;
127     xi.height = height;
128     xi.xoffset = 0;
129     xi.format = ZPixmap;
130 root 1.54 xi.byte_order = ImageByteOrder (dpy);
131 root 1.55 xi.bitmap_unit = 0; //XY only, unused
132     xi.bitmap_bit_order = 0; //XY only, unused
133 root 1.48 xi.bitmap_pad = BitmapPad (dpy);
134 root 1.76 xi.depth = 32;
135 root 1.47 xi.bytes_per_line = 0;
136 root 1.51 xi.bits_per_pixel = 32; //Z only
137 root 1.52 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 root 1.47
142 root 1.54 bool byte_order_mismatch = byte_order != xi.byte_order;
143    
144 root 1.47 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 root 1.76 bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb);
154     unsigned char *row = gdk_pixbuf_get_pixels (pb);
155 root 1.47
156     char *line = xi.data;
157    
158     for (int y = 0; y < height; y++)
159     {
160 root 1.50 unsigned char *src = row;
161     uint32_t *dst = (uint32_t *)line;
162 root 1.47
163 root 1.76 if (!pb_has_alpha)
164 root 1.47 for (int x = 0; x < width; x++)
165     {
166 root 1.50 uint8_t r = *src++;
167     uint8_t g = *src++;
168     uint8_t b = *src++;
169    
170 root 1.76 uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b;
171 root 1.50
172 root 1.56 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch)
173 root 1.54 v = ecb_bswap32 (v);
174    
175 root 1.51 *dst++ = v;
176 root 1.47 }
177     else
178     for (int x = 0; x < width; x++)
179     {
180 root 1.50 uint32_t v = *(uint32_t *)src; src += 4;
181 root 1.51
182 root 1.57 if (ecb_big_endian ())
183 root 1.51 v = ecb_bswap32 (v);
184    
185 root 1.58 v = ecb_rotl32 (v, 8); // abgr to bgra
186 root 1.52
187 root 1.57 if (!byte_order_mismatch)
188 root 1.54 v = ecb_bswap32 (v);
189    
190 root 1.51 *dst++ = v;
191 root 1.47 }
192    
193     row += rowstride;
194 root 1.50 line += xi.bytes_per_line;
195 root 1.47 }
196    
197 root 1.76 rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height);
198 root 1.47 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;
207     }
208    
209     rxvt_img *
210 root 1.4 rxvt_img::new_from_file (rxvt_screen *s, const char *filename)
211     {
212 root 1.11 GError *err = 0;
213 root 1.4 GdkPixbuf *pb = gdk_pixbuf_new_from_file (filename, &err);
214    
215     if (!pb)
216 root 1.11 rxvt_fatal ("rxvt_img::new_from_file: %s\n", err->message);
217 root 1.4
218 root 1.47 rxvt_img *img = new_from_pixbuf (s, pb);
219 root 1.4
220 sf-exg 1.26 g_object_unref (pb);
221    
222 root 1.4 return img;
223     }
224 root 1.77
225 sf-exg 1.73 # endif
226 root 1.4
227 root 1.32 void
228     rxvt_img::destroy ()
229     {
230 root 1.34 if (--ref->cnt)
231 root 1.32 return;
232    
233 root 1.34 if (pm && ref->ours)
234 root 1.32 XFreePixmap (s->display->dpy, pm);
235    
236 root 1.34 delete ref;
237 root 1.32 }
238    
239 root 1.1 rxvt_img::~rxvt_img ()
240     {
241 root 1.32 destroy ();
242     }
243    
244     void
245     rxvt_img::alloc ()
246     {
247     pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
248 root 1.34 ref = new pixref (w, h);
249     }
250    
251     Picture
252 root 1.85 rxvt_img::picture ()
253 root 1.34 {
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;
261 root 1.1 }
262    
263     void
264 root 1.11 rxvt_img::unshare ()
265     {
266 root 1.34 if (ref->cnt == 1 && ref->ours)
267 root 1.11 return;
268    
269 root 1.34 Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth);
270 root 1.32 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
271 root 1.34 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0);
272 root 1.32 XFreeGC (s->display->dpy, gc);
273 root 1.11
274 root 1.32 destroy ();
275 root 1.11
276 root 1.32 pm = pm2;
277 root 1.34 ref = new pixref (ref->w, ref->h);
278 root 1.11 }
279    
280     void
281 root 1.82 rxvt_img::fill (const rgba &c)
282 root 1.1 {
283 root 1.82 XRenderColor rc = { c.r, c.g, c.b, c.a };
284 root 1.77
285     Display *dpy = s->display->dpy;
286 root 1.85 Picture src = picture ();
287 root 1.77 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
288     XRenderFreePicture (dpy, src);
289 root 1.1 }
290    
291 root 1.83 void
292     rxvt_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 root 1.85 Picture src = picture ();
303 root 1.83 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 );
312    
313     delete img;
314     }
315    
316 sf-exg 1.6 static void
317 root 1.84 get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params)
318 sf-exg 1.6 {
319 root 1.84 rxvt_img::nv sigma = radius / 2.0;
320     rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma;
321     rxvt_img::nv sum = 0.0;
322 sf-exg 1.6
323     for (int i = 0; i < width; i++)
324     {
325 root 1.84 rxvt_img::nv x = i - width / 2;
326 sf-exg 1.6 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
327     sum += kernel[i];
328     }
329    
330     params[0] = XDoubleToFixed (width);
331     params[1] = XDoubleToFixed (1);
332    
333     for (int i = 0; i < width; i++)
334     params[i+2] = XDoubleToFixed (kernel[i] / sum);
335     }
336    
337 sf-exg 1.22 rxvt_img *
338 root 1.3 rxvt_img::blur (int rh, int rv)
339     {
340 sf-exg 1.6 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
341 sf-exg 1.22 return clone ();
342 sf-exg 1.6
343     Display *dpy = s->display->dpy;
344     int size = max (rh, rv) * 2 + 1;
345 root 1.84 nv *kernel = (nv *)malloc (size * sizeof (nv));
346 sf-exg 1.6 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
347 root 1.41 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
348 root 1.32 img->alloc ();
349 sf-exg 1.6
350     XRenderPictureAttributes pa;
351     pa.repeat = RepeatPad;
352 sf-exg 1.61 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
353     Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
354 sf-exg 1.22
355     Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
356     Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
357     XFreePixmap (dpy, tmp_pm);
358 sf-exg 1.6
359     if (kernel && params)
360     {
361     size = rh * 2 + 1;
362     get_gaussian_kernel (rh, size, kernel, params);
363    
364     XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
365     XRenderComposite (dpy,
366     PictOpSrc,
367     src,
368     None,
369 sf-exg 1.22 tmp,
370 sf-exg 1.6 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 sf-exg 1.60 XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2);
380 sf-exg 1.6 XRenderComposite (dpy,
381     PictOpSrc,
382 sf-exg 1.22 tmp,
383 sf-exg 1.6 None,
384     dst,
385     0, 0,
386     0, 0,
387     0, 0,
388     w, h);
389     }
390    
391     free (kernel);
392     free (params);
393 root 1.77
394 sf-exg 1.6 XRenderFreePicture (dpy, src);
395     XRenderFreePicture (dpy, dst);
396 sf-exg 1.22 XRenderFreePicture (dpy, tmp);
397    
398     return img;
399 sf-exg 1.6 }
400    
401 root 1.74 static Picture
402 sf-exg 1.75 create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
403 root 1.74 {
404     Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
405    
406     XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
407     XRenderPictureAttributes pa;
408     pa.repeat = RepeatNormal;
409 sf-exg 1.75 pa.component_alpha = component_alpha;
410     Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
411 root 1.74
412     XFreePixmap (dpy, pixmap);
413    
414     return mask;
415     }
416    
417 root 1.67 static void
418     extract (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    
425     static bool
426     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)
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    
436 root 1.3 void
437 root 1.67 rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a)
438 root 1.3 {
439 root 1.67 unshare ();
440    
441 sf-exg 1.6 Display *dpy = s->display->dpy;
442     Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
443    
444 root 1.69 // loop should not be needed for brightness, as only -1..1 makes sense
445 root 1.70 //while (r | g | b | a)
446 root 1.67 {
447     unsigned short xr, xg, xb, xa;
448     XRenderColor mask_c;
449    
450     if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
451     XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
452    
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 root 1.70 mask_c.red = -mask_c.red; //TODO: verify that doing clamp, assign, and negation does the right thing
458 root 1.68 mask_c.green = -mask_c.green;
459     mask_c.blue = -mask_c.blue;
460     mask_c.alpha = -mask_c.alpha;
461 root 1.67 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
462     XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h);
463     }
464     }
465 sf-exg 1.6
466 sf-exg 1.23 XRenderFreePicture (dpy, dst);
467 root 1.3 }
468    
469     void
470 root 1.67 rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a)
471 root 1.1 {
472 root 1.74 if (r < 0 || g < 0 || b < 0 || a < 0)
473     rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
474    
475     rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
476     img->alloc ();
477 root 1.82 img->fill (rgba (0, 0, 0, 0));
478 root 1.74
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 root 1.67
484 sf-exg 1.6 Display *dpy = s->display->dpy;
485    
486 root 1.85 Picture src = picture ();
487 root 1.74 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
488 sf-exg 1.75 Picture mul = create_xrender_mask (dpy, pm, True, True);
489 root 1.74
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;
494     XRenderColor mask_c;
495    
496     if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
497     {
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 sf-exg 1.6
503 root 1.74 XRenderFreePicture (dpy, mul);
504 sf-exg 1.23 XRenderFreePicture (dpy, dst);
505 root 1.74 XRenderFreePicture (dpy, src);
506    
507     ::swap (img->ref, ref);
508     ::swap (img->pm , pm );
509    
510     delete img;
511 root 1.1 }
512    
513 root 1.85 void
514     rxvt_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) };
527     XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
528     }
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)
536     XRenderFreePicture (dpy, mask_p);
537     }
538    
539 root 1.3 rxvt_img *
540 root 1.11 rxvt_img::clone ()
541 root 1.3 {
542 root 1.32 return new rxvt_img (*this);
543 root 1.3 }
544    
545     rxvt_img *
546 root 1.33 rxvt_img::reify ()
547 root 1.19 {
548 root 1.35 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
549     return clone ();
550    
551 root 1.40 Display *dpy = s->display->dpy;
552    
553 root 1.62 // 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 root 1.40
558 root 1.46 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat);
559 root 1.32 img->alloc ();
560 root 1.19
561 root 1.85 Picture src = picture ();
562 root 1.34 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
563 root 1.33
564 root 1.42 if (alpha)
565     {
566     XRenderColor rc = { 0, 0, 0, 0 };
567 root 1.44 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles
568 root 1.42 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h);
569     }
570     else
571     XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h);
572    
573 root 1.33 XRenderFreePicture (dpy, src);
574     XRenderFreePicture (dpy, dst);
575 root 1.19
576 root 1.33 return img;
577     }
578 root 1.19
579 root 1.33 rxvt_img *
580     rxvt_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 root 1.19
587 root 1.37 if (w != width || h != height)
588     {
589     img->w = width;
590     img->h = height;
591    
592 root 1.39 rxvt_img *img2 = img->reify ();
593     delete img;
594     img = img2;
595 root 1.37 }
596 root 1.36
597 root 1.19 return img;
598     }
599    
600 root 1.80 static void
601 root 1.84 mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3])
602 root 1.80 {
603 root 1.84 rxvt_img::nv s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2];
604     rxvt_img::nv s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1];
605     rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2];
606 root 1.80
607 root 1.84 rxvt_img::nv invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2);
608 root 1.80
609     inv [0][0] = invdet * s0;
610     inv [0][1] = invdet * s1;
611     inv [0][2] = invdet * s2;
612    
613     inv [1][0] = invdet * (mat [2][0] * mat [1][2] - mat [2][2] * mat [1][0]);
614     inv [1][1] = invdet * (mat [2][2] * mat [0][0] - mat [2][0] * mat [0][2]);
615     inv [1][2] = invdet * (mat [1][0] * mat [0][2] - mat [1][2] * mat [0][0]);
616    
617     inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]);
618     inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]);
619     inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]);
620     }
621    
622 root 1.84 static rxvt_img::nv
623     mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y)
624 root 1.80 {
625 root 1.84 rxvt_img::nv v = mat [i][0] * x + mat [i][1] * y + mat [i][2];
626     rxvt_img::nv w = mat [2][0] * x + mat [2][1] * y + mat [2][2];
627 root 1.80
628     return v * (1. / w);
629     }
630    
631 root 1.19 rxvt_img *
632 root 1.84 rxvt_img::transform (nv matrix[3][3])
633 root 1.3 {
634 root 1.80 // find new offset
635     int ox = mat_apply (matrix, 0, x, y);
636     int oy = mat_apply (matrix, 1, x, y);
637    
638     // calculate new pixel bounding box coordinates
639 root 1.84 nv d [2], rmin[2], rmax[2];
640 root 1.80
641     for (int i = 0; i < 2; ++i)
642     {
643 root 1.84 nv v;
644 root 1.81 v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v;
645 root 1.80 v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v);
646     v = mat_apply (matrix, i, 0, h); min_it (rmin [i], v); max_it (rmax [i], v);
647     v = mat_apply (matrix, i, w, h); min_it (rmin [i], v); max_it (rmax [i], v);
648     }
649    
650     int dx = floor (rmin [0]);
651     int dy = floor (rmin [1]);
652    
653     int new_width = ceil (rmax [0] - dx);
654     int new_height = ceil (rmax [1] - dy);
655    
656 root 1.84 nv inv[3][3];
657 root 1.80 mat_invert (matrix, inv);
658    
659 root 1.81 rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat);
660 root 1.32 img->alloc ();
661 root 1.12
662     Display *dpy = s->display->dpy;
663 root 1.85 Picture src = picture ();
664 root 1.34 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
665 root 1.12
666     XTransform xfrm;
667    
668     for (int i = 0; i < 3; ++i)
669     for (int j = 0; j < 3; ++j)
670 root 1.80 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
671 root 1.33
672 root 1.17 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
673 root 1.12 XRenderSetPictureTransform (dpy, src, &xfrm);
674 root 1.80 XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height);
675 root 1.12
676     XRenderFreePicture (dpy, src);
677     XRenderFreePicture (dpy, dst);
678    
679     return img;
680 root 1.3 }
681    
682     rxvt_img *
683     rxvt_img::scale (int new_width, int new_height)
684     {
685 sf-exg 1.43 if (w == new_width && h == new_height)
686     return clone ();
687    
688 root 1.84 nv matrix[3][3] = {
689     { new_width / (nv)w, 0, 0 },
690     { 0, new_height / (nv)h, 0 },
691     { 0, 0, 1 }
692 root 1.11 };
693    
694 root 1.42 int old_repeat_mode = repeat;
695 sf-exg 1.78 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
696 root 1.42
697 root 1.80 rxvt_img *img = transform (matrix);
698 root 1.42
699     repeat = old_repeat_mode;
700     img->repeat = repeat;
701    
702     return img;
703 root 1.3 }
704    
705 sf-exg 1.7 rxvt_img *
706 root 1.84 rxvt_img::rotate (int cx, int cy, nv phi)
707 root 1.17 {
708 root 1.84 nv s = sin (phi);
709     nv c = cos (phi);
710 root 1.17
711 root 1.84 nv matrix[3][3] = {
712 root 1.81 { c, -s, cx - c * cx + s * cy },
713     { s, c, cy - s * cx - c * cy },
714     { 0, 0, 1 }
715     //{ c, -s, 0 },
716     //{ s, c, 0 },
717     //{ 0, 0, 1 }
718 root 1.17 };
719    
720 root 1.81 //move (-cx, -cy);
721 root 1.80 rxvt_img *img = transform (matrix);
722 root 1.81 //move ( cx, cy);
723     //img->move (cx, cy);
724 root 1.80
725     return img;
726 root 1.17 }
727    
728     rxvt_img *
729 root 1.82 rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
730 sf-exg 1.7 {
731 root 1.32 if (new_format == format)
732     return clone ();
733    
734 root 1.45 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
735 root 1.32 img->alloc ();
736 root 1.9
737 sf-exg 1.7 Display *dpy = s->display->dpy;
738 root 1.85 Picture src = picture ();
739 root 1.8 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
740 root 1.27 int op = PictOpSrc;
741 sf-exg 1.7
742 root 1.29 if (format->direct.alphaMask && !new_format->direct.alphaMask)
743 root 1.27 {
744     // does it have to be that complicated
745 root 1.82 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
746 root 1.28 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
747 root 1.27
748     op = PictOpOver;
749     }
750    
751     XRenderComposite (dpy, op, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
752 sf-exg 1.7
753     XRenderFreePicture (dpy, src);
754     XRenderFreePicture (dpy, dst);
755    
756     return img;
757     }
758 root 1.3
759 sf-exg 1.24 rxvt_img *
760 root 1.84 rxvt_img::blend (rxvt_img *img, nv factor)
761 sf-exg 1.24 {
762     rxvt_img *img2 = clone ();
763     Display *dpy = s->display->dpy;
764 root 1.85 Picture src = img->picture ();
765 sf-exg 1.24 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
766 sf-exg 1.75 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
767 sf-exg 1.24
768     XRenderColor mask_c;
769    
770     mask_c.alpha = float_to_component (factor);
771     mask_c.red =
772     mask_c.green =
773     mask_c.blue = 0;
774     XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
775    
776     XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, w, h);
777    
778     XRenderFreePicture (dpy, src);
779     XRenderFreePicture (dpy, dst);
780     XRenderFreePicture (dpy, mask);
781    
782     return img2;
783     }
784    
785 root 1.1 #endif
786