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 /* optimize inverting simple scaling and translation matrices */
201 if(matrix
->matrix
[1] == 0 && matrix
->matrix
[2] == 0)
203 matrix
->matrix
[4] = -matrix
->matrix
[4] / matrix
->matrix
[0];
204 matrix
->matrix
[5] = -matrix
->matrix
[5] / matrix
->matrix
[3];
205 matrix
->matrix
[0] = 1 / matrix
->matrix
[0];
206 matrix
->matrix
[3] = 1 / matrix
->matrix
[3];
211 det
= matrix_det(matrix
);
215 matrix
->matrix
[0] = copy
.matrix
[3] / det
;
216 matrix
->matrix
[1] = -copy
.matrix
[1] / det
;
217 matrix
->matrix
[2] = -copy
.matrix
[2] / det
;
218 matrix
->matrix
[3] = copy
.matrix
[0] / det
;
219 matrix
->matrix
[4] = (copy
.matrix
[2]*copy
.matrix
[5]-copy
.matrix
[3]*copy
.matrix
[4]) / det
;
220 matrix
->matrix
[5] = -(copy
.matrix
[0]*copy
.matrix
[5]-copy
.matrix
[1]*copy
.matrix
[4]) / det
;
225 GpStatus WINGDIPAPI
GdipIsMatrixInvertible(GDIPCONST GpMatrix
*matrix
, BOOL
*result
)
227 TRACE("(%p, %p)\n", matrix
, result
);
229 if(!matrix
|| !result
)
230 return InvalidParameter
;
232 if(matrix
->matrix
[1] == 0 && matrix
->matrix
[2] == 0)
233 *result
= matrix
->matrix
[0] != 0 && matrix
->matrix
[3] != 0;
235 *result
= (fabs(matrix_det(matrix
)) >= 1e-5);
240 GpStatus WINGDIPAPI
GdipMultiplyMatrix(GpMatrix
*matrix
, GDIPCONST GpMatrix
* matrix2
,
243 TRACE("(%p, %p, %d)\n", matrix
, matrix2
, order
);
245 if(!matrix
|| !matrix2
)
246 return InvalidParameter
;
248 if(order
== MatrixOrderAppend
)
249 matrix_multiply(matrix
->matrix
, matrix2
->matrix
, matrix
->matrix
);
250 else if (order
== MatrixOrderPrepend
)
251 matrix_multiply(matrix2
->matrix
, matrix
->matrix
, matrix
->matrix
);
253 return InvalidParameter
;
258 GpStatus WINGDIPAPI
GdipRotateMatrix(GpMatrix
*matrix
, REAL angle
,
261 REAL cos_theta
, sin_theta
, rotate
[6];
263 TRACE("(%p, %.2f, %d)\n", matrix
, angle
, order
);
266 return InvalidParameter
;
268 angle
= deg2rad(angle
);
269 cos_theta
= cos(angle
);
270 sin_theta
= sin(angle
);
272 rotate
[0] = cos_theta
;
273 rotate
[1] = sin_theta
;
274 rotate
[2] = -sin_theta
;
275 rotate
[3] = cos_theta
;
279 if(order
== MatrixOrderAppend
)
280 matrix_multiply(matrix
->matrix
, rotate
, matrix
->matrix
);
281 else if (order
== MatrixOrderPrepend
)
282 matrix_multiply(rotate
, matrix
->matrix
, matrix
->matrix
);
284 return InvalidParameter
;
289 GpStatus WINGDIPAPI
GdipScaleMatrix(GpMatrix
*matrix
, REAL scaleX
, REAL scaleY
,
294 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, scaleX
, scaleY
, order
);
297 return InvalidParameter
;
306 if(order
== MatrixOrderAppend
)
307 matrix_multiply(matrix
->matrix
, scale
, matrix
->matrix
);
308 else if (order
== MatrixOrderPrepend
)
309 matrix_multiply(scale
, matrix
->matrix
, matrix
->matrix
);
311 return InvalidParameter
;
316 GpStatus WINGDIPAPI
GdipSetMatrixElements(GpMatrix
*matrix
, REAL m11
, REAL m12
,
317 REAL m21
, REAL m22
, REAL dx
, REAL dy
)
319 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", matrix
, m11
, m12
,
323 return InvalidParameter
;
325 matrix
->matrix
[0] = m11
;
326 matrix
->matrix
[1] = m12
;
327 matrix
->matrix
[2] = m21
;
328 matrix
->matrix
[3] = m22
;
329 matrix
->matrix
[4] = dx
;
330 matrix
->matrix
[5] = dy
;
335 GpStatus WINGDIPAPI
GdipShearMatrix(GpMatrix
*matrix
, REAL shearX
, REAL shearY
,
340 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, shearX
, shearY
, order
);
343 return InvalidParameter
;
345 /* prepare transformation matrix */
353 if(order
== MatrixOrderAppend
)
354 matrix_multiply(matrix
->matrix
, shear
, matrix
->matrix
);
355 else if (order
== MatrixOrderPrepend
)
356 matrix_multiply(shear
, matrix
->matrix
, matrix
->matrix
);
358 return InvalidParameter
;
363 GpStatus WINGDIPAPI
GdipTransformMatrixPoints(GpMatrix
*matrix
, GpPointF
*pts
,
369 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
371 if(!matrix
|| !pts
|| count
<= 0)
372 return InvalidParameter
;
374 for(i
= 0; i
< count
; i
++)
379 pts
[i
].X
= x
* matrix
->matrix
[0] + y
* matrix
->matrix
[2] + matrix
->matrix
[4];
380 pts
[i
].Y
= x
* matrix
->matrix
[1] + y
* matrix
->matrix
[3] + matrix
->matrix
[5];
386 GpStatus WINGDIPAPI
GdipTransformMatrixPointsI(GpMatrix
*matrix
, GpPoint
*pts
, INT count
)
392 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
395 return InvalidParameter
;
397 ptsF
= heap_alloc_zero(sizeof(GpPointF
) * count
);
401 for(i
= 0; i
< count
; i
++){
402 ptsF
[i
].X
= (REAL
)pts
[i
].X
;
403 ptsF
[i
].Y
= (REAL
)pts
[i
].Y
;
406 ret
= GdipTransformMatrixPoints(matrix
, ptsF
, count
);
409 for(i
= 0; i
< count
; i
++){
410 pts
[i
].X
= gdip_round(ptsF
[i
].X
);
411 pts
[i
].Y
= gdip_round(ptsF
[i
].Y
);
418 GpStatus WINGDIPAPI
GdipTranslateMatrix(GpMatrix
*matrix
, REAL offsetX
,
419 REAL offsetY
, GpMatrixOrder order
)
423 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, offsetX
, offsetY
, order
);
426 return InvalidParameter
;
432 translate
[4] = offsetX
;
433 translate
[5] = offsetY
;
435 if(order
== MatrixOrderAppend
)
436 matrix_multiply(matrix
->matrix
, translate
, matrix
->matrix
);
437 else if (order
== MatrixOrderPrepend
)
438 matrix_multiply(translate
, matrix
->matrix
, matrix
->matrix
);
440 return InvalidParameter
;
445 GpStatus WINGDIPAPI
GdipVectorTransformMatrixPoints(GpMatrix
*matrix
, GpPointF
*pts
, INT count
)
450 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
452 if(!matrix
|| !pts
|| count
<= 0)
453 return InvalidParameter
;
455 for(i
= 0; i
< count
; i
++)
460 pts
[i
].X
= x
* matrix
->matrix
[0] + y
* matrix
->matrix
[2];
461 pts
[i
].Y
= x
* matrix
->matrix
[1] + y
* matrix
->matrix
[3];
467 GpStatus WINGDIPAPI
GdipVectorTransformMatrixPointsI(GpMatrix
*matrix
, GpPoint
*pts
, INT count
)
473 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
476 return InvalidParameter
;
478 ptsF
= heap_alloc_zero(sizeof(GpPointF
) * count
);
482 for(i
= 0; i
< count
; i
++){
483 ptsF
[i
].X
= (REAL
)pts
[i
].X
;
484 ptsF
[i
].Y
= (REAL
)pts
[i
].Y
;
487 ret
= GdipVectorTransformMatrixPoints(matrix
, ptsF
, count
);
490 for(i
= 0; i
< count
; i
++){
491 pts
[i
].X
= gdip_round(ptsF
[i
].X
);
492 pts
[i
].Y
= gdip_round(ptsF
[i
].Y
);
499 GpStatus WINGDIPAPI
GdipIsMatrixEqual(GDIPCONST GpMatrix
*matrix
, GDIPCONST GpMatrix
*matrix2
,
502 TRACE("(%p, %p, %p)\n", matrix
, matrix2
, result
);
504 if(!matrix
|| !matrix2
|| !result
)
505 return InvalidParameter
;
506 /* based on single array member of GpMatrix */
507 *result
= (memcmp(matrix
->matrix
, matrix2
->matrix
, sizeof(GpMatrix
)) == 0);
512 GpStatus WINGDIPAPI
GdipIsMatrixIdentity(GDIPCONST GpMatrix
*matrix
, BOOL
*result
)
514 static const GpMatrix identity
=
521 TRACE("(%p, %p)\n", matrix
, result
);
523 if(!matrix
|| !result
)
524 return InvalidParameter
;
526 return GdipIsMatrixEqual(matrix
, &identity
, result
);