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
27 #include "pixman-private.h"
30 _pixman_gradient_walker_init (pixman_gradient_walker_t *walker,
31 gradient_t * gradient,
34 walker->num_stops = gradient->n_stops;
35 walker->stops = gradient->stops;
37 walker->right_x = 0x10000;
43 walker->spread = spread;
45 walker->need_reset = TRUE;
49 _pixman_gradient_walker_reset (pixman_gradient_walker_t *walker,
50 pixman_fixed_32_32_t pos)
52 int32_t x, left_x, right_x;
53 pixman_color_t *left_c, *right_c;
54 int n, count = walker->num_stops;
55 pixman_gradient_stop_t * stops = walker->stops;
57 static const pixman_color_t transparent_black = { 0, 0, 0, 0 };
59 switch (walker->spread)
61 case PIXMAN_REPEAT_NORMAL:
62 x = (int32_t)pos & 0xFFFF;
63 for (n = 0; n < count; n++)
68 left_x = stops[count - 1].x - 0x10000;
69 left_c = &stops[count - 1].color;
73 left_x = stops[n - 1].x;
74 left_c = &stops[n - 1].color;
79 right_x = stops[0].x + 0x10000;
80 right_c = &stops[0].color;
85 right_c = &stops[n].color;
91 case PIXMAN_REPEAT_PAD:
92 for (n = 0; n < count; n++)
99 left_c = &stops[0].color;
103 left_x = stops[n - 1].x;
104 left_c = &stops[n - 1].color;
110 right_c = &stops[n - 1].color;
114 right_x = stops[n].x;
115 right_c = &stops[n].color;
119 case PIXMAN_REPEAT_REFLECT:
120 x = (int32_t)pos & 0xFFFF;
121 if ((int32_t)pos & 0x10000)
123 for (n = 0; n < count; n++)
129 left_x = -stops[0].x;
130 left_c = &stops[0].color;
134 left_x = stops[n - 1].x;
135 left_c = &stops[n - 1].color;
140 right_x = 0x20000 - stops[n - 1].x;
141 right_c = &stops[n - 1].color;
145 right_x = stops[n].x;
146 right_c = &stops[n].color;
149 if ((int32_t)pos & 0x10000)
151 pixman_color_t *tmp_c;
154 tmp_x = 0x10000 - right_x;
155 right_x = 0x10000 - left_x;
165 right_x += (pos - x);
168 default: /* REPEAT_NONE */
169 for (n = 0; n < count; n++)
170 if (pos < stops[n].x)
176 right_x = stops[0].x;
177 left_c = right_c = (pixman_color_t*) &transparent_black;
181 left_x = stops[n - 1].x;
183 left_c = right_c = (pixman_color_t*) &transparent_black;
187 left_x = stops[n - 1].x;
188 right_x = stops[n].x;
189 left_c = &stops[n - 1].color;
190 right_c = &stops[n].color;
194 walker->left_x = left_x;
195 walker->right_x = right_x;
196 walker->left_ag = ((left_c->alpha >> 8) << 16) | (left_c->green >> 8);
197 walker->left_rb = ((left_c->red & 0xff00) << 8) | (left_c->blue >> 8);
198 walker->right_ag = ((right_c->alpha >> 8) << 16) | (right_c->green >> 8);
199 walker->right_rb = ((right_c->red & 0xff00) << 8) | (right_c->blue >> 8);
201 if (walker->left_x == walker->right_x ||
202 ( walker->left_ag == walker->right_ag &&
203 walker->left_rb == walker->right_rb ) )
209 int32_t width = right_x - left_x;
210 walker->stepper = ((1 << 24) + width / 2) / width;
213 walker->need_reset = FALSE;
216 #define PIXMAN_GRADIENT_WALKER_NEED_RESET(w, x) \
217 ( (w)->need_reset || (x) < (w)->left_x || (x) >= (w)->right_x)
220 /* the following assumes that PIXMAN_GRADIENT_WALKER_NEED_RESET(w,x) is FALSE */
222 _pixman_gradient_walker_pixel (pixman_gradient_walker_t *walker,
223 pixman_fixed_32_32_t x)
226 uint32_t t1, t2, a, color;
228 if (PIXMAN_GRADIENT_WALKER_NEED_RESET (walker, x))
229 _pixman_gradient_walker_reset (walker, x);
231 dist = ((int)(x - walker->left_x) * walker->stepper) >> 16;
234 /* combined INTERPOLATE and premultiply */
235 t1 = walker->left_rb * idist + walker->right_rb * dist;
236 t1 = (t1 >> 8) & 0xff00ff;
238 t2 = walker->left_ag * idist + walker->right_ag * dist;
241 color = t2 & 0xff000000;
244 t1 = t1 * a + 0x800080;
245 t1 = (t1 + ((t1 >> 8) & 0xff00ff)) >> 8;
247 t2 = (t2 >> 8) * a + 0x800080;
248 t2 = (t2 + ((t2 >> 8) & 0xff00ff));
250 return (color | (t1 & 0xff00ff) | (t2 & 0xff00));