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,
34 pixman_repeat_t repeat)
36 walker->num_stops = gradient->n_stops;
37 walker->stops = gradient->stops;
39 walker->right_x = 0x10000;
45 walker->repeat = repeat;
47 walker->need_reset = TRUE;
51 gradient_walker_reset (pixman_gradient_walker_t *walker,
52 pixman_fixed_48_16_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 if (walker->repeat == PIXMAN_REPEAT_NORMAL)
61 x = (int32_t)pos & 0xffff;
63 else if (walker->repeat == PIXMAN_REPEAT_REFLECT)
65 x = (int32_t)pos & 0xffff;
66 if ((int32_t)pos & 0x10000)
74 for (n = 0; n < count; n++)
80 left_x = stops[n - 1].x;
81 left_c = &stops[n - 1].color;
84 right_c = &stops[n].color;
86 if (walker->repeat == PIXMAN_REPEAT_NORMAL)
91 else if (walker->repeat == PIXMAN_REPEAT_REFLECT)
93 if ((int32_t)pos & 0x10000)
95 pixman_color_t *tmp_c;
98 tmp_x = 0x10000 - right_x;
99 right_x = 0x10000 - left_x;
109 right_x += (pos - x);
111 else if (walker->repeat == PIXMAN_REPEAT_NONE)
119 walker->left_x = left_x;
120 walker->right_x = right_x;
121 walker->left_ag = ((left_c->alpha >> 8) << 16) | (left_c->green >> 8);
122 walker->left_rb = ((left_c->red & 0xff00) << 8) | (left_c->blue >> 8);
123 walker->right_ag = ((right_c->alpha >> 8) << 16) | (right_c->green >> 8);
124 walker->right_rb = ((right_c->red & 0xff00) << 8) | (right_c->blue >> 8);
126 if (walker->left_x == walker->right_x ||
127 (walker->left_ag == walker->right_ag &&
128 walker->left_rb == walker->right_rb))
134 int32_t width = right_x - left_x;
135 walker->stepper = ((1 << 24) + width / 2) / width;
138 walker->need_reset = FALSE;
142 _pixman_gradient_walker_pixel (pixman_gradient_walker_t *walker,
143 pixman_fixed_48_16_t x)
146 uint32_t t1, t2, a, color;
148 if (walker->need_reset || x < walker->left_x || x >= walker->right_x)
149 gradient_walker_reset (walker, x);
151 dist = ((int)(x - walker->left_x) * walker->stepper) >> 16;
154 /* combined INTERPOLATE and premultiply */
155 t1 = walker->left_rb * idist + walker->right_rb * dist;
156 t1 = (t1 >> 8) & 0xff00ff;
158 t2 = walker->left_ag * idist + walker->right_ag * dist;
161 color = t2 & 0xff000000;
164 t1 = t1 * a + 0x800080;
165 t1 = (t1 + ((t1 >> 8) & 0xff00ff)) >> 8;
167 t2 = (t2 >> 8) * a + 0x800080;
168 t2 = (t2 + ((t2 >> 8) & 0xff00ff));
170 return (color | (t1 & 0xff00ff) | (t2 & 0xff00));