push 337eb2e2d902d84a5d689451984c5832d7e04fc4
[wine/hacks.git] / dlls / gdiplus / matrix.c
blob2176da6cbfe72ed7885f1fff4acc4cb037101b91
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 TRACE("(%p, %p, %p)\n", rect, pt, matrix);
90 if(!matrix)
91 return InvalidParameter;
93 *matrix = GdipAlloc(sizeof(GpMatrix));
94 if(!*matrix) return OutOfMemory;
96 memcpy((*matrix)->matrix, rect, 4 * sizeof(REAL));
98 (*matrix)->matrix[4] = pt->X;
99 (*matrix)->matrix[5] = pt->Y;
101 return Ok;
104 GpStatus WINGDIPAPI GdipCreateMatrix3I(GDIPCONST GpRect *rect, GDIPCONST GpPoint *pt,
105 GpMatrix **matrix)
107 GpRectF rectF;
108 GpPointF ptF;
110 TRACE("(%p, %p, %p)\n", rect, pt, matrix);
112 rectF.X = (REAL)rect->X;
113 rectF.Y = (REAL)rect->Y;
114 rectF.Width = (REAL)rect->Width;
115 rectF.Height = (REAL)rect->Height;
117 ptF.X = (REAL)pt->X;
118 ptF.Y = (REAL)pt->Y;
120 return GdipCreateMatrix3(&rectF, &ptF, matrix);
123 GpStatus WINGDIPAPI GdipCloneMatrix(GpMatrix *matrix, GpMatrix **clone)
125 TRACE("(%p, %p)\n", matrix, clone);
127 if(!matrix || !clone)
128 return InvalidParameter;
130 *clone = GdipAlloc(sizeof(GpMatrix));
131 if(!*clone) return OutOfMemory;
133 **clone = *matrix;
135 return Ok;
138 GpStatus WINGDIPAPI GdipCreateMatrix(GpMatrix **matrix)
140 TRACE("(%p)\n", matrix);
142 if(!matrix)
143 return InvalidParameter;
145 *matrix = GdipAlloc(sizeof(GpMatrix));
146 if(!*matrix) return OutOfMemory;
148 (*matrix)->matrix[0] = 1.0;
149 (*matrix)->matrix[1] = 0.0;
150 (*matrix)->matrix[2] = 0.0;
151 (*matrix)->matrix[3] = 1.0;
152 (*matrix)->matrix[4] = 0.0;
153 (*matrix)->matrix[5] = 0.0;
155 return Ok;
158 GpStatus WINGDIPAPI GdipDeleteMatrix(GpMatrix *matrix)
160 TRACE("(%p)\n", matrix);
162 if(!matrix)
163 return InvalidParameter;
165 GdipFree(matrix);
167 return Ok;
170 GpStatus WINGDIPAPI GdipGetMatrixElements(GDIPCONST GpMatrix *matrix,
171 REAL *out)
173 TRACE("(%p, %p)\n", matrix, out);
175 if(!matrix || !out)
176 return InvalidParameter;
178 memcpy(out, matrix->matrix, sizeof(matrix->matrix));
180 return Ok;
183 GpStatus WINGDIPAPI GdipInvertMatrix(GpMatrix *matrix)
185 GpMatrix copy;
186 REAL det;
187 BOOL invertible;
189 TRACE("(%p)\n", matrix);
191 if(!matrix)
192 return InvalidParameter;
194 GdipIsMatrixInvertible(matrix, &invertible);
195 if(!invertible)
196 return InvalidParameter;
198 det = matrix_det(matrix);
200 copy = *matrix;
201 /* store result */
202 matrix->matrix[0] = copy.matrix[3] / det;
203 matrix->matrix[1] = -copy.matrix[1] / det;
204 matrix->matrix[2] = -copy.matrix[2] / det;
205 matrix->matrix[3] = copy.matrix[0] / det;
206 matrix->matrix[4] = (copy.matrix[2]*copy.matrix[5]-copy.matrix[3]*copy.matrix[4]) / det;
207 matrix->matrix[5] = -(copy.matrix[0]*copy.matrix[5]-copy.matrix[1]*copy.matrix[4]) / det;
209 return Ok;
212 GpStatus WINGDIPAPI GdipIsMatrixInvertible(GDIPCONST GpMatrix *matrix, BOOL *result)
214 TRACE("(%p, %p)\n", matrix, result);
216 if(!matrix || !result)
217 return InvalidParameter;
219 *result = (fabs(matrix_det(matrix)) >= 1e-5);
221 return Ok;
224 GpStatus WINGDIPAPI GdipMultiplyMatrix(GpMatrix *matrix, GDIPCONST GpMatrix* matrix2,
225 GpMatrixOrder order)
227 TRACE("(%p, %p, %d)\n", matrix, matrix2, order);
229 if(!matrix || !matrix2)
230 return InvalidParameter;
232 if(order == MatrixOrderAppend)
233 matrix_multiply(matrix->matrix, matrix2->matrix, matrix->matrix);
234 else
235 matrix_multiply(matrix2->matrix, matrix->matrix, matrix->matrix);
237 return Ok;
240 GpStatus WINGDIPAPI GdipRotateMatrix(GpMatrix *matrix, REAL angle,
241 GpMatrixOrder order)
243 REAL cos_theta, sin_theta, rotate[6];
245 TRACE("(%p, %.2f, %d)\n", matrix, angle, order);
247 if(!matrix)
248 return InvalidParameter;
250 angle = deg2rad(angle);
251 cos_theta = cos(angle);
252 sin_theta = sin(angle);
254 rotate[0] = cos_theta;
255 rotate[1] = sin_theta;
256 rotate[2] = -sin_theta;
257 rotate[3] = cos_theta;
258 rotate[4] = 0.0;
259 rotate[5] = 0.0;
261 if(order == MatrixOrderAppend)
262 matrix_multiply(matrix->matrix, rotate, matrix->matrix);
263 else
264 matrix_multiply(rotate, matrix->matrix, matrix->matrix);
266 return Ok;
269 GpStatus WINGDIPAPI GdipScaleMatrix(GpMatrix *matrix, REAL scaleX, REAL scaleY,
270 GpMatrixOrder order)
272 REAL scale[6];
274 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, scaleX, scaleY, order);
276 if(!matrix)
277 return InvalidParameter;
279 scale[0] = scaleX;
280 scale[1] = 0.0;
281 scale[2] = 0.0;
282 scale[3] = scaleY;
283 scale[4] = 0.0;
284 scale[5] = 0.0;
286 if(order == MatrixOrderAppend)
287 matrix_multiply(matrix->matrix, scale, matrix->matrix);
288 else
289 matrix_multiply(scale, matrix->matrix, matrix->matrix);
291 return Ok;
294 GpStatus WINGDIPAPI GdipSetMatrixElements(GpMatrix *matrix, REAL m11, REAL m12,
295 REAL m21, REAL m22, REAL dx, REAL dy)
297 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", matrix, m11, m12,
298 m21, m22, dx, dy);
300 if(!matrix)
301 return InvalidParameter;
303 matrix->matrix[0] = m11;
304 matrix->matrix[1] = m12;
305 matrix->matrix[2] = m21;
306 matrix->matrix[3] = m22;
307 matrix->matrix[4] = dx;
308 matrix->matrix[5] = dy;
310 return Ok;
313 GpStatus WINGDIPAPI GdipShearMatrix(GpMatrix *matrix, REAL shearX, REAL shearY,
314 GpMatrixOrder order)
316 REAL shear[6];
318 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, shearX, shearY, order);
320 if(!matrix)
321 return InvalidParameter;
323 /* prepare transformation matrix */
324 shear[0] = 1.0;
325 shear[1] = shearY;
326 shear[2] = shearX;
327 shear[3] = 1.0;
328 shear[4] = 0.0;
329 shear[5] = 0.0;
331 if(order == MatrixOrderAppend)
332 matrix_multiply(matrix->matrix, shear, matrix->matrix);
333 else
334 matrix_multiply(shear, matrix->matrix, matrix->matrix);
336 return Ok;
339 GpStatus WINGDIPAPI GdipTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts,
340 INT count)
342 REAL x, y;
343 INT i;
345 TRACE("(%p, %p, %d)\n", matrix, pts, count);
347 if(!matrix || !pts || count <= 0)
348 return InvalidParameter;
350 for(i = 0; i < count; i++)
352 x = pts[i].X;
353 y = pts[i].Y;
355 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2] + matrix->matrix[4];
356 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3] + matrix->matrix[5];
359 return Ok;
362 GpStatus WINGDIPAPI GdipTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
364 GpPointF *ptsF;
365 GpStatus ret;
366 INT i;
368 TRACE("(%p, %p, %d)\n", matrix, pts, count);
370 if(count <= 0)
371 return InvalidParameter;
373 ptsF = GdipAlloc(sizeof(GpPointF) * count);
374 if(!ptsF)
375 return OutOfMemory;
377 for(i = 0; i < count; i++){
378 ptsF[i].X = (REAL)pts[i].X;
379 ptsF[i].Y = (REAL)pts[i].Y;
382 ret = GdipTransformMatrixPoints(matrix, ptsF, count);
384 if(ret == Ok)
385 for(i = 0; i < count; i++){
386 pts[i].X = roundr(ptsF[i].X);
387 pts[i].Y = roundr(ptsF[i].Y);
389 GdipFree(ptsF);
391 return ret;
394 GpStatus WINGDIPAPI GdipTranslateMatrix(GpMatrix *matrix, REAL offsetX,
395 REAL offsetY, GpMatrixOrder order)
397 REAL translate[6];
399 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, offsetX, offsetY, order);
401 if(!matrix)
402 return InvalidParameter;
404 translate[0] = 1.0;
405 translate[1] = 0.0;
406 translate[2] = 0.0;
407 translate[3] = 1.0;
408 translate[4] = offsetX;
409 translate[5] = offsetY;
411 if(order == MatrixOrderAppend)
412 matrix_multiply(matrix->matrix, translate, matrix->matrix);
413 else
414 matrix_multiply(translate, matrix->matrix, matrix->matrix);
416 return Ok;
419 GpStatus WINGDIPAPI GdipVectorTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts, INT count)
421 REAL x, y;
422 INT i;
424 TRACE("(%p, %p, %d)\n", matrix, pts, count);
426 if(!matrix || !pts || count <= 0)
427 return InvalidParameter;
429 for(i = 0; i < count; i++)
431 x = pts[i].X;
432 y = pts[i].Y;
434 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2];
435 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3];
438 return Ok;
441 GpStatus WINGDIPAPI GdipVectorTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
443 GpPointF *ptsF;
444 GpStatus ret;
445 INT i;
447 TRACE("(%p, %p, %d)\n", matrix, pts, count);
449 if(count <= 0)
450 return InvalidParameter;
452 ptsF = GdipAlloc(sizeof(GpPointF) * count);
453 if(!ptsF)
454 return OutOfMemory;
456 for(i = 0; i < count; i++){
457 ptsF[i].X = (REAL)pts[i].X;
458 ptsF[i].Y = (REAL)pts[i].Y;
461 ret = GdipVectorTransformMatrixPoints(matrix, ptsF, count);
462 /* store back */
463 if(ret == Ok)
464 for(i = 0; i < count; i++){
465 pts[i].X = roundr(ptsF[i].X);
466 pts[i].Y = roundr(ptsF[i].Y);
468 GdipFree(ptsF);
470 return ret;
473 GpStatus WINGDIPAPI GdipIsMatrixEqual(GDIPCONST GpMatrix *matrix, GDIPCONST GpMatrix *matrix2,
474 BOOL *result)
476 TRACE("(%p, %p, %p)\n", matrix, matrix2, result);
478 if(!matrix || !matrix2 || !result)
479 return InvalidParameter;
480 /* based on single array member of GpMatrix */
481 *result = (memcmp(matrix->matrix, matrix2->matrix, sizeof(GpMatrix)) == 0);
483 return Ok;
486 GpStatus WINGDIPAPI GdipIsMatrixIdentity(GDIPCONST GpMatrix *matrix, BOOL *result)
488 GpMatrix *e;
489 GpStatus ret;
490 BOOL isIdentity;
492 TRACE("(%p, %p)\n", matrix, result);
494 if(!matrix || !result)
495 return InvalidParameter;
497 ret = GdipCreateMatrix(&e);
498 if(ret != Ok) return ret;
500 ret = GdipIsMatrixEqual(matrix, e, &isIdentity);
501 if(ret == Ok)
502 *result = isIdentity;
504 GdipFree(e);
506 return ret;