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.75 by sf-exg, Sun Jun 10 07:51:45 2012 UTC vs.
Revision 1.92 by root, Fri Jun 15 18:05:15 2012 UTC

1#include <string.h>
1#include <math.h> 2#include <math.h>
2#include "../config.h" 3#include "../config.h"
3#include "rxvt.h" 4#include "rxvt.h"
4 5
5#if HAVE_IMG 6#if HAVE_IMG
7
8typedef rxvt_img::nv nv;
9
10struct mat3x3
11{
12 nv v[3][3];
13
14 mat3x3 ()
15 {
16 }
17
18 mat3x3 (nv matrix[3][3])
19 {
20 memcpy (v, matrix, sizeof (v));
21 }
22
23 mat3x3 (nv v11, nv v12, nv v13, nv v21, nv v22, nv v23, nv v31, nv v32, nv v33)
24 {
25 v[0][0] = v11; v[0][1] = v12; v[0][2] = v13;
26 v[1][0] = v21; v[1][1] = v22; v[1][2] = v23;
27 v[2][0] = v31; v[2][1] = v32; v[2][2] = v33;
28 }
29
30 mat3x3 invert ();
31
32 nv *operator [](int i) { return &v[i][0]; }
33 const nv *operator [](int i) const { return &v[i][0]; }
34
35 // quite inefficient, hopefully gcc pulls the w calc out of any loops
36 nv apply1 (int i, nv x, nv y)
37 {
38 mat3x3 &m = *this;
39
40 nv v = m[i][0] * x + m[i][1] * y + m[i][2];
41 nv w = m[2][0] * x + m[2][1] * y + m[2][2];
42
43 return v * (1. / w);
44 }
45
46 static mat3x3 translate (nv x, nv y);
47};
48
49mat3x3
50mat3x3::invert ()
51{
52 mat3x3 &m = *this;
53 mat3x3 inv;
54
55 nv s0 = m[2][2] * m[1][1] - m[2][1] * m[1][2];
56 nv s1 = m[2][1] * m[0][2] - m[2][2] * m[0][1];
57 nv s2 = m[1][2] * m[0][1] - m[1][1] * m[0][2];
58
59 nv invdet = 1. / (m[0][0] * s0 + m[1][0] * s1 + m[2][0] * s2);
60
61 inv[0][0] = invdet * s0;
62 inv[0][1] = invdet * s1;
63 inv[0][2] = invdet * s2;
64
65 inv[1][0] = invdet * (m[2][0] * m[1][2] - m[2][2] * m[1][0]);
66 inv[1][1] = invdet * (m[2][2] * m[0][0] - m[2][0] * m[0][2]);
67 inv[1][2] = invdet * (m[1][0] * m[0][2] - m[1][2] * m[0][0]);
68
69 inv[2][0] = invdet * (m[2][1] * m[1][0] - m[2][0] * m[1][1]);
70 inv[2][1] = invdet * (m[2][0] * m[0][1] - m[2][1] * m[0][0]);
71 inv[2][2] = invdet * (m[1][1] * m[0][0] - m[1][0] * m[0][1]);
72
73 return inv;
74}
75
76static mat3x3
77operator *(const mat3x3 &a, const mat3x3 &b)
78{
79 mat3x3 r;
80
81 for (int i = 0; i < 3; ++i)
82 for (int j = 0; j < 3; ++j)
83 r[i][j] = a[i][0] * b[0][j]
84 + a[i][1] * b[1][j]
85 + a[i][2] * b[2][j];
86
87 return r;
88}
89
90mat3x3
91mat3x3::translate (nv x, nv y)
92{
93 return mat3x3 (
94 1, 0, x,
95 0, 1, y,
96 0, 0, 1
97 );
98}
99
100#if 0
101struct pict
102{
103 Display *dpy;
104 Picture pic;
105
106 operator Picture () const
107 {
108 return pic;
109 }
110
111 pict ()
112 : pic (0)
113 {
114 }
115
116 pict (rxvt_img *img, XRenderPictFormat *format = 0)
117 : dpy (img->s->display->dpy)
118 {
119 XRenderPictureAttributes pa;
120 pa.repeat = img->repeat;
121 pic = XRenderCreatePicture (dpy, img->pm, format ? format : img->format, CPRepeat, &pa);
122 }
123
124 ~pict ()
125 {
126 if (pic)
127 XRenderFreePicture (dpy, pic);
128 }
129};
130#endif
131
132static XRenderPictFormat *
133find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
134{
135 if (format->direct.alphaMask)
136 return format; // already has alpha
137
138 // try to find a suitable alpha format, one bit alpha is enough for our purposes
139 if (format->type == PictTypeDirect)
140 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
141 if (f->direct.alphaMask
142 && f->type == PictTypeDirect
143 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
144 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
145 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
146 return f;
147
148 // should be a very good fallback
149 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
150}
6 151
7rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) 152rxvt_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), 153: s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat),
9 pm(0), ref(0) 154 pm(0), ref(0)
10{ 155{
50 195
51 return img; 196 return img;
52} 197}
53 198
54# if HAVE_PIXBUF 199# if HAVE_PIXBUF
200
55rxvt_img * 201rxvt_img *
56rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb) 202rxvt_img::new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb)
57{ 203{
58 Display *dpy = s->display->dpy; 204 Display *dpy = s->display->dpy;
59 205
63 if (width > 32767 || height > 32767) // well, we *could* upload in chunks 209 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"); 210 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big (maximum size 32768x32768).\n");
65 211
66 // since we require rgb24/argb32 formats from xrender we assume 212 // 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 213 // 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 214
70 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst; 215 int byte_order = ecb_big_endian () ? MSBFirst : LSBFirst;
71 216
72 XImage xi; 217 XImage xi;
73 218
77 xi.format = ZPixmap; 222 xi.format = ZPixmap;
78 xi.byte_order = ImageByteOrder (dpy); 223 xi.byte_order = ImageByteOrder (dpy);
79 xi.bitmap_unit = 0; //XY only, unused 224 xi.bitmap_unit = 0; //XY only, unused
80 xi.bitmap_bit_order = 0; //XY only, unused 225 xi.bitmap_bit_order = 0; //XY only, unused
81 xi.bitmap_pad = BitmapPad (dpy); 226 xi.bitmap_pad = BitmapPad (dpy);
82 xi.depth = depth; 227 xi.depth = 32;
83 xi.bytes_per_line = 0; 228 xi.bytes_per_line = 0;
84 xi.bits_per_pixel = 32; //Z only 229 xi.bits_per_pixel = 32; //Z only
85 xi.red_mask = 0x00000000; //Z only, unused 230 xi.red_mask = 0x00000000; //Z only, unused
86 xi.green_mask = 0x00000000; //Z only, unused 231 xi.green_mask = 0x00000000; //Z only, unused
87 xi.blue_mask = 0x00000000; //Z only, unused 232 xi.blue_mask = 0x00000000; //Z only, unused
96 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n"); 241 rxvt_fatal ("rxvt_img::new_from_pixbuf: image too big for Xlib.\n");
97 242
98 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line); 243 xi.data = (char *)rxvt_malloc (height * xi.bytes_per_line);
99 244
100 int rowstride = gdk_pixbuf_get_rowstride (pb); 245 int rowstride = gdk_pixbuf_get_rowstride (pb);
101 246 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); 247 unsigned char *row = gdk_pixbuf_get_pixels (pb);
248
104 char *line = xi.data; 249 char *line = xi.data;
105 250
106 for (int y = 0; y < height; y++) 251 for (int y = 0; y < height; y++)
107 { 252 {
108 unsigned char *src = row; 253 unsigned char *src = row;
109 uint32_t *dst = (uint32_t *)line; 254 uint32_t *dst = (uint32_t *)line;
110 255
111 if (depth == 24) 256 if (!pb_has_alpha)
112 for (int x = 0; x < width; x++) 257 for (int x = 0; x < width; x++)
113 { 258 {
114 uint8_t r = *src++; 259 uint8_t r = *src++;
115 uint8_t g = *src++; 260 uint8_t g = *src++;
116 uint8_t b = *src++; 261 uint8_t b = *src++;
117 262
118 uint32_t v = (r << 16) | (g << 8) | b; 263 uint32_t v = (255 << 24) | (r << 16) | (g << 8) | b;
119 264
120 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch) 265 if (ecb_big_endian () ? !byte_order_mismatch : byte_order_mismatch)
121 v = ecb_bswap32 (v); 266 v = ecb_bswap32 (v);
122 267
123 *dst++ = v; 268 *dst++ = v;
140 285
141 row += rowstride; 286 row += rowstride;
142 line += xi.bytes_per_line; 287 line += xi.bytes_per_line;
143 } 288 }
144 289
145 rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, depth == 24 ? PictStandardRGB24 : PictStandardARGB32), 0, 0, width, height); 290 rxvt_img *img = new rxvt_img (s, XRenderFindStandardFormat (dpy, PictStandardARGB32), 0, 0, width, height);
146 img->alloc (); 291 img->alloc ();
147 292
148 GC gc = XCreateGC (dpy, img->pm, 0, 0); 293 GC gc = XCreateGC (dpy, img->pm, 0, 0);
149 XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height); 294 XPutImage (dpy, img->pm, gc, &xi, 0, 0, 0, 0, width, height);
150 XFreeGC (dpy, gc); 295 XFreeGC (dpy, gc);
167 312
168 g_object_unref (pb); 313 g_object_unref (pb);
169 314
170 return img; 315 return img;
171} 316}
317
172# endif 318# endif
173 319
174void 320void
175rxvt_img::destroy () 321rxvt_img::destroy ()
176{ 322{
194 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); 340 pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth);
195 ref = new pixref (w, h); 341 ref = new pixref (w, h);
196} 342}
197 343
198Picture 344Picture
199rxvt_img::src_picture () 345rxvt_img::picture ()
200{ 346{
201 Display *dpy = s->display->dpy; 347 Display *dpy = s->display->dpy;
202 348
203 XRenderPictureAttributes pa; 349 XRenderPictureAttributes pa;
204 pa.repeat = repeat; 350 pa.repeat = repeat;
223 pm = pm2; 369 pm = pm2;
224 ref = new pixref (ref->w, ref->h); 370 ref = new pixref (ref->w, ref->h);
225} 371}
226 372
227void 373void
228rxvt_img::fill (const rxvt_color &c) 374rxvt_img::fill (const rgba &c)
229{ 375{
230 XGCValues gcv; 376 XRenderColor rc = { c.r, c.g, c.b, c.a };
231 gcv.foreground = c; 377
232 GC gc = XCreateGC (s->display->dpy, pm, GCForeground, &gcv); 378 Display *dpy = s->display->dpy;
233 XFillRectangle (s->display->dpy, pm, gc, 0, 0, w, h); 379 Picture src = picture ();
234 XFreeGC (s->display->dpy, gc); 380 XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h);
381 XRenderFreePicture (dpy, src);
382}
383
384void
385rxvt_img::add_alpha ()
386{
387 if (format->direct.alphaMask)
388 return;
389
390 Display *dpy = s->display->dpy;
391
392 rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat);
393 img->alloc ();
394
395 Picture src = picture ();
396 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
397
398 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h);
399
400 XRenderFreePicture (dpy, src);
401 XRenderFreePicture (dpy, dst);
402
403 ::swap (img->ref, ref);
404 ::swap (img->pm , pm );
405
406 delete img;
235} 407}
236 408
237static void 409static void
238get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) 410get_gaussian_kernel (int radius, int width, nv *kernel, XFixed *params)
239{ 411{
240 double sigma = radius / 2.0; 412 nv sigma = radius / 2.0;
241 double scale = sqrt (2.0 * M_PI) * sigma; 413 nv scale = sqrt (2.0 * M_PI) * sigma;
242 double sum = 0.0; 414 nv sum = 0.0;
243 415
244 for (int i = 0; i < width; i++) 416 for (int i = 0; i < width; i++)
245 { 417 {
246 double x = i - width / 2; 418 nv x = i - width / 2;
247 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; 419 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
248 sum += kernel[i]; 420 sum += kernel[i];
249 } 421 }
250 422
251 params[0] = XDoubleToFixed (width); 423 params[0] = XDoubleToFixed (width);
261 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) 433 if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV))
262 return clone (); 434 return clone ();
263 435
264 Display *dpy = s->display->dpy; 436 Display *dpy = s->display->dpy;
265 int size = max (rh, rv) * 2 + 1; 437 int size = max (rh, rv) * 2 + 1;
266 double *kernel = (double *)malloc (size * sizeof (double)); 438 nv *kernel = (nv *)malloc (size * sizeof (nv));
267 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); 439 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
268 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 440 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
269 img->alloc (); 441 img->alloc ();
270 442
271 XRenderPictureAttributes pa; 443 XRenderPictureAttributes pa;
309 w, h); 481 w, h);
310 } 482 }
311 483
312 free (kernel); 484 free (kernel);
313 free (params); 485 free (params);
486
314 XRenderFreePicture (dpy, src); 487 XRenderFreePicture (dpy, src);
315 XRenderFreePicture (dpy, dst); 488 XRenderFreePicture (dpy, dst);
316 XRenderFreePicture (dpy, tmp); 489 XRenderFreePicture (dpy, tmp);
317 490
318 return img; 491 return img;
392 if (r < 0 || g < 0 || b < 0 || a < 0) 565 if (r < 0 || g < 0 || b < 0 || a < 0)
393 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); 566 rxvt_fatal ("rxvt_img::contrast does not support negative values.\n");
394 567
395 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); 568 rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat);
396 img->alloc (); 569 img->alloc ();
397 570 img->fill (rgba (0, 0, 0, 0));
398 {
399 rxvt_color empty;
400 empty.set (s, rgba (0, 0, 0, 0));
401 img->fill (empty);
402 }
403 571
404 // premultiply (yeah, these are not exact, sue me or fix it) 572 // premultiply (yeah, these are not exact, sue me or fix it)
405 r = (r * (a >> 8)) >> 8; 573 r = (r * (a >> 8)) >> 8;
406 g = (g * (a >> 8)) >> 8; 574 g = (g * (a >> 8)) >> 8;
407 b = (b * (a >> 8)) >> 8; 575 b = (b * (a >> 8)) >> 8;
408 576
409 Display *dpy = s->display->dpy; 577 Display *dpy = s->display->dpy;
410 578
411 Picture src = src_picture (); 579 Picture src = picture ();
412 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); 580 Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0);
413 Picture mul = create_xrender_mask (dpy, pm, True, True); 581 Picture mul = create_xrender_mask (dpy, pm, True, True);
414 582
415 //TODO: this operator does not yet implement some useful contrast 583 //TODO: this operator does not yet implement some useful contrast
416 while (r | g | b | a) 584 while (r | g | b | a)
433 ::swap (img->pm , pm ); 601 ::swap (img->pm , pm );
434 602
435 delete img; 603 delete img;
436} 604}
437 605
606void
607rxvt_img::draw (rxvt_img *img, int op, nv mask)
608{
609 unshare ();
610
611 Display *dpy = s->display->dpy;
612 Picture src = img->picture ();
613 Picture dst = picture ();
614 Picture mask_p = 0;
615
616 if (mask != 1.)
617 {
618 mask_p = create_xrender_mask (dpy, img->pm, False, False);
619 XRenderColor mask_c = { 0, 0, 0, float_to_component (mask) };
620 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
621 }
622
623 XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h);
624
625 XRenderFreePicture (dpy, src);
626 XRenderFreePicture (dpy, dst);
627
628 if (mask_p)
629 XRenderFreePicture (dpy, mask_p);
630}
631
438rxvt_img * 632rxvt_img *
439rxvt_img::clone () 633rxvt_img::clone ()
440{ 634{
441 return new rxvt_img (*this); 635 return new rxvt_img (*this);
442}
443
444static XRenderPictFormat *
445find_alpha_format_for (Display *dpy, XRenderPictFormat *format)
446{
447 if (format->direct.alphaMask)
448 return format; // already has alpha
449
450 // try to find a suitable alpha format, one bit alpha is enough for our purposes
451 if (format->type == PictTypeDirect)
452 for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n)
453 if (f->direct.alphaMask
454 && f->type == PictTypeDirect
455 && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask )
456 && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask)
457 && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask ))
458 return f;
459
460 // should be a very good fallback
461 return XRenderFindStandardFormat (dpy, PictStandardARGB32);
462} 636}
463 637
464rxvt_img * 638rxvt_img *
465rxvt_img::reify () 639rxvt_img::reify ()
466{ 640{
475 && repeat == RepeatNone; // and we have no good pixels to fill with 649 && repeat == RepeatNone; // and we have no good pixels to fill with
476 650
477 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); 651 rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat);
478 img->alloc (); 652 img->alloc ();
479 653
480 Picture src = src_picture (); 654 Picture src = picture ();
481 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 655 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
482 656
483 if (alpha) 657 if (alpha)
484 { 658 {
485 XRenderColor rc = { 0, 0, 0, 0 }; 659 XRenderColor rc = { 0, 0, 0, 0 };
486 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles 660 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles
487 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); 661 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, ref->w, ref->h);
488 } 662 }
489 else 663 else
490 XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); 664 XRenderComposite (dpy, PictOpSrc, src, None, dst, -x, -y, 0, 0, 0, 0, w, h);
491 665
492 XRenderFreePicture (dpy, src); 666 XRenderFreePicture (dpy, src);
493 XRenderFreePicture (dpy, dst); 667 XRenderFreePicture (dpy, dst);
494 668
495 return img; 669 return img;
498rxvt_img * 672rxvt_img *
499rxvt_img::sub_rect (int x, int y, int width, int height) 673rxvt_img::sub_rect (int x, int y, int width, int height)
500{ 674{
501 rxvt_img *img = clone (); 675 rxvt_img *img = clone ();
502 676
503 img->x += x; 677 img->x -= x;
504 img->y += y; 678 img->y -= y;
505 679
506 if (w != width || h != height) 680 if (w != width || h != height)
507 { 681 {
508 img->w = width; 682 img->w = width;
509 img->h = height; 683 img->h = height;
515 689
516 return img; 690 return img;
517} 691}
518 692
519rxvt_img * 693rxvt_img *
520rxvt_img::transform (int new_width, int new_height, double matrix[9]) 694rxvt_img::transform (nv matrix[3][3])
521{ 695{
696 // calculate new pixel bounding box coordinates
697 nv r[2], rmin[2], rmax[2];
698
699 mat3x3 m (matrix);
700
701 for (int i = 0; i < 2; ++i)
702 {
703 nv v;
704
705 v = m.apply1 (i, 0+x, 0+y); rmin [i] = rmax [i] = v; r [i] = v;
706 v = m.apply1 (i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v);
707 v = m.apply1 (i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v);
708 v = m.apply1 (i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v);
709 }
710
711 float sx = rmin [0] - x;
712 float sy = rmin [1] - y;
713
714 // TODO: adjust matrix for subpixel accuracy
715 int nx = floor (rmin [0]);
716 int ny = floor (rmin [1]);
717
718 int new_width = ceil (rmax [0] - rmin [0]);
719 int new_height = ceil (rmax [1] - rmin [1]);
720
721 m = mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y);
722
723 mat3x3 inv = m.invert ();
724
522 rxvt_img *img = new rxvt_img (s, format, 0, 0, new_width, new_height, repeat); 725 rxvt_img *img = new rxvt_img (s, format, nx, ny, new_width, new_height, repeat);
523 img->alloc (); 726 img->alloc ();
524 727
525 Display *dpy = s->display->dpy; 728 Display *dpy = s->display->dpy;
526 Picture src = src_picture (); 729 Picture src = picture ();
527 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); 730 Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0);
528 731
529 XTransform xfrm; 732 XTransform xfrm;
530 733
531 for (int i = 0; i < 3; ++i) 734 for (int i = 0; i < 3; ++i)
532 for (int j = 0; j < 3; ++j) 735 for (int j = 0; j < 3; ++j)
533 xfrm.matrix [i][j] = XDoubleToFixed (matrix [i * 3 + j]); 736 xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]);
534
535 xfrm.matrix [0][2] -= XDoubleToFixed (x);//TODO
536 xfrm.matrix [1][2] -= XDoubleToFixed (y);
537 737
538 XRenderSetPictureFilter (dpy, src, "good", 0, 0); 738 XRenderSetPictureFilter (dpy, src, "good", 0, 0);
539 XRenderSetPictureTransform (dpy, src, &xfrm); 739 XRenderSetPictureTransform (dpy, src, &xfrm);
540 XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, new_width, new_height); 740 XRenderComposite (dpy, PictOpSrc, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height);
741#if 1
742 {
743 XRenderColor rc = { 65535,0,0,65535 };
744 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, new_width, new_height);
745 }{
746 XRenderColor rc = { 0,0,0,65535 };
747 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 1, 1, new_width - 2, new_height - 2);
748 }
749 XRenderComposite (dpy, PictOpOver, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height);
750#endif
541 751
542 XRenderFreePicture (dpy, src); 752 XRenderFreePicture (dpy, src);
543 XRenderFreePicture (dpy, dst); 753 XRenderFreePicture (dpy, dst);
544 754
545 return img; 755 return img;
549rxvt_img::scale (int new_width, int new_height) 759rxvt_img::scale (int new_width, int new_height)
550{ 760{
551 if (w == new_width && h == new_height) 761 if (w == new_width && h == new_height)
552 return clone (); 762 return clone ();
553 763
554 double matrix[9] = { 764 nv matrix[3][3] = {
555 w / (double)new_width, 0, 0, 765 { new_width / (nv)w, 0, 0 },
556 0, h / (double)new_height, 0, 766 { 0, new_height / (nv)h, 0 },
557 0, 0, 1 767 { 0, 0, 1 }
558 }; 768 };
559 769
560 int old_repeat_mode = repeat; 770 int old_repeat_mode = repeat;
561 repeat = RepeatPad; // not right, but xrender can't proeprly scale it seems 771 repeat = RepeatPad; // not right, but xrender can't properly scale it seems
562 772
563 rxvt_img *img = transform (new_width, new_height, matrix); 773 rxvt_img *img = transform (matrix);
564 774
565 repeat = old_repeat_mode; 775 repeat = old_repeat_mode;
566 img->repeat = repeat; 776 img->repeat = repeat;
567 777
568 return img; 778 return img;
569} 779}
570 780
571rxvt_img * 781rxvt_img *
572rxvt_img::rotate (int new_width, int new_height, int x, int y, double phi) 782rxvt_img::rotate (int cx, int cy, nv phi)
573{ 783{
574 double s = sin (phi); 784 nv s = sin (phi);
575 double c = cos (phi); 785 nv c = cos (phi);
576 786
577 double matrix[9] = { 787 nv matrix[3][3] = {
788#if 0
578 c, -s, -c * x + s * y + x, 789 { c, -s, cx - c * cx + s * cy },
579 s, c, -s * x - c * y + y, 790 { s, c, cy - s * cx - c * cy },
580 0, 0, 1 791 { 0, 0, 1 }
792#else
793 { c, -s, 0 },
794 { s, c, 0 },
795 { 0, 0, 1 }
796#endif
581 }; 797 };
582 798
583 return transform (new_width, new_height, matrix); 799 move (-cx, -cy);
584} 800 rxvt_img *img = transform (matrix);
801 move ( cx, cy);
802 img->move (cx, cy);
585 803
804 return img;
805}
806
586rxvt_img * 807rxvt_img *
587rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) 808rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg)
588{ 809{
589 if (new_format == format) 810 if (new_format == format)
590 return clone (); 811 return clone ();
591 812
592 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); 813 rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat);
593 img->alloc (); 814 img->alloc ();
594 815
595 Display *dpy = s->display->dpy; 816 Display *dpy = s->display->dpy;
596 Picture src = src_picture (); 817 Picture src = picture ();
597 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); 818 Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0);
598 int op = PictOpSrc; 819 int op = PictOpSrc;
599 820
600 if (format->direct.alphaMask && !new_format->direct.alphaMask) 821 if (format->direct.alphaMask && !new_format->direct.alphaMask)
601 { 822 {
602 // does it have to be that complicated 823 // does it have to be that complicated
603 rgba c;
604 bg.get (c);
605
606 XRenderColor rc = { c.r, c.g, c.b, 0xffff }; 824 XRenderColor rc = { bg.r, bg.g, bg.b, bg.a };
607 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); 825 XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);
608 826
609 op = PictOpOver; 827 op = PictOpOver;
610 } 828 }
611 829
616 834
617 return img; 835 return img;
618} 836}
619 837
620rxvt_img * 838rxvt_img *
621rxvt_img::blend (rxvt_img *img, double factor) 839rxvt_img::blend (rxvt_img *img, nv factor)
622{ 840{
623 rxvt_img *img2 = clone (); 841 rxvt_img *img2 = clone ();
624 Display *dpy = s->display->dpy; 842 Display *dpy = s->display->dpy;
625 Picture src = img->src_picture (); 843 Picture src = img->picture ();
626 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); 844 Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0);
627 Picture mask = create_xrender_mask (dpy, img->pm, False, False); 845 Picture mask = create_xrender_mask (dpy, img->pm, False, False);
628 846
629 XRenderColor mask_c; 847 XRenderColor mask_c;
630 848

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines