michael@0: /* -*- Mode: c; c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- */ 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: #include michael@0: #include "pixman-private.h" michael@0: michael@0: #include "pixman-dither.h" michael@0: michael@0: static pixman_bool_t michael@0: linear_gradient_is_horizontal (pixman_image_t *image, michael@0: int x, michael@0: int y, michael@0: int width, michael@0: int height) michael@0: { michael@0: linear_gradient_t *linear = (linear_gradient_t *)image; michael@0: pixman_vector_t v; michael@0: pixman_fixed_32_32_t l; michael@0: pixman_fixed_48_16_t dx, dy; michael@0: double inc; michael@0: michael@0: if (image->common.transform) michael@0: { michael@0: /* projective transformation */ michael@0: if (image->common.transform->matrix[2][0] != 0 || michael@0: image->common.transform->matrix[2][1] != 0 || michael@0: image->common.transform->matrix[2][2] == 0) michael@0: { michael@0: return FALSE; michael@0: } michael@0: michael@0: v.vector[0] = image->common.transform->matrix[0][1]; michael@0: v.vector[1] = image->common.transform->matrix[1][1]; michael@0: v.vector[2] = image->common.transform->matrix[2][2]; michael@0: } michael@0: else michael@0: { michael@0: v.vector[0] = 0; michael@0: v.vector[1] = pixman_fixed_1; michael@0: v.vector[2] = pixman_fixed_1; michael@0: } michael@0: michael@0: dx = linear->p2.x - linear->p1.x; michael@0: dy = linear->p2.y - linear->p1.y; michael@0: michael@0: l = dx * dx + dy * dy; michael@0: michael@0: if (l == 0) michael@0: return FALSE; michael@0: michael@0: /* michael@0: * compute how much the input of the gradient walked changes michael@0: * when moving vertically through the whole image michael@0: */ michael@0: inc = height * (double) pixman_fixed_1 * pixman_fixed_1 * michael@0: (dx * v.vector[0] + dy * v.vector[1]) / michael@0: (v.vector[2] * (double) l); michael@0: michael@0: /* check that casting to integer would result in 0 */ michael@0: if (-1 < inc && inc < 1) michael@0: return TRUE; michael@0: michael@0: return FALSE; michael@0: } michael@0: michael@0: static uint32_t * michael@0: linear_get_scanline_narrow (pixman_iter_t *iter, michael@0: 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: pixman_vector_t v, unit; michael@0: pixman_fixed_32_32_t l; michael@0: pixman_fixed_48_16_t dx, dy; michael@0: gradient_t *gradient = (gradient_t *)image; michael@0: linear_gradient_t *linear = (linear_gradient_t *)image; michael@0: uint32_t *end = buffer + width; michael@0: pixman_gradient_walker_t walker; michael@0: michael@0: _pixman_gradient_walker_init (&walker, gradient, image->common.repeat); 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 (image->common.transform) michael@0: { michael@0: if (!pixman_transform_point_3d (image->common.transform, &v)) michael@0: return iter->buffer; michael@0: michael@0: unit.vector[0] = image->common.transform->matrix[0][0]; michael@0: unit.vector[1] = image->common.transform->matrix[1][0]; michael@0: unit.vector[2] = image->common.transform->matrix[2][0]; michael@0: } michael@0: else michael@0: { michael@0: unit.vector[0] = pixman_fixed_1; michael@0: unit.vector[1] = 0; michael@0: unit.vector[2] = 0; michael@0: } michael@0: michael@0: dx = linear->p2.x - linear->p1.x; michael@0: dy = linear->p2.y - linear->p1.y; michael@0: michael@0: l = dx * dx + dy * dy; michael@0: michael@0: if (l == 0 || unit.vector[2] == 0) michael@0: { michael@0: /* affine transformation only */ michael@0: pixman_fixed_32_32_t t, next_inc; michael@0: double inc; michael@0: michael@0: if (l == 0 || v.vector[2] == 0) michael@0: { michael@0: t = 0; michael@0: inc = 0; michael@0: } michael@0: else michael@0: { michael@0: double invden, v2; michael@0: michael@0: invden = pixman_fixed_1 * (double) pixman_fixed_1 / michael@0: (l * (double) v.vector[2]); michael@0: v2 = v.vector[2] * (1. / pixman_fixed_1); michael@0: t = ((dx * v.vector[0] + dy * v.vector[1]) - michael@0: (dx * linear->p1.x + dy * linear->p1.y) * v2) * invden; michael@0: inc = (dx * unit.vector[0] + dy * unit.vector[1]) * invden; michael@0: } michael@0: next_inc = 0; michael@0: michael@0: if (((pixman_fixed_32_32_t )(inc * width)) == 0) michael@0: { michael@0: register uint32_t color; michael@0: michael@0: color = _pixman_gradient_walker_pixel (&walker, t); michael@0: while (buffer < end) michael@0: *buffer++ = color; michael@0: } michael@0: else michael@0: { michael@0: int i; michael@0: michael@0: i = 0; michael@0: while (buffer < end) michael@0: { michael@0: if (!mask || *mask++) michael@0: { michael@0: *buffer = _pixman_gradient_walker_pixel (&walker, michael@0: t + next_inc); michael@0: } michael@0: i++; michael@0: next_inc = inc * i; michael@0: buffer++; michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: /* projective transformation */ michael@0: double t; michael@0: michael@0: t = 0; michael@0: michael@0: while (buffer < end) michael@0: { michael@0: if (!mask || *mask++) michael@0: { michael@0: if (v.vector[2] != 0) michael@0: { michael@0: double invden, v2; michael@0: michael@0: invden = pixman_fixed_1 * (double) pixman_fixed_1 / michael@0: (l * (double) v.vector[2]); michael@0: v2 = v.vector[2] * (1. / pixman_fixed_1); michael@0: t = ((dx * v.vector[0] + dy * v.vector[1]) - michael@0: (dx * linear->p1.x + dy * linear->p1.y) * v2) * invden; michael@0: } michael@0: michael@0: *buffer = _pixman_gradient_walker_pixel (&walker, t); michael@0: } michael@0: michael@0: ++buffer; michael@0: michael@0: v.vector[0] += unit.vector[0]; michael@0: v.vector[1] += unit.vector[1]; michael@0: v.vector[2] += unit.vector[2]; michael@0: } michael@0: } michael@0: michael@0: iter->y++; michael@0: michael@0: return iter->buffer; michael@0: } michael@0: michael@0: static uint32_t * michael@0: linear_get_scanline_16 (pixman_iter_t *iter, michael@0: 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: uint16_t * buffer = (uint16_t*)iter->buffer; michael@0: pixman_bool_t toggle = ((x ^ y) & 1); michael@0: michael@0: pixman_vector_t v, unit; michael@0: pixman_fixed_32_32_t l; michael@0: pixman_fixed_48_16_t dx, dy; michael@0: gradient_t *gradient = (gradient_t *)image; michael@0: linear_gradient_t *linear = (linear_gradient_t *)image; michael@0: uint16_t *end = buffer + width; michael@0: pixman_gradient_walker_t walker; michael@0: michael@0: _pixman_gradient_walker_init (&walker, gradient, image->common.repeat); 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 (image->common.transform) michael@0: { michael@0: if (!pixman_transform_point_3d (image->common.transform, &v)) michael@0: return iter->buffer; michael@0: michael@0: unit.vector[0] = image->common.transform->matrix[0][0]; michael@0: unit.vector[1] = image->common.transform->matrix[1][0]; michael@0: unit.vector[2] = image->common.transform->matrix[2][0]; michael@0: } michael@0: else michael@0: { michael@0: unit.vector[0] = pixman_fixed_1; michael@0: unit.vector[1] = 0; michael@0: unit.vector[2] = 0; michael@0: } michael@0: michael@0: dx = linear->p2.x - linear->p1.x; michael@0: dy = linear->p2.y - linear->p1.y; michael@0: michael@0: l = dx * dx + dy * dy; michael@0: michael@0: if (l == 0 || unit.vector[2] == 0) michael@0: { michael@0: /* affine transformation only */ michael@0: pixman_fixed_32_32_t t, next_inc; michael@0: double inc; michael@0: michael@0: if (l == 0 || v.vector[2] == 0) michael@0: { michael@0: t = 0; michael@0: inc = 0; michael@0: } michael@0: else michael@0: { michael@0: double invden, v2; michael@0: michael@0: invden = pixman_fixed_1 * (double) pixman_fixed_1 / michael@0: (l * (double) v.vector[2]); michael@0: v2 = v.vector[2] * (1. / pixman_fixed_1); michael@0: t = ((dx * v.vector[0] + dy * v.vector[1]) - michael@0: (dx * linear->p1.x + dy * linear->p1.y) * v2) * invden; michael@0: inc = (dx * unit.vector[0] + dy * unit.vector[1]) * invden; michael@0: } michael@0: next_inc = 0; michael@0: michael@0: if (((pixman_fixed_32_32_t )(inc * width)) == 0) michael@0: { michael@0: register uint32_t color; michael@0: uint16_t dither_diff; michael@0: uint16_t color16; michael@0: uint16_t color16b; michael@0: michael@0: color = _pixman_gradient_walker_pixel (&walker, t); michael@0: color16 = dither_8888_to_0565(color, toggle); michael@0: color16b = dither_8888_to_0565(color, toggle^1); michael@0: // compute the difference michael@0: dither_diff = color16 ^ color16b; michael@0: while (buffer < end) { michael@0: *buffer++ = color16; michael@0: // use dither_diff to toggle between color16 and color16b michael@0: color16 ^= dither_diff; michael@0: toggle ^= 1; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: int i; michael@0: michael@0: i = 0; michael@0: while (buffer < end) michael@0: { michael@0: if (!mask || *mask++) michael@0: { michael@0: *buffer = dither_8888_to_0565(_pixman_gradient_walker_pixel (&walker, michael@0: t + next_inc), michael@0: toggle); michael@0: } michael@0: toggle ^= 1; michael@0: i++; michael@0: next_inc = inc * i; michael@0: buffer++; michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: /* projective transformation */ michael@0: double t; michael@0: michael@0: t = 0; michael@0: michael@0: while (buffer < end) michael@0: { michael@0: if (!mask || *mask++) michael@0: { michael@0: if (v.vector[2] != 0) michael@0: { michael@0: double invden, v2; michael@0: michael@0: invden = pixman_fixed_1 * (double) pixman_fixed_1 / michael@0: (l * (double) v.vector[2]); michael@0: v2 = v.vector[2] * (1. / pixman_fixed_1); michael@0: t = ((dx * v.vector[0] + dy * v.vector[1]) - michael@0: (dx * linear->p1.x + dy * linear->p1.y) * v2) * invden; michael@0: } michael@0: michael@0: *buffer = dither_8888_to_0565(_pixman_gradient_walker_pixel (&walker, t), michael@0: toggle); michael@0: } michael@0: toggle ^= 1; michael@0: michael@0: ++buffer; michael@0: michael@0: v.vector[0] += unit.vector[0]; michael@0: v.vector[1] += unit.vector[1]; michael@0: v.vector[2] += unit.vector[2]; michael@0: } michael@0: } michael@0: michael@0: iter->y++; michael@0: michael@0: return iter->buffer; michael@0: } michael@0: michael@0: static uint32_t * michael@0: linear_get_scanline_wide (pixman_iter_t *iter, const uint32_t *mask) michael@0: { michael@0: uint32_t *buffer = linear_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_linear_gradient_iter_init (pixman_image_t *image, pixman_iter_t *iter) michael@0: { michael@0: // XXX: we can't use this optimization when dithering michael@0: if (0 && linear_gradient_is_horizontal ( michael@0: iter->image, iter->x, iter->y, iter->width, iter->height)) michael@0: { michael@0: if (iter->iter_flags & ITER_16) michael@0: linear_get_scanline_16 (iter, NULL); michael@0: else if (iter->iter_flags & ITER_NARROW) michael@0: linear_get_scanline_narrow (iter, NULL); michael@0: else michael@0: linear_get_scanline_wide (iter, NULL); michael@0: michael@0: iter->get_scanline = _pixman_iter_get_scanline_noop; michael@0: } michael@0: else michael@0: { michael@0: if (iter->iter_flags & ITER_16) michael@0: iter->get_scanline = linear_get_scanline_16; michael@0: else if (iter->iter_flags & ITER_NARROW) michael@0: iter->get_scanline = linear_get_scanline_narrow; michael@0: else michael@0: iter->get_scanline = linear_get_scanline_wide; michael@0: } michael@0: } michael@0: michael@0: PIXMAN_EXPORT pixman_image_t * michael@0: pixman_image_create_linear_gradient (const pixman_point_fixed_t * p1, michael@0: const pixman_point_fixed_t * p2, michael@0: const pixman_gradient_stop_t *stops, michael@0: int n_stops) michael@0: { michael@0: pixman_image_t *image; michael@0: linear_gradient_t *linear; michael@0: michael@0: image = _pixman_image_allocate (); michael@0: michael@0: if (!image) michael@0: return NULL; michael@0: michael@0: linear = &image->linear; michael@0: michael@0: if (!_pixman_init_gradient (&linear->common, stops, n_stops)) michael@0: { michael@0: free (image); michael@0: return NULL; michael@0: } michael@0: michael@0: linear->p1 = *p1; michael@0: linear->p2 = *p2; michael@0: michael@0: image->type = LINEAR; michael@0: michael@0: return image; michael@0: } michael@0: