3 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
4 * 2005 Lars Knoll & Zack Rusin, Trolltech
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
21 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
22 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
29 #include "pixman-private.h"
32 _pixman_gradient_walker_init (pixman_gradient_walker_t *walker,
33 gradient_t * gradient,
36 walker->num_stops = gradient->n_stops;
37 walker->stops = gradient->stops;
39 walker->right_x = 0x10000;
45 walker->spread = spread;
47 walker->need_reset = TRUE;
51 _pixman_gradient_walker_reset (pixman_gradient_walker_t *walker,
52 pixman_fixed_32_32_t pos)
54 int32_t x, left_x, right_x;
55 pixman_color_t *left_c, *right_c;
56 int n, count = walker->num_stops;
57 pixman_gradient_stop_t * stops = walker->stops;
59 static const pixman_color_t transparent_black = { 0, 0, 0, 0 };
61 switch (walker->spread)
63 case PIXMAN_REPEAT_NORMAL:
64 x = (int32_t)pos & 0xFFFF;
65 for (n = 0; n < count; n++)
70 left_x = stops[count - 1].x - 0x10000;
71 left_c = &stops[count - 1].color;
75 left_x = stops[n - 1].x;
76 left_c = &stops[n - 1].color;
81 right_x = stops[0].x + 0x10000;
82 right_c = &stops[0].color;
87 right_c = &stops[n].color;
93 case PIXMAN_REPEAT_PAD:
94 for (n = 0; n < count; n++)
101 left_c = &stops[0].color;
105 left_x = stops[n - 1].x;
106 left_c = &stops[n - 1].color;
112 right_c = &stops[n - 1].color;
116 right_x = stops[n].x;
117 right_c = &stops[n].color;
121 case PIXMAN_REPEAT_REFLECT:
122 x = (int32_t)pos & 0xFFFF;
123 if ((int32_t)pos & 0x10000)
125 for (n = 0; n < count; n++)
131 left_x = -stops[0].x;
132 left_c = &stops[0].color;
136 left_x = stops[n - 1].x;
137 left_c = &stops[n - 1].color;
142 right_x = 0x20000 - stops[n - 1].x;
143 right_c = &stops[n - 1].color;
147 right_x = stops[n].x;
148 right_c = &stops[n].color;
151 if ((int32_t)pos & 0x10000)
153 pixman_color_t *tmp_c;
156 tmp_x = 0x10000 - right_x;
157 right_x = 0x10000 - left_x;
167 right_x += (pos - x);
170 default: /* REPEAT_NONE */
171 for (n = 0; n < count; n++)
172 if (pos < stops[n].x)
178 right_x = stops[0].x;
179 left_c = right_c = (pixman_color_t*) &transparent_black;
183 left_x = stops[n - 1].x;
185 left_c = right_c = (pixman_color_t*) &transparent_black;
189 left_x = stops[n - 1].x;
190 right_x = stops[n].x;
191 left_c = &stops[n - 1].color;
192 right_c = &stops[n].color;
196 walker->left_x = left_x;
197 walker->right_x = right_x;
198 walker->left_ag = ((left_c->alpha >> 8) << 16) | (left_c->green >> 8);
199 walker->left_rb = ((left_c->red & 0xff00) << 8) | (left_c->blue >> 8);
200 walker->right_ag = ((right_c->alpha >> 8) << 16) | (right_c->green >> 8);
201 walker->right_rb = ((right_c->red & 0xff00) << 8) | (right_c->blue >> 8);
203 if (walker->left_x == walker->right_x ||
204 ( walker->left_ag == walker->right_ag &&
205 walker->left_rb == walker->right_rb ) )
211 int32_t width = right_x - left_x;
212 walker->stepper = ((1 << 24) + width / 2) / width;
215 walker->need_reset = FALSE;
218 #define PIXMAN_GRADIENT_WALKER_NEED_RESET(w, x) \
219 ( (w)->need_reset || (x) < (w)->left_x || (x) >= (w)->right_x)
222 /* the following assumes that PIXMAN_GRADIENT_WALKER_NEED_RESET(w,x) is FALSE */
224 _pixman_gradient_walker_pixel (pixman_gradient_walker_t *walker,
225 pixman_fixed_32_32_t x)
228 uint32_t t1, t2, a, color;
230 if (PIXMAN_GRADIENT_WALKER_NEED_RESET (walker, x))
231 _pixman_gradient_walker_reset (walker, x);
233 dist = ((int)(x - walker->left_x) * walker->stepper) >> 16;
236 /* combined INTERPOLATE and premultiply */
237 t1 = walker->left_rb * idist + walker->right_rb * dist;
238 t1 = (t1 >> 8) & 0xff00ff;
240 t2 = walker->left_ag * idist + walker->right_ag * dist;
243 color = t2 & 0xff000000;
246 t1 = t1 * a + 0x800080;
247 t1 = (t1 + ((t1 >> 8) & 0xff00ff)) >> 8;
249 t2 = (t2 >> 8) * a + 0x800080;
250 t2 = (t2 + ((t2 >> 8) & 0xff00ff));
252 return (color | (t1 & 0xff00ff) | (t2 & 0xff00));