|
|
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 | |
|
|
8 | typedef rxvt_img::nv nv; |
|
|
9 | |
|
|
10 | namespace |
|
|
11 | { |
|
|
12 | |
|
|
13 | struct mat3x3 |
|
|
14 | { |
|
|
15 | nv v[3][3]; |
|
|
16 | |
|
|
17 | mat3x3 () |
|
|
18 | { |
|
|
19 | } |
|
|
20 | |
|
|
21 | mat3x3 (nv matrix[3][3]) |
|
|
22 | { |
|
|
23 | memcpy (v, matrix, sizeof (v)); |
|
|
24 | } |
|
|
25 | |
|
|
26 | mat3x3 (nv v11, nv v12, nv v13, nv v21, nv v22, nv v23, nv v31, nv v32, nv v33) |
|
|
27 | { |
|
|
28 | v[0][0] = v11; v[0][1] = v12; v[0][2] = v13; |
|
|
29 | v[1][0] = v21; v[1][1] = v22; v[1][2] = v23; |
|
|
30 | v[2][0] = v31; v[2][1] = v32; v[2][2] = v33; |
|
|
31 | } |
|
|
32 | |
|
|
33 | mat3x3 invert (); |
|
|
34 | |
|
|
35 | nv *operator [](int i) { return &v[i][0]; } |
|
|
36 | const nv *operator [](int i) const { return &v[i][0]; } |
|
|
37 | |
|
|
38 | // quite inefficient, hopefully gcc pulls the w calc out of any loops |
|
|
39 | nv apply1 (int i, nv x, nv y) |
|
|
40 | { |
|
|
41 | mat3x3 &m = *this; |
|
|
42 | |
|
|
43 | nv v = m[i][0] * x + m[i][1] * y + m[i][2]; |
|
|
44 | nv w = m[2][0] * x + m[2][1] * y + m[2][2]; |
|
|
45 | |
|
|
46 | return v * (1. / w); |
|
|
47 | } |
|
|
48 | |
|
|
49 | static mat3x3 translate (nv x, nv y); |
|
|
50 | }; |
|
|
51 | |
|
|
52 | mat3x3 |
|
|
53 | mat3x3::invert () |
|
|
54 | { |
|
|
55 | mat3x3 &m = *this; |
|
|
56 | mat3x3 inv; |
|
|
57 | |
|
|
58 | nv s0 = m[2][2] * m[1][1] - m[2][1] * m[1][2]; |
|
|
59 | nv s1 = m[2][1] * m[0][2] - m[2][2] * m[0][1]; |
|
|
60 | nv s2 = m[1][2] * m[0][1] - m[1][1] * m[0][2]; |
|
|
61 | |
|
|
62 | nv invdet = 1. / (m[0][0] * s0 + m[1][0] * s1 + m[2][0] * s2); |
|
|
63 | |
|
|
64 | inv[0][0] = invdet * s0; |
|
|
65 | inv[0][1] = invdet * s1; |
|
|
66 | inv[0][2] = invdet * s2; |
|
|
67 | |
|
|
68 | inv[1][0] = invdet * (m[2][0] * m[1][2] - m[2][2] * m[1][0]); |
|
|
69 | inv[1][1] = invdet * (m[2][2] * m[0][0] - m[2][0] * m[0][2]); |
|
|
70 | inv[1][2] = invdet * (m[1][0] * m[0][2] - m[1][2] * m[0][0]); |
|
|
71 | |
|
|
72 | inv[2][0] = invdet * (m[2][1] * m[1][0] - m[2][0] * m[1][1]); |
|
|
73 | inv[2][1] = invdet * (m[2][0] * m[0][1] - m[2][1] * m[0][0]); |
|
|
74 | inv[2][2] = invdet * (m[1][1] * m[0][0] - m[1][0] * m[0][1]); |
|
|
75 | |
|
|
76 | return inv; |
|
|
77 | } |
|
|
78 | |
|
|
79 | static mat3x3 |
|
|
80 | operator *(const mat3x3 &a, const mat3x3 &b) |
|
|
81 | { |
|
|
82 | mat3x3 r; |
|
|
83 | |
|
|
84 | for (int i = 0; i < 3; ++i) |
|
|
85 | for (int j = 0; j < 3; ++j) |
|
|
86 | r[i][j] = a[i][0] * b[0][j] |
|
|
87 | + a[i][1] * b[1][j] |
|
|
88 | + a[i][2] * b[2][j]; |
|
|
89 | |
|
|
90 | return r; |
|
|
91 | } |
|
|
92 | |
|
|
93 | mat3x3 |
|
|
94 | mat3x3::translate (nv x, nv y) |
|
|
95 | { |
|
|
96 | return mat3x3 ( |
|
|
97 | 1, 0, x, |
|
|
98 | 0, 1, y, |
|
|
99 | 0, 0, 1 |
|
|
100 | ); |
|
|
101 | } |
|
|
102 | |
|
|
103 | } |
|
|
104 | |
|
|
105 | #if 0 |
|
|
106 | struct pict |
|
|
107 | { |
|
|
108 | Display *dpy; |
|
|
109 | Picture pic; |
|
|
110 | |
|
|
111 | operator Picture () const |
|
|
112 | { |
|
|
113 | return pic; |
|
|
114 | } |
|
|
115 | |
|
|
116 | pict () |
|
|
117 | : pic (0) |
|
|
118 | { |
|
|
119 | } |
|
|
120 | |
|
|
121 | pict (rxvt_img *img, XRenderPictFormat *format = 0) |
|
|
122 | : dpy (img->s->display->dpy) |
|
|
123 | { |
|
|
124 | XRenderPictureAttributes pa; |
|
|
125 | pa.repeat = img->repeat; |
|
|
126 | pic = XRenderCreatePicture (dpy, img->pm, format ? format : img->format, CPRepeat, &pa); |
|
|
127 | } |
|
|
128 | |
|
|
129 | ~pict () |
|
|
130 | { |
|
|
131 | if (pic) |
|
|
132 | XRenderFreePicture (dpy, pic); |
|
|
133 | } |
|
|
134 | }; |
|
|
135 | #endif |
|
|
136 | |
|
|
137 | static XRenderPictFormat * |
|
|
138 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
139 | { |
|
|
140 | if (format->direct.alphaMask) |
|
|
141 | return format; // already has alpha |
|
|
142 | |
|
|
143 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
144 | if (format->type == PictTypeDirect) |
|
|
145 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
146 | if (f->direct.alphaMask |
|
|
147 | && f->type == PictTypeDirect |
|
|
148 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
149 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
150 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
|
|
151 | return f; |
|
|
152 | |
|
|
153 | // should be a very good fallback |
|
|
154 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
155 | } |
6 | |
156 | |
7 | rxvt_img::rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat) |
157 | rxvt_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), |
158 | : s(screen), x(x), y(y), w(width), h(height), format(format), repeat(repeat), |
9 | pm(0), ref(0) |
159 | pm(0), ref(0) |
10 | { |
160 | { |
… | |
… | |
195 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
345 | pm = XCreatePixmap (s->display->dpy, s->display->root, w, h, format->depth); |
196 | ref = new pixref (w, h); |
346 | ref = new pixref (w, h); |
197 | } |
347 | } |
198 | |
348 | |
199 | Picture |
349 | Picture |
200 | rxvt_img::src_picture () |
350 | rxvt_img::picture () |
201 | { |
351 | { |
202 | Display *dpy = s->display->dpy; |
352 | Display *dpy = s->display->dpy; |
203 | |
353 | |
204 | XRenderPictureAttributes pa; |
354 | XRenderPictureAttributes pa; |
205 | pa.repeat = repeat; |
355 | pa.repeat = repeat; |
… | |
… | |
224 | pm = pm2; |
374 | pm = pm2; |
225 | ref = new pixref (ref->w, ref->h); |
375 | ref = new pixref (ref->w, ref->h); |
226 | } |
376 | } |
227 | |
377 | |
228 | void |
378 | void |
229 | rxvt_img::fill (const rxvt_color &c) |
379 | rxvt_img::fill (const rgba &c) |
230 | { |
380 | { |
231 | rgba cc; |
|
|
232 | c.get (cc); |
|
|
233 | XRenderColor rc = { cc.r, cc.g, cc.b, cc.a }; |
381 | XRenderColor rc = { c.r, c.g, c.b, c.a }; |
234 | |
382 | |
235 | Display *dpy = s->display->dpy; |
383 | Display *dpy = s->display->dpy; |
236 | Picture src = src_picture (); |
384 | Picture src = picture (); |
237 | XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); |
385 | XRenderFillRectangle (dpy, PictOpSrc, src, &rc, 0, 0, w, h); |
238 | XRenderFreePicture (dpy, src); |
386 | XRenderFreePicture (dpy, src); |
239 | } |
387 | } |
240 | |
388 | |
|
|
389 | void |
|
|
390 | rxvt_img::add_alpha () |
|
|
391 | { |
|
|
392 | if (format->direct.alphaMask) |
|
|
393 | return; |
|
|
394 | |
|
|
395 | Display *dpy = s->display->dpy; |
|
|
396 | |
|
|
397 | rxvt_img *img = new rxvt_img (s, find_alpha_format_for (dpy, format), x, y, w, h, repeat); |
|
|
398 | img->alloc (); |
|
|
399 | |
|
|
400 | Picture src = picture (); |
|
|
401 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
|
|
402 | |
|
|
403 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, 0, 0, w, h); |
|
|
404 | |
|
|
405 | XRenderFreePicture (dpy, src); |
|
|
406 | XRenderFreePicture (dpy, dst); |
|
|
407 | |
|
|
408 | ::swap (img->ref, ref); |
|
|
409 | ::swap (img->pm , pm ); |
|
|
410 | |
|
|
411 | delete img; |
|
|
412 | } |
|
|
413 | |
241 | static void |
414 | static void |
242 | get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params) |
415 | get_gaussian_kernel (int radius, int width, nv *kernel, XFixed *params) |
243 | { |
416 | { |
244 | double sigma = radius / 2.0; |
417 | nv sigma = radius / 2.0; |
245 | double scale = sqrt (2.0 * M_PI) * sigma; |
418 | nv scale = sqrt (2.0 * M_PI) * sigma; |
246 | double sum = 0.0; |
419 | nv sum = 0.0; |
247 | |
420 | |
248 | for (int i = 0; i < width; i++) |
421 | for (int i = 0; i < width; i++) |
249 | { |
422 | { |
250 | double x = i - width / 2; |
423 | nv x = i - width / 2; |
251 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
424 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
252 | sum += kernel[i]; |
425 | sum += kernel[i]; |
253 | } |
426 | } |
254 | |
427 | |
255 | params[0] = XDoubleToFixed (width); |
428 | params[0] = XDoubleToFixed (width); |
… | |
… | |
265 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
438 | if (!(s->display->flags & DISPLAY_HAS_RENDER_CONV)) |
266 | return clone (); |
439 | return clone (); |
267 | |
440 | |
268 | Display *dpy = s->display->dpy; |
441 | Display *dpy = s->display->dpy; |
269 | int size = max (rh, rv) * 2 + 1; |
442 | int size = max (rh, rv) * 2 + 1; |
270 | double *kernel = (double *)malloc (size * sizeof (double)); |
443 | nv *kernel = (nv *)malloc (size * sizeof (nv)); |
271 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
444 | XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed)); |
272 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
445 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
273 | img->alloc (); |
446 | img->alloc (); |
274 | |
447 | |
275 | XRenderPictureAttributes pa; |
448 | XRenderPictureAttributes pa; |
… | |
… | |
397 | if (r < 0 || g < 0 || b < 0 || a < 0) |
570 | if (r < 0 || g < 0 || b < 0 || a < 0) |
398 | rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); |
571 | rxvt_fatal ("rxvt_img::contrast does not support negative values.\n"); |
399 | |
572 | |
400 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
573 | rxvt_img *img = new rxvt_img (s, format, x, y, w, h, repeat); |
401 | img->alloc (); |
574 | img->alloc (); |
402 | |
575 | img->fill (rgba (0, 0, 0, 0)); |
403 | { |
|
|
404 | rxvt_color empty; |
|
|
405 | empty.set (s, rgba (0, 0, 0, 0)); |
|
|
406 | img->fill (empty); |
|
|
407 | } |
|
|
408 | |
576 | |
409 | // premultiply (yeah, these are not exact, sue me or fix it) |
577 | // premultiply (yeah, these are not exact, sue me or fix it) |
410 | r = (r * (a >> 8)) >> 8; |
578 | r = (r * (a >> 8)) >> 8; |
411 | g = (g * (a >> 8)) >> 8; |
579 | g = (g * (a >> 8)) >> 8; |
412 | b = (b * (a >> 8)) >> 8; |
580 | b = (b * (a >> 8)) >> 8; |
413 | |
581 | |
414 | Display *dpy = s->display->dpy; |
582 | Display *dpy = s->display->dpy; |
415 | |
583 | |
416 | Picture src = src_picture (); |
584 | Picture src = picture (); |
417 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
585 | Picture dst = XRenderCreatePicture (dpy, img->pm, format, 0, 0); |
418 | Picture mul = create_xrender_mask (dpy, pm, True, True); |
586 | Picture mul = create_xrender_mask (dpy, pm, True, True); |
419 | |
587 | |
420 | //TODO: this operator does not yet implement some useful contrast |
588 | //TODO: this operator does not yet implement some useful contrast |
421 | while (r | g | b | a) |
589 | while (r | g | b | a) |
… | |
… | |
438 | ::swap (img->pm , pm ); |
606 | ::swap (img->pm , pm ); |
439 | |
607 | |
440 | delete img; |
608 | delete img; |
441 | } |
609 | } |
442 | |
610 | |
|
|
611 | void |
|
|
612 | rxvt_img::draw (rxvt_img *img, int op, nv mask) |
|
|
613 | { |
|
|
614 | unshare (); |
|
|
615 | |
|
|
616 | Display *dpy = s->display->dpy; |
|
|
617 | Picture src = img->picture (); |
|
|
618 | Picture dst = picture (); |
|
|
619 | Picture mask_p = 0; |
|
|
620 | |
|
|
621 | if (mask != 1.) |
|
|
622 | { |
|
|
623 | mask_p = create_xrender_mask (dpy, img->pm, False, False); |
|
|
624 | XRenderColor mask_c = { 0, 0, 0, float_to_component (mask) }; |
|
|
625 | XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1); |
|
|
626 | } |
|
|
627 | |
|
|
628 | XRenderComposite (dpy, op, src, mask_p, dst, x - img->x, y - img->y, 0, 0, 0, 0, w, h); |
|
|
629 | |
|
|
630 | XRenderFreePicture (dpy, src); |
|
|
631 | XRenderFreePicture (dpy, dst); |
|
|
632 | |
|
|
633 | if (mask_p) |
|
|
634 | XRenderFreePicture (dpy, mask_p); |
|
|
635 | } |
|
|
636 | |
443 | rxvt_img * |
637 | rxvt_img * |
444 | rxvt_img::clone () |
638 | rxvt_img::clone () |
445 | { |
639 | { |
446 | return new rxvt_img (*this); |
640 | return new rxvt_img (*this); |
447 | } |
|
|
448 | |
|
|
449 | static XRenderPictFormat * |
|
|
450 | find_alpha_format_for (Display *dpy, XRenderPictFormat *format) |
|
|
451 | { |
|
|
452 | if (format->direct.alphaMask) |
|
|
453 | return format; // already has alpha |
|
|
454 | |
|
|
455 | // try to find a suitable alpha format, one bit alpha is enough for our purposes |
|
|
456 | if (format->type == PictTypeDirect) |
|
|
457 | for (int n = 0; XRenderPictFormat *f = XRenderFindFormat (dpy, 0, 0, n); ++n) |
|
|
458 | if (f->direct.alphaMask |
|
|
459 | && f->type == PictTypeDirect |
|
|
460 | && ecb_popcount32 (f->direct.redMask ) >= ecb_popcount32 (format->direct.redMask ) |
|
|
461 | && ecb_popcount32 (f->direct.greenMask) >= ecb_popcount32 (format->direct.greenMask) |
|
|
462 | && ecb_popcount32 (f->direct.blueMask ) >= ecb_popcount32 (format->direct.blueMask )) |
|
|
463 | return f; |
|
|
464 | |
|
|
465 | // should be a very good fallback |
|
|
466 | return XRenderFindStandardFormat (dpy, PictStandardARGB32); |
|
|
467 | } |
641 | } |
468 | |
642 | |
469 | rxvt_img * |
643 | rxvt_img * |
470 | rxvt_img::reify () |
644 | rxvt_img::reify () |
471 | { |
645 | { |
… | |
… | |
480 | && repeat == RepeatNone; // and we have no good pixels to fill with |
654 | && repeat == RepeatNone; // and we have no good pixels to fill with |
481 | |
655 | |
482 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
656 | rxvt_img *img = new rxvt_img (s, alpha ? find_alpha_format_for (dpy, format) : format, 0, 0, w, h, repeat); |
483 | img->alloc (); |
657 | img->alloc (); |
484 | |
658 | |
485 | Picture src = src_picture (); |
659 | Picture src = picture (); |
486 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
660 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
487 | |
661 | |
488 | if (alpha) |
662 | if (alpha) |
489 | { |
663 | { |
490 | XRenderColor rc = { 0, 0, 0, 0 }; |
664 | XRenderColor rc = { 0, 0, 0, 0 }; |
491 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
665 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h);//TODO: split into four fillrectangles |
492 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, -x, -y, ref->w, ref->h); |
666 | XRenderComposite (dpy, PictOpSrc, src, None, dst, 0, 0, 0, 0, x, y, ref->w, ref->h); |
493 | } |
667 | } |
494 | else |
668 | else |
495 | XRenderComposite (dpy, PictOpSrc, src, None, dst, x, y, 0, 0, 0, 0, w, h); |
669 | XRenderComposite (dpy, PictOpSrc, src, None, dst, -x, -y, 0, 0, 0, 0, w, h); |
496 | |
670 | |
497 | XRenderFreePicture (dpy, src); |
671 | XRenderFreePicture (dpy, src); |
498 | XRenderFreePicture (dpy, dst); |
672 | XRenderFreePicture (dpy, dst); |
499 | |
673 | |
500 | return img; |
674 | return img; |
… | |
… | |
503 | rxvt_img * |
677 | rxvt_img * |
504 | rxvt_img::sub_rect (int x, int y, int width, int height) |
678 | rxvt_img::sub_rect (int x, int y, int width, int height) |
505 | { |
679 | { |
506 | rxvt_img *img = clone (); |
680 | rxvt_img *img = clone (); |
507 | |
681 | |
508 | img->x += x; |
682 | img->x -= x; |
509 | img->y += y; |
683 | img->y -= y; |
510 | |
684 | |
511 | if (w != width || h != height) |
685 | if (w != width || h != height) |
512 | { |
686 | { |
513 | img->w = width; |
687 | img->w = width; |
514 | img->h = height; |
688 | img->h = height; |
… | |
… | |
519 | } |
693 | } |
520 | |
694 | |
521 | return img; |
695 | return img; |
522 | } |
696 | } |
523 | |
697 | |
524 | static void |
|
|
525 | mat_invert (double mat[3][3], double (&inv)[3][3]) |
|
|
526 | { |
|
|
527 | double s0 = mat [2][2] * mat [1][1] - mat [2][1] * mat [1][2]; |
|
|
528 | double s1 = mat [2][1] * mat [0][2] - mat [2][2] * mat [0][1]; |
|
|
529 | double s2 = mat [1][2] * mat [0][1] - mat [1][1] * mat [0][2]; |
|
|
530 | |
|
|
531 | double invdet = 1. / (mat [0][0] * s0 + mat [1][0] * s1 + mat [2][0] * s2); |
|
|
532 | |
|
|
533 | inv [0][0] = invdet * s0; |
|
|
534 | inv [0][1] = invdet * s1; |
|
|
535 | inv [0][2] = invdet * s2; |
|
|
536 | |
|
|
537 | inv [1][0] = invdet * (mat [2][0] * mat [1][2] - mat [2][2] * mat [1][0]); |
|
|
538 | inv [1][1] = invdet * (mat [2][2] * mat [0][0] - mat [2][0] * mat [0][2]); |
|
|
539 | inv [1][2] = invdet * (mat [1][0] * mat [0][2] - mat [1][2] * mat [0][0]); |
|
|
540 | |
|
|
541 | inv [2][0] = invdet * (mat [2][1] * mat [1][0] - mat [2][0] * mat [1][1]); |
|
|
542 | inv [2][1] = invdet * (mat [2][0] * mat [0][1] - mat [2][1] * mat [0][0]); |
|
|
543 | inv [2][2] = invdet * (mat [1][1] * mat [0][0] - mat [1][0] * mat [0][1]); |
|
|
544 | } |
|
|
545 | |
|
|
546 | static double |
|
|
547 | mat_apply (double mat[3][3], int i, double x, double y) |
|
|
548 | { |
|
|
549 | double v = mat [i][0] * x + mat [i][1] * y + mat [i][2]; |
|
|
550 | double w = mat [2][0] * x + mat [2][1] * y + mat [2][2]; |
|
|
551 | |
|
|
552 | return v * (1. / w); |
|
|
553 | } |
|
|
554 | |
|
|
555 | rxvt_img * |
698 | rxvt_img * |
556 | rxvt_img::transform (double matrix[3][3]) |
699 | rxvt_img::transform (nv matrix[3][3]) |
557 | { |
700 | { |
558 | // find new offset |
|
|
559 | int ox = mat_apply (matrix, 0, x, y); |
|
|
560 | int oy = mat_apply (matrix, 1, x, y); |
|
|
561 | |
|
|
562 | // calculate new pixel bounding box coordinates |
701 | // calculate new pixel bounding box coordinates |
563 | double rmin[2], rmax[2]; |
702 | nv r[2], rmin[2], rmax[2]; |
|
|
703 | |
|
|
704 | mat3x3 m (matrix); |
564 | |
705 | |
565 | for (int i = 0; i < 2; ++i) |
706 | for (int i = 0; i < 2; ++i) |
566 | { |
707 | { |
567 | double v; |
708 | nv v; |
|
|
709 | |
568 | v = mat_apply (matrix, i, 0, 0); rmin [i] = rmax [i] = v; |
710 | v = m.apply1 (i, 0+x, 0+y); rmin [i] = rmax [i] = v; r [i] = v; |
569 | v = mat_apply (matrix, i, w, 0); min_it (rmin [i], v); max_it (rmax [i], v); |
711 | v = m.apply1 (i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v); |
570 | v = mat_apply (matrix, i, 0, h); min_it (rmin [i], v); max_it (rmax [i], v); |
712 | v = m.apply1 (i, 0+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
571 | v = mat_apply (matrix, i, w, h); min_it (rmin [i], v); max_it (rmax [i], v); |
713 | v = m.apply1 (i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
572 | } |
714 | } |
573 | |
715 | |
|
|
716 | float sx = rmin [0] - x; |
|
|
717 | float sy = rmin [1] - y; |
|
|
718 | |
|
|
719 | // TODO: adjust matrix for subpixel accuracy |
574 | int dx = floor (rmin [0]); |
720 | int nx = floor (rmin [0]); |
575 | int dy = floor (rmin [1]); |
721 | int ny = floor (rmin [1]); |
576 | |
722 | |
577 | int new_width = ceil (rmax [0] - dx); |
723 | int new_width = ceil (rmax [0] - rmin [0]); |
578 | int new_height = ceil (rmax [1] - dy); |
724 | int new_height = ceil (rmax [1] - rmin [1]); |
579 | |
725 | |
580 | double inv[3][3]; |
726 | m = mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y); |
581 | mat_invert (matrix, inv); |
|
|
582 | |
727 | |
|
|
728 | mat3x3 inv = m.invert (); |
|
|
729 | |
583 | rxvt_img *img = new rxvt_img (s, format, ox - dx, oy - dy, new_width, new_height, repeat); |
730 | rxvt_img *img = new rxvt_img (s, format, nx, ny, new_width, new_height, repeat); |
584 | img->alloc (); |
731 | img->alloc (); |
585 | |
732 | |
586 | Display *dpy = s->display->dpy; |
733 | Display *dpy = s->display->dpy; |
587 | Picture src = src_picture (); |
734 | Picture src = picture (); |
588 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
735 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
589 | |
736 | |
590 | XTransform xfrm; |
737 | XTransform xfrm; |
591 | |
738 | |
592 | for (int i = 0; i < 3; ++i) |
739 | for (int i = 0; i < 3; ++i) |
593 | for (int j = 0; j < 3; ++j) |
740 | for (int j = 0; j < 3; ++j) |
594 | xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); |
741 | xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); |
595 | |
742 | |
596 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
743 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
597 | XRenderSetPictureTransform (dpy, src, &xfrm); |
744 | XRenderSetPictureTransform (dpy, src, &xfrm); |
598 | XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height); |
745 | XRenderComposite (dpy, PictOpSrc, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height); |
|
|
746 | #if 1 |
|
|
747 | { |
|
|
748 | XRenderColor rc = { 65535,0,0,65535 }; |
|
|
749 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, new_width, new_height); |
|
|
750 | }{ |
|
|
751 | XRenderColor rc = { 0,0,0,65535 }; |
|
|
752 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 1, 1, new_width - 2, new_height - 2); |
|
|
753 | } |
|
|
754 | XRenderComposite (dpy, PictOpOver, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height); |
|
|
755 | #endif |
599 | |
756 | |
600 | XRenderFreePicture (dpy, src); |
757 | XRenderFreePicture (dpy, src); |
601 | XRenderFreePicture (dpy, dst); |
758 | XRenderFreePicture (dpy, dst); |
602 | |
759 | |
603 | return img; |
760 | return img; |
… | |
… | |
607 | rxvt_img::scale (int new_width, int new_height) |
764 | rxvt_img::scale (int new_width, int new_height) |
608 | { |
765 | { |
609 | if (w == new_width && h == new_height) |
766 | if (w == new_width && h == new_height) |
610 | return clone (); |
767 | return clone (); |
611 | |
768 | |
612 | double matrix[3][3] = { |
769 | nv matrix[3][3] = { |
613 | { new_width / (double)w, 0, 0 }, |
770 | { new_width / (nv)w, 0, 0 }, |
614 | { 0, new_height / (double)h, 0 }, |
771 | { 0, new_height / (nv)h, 0 }, |
615 | { 0, 0, 1 } |
772 | { 0, 0, 1 } |
616 | }; |
773 | }; |
617 | |
774 | |
618 | int old_repeat_mode = repeat; |
775 | int old_repeat_mode = repeat; |
619 | repeat = RepeatPad; // not right, but xrender can't properly scale it seems |
776 | repeat = RepeatPad; // not right, but xrender can't properly scale it seems |
620 | |
777 | |
… | |
… | |
625 | |
782 | |
626 | return img; |
783 | return img; |
627 | } |
784 | } |
628 | |
785 | |
629 | rxvt_img * |
786 | rxvt_img * |
630 | rxvt_img::rotate (int cx, int cy, double phi) |
787 | rxvt_img::rotate (int cx, int cy, nv phi) |
631 | { |
788 | { |
632 | double s = sin (phi); |
789 | nv s = sin (phi); |
633 | double c = cos (phi); |
790 | nv c = cos (phi); |
634 | |
791 | |
635 | double matrix[3][3] = { |
792 | nv matrix[3][3] = { |
|
|
793 | #if 0 |
636 | //{ c, -s, cx - c * cx + s * cy }, |
794 | { c, -s, cx - c * cx + s * cy }, |
637 | //{ s, c, cy - s * cx - c * cy }, |
795 | { s, c, cy - s * cx - c * cy }, |
638 | //{ 0, 0, 1 } |
796 | { 0, 0, 1 } |
|
|
797 | #else |
639 | { c, -s, 0 }, |
798 | { c, -s, 0 }, |
640 | { s, c, 0 }, |
799 | { s, c, 0 }, |
641 | { 0, 0, 1 } |
800 | { 0, 0, 1 } |
|
|
801 | #endif |
642 | }; |
802 | }; |
643 | |
803 | |
644 | move (-cx, -cy); |
804 | move (-cx, -cy); |
645 | rxvt_img *img = transform (matrix); |
805 | rxvt_img *img = transform (matrix); |
646 | move ( cx, cy); |
806 | move ( cx, cy); |
… | |
… | |
648 | |
808 | |
649 | return img; |
809 | return img; |
650 | } |
810 | } |
651 | |
811 | |
652 | rxvt_img * |
812 | rxvt_img * |
653 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rxvt_color &bg) |
813 | rxvt_img::convert_format (XRenderPictFormat *new_format, const rgba &bg) |
654 | { |
814 | { |
655 | if (new_format == format) |
815 | if (new_format == format) |
656 | return clone (); |
816 | return clone (); |
657 | |
817 | |
658 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
818 | rxvt_img *img = new rxvt_img (s, new_format, x, y, w, h, repeat); |
659 | img->alloc (); |
819 | img->alloc (); |
660 | |
820 | |
661 | Display *dpy = s->display->dpy; |
821 | Display *dpy = s->display->dpy; |
662 | Picture src = src_picture (); |
822 | Picture src = picture (); |
663 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
823 | Picture dst = XRenderCreatePicture (dpy, img->pm, new_format, 0, 0); |
664 | int op = PictOpSrc; |
824 | int op = PictOpSrc; |
665 | |
825 | |
666 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
826 | if (format->direct.alphaMask && !new_format->direct.alphaMask) |
667 | { |
827 | { |
668 | // does it have to be that complicated |
828 | // does it have to be that complicated |
669 | rgba c; |
|
|
670 | bg.get (c); |
|
|
671 | |
|
|
672 | XRenderColor rc = { c.r, c.g, c.b, 0xffff }; |
829 | XRenderColor rc = { bg.r, bg.g, bg.b, bg.a }; |
673 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
830 | XRenderFillRectangle (dpy, PictOpSrc, dst, &rc, 0, 0, w, h); |
674 | |
831 | |
675 | op = PictOpOver; |
832 | op = PictOpOver; |
676 | } |
833 | } |
677 | |
834 | |
… | |
… | |
682 | |
839 | |
683 | return img; |
840 | return img; |
684 | } |
841 | } |
685 | |
842 | |
686 | rxvt_img * |
843 | rxvt_img * |
687 | rxvt_img::blend (rxvt_img *img, double factor) |
844 | rxvt_img::blend (rxvt_img *img, nv factor) |
688 | { |
845 | { |
689 | rxvt_img *img2 = clone (); |
846 | rxvt_img *img2 = clone (); |
690 | Display *dpy = s->display->dpy; |
847 | Display *dpy = s->display->dpy; |
691 | Picture src = img->src_picture (); |
848 | Picture src = img->picture (); |
692 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
849 | Picture dst = XRenderCreatePicture (dpy, img2->pm, img2->format, 0, 0); |
693 | Picture mask = create_xrender_mask (dpy, img->pm, False, False); |
850 | Picture mask = create_xrender_mask (dpy, img->pm, False, False); |
694 | |
851 | |
695 | XRenderColor mask_c; |
852 | XRenderColor mask_c; |
696 | |
853 | |