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"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus
);
34 /* Multiplies two matrices of the form
40 * and puts the output in out.
42 static void matrix_multiply(GDIPCONST REAL
* left
, GDIPCONST REAL
* right
, REAL
* out
)
47 for(i
= 0; i
< 6; i
++){
49 temp
[i
] = left
[i
- odd
] * right
[odd
] + left
[i
- odd
+ 1] * right
[odd
+ 2] +
50 (i
>= 4 ? right
[odd
+ 4] : 0.0);
53 memcpy(out
, temp
, 6 * sizeof(REAL
));
56 static REAL
matrix_det(GDIPCONST GpMatrix
*matrix
)
58 return matrix
->matrix
[0]*matrix
->matrix
[3] - matrix
->matrix
[1]*matrix
->matrix
[2];
61 GpStatus WINGDIPAPI
GdipCreateMatrix2(REAL m11
, REAL m12
, REAL m21
, REAL m22
,
62 REAL dx
, REAL dy
, GpMatrix
**matrix
)
64 TRACE("(%.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %p)\n", m11
, m12
, m21
, m22
, dx
, dy
, matrix
);
67 return InvalidParameter
;
69 *matrix
= heap_alloc_zero(sizeof(GpMatrix
));
70 if(!*matrix
) return OutOfMemory
;
73 (*matrix
)->matrix
[0] = m11
;
74 (*matrix
)->matrix
[1] = m12
;
76 (*matrix
)->matrix
[2] = m21
;
77 (*matrix
)->matrix
[3] = m22
;
79 (*matrix
)->matrix
[4] = dx
;
80 (*matrix
)->matrix
[5] = dy
;
85 GpStatus WINGDIPAPI
GdipCreateMatrix3(GDIPCONST GpRectF
*rect
,
86 GDIPCONST GpPointF
*pt
, GpMatrix
**matrix
)
88 REAL m11
, m12
, m21
, m22
, dx
, dy
;
90 TRACE("(%s, %p, %p)\n", debugstr_rectf(rect
), pt
, matrix
);
93 return InvalidParameter
;
95 m11
= (pt
[1].X
- pt
[0].X
) / rect
->Width
;
96 m21
= (pt
[2].X
- pt
[0].X
) / rect
->Height
;
97 dx
= pt
[0].X
- m11
* rect
->X
- m21
* rect
->Y
;
98 m12
= (pt
[1].Y
- pt
[0].Y
) / rect
->Width
;
99 m22
= (pt
[2].Y
- pt
[0].Y
) / rect
->Height
;
100 dy
= pt
[0].Y
- m12
* rect
->X
- m22
* rect
->Y
;
102 return GdipCreateMatrix2(m11
, m12
, m21
, m22
, dx
, dy
, matrix
);
105 GpStatus WINGDIPAPI
GdipCreateMatrix3I(GDIPCONST GpRect
*rect
, GDIPCONST GpPoint
*pt
,
112 TRACE("(%p, %p, %p)\n", rect
, pt
, matrix
);
114 set_rect(&rectF
, rect
->X
, rect
->Y
, rect
->Width
, rect
->Height
);
116 for (i
= 0; i
< 3; i
++) {
117 ptF
[i
].X
= (REAL
)pt
[i
].X
;
118 ptF
[i
].Y
= (REAL
)pt
[i
].Y
;
120 return GdipCreateMatrix3(&rectF
, ptF
, matrix
);
123 GpStatus WINGDIPAPI
GdipCloneMatrix(GpMatrix
*matrix
, GpMatrix
**clone
)
125 TRACE("(%p, %p)\n", matrix
, clone
);
127 if(!matrix
|| !clone
)
128 return InvalidParameter
;
130 *clone
= heap_alloc_zero(sizeof(GpMatrix
));
131 if(!*clone
) return OutOfMemory
;
138 GpStatus WINGDIPAPI
GdipCreateMatrix(GpMatrix
**matrix
)
140 TRACE("(%p)\n", matrix
);
143 return InvalidParameter
;
145 *matrix
= heap_alloc_zero(sizeof(GpMatrix
));
146 if(!*matrix
) return OutOfMemory
;
148 (*matrix
)->matrix
[0] = 1.0;
149 (*matrix
)->matrix
[1] = 0.0;
150 (*matrix
)->matrix
[2] = 0.0;
151 (*matrix
)->matrix
[3] = 1.0;
152 (*matrix
)->matrix
[4] = 0.0;
153 (*matrix
)->matrix
[5] = 0.0;
158 GpStatus WINGDIPAPI
GdipDeleteMatrix(GpMatrix
*matrix
)
160 TRACE("(%p)\n", matrix
);
163 return InvalidParameter
;
170 GpStatus WINGDIPAPI
GdipGetMatrixElements(GDIPCONST GpMatrix
*matrix
,
173 TRACE("(%p, %p)\n", matrix
, out
);
176 return InvalidParameter
;
178 memcpy(out
, matrix
->matrix
, sizeof(matrix
->matrix
));
183 GpStatus WINGDIPAPI
GdipInvertMatrix(GpMatrix
*matrix
)
189 TRACE("(%p)\n", matrix
);
192 return InvalidParameter
;
194 GdipIsMatrixInvertible(matrix
, &invertible
);
196 return InvalidParameter
;
198 /* optimize inverting simple scaling and translation matrices */
199 if(matrix
->matrix
[1] == 0 && matrix
->matrix
[2] == 0)
201 matrix
->matrix
[4] = -matrix
->matrix
[4] / matrix
->matrix
[0];
202 matrix
->matrix
[5] = -matrix
->matrix
[5] / matrix
->matrix
[3];
203 matrix
->matrix
[0] = 1 / matrix
->matrix
[0];
204 matrix
->matrix
[3] = 1 / matrix
->matrix
[3];
209 det
= matrix_det(matrix
);
213 matrix
->matrix
[0] = copy
.matrix
[3] / det
;
214 matrix
->matrix
[1] = -copy
.matrix
[1] / det
;
215 matrix
->matrix
[2] = -copy
.matrix
[2] / det
;
216 matrix
->matrix
[3] = copy
.matrix
[0] / det
;
217 matrix
->matrix
[4] = (copy
.matrix
[2]*copy
.matrix
[5]-copy
.matrix
[3]*copy
.matrix
[4]) / det
;
218 matrix
->matrix
[5] = -(copy
.matrix
[0]*copy
.matrix
[5]-copy
.matrix
[1]*copy
.matrix
[4]) / det
;
223 GpStatus WINGDIPAPI
GdipIsMatrixInvertible(GDIPCONST GpMatrix
*matrix
, BOOL
*result
)
225 TRACE("(%p, %p)\n", matrix
, result
);
227 if(!matrix
|| !result
)
228 return InvalidParameter
;
230 if(matrix
->matrix
[1] == 0 && matrix
->matrix
[2] == 0)
231 *result
= matrix
->matrix
[0] != 0 && matrix
->matrix
[3] != 0;
233 *result
= (fabs(matrix_det(matrix
)) >= 1e-5);
238 GpStatus WINGDIPAPI
GdipMultiplyMatrix(GpMatrix
*matrix
, GDIPCONST GpMatrix
* matrix2
,
241 TRACE("(%p, %p, %d)\n", matrix
, matrix2
, order
);
243 if(!matrix
|| !matrix2
)
244 return InvalidParameter
;
246 if(order
== MatrixOrderAppend
)
247 matrix_multiply(matrix
->matrix
, matrix2
->matrix
, matrix
->matrix
);
248 else if (order
== MatrixOrderPrepend
)
249 matrix_multiply(matrix2
->matrix
, matrix
->matrix
, matrix
->matrix
);
251 return InvalidParameter
;
256 GpStatus WINGDIPAPI
GdipRotateMatrix(GpMatrix
*matrix
, REAL angle
,
259 REAL cos_theta
, sin_theta
, rotate
[6];
261 TRACE("(%p, %.2f, %d)\n", matrix
, angle
, order
);
264 return InvalidParameter
;
266 angle
= deg2rad(angle
);
267 cos_theta
= cos(angle
);
268 sin_theta
= sin(angle
);
270 rotate
[0] = cos_theta
;
271 rotate
[1] = sin_theta
;
272 rotate
[2] = -sin_theta
;
273 rotate
[3] = cos_theta
;
277 if(order
== MatrixOrderAppend
)
278 matrix_multiply(matrix
->matrix
, rotate
, matrix
->matrix
);
279 else if (order
== MatrixOrderPrepend
)
280 matrix_multiply(rotate
, matrix
->matrix
, matrix
->matrix
);
282 return InvalidParameter
;
287 GpStatus WINGDIPAPI
GdipScaleMatrix(GpMatrix
*matrix
, REAL scaleX
, REAL scaleY
,
292 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, scaleX
, scaleY
, order
);
295 return InvalidParameter
;
304 if(order
== MatrixOrderAppend
)
305 matrix_multiply(matrix
->matrix
, scale
, matrix
->matrix
);
306 else if (order
== MatrixOrderPrepend
)
307 matrix_multiply(scale
, matrix
->matrix
, matrix
->matrix
);
309 return InvalidParameter
;
314 GpStatus WINGDIPAPI
GdipSetMatrixElements(GpMatrix
*matrix
, REAL m11
, REAL m12
,
315 REAL m21
, REAL m22
, REAL dx
, REAL dy
)
317 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", matrix
, m11
, m12
,
321 return InvalidParameter
;
323 matrix
->matrix
[0] = m11
;
324 matrix
->matrix
[1] = m12
;
325 matrix
->matrix
[2] = m21
;
326 matrix
->matrix
[3] = m22
;
327 matrix
->matrix
[4] = dx
;
328 matrix
->matrix
[5] = dy
;
333 GpStatus WINGDIPAPI
GdipShearMatrix(GpMatrix
*matrix
, REAL shearX
, REAL shearY
,
338 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, shearX
, shearY
, order
);
341 return InvalidParameter
;
343 /* prepare transformation matrix */
351 if(order
== MatrixOrderAppend
)
352 matrix_multiply(matrix
->matrix
, shear
, matrix
->matrix
);
353 else if (order
== MatrixOrderPrepend
)
354 matrix_multiply(shear
, matrix
->matrix
, matrix
->matrix
);
356 return InvalidParameter
;
361 GpStatus WINGDIPAPI
GdipTransformMatrixPoints(GpMatrix
*matrix
, GpPointF
*pts
,
367 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
369 if(!matrix
|| !pts
|| count
<= 0)
370 return InvalidParameter
;
372 for(i
= 0; i
< count
; i
++)
377 pts
[i
].X
= x
* matrix
->matrix
[0] + y
* matrix
->matrix
[2] + matrix
->matrix
[4];
378 pts
[i
].Y
= x
* matrix
->matrix
[1] + y
* matrix
->matrix
[3] + matrix
->matrix
[5];
384 GpStatus WINGDIPAPI
GdipTransformMatrixPointsI(GpMatrix
*matrix
, GpPoint
*pts
, INT count
)
390 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
393 return InvalidParameter
;
395 ptsF
= heap_alloc_zero(sizeof(GpPointF
) * count
);
399 for(i
= 0; i
< count
; i
++){
400 ptsF
[i
].X
= (REAL
)pts
[i
].X
;
401 ptsF
[i
].Y
= (REAL
)pts
[i
].Y
;
404 ret
= GdipTransformMatrixPoints(matrix
, ptsF
, count
);
407 for(i
= 0; i
< count
; i
++){
408 pts
[i
].X
= gdip_round(ptsF
[i
].X
);
409 pts
[i
].Y
= gdip_round(ptsF
[i
].Y
);
416 GpStatus WINGDIPAPI
GdipTranslateMatrix(GpMatrix
*matrix
, REAL offsetX
,
417 REAL offsetY
, GpMatrixOrder order
)
421 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, offsetX
, offsetY
, order
);
424 return InvalidParameter
;
430 translate
[4] = offsetX
;
431 translate
[5] = offsetY
;
433 if(order
== MatrixOrderAppend
)
434 matrix_multiply(matrix
->matrix
, translate
, matrix
->matrix
);
435 else if (order
== MatrixOrderPrepend
)
436 matrix_multiply(translate
, matrix
->matrix
, matrix
->matrix
);
438 return InvalidParameter
;
443 GpStatus WINGDIPAPI
GdipVectorTransformMatrixPoints(GpMatrix
*matrix
, GpPointF
*pts
, INT count
)
448 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
450 if(!matrix
|| !pts
|| count
<= 0)
451 return InvalidParameter
;
453 for(i
= 0; i
< count
; i
++)
458 pts
[i
].X
= x
* matrix
->matrix
[0] + y
* matrix
->matrix
[2];
459 pts
[i
].Y
= x
* matrix
->matrix
[1] + y
* matrix
->matrix
[3];
465 GpStatus WINGDIPAPI
GdipVectorTransformMatrixPointsI(GpMatrix
*matrix
, GpPoint
*pts
, INT count
)
471 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
474 return InvalidParameter
;
476 ptsF
= heap_alloc_zero(sizeof(GpPointF
) * count
);
480 for(i
= 0; i
< count
; i
++){
481 ptsF
[i
].X
= (REAL
)pts
[i
].X
;
482 ptsF
[i
].Y
= (REAL
)pts
[i
].Y
;
485 ret
= GdipVectorTransformMatrixPoints(matrix
, ptsF
, count
);
488 for(i
= 0; i
< count
; i
++){
489 pts
[i
].X
= gdip_round(ptsF
[i
].X
);
490 pts
[i
].Y
= gdip_round(ptsF
[i
].Y
);
497 GpStatus WINGDIPAPI
GdipIsMatrixEqual(GDIPCONST GpMatrix
*matrix
, GDIPCONST GpMatrix
*matrix2
,
500 TRACE("(%p, %p, %p)\n", matrix
, matrix2
, result
);
502 if(!matrix
|| !matrix2
|| !result
)
503 return InvalidParameter
;
504 /* based on single array member of GpMatrix */
505 *result
= (memcmp(matrix
->matrix
, matrix2
->matrix
, sizeof(GpMatrix
)) == 0);
510 GpStatus WINGDIPAPI
GdipIsMatrixIdentity(GDIPCONST GpMatrix
*matrix
, BOOL
*result
)
512 static const GpMatrix identity
=
519 TRACE("(%p, %p)\n", matrix
, result
);
521 if(!matrix
|| !result
)
522 return InvalidParameter
;
524 return GdipIsMatrixEqual(matrix
, &identity
, result
);