a29508db8b0a7c3f744e63f5061687fd4aeacdfd
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
3 *
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.
8 *
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.
13 *
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
17 */
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"
31 /* Multiplies two matrices of the form
32 *
33 * idx:0 idx:1 0
34 * idx:2 idx:3 0
35 * idx:4 idx:5 1
36 *
37 * and puts the output in out.
38 * */
39 static void matrix_multiply(GDIPCONST REAL * left, GDIPCONST REAL * right, REAL * out)
40 {
41 REAL temp[6];
42 int i, odd;
44 for(i = 0; i < 6; i++){
45 odd = i % 2;
46 temp[i] = left[i - odd] * right[odd] + left[i - odd + 1] * right[odd + 2] +
47 (i >= 4 ? right[odd + 4] : 0.0);
48 }
50 memcpy(out, temp, 6 * sizeof(REAL));
51 }
53 static REAL matrix_det(GDIPCONST GpMatrix *matrix)
54 {
55 return matrix->matrix[0]*matrix->matrix[3] - matrix->matrix[1]*matrix->matrix[2];
56 }
58 GpStatus WINGDIPAPI GdipCreateMatrix2(REAL m11, REAL m12, REAL m21, REAL m22,
59 REAL dx, REAL dy, GpMatrix **matrix)
60 {
61 if(!matrix)
62 return InvalidParameter;
64 *matrix = GdipAlloc(sizeof(GpMatrix));
65 if(!*matrix) return OutOfMemory;
67 /* first row */
68 (*matrix)->matrix[0] = m11;
69 (*matrix)->matrix[1] = m12;
70 /* second row */
71 (*matrix)->matrix[2] = m21;
72 (*matrix)->matrix[3] = m22;
73 /* third row */
74 (*matrix)->matrix[4] = dx;
75 (*matrix)->matrix[5] = dy;
77 return Ok;
78 }
80 GpStatus WINGDIPAPI GdipCreateMatrix3(GDIPCONST GpRectF *rect,
81 GDIPCONST GpPointF *pt, GpMatrix **matrix)
82 {
83 if(!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;
94 return Ok;
95 }
97 GpStatus WINGDIPAPI GdipCreateMatrix3I(GDIPCONST GpRect *rect, GDIPCONST GpPoint *pt,
98 GpMatrix **matrix)
99 {
100 GpRectF rectF;
101 GpPointF ptF;
103 rectF.X = (REAL)rect->X;
104 rectF.Y = (REAL)rect->Y;
105 rectF.Width = (REAL)rect->Width;
106 rectF.Height = (REAL)rect->Height;
108 ptF.X = (REAL)pt->X;
109 ptF.Y = (REAL)pt->Y;
111 return GdipCreateMatrix3(&rectF, &ptF, matrix);
112 }
114 GpStatus WINGDIPAPI GdipCloneMatrix(GpMatrix *matrix, GpMatrix **clone)
115 {
116 if(!matrix || !clone)
117 return InvalidParameter;
119 *clone = GdipAlloc(sizeof(GpMatrix));
120 if(!*clone) return OutOfMemory;
122 **clone = *matrix;
124 return Ok;
125 }
127 GpStatus WINGDIPAPI GdipCreateMatrix(GpMatrix **matrix)
128 {
129 if(!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;
142 return Ok;
143 }
145 GpStatus WINGDIPAPI GdipDeleteMatrix(GpMatrix *matrix)
146 {
147 if(!matrix)
148 return InvalidParameter;
150 GdipFree(matrix);
152 return Ok;
153 }
155 GpStatus WINGDIPAPI GdipGetMatrixElements(GDIPCONST GpMatrix *matrix,
156 REAL *out)
157 {
158 if(!matrix || !out)
159 return InvalidParameter;
161 memcpy(out, matrix->matrix, sizeof(matrix->matrix));
163 return Ok;
164 }
166 GpStatus WINGDIPAPI GdipInvertMatrix(GpMatrix *matrix)
167 {
168 GpMatrix copy;
169 REAL det;
170 BOOL invertible;
172 if(!matrix)
173 return InvalidParameter;
175 GdipIsMatrixInvertible(matrix, &invertible);
176 if(!invertible)
177 return InvalidParameter;
179 det = matrix_det(matrix);
181 copy = *matrix;
182 /* store result */
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;
190 return Ok;
191 }
193 GpStatus WINGDIPAPI GdipIsMatrixInvertible(GDIPCONST GpMatrix *matrix, BOOL *result)
194 {
195 if(!matrix || !result)
196 return InvalidParameter;
198 *result = (fabs(matrix_det(matrix)) >= 1e-5);
200 return Ok;
201 }
203 GpStatus WINGDIPAPI GdipMultiplyMatrix(GpMatrix *matrix, GpMatrix* matrix2,
204 GpMatrixOrder order)
205 {
206 if(!matrix || !matrix2)
207 return InvalidParameter;
209 if(order == MatrixOrderAppend)
210 matrix_multiply(matrix->matrix, matrix2->matrix, matrix->matrix);
211 else
212 matrix_multiply(matrix2->matrix, matrix->matrix, matrix->matrix);
214 return Ok;
215 }
217 GpStatus WINGDIPAPI GdipRotateMatrix(GpMatrix *matrix, REAL angle,
218 GpMatrixOrder order)
219 {
220 REAL cos_theta, sin_theta, rotate[6];
222 if(!matrix)
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;
233 rotate[4] = 0.0;
234 rotate[5] = 0.0;
236 if(order == MatrixOrderAppend)
237 matrix_multiply(matrix->matrix, rotate, matrix->matrix);
238 else
239 matrix_multiply(rotate, matrix->matrix, matrix->matrix);
241 return Ok;
242 }
244 GpStatus WINGDIPAPI GdipScaleMatrix(GpMatrix *matrix, REAL scaleX, REAL scaleY,
245 GpMatrixOrder order)
246 {
247 REAL scale[6];
249 if(!matrix)
250 return InvalidParameter;
252 scale[0] = scaleX;
253 scale[1] = 0.0;
254 scale[2] = 0.0;
255 scale[3] = scaleY;
256 scale[4] = 0.0;
257 scale[5] = 0.0;
259 if(order == MatrixOrderAppend)
260 matrix_multiply(matrix->matrix, scale, matrix->matrix);
261 else
262 matrix_multiply(scale, matrix->matrix, matrix->matrix);
264 return Ok;
265 }
267 GpStatus WINGDIPAPI GdipSetMatrixElements(GpMatrix *matrix, REAL m11, REAL m12,
268 REAL m21, REAL m22, REAL dx, REAL dy)
269 {
270 if(!matrix)
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;
280 return Ok;
281 }
283 GpStatus WINGDIPAPI GdipShearMatrix(GpMatrix *matrix, REAL shearX, REAL shearY,
284 GpMatrixOrder order)
285 {
286 REAL shear[6];
288 if(!matrix)
289 return InvalidParameter;
291 /* prepare transformation matrix */
292 shear[0] = 1.0;
293 shear[1] = shearY;
294 shear[2] = shearX;
295 shear[3] = 1.0;
296 shear[4] = 0.0;
297 shear[5] = 0.0;
299 if(order == MatrixOrderAppend)
300 matrix_multiply(matrix->matrix, shear, matrix->matrix);
301 else
302 matrix_multiply(shear, matrix->matrix, matrix->matrix);
304 return Ok;
305 }
307 GpStatus WINGDIPAPI GdipTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts,
308 INT count)
309 {
310 REAL x, y;
311 INT i;
313 if(!matrix || !pts || count <= 0)
314 return InvalidParameter;
316 for(i = 0; i < count; i++)
317 {
318 x = pts[i].X;
319 y = pts[i].Y;
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];
323 }
325 return Ok;
326 }
328 GpStatus WINGDIPAPI GdipTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
329 {
330 GpPointF *ptsF;
331 GpStatus ret;
332 INT i;
334 if(count <= 0)
335 return InvalidParameter;
337 ptsF = GdipAlloc(sizeof(GpPointF) * count);
338 if(!ptsF)
339 return OutOfMemory;
341 for(i = 0; i < count; i++){
342 ptsF[i].X = (REAL)pts[i].X;
343 ptsF[i].Y = (REAL)pts[i].Y;
344 }
346 ret = GdipTransformMatrixPoints(matrix, ptsF, count);
348 if(ret == Ok)
349 for(i = 0; i < count; i++){
350 pts[i].X = roundr(ptsF[i].X);
351 pts[i].Y = roundr(ptsF[i].Y);
352 }
353 GdipFree(ptsF);
355 return ret;
356 }
358 GpStatus WINGDIPAPI GdipTranslateMatrix(GpMatrix *matrix, REAL offsetX,
359 REAL offsetY, GpMatrixOrder order)
360 {
361 REAL translate[6];
363 if(!matrix)
364 return InvalidParameter;
366 translate[0] = 1.0;
367 translate[1] = 0.0;
368 translate[2] = 0.0;
369 translate[3] = 1.0;
370 translate[4] = offsetX;
371 translate[5] = offsetY;
373 if(order == MatrixOrderAppend)
374 matrix_multiply(matrix->matrix, translate, matrix->matrix);
375 else
376 matrix_multiply(translate, matrix->matrix, matrix->matrix);
378 return Ok;
379 }
381 GpStatus WINGDIPAPI GdipVectorTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts, INT count)
382 {
383 REAL x, y;
384 INT i;
386 if(!matrix || !pts || count <= 0)
387 return InvalidParameter;
389 for(i = 0; i < count; i++)
390 {
391 x = pts[i].X;
392 y = pts[i].Y;
394 pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2];
395 pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3];
396 }
398 return Ok;
399 }
401 GpStatus WINGDIPAPI GdipVectorTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
402 {
403 GpPointF *ptsF;
404 GpStatus ret;
405 INT i;
407 if(count <= 0)
408 return InvalidParameter;
410 ptsF = GdipAlloc(sizeof(GpPointF) * count);
411 if(!ptsF)
412 return OutOfMemory;
414 for(i = 0; i < count; i++){
415 ptsF[i].X = (REAL)pts[i].X;
416 ptsF[i].Y = (REAL)pts[i].Y;
417 }
419 ret = GdipVectorTransformMatrixPoints(matrix, ptsF, count);
420 /* store back */
421 if(ret == Ok)
422 for(i = 0; i < count; i++){
423 pts[i].X = roundr(ptsF[i].X);
424 pts[i].Y = roundr(ptsF[i].Y);
425 }
426 GdipFree(ptsF);
428 return ret;
429 }
431 GpStatus WINGDIPAPI GdipIsMatrixEqual(GDIPCONST GpMatrix *matrix, GDIPCONST GpMatrix *matrix2,
432 BOOL *result)
433 {
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);
439 return Ok;
440 }
442 GpStatus WINGDIPAPI GdipIsMatrixIdentity(GDIPCONST GpMatrix *matrix, BOOL *result)
443 {
444 GpMatrix *e;
445 GpStatus ret;
446 BOOL isIdentity;
448 if(!matrix || !result)
449 return InvalidParameter;
451 ret = GdipCreateMatrix(&e);
452 if(ret != Ok) return ret;
454 ret = GdipIsMatrixEqual(matrix, e, &isIdentity);
455 if(ret == Ok)
456 *result = isIdentity;
458 GdipFree(e);
460 return ret;
461 }