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
29 #include "gdiplus_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
34 /* Multiplies two matrices of the form
40 * and puts the output in out.
42 static void matrix_multiply(GDIPCONST REAL * left, GDIPCONST REAL * right, REAL * out)
47 for(i = 0; i < 6; i++){
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);
67 return InvalidParameter;
69 *matrix = GdipAlloc(sizeof(GpMatrix));
70 if(!*matrix) return OutOfMemory;
73 (*matrix)->matrix[0] = m11;
74 (*matrix)->matrix[1] = m12;
76 (*matrix)->matrix[2] = m21;
77 (*matrix)->matrix[3] = m22;
79 (*matrix)->matrix[4] = dx;
80 (*matrix)->matrix[5] = dy;
85 GpStatus WINGDIPAPI GdipCreateMatrix3(GDIPCONST GpRectF *rect,
86 GDIPCONST GpPointF *pt, GpMatrix **matrix)
88 TRACE("(%p, %p, %p)\n", rect, pt, 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;
104 GpStatus WINGDIPAPI GdipCreateMatrix3I(GDIPCONST GpRect *rect, GDIPCONST GpPoint *pt,
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;
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;
138 GpStatus WINGDIPAPI GdipCreateMatrix(GpMatrix **matrix)
140 TRACE("(%p)\n", 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;
158 GpStatus WINGDIPAPI GdipDeleteMatrix(GpMatrix *matrix)
160 TRACE("(%p)\n", matrix);
163 return InvalidParameter;
170 GpStatus WINGDIPAPI GdipGetMatrixElements(GDIPCONST GpMatrix *matrix,
173 TRACE("(%p, %p)\n", matrix, out);
176 return InvalidParameter;
178 memcpy(out, matrix->matrix, sizeof(matrix->matrix));
183 GpStatus WINGDIPAPI GdipInvertMatrix(GpMatrix *matrix)
189 TRACE("(%p)\n", matrix);
192 return InvalidParameter;
194 GdipIsMatrixInvertible(matrix, &invertible);
196 return InvalidParameter;
198 det = matrix_det(matrix);
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;
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);
224 GpStatus WINGDIPAPI GdipMultiplyMatrix(GpMatrix *matrix, GDIPCONST GpMatrix* matrix2,
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);
235 matrix_multiply(matrix2->matrix, matrix->matrix, matrix->matrix);
240 GpStatus WINGDIPAPI GdipRotateMatrix(GpMatrix *matrix, REAL angle,
243 REAL cos_theta, sin_theta, rotate[6];
245 TRACE("(%p, %.2f, %d)\n", matrix, angle, order);
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;
261 if(order == MatrixOrderAppend)
262 matrix_multiply(matrix->matrix, rotate, matrix->matrix);
264 matrix_multiply(rotate, matrix->matrix, matrix->matrix);
269 GpStatus WINGDIPAPI GdipScaleMatrix(GpMatrix *matrix, REAL scaleX, REAL scaleY,
274 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, scaleX, scaleY, order);
277 return InvalidParameter;
286 if(order == MatrixOrderAppend)
287 matrix_multiply(matrix->matrix, scale, matrix->matrix);
289 matrix_multiply(scale, matrix->matrix, matrix->matrix);
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,
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;
313 GpStatus WINGDIPAPI GdipShearMatrix(GpMatrix *matrix, REAL shearX, REAL shearY,
318 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, shearX, shearY, order);
321 return InvalidParameter;
323 /* prepare transformation matrix */
331 if(order == MatrixOrderAppend)
332 matrix_multiply(matrix->matrix, shear, matrix->matrix);
334 matrix_multiply(shear, matrix->matrix, matrix->matrix);
339 GpStatus WINGDIPAPI GdipTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts,
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++)
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];
362 GpStatus WINGDIPAPI GdipTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
368 TRACE("(%p, %p, %d)\n", matrix, pts, count);
371 return InvalidParameter;
373 ptsF = GdipAlloc(sizeof(GpPointF) * count);
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);
385 for(i = 0; i < count; i++){
386 pts[i].X = roundr(ptsF[i].X);
387 pts[i].Y = roundr(ptsF[i].Y);
394 GpStatus WINGDIPAPI GdipTranslateMatrix(GpMatrix *matrix, REAL offsetX,
395 REAL offsetY, GpMatrixOrder order)
399 TRACE("(%p, %.2f, %.2f, %d)\n", matrix, offsetX, offsetY, order);
402 return InvalidParameter;
408 translate[4] = offsetX;
409 translate[5] = offsetY;
411 if(order == MatrixOrderAppend)
412 matrix_multiply(matrix->matrix, translate, matrix->matrix);
414 matrix_multiply(translate, matrix->matrix, matrix->matrix);
419 GpStatus WINGDIPAPI GdipVectorTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts, INT count)
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++)
434 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2];
435 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3];
441 GpStatus WINGDIPAPI GdipVectorTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
447 TRACE("(%p, %p, %d)\n", matrix, pts, count);
450 return InvalidParameter;
452 ptsF = GdipAlloc(sizeof(GpPointF) * count);
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);
464 for(i = 0; i < count; i++){
465 pts[i].X = roundr(ptsF[i].X);
466 pts[i].Y = roundr(ptsF[i].Y);
473 GpStatus WINGDIPAPI GdipIsMatrixEqual(GDIPCONST GpMatrix *matrix, GDIPCONST GpMatrix *matrix2,
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);
486 GpStatus WINGDIPAPI GdipIsMatrixIdentity(GDIPCONST GpMatrix *matrix, BOOL *result)
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);
502 *result = isIdentity;