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,
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++)
67 left_x = stops[count-1].x - 0x10000;
68 left_c = &stops[count-1].color;
70 left_x = stops[n-1].x;
71 left_c = &stops[n-1].color;
75 right_x = stops[0].x + 0x10000;
76 right_c = &stops[0].color;
79 right_c = &stops[n].color;
85 case PIXMAN_REPEAT_PAD:
86 for (n = 0; n < count; n++)
92 left_c = &stops[0].color;
94 left_x = stops[n-1].x;
95 left_c = &stops[n-1].color;
100 right_c = &stops[n-1].color;
102 right_x = stops[n].x;
103 right_c = &stops[n].color;
107 case PIXMAN_REPEAT_REFLECT:
108 x = (int32_t)pos & 0xFFFF;
109 if ((int32_t)pos & 0x10000)
111 for (n = 0; n < count; n++)
116 left_x = -stops[0].x;
117 left_c = &stops[0].color;
119 left_x = stops[n-1].x;
120 left_c = &stops[n-1].color;
124 right_x = 0x20000 - stops[n-1].x;
125 right_c = &stops[n-1].color;
127 right_x = stops[n].x;
128 right_c = &stops[n].color;
131 if ((int32_t)pos & 0x10000) {
132 pixman_color_t *tmp_c;
135 tmp_x = 0x10000 - right_x;
136 right_x = 0x10000 - left_x;
146 right_x += (pos - x);
149 default: /* REPEAT_NONE */
150 for (n = 0; n < count; n++)
151 if (pos < stops[n].x)
157 right_x = stops[0].x;
158 left_c = right_c = (pixman_color_t*) &transparent_black;
162 left_x = stops[n-1].x;
164 left_c = right_c = (pixman_color_t*) &transparent_black;
168 left_x = stops[n-1].x;
169 right_x = stops[n].x;
170 left_c = &stops[n-1].color;
171 right_c = &stops[n].color;
175 walker->left_x = left_x;
176 walker->right_x = right_x;
177 walker->left_ag = ((left_c->alpha >> 8) << 16) | (left_c->green >> 8);
178 walker->left_rb = ((left_c->red & 0xff00) << 8) | (left_c->blue >> 8);
179 walker->right_ag = ((right_c->alpha >> 8) << 16) | (right_c->green >> 8);
180 walker->right_rb = ((right_c->red & 0xff00) << 8) | (right_c->blue >> 8);
182 if ( walker->left_x == walker->right_x ||
183 ( walker->left_ag == walker->right_ag &&
184 walker->left_rb == walker->right_rb ) )
190 int32_t width = right_x - left_x;
191 walker->stepper = ((1 << 24) + width/2)/width;
194 walker->need_reset = FALSE;
197 #define PIXMAN_GRADIENT_WALKER_NEED_RESET(w,x) \
198 ( (w)->need_reset || (x) < (w)->left_x || (x) >= (w)->right_x)
201 /* the following assumes that PIXMAN_GRADIENT_WALKER_NEED_RESET(w,x) is FALSE */
203 _pixman_gradient_walker_pixel (pixman_gradient_walker_t *walker,
204 pixman_fixed_32_32_t x)
207 uint32_t t1, t2, a, color;
209 if (PIXMAN_GRADIENT_WALKER_NEED_RESET (walker, x))
210 _pixman_gradient_walker_reset (walker, x);
212 dist = ((int)(x - walker->left_x)*walker->stepper) >> 16;
215 /* combined INTERPOLATE and premultiply */
216 t1 = walker->left_rb*idist + walker->right_rb*dist;
217 t1 = (t1 >> 8) & 0xff00ff;
219 t2 = walker->left_ag*idist + walker->right_ag*dist;
222 color = t2 & 0xff000000;
225 t1 = t1*a + 0x800080;
226 t1 = (t1 + ((t1 >> 8) & 0xff00ff)) >> 8;
228 t2 = (t2 >> 8)*a + 0x800080;
229 t2 = (t2 + ((t2 >> 8) & 0xff00ff));
231 return (color | (t1 & 0xff00ff) | (t2 & 0xff00));