xmllite: Fail to set input for external IXmlReaderInput.
[wine] / dlls / gdiplus / matrix.c
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  */
18
19 #include <stdarg.h>
20 #include <math.h>
21
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25
26 #include "objbase.h"
27
28 #include "gdiplus.h"
29 #include "gdiplus_private.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
33
34 /* Multiplies two matrices of the form
35  *
36  * idx:0 idx:1     0
37  * idx:2 idx:3     0
38  * idx:4 idx:5     1
39  *
40  * and puts the output in out.
41  * */
42 static void matrix_multiply(GDIPCONST REAL * left, GDIPCONST REAL * right, REAL * out)
43 {
44     REAL temp[6];
45     int i, odd;
46
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);
51     }
52
53     memcpy(out, temp, 6 * sizeof(REAL));
54 }
55
56 static REAL matrix_det(GDIPCONST GpMatrix *matrix)
57 {
58     return matrix->matrix[0]*matrix->matrix[3] - matrix->matrix[1]*matrix->matrix[2];
59 }
60
61 GpStatus WINGDIPAPI GdipCreateMatrix2(REAL m11, REAL m12, REAL m21, REAL m22,
62     REAL dx, REAL dy, GpMatrix **matrix)
63 {
64     TRACE("(%.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %p)\n", m11, m12, m21, m22, dx, dy, matrix);
65
66     if(!matrix)
67         return InvalidParameter;
68
69     *matrix = GdipAlloc(sizeof(GpMatrix));
70     if(!*matrix)    return OutOfMemory;
71
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;
81
82     return Ok;
83 }
84
85 GpStatus WINGDIPAPI GdipCreateMatrix3(GDIPCONST GpRectF *rect,
86     GDIPCONST GpPointF *pt, GpMatrix **matrix)
87 {
88     REAL m11, m12, m21, m22, dx, dy;
89     TRACE("(%p, %p, %p)\n", rect, pt, matrix);
90
91     if(!matrix || !pt)
92         return InvalidParameter;
93
94     m11 = (pt[1].X - pt[0].X) / rect->Width;
95     m21 = (pt[2].X - pt[0].X) / rect->Height;
96     dx = pt[0].X - m11 * rect->X - m21 * rect->Y;
97     m12 = (pt[1].Y - pt[0].Y) / rect->Width;
98     m22 = (pt[2].Y - pt[0].Y) / rect->Height;
99     dy = pt[0].Y - m12 * rect->X - m22 * rect->Y;
100
101     return GdipCreateMatrix2(m11, m12, m21, m22, dx, dy, matrix);
102 }
103
104 GpStatus WINGDIPAPI GdipCreateMatrix3I(GDIPCONST GpRect *rect, GDIPCONST GpPoint *pt,
105     GpMatrix **matrix)
106 {
107     GpRectF rectF;
108     GpPointF ptF[3];
109     int i;
110
111     TRACE("(%p, %p, %p)\n", rect, pt, matrix);
112
113     rectF.X = (REAL)rect->X;
114     rectF.Y = (REAL)rect->Y;
115     rectF.Width = (REAL)rect->Width;
116     rectF.Height = (REAL)rect->Height;
117
118     for (i = 0; i < 3; i++) {
119         ptF[i].X = (REAL)pt[i].X;
120         ptF[i].Y = (REAL)pt[i].Y;
121     }
122     return GdipCreateMatrix3(&rectF, ptF, matrix);
123 }
124
125 GpStatus WINGDIPAPI GdipCloneMatrix(GpMatrix *matrix, GpMatrix **clone)
126 {
127     TRACE("(%p, %p)\n", matrix, clone);
128
129     if(!matrix || !clone)
130         return InvalidParameter;
131
132     *clone = GdipAlloc(sizeof(GpMatrix));
133     if(!*clone)    return OutOfMemory;
134
135     **clone = *matrix;
136
137     return Ok;
138 }
139
140 GpStatus WINGDIPAPI GdipCreateMatrix(GpMatrix **matrix)
141 {
142     TRACE("(%p)\n", matrix);
143
144     if(!matrix)
145         return InvalidParameter;
146
147     *matrix = GdipAlloc(sizeof(GpMatrix));
148     if(!*matrix)    return OutOfMemory;
149
150     (*matrix)->matrix[0] = 1.0;
151     (*matrix)->matrix[1] = 0.0;
152     (*matrix)->matrix[2] = 0.0;
153     (*matrix)->matrix[3] = 1.0;
154     (*matrix)->matrix[4] = 0.0;
155     (*matrix)->matrix[5] = 0.0;
156
157     return Ok;
158 }
159
160 GpStatus WINGDIPAPI GdipDeleteMatrix(GpMatrix *matrix)
161 {
162     TRACE("(%p)\n", matrix);
163
164     if(!matrix)
165         return InvalidParameter;
166
167     GdipFree(matrix);
168
169     return Ok;
170 }
171
172 GpStatus WINGDIPAPI GdipGetMatrixElements(GDIPCONST GpMatrix *matrix,
173     REAL *out)
174 {
175     TRACE("(%p, %p)\n", matrix, out);
176
177     if(!matrix || !out)
178         return InvalidParameter;
179
180     memcpy(out, matrix->matrix, sizeof(matrix->matrix));
181
182     return Ok;
183 }
184
185 GpStatus WINGDIPAPI GdipInvertMatrix(GpMatrix *matrix)
186 {
187     GpMatrix copy;
188     REAL det;
189     BOOL invertible;
190
191     TRACE("(%p)\n", matrix);
192
193     if(!matrix)
194         return InvalidParameter;
195
196     GdipIsMatrixInvertible(matrix, &invertible);
197     if(!invertible)
198         return InvalidParameter;
199
200     det = matrix_det(matrix);
201
202     copy = *matrix;
203     /* store result */
204     matrix->matrix[0] =   copy.matrix[3] / det;
205     matrix->matrix[1] =  -copy.matrix[1] / det;
206     matrix->matrix[2] =  -copy.matrix[2] / det;
207     matrix->matrix[3] =   copy.matrix[0] / det;
208     matrix->matrix[4] =  (copy.matrix[2]*copy.matrix[5]-copy.matrix[3]*copy.matrix[4]) / det;
209     matrix->matrix[5] = -(copy.matrix[0]*copy.matrix[5]-copy.matrix[1]*copy.matrix[4]) / det;
210
211     return Ok;
212 }
213
214 GpStatus WINGDIPAPI GdipIsMatrixInvertible(GDIPCONST GpMatrix *matrix, BOOL *result)
215 {
216     TRACE("(%p, %p)\n", matrix, result);
217
218     if(!matrix || !result)
219         return InvalidParameter;
220
221     *result = (fabs(matrix_det(matrix)) >= 1e-5);
222
223     return Ok;
224 }
225
226 GpStatus WINGDIPAPI GdipMultiplyMatrix(GpMatrix *matrix, GDIPCONST GpMatrix* matrix2,
227     GpMatrixOrder order)
228 {
229     TRACE("(%p, %p, %d)\n", matrix, matrix2, order);
230
231     if(!matrix || !matrix2)
232         return InvalidParameter;
233
234     if(order == MatrixOrderAppend)
235         matrix_multiply(matrix->matrix, matrix2->matrix, matrix->matrix);
236     else if (order == MatrixOrderPrepend)
237         matrix_multiply(matrix2->matrix, matrix->matrix, matrix->matrix);
238     else
239         return InvalidParameter;
240
241     return Ok;
242 }
243
244 GpStatus WINGDIPAPI GdipRotateMatrix(GpMatrix *matrix, REAL angle,
245     GpMatrixOrder order)
246 {
247     REAL cos_theta, sin_theta, rotate[6];
248
249     TRACE("(%p, %.2f, %d)\n", matrix, angle, order);
250
251     if(!matrix)
252         return InvalidParameter;
253
254     angle = deg2rad(angle);
255     cos_theta = cos(angle);
256     sin_theta = sin(angle);
257
258     rotate[0] = cos_theta;
259     rotate[1] = sin_theta;
260     rotate[2] = -sin_theta;
261     rotate[3] = cos_theta;
262     rotate[4] = 0.0;
263     rotate[5] = 0.0;
264
265     if(order == MatrixOrderAppend)
266         matrix_multiply(matrix->matrix, rotate, matrix->matrix);
267     else if (order == MatrixOrderPrepend)
268         matrix_multiply(rotate, matrix->matrix, matrix->matrix);
269     else
270         return InvalidParameter;
271
272     return Ok;
273 }
274
275 GpStatus WINGDIPAPI GdipScaleMatrix(GpMatrix *matrix, REAL scaleX, REAL scaleY,
276     GpMatrixOrder order)
277 {
278     REAL scale[6];
279
280     TRACE("(%p, %.2f, %.2f, %d)\n", matrix, scaleX, scaleY, order);
281
282     if(!matrix)
283         return InvalidParameter;
284
285     scale[0] = scaleX;
286     scale[1] = 0.0;
287     scale[2] = 0.0;
288     scale[3] = scaleY;
289     scale[4] = 0.0;
290     scale[5] = 0.0;
291
292     if(order == MatrixOrderAppend)
293         matrix_multiply(matrix->matrix, scale, matrix->matrix);
294     else if (order == MatrixOrderPrepend)
295         matrix_multiply(scale, matrix->matrix, matrix->matrix);
296     else
297         return InvalidParameter;
298
299     return Ok;
300 }
301
302 GpStatus WINGDIPAPI GdipSetMatrixElements(GpMatrix *matrix, REAL m11, REAL m12,
303     REAL m21, REAL m22, REAL dx, REAL dy)
304 {
305     TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", matrix, m11, m12,
306             m21, m22, dx, dy);
307
308     if(!matrix)
309         return InvalidParameter;
310
311     matrix->matrix[0] = m11;
312     matrix->matrix[1] = m12;
313     matrix->matrix[2] = m21;
314     matrix->matrix[3] = m22;
315     matrix->matrix[4] = dx;
316     matrix->matrix[5] = dy;
317
318     return Ok;
319 }
320
321 GpStatus WINGDIPAPI GdipShearMatrix(GpMatrix *matrix, REAL shearX, REAL shearY,
322     GpMatrixOrder order)
323 {
324     REAL shear[6];
325
326     TRACE("(%p, %.2f, %.2f, %d)\n", matrix, shearX, shearY, order);
327
328     if(!matrix)
329         return InvalidParameter;
330
331     /* prepare transformation matrix */
332     shear[0] = 1.0;
333     shear[1] = shearY;
334     shear[2] = shearX;
335     shear[3] = 1.0;
336     shear[4] = 0.0;
337     shear[5] = 0.0;
338
339     if(order == MatrixOrderAppend)
340         matrix_multiply(matrix->matrix, shear, matrix->matrix);
341     else if (order == MatrixOrderPrepend)
342         matrix_multiply(shear, matrix->matrix, matrix->matrix);
343     else
344         return InvalidParameter;
345
346     return Ok;
347 }
348
349 GpStatus WINGDIPAPI GdipTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts,
350                                               INT count)
351 {
352     REAL x, y;
353     INT i;
354
355     TRACE("(%p, %p, %d)\n", matrix, pts, count);
356
357     if(!matrix || !pts || count <= 0)
358         return InvalidParameter;
359
360     for(i = 0; i < count; i++)
361     {
362         x = pts[i].X;
363         y = pts[i].Y;
364
365         pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2] + matrix->matrix[4];
366         pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3] + matrix->matrix[5];
367     }
368
369     return Ok;
370 }
371
372 GpStatus WINGDIPAPI GdipTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
373 {
374     GpPointF *ptsF;
375     GpStatus ret;
376     INT i;
377
378     TRACE("(%p, %p, %d)\n", matrix, pts, count);
379
380     if(count <= 0)
381         return InvalidParameter;
382
383     ptsF = GdipAlloc(sizeof(GpPointF) * count);
384     if(!ptsF)
385         return OutOfMemory;
386
387     for(i = 0; i < count; i++){
388         ptsF[i].X = (REAL)pts[i].X;
389         ptsF[i].Y = (REAL)pts[i].Y;
390     }
391
392     ret = GdipTransformMatrixPoints(matrix, ptsF, count);
393
394     if(ret == Ok)
395         for(i = 0; i < count; i++){
396             pts[i].X = gdip_round(ptsF[i].X);
397             pts[i].Y = gdip_round(ptsF[i].Y);
398         }
399     GdipFree(ptsF);
400
401     return ret;
402 }
403
404 GpStatus WINGDIPAPI GdipTranslateMatrix(GpMatrix *matrix, REAL offsetX,
405     REAL offsetY, GpMatrixOrder order)
406 {
407     REAL translate[6];
408
409     TRACE("(%p, %.2f, %.2f, %d)\n", matrix, offsetX, offsetY, order);
410
411     if(!matrix)
412         return InvalidParameter;
413
414     translate[0] = 1.0;
415     translate[1] = 0.0;
416     translate[2] = 0.0;
417     translate[3] = 1.0;
418     translate[4] = offsetX;
419     translate[5] = offsetY;
420
421     if(order == MatrixOrderAppend)
422         matrix_multiply(matrix->matrix, translate, matrix->matrix);
423     else if (order == MatrixOrderPrepend)
424         matrix_multiply(translate, matrix->matrix, matrix->matrix);
425     else
426         return InvalidParameter;
427
428     return Ok;
429 }
430
431 GpStatus WINGDIPAPI GdipVectorTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts, INT count)
432 {
433     REAL x, y;
434     INT i;
435
436     TRACE("(%p, %p, %d)\n", matrix, pts, count);
437
438     if(!matrix || !pts || count <= 0)
439         return InvalidParameter;
440
441     for(i = 0; i < count; i++)
442     {
443         x = pts[i].X;
444         y = pts[i].Y;
445
446         pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2];
447         pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3];
448     }
449
450     return Ok;
451 }
452
453 GpStatus WINGDIPAPI GdipVectorTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
454 {
455     GpPointF *ptsF;
456     GpStatus ret;
457     INT i;
458
459     TRACE("(%p, %p, %d)\n", matrix, pts, count);
460
461     if(count <= 0)
462         return InvalidParameter;
463
464     ptsF = GdipAlloc(sizeof(GpPointF) * count);
465     if(!ptsF)
466         return OutOfMemory;
467
468     for(i = 0; i < count; i++){
469         ptsF[i].X = (REAL)pts[i].X;
470         ptsF[i].Y = (REAL)pts[i].Y;
471     }
472
473     ret = GdipVectorTransformMatrixPoints(matrix, ptsF, count);
474     /* store back */
475     if(ret == Ok)
476         for(i = 0; i < count; i++){
477             pts[i].X = gdip_round(ptsF[i].X);
478             pts[i].Y = gdip_round(ptsF[i].Y);
479         }
480     GdipFree(ptsF);
481
482     return ret;
483 }
484
485 GpStatus WINGDIPAPI GdipIsMatrixEqual(GDIPCONST GpMatrix *matrix, GDIPCONST GpMatrix *matrix2,
486     BOOL *result)
487 {
488     TRACE("(%p, %p, %p)\n", matrix, matrix2, result);
489
490     if(!matrix || !matrix2 || !result)
491         return InvalidParameter;
492     /* based on single array member of GpMatrix */
493     *result = (memcmp(matrix->matrix, matrix2->matrix, sizeof(GpMatrix)) == 0);
494
495     return Ok;
496 }
497
498 GpStatus WINGDIPAPI GdipIsMatrixIdentity(GDIPCONST GpMatrix *matrix, BOOL *result)
499 {
500     GpMatrix *e;
501     GpStatus ret;
502     BOOL isIdentity;
503
504     TRACE("(%p, %p)\n", matrix, result);
505
506     if(!matrix || !result)
507         return InvalidParameter;
508
509     ret = GdipCreateMatrix(&e);
510     if(ret != Ok) return ret;
511
512     ret = GdipIsMatrixEqual(matrix, e, &isIdentity);
513     if(ret == Ok)
514         *result = isIdentity;
515
516     GdipFree(e);
517
518     return ret;
519 }