|
|
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 (const nv *matrix) |
|
|
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 | operator const nv * () const { return &v[0][0]; } |
|
|
39 | operator nv * () { return &v[0][0]; } |
|
|
40 | |
|
|
41 | // quite inefficient, hopefully gcc pulls the w calc out of any loops |
|
|
42 | nv apply1 (int i, nv x, nv y) |
|
|
43 | { |
|
|
44 | mat3x3 &m = *this; |
|
|
45 | |
|
|
46 | nv v = m[i][0] * x + m[i][1] * y + m[i][2]; |
|
|
47 | nv w = m[2][0] * x + m[2][1] * y + m[2][2]; |
|
|
48 | |
|
|
49 | return v * (1. / w); |
|
|
50 | } |
|
|
51 | |
|
|
52 | static mat3x3 translate (nv x, nv y); |
|
|
53 | static mat3x3 scale (nv s, nv t); |
|
|
54 | static mat3x3 rotate (nv phi); |
|
|
55 | }; |
|
|
56 | |
|
|
57 | mat3x3 |
|
|
58 | mat3x3::invert () |
|
|
59 | { |
|
|
60 | mat3x3 &m = *this; |
|
|
61 | mat3x3 inv; |
|
|
62 | |
|
|
63 | nv s0 = m[2][2] * m[1][1] - m[2][1] * m[1][2]; |
|
|
64 | nv s1 = m[2][1] * m[0][2] - m[2][2] * m[0][1]; |
|
|
65 | nv s2 = m[1][2] * m[0][1] - m[1][1] * m[0][2]; |
|
|
66 | |
|
|
67 | nv invdet = 1. / (m[0][0] * s0 + m[1][0] * s1 + m[2][0] * s2); |
|
|
68 | |
|
|
69 | inv[0][0] = invdet * s0; |
|
|
70 | inv[0][1] = invdet * s1; |
|
|
71 | inv[0][2] = invdet * s2; |
|
|
72 | |
|
|
73 | inv[1][0] = invdet * (m[2][0] * m[1][2] - m[2][2] * m[1][0]); |
|
|
74 | inv[1][1] = invdet * (m[2][2] * m[0][0] - m[2][0] * m[0][2]); |
|
|
75 | inv[1][2] = invdet * (m[1][0] * m[0][2] - m[1][2] * m[0][0]); |
|
|
76 | |
|
|
77 | inv[2][0] = invdet * (m[2][1] * m[1][0] - m[2][0] * m[1][1]); |
|
|
78 | inv[2][1] = invdet * (m[2][0] * m[0][1] - m[2][1] * m[0][0]); |
|
|
79 | inv[2][2] = invdet * (m[1][1] * m[0][0] - m[1][0] * m[0][1]); |
|
|
80 | |
|
|
81 | return inv; |
|
|
82 | } |
|
|
83 | |
|
|
84 | static mat3x3 |
|
|
85 | operator *(const mat3x3 &a, const mat3x3 &b) |
|
|
86 | { |
|
|
87 | mat3x3 r; |
|
|
88 | |
|
|
89 | for (int i = 0; i < 3; ++i) |
|
|
90 | for (int j = 0; j < 3; ++j) |
|
|
91 | r[i][j] = a[i][0] * b[0][j] |
|
|
92 | + a[i][1] * b[1][j] |
|
|
93 | + a[i][2] * b[2][j]; |
|
|
94 | |
|
|
95 | return r; |
|
|
96 | } |
|
|
97 | |
|
|
98 | mat3x3 |
|
|
99 | mat3x3::translate (nv x, nv y) |
|
|
100 | { |
|
|
101 | return mat3x3 ( |
|
|
102 | 1, 0, x, |
|
|
103 | 0, 1, y, |
|
|
104 | 0, 0, 1 |
|
|
105 | ); |
|
|
106 | } |
|
|
107 | |
|
|
108 | mat3x3 |
|
|
109 | mat3x3::scale (nv s, nv t) |
|
|
110 | { |
|
|
111 | return mat3x3 ( |
|
|
112 | s, 0, 0, |
|
|
113 | 0, t, 0, |
|
|
114 | 0, 0, 1 |
|
|
115 | ); |
|
|
116 | } |
|
|
117 | |
|
|
118 | // clockwise |
|
|
119 | mat3x3 |
|
|
120 | mat3x3::rotate (nv phi) |
|
|
121 | { |
|
|
122 | nv s = sin (phi); |
|
|
123 | nv c = cos (phi); |
|
|
124 | |
|
|
125 | return mat3x3 ( |
|
|
126 | c, -s, 0, |
|
|
127 | s, c, 0, |
|
|
128 | 0, 0, 1 |
|
|
129 | ); |
|
|
130 | } |
|
|
131 | |
|
|
132 | } |
6 | |
133 | |
7 | #if 0 |
134 | #if 0 |
8 | struct pict |
135 | struct pict |
9 | { |
136 | { |
10 | Display *dpy; |
137 | Display *dpy; |
… | |
… | |
312 | |
439 | |
313 | delete img; |
440 | delete img; |
314 | } |
441 | } |
315 | |
442 | |
316 | static void |
443 | static void |
317 | get_gaussian_kernel (int radius, int width, rxvt_img::nv *kernel, XFixed *params) |
444 | get_gaussian_kernel (int radius, int width, nv *kernel, XFixed *params) |
318 | { |
445 | { |
319 | rxvt_img::nv sigma = radius / 2.0; |
446 | nv sigma = radius / 2.0; |
320 | rxvt_img::nv scale = sqrt (2.0 * M_PI) * sigma; |
447 | nv scale = sqrt (2.0 * M_PI) * sigma; |
321 | rxvt_img::nv sum = 0.0; |
448 | nv sum = 0.0; |
322 | |
449 | |
323 | for (int i = 0; i < width; i++) |
450 | for (int i = 0; i < width; i++) |
324 | { |
451 | { |
325 | rxvt_img::nv x = i - width / 2; |
452 | nv x = i - width / 2; |
326 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
453 | kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale; |
327 | sum += kernel[i]; |
454 | sum += kernel[i]; |
328 | } |
455 | } |
329 | |
456 | |
330 | params[0] = XDoubleToFixed (width); |
457 | params[0] = XDoubleToFixed (width); |
… | |
… | |
595 | } |
722 | } |
596 | |
723 | |
597 | return img; |
724 | return img; |
598 | } |
725 | } |
599 | |
726 | |
600 | static void |
|
|
601 | mat_invert (rxvt_img::nv mat[3][3], rxvt_img::nv (&inv)[3][3]) |
|
|
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 | |
|
|
622 | static rxvt_img::nv |
|
|
623 | mat_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 | |
|
|
631 | rxvt_img * |
727 | rxvt_img * |
632 | rxvt_img::transform (nv matrix[3][3]) |
728 | rxvt_img::transform (const nv matrix[3][3]) |
633 | { |
729 | { |
|
|
730 | return transform (mat3x3 (&matrix[0][0])); |
|
|
731 | } |
|
|
732 | |
|
|
733 | rxvt_img * |
|
|
734 | rxvt_img::transform (const nv *matrix) |
|
|
735 | { |
|
|
736 | mat3x3 m (matrix); |
|
|
737 | |
634 | // calculate new pixel bounding box coordinates |
738 | // calculate new pixel bounding box coordinates |
635 | nv rmin[2], rmax[2]; |
739 | nv r[2], rmin[2], rmax[2]; |
636 | |
740 | |
637 | for (int i = 0; i < 2; ++i) |
741 | for (int i = 0; i < 2; ++i) |
638 | { |
742 | { |
639 | nv v; |
743 | nv v; |
640 | |
744 | |
641 | v = mat_apply (matrix, i, 0+x, 0+y); rmin [i] = rmax [i] = v; |
745 | v = m.apply1 (i, 0+x, 0+y); rmin [i] = rmax [i] = v; r [i] = v; |
642 | v = mat_apply (matrix, i, w+x, 0+y); min_it (rmin [i], v); max_it (rmax [i], v); |
746 | v = m.apply1 (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); |
747 | v = m.apply1 (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); |
748 | v = m.apply1 (i, w+x, h+y); min_it (rmin [i], v); max_it (rmax [i], v); |
645 | } |
749 | } |
|
|
750 | |
|
|
751 | float sx = rmin [0] - x; |
|
|
752 | float sy = rmin [1] - y; |
646 | |
753 | |
647 | // TODO: adjust matrix for subpixel accuracy |
754 | // TODO: adjust matrix for subpixel accuracy |
648 | int dx = floor (rmin [0]); |
755 | int nx = floor (rmin [0]); |
649 | int dy = floor (rmin [1]); |
756 | int ny = floor (rmin [1]); |
650 | |
757 | |
651 | int new_width = ceil (rmax [0] - dx); |
758 | int new_width = ceil (rmax [0] - rmin [0]); |
652 | int new_height = ceil (rmax [1] - dy); |
759 | int new_height = ceil (rmax [1] - rmin [1]); |
653 | |
760 | |
654 | nv inv[3][3]; |
761 | m = mat3x3::translate (-x, -y) * m * mat3x3::translate (x, y); |
655 | mat_invert (matrix, inv); |
|
|
656 | |
762 | |
|
|
763 | mat3x3 inv = m.invert (); |
|
|
764 | |
657 | rxvt_img *img = new rxvt_img (s, format, dx, dy, new_width, new_height, repeat); |
765 | rxvt_img *img = new rxvt_img (s, format, nx, ny, new_width, new_height, repeat); |
658 | img->alloc (); |
766 | img->alloc (); |
659 | |
767 | |
660 | Display *dpy = s->display->dpy; |
768 | Display *dpy = s->display->dpy; |
661 | Picture src = picture (); |
769 | Picture src = picture (); |
662 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
770 | Picture dst = XRenderCreatePicture (dpy, img->pm, img->format, 0, 0); |
… | |
… | |
667 | for (int j = 0; j < 3; ++j) |
775 | for (int j = 0; j < 3; ++j) |
668 | xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); |
776 | xfrm.matrix [i][j] = XDoubleToFixed (inv [i][j]); |
669 | |
777 | |
670 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
778 | XRenderSetPictureFilter (dpy, src, "good", 0, 0); |
671 | XRenderSetPictureTransform (dpy, src, &xfrm); |
779 | XRenderSetPictureTransform (dpy, src, &xfrm); |
672 | XRenderComposite (dpy, PictOpSrc, src, None, dst, dx, dy, 0, 0, 0, 0, new_width, new_height); |
780 | XRenderComposite (dpy, PictOpSrc, src, None, dst, sx, sy, 0, 0, 0, 0, new_width, new_height); |
673 | |
781 | |
674 | XRenderFreePicture (dpy, src); |
782 | XRenderFreePicture (dpy, src); |
675 | XRenderFreePicture (dpy, dst); |
783 | XRenderFreePicture (dpy, dst); |
676 | |
784 | |
677 | return img; |
785 | return img; |
… | |
… | |
681 | rxvt_img::scale (int new_width, int new_height) |
789 | rxvt_img::scale (int new_width, int new_height) |
682 | { |
790 | { |
683 | if (w == new_width && h == new_height) |
791 | if (w == new_width && h == new_height) |
684 | return clone (); |
792 | return clone (); |
685 | |
793 | |
686 | nv matrix[3][3] = { |
|
|
687 | { new_width / (nv)w, 0, 0 }, |
|
|
688 | { 0, new_height / (nv)h, 0 }, |
|
|
689 | { 0, 0, 1 } |
|
|
690 | }; |
|
|
691 | |
|
|
692 | int old_repeat_mode = repeat; |
794 | int old_repeat_mode = repeat; |
693 | repeat = RepeatPad; // not right, but xrender can't properly scale it seems |
795 | repeat = RepeatPad; // not right, but xrender can't properly scale it seems |
694 | |
796 | |
695 | rxvt_img *img = transform (matrix); |
797 | rxvt_img *img = transform (mat3x3::scale (new_width / (nv)w, new_height / (nv)h)); |
696 | |
798 | |
697 | repeat = old_repeat_mode; |
799 | repeat = old_repeat_mode; |
698 | img->repeat = repeat; |
800 | img->repeat = repeat; |
699 | |
801 | |
700 | return img; |
802 | return img; |
701 | } |
803 | } |
702 | |
804 | |
703 | rxvt_img * |
805 | rxvt_img * |
704 | rxvt_img::rotate (int cx, int cy, nv phi) |
806 | rxvt_img::rotate (int cx, int cy, nv phi) |
705 | { |
807 | { |
706 | nv s = sin (phi); |
808 | #if 0 |
707 | nv c = cos (phi); |
|
|
708 | |
|
|
709 | nv matrix[3][3] = { |
|
|
710 | { c, -s, cx - c * cx + s * cy + 200 }, |
809 | { c, -s, cx - c * cx + s * cy }, |
711 | { s, c, cy - s * cx - c * cy }, |
810 | { s, c, cy - s * cx - c * cy }, |
712 | { 0, 0, 1 } |
811 | { 0, 0, 1 } |
713 | //{ c, -s, 0 }, |
812 | #endif |
714 | //{ s, c, 0 }, |
|
|
715 | //{ 0, 0, 1 } |
|
|
716 | }; |
|
|
717 | |
813 | |
718 | //move (-cx, -cy); |
814 | move (-cx, -cy); |
719 | rxvt_img *img = transform (matrix); |
815 | rxvt_img *img = transform (mat3x3::rotate (phi)); |
720 | //move ( cx, cy); |
816 | move ( cx, cy); |
721 | //img->move (cx, cy); |
817 | img->move (cx, cy); |
722 | |
818 | |
723 | return img; |
819 | return img; |
724 | } |
820 | } |
725 | |
821 | |
726 | rxvt_img * |
822 | rxvt_img * |