reftests: Make image-compare use textures

All image comparisons are done on textures now.
This commit is contained in:
Benjamin Otte 2021-09-15 22:54:14 +02:00
parent 47330598fb
commit 58f66ebd07
3 changed files with 41 additions and 116 deletions

View File

@ -10,9 +10,9 @@ static gboolean opt_quiet;
int
main (int argc, char **argv)
{
cairo_surface_t *image1;
cairo_surface_t *image2;
cairo_surface_t *diff;
GdkTexture *image1;
GdkTexture *image2;
GdkTexture *diff;
GOptionEntry entries[] = {
{"output", 'o', 0, G_OPTION_ARG_FILENAME, &opt_filename, "Output location", "FILE" },
{"quiet", 'q', 0, G_OPTION_ARG_NONE, &opt_quiet, "Don't talk", NULL },
@ -38,13 +38,29 @@ main (int argc, char **argv)
exit (1);
}
image1 = cairo_image_surface_create_from_png (argv[1]);
image2 = cairo_image_surface_create_from_png (argv[2]);
image1 = gdk_texture_new_from_filename (argv[1], &error);
if (image1 == NULL)
{
g_printerr ("Error loading %s: %s\n", argv[1], error->message);
exit (1);
}
image2 = gdk_texture_new_from_filename (argv[2], &error);
if (image2 == NULL)
{
g_printerr ("Error loading %s: %s\n", argv[2], error->message);
exit (1);
}
diff = reftest_compare_surfaces (image1, image2);
diff = reftest_compare_textures (image1, image2);
if (opt_filename && diff)
cairo_surface_write_to_png (diff, opt_filename);
{
if (!gdk_texture_save_to_png (diff, opt_filename))
{
g_printerr ("Could not save diff image to %s\n", opt_filename);
exit (1);
}
}
if (!opt_quiet)
{
@ -59,7 +75,5 @@ main (int argc, char **argv)
g_print ("No differences.\n");
}
if (!opt_quiet)
return diff != NULL ? 1 : 0;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2011 Red Hat Inc.
* Copyright (C) 2011,2021 Red Hat Inc.
*
* Author:
* Benjamin Otte <otte@gnome.org>
@ -22,62 +22,14 @@
#include "reftest-compare.h"
static void
get_surface_size (cairo_surface_t *surface,
int *width,
int *height)
{
cairo_t *cr;
double x1, x2, y1, y2;
cr = cairo_create (surface);
cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
cairo_destroy (cr);
g_assert_true (x1 == 0 && y1 == 0);
g_assert_true (x2 > 0 && y2 > 0);
g_assert_true ((int) x2 == x2 && (int) y2 == y2);
*width = x2;
*height = y2;
}
static cairo_surface_t *
coerce_surface_for_comparison (cairo_surface_t *surface,
int width,
int height)
{
cairo_surface_t *coerced;
cairo_t *cr;
coerced = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
width,
height);
cr = cairo_create (coerced);
cairo_set_source_surface (cr, surface, 0, 0);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);
cairo_destroy (cr);
g_assert_true (cairo_surface_status (coerced) == CAIRO_STATUS_SUCCESS);
return coerced;
}
/* Compares two CAIRO_FORMAT_ARGB32 buffers, returning NULL if the
/* Compares two GDK_MEMORY_DEFAULT buffers, returning NULL if the
* buffers are equal or a surface containing a diff between the two
* surfaces.
*
* This function should be rewritten to compare all formats supported by
* cairo_format_t instead of taking a mask as a parameter.
*
* This function is originally from cairo:test/buffer-diff.c.
* Copyright © 2004 Richard D. Worth
*/
static cairo_surface_t *
static GdkTexture *
buffer_diff_core (const guchar *buf_a,
int stride_a,
const guchar *buf_b,
@ -88,7 +40,7 @@ buffer_diff_core (const guchar *buf_a,
int x, y;
guchar *buf_diff = NULL;
int stride_diff = 0;
cairo_surface_t *diff = NULL;
GdkTexture *diff = NULL;
for (y = 0; y < height; y++)
{
@ -112,12 +64,15 @@ buffer_diff_core (const guchar *buf_a,
if (diff == NULL)
{
diff = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
width,
height);
g_assert_true (cairo_surface_status (diff) == CAIRO_STATUS_SUCCESS);
buf_diff = cairo_image_surface_get_data (diff);
stride_diff = cairo_image_surface_get_stride (diff);
GBytes *bytes;
stride_diff = 4 * width;
buf_diff = g_malloc0_n (stride_diff, height);
bytes = g_bytes_new_take (buf_diff, stride_diff * height);
diff = gdk_memory_texture_new (width, height,
GDK_MEMORY_DEFAULT,
bytes,
stride_diff);
row = (guint32 *) (buf_diff + y * stride_diff);
}
@ -153,40 +108,12 @@ buffer_diff_core (const guchar *buf_a,
return diff;
}
cairo_surface_t *
reftest_compare_surfaces (cairo_surface_t *surface1,
cairo_surface_t *surface2)
{
int w1, h1, w2, h2, w, h;
cairo_surface_t *coerced1, *coerced2, *diff;
get_surface_size (surface1, &w1, &h1);
get_surface_size (surface2, &w2, &h2);
w = MAX (w1, w2);
h = MAX (h1, h2);
coerced1 = coerce_surface_for_comparison (surface1, w, h);
coerced2 = coerce_surface_for_comparison (surface2, w, h);
diff = buffer_diff_core (cairo_image_surface_get_data (coerced1),
cairo_image_surface_get_stride (coerced1),
cairo_image_surface_get_data (coerced2),
cairo_image_surface_get_stride (coerced2),
w, h);
cairo_surface_destroy (coerced1);
cairo_surface_destroy (coerced2);
return diff;
}
GdkTexture *
reftest_compare_textures (GdkTexture *texture1,
GdkTexture *texture2)
{
int w, h;
guchar *data1, *data2;
GBytes *bytes;
cairo_surface_t *surface;
GdkTexture *diff;
w = MAX (gdk_texture_get_width (texture1), gdk_texture_get_width (texture2));
@ -197,25 +124,12 @@ reftest_compare_textures (GdkTexture *texture1,
data2 = g_malloc_n (w * 4, h);
gdk_texture_download (texture2, data2, w * 4);
surface = buffer_diff_core (data1, w * 4,
data2, w * 4,
w, h);
if (surface == NULL)
return NULL;
diff = buffer_diff_core (data1, w * 4,
data2, w * 4,
w, h);
bytes = g_bytes_new_with_free_func (cairo_image_surface_get_data (surface),
cairo_image_surface_get_height (surface)
* cairo_image_surface_get_stride (surface),
(GDestroyNotify) cairo_surface_destroy,
cairo_surface_reference (surface));
diff = gdk_memory_texture_new (cairo_image_surface_get_width (surface),
cairo_image_surface_get_height (surface),
GDK_MEMORY_DEFAULT,
bytes,
cairo_image_surface_get_stride (surface));
g_bytes_unref (bytes);
g_free (data1);
g_free (data2);
return diff;
}

View File

@ -22,9 +22,6 @@
G_BEGIN_DECLS
G_MODULE_EXPORT
cairo_surface_t * reftest_compare_surfaces (cairo_surface_t *surface1,
cairo_surface_t *surface2);
G_MODULE_EXPORT
GdkTexture * reftest_compare_textures (GdkTexture *texture1,
GdkTexture *texture2);