ole32: Use wide-char string literals.
[wine.git] / dlls / gdiplus / matrix.c
blob5d8cf8900790913a59be237d45f917fbaf440566
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 rectF.X = (REAL)rect->X;
115 rectF.Y = (REAL)rect->Y;
116 rectF.Width = (REAL)rect->Width;
117 rectF.Height = (REAL)rect->Height;
119 for (i = 0; i < 3; i++) {
120 ptF[i].X = (REAL)pt[i].X;
121 ptF[i].Y = (REAL)pt[i].Y;
123 return GdipCreateMatrix3(&rectF, ptF, matrix);
126 GpStatus WINGDIPAPI GdipCloneMatrix(GpMatrix *matrix, GpMatrix **clone)
128 TRACE("(%p, %p)\n", matrix, clone);
130 if(!matrix || !clone)
131 return InvalidParameter;
133 *clone = heap_alloc_zero(sizeof(GpMatrix));
134 if(!*clone) return OutOfMemory;
136 **clone = *matrix;
138 return Ok;
141 GpStatus WINGDIPAPI GdipCreateMatrix(GpMatrix **matrix)
143 TRACE("(%p)\n", matrix);
145 if(!matrix)
146 return InvalidParameter;
148 *matrix = heap_alloc_zero(sizeof(GpMatrix));
149 if(!*matrix) return OutOfMemory;
151 (*matrix)->matrix[0] = 1.0;
152 (*matrix)->matrix[1] = 0.0;
153 (*matrix)->matrix[2] = 0.0;
154 (*matrix)->matrix[3] = 1.0;
155 (*matrix)->matrix[4] = 0.0;
156 (*matrix)->matrix[5] = 0.0;
158 return Ok;
161 GpStatus WINGDIPAPI GdipDeleteMatrix(GpMatrix *matrix)
163 TRACE("(%p)\n", matrix);
165 if(!matrix)
166 return InvalidParameter;
168 heap_free(matrix);
170 return Ok;
173 GpStatus WINGDIPAPI GdipGetMatrixElements(GDIPCONST GpMatrix *matrix,
174 REAL *out)
176 TRACE("(%p, %p)\n", matrix, out);
178 if(!matrix || !out)
179 return InvalidParameter;
181 memcpy(out, matrix->matrix, sizeof(matrix->matrix));
183 return Ok;
186 GpStatus WINGDIPAPI GdipInvertMatrix(GpMatrix *matrix)
188 GpMatrix copy;
189 REAL det;
190 BOOL invertible;
192 TRACE("(%p)\n", matrix);
194 if(!matrix)
195 return InvalidParameter;
197 GdipIsMatrixInvertible(matrix, &invertible);
198 if(!invertible)
199 return InvalidParameter;
201 /* optimize inverting simple scaling and translation matrices */
202 if(matrix->matrix[1] == 0 && matrix->matrix[2] == 0)
204 matrix->matrix[4] = -matrix->matrix[4] / matrix->matrix[0];
205 matrix->matrix[5] = -matrix->matrix[5] / matrix->matrix[3];
206 matrix->matrix[0] = 1 / matrix->matrix[0];
207 matrix->matrix[3] = 1 / matrix->matrix[3];
209 return Ok;
212 det = matrix_det(matrix);
214 copy = *matrix;
215 /* store result */
216 matrix->matrix[0] = copy.matrix[3] / det;
217 matrix->matrix[1] = -copy.matrix[1] / det;
218 matrix->matrix[2] = -copy.matrix[2] / det;
219 matrix->matrix[3] = copy.matrix[0] / det;
220 matrix->matrix[4] = (copy.matrix[2]*copy.matrix[5]-copy.matrix[3]*copy.matrix[4]) / det;
221 matrix->matrix[5] = -(copy.matrix[0]*copy.matrix[5]-copy.matrix[1]*copy.matrix[4]) / det;
223 return Ok;
226 GpStatus WINGDIPAPI GdipIsMatrixInvertible(GDIPCONST GpMatrix *matrix, BOOL *result)
228 TRACE("(%p, %p)\n", matrix, result);
230 if(!matrix || !result)
231 return InvalidParameter;
233 if(matrix->matrix[1] == 0 && matrix->matrix[2] == 0)
234 *result = matrix->matrix[0] != 0 && matrix->matrix[3] != 0;
235 else
236 *result = (fabs(matrix_det(matrix)) >= 1e-5);
238 return Ok;
241 GpStatus WINGDIPAPI GdipMultiplyMatrix(GpMatrix *matrix, GDIPCONST GpMatrix* matrix2,
242 GpMatrixOrder order)
244 TRACE("(%p, %p, %d)\n", matrix, matrix2, order);
246 if(!matrix || !matrix2)
247 return InvalidParameter;
249 if(order == MatrixOrderAppend)
250 matrix_multiply(matrix->matrix, matrix2->matrix, matrix->matrix);
251 else if (order == MatrixOrderPrepend)
252 matrix_multiply(matrix2->matrix, matrix->matrix, matrix->matrix);
253 else
254 return InvalidParameter;
256 return Ok;
259 GpStatus WINGDIPAPI GdipRotateMatrix(GpMatrix *matrix, REAL angle,
260 GpMatrixOrder order)
262 REAL cos_theta, sin_theta, rotate[6];
264 TRACE("(%p, %.2f, %d)\n", matrix, angle, order);
266 if(!matrix)
267 return InvalidParameter;
269 angle = deg2rad(angle);
270 cos_theta = cos(angle);
271 sin_theta = sin(angle);
273 rotate[0] = cos_theta;
274 rotate[1] = sin_theta;
275 rotate[2] = -sin_theta;
276 rotate[3] = cos_theta;
277 rotate[4] = 0.0;
278 rotate[5] = 0.0;
280 if(order == MatrixOrderAppend)
281 matrix_multiply(matrix->matrix, rotate, matrix->matrix);
282 else if (order == MatrixOrderPrepend)
283 matrix_multiply(rotate, matrix->matrix, matrix->matrix);
284 else
285 return InvalidParameter;
287 return Ok;
290 GpStatus WINGDIPAPI GdipScaleMatrix(GpMatrix *matrix, REAL scaleX, REAL scaleY,
291 GpMatrixOrder order)
293 REAL scale[6];
295 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, scaleX, scaleY, order);
297 if(!matrix)
298 return InvalidParameter;
300 scale[0] = scaleX;
301 scale[1] = 0.0;
302 scale[2] = 0.0;
303 scale[3] = scaleY;
304 scale[4] = 0.0;
305 scale[5] = 0.0;
307 if(order == MatrixOrderAppend)
308 matrix_multiply(matrix->matrix, scale, matrix->matrix);
309 else if (order == MatrixOrderPrepend)
310 matrix_multiply(scale, matrix->matrix, matrix->matrix);
311 else
312 return InvalidParameter;
314 return Ok;
317 GpStatus WINGDIPAPI GdipSetMatrixElements(GpMatrix *matrix, REAL m11, REAL m12,
318 REAL m21, REAL m22, REAL dx, REAL dy)
320 TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", matrix, m11, m12,
321 m21, m22, dx, dy);
323 if(!matrix)
324 return InvalidParameter;
326 matrix->matrix[0] = m11;
327 matrix->matrix[1] = m12;
328 matrix->matrix[2] = m21;
329 matrix->matrix[3] = m22;
330 matrix->matrix[4] = dx;
331 matrix->matrix[5] = dy;
333 return Ok;
336 GpStatus WINGDIPAPI GdipShearMatrix(GpMatrix *matrix, REAL shearX, REAL shearY,
337 GpMatrixOrder order)
339 REAL shear[6];
341 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, shearX, shearY, order);
343 if(!matrix)
344 return InvalidParameter;
346 /* prepare transformation matrix */
347 shear[0] = 1.0;
348 shear[1] = shearY;
349 shear[2] = shearX;
350 shear[3] = 1.0;
351 shear[4] = 0.0;
352 shear[5] = 0.0;
354 if(order == MatrixOrderAppend)
355 matrix_multiply(matrix->matrix, shear, matrix->matrix);
356 else if (order == MatrixOrderPrepend)
357 matrix_multiply(shear, matrix->matrix, matrix->matrix);
358 else
359 return InvalidParameter;
361 return Ok;
364 GpStatus WINGDIPAPI GdipTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts,
365 INT count)
367 REAL x, y;
368 INT i;
370 TRACE("(%p, %p, %d)\n", matrix, pts, count);
372 if(!matrix || !pts || count <= 0)
373 return InvalidParameter;
375 for(i = 0; i < count; i++)
377 x = pts[i].X;
378 y = pts[i].Y;
380 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2] + matrix->matrix[4];
381 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3] + matrix->matrix[5];
384 return Ok;
387 GpStatus WINGDIPAPI GdipTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
389 GpPointF *ptsF;
390 GpStatus ret;
391 INT i;
393 TRACE("(%p, %p, %d)\n", matrix, pts, count);
395 if(count <= 0)
396 return InvalidParameter;
398 ptsF = heap_alloc_zero(sizeof(GpPointF) * count);
399 if(!ptsF)
400 return OutOfMemory;
402 for(i = 0; i < count; i++){
403 ptsF[i].X = (REAL)pts[i].X;
404 ptsF[i].Y = (REAL)pts[i].Y;
407 ret = GdipTransformMatrixPoints(matrix, ptsF, count);
409 if(ret == Ok)
410 for(i = 0; i < count; i++){
411 pts[i].X = gdip_round(ptsF[i].X);
412 pts[i].Y = gdip_round(ptsF[i].Y);
414 heap_free(ptsF);
416 return ret;
419 GpStatus WINGDIPAPI GdipTranslateMatrix(GpMatrix *matrix, REAL offsetX,
420 REAL offsetY, GpMatrixOrder order)
422 REAL translate[6];
424 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, offsetX, offsetY, order);
426 if(!matrix)
427 return InvalidParameter;
429 translate[0] = 1.0;
430 translate[1] = 0.0;
431 translate[2] = 0.0;
432 translate[3] = 1.0;
433 translate[4] = offsetX;
434 translate[5] = offsetY;
436 if(order == MatrixOrderAppend)
437 matrix_multiply(matrix->matrix, translate, matrix->matrix);
438 else if (order == MatrixOrderPrepend)
439 matrix_multiply(translate, matrix->matrix, matrix->matrix);
440 else
441 return InvalidParameter;
443 return Ok;
446 GpStatus WINGDIPAPI GdipVectorTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts, INT count)
448 REAL x, y;
449 INT i;
451 TRACE("(%p, %p, %d)\n", matrix, pts, count);
453 if(!matrix || !pts || count <= 0)
454 return InvalidParameter;
456 for(i = 0; i < count; i++)
458 x = pts[i].X;
459 y = pts[i].Y;
461 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2];
462 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3];
465 return Ok;
468 GpStatus WINGDIPAPI GdipVectorTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
470 GpPointF *ptsF;
471 GpStatus ret;
472 INT i;
474 TRACE("(%p, %p, %d)\n", matrix, pts, count);
476 if(count <= 0)
477 return InvalidParameter;
479 ptsF = heap_alloc_zero(sizeof(GpPointF) * count);
480 if(!ptsF)
481 return OutOfMemory;
483 for(i = 0; i < count; i++){
484 ptsF[i].X = (REAL)pts[i].X;
485 ptsF[i].Y = (REAL)pts[i].Y;
488 ret = GdipVectorTransformMatrixPoints(matrix, ptsF, count);
489 /* store back */
490 if(ret == Ok)
491 for(i = 0; i < count; i++){
492 pts[i].X = gdip_round(ptsF[i].X);
493 pts[i].Y = gdip_round(ptsF[i].Y);
495 heap_free(ptsF);
497 return ret;
500 GpStatus WINGDIPAPI GdipIsMatrixEqual(GDIPCONST GpMatrix *matrix, GDIPCONST GpMatrix *matrix2,
501 BOOL *result)
503 TRACE("(%p, %p, %p)\n", matrix, matrix2, result);
505 if(!matrix || !matrix2 || !result)
506 return InvalidParameter;
507 /* based on single array member of GpMatrix */
508 *result = (memcmp(matrix->matrix, matrix2->matrix, sizeof(GpMatrix)) == 0);
510 return Ok;
513 GpStatus WINGDIPAPI GdipIsMatrixIdentity(GDIPCONST GpMatrix *matrix, BOOL *result)
515 static const GpMatrix identity =
517 { 1.0, 0.0,
518 0.0, 1.0,
519 0.0, 0.0 }
522 TRACE("(%p, %p)\n", matrix, result);
524 if(!matrix || !result)
525 return InvalidParameter;
527 return GdipIsMatrixEqual(matrix, &identity, result);