ea6744f012eb298844b862dc72679d484004df69
[profile/ivi/pixman.git] / pixman / pixman-conical-gradient.c
1 /*
2  * Copyright © 2000 SuSE, Inc.
3  * Copyright © 2007 Red Hat, Inc.
4  * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
5  *             2005 Lars Knoll & Zack Rusin, Trolltech
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of Keith Packard not be used in
12  * advertising or publicity pertaining to distribution of the software without
13  * specific, written prior permission.  Keith Packard makes no
14  * representations about the suitability of this software for any purpose.  It
15  * is provided "as is" without express or implied warranty.
16  *
17  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
18  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
22  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
23  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24  * SOFTWARE.
25  */
26 #include <config.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include "pixman-private.h"
30
31 static void
32 conical_gradient_get_scanline_32 (pixman_image_t *image,
33                                   int x, int y, int width,
34                                   uint32_t *buffer,
35                                   uint32_t *mask, uint32_t maskBits)
36 {
37     source_image_t *source = (source_image_t *)image;
38     gradient_t *gradient = (gradient_t *)source;
39     conical_gradient_t *conical = (conical_gradient_t *)image;
40     uint32_t       *end = buffer + width;
41     pixman_gradient_walker_t  walker;
42     pixman_bool_t affine = TRUE;
43     double cx = 1.;
44     double cy = 0.;
45     double cz = 0.;
46     double rx = x + 0.5;
47     double ry = y + 0.5;
48     double rz = 1.;
49     double a = conical->angle/(180.*65536);
50
51     _pixman_gradient_walker_init (&walker, gradient, source->common.repeat);
52     
53     if (source->common.transform) {
54         pixman_vector_t v;
55         /* reference point is the center of the pixel */
56         v.vector[0] = pixman_int_to_fixed(x) + pixman_fixed_1/2;
57         v.vector[1] = pixman_int_to_fixed(y) + pixman_fixed_1/2;
58         v.vector[2] = pixman_fixed_1;
59         if (!pixman_transform_point_3d (source->common.transform, &v))
60             return;
61         
62         cx = source->common.transform->matrix[0][0]/65536.;
63         cy = source->common.transform->matrix[1][0]/65536.;
64         cz = source->common.transform->matrix[2][0]/65536.;
65         rx = v.vector[0]/65536.;
66         ry = v.vector[1]/65536.;
67         rz = v.vector[2]/65536.;
68         affine = source->common.transform->matrix[2][0] == 0 && v.vector[2] == pixman_fixed_1;
69     }
70     
71     if (affine) {
72         rx -= conical->center.x/65536.;
73         ry -= conical->center.y/65536.;
74         
75         while (buffer < end) {
76             double angle;
77             
78             if (!mask || *mask++ & maskBits)
79             {
80                 pixman_fixed_48_16_t   t;
81                 
82                 angle = atan2(ry, rx) + a;
83                 t     = (pixman_fixed_48_16_t) (angle * (65536. / (2*M_PI)));
84                 
85                 *(buffer) = _pixman_gradient_walker_pixel (&walker, t);
86             }
87             
88             ++buffer;
89             rx += cx;
90             ry += cy;
91         }
92     } else {
93         while (buffer < end) {
94             double x, y;
95             double angle;
96             
97             if (!mask || *mask++ & maskBits)
98             {
99                 pixman_fixed_48_16_t  t;
100                 
101                 if (rz != 0) {
102                     x = rx/rz;
103                     y = ry/rz;
104                 } else {
105                     x = y = 0.;
106                 }
107                 x -= conical->center.x/65536.;
108                 y -= conical->center.y/65536.;
109                 angle = atan2(y, x) + a;
110                 t     = (pixman_fixed_48_16_t) (angle * (65536. / (2*M_PI)));
111                 
112                 *(buffer) = _pixman_gradient_walker_pixel (&walker, t);
113             }
114             
115             ++buffer;
116             rx += cx;
117             ry += cy;
118             rz += cz;
119         }
120     }
121 }
122
123 static void
124 conical_gradient_property_changed (pixman_image_t *image)
125 {
126     image->common.get_scanline_32 = conical_gradient_get_scanline_32;
127     image->common.get_scanline_64 = _pixman_image_get_scanline_64_generic;
128 }
129
130 PIXMAN_EXPORT pixman_image_t *
131 pixman_image_create_conical_gradient (pixman_point_fixed_t         *center,
132                                       pixman_fixed_t                angle,
133                                       const pixman_gradient_stop_t *stops,
134                                       int                           n_stops)
135 {
136     pixman_image_t *image = _pixman_image_allocate();
137     conical_gradient_t *conical;
138     
139     if (!image)
140         return NULL;
141     
142     conical = &image->conical;
143     
144     if (!_pixman_init_gradient (&conical->common, stops, n_stops))
145     {
146         free (image);
147         return NULL;
148     }
149     
150     image->type = CONICAL;
151     conical->center = *center;
152     conical->angle = angle;
153     
154     image->common.property_changed = conical_gradient_property_changed;
155     
156     conical_gradient_property_changed (image);
157     
158     return image;
159 }