2
// Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 3 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
// Original author: Thatcher Ulrich <tu@tulrich.com> 2003
24
#ifndef GNASH_MATRIX_H
25
#define GNASH_MATRIX_H
27
#include "dsodefs.h" // for DSOEXPORT
28
#include "Range2d.h" // for transforming Range2d<float>
29
#include "rect.h" // for rect
30
#include "Point2d.h" // for Point2d
31
#include "utility.h" // for TRUST_FLOAT_TO_UINT32_CONVERSION
34
#include <boost/cstdint.hpp>
36
// Forward declarations
44
/// The SWF matrix record.
46
/// Conceptually, it represents a 3*3 linear transformation matrix like this:
48
/// | scale_x rotateSkew_y translate_x |
49
/// | rotateSkey_x scale_y traslate_y |
52
class DSOEXPORT matrix
56
/// Xscale, 16.16 fixed point. xx in swfdec. 'a' in AS Matrix.
59
/// Xshear, 16.16 fixed point. yx in swfdec. 'b' in AS Matrix.
62
/// Xtranslation, TWIPS. x0 in swfdec. 'tx' in AS Matrix.
65
/// Yscale, 16.16 fixed point. yy in swfdec. 'd' in AS Matrix.
68
/// Yshear, 16.16 fixed point. xy in swfdec. 'c' in AS Matrix.
71
/// Ytranslation, TWIPS. y0 in swfdec. 'ty' in AS Matrix.
74
friend bool operator== (const matrix&, const matrix&);
75
friend std::ostream& operator<< (std::ostream&, const matrix&);
77
/// Defaults to identity
80
/// Check validity of the matrix values
81
bool is_valid() const;
83
/// Set the matrix to identity.
86
/// Concatenate m's transform onto ours.
88
/// When transforming points, m happens first,
89
/// then our original xform.
91
void concatenate(const matrix& m);
93
/// Concatenate a translation onto the front of our matrix.
95
/// When transforming points, the translation
96
/// happens first, then our original xform.
98
void concatenate_translation(int tx, int ty);
100
/// Concatenate scale x and y to the front of our matrix
102
/// When transforming points, these scales happen first, then
103
/// our original matrix.
105
void concatenate_scale(double x, double y);
107
/// Set this matrix to a blend of m1 and m2, parameterized by t.
108
void set_lerp(const matrix& m1, const matrix& m2, float t);
110
/// Set the scale & rotation part of the matrix. angle in radians.
111
void set_scale_rotation(double x_scale, double y_scale, double rotation);
113
/// Set x and y scales, rotation is unchanged.
114
void set_scale(double x_scale, double y_scale);
116
/// Set x scale, rotation any y scale are unchanged.
117
void set_x_scale(double scale);
119
/// Set y scale, rotation and x scale are unchanged.
120
void set_y_scale(double scale);
122
/// Set rotation in radians, scales component are unchanged.
123
void set_rotation(double rotation);
125
/// Set x translation in TWIPS
126
void set_x_translation(int x)
131
/// Set y translation in TWIPS.
132
void set_y_translation(int y)
137
/// Set x and y translation in TWIPS.
138
void set_translation(int x, int y)
144
/// Initialize from the SWF input stream.
145
void read(SWFStream& in);
147
/// Transform a given point by our matrix
148
void transform(geometry::Point2d& p) const
150
boost::int32_t t0 = Fixed16Mul(sx, p.x) + Fixed16Mul(shy, p.y) + tx;
151
boost::int32_t t1 = Fixed16Mul(shx,p.x) + Fixed16Mul(sy, p.y) + ty;
156
/// Transform the given point by our matrix.
157
void transform(boost::int32_t& x, boost::int32_t& y) const
159
boost::int32_t t0 = Fixed16Mul(sx, x) + Fixed16Mul(shy, y) + tx;
160
boost::int32_t t1 = Fixed16Mul(shx,x) + Fixed16Mul(sy, y) + ty;
165
/// Transform point 'p' by our matrix.
167
/// Put the result in *result.
169
void transform(point* result, const point& p) const;
171
/// Transform Range2d<float> 'r' by our matrix.
173
/// NULL and WORLD ranges are untouched.
175
void transform(geometry::Range2d<float>& r) const;
177
void transform(rect& r) const;
179
/// Invert this matrix and return the result.
180
const matrix& invert();
182
/// return the magnitude scale of our x coord output
183
double get_x_scale() const;
185
/// return the magnitude scale of our y coord output
186
double get_y_scale() const;
188
/// return rotation component in radians.
189
double get_rotation() const;
191
/// return x translation n TWIPS unit.
192
int get_x_translation() const
197
/// return y translation in TWIPS unit.
198
int get_y_translation() const
204
/// Return the determinant of this matrix in 32.32 fixed point format.
205
boost::int64_t determinant() const;
207
inline boost::int32_t FloatToFixed16(float a)
209
#ifdef TRUST_FLOAT_TO_UINT32_CONVERSION
210
// truncate when overflow occurs.
211
return static_cast<boost::int32_t>(static_cast<boost::uint32_t>(a * 65536.0f));
216
b = static_cast<boost::uint32_t>(std::fmod(a * 65536.0, 4294967296.0));
220
b = -static_cast<boost::uint32_t>(std::fmod(-a * 65536.0, 4294967296.0));
226
inline boost::int32_t DoubleToFixed16(double a)
228
#ifdef TRUST_FLOAT_TO_UINT32_CONVERSION
229
// truncate when overflow occurs.
230
return static_cast<boost::int32_t>(static_cast<boost::uint32_t>(a * 65536.0));
235
b = static_cast<boost::uint32_t>(std::fmod(a * 65536.0, 4294967296.0));
239
b = -static_cast<boost::uint32_t>(std::fmod(-a * 65536.0, 4294967296.0));
247
inline bool operator== (const matrix& a, const matrix& b)
260
#endif // GNASH_MATRIX_H
265
// indent-tabs-mode: t