/
matrix.c
444 lines (343 loc) · 10.3 KB
/
matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
* matrix.c
*
* Copyright (C) Novell, Inc. 2003-2004, 2007.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Author: Duncan Mak (duncan@ximian.com)
*
*
**/
#include "matrix-private.h"
/*
GDI+ matrix takes 6 elements arranged in 3 rows by 2 columns. The identity matrix is
[1, 0] [1, 0, 0]
[0, 1] that is a simplification of [0, 1, 0]
[0, 0] [0, 0, 1]
Point v1, v2 Matrix: [m11, m12]
[m21, m22]
[m31, m32]
Calcutation of X, Y using the previous matrix
X = v1 * m11 + v2 * m12 + m31
Y = v1 * m21 + v2 * m22 + m32
M31 and M32 are used to do matrix translations
*/
/* private functions */
/*
* In System.Drawing it is often impossible to specify a 'null' matrix.
* Instead we supply an empty matrix (i.e. new Matrix ()). However this
* "empty" matrix can cause a lot of extra calculation in libgdiplus
* (e.g. invalidating the bitmap) unless we consider it as a special case.
*/
BOOL
gdip_is_matrix_empty (GpMatrix* matrix)
{
if (!matrix)
return TRUE;
/* compare the matrix elements with the empty (no-op) version */
return (gdip_near_one (matrix->xx) && gdip_near_zero (matrix->yx) &&
gdip_near_zero (matrix->xy) && gdip_near_one (matrix->yy) &&
gdip_near_zero (matrix->x0) && gdip_near_zero (matrix->y0));
}
BOOL
gdip_is_matrix_a_translation (GpMatrix *matrix)
{
if (!matrix)
return TRUE;
return ((matrix->xx == 1.0f) && (matrix->yx == 0.0f) &&
(matrix->xy == 0.0f) && (matrix->yy == 1.0f));
}
/* this helper function avoid GpMatrix allocation and reduce code duplication */
GpStatus
gdip_matrix_init_from_rect_3points (GpMatrix *matrix, const GpRectF *rect, const GpPointF *dstplg)
{
GpPointF *p, *p0, *p1, *p2;
double m11, m12, m21, m22;
if (!matrix || !rect || !dstplg)
return InvalidParameter;
if ((rect->Width == 0) || (rect->Height == 0))
return OutOfMemory;
p = (GpPointF*) dstplg;
p0 = p++;
p1 = p++;
p2 = p;
m11 = (p1->X - p0->X) / rect->Width;
m12 = (p1->Y - p0->Y) / rect->Width;
m21 = (p2->X - p0->X) / rect->Height;
m22 = (p2->Y - p0->Y) / rect->Height;
cairo_matrix_init (matrix, m11, m12, m21, m22, p0->X, p0->Y);
cairo_matrix_translate (matrix, -rect->X, -rect->Y);
return Ok;
}
/* public (exported) functions */
// coverity[+alloc : arg-*0]
GpStatus
GdipCreateMatrix (GpMatrix **matrix)
{
GpMatrix *result;
if (!matrix)
return InvalidParameter;
result = GdipAlloc (sizeof (GpMatrix));
if (!result)
return OutOfMemory;
cairo_matrix_init_identity (result);
*matrix = result;
return Ok;
}
// coverity[+alloc : arg-*6]
GpStatus
GdipCreateMatrix2 (float m11, float m12, float m21, float m22, float dx, float dy, GpMatrix **matrix)
{
GpMatrix *result;
if (!matrix)
return InvalidParameter;
result = GdipAlloc (sizeof (GpMatrix));
if (!result)
return OutOfMemory;
cairo_matrix_init (result, m11, m12, m21, m22, dx, dy);
*matrix = result;
return Ok;
}
// coverity[+alloc : arg-*2]
GpStatus
GdipCreateMatrix3 (const GpRectF *rect, const GpPointF *dstplg, GpMatrix **matrix)
{
GpMatrix *result;
GpStatus status;
if (!rect || !dstplg || !matrix)
return InvalidParameter;
result = GdipAlloc (sizeof (GpMatrix));
if (!result)
return OutOfMemory;
status = gdip_matrix_init_from_rect_3points (result, rect, dstplg);
if (status == Ok)
*matrix = result;
else
GdipFree (result);
return status;
}
// coverity[+alloc : arg-*2]
GpStatus
GdipCreateMatrix3I (const GpRect *rect, const GpPoint *dstplg, GpMatrix **matrix)
{
GpRectF r;
GpPointF pts[3];
if (!rect || !dstplg || !matrix)
return InvalidParameter;
r.X = rect->X;
r.Y = rect->Y;
r.Width = rect->Width;
r.Height = rect->Height;
pts [0].X = dstplg [0].X;
pts [0].Y = dstplg [0].Y;
pts [1].X = dstplg [1].X;
pts [1].Y = dstplg [1].Y;
pts [2].X = dstplg [2].X;
pts [2].Y = dstplg [2].Y;
return GdipCreateMatrix3 (&r, (GpPointF*)&pts, matrix);
}
// coverity[+alloc : arg-*1]
GpStatus
GdipCloneMatrix (GpMatrix *matrix, GpMatrix **cloneMatrix)
{
GpMatrix *result;
if (!matrix || !cloneMatrix)
return InvalidParameter;
result = GdipAlloc (sizeof (GpMatrix));
if (!result)
return OutOfMemory;
gdip_cairo_matrix_copy (result, matrix);
*cloneMatrix = result;
return Ok;
}
GpStatus
GdipDeleteMatrix (GpMatrix *matrix)
{
if (!matrix)
return InvalidParameter;
GdipFree (matrix);
return Ok;
}
GpStatus
GdipSetMatrixElements (GpMatrix *matrix, float m11, float m12, float m21, float m22, float dx, float dy)
{
if (!matrix)
return InvalidParameter;
cairo_matrix_init (matrix, m11, m12, m21, m22, dx, dy);
return Ok;
}
GpStatus
GdipGetMatrixElements (GpMatrix *matrix, float *matrixOut)
{
if (!matrix || !matrixOut)
return InvalidParameter;
matrixOut[0] = (float) matrix->xx;
matrixOut[1] = (float) matrix->yx;
matrixOut[2] = (float) matrix->xy;
matrixOut[3] = (float) matrix->yy;
matrixOut[4] = (float) matrix->x0;
matrixOut[5] = (float) matrix->y0;
return Ok;
}
GpStatus
GdipMultiplyMatrix (GpMatrix *matrix, GpMatrix *matrix2, GpMatrixOrder order)
{
if (!matrix || !matrix2)
return InvalidParameter;
if (order == MatrixOrderAppend)
cairo_matrix_multiply (matrix, matrix, matrix2);
else if (order == MatrixOrderPrepend)
cairo_matrix_multiply (matrix, matrix2, matrix);
else
return InvalidParameter;
return Ok;
}
GpStatus
GdipTranslateMatrix (GpMatrix *matrix, float offsetX, float offsetY, GpMatrixOrder order)
{
cairo_matrix_t tmp;
GpStatus s;
cairo_matrix_init (&tmp, 1, 0, 0, 1, offsetX, offsetY);
s = GdipMultiplyMatrix (matrix, &tmp, order);
return s;
}
GpStatus
GdipScaleMatrix (GpMatrix *matrix, float scaleX, float scaleY, GpMatrixOrder order)
{
GpMatrix tmp;
GpStatus s;
cairo_matrix_init (&tmp, scaleX, 0, 0, scaleY, 0, 0);
s = GdipMultiplyMatrix (matrix, &tmp, order);
return s;
}
GpStatus
GdipRotateMatrix (GpMatrix *matrix, float angle, GpMatrixOrder order)
{
cairo_matrix_t tmp;
GpStatus s;
float rad = angle * DEGTORAD;
cairo_matrix_init (&tmp, cos (rad), sin (rad), -sin (rad), cos (rad), 0, 0);
s = GdipMultiplyMatrix (matrix, &tmp, order);
return s;
}
GpStatus
GdipShearMatrix (GpMatrix *matrix, float shearX, float shearY, GpMatrixOrder order)
{
cairo_matrix_t tmp;
GpStatus s;
cairo_matrix_init (&tmp, 1, shearY, shearX, 1, 0, 0);
s = GdipMultiplyMatrix (matrix, &tmp, order);
return s;
}
GpStatus
GdipInvertMatrix (GpMatrix *matrix)
{
if (!matrix)
return InvalidParameter;
return gdip_get_status (
cairo_matrix_invert (matrix));
}
GpStatus
GdipTransformMatrixPoints (GpMatrix *matrix, GpPointF *pts, int count)
{
int i;
if (!matrix || !pts || (count < 1))
return InvalidParameter;
for (i = 0; i < count; i++, pts++) {
double x = pts->X;
double y = pts->Y;
cairo_matrix_transform_point (matrix, &x, &y);
pts->X = (float) x;
pts->Y = (float) y;
}
return Ok;
}
GpStatus
GdipTransformMatrixPointsI (GpMatrix *matrix, GpPoint *pts, int count)
{
int i;
if (!matrix || !pts || (count < 1))
return InvalidParameter;
for (i = 0; i < count; i++, pts++) {
double x = pts->X;
double y = pts->Y;
cairo_matrix_transform_point (matrix, &x, &y);
pts->X = (int) x;
pts->Y = (int) y;
}
return Ok;
}
GpStatus
GdipVectorTransformMatrixPoints (GpMatrix *matrix, GpPointF *pts, int count)
{
int i;
if (!matrix || !pts || (count < 1))
return InvalidParameter;
for (i = 0; i < count; i++, pts++) {
double x = pts->X;
double y = pts->Y;
cairo_matrix_transform_distance (matrix, &x, &y);
pts->X = (float) x;
pts->Y = (float) y;
}
return Ok;
}
GpStatus
GdipVectorTransformMatrixPointsI (GpMatrix *matrix, GpPoint *pts, int count)
{
int i;
if (!matrix || !pts || (count < 1))
return InvalidParameter;
for (i = 0; i < count; i++, pts++) {
double x = pts->X;
double y = pts->Y;
cairo_matrix_transform_distance (matrix, &x, &y);
pts->X = (int) x;
pts->Y = (int) y;
}
return Ok;
}
GpStatus
GdipIsMatrixInvertible (GpMatrix *matrix, BOOL *result)
{
cairo_status_t status;
cairo_matrix_t copy;
if (!matrix || !result)
return InvalidParameter;
gdip_cairo_matrix_copy (©, matrix);
status = cairo_matrix_invert (©);
*result = (status != CAIRO_STATUS_INVALID_MATRIX);
return Ok;
}
GpStatus
GdipIsMatrixIdentity (GpMatrix *matrix, BOOL *result)
{
if (!matrix || !result)
return InvalidParameter;
/* note: we CAN'T use GdipIsMatrixEqual - as the precision is different */
*result = gdip_is_matrix_empty (matrix);
return Ok;
}
GpStatus
GdipIsMatrixEqual (GpMatrix *matrix, GpMatrix *matrix2, BOOL *result)
{
if (!matrix || !matrix2 || !result)
return InvalidParameter;
*result = ((matrix->xx == matrix2->xx) && (matrix->yx == matrix2->yx) && (matrix->xy == matrix2->xy) &&
(matrix->yy == matrix2->yy) && (matrix->x0 == matrix2->x0) && (matrix->y0 == matrix2->y0));
return Ok;
}