dinput: Enumerate user format object forwards.
[wine.git] / dlls / gdiplus / matrix.c
blob40abbc93e21fec63ac2a5575e6d5ea3813393f88
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 = heap_alloc_zero(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;
90 TRACE("(%s, %p, %p)\n", debugstr_rectf(rect), pt, matrix);
92 if(!matrix || !pt)
93 return InvalidParameter;
95 m11 = (pt[1].X - pt[0].X) / rect->Width;
96 m21 = (pt[2].X - pt[0].X) / rect->Height;
97 dx = pt[0].X - m11 * rect->X - m21 * rect->Y;
98 m12 = (pt[1].Y - pt[0].Y) / rect->Width;
99 m22 = (pt[2].Y - pt[0].Y) / rect->Height;
100 dy = pt[0].Y - m12 * rect->X - m22 * rect->Y;
102 return GdipCreateMatrix2(m11, m12, m21, m22, dx, dy, matrix);
105 GpStatus WINGDIPAPI GdipCreateMatrix3I(GDIPCONST GpRect *rect, GDIPCONST GpPoint *pt,
106 GpMatrix **matrix)
108 GpRectF rectF;
109 GpPointF ptF[3];
110 int i;
112 TRACE("(%p, %p, %p)\n", rect, pt, matrix);
114 set_rect(&rectF, rect->X, rect->Y, rect->Width, rect->Height);
116 for (i = 0; i < 3; i++) {
117 ptF[i].X = (REAL)pt[i].X;
118 ptF[i].Y = (REAL)pt[i].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 = heap_alloc_zero(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 = heap_alloc_zero(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 heap_free(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 /* optimize inverting simple scaling and translation matrices */
199 if(matrix->matrix[1] == 0 && matrix->matrix[2] == 0)
201 matrix->matrix[4] = -matrix->matrix[4] / matrix->matrix[0];
202 matrix->matrix[5] = -matrix->matrix[5] / matrix->matrix[3];
203 matrix->matrix[0] = 1 / matrix->matrix[0];
204 matrix->matrix[3] = 1 / matrix->matrix[3];
206 return Ok;
209 det = matrix_det(matrix);
211 copy = *matrix;
212 /* store result */
213 matrix->matrix[0] = copy.matrix[3] / det;
214 matrix->matrix[1] = -copy.matrix[1] / det;
215 matrix->matrix[2] = -copy.matrix[2] / det;
216 matrix->matrix[3] = copy.matrix[0] / det;
217 matrix->matrix[4] = (copy.matrix[2]*copy.matrix[5]-copy.matrix[3]*copy.matrix[4]) / det;
218 matrix->matrix[5] = -(copy.matrix[0]*copy.matrix[5]-copy.matrix[1]*copy.matrix[4]) / det;
220 return Ok;
223 GpStatus WINGDIPAPI GdipIsMatrixInvertible(GDIPCONST GpMatrix *matrix, BOOL *result)
225 TRACE("(%p, %p)\n", matrix, result);
227 if(!matrix || !result)
228 return InvalidParameter;
230 if(matrix->matrix[1] == 0 && matrix->matrix[2] == 0)
231 *result = matrix->matrix[0] != 0 && matrix->matrix[3] != 0;
232 else
233 *result = (fabs(matrix_det(matrix)) >= 1e-5);
235 return Ok;
238 GpStatus WINGDIPAPI GdipMultiplyMatrix(GpMatrix *matrix, GDIPCONST GpMatrix* matrix2,
239 GpMatrixOrder order)
241 TRACE("(%p, %p, %d)\n", matrix, matrix2, order);
243 if(!matrix || !matrix2)
244 return InvalidParameter;
246 if(order == MatrixOrderAppend)
247 matrix_multiply(matrix->matrix, matrix2->matrix, matrix->matrix);
248 else if (order == MatrixOrderPrepend)
249 matrix_multiply(matrix2->matrix, matrix->matrix, matrix->matrix);
250 else
251 return InvalidParameter;
253 return Ok;
256 GpStatus WINGDIPAPI GdipRotateMatrix(GpMatrix *matrix, REAL angle,
257 GpMatrixOrder order)
259 REAL cos_theta, sin_theta, rotate[6];
261 TRACE("(%p, %.2f, %d)\n", matrix, angle, order);
263 if(!matrix)
264 return InvalidParameter;
266 angle = deg2rad(angle);
267 cos_theta = cos(angle);
268 sin_theta = sin(angle);
270 rotate[0] = cos_theta;
271 rotate[1] = sin_theta;
272 rotate[2] = -sin_theta;
273 rotate[3] = cos_theta;
274 rotate[4] = 0.0;
275 rotate[5] = 0.0;
277 if(order == MatrixOrderAppend)
278 matrix_multiply(matrix->matrix, rotate, matrix->matrix);
279 else if (order == MatrixOrderPrepend)
280 matrix_multiply(rotate, matrix->matrix, matrix->matrix);
281 else
282 return InvalidParameter;
284 return Ok;
287 GpStatus WINGDIPAPI GdipScaleMatrix(GpMatrix *matrix, REAL scaleX, REAL scaleY,
288 GpMatrixOrder order)
290 REAL scale[6];
292 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, scaleX, scaleY, order);
294 if(!matrix)
295 return InvalidParameter;
297 scale[0] = scaleX;
298 scale[1] = 0.0;
299 scale[2] = 0.0;
300 scale[3] = scaleY;
301 scale[4] = 0.0;
302 scale[5] = 0.0;
304 if(order == MatrixOrderAppend)
305 matrix_multiply(matrix->matrix, scale, matrix->matrix);
306 else if (order == MatrixOrderPrepend)
307 matrix_multiply(scale, matrix->matrix, matrix->matrix);
308 else
309 return InvalidParameter;
311 return Ok;
314 GpStatus WINGDIPAPI GdipSetMatrixElements(GpMatrix *matrix, REAL m11, REAL m12,
315 REAL m21, REAL m22, REAL dx, REAL dy)
317 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", matrix, m11, m12,
318 m21, m22, dx, dy);
320 if(!matrix)
321 return InvalidParameter;
323 matrix->matrix[0] = m11;
324 matrix->matrix[1] = m12;
325 matrix->matrix[2] = m21;
326 matrix->matrix[3] = m22;
327 matrix->matrix[4] = dx;
328 matrix->matrix[5] = dy;
330 return Ok;
333 GpStatus WINGDIPAPI GdipShearMatrix(GpMatrix *matrix, REAL shearX, REAL shearY,
334 GpMatrixOrder order)
336 REAL shear[6];
338 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, shearX, shearY, order);
340 if(!matrix)
341 return InvalidParameter;
343 /* prepare transformation matrix */
344 shear[0] = 1.0;
345 shear[1] = shearY;
346 shear[2] = shearX;
347 shear[3] = 1.0;
348 shear[4] = 0.0;
349 shear[5] = 0.0;
351 if(order == MatrixOrderAppend)
352 matrix_multiply(matrix->matrix, shear, matrix->matrix);
353 else if (order == MatrixOrderPrepend)
354 matrix_multiply(shear, matrix->matrix, matrix->matrix);
355 else
356 return InvalidParameter;
358 return Ok;
361 GpStatus WINGDIPAPI GdipTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts,
362 INT count)
364 REAL x, y;
365 INT i;
367 TRACE("(%p, %p, %d)\n", matrix, pts, count);
369 if(!matrix || !pts || count <= 0)
370 return InvalidParameter;
372 for(i = 0; i < count; i++)
374 x = pts[i].X;
375 y = pts[i].Y;
377 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2] + matrix->matrix[4];
378 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3] + matrix->matrix[5];
381 return Ok;
384 GpStatus WINGDIPAPI GdipTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
386 GpPointF *ptsF;
387 GpStatus ret;
388 INT i;
390 TRACE("(%p, %p, %d)\n", matrix, pts, count);
392 if(count <= 0)
393 return InvalidParameter;
395 ptsF = heap_alloc_zero(sizeof(GpPointF) * count);
396 if(!ptsF)
397 return OutOfMemory;
399 for(i = 0; i < count; i++){
400 ptsF[i].X = (REAL)pts[i].X;
401 ptsF[i].Y = (REAL)pts[i].Y;
404 ret = GdipTransformMatrixPoints(matrix, ptsF, count);
406 if(ret == Ok)
407 for(i = 0; i < count; i++){
408 pts[i].X = gdip_round(ptsF[i].X);
409 pts[i].Y = gdip_round(ptsF[i].Y);
411 heap_free(ptsF);
413 return ret;
416 GpStatus WINGDIPAPI GdipTranslateMatrix(GpMatrix *matrix, REAL offsetX,
417 REAL offsetY, GpMatrixOrder order)
419 REAL translate[6];
421 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, offsetX, offsetY, order);
423 if(!matrix)
424 return InvalidParameter;
426 translate[0] = 1.0;
427 translate[1] = 0.0;
428 translate[2] = 0.0;
429 translate[3] = 1.0;
430 translate[4] = offsetX;
431 translate[5] = offsetY;
433 if(order == MatrixOrderAppend)
434 matrix_multiply(matrix->matrix, translate, matrix->matrix);
435 else if (order == MatrixOrderPrepend)
436 matrix_multiply(translate, matrix->matrix, matrix->matrix);
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);