2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "gdiplus_private.h"
31 /* Multiplies two matrices of the form
37 * and puts the output in out.
39 static void matrix_multiply(GDIPCONST REAL
* left
, GDIPCONST REAL
* right
, REAL
* out
)
44 for(i
= 0; i
< 6; i
++){
46 temp
[i
] = left
[i
- odd
] * right
[odd
] + left
[i
- odd
+ 1] * right
[odd
+ 2] +
47 (i
>= 4 ? right
[odd
+ 4] : 0.0);
50 memcpy(out
, temp
, 6 * sizeof(REAL
));
53 GpStatus WINGDIPAPI
GdipCreateMatrix2(REAL m11
, REAL m12
, REAL m21
, REAL m22
,
54 REAL dx
, REAL dy
, GpMatrix
**matrix
)
57 return InvalidParameter
;
59 *matrix
= GdipAlloc(sizeof(GpMatrix
));
60 if(!*matrix
) return OutOfMemory
;
63 (*matrix
)->matrix
[0] = m11
;
64 (*matrix
)->matrix
[1] = m12
;
66 (*matrix
)->matrix
[2] = m21
;
67 (*matrix
)->matrix
[3] = m22
;
69 (*matrix
)->matrix
[4] = dx
;
70 (*matrix
)->matrix
[5] = dy
;
75 GpStatus WINGDIPAPI
GdipCreateMatrix3(GDIPCONST GpRectF
*rect
,
76 GDIPCONST GpPointF
*pt
, GpMatrix
**matrix
)
79 return InvalidParameter
;
81 *matrix
= GdipAlloc(sizeof(GpMatrix
));
82 if(!*matrix
) return OutOfMemory
;
84 memcpy((*matrix
)->matrix
, rect
, 4 * sizeof(REAL
));
86 (*matrix
)->matrix
[4] = pt
->X
;
87 (*matrix
)->matrix
[5] = pt
->Y
;
92 GpStatus WINGDIPAPI
GdipCloneMatrix(GpMatrix
*matrix
, GpMatrix
**clone
)
95 return InvalidParameter
;
97 *clone
= GdipAlloc(sizeof(GpMatrix
));
98 if(!*clone
) return OutOfMemory
;
105 GpStatus WINGDIPAPI
GdipCreateMatrix(GpMatrix
**matrix
)
108 return InvalidParameter
;
110 *matrix
= GdipAlloc(sizeof(GpMatrix
));
111 if(!*matrix
) return OutOfMemory
;
113 (*matrix
)->matrix
[0] = 1.0;
114 (*matrix
)->matrix
[1] = 0.0;
115 (*matrix
)->matrix
[2] = 0.0;
116 (*matrix
)->matrix
[3] = 1.0;
117 (*matrix
)->matrix
[4] = 0.0;
118 (*matrix
)->matrix
[5] = 0.0;
123 GpStatus WINGDIPAPI
GdipDeleteMatrix(GpMatrix
*matrix
)
126 return InvalidParameter
;
133 GpStatus WINGDIPAPI
GdipGetMatrixElements(GDIPCONST GpMatrix
*matrix
,
137 return InvalidParameter
;
139 memcpy(out
, matrix
->matrix
, sizeof(matrix
->matrix
));
144 GpStatus WINGDIPAPI
GdipMultiplyMatrix(GpMatrix
*matrix
, GpMatrix
* matrix2
,
147 if(!matrix
|| !matrix2
)
148 return InvalidParameter
;
150 if(order
== MatrixOrderAppend
)
151 matrix_multiply(matrix
->matrix
, matrix2
->matrix
, matrix
->matrix
);
153 matrix_multiply(matrix2
->matrix
, matrix
->matrix
, matrix
->matrix
);
158 GpStatus WINGDIPAPI
GdipRotateMatrix(GpMatrix
*matrix
, REAL angle
,
161 REAL cos_theta
, sin_theta
, rotate
[6];
164 return InvalidParameter
;
166 angle
= deg2rad(angle
);
167 cos_theta
= cos(angle
);
168 sin_theta
= sin(angle
);
170 rotate
[0] = cos_theta
;
171 rotate
[1] = sin_theta
;
172 rotate
[2] = -sin_theta
;
173 rotate
[3] = cos_theta
;
177 if(order
== MatrixOrderAppend
)
178 matrix_multiply(matrix
->matrix
, rotate
, matrix
->matrix
);
180 matrix_multiply(rotate
, matrix
->matrix
, matrix
->matrix
);
185 GpStatus WINGDIPAPI
GdipScaleMatrix(GpMatrix
*matrix
, REAL scaleX
, REAL scaleY
,
191 return InvalidParameter
;
200 if(order
== MatrixOrderAppend
)
201 matrix_multiply(matrix
->matrix
, scale
, matrix
->matrix
);
203 matrix_multiply(scale
, matrix
->matrix
, matrix
->matrix
);
208 GpStatus WINGDIPAPI
GdipSetMatrixElements(GpMatrix
*matrix
, REAL m11
, REAL m12
,
209 REAL m21
, REAL m22
, REAL dx
, REAL dy
)
212 return InvalidParameter
;
214 matrix
->matrix
[0] = m11
;
215 matrix
->matrix
[1] = m12
;
216 matrix
->matrix
[2] = m21
;
217 matrix
->matrix
[3] = m22
;
218 matrix
->matrix
[4] = dx
;
219 matrix
->matrix
[5] = dy
;
224 GpStatus WINGDIPAPI
GdipTransformMatrixPoints(GpMatrix
*matrix
, GpPointF
*pts
,
231 return InvalidParameter
;
233 for(i
= 0; i
< count
; i
++)
238 pts
[i
].X
= x
* matrix
->matrix
[0] + y
* matrix
->matrix
[2] + matrix
->matrix
[4];
239 pts
[i
].Y
= x
* matrix
->matrix
[1] + y
* matrix
->matrix
[3] + matrix
->matrix
[5];
245 GpStatus WINGDIPAPI
GdipTranslateMatrix(GpMatrix
*matrix
, REAL offsetX
,
246 REAL offsetY
, GpMatrixOrder order
)
251 return InvalidParameter
;
257 translate
[4] = offsetX
;
258 translate
[5] = offsetY
;
260 if(order
== MatrixOrderAppend
)
261 matrix_multiply(matrix
->matrix
, translate
, matrix
->matrix
);
263 matrix_multiply(translate
, matrix
->matrix
, matrix
->matrix
);