[sapwood] add a test case for the simple rendering



commit 789bf1b5888306c2035274b3b791f82a95c3661e
Author: Sven Herzberg <sven herzberg lanedo com>
Date:   Thu Aug 5 10:03:40 2010 +0200

    add a test case for the simple rendering
    
    This test will allow to rewrite sapwood_pixmap_render_rects() to use
    cairo _and_ make sure the behavior is still the same.
    
    * tests/rendering.c: implement an initial test for the rendering
    * tests/test-larger.png: the reference image for the test

 tests/rendering.c     |  155 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test-larger.png |  Bin 0 -> 901 bytes
 2 files changed, 155 insertions(+), 0 deletions(-)
---
diff --git a/tests/rendering.c b/tests/rendering.c
index fc4350b..690b235 100644
--- a/tests/rendering.c
+++ b/tests/rendering.c
@@ -20,7 +20,160 @@
  * if advised of the possibility of such damage.
  */
 
+#include <errno.h>          /* errno */
+#include <stdint.h>         /* uintptr_t */
+#include <stdlib.h>         /* realpath() */
 #include <sapwood-pixmap.h>
+#include <gdk/gdkx.h>       /* GDK_DISPLAY_XDISPLAY() */
+
+#define assert_cmp_pixbuf(a,op,b) \
+  G_STMT_START { \
+    if (a && b) \
+      { \
+        int cmp_x, cmp_y; \
+        guchar* pixels_##a; \
+        guchar* pixels_##b; \
+        g_assert_cmpint (gdk_pixbuf_get_n_channels (a), \
+                         ==, \
+                         gdk_pixbuf_get_n_channels (b)); \
+        g_assert_cmpint (gdk_pixbuf_get_bits_per_sample (a), \
+                         ==, \
+                         gdk_pixbuf_get_bits_per_sample (b)); \
+        g_assert_cmpint (gdk_pixbuf_get_colorspace (a), \
+                         ==, \
+                         gdk_pixbuf_get_colorspace (b)); \
+        g_assert_cmpint (gdk_pixbuf_get_has_alpha (a), \
+                         ==, \
+                         gdk_pixbuf_get_has_alpha (b)); \
+        g_assert_cmpint (gdk_pixbuf_get_height (a), \
+                         ==, \
+                         gdk_pixbuf_get_height (b)); \
+        g_assert_cmpint (gdk_pixbuf_get_width (a), \
+                         ==, \
+                         gdk_pixbuf_get_width (b)); \
+        pixels_##a = gdk_pixbuf_get_pixels (a); \
+        pixels_##b = gdk_pixbuf_get_pixels (b); \
+        for (cmp_y = 0; cmp_y < gdk_pixbuf_get_height (a); cmp_y++) \
+          { \
+            for (cmp_x = 0; cmp_x < gdk_pixbuf_get_width (a); cmp_x++) \
+              { \
+                guchar* pixel_##a = pixels_##a + cmp_y * gdk_pixbuf_get_rowstride (a) + cmp_x * gdk_pixbuf_get_n_channels (a); \
+                guchar* pixel_##b = pixels_##b + cmp_y * gdk_pixbuf_get_rowstride (b) + cmp_x * gdk_pixbuf_get_n_channels (b); \
+                guint value_##a = (pixel_##a[0] << 16) + (pixel_##a[1] << 8) + pixel_##a[2]; \
+                guint value_##b = (pixel_##b[0] << 16) + (pixel_##b[1] << 8) + pixel_##b[2]; \
+                if (gdk_pixbuf_get_has_alpha (a)) \
+                  { \
+                    value_##a <<= 8; value_##a += pixel_##a[3]; \
+                    value_##b <<= 8; value_##b += pixel_##b[3]; \
+                  } \
+                if (value_##a op value_##b) ; else \
+                  { \
+                    gchar const* name_one = G_STRINGIFY (a) ".png"; \
+                    gchar const* name_two = G_STRINGIFY (b) ".png"; \
+                    gdk_pixbuf_save (a, name_one, "png", NULL, NULL); \
+                    gdk_pixbuf_save (b, name_two, "png", NULL, NULL); \
+                    g_message ("pixels at (%d;%d) differ: output images at \"%s\" and \"%s\"", \
+                               cmp_x, cmp_y, name_one, name_two); \
+                    g_assert_cmphex (value_##a, op, value_##b); \
+                  } \
+              } \
+          } \
+      } \
+    else \
+      { \
+        g_assert_cmpuint ((uintptr_t)a, ==, (uintptr_t)b); \
+      } \
+  } G_STMT_END
+
+static void
+test_larger (void)
+{
+  SapwoodPixmap* pixmap;
+  SapwoodRect    rects[9];
+  GdkColormap  * colormap;
+  GdkDrawable  * drawable = NULL;
+  GdkPixbuf    * result;
+  GdkPixbuf    * expected;
+  cairo_t      * cr;
+  GError       * error = NULL;
+  char           abspath[PATH_MAX + 1];
+  int            code;
+  int            i;
+
+  if (!realpath (TOP_SRCDIR G_DIR_SEPARATOR_S "tests" G_DIR_SEPARATOR_S "cropping-label1.png", abspath))
+    {
+      g_warning ("error in realpath(): \"%s\" causes %s",
+                 abspath,
+                 g_strerror (errno));
+    }
+  pixmap = sapwood_pixmap_get_for_file (abspath,
+                                        16, 16, 16, 16,
+                                        &error);
+  g_test_queue_destroy ((GFreeFunc) sapwood_pixmap_free, pixmap);
+  g_assert_no_error (error);
+
+  for (i = 0; i < G_N_ELEMENTS (rects); i++)
+    {
+      int col = i % 3;
+      int row = i / 3;
+
+      sapwood_pixmap_get_pixmap (pixmap, col, row,
+                                 &rects[i].pixmap, &rects[i].pixmask);
+
+      rects[i].dest.x = col < 1 ? 0 : col < 2 ? 16 : 200 - 16;
+      rects[i].dest.y = row < 1 ? 0 : row < 2 ? 16 : 200 - 16;
+      rects[i].dest.width =  col == 1 ? 200 - 2 * 16 : 16;
+      rects[i].dest.height = row == 1 ? 200 - 2 * 16 : 16;
+    }
+
+  colormap = gdk_screen_get_system_colormap (gdk_screen_get_default ());
+  g_assert (colormap);
+
+  drawable = gdk_pixmap_new (NULL, 200, 200, gdk_colormap_get_visual (colormap)->depth);
+  g_test_queue_unref (drawable);
+  gdk_drawable_set_colormap (drawable, colormap);
+
+  cr = gdk_cairo_create (drawable);
+  cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
+  cairo_paint (cr);
+  cairo_destroy (cr);
+
+  gdk_error_trap_push ();
+
+  sapwood_pixmap_render_rects (pixmap,
+                               GTK_TYPE_BUTTON,
+                               drawable,
+                               0, 0,
+                               200, 200,
+                               NULL,
+                               0, 0,
+                               FALSE,
+                               NULL,
+                               G_N_ELEMENTS (rects), rects);
+
+  gdk_flush ();
+  code = gdk_error_trap_pop ();
+  if (code)
+    {
+      XGetErrorText (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
+                     code,
+                     abspath,
+                     sizeof (abspath)); /* FIXME: check return code once we know _what_ it returns */
+      g_warning ("X11 error detected: %s (%d)", abspath, code);
+    }
+
+  result = gdk_pixbuf_get_from_drawable (NULL, drawable,
+                                         gdk_drawable_get_colormap (drawable),
+                                         0, 0, 0, 0,
+                                         200, 200);
+  g_test_queue_unref (result);
+
+  expected = gdk_pixbuf_new_from_file (TOP_SRCDIR G_DIR_SEPARATOR_S "tests" G_DIR_SEPARATOR_S "test-larger.png", &error);
+  g_assert_no_error (error);
+  g_test_queue_unref (expected);
+
+  assert_cmp_pixbuf (result, ==, expected);
+}
 
 int
 main (int   argc,
@@ -28,6 +181,8 @@ main (int   argc,
 {
   gtk_test_init (&argc, &argv, NULL);
 
+  g_test_add_func ("/sapwood/pixmap/render-rects/larger", test_larger);
+
   return g_test_run ();
 }
 
diff --git a/tests/test-larger.png b/tests/test-larger.png
new file mode 100644
index 0000000..d1b807a
Binary files /dev/null and b/tests/test-larger.png differ



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]