1 #include "../test/utils.h"
4 #define NUM_GRADIENTS 9
8 #define WIDTH (SIZE * NUM_GRADIENTS)
9 #define HEIGHT (SIZE * NUM_REPEAT)
12 * We want to test all the possible relative positions of the start
15 * - The start circle can be smaller/equal/bigger than the end
16 * circle. A radial gradient can be classified in one of these
17 * three cases depending on the sign of dr.
19 * - The smaller circle can be completely inside/internally
20 * tangent/outside (at least in part) of the bigger circle. This
21 * classification is the same as the one which can be computed by
22 * examining the sign of a = (dx^2 + dy^2 - dr^2).
24 * - If the two circles have the same size, neither can be inside or
27 * This test draws radial gradients whose circles always have the same
28 * centers (0, 0) and (1, 0), but with different radiuses. From left
31 * - Degenerate start circle completely inside the end circle
32 * 0.00 -> 1.75; dr = 1.75 > 0; a = 1 - 1.75^2 < 0
34 * - Small start circle completely inside the end circle
35 * 0.25 -> 1.75; dr = 1.5 > 0; a = 1 - 1.50^2 < 0
37 * - Small start circle internally tangent to the end circle
38 * 0.50 -> 1.50; dr = 1.0 > 0; a = 1 - 1.00^2 = 0
40 * - Small start circle outside of the end circle
41 * 0.50 -> 1.00; dr = 0.5 > 0; a = 1 - 0.50^2 > 0
43 * - Start circle with the same size as the end circle
44 * 1.00 -> 1.00; dr = 0.0 = 0; a = 1 - 0.00^2 > 0
46 * - Small end circle outside of the start circle
47 * 1.00 -> 0.50; dr = -0.5 > 0; a = 1 - 0.50^2 > 0
49 * - Small end circle internally tangent to the start circle
50 * 1.50 -> 0.50; dr = -1.0 > 0; a = 1 - 1.00^2 = 0
52 * - Small end circle completely inside the start circle
53 * 1.75 -> 0.25; dr = -1.5 > 0; a = 1 - 1.50^2 < 0
55 * - Degenerate end circle completely inside the start circle
56 * 0.00 -> 1.75; dr = 1.75 > 0; a = 1 - 1.75^2 < 0
60 const static double radiuses[NUM_GRADIENTS] = {
72 #define double_to_color(x) \
73 (((uint32_t) ((x)*65536)) - (((uint32_t) ((x)*65536)) >> 16))
75 #define PIXMAN_STOP(offset,r,g,b,a) \
76 { pixman_double_to_fixed (offset), \
78 double_to_color (r), \
79 double_to_color (g), \
80 double_to_color (b), \
85 static const pixman_gradient_stop_t stops[NUM_STOPS] = {
86 PIXMAN_STOP (0.0, 1, 0, 0, 0.75),
87 PIXMAN_STOP (0.70710678, 0, 1, 0, 0),
88 PIXMAN_STOP (1.0, 0, 0, 1, 1)
91 static pixman_image_t *
92 create_radial (int index)
94 pixman_point_fixed_t p0, p1;
95 pixman_fixed_t r0, r1;
96 double x0, x1, radius0, radius1, left, right, center;
100 radius0 = radiuses[index];
101 radius1 = radiuses[NUM_GRADIENTS - index - 1];
103 /* center the gradient */
104 left = MIN (x0 - radius0, x1 - radius1);
105 right = MAX (x0 + radius0, x1 + radius1);
106 center = (left + right) * 0.5;
110 /* scale to make it fit within a 1x1 rect centered in (0,0) */
116 p0.x = pixman_double_to_fixed (x0);
117 p0.y = pixman_double_to_fixed (0);
119 p1.x = pixman_double_to_fixed (x1);
120 p1.y = pixman_double_to_fixed (0);
122 r0 = pixman_double_to_fixed (radius0);
123 r1 = pixman_double_to_fixed (radius1);
125 return pixman_image_create_radial_gradient (&p0, &p1,
130 static const pixman_repeat_t repeat[NUM_REPEAT] = {
132 PIXMAN_REPEAT_NORMAL,
133 PIXMAN_REPEAT_REFLECT,
138 main (int argc, char **argv)
140 pixman_transform_t transform;
141 pixman_image_t *src_img, *dest_img;
144 enable_divbyzero_exceptions ();
146 dest_img = pixman_image_create_bits (PIXMAN_a8r8g8b8,
150 draw_checkerboard (dest_img, 25, 0xffaaaaaa, 0xffbbbbbb);
152 pixman_transform_init_identity (&transform);
155 * The create_radial() function returns gradients centered in the
156 * origin and whose interesting part fits a 1x1 square. We want to
157 * paint these gradients on a SIZExSIZE square and to make things
158 * easier we want the origin in the top-left corner of the square
161 pixman_transform_translate (NULL, &transform,
162 pixman_double_to_fixed (0.5),
163 pixman_double_to_fixed (0.5));
165 pixman_transform_scale (NULL, &transform,
166 pixman_double_to_fixed (SIZE),
167 pixman_double_to_fixed (SIZE));
170 * Gradients are evaluated at the center of each pixel, so we need
171 * to translate by half a pixel to trigger some interesting
172 * cornercases. In particular, the original implementation of PDF
173 * radial gradients tried to divide by 0 when using this transform
174 * on the "tangent circles" cases.
176 pixman_transform_translate (NULL, &transform,
177 pixman_double_to_fixed (0.5),
178 pixman_double_to_fixed (0.5));
180 for (i = 0; i < NUM_GRADIENTS; i++)
182 src_img = create_radial (i);
183 pixman_image_set_transform (src_img, &transform);
185 for (j = 0; j < NUM_REPEAT; j++)
187 pixman_image_set_repeat (src_img, repeat[j]);
189 pixman_image_composite32 (PIXMAN_OP_OVER,
200 pixman_image_unref (src_img);
203 show_image (dest_img);
205 pixman_image_unref (dest_img);