[Pixman] [PATCH 2/2] demos: Add checkerboard demo

Søren Sandmann sandmann at cs.au.dk
Sat Mar 24 13:25:00 PDT 2012


From: Søren Sandmann Pedersen <ssp at redhat.com>

This is a simple demo that displays a checkboard with a projective
transformation.
---
 demos/Makefile.am    |    5 +-
 demos/checkerboard.c |  152 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 156 insertions(+), 1 deletions(-)
 create mode 100644 demos/checkerboard.c

diff --git a/demos/Makefile.am b/demos/Makefile.am
index 070c2d7..6049090 100644
--- a/demos/Makefile.am
+++ b/demos/Makefile.am
@@ -18,7 +18,9 @@ DEMOS =				\
 	screen-test		\
 	convolution-test	\
 	trap-test		\
-	tri-test
+	tri-test		\
+	quad2quad		\
+	checkerboard
 
 gradient_test_SOURCES = gradient-test.c $(GTK_UTILS)
 alpha_test_SOURCES = alpha-test.c $(GTK_UTILS)
@@ -30,6 +32,7 @@ screen_test_SOURCES = screen-test.c $(GTK_UTILS)
 convolution_test_SOURCES = convolution-test.c $(GTK_UTILS)
 radial_test_SOURCES = radial-test.c ../test/utils.c ../test/utils.h $(GTK_UTILS)
 tri_test_SOURCES = tri-test.c ../test/utils.c ../test/utils.h $(GTK_UTILS)
+checkerboard_SOURCES = checkerboard.c $(GTK_UTILS)
 
 noinst_PROGRAMS = $(DEMOS)
 
diff --git a/demos/checkerboard.c b/demos/checkerboard.c
new file mode 100644
index 0000000..264717d
--- /dev/null
+++ b/demos/checkerboard.c
@@ -0,0 +1,152 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "pixman.h"
+#include "gtk-utils.h"
+
+int
+main (int argc, char **argv)
+{
+#define WIDTH 400
+#define HEIGHT 400
+#define TILE_SIZE 25
+    
+    pixman_image_t *checkerboard;
+    pixman_image_t *destination;
+#define D2F(d) (pixman_double_to_fixed(d))
+    pixman_transform_t trans = { {
+	{ D2F (-1.96830), D2F (-1.82250), D2F (512.12250)},
+	{ D2F (0.00000), D2F (-7.29000), D2F (1458.00000)},
+	{ D2F (0.00000), D2F (-0.00911), D2F (0.59231)},
+	}};
+	
+	
+    int i, j;
+
+    checkerboard = pixman_image_create_bits (PIXMAN_a8r8g8b8,
+					     WIDTH, HEIGHT,
+					     NULL, 0);
+
+    destination = pixman_image_create_bits (PIXMAN_a8r8g8b8,
+					    WIDTH, HEIGHT,
+					    NULL, 0);
+
+    for (i = 0; i < HEIGHT / TILE_SIZE; ++i)
+    {
+	for (j = 0; j < WIDTH / TILE_SIZE; ++j)
+	{
+	    double u = (double)(j + 1) / (WIDTH / TILE_SIZE);
+	    double v = (double)(i + 1) / (HEIGHT / TILE_SIZE);
+	    pixman_color_t black = { 0, 0, 0, 0xffff };
+	    pixman_color_t white = {
+		v * 0xffff,
+		u * 0xffff,
+		(1 - (double)u) * 0xffff,
+		0xffff };
+	    pixman_color_t *c;
+	    pixman_image_t *fill;
+
+	    if ((j & 1) != (i & 1))
+		c = &black;
+	    else
+		c = &white;
+
+	    fill = pixman_image_create_solid_fill (c);
+
+	    pixman_image_composite (PIXMAN_OP_SRC, fill, NULL, checkerboard,
+				    0, 0, 0, 0, j * TILE_SIZE, i * TILE_SIZE,
+				    TILE_SIZE, TILE_SIZE);
+	}
+    }
+
+    pixman_image_set_transform (checkerboard, &trans);
+    pixman_image_set_filter (checkerboard, PIXMAN_FILTER_BEST, NULL, 0);
+    pixman_image_set_repeat (checkerboard, PIXMAN_REPEAT_NONE);
+    
+    pixman_image_composite (PIXMAN_OP_SRC,
+			    checkerboard, NULL, destination,
+			    0, 0, 0, 0, 0, 0,
+			    WIDTH, HEIGHT);
+    
+    show_image (destination);
+#if 0
+
+    
+    uint32_t *dest = malloc (WIDTH * HEIGHT * 4);
+    pixman_image_t *src_img;
+    pixman_image_t *dest_img;
+    int i;
+    pixman_gradient_stop_t stops[2] =
+	{
+	    { pixman_int_to_fixed (0), { 0xffff, 0xeeee, 0xeeee, 0xeeee } },
+	    { pixman_int_to_fixed (1), { 0xffff, 0x1111, 0x1111, 0x1111 } }
+	};
+    pixman_point_fixed_t p1 = { pixman_double_to_fixed (0), 0 };
+    pixman_point_fixed_t p2 = { pixman_double_to_fixed (WIDTH / 8.),
+				pixman_int_to_fixed (0) };
+#if 0
+    pixman_transform_t trans = {
+	{ { pixman_double_to_fixed (2), pixman_double_to_fixed (0.5), pixman_double_to_fixed (-100), },
+	  { pixman_double_to_fixed (0), pixman_double_to_fixed (3), pixman_double_to_fixed (0), },
+	  { pixman_double_to_fixed (0), pixman_double_to_fixed (0.000), pixman_double_to_fixed (1.0) } 
+	}
+    };
+#else
+    pixman_transform_t trans = {
+	{ { pixman_fixed_1, 0, 0 },
+	  { 0, pixman_fixed_1, 0 },
+	  { 0, 0, pixman_fixed_1 } }
+    };
+#endif
+
+    pixman_point_fixed_t c_inner;
+    pixman_point_fixed_t c_outer;
+    pixman_fixed_t r_inner;
+    pixman_fixed_t r_outer;
+    
+    for (i = 0; i < WIDTH * HEIGHT; ++i)
+	dest[i] = 0x4f00004f; /* pale blue */
+    
+    dest_img = pixman_image_create_bits (PIXMAN_a8r8g8b8,
+					 WIDTH, HEIGHT, 
+					 dest,
+					 WIDTH * 4);
+
+    c_inner.x = pixman_double_to_fixed (50.0);
+    c_inner.y = pixman_double_to_fixed (50.0);
+    c_outer.x = pixman_double_to_fixed (50.0);
+    c_outer.y = pixman_double_to_fixed (50.0);
+    r_inner = 0;
+    r_outer = pixman_double_to_fixed (50.0);
+    
+    src_img = pixman_image_create_conical_gradient (&c_inner, r_inner,
+						    stops, 2);
+#if 0
+    src_img = pixman_image_create_conical_gradient (&c_inner, r_inner,
+						    stops, 2);
+    src_img = pixman_image_create_linear_gradient (&c_inner, &c_outer,
+						   r_inner, r_outer,
+						   stops, 2);
+#endif
+    
+    src_img = pixman_image_create_linear_gradient  (&p1, &p2,
+						    stops, 2);
+    
+    pixman_image_set_transform (src_img, &trans);
+    pixman_image_set_repeat (src_img, PIXMAN_REPEAT_PAD);
+    
+    pixman_image_composite (PIXMAN_OP_OVER, src_img, NULL, dest_img,
+			    0, 0, 0, 0, 0, 0, 10 * WIDTH, HEIGHT);
+    
+    printf ("0, 0: %x\n", dest[0]);
+    printf ("10, 10: %x\n", dest[10 * 10 + 10]);
+    printf ("w, h: %x\n", dest[(HEIGHT - 1) * 100 + (WIDTH - 1)]);
+    
+    show_image (dest_img);
+    
+    pixman_image_unref (src_img);
+    pixman_image_unref (dest_img);
+    free (dest);
+#endif
+    
+    return 0;
+}
-- 
1.6.0.6



More information about the Pixman mailing list