po: Fix a mistake in Dutch translation.
[wine.git] / dlls / gdiplus / matrix.c
blobfe03e65c0ebc1b4749d14899c5adbcc7c15d30be
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 void matrix_multiply(GDIPCONST REAL * left, GDIPCONST REAL * right, REAL * out)
44 REAL temp[6];
45 int i, odd;
47 for(i = 0; i < 6; i++){
48 odd = i % 2;
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);
66 if(!matrix)
67 return InvalidParameter;
69 *matrix = GdipAlloc(sizeof(GpMatrix));
70 if(!*matrix) return OutOfMemory;
72 /* first row */
73 (*matrix)->matrix[0] = m11;
74 (*matrix)->matrix[1] = m12;
75 /* second row */
76 (*matrix)->matrix[2] = m21;
77 (*matrix)->matrix[3] = m22;
78 /* third row */
79 (*matrix)->matrix[4] = dx;
80 (*matrix)->matrix[5] = dy;
82 return Ok;
85 GpStatus WINGDIPAPI GdipCreateMatrix3(GDIPCONST GpRectF *rect,
86 GDIPCONST GpPointF *pt, GpMatrix **matrix)
88 REAL m11, m12, m21, m22, dx, dy;
89 TRACE("(%p, %p, %p)\n", rect, pt, matrix);
91 if(!matrix || !pt)
92 return InvalidParameter;
94 m11 = (pt[1].X - pt[0].X) / rect->Width;
95 m21 = (pt[2].X - pt[0].X) / rect->Height;
96 dx = pt[0].X - m11 * rect->X - m21 * rect->Y;
97 m12 = (pt[1].Y - pt[0].Y) / rect->Width;
98 m22 = (pt[2].Y - pt[0].Y) / rect->Height;
99 dy = pt[0].Y - m12 * rect->X - m22 * rect->Y;
101 return GdipCreateMatrix2(m11, m12, m21, m22, dx, dy, matrix);
104 GpStatus WINGDIPAPI GdipCreateMatrix3I(GDIPCONST GpRect *rect, GDIPCONST GpPoint *pt,
105 GpMatrix **matrix)
107 GpRectF rectF;
108 GpPointF ptF[3];
109 int i;
111 TRACE("(%p, %p, %p)\n", rect, pt, matrix);
113 rectF.X = (REAL)rect->X;
114 rectF.Y = (REAL)rect->Y;
115 rectF.Width = (REAL)rect->Width;
116 rectF.Height = (REAL)rect->Height;
118 for (i = 0; i < 3; i++) {
119 ptF[i].X = (REAL)pt[i].X;
120 ptF[i].Y = (REAL)pt[i].Y;
122 return GdipCreateMatrix3(&rectF, ptF, matrix);
125 GpStatus WINGDIPAPI GdipCloneMatrix(GpMatrix *matrix, GpMatrix **clone)
127 TRACE("(%p, %p)\n", matrix, clone);
129 if(!matrix || !clone)
130 return InvalidParameter;
132 *clone = GdipAlloc(sizeof(GpMatrix));
133 if(!*clone) return OutOfMemory;
135 **clone = *matrix;
137 return Ok;
140 GpStatus WINGDIPAPI GdipCreateMatrix(GpMatrix **matrix)
142 TRACE("(%p)\n", matrix);
144 if(!matrix)
145 return InvalidParameter;
147 *matrix = GdipAlloc(sizeof(GpMatrix));
148 if(!*matrix) return OutOfMemory;
150 (*matrix)->matrix[0] = 1.0;
151 (*matrix)->matrix[1] = 0.0;
152 (*matrix)->matrix[2] = 0.0;
153 (*matrix)->matrix[3] = 1.0;
154 (*matrix)->matrix[4] = 0.0;
155 (*matrix)->matrix[5] = 0.0;
157 return Ok;
160 GpStatus WINGDIPAPI GdipDeleteMatrix(GpMatrix *matrix)
162 TRACE("(%p)\n", matrix);
164 if(!matrix)
165 return InvalidParameter;
167 GdipFree(matrix);
169 return Ok;
172 GpStatus WINGDIPAPI GdipGetMatrixElements(GDIPCONST GpMatrix *matrix,
173 REAL *out)
175 TRACE("(%p, %p)\n", matrix, out);
177 if(!matrix || !out)
178 return InvalidParameter;
180 memcpy(out, matrix->matrix, sizeof(matrix->matrix));
182 return Ok;
185 GpStatus WINGDIPAPI GdipInvertMatrix(GpMatrix *matrix)
187 GpMatrix copy;
188 REAL det;
189 BOOL invertible;
191 TRACE("(%p)\n", matrix);
193 if(!matrix)
194 return InvalidParameter;
196 GdipIsMatrixInvertible(matrix, &invertible);
197 if(!invertible)
198 return InvalidParameter;
200 det = matrix_det(matrix);
202 copy = *matrix;
203 /* store result */
204 matrix->matrix[0] = copy.matrix[3] / det;
205 matrix->matrix[1] = -copy.matrix[1] / det;
206 matrix->matrix[2] = -copy.matrix[2] / det;
207 matrix->matrix[3] = copy.matrix[0] / det;
208 matrix->matrix[4] = (copy.matrix[2]*copy.matrix[5]-copy.matrix[3]*copy.matrix[4]) / det;
209 matrix->matrix[5] = -(copy.matrix[0]*copy.matrix[5]-copy.matrix[1]*copy.matrix[4]) / det;
211 return Ok;
214 GpStatus WINGDIPAPI GdipIsMatrixInvertible(GDIPCONST GpMatrix *matrix, BOOL *result)
216 TRACE("(%p, %p)\n", matrix, result);
218 if(!matrix || !result)
219 return InvalidParameter;
221 *result = (fabs(matrix_det(matrix)) >= 1e-5);
223 return Ok;
226 GpStatus WINGDIPAPI GdipMultiplyMatrix(GpMatrix *matrix, GDIPCONST GpMatrix* matrix2,
227 GpMatrixOrder order)
229 TRACE("(%p, %p, %d)\n", matrix, matrix2, order);
231 if(!matrix || !matrix2)
232 return InvalidParameter;
234 if(order == MatrixOrderAppend)
235 matrix_multiply(matrix->matrix, matrix2->matrix, matrix->matrix);
236 else if (order == MatrixOrderPrepend)
237 matrix_multiply(matrix2->matrix, matrix->matrix, matrix->matrix);
238 else
239 return InvalidParameter;
241 return Ok;
244 GpStatus WINGDIPAPI GdipRotateMatrix(GpMatrix *matrix, REAL angle,
245 GpMatrixOrder order)
247 REAL cos_theta, sin_theta, rotate[6];
249 TRACE("(%p, %.2f, %d)\n", matrix, angle, order);
251 if(!matrix)
252 return InvalidParameter;
254 angle = deg2rad(angle);
255 cos_theta = cos(angle);
256 sin_theta = sin(angle);
258 rotate[0] = cos_theta;
259 rotate[1] = sin_theta;
260 rotate[2] = -sin_theta;
261 rotate[3] = cos_theta;
262 rotate[4] = 0.0;
263 rotate[5] = 0.0;
265 if(order == MatrixOrderAppend)
266 matrix_multiply(matrix->matrix, rotate, matrix->matrix);
267 else if (order == MatrixOrderPrepend)
268 matrix_multiply(rotate, matrix->matrix, matrix->matrix);
269 else
270 return InvalidParameter;
272 return Ok;
275 GpStatus WINGDIPAPI GdipScaleMatrix(GpMatrix *matrix, REAL scaleX, REAL scaleY,
276 GpMatrixOrder order)
278 REAL scale[6];
280 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, scaleX, scaleY, order);
282 if(!matrix)
283 return InvalidParameter;
285 scale[0] = scaleX;
286 scale[1] = 0.0;
287 scale[2] = 0.0;
288 scale[3] = scaleY;
289 scale[4] = 0.0;
290 scale[5] = 0.0;
292 if(order == MatrixOrderAppend)
293 matrix_multiply(matrix->matrix, scale, matrix->matrix);
294 else if (order == MatrixOrderPrepend)
295 matrix_multiply(scale, matrix->matrix, matrix->matrix);
296 else
297 return InvalidParameter;
299 return Ok;
302 GpStatus WINGDIPAPI GdipSetMatrixElements(GpMatrix *matrix, REAL m11, REAL m12,
303 REAL m21, REAL m22, REAL dx, REAL dy)
305 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", matrix, m11, m12,
306 m21, m22, dx, dy);
308 if(!matrix)
309 return InvalidParameter;
311 matrix->matrix[0] = m11;
312 matrix->matrix[1] = m12;
313 matrix->matrix[2] = m21;
314 matrix->matrix[3] = m22;
315 matrix->matrix[4] = dx;
316 matrix->matrix[5] = dy;
318 return Ok;
321 GpStatus WINGDIPAPI GdipShearMatrix(GpMatrix *matrix, REAL shearX, REAL shearY,
322 GpMatrixOrder order)
324 REAL shear[6];
326 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, shearX, shearY, order);
328 if(!matrix)
329 return InvalidParameter;
331 /* prepare transformation matrix */
332 shear[0] = 1.0;
333 shear[1] = shearY;
334 shear[2] = shearX;
335 shear[3] = 1.0;
336 shear[4] = 0.0;
337 shear[5] = 0.0;
339 if(order == MatrixOrderAppend)
340 matrix_multiply(matrix->matrix, shear, matrix->matrix);
341 else if (order == MatrixOrderPrepend)
342 matrix_multiply(shear, matrix->matrix, matrix->matrix);
343 else
344 return InvalidParameter;
346 return Ok;
349 GpStatus WINGDIPAPI GdipTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts,
350 INT count)
352 REAL x, y;
353 INT i;
355 TRACE("(%p, %p, %d)\n", matrix, pts, count);
357 if(!matrix || !pts || count <= 0)
358 return InvalidParameter;
360 for(i = 0; i < count; i++)
362 x = pts[i].X;
363 y = pts[i].Y;
365 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2] + matrix->matrix[4];
366 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3] + matrix->matrix[5];
369 return Ok;
372 GpStatus WINGDIPAPI GdipTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
374 GpPointF *ptsF;
375 GpStatus ret;
376 INT i;
378 TRACE("(%p, %p, %d)\n", matrix, pts, count);
380 if(count <= 0)
381 return InvalidParameter;
383 ptsF = GdipAlloc(sizeof(GpPointF) * count);
384 if(!ptsF)
385 return OutOfMemory;
387 for(i = 0; i < count; i++){
388 ptsF[i].X = (REAL)pts[i].X;
389 ptsF[i].Y = (REAL)pts[i].Y;
392 ret = GdipTransformMatrixPoints(matrix, ptsF, count);
394 if(ret == Ok)
395 for(i = 0; i < count; i++){
396 pts[i].X = gdip_round(ptsF[i].X);
397 pts[i].Y = gdip_round(ptsF[i].Y);
399 GdipFree(ptsF);
401 return ret;
404 GpStatus WINGDIPAPI GdipTranslateMatrix(GpMatrix *matrix, REAL offsetX,
405 REAL offsetY, GpMatrixOrder order)
407 REAL translate[6];
409 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, offsetX, offsetY, order);
411 if(!matrix)
412 return InvalidParameter;
414 translate[0] = 1.0;
415 translate[1] = 0.0;
416 translate[2] = 0.0;
417 translate[3] = 1.0;
418 translate[4] = offsetX;
419 translate[5] = offsetY;
421 if(order == MatrixOrderAppend)
422 matrix_multiply(matrix->matrix, translate, matrix->matrix);
423 else if (order == MatrixOrderPrepend)
424 matrix_multiply(translate, matrix->matrix, matrix->matrix);
425 else
426 return InvalidParameter;
428 return Ok;
431 GpStatus WINGDIPAPI GdipVectorTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts, INT count)
433 REAL x, y;
434 INT i;
436 TRACE("(%p, %p, %d)\n", matrix, pts, count);
438 if(!matrix || !pts || count <= 0)
439 return InvalidParameter;
441 for(i = 0; i < count; i++)
443 x = pts[i].X;
444 y = pts[i].Y;
446 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2];
447 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3];
450 return Ok;
453 GpStatus WINGDIPAPI GdipVectorTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
455 GpPointF *ptsF;
456 GpStatus ret;
457 INT i;
459 TRACE("(%p, %p, %d)\n", matrix, pts, count);
461 if(count <= 0)
462 return InvalidParameter;
464 ptsF = GdipAlloc(sizeof(GpPointF) * count);
465 if(!ptsF)
466 return OutOfMemory;
468 for(i = 0; i < count; i++){
469 ptsF[i].X = (REAL)pts[i].X;
470 ptsF[i].Y = (REAL)pts[i].Y;
473 ret = GdipVectorTransformMatrixPoints(matrix, ptsF, count);
474 /* store back */
475 if(ret == Ok)
476 for(i = 0; i < count; i++){
477 pts[i].X = gdip_round(ptsF[i].X);
478 pts[i].Y = gdip_round(ptsF[i].Y);
480 GdipFree(ptsF);
482 return ret;
485 GpStatus WINGDIPAPI GdipIsMatrixEqual(GDIPCONST GpMatrix *matrix, GDIPCONST GpMatrix *matrix2,
486 BOOL *result)
488 TRACE("(%p, %p, %p)\n", matrix, matrix2, result);
490 if(!matrix || !matrix2 || !result)
491 return InvalidParameter;
492 /* based on single array member of GpMatrix */
493 *result = (memcmp(matrix->matrix, matrix2->matrix, sizeof(GpMatrix)) == 0);
495 return Ok;
498 GpStatus WINGDIPAPI GdipIsMatrixIdentity(GDIPCONST GpMatrix *matrix, BOOL *result)
500 GpMatrix *e;
501 GpStatus ret;
502 BOOL isIdentity;
504 TRACE("(%p, %p)\n", matrix, result);
506 if(!matrix || !result)
507 return InvalidParameter;
509 ret = GdipCreateMatrix(&e);
510 if(ret != Ok) return ret;
512 ret = GdipIsMatrixEqual(matrix, e, &isIdentity);
513 if(ret == Ok)
514 *result = isIdentity;
516 GdipFree(e);
518 return ret;