gdiplus: Improve performance of GdipTranslateMatrix.
[wine.git] / dlls / gdiplus / matrix.c
blobd69672bddcf04c51cc8dfc387e2878bb66909bfc
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"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
34 /* Multiplies two matrices of the form
36 * idx:0 idx:1 0
37 * idx:2 idx:3 0
38 * idx:4 idx:5 1
40 * and puts the output in out.
41 * */
42 static inline void matrix_multiply(GDIPCONST REAL * left, GDIPCONST REAL * right, REAL * out)
44 REAL temp[6];
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);
64 if(!matrix)
65 return InvalidParameter;
67 *matrix = heap_alloc_zero(sizeof(GpMatrix));
68 if(!*matrix) return OutOfMemory;
70 /* first row */
71 (*matrix)->matrix[0] = m11;
72 (*matrix)->matrix[1] = m12;
73 /* second row */
74 (*matrix)->matrix[2] = m21;
75 (*matrix)->matrix[3] = m22;
76 /* third row */
77 (*matrix)->matrix[4] = dx;
78 (*matrix)->matrix[5] = dy;
80 return Ok;
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);
90 if(!matrix || !pt)
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,
104 GpMatrix **matrix)
106 GpRectF rectF;
107 GpPointF ptF[3];
108 int i;
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;
131 **clone = *matrix;
133 return Ok;
136 GpStatus WINGDIPAPI GdipCreateMatrix(GpMatrix **matrix)
138 TRACE("(%p)\n", matrix);
140 if(!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;
153 return Ok;
156 GpStatus WINGDIPAPI GdipDeleteMatrix(GpMatrix *matrix)
158 TRACE("(%p)\n", matrix);
160 if(!matrix)
161 return InvalidParameter;
163 heap_free(matrix);
165 return Ok;
168 GpStatus WINGDIPAPI GdipGetMatrixElements(GDIPCONST GpMatrix *matrix,
169 REAL *out)
171 TRACE("(%p, %p)\n", matrix, out);
173 if(!matrix || !out)
174 return InvalidParameter;
176 memcpy(out, matrix->matrix, sizeof(matrix->matrix));
178 return Ok;
181 GpStatus WINGDIPAPI GdipInvertMatrix(GpMatrix *matrix)
183 GpMatrix copy;
184 REAL det;
185 BOOL invertible;
187 TRACE("(%p)\n", matrix);
189 if(!matrix)
190 return InvalidParameter;
192 GdipIsMatrixInvertible(matrix, &invertible);
193 if(!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];
204 return Ok;
207 det = matrix_det(matrix);
209 copy = *matrix;
210 /* store result */
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;
218 return Ok;
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;
230 else
231 *result = (fabs(matrix_det(matrix)) >= 1e-5);
233 return Ok;
236 GpStatus WINGDIPAPI GdipMultiplyMatrix(GpMatrix *matrix, GDIPCONST GpMatrix* matrix2,
237 GpMatrixOrder order)
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);
248 else
249 return InvalidParameter;
251 return Ok;
254 GpStatus WINGDIPAPI GdipRotateMatrix(GpMatrix *matrix, REAL angle,
255 GpMatrixOrder order)
257 REAL cos_theta, sin_theta, rotate[6];
259 TRACE("(%p, %.2f, %d)\n", matrix, angle, order);
261 if(!matrix)
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;
272 rotate[4] = 0.0;
273 rotate[5] = 0.0;
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);
279 else
280 return InvalidParameter;
282 return Ok;
285 GpStatus WINGDIPAPI GdipScaleMatrix(GpMatrix *matrix, REAL scaleX, REAL scaleY,
286 GpMatrixOrder order)
288 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, scaleX, scaleY, order);
290 if(!matrix)
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;
309 else
310 return InvalidParameter;
312 return Ok;
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,
319 m21, m22, dx, dy);
321 if(!matrix)
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;
331 return Ok;
334 GpStatus WINGDIPAPI GdipShearMatrix(GpMatrix *matrix, REAL shearX, REAL shearY,
335 GpMatrixOrder order)
337 REAL shear[6];
339 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, shearX, shearY, order);
341 if(!matrix)
342 return InvalidParameter;
344 /* prepare transformation matrix */
345 shear[0] = 1.0;
346 shear[1] = shearY;
347 shear[2] = shearX;
348 shear[3] = 1.0;
349 shear[4] = 0.0;
350 shear[5] = 0.0;
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);
356 else
357 return InvalidParameter;
359 return Ok;
362 GpStatus WINGDIPAPI GdipTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts,
363 INT count)
365 REAL x, y;
366 INT i;
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++)
375 x = pts[i].X;
376 y = pts[i].Y;
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];
382 return Ok;
385 GpStatus WINGDIPAPI GdipTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
387 GpPointF *ptsF;
388 GpStatus ret;
389 INT i;
391 TRACE("(%p, %p, %d)\n", matrix, pts, count);
393 if(count <= 0)
394 return InvalidParameter;
396 ptsF = heap_alloc_zero(sizeof(GpPointF) * count);
397 if(!ptsF)
398 return OutOfMemory;
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);
407 if(ret == Ok)
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);
412 heap_free(ptsF);
414 return ret;
417 GpStatus WINGDIPAPI GdipTranslateMatrix(GpMatrix *matrix, REAL offsetX,
418 REAL offsetY, GpMatrixOrder order)
420 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, offsetX, offsetY, order);
422 if(!matrix)
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]
433 + matrix->matrix[4];
434 matrix->matrix[5] = offsetX * matrix->matrix[1] + offsetY * matrix->matrix[3]
435 + matrix->matrix[5];
437 else
438 return InvalidParameter;
440 return Ok;
443 GpStatus WINGDIPAPI GdipVectorTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts, INT count)
445 REAL x, y;
446 INT i;
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++)
455 x = pts[i].X;
456 y = pts[i].Y;
458 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2];
459 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3];
462 return Ok;
465 GpStatus WINGDIPAPI GdipVectorTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
467 GpPointF *ptsF;
468 GpStatus ret;
469 INT i;
471 TRACE("(%p, %p, %d)\n", matrix, pts, count);
473 if(count <= 0)
474 return InvalidParameter;
476 ptsF = heap_alloc_zero(sizeof(GpPointF) * count);
477 if(!ptsF)
478 return OutOfMemory;
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);
486 /* store back */
487 if(ret == Ok)
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);
492 heap_free(ptsF);
494 return ret;
497 GpStatus WINGDIPAPI GdipIsMatrixEqual(GDIPCONST GpMatrix *matrix, GDIPCONST GpMatrix *matrix2,
498 BOOL *result)
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);
507 return Ok;
510 GpStatus WINGDIPAPI GdipIsMatrixIdentity(GDIPCONST GpMatrix *matrix, BOOL *result)
512 static const GpMatrix identity =
514 { 1.0, 0.0,
515 0.0, 1.0,
516 0.0, 0.0 }
519 TRACE("(%p, %p)\n", matrix, result);
521 if(!matrix || !result)
522 return InvalidParameter;
524 return GdipIsMatrixEqual(matrix, &identity, result);