[librsvg] tests: move compare_surfaces to test-utils



commit 9b1d2e474523a804f04c0ee5ca27f65f8c44268a
Author: Paolo Borelli <pborelli gnome org>
Date:   Wed Dec 26 09:46:25 2018 +0100

    tests: move compare_surfaces to test-utils
    
    We will use it also in the rsvg_handle_get_pixbuf tests.

 tests/rsvg-test.c  | 103 ++---------------------------------------------------
 tests/test-utils.c |  95 ++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test-utils.h |  13 ++++++-
 3 files changed, 109 insertions(+), 102 deletions(-)
---
diff --git a/tests/rsvg-test.c b/tests/rsvg-test.c
index 05a7c6b9..dc4a99a3 100644
--- a/tests/rsvg-test.c
+++ b/tests/rsvg-test.c
@@ -42,105 +42,6 @@
 
 static char *_output_dir;
 
-typedef struct _buffer_diff_result {
-    unsigned int pixels_changed;
-    unsigned int max_diff;
-} buffer_diff_result_t;
-
-/* Compare two buffers, returning the number of pixels that are
- * different and the maximum difference of any single color channel in
- * result_ret.
- *
- * This function should be rewritten to compare all formats supported by
- * cairo_format_t instead of taking a mask as a parameter.
- */
-static void
-buffer_diff_core (unsigned char *_buf_a,
-                 unsigned char *_buf_b,
-                 unsigned char *_buf_diff,
-                 int           width,
-                 int           height,
-                 int           stride,
-                 guint32       mask,
-                 buffer_diff_result_t *result_ret)
-{
-    int x, y;
-    guint32 *row_a, *row_b, *row;
-    buffer_diff_result_t result = {0, 0};
-    guint32 *buf_a = (guint32 *) _buf_a;
-    guint32 *buf_b = (guint32 *) _buf_b;
-    guint32 *buf_diff = (guint32 *) _buf_diff;
-
-    stride /= sizeof(guint32);
-    for (y = 0; y < height; y++)
-    {
-       row_a = buf_a + y * stride;
-       row_b = buf_b + y * stride;
-       row = buf_diff + y * stride;
-       for (x = 0; x < width; x++)
-       {
-           /* check if the pixels are the same */
-           if ((row_a[x] & mask) != (row_b[x] & mask)) {
-               int channel;
-               guint32 diff_pixel = 0;
-
-               /* calculate a difference value for all 4 channels */
-               for (channel = 0; channel < 4; channel++) {
-                   int value_a = (row_a[x] >> (channel*8)) & 0xff;
-                   int value_b = (row_b[x] >> (channel*8)) & 0xff;
-                   unsigned int diff;
-                   diff = abs (value_a - value_b);
-                   if (diff > result.max_diff)
-                       result.max_diff = diff;
-                   diff *= 4;  /* emphasize */
-                   if (diff)
-                       diff += 128; /* make sure it's visible */
-                   if (diff > 255)
-                       diff = 255;
-                   diff_pixel |= diff << (channel*8);
-               }
-
-               result.pixels_changed++;
-               if ((diff_pixel & 0x00ffffff) == 0) {
-                   /* alpha only difference, convert to luminance */
-                   guint8 alpha = diff_pixel >> 24;
-                   diff_pixel = alpha * 0x010101;
-               }
-               row[x] = diff_pixel;
-           } else {
-               row[x] = 0;
-           }
-           row[x] |= 0xff000000; /* Set ALPHA to 100% (opaque) */
-       }
-    }
-
-    *result_ret = result;
-}
-
-static void
-compare_surfaces (cairo_surface_t      *surface_a,
-                 cairo_surface_t       *surface_b,
-                 cairo_surface_t       *surface_diff,
-                 buffer_diff_result_t  *result)
-{
-    /* Here, we run cairo's old buffer_diff algorithm which looks for
-     * pixel-perfect images.
-     */
-    buffer_diff_core (cairo_image_surface_get_data (surface_a),
-                     cairo_image_surface_get_data (surface_b),
-                     cairo_image_surface_get_data (surface_diff),
-                     cairo_image_surface_get_width (surface_a),
-                     cairo_image_surface_get_height (surface_a),
-                     cairo_image_surface_get_stride (surface_a),
-                     0xffffffff,
-                     result);
-    if (result->pixels_changed == 0)
-       return;
-
-    g_test_message ("%d pixels differ (with maximum difference of %d) from reference image\n",
-                   result->pixels_changed, result->max_diff);
-}
-
 static char *
 get_output_dir (void) {
     if (_output_dir == NULL) {
@@ -319,7 +220,7 @@ rsvg_cairo_check (gconstpointer data)
     cairo_t *cr;
     cairo_surface_t *render_surface;
     cairo_surface_t *surface_a, *surface_b, *surface_diff;
-    buffer_diff_result_t result;
+    TestUtilsBufferDiffResult result;
     char *test_file_base;
     unsigned int width_a, height_a, stride_a;
     unsigned int width_b, height_b, stride_b;
@@ -387,7 +288,7 @@ rsvg_cairo_check (gconstpointer data)
        surface_diff = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                                   dimensions.width, dimensions.height);
 
-       compare_surfaces (surface_a, surface_b, surface_diff, &result);
+       test_utils_compare_surfaces (surface_a, surface_b, surface_diff, &result);
 
        if (result.pixels_changed && result.max_diff > MAX_DIFF) {
             g_test_fail ();
diff --git a/tests/test-utils.c b/tests/test-utils.c
index 40d3d07a..faa4a56d 100644
--- a/tests/test-utils.c
+++ b/tests/test-utils.c
@@ -6,7 +6,102 @@
 
 #include <string.h>
 
+/* Compare two buffers, returning the number of pixels that are
+ * different and the maximum difference of any single color channel in
+ * result_ret.
+ *
+ * This function should be rewritten to compare all formats supported by
+ * cairo_format_t instead of taking a mask as a parameter.
+ */
+static void
+buffer_diff_core (unsigned char             *_buf_a,
+                  unsigned char             *_buf_b,
+                  unsigned char             *_buf_diff,
+                  int                        width,
+                  int                        height,
+                  int                        stride,
+                  guint32                    mask,
+                  TestUtilsBufferDiffResult *result_ret)
+{
+    int x, y;
+    guint32 *row_a, *row_b, *row;
+    TestUtilsBufferDiffResult result = {0, 0};
+    guint32 *buf_a = (guint32 *) _buf_a;
+    guint32 *buf_b = (guint32 *) _buf_b;
+    guint32 *buf_diff = (guint32 *) _buf_diff;
+
+    stride /= sizeof(guint32);
+    for (y = 0; y < height; y++)
+    {
+        row_a = buf_a + y * stride;
+        row_b = buf_b + y * stride;
+        row = buf_diff + y * stride;
+        for (x = 0; x < width; x++)
+        {
+            /* check if the pixels are the same */
+            if ((row_a[x] & mask) != (row_b[x] & mask)) {
+                int channel;
+                guint32 diff_pixel = 0;
+
+                /* calculate a difference value for all 4 channels */
+                for (channel = 0; channel < 4; channel++) {
+                    int value_a = (row_a[x] >> (channel*8)) & 0xff;
+                    int value_b = (row_b[x] >> (channel*8)) & 0xff;
+                    unsigned int diff;
+                    diff = abs (value_a - value_b);
+                    if (diff > result.max_diff)
+                        result.max_diff = diff;
+                    diff *= 4;  /* emphasize */
+                    if (diff)
+                        diff += 128; /* make sure it's visible */
+                    if (diff > 255)
+                        diff = 255;
+                    diff_pixel |= diff << (channel*8);
+                }
+
+                result.pixels_changed++;
+                if ((diff_pixel & 0x00ffffff) == 0) {
+                    /* alpha only difference, convert to luminance */
+                    guint8 alpha = diff_pixel >> 24;
+                    diff_pixel = alpha * 0x010101;
+                }
+                row[x] = diff_pixel;
+            } else {
+                row[x] = 0;
+            }
+            row[x] |= 0xff000000; /* Set ALPHA to 100% (opaque) */
+        }
+    }
+
+    *result_ret = result;
+}
+
+void
+test_utils_compare_surfaces (cairo_surface_t           *surface_a,
+                             cairo_surface_t           *surface_b,
+                             cairo_surface_t           *surface_diff,
+                             TestUtilsBufferDiffResult *result)
+{
+    /* Here, we run cairo's old buffer_diff algorithm which looks for
+     * pixel-perfect images.
+     */
+    buffer_diff_core (cairo_image_surface_get_data (surface_a),
+                      cairo_image_surface_get_data (surface_b),
+                      cairo_image_surface_get_data (surface_diff),
+                      cairo_image_surface_get_width (surface_a),
+                      cairo_image_surface_get_height (surface_a),
+                      cairo_image_surface_get_stride (surface_a),
+                      0xffffffff,
+                      result);
+    if (result->pixels_changed == 0)
+        return;
+
+    g_test_message ("%d pixels differ (with maximum difference of %d) from reference image\n",
+                    result->pixels_changed, result->max_diff);
+}
+
 static gchar *data_path = NULL;
+
 const gchar *
 test_utils_get_test_data_path (void)
 {
diff --git a/tests/test-utils.h b/tests/test-utils.h
index 8d7fce03..68819956 100644
--- a/tests/test-utils.h
+++ b/tests/test-utils.h
@@ -4,9 +4,20 @@
 #ifndef TEST_UTILS_H
 #define TEST_UTILS_H
 
+#include <cairo.h>
 #include <gio/gio.h>
 
-G_BEGIN_DECLS 
+G_BEGIN_DECLS
+
+typedef struct {
+    unsigned int pixels_changed;
+    unsigned int max_diff;
+} TestUtilsBufferDiffResult;
+
+void test_utils_compare_surfaces (cairo_surface_t           *surface_a,
+                                  cairo_surface_t           *surface_b,
+                                  cairo_surface_t           *surface_diff,
+                                  TestUtilsBufferDiffResult *result);
 
 typedef gboolean (* AddTestFunc) (GFile *file);
 


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