Move iterator initialization to the respective image files
[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
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <stdlib.h>
32 #include <math.h>
33 #include "pixman-private.h"
34
35 static force_inline double
36 coordinates_to_parameter (double x, double y, double angle)
37 {
38     double t;
39
40     t = atan2 (y, x) + angle;
41
42     while (t < 0)
43         t += 2 * M_PI;
44
45     while (t >= 2 * M_PI)
46         t -= 2 * M_PI;
47
48     return 1 - t * (1 / (2 * M_PI)); /* Scale t to [0, 1] and
49                                       * make rotation CCW
50                                       */
51 }
52
53 static void
54 conical_gradient_get_scanline_32 (pixman_image_t *image,
55                                   int             x,
56                                   int             y,
57                                   int             width,
58                                   uint32_t *      buffer,
59                                   const uint32_t *mask)
60 {
61     gradient_t *gradient = (gradient_t *)image;
62     conical_gradient_t *conical = (conical_gradient_t *)image;
63     uint32_t       *end = buffer + width;
64     pixman_gradient_walker_t walker;
65     pixman_bool_t affine = TRUE;
66     double cx = 1.;
67     double cy = 0.;
68     double cz = 0.;
69     double rx = x + 0.5;
70     double ry = y + 0.5;
71     double rz = 1.;
72
73     _pixman_gradient_walker_init (&walker, gradient, image->common.repeat);
74
75     if (image->common.transform)
76     {
77         pixman_vector_t v;
78
79         /* reference point is the center of the pixel */
80         v.vector[0] = pixman_int_to_fixed (x) + pixman_fixed_1 / 2;
81         v.vector[1] = pixman_int_to_fixed (y) + pixman_fixed_1 / 2;
82         v.vector[2] = pixman_fixed_1;
83
84         if (!pixman_transform_point_3d (image->common.transform, &v))
85             return;
86
87         cx = image->common.transform->matrix[0][0] / 65536.;
88         cy = image->common.transform->matrix[1][0] / 65536.;
89         cz = image->common.transform->matrix[2][0] / 65536.;
90
91         rx = v.vector[0] / 65536.;
92         ry = v.vector[1] / 65536.;
93         rz = v.vector[2] / 65536.;
94
95         affine =
96             image->common.transform->matrix[2][0] == 0 &&
97             v.vector[2] == pixman_fixed_1;
98     }
99
100     if (affine)
101     {
102         rx -= conical->center.x / 65536.;
103         ry -= conical->center.y / 65536.;
104
105         while (buffer < end)
106         {
107             if (!mask || *mask++)
108             {
109                 double t = coordinates_to_parameter (rx, ry, conical->angle);
110
111                 *buffer = _pixman_gradient_walker_pixel (
112                     &walker, (pixman_fixed_48_16_t)pixman_double_to_fixed (t));
113             }
114
115             ++buffer;
116
117             rx += cx;
118             ry += cy;
119         }
120     }
121     else
122     {
123         while (buffer < end)
124         {
125             double x, y;
126
127             if (!mask || *mask++)
128             {
129                 double t;
130
131                 if (rz != 0)
132                 {
133                     x = rx / rz;
134                     y = ry / rz;
135                 }
136                 else
137                 {
138                     x = y = 0.;
139                 }
140
141                 x -= conical->center.x / 65536.;
142                 y -= conical->center.y / 65536.;
143
144                 t = coordinates_to_parameter (x, y, conical->angle);
145
146                 *buffer = _pixman_gradient_walker_pixel (
147                     &walker, (pixman_fixed_48_16_t)pixman_double_to_fixed (t));
148             }
149
150             ++buffer;
151
152             rx += cx;
153             ry += cy;
154             rz += cz;
155         }
156     }
157 }
158
159 static void
160 conical_gradient_property_changed (pixman_image_t *image)
161 {
162     image->common.get_scanline_32 = conical_gradient_get_scanline_32;
163     image->common.get_scanline_64 = _pixman_image_get_scanline_generic_64;
164 }
165
166 static uint32_t *
167 conical_get_scanline_narrow (pixman_iter_t *iter, const uint32_t *mask)
168 {
169     conical_gradient_get_scanline_32 (iter->image, iter->x, iter->y,
170                                       iter->width, iter->buffer,
171                                       mask);
172
173     iter->y++;
174     return iter->buffer;
175 }
176
177 static uint32_t *
178 conical_get_scanline_wide (pixman_iter_t *iter, const uint32_t *mask)
179 {
180     uint32_t *buffer = conical_get_scanline_narrow (iter, NULL);
181
182     pixman_expand ((uint64_t *)buffer, buffer, PIXMAN_a8r8g8b8, iter->width);
183
184     return buffer;
185 }
186
187 void
188 _pixman_conical_gradient_iter_init (pixman_image_t *image,
189                                     pixman_iter_t *iter,
190                                     int x, int y, int width, int height,
191                                     uint8_t *buffer, iter_flags_t flags)
192 {
193     if (flags & ITER_NARROW)
194         iter->get_scanline = conical_get_scanline_narrow;
195     else
196         iter->get_scanline = conical_get_scanline_wide;
197 }
198
199 PIXMAN_EXPORT pixman_image_t *
200 pixman_image_create_conical_gradient (pixman_point_fixed_t *        center,
201                                       pixman_fixed_t                angle,
202                                       const pixman_gradient_stop_t *stops,
203                                       int                           n_stops)
204 {
205     pixman_image_t *image = _pixman_image_allocate ();
206     conical_gradient_t *conical;
207
208     if (!image)
209         return NULL;
210
211     conical = &image->conical;
212
213     if (!_pixman_init_gradient (&conical->common, stops, n_stops))
214     {
215         free (image);
216         return NULL;
217     }
218
219     angle = MOD (angle, pixman_int_to_fixed (360));
220
221     image->type = CONICAL;
222
223     conical->center = *center;
224     conical->angle = (pixman_fixed_to_double (angle) / 180.0) * M_PI;
225
226     image->common.property_changed = conical_gradient_property_changed;
227
228     return image;
229 }
230