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.54 by root, Thu Jun 7 20:24:39 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;
75 89
76 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; 90 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
77 91
78 XImage xi; 92 XImage xi;
79 93
80 xi.width = width; 94 xi.width = width;
81 xi.height = height; 95 xi.height = height;
82 xi.xoffset = 0; 96 xi.xoffset = 0;
83 xi.format = ZPixmap; 97 xi.format = ZPixmap;
84 xi.byte_order = ImageByteOrder (dpy); 98 xi.byte_order = ImageByteOrder (dpy);
85 xi.bitmap_unit = 32; //XY only, unused 99 xi.bitmap_unit = 0; //XY only, unused
86 xi.bitmap_bit_order = LSBFirst; //XY only, unused 100 xi.bitmap_bit_order = 0; //XY only, unused
87 xi.bitmap_pad = BitmapPad (dpy); 101 xi.bitmap_pad = BitmapPad (dpy);
88 xi.depth = depth; 102 xi.depth = 32;
89 xi.bytes_per_line = 0; 103 xi.bytes_per_line = 0;
90 xi.bits_per_pixel = 32; //Z only 104 xi.bits_per_pixel = 32; //Z only
91 xi.red_mask = 0x00000000; //Z only, unused 105 xi.red_mask = 0x00000000; //Z only, unused
92 xi.green_mask = 0x00000000; //Z only, unused 106 xi.green_mask = 0x00000000; //Z only, unused
93 xi.blue_mask = 0x00000000; //Z only, unused 107 xi.blue_mask = 0x00000000; //Z only, unused
102 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");
103 117
104 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); 118 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line);
105 119
106 int rowstride = gdk_pixbuf_get_rowstride (pb); 120 int rowstride = gdk_pixbuf_get_rowstride (pb);
107 121 bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb);
108 assert (3 + (depth == 32) == gdk_pixbuf_get_n_channels (pb));
109 unsigned char *row = gdk_pixbuf_get_pixels (pb); 122 unsigned char *row = gdk_pixbuf_get_pixels (pb);
123
110 char *line = xi.data; 124 char *line = xi.data;
111 125
112 for (int y = 0; y < height; y++) 126 for (int y = 0; y < height; y++)
113 { 127 {
114 unsigned char *src = row; 128 unsigned char *src = row;
115 uint32_t *dst = (uint32_t *)line; 129 uint32_t *dst = (uint32_t *)line;
116 130
117 if (depth == 24) 131 if (!pb_has_alpha)
118 for (int x = 0; x < width; x++) 132 for (int x = 0; x < width; x++)
119 { 133 {
120 uint8_t r = *src++; 134 uint8_t r = *src++;
121 uint8_t g = *src++; 135 uint8_t g = *src++;
122 uint8_t b = *src++; 136 uint8_t b = *src++;
123 137
124 uint32_t v = (r << 16) | (g << 8) | b; 138 uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b;
125 139
126 if (ecb_big_endian ()) 140 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch)
127 v = ecb_bswap32 (v);
128
129 if (byte_order_mismatch)
130 v = ecb_bswap32 (v); 141 v = ecb_bswap32 (v);
131 142
132 *dst++ = v; 143 *dst++ = v;
133 } 144 }
134 else 145 else
135 for (int x = 0; x < width; x++) 146 for (int x = 0; x < width; x++)
136 { 147 {
137 uint32_t v = *(uint32_t *)src; src += 4; 148 uint32_t v = *(uint32_t *)src; src += 4;
138 149
139 if (ecb_little_endian ()) 150 if (ecb_big_endian ())
140 v = ecb_bswap32 (v); 151 v = ecb_bswap32 (v);
141 152
142 v = ecb_rotr32 (v, 8); 153 v = ecb_rotl32 (v, 8); // abgr to bgra
143 154
144 if (byte_order_mismatch) 155 if (!byte_order_mismatch)
145 v = ecb_bswap32 (v); 156 v = ecb_bswap32 (v);
146 157
147 *dst++ = v; 158 *dst++ = v;
148 } 159 }
149 160
150 row += rowstride; 161 row += rowstride;
151 line += xi.bytes_per_line; 162 line += xi.bytes_per_line;
152 } 163 }
153 164
154 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);
155 img->alloc (); 166 img->alloc ();
156 167
157 GC gc = XCreateGC (dpy, img->pm, 0, 0); 168 GC gc = XCreateGC (dpy, img->pm, 0, 0);
158 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);
159 XFreeGC (dpy, gc); 170 XFreeGC (dpy, gc);
177 g_object_unref (pb); 188 g_object_unref (pb);
178 189
179 return img; 190 return img;
180} 191}
181 192
193# endif
194
182void 195void
183rxvt_img::destroy () 196rxvt_img::destroy ()
184{ 197{
185 if (--ref->cnt) 198 if (--ref->cnt)
186 return; 199 return;
219rxvt_img::unshare () 232rxvt_img::unshare ()
220{ 233{
221 if (ref->cnt == 1 && ref->ours) 234 if (ref->cnt == 1 && ref->ours)
222 return; 235 return;
223 236
224 //TODO: maybe should reify instead
225 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);
226 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 238 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
227 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);
228 XFreeGC (s->display->dpy, gc); 240 XFreeGC (s->display->dpy, gc);
229 241
232 pm = pm2; 244 pm = pm2;
233 ref = new pixref (ref->w, ref->h); 245 ref = new pixref (ref->w, ref->h);
234} 246}
235 247
236void 248void
237rxvt_img::fill (const rxvt_color &c) 249rxvt_img::fill (const rgba &c)
238{ 250{
239 XGCValues gcv; 251 XRenderColor rc = { c.r, c.g, c.b, c.a };
240 gcv.foreground = c; 252
241 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 253 Display *dpy = s->display->dpy;
242 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 254 Picture src = src_picture ();
243 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;
244} 282}
245 283
246static void 284static void
247get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 285get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params)
248{ 286{
249 double sigma = radius / 2.0; 287 rxvt_img::nv sigma = radius / 2.0;
250 double scale = sqrt (2.0 * M_PI) * sigma; 288 rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma;
251 double sum = 0.0; 289 rxvt_img::nv sum = 0.0;
252 290
253 for (int i = 0; i < width; i++) 291 for (int i = 0; i < width; i++)
254 { 292 {
255 double x = i - width / 2; 293 rxvt_img::nv x = i - width / 2;
256 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; 294 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
257 sum += kernel[i]; 295 sum += kernel[i];
258 } 296 }
259 297
260 params[0] = XDoubleToFixed (width); 298 params[0] = XDoubleToFixed (width);
270 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 308 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
271 return clone (); 309 return clone ();
272 310
273 Display *dpy = s->display->dpy; 311 Display *dpy = s->display->dpy;
274 int size = max (rh, rv) * 2 + 1; 312 int size = max (rh, rv) * 2 + 1;
275 double *kernel = (double *)malloc (size * sizeof (double)); 313 nv *kernel = (nv *)malloc (size * sizeof (nv));
276 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 314 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
277 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);
278 img->alloc (); 316 img->alloc ();
279 317
280 Picture src = src_picture ();
281
282 XRenderPictureAttributes pa; 318 XRenderPictureAttributes pa;
283 pa.repeat = RepeatPad; 319 pa.repeat = RepeatPad;
284 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);
285 322
286 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); 323 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
287 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); 324 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
288 XFreePixmap (dpy, tmp_pm); 325 XFreePixmap (dpy, tmp_pm);
289 326
305 342
306 size = rv * 2 + 1; 343 size = rv * 2 + 1;
307 get_gaussian_kernel (rv, size, kernel, params); 344 get_gaussian_kernel (rv, size, kernel, params);
308 ::swap (params[0], params[1]); 345 ::swap (params[0], params[1]);
309 346
310 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 347 XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2);
311 XRenderComposite (dpy, 348 XRenderComposite (dpy,
312 PictOpSrc, 349 PictOpSrc,
313 tmp, 350 tmp,
314 None, 351 None,
315 dst, 352 dst,
319 w, h); 356 w, h);
320 } 357 }
321 358
322 free (kernel); 359 free (kernel);
323 free (params); 360 free (params);
361
324 XRenderFreePicture (dpy, src); 362 XRenderFreePicture (dpy, src);
325 XRenderFreePicture (dpy, dst); 363 XRenderFreePicture (dpy, dst);
326 XRenderFreePicture (dpy, tmp); 364 XRenderFreePicture (dpy, tmp);
327 365
328 return img; 366 return img;
329} 367}
330 368
331static Picture 369static Picture
332create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) 370create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
333{ 371{
334 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); 372 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
335 373
336 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); 374 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
337 XRenderPictureAttributes pa; 375 XRenderPictureAttributes pa;
338 pa.repeat = True; 376 pa.repeat = RepeatNormal;
377 pa.component_alpha = component_alpha;
339 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); 378 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
340 379
341 XFreePixmap (dpy, pixmap); 380 XFreePixmap (dpy, pixmap);
342 381
343 return mask; 382 return mask;
344} 383}
345 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
346void 404void
347rxvt_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)
348{ 406{
407 unshare ();
408
349 Display *dpy = s->display->dpy; 409 Display *dpy = s->display->dpy;
350 Picture src = create_xrender_mask (dpy, pm, True);
351 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 410 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
352 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;
353 XRenderColor mask_c; 416 XRenderColor mask_c;
354 mask_c.red = r; 417
355 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))
356 mask_c.blue = b; 419 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
357 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 {
358 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 466 XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1);
359
360 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 }
361 470
471 XRenderFreePicture (dpy, mul);
472 XRenderFreePicture (dpy, dst);
362 XRenderFreePicture (dpy, src); 473 XRenderFreePicture (dpy, src);
363 XRenderFreePicture (dpy, dst);
364}
365 474
366void 475 ::swap (img->ref, ref);
367rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) 476 ::swap (img->pm , pm );
368{
369 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
370 return;
371 477
372 Display *dpy = s->display->dpy; 478 delete img;
373 Picture src = create_xrender_mask (dpy, pm, True);
374 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
375
376 XRenderColor mask_c;
377 mask_c.red = r;
378 mask_c.green = g;
379 mask_c.blue = b;
380 mask_c.alpha = a;
381 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
382
383 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
384
385 XRenderFreePicture (dpy, src);
386 XRenderFreePicture (dpy, dst);
387} 479}
388 480
389rxvt_img * 481rxvt_img *
390rxvt_img::clone () 482rxvt_img::clone ()
391{ 483{
392 return new rxvt_img (*this); 484 return new rxvt_img (*this);
393}
394
395static XRenderPictFormat *
396find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
397{
398 if (format->direct.alphaMask)
399 return format; // already has alpha
400
401 // try to find a suitable alpha format, one bit alpha is enough for our purposes
402 if (format->type == PictTypeDirect)
403 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
404 if (f->direct.alphaMask
405 && f->type == PictTypeDirect
406 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
407 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
408 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
409 return f;
410
411 // should be a very good fallback
412 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
413} 485}
414 486
415rxvt_img * 487rxvt_img *
416rxvt_img::reify () 488rxvt_img::reify ()
417{ 489{
418 if (x == 0 && y == 0 && w == ref->w && h == ref->h) 490 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
419 return clone (); 491 return clone ();
420 492
421 Display *dpy = s->display->dpy; 493 Display *dpy = s->display->dpy;
422 494
495 // add an alpha channel if...
423 bool alpha = !format->direct.alphaMask 496 bool alpha = !format->direct.alphaMask // pixmap has none yet
424 && (x || y) 497 && (x || y) // we need one because of non-zero offset
425 && repeat == RepeatNone; 498 && repeat == RepeatNone; // and we have no good pixels to fill with
426 499
427 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);
428 img->alloc (); 501 img->alloc ();
429 502
430 Picture src = src_picture (); 503 Picture src = src_picture ();
464 } 537 }
465 538
466 return img; 539 return img;
467} 540}
468 541
469rxvt_img * 542static void
470rxvt_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])
471{ 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
472 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);
473 img->alloc (); 602 img->alloc ();
474 603
475 Display *dpy = s->display->dpy; 604 Display *dpy = s->display->dpy;
476 Picture src = src_picture (); 605 Picture src = src_picture ();
477 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 606 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
478 607
479 XTransform xfrm; 608 XTransform xfrm;
480 609
481 for (int i = 0; i < 3; ++i) 610 for (int i = 0; i < 3; ++i)
482 for (int j = 0; j < 3; ++j) 611 for (int j = 0; j < 3; ++j)
483 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 612 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
484
485#if 0
486 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO
487 xfrm.matrix [1][2] -= XDoubleToFixed (y);
488#endif
489 613
490 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 614 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
491 XRenderSetPictureTransform (dpy, src, &xfrm); 615 XRenderSetPictureTransform (dpy, src, &xfrm);
492 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);
493 617
494 XRenderFreePicture (dpy, src); 618 XRenderFreePicture (dpy, src);
495 XRenderFreePicture (dpy, dst); 619 XRenderFreePicture (dpy, dst);
496 620
497 return img; 621 return img;
501rxvt_img::scale (int new_width, int new_height) 625rxvt_img::scale (int new_width, int new_height)
502{ 626{
503 if (w == new_width && h == new_height) 627 if (w == new_width && h == new_height)
504 return clone (); 628 return clone ();
505 629
506 double matrix[9] = { 630 nv matrix[3][3] = {
507 w / (double)new_width, 0, 0, 631 { new_width / (nv)w, 0, 0 },
508 0, h / (double)new_height, 0, 632 { 0, new_height / (nv)h, 0 },
509 0, 0, 1 633 { 0, 0, 1 }
510 }; 634 };
511 635
512 int old_repeat_mode = repeat; 636 int old_repeat_mode = repeat;
513 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
514 638
515 rxvt_img *img = transform (new_width, new_height, matrix); 639 rxvt_img *img = transform (matrix);
516 640
517 repeat = old_repeat_mode; 641 repeat = old_repeat_mode;
518 img->repeat = repeat; 642 img->repeat = repeat;
519 643
520 return img; 644 return img;
521} 645}
522 646
523rxvt_img * 647rxvt_img *
524rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) 648rxvt_img::rotate (int cx, int cy, nv phi)
525{ 649{
526 double s = sin (phi); 650 nv s = sin (phi);
527 double c = cos (phi); 651 nv c = cos (phi);
528 652
529 double matrix[9] = { 653 nv matrix[3][3] = {
530 c, -s, -c * x + s * y + x, 654 { c, -s, cx - c * cx + s * cy },
531 s, c, -s * x - c * y + y, 655 { s, c, cy - s * cx - c * cy },
532 0, 0, 1 656 { 0, 0, 1 }
657 //{ c, -s, 0 },
658 //{ s, c, 0 },
659 //{ 0, 0, 1 }
533 }; 660 };
534 661
535 return transform (new_width, new_height, matrix); 662 //move (-cx, -cy);
536} 663 rxvt_img *img = transform (matrix);
664 //move ( cx, cy);
665 //img->move (cx, cy);
537 666
667 return img;
668}
669
538rxvt_img * 670rxvt_img *
539rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) 671rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
540{ 672{
541 if (new_format == format) 673 if (new_format == format)
542 return clone (); 674 return clone ();
543 675
544 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);
550 int op = PictOpSrc; 682 int op = PictOpSrc;
551 683
552 if (format->direct.alphaMask && !new_format->direct.alphaMask) 684 if (format->direct.alphaMask && !new_format->direct.alphaMask)
553 { 685 {
554 // does it have to be that complicated 686 // does it have to be that complicated
555 rgba c;
556 bg.get (c);
557
558 XRenderColor rc = { c.r, c.g, c.b, 0xffff }; 687 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
559 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); 688 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
560 689
561 op = PictOpOver; 690 op = PictOpOver;
562 } 691 }
563 692
568 697
569 return img; 698 return img;
570} 699}
571 700
572rxvt_img * 701rxvt_img *
573rxvt_img::blend (rxvt_img *img, double factor) 702rxvt_img::blend (rxvt_img *img, nv factor)
574{ 703{
575 rxvt_img *img2 = clone (); 704 rxvt_img *img2 = clone ();
576 Display *dpy = s->display->dpy; 705 Display *dpy = s->display->dpy;
577 Picture src = img->src_picture (); 706 Picture src = img->src_picture ();
578 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 707 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
579 Picture mask = create_xrender_mask (dpy, img->pm, False); 708 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
580 709
581 XRenderColor mask_c; 710 XRenderColor mask_c;
582 711
583 mask_c.alpha = float_to_component (factor); 712 mask_c.alpha = float_to_component (factor);
584 mask_c.red = 713 mask_c.red =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines