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.51 by root, Thu Jun 7 20:04:54 2012 UTC vs.
Revision 1.84 by root, Thu Jun 14 18:19:11 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 = 0x000000ff; //Z only 105 xi.red_mask = 0x00000000; //Z only, unused
90 xi.green_mask = 0x0000ff00; //Z only 106 xi.green_mask = 0x00000000; //Z only, unused
91 xi.blue_mask = 0x00ff0000; //Z only 107 xi.blue_mask = 0x00000000; //Z only, unused
92 xi.obdata = 0; // probbaly unused 108 xi.obdata = 0; // probably unused
109
110 bool byte_order_mismatch = byte_order != xi.byte_order;
93 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
130 uint32_t v = *(uint32_t *)src; src += 4; 148 uint32_t v = *(uint32_t *)src; src += 4;
131 149
132 if (ecb_big_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)
156 v = ecb_bswap32 (v);
157
135 *dst++ = v; 158 *dst++ = v;
136 } 159 }
137 160
138 row += rowstride; 161 row += rowstride;
139 line += xi.bytes_per_line; 162 line += xi.bytes_per_line;
140 } 163 }
141 164
142 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);
143 img->alloc (); 166 img->alloc ();
144 167
145 GC gc = XCreateGC (dpy, img->pm, 0, 0); 168 GC gc = XCreateGC (dpy, img->pm, 0, 0);
146 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);
147 XFreeGC (dpy, gc); 170 XFreeGC (dpy, gc);
165 g_object_unref (pb); 188 g_object_unref (pb);
166 189
167 return img; 190 return img;
168} 191}
169 192
193# endif
194
170void 195void
171rxvt_img::destroy () 196rxvt_img::destroy ()
172{ 197{
173 if (--ref->cnt) 198 if (--ref->cnt)
174 return; 199 return;
207rxvt_img::unshare () 232rxvt_img::unshare ()
208{ 233{
209 if (ref->cnt == 1 && ref->ours) 234 if (ref->cnt == 1 && ref->ours)
210 return; 235 return;
211 236
212 //TODO: maybe should reify instead
213 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);
214 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 238 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
215 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);
216 XFreeGC (s->display->dpy, gc); 240 XFreeGC (s->display->dpy, gc);
217 241
220 pm = pm2; 244 pm = pm2;
221 ref = new pixref (ref->w, ref->h); 245 ref = new pixref (ref->w, ref->h);
222} 246}
223 247
224void 248void
225rxvt_img::fill (const rxvt_color &c) 249rxvt_img::fill (const rgba &c)
226{ 250{
227 XGCValues gcv; 251 XRenderColor rc = { c.r, c.g, c.b, c.a };
228 gcv.foreground = c; 252
229 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 253 Display *dpy = s->display->dpy;
230 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 254 Picture src = src_picture ();
231 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;
232} 282}
233 283
234static void 284static void
235get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 285get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params)
236{ 286{
237 double sigma = radius / 2.0; 287 rxvt_img::nv sigma = radius / 2.0;
238 double scale = sqrt (2.0 * M_PI) * sigma; 288 rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma;
239 double sum = 0.0; 289 rxvt_img::nv sum = 0.0;
240 290
241 for (int i = 0; i < width; i++) 291 for (int i = 0; i < width; i++)
242 { 292 {
243 double x = i - width / 2; 293 rxvt_img::nv x = i - width / 2;
244 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; 294 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
245 sum += kernel[i]; 295 sum += kernel[i];
246 } 296 }
247 297
248 params[0] = XDoubleToFixed (width); 298 params[0] = XDoubleToFixed (width);
258 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 308 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
259 return clone (); 309 return clone ();
260 310
261 Display *dpy = s->display->dpy; 311 Display *dpy = s->display->dpy;
262 int size = max (rh, rv) * 2 + 1; 312 int size = max (rh, rv) * 2 + 1;
263 double *kernel = (double *)malloc (size * sizeof (double)); 313 nv *kernel = (nv *)malloc (size * sizeof (nv));
264 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 314 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
265 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);
266 img->alloc (); 316 img->alloc ();
267 317
268 Picture src = src_picture ();
269
270 XRenderPictureAttributes pa; 318 XRenderPictureAttributes pa;
271 pa.repeat = RepeatPad; 319 pa.repeat = RepeatPad;
272 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);
273 322
274 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); 323 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
275 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); 324 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
276 XFreePixmap (dpy, tmp_pm); 325 XFreePixmap (dpy, tmp_pm);
277 326
293 342
294 size = rv * 2 + 1; 343 size = rv * 2 + 1;
295 get_gaussian_kernel (rv, size, kernel, params); 344 get_gaussian_kernel (rv, size, kernel, params);
296 ::swap (params[0], params[1]); 345 ::swap (params[0], params[1]);
297 346
298 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 347 XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2);
299 XRenderComposite (dpy, 348 XRenderComposite (dpy,
300 PictOpSrc, 349 PictOpSrc,
301 tmp, 350 tmp,
302 None, 351 None,
303 dst, 352 dst,
307 w, h); 356 w, h);
308 } 357 }
309 358
310 free (kernel); 359 free (kernel);
311 free (params); 360 free (params);
361
312 XRenderFreePicture (dpy, src); 362 XRenderFreePicture (dpy, src);
313 XRenderFreePicture (dpy, dst); 363 XRenderFreePicture (dpy, dst);
314 XRenderFreePicture (dpy, tmp); 364 XRenderFreePicture (dpy, tmp);
315 365
316 return img; 366 return img;
317} 367}
318 368
319static Picture 369static Picture
320create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) 370create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
321{ 371{
322 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); 372 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
323 373
324 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); 374 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
325 XRenderPictureAttributes pa; 375 XRenderPictureAttributes pa;
326 pa.repeat = True; 376 pa.repeat = RepeatNormal;
377 pa.component_alpha = component_alpha;
327 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); 378 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
328 379
329 XFreePixmap (dpy, pixmap); 380 XFreePixmap (dpy, pixmap);
330 381
331 return mask; 382 return mask;
332} 383}
333 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
334void 404void
335rxvt_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)
336{ 406{
407 unshare ();
408
337 Display *dpy = s->display->dpy; 409 Display *dpy = s->display->dpy;
338 Picture src = create_xrender_mask (dpy, pm, True);
339 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 410 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
340 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;
341 XRenderColor mask_c; 416 XRenderColor mask_c;
342 mask_c.red = r; 417
343 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))
344 mask_c.blue = b; 419 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
345 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 {
346 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 466 XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1);
347
348 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 }
349 470
471 XRenderFreePicture (dpy, mul);
472 XRenderFreePicture (dpy, dst);
350 XRenderFreePicture (dpy, src); 473 XRenderFreePicture (dpy, src);
351 XRenderFreePicture (dpy, dst);
352}
353 474
354void 475 ::swap (img->ref, ref);
355rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) 476 ::swap (img->pm , pm );
356{
357 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
358 return;
359 477
360 Display *dpy = s->display->dpy; 478 delete img;
361 Picture src = create_xrender_mask (dpy, pm, True);
362 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
363
364 XRenderColor mask_c;
365 mask_c.red = r;
366 mask_c.green = g;
367 mask_c.blue = b;
368 mask_c.alpha = a;
369 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
370
371 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
372
373 XRenderFreePicture (dpy, src);
374 XRenderFreePicture (dpy, dst);
375} 479}
376 480
377rxvt_img * 481rxvt_img *
378rxvt_img::clone () 482rxvt_img::clone ()
379{ 483{
380 return new rxvt_img (*this); 484 return new rxvt_img (*this);
381}
382
383static XRenderPictFormat *
384find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
385{
386 if (format->direct.alphaMask)
387 return format; // already has alpha
388
389 // try to find a suitable alpha format, one bit alpha is enough for our purposes
390 if (format->type == PictTypeDirect)
391 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
392 if (f->direct.alphaMask
393 && f->type == PictTypeDirect
394 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
395 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
396 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
397 return f;
398
399 // should be a very good fallback
400 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
401} 485}
402 486
403rxvt_img * 487rxvt_img *
404rxvt_img::reify () 488rxvt_img::reify ()
405{ 489{
406 if (x == 0 && y == 0 && w == ref->w && h == ref->h) 490 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
407 return clone (); 491 return clone ();
408 492
409 Display *dpy = s->display->dpy; 493 Display *dpy = s->display->dpy;
410 494
495 // add an alpha channel if...
411 bool alpha = !format->direct.alphaMask 496 bool alpha = !format->direct.alphaMask // pixmap has none yet
412 && (x || y) 497 && (x || y) // we need one because of non-zero offset
413 && repeat == RepeatNone; 498 && repeat == RepeatNone; // and we have no good pixels to fill with
414 499
415 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);
416 img->alloc (); 501 img->alloc ();
417 502
418 Picture src = src_picture (); 503 Picture src = src_picture ();
452 } 537 }
453 538
454 return img; 539 return img;
455} 540}
456 541
457rxvt_img * 542static void
458rxvt_img::transform (int new_width, int new_height, double matrix[9]) 543mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3])
459{ 544{
545 rxvt_img::nv s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2];
546 rxvt_img::nv s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1];
547 rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2];
548
549 rxvt_img::nv 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 rxvt_img::nv
565mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y)
566{
567 rxvt_img::nv v = mat [i][0] * x + mat [i][1] * y + mat [i][2];
568 rxvt_img::nv 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 (nv 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 nv d [2], rmin[2], rmax[2];
582
583 for (int i = 0; i < 2; ++i)
584 {
585 nv 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 nv inv[3][3];
599 mat_invert (matrix, inv);
600
460 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);
461 img->alloc (); 602 img->alloc ();
462 603
463 Display *dpy = s->display->dpy; 604 Display *dpy = s->display->dpy;
464 Picture src = src_picture (); 605 Picture src = src_picture ();
465 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 606 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
466 607
467 XTransform xfrm; 608 XTransform xfrm;
468 609
469 for (int i = 0; i < 3; ++i) 610 for (int i = 0; i < 3; ++i)
470 for (int j = 0; j < 3; ++j) 611 for (int j = 0; j < 3; ++j)
471 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 612 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
472
473#if 0
474 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO
475 xfrm.matrix [1][2] -= XDoubleToFixed (y);
476#endif
477 613
478 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 614 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
479 XRenderSetPictureTransform (dpy, src, &xfrm); 615 XRenderSetPictureTransform (dpy, src, &xfrm);
480 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);
481 617
482 XRenderFreePicture (dpy, src); 618 XRenderFreePicture (dpy, src);
483 XRenderFreePicture (dpy, dst); 619 XRenderFreePicture (dpy, dst);
484 620
485 return img; 621 return img;
489rxvt_img::scale (int new_width, int new_height) 625rxvt_img::scale (int new_width, int new_height)
490{ 626{
491 if (w == new_width && h == new_height) 627 if (w == new_width && h == new_height)
492 return clone (); 628 return clone ();
493 629
494 double matrix[9] = { 630 nv matrix[3][3] = {
495 w / (double)new_width, 0, 0, 631 { new_width / (nv)w, 0, 0 },
496 0, h / (double)new_height, 0, 632 { 0, new_height / (nv)h, 0 },
497 0, 0, 1 633 { 0, 0, 1 }
498 }; 634 };
499 635
500 int old_repeat_mode = repeat; 636 int old_repeat_mode = repeat;
501 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
502 638
503 rxvt_img *img = transform (new_width, new_height, matrix); 639 rxvt_img *img = transform (matrix);
504 640
505 repeat = old_repeat_mode; 641 repeat = old_repeat_mode;
506 img->repeat = repeat; 642 img->repeat = repeat;
507 643
508 return img; 644 return img;
509} 645}
510 646
511rxvt_img * 647rxvt_img *
512rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) 648rxvt_img::rotate (int cx, int cy, nv phi)
513{ 649{
514 double s = sin (phi); 650 nv s = sin (phi);
515 double c = cos (phi); 651 nv c = cos (phi);
516 652
517 double matrix[9] = { 653 nv matrix[3][3] = {
518 c, -s, -c * x + s * y + x, 654 { c, -s, cx - c * cx + s * cy },
519 s, c, -s * x - c * y + y, 655 { s, c, cy - s * cx - c * cy },
520 0, 0, 1 656 { 0, 0, 1 }
657 //{ c, -s, 0 },
658 //{ s, c, 0 },
659 //{ 0, 0, 1 }
521 }; 660 };
522 661
523 return transform (new_width, new_height, matrix); 662 //move (-cx, -cy);
524} 663 rxvt_img *img = transform (matrix);
664 //move ( cx, cy);
665 //img->move (cx, cy);
525 666
667 return img;
668}
669
526rxvt_img * 670rxvt_img *
527rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) 671rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
528{ 672{
529 if (new_format == format) 673 if (new_format == format)
530 return clone (); 674 return clone ();
531 675
532 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);
538 int op = PictOpSrc; 682 int op = PictOpSrc;
539 683
540 if (format->direct.alphaMask && !new_format->direct.alphaMask) 684 if (format->direct.alphaMask && !new_format->direct.alphaMask)
541 { 685 {
542 // does it have to be that complicated 686 // does it have to be that complicated
543 rgba c;
544 bg.get (c);
545
546 XRenderColor rc = { c.r, c.g, c.b, 0xffff }; 687 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
547 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); 688 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
548 689
549 op = PictOpOver; 690 op = PictOpOver;
550 } 691 }
551 692
556 697
557 return img; 698 return img;
558} 699}
559 700
560rxvt_img * 701rxvt_img *
561rxvt_img::blend (rxvt_img *img, double factor) 702rxvt_img::blend (rxvt_img *img, nv factor)
562{ 703{
563 rxvt_img *img2 = clone (); 704 rxvt_img *img2 = clone ();
564 Display *dpy = s->display->dpy; 705 Display *dpy = s->display->dpy;
565 Picture src = img->src_picture (); 706 Picture src = img->src_picture ();
566 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 707 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
567 Picture mask = create_xrender_mask (dpy, img->pm, False); 708 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
568 709
569 XRenderColor mask_c; 710 XRenderColor mask_c;
570 711
571 mask_c.alpha = float_to_component (factor); 712 mask_c.alpha = float_to_component (factor);
572 mask_c.red = 713 mask_c.red =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines