Reindent and reformat pixman-radial-gradient.c
[profile/ivi/pixman.git] / pixman / pixman-radial-gradient.c
1 /*
2  *
3  * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
4  * Copyright © 2000 SuSE, Inc.
5  *             2005 Lars Knoll & Zack Rusin, Trolltech
6  * Copyright © 2007 Red Hat, Inc.
7  *
8  *
9  * Permission to use, copy, modify, distribute, and sell this software and its
10  * documentation for any purpose is hereby granted without fee, provided that
11  * the above copyright notice appear in all copies and that both that
12  * copyright notice and this permission notice appear in supporting
13  * documentation, and that the name of Keith Packard not be used in
14  * advertising or publicity pertaining to distribution of the software without
15  * specific, written prior permission.  Keith Packard makes no
16  * representations about the suitability of this software for any purpose.  It
17  * is provided "as is" without express or implied warranty.
18  *
19  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
20  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
23  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
24  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
25  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
26  * SOFTWARE.
27  */
28
29 #include <config.h>
30 #include <stdlib.h>
31 #include <math.h>
32 #include "pixman-private.h"
33
34 static void
35 radial_gradient_get_scanline_32 (pixman_image_t *image,
36                                  int             x,
37                                  int             y,
38                                  int             width,
39                                  uint32_t *      buffer,
40                                  const uint32_t *mask,
41                                  uint32_t        mask_bits)
42 {
43     /*
44      * In the radial gradient problem we are given two circles (c₁,r₁) and
45      * (c₂,r₂) that define the gradient itself. Then, for any point p, we
46      * must compute the value(s) of t within [0.0, 1.0] representing the
47      * circle(s) that would color the point.
48      *
49      * There are potentially two values of t since the point p can be
50      * colored by both sides of the circle, (which happens whenever one
51      * circle is not entirely contained within the other).
52      *
53      * If we solve for a value of t that is outside of [0.0, 1.0] then we
54      * use the extend mode (NONE, REPEAT, REFLECT, or PAD) to map to a
55      * value within [0.0, 1.0].
56      *
57      * Here is an illustration of the problem:
58      *
59      *              p₂
60      *           p  •
61      *           •   ╲
62      *        ·       ╲r₂
63      *  p₁ ·           ╲
64      *  •              θ╲
65      *   ╲             ╌╌•
66      *    ╲r₁        ·   c₂
67      *    θ╲    ·
68      *    ╌╌•
69      *      c₁
70      *
71      * Given (c₁,r₁), (c₂,r₂) and p, we must find an angle θ such that two
72      * points p₁ and p₂ on the two circles are collinear with p. Then, the
73      * desired value of t is the ratio of the length of p₁p to the length
74      * of p₁p₂.
75      *
76      * So, we have six unknown values: (p₁x, p₁y), (p₂x, p₂y), θ and t.
77      * We can also write six equations that constrain the problem:
78      *
79      * Point p₁ is a distance r₁ from c₁ at an angle of θ:
80      *
81      *  1. p₁x = c₁x + r₁·cos θ
82      *  2. p₁y = c₁y + r₁·sin θ
83      *
84      * Point p₂ is a distance r₂ from c₂ at an angle of θ:
85      *
86      *  3. p₂x = c₂x + r2·cos θ
87      *  4. p₂y = c₂y + r2·sin θ
88      *
89      * Point p lies at a fraction t along the line segment p₁p₂:
90      *
91      *  5. px = t·p₂x + (1-t)·p₁x
92      *  6. py = t·p₂y + (1-t)·p₁y
93      *
94      * To solve, first subtitute 1-4 into 5 and 6:
95      *
96      * px = t·(c₂x + r₂·cos θ) + (1-t)·(c₁x + r₁·cos θ)
97      * py = t·(c₂y + r₂·sin θ) + (1-t)·(c₁y + r₁·sin θ)
98      *
99      * Then solve each for cos θ and sin θ expressed as a function of t:
100      *
101      * cos θ = (-(c₂x - c₁x)·t + (px - c₁x)) / ((r₂-r₁)·t + r₁)
102      * sin θ = (-(c₂y - c₁y)·t + (py - c₁y)) / ((r₂-r₁)·t + r₁)
103      *
104      * To simplify this a bit, we define new variables for several of the
105      * common terms as shown below:
106      *
107      *              p₂
108      *           p  •
109      *           •   ╲
110      *        ·  ┆    ╲r₂
111      *  p₁ ·     ┆     ╲
112      *  •     pdy┆      ╲
113      *   ╲       ┆       •c₂
114      *    ╲r₁    ┆   ·   ┆
115      *     ╲    ·┆       ┆cdy
116      *      •╌╌╌╌┴╌╌╌╌╌╌╌┘
117      *    c₁  pdx   cdx
118      *
119      * cdx = (c₂x - c₁x)
120      * cdy = (c₂y - c₁y)
121      *  dr =  r₂-r₁
122      * pdx =  px - c₁x
123      * pdy =  py - c₁y
124      *
125      * Note that cdx, cdy, and dr do not depend on point p at all, so can
126      * be pre-computed for the entire gradient. The simplifed equations
127      * are now:
128      *
129      * cos θ = (-cdx·t + pdx) / (dr·t + r₁)
130      * sin θ = (-cdy·t + pdy) / (dr·t + r₁)
131      *
132      * Finally, to get a single function of t and eliminate the last
133      * unknown θ, we use the identity sin²θ + cos²θ = 1. First, square
134      * each equation, (we knew a quadratic was coming since it must be
135      * possible to obtain two solutions in some cases):
136      *
137      * cos²θ = (cdx²t² - 2·cdx·pdx·t + pdx²) / (dr²·t² + 2·r₁·dr·t + r₁²)
138      * sin²θ = (cdy²t² - 2·cdy·pdy·t + pdy²) / (dr²·t² + 2·r₁·dr·t + r₁²)
139      *
140      * Then add both together, set the result equal to 1, and express as a
141      * standard quadratic equation in t of the form At² + Bt + C = 0
142      *
143      * (cdx² + cdy² - dr²)·t² - 2·(cdx·pdx + cdy·pdy + r₁·dr)·t + (pdx² + pdy² - r₁²) = 0
144      *
145      * In other words:
146      *
147      * A = cdx² + cdy² - dr²
148      * B = -2·(pdx·cdx + pdy·cdy + r₁·dr)
149      * C = pdx² + pdy² - r₁²
150      *
151      * And again, notice that A does not depend on p, so can be
152      * precomputed. From here we just use the quadratic formula to solve
153      * for t:
154      *
155      * t = (-2·B ± ⎷(B² - 4·A·C)) / 2·A
156      */
157
158     gradient_t *gradient = (gradient_t *)image;
159     source_image_t *source = (source_image_t *)image;
160     radial_gradient_t *radial = (radial_gradient_t *)image;
161     uint32_t *end = buffer + width;
162     pixman_gradient_walker_t walker;
163     pixman_bool_t affine = TRUE;
164     double cx = 1.;
165     double cy = 0.;
166     double cz = 0.;
167     double rx = x + 0.5;
168     double ry = y + 0.5;
169     double rz = 1.;
170
171     _pixman_gradient_walker_init (&walker, gradient, source->common.repeat);
172
173     if (source->common.transform)
174     {
175         pixman_vector_t v;
176         /* reference point is the center of the pixel */
177         v.vector[0] = pixman_int_to_fixed (x) + pixman_fixed_1 / 2;
178         v.vector[1] = pixman_int_to_fixed (y) + pixman_fixed_1 / 2;
179         v.vector[2] = pixman_fixed_1;
180         
181         if (!pixman_transform_point_3d (source->common.transform, &v))
182             return;
183
184         cx = source->common.transform->matrix[0][0] / 65536.;
185         cy = source->common.transform->matrix[1][0] / 65536.;
186         cz = source->common.transform->matrix[2][0] / 65536.;
187         
188         rx = v.vector[0] / 65536.;
189         ry = v.vector[1] / 65536.;
190         rz = v.vector[2] / 65536.;
191
192         affine =
193             source->common.transform->matrix[2][0] == 0 &&
194             v.vector[2] == pixman_fixed_1;
195     }
196
197     if (affine)
198     {
199         while (buffer < end)
200         {
201             if (!mask || *mask++ & mask_bits)
202             {
203                 double pdx, pdy;
204                 double B, C;
205                 double det;
206                 double c1x = radial->c1.x / 65536.0;
207                 double c1y = radial->c1.y / 65536.0;
208                 double r1  = radial->c1.radius / 65536.0;
209                 pixman_fixed_48_16_t t;
210
211                 pdx = rx - c1x;
212                 pdy = ry - c1y;
213
214                 B = -2 * (pdx * radial->cdx +
215                           pdy * radial->cdy +
216                           r1 * radial->dr);
217                 C = pdx * pdx + pdy * pdy - r1 * r1;
218
219                 det = (B * B) - (4 * radial->A * C);
220                 if (det < 0.0)
221                     det = 0.0;
222
223                 if (radial->A < 0)
224                     t = (pixman_fixed_48_16_t) ((-B - sqrt (det)) / (2.0 * radial->A) * 65536);
225                 else
226                     t = (pixman_fixed_48_16_t) ((-B + sqrt (det)) / (2.0 * radial->A) * 65536);
227
228                 *buffer = _pixman_gradient_walker_pixel (&walker, t);
229             }
230             ++buffer;
231
232             rx += cx;
233             ry += cy;
234         }
235     }
236     else
237     {
238         /* projective */
239         while (buffer < end)
240         {
241             if (!mask || *mask++ & mask_bits)
242             {
243                 double pdx, pdy;
244                 double B, C;
245                 double det;
246                 double c1x = radial->c1.x / 65536.0;
247                 double c1y = radial->c1.y / 65536.0;
248                 double r1  = radial->c1.radius / 65536.0;
249                 pixman_fixed_48_16_t t;
250                 double x, y;
251
252                 if (rz != 0)
253                 {
254                     x = rx / rz;
255                     y = ry / rz;
256                 }
257                 else
258                 {
259                     x = y = 0.;
260                 }
261
262                 pdx = x - c1x;
263                 pdy = y - c1y;
264
265                 B = -2 * (pdx * radial->cdx +
266                           pdy * radial->cdy +
267                           r1 * radial->dr);
268                 C = (pdx * pdx + pdy * pdy - r1 * r1);
269
270                 det = (B * B) - (4 * radial->A * C);
271                 if (det < 0.0)
272                     det = 0.0;
273
274                 if (radial->A < 0)
275                     t = (pixman_fixed_48_16_t) ((-B - sqrt (det)) / (2.0 * radial->A) * 65536);
276                 else
277                     t = (pixman_fixed_48_16_t) ((-B + sqrt (det)) / (2.0 * radial->A) * 65536);
278
279                 *buffer = _pixman_gradient_walker_pixel (&walker, t);
280             }
281             
282             ++buffer;
283
284             rx += cx;
285             ry += cy;
286             rz += cz;
287         }
288     }
289 }
290
291 static void
292 radial_gradient_property_changed (pixman_image_t *image)
293 {
294     image->common.get_scanline_32 = radial_gradient_get_scanline_32;
295     image->common.get_scanline_64 = _pixman_image_get_scanline_generic_64;
296 }
297
298 PIXMAN_EXPORT pixman_image_t *
299 pixman_image_create_radial_gradient (pixman_point_fixed_t *        inner,
300                                      pixman_point_fixed_t *        outer,
301                                      pixman_fixed_t                inner_radius,
302                                      pixman_fixed_t                outer_radius,
303                                      const pixman_gradient_stop_t *stops,
304                                      int                           n_stops)
305 {
306     pixman_image_t *image;
307     radial_gradient_t *radial;
308
309     return_val_if_fail (n_stops >= 2, NULL);
310
311     image = _pixman_image_allocate ();
312
313     if (!image)
314         return NULL;
315
316     radial = &image->radial;
317
318     if (!_pixman_init_gradient (&radial->common, stops, n_stops))
319     {
320         free (image);
321         return NULL;
322     }
323
324     image->type = RADIAL;
325
326     radial->c1.x = inner->x;
327     radial->c1.y = inner->y;
328     radial->c1.radius = inner_radius;
329     radial->c2.x = outer->x;
330     radial->c2.y = outer->y;
331     radial->c2.radius = outer_radius;
332     radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
333     radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
334     radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
335     radial->A = (radial->cdx * radial->cdx +
336                  radial->cdy * radial->cdy -
337                  radial->dr  * radial->dr);
338
339     image->common.property_changed = radial_gradient_property_changed;
340
341     radial_gradient_property_changed (image);
342
343     return image;
344 }
345