gdiplus: Added GdipAddPathEllipse.
[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 "gdiplus.h"
27 #include "gdiplus_private.h"
28
29 /* Multiplies two matrices of the form
30  *
31  * idx:0 idx:1     0
32  * idx:2 idx:3     0
33  * idx:4 idx:5     1
34  *
35  * and puts the output in out.
36  * */
37 static void matrix_multiply(GDIPCONST REAL * left, GDIPCONST REAL * right, REAL * out)
38 {
39     REAL temp[6];
40     int i, odd;
41
42     for(i = 0; i < 6; i++){
43         odd = i % 2;
44         temp[i] = left[i - odd] * right[odd] + left[i - odd + 1] * right[odd + 2] +
45                   (i >= 4 ? right[odd + 4] : 0.0);
46     }
47
48     memcpy(out, temp, 6 * sizeof(REAL));
49 }
50
51 GpStatus WINGDIPAPI GdipCreateMatrix2(REAL m11, REAL m12, REAL m21, REAL m22,
52     REAL dx, REAL dy, GpMatrix **matrix)
53 {
54     if(!matrix)
55         return InvalidParameter;
56
57     *matrix = GdipAlloc(sizeof(GpMatrix));
58     if(!*matrix)    return OutOfMemory;
59
60     /* first row */
61     (*matrix)->matrix[0] = m11;
62     (*matrix)->matrix[1] = m12;
63     /* second row */
64     (*matrix)->matrix[2] = m21;
65     (*matrix)->matrix[3] = m22;
66     /* third row */
67     (*matrix)->matrix[4] = dx;
68     (*matrix)->matrix[5] = dy;
69
70     return Ok;
71 }
72
73 GpStatus WINGDIPAPI GdipCloneMatrix(GpMatrix *matrix, GpMatrix **clone)
74 {
75     if(!matrix || !clone)
76         return InvalidParameter;
77
78     *clone = GdipAlloc(sizeof(GpMatrix));
79     if(!*clone)    return OutOfMemory;
80
81     memcpy(*clone, matrix, sizeof(GpMatrix));
82
83     return Ok;
84 }
85
86 GpStatus WINGDIPAPI GdipCreateMatrix(GpMatrix **matrix)
87 {
88     if(!matrix)
89         return InvalidParameter;
90
91     *matrix = GdipAlloc(sizeof(GpMatrix));
92     if(!*matrix)    return OutOfMemory;
93
94     (*matrix)->matrix[0] = 1.0;
95     (*matrix)->matrix[1] = 0.0;
96     (*matrix)->matrix[2] = 0.0;
97     (*matrix)->matrix[3] = 1.0;
98     (*matrix)->matrix[4] = 0.0;
99     (*matrix)->matrix[5] = 0.0;
100
101     return Ok;
102 }
103
104 GpStatus WINGDIPAPI GdipDeleteMatrix(GpMatrix *matrix)
105 {
106     if(!matrix)
107         return InvalidParameter;
108
109     GdipFree(matrix);
110
111     return Ok;
112 }
113
114 GpStatus WINGDIPAPI GdipMultiplyMatrix(GpMatrix *matrix, GpMatrix* matrix2,
115     GpMatrixOrder order)
116 {
117     if(!matrix || !matrix2)
118         return InvalidParameter;
119
120     if(order == MatrixOrderAppend)
121         matrix_multiply(matrix->matrix, matrix2->matrix, matrix->matrix);
122     else
123         matrix_multiply(matrix2->matrix, matrix->matrix, matrix->matrix);
124
125     return Ok;
126 }
127
128 GpStatus WINGDIPAPI GdipRotateMatrix(GpMatrix *matrix, REAL angle,
129     GpMatrixOrder order)
130 {
131     REAL cos_theta, sin_theta, rotate[6];
132
133     if(!matrix)
134         return InvalidParameter;
135
136     angle = deg2rad(angle);
137     cos_theta = cos(angle);
138     sin_theta = sin(angle);
139
140     rotate[0] = cos_theta;
141     rotate[1] = sin_theta;
142     rotate[2] = -sin_theta;
143     rotate[3] = cos_theta;
144     rotate[4] = 0.0;
145     rotate[5] = 0.0;
146
147     if(order == MatrixOrderAppend)
148         matrix_multiply(matrix->matrix, rotate, matrix->matrix);
149     else
150         matrix_multiply(rotate, matrix->matrix, matrix->matrix);
151
152     return Ok;
153 }
154
155 GpStatus WINGDIPAPI GdipScaleMatrix(GpMatrix *matrix, REAL scaleX, REAL scaleY,
156     GpMatrixOrder order)
157 {
158     REAL scale[6];
159
160     if(!matrix)
161         return InvalidParameter;
162
163     scale[0] = scaleX;
164     scale[1] = 0.0;
165     scale[2] = 0.0;
166     scale[3] = scaleY;
167     scale[4] = 0.0;
168     scale[5] = 0.0;
169
170     if(order == MatrixOrderAppend)
171         matrix_multiply(matrix->matrix, scale, matrix->matrix);
172     else
173         matrix_multiply(scale, matrix->matrix, matrix->matrix);
174
175     return Ok;
176 }
177
178 GpStatus WINGDIPAPI GdipTransformMatrixPoints(GpMatrix *matrix, GpPointF *pts,
179                                               INT count)
180 {
181     REAL x, y;
182     INT i;
183
184     if(!matrix || !pts)
185         return InvalidParameter;
186
187     for(i = 0; i < count; i++)
188     {
189         x = pts[i].X;
190         y = pts[i].Y;
191
192         pts[i].X = x * matrix->matrix[0] + y * matrix->matrix[2] + matrix->matrix[4];
193         pts[i].Y = x * matrix->matrix[1] + y * matrix->matrix[3] + matrix->matrix[5];
194     }
195
196     return Ok;
197 }
198
199 GpStatus WINGDIPAPI GdipTranslateMatrix(GpMatrix *matrix, REAL offsetX,
200     REAL offsetY, GpMatrixOrder order)
201 {
202     REAL translate[6];
203
204     if(!matrix)
205         return InvalidParameter;
206
207     translate[0] = 1.0;
208     translate[1] = 0.0;
209     translate[2] = 0.0;
210     translate[3] = 1.0;
211     translate[4] = offsetX;
212     translate[5] = offsetY;
213
214     if(order == MatrixOrderAppend)
215         matrix_multiply(matrix->matrix, translate, matrix->matrix);
216     else
217         matrix_multiply(translate, matrix->matrix, matrix->matrix);
218
219     return Ok;
220 }