michael@0: /* michael@0: * Copyright © 2000 SuSE, Inc. michael@0: * Copyright © 2007 Red Hat, Inc. michael@0: * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc. michael@0: * 2005 Lars Knoll & Zack Rusin, Trolltech michael@0: * michael@0: * Permission to use, copy, modify, distribute, and sell this software and its michael@0: * documentation for any purpose is hereby granted without fee, provided that michael@0: * the above copyright notice appear in all copies and that both that michael@0: * copyright notice and this permission notice appear in supporting michael@0: * documentation, and that the name of Keith Packard not be used in michael@0: * advertising or publicity pertaining to distribution of the software without michael@0: * specific, written prior permission. Keith Packard makes no michael@0: * representations about the suitability of this software for any purpose. It michael@0: * is provided "as is" without express or implied warranty. michael@0: * michael@0: * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS michael@0: * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY michael@0: * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES michael@0: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN michael@0: * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING michael@0: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS michael@0: * SOFTWARE. michael@0: */ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include "pixman-private.h" michael@0: michael@0: static force_inline double michael@0: coordinates_to_parameter (double x, double y, double angle) michael@0: { michael@0: double t; michael@0: michael@0: t = atan2 (y, x) + angle; michael@0: michael@0: while (t < 0) michael@0: t += 2 * M_PI; michael@0: michael@0: while (t >= 2 * M_PI) michael@0: t -= 2 * M_PI; michael@0: michael@0: return 1 - t * (1 / (2 * M_PI)); /* Scale t to [0, 1] and michael@0: * make rotation CCW michael@0: */ michael@0: } michael@0: michael@0: static uint32_t * michael@0: conical_get_scanline_narrow (pixman_iter_t *iter, const uint32_t *mask) michael@0: { michael@0: pixman_image_t *image = iter->image; michael@0: int x = iter->x; michael@0: int y = iter->y; michael@0: int width = iter->width; michael@0: uint32_t *buffer = iter->buffer; michael@0: michael@0: gradient_t *gradient = (gradient_t *)image; michael@0: conical_gradient_t *conical = (conical_gradient_t *)image; michael@0: uint32_t *end = buffer + width; michael@0: pixman_gradient_walker_t walker; michael@0: pixman_bool_t affine = TRUE; michael@0: double cx = 1.; michael@0: double cy = 0.; michael@0: double cz = 0.; michael@0: double rx = x + 0.5; michael@0: double ry = y + 0.5; michael@0: double rz = 1.; michael@0: michael@0: _pixman_gradient_walker_init (&walker, gradient, image->common.repeat); michael@0: michael@0: if (image->common.transform) michael@0: { michael@0: pixman_vector_t v; michael@0: michael@0: /* reference point is the center of the pixel */ michael@0: v.vector[0] = pixman_int_to_fixed (x) + pixman_fixed_1 / 2; michael@0: v.vector[1] = pixman_int_to_fixed (y) + pixman_fixed_1 / 2; michael@0: v.vector[2] = pixman_fixed_1; michael@0: michael@0: if (!pixman_transform_point_3d (image->common.transform, &v)) michael@0: return iter->buffer; michael@0: michael@0: cx = image->common.transform->matrix[0][0] / 65536.; michael@0: cy = image->common.transform->matrix[1][0] / 65536.; michael@0: cz = image->common.transform->matrix[2][0] / 65536.; michael@0: michael@0: rx = v.vector[0] / 65536.; michael@0: ry = v.vector[1] / 65536.; michael@0: rz = v.vector[2] / 65536.; michael@0: michael@0: affine = michael@0: image->common.transform->matrix[2][0] == 0 && michael@0: v.vector[2] == pixman_fixed_1; michael@0: } michael@0: michael@0: if (affine) michael@0: { michael@0: rx -= conical->center.x / 65536.; michael@0: ry -= conical->center.y / 65536.; michael@0: michael@0: while (buffer < end) michael@0: { michael@0: if (!mask || *mask++) michael@0: { michael@0: double t = coordinates_to_parameter (rx, ry, conical->angle); michael@0: michael@0: *buffer = _pixman_gradient_walker_pixel ( michael@0: &walker, (pixman_fixed_48_16_t)pixman_double_to_fixed (t)); michael@0: } michael@0: michael@0: ++buffer; michael@0: michael@0: rx += cx; michael@0: ry += cy; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: while (buffer < end) michael@0: { michael@0: double x, y; michael@0: michael@0: if (!mask || *mask++) michael@0: { michael@0: double t; michael@0: michael@0: if (rz != 0) michael@0: { michael@0: x = rx / rz; michael@0: y = ry / rz; michael@0: } michael@0: else michael@0: { michael@0: x = y = 0.; michael@0: } michael@0: michael@0: x -= conical->center.x / 65536.; michael@0: y -= conical->center.y / 65536.; michael@0: michael@0: t = coordinates_to_parameter (x, y, conical->angle); michael@0: michael@0: *buffer = _pixman_gradient_walker_pixel ( michael@0: &walker, (pixman_fixed_48_16_t)pixman_double_to_fixed (t)); michael@0: } michael@0: michael@0: ++buffer; michael@0: michael@0: rx += cx; michael@0: ry += cy; michael@0: rz += cz; michael@0: } michael@0: } michael@0: michael@0: iter->y++; michael@0: return iter->buffer; michael@0: } michael@0: michael@0: static uint32_t * michael@0: conical_get_scanline_wide (pixman_iter_t *iter, const uint32_t *mask) michael@0: { michael@0: uint32_t *buffer = conical_get_scanline_narrow (iter, NULL); michael@0: michael@0: pixman_expand_to_float ( michael@0: (argb_t *)buffer, buffer, PIXMAN_a8r8g8b8, iter->width); michael@0: michael@0: return buffer; michael@0: } michael@0: michael@0: void michael@0: _pixman_conical_gradient_iter_init (pixman_image_t *image, pixman_iter_t *iter) michael@0: { michael@0: if (iter->iter_flags & ITER_NARROW) michael@0: iter->get_scanline = conical_get_scanline_narrow; michael@0: else michael@0: iter->get_scanline = conical_get_scanline_wide; michael@0: } michael@0: michael@0: PIXMAN_EXPORT pixman_image_t * michael@0: pixman_image_create_conical_gradient (const pixman_point_fixed_t * center, michael@0: pixman_fixed_t angle, michael@0: const pixman_gradient_stop_t *stops, michael@0: int n_stops) michael@0: { michael@0: pixman_image_t *image = _pixman_image_allocate (); michael@0: conical_gradient_t *conical; michael@0: michael@0: if (!image) michael@0: return NULL; michael@0: michael@0: conical = &image->conical; michael@0: michael@0: if (!_pixman_init_gradient (&conical->common, stops, n_stops)) michael@0: { michael@0: free (image); michael@0: return NULL; michael@0: } michael@0: michael@0: angle = MOD (angle, pixman_int_to_fixed (360)); michael@0: michael@0: image->type = CONICAL; michael@0: michael@0: conical->center = *center; michael@0: conical->angle = (pixman_fixed_to_double (angle) / 180.0) * M_PI; michael@0: michael@0: return image; michael@0: } michael@0: