Delete the source_image_t struct.
[profile/ivi/pixman.git] / pixman / pixman-radial-gradient.c
1 /* -*- Mode: c; c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- */
2 /*
3  *
4  * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
5  * Copyright © 2000 SuSE, Inc.
6  *             2005 Lars Knoll & Zack Rusin, Trolltech
7  * Copyright © 2007 Red Hat, Inc.
8  *
9  *
10  * Permission to use, copy, modify, distribute, and sell this software and its
11  * documentation for any purpose is hereby granted without fee, provided that
12  * the above copyright notice appear in all copies and that both that
13  * copyright notice and this permission notice appear in supporting
14  * documentation, and that the name of Keith Packard not be used in
15  * advertising or publicity pertaining to distribution of the software without
16  * specific, written prior permission.  Keith Packard makes no
17  * representations about the suitability of this software for any purpose.  It
18  * is provided "as is" without express or implied warranty.
19  *
20  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
21  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
22  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
23  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
25  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
26  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
27  * SOFTWARE.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 #include <stdlib.h>
34 #include <math.h>
35 #include "pixman-private.h"
36
37 static inline pixman_fixed_32_32_t
38 dot (pixman_fixed_48_16_t x1,
39      pixman_fixed_48_16_t y1,
40      pixman_fixed_48_16_t z1,
41      pixman_fixed_48_16_t x2,
42      pixman_fixed_48_16_t y2,
43      pixman_fixed_48_16_t z2)
44 {
45     /*
46      * Exact computation, assuming that the input values can
47      * be represented as pixman_fixed_16_16_t
48      */
49     return x1 * x2 + y1 * y2 + z1 * z2;
50 }
51
52 static inline double
53 fdot (double x1,
54       double y1,
55       double z1,
56       double x2,
57       double y2,
58       double z2)
59 {
60     /*
61      * Error can be unbound in some special cases.
62      * Using clever dot product algorithms (for example compensated
63      * dot product) would improve this but make the code much less
64      * obvious
65      */
66     return x1 * x2 + y1 * y2 + z1 * z2;
67 }
68
69 static uint32_t
70 radial_compute_color (double                    a,
71                       double                    b,
72                       double                    c,
73                       double                    inva,
74                       double                    dr,
75                       double                    mindr,
76                       pixman_gradient_walker_t *walker,
77                       pixman_repeat_t           repeat)
78 {
79     /*
80      * In this function error propagation can lead to bad results:
81      *  - det can have an unbound error (if b*b-a*c is very small),
82      *    potentially making it the opposite sign of what it should have been
83      *    (thus clearing a pixel that would have been colored or vice-versa)
84      *    or propagating the error to sqrtdet;
85      *    if det has the wrong sign or b is very small, this can lead to bad
86      *    results
87      *
88      *  - the algorithm used to compute the solutions of the quadratic
89      *    equation is not numerically stable (but saves one division compared
90      *    to the numerically stable one);
91      *    this can be a problem if a*c is much smaller than b*b
92      *
93      *  - the above problems are worse if a is small (as inva becomes bigger)
94      */
95     double det;
96
97     if (a == 0)
98     {
99         return _pixman_gradient_walker_pixel (walker,
100                                               pixman_fixed_1 / 2 * c / b);
101     }
102
103     det = fdot (b, a, 0, b, -c, 0);
104     if (det >= 0)
105     {
106         double sqrtdet, t0, t1;
107
108         sqrtdet = sqrt (det);
109         t0 = (b + sqrtdet) * inva;
110         t1 = (b - sqrtdet) * inva;
111
112         if (repeat == PIXMAN_REPEAT_NONE)
113         {
114             if (0 <= t0 && t0 <= pixman_fixed_1)
115                 return _pixman_gradient_walker_pixel (walker, t0);
116             else if (0 <= t1 && t1 <= pixman_fixed_1)
117                 return _pixman_gradient_walker_pixel (walker, t1);
118         }
119         else
120         {
121             if (t0 * dr > mindr)
122                 return _pixman_gradient_walker_pixel (walker, t0);
123             else if (t1 * dr > mindr)
124                 return _pixman_gradient_walker_pixel (walker, t1);
125         }
126     }
127
128     return 0;
129 }
130
131 static void
132 radial_gradient_get_scanline_32 (pixman_image_t *image,
133                                  int             x,
134                                  int             y,
135                                  int             width,
136                                  uint32_t *      buffer,
137                                  const uint32_t *mask)
138 {
139     /*
140      * Implementation of radial gradients following the PDF specification.
141      * See section 8.7.4.5.4 Type 3 (Radial) Shadings of the PDF Reference
142      * Manual (PDF 32000-1:2008 at the time of this writing).
143      * 
144      * In the radial gradient problem we are given two circles (c₁,r₁) and
145      * (c₂,r₂) that define the gradient itself.
146      *
147      * Mathematically the gradient can be defined as the family of circles
148      *
149      *     ((1-t)·c₁ + t·(c₂), (1-t)·r₁ + t·r₂)
150      *
151      * excluding those circles whose radius would be < 0. When a point
152      * belongs to more than one circle, the one with a bigger t is the only
153      * one that contributes to its color. When a point does not belong
154      * to any of the circles, it is transparent black, i.e. RGBA (0, 0, 0, 0).
155      * Further limitations on the range of values for t are imposed when
156      * the gradient is not repeated, namely t must belong to [0,1].
157      *
158      * The graphical result is the same as drawing the valid (radius > 0)
159      * circles with increasing t in [-inf, +inf] (or in [0,1] if the gradient
160      * is not repeated) using SOURCE operatior composition.
161      *
162      * It looks like a cone pointing towards the viewer if the ending circle
163      * is smaller than the starting one, a cone pointing inside the page if
164      * the starting circle is the smaller one and like a cylinder if they
165      * have the same radius.
166      *
167      * What we actually do is, given the point whose color we are interested
168      * in, compute the t values for that point, solving for t in:
169      *
170      *     length((1-t)·c₁ + t·(c₂) - p) = (1-t)·r₁ + t·r₂
171      * 
172      * Let's rewrite it in a simpler way, by defining some auxiliary
173      * variables:
174      *
175      *     cd = c₂ - c₁
176      *     pd = p - c₁
177      *     dr = r₂ - r₁
178      *     lenght(t·cd - pd) = r₁ + t·dr
179      *
180      * which actually means
181      *
182      *     hypot(t·cdx - pdx, t·cdy - pdy) = r₁ + t·dr
183      *
184      * or
185      *
186      *     ⎷((t·cdx - pdx)² + (t·cdy - pdy)²) = r₁ + t·dr.
187      *
188      * If we impose (as stated earlier) that r₁ + t·dr >= 0, it becomes:
189      *
190      *     (t·cdx - pdx)² + (t·cdy - pdy)² = (r₁ + t·dr)²
191      *
192      * where we can actually expand the squares and solve for t:
193      *
194      *     t²cdx² - 2t·cdx·pdx + pdx² + t²cdy² - 2t·cdy·pdy + pdy² =
195      *       = r₁² + 2·r₁·t·dr + t²·dr²
196      *
197      *     (cdx² + cdy² - dr²)t² - 2(cdx·pdx + cdy·pdy + r₁·dr)t +
198      *         (pdx² + pdy² - r₁²) = 0
199      *
200      *     A = cdx² + cdy² - dr²
201      *     B = pdx·cdx + pdy·cdy + r₁·dr
202      *     C = pdx² + pdy² - r₁²
203      *     At² - 2Bt + C = 0
204      * 
205      * The solutions (unless the equation degenerates because of A = 0) are:
206      *
207      *     t = (B ± ⎷(B² - A·C)) / A
208      *
209      * The solution we are going to prefer is the bigger one, unless the
210      * radius associated to it is negative (or it falls outside the valid t
211      * range).
212      *
213      * Additional observations (useful for optimizations):
214      * A does not depend on p
215      *
216      * A < 0 <=> one of the two circles completely contains the other one
217      *   <=> for every p, the radiuses associated with the two t solutions
218      *       have opposite sign
219      */
220
221     gradient_t *gradient = (gradient_t *)image;
222     radial_gradient_t *radial = (radial_gradient_t *)image;
223     uint32_t *end = buffer + width;
224     pixman_gradient_walker_t walker;
225     pixman_vector_t v, unit;
226
227     /* reference point is the center of the pixel */
228     v.vector[0] = pixman_int_to_fixed (x) + pixman_fixed_1 / 2;
229     v.vector[1] = pixman_int_to_fixed (y) + pixman_fixed_1 / 2;
230     v.vector[2] = pixman_fixed_1;
231
232     _pixman_gradient_walker_init (&walker, gradient, image->common.repeat);
233
234     if (image->common.transform)
235     {
236         if (!pixman_transform_point_3d (image->common.transform, &v))
237             return;
238         
239         unit.vector[0] = image->common.transform->matrix[0][0];
240         unit.vector[1] = image->common.transform->matrix[1][0];
241         unit.vector[2] = image->common.transform->matrix[2][0];
242     }
243     else
244     {
245         unit.vector[0] = pixman_fixed_1;
246         unit.vector[1] = 0;
247         unit.vector[2] = 0;
248     }
249
250     if (unit.vector[2] == 0 && v.vector[2] == pixman_fixed_1)
251     {
252         /*
253          * Given:
254          *
255          * t = (B ± ⎷(B² - A·C)) / A
256          *
257          * where
258          *
259          * A = cdx² + cdy² - dr²
260          * B = pdx·cdx + pdy·cdy + r₁·dr
261          * C = pdx² + pdy² - r₁²
262          * det = B² - A·C
263          *
264          * Since we have an affine transformation, we know that (pdx, pdy)
265          * increase linearly with each pixel,
266          *
267          * pdx = pdx₀ + n·ux,
268          * pdy = pdy₀ + n·uy,
269          *
270          * we can then express B, C and det through multiple differentiation.
271          */
272         pixman_fixed_32_32_t b, db, c, dc, ddc;
273
274         /* warning: this computation may overflow */
275         v.vector[0] -= radial->c1.x;
276         v.vector[1] -= radial->c1.y;
277
278         /*
279          * B and C are computed and updated exactly.
280          * If fdot was used instead of dot, in the worst case it would
281          * lose 11 bits of precision in each of the multiplication and
282          * summing up would zero out all the bit that were preserved,
283          * thus making the result 0 instead of the correct one.
284          * This would mean a worst case of unbound relative error or
285          * about 2^10 absolute error
286          */
287         b = dot (v.vector[0], v.vector[1], radial->c1.radius,
288                  radial->delta.x, radial->delta.y, radial->delta.radius);
289         db = dot (unit.vector[0], unit.vector[1], 0,
290                   radial->delta.x, radial->delta.y, 0);
291
292         c = dot (v.vector[0], v.vector[1],
293                  -((pixman_fixed_48_16_t) radial->c1.radius),
294                  v.vector[0], v.vector[1], radial->c1.radius);
295         dc = dot (2 * (pixman_fixed_48_16_t) v.vector[0] + unit.vector[0],
296                   2 * (pixman_fixed_48_16_t) v.vector[1] + unit.vector[1],
297                   0,
298                   unit.vector[0], unit.vector[1], 0);
299         ddc = 2 * dot (unit.vector[0], unit.vector[1], 0,
300                        unit.vector[0], unit.vector[1], 0);
301
302         while (buffer < end)
303         {
304             if (!mask || *mask++)
305             {
306                 *buffer = radial_compute_color (radial->a, b, c,
307                                                 radial->inva,
308                                                 radial->delta.radius,
309                                                 radial->mindr,
310                                                 &walker,
311                                                 image->common.repeat);
312             }
313
314             b += db;
315             c += dc;
316             dc += ddc;
317             ++buffer;
318         }
319     }
320     else
321     {
322         /* projective */
323         /* Warning:
324          * error propagation guarantees are much looser than in the affine case
325          */
326         while (buffer < end)
327         {
328             if (!mask || *mask++)
329             {
330                 if (v.vector[2] != 0)
331                 {
332                     double pdx, pdy, invv2, b, c;
333
334                     invv2 = 1. * pixman_fixed_1 / v.vector[2];
335
336                     pdx = v.vector[0] * invv2 - radial->c1.x;
337                     /*    / pixman_fixed_1 */
338
339                     pdy = v.vector[1] * invv2 - radial->c1.y;
340                     /*    / pixman_fixed_1 */
341
342                     b = fdot (pdx, pdy, radial->c1.radius,
343                               radial->delta.x, radial->delta.y,
344                               radial->delta.radius);
345                     /*  / pixman_fixed_1 / pixman_fixed_1 */
346
347                     c = fdot (pdx, pdy, -radial->c1.radius,
348                               pdx, pdy, radial->c1.radius);
349                     /*  / pixman_fixed_1 / pixman_fixed_1 */
350
351                     *buffer = radial_compute_color (radial->a, b, c,
352                                                     radial->inva,
353                                                     radial->delta.radius,
354                                                     radial->mindr,
355                                                     &walker,
356                                                     image->common.repeat);
357                 }
358                 else
359                 {
360                     *buffer = 0;
361                 }
362             }
363
364             ++buffer;
365
366             v.vector[0] += unit.vector[0];
367             v.vector[1] += unit.vector[1];
368             v.vector[2] += unit.vector[2];
369         }
370     }
371 }
372
373 static void
374 radial_gradient_property_changed (pixman_image_t *image)
375 {
376     image->common.get_scanline_32 = radial_gradient_get_scanline_32;
377     image->common.get_scanline_64 = _pixman_image_get_scanline_generic_64;
378 }
379
380 PIXMAN_EXPORT pixman_image_t *
381 pixman_image_create_radial_gradient (pixman_point_fixed_t *        inner,
382                                      pixman_point_fixed_t *        outer,
383                                      pixman_fixed_t                inner_radius,
384                                      pixman_fixed_t                outer_radius,
385                                      const pixman_gradient_stop_t *stops,
386                                      int                           n_stops)
387 {
388     pixman_image_t *image;
389     radial_gradient_t *radial;
390
391     image = _pixman_image_allocate ();
392
393     if (!image)
394         return NULL;
395
396     radial = &image->radial;
397
398     if (!_pixman_init_gradient (&radial->common, stops, n_stops))
399     {
400         free (image);
401         return NULL;
402     }
403
404     image->type = RADIAL;
405
406     radial->c1.x = inner->x;
407     radial->c1.y = inner->y;
408     radial->c1.radius = inner_radius;
409     radial->c2.x = outer->x;
410     radial->c2.y = outer->y;
411     radial->c2.radius = outer_radius;
412
413     /* warning: this computations may overflow */
414     radial->delta.x = radial->c2.x - radial->c1.x;
415     radial->delta.y = radial->c2.y - radial->c1.y;
416     radial->delta.radius = radial->c2.radius - radial->c1.radius;
417
418     /* computed exactly, then cast to double -> every bit of the double
419        representation is correct (53 bits) */
420     radial->a = dot (radial->delta.x, radial->delta.y, -radial->delta.radius,
421                      radial->delta.x, radial->delta.y, radial->delta.radius);
422     if (radial->a != 0)
423         radial->inva = 1. * pixman_fixed_1 / radial->a;
424
425     radial->mindr = -1. * pixman_fixed_1 * radial->c1.radius;
426
427     image->common.property_changed = radial_gradient_property_changed;
428
429     return image;
430 }
431