2 * Copyright © 2008 Keith Packard
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
33 #include "pixman-private.h"
35 #define F(x) pixman_int_to_fixed (x)
37 static force_inline
int
38 count_leading_zeros (uint32_t x
)
40 #ifdef HAVE_BUILTIN_CLZ
41 return __builtin_clz (x
);
54 * Large signed/unsigned integer division with rounding for the platforms with
55 * only 64-bit integer data type supported (no 128-bit data type).
58 * hi, lo - high and low 64-bit parts of the dividend
59 * div - 48-bit divisor
61 * Returns: lowest 64 bits of the result as a return value and highest 64
62 * bits of the result to "result_hi" pointer
65 /* grade-school unsigned division (128-bit by 48-bit) with rounding to nearest */
66 static force_inline
uint64_t
67 rounded_udiv_128_by_48 (uint64_t hi
,
72 uint64_t tmp
, remainder
, result_lo
;
73 assert(div
< ((uint64_t)1 << 48));
76 *result_hi
= hi
/ div
;
78 tmp
= (remainder
<< 16) + (lo
>> 48);
79 result_lo
= tmp
/ div
;
80 remainder
= tmp
% div
;
82 tmp
= (remainder
<< 16) + ((lo
>> 32) & 0xFFFF);
83 result_lo
= (result_lo
<< 16) + (tmp
/ div
);
84 remainder
= tmp
% div
;
86 tmp
= (remainder
<< 16) + ((lo
>> 16) & 0xFFFF);
87 result_lo
= (result_lo
<< 16) + (tmp
/ div
);
88 remainder
= tmp
% div
;
90 tmp
= (remainder
<< 16) + (lo
& 0xFFFF);
91 result_lo
= (result_lo
<< 16) + (tmp
/ div
);
92 remainder
= tmp
% div
;
94 /* round to nearest */
95 if (remainder
* 2 >= div
&& ++result_lo
== 0)
101 /* signed division (128-bit by 49-bit) with rounding to nearest */
102 static inline int64_t
103 rounded_sdiv_128_by_49 (int64_t hi
,
106 int64_t *signed_result_hi
)
108 uint64_t result_lo
, result_hi
;
123 result_lo
= rounded_udiv_128_by_48 (hi
, lo
, div
, &result_hi
);
128 result_hi
= -result_hi
;
129 result_lo
= -result_lo
;
131 if (signed_result_hi
)
133 *signed_result_hi
= result_hi
;
139 * Multiply 64.16 fixed point value by (2^scalebits) and convert
140 * to 128-bit integer.
142 static force_inline
void
143 fixed_64_16_to_int128 (int64_t hi
,
149 /* separate integer and fractional parts */
155 *rlo
= hi
>> (-scalebits
);
160 *rhi
= hi
>> (64 - scalebits
);
161 *rlo
= (uint64_t)hi
<< scalebits
;
163 *rlo
+= lo
>> (16 - scalebits
);
165 *rlo
+= lo
<< (scalebits
- 16);
170 * Convert 112.16 fixed point value to 48.16 with clamping for the out
173 static force_inline pixman_fixed_48_16_t
174 fixed_112_16_to_fixed_48_16 (int64_t hi
, int64_t lo
, pixman_bool_t
*clampflag
)
176 if ((lo
>> 63) != hi
)
179 return hi
>= 0 ? INT64_MAX
: INT64_MIN
;
188 * Transform a point with 31.16 fixed point coordinates from the destination
189 * space to a point with 48.16 fixed point coordinates in the source space.
190 * No overflows are possible for affine transformations and the results are
191 * accurate including the least significant bit. Projective transformations
192 * may overflow, in this case the results are just clamped to return maximum
193 * or minimum 48.16 values (so that the caller can at least handle the NONE
194 * and PAD repeats correctly) and the return value is FALSE to indicate that
195 * such clamping has happened.
197 PIXMAN_EXPORT pixman_bool_t
198 pixman_transform_point_31_16 (const pixman_transform_t
*t
,
199 const pixman_vector_48_16_t
*v
,
200 pixman_vector_48_16_t
*result
)
202 pixman_bool_t clampflag
= FALSE
;
204 int64_t tmp
[3][2], divint
;
207 /* input vector values must have no more than 31 bits (including sign)
208 * in the integer part */
209 assert (v
->v
[0] < ((pixman_fixed_48_16_t
)1 << (30 + 16)));
210 assert (v
->v
[0] >= -((pixman_fixed_48_16_t
)1 << (30 + 16)));
211 assert (v
->v
[1] < ((pixman_fixed_48_16_t
)1 << (30 + 16)));
212 assert (v
->v
[1] >= -((pixman_fixed_48_16_t
)1 << (30 + 16)));
213 assert (v
->v
[2] < ((pixman_fixed_48_16_t
)1 << (30 + 16)));
214 assert (v
->v
[2] >= -((pixman_fixed_48_16_t
)1 << (30 + 16)));
216 for (i
= 0; i
< 3; i
++)
218 tmp
[i
][0] = (int64_t)t
->matrix
[i
][0] * (v
->v
[0] >> 16);
219 tmp
[i
][1] = (int64_t)t
->matrix
[i
][0] * (v
->v
[0] & 0xFFFF);
220 tmp
[i
][0] += (int64_t)t
->matrix
[i
][1] * (v
->v
[1] >> 16);
221 tmp
[i
][1] += (int64_t)t
->matrix
[i
][1] * (v
->v
[1] & 0xFFFF);
222 tmp
[i
][0] += (int64_t)t
->matrix
[i
][2] * (v
->v
[2] >> 16);
223 tmp
[i
][1] += (int64_t)t
->matrix
[i
][2] * (v
->v
[2] & 0xFFFF);
227 * separate 64-bit integer and 16-bit fractional parts for the divisor,
228 * which is also scaled by 65536 after fixed point multiplication.
230 divint
= tmp
[2][0] + (tmp
[2][1] >> 16);
231 divfrac
= tmp
[2][1] & 0xFFFF;
233 if (divint
== pixman_fixed_1
&& divfrac
== 0)
236 * this is a simple affine transformation
238 result
->v
[0] = tmp
[0][0] + ((tmp
[0][1] + 0x8000) >> 16);
239 result
->v
[1] = tmp
[1][0] + ((tmp
[1][1] + 0x8000) >> 16);
240 result
->v
[2] = pixman_fixed_1
;
242 else if (divint
== 0 && divfrac
== 0)
245 * handle zero divisor (if the values are non-zero, set the
246 * results to maximum positive or minimum negative)
250 result
->v
[0] = tmp
[0][0] + ((tmp
[0][1] + 0x8000) >> 16);
251 result
->v
[1] = tmp
[1][0] + ((tmp
[1][1] + 0x8000) >> 16);
253 if (result
->v
[0] > 0)
254 result
->v
[0] = INT64_MAX
;
255 else if (result
->v
[0] < 0)
256 result
->v
[0] = INT64_MIN
;
258 if (result
->v
[1] > 0)
259 result
->v
[1] = INT64_MAX
;
260 else if (result
->v
[1] < 0)
261 result
->v
[1] = INT64_MIN
;
266 * projective transformation, analyze the top 32 bits of the divisor
268 int32_t hi32divbits
= divint
>> 32;
270 hi32divbits
= ~hi32divbits
;
272 if (hi32divbits
== 0)
274 /* the divisor is small, we can actually keep all the bits */
275 int64_t hi
, rhi
, lo
, rlo
;
276 int64_t div
= (divint
<< 16) + divfrac
;
278 fixed_64_16_to_int128 (tmp
[0][0], tmp
[0][1], &hi
, &lo
, 32);
279 rlo
= rounded_sdiv_128_by_49 (hi
, lo
, div
, &rhi
);
280 result
->v
[0] = fixed_112_16_to_fixed_48_16 (rhi
, rlo
, &clampflag
);
282 fixed_64_16_to_int128 (tmp
[1][0], tmp
[1][1], &hi
, &lo
, 32);
283 rlo
= rounded_sdiv_128_by_49 (hi
, lo
, div
, &rhi
);
284 result
->v
[1] = fixed_112_16_to_fixed_48_16 (rhi
, rlo
, &clampflag
);
288 /* the divisor needs to be reduced to 48 bits */
289 int64_t hi
, rhi
, lo
, rlo
, div
;
290 int shift
= 32 - count_leading_zeros (hi32divbits
);
291 fixed_64_16_to_int128 (divint
, divfrac
, &hi
, &div
, 16 - shift
);
293 fixed_64_16_to_int128 (tmp
[0][0], tmp
[0][1], &hi
, &lo
, 32 - shift
);
294 rlo
= rounded_sdiv_128_by_49 (hi
, lo
, div
, &rhi
);
295 result
->v
[0] = fixed_112_16_to_fixed_48_16 (rhi
, rlo
, &clampflag
);
297 fixed_64_16_to_int128 (tmp
[1][0], tmp
[1][1], &hi
, &lo
, 32 - shift
);
298 rlo
= rounded_sdiv_128_by_49 (hi
, lo
, div
, &rhi
);
299 result
->v
[1] = fixed_112_16_to_fixed_48_16 (rhi
, rlo
, &clampflag
);
302 result
->v
[2] = pixman_fixed_1
;
307 pixman_transform_point_31_16_affine (const pixman_transform_t
*t
,
308 const pixman_vector_48_16_t
*v
,
309 pixman_vector_48_16_t
*result
)
311 int64_t hi0
, lo0
, hi1
, lo1
;
313 /* input vector values must have no more than 31 bits (including sign)
314 * in the integer part */
315 assert (v
->v
[0] < ((pixman_fixed_48_16_t
)1 << (30 + 16)));
316 assert (v
->v
[0] >= -((pixman_fixed_48_16_t
)1 << (30 + 16)));
317 assert (v
->v
[1] < ((pixman_fixed_48_16_t
)1 << (30 + 16)));
318 assert (v
->v
[1] >= -((pixman_fixed_48_16_t
)1 << (30 + 16)));
320 hi0
= (int64_t)t
->matrix
[0][0] * (v
->v
[0] >> 16);
321 lo0
= (int64_t)t
->matrix
[0][0] * (v
->v
[0] & 0xFFFF);
322 hi0
+= (int64_t)t
->matrix
[0][1] * (v
->v
[1] >> 16);
323 lo0
+= (int64_t)t
->matrix
[0][1] * (v
->v
[1] & 0xFFFF);
324 hi0
+= (int64_t)t
->matrix
[0][2];
326 hi1
= (int64_t)t
->matrix
[1][0] * (v
->v
[0] >> 16);
327 lo1
= (int64_t)t
->matrix
[1][0] * (v
->v
[0] & 0xFFFF);
328 hi1
+= (int64_t)t
->matrix
[1][1] * (v
->v
[1] >> 16);
329 lo1
+= (int64_t)t
->matrix
[1][1] * (v
->v
[1] & 0xFFFF);
330 hi1
+= (int64_t)t
->matrix
[1][2];
332 result
->v
[0] = hi0
+ ((lo0
+ 0x8000) >> 16);
333 result
->v
[1] = hi1
+ ((lo1
+ 0x8000) >> 16);
334 result
->v
[2] = pixman_fixed_1
;
338 pixman_transform_point_31_16_3d (const pixman_transform_t
*t
,
339 const pixman_vector_48_16_t
*v
,
340 pixman_vector_48_16_t
*result
)
345 /* input vector values must have no more than 31 bits (including sign)
346 * in the integer part */
347 assert (v
->v
[0] < ((pixman_fixed_48_16_t
)1 << (30 + 16)));
348 assert (v
->v
[0] >= -((pixman_fixed_48_16_t
)1 << (30 + 16)));
349 assert (v
->v
[1] < ((pixman_fixed_48_16_t
)1 << (30 + 16)));
350 assert (v
->v
[1] >= -((pixman_fixed_48_16_t
)1 << (30 + 16)));
351 assert (v
->v
[2] < ((pixman_fixed_48_16_t
)1 << (30 + 16)));
352 assert (v
->v
[2] >= -((pixman_fixed_48_16_t
)1 << (30 + 16)));
354 for (i
= 0; i
< 3; i
++)
356 tmp
[i
][0] = (int64_t)t
->matrix
[i
][0] * (v
->v
[0] >> 16);
357 tmp
[i
][1] = (int64_t)t
->matrix
[i
][0] * (v
->v
[0] & 0xFFFF);
358 tmp
[i
][0] += (int64_t)t
->matrix
[i
][1] * (v
->v
[1] >> 16);
359 tmp
[i
][1] += (int64_t)t
->matrix
[i
][1] * (v
->v
[1] & 0xFFFF);
360 tmp
[i
][0] += (int64_t)t
->matrix
[i
][2] * (v
->v
[2] >> 16);
361 tmp
[i
][1] += (int64_t)t
->matrix
[i
][2] * (v
->v
[2] & 0xFFFF);
364 result
->v
[0] = tmp
[0][0] + ((tmp
[0][1] + 0x8000) >> 16);
365 result
->v
[1] = tmp
[1][0] + ((tmp
[1][1] + 0x8000) >> 16);
366 result
->v
[2] = tmp
[2][0] + ((tmp
[2][1] + 0x8000) >> 16);
370 pixman_transform_init_identity (struct pixman_transform
*matrix
)
374 memset (matrix
, '\0', sizeof (struct pixman_transform
));
375 for (i
= 0; i
< 3; i
++)
376 matrix
->matrix
[i
][i
] = F (1);
379 typedef pixman_fixed_32_32_t pixman_fixed_34_30_t
;
381 PIXMAN_EXPORT pixman_bool_t
382 pixman_transform_point_3d (const struct pixman_transform
*transform
,
383 struct pixman_vector
* vector
)
385 pixman_vector_48_16_t tmp
;
386 tmp
.v
[0] = vector
->vector
[0];
387 tmp
.v
[1] = vector
->vector
[1];
388 tmp
.v
[2] = vector
->vector
[2];
390 pixman_transform_point_31_16_3d (transform
, &tmp
, &tmp
);
392 vector
->vector
[0] = tmp
.v
[0];
393 vector
->vector
[1] = tmp
.v
[1];
394 vector
->vector
[2] = tmp
.v
[2];
396 return vector
->vector
[0] == tmp
.v
[0] &&
397 vector
->vector
[1] == tmp
.v
[1] &&
398 vector
->vector
[2] == tmp
.v
[2];
401 PIXMAN_EXPORT pixman_bool_t
402 pixman_transform_point (const struct pixman_transform
*transform
,
403 struct pixman_vector
* vector
)
405 pixman_vector_48_16_t tmp
;
406 tmp
.v
[0] = vector
->vector
[0];
407 tmp
.v
[1] = vector
->vector
[1];
408 tmp
.v
[2] = vector
->vector
[2];
410 if (!pixman_transform_point_31_16 (transform
, &tmp
, &tmp
))
413 vector
->vector
[0] = tmp
.v
[0];
414 vector
->vector
[1] = tmp
.v
[1];
415 vector
->vector
[2] = tmp
.v
[2];
417 return vector
->vector
[0] == tmp
.v
[0] &&
418 vector
->vector
[1] == tmp
.v
[1] &&
419 vector
->vector
[2] == tmp
.v
[2];
422 PIXMAN_EXPORT pixman_bool_t
423 pixman_transform_multiply (struct pixman_transform
* dst
,
424 const struct pixman_transform
*l
,
425 const struct pixman_transform
*r
)
427 struct pixman_transform d
;
431 for (dy
= 0; dy
< 3; dy
++)
433 for (dx
= 0; dx
< 3; dx
++)
435 pixman_fixed_48_16_t v
;
436 pixman_fixed_32_32_t partial
;
439 for (o
= 0; o
< 3; o
++)
442 (pixman_fixed_32_32_t
) l
->matrix
[dy
][o
] *
443 (pixman_fixed_32_32_t
) r
->matrix
[o
][dx
];
445 v
+= (partial
+ 0x8000) >> 16;
448 if (v
> pixman_max_fixed_48_16
|| v
< pixman_min_fixed_48_16
)
451 d
.matrix
[dy
][dx
] = (pixman_fixed_t
) v
;
460 pixman_transform_init_scale (struct pixman_transform
*t
,
464 memset (t
, '\0', sizeof (struct pixman_transform
));
466 t
->matrix
[0][0] = sx
;
467 t
->matrix
[1][1] = sy
;
468 t
->matrix
[2][2] = F (1);
471 static pixman_fixed_t
472 fixed_inverse (pixman_fixed_t x
)
474 return (pixman_fixed_t
) ((((pixman_fixed_48_16_t
) F (1)) * F (1)) / x
);
477 PIXMAN_EXPORT pixman_bool_t
478 pixman_transform_scale (struct pixman_transform
*forward
,
479 struct pixman_transform
*reverse
,
483 struct pixman_transform t
;
485 if (sx
== 0 || sy
== 0)
490 pixman_transform_init_scale (&t
, sx
, sy
);
491 if (!pixman_transform_multiply (forward
, &t
, forward
))
497 pixman_transform_init_scale (&t
, fixed_inverse (sx
),
499 if (!pixman_transform_multiply (reverse
, reverse
, &t
))
507 pixman_transform_init_rotate (struct pixman_transform
*t
,
511 memset (t
, '\0', sizeof (struct pixman_transform
));
514 t
->matrix
[0][1] = -s
;
517 t
->matrix
[2][2] = F (1);
520 PIXMAN_EXPORT pixman_bool_t
521 pixman_transform_rotate (struct pixman_transform
*forward
,
522 struct pixman_transform
*reverse
,
526 struct pixman_transform t
;
530 pixman_transform_init_rotate (&t
, c
, s
);
531 if (!pixman_transform_multiply (forward
, &t
, forward
))
537 pixman_transform_init_rotate (&t
, c
, -s
);
538 if (!pixman_transform_multiply (reverse
, reverse
, &t
))
546 pixman_transform_init_translate (struct pixman_transform
*t
,
550 memset (t
, '\0', sizeof (struct pixman_transform
));
552 t
->matrix
[0][0] = F (1);
553 t
->matrix
[0][2] = tx
;
554 t
->matrix
[1][1] = F (1);
555 t
->matrix
[1][2] = ty
;
556 t
->matrix
[2][2] = F (1);
559 PIXMAN_EXPORT pixman_bool_t
560 pixman_transform_translate (struct pixman_transform
*forward
,
561 struct pixman_transform
*reverse
,
565 struct pixman_transform t
;
569 pixman_transform_init_translate (&t
, tx
, ty
);
571 if (!pixman_transform_multiply (forward
, &t
, forward
))
577 pixman_transform_init_translate (&t
, -tx
, -ty
);
579 if (!pixman_transform_multiply (reverse
, reverse
, &t
))
585 PIXMAN_EXPORT pixman_bool_t
586 pixman_transform_bounds (const struct pixman_transform
*matrix
,
587 struct pixman_box16
* b
)
590 struct pixman_vector v
[4];
594 v
[0].vector
[0] = F (b
->x1
);
595 v
[0].vector
[1] = F (b
->y1
);
596 v
[0].vector
[2] = F (1);
598 v
[1].vector
[0] = F (b
->x2
);
599 v
[1].vector
[1] = F (b
->y1
);
600 v
[1].vector
[2] = F (1);
602 v
[2].vector
[0] = F (b
->x2
);
603 v
[2].vector
[1] = F (b
->y2
);
604 v
[2].vector
[2] = F (1);
606 v
[3].vector
[0] = F (b
->x1
);
607 v
[3].vector
[1] = F (b
->y2
);
608 v
[3].vector
[2] = F (1);
610 for (i
= 0; i
< 4; i
++)
612 if (!pixman_transform_point (matrix
, &v
[i
]))
615 x1
= pixman_fixed_to_int (v
[i
].vector
[0]);
616 y1
= pixman_fixed_to_int (v
[i
].vector
[1]);
617 x2
= pixman_fixed_to_int (pixman_fixed_ceil (v
[i
].vector
[0]));
618 y2
= pixman_fixed_to_int (pixman_fixed_ceil (v
[i
].vector
[1]));
629 if (x1
< b
->x1
) b
->x1
= x1
;
630 if (y1
< b
->y1
) b
->y1
= y1
;
631 if (x2
> b
->x2
) b
->x2
= x2
;
632 if (y2
> b
->y2
) b
->y2
= y2
;
639 PIXMAN_EXPORT pixman_bool_t
640 pixman_transform_invert (struct pixman_transform
* dst
,
641 const struct pixman_transform
*src
)
643 struct pixman_f_transform m
;
645 pixman_f_transform_from_pixman_transform (&m
, src
);
647 if (!pixman_f_transform_invert (&m
, &m
))
650 if (!pixman_transform_from_pixman_f_transform (dst
, &m
))
657 within_epsilon (pixman_fixed_t a
,
659 pixman_fixed_t epsilon
)
661 pixman_fixed_t t
= a
- b
;
669 #define EPSILON (pixman_fixed_t) (2)
671 #define IS_SAME(a, b) (within_epsilon (a, b, EPSILON))
672 #define IS_ZERO(a) (within_epsilon (a, 0, EPSILON))
673 #define IS_ONE(a) (within_epsilon (a, F (1), EPSILON))
675 (within_epsilon (a, F (1), EPSILON) || \
676 within_epsilon (a, F (-1), EPSILON) || \
678 #define IS_INT(a) (IS_ZERO (pixman_fixed_frac (a)))
680 PIXMAN_EXPORT pixman_bool_t
681 pixman_transform_is_identity (const struct pixman_transform
*t
)
683 return (IS_SAME (t
->matrix
[0][0], t
->matrix
[1][1]) &&
684 IS_SAME (t
->matrix
[0][0], t
->matrix
[2][2]) &&
685 !IS_ZERO (t
->matrix
[0][0]) &&
686 IS_ZERO (t
->matrix
[0][1]) &&
687 IS_ZERO (t
->matrix
[0][2]) &&
688 IS_ZERO (t
->matrix
[1][0]) &&
689 IS_ZERO (t
->matrix
[1][2]) &&
690 IS_ZERO (t
->matrix
[2][0]) &&
691 IS_ZERO (t
->matrix
[2][1]));
694 PIXMAN_EXPORT pixman_bool_t
695 pixman_transform_is_scale (const struct pixman_transform
*t
)
697 return (!IS_ZERO (t
->matrix
[0][0]) &&
698 IS_ZERO (t
->matrix
[0][1]) &&
699 IS_ZERO (t
->matrix
[0][2]) &&
701 IS_ZERO (t
->matrix
[1][0]) &&
702 !IS_ZERO (t
->matrix
[1][1]) &&
703 IS_ZERO (t
->matrix
[1][2]) &&
705 IS_ZERO (t
->matrix
[2][0]) &&
706 IS_ZERO (t
->matrix
[2][1]) &&
707 !IS_ZERO (t
->matrix
[2][2]));
710 PIXMAN_EXPORT pixman_bool_t
711 pixman_transform_is_int_translate (const struct pixman_transform
*t
)
713 return (IS_ONE (t
->matrix
[0][0]) &&
714 IS_ZERO (t
->matrix
[0][1]) &&
715 IS_INT (t
->matrix
[0][2]) &&
717 IS_ZERO (t
->matrix
[1][0]) &&
718 IS_ONE (t
->matrix
[1][1]) &&
719 IS_INT (t
->matrix
[1][2]) &&
721 IS_ZERO (t
->matrix
[2][0]) &&
722 IS_ZERO (t
->matrix
[2][1]) &&
723 IS_ONE (t
->matrix
[2][2]));
726 PIXMAN_EXPORT pixman_bool_t
727 pixman_transform_is_inverse (const struct pixman_transform
*a
,
728 const struct pixman_transform
*b
)
730 struct pixman_transform t
;
732 if (!pixman_transform_multiply (&t
, a
, b
))
735 return pixman_transform_is_identity (&t
);
739 pixman_f_transform_from_pixman_transform (struct pixman_f_transform
* ft
,
740 const struct pixman_transform
*t
)
744 for (j
= 0; j
< 3; j
++)
746 for (i
= 0; i
< 3; i
++)
747 ft
->m
[j
][i
] = pixman_fixed_to_double (t
->matrix
[j
][i
]);
751 PIXMAN_EXPORT pixman_bool_t
752 pixman_transform_from_pixman_f_transform (struct pixman_transform
* t
,
753 const struct pixman_f_transform
*ft
)
757 for (j
= 0; j
< 3; j
++)
759 for (i
= 0; i
< 3; i
++)
761 double d
= ft
->m
[j
][i
];
762 if (d
< -32767.0 || d
> 32767.0)
764 d
= d
* 65536.0 + 0.5;
765 t
->matrix
[j
][i
] = (pixman_fixed_t
) floor (d
);
772 PIXMAN_EXPORT pixman_bool_t
773 pixman_f_transform_invert (struct pixman_f_transform
* dst
,
774 const struct pixman_f_transform
*src
)
776 static const int a
[3] = { 2, 2, 1 };
777 static const int b
[3] = { 1, 0, 0 };
778 pixman_f_transform_t d
;
783 for (i
= 0; i
< 3; i
++)
788 p
= src
->m
[i
][0] * (src
->m
[ai
][2] * src
->m
[bi
][1] -
789 src
->m
[ai
][1] * src
->m
[bi
][2]);
799 for (j
= 0; j
< 3; j
++)
801 for (i
= 0; i
< 3; i
++)
809 p
= (src
->m
[ai
][aj
] * src
->m
[bi
][bj
] -
810 src
->m
[ai
][bj
] * src
->m
[bi
][aj
]);
812 if (((i
+ j
) & 1) != 0)
824 PIXMAN_EXPORT pixman_bool_t
825 pixman_f_transform_point (const struct pixman_f_transform
*t
,
826 struct pixman_f_vector
* v
)
828 struct pixman_f_vector result
;
832 for (j
= 0; j
< 3; j
++)
835 for (i
= 0; i
< 3; i
++)
836 a
+= t
->m
[j
][i
] * v
->v
[i
];
843 for (j
= 0; j
< 2; j
++)
844 v
->v
[j
] = result
.v
[j
] / result
.v
[2];
852 pixman_f_transform_point_3d (const struct pixman_f_transform
*t
,
853 struct pixman_f_vector
* v
)
855 struct pixman_f_vector result
;
859 for (j
= 0; j
< 3; j
++)
862 for (i
= 0; i
< 3; i
++)
863 a
+= t
->m
[j
][i
] * v
->v
[i
];
871 pixman_f_transform_multiply (struct pixman_f_transform
* dst
,
872 const struct pixman_f_transform
*l
,
873 const struct pixman_f_transform
*r
)
875 struct pixman_f_transform d
;
879 for (dy
= 0; dy
< 3; dy
++)
881 for (dx
= 0; dx
< 3; dx
++)
884 for (o
= 0; o
< 3; o
++)
885 v
+= l
->m
[dy
][o
] * r
->m
[o
][dx
];
894 pixman_f_transform_init_scale (struct pixman_f_transform
*t
,
909 PIXMAN_EXPORT pixman_bool_t
910 pixman_f_transform_scale (struct pixman_f_transform
*forward
,
911 struct pixman_f_transform
*reverse
,
915 struct pixman_f_transform t
;
917 if (sx
== 0 || sy
== 0)
922 pixman_f_transform_init_scale (&t
, sx
, sy
);
923 pixman_f_transform_multiply (forward
, &t
, forward
);
928 pixman_f_transform_init_scale (&t
, 1 / sx
, 1 / sy
);
929 pixman_f_transform_multiply (reverse
, reverse
, &t
);
936 pixman_f_transform_init_rotate (struct pixman_f_transform
*t
,
951 PIXMAN_EXPORT pixman_bool_t
952 pixman_f_transform_rotate (struct pixman_f_transform
*forward
,
953 struct pixman_f_transform
*reverse
,
957 struct pixman_f_transform t
;
961 pixman_f_transform_init_rotate (&t
, c
, s
);
962 pixman_f_transform_multiply (forward
, &t
, forward
);
967 pixman_f_transform_init_rotate (&t
, c
, -s
);
968 pixman_f_transform_multiply (reverse
, reverse
, &t
);
975 pixman_f_transform_init_translate (struct pixman_f_transform
*t
,
990 PIXMAN_EXPORT pixman_bool_t
991 pixman_f_transform_translate (struct pixman_f_transform
*forward
,
992 struct pixman_f_transform
*reverse
,
996 struct pixman_f_transform t
;
1000 pixman_f_transform_init_translate (&t
, tx
, ty
);
1001 pixman_f_transform_multiply (forward
, &t
, forward
);
1006 pixman_f_transform_init_translate (&t
, -tx
, -ty
);
1007 pixman_f_transform_multiply (reverse
, reverse
, &t
);
1013 PIXMAN_EXPORT pixman_bool_t
1014 pixman_f_transform_bounds (const struct pixman_f_transform
*t
,
1015 struct pixman_box16
* b
)
1017 struct pixman_f_vector v
[4];
1034 for (i
= 0; i
< 4; i
++)
1036 if (!pixman_f_transform_point (t
, &v
[i
]))
1039 x1
= floor (v
[i
].v
[0]);
1040 y1
= floor (v
[i
].v
[1]);
1041 x2
= ceil (v
[i
].v
[0]);
1042 y2
= ceil (v
[i
].v
[1]);
1053 if (x1
< b
->x1
) b
->x1
= x1
;
1054 if (y1
< b
->y1
) b
->y1
= y1
;
1055 if (x2
> b
->x2
) b
->x2
= x2
;
1056 if (y2
> b
->y2
) b
->y2
= y2
;
1064 pixman_f_transform_init_identity (struct pixman_f_transform
*t
)
1068 for (j
= 0; j
< 3; j
++)
1070 for (i
= 0; i
< 3; i
++)
1071 t
->m
[j
][i
] = i
== j
? 1 : 0;