ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtimg.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtimg.C (file contents):
Revision 1.52 by root, Thu Jun 7 20:20:35 2012 UTC vs.
Revision 1.83 by root, Thu Jun 14 18:06:15 2012 UTC

2#include "../config.h" 2#include "../config.h"
3#include "rxvt.h" 3#include "rxvt.h"
4 4
5#if HAVE_IMG 5#if HAVE_IMG
6 6
7static XRenderPictFormat *
8find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
9{
10 if (format->direct.alphaMask)
11 return format; // already has alpha
12
13 // try to find a suitable alpha format, one bit alpha is enough for our purposes
14 if (format->type == PictTypeDirect)
15 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
16 if (f->direct.alphaMask
17 && f->type == PictTypeDirect
18 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
19 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
20 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
21 return f;
22
23 // should be a very good fallback
24 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
25}
26
7rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) 27rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat)
8: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), 28: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat),
9 pm(0), ref(0) 29 pm(0), ref(0)
10{ 30{
11} 31}
13rxvt_img::rxvt_img (const rxvt_img &img) 33rxvt_img::rxvt_img (const rxvt_img &img)
14: 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) 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)
15{ 35{
16 ++ref->cnt; 36 ++ref->cnt;
17} 37}
18
19#if 0
20rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int width, int height, Pixmap pixmap)
21: s(screen), x(0), y(0), w(width), h(height), format(format), repeat(RepeatNormal), shared(false), pm(pixmap)
22{
23}
24#endif
25 38
26rxvt_img * 39rxvt_img *
27rxvt_img::new_from_root (rxvt_screen *s) 40rxvt_img::new_from_root (rxvt_screen *s)
28{ 41{
29 Display *dpy = s->display->dpy; 42 Display *dpy = s->display->dpy;
56 img->ref->ours = false; 69 img->ref->ours = false;
57 70
58 return img; 71 return img;
59} 72}
60 73
74# if HAVE_PIXBUF
75
61rxvt_img * 76rxvt_img *
62rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) 77rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb)
63{ 78{
64 Display *dpy = s->display->dpy; 79 Display *dpy = s->display->dpy;
65 80
69 if (width > 32767 || height > 32767) // well, we *could* upload in chunks 84 if (width > 32767 || height > 32767) // well, we *could* upload in chunks
70 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); 85 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n");
71 86
72 // since we require rgb24/argb32 formats from xrender we assume 87 // since we require rgb24/argb32 formats from xrender we assume
73 // that both 24 and 32 bpp MUST be supported by any screen that supports xrender 88 // that both 24 and 32 bpp MUST be supported by any screen that supports xrender
74 int depth = gdk_pixbuf_get_has_alpha (pb) ? 32 : 24; 89
90 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
75 91
76 XImage xi; 92 XImage xi;
77 93
78 xi.width = width; 94 xi.width = width;
79 xi.height = height; 95 xi.height = height;
80 xi.xoffset = 0; 96 xi.xoffset = 0;
81 xi.format = ZPixmap; 97 xi.format = ZPixmap;
82 xi.byte_order = LSBFirst; // maybe go for host byte order, because servers are usually local? 98 xi.byte_order = ImageByteOrder (dpy);
83 xi.bitmap_unit = 32; //XY only, unused 99 xi.bitmap_unit = 0; //XY only, unused
84 xi.bitmap_bit_order = LSBFirst; //XY only, unused 100 xi.bitmap_bit_order = 0; //XY only, unused
85 xi.bitmap_pad = BitmapPad (dpy); 101 xi.bitmap_pad = BitmapPad (dpy);
86 xi.depth = depth; 102 xi.depth = 32;
87 xi.bytes_per_line = 0; 103 xi.bytes_per_line = 0;
88 xi.bits_per_pixel = 32; //Z only 104 xi.bits_per_pixel = 32; //Z only
89 xi.red_mask = 0x00000000; //Z only, unused 105 xi.red_mask = 0x00000000; //Z only, unused
90 xi.green_mask = 0x00000000; //Z only, unused 106 xi.green_mask = 0x00000000; //Z only, unused
91 xi.blue_mask = 0x00000000; //Z only, unused 107 xi.blue_mask = 0x00000000; //Z only, unused
92 xi.obdata = 0; // probably unused 108 xi.obdata = 0; // probably unused
93 109
110 bool byte_order_mismatch = byte_order != xi.byte_order;
111
94 if (!XInitImage (&xi)) 112 if (!XInitImage (&xi))
95 rxvt_fatal ("unable to initialise ximage, please report.\n"); 113 rxvt_fatal ("unable to initialise ximage, please report.\n");
96 114
97 if (height > INT_MAX / xi.bytes_per_line) 115 if (height > INT_MAX / xi.bytes_per_line)
98 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); 116 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n");
99 117
100 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); 118 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line);
101 119
102 int rowstride = gdk_pixbuf_get_rowstride (pb); 120 int rowstride = gdk_pixbuf_get_rowstride (pb);
103 121 bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb);
104 assert (3 + (depth == 32) == gdk_pixbuf_get_n_channels (pb));
105 unsigned char *row = gdk_pixbuf_get_pixels (pb); 122 unsigned char *row = gdk_pixbuf_get_pixels (pb);
123
106 char *line = xi.data; 124 char *line = xi.data;
107 125
108 for (int y = 0; y < height; y++) 126 for (int y = 0; y < height; y++)
109 { 127 {
110 unsigned char *src = row; 128 unsigned char *src = row;
111 uint32_t *dst = (uint32_t *)line; 129 uint32_t *dst = (uint32_t *)line;
112 130
113 if (depth == 24) 131 if (!pb_has_alpha)
114 for (int x = 0; x < width; x++) 132 for (int x = 0; x < width; x++)
115 { 133 {
116 uint8_t r = *src++; 134 uint8_t r = *src++;
117 uint8_t g = *src++; 135 uint8_t g = *src++;
118 uint8_t b = *src++; 136 uint8_t b = *src++;
119 137
120 uint32_t v = r | (g << 8) | (b << 16); 138 uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b;
121 139
122 if (ecb_big_endian ()) 140 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch)
123 v = ecb_bswap32 (v); 141 v = ecb_bswap32 (v);
124 142
125 *dst++ = v; 143 *dst++ = v;
126 } 144 }
127 else 145 else
128 for (int x = 0; x < width; x++) 146 for (int x = 0; x < width; x++)
129 { 147 {
130 uint32_t v = *(uint32_t *)src; src += 4; 148 uint32_t v = *(uint32_t *)src; src += 4;
131 149
132 if (ecb_little_endian ()) 150 if (ecb_big_endian ())
133 v = ecb_bswap32 (v); 151 v = ecb_bswap32 (v);
134 152
153 v = ecb_rotl32 (v, 8); // abgr to bgra
154
155 if (!byte_order_mismatch)
135 v = ecb_rotr32 (v, 8); 156 v = ecb_bswap32 (v);
136 157
137 *dst++ = v; 158 *dst++ = v;
138 } 159 }
139 160
140 row += rowstride; 161 row += rowstride;
141 line += xi.bytes_per_line; 162 line += xi.bytes_per_line;
142 } 163 }
143 164
144 rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, depth == 24 ? PictStandardRGB24 : PictStandardARGB32), 0, 0, width, height); 165 rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height);
145 img->alloc (); 166 img->alloc ();
146 167
147 GC gc = XCreateGC (dpy, img->pm, 0, 0); 168 GC gc = XCreateGC (dpy, img->pm, 0, 0);
148 XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); 169 XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height);
149 XFreeGC (dpy, gc); 170 XFreeGC (dpy, gc);
167 g_object_unref (pb); 188 g_object_unref (pb);
168 189
169 return img; 190 return img;
170} 191}
171 192
193# endif
194
172void 195void
173rxvt_img::destroy () 196rxvt_img::destroy ()
174{ 197{
175 if (--ref->cnt) 198 if (--ref->cnt)
176 return; 199 return;
209rxvt_img::unshare () 232rxvt_img::unshare ()
210{ 233{
211 if (ref->cnt == 1 && ref->ours) 234 if (ref->cnt == 1 && ref->ours)
212 return; 235 return;
213 236
214 //TODO: maybe should reify instead
215 Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth); 237 Pixmap pm2 = XCreatePixmap (s->display->dpy, s->display->root, ref->w, ref->h, format->depth);
216 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 238 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
217 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0); 239 XCopyArea (s->display->dpy, pm, pm2, gc, 0, 0, ref->w, ref->h, 0, 0);
218 XFreeGC (s->display->dpy, gc); 240 XFreeGC (s->display->dpy, gc);
219 241
222 pm = pm2; 244 pm = pm2;
223 ref = new pixref (ref->w, ref->h); 245 ref = new pixref (ref->w, ref->h);
224} 246}
225 247
226void 248void
227rxvt_img::fill (const rxvt_color &c) 249rxvt_img::fill (const rgba &c)
228{ 250{
229 XGCValues gcv; 251 XRenderColor rc = { c.r, c.g, c.b, c.a };
230 gcv.foreground = c; 252
231 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 253 Display *dpy = s->display->dpy;
232 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 254 Picture src = src_picture ();
233 XFreeGC (s->display->dpy, gc); 255 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
256 XRenderFreePicture (dpy, src);
257}
258
259void
260rxvt_img::add_alpha ()
261{
262 if (format->direct.alphaMask)
263 return;
264
265 Display *dpy = s->display->dpy;
266
267 rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat);
268 img->alloc ();
269
270 Picture src = src_picture ();
271 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
272
273 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
274
275 XRenderFreePicture (dpy, src);
276 XRenderFreePicture (dpy, dst);
277
278 ::swap (img->ref, ref);
279 ::swap (img->pm , pm );
280
281 delete img;
234} 282}
235 283
236static void 284static void
237get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 285get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
238{ 286{
265 double *kernel = (double *)malloc (size * sizeof (double)); 313 double *kernel = (double *)malloc (size * sizeof (double));
266 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 314 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
267 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 315 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
268 img->alloc (); 316 img->alloc ();
269 317
270 Picture src = src_picture ();
271
272 XRenderPictureAttributes pa; 318 XRenderPictureAttributes pa;
273 pa.repeat = RepeatPad; 319 pa.repeat = RepeatPad;
274 Picture dst = XRenderCreatePicture (dpy, img->pm, format, CPRepeat, &pa); 320 Picture src = XRenderCreatePicture (dpy, pm, format, CPRepeat, &pa);
321 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
275 322
276 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); 323 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
277 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); 324 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
278 XFreePixmap (dpy, tmp_pm); 325 XFreePixmap (dpy, tmp_pm);
279 326
295 342
296 size = rv * 2 + 1; 343 size = rv * 2 + 1;
297 get_gaussian_kernel (rv, size, kernel, params); 344 get_gaussian_kernel (rv, size, kernel, params);
298 ::swap (params[0], params[1]); 345 ::swap (params[0], params[1]);
299 346
300 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 347 XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2);
301 XRenderComposite (dpy, 348 XRenderComposite (dpy,
302 PictOpSrc, 349 PictOpSrc,
303 tmp, 350 tmp,
304 None, 351 None,
305 dst, 352 dst,
309 w, h); 356 w, h);
310 } 357 }
311 358
312 free (kernel); 359 free (kernel);
313 free (params); 360 free (params);
361
314 XRenderFreePicture (dpy, src); 362 XRenderFreePicture (dpy, src);
315 XRenderFreePicture (dpy, dst); 363 XRenderFreePicture (dpy, dst);
316 XRenderFreePicture (dpy, tmp); 364 XRenderFreePicture (dpy, tmp);
317 365
318 return img; 366 return img;
319} 367}
320 368
321static Picture 369static Picture
322create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) 370create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
323{ 371{
324 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); 372 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
325 373
326 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); 374 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
327 XRenderPictureAttributes pa; 375 XRenderPictureAttributes pa;
328 pa.repeat = True; 376 pa.repeat = RepeatNormal;
377 pa.component_alpha = component_alpha;
329 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); 378 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
330 379
331 XFreePixmap (dpy, pixmap); 380 XFreePixmap (dpy, pixmap);
332 381
333 return mask; 382 return mask;
334} 383}
335 384
385static void
386extract (int32_t cl0, int32_t cl1, int32_t &c, unsigned short &xc)
387{
388 int32_t x = clamp (c, cl0, cl1);
389 c -= x;
390 xc = x;
391}
392
393static bool
394extract (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)
395{
396 extract (cl0, cl1, r, xr);
397 extract (cl0, cl1, g, xg);
398 extract (cl0, cl1, b, xb);
399 extract (cl0, cl1, a, xa);
400
401 return xr | xg | xb | xa;
402}
403
336void 404void
337rxvt_img::brightness (unsigned short r, unsigned short g, unsigned short b, unsigned short a) 405rxvt_img::brightness (int32_t r, int32_t g, int32_t b, int32_t a)
338{ 406{
407 unshare ();
408
339 Display *dpy = s->display->dpy; 409 Display *dpy = s->display->dpy;
340 Picture src = create_xrender_mask (dpy, pm, True);
341 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 410 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
342 411
412 // loop should not be needed for brightness, as only -1..1 makes sense
413 //while (r | g | b | a)
414 {
415 unsigned short xr, xg, xb, xa;
343 XRenderColor mask_c; 416 XRenderColor mask_c;
344 mask_c.red = r; 417
345 mask_c.green = g; 418 if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
346 mask_c.blue = b; 419 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
347 mask_c.alpha = a; 420
421 if (extract (-65535, 0, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
422 {
423 XRenderColor mask_w = { 65535, 65535, 65535, 65535 };
424 XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h);
425 mask_c.red = -mask_c.red; //TODO: verify that doing clamp, assign, and negation does the right thing
426 mask_c.green = -mask_c.green;
427 mask_c.blue = -mask_c.blue;
428 mask_c.alpha = -mask_c.alpha;
429 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
430 XRenderFillRectangle (dpy, PictOpDifference, dst, &mask_w, 0, 0, w, h);
431 }
432 }
433
434 XRenderFreePicture (dpy, dst);
435}
436
437void
438rxvt_img::contrast (int32_t r, int32_t g, int32_t b, int32_t a)
439{
440 if (r < 0 || g < 0 || b < 0 || a < 0)
441 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
442
443 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
444 img->alloc ();
445 img->fill (rgba (0, 0, 0, 0));
446
447 // premultiply (yeah, these are not exact, sue me or fix it)
448 r = (r * (a >> 8)) >> 8;
449 g = (g * (a >> 8)) >> 8;
450 b = (b * (a >> 8)) >> 8;
451
452 Display *dpy = s->display->dpy;
453
454 Picture src = src_picture ();
455 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
456 Picture mul = create_xrender_mask (dpy, pm, True, True);
457
458 //TODO: this operator does not yet implement some useful contrast
459 while (r | g | b | a)
460 {
461 unsigned short xr, xg, xb, xa;
462 XRenderColor mask_c;
463
464 if (extract (0, 65535, r, g, b, a, mask_c.red, mask_c.green, mask_c.blue, mask_c.alpha))
465 {
348 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 466 XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1);
349
350 XRenderComposite (dpy, PictOpAdd, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); 467 XRenderComposite (dpy, PictOpAdd, src, mul, dst, 0, 0, 0, 0, 0, 0, w, h);
468 }
469 }
351 470
471 XRenderFreePicture (dpy, mul);
472 XRenderFreePicture (dpy, dst);
352 XRenderFreePicture (dpy, src); 473 XRenderFreePicture (dpy, src);
353 XRenderFreePicture (dpy, dst);
354}
355 474
356void 475 ::swap (img->ref, ref);
357rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) 476 ::swap (img->pm , pm );
358{
359 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
360 return;
361 477
362 Display *dpy = s->display->dpy; 478 delete img;
363 Picture src = create_xrender_mask (dpy, pm, True);
364 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
365
366 XRenderColor mask_c;
367 mask_c.red = r;
368 mask_c.green = g;
369 mask_c.blue = b;
370 mask_c.alpha = a;
371 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
372
373 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
374
375 XRenderFreePicture (dpy, src);
376 XRenderFreePicture (dpy, dst);
377} 479}
378 480
379rxvt_img * 481rxvt_img *
380rxvt_img::clone () 482rxvt_img::clone ()
381{ 483{
382 return new rxvt_img (*this); 484 return new rxvt_img (*this);
383}
384
385static XRenderPictFormat *
386find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
387{
388 if (format->direct.alphaMask)
389 return format; // already has alpha
390
391 // try to find a suitable alpha format, one bit alpha is enough for our purposes
392 if (format->type == PictTypeDirect)
393 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
394 if (f->direct.alphaMask
395 && f->type == PictTypeDirect
396 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
397 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
398 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
399 return f;
400
401 // should be a very good fallback
402 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
403} 485}
404 486
405rxvt_img * 487rxvt_img *
406rxvt_img::reify () 488rxvt_img::reify ()
407{ 489{
408 if (x == 0 && y == 0 && w == ref->w && h == ref->h) 490 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
409 return clone (); 491 return clone ();
410 492
411 Display *dpy = s->display->dpy; 493 Display *dpy = s->display->dpy;
412 494
495 // add an alpha channel if...
413 bool alpha = !format->direct.alphaMask 496 bool alpha = !format->direct.alphaMask // pixmap has none yet
414 && (x || y) 497 && (x || y) // we need one because of non-zero offset
415 && repeat == RepeatNone; 498 && repeat == RepeatNone; // and we have no good pixels to fill with
416 499
417 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); 500 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat);
418 img->alloc (); 501 img->alloc ();
419 502
420 Picture src = src_picture (); 503 Picture src = src_picture ();
454 } 537 }
455 538
456 return img; 539 return img;
457} 540}
458 541
459rxvt_img * 542static void
460rxvt_img::transform (int new_width, int new_height, double matrix[9]) 543mat_invert (double mat[3][3], double (&inv)[3][3])
461{ 544{
545 double s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2];
546 double s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1];
547 double s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2];
548
549 double invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2);
550
551 inv [0][0] = invdet * s0;
552 inv [0][1] = invdet * s1;
553 inv [0][2] = invdet * s2;
554
555 inv [1][0] = invdet * (mat [2][0] * mat [1][2] - mat [2][2] * mat [1][0]);
556 inv [1][1] = invdet * (mat [2][2] * mat [0][0] - mat [2][0] * mat [0][2]);
557 inv [1][2] = invdet * (mat [1][0] * mat [0][2] - mat [1][2] * mat [0][0]);
558
559 inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]);
560 inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]);
561 inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]);
562}
563
564static double
565mat_apply (double mat[3][3], int i, double x, double y)
566{
567 double v = mat [i][0] * x + mat [i][1] * y + mat [i][2];
568 double w = mat [2][0] * x + mat [2][1] * y + mat [2][2];
569
570 return v * (1. / w);
571}
572
573rxvt_img *
574rxvt_img::transform (double matrix[3][3])
575{
576 // find new offset
577 int ox = mat_apply (matrix, 0, x, y);
578 int oy = mat_apply (matrix, 1, x, y);
579
580 // calculate new pixel bounding box coordinates
581 double d [2], rmin[2], rmax[2];
582
583 for (int i = 0; i < 2; ++i)
584 {
585 double v;
586 v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; d [i] = v;
587 v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v);
588 v = mat_apply (matrix, i, 0, h); min_it (rmin [i], v); max_it (rmax [i], v);
589 v = mat_apply (matrix, i, w, h); min_it (rmin [i], v); max_it (rmax [i], v);
590 }
591
592 int dx = floor (rmin [0]);
593 int dy = floor (rmin [1]);
594
595 int new_width = ceil (rmax [0] - dx);
596 int new_height = ceil (rmax [1] - dy);
597
598 double inv[3][3];
599 mat_invert (matrix, inv);
600
462 rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); 601 rxvt_img *img = new rxvt_img (s, format, ox - dx - d [0], oy - dy - d [1], new_width, new_height, repeat);
463 img->alloc (); 602 img->alloc ();
464 603
465 Display *dpy = s->display->dpy; 604 Display *dpy = s->display->dpy;
466 Picture src = src_picture (); 605 Picture src = src_picture ();
467 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 606 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
468 607
469 XTransform xfrm; 608 XTransform xfrm;
470 609
471 for (int i = 0; i < 3; ++i) 610 for (int i = 0; i < 3; ++i)
472 for (int j = 0; j < 3; ++j) 611 for (int j = 0; j < 3; ++j)
473 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 612 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
474
475#if 0
476 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO
477 xfrm.matrix [1][2] -= XDoubleToFixed (y);
478#endif
479 613
480 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 614 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
481 XRenderSetPictureTransform (dpy, src, &xfrm); 615 XRenderSetPictureTransform (dpy, src, &xfrm);
482 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); 616 XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height);
483 617
484 XRenderFreePicture (dpy, src); 618 XRenderFreePicture (dpy, src);
485 XRenderFreePicture (dpy, dst); 619 XRenderFreePicture (dpy, dst);
486 620
487 return img; 621 return img;
491rxvt_img::scale (int new_width, int new_height) 625rxvt_img::scale (int new_width, int new_height)
492{ 626{
493 if (w == new_width && h == new_height) 627 if (w == new_width && h == new_height)
494 return clone (); 628 return clone ();
495 629
496 double matrix[9] = { 630 double matrix[3][3] = {
497 w / (double)new_width, 0, 0, 631 { new_width / (double)w, 0, 0 },
498 0, h / (double)new_height, 0, 632 { 0, new_height / (double)h, 0 },
499 0, 0, 1 633 { 0, 0, 1 }
500 }; 634 };
501 635
502 int old_repeat_mode = repeat; 636 int old_repeat_mode = repeat;
503 repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems 637 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
504 638
505 rxvt_img *img = transform (new_width, new_height, matrix); 639 rxvt_img *img = transform (matrix);
506 640
507 repeat = old_repeat_mode; 641 repeat = old_repeat_mode;
508 img->repeat = repeat; 642 img->repeat = repeat;
509 643
510 return img; 644 return img;
511} 645}
512 646
513rxvt_img * 647rxvt_img *
514rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) 648rxvt_img::rotate (int cx, int cy, double phi)
515{ 649{
516 double s = sin (phi); 650 double s = sin (phi);
517 double c = cos (phi); 651 double c = cos (phi);
518 652
519 double matrix[9] = { 653 double matrix[3][3] = {
520 c, -s, -c * x + s * y + x, 654 { c, -s, cx - c * cx + s * cy },
521 s, c, -s * x - c * y + y, 655 { s, c, cy - s * cx - c * cy },
522 0, 0, 1 656 { 0, 0, 1 }
657 //{ c, -s, 0 },
658 //{ s, c, 0 },
659 //{ 0, 0, 1 }
523 }; 660 };
524 661
525 return transform (new_width, new_height, matrix); 662 //move (-cx, -cy);
526} 663 rxvt_img *img = transform (matrix);
664 //move ( cx, cy);
665 //img->move (cx, cy);
527 666
667 return img;
668}
669
528rxvt_img * 670rxvt_img *
529rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) 671rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
530{ 672{
531 if (new_format == format) 673 if (new_format == format)
532 return clone (); 674 return clone ();
533 675
534 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); 676 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
540 int op = PictOpSrc; 682 int op = PictOpSrc;
541 683
542 if (format->direct.alphaMask && !new_format->direct.alphaMask) 684 if (format->direct.alphaMask && !new_format->direct.alphaMask)
543 { 685 {
544 // does it have to be that complicated 686 // does it have to be that complicated
545 rgba c;
546 bg.get (c);
547
548 XRenderColor rc = { c.r, c.g, c.b, 0xffff }; 687 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
549 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); 688 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
550 689
551 op = PictOpOver; 690 op = PictOpOver;
552 } 691 }
553 692
564{ 703{
565 rxvt_img *img2 = clone (); 704 rxvt_img *img2 = clone ();
566 Display *dpy = s->display->dpy; 705 Display *dpy = s->display->dpy;
567 Picture src = img->src_picture (); 706 Picture src = img->src_picture ();
568 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 707 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
569 Picture mask = create_xrender_mask (dpy, img->pm, False); 708 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
570 709
571 XRenderColor mask_c; 710 XRenderColor mask_c;
572 711
573 mask_c.alpha = float_to_component (factor); 712 mask_c.alpha = float_to_component (factor);
574 mask_c.red = 713 mask_c.red =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines