Blame demos/radial-test.c

Packit 030a23
#include "../test/utils.h"
Packit 030a23
#include "gtk-utils.h"
Packit 030a23
Packit 030a23
#define NUM_GRADIENTS 9
Packit 030a23
#define NUM_STOPS 3
Packit 030a23
#define NUM_REPEAT 4
Packit 030a23
#define SIZE 128
Packit 030a23
#define WIDTH (SIZE * NUM_GRADIENTS)
Packit 030a23
#define HEIGHT (SIZE * NUM_REPEAT)
Packit 030a23
Packit 030a23
/*
Packit 030a23
 * We want to test all the possible relative positions of the start
Packit 030a23
 * and end circle:
Packit 030a23
 *
Packit 030a23
 *  - The start circle can be smaller/equal/bigger than the end
Packit 030a23
 *    circle. A radial gradient can be classified in one of these
Packit 030a23
 *    three cases depending on the sign of dr.
Packit 030a23
 *
Packit 030a23
 *  - The smaller circle can be completely inside/internally
Packit 030a23
 *    tangent/outside (at least in part) of the bigger circle. This
Packit 030a23
 *    classification is the same as the one which can be computed by
Packit 030a23
 *    examining the sign of a = (dx^2 + dy^2 - dr^2).
Packit 030a23
 *
Packit 030a23
 *  - If the two circles have the same size, neither can be inside or
Packit 030a23
 *    internally tangent
Packit 030a23
 *
Packit 030a23
 * This test draws radial gradients whose circles always have the same
Packit 030a23
 * centers (0, 0) and (1, 0), but with different radiuses. From left
Packit 030a23
 * to right:
Packit 030a23
 *
Packit 030a23
 * - Degenerate start circle completely inside the end circle
Packit 030a23
 *     0.00 -> 1.75; dr = 1.75 > 0; a = 1 - 1.75^2 < 0
Packit 030a23
 *
Packit 030a23
 * - Small start circle completely inside the end circle
Packit 030a23
 *     0.25 -> 1.75; dr =  1.5 > 0; a = 1 - 1.50^2 < 0
Packit 030a23
 *
Packit 030a23
 * - Small start circle internally tangent to the end circle
Packit 030a23
 *     0.50 -> 1.50; dr =  1.0 > 0; a = 1 - 1.00^2 = 0
Packit 030a23
 *
Packit 030a23
 * - Small start circle outside of the end circle
Packit 030a23
 *     0.50 -> 1.00; dr =  0.5 > 0; a = 1 - 0.50^2 > 0
Packit 030a23
 *
Packit 030a23
 * - Start circle with the same size as the end circle
Packit 030a23
 *     1.00 -> 1.00; dr =  0.0 = 0; a = 1 - 0.00^2 > 0
Packit 030a23
 *
Packit 030a23
 * - Small end circle outside of the start circle
Packit 030a23
 *     1.00 -> 0.50; dr = -0.5 > 0; a = 1 - 0.50^2 > 0
Packit 030a23
 *
Packit 030a23
 * - Small end circle internally tangent to the start circle
Packit 030a23
 *     1.50 -> 0.50; dr = -1.0 > 0; a = 1 - 1.00^2 = 0
Packit 030a23
 *
Packit 030a23
 * - Small end circle completely inside the start circle
Packit 030a23
 *     1.75 -> 0.25; dr = -1.5 > 0; a = 1 - 1.50^2 < 0
Packit 030a23
 *
Packit 030a23
 * - Degenerate end circle completely inside the start circle
Packit 030a23
 *     0.00 -> 1.75; dr = 1.75 > 0; a = 1 - 1.75^2 < 0
Packit 030a23
 *
Packit 030a23
 */
Packit 030a23
Packit 030a23
const static double radiuses[NUM_GRADIENTS] = {
Packit 030a23
    0.00,
Packit 030a23
    0.25,
Packit 030a23
    0.50,
Packit 030a23
    0.50,
Packit 030a23
    1.00,
Packit 030a23
    1.00,
Packit 030a23
    1.50,
Packit 030a23
    1.75,
Packit 030a23
    1.75
Packit 030a23
};
Packit 030a23
Packit 030a23
#define double_to_color(x)					\
Packit 030a23
    (((uint32_t) ((x)*65536)) - (((uint32_t) ((x)*65536)) >> 16))
Packit 030a23
Packit 030a23
#define PIXMAN_STOP(offset,r,g,b,a)		\
Packit 030a23
    { pixman_double_to_fixed (offset),		\
Packit 030a23
	{					\
Packit 030a23
	double_to_color (r),			\
Packit 030a23
	double_to_color (g),			\
Packit 030a23
	double_to_color (b),			\
Packit 030a23
	double_to_color (a)			\
Packit 030a23
	}					\
Packit 030a23
    }
Packit 030a23
Packit 030a23
static const pixman_gradient_stop_t stops[NUM_STOPS] = {
Packit 030a23
    PIXMAN_STOP (0.0,        1, 0, 0, 0.75),
Packit 030a23
    PIXMAN_STOP (0.70710678, 0, 1, 0, 0),
Packit 030a23
    PIXMAN_STOP (1.0,        0, 0, 1, 1)
Packit 030a23
};
Packit 030a23
Packit 030a23
static pixman_image_t *
Packit 030a23
create_radial (int index)
Packit 030a23
{
Packit 030a23
    pixman_point_fixed_t p0, p1;
Packit 030a23
    pixman_fixed_t r0, r1;
Packit 030a23
    double x0, x1, radius0, radius1, left, right, center;
Packit 030a23
Packit 030a23
    x0 = 0;
Packit 030a23
    x1 = 1;
Packit 030a23
    radius0 = radiuses[index];
Packit 030a23
    radius1 = radiuses[NUM_GRADIENTS - index - 1];
Packit 030a23
Packit 030a23
    /* center the gradient */
Packit 030a23
    left = MIN (x0 - radius0, x1 - radius1);
Packit 030a23
    right = MAX (x0 + radius0, x1 + radius1);
Packit 030a23
    center = (left + right) * 0.5;
Packit 030a23
    x0 -= center;
Packit 030a23
    x1 -= center;
Packit 030a23
Packit 030a23
    /* scale to make it fit within a 1x1 rect centered in (0,0) */
Packit 030a23
    x0 *= 0.25;
Packit 030a23
    x1 *= 0.25;
Packit 030a23
    radius0 *= 0.25;
Packit 030a23
    radius1 *= 0.25;
Packit 030a23
Packit 030a23
    p0.x = pixman_double_to_fixed (x0);
Packit 030a23
    p0.y = pixman_double_to_fixed (0);
Packit 030a23
Packit 030a23
    p1.x = pixman_double_to_fixed (x1);
Packit 030a23
    p1.y = pixman_double_to_fixed (0);
Packit 030a23
Packit 030a23
    r0 = pixman_double_to_fixed (radius0);
Packit 030a23
    r1 = pixman_double_to_fixed (radius1);
Packit 030a23
Packit 030a23
    return pixman_image_create_radial_gradient (&p0, &p1,
Packit 030a23
						r0, r1,
Packit 030a23
						stops, NUM_STOPS);
Packit 030a23
}
Packit 030a23
Packit 030a23
static const pixman_repeat_t repeat[NUM_REPEAT] = {
Packit 030a23
    PIXMAN_REPEAT_NONE,
Packit 030a23
    PIXMAN_REPEAT_NORMAL,
Packit 030a23
    PIXMAN_REPEAT_REFLECT,
Packit 030a23
    PIXMAN_REPEAT_PAD
Packit 030a23
};
Packit 030a23
Packit 030a23
int
Packit 030a23
main (int argc, char **argv)
Packit 030a23
{
Packit 030a23
    pixman_transform_t transform;
Packit 030a23
    pixman_image_t *src_img, *dest_img;
Packit 030a23
    int i, j;
Packit 030a23
Packit 030a23
    enable_divbyzero_exceptions ();
Packit 030a23
Packit 030a23
    dest_img = pixman_image_create_bits (PIXMAN_a8r8g8b8,
Packit 030a23
					 WIDTH, HEIGHT,
Packit 030a23
					 NULL, 0);
Packit 030a23
Packit 030a23
    draw_checkerboard (dest_img, 25, 0xffaaaaaa, 0xffbbbbbb);
Packit 030a23
    
Packit 030a23
    pixman_transform_init_identity (&transform);
Packit 030a23
Packit 030a23
    /*
Packit 030a23
     * The create_radial() function returns gradients centered in the
Packit 030a23
     * origin and whose interesting part fits a 1x1 square. We want to
Packit 030a23
     * paint these gradients on a SIZExSIZE square and to make things
Packit 030a23
     * easier we want the origin in the top-left corner of the square
Packit 030a23
     * we want to see.
Packit 030a23
     */
Packit 030a23
    pixman_transform_translate (NULL, &transform,
Packit 030a23
				pixman_double_to_fixed (0.5),
Packit 030a23
				pixman_double_to_fixed (0.5));
Packit 030a23
Packit 030a23
    pixman_transform_scale (NULL, &transform,
Packit 030a23
			    pixman_double_to_fixed (SIZE),
Packit 030a23
			    pixman_double_to_fixed (SIZE));
Packit 030a23
Packit 030a23
    /*
Packit 030a23
     * Gradients are evaluated at the center of each pixel, so we need
Packit 030a23
     * to translate by half a pixel to trigger some interesting
Packit 030a23
     * cornercases. In particular, the original implementation of PDF
Packit 030a23
     * radial gradients tried to divide by 0 when using this transform
Packit 030a23
     * on the "tangent circles" cases.
Packit 030a23
     */
Packit 030a23
    pixman_transform_translate (NULL, &transform,
Packit 030a23
				pixman_double_to_fixed (0.5),
Packit 030a23
				pixman_double_to_fixed (0.5));
Packit 030a23
Packit 030a23
    for (i = 0; i < NUM_GRADIENTS; i++)
Packit 030a23
    {
Packit 030a23
	src_img = create_radial (i);
Packit 030a23
	pixman_image_set_transform (src_img, &transform);
Packit 030a23
Packit 030a23
	for (j = 0; j < NUM_REPEAT; j++)
Packit 030a23
	{
Packit 030a23
	    pixman_image_set_repeat (src_img, repeat[j]);
Packit 030a23
Packit 030a23
	    pixman_image_composite32 (PIXMAN_OP_OVER,
Packit 030a23
				      src_img,
Packit 030a23
				      NULL,
Packit 030a23
				      dest_img,
Packit 030a23
				      0, 0,
Packit 030a23
				      0, 0,
Packit 030a23
				      i * SIZE, j * SIZE,
Packit 030a23
				      SIZE, SIZE);
Packit 030a23
Packit 030a23
	}
Packit 030a23
Packit 030a23
	pixman_image_unref (src_img);
Packit 030a23
    }
Packit 030a23
Packit 030a23
    show_image (dest_img);
Packit 030a23
Packit 030a23
    pixman_image_unref (dest_img);
Packit 030a23
Packit 030a23
    return 0;
Packit 030a23
}