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"
31 /* Multiplies two matrices of the form
37 * and puts the output in out.
39 static void matrix_multiply(GDIPCONST REAL * left, GDIPCONST REAL * right, REAL * out)
44 for(i = 0; i < 6; i++){
46 temp[i] = left[i - odd] * right[odd] + left[i - odd + 1] * right[odd + 2] +
47 (i >= 4 ? right[odd + 4] : 0.0);
50 memcpy(out, temp, 6 * sizeof(REAL));
53 static REAL matrix_det(GDIPCONST GpMatrix *matrix)
55 return matrix->matrix[0]*matrix->matrix[3] - matrix->matrix[1]*matrix->matrix[2];
58 GpStatus WINGDIPAPI GdipCreateMatrix2(REAL m11, REAL m12, REAL m21, REAL m22,
59 REAL dx, REAL dy, GpMatrix **matrix)
62 return InvalidParameter;
64 *matrix = GdipAlloc(sizeof(GpMatrix));
65 if(!*matrix) return OutOfMemory;
68 (*matrix)->matrix[0] = m11;
69 (*matrix)->matrix[1] = m12;
71 (*matrix)->matrix[2] = m21;
72 (*matrix)->matrix[3] = m22;
74 (*matrix)->matrix[4] = dx;
75 (*matrix)->matrix[5] = dy;
80 GpStatus WINGDIPAPI GdipCreateMatrix3(GDIPCONST GpRectF *rect,
81 GDIPCONST GpPointF *pt, GpMatrix **matrix)
84 return InvalidParameter;
86 *matrix = GdipAlloc(sizeof(GpMatrix));
87 if(!*matrix) return OutOfMemory;
89 memcpy((*matrix)->matrix, rect, 4 * sizeof(REAL));
91 (*matrix)->matrix[4] = pt->X;
92 (*matrix)->matrix[5] = pt->Y;
97 GpStatus WINGDIPAPI GdipCreateMatrix3I(GDIPCONST GpRect *rect, GDIPCONST GpPoint *pt,
103 rectF.X = (REAL)rect->X;
104 rectF.Y = (REAL)rect->Y;
105 rectF.Width = (REAL)rect->Width;
106 rectF.Height = (REAL)rect->Height;
111 return GdipCreateMatrix3(&rectF, &ptF, matrix);
114 GpStatus WINGDIPAPI GdipCloneMatrix(GpMatrix *matrix, GpMatrix **clone)
116 if(!matrix || !clone)
117 return InvalidParameter;
119 *clone = GdipAlloc(sizeof(GpMatrix));
120 if(!*clone) return OutOfMemory;
127 GpStatus WINGDIPAPI GdipCreateMatrix(GpMatrix **matrix)
130 return InvalidParameter;
132 *matrix = GdipAlloc(sizeof(GpMatrix));
133 if(!*matrix) return OutOfMemory;
135 (*matrix)->matrix[0] = 1.0;
136 (*matrix)->matrix[1] = 0.0;
137 (*matrix)->matrix[2] = 0.0;
138 (*matrix)->matrix[3] = 1.0;
139 (*matrix)->matrix[4] = 0.0;
140 (*matrix)->matrix[5] = 0.0;
145 GpStatus WINGDIPAPI GdipDeleteMatrix(GpMatrix *matrix)
148 return InvalidParameter;
155 GpStatus WINGDIPAPI GdipGetMatrixElements(GDIPCONST GpMatrix *matrix,
159 return InvalidParameter;
161 memcpy(out, matrix->matrix, sizeof(matrix->matrix));
166 GpStatus WINGDIPAPI GdipInvertMatrix(GpMatrix *matrix)
173 return InvalidParameter;
175 GdipIsMatrixInvertible(matrix, &invertible);
177 return InvalidParameter;
179 det = matrix_det(matrix);
183 matrix->matrix[0] = copy.matrix[3] / det;
184 matrix->matrix[1] = -copy.matrix[1] / det;
185 matrix->matrix[2] = -copy.matrix[2] / det;
186 matrix->matrix[3] = copy.matrix[0] / det;
187 matrix->matrix[4] = (copy.matrix[2]*copy.matrix[5]-copy.matrix[3]*copy.matrix[4]) / det;
188 matrix->matrix[5] = -(copy.matrix[0]*copy.matrix[5]-copy.matrix[1]*copy.matrix[4]) / det;
193 GpStatus WINGDIPAPI GdipIsMatrixInvertible(GDIPCONST GpMatrix *matrix, BOOL *result)
195 if(!matrix || !result)
196 return InvalidParameter;
198 *result = (fabs(matrix_det(matrix)) >= 1e-5);
203 GpStatus WINGDIPAPI GdipMultiplyMatrix(GpMatrix *matrix, GpMatrix* matrix2,
206 if(!matrix || !matrix2)
207 return InvalidParameter;
209 if(order == MatrixOrderAppend)
210 matrix_multiply(matrix->matrix, matrix2->matrix, matrix->matrix);
212 matrix_multiply(matrix2->matrix, matrix->matrix, matrix->matrix);
217 GpStatus WINGDIPAPI GdipRotateMatrix(GpMatrix *matrix, REAL angle,
220 REAL cos_theta, sin_theta, rotate[6];
223 return InvalidParameter;
225 angle = deg2rad(angle);
226 cos_theta = cos(angle);
227 sin_theta = sin(angle);
229 rotate[0] = cos_theta;
230 rotate[1] = sin_theta;
231 rotate[2] = -sin_theta;
232 rotate[3] = cos_theta;
236 if(order == MatrixOrderAppend)
237 matrix_multiply(matrix->matrix, rotate, matrix->matrix);
239 matrix_multiply(rotate, matrix->matrix, matrix->matrix);
244 GpStatus WINGDIPAPI GdipScaleMatrix(GpMatrix *matrix, REAL scaleX, REAL scaleY,
250 return InvalidParameter;
259 if(order == MatrixOrderAppend)
260 matrix_multiply(matrix->matrix, scale, matrix->matrix);
262 matrix_multiply(scale, matrix->matrix, matrix->matrix);
267 GpStatus WINGDIPAPI GdipSetMatrixElements(GpMatrix *matrix, REAL m11, REAL m12,
268 REAL m21, REAL m22, REAL dx, REAL dy)
271 return InvalidParameter;
273 matrix->matrix[0] = m11;
274 matrix->matrix[1] = m12;
275 matrix->matrix[2] = m21;
276 matrix->matrix[3] = m22;
277 matrix->matrix[4] = dx;
278 matrix->matrix[5] = dy;
283 GpStatus WINGDIPAPI GdipShearMatrix(GpMatrix *matrix, REAL shearX, REAL shearY,
289 return InvalidParameter;
291 /* prepare transformation matrix */
299 if(order == MatrixOrderAppend)
300 matrix_multiply(matrix->matrix, shear, matrix->matrix);
302 matrix_multiply(shear, matrix->matrix, matrix->matrix);
307 GpStatus WINGDIPAPI GdipTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts,
313 if(!matrix || !pts || count <= 0)
314 return InvalidParameter;
316 for(i = 0; i < count; i++)
321 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2] + matrix->matrix[4];
322 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3] + matrix->matrix[5];
328 GpStatus WINGDIPAPI GdipTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
335 return InvalidParameter;
337 ptsF = GdipAlloc(sizeof(GpPointF) * count);
341 for(i = 0; i < count; i++){
342 ptsF[i].X = (REAL)pts[i].X;
343 ptsF[i].Y = (REAL)pts[i].Y;
346 ret = GdipTransformMatrixPoints(matrix, ptsF, count);
349 for(i = 0; i < count; i++){
350 pts[i].X = roundr(ptsF[i].X);
351 pts[i].Y = roundr(ptsF[i].Y);
358 GpStatus WINGDIPAPI GdipTranslateMatrix(GpMatrix *matrix, REAL offsetX,
359 REAL offsetY, GpMatrixOrder order)
364 return InvalidParameter;
370 translate[4] = offsetX;
371 translate[5] = offsetY;
373 if(order == MatrixOrderAppend)
374 matrix_multiply(matrix->matrix, translate, matrix->matrix);
376 matrix_multiply(translate, matrix->matrix, matrix->matrix);
381 GpStatus WINGDIPAPI GdipVectorTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts, INT count)
386 if(!matrix || !pts || count <= 0)
387 return InvalidParameter;
389 for(i = 0; i < count; i++)
394 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2];
395 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3];
401 GpStatus WINGDIPAPI GdipVectorTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
408 return InvalidParameter;
410 ptsF = GdipAlloc(sizeof(GpPointF) * count);
414 for(i = 0; i < count; i++){
415 ptsF[i].X = (REAL)pts[i].X;
416 ptsF[i].Y = (REAL)pts[i].Y;
419 ret = GdipVectorTransformMatrixPoints(matrix, ptsF, count);
422 for(i = 0; i < count; i++){
423 pts[i].X = roundr(ptsF[i].X);
424 pts[i].Y = roundr(ptsF[i].Y);
431 GpStatus WINGDIPAPI GdipIsMatrixEqual(GDIPCONST GpMatrix *matrix, GDIPCONST GpMatrix *matrix2,
434 if(!matrix || !matrix2 || !result)
435 return InvalidParameter;
436 /* based on single array member of GpMatrix */
437 *result = (memcmp(matrix->matrix, matrix2->matrix, sizeof(GpMatrix)) == 0);
442 GpStatus WINGDIPAPI GdipIsMatrixIdentity(GDIPCONST GpMatrix *matrix, BOOL *result)
448 if(!matrix || !result)
449 return InvalidParameter;
451 ret = GdipCreateMatrix(&e);
452 if(ret != Ok) return ret;
454 ret = GdipIsMatrixEqual(matrix, e, &isIdentity);
456 *result = isIdentity;