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 rectF
.X
= (REAL
)rect
->X
;
115 rectF
.Y
= (REAL
)rect
->Y
;
116 rectF
.Width
= (REAL
)rect
->Width
;
117 rectF
.Height
= (REAL
)rect
->Height
;
119 for (i
= 0; i
< 3; i
++) {
120 ptF
[i
].X
= (REAL
)pt
[i
].X
;
121 ptF
[i
].Y
= (REAL
)pt
[i
].Y
;
123 return GdipCreateMatrix3(&rectF
, ptF
, matrix
);
126 GpStatus WINGDIPAPI
GdipCloneMatrix(GpMatrix
*matrix
, GpMatrix
**clone
)
128 TRACE("(%p, %p)\n", matrix
, clone
);
130 if(!matrix
|| !clone
)
131 return InvalidParameter
;
133 *clone
= heap_alloc_zero(sizeof(GpMatrix
));
134 if(!*clone
) return OutOfMemory
;
141 GpStatus WINGDIPAPI
GdipCreateMatrix(GpMatrix
**matrix
)
143 TRACE("(%p)\n", matrix
);
146 return InvalidParameter
;
148 *matrix
= heap_alloc_zero(sizeof(GpMatrix
));
149 if(!*matrix
) return OutOfMemory
;
151 (*matrix
)->matrix
[0] = 1.0;
152 (*matrix
)->matrix
[1] = 0.0;
153 (*matrix
)->matrix
[2] = 0.0;
154 (*matrix
)->matrix
[3] = 1.0;
155 (*matrix
)->matrix
[4] = 0.0;
156 (*matrix
)->matrix
[5] = 0.0;
161 GpStatus WINGDIPAPI
GdipDeleteMatrix(GpMatrix
*matrix
)
163 TRACE("(%p)\n", matrix
);
166 return InvalidParameter
;
173 GpStatus WINGDIPAPI
GdipGetMatrixElements(GDIPCONST GpMatrix
*matrix
,
176 TRACE("(%p, %p)\n", matrix
, out
);
179 return InvalidParameter
;
181 memcpy(out
, matrix
->matrix
, sizeof(matrix
->matrix
));
186 GpStatus WINGDIPAPI
GdipInvertMatrix(GpMatrix
*matrix
)
192 TRACE("(%p)\n", matrix
);
195 return InvalidParameter
;
197 GdipIsMatrixInvertible(matrix
, &invertible
);
199 return InvalidParameter
;
201 /* optimize inverting simple scaling and translation matrices */
202 if(matrix
->matrix
[1] == 0 && matrix
->matrix
[2] == 0)
204 matrix
->matrix
[4] = -matrix
->matrix
[4] / matrix
->matrix
[0];
205 matrix
->matrix
[5] = -matrix
->matrix
[5] / matrix
->matrix
[3];
206 matrix
->matrix
[0] = 1 / matrix
->matrix
[0];
207 matrix
->matrix
[3] = 1 / matrix
->matrix
[3];
212 det
= matrix_det(matrix
);
216 matrix
->matrix
[0] = copy
.matrix
[3] / det
;
217 matrix
->matrix
[1] = -copy
.matrix
[1] / det
;
218 matrix
->matrix
[2] = -copy
.matrix
[2] / det
;
219 matrix
->matrix
[3] = copy
.matrix
[0] / det
;
220 matrix
->matrix
[4] = (copy
.matrix
[2]*copy
.matrix
[5]-copy
.matrix
[3]*copy
.matrix
[4]) / det
;
221 matrix
->matrix
[5] = -(copy
.matrix
[0]*copy
.matrix
[5]-copy
.matrix
[1]*copy
.matrix
[4]) / det
;
226 GpStatus WINGDIPAPI
GdipIsMatrixInvertible(GDIPCONST GpMatrix
*matrix
, BOOL
*result
)
228 TRACE("(%p, %p)\n", matrix
, result
);
230 if(!matrix
|| !result
)
231 return InvalidParameter
;
233 if(matrix
->matrix
[1] == 0 && matrix
->matrix
[2] == 0)
234 *result
= matrix
->matrix
[0] != 0 && matrix
->matrix
[3] != 0;
236 *result
= (fabs(matrix_det(matrix
)) >= 1e-5);
241 GpStatus WINGDIPAPI
GdipMultiplyMatrix(GpMatrix
*matrix
, GDIPCONST GpMatrix
* matrix2
,
244 TRACE("(%p, %p, %d)\n", matrix
, matrix2
, order
);
246 if(!matrix
|| !matrix2
)
247 return InvalidParameter
;
249 if(order
== MatrixOrderAppend
)
250 matrix_multiply(matrix
->matrix
, matrix2
->matrix
, matrix
->matrix
);
251 else if (order
== MatrixOrderPrepend
)
252 matrix_multiply(matrix2
->matrix
, matrix
->matrix
, matrix
->matrix
);
254 return InvalidParameter
;
259 GpStatus WINGDIPAPI
GdipRotateMatrix(GpMatrix
*matrix
, REAL angle
,
262 REAL cos_theta
, sin_theta
, rotate
[6];
264 TRACE("(%p, %.2f, %d)\n", matrix
, angle
, order
);
267 return InvalidParameter
;
269 angle
= deg2rad(angle
);
270 cos_theta
= cos(angle
);
271 sin_theta
= sin(angle
);
273 rotate
[0] = cos_theta
;
274 rotate
[1] = sin_theta
;
275 rotate
[2] = -sin_theta
;
276 rotate
[3] = cos_theta
;
280 if(order
== MatrixOrderAppend
)
281 matrix_multiply(matrix
->matrix
, rotate
, matrix
->matrix
);
282 else if (order
== MatrixOrderPrepend
)
283 matrix_multiply(rotate
, matrix
->matrix
, matrix
->matrix
);
285 return InvalidParameter
;
290 GpStatus WINGDIPAPI
GdipScaleMatrix(GpMatrix
*matrix
, REAL scaleX
, REAL scaleY
,
295 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, scaleX
, scaleY
, order
);
298 return InvalidParameter
;
307 if(order
== MatrixOrderAppend
)
308 matrix_multiply(matrix
->matrix
, scale
, matrix
->matrix
);
309 else if (order
== MatrixOrderPrepend
)
310 matrix_multiply(scale
, matrix
->matrix
, matrix
->matrix
);
312 return InvalidParameter
;
317 GpStatus WINGDIPAPI
GdipSetMatrixElements(GpMatrix
*matrix
, REAL m11
, REAL m12
,
318 REAL m21
, REAL m22
, REAL dx
, REAL dy
)
320 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", matrix
, m11
, m12
,
324 return InvalidParameter
;
326 matrix
->matrix
[0] = m11
;
327 matrix
->matrix
[1] = m12
;
328 matrix
->matrix
[2] = m21
;
329 matrix
->matrix
[3] = m22
;
330 matrix
->matrix
[4] = dx
;
331 matrix
->matrix
[5] = dy
;
336 GpStatus WINGDIPAPI
GdipShearMatrix(GpMatrix
*matrix
, REAL shearX
, REAL shearY
,
341 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, shearX
, shearY
, order
);
344 return InvalidParameter
;
346 /* prepare transformation matrix */
354 if(order
== MatrixOrderAppend
)
355 matrix_multiply(matrix
->matrix
, shear
, matrix
->matrix
);
356 else if (order
== MatrixOrderPrepend
)
357 matrix_multiply(shear
, matrix
->matrix
, matrix
->matrix
);
359 return InvalidParameter
;
364 GpStatus WINGDIPAPI
GdipTransformMatrixPoints(GpMatrix
*matrix
, GpPointF
*pts
,
370 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
372 if(!matrix
|| !pts
|| count
<= 0)
373 return InvalidParameter
;
375 for(i
= 0; i
< count
; i
++)
380 pts
[i
].X
= x
* matrix
->matrix
[0] + y
* matrix
->matrix
[2] + matrix
->matrix
[4];
381 pts
[i
].Y
= x
* matrix
->matrix
[1] + y
* matrix
->matrix
[3] + matrix
->matrix
[5];
387 GpStatus WINGDIPAPI
GdipTransformMatrixPointsI(GpMatrix
*matrix
, GpPoint
*pts
, INT count
)
393 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
396 return InvalidParameter
;
398 ptsF
= heap_alloc_zero(sizeof(GpPointF
) * count
);
402 for(i
= 0; i
< count
; i
++){
403 ptsF
[i
].X
= (REAL
)pts
[i
].X
;
404 ptsF
[i
].Y
= (REAL
)pts
[i
].Y
;
407 ret
= GdipTransformMatrixPoints(matrix
, ptsF
, count
);
410 for(i
= 0; i
< count
; i
++){
411 pts
[i
].X
= gdip_round(ptsF
[i
].X
);
412 pts
[i
].Y
= gdip_round(ptsF
[i
].Y
);
419 GpStatus WINGDIPAPI
GdipTranslateMatrix(GpMatrix
*matrix
, REAL offsetX
,
420 REAL offsetY
, GpMatrixOrder order
)
424 TRACE("(%p, %.2f, %.2f, %d)\n", matrix
, offsetX
, offsetY
, order
);
427 return InvalidParameter
;
433 translate
[4] = offsetX
;
434 translate
[5] = offsetY
;
436 if(order
== MatrixOrderAppend
)
437 matrix_multiply(matrix
->matrix
, translate
, matrix
->matrix
);
438 else if (order
== MatrixOrderPrepend
)
439 matrix_multiply(translate
, matrix
->matrix
, matrix
->matrix
);
441 return InvalidParameter
;
446 GpStatus WINGDIPAPI
GdipVectorTransformMatrixPoints(GpMatrix
*matrix
, GpPointF
*pts
, INT count
)
451 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
453 if(!matrix
|| !pts
|| count
<= 0)
454 return InvalidParameter
;
456 for(i
= 0; i
< count
; i
++)
461 pts
[i
].X
= x
* matrix
->matrix
[0] + y
* matrix
->matrix
[2];
462 pts
[i
].Y
= x
* matrix
->matrix
[1] + y
* matrix
->matrix
[3];
468 GpStatus WINGDIPAPI
GdipVectorTransformMatrixPointsI(GpMatrix
*matrix
, GpPoint
*pts
, INT count
)
474 TRACE("(%p, %p, %d)\n", matrix
, pts
, count
);
477 return InvalidParameter
;
479 ptsF
= heap_alloc_zero(sizeof(GpPointF
) * count
);
483 for(i
= 0; i
< count
; i
++){
484 ptsF
[i
].X
= (REAL
)pts
[i
].X
;
485 ptsF
[i
].Y
= (REAL
)pts
[i
].Y
;
488 ret
= GdipVectorTransformMatrixPoints(matrix
, ptsF
, count
);
491 for(i
= 0; i
< count
; i
++){
492 pts
[i
].X
= gdip_round(ptsF
[i
].X
);
493 pts
[i
].Y
= gdip_round(ptsF
[i
].Y
);
500 GpStatus WINGDIPAPI
GdipIsMatrixEqual(GDIPCONST GpMatrix
*matrix
, GDIPCONST GpMatrix
*matrix2
,
503 TRACE("(%p, %p, %p)\n", matrix
, matrix2
, result
);
505 if(!matrix
|| !matrix2
|| !result
)
506 return InvalidParameter
;
507 /* based on single array member of GpMatrix */
508 *result
= (memcmp(matrix
->matrix
, matrix2
->matrix
, sizeof(GpMatrix
)) == 0);
513 GpStatus WINGDIPAPI
GdipIsMatrixIdentity(GDIPCONST GpMatrix
*matrix
, BOOL
*result
)
515 static const GpMatrix identity
=
522 TRACE("(%p, %p)\n", matrix
, result
);
524 if(!matrix
|| !result
)
525 return InvalidParameter
;
527 return GdipIsMatrixEqual(matrix
, &identity
, result
);