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"
31 /* Multiplies two matrices of the form
37 * and puts the output in out.
39 static void matrix_multiply(GDIPCONST REAL
* left
, GDIPCONST REAL
* right
, REAL
* out
)
44 for(i
= 0; i
< 6; i
++){
46 temp
[i
] = left
[i
- odd
] * right
[odd
] + left
[i
- odd
+ 1] * right
[odd
+ 2] +
47 (i
>= 4 ? right
[odd
+ 4] : 0.0);
50 memcpy(out
, temp
, 6 * sizeof(REAL
));
53 GpStatus WINGDIPAPI
GdipCreateMatrix2(REAL m11
, REAL m12
, REAL m21
, REAL m22
,
54 REAL dx
, REAL dy
, GpMatrix
**matrix
)
57 return InvalidParameter
;
59 *matrix
= GdipAlloc(sizeof(GpMatrix
));
60 if(!*matrix
) return OutOfMemory
;
63 (*matrix
)->matrix
[0] = m11
;
64 (*matrix
)->matrix
[1] = m12
;
66 (*matrix
)->matrix
[2] = m21
;
67 (*matrix
)->matrix
[3] = m22
;
69 (*matrix
)->matrix
[4] = dx
;
70 (*matrix
)->matrix
[5] = dy
;
75 GpStatus WINGDIPAPI
GdipCreateMatrix3(GDIPCONST GpRectF
*rect
,
76 GDIPCONST GpPointF
*pt
, GpMatrix
**matrix
)
79 return InvalidParameter
;
81 *matrix
= GdipAlloc(sizeof(GpMatrix
));
82 if(!*matrix
) return OutOfMemory
;
84 memcpy((*matrix
)->matrix
, rect
, 4 * sizeof(REAL
));
86 (*matrix
)->matrix
[4] = pt
->X
;
87 (*matrix
)->matrix
[5] = pt
->Y
;
92 GpStatus WINGDIPAPI
GdipCreateMatrix3I(GDIPCONST GpRect
*rect
, GDIPCONST GpPoint
*pt
,
98 rectF
.X
= (REAL
)rect
->X
;
99 rectF
.Y
= (REAL
)rect
->Y
;
100 rectF
.Width
= (REAL
)rect
->Width
;
101 rectF
.Height
= (REAL
)rect
->Height
;
106 return GdipCreateMatrix3(&rectF
, &ptF
, matrix
);
109 GpStatus WINGDIPAPI
GdipCloneMatrix(GpMatrix
*matrix
, GpMatrix
**clone
)
111 if(!matrix
|| !clone
)
112 return InvalidParameter
;
114 *clone
= GdipAlloc(sizeof(GpMatrix
));
115 if(!*clone
) return OutOfMemory
;
122 GpStatus WINGDIPAPI
GdipCreateMatrix(GpMatrix
**matrix
)
125 return InvalidParameter
;
127 *matrix
= GdipAlloc(sizeof(GpMatrix
));
128 if(!*matrix
) return OutOfMemory
;
130 (*matrix
)->matrix
[0] = 1.0;
131 (*matrix
)->matrix
[1] = 0.0;
132 (*matrix
)->matrix
[2] = 0.0;
133 (*matrix
)->matrix
[3] = 1.0;
134 (*matrix
)->matrix
[4] = 0.0;
135 (*matrix
)->matrix
[5] = 0.0;
140 GpStatus WINGDIPAPI
GdipDeleteMatrix(GpMatrix
*matrix
)
143 return InvalidParameter
;
150 GpStatus WINGDIPAPI
GdipGetMatrixElements(GDIPCONST GpMatrix
*matrix
,
154 return InvalidParameter
;
156 memcpy(out
, matrix
->matrix
, sizeof(matrix
->matrix
));
161 GpStatus WINGDIPAPI
GdipIsMatrixInvertible(GDIPCONST GpMatrix
*matrix
, BOOL
*result
)
165 if(!matrix
|| !result
)
166 return InvalidParameter
;
168 det
= matrix
->matrix
[0]*matrix
->matrix
[3] - matrix
->matrix
[1]*matrix
->matrix
[2];
170 *result
= (fabs(det
) >= 1e-5);
175 GpStatus WINGDIPAPI
GdipMultiplyMatrix(GpMatrix
*matrix
, GpMatrix
* matrix2
,
178 if(!matrix
|| !matrix2
)
179 return InvalidParameter
;
181 if(order
== MatrixOrderAppend
)
182 matrix_multiply(matrix
->matrix
, matrix2
->matrix
, matrix
->matrix
);
184 matrix_multiply(matrix2
->matrix
, matrix
->matrix
, matrix
->matrix
);
189 GpStatus WINGDIPAPI
GdipRotateMatrix(GpMatrix
*matrix
, REAL angle
,
192 REAL cos_theta
, sin_theta
, rotate
[6];
195 return InvalidParameter
;
197 angle
= deg2rad(angle
);
198 cos_theta
= cos(angle
);
199 sin_theta
= sin(angle
);
201 rotate
[0] = cos_theta
;
202 rotate
[1] = sin_theta
;
203 rotate
[2] = -sin_theta
;
204 rotate
[3] = cos_theta
;
208 if(order
== MatrixOrderAppend
)
209 matrix_multiply(matrix
->matrix
, rotate
, matrix
->matrix
);
211 matrix_multiply(rotate
, matrix
->matrix
, matrix
->matrix
);
216 GpStatus WINGDIPAPI
GdipScaleMatrix(GpMatrix
*matrix
, REAL scaleX
, REAL scaleY
,
222 return InvalidParameter
;
231 if(order
== MatrixOrderAppend
)
232 matrix_multiply(matrix
->matrix
, scale
, matrix
->matrix
);
234 matrix_multiply(scale
, matrix
->matrix
, matrix
->matrix
);
239 GpStatus WINGDIPAPI
GdipSetMatrixElements(GpMatrix
*matrix
, REAL m11
, REAL m12
,
240 REAL m21
, REAL m22
, REAL dx
, REAL dy
)
243 return InvalidParameter
;
245 matrix
->matrix
[0] = m11
;
246 matrix
->matrix
[1] = m12
;
247 matrix
->matrix
[2] = m21
;
248 matrix
->matrix
[3] = m22
;
249 matrix
->matrix
[4] = dx
;
250 matrix
->matrix
[5] = dy
;
255 GpStatus WINGDIPAPI
GdipTransformMatrixPoints(GpMatrix
*matrix
, GpPointF
*pts
,
261 if(!matrix
|| !pts
|| count
<= 0)
262 return InvalidParameter
;
264 for(i
= 0; i
< count
; i
++)
269 pts
[i
].X
= x
* matrix
->matrix
[0] + y
* matrix
->matrix
[2] + matrix
->matrix
[4];
270 pts
[i
].Y
= x
* matrix
->matrix
[1] + y
* matrix
->matrix
[3] + matrix
->matrix
[5];
276 GpStatus WINGDIPAPI
GdipTransformMatrixPointsI(GpMatrix
*matrix
, GpPoint
*pts
, INT count
)
283 return InvalidParameter
;
285 ptsF
= GdipAlloc(sizeof(GpPointF
) * count
);
289 for(i
= 0; i
< count
; i
++){
290 ptsF
[i
].X
= (REAL
)pts
[i
].X
;
291 ptsF
[i
].Y
= (REAL
)pts
[i
].Y
;
294 ret
= GdipTransformMatrixPoints(matrix
, ptsF
, count
);
297 for(i
= 0; i
< count
; i
++){
298 pts
[i
].X
= roundr(ptsF
[i
].X
);
299 pts
[i
].Y
= roundr(ptsF
[i
].Y
);
306 GpStatus WINGDIPAPI
GdipTranslateMatrix(GpMatrix
*matrix
, REAL offsetX
,
307 REAL offsetY
, GpMatrixOrder order
)
312 return InvalidParameter
;
318 translate
[4] = offsetX
;
319 translate
[5] = offsetY
;
321 if(order
== MatrixOrderAppend
)
322 matrix_multiply(matrix
->matrix
, translate
, matrix
->matrix
);
324 matrix_multiply(translate
, matrix
->matrix
, matrix
->matrix
);
329 GpStatus WINGDIPAPI
GdipVectorTransformMatrixPoints(GpMatrix
*matrix
, GpPointF
*pts
, INT count
)
334 if(!matrix
|| !pts
|| count
<= 0)
335 return InvalidParameter
;
337 for(i
= 0; i
< count
; i
++)
342 pts
[i
].X
= x
* matrix
->matrix
[0] + y
* matrix
->matrix
[2];
343 pts
[i
].Y
= x
* matrix
->matrix
[1] + y
* matrix
->matrix
[3];
349 GpStatus WINGDIPAPI
GdipVectorTransformMatrixPointsI(GpMatrix
*matrix
, GpPoint
*pts
, INT count
)
356 return InvalidParameter
;
358 ptsF
= GdipAlloc(sizeof(GpPointF
) * count
);
362 for(i
= 0; i
< count
; i
++){
363 ptsF
[i
].X
= (REAL
)pts
[i
].X
;
364 ptsF
[i
].Y
= (REAL
)pts
[i
].Y
;
367 ret
= GdipVectorTransformMatrixPoints(matrix
, ptsF
, count
);
370 for(i
= 0; i
< count
; i
++){
371 pts
[i
].X
= roundr(ptsF
[i
].X
);
372 pts
[i
].Y
= roundr(ptsF
[i
].Y
);
379 GpStatus WINGDIPAPI
GdipIsMatrixEqual(GDIPCONST GpMatrix
*matrix
, GDIPCONST GpMatrix
*matrix2
,
382 if(!matrix
|| !matrix2
|| !result
)
383 return InvalidParameter
;
384 /* based on single array member of GpMatrix */
385 *result
= (memcmp(matrix
->matrix
, matrix2
->matrix
, sizeof(GpMatrix
)) == 0);
390 GpStatus WINGDIPAPI
GdipIsMatrixIdentity(GDIPCONST GpMatrix
*matrix
, BOOL
*result
)
396 if(!matrix
|| !result
)
397 return InvalidParameter
;
399 ret
= GdipCreateMatrix(&e
);
400 if(ret
!= Ok
) return ret
;
402 ret
= GdipIsMatrixEqual(matrix
, e
, &isIdentity
);
404 *result
= isIdentity
;