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