void _pixman_general_conical_gradient_get_scanline_32 (pixman_image_t *image, int x, int y, int width, uint32_t * buffer, const uint32_t *mask, uint32_t mask_bits) { source_image_t *source = (source_image_t *)image; gradient_t *gradient = (gradient_t *)source; conical_gradient_t *conical = (conical_gradient_t *)image; uint32_t *end = buffer + width; pixman_gradient_walker_t walker; pixman_bool_t affine = TRUE; double cx = 1.; double cy = 0.; double cz = 0.; double rx = x + 0.5; double ry = y + 0.5; double rz = 1.; double a = (conical->angle * M_PI) / (180. * 65536); _pixman_gradient_walker_init (&walker, gradient, source->common.repeat); if (source->common.transform) { pixman_vector_t v; /* reference point is the center of the pixel */ v.vector[0] = pixman_int_to_fixed (x) + pixman_fixed_1 / 2; v.vector[1] = pixman_int_to_fixed (y) + pixman_fixed_1 / 2; v.vector[2] = pixman_fixed_1; if (!pixman_transform_point_3d (source->common.transform, &v)) return; cx = source->common.transform->matrix[0][0] / 65536.; cy = source->common.transform->matrix[1][0] / 65536.; cz = source->common.transform->matrix[2][0] / 65536.; rx = v.vector[0] / 65536.; ry = v.vector[1] / 65536.; rz = v.vector[2] / 65536.; affine = source->common.transform->matrix[2][0] == 0 && v.vector[2] == pixman_fixed_1; } if (affine) { rx -= conical->center.x / 65536.; ry -= conical->center.y / 65536.; while (buffer < end) { double angle; if (!mask || *mask++ & mask_bits) { pixman_fixed_48_16_t t; angle = atan2 (ry, rx) + a; t = (pixman_fixed_48_16_t) (angle * (65536. / (2 * M_PI))); *buffer = _pixman_gradient_walker_pixel (&walker, t); } ++buffer; rx += cx; ry += cy; } } else { while (buffer < end) { double x, y; double angle; if (!mask || *mask++ & mask_bits) { pixman_fixed_48_16_t t; if (rz != 0) { x = rx / rz; y = ry / rz; } else { x = y = 0.; } x -= conical->center.x / 65536.; y -= conical->center.y / 65536.; angle = atan2 (y, x) + a; t = (pixman_fixed_48_16_t) (angle * (65536. / (2 * M_PI))); *buffer = _pixman_gradient_walker_pixel (&walker, t); } ++buffer; rx += cx; ry += cy; rz += cz; } } }
static uint32_t radial_compute_color (double a, double b, double c, double inva, double dr, double mindr, pixman_gradient_walker_t *walker, pixman_repeat_t repeat) { /* * In this function error propagation can lead to bad results: * - discr can have an unbound error (if b*b-a*c is very small), * potentially making it the opposite sign of what it should have been * (thus clearing a pixel that would have been colored or vice-versa) * or propagating the error to sqrtdiscr; * if discr has the wrong sign or b is very small, this can lead to bad * results * * - the algorithm used to compute the solutions of the quadratic * equation is not numerically stable (but saves one division compared * to the numerically stable one); * this can be a problem if a*c is much smaller than b*b * * - the above problems are worse if a is small (as inva becomes bigger) */ double discr; if (a == 0) { double t; if (b == 0) return 0; t = pixman_fixed_1 / 2 * c / b; if (repeat == PIXMAN_REPEAT_NONE) { if (0 <= t && t <= pixman_fixed_1) return _pixman_gradient_walker_pixel (walker, t); } else { if (t * dr >= mindr) return _pixman_gradient_walker_pixel (walker, t); } return 0; } discr = fdot (b, a, 0, b, -c, 0); if (discr >= 0) { double sqrtdiscr, t0, t1; sqrtdiscr = sqrt (discr); t0 = (b + sqrtdiscr) * inva; t1 = (b - sqrtdiscr) * inva; /* * The root that must be used is the biggest one that belongs * to the valid range ([0,1] for PIXMAN_REPEAT_NONE, any * solution that results in a positive radius otherwise). * * If a > 0, t0 is the biggest solution, so if it is valid, it * is the correct result. * * If a < 0, only one of the solutions can be valid, so the * order in which they are tested is not important. */ if (repeat == PIXMAN_REPEAT_NONE) { if (0 <= t0 && t0 <= pixman_fixed_1) return _pixman_gradient_walker_pixel (walker, t0); else if (0 <= t1 && t1 <= pixman_fixed_1) return _pixman_gradient_walker_pixel (walker, t1); } else { if (t0 * dr >= mindr) return _pixman_gradient_walker_pixel (walker, t0); else if (t1 * dr >= mindr) return _pixman_gradient_walker_pixel (walker, t1); } } return 0; }
static uint32_t radial_compute_color (double a, double b, double c, double inva, double dr, double mindr, pixman_gradient_walker_t *walker, pixman_repeat_t repeat) { /* * In this function error propagation can lead to bad results: * - det can have an unbound error (if b*b-a*c is very small), * potentially making it the opposite sign of what it should have been * (thus clearing a pixel that would have been colored or vice-versa) * or propagating the error to sqrtdet; * if det has the wrong sign or b is very small, this can lead to bad * results * * - the algorithm used to compute the solutions of the quadratic * equation is not numerically stable (but saves one division compared * to the numerically stable one); * this can be a problem if a*c is much smaller than b*b * * - the above problems are worse if a is small (as inva becomes bigger) */ double det; if (a == 0) { double t; if (b == 0) return 0; t = pixman_fixed_1 / 2 * c / b; if (repeat == PIXMAN_REPEAT_NONE) { if (0 <= t && t <= pixman_fixed_1) return _pixman_gradient_walker_pixel (walker, t); } else { if (t * dr > mindr) return _pixman_gradient_walker_pixel (walker, t); } return 0; } det = fdot (b, a, 0, b, -c, 0); if (det >= 0) { double sqrtdet, t0, t1; sqrtdet = sqrt (det); t0 = (b + sqrtdet) * inva; t1 = (b - sqrtdet) * inva; if (repeat == PIXMAN_REPEAT_NONE) { if (0 <= t0 && t0 <= pixman_fixed_1) return _pixman_gradient_walker_pixel (walker, t0); else if (0 <= t1 && t1 <= pixman_fixed_1) return _pixman_gradient_walker_pixel (walker, t1); } else { if (t0 * dr > mindr) return _pixman_gradient_walker_pixel (walker, t0); else if (t1 * dr > mindr) return _pixman_gradient_walker_pixel (walker, t1); } } return 0; }
static uint32_t * conical_get_scanline_narrow (pixman_iter_t *iter, const uint32_t *mask) { pixman_image_t *image = iter->image; int x = iter->x; int y = iter->y; int width = iter->width; uint32_t *buffer = iter->buffer; gradient_t *gradient = (gradient_t *)image; conical_gradient_t *conical = (conical_gradient_t *)image; uint32_t *end = buffer + width; pixman_gradient_walker_t walker; pixman_bool_t affine = TRUE; double cx = 1.; double cy = 0.; double cz = 0.; double rx = x + 0.5; double ry = y + 0.5; double rz = 1.; _pixman_gradient_walker_init (&walker, gradient, image->common.repeat); if (image->common.transform) { pixman_vector_t v; /* reference point is the center of the pixel */ v.vector[0] = pixman_int_to_fixed (x) + pixman_fixed_1 / 2; v.vector[1] = pixman_int_to_fixed (y) + pixman_fixed_1 / 2; v.vector[2] = pixman_fixed_1; if (!pixman_transform_point_3d (image->common.transform, &v)) return iter->buffer; cx = image->common.transform->matrix[0][0] / 65536.; cy = image->common.transform->matrix[1][0] / 65536.; cz = image->common.transform->matrix[2][0] / 65536.; rx = v.vector[0] / 65536.; ry = v.vector[1] / 65536.; rz = v.vector[2] / 65536.; affine = image->common.transform->matrix[2][0] == 0 && v.vector[2] == pixman_fixed_1; } if (affine) { rx -= conical->center.x / 65536.; ry -= conical->center.y / 65536.; while (buffer < end) { if (!mask || *mask++) { double t = coordinates_to_parameter (rx, ry, conical->angle); *buffer = _pixman_gradient_walker_pixel ( &walker, (pixman_fixed_48_16_t)pixman_double_to_fixed (t)); } ++buffer; rx += cx; ry += cy; } } else { while (buffer < end) { double x, y; if (!mask || *mask++) { double t; if (rz != 0) { x = rx / rz; y = ry / rz; } else { x = y = 0.; } x -= conical->center.x / 65536.; y -= conical->center.y / 65536.; t = coordinates_to_parameter (x, y, conical->angle); *buffer = _pixman_gradient_walker_pixel ( &walker, (pixman_fixed_48_16_t)pixman_double_to_fixed (t)); } ++buffer; rx += cx; ry += cy; rz += cz; } } iter->y++; return iter->buffer; }
void _pixman_general_radial_gradient_get_scanline_32 (pixman_image_t *image, int x, int y, int width, uint32_t * buffer, const uint32_t *mask, uint32_t mask_bits) { /* * In the radial gradient problem we are given two circles (c₁,r₁) and * (c₂,r₂) that define the gradient itself. Then, for any point p, we * must compute the value(s) of t within [0.0, 1.0] representing the * circle(s) that would color the point. * * There are potentially two values of t since the point p can be * colored by both sides of the circle, (which happens whenever one * circle is not entirely contained within the other). * * If we solve for a value of t that is outside of [0.0, 1.0] then we * use the extend mode (NONE, REPEAT, REFLECT, or PAD) to map to a * value within [0.0, 1.0]. * * Here is an illustration of the problem: * * p₂ * p • * • ╲ * · ╲r₂ * p₁ · ╲ * • θ╲ * ╲ ╌╌• * ╲r₁ · c₂ * θ╲ · * ╌╌• * c₁ * * Given (c₁,r₁), (c₂,r₂) and p, we must find an angle θ such that two * points p₁ and p₂ on the two circles are collinear with p. Then, the * desired value of t is the ratio of the length of p₁p to the length * of p₁p₂. * * So, we have six unknown values: (p₁x, p₁y), (p₂x, p₂y), θ and t. * We can also write six equations that constrain the problem: * * Point p₁ is a distance r₁ from c₁ at an angle of θ: * * 1. p₁x = c₁x + r₁·cos θ * 2. p₁y = c₁y + r₁·sin θ * * Point p₂ is a distance r₂ from c₂ at an angle of θ: * * 3. p₂x = c₂x + r2·cos θ * 4. p₂y = c₂y + r2·sin θ * * Point p lies at a fraction t along the line segment p₁p₂: * * 5. px = t·p₂x + (1-t)·p₁x * 6. py = t·p₂y + (1-t)·p₁y * * To solve, first subtitute 1-4 into 5 and 6: * * px = t·(c₂x + r₂·cos θ) + (1-t)·(c₁x + r₁·cos θ) * py = t·(c₂y + r₂·sin θ) + (1-t)·(c₁y + r₁·sin θ) * * Then solve each for cos θ and sin θ expressed as a function of t: * * cos θ = (-(c₂x - c₁x)·t + (px - c₁x)) / ((r₂-r₁)·t + r₁) * sin θ = (-(c₂y - c₁y)·t + (py - c₁y)) / ((r₂-r₁)·t + r₁) * * To simplify this a bit, we define new variables for several of the * common terms as shown below: * * p₂ * p • * • ╲ * · ┆ ╲r₂ * p₁ · ┆ ╲ * • pdy┆ ╲ * ╲ ┆ •c₂ * ╲r₁ ┆ · ┆ * ╲ ·┆ ┆cdy * •╌╌╌╌┴╌╌╌╌╌╌╌┘ * c₁ pdx cdx * * cdx = (c₂x - c₁x) * cdy = (c₂y - c₁y) * dr = r₂-r₁ * pdx = px - c₁x * pdy = py - c₁y * * Note that cdx, cdy, and dr do not depend on point p at all, so can * be pre-computed for the entire gradient. The simplifed equations * are now: * * cos θ = (-cdx·t + pdx) / (dr·t + r₁) * sin θ = (-cdy·t + pdy) / (dr·t + r₁) * * Finally, to get a single function of t and eliminate the last * unknown θ, we use the identity sin²θ + cos²θ = 1. First, square * each equation, (we knew a quadratic was coming since it must be * possible to obtain two solutions in some cases): * * cos²θ = (cdx²t² - 2·cdx·pdx·t + pdx²) / (dr²·t² + 2·r₁·dr·t + r₁²) * sin²θ = (cdy²t² - 2·cdy·pdy·t + pdy²) / (dr²·t² + 2·r₁·dr·t + r₁²) * * Then add both together, set the result equal to 1, and express as a * standard quadratic equation in t of the form At² + Bt + C = 0 * * (cdx² + cdy² - dr²)·t² - 2·(cdx·pdx + cdy·pdy + r₁·dr)·t + (pdx² + pdy² - r₁²) = 0 * * In other words: * * A = cdx² + cdy² - dr² * B = -2·(pdx·cdx + pdy·cdy + r₁·dr) * C = pdx² + pdy² - r₁² * * And again, notice that A does not depend on p, so can be * precomputed. From here we just use the quadratic formula to solve * for t: * * t = (-2·B ± ⎷(B² - 4·A·C)) / 2·A */ gradient_t *gradient = (gradient_t *)image; source_image_t *source = (source_image_t *)image; radial_gradient_t *radial = (radial_gradient_t *)image; uint32_t *end = buffer + width; pixman_gradient_walker_t walker; pixman_bool_t affine = TRUE; double cx = 1.; double cy = 0.; double cz = 0.; double rx = x + 0.5; double ry = y + 0.5; double rz = 1.; _pixman_gradient_walker_init (&walker, gradient, source->common.repeat); if (source->common.transform) { pixman_vector_t v; /* reference point is the center of the pixel */ v.vector[0] = pixman_int_to_fixed (x) + pixman_fixed_1 / 2; v.vector[1] = pixman_int_to_fixed (y) + pixman_fixed_1 / 2; v.vector[2] = pixman_fixed_1; if (!pixman_transform_point_3d (source->common.transform, &v)) return; cx = source->common.transform->matrix[0][0] / 65536.; cy = source->common.transform->matrix[1][0] / 65536.; cz = source->common.transform->matrix[2][0] / 65536.; rx = v.vector[0] / 65536.; ry = v.vector[1] / 65536.; rz = v.vector[2] / 65536.; affine = source->common.transform->matrix[2][0] == 0 && v.vector[2] == pixman_fixed_1; } if (affine) { /* When computing t over a scanline, we notice that some expressions * are constant so we can compute them just once. Given: * * t = (-2·B ± ⎷(B² - 4·A·C)) / 2·A * * where * * A = cdx² + cdy² - dr² [precomputed as radial->A] * B = -2·(pdx·cdx + pdy·cdy + r₁·dr) * C = pdx² + pdy² - r₁² * * Since we have an affine transformation, we know that (pdx, pdy) * increase linearly with each pixel, * * pdx = pdx₀ + n·cx, * pdy = pdy₀ + n·cy, * * we can then express B in terms of an linear increment along * the scanline: * * B = B₀ + n·cB, with * B₀ = -2·(pdx₀·cdx + pdy₀·cdy + r₁·dr) and * cB = -2·(cx·cdx + cy·cdy) * * Thus we can replace the full evaluation of B per-pixel (4 multiplies, * 2 additions) with a single addition. */ float r1 = radial->c1.radius / 65536.; float r1sq = r1 * r1; float pdx = rx - radial->c1.x / 65536.; float pdy = ry - radial->c1.y / 65536.; float A = radial->A; float invA = -65536. / (2. * A); float A4 = -4. * A; float B = -2. * (pdx*radial->cdx + pdy*radial->cdy + r1*radial->dr); float cB = -2. * (cx*radial->cdx + cy*radial->cdy); pixman_bool_t invert = A * radial->dr < 0; while (buffer < end) { if (!mask || *mask++ & mask_bits) { pixman_fixed_t t; double det = B * B + A4 * (pdx * pdx + pdy * pdy - r1sq); if (det <= 0.) t = (pixman_fixed_t) (B * invA); else if (invert) t = (pixman_fixed_t) ((B + sqrt (det)) * invA); else t = (pixman_fixed_t) ((B - sqrt (det)) * invA); *buffer = _pixman_gradient_walker_pixel (&walker, t); } ++buffer; pdx += cx; pdy += cy; B += cB; } } else { /* projective */ while (buffer < end) { if (!mask || *mask++ & mask_bits) { double pdx, pdy; double B, C; double det; double c1x = radial->c1.x / 65536.0; double c1y = radial->c1.y / 65536.0; double r1 = radial->c1.radius / 65536.0; pixman_fixed_48_16_t t; double x, y; if (rz != 0) { x = rx / rz; y = ry / rz; } else { x = y = 0.; } pdx = x - c1x; pdy = y - c1y; B = -2 * (pdx * radial->cdx + pdy * radial->cdy + r1 * radial->dr); C = (pdx * pdx + pdy * pdy - r1 * r1); det = (B * B) - (4 * radial->A * C); if (det < 0.0) det = 0.0; if (radial->A * radial->dr < 0) t = (pixman_fixed_48_16_t) ((-B - sqrt (det)) / (2.0 * radial->A) * 65536); else t = (pixman_fixed_48_16_t) ((-B + sqrt (det)) / (2.0 * radial->A) * 65536); *buffer = _pixman_gradient_walker_pixel (&walker, t); } ++buffer; rx += cx; ry += cy; rz += cz; } } }
static uint32_t * linear_get_scanline_narrow (pixman_iter_t *iter, const uint32_t *mask) { pixman_image_t *image = iter->image; int x = iter->x; int y = iter->y; int width = iter->width; uint32_t * buffer = iter->buffer; pixman_vector_t v, unit; pixman_fixed_32_32_t l; pixman_fixed_48_16_t dx, dy; gradient_t *gradient = (gradient_t *)image; linear_gradient_t *linear = (linear_gradient_t *)image; uint32_t *end = buffer + width; pixman_gradient_walker_t walker; _pixman_gradient_walker_init (&walker, gradient, image->common.repeat); /* reference point is the center of the pixel */ v.vector[0] = pixman_int_to_fixed (x) + pixman_fixed_1 / 2; v.vector[1] = pixman_int_to_fixed (y) + pixman_fixed_1 / 2; v.vector[2] = pixman_fixed_1; if (image->common.transform) { if (!pixman_transform_point_3d (image->common.transform, &v)) return iter->buffer; unit.vector[0] = image->common.transform->matrix[0][0]; unit.vector[1] = image->common.transform->matrix[1][0]; unit.vector[2] = image->common.transform->matrix[2][0]; } else { unit.vector[0] = pixman_fixed_1; unit.vector[1] = 0; unit.vector[2] = 0; } dx = linear->p2.x - linear->p1.x; dy = linear->p2.y - linear->p1.y; l = dx * dx + dy * dy; if (l == 0 || unit.vector[2] == 0) { /* affine transformation only */ pixman_fixed_32_32_t t, next_inc; double inc; if (l == 0 || v.vector[2] == 0) { t = 0; inc = 0; } else { double invden, v2; invden = pixman_fixed_1 * (double) pixman_fixed_1 / (l * (double) v.vector[2]); v2 = v.vector[2] * (1. / pixman_fixed_1); t = ((dx * v.vector[0] + dy * v.vector[1]) - (dx * linear->p1.x + dy * linear->p1.y) * v2) * invden; inc = (dx * unit.vector[0] + dy * unit.vector[1]) * invden; } next_inc = 0; if (((pixman_fixed_32_32_t )(inc * width)) == 0) { register uint32_t color; color = _pixman_gradient_walker_pixel (&walker, t); while (buffer < end) *buffer++ = color; } else { int i; i = 0; while (buffer < end) { if (!mask || *mask++) { *buffer = _pixman_gradient_walker_pixel (&walker, t + next_inc); } i++; next_inc = inc * i; buffer++; } } } else { /* projective transformation */ double t; t = 0; while (buffer < end) { if (!mask || *mask++) { if (v.vector[2] != 0) { double invden, v2; invden = pixman_fixed_1 * (double) pixman_fixed_1 / (l * (double) v.vector[2]); v2 = v.vector[2] * (1. / pixman_fixed_1); t = ((dx * v.vector[0] + dy * v.vector[1]) - (dx * linear->p1.x + dy * linear->p1.y) * v2) * invden; } *buffer = _pixman_gradient_walker_pixel (&walker, t); } ++buffer; v.vector[0] += unit.vector[0]; v.vector[1] += unit.vector[1]; v.vector[2] += unit.vector[2]; } } iter->y++; return iter->buffer; }