gdiplus: GdipInvertMatrix implementation with tests.
[wine.git] / dlls / gdiplus / matrix.c
blob871490824fa718167df6291278193aa8958519a3
1 /*
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
19 #include <stdarg.h>
20 #include <math.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
26 #include "objbase.h"
28 #include "gdiplus.h"
29 #include "gdiplus_private.h"
31 /* Multiplies two matrices of the form
33 * idx:0 idx:1 0
34 * idx:2 idx:3 0
35 * idx:4 idx:5 1
37 * and puts the output in out.
38 * */
39 static void matrix_multiply(GDIPCONST REAL * left, GDIPCONST REAL * right, REAL * out)
41 REAL temp[6];
42 int i, odd;
44 for(i = 0; i < 6; i++){
45 odd = i % 2;
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 static REAL matrix_det(GDIPCONST GpMatrix *matrix)
55 return matrix->matrix[0]*matrix->matrix[3] - matrix->matrix[1]*matrix->matrix[2];
58 GpStatus WINGDIPAPI GdipCreateMatrix2(REAL m11, REAL m12, REAL m21, REAL m22,
59 REAL dx, REAL dy, GpMatrix **matrix)
61 if(!matrix)
62 return InvalidParameter;
64 *matrix = GdipAlloc(sizeof(GpMatrix));
65 if(!*matrix) return OutOfMemory;
67 /* first row */
68 (*matrix)->matrix[0] = m11;
69 (*matrix)->matrix[1] = m12;
70 /* second row */
71 (*matrix)->matrix[2] = m21;
72 (*matrix)->matrix[3] = m22;
73 /* third row */
74 (*matrix)->matrix[4] = dx;
75 (*matrix)->matrix[5] = dy;
77 return Ok;
80 GpStatus WINGDIPAPI GdipCreateMatrix3(GDIPCONST GpRectF *rect,
81 GDIPCONST GpPointF *pt, GpMatrix **matrix)
83 if(!matrix)
84 return InvalidParameter;
86 *matrix = GdipAlloc(sizeof(GpMatrix));
87 if(!*matrix) return OutOfMemory;
89 memcpy((*matrix)->matrix, rect, 4 * sizeof(REAL));
91 (*matrix)->matrix[4] = pt->X;
92 (*matrix)->matrix[5] = pt->Y;
94 return Ok;
97 GpStatus WINGDIPAPI GdipCreateMatrix3I(GDIPCONST GpRect *rect, GDIPCONST GpPoint *pt,
98 GpMatrix **matrix)
100 GpRectF rectF;
101 GpPointF ptF;
103 rectF.X = (REAL)rect->X;
104 rectF.Y = (REAL)rect->Y;
105 rectF.Width = (REAL)rect->Width;
106 rectF.Height = (REAL)rect->Height;
108 ptF.X = (REAL)pt->X;
109 ptF.Y = (REAL)pt->Y;
111 return GdipCreateMatrix3(&rectF, &ptF, matrix);
114 GpStatus WINGDIPAPI GdipCloneMatrix(GpMatrix *matrix, GpMatrix **clone)
116 if(!matrix || !clone)
117 return InvalidParameter;
119 *clone = GdipAlloc(sizeof(GpMatrix));
120 if(!*clone) return OutOfMemory;
122 **clone = *matrix;
124 return Ok;
127 GpStatus WINGDIPAPI GdipCreateMatrix(GpMatrix **matrix)
129 if(!matrix)
130 return InvalidParameter;
132 *matrix = GdipAlloc(sizeof(GpMatrix));
133 if(!*matrix) return OutOfMemory;
135 (*matrix)->matrix[0] = 1.0;
136 (*matrix)->matrix[1] = 0.0;
137 (*matrix)->matrix[2] = 0.0;
138 (*matrix)->matrix[3] = 1.0;
139 (*matrix)->matrix[4] = 0.0;
140 (*matrix)->matrix[5] = 0.0;
142 return Ok;
145 GpStatus WINGDIPAPI GdipDeleteMatrix(GpMatrix *matrix)
147 if(!matrix)
148 return InvalidParameter;
150 GdipFree(matrix);
152 return Ok;
155 GpStatus WINGDIPAPI GdipGetMatrixElements(GDIPCONST GpMatrix *matrix,
156 REAL *out)
158 if(!matrix || !out)
159 return InvalidParameter;
161 memcpy(out, matrix->matrix, sizeof(matrix->matrix));
163 return Ok;
166 GpStatus WINGDIPAPI GdipInvertMatrix(GpMatrix *matrix)
168 GpMatrix copy;
169 REAL det;
170 BOOL invertible;
172 if(!matrix)
173 return InvalidParameter;
175 GdipIsMatrixInvertible(matrix, &invertible);
176 if(!invertible)
177 return InvalidParameter;
179 det = matrix_det(matrix);
181 copy = *matrix;
182 /* store result */
183 matrix->matrix[0] = copy.matrix[3] / det;
184 matrix->matrix[1] = -copy.matrix[1] / det;
185 matrix->matrix[2] = -copy.matrix[2] / det;
186 matrix->matrix[3] = copy.matrix[0] / det;
187 matrix->matrix[4] = (copy.matrix[2]*copy.matrix[5]-copy.matrix[3]*copy.matrix[4]) / det;
188 matrix->matrix[5] = -(copy.matrix[0]*copy.matrix[5]-copy.matrix[1]*copy.matrix[4]) / det;
190 return Ok;
193 GpStatus WINGDIPAPI GdipIsMatrixInvertible(GDIPCONST GpMatrix *matrix, BOOL *result)
195 if(!matrix || !result)
196 return InvalidParameter;
198 *result = (fabs(matrix_det(matrix)) >= 1e-5);
200 return Ok;
203 GpStatus WINGDIPAPI GdipMultiplyMatrix(GpMatrix *matrix, GpMatrix* matrix2,
204 GpMatrixOrder order)
206 if(!matrix || !matrix2)
207 return InvalidParameter;
209 if(order == MatrixOrderAppend)
210 matrix_multiply(matrix->matrix, matrix2->matrix, matrix->matrix);
211 else
212 matrix_multiply(matrix2->matrix, matrix->matrix, matrix->matrix);
214 return Ok;
217 GpStatus WINGDIPAPI GdipRotateMatrix(GpMatrix *matrix, REAL angle,
218 GpMatrixOrder order)
220 REAL cos_theta, sin_theta, rotate[6];
222 if(!matrix)
223 return InvalidParameter;
225 angle = deg2rad(angle);
226 cos_theta = cos(angle);
227 sin_theta = sin(angle);
229 rotate[0] = cos_theta;
230 rotate[1] = sin_theta;
231 rotate[2] = -sin_theta;
232 rotate[3] = cos_theta;
233 rotate[4] = 0.0;
234 rotate[5] = 0.0;
236 if(order == MatrixOrderAppend)
237 matrix_multiply(matrix->matrix, rotate, matrix->matrix);
238 else
239 matrix_multiply(rotate, matrix->matrix, matrix->matrix);
241 return Ok;
244 GpStatus WINGDIPAPI GdipScaleMatrix(GpMatrix *matrix, REAL scaleX, REAL scaleY,
245 GpMatrixOrder order)
247 REAL scale[6];
249 if(!matrix)
250 return InvalidParameter;
252 scale[0] = scaleX;
253 scale[1] = 0.0;
254 scale[2] = 0.0;
255 scale[3] = scaleY;
256 scale[4] = 0.0;
257 scale[5] = 0.0;
259 if(order == MatrixOrderAppend)
260 matrix_multiply(matrix->matrix, scale, matrix->matrix);
261 else
262 matrix_multiply(scale, matrix->matrix, matrix->matrix);
264 return Ok;
267 GpStatus WINGDIPAPI GdipSetMatrixElements(GpMatrix *matrix, REAL m11, REAL m12,
268 REAL m21, REAL m22, REAL dx, REAL dy)
270 if(!matrix)
271 return InvalidParameter;
273 matrix->matrix[0] = m11;
274 matrix->matrix[1] = m12;
275 matrix->matrix[2] = m21;
276 matrix->matrix[3] = m22;
277 matrix->matrix[4] = dx;
278 matrix->matrix[5] = dy;
280 return Ok;
283 GpStatus WINGDIPAPI GdipTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts,
284 INT count)
286 REAL x, y;
287 INT i;
289 if(!matrix || !pts || count <= 0)
290 return InvalidParameter;
292 for(i = 0; i < count; i++)
294 x = pts[i].X;
295 y = pts[i].Y;
297 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2] + matrix->matrix[4];
298 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3] + matrix->matrix[5];
301 return Ok;
304 GpStatus WINGDIPAPI GdipTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
306 GpPointF *ptsF;
307 GpStatus ret;
308 INT i;
310 if(count <= 0)
311 return InvalidParameter;
313 ptsF = GdipAlloc(sizeof(GpPointF) * count);
314 if(!ptsF)
315 return OutOfMemory;
317 for(i = 0; i < count; i++){
318 ptsF[i].X = (REAL)pts[i].X;
319 ptsF[i].Y = (REAL)pts[i].Y;
322 ret = GdipTransformMatrixPoints(matrix, ptsF, count);
324 if(ret == Ok)
325 for(i = 0; i < count; i++){
326 pts[i].X = roundr(ptsF[i].X);
327 pts[i].Y = roundr(ptsF[i].Y);
329 GdipFree(ptsF);
331 return ret;
334 GpStatus WINGDIPAPI GdipTranslateMatrix(GpMatrix *matrix, REAL offsetX,
335 REAL offsetY, GpMatrixOrder order)
337 REAL translate[6];
339 if(!matrix)
340 return InvalidParameter;
342 translate[0] = 1.0;
343 translate[1] = 0.0;
344 translate[2] = 0.0;
345 translate[3] = 1.0;
346 translate[4] = offsetX;
347 translate[5] = offsetY;
349 if(order == MatrixOrderAppend)
350 matrix_multiply(matrix->matrix, translate, matrix->matrix);
351 else
352 matrix_multiply(translate, matrix->matrix, matrix->matrix);
354 return Ok;
357 GpStatus WINGDIPAPI GdipVectorTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts, INT count)
359 REAL x, y;
360 INT i;
362 if(!matrix || !pts || count <= 0)
363 return InvalidParameter;
365 for(i = 0; i < count; i++)
367 x = pts[i].X;
368 y = pts[i].Y;
370 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2];
371 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3];
374 return Ok;
377 GpStatus WINGDIPAPI GdipVectorTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
379 GpPointF *ptsF;
380 GpStatus ret;
381 INT i;
383 if(count <= 0)
384 return InvalidParameter;
386 ptsF = GdipAlloc(sizeof(GpPointF) * count);
387 if(!ptsF)
388 return OutOfMemory;
390 for(i = 0; i < count; i++){
391 ptsF[i].X = (REAL)pts[i].X;
392 ptsF[i].Y = (REAL)pts[i].Y;
395 ret = GdipVectorTransformMatrixPoints(matrix, ptsF, count);
396 /* store back */
397 if(ret == Ok)
398 for(i = 0; i < count; i++){
399 pts[i].X = roundr(ptsF[i].X);
400 pts[i].Y = roundr(ptsF[i].Y);
402 GdipFree(ptsF);
404 return ret;
407 GpStatus WINGDIPAPI GdipIsMatrixEqual(GDIPCONST GpMatrix *matrix, GDIPCONST GpMatrix *matrix2,
408 BOOL *result)
410 if(!matrix || !matrix2 || !result)
411 return InvalidParameter;
412 /* based on single array member of GpMatrix */
413 *result = (memcmp(matrix->matrix, matrix2->matrix, sizeof(GpMatrix)) == 0);
415 return Ok;
418 GpStatus WINGDIPAPI GdipIsMatrixIdentity(GDIPCONST GpMatrix *matrix, BOOL *result)
420 GpMatrix *e;
421 GpStatus ret;
422 BOOL isIdentity;
424 if(!matrix || !result)
425 return InvalidParameter;
427 ret = GdipCreateMatrix(&e);
428 if(ret != Ok) return ret;
430 ret = GdipIsMatrixEqual(matrix, e, &isIdentity);
431 if(ret == Ok)
432 *result = isIdentity;
434 GdipFree(e);
436 return ret;