|
|
1 | /*----------------------------------------------------------------------* |
|
|
2 | * File: rxvtimg.h |
|
|
3 | *----------------------------------------------------------------------* |
|
|
4 | * |
|
|
5 | * All portions of code are copyright by their respective author/s. |
|
|
6 | * Copyright (c) 2012 Marc Lehmann <schmorp@schmorp.de> |
|
|
7 | * Copyright (c) 2012 Emanuele Giaquinta <e.giaquinta@glauco.it> |
|
|
8 | * |
|
|
9 | * This program is free software; you can redistribute it and/or modify |
|
|
10 | * it under the terms of the GNU General Public License as published by |
|
|
11 | * the Free Software Foundation; either version 2 of the License, or |
|
|
12 | * (at your option) any later version. |
|
|
13 | * |
|
|
14 | * This program is distributed in the hope that it will be useful, |
|
|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
17 | * GNU General Public License for more details. |
|
|
18 | * |
|
|
19 | * You should have received a copy of the GNU General Public License |
|
|
20 | * along with this program; if not, write to the Free Software |
|
|
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
22 | *---------------------------------------------------------------------*/ |
|
|
23 | |
1 | #include <string.h> |
24 | #include <string.h> |
2 | #include <math.h> |
25 | #include <math.h> |
3 | #include "../config.h" |
26 | #include "../config.h" |
4 | #include "rxvt.h" |
27 | #include "rxvt.h" |
5 | |
28 | |
… | |
… | |
16 | |
39 | |
17 | mat3x3 () |
40 | mat3x3 () |
18 | { |
41 | { |
19 | } |
42 | } |
20 | |
43 | |
21 | mat3x3 (nv matrix[3][3]) |
44 | mat3x3 (const nv *matrix) |
22 | { |
45 | { |
23 | memcpy (v, matrix, sizeof (v)); |
46 | memcpy (v, matrix, sizeof (v)); |
24 | } |
47 | } |
25 | |
48 | |
26 | mat3x3 (nv v11, nv v12, nv v13, nv v21, nv v22, nv v23, nv v31, nv v32, nv v33) |
49 | mat3x3 (nv v11, nv v12, nv v13, nv v21, nv v22, nv v23, nv v31, nv v32, nv v33) |
… | |
… | |
33 | mat3x3 invert (); |
56 | mat3x3 invert (); |
34 | |
57 | |
35 | nv *operator [](int i) { return &v[i][0]; } |
58 | nv *operator [](int i) { return &v[i][0]; } |
36 | const nv *operator [](int i) const { return &v[i][0]; } |
59 | const nv *operator [](int i) const { return &v[i][0]; } |
37 | |
60 | |
|
|
61 | operator const nv * () const { return &v[0][0]; } |
|
|
62 | operator nv * () { return &v[0][0]; } |
|
|
63 | |
38 | // quite inefficient, hopefully gcc pulls the w calc out of any loops |
64 | // quite inefficient, hopefully gcc pulls the w calc out of any loops |
39 | nv apply1 (int i, nv x, nv y) |
65 | nv apply1 (int i, nv x, nv y) |
40 | { |
66 | { |
41 | mat3x3 &m = *this; |
67 | mat3x3 &m = *this; |
42 | |
68 | |
… | |
… | |
45 | |
71 | |
46 | return v * (1. / w); |
72 | return v * (1. / w); |
47 | } |
73 | } |
48 | |
74 | |
49 | static mat3x3 translate (nv x, nv y); |
75 | static mat3x3 translate (nv x, nv y); |
|
|
76 | static mat3x3 scale (nv s, nv t); |
|
|
77 | static mat3x3 rotate (nv phi); |
50 | }; |
78 | }; |
51 | |
79 | |
52 | mat3x3 |
80 | mat3x3 |
53 | mat3x3::invert () |
81 | mat3x3::invert () |
54 | { |
82 | { |
… | |
… | |
95 | { |
123 | { |
96 | return mat3x3 ( |
124 | return mat3x3 ( |
97 | 1, 0, x, |
125 | 1, 0, x, |
98 | 0, 1, y, |
126 | 0, 1, y, |
99 | 0, 0, 1 |
127 | 0, 0, 1 |
|
|
128 | ); |
|
|
129 | } |
|
|
130 | |
|
|
131 | mat3x3 |
|
|
132 | mat3x3::scale (nv s, nv t) |
|
|
133 | { |
|
|
134 | return mat3x3 ( |
|
|
135 | s, 0, 0, |
|
|
136 | 0, t, 0, |
|
|
137 | 0, 0, 1 |
|
|
138 | ); |
|
|
139 | } |
|
|
140 | |
|
|
141 | // clockwise |
|
|
142 | mat3x3 |
|
|
143 | mat3x3::rotate (nv phi) |
|
|
144 | { |
|
|
145 | nv s = sin (phi); |
|
|
146 | nv c = cos (phi); |
|
|
147 | |
|
|
148 | return mat3x3 ( |
|
|
149 | c, -s, 0, |
|
|
150 | s, c, 0, |
|
|
151 | 0, 0, 1 |
100 | ); |
152 | ); |
101 | } |
153 | } |
102 | |
154 | |
103 | } |
155 | } |
104 | |
156 | |
… | |
… | |
694 | |
746 | |
695 | return img; |
747 | return img; |
696 | } |
748 | } |
697 | |
749 | |
698 | rxvt_img * |
750 | rxvt_img * |
699 | rxvt_img::transform (nv matrix[3][3]) |
751 | rxvt_img::transform (const nv matrix[3][3]) |
700 | { |
752 | { |
|
|
753 | return transform (mat3x3 (&matrix[0][0])); |
|
|
754 | } |
|
|
755 | |
|
|
756 | rxvt_img * |
|
|
757 | rxvt_img::transform (const nv *matrix) |
|
|
758 | { |
|
|
759 | mat3x3 m (matrix); |
|
|
760 | |
701 | // calculate new pixel bounding box coordinates |
761 | // calculate new pixel bounding box coordinates |
702 | nv r[2], rmin[2], rmax[2]; |
762 | nv r[2], rmin[2], rmax[2]; |
703 | |
|
|
704 | mat3x3 m (matrix); |
|
|
705 | |
763 | |
706 | for (int i = 0; i < 2; ++i) |
764 | for (int i = 0; i < 2; ++i) |
707 | { |
765 | { |
708 | nv v; |
766 | nv v; |
709 | |
767 | |
… | |
… | |
754 | rxvt_img::scale (int new_width, int new_height) |
812 | rxvt_img::scale (int new_width, int new_height) |
755 | { |
813 | { |
756 | if (w == new_width && h == new_height) |
814 | if (w == new_width && h == new_height) |
757 | return clone (); |
815 | return clone (); |
758 | |
816 | |
759 | nv matrix[3][3] = { |
|
|
760 | { new_width / (nv)w, 0, 0 }, |
|
|
761 | { 0, new_height / (nv)h, 0 }, |
|
|
762 | { 0, 0, 1 } |
|
|
763 | }; |
|
|
764 | |
|
|
765 | int old_repeat_mode = repeat; |
817 | int old_repeat_mode = repeat; |
766 | repeat = RepeatPad; // not right, but xrender can't properly scale it seems |
818 | repeat = RepeatPad; // not right, but xrender can't properly scale it seems |
767 | |
819 | |
768 | rxvt_img *img = transform (matrix); |
820 | rxvt_img *img = transform (mat3x3::scale (new_width / (nv)w, new_height / (nv)h)); |
769 | |
821 | |
770 | repeat = old_repeat_mode; |
822 | repeat = old_repeat_mode; |
771 | img->repeat = repeat; |
823 | img->repeat = repeat; |
772 | |
824 | |
773 | return img; |
825 | return img; |
774 | } |
826 | } |
775 | |
827 | |
776 | rxvt_img * |
828 | rxvt_img * |
777 | rxvt_img::rotate (int cx, int cy, nv phi) |
829 | rxvt_img::rotate (int cx, int cy, nv phi) |
778 | { |
830 | { |
779 | nv s = sin (phi); |
|
|
780 | nv c = cos (phi); |
|
|
781 | |
|
|
782 | nv matrix[3][3] = { |
|
|
783 | #if 0 |
831 | #if 0 |
784 | { c, -s, cx - c * cx + s * cy }, |
832 | { c, -s, cx - c * cx + s * cy }, |
785 | { s, c, cy - s * cx - c * cy }, |
833 | { s, c, cy - s * cx - c * cy }, |
786 | { 0, 0, 1 } |
834 | { 0, 0, 1 } |
787 | #else |
|
|
788 | { c, -s, 0 }, |
|
|
789 | { s, c, 0 }, |
|
|
790 | { 0, 0, 1 } |
|
|
791 | #endif |
835 | #endif |
792 | }; |
|
|
793 | |
836 | |
794 | move (-cx, -cy); |
837 | move (-cx, -cy); |
795 | rxvt_img *img = transform (matrix); |
838 | rxvt_img *img = transform (mat3x3::rotate (phi)); |
796 | move ( cx, cy); |
839 | move ( cx, cy); |
797 | img->move (cx, cy); |
840 | img->move (cx, cy); |
798 | |
841 | |
799 | return img; |
842 | return img; |
800 | } |
843 | } |