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 inline void matrix_multiply(GDIPCONST REAL
* left
, GDIPCONST REAL
* right
, REAL
* out
)
45 temp
[0] = left
[0] * right
[0] + left
[1] * right
[2];
46 temp
[1] = left
[0] * right
[1] + left
[1] * right
[3];
47 temp
[2] = left
[2] * right
[0] + left
[3] * right
[2];
48 temp
[3] = left
[2] * right
[1] + left
[3] * right
[3];
49 temp
[4] = left
[4] * right
[0] + left
[5] * right
[2] + right
[4];
50 temp
[5] = left
[4] * right
[1] + left
[5] * right
[3] + right
[5];
51 memcpy(out
, temp
, 6 * sizeof(REAL
));
54 static REAL
matrix_det(GDIPCONST GpMatrix
*matrix
)
56 return matrix
->matrix
[0]*matrix
->matrix
[3] - matrix
->matrix
[1]*matrix
->matrix
[2];
59 GpStatus WINGDIPAPI
GdipCreateMatrix2(REAL m11
, REAL m12
, REAL m21
, REAL m22
,
60 REAL dx
, REAL dy
, GpMatrix
**matrix
)
62 TRACE("(%.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %p)\n", m11
, m12
, m21
, m22
, dx
, dy
, matrix
);
65 return InvalidParameter
;
67 *matrix
= heap_alloc_zero(sizeof(GpMatrix
));
68 if(!*matrix
) return OutOfMemory
;
71 (*matrix
)->matrix
[0] = m11
;
72 (*matrix
)->matrix
[1] = m12
;
74 (*matrix
)->matrix
[2] = m21
;
75 (*matrix
)->matrix
[3] = m22
;
77 (*matrix
)->matrix
[4] = dx
;
78 (*matrix
)->matrix
[5] = dy
;
83 GpStatus WINGDIPAPI
GdipCreateMatrix3(GDIPCONST GpRectF
*rect
,
84 GDIPCONST GpPointF
*pt
, GpMatrix
**matrix
)
86 REAL m11
, m12
, m21
, m22
, dx
, dy
;
88 TRACE("(%s, %p, %p)\n", debugstr_rectf(rect
), pt
, matrix
);
91 return InvalidParameter
;
93 m11
= (pt
[1].X
- pt
[0].X
) / rect
->Width
;
94 m21
= (pt
[2].X
- pt
[0].X
) / rect
->Height
;
95 dx
= pt
[0].X
- m11
* rect
->X
- m21
* rect
->Y
;
96 m12
= (pt
[1].Y
- pt
[0].Y
) / rect
->Width
;
97 m22
= (pt
[2].Y
- pt
[0].Y
) / rect
->Height
;
98 dy
= pt
[0].Y
- m12
* rect
->X
- m22
* rect
->Y
;
100 return GdipCreateMatrix2(m11
, m12
, m21
, m22
, dx
, dy
, matrix
);
103 GpStatus WINGDIPAPI
GdipCreateMatrix3I(GDIPCONST GpRect
*rect
, GDIPCONST GpPoint
*pt
,
110 TRACE("(%p, %p, %p)\n", rect
, pt
, matrix
);
112 set_rect(&rectF
, rect
->X
, rect
->Y
, rect
->Width
, rect
->Height
);
114 for (i
= 0; i
< 3; i
++) {
115 ptF
[i
].X
= (REAL
)pt
[i
].X
;
116 ptF
[i
].Y
= (REAL
)pt
[i
].Y
;
118 return GdipCreateMatrix3(&rectF
, ptF
, matrix
);
121 GpStatus WINGDIPAPI
GdipCloneMatrix(GpMatrix
*matrix
, GpMatrix
**clone
)
123 TRACE("(%p, %p)\n", matrix
, clone
);
125 if(!matrix
|| !clone
)
126 return InvalidParameter
;
128 *clone
= heap_alloc_zero(sizeof(GpMatrix
));
129 if(!*clone
) return OutOfMemory
;
136 GpStatus WINGDIPAPI
GdipCreateMatrix(GpMatrix
**matrix
)
138 TRACE("(%p)\n", matrix
);
141 return InvalidParameter
;
143 *matrix
= heap_alloc_zero(sizeof(GpMatrix
));
144 if(!*matrix
) return OutOfMemory
;
146 (*matrix
)->matrix
[0] = 1.0;
147 (*matrix
)->matrix
[1] = 0.0;
148 (*matrix
)->matrix
[2] = 0.0;
149 (*matrix
)->matrix
[3] = 1.0;
150 (*matrix
)->matrix
[4] = 0.0;
151 (*matrix
)->matrix
[5] = 0.0;
156 GpStatus WINGDIPAPI
GdipDeleteMatrix(GpMatrix
*matrix
)
158 TRACE("(%p)\n", matrix
);
161 return InvalidParameter
;
168 GpStatus WINGDIPAPI
GdipGetMatrixElements(GDIPCONST GpMatrix
*matrix
,
171 TRACE("(%p, %p)\n", matrix
, out
);
174 return InvalidParameter
;
176 memcpy(out
, matrix
->matrix
, sizeof(matrix
->matrix
));
181 GpStatus WINGDIPAPI
GdipInvertMatrix(GpMatrix
*matrix
)
187 TRACE("(%p)\n", matrix
);
190 return InvalidParameter
;
192 GdipIsMatrixInvertible(matrix
, &invertible
);
194 return InvalidParameter
;
196 /* optimize inverting simple scaling and translation matrices */
197 if(matrix
->matrix
[1] == 0 && matrix
->matrix
[2] == 0)
199 matrix
->matrix
[4] = -matrix
->matrix
[4] / matrix
->matrix
[0];
200 matrix
->matrix
[5] = -matrix
->matrix
[5] / matrix
->matrix
[3];
201 matrix
->matrix
[0] = 1 / matrix
->matrix
[0];
202 matrix
->matrix
[3] = 1 / matrix
->matrix
[3];
207 det
= matrix_det(matrix
);
211 matrix
->matrix
[0] = copy
.matrix
[3] / det
;
212 matrix
->matrix
[1] = -copy
.matrix
[1] / det
;
213 matrix
->matrix
[2] = -copy
.matrix
[2] / det
;
214 matrix
->matrix
[3] = copy
.matrix
[0] / det
;
215 matrix
->matrix
[4] = (copy
.matrix
[2]*copy
.matrix
[5]-copy
.matrix
[3]*copy
.matrix
[4]) / det
;
216 matrix
->matrix
[5] = -(copy
.matrix
[0]*copy
.matrix
[5]-copy
.matrix
[1]*copy
.matrix
[4]) / det
;
221 GpStatus WINGDIPAPI
GdipIsMatrixInvertible(GDIPCONST GpMatrix
*matrix
, BOOL
*result
)
223 TRACE("(%p, %p)\n", matrix
, result
);
225 if(!matrix
|| !result
)
226 return InvalidParameter
;
228 if(matrix
->matrix
[1] == 0 && matrix
->matrix
[2] == 0)
229 *result
= matrix
->matrix
[0] != 0 && matrix
->matrix
[3] != 0;
231 *result
= (fabs(matrix_det(matrix
)) >= 1e-5);
236 GpStatus WINGDIPAPI
GdipMultiplyMatrix(GpMatrix
*matrix
, GDIPCONST GpMatrix
* matrix2
,
239 TRACE("(%p, %p, %d)\n", matrix
, matrix2
, order
);
241 if(!matrix
|| !matrix2
)
242 return InvalidParameter
;
244 if(order
== MatrixOrderAppend
)
245 matrix_multiply(matrix
->matrix
, matrix2
->matrix
, matrix
->matrix
);
246 else if (order
== MatrixOrderPrepend
)
247 matrix_multiply(matrix2
->matrix
, matrix
->matrix
, matrix
->matrix
);
249 return InvalidParameter
;
254 GpStatus WINGDIPAPI
GdipRotateMatrix(GpMatrix
*matrix
, REAL angle
,
257 REAL cos_theta
, sin_theta
, rotate
[6];
259 TRACE("(%p, %.2f, %d)\n", matrix
, angle
, order
);
262 return InvalidParameter
;
264 angle
= deg2rad(angle
);
265 cos_theta
= cos(angle
);
266 sin_theta
= sin(angle
);
268 rotate
[0] = cos_theta
;
269 rotate
[1] = sin_theta
;
270 rotate
[2] = -sin_theta
;
271 rotate
[3] = cos_theta
;
275 if(order
== MatrixOrderAppend
)
276 matrix_multiply(matrix
->matrix
, rotate
, matrix
->matrix
);
277 else if (order
== MatrixOrderPrepend
)
278 matrix_multiply(rotate
, matrix
->matrix
, matrix
->matrix
);
280 return InvalidParameter
;
285 GpStatus WINGDIPAPI
GdipScaleMatrix(GpMatrix
*matrix
, REAL scaleX
, REAL scaleY
,
288 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, scaleX
, scaleY
, order
);
291 return InvalidParameter
;
293 if(order
== MatrixOrderAppend
)
295 matrix
->matrix
[0] *= scaleX
;
296 matrix
->matrix
[1] *= scaleY
;
297 matrix
->matrix
[2] *= scaleX
;
298 matrix
->matrix
[3] *= scaleY
;
299 matrix
->matrix
[4] *= scaleX
;
300 matrix
->matrix
[5] *= scaleY
;
302 else if (order
== MatrixOrderPrepend
)
304 matrix
->matrix
[0] *= scaleX
;
305 matrix
->matrix
[1] *= scaleX
;
306 matrix
->matrix
[2] *= scaleY
;
307 matrix
->matrix
[3] *= scaleY
;
310 return InvalidParameter
;
315 GpStatus WINGDIPAPI
GdipSetMatrixElements(GpMatrix
*matrix
, REAL m11
, REAL m12
,
316 REAL m21
, REAL m22
, REAL dx
, REAL dy
)
318 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", matrix
, m11
, m12
,
322 return InvalidParameter
;
324 matrix
->matrix
[0] = m11
;
325 matrix
->matrix
[1] = m12
;
326 matrix
->matrix
[2] = m21
;
327 matrix
->matrix
[3] = m22
;
328 matrix
->matrix
[4] = dx
;
329 matrix
->matrix
[5] = dy
;
334 GpStatus WINGDIPAPI
GdipShearMatrix(GpMatrix
*matrix
, REAL shearX
, REAL shearY
,
339 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, shearX
, shearY
, order
);
342 return InvalidParameter
;
344 /* prepare transformation matrix */
352 if(order
== MatrixOrderAppend
)
353 matrix_multiply(matrix
->matrix
, shear
, matrix
->matrix
);
354 else if (order
== MatrixOrderPrepend
)
355 matrix_multiply(shear
, matrix
->matrix
, matrix
->matrix
);
357 return InvalidParameter
;
362 GpStatus WINGDIPAPI
GdipTransformMatrixPoints(GpMatrix
*matrix
, GpPointF
*pts
,
368 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
370 if(!matrix
|| !pts
|| count
<= 0)
371 return InvalidParameter
;
373 for(i
= 0; i
< count
; i
++)
378 pts
[i
].X
= x
* matrix
->matrix
[0] + y
* matrix
->matrix
[2] + matrix
->matrix
[4];
379 pts
[i
].Y
= x
* matrix
->matrix
[1] + y
* matrix
->matrix
[3] + matrix
->matrix
[5];
385 GpStatus WINGDIPAPI
GdipTransformMatrixPointsI(GpMatrix
*matrix
, GpPoint
*pts
, INT count
)
391 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
394 return InvalidParameter
;
396 ptsF
= heap_alloc_zero(sizeof(GpPointF
) * count
);
400 for(i
= 0; i
< count
; i
++){
401 ptsF
[i
].X
= (REAL
)pts
[i
].X
;
402 ptsF
[i
].Y
= (REAL
)pts
[i
].Y
;
405 ret
= GdipTransformMatrixPoints(matrix
, ptsF
, count
);
408 for(i
= 0; i
< count
; i
++){
409 pts
[i
].X
= gdip_round(ptsF
[i
].X
);
410 pts
[i
].Y
= gdip_round(ptsF
[i
].Y
);
417 GpStatus WINGDIPAPI
GdipTranslateMatrix(GpMatrix
*matrix
, REAL offsetX
,
418 REAL offsetY
, GpMatrixOrder order
)
420 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, offsetX
, offsetY
, order
);
423 return InvalidParameter
;
425 if(order
== MatrixOrderAppend
)
427 matrix
->matrix
[4] += offsetX
;
428 matrix
->matrix
[5] += offsetY
;
430 else if (order
== MatrixOrderPrepend
)
432 matrix
->matrix
[4] = offsetX
* matrix
->matrix
[0] + offsetY
* matrix
->matrix
[2]
434 matrix
->matrix
[5] = offsetX
* matrix
->matrix
[1] + offsetY
* matrix
->matrix
[3]
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
);