test: Add new alphamap test program.
authorSøren Sandmann Pedersen <sandmann@redhat.com>
Sat, 16 Jan 2010 14:48:45 +0000 (09:48 -0500)
committerSøren Sandmann Pedersen <sandmann@redhat.com>
Sun, 17 Jan 2010 21:47:15 +0000 (16:47 -0500)
This program demonstrates three bugs relating to alpha maps:

- When fetching from an alpha map into 32 bit intermediates, we use
  the fetcher from the image, and not the one from the alpha map.

- For 64 bit intermediates we call fetch_pixel_generic_lossy_32()
  which then calls fetch_pixel_raw_64, which is NULL because alpha
  images are never validated.

- The alpha map should be used *in place* of any existing alpha
  channel, but we are actually multiplying it onto the image.

test/Makefile.am
test/alphamap.c [new file with mode: 0644]
test/utils.c
test/utils.h

index ab98756..5f6ba13 100644 (file)
@@ -7,6 +7,7 @@ TESTPROGRAMS =                  \
        oob-test                \
        window-test             \
        trap-crasher            \
+       alphamap                \
        blitters-test           \
        scaling-test            \
        composite
@@ -24,6 +25,9 @@ blitters_test_SOURCES = blitters-test.c utils.c utils.h
 scaling_test_LDADD = $(TEST_LDADD)
 scaling_test_SOURCES = scaling-test.c utils.c utils.h
 
+alphamap_LDADD = $(TEST_LDADD)
+alphamap_SOURCES = alphamap.c utils.c utils.h
+
 # GTK using test programs
 
 if HAVE_GTK
@@ -39,7 +43,8 @@ TESTPROGRAMS_GTK =            \
        alpha-test              \
        screen-test             \
        convolution-test        \
-       trap-test
+       trap-test               \
+       alphamap
 
 INCLUDES += $(GTK_CFLAGS)
 
diff --git a/test/alphamap.c b/test/alphamap.c
new file mode 100644 (file)
index 0000000..e6a25ef
--- /dev/null
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "utils.h"
+
+#define WIDTH 400
+#define HEIGHT 200
+
+int
+main (int argc, char **argv)
+{
+    uint8_t *alpha = make_random_bytes (WIDTH * HEIGHT);
+    uint32_t *src = (uint32_t *)make_random_bytes (WIDTH * HEIGHT * 4);
+    uint32_t *dest = (uint32_t *)make_random_bytes (WIDTH * HEIGHT * 4);
+    int i;
+
+    pixman_image_t *a = pixman_image_create_bits (PIXMAN_a8, WIDTH, HEIGHT, (uint32_t *)alpha, WIDTH);
+    pixman_image_t *d = pixman_image_create_bits (PIXMAN_a8r8g8b8, WIDTH, HEIGHT, dest, WIDTH * 4);
+
+    for (i = 0; i < 2; ++i)
+    {
+       pixman_format_code_t sformat = (i == 0)? PIXMAN_a8r8g8b8 : PIXMAN_a2r10g10b10;
+       pixman_image_t *s = pixman_image_create_bits (sformat, WIDTH, HEIGHT, src, WIDTH * 4);
+       int j, k;
+
+       pixman_image_set_alpha_map (s, a, 0, 0);
+
+       pixman_image_composite (PIXMAN_OP_SRC, s, NULL, d, 0, 0, 0, 0, 0, 0, WIDTH, HEIGHT);
+
+       for (j = 0; j < HEIGHT; ++j)
+       {
+           for (k = 0; k < WIDTH; ++k)
+           {
+               uint8_t ap = ((uint8_t *)alpha)[j * WIDTH + k];
+               uint32_t dap = (dest[j * WIDTH + k] >> 24);
+               uint32_t sap = (src[j * WIDTH + k] >> 24);
+
+               if (ap != dap)
+               {
+                   printf ("Wrong alpha value at (%d, %d). Should be %d; got %d (src was %d)\n", k, j, ap, dap, sap);
+                   return 1;
+               }
+           }
+       }
+
+       pixman_image_unref (s);
+    }
+
+    return 0;
+}
index 1e42d89..58cd100 100644 (file)
@@ -192,3 +192,17 @@ image_endian_swap (pixman_image_t *img, int bpp)
     }
 }
 
+uint8_t *
+make_random_bytes (int n_bytes)
+{
+    uint8_t *bytes = malloc (n_bytes);
+    int i;
+
+    if (!bytes)
+       return NULL;
+
+    for (i = 0; i < n_bytes; ++i)
+       bytes[i] = lcg_rand () & 0xff;
+
+    return bytes;
+}
index 8fdb2ce..fb1ccec 100644 (file)
@@ -39,3 +39,7 @@ compute_crc32 (uint32_t    in_crc32,
  */
 void
 image_endian_swap (pixman_image_t *img, int bpp);
+
+/* Generate n_bytes random bytes in malloced memory */
+uint8_t *
+make_random_bytes (int n_bytes);