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
;
89 TRACE("(%p, %p, %p)\n", rect
, pt
, matrix
);
92 return InvalidParameter
;
94 m11
= (pt
[1].X
- pt
[0].X
) / rect
->Width
;
95 m21
= (pt
[2].X
- pt
[0].X
) / rect
->Height
;
96 dx
= pt
[0].X
- m11
* rect
->X
- m21
* rect
->Y
;
97 m12
= (pt
[1].Y
- pt
[0].Y
) / rect
->Width
;
98 m22
= (pt
[2].Y
- pt
[0].Y
) / rect
->Height
;
99 dy
= pt
[0].Y
- m12
* rect
->X
- m22
* rect
->Y
;
101 return GdipCreateMatrix2(m11
, m12
, m21
, m22
, dx
, dy
, matrix
);
104 GpStatus WINGDIPAPI
GdipCreateMatrix3I(GDIPCONST GpRect
*rect
, GDIPCONST GpPoint
*pt
,
111 TRACE("(%p, %p, %p)\n", rect
, pt
, matrix
);
113 rectF
.X
= (REAL
)rect
->X
;
114 rectF
.Y
= (REAL
)rect
->Y
;
115 rectF
.Width
= (REAL
)rect
->Width
;
116 rectF
.Height
= (REAL
)rect
->Height
;
118 for (i
= 0; i
< 3; i
++) {
119 ptF
[i
].X
= (REAL
)pt
[i
].X
;
120 ptF
[i
].Y
= (REAL
)pt
[i
].Y
;
122 return GdipCreateMatrix3(&rectF
, ptF
, matrix
);
125 GpStatus WINGDIPAPI
GdipCloneMatrix(GpMatrix
*matrix
, GpMatrix
**clone
)
127 TRACE("(%p, %p)\n", matrix
, clone
);
129 if(!matrix
|| !clone
)
130 return InvalidParameter
;
132 *clone
= heap_alloc_zero(sizeof(GpMatrix
));
133 if(!*clone
) return OutOfMemory
;
140 GpStatus WINGDIPAPI
GdipCreateMatrix(GpMatrix
**matrix
)
142 TRACE("(%p)\n", matrix
);
145 return InvalidParameter
;
147 *matrix
= heap_alloc_zero(sizeof(GpMatrix
));
148 if(!*matrix
) return OutOfMemory
;
150 (*matrix
)->matrix
[0] = 1.0;
151 (*matrix
)->matrix
[1] = 0.0;
152 (*matrix
)->matrix
[2] = 0.0;
153 (*matrix
)->matrix
[3] = 1.0;
154 (*matrix
)->matrix
[4] = 0.0;
155 (*matrix
)->matrix
[5] = 0.0;
160 GpStatus WINGDIPAPI
GdipDeleteMatrix(GpMatrix
*matrix
)
162 TRACE("(%p)\n", matrix
);
165 return InvalidParameter
;
172 GpStatus WINGDIPAPI
GdipGetMatrixElements(GDIPCONST GpMatrix
*matrix
,
175 TRACE("(%p, %p)\n", matrix
, out
);
178 return InvalidParameter
;
180 memcpy(out
, matrix
->matrix
, sizeof(matrix
->matrix
));
185 GpStatus WINGDIPAPI
GdipInvertMatrix(GpMatrix
*matrix
)
191 TRACE("(%p)\n", matrix
);
194 return InvalidParameter
;
196 GdipIsMatrixInvertible(matrix
, &invertible
);
198 return InvalidParameter
;
200 det
= matrix_det(matrix
);
204 matrix
->matrix
[0] = copy
.matrix
[3] / det
;
205 matrix
->matrix
[1] = -copy
.matrix
[1] / det
;
206 matrix
->matrix
[2] = -copy
.matrix
[2] / det
;
207 matrix
->matrix
[3] = copy
.matrix
[0] / det
;
208 matrix
->matrix
[4] = (copy
.matrix
[2]*copy
.matrix
[5]-copy
.matrix
[3]*copy
.matrix
[4]) / det
;
209 matrix
->matrix
[5] = -(copy
.matrix
[0]*copy
.matrix
[5]-copy
.matrix
[1]*copy
.matrix
[4]) / det
;
214 GpStatus WINGDIPAPI
GdipIsMatrixInvertible(GDIPCONST GpMatrix
*matrix
, BOOL
*result
)
216 TRACE("(%p, %p)\n", matrix
, result
);
218 if(!matrix
|| !result
)
219 return InvalidParameter
;
221 *result
= (fabs(matrix_det(matrix
)) >= 1e-5);
226 GpStatus WINGDIPAPI
GdipMultiplyMatrix(GpMatrix
*matrix
, GDIPCONST GpMatrix
* matrix2
,
229 TRACE("(%p, %p, %d)\n", matrix
, matrix2
, order
);
231 if(!matrix
|| !matrix2
)
232 return InvalidParameter
;
234 if(order
== MatrixOrderAppend
)
235 matrix_multiply(matrix
->matrix
, matrix2
->matrix
, matrix
->matrix
);
236 else if (order
== MatrixOrderPrepend
)
237 matrix_multiply(matrix2
->matrix
, matrix
->matrix
, matrix
->matrix
);
239 return InvalidParameter
;
244 GpStatus WINGDIPAPI
GdipRotateMatrix(GpMatrix
*matrix
, REAL angle
,
247 REAL cos_theta
, sin_theta
, rotate
[6];
249 TRACE("(%p, %.2f, %d)\n", matrix
, angle
, order
);
252 return InvalidParameter
;
254 angle
= deg2rad(angle
);
255 cos_theta
= cos(angle
);
256 sin_theta
= sin(angle
);
258 rotate
[0] = cos_theta
;
259 rotate
[1] = sin_theta
;
260 rotate
[2] = -sin_theta
;
261 rotate
[3] = cos_theta
;
265 if(order
== MatrixOrderAppend
)
266 matrix_multiply(matrix
->matrix
, rotate
, matrix
->matrix
);
267 else if (order
== MatrixOrderPrepend
)
268 matrix_multiply(rotate
, matrix
->matrix
, matrix
->matrix
);
270 return InvalidParameter
;
275 GpStatus WINGDIPAPI
GdipScaleMatrix(GpMatrix
*matrix
, REAL scaleX
, REAL scaleY
,
280 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, scaleX
, scaleY
, order
);
283 return InvalidParameter
;
292 if(order
== MatrixOrderAppend
)
293 matrix_multiply(matrix
->matrix
, scale
, matrix
->matrix
);
294 else if (order
== MatrixOrderPrepend
)
295 matrix_multiply(scale
, matrix
->matrix
, matrix
->matrix
);
297 return InvalidParameter
;
302 GpStatus WINGDIPAPI
GdipSetMatrixElements(GpMatrix
*matrix
, REAL m11
, REAL m12
,
303 REAL m21
, REAL m22
, REAL dx
, REAL dy
)
305 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", matrix
, m11
, m12
,
309 return InvalidParameter
;
311 matrix
->matrix
[0] = m11
;
312 matrix
->matrix
[1] = m12
;
313 matrix
->matrix
[2] = m21
;
314 matrix
->matrix
[3] = m22
;
315 matrix
->matrix
[4] = dx
;
316 matrix
->matrix
[5] = dy
;
321 GpStatus WINGDIPAPI
GdipShearMatrix(GpMatrix
*matrix
, REAL shearX
, REAL shearY
,
326 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, shearX
, shearY
, order
);
329 return InvalidParameter
;
331 /* prepare transformation matrix */
339 if(order
== MatrixOrderAppend
)
340 matrix_multiply(matrix
->matrix
, shear
, matrix
->matrix
);
341 else if (order
== MatrixOrderPrepend
)
342 matrix_multiply(shear
, matrix
->matrix
, matrix
->matrix
);
344 return InvalidParameter
;
349 GpStatus WINGDIPAPI
GdipTransformMatrixPoints(GpMatrix
*matrix
, GpPointF
*pts
,
355 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
357 if(!matrix
|| !pts
|| count
<= 0)
358 return InvalidParameter
;
360 for(i
= 0; i
< count
; i
++)
365 pts
[i
].X
= x
* matrix
->matrix
[0] + y
* matrix
->matrix
[2] + matrix
->matrix
[4];
366 pts
[i
].Y
= x
* matrix
->matrix
[1] + y
* matrix
->matrix
[3] + matrix
->matrix
[5];
372 GpStatus WINGDIPAPI
GdipTransformMatrixPointsI(GpMatrix
*matrix
, GpPoint
*pts
, INT count
)
378 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
381 return InvalidParameter
;
383 ptsF
= heap_alloc_zero(sizeof(GpPointF
) * count
);
387 for(i
= 0; i
< count
; i
++){
388 ptsF
[i
].X
= (REAL
)pts
[i
].X
;
389 ptsF
[i
].Y
= (REAL
)pts
[i
].Y
;
392 ret
= GdipTransformMatrixPoints(matrix
, ptsF
, count
);
395 for(i
= 0; i
< count
; i
++){
396 pts
[i
].X
= gdip_round(ptsF
[i
].X
);
397 pts
[i
].Y
= gdip_round(ptsF
[i
].Y
);
404 GpStatus WINGDIPAPI
GdipTranslateMatrix(GpMatrix
*matrix
, REAL offsetX
,
405 REAL offsetY
, GpMatrixOrder order
)
409 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, offsetX
, offsetY
, order
);
412 return InvalidParameter
;
418 translate
[4] = offsetX
;
419 translate
[5] = offsetY
;
421 if(order
== MatrixOrderAppend
)
422 matrix_multiply(matrix
->matrix
, translate
, matrix
->matrix
);
423 else if (order
== MatrixOrderPrepend
)
424 matrix_multiply(translate
, matrix
->matrix
, matrix
->matrix
);
426 return InvalidParameter
;
431 GpStatus WINGDIPAPI
GdipVectorTransformMatrixPoints(GpMatrix
*matrix
, GpPointF
*pts
, INT count
)
436 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
438 if(!matrix
|| !pts
|| count
<= 0)
439 return InvalidParameter
;
441 for(i
= 0; i
< count
; i
++)
446 pts
[i
].X
= x
* matrix
->matrix
[0] + y
* matrix
->matrix
[2];
447 pts
[i
].Y
= x
* matrix
->matrix
[1] + y
* matrix
->matrix
[3];
453 GpStatus WINGDIPAPI
GdipVectorTransformMatrixPointsI(GpMatrix
*matrix
, GpPoint
*pts
, INT count
)
459 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
462 return InvalidParameter
;
464 ptsF
= heap_alloc_zero(sizeof(GpPointF
) * count
);
468 for(i
= 0; i
< count
; i
++){
469 ptsF
[i
].X
= (REAL
)pts
[i
].X
;
470 ptsF
[i
].Y
= (REAL
)pts
[i
].Y
;
473 ret
= GdipVectorTransformMatrixPoints(matrix
, ptsF
, count
);
476 for(i
= 0; i
< count
; i
++){
477 pts
[i
].X
= gdip_round(ptsF
[i
].X
);
478 pts
[i
].Y
= gdip_round(ptsF
[i
].Y
);
485 GpStatus WINGDIPAPI
GdipIsMatrixEqual(GDIPCONST GpMatrix
*matrix
, GDIPCONST GpMatrix
*matrix2
,
488 TRACE("(%p, %p, %p)\n", matrix
, matrix2
, result
);
490 if(!matrix
|| !matrix2
|| !result
)
491 return InvalidParameter
;
492 /* based on single array member of GpMatrix */
493 *result
= (memcmp(matrix
->matrix
, matrix2
->matrix
, sizeof(GpMatrix
)) == 0);
498 GpStatus WINGDIPAPI
GdipIsMatrixIdentity(GDIPCONST GpMatrix
*matrix
, BOOL
*result
)
504 TRACE("(%p, %p)\n", matrix
, result
);
506 if(!matrix
|| !result
)
507 return InvalidParameter
;
509 ret
= GdipCreateMatrix(&e
);
510 if(ret
!= Ok
) return ret
;
512 ret
= GdipIsMatrixEqual(matrix
, e
, &isIdentity
);
514 *result
= isIdentity
;