wined3d: Get rid of the swapchain destroy callback.
[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;
109
110     TRACE("(%p, %p, %p)\n", rect, pt, matrix);
111
112     rectF.X = (REAL)rect->X;
113     rectF.Y = (REAL)rect->Y;
114     rectF.Width = (REAL)rect->Width;
115     rectF.Height = (REAL)rect->Height;
116
117     ptF.X = (REAL)pt->X;
118     ptF.Y = (REAL)pt->Y;
119
120     return GdipCreateMatrix3(&rectF, &ptF, matrix);
121 }
122
123 GpStatus WINGDIPAPI GdipCloneMatrix(GpMatrix *matrix, GpMatrix **clone)
124 {
125     TRACE("(%p, %p)\n", matrix, clone);
126
127     if(!matrix || !clone)
128         return InvalidParameter;
129
130     *clone = GdipAlloc(sizeof(GpMatrix));
131     if(!*clone)    return OutOfMemory;
132
133     **clone = *matrix;
134
135     return Ok;
136 }
137
138 GpStatus WINGDIPAPI GdipCreateMatrix(GpMatrix **matrix)
139 {
140     TRACE("(%p)\n", matrix);
141
142     if(!matrix)
143         return InvalidParameter;
144
145     *matrix = GdipAlloc(sizeof(GpMatrix));
146     if(!*matrix)    return OutOfMemory;
147
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;
154
155     return Ok;
156 }
157
158 GpStatus WINGDIPAPI GdipDeleteMatrix(GpMatrix *matrix)
159 {
160     TRACE("(%p)\n", matrix);
161
162     if(!matrix)
163         return InvalidParameter;
164
165     GdipFree(matrix);
166
167     return Ok;
168 }
169
170 GpStatus WINGDIPAPI GdipGetMatrixElements(GDIPCONST GpMatrix *matrix,
171     REAL *out)
172 {
173     TRACE("(%p, %p)\n", matrix, out);
174
175     if(!matrix || !out)
176         return InvalidParameter;
177
178     memcpy(out, matrix->matrix, sizeof(matrix->matrix));
179
180     return Ok;
181 }
182
183 GpStatus WINGDIPAPI GdipInvertMatrix(GpMatrix *matrix)
184 {
185     GpMatrix copy;
186     REAL det;
187     BOOL invertible;
188
189     TRACE("(%p)\n", matrix);
190
191     if(!matrix)
192         return InvalidParameter;
193
194     GdipIsMatrixInvertible(matrix, &invertible);
195     if(!invertible)
196         return InvalidParameter;
197
198     det = matrix_det(matrix);
199
200     copy = *matrix;
201     /* store result */
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;
208
209     return Ok;
210 }
211
212 GpStatus WINGDIPAPI GdipIsMatrixInvertible(GDIPCONST GpMatrix *matrix, BOOL *result)
213 {
214     TRACE("(%p, %p)\n", matrix, result);
215
216     if(!matrix || !result)
217         return InvalidParameter;
218
219     *result = (fabs(matrix_det(matrix)) >= 1e-5);
220
221     return Ok;
222 }
223
224 GpStatus WINGDIPAPI GdipMultiplyMatrix(GpMatrix *matrix, GDIPCONST GpMatrix* matrix2,
225     GpMatrixOrder order)
226 {
227     TRACE("(%p, %p, %d)\n", matrix, matrix2, order);
228
229     if(!matrix || !matrix2)
230         return InvalidParameter;
231
232     if(order == MatrixOrderAppend)
233         matrix_multiply(matrix->matrix, matrix2->matrix, matrix->matrix);
234     else if (order == MatrixOrderPrepend)
235         matrix_multiply(matrix2->matrix, matrix->matrix, matrix->matrix);
236     else
237         return InvalidParameter;
238
239     return Ok;
240 }
241
242 GpStatus WINGDIPAPI GdipRotateMatrix(GpMatrix *matrix, REAL angle,
243     GpMatrixOrder order)
244 {
245     REAL cos_theta, sin_theta, rotate[6];
246
247     TRACE("(%p, %.2f, %d)\n", matrix, angle, order);
248
249     if(!matrix)
250         return InvalidParameter;
251
252     angle = deg2rad(angle);
253     cos_theta = cos(angle);
254     sin_theta = sin(angle);
255
256     rotate[0] = cos_theta;
257     rotate[1] = sin_theta;
258     rotate[2] = -sin_theta;
259     rotate[3] = cos_theta;
260     rotate[4] = 0.0;
261     rotate[5] = 0.0;
262
263     if(order == MatrixOrderAppend)
264         matrix_multiply(matrix->matrix, rotate, matrix->matrix);
265     else if (order == MatrixOrderPrepend)
266         matrix_multiply(rotate, matrix->matrix, matrix->matrix);
267     else
268         return InvalidParameter;
269
270     return Ok;
271 }
272
273 GpStatus WINGDIPAPI GdipScaleMatrix(GpMatrix *matrix, REAL scaleX, REAL scaleY,
274     GpMatrixOrder order)
275 {
276     REAL scale[6];
277
278     TRACE("(%p, %.2f, %.2f, %d)\n", matrix, scaleX, scaleY, order);
279
280     if(!matrix)
281         return InvalidParameter;
282
283     scale[0] = scaleX;
284     scale[1] = 0.0;
285     scale[2] = 0.0;
286     scale[3] = scaleY;
287     scale[4] = 0.0;
288     scale[5] = 0.0;
289
290     if(order == MatrixOrderAppend)
291         matrix_multiply(matrix->matrix, scale, matrix->matrix);
292     else if (order == MatrixOrderPrepend)
293         matrix_multiply(scale, matrix->matrix, matrix->matrix);
294     else
295         return InvalidParameter;
296
297     return Ok;
298 }
299
300 GpStatus WINGDIPAPI GdipSetMatrixElements(GpMatrix *matrix, REAL m11, REAL m12,
301     REAL m21, REAL m22, REAL dx, REAL dy)
302 {
303     TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", matrix, m11, m12,
304             m21, m22, dx, dy);
305
306     if(!matrix)
307         return InvalidParameter;
308
309     matrix->matrix[0] = m11;
310     matrix->matrix[1] = m12;
311     matrix->matrix[2] = m21;
312     matrix->matrix[3] = m22;
313     matrix->matrix[4] = dx;
314     matrix->matrix[5] = dy;
315
316     return Ok;
317 }
318
319 GpStatus WINGDIPAPI GdipShearMatrix(GpMatrix *matrix, REAL shearX, REAL shearY,
320     GpMatrixOrder order)
321 {
322     REAL shear[6];
323
324     TRACE("(%p, %.2f, %.2f, %d)\n", matrix, shearX, shearY, order);
325
326     if(!matrix)
327         return InvalidParameter;
328
329     /* prepare transformation matrix */
330     shear[0] = 1.0;
331     shear[1] = shearY;
332     shear[2] = shearX;
333     shear[3] = 1.0;
334     shear[4] = 0.0;
335     shear[5] = 0.0;
336
337     if(order == MatrixOrderAppend)
338         matrix_multiply(matrix->matrix, shear, matrix->matrix);
339     else if (order == MatrixOrderPrepend)
340         matrix_multiply(shear, matrix->matrix, matrix->matrix);
341     else
342         return InvalidParameter;
343
344     return Ok;
345 }
346
347 GpStatus WINGDIPAPI GdipTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts,
348                                               INT count)
349 {
350     REAL x, y;
351     INT i;
352
353     TRACE("(%p, %p, %d)\n", matrix, pts, count);
354
355     if(!matrix || !pts || count <= 0)
356         return InvalidParameter;
357
358     for(i = 0; i < count; i++)
359     {
360         x = pts[i].X;
361         y = pts[i].Y;
362
363         pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2] + matrix->matrix[4];
364         pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3] + matrix->matrix[5];
365     }
366
367     return Ok;
368 }
369
370 GpStatus WINGDIPAPI GdipTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
371 {
372     GpPointF *ptsF;
373     GpStatus ret;
374     INT i;
375
376     TRACE("(%p, %p, %d)\n", matrix, pts, count);
377
378     if(count <= 0)
379         return InvalidParameter;
380
381     ptsF = GdipAlloc(sizeof(GpPointF) * count);
382     if(!ptsF)
383         return OutOfMemory;
384
385     for(i = 0; i < count; i++){
386         ptsF[i].X = (REAL)pts[i].X;
387         ptsF[i].Y = (REAL)pts[i].Y;
388     }
389
390     ret = GdipTransformMatrixPoints(matrix, ptsF, count);
391
392     if(ret == Ok)
393         for(i = 0; i < count; i++){
394             pts[i].X = roundr(ptsF[i].X);
395             pts[i].Y = roundr(ptsF[i].Y);
396         }
397     GdipFree(ptsF);
398
399     return ret;
400 }
401
402 GpStatus WINGDIPAPI GdipTranslateMatrix(GpMatrix *matrix, REAL offsetX,
403     REAL offsetY, GpMatrixOrder order)
404 {
405     REAL translate[6];
406
407     TRACE("(%p, %.2f, %.2f, %d)\n", matrix, offsetX, offsetY, order);
408
409     if(!matrix)
410         return InvalidParameter;
411
412     translate[0] = 1.0;
413     translate[1] = 0.0;
414     translate[2] = 0.0;
415     translate[3] = 1.0;
416     translate[4] = offsetX;
417     translate[5] = offsetY;
418
419     if(order == MatrixOrderAppend)
420         matrix_multiply(matrix->matrix, translate, matrix->matrix);
421     else if (order == MatrixOrderPrepend)
422         matrix_multiply(translate, matrix->matrix, matrix->matrix);
423     else
424         return InvalidParameter;
425
426     return Ok;
427 }
428
429 GpStatus WINGDIPAPI GdipVectorTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts, INT count)
430 {
431     REAL x, y;
432     INT i;
433
434     TRACE("(%p, %p, %d)\n", matrix, pts, count);
435
436     if(!matrix || !pts || count <= 0)
437         return InvalidParameter;
438
439     for(i = 0; i < count; i++)
440     {
441         x = pts[i].X;
442         y = pts[i].Y;
443
444         pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2];
445         pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3];
446     }
447
448     return Ok;
449 }
450
451 GpStatus WINGDIPAPI GdipVectorTransformMatrixPointsI(GpMatrix *matrix, GpPoint *pts, INT count)
452 {
453     GpPointF *ptsF;
454     GpStatus ret;
455     INT i;
456
457     TRACE("(%p, %p, %d)\n", matrix, pts, count);
458
459     if(count <= 0)
460         return InvalidParameter;
461
462     ptsF = GdipAlloc(sizeof(GpPointF) * count);
463     if(!ptsF)
464         return OutOfMemory;
465
466     for(i = 0; i < count; i++){
467         ptsF[i].X = (REAL)pts[i].X;
468         ptsF[i].Y = (REAL)pts[i].Y;
469     }
470
471     ret = GdipVectorTransformMatrixPoints(matrix, ptsF, count);
472     /* store back */
473     if(ret == Ok)
474         for(i = 0; i < count; i++){
475             pts[i].X = roundr(ptsF[i].X);
476             pts[i].Y = roundr(ptsF[i].Y);
477         }
478     GdipFree(ptsF);
479
480     return ret;
481 }
482
483 GpStatus WINGDIPAPI GdipIsMatrixEqual(GDIPCONST GpMatrix *matrix, GDIPCONST GpMatrix *matrix2,
484     BOOL *result)
485 {
486     TRACE("(%p, %p, %p)\n", matrix, matrix2, result);
487
488     if(!matrix || !matrix2 || !result)
489         return InvalidParameter;
490     /* based on single array member of GpMatrix */
491     *result = (memcmp(matrix->matrix, matrix2->matrix, sizeof(GpMatrix)) == 0);
492
493     return Ok;
494 }
495
496 GpStatus WINGDIPAPI GdipIsMatrixIdentity(GDIPCONST GpMatrix *matrix, BOOL *result)
497 {
498     GpMatrix *e;
499     GpStatus ret;
500     BOOL isIdentity;
501
502     TRACE("(%p, %p)\n", matrix, result);
503
504     if(!matrix || !result)
505         return InvalidParameter;
506
507     ret = GdipCreateMatrix(&e);
508     if(ret != Ok) return ret;
509
510     ret = GdipIsMatrixEqual(matrix, e, &isIdentity);
511     if(ret == Ok)
512         *result = isIdentity;
513
514     GdipFree(e);
515
516     return ret;
517 }