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.49 by root, Thu Jun 7 19:24:13 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 = MSBFirst; // maybe go for host byte order, because servers are usually local? 98 xi.byte_order = ImageByteOrder (dpy);
83 xi.bitmap_unit = 32; 99 xi.bitmap_unit = 0; //XY only, unused
84 xi.bitmap_bit_order = MSBFirst; 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; 104 xi.bits_per_pixel = 32; //Z only
89 xi.red_mask = 0x00ff0000; 105 xi.red_mask = 0x00000000; //Z only, unused
90 xi.green_mask = 0x0000ff00; 106 xi.green_mask = 0x00000000; //Z only, unused
91 xi.blue_mask = 0x000000ff; 107 xi.blue_mask = 0x00000000; //Z only, unused
108 xi.obdata = 0; // probably unused
109
110 bool byte_order_mismatch = byte_order != xi.byte_order;
92 111
93 if (!XInitImage (&xi)) 112 if (!XInitImage (&xi))
94 rxvt_fatal ("unable to initialise ximage, please report.\n"); 113 rxvt_fatal ("unable to initialise ximage, please report.\n");
95 114
96 if (height > INT_MAX / xi.bytes_per_line) 115 if (height > INT_MAX / xi.bytes_per_line)
97 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");
98 117
99 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); 118 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line);
100 119
101 int rowstride = gdk_pixbuf_get_rowstride (pb); 120 int rowstride = gdk_pixbuf_get_rowstride (pb);
102 121 bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb);
103 assert (3 + (depth == 32) == gdk_pixbuf_get_n_channels (pb));
104 unsigned char *row = gdk_pixbuf_get_pixels (pb); 122 unsigned char *row = gdk_pixbuf_get_pixels (pb);
123
105 char *line = xi.data; 124 char *line = xi.data;
106 125
107 for (int y = 0; y < height; y++) 126 for (int y = 0; y < height; y++)
108 { 127 {
109 unsigned char r, g, b, a;
110 unsigned char *data = row; 128 unsigned char *src = row;
129 uint32_t *dst = (uint32_t *)line;
111 130
112 if (depth == 24) 131 if (!pb_has_alpha)
113 for (int x = 0; x < width; x++) 132 for (int x = 0; x < width; x++)
114 { 133 {
115 r = *data++; 134 uint8_t r = *src++;
116 g = *data++; 135 uint8_t g = *src++;
117 b = *data++; 136 uint8_t b = *src++;
137
138 uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b;
139
140 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch)
141 v = ecb_bswap32 (v);
142
118 *line++ = 0; 143 *dst++ = v;
119 *line++ = r;
120 *line++ = g;
121 *line++ = b;
122 } 144 }
123 else 145 else
124 for (int x = 0; x < width; x++) 146 for (int x = 0; x < width; x++)
125 { 147 {
126 uint32_t v = *(uint32_t *)data; data += 4; 148 uint32_t v = *(uint32_t *)src; src += 4;
127 v = ecb_big_endian () ? ecb_rotr32 (v, 8) : ecb_rotl32 (v, 8); 149
128 *(uint32_t *)line = x; line += 4; 150 if (ecb_big_endian ())
151 v = ecb_bswap32 (v);
152
153 v = ecb_rotl32 (v, 8); // abgr to bgra
154
155 if (!byte_order_mismatch)
156 v = ecb_bswap32 (v);
157
158 *dst++ = v;
129 } 159 }
130 160
131 row += rowstride; 161 row += rowstride;
162 line += xi.bytes_per_line;
132 } 163 }
133 164
134 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);
135 img->alloc (); 166 img->alloc ();
136 167
137 GC gc = XCreateGC (dpy, img->pm, 0, 0); 168 GC gc = XCreateGC (dpy, img->pm, 0, 0);
138 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);
139 XFreeGC (dpy, gc); 170 XFreeGC (dpy, gc);
157 g_object_unref (pb); 188 g_object_unref (pb);
158 189
159 return img; 190 return img;
160} 191}
161 192
193# endif
194
162void 195void
163rxvt_img::destroy () 196rxvt_img::destroy ()
164{ 197{
165 if (--ref->cnt) 198 if (--ref->cnt)
166 return; 199 return;
199rxvt_img::unshare () 232rxvt_img::unshare ()
200{ 233{
201 if (ref->cnt == 1 && ref->ours) 234 if (ref->cnt == 1 && ref->ours)
202 return; 235 return;
203 236
204 //TODO: maybe should reify instead
205 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);
206 GC gc = XCreateGC (s->display->dpy, pm, 0, 0); 238 GC gc = XCreateGC (s->display->dpy, pm, 0, 0);
207 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);
208 XFreeGC (s->display->dpy, gc); 240 XFreeGC (s->display->dpy, gc);
209 241
212 pm = pm2; 244 pm = pm2;
213 ref = new pixref (ref->w, ref->h); 245 ref = new pixref (ref->w, ref->h);
214} 246}
215 247
216void 248void
217rxvt_img::fill (const rxvt_color &c) 249rxvt_img::fill (const rgba &c)
218{ 250{
219 XGCValues gcv; 251 XRenderColor rc = { c.r, c.g, c.b, c.a };
220 gcv.foreground = c; 252
221 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 253 Display *dpy = s->display->dpy;
222 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 254 Picture src = src_picture ();
223 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;
224} 282}
225 283
226static void 284static void
227get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 285get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
228{ 286{
255 double *kernel = (double *)malloc (size * sizeof (double)); 313 double *kernel = (double *)malloc (size * sizeof (double));
256 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 314 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
257 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);
258 img->alloc (); 316 img->alloc ();
259 317
260 Picture src = src_picture ();
261
262 XRenderPictureAttributes pa; 318 XRenderPictureAttributes pa;
263 pa.repeat = RepeatPad; 319 pa.repeat = RepeatPad;
264 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);
265 322
266 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth); 323 Pixmap tmp_pm = XCreatePixmap (dpy, pm, w, h, format->depth);
267 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa); 324 Picture tmp = XRenderCreatePicture (dpy, tmp_pm , format, CPRepeat, &pa);
268 XFreePixmap (dpy, tmp_pm); 325 XFreePixmap (dpy, tmp_pm);
269 326
285 342
286 size = rv * 2 + 1; 343 size = rv * 2 + 1;
287 get_gaussian_kernel (rv, size, kernel, params); 344 get_gaussian_kernel (rv, size, kernel, params);
288 ::swap (params[0], params[1]); 345 ::swap (params[0], params[1]);
289 346
290 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2); 347 XRenderSetPictureFilter (dpy, tmp, FilterConvolution, params, size+2);
291 XRenderComposite (dpy, 348 XRenderComposite (dpy,
292 PictOpSrc, 349 PictOpSrc,
293 tmp, 350 tmp,
294 None, 351 None,
295 dst, 352 dst,
299 w, h); 356 w, h);
300 } 357 }
301 358
302 free (kernel); 359 free (kernel);
303 free (params); 360 free (params);
361
304 XRenderFreePicture (dpy, src); 362 XRenderFreePicture (dpy, src);
305 XRenderFreePicture (dpy, dst); 363 XRenderFreePicture (dpy, dst);
306 XRenderFreePicture (dpy, tmp); 364 XRenderFreePicture (dpy, tmp);
307 365
308 return img; 366 return img;
309} 367}
310 368
311static Picture 369static Picture
312create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) 370create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
313{ 371{
314 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); 372 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
315 373
316 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); 374 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
317 XRenderPictureAttributes pa; 375 XRenderPictureAttributes pa;
318 pa.repeat = True; 376 pa.repeat = RepeatNormal;
377 pa.component_alpha = component_alpha;
319 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); 378 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
320 379
321 XFreePixmap (dpy, pixmap); 380 XFreePixmap (dpy, pixmap);
322 381
323 return mask; 382 return mask;
324} 383}
325 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
326void 404void
327rxvt_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)
328{ 406{
407 unshare ();
408
329 Display *dpy = s->display->dpy; 409 Display *dpy = s->display->dpy;
330 Picture src = create_xrender_mask (dpy, pm, True);
331 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0); 410 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
332 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;
333 XRenderColor mask_c; 416 XRenderColor mask_c;
334 mask_c.red = r; 417
335 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))
336 mask_c.blue = b; 419 XRenderFillRectangle (dpy, PictOpAdd, dst, &mask_c, 0, 0, w, h);
337 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 {
338 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1); 466 XRenderFillRectangle (dpy, PictOpSrc, mul, &mask_c, 0, 0, 1, 1);
339
340 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 }
341 470
471 XRenderFreePicture (dpy, mul);
472 XRenderFreePicture (dpy, dst);
342 XRenderFreePicture (dpy, src); 473 XRenderFreePicture (dpy, src);
343 XRenderFreePicture (dpy, dst);
344}
345 474
346void 475 ::swap (img->ref, ref);
347rxvt_img::contrast (unsigned short r, unsigned short g, unsigned short b, unsigned short a) 476 ::swap (img->pm , pm );
348{
349 if (!(s->display->flags & DISPLAY_HAS_RENDER_MUL))
350 return;
351 477
352 Display *dpy = s->display->dpy; 478 delete img;
353 Picture src = create_xrender_mask (dpy, pm, True);
354 Picture dst = XRenderCreatePicture (dpy, pm, format, 0, 0);
355
356 XRenderColor mask_c;
357 mask_c.red = r;
358 mask_c.green = g;
359 mask_c.blue = b;
360 mask_c.alpha = a;
361 XRenderFillRectangle (dpy, PictOpSrc, src, &mask_c, 0, 0, 1, 1);
362
363 XRenderComposite (dpy, PictOpMultiply, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
364
365 XRenderFreePicture (dpy, src);
366 XRenderFreePicture (dpy, dst);
367} 479}
368 480
369rxvt_img * 481rxvt_img *
370rxvt_img::clone () 482rxvt_img::clone ()
371{ 483{
372 return new rxvt_img (*this); 484 return new rxvt_img (*this);
373}
374
375static XRenderPictFormat *
376find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
377{
378 if (format->direct.alphaMask)
379 return format; // already has alpha
380
381 // try to find a suitable alpha format, one bit alpha is enough for our purposes
382 if (format->type == PictTypeDirect)
383 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
384 if (f->direct.alphaMask
385 && f->type == PictTypeDirect
386 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
387 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
388 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
389 return f;
390
391 // should be a very good fallback
392 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
393} 485}
394 486
395rxvt_img * 487rxvt_img *
396rxvt_img::reify () 488rxvt_img::reify ()
397{ 489{
398 if (x == 0 && y == 0 && w == ref->w && h == ref->h) 490 if (x == 0 && y == 0 && w == ref->w && h == ref->h)
399 return clone (); 491 return clone ();
400 492
401 Display *dpy = s->display->dpy; 493 Display *dpy = s->display->dpy;
402 494
495 // add an alpha channel if...
403 bool alpha = !format->direct.alphaMask 496 bool alpha = !format->direct.alphaMask // pixmap has none yet
404 && (x || y) 497 && (x || y) // we need one because of non-zero offset
405 && repeat == RepeatNone; 498 && repeat == RepeatNone; // and we have no good pixels to fill with
406 499
407 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);
408 img->alloc (); 501 img->alloc ();
409 502
410 Picture src = src_picture (); 503 Picture src = src_picture ();
444 } 537 }
445 538
446 return img; 539 return img;
447} 540}
448 541
449rxvt_img * 542static void
450rxvt_img::transform (int new_width, int new_height, double matrix[9]) 543mat_invert (double mat[3][3], double (&inv)[3][3])
451{ 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
452 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);
453 img->alloc (); 602 img->alloc ();
454 603
455 Display *dpy = s->display->dpy; 604 Display *dpy = s->display->dpy;
456 Picture src = src_picture (); 605 Picture src = src_picture ();
457 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 606 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
458 607
459 XTransform xfrm; 608 XTransform xfrm;
460 609
461 for (int i = 0; i < 3; ++i) 610 for (int i = 0; i < 3; ++i)
462 for (int j = 0; j < 3; ++j) 611 for (int j = 0; j < 3; ++j)
463 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 612 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
464
465#if 0
466 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO
467 xfrm.matrix [1][2] -= XDoubleToFixed (y);
468#endif
469 613
470 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 614 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
471 XRenderSetPictureTransform (dpy, src, &xfrm); 615 XRenderSetPictureTransform (dpy, src, &xfrm);
472 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);
473 617
474 XRenderFreePicture (dpy, src); 618 XRenderFreePicture (dpy, src);
475 XRenderFreePicture (dpy, dst); 619 XRenderFreePicture (dpy, dst);
476 620
477 return img; 621 return img;
481rxvt_img::scale (int new_width, int new_height) 625rxvt_img::scale (int new_width, int new_height)
482{ 626{
483 if (w == new_width && h == new_height) 627 if (w == new_width && h == new_height)
484 return clone (); 628 return clone ();
485 629
486 double matrix[9] = { 630 double matrix[3][3] = {
487 w / (double)new_width, 0, 0, 631 { new_width / (double)w, 0, 0 },
488 0, h / (double)new_height, 0, 632 { 0, new_height / (double)h, 0 },
489 0, 0, 1 633 { 0, 0, 1 }
490 }; 634 };
491 635
492 int old_repeat_mode = repeat; 636 int old_repeat_mode = repeat;
493 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
494 638
495 rxvt_img *img = transform (new_width, new_height, matrix); 639 rxvt_img *img = transform (matrix);
496 640
497 repeat = old_repeat_mode; 641 repeat = old_repeat_mode;
498 img->repeat = repeat; 642 img->repeat = repeat;
499 643
500 return img; 644 return img;
501} 645}
502 646
503rxvt_img * 647rxvt_img *
504rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) 648rxvt_img::rotate (int cx, int cy, double phi)
505{ 649{
506 double s = sin (phi); 650 double s = sin (phi);
507 double c = cos (phi); 651 double c = cos (phi);
508 652
509 double matrix[9] = { 653 double matrix[3][3] = {
510 c, -s, -c * x + s * y + x, 654 { c, -s, cx - c * cx + s * cy },
511 s, c, -s * x - c * y + y, 655 { s, c, cy - s * cx - c * cy },
512 0, 0, 1 656 { 0, 0, 1 }
657 //{ c, -s, 0 },
658 //{ s, c, 0 },
659 //{ 0, 0, 1 }
513 }; 660 };
514 661
515 return transform (new_width, new_height, matrix); 662 //move (-cx, -cy);
516} 663 rxvt_img *img = transform (matrix);
664 //move ( cx, cy);
665 //img->move (cx, cy);
517 666
667 return img;
668}
669
518rxvt_img * 670rxvt_img *
519rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) 671rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
520{ 672{
521 if (new_format == format) 673 if (new_format == format)
522 return clone (); 674 return clone ();
523 675
524 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);
530 int op = PictOpSrc; 682 int op = PictOpSrc;
531 683
532 if (format->direct.alphaMask && !new_format->direct.alphaMask) 684 if (format->direct.alphaMask && !new_format->direct.alphaMask)
533 { 685 {
534 // does it have to be that complicated 686 // does it have to be that complicated
535 rgba c;
536 bg.get (c);
537
538 XRenderColor rc = { c.r, c.g, c.b, 0xffff }; 687 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
539 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); 688 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
540 689
541 op = PictOpOver; 690 op = PictOpOver;
542 } 691 }
543 692
554{ 703{
555 rxvt_img *img2 = clone (); 704 rxvt_img *img2 = clone ();
556 Display *dpy = s->display->dpy; 705 Display *dpy = s->display->dpy;
557 Picture src = img->src_picture (); 706 Picture src = img->src_picture ();
558 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 707 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
559 Picture mask = create_xrender_mask (dpy, img->pm, False); 708 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
560 709
561 XRenderColor mask_c; 710 XRenderColor mask_c;
562 711
563 mask_c.alpha = float_to_component (factor); 712 mask_c.alpha = float_to_component (factor);
564 mask_c.red = 713 mask_c.red =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines