1 //
2 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 //   Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 
19 //
20 //
21 // Original author: Thatcher Ulrich <tu@tulrich.com> 2003
22 //
23 //
24 
25 #ifndef GNASH_MATRIX_H
26 #define GNASH_MATRIX_H
27 
28 #include "dsodefs.h" // for DSOEXPORT
29 
30 #include <iosfwd>
31 #include <cstdint>
32 
33 // Forward declarations
34 namespace gnash {
35     class SWFRect;
36     namespace geometry {
37         class Point2d;
38         template<typename T> class Range2d;
39     }
40 }
41 
42 
43 namespace gnash {
44 
45 /// The SWF SWFMatrix record.
46 ///
47 /// Conceptually, it represents a 3*3 linear transformation SWFMatrix like this:
48 ///
49 ///   | scale_x       rotateSkew_y  translate_x |
50 ///   | rotateSkey_x  scale_y       traslate_y  |
51 ///   | 0             0             1           |
52 ///
53 class DSOEXPORT SWFMatrix
54 {
55 public:
56 
57     /// Construct an identity SWFMatrix
SWFMatrix()58     constexpr SWFMatrix()
59         :
60         _a(65536),
61         _b(0),
62         _c(0),
63         _d(65536),
64         _tx(0),
65         _ty(0)
66     {}
67 
68     /// Construct a SWFMatrix with all values.
SWFMatrix(int a,int b,int c,int d,int x,int y)69     SWFMatrix(int a, int b, int c, int d, int x, int y)
70         :
71         _a(a),
72         _b(b),
73         _c(c),
74         _d(d),
75         _tx(x),
76         _ty(y)
77     {}
78 
a()79     std::int32_t a() const {
80         return _a;
81     }
82 
b()83     std::int32_t b() const {
84         return _b;
85     }
86 
c()87     std::int32_t c() const {
88         return _c;
89     }
90 
d()91     std::int32_t d() const {
92         return _d;
93     }
94 
tx()95     std::int32_t tx() const {
96         return _tx;
97     }
98 
ty()99     std::int32_t ty() const {
100         return _ty;
101     }
102 
103     /// Set the SWFMatrix to identity.
104     void set_identity();
105 
106     /// Concatenate m's transform onto ours.
107     //
108     /// When transforming points, m happens first,
109     /// then our original xform.
110     void concatenate(const SWFMatrix& m);
111 
112     /// Concatenate a translation onto the front of our SWFMatrix.
113     //
114     /// When transforming points, the translation
115     /// happens first, then our original xform.
116     void concatenate_translation(int _tx, int _ty);
117 
118     /// Concatenate scale x and y to the front of our SWFMatrix
119     //
120     /// When transforming points, these scales happen first, then
121     /// our original SWFMatrix.
122     void concatenate_scale(double x, double y);
123 
124     /// Set this SWFMatrix to a blend of m1 and m2, parameterized by t.
125     void set_lerp(const SWFMatrix& m1, const SWFMatrix& m2, float t);
126 
127     /// Set the scale & rotation part of the SWFMatrix. angle in radians.
128     void set_scale_rotation(double x_scale, double y_scale, double rotation);
129 
130     /// Set x and y scales, rotation is unchanged.
131     void set_scale(double x_scale, double y_scale);
132 
133     /// Set x scale, rotation any y scale are unchanged.
134     void set_x_scale(double scale);
135 
136     /// Set y scale, rotation and x scale are unchanged.
137     void set_y_scale(double scale);
138 
139     /// Set rotation in radians, scales component are unchanged.
140     void set_rotation(double rotation);
141 
142     /// Set x translation in TWIPS
set_x_translation(int x)143     void set_x_translation(int x) {
144         _tx = x;
145     }
146 
147     /// Set y translation in TWIPS.
set_y_translation(int y)148     void set_y_translation(int y) {
149         _ty = y;
150     }
151 
152     /// Set x and y translation in TWIPS.
set_translation(int x,int y)153     void set_translation(int x, int y) {
154         _tx = x;
155         _ty = y;
156     }
157 
158     /// Transform a given point by our SWFMatrix
159     void transform(geometry::Point2d& p) const;
160 
161     /// Transform the given point by our SWFMatrix.
162     void transform(std::int32_t& x, std::int32_t& y) const;
163 
164     /// Transform point 'p' by our SWFMatrix.
165     //
166     /// Put the result in *result.
167     ///
168     void transform(geometry::Point2d* result, const geometry::Point2d& p) const;
169 
170     /// Transform Range2d<float> 'r' by our SWFMatrix.
171     //
172     /// NULL and WORLD ranges are untouched.
173     ///
174     void transform(geometry::Range2d<std::int32_t>& r) const;
175 
176     void transform(SWFRect& r) const;
177 
178     /// Invert this SWFMatrix and return the result.
179     SWFMatrix& invert();
180 
181     /// return the magnitude scale of our x coord output
182     double get_x_scale() const;
183 
184     /// return the magnitude scale of our y coord output
185     double get_y_scale() const;
186 
187     /// return rotation component in radians.
188     double get_rotation() const;
189 
190     /// return x translation n TWIPS unit.
get_x_translation()191     int get_x_translation() const {
192         return _tx;
193     }
194 
195     /// return y translation in TWIPS unit.
get_y_translation()196     int get_y_translation() const {
197         return _ty;
198     }
199 
200     /// Allow direct access to values for equality
201     friend bool operator==(const SWFMatrix& a, const SWFMatrix& b);
202 
203 private:
204 
205     /// Return the determinant of this SWFMatrix in 32.32 fixed point format.
206     std::int64_t  determinant() const;
207 
208     /// Xscale, 16.16 fixed point. xx in swfdec. 'a' in AS Matrix.
209     std::int32_t _a;
210 
211     /// Xshear, 16.16 fixed point. yx in swfdec. 'b' in AS Matrix.
212     std::int32_t _b;
213 
214     /// Yshear, 16.16 fixed point. xy in swfdec. 'c' in AS Matrix.
215     std::int32_t _c;
216 
217     /// Yscale, 16.16 fixed point. yy in swfdec. 'd' in AS Matrix.
218     std::int32_t _d;
219 
220     /// Xtranslation, TWIPS. x0 in swfdec. '_tx' in AS Matrix.
221     std::int32_t _tx;
222 
223     /// Ytranslation, TWIPS. y0 in swfdec. '_ty' in AS Matrix.
224     std::int32_t _ty;
225 
226 
227 }; //end of SWFMatrix
228 
229 inline bool
230 operator==(const SWFMatrix& a, const SWFMatrix& b)
231 {
232     return
233         a.a()  == b._a  &&
234         a._b == b._b &&
235         a._tx  == b._tx  &&
236         a._d  == b._d  &&
237         a._c == b._c &&
238         a._ty  == b._ty;
239 }
240 
241 DSOTEXPORT std::ostream& operator<<(std::ostream& o, const SWFMatrix& m);
242 
243 } // namespace gnash
244 
245 #endif
246 
247 
248 // Local Variables:
249 // mode: C++
250 // indent-tabs-mode: t
251 // End:
252 //
253