| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* vim: set sw=4 sts=4 ts=4 expandtab: */ |
| 3 |
|
| 4 |
#include "test-utils.h" |
| 5 |
|
| 6 |
#include <string.h> |
| 7 |
#include <pango/pango.h> |
| 8 |
#include <pango/pangocairo.h> |
| 9 |
|
| 10 |
#if !PANGO_VERSION_CHECK (1, 44, 0) |
| 11 |
# include <hb.h> |
| 12 |
#endif |
| 13 |
|
| 14 |
#include <ft2build.h> |
| 15 |
#include FT_FREETYPE_H |
| 16 |
|
| 17 |
|
| 18 |
/* Compare two buffers, returning the number of pixels that are |
| 19 |
* different and the maximum difference of any single color channel in |
| 20 |
* result_ret. |
| 21 |
* |
| 22 |
* This function should be rewritten to compare all formats supported by |
| 23 |
* cairo_format_t instead of taking a mask as a parameter. |
| 24 |
*/ |
| 25 |
static void |
| 26 |
buffer_diff_core (unsigned char *_buf_a, |
| 27 |
unsigned char *_buf_b, |
| 28 |
unsigned char *_buf_diff, |
| 29 |
int width, |
| 30 |
int height, |
| 31 |
int stride, |
| 32 |
guint32 mask, |
| 33 |
TestUtilsBufferDiffResult *result_ret) |
| 34 |
{ |
| 35 |
int x, y; |
| 36 |
guint32 *row_a, *row_b, *row; |
| 37 |
TestUtilsBufferDiffResult result = {0, 0}; |
| 38 |
guint32 *buf_a = (guint32 *) _buf_a; |
| 39 |
guint32 *buf_b = (guint32 *) _buf_b; |
| 40 |
guint32 *buf_diff = (guint32 *) _buf_diff; |
| 41 |
|
| 42 |
stride /= sizeof(guint32); |
| 43 |
for (y = 0; y < height; y++) |
| 44 |
{ |
| 45 |
row_a = buf_a + y * stride; |
| 46 |
row_b = buf_b + y * stride; |
| 47 |
row = buf_diff + y * stride; |
| 48 |
for (x = 0; x < width; x++) |
| 49 |
{ |
| 50 |
/* check if the pixels are the same */ |
| 51 |
if ((row_a[x] & mask) != (row_b[x] & mask)) { |
| 52 |
int channel; |
| 53 |
guint32 diff_pixel = 0; |
| 54 |
|
| 55 |
/* calculate a difference value for all 4 channels */ |
| 56 |
for (channel = 0; channel < 4; channel++) { |
| 57 |
int value_a = (row_a[x] >> (channel*8)) & 0xff; |
| 58 |
int value_b = (row_b[x] >> (channel*8)) & 0xff; |
| 59 |
unsigned int diff; |
| 60 |
diff = abs (value_a - value_b); |
| 61 |
if (diff > result.max_diff) |
| 62 |
result.max_diff = diff; |
| 63 |
diff *= 4; /* emphasize */ |
| 64 |
if (diff) |
| 65 |
diff += 128; /* make sure it's visible */ |
| 66 |
if (diff > 255) |
| 67 |
diff = 255; |
| 68 |
diff_pixel |= diff << (channel*8); |
| 69 |
} |
| 70 |
|
| 71 |
result.pixels_changed++; |
| 72 |
if ((diff_pixel & 0x00ffffff) == 0) { |
| 73 |
/* alpha only difference, convert to luminance */ |
| 74 |
guint8 alpha = diff_pixel >> 24; |
| 75 |
diff_pixel = alpha * 0x010101; |
| 76 |
} |
| 77 |
row[x] = diff_pixel; |
| 78 |
} else { |
| 79 |
row[x] = 0; |
| 80 |
} |
| 81 |
row[x] |= 0xff000000; /* Set ALPHA to 100% (opaque) */ |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
*result_ret = result; |
| 86 |
} |
| 87 |
|
| 88 |
void |
| 89 |
test_utils_compare_surfaces (cairo_surface_t *surface_a, |
| 90 |
cairo_surface_t *surface_b, |
| 91 |
cairo_surface_t *surface_diff, |
| 92 |
TestUtilsBufferDiffResult *result) |
| 93 |
{ |
| 94 |
/* Here, we run cairo's old buffer_diff algorithm which looks for |
| 95 |
* pixel-perfect images. |
| 96 |
*/ |
| 97 |
buffer_diff_core (cairo_image_surface_get_data (surface_a), |
| 98 |
cairo_image_surface_get_data (surface_b), |
| 99 |
cairo_image_surface_get_data (surface_diff), |
| 100 |
cairo_image_surface_get_width (surface_a), |
| 101 |
cairo_image_surface_get_height (surface_a), |
| 102 |
cairo_image_surface_get_stride (surface_a), |
| 103 |
0xffffffff, |
| 104 |
result); |
| 105 |
if (result->pixels_changed == 0) |
| 106 |
return; |
| 107 |
|
| 108 |
g_test_message ("%d pixels differ (with maximum difference of %d) from reference image\n", |
| 109 |
result->pixels_changed, result->max_diff); |
| 110 |
} |
| 111 |
|
| 112 |
#ifdef HAVE_PIXBUF |
| 113 |
/* Copied from gdk_cairo_surface_paint_pixbuf in gdkcairo.c, |
| 114 |
* we do not want to depend on GDK |
| 115 |
*/ |
| 116 |
static void |
| 117 |
test_utils_cairo_surface_paint_pixbuf (cairo_surface_t *surface, |
| 118 |
const GdkPixbuf *pixbuf) |
| 119 |
{ |
| 120 |
gint width, height; |
| 121 |
guchar *gdk_pixels, *cairo_pixels; |
| 122 |
int gdk_rowstride, cairo_stride; |
| 123 |
int n_channels; |
| 124 |
int j; |
| 125 |
|
| 126 |
if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) |
| 127 |
return; |
| 128 |
|
| 129 |
/* This function can't just copy any pixbuf to any surface, be |
| 130 |
* sure to read the invariants here before calling it */ |
| 131 |
|
| 132 |
g_assert (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE); |
| 133 |
g_assert (cairo_image_surface_get_format (surface) == CAIRO_FORMAT_RGB24 || |
| 134 |
cairo_image_surface_get_format (surface) == CAIRO_FORMAT_ARGB32); |
| 135 |
g_assert (cairo_image_surface_get_width (surface) == gdk_pixbuf_get_width (pixbuf)); |
| 136 |
g_assert (cairo_image_surface_get_height (surface) == gdk_pixbuf_get_height (pixbuf)); |
| 137 |
|
| 138 |
cairo_surface_flush (surface); |
| 139 |
|
| 140 |
width = gdk_pixbuf_get_width (pixbuf); |
| 141 |
height = gdk_pixbuf_get_height (pixbuf); |
| 142 |
gdk_pixels = gdk_pixbuf_get_pixels (pixbuf); |
| 143 |
gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
| 144 |
n_channels = gdk_pixbuf_get_n_channels (pixbuf); |
| 145 |
cairo_stride = cairo_image_surface_get_stride (surface); |
| 146 |
cairo_pixels = cairo_image_surface_get_data (surface); |
| 147 |
|
| 148 |
for (j = height; j; j--) |
| 149 |
{ |
| 150 |
guchar *p = gdk_pixels; |
| 151 |
guchar *q = cairo_pixels; |
| 152 |
|
| 153 |
if (n_channels == 3) |
| 154 |
{ |
| 155 |
guchar *end = p + 3 * width; |
| 156 |
|
| 157 |
while (p < end) |
| 158 |
{ |
| 159 |
#if G_BYTE_ORDER == G_LITTLE_ENDIAN |
| 160 |
q[0] = p[2]; |
| 161 |
q[1] = p[1]; |
| 162 |
q[2] = p[0]; |
| 163 |
#else |
| 164 |
q[1] = p[0]; |
| 165 |
q[2] = p[1]; |
| 166 |
q[3] = p[2]; |
| 167 |
#endif |
| 168 |
p += 3; |
| 169 |
q += 4; |
| 170 |
} |
| 171 |
} |
| 172 |
else |
| 173 |
{ |
| 174 |
guchar *end = p + 4 * width; |
| 175 |
guint t1,t2,t3; |
| 176 |
|
| 177 |
#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x80; d = ((t >> 8) + t) >> 8; } G_STMT_END |
| 178 |
|
| 179 |
while (p < end) |
| 180 |
{ |
| 181 |
#if G_BYTE_ORDER == G_LITTLE_ENDIAN |
| 182 |
MULT(q[0], p[2], p[3], t1); |
| 183 |
MULT(q[1], p[1], p[3], t2); |
| 184 |
MULT(q[2], p[0], p[3], t3); |
| 185 |
q[3] = p[3]; |
| 186 |
#else |
| 187 |
q[0] = p[3]; |
| 188 |
MULT(q[1], p[0], p[3], t1); |
| 189 |
MULT(q[2], p[1], p[3], t2); |
| 190 |
MULT(q[3], p[2], p[3], t3); |
| 191 |
#endif |
| 192 |
|
| 193 |
p += 4; |
| 194 |
q += 4; |
| 195 |
} |
| 196 |
|
| 197 |
#undef MULT |
| 198 |
} |
| 199 |
|
| 200 |
gdk_pixels += gdk_rowstride; |
| 201 |
cairo_pixels += cairo_stride; |
| 202 |
} |
| 203 |
|
| 204 |
cairo_surface_mark_dirty (surface); |
| 205 |
} |
| 206 |
|
| 207 |
cairo_surface_t * |
| 208 |
test_utils_cairo_surface_from_pixbuf (const GdkPixbuf *pixbuf) |
| 209 |
{ |
| 210 |
cairo_surface_t *surface; |
| 211 |
|
| 212 |
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL); |
| 213 |
g_return_val_if_fail (gdk_pixbuf_get_n_channels (pixbuf) == 4, NULL); |
| 214 |
|
| 215 |
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, |
| 216 |
gdk_pixbuf_get_width (pixbuf), |
| 217 |
gdk_pixbuf_get_height (pixbuf)); |
| 218 |
|
| 219 |
test_utils_cairo_surface_paint_pixbuf (surface, pixbuf); |
| 220 |
|
| 221 |
return surface; |
| 222 |
} |
| 223 |
#endif /* defined(HAVE_PIXBUF) */ |
| 224 |
|
| 225 |
static gchar *data_path = NULL; |
| 226 |
|
| 227 |
const gchar * |
| 228 |
test_utils_get_test_data_path (void) |
| 229 |
{ |
| 230 |
if (data_path) |
| 231 |
return data_path; |
| 232 |
|
| 233 |
data_path = g_test_build_filename (G_TEST_DIST, "../../rsvg/tests/fixtures", NULL); |
| 234 |
|
| 235 |
return data_path; |
| 236 |
} |
| 237 |
|
| 238 |
void |
| 239 |
test_utils_print_dependency_versions (void) |
| 240 |
{ |
| 241 |
FT_Library ft_lib; |
| 242 |
FT_Int ft_major = 0; |
| 243 |
FT_Int ft_minor = 0; |
| 244 |
FT_Int ft_patch = 0; |
| 245 |
|
| 246 |
FT_Init_FreeType (&ft_lib); |
| 247 |
FT_Library_Version (ft_lib, &ft_major, &ft_minor, &ft_patch); |
| 248 |
FT_Done_FreeType (ft_lib); |
| 249 |
|
| 250 |
g_test_message ("Cairo version: %s", cairo_version_string ()); |
| 251 |
g_test_message ("Pango version: %s", pango_version_string ()); |
| 252 |
g_test_message ("Freetype version: %d.%d.%d", ft_major, ft_minor, ft_patch); |
| 253 |
#if PANGO_VERSION_CHECK (1, 44, 0) |
| 254 |
g_test_message ("Harfbuzz version: %s", hb_version_string ()); |
| 255 |
#else |
| 256 |
g_test_message ("Not printing Harfbuzz version since Pango is older than 1.44"); |
| 257 |
#endif |
| 258 |
} |