Disable MMX when Clang is being used.
[profile/ivi/pixman.git] / demos / radial-test.c
1 #include "../test/utils.h"
2 #include "gtk-utils.h"
3
4 #define NUM_GRADIENTS 7
5 #define NUM_STOPS 3
6 #define NUM_REPEAT 4
7 #define SIZE 128
8 #define WIDTH (SIZE * NUM_GRADIENTS)
9 #define HEIGHT (SIZE * NUM_REPEAT)
10
11 /*
12  * We want to test all the possible relative positions of the start
13  * and end circle:
14  *
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.
18  *
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).
23  *
24  *  - If the two circles have the same size, neither can be inside or
25  *    internally tangent
26  *
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
29  * to right:
30  *
31  * - Small start circle completely inside the end circle
32  *     0.25 -> 1.75; dr =  1.5 > 0; a = 1 - 1.50^2 < 0
33  *
34  * - Small start circle internally tangent to the end circle
35  *     0.50 -> 1.50; dr =  1.0 > 0; a = 1 - 1.00^2 = 0
36  *
37  * - Small start circle outside of the end circle
38  *     0.50 -> 1.00; dr =  0.5 > 0; a = 1 - 0.50^2 > 0
39  *
40  * - Start circle with the same size as the end circle
41  *     1.00 -> 1.00; dr =  0.0 = 0; a = 1 - 0.00^2 > 0
42  *
43  * - Small end circle outside of the start circle
44  *     1.00 -> 0.50; dr = -0.5 > 0; a = 1 - 0.50^2 > 0
45  *
46  * - Small end circle internally tangent to the start circle
47  *     1.50 -> 0.50; dr = -1.0 > 0; a = 1 - 1.00^2 = 0
48  *
49  * - Small end circle completely inside the start circle
50  *     1.75 -> 0.25; dr = -1.5 > 0; a = 1 - 1.50^2 < 0
51  *
52  */
53
54 const static double radiuses[NUM_GRADIENTS] = {
55     0.25,
56     0.50,
57     0.50,
58     1.00,
59     1.00,
60     1.50,
61     1.75
62 };
63
64 #define double_to_color(x)                                      \
65     (((uint32_t) ((x)*65536)) - (((uint32_t) ((x)*65536)) >> 16))
66
67 #define PIXMAN_STOP(offset,r,g,b,a)             \
68     { pixman_double_to_fixed (offset),          \
69         {                                       \
70         double_to_color (r),                    \
71         double_to_color (g),                    \
72         double_to_color (b),                    \
73         double_to_color (a)                     \
74         }                                       \
75     }
76
77 static const pixman_gradient_stop_t stops[NUM_STOPS] = {
78     PIXMAN_STOP (0.0,        1, 0, 0, 0.75),
79     PIXMAN_STOP (0.70710678, 0, 1, 0, 0),
80     PIXMAN_STOP (1.0,        0, 0, 1, 1)
81 };
82
83 static pixman_image_t *
84 create_radial (int index)
85 {
86     pixman_point_fixed_t p0, p1;
87     pixman_fixed_t r0, r1;
88     double x0, x1, radius0, radius1, left, right, center;
89
90     x0 = 0;
91     x1 = 1;
92     radius0 = radiuses[index];
93     radius1 = radiuses[NUM_GRADIENTS - index - 1];
94
95     /* center the gradient */
96     left = MIN (x0 - radius0, x1 - radius1);
97     right = MAX (x0 + radius0, x1 + radius1);
98     center = (left + right) * 0.5;
99     x0 -= center;
100     x1 -= center;
101
102     /* scale to make it fit within a 1x1 rect centered in (0,0) */
103     x0 *= 0.25;
104     x1 *= 0.25;
105     radius0 *= 0.25;
106     radius1 *= 0.25;
107
108     p0.x = pixman_double_to_fixed (x0);
109     p0.y = pixman_double_to_fixed (0);
110
111     p1.x = pixman_double_to_fixed (x1);
112     p1.y = pixman_double_to_fixed (0);
113
114     r0 = pixman_double_to_fixed (radius0);
115     r1 = pixman_double_to_fixed (radius1);
116
117     return pixman_image_create_radial_gradient (&p0, &p1,
118                                                 r0, r1,
119                                                 stops, NUM_STOPS);
120 }
121
122 static const pixman_repeat_t repeat[NUM_REPEAT] = {
123     PIXMAN_REPEAT_NONE,
124     PIXMAN_REPEAT_NORMAL,
125     PIXMAN_REPEAT_REFLECT,
126     PIXMAN_REPEAT_PAD
127 };
128
129 int
130 main (int argc, char **argv)
131 {
132     pixman_transform_t transform;
133     pixman_image_t *src_img, *dest_img;
134     int i, j;
135
136     enable_fp_exceptions ();
137
138     dest_img = pixman_image_create_bits (PIXMAN_a8r8g8b8,
139                                          WIDTH, HEIGHT,
140                                          NULL, 0);
141
142     pixman_transform_init_identity (&transform);
143
144     /*
145      * The create_radial() function returns gradients centered in the
146      * origin and whose interesting part fits a 1x1 square. We want to
147      * paint these gradients on a SIZExSIZE square and to make things
148      * easier we want the origin in the top-left corner of the square
149      * we want to see.
150      */
151     pixman_transform_translate (NULL, &transform,
152                                 pixman_double_to_fixed (0.5),
153                                 pixman_double_to_fixed (0.5));
154
155     pixman_transform_scale (NULL, &transform,
156                             pixman_double_to_fixed (SIZE),
157                             pixman_double_to_fixed (SIZE));
158
159     /*
160      * Gradients are evaluated at the center of each pixel, so we need
161      * to translate by half a pixel to trigger some interesting
162      * cornercases. In particular, the original implementation of PDF
163      * radial gradients tried to divide by 0 when using this transform
164      * on the "tangent circles" cases.
165      */
166     pixman_transform_translate (NULL, &transform,
167                                 pixman_double_to_fixed (0.5),
168                                 pixman_double_to_fixed (0.5));
169
170     for (i = 0; i < NUM_GRADIENTS; i++)
171     {
172         src_img = create_radial (i);
173         pixman_image_set_transform (src_img, &transform);
174
175         for (j = 0; j < NUM_REPEAT; j++)
176         {
177             pixman_image_set_repeat (src_img, repeat[j]);
178
179             pixman_image_composite32 (PIXMAN_OP_OVER,
180                                       src_img,
181                                       NULL,
182                                       dest_img,
183                                       0, 0,
184                                       0, 0,
185                                       i * SIZE, j * SIZE,
186                                       SIZE, SIZE);
187
188         }
189
190         pixman_image_unref (src_img);
191     }
192
193     show_image (dest_img);
194
195     pixman_image_unref (dest_img);
196
197     return 0;
198 }