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
33
#include "pixman-private.h"
45
pixman_gradient_stop_t *stops;
53
_gradient_walker_init (GradientWalker *walker,
57
walker->num_stops = gradient->n_stops;
58
walker->stops = gradient->stops;
60
walker->right_x = 0x10000;
66
walker->spread = spread;
68
walker->need_reset = TRUE;
72
_gradient_walker_reset (GradientWalker *walker,
73
pixman_fixed_32_32_t pos)
75
int32_t x, left_x, right_x;
76
pixman_color_t *left_c, *right_c;
77
int n, count = walker->num_stops;
78
pixman_gradient_stop_t * stops = walker->stops;
80
static const pixman_color_t transparent_black = { 0, 0, 0, 0 };
82
switch (walker->spread)
84
case PIXMAN_REPEAT_NORMAL:
85
x = (int32_t)pos & 0xFFFF;
86
for (n = 0; n < count; n++)
90
left_x = stops[count-1].x - 0x10000;
91
left_c = &stops[count-1].color;
93
left_x = stops[n-1].x;
94
left_c = &stops[n-1].color;
98
right_x = stops[0].x + 0x10000;
99
right_c = &stops[0].color;
101
right_x = stops[n].x;
102
right_c = &stops[n].color;
105
right_x += (pos - x);
108
case PIXMAN_REPEAT_PAD:
109
for (n = 0; n < count; n++)
110
if (pos < stops[n].x)
115
left_c = &stops[0].color;
117
left_x = stops[n-1].x;
118
left_c = &stops[n-1].color;
123
right_c = &stops[n-1].color;
125
right_x = stops[n].x;
126
right_c = &stops[n].color;
130
case PIXMAN_REPEAT_REFLECT:
131
x = (int32_t)pos & 0xFFFF;
132
if ((int32_t)pos & 0x10000)
134
for (n = 0; n < count; n++)
139
left_x = -stops[0].x;
140
left_c = &stops[0].color;
142
left_x = stops[n-1].x;
143
left_c = &stops[n-1].color;
147
right_x = 0x20000 - stops[n-1].x;
148
right_c = &stops[n-1].color;
150
right_x = stops[n].x;
151
right_c = &stops[n].color;
154
if ((int32_t)pos & 0x10000) {
155
pixman_color_t *tmp_c;
158
tmp_x = 0x10000 - right_x;
159
right_x = 0x10000 - left_x;
169
right_x += (pos - x);
172
default: /* RepeatNone */
173
for (n = 0; n < count; n++)
174
if (pos < stops[n].x)
180
right_x = stops[0].x;
181
left_c = right_c = (pixman_color_t*) &transparent_black;
185
left_x = stops[n-1].x;
187
left_c = right_c = (pixman_color_t*) &transparent_black;
191
left_x = stops[n-1].x;
192
right_x = stops[n].x;
193
left_c = &stops[n-1].color;
194
right_c = &stops[n].color;
198
walker->left_x = left_x;
199
walker->right_x = right_x;
200
walker->left_ag = ((left_c->alpha >> 8) << 16) | (left_c->green >> 8);
201
walker->left_rb = ((left_c->red & 0xff00) << 8) | (left_c->blue >> 8);
202
walker->right_ag = ((right_c->alpha >> 8) << 16) | (right_c->green >> 8);
203
walker->right_rb = ((right_c->red & 0xff00) << 8) | (right_c->blue >> 8);
205
if ( walker->left_x == walker->right_x ||
206
( walker->left_ag == walker->right_ag &&
207
walker->left_rb == walker->right_rb ) )
213
int32_t width = right_x - left_x;
214
walker->stepper = ((1 << 24) + width/2)/width;
217
walker->need_reset = FALSE;
220
#define GRADIENT_WALKER_NEED_RESET(w,x) \
221
( (w)->need_reset || (x) < (w)->left_x || (x) >= (w)->right_x)
224
/* the following assumes that GRADIENT_WALKER_NEED_RESET(w,x) is FALSE */
226
_gradient_walker_pixel (GradientWalker *walker,
227
pixman_fixed_32_32_t x)
230
uint32_t t1, t2, a, color;
232
if (GRADIENT_WALKER_NEED_RESET (walker, x))
233
_gradient_walker_reset (walker, x);
235
dist = ((int)(x - walker->left_x)*walker->stepper) >> 16;
238
/* combined INTERPOLATE and premultiply */
239
t1 = walker->left_rb*idist + walker->right_rb*dist;
240
t1 = (t1 >> 8) & 0xff00ff;
242
t2 = walker->left_ag*idist + walker->right_ag*dist;
245
color = t2 & 0xff000000;
248
t1 = t1*a + 0x800080;
249
t1 = (t1 + ((t1 >> 8) & 0xff00ff)) >> 8;
251
t2 = (t2 >> 8)*a + 0x800080;
252
t2 = (t2 + ((t2 >> 8) & 0xff00ff));
254
return (color | (t1 & 0xff00ff) | (t2 & 0xff00));
257
void pixmanFetchSourcePict(source_image_t * pict, int x, int y, int width,
258
uint32_t *buffer, uint32_t *mask, uint32_t maskBits)
261
SourcePictPtr pGradient = pict->pSourcePict;
263
GradientWalker walker;
264
uint32_t *end = buffer + width;
265
gradient_t *gradient;
267
if (pict->common.type == SOLID)
269
register uint32_t color = ((solid_fill_t *)pict)->color;
277
gradient = (gradient_t *)pict;
279
_gradient_walker_init (&walker, gradient, pict->common.repeat);
281
if (pict->common.type == LINEAR) {
282
pixman_vector_t v, unit;
283
pixman_fixed_32_32_t l;
284
pixman_fixed_48_16_t dx, dy, a, b, off;
285
linear_gradient_t *linear = (linear_gradient_t *)pict;
287
/* reference point is the center of the pixel */
288
v.vector[0] = pixman_int_to_fixed(x) + pixman_fixed_1/2;
289
v.vector[1] = pixman_int_to_fixed(y) + pixman_fixed_1/2;
290
v.vector[2] = pixman_fixed_1;
291
if (pict->common.transform) {
292
if (!pixman_transform_point_3d (pict->common.transform, &v))
294
unit.vector[0] = pict->common.transform->matrix[0][0];
295
unit.vector[1] = pict->common.transform->matrix[1][0];
296
unit.vector[2] = pict->common.transform->matrix[2][0];
298
unit.vector[0] = pixman_fixed_1;
303
dx = linear->p2.x - linear->p1.x;
304
dy = linear->p2.y - linear->p1.y;
309
off = (-a*linear->p1.x - b*linear->p1.y)>>16;
311
if (l == 0 || (unit.vector[2] == 0 && v.vector[2] == pixman_fixed_1)) {
312
pixman_fixed_48_16_t inc, t;
313
/* affine transformation only */
318
t = ((a*v.vector[0] + b*v.vector[1]) >> 16) + off;
319
inc = (a * unit.vector[0] + b * unit.vector[1]) >> 16;
322
if (pict->class == SOURCE_IMAGE_CLASS_VERTICAL)
324
register uint32_t color;
326
color = _gradient_walker_pixel( &walker, t );
335
*(buffer) = _gradient_walker_pixel (&walker, t);
340
while (buffer < end) {
341
if (*mask++ & maskBits)
343
*(buffer) = _gradient_walker_pixel (&walker, t);
351
else /* projective transformation */
353
pixman_fixed_48_16_t t;
355
if (pict->class == SOURCE_IMAGE_CLASS_VERTICAL)
357
register uint32_t color;
359
if (v.vector[2] == 0)
365
pixman_fixed_48_16_t x, y;
367
x = ((pixman_fixed_48_16_t) v.vector[0] << 16) / v.vector[2];
368
y = ((pixman_fixed_48_16_t) v.vector[1] << 16) / v.vector[2];
369
t = ((a * x + b * y) >> 16) + off;
372
color = _gradient_walker_pixel( &walker, t );
380
if (!mask || *mask++ & maskBits)
382
if (v.vector[2] == 0) {
385
pixman_fixed_48_16_t x, y;
386
x = ((pixman_fixed_48_16_t)v.vector[0] << 16) / v.vector[2];
387
y = ((pixman_fixed_48_16_t)v.vector[1] << 16) / v.vector[2];
388
t = ((a*x + b*y) >> 16) + off;
390
*(buffer) = _gradient_walker_pixel (&walker, t);
393
v.vector[0] += unit.vector[0];
394
v.vector[1] += unit.vector[1];
395
v.vector[2] += unit.vector[2];
402
* In the radial gradient problem we are given two circles (c₁,r₁) and
403
* (c₂,r₂) that define the gradient itself. Then, for any point p, we
404
* must compute the value(s) of t within [0.0, 1.0] representing the
405
* circle(s) that would color the point.
407
* There are potentially two values of t since the point p can be
408
* colored by both sides of the circle, (which happens whenever one
409
* circle is not entirely contained within the other).
411
* If we solve for a value of t that is outside of [0.0, 1.0] then we
412
* use the extend mode (NONE, REPEAT, REFLECT, or PAD) to map to a
413
* value within [0.0, 1.0].
415
* Here is an illustration of the problem:
429
* Given (c₁,r₁), (c₂,r₂) and p, we must find an angle θ such that two
430
* points p₁ and p₂ on the two circles are collinear with p. Then, the
431
* desired value of t is the ratio of the length of p₁p to the length
434
* So, we have six unknown values: (p₁x, p₁y), (p₂x, p₂y), θ and t.
435
* We can also write six equations that constrain the problem:
437
* Point p₁ is a distance r₁ from c₁ at an angle of θ:
439
* 1. p₁x = c₁x + r₁·cos θ
440
* 2. p₁y = c₁y + r₁·sin θ
442
* Point p₂ is a distance r₂ from c₂ at an angle of θ:
444
* 3. p₂x = c₂x + r2·cos θ
445
* 4. p₂y = c₂y + r2·sin θ
447
* Point p lies at a fraction t along the line segment p₁p₂:
449
* 5. px = t·p₂x + (1-t)·p₁x
450
* 6. py = t·p₂y + (1-t)·p₁y
452
* To solve, first subtitute 1-4 into 5 and 6:
454
* px = t·(c₂x + r₂·cos θ) + (1-t)·(c₁x + r₁·cos θ)
455
* py = t·(c₂y + r₂·sin θ) + (1-t)·(c₁y + r₁·sin θ)
457
* Then solve each for cos θ and sin θ expressed as a function of t:
459
* cos θ = (-(c₂x - c₁x)·t + (px - c₁x)) / ((r₂-r₁)·t + r₁)
460
* sin θ = (-(c₂y - c₁y)·t + (py - c₁y)) / ((r₂-r₁)·t + r₁)
462
* To simplify this a bit, we define new variables for several of the
463
* common terms as shown below:
483
* Note that cdx, cdy, and dr do not depend on point p at all, so can
484
* be pre-computed for the entire gradient. The simplifed equations
487
* cos θ = (-cdx·t + pdx) / (dr·t + r₁)
488
* sin θ = (-cdy·t + pdy) / (dr·t + r₁)
490
* Finally, to get a single function of t and eliminate the last
491
* unknown θ, we use the identity sin²θ + cos²θ = 1. First, square
492
* each equation, (we knew a quadratic was coming since it must be
493
* possible to obtain two solutions in some cases):
495
* cos²θ = (cdx²t² - 2·cdx·pdx·t + pdx²) / (dr²·t² + 2·r₁·dr·t + r₁²)
496
* sin²θ = (cdy²t² - 2·cdy·pdy·t + pdy²) / (dr²·t² + 2·r₁·dr·t + r₁²)
498
* Then add both together, set the result equal to 1, and express as a
499
* standard quadratic equation in t of the form At² + Bt + C = 0
501
* (cdx² + cdy² - dr²)·t² - 2·(cdx·pdx + cdy·pdy + r₁·dr)·t + (pdx² + pdy² - r₁²) = 0
505
* A = cdx² + cdy² - dr²
506
* B = -2·(pdx·cdx + pdy·cdy + r₁·dr)
507
* C = pdx² + pdy² - r₁²
509
* And again, notice that A does not depend on p, so can be
510
* precomputed. From here we just use the quadratic formula to solve
513
* t = (-2·B ± ⎷(B² - 4·A·C)) / 2·A
515
/* radial or conical */
516
pixman_bool_t affine = TRUE;
524
if (pict->common.transform) {
526
/* reference point is the center of the pixel */
527
v.vector[0] = pixman_int_to_fixed(x) + pixman_fixed_1/2;
528
v.vector[1] = pixman_int_to_fixed(y) + pixman_fixed_1/2;
529
v.vector[2] = pixman_fixed_1;
530
if (!pixman_transform_point_3d (pict->common.transform, &v))
533
cx = pict->common.transform->matrix[0][0]/65536.;
534
cy = pict->common.transform->matrix[1][0]/65536.;
535
cz = pict->common.transform->matrix[2][0]/65536.;
536
rx = v.vector[0]/65536.;
537
ry = v.vector[1]/65536.;
538
rz = v.vector[2]/65536.;
539
affine = pict->common.transform->matrix[2][0] == 0 && v.vector[2] == pixman_fixed_1;
542
if (pict->common.type == RADIAL) {
543
radial_gradient_t *radial = (radial_gradient_t *)pict;
545
while (buffer < end) {
546
if (!mask || *mask++ & maskBits)
551
double c1x = radial->c1.x / 65536.0;
552
double c1y = radial->c1.y / 65536.0;
553
double r1 = radial->c1.radius / 65536.0;
554
pixman_fixed_48_16_t t;
559
B = -2 * ( pdx * radial->cdx
562
C = (pdx * pdx + pdy * pdy - r1 * r1);
564
det = (B * B) - (4 * radial->A * C);
569
t = (pixman_fixed_48_16_t) ((- B - sqrt(det)) / (2.0 * radial->A) * 65536);
571
t = (pixman_fixed_48_16_t) ((- B + sqrt(det)) / (2.0 * radial->A) * 65536);
573
*(buffer) = _gradient_walker_pixel (&walker, t);
582
while (buffer < end) {
583
if (!mask || *mask++ & maskBits)
588
double c1x = radial->c1.x / 65536.0;
589
double c1y = radial->c1.y / 65536.0;
590
double r1 = radial->c1.radius / 65536.0;
591
pixman_fixed_48_16_t t;
604
B = -2 * ( pdx * radial->cdx
607
C = (pdx * pdx + pdy * pdy - r1 * r1);
609
det = (B * B) - (4 * radial->A * C);
614
t = (pixman_fixed_48_16_t) ((- B - sqrt(det)) / (2.0 * radial->A) * 65536);
616
t = (pixman_fixed_48_16_t) ((- B + sqrt(det)) / (2.0 * radial->A) * 65536);
618
*(buffer) = _gradient_walker_pixel (&walker, t);
627
} else /* SourcePictTypeConical */ {
628
conical_gradient_t *conical = (conical_gradient_t *)pict;
629
double a = conical->angle/(180.*65536);
631
rx -= conical->center.x/65536.;
632
ry -= conical->center.y/65536.;
634
while (buffer < end) {
637
if (!mask || *mask++ & maskBits)
639
pixman_fixed_48_16_t t;
641
angle = atan2(ry, rx) + a;
642
t = (pixman_fixed_48_16_t) (angle * (65536. / (2*M_PI)));
644
*(buffer) = _gradient_walker_pixel (&walker, t);
652
while (buffer < end) {
656
if (!mask || *mask++ & maskBits)
658
pixman_fixed_48_16_t t;
666
x -= conical->center.x/65536.;
667
y -= conical->center.y/65536.;
668
angle = atan2(y, x) + a;
669
t = (pixman_fixed_48_16_t) (angle * (65536. / (2*M_PI)));
671
*(buffer) = _gradient_walker_pixel (&walker, t);
685
* For now, just evaluate the source picture at 32bpp and expand. We could
686
* produce smoother gradients by evaluating them at higher color depth, but
687
* that's a project for the future.
689
void pixmanFetchSourcePict64(source_image_t * pict, int x, int y, int width,
690
uint64_t *buffer, uint64_t *mask, uint32_t maskBits)
692
uint32_t *mask8 = NULL;
694
// Contract the mask image, if one exists, so that the 32-bit fetch function
697
mask8 = pixman_malloc_ab(width, sizeof(uint32_t));
698
pixman_contract(mask8, mask, width);
701
// Fetch the source image into the first half of buffer.
702
pixmanFetchSourcePict(pict, x, y, width, (uint32_t*)buffer, mask8,
705
// Expand from 32bpp to 64bpp in place.
706
pixman_expand(buffer, (uint32_t*)buffer, PIXMAN_a8r8g8b8, width);