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.74 by root, Sat Jun 9 19:20:04 2012 UTC vs.
Revision 1.90 by root, Thu Jun 14 19:44:45 2012 UTC

1#include <math.h> 1#include <math.h>
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
7#if 0
8struct pict
9{
10 Display *dpy;
11 Picture pic;
12
13 operator Picture () const
14 {
15 return pic;
16 }
17
18 pict ()
19 : pic (0)
20 {
21 }
22
23 pict (rxvt_img *img, XRenderPictFormat *format = 0)
24 : dpy (img->s->display->dpy)
25 {
26 XRenderPictureAttributes pa;
27 pa.repeat = img->repeat;
28 pic = XRenderCreatePicture (dpy, img->pm, format ? format : img->format, CPRepeat, &pa);
29 }
30
31 ~pict ()
32 {
33 if (pic)
34 XRenderFreePicture (dpy, pic);
35 }
36};
37#endif
38
39static XRenderPictFormat *
40find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
41{
42 if (format->direct.alphaMask)
43 return format; // already has alpha
44
45 // try to find a suitable alpha format, one bit alpha is enough for our purposes
46 if (format->type == PictTypeDirect)
47 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
48 if (f->direct.alphaMask
49 && f->type == PictTypeDirect
50 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
51 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
52 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
53 return f;
54
55 // should be a very good fallback
56 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
57}
6 58
7rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) 59rxvt_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), 60: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat),
9 pm(0), ref(0) 61 pm(0), ref(0)
10{ 62{
50 102
51 return img; 103 return img;
52} 104}
53 105
54# if HAVE_PIXBUF 106# if HAVE_PIXBUF
107
55rxvt_img * 108rxvt_img *
56rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) 109rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb)
57{ 110{
58 Display *dpy = s->display->dpy; 111 Display *dpy = s->display->dpy;
59 112
63 if (width > 32767 || height > 32767) // well, we *could* upload in chunks 116 if (width > 32767 || height > 32767) // well, we *could* upload in chunks
64 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n"); 117 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n");
65 118
66 // since we require rgb24/argb32 formats from xrender we assume 119 // since we require rgb24/argb32 formats from xrender we assume
67 // that both 24 and 32 bpp MUST be supported by any screen that supports xrender 120 // that both 24 and 32 bpp MUST be supported by any screen that supports xrender
68 int depth = gdk_pixbuf_get_has_alpha (pb) ? 32 : 24;
69 121
70 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; 122 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
71 123
72 XImage xi; 124 XImage xi;
73 125
77 xi.format = ZPixmap; 129 xi.format = ZPixmap;
78 xi.byte_order = ImageByteOrder (dpy); 130 xi.byte_order = ImageByteOrder (dpy);
79 xi.bitmap_unit = 0; //XY only, unused 131 xi.bitmap_unit = 0; //XY only, unused
80 xi.bitmap_bit_order = 0; //XY only, unused 132 xi.bitmap_bit_order = 0; //XY only, unused
81 xi.bitmap_pad = BitmapPad (dpy); 133 xi.bitmap_pad = BitmapPad (dpy);
82 xi.depth = depth; 134 xi.depth = 32;
83 xi.bytes_per_line = 0; 135 xi.bytes_per_line = 0;
84 xi.bits_per_pixel = 32; //Z only 136 xi.bits_per_pixel = 32; //Z only
85 xi.red_mask = 0x00000000; //Z only, unused 137 xi.red_mask = 0x00000000; //Z only, unused
86 xi.green_mask = 0x00000000; //Z only, unused 138 xi.green_mask = 0x00000000; //Z only, unused
87 xi.blue_mask = 0x00000000; //Z only, unused 139 xi.blue_mask = 0x00000000; //Z only, unused
96 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); 148 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n");
97 149
98 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); 150 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line);
99 151
100 int rowstride = gdk_pixbuf_get_rowstride (pb); 152 int rowstride = gdk_pixbuf_get_rowstride (pb);
101 153 bool pb_has_alpha = gdk_pixbuf_get_has_alpha (pb);
102 assert (3 + (depth == 32) == gdk_pixbuf_get_n_channels (pb));
103 unsigned char *row = gdk_pixbuf_get_pixels (pb); 154 unsigned char *row = gdk_pixbuf_get_pixels (pb);
155
104 char *line = xi.data; 156 char *line = xi.data;
105 157
106 for (int y = 0; y < height; y++) 158 for (int y = 0; y < height; y++)
107 { 159 {
108 unsigned char *src = row; 160 unsigned char *src = row;
109 uint32_t *dst = (uint32_t *)line; 161 uint32_t *dst = (uint32_t *)line;
110 162
111 if (depth == 24) 163 if (!pb_has_alpha)
112 for (int x = 0; x < width; x++) 164 for (int x = 0; x < width; x++)
113 { 165 {
114 uint8_t r = *src++; 166 uint8_t r = *src++;
115 uint8_t g = *src++; 167 uint8_t g = *src++;
116 uint8_t b = *src++; 168 uint8_t b = *src++;
117 169
118 uint32_t v = (r << 16) | (g << 8) | b; 170 uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b;
119 171
120 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) 172 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch)
121 v = ecb_bswap32 (v); 173 v = ecb_bswap32 (v);
122 174
123 *dst++ = v; 175 *dst++ = v;
140 192
141 row += rowstride; 193 row += rowstride;
142 line += xi.bytes_per_line; 194 line += xi.bytes_per_line;
143 } 195 }
144 196
145 rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, depth == 24 ? PictStandardRGB24 : PictStandardARGB32), 0, 0, width, height); 197 rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height);
146 img->alloc (); 198 img->alloc ();
147 199
148 GC gc = XCreateGC (dpy, img->pm, 0, 0); 200 GC gc = XCreateGC (dpy, img->pm, 0, 0);
149 XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); 201 XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height);
150 XFreeGC (dpy, gc); 202 XFreeGC (dpy, gc);
167 219
168 g_object_unref (pb); 220 g_object_unref (pb);
169 221
170 return img; 222 return img;
171} 223}
224
172# endif 225# endif
173 226
174void 227void
175rxvt_img::destroy () 228rxvt_img::destroy ()
176{ 229{
194 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); 247 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
195 ref = new pixref (w, h); 248 ref = new pixref (w, h);
196} 249}
197 250
198Picture 251Picture
199rxvt_img::src_picture () 252rxvt_img::picture ()
200{ 253{
201 Display *dpy = s->display->dpy; 254 Display *dpy = s->display->dpy;
202 255
203 XRenderPictureAttributes pa; 256 XRenderPictureAttributes pa;
204 pa.repeat = repeat; 257 pa.repeat = repeat;
223 pm = pm2; 276 pm = pm2;
224 ref = new pixref (ref->w, ref->h); 277 ref = new pixref (ref->w, ref->h);
225} 278}
226 279
227void 280void
228rxvt_img::fill (const rxvt_color &c) 281rxvt_img::fill (const rgba &c)
229{ 282{
230 XGCValues gcv; 283 XRenderColor rc = { c.r, c.g, c.b, c.a };
231 gcv.foreground = c; 284
232 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 285 Display *dpy = s->display->dpy;
233 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 286 Picture src = picture ();
234 XFreeGC (s->display->dpy, gc); 287 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
288 XRenderFreePicture (dpy, src);
289}
290
291void
292rxvt_img::add_alpha ()
293{
294 if (format->direct.alphaMask)
295 return;
296
297 Display *dpy = s->display->dpy;
298
299 rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat);
300 img->alloc ();
301
302 Picture src = picture ();
303 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
304
305 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
306
307 XRenderFreePicture (dpy, src);
308 XRenderFreePicture (dpy, dst);
309
310 ::swap (img->ref, ref);
311 ::swap (img->pm , pm );
312
313 delete img;
235} 314}
236 315
237static void 316static void
238get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 317get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params)
239{ 318{
240 double sigma = radius / 2.0; 319 rxvt_img::nv sigma = radius / 2.0;
241 double scale = sqrt (2.0 * M_PI) * sigma; 320 rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma;
242 double sum = 0.0; 321 rxvt_img::nv sum = 0.0;
243 322
244 for (int i = 0; i < width; i++) 323 for (int i = 0; i < width; i++)
245 { 324 {
246 double x = i - width / 2; 325 rxvt_img::nv x = i - width / 2;
247 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; 326 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
248 sum += kernel[i]; 327 sum += kernel[i];
249 } 328 }
250 329
251 params[0] = XDoubleToFixed (width); 330 params[0] = XDoubleToFixed (width);
261 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 340 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
262 return clone (); 341 return clone ();
263 342
264 Display *dpy = s->display->dpy; 343 Display *dpy = s->display->dpy;
265 int size = max (rh, rv) * 2 + 1; 344 int size = max (rh, rv) * 2 + 1;
266 double *kernel = (double *)malloc (size * sizeof (double)); 345 nv *kernel = (nv *)malloc (size * sizeof (nv));
267 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 346 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
268 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 347 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
269 img->alloc (); 348 img->alloc ();
270 349
271 XRenderPictureAttributes pa; 350 XRenderPictureAttributes pa;
309 w, h); 388 w, h);
310 } 389 }
311 390
312 free (kernel); 391 free (kernel);
313 free (params); 392 free (params);
393
314 XRenderFreePicture (dpy, src); 394 XRenderFreePicture (dpy, src);
315 XRenderFreePicture (dpy, dst); 395 XRenderFreePicture (dpy, dst);
316 XRenderFreePicture (dpy, tmp); 396 XRenderFreePicture (dpy, tmp);
317 397
318 return img; 398 return img;
319} 399}
320 400
321static Picture 401static Picture
322create_xrender_mask (Display *dpy, Drawable drawable, Bool argb) 402create_xrender_mask (Display *dpy, Drawable drawable, Bool argb, Bool component_alpha)
323{ 403{
324 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8); 404 Pixmap pixmap = XCreatePixmap (dpy, drawable, 1, 1, argb ? 32 : 8);
325 405
326 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8); 406 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, argb ? PictStandardARGB32 : PictStandardA8);
327 XRenderPictureAttributes pa; 407 XRenderPictureAttributes pa;
328 pa.repeat = RepeatNormal; 408 pa.repeat = RepeatNormal;
409 pa.component_alpha = component_alpha;
329 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa); 410 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat | CPComponentAlpha, &pa);
330 411
331 XFreePixmap (dpy, pixmap); 412 XFreePixmap (dpy, pixmap);
332 413
333 return mask; 414 return mask;
334} 415}
391 if (r < 0 || g < 0 || b < 0 || a < 0) 472 if (r < 0 || g < 0 || b < 0 || a < 0)
392 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); 473 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
393 474
394 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 475 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
395 img->alloc (); 476 img->alloc ();
396 477 img->fill (rgba (0, 0, 0, 0));
397 {
398 rxvt_color empty;
399 empty.set (s, rgba (0, 0, 0, 0));
400 img->fill (empty);
401 }
402 478
403 // premultiply (yeah, these are not exact, sue me or fix it) 479 // premultiply (yeah, these are not exact, sue me or fix it)
404 r = (r * (a >> 8)) >> 8; 480 r = (r * (a >> 8)) >> 8;
405 g = (g * (a >> 8)) >> 8; 481 g = (g * (a >> 8)) >> 8;
406 b = (b * (a >> 8)) >> 8; 482 b = (b * (a >> 8)) >> 8;
407 483
408 Display *dpy = s->display->dpy; 484 Display *dpy = s->display->dpy;
409 485
410 Picture src = src_picture (); 486 Picture src = picture ();
411 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); 487 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
412 Picture mul = create_xrender_mask (dpy, pm, True); 488 Picture mul = create_xrender_mask (dpy, pm, True, True);
413
414 XRenderPictureAttributes pa;
415 pa.component_alpha = 1;
416 XRenderChangePicture (dpy, mul, CPComponentAlpha, &pa);
417 489
418 //TODO: this operator does not yet implement some useful contrast 490 //TODO: this operator does not yet implement some useful contrast
419 while (r | g | b | a) 491 while (r | g | b | a)
420 { 492 {
421 unsigned short xr, xg, xb, xa; 493 unsigned short xr, xg, xb, xa;
436 ::swap (img->pm , pm ); 508 ::swap (img->pm , pm );
437 509
438 delete img; 510 delete img;
439} 511}
440 512
513void
514rxvt_img::draw (rxvt_img *img, int op, nv mask)
515{
516 unshare ();
517
518 Display *dpy = s->display->dpy;
519 Picture src = img->picture ();
520 Picture dst = picture ();
521 Picture mask_p = 0;
522
523 if (mask != 1.)
524 {
525 mask_p = create_xrender_mask (dpy, img->pm, False, False);
526 XRenderColor mask_c = { 0, 0, 0, float_to_component (mask) };
527 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
528 }
529
530 XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h);
531
532 XRenderFreePicture (dpy, src);
533 XRenderFreePicture (dpy, dst);
534
535 if (mask_p)
536 XRenderFreePicture (dpy, mask_p);
537}
538
441rxvt_img * 539rxvt_img *
442rxvt_img::clone () 540rxvt_img::clone ()
443{ 541{
444 return new rxvt_img (*this); 542 return new rxvt_img (*this);
445}
446
447static XRenderPictFormat *
448find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
449{
450 if (format->direct.alphaMask)
451 return format; // already has alpha
452
453 // try to find a suitable alpha format, one bit alpha is enough for our purposes
454 if (format->type == PictTypeDirect)
455 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
456 if (f->direct.alphaMask
457 && f->type == PictTypeDirect
458 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
459 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
460 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
461 return f;
462
463 // should be a very good fallback
464 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
465} 543}
466 544
467rxvt_img * 545rxvt_img *
468rxvt_img::reify () 546rxvt_img::reify ()
469{ 547{
478 && repeat == RepeatNone; // and we have no good pixels to fill with 556 && repeat == RepeatNone; // and we have no good pixels to fill with
479 557
480 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); 558 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat);
481 img->alloc (); 559 img->alloc ();
482 560
483 Picture src = src_picture (); 561 Picture src = picture ();
484 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 562 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
485 563
486 if (alpha) 564 if (alpha)
487 { 565 {
488 XRenderColor rc = { 0, 0, 0, 0 }; 566 XRenderColor rc = { 0, 0, 0, 0 };
489 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles 567 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles
490 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); 568 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, ref->w, ref->h);
491 } 569 }
492 else 570 else
493 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); 571 XRenderComposite (dpy, PictOpSrc, src, None, dst, -x, -y, 0, 0, 0, 0, w, h);
494 572
495 XRenderFreePicture (dpy, src); 573 XRenderFreePicture (dpy, src);
496 XRenderFreePicture (dpy, dst); 574 XRenderFreePicture (dpy, dst);
497 575
498 return img; 576 return img;
501rxvt_img * 579rxvt_img *
502rxvt_img::sub_rect (int x, int y, int width, int height) 580rxvt_img::sub_rect (int x, int y, int width, int height)
503{ 581{
504 rxvt_img *img = clone (); 582 rxvt_img *img = clone ();
505 583
506 img->x += x; 584 img->x -= x;
507 img->y += y; 585 img->y -= y;
508 586
509 if (w != width || h != height) 587 if (w != width || h != height)
510 { 588 {
511 img->w = width; 589 img->w = width;
512 img->h = height; 590 img->h = height;
517 } 595 }
518 596
519 return img; 597 return img;
520} 598}
521 599
522rxvt_img * 600static void
523rxvt_img::transform (int new_width, int new_height, double matrix[9]) 601mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3])
524{ 602{
603 rxvt_img::nv s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2];
604 rxvt_img::nv s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1];
605 rxvt_img::nv s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2];
606
607 rxvt_img::nv invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2);
608
609 inv [0][0] = invdet * s0;
610 inv [0][1] = invdet * s1;
611 inv [0][2] = invdet * s2;
612
613 inv [1][0] = invdet * (mat [2][0] * mat [1][2] - mat [2][2] * mat [1][0]);
614 inv [1][1] = invdet * (mat [2][2] * mat [0][0] - mat [2][0] * mat [0][2]);
615 inv [1][2] = invdet * (mat [1][0] * mat [0][2] - mat [1][2] * mat [0][0]);
616
617 inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]);
618 inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]);
619 inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]);
620}
621
622static rxvt_img::nv
623mat_apply (rxvt_img::nv mat[3][3], int i, rxvt_img::nv x, rxvt_img::nv y)
624{
625 rxvt_img::nv v = mat [i][0] * x + mat [i][1] * y + mat [i][2];
626 rxvt_img::nv w = mat [2][0] * x + mat [2][1] * y + mat [2][2];
627
628 return v * (1. / w);
629}
630
631rxvt_img *
632rxvt_img::transform (nv matrix[3][3])
633{
634 // calculate new pixel bounding box coordinates
635 nv rmin[2], rmax[2];
636
637 for (int i = 0; i < 2; ++i)
638 {
639 nv v;
640
641 v = mat_apply (matrix, i, 0+x, 0+y); rmin [i] = rmax [i] = v;
642 v = mat_apply (matrix, i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v);
643 v = mat_apply (matrix, i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v);
644 v = mat_apply (matrix, i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v);
645 }
646
647 // TODO: adjust matrix for subpixel accuracy
648 int dx = floor (rmin [0]);
649 int dy = floor (rmin [1]);
650
651 int new_width = ceil (rmax [0] - dx);
652 int new_height = ceil (rmax [1] - dy);
653
654 nv inv[3][3];
655 mat_invert (matrix, inv);
656
525 rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); 657 rxvt_img *img = new rxvt_img (s, format, dx, dy, new_width, new_height, repeat);
526 img->alloc (); 658 img->alloc ();
527 659
528 Display *dpy = s->display->dpy; 660 Display *dpy = s->display->dpy;
529 Picture src = src_picture (); 661 Picture src = picture ();
530 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 662 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
531 663
532 XTransform xfrm; 664 XTransform xfrm;
533 665
534 for (int i = 0; i < 3; ++i) 666 for (int i = 0; i < 3; ++i)
535 for (int j = 0; j < 3; ++j) 667 for (int j = 0; j < 3; ++j)
536 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 668 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
537
538 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO
539 xfrm.matrix [1][2] -= XDoubleToFixed (y);
540 669
541 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 670 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
542 XRenderSetPictureTransform (dpy, src, &xfrm); 671 XRenderSetPictureTransform (dpy, src, &xfrm);
543 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); 672 XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height);
544 673
545 XRenderFreePicture (dpy, src); 674 XRenderFreePicture (dpy, src);
546 XRenderFreePicture (dpy, dst); 675 XRenderFreePicture (dpy, dst);
547 676
548 return img; 677 return img;
552rxvt_img::scale (int new_width, int new_height) 681rxvt_img::scale (int new_width, int new_height)
553{ 682{
554 if (w == new_width && h == new_height) 683 if (w == new_width && h == new_height)
555 return clone (); 684 return clone ();
556 685
557 double matrix[9] = { 686 nv matrix[3][3] = {
558 w / (double)new_width, 0, 0, 687 { new_width / (nv)w, 0, 0 },
559 0, h / (double)new_height, 0, 688 { 0, new_height / (nv)h, 0 },
560 0, 0, 1 689 { 0, 0, 1 }
561 }; 690 };
562 691
563 int old_repeat_mode = repeat; 692 int old_repeat_mode = repeat;
564 repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems 693 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
565 694
566 rxvt_img *img = transform (new_width, new_height, matrix); 695 rxvt_img *img = transform (matrix);
567 696
568 repeat = old_repeat_mode; 697 repeat = old_repeat_mode;
569 img->repeat = repeat; 698 img->repeat = repeat;
570 699
571 return img; 700 return img;
572} 701}
573 702
574rxvt_img * 703rxvt_img *
575rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) 704rxvt_img::rotate (int cx, int cy, nv phi)
576{ 705{
577 double s = sin (phi); 706 nv s = sin (phi);
578 double c = cos (phi); 707 nv c = cos (phi);
579 708
580 double matrix[9] = { 709 nv matrix[3][3] = {
581 c, -s, -c * x + s * y + x, 710 { c, -s, cx - c * cx + s * cy + 200 },
582 s, c, -s * x - c * y + y, 711 { s, c, cy - s * cx - c * cy },
583 0, 0, 1 712 { 0, 0, 1 }
713 //{ c, -s, 0 },
714 //{ s, c, 0 },
715 //{ 0, 0, 1 }
584 }; 716 };
585 717
586 return transform (new_width, new_height, matrix); 718 //move (-cx, -cy);
587} 719 rxvt_img *img = transform (matrix);
720 //move ( cx, cy);
721 //img->move (cx, cy);
588 722
723 return img;
724}
725
589rxvt_img * 726rxvt_img *
590rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) 727rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
591{ 728{
592 if (new_format == format) 729 if (new_format == format)
593 return clone (); 730 return clone ();
594 731
595 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); 732 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
596 img->alloc (); 733 img->alloc ();
597 734
598 Display *dpy = s->display->dpy; 735 Display *dpy = s->display->dpy;
599 Picture src = src_picture (); 736 Picture src = picture ();
600 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); 737 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
601 int op = PictOpSrc; 738 int op = PictOpSrc;
602 739
603 if (format->direct.alphaMask && !new_format->direct.alphaMask) 740 if (format->direct.alphaMask && !new_format->direct.alphaMask)
604 { 741 {
605 // does it have to be that complicated 742 // does it have to be that complicated
606 rgba c;
607 bg.get (c);
608
609 XRenderColor rc = { c.r, c.g, c.b, 0xffff }; 743 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
610 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); 744 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
611 745
612 op = PictOpOver; 746 op = PictOpOver;
613 } 747 }
614 748
619 753
620 return img; 754 return img;
621} 755}
622 756
623rxvt_img * 757rxvt_img *
624rxvt_img::blend (rxvt_img *img, double factor) 758rxvt_img::blend (rxvt_img *img, nv factor)
625{ 759{
626 rxvt_img *img2 = clone (); 760 rxvt_img *img2 = clone ();
627 Display *dpy = s->display->dpy; 761 Display *dpy = s->display->dpy;
628 Picture src = img->src_picture (); 762 Picture src = img->picture ();
629 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 763 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
630 764 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
631 Pixmap pixmap = XCreatePixmap (dpy, img->pm, 1, 1, 8);
632 XRenderPictFormat *format = XRenderFindStandardFormat (dpy, PictStandardA8);
633 XRenderPictureAttributes pa;
634 pa.repeat = True;
635 Picture mask = XRenderCreatePicture (dpy, pixmap, format, CPRepeat, &pa);
636 XFreePixmap (dpy, pixmap);
637 765
638 XRenderColor mask_c; 766 XRenderColor mask_c;
639 767
640 mask_c.alpha = float_to_component (factor); 768 mask_c.alpha = float_to_component (factor);
641 mask_c.red = 769 mask_c.red =

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines