| 1 |
/* |
| 2 |
* Copyright (C) 2011 Red Hat Inc. |
| 3 |
* |
| 4 |
* Author: |
| 5 |
* Benjamin Otte <[email protected]> |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Library General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Library General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Library General Public |
| 18 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "reftest-compare.h" |
| 24 |
|
| 25 |
static void |
| 26 |
get_surface_size (cairo_surface_t *surface, |
| 27 |
int *width, |
| 28 |
int *height) |
| 29 |
{ |
| 30 |
GdkRectangle area; |
| 31 |
cairo_t *cr; |
| 32 |
|
| 33 |
cr = cairo_create (surface); |
| 34 |
if (!gdk_cairo_get_clip_rectangle (cr, &area)) |
| 35 |
{ |
| 36 |
g_assert_not_reached (); |
| 37 |
} |
| 38 |
|
| 39 |
g_assert (area.x == 0 && area.y == 0); |
| 40 |
g_assert (area.width > 0 && area.height > 0); |
| 41 |
|
| 42 |
*width = area.width; |
| 43 |
*height = area.height; |
| 44 |
} |
| 45 |
|
| 46 |
static cairo_surface_t * |
| 47 |
coerce_surface_for_comparison (cairo_surface_t *surface, |
| 48 |
int width, |
| 49 |
int height) |
| 50 |
{ |
| 51 |
cairo_surface_t *coerced; |
| 52 |
cairo_t *cr; |
| 53 |
|
| 54 |
coerced = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, |
| 55 |
width, |
| 56 |
height); |
| 57 |
cr = cairo_create (coerced); |
| 58 |
|
| 59 |
cairo_set_source_surface (cr, surface, 0, 0); |
| 60 |
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE); |
| 61 |
cairo_paint (cr); |
| 62 |
|
| 63 |
cairo_destroy (cr); |
| 64 |
cairo_surface_destroy (surface); |
| 65 |
|
| 66 |
g_assert (cairo_surface_status (coerced) == CAIRO_STATUS_SUCCESS); |
| 67 |
|
| 68 |
return coerced; |
| 69 |
} |
| 70 |
|
| 71 |
/* Compares two CAIRO_FORMAT_ARGB32 buffers, returning NULL if the |
| 72 |
* buffers are equal or a surface containing a diff between the two |
| 73 |
* surfaces. |
| 74 |
* |
| 75 |
* This function should be rewritten to compare all formats supported by |
| 76 |
* cairo_format_t instead of taking a mask as a parameter. |
| 77 |
* |
| 78 |
* This function is originally from cairo:test/buffer-diff.c. |
| 79 |
* Copyright © 2004 Richard D. Worth |
| 80 |
*/ |
| 81 |
static cairo_surface_t * |
| 82 |
buffer_diff_core (const guchar *buf_a, |
| 83 |
int stride_a, |
| 84 |
const guchar *buf_b, |
| 85 |
int stride_b, |
| 86 |
int width, |
| 87 |
int height) |
| 88 |
{ |
| 89 |
int x, y; |
| 90 |
guchar *buf_diff = NULL; |
| 91 |
int stride_diff = 0; |
| 92 |
cairo_surface_t *diff = NULL; |
| 93 |
|
| 94 |
for (y = 0; y < height; y++) |
| 95 |
{ |
| 96 |
const guint32 *row_a = (const guint32 *) (buf_a + y * stride_a); |
| 97 |
const guint32 *row_b = (const guint32 *) (buf_b + y * stride_b); |
| 98 |
guint32 *row = (guint32 *) (buf_diff + y * stride_diff); |
| 99 |
|
| 100 |
for (x = 0; x < width; x++) |
| 101 |
{ |
| 102 |
int channel; |
| 103 |
guint32 diff_pixel = 0; |
| 104 |
|
| 105 |
/* check if the pixels are the same */ |
| 106 |
if (row_a[x] == row_b[x]) |
| 107 |
continue; |
| 108 |
|
| 109 |
if (diff == NULL) |
| 110 |
{ |
| 111 |
diff = cairo_image_surface_create (CAIRO_FORMAT_RGB24, |
| 112 |
width, |
| 113 |
height); |
| 114 |
g_assert (cairo_surface_status (diff) == CAIRO_STATUS_SUCCESS); |
| 115 |
buf_diff = cairo_image_surface_get_data (diff); |
| 116 |
stride_diff = cairo_image_surface_get_stride (diff); |
| 117 |
row = (guint32 *) (buf_diff + y * stride_diff); |
| 118 |
} |
| 119 |
|
| 120 |
/* calculate a difference value for all 4 channels */ |
| 121 |
for (channel = 0; channel < 4; channel++) |
| 122 |
{ |
| 123 |
int value_a = (row_a[x] >> (channel*8)) & 0xff; |
| 124 |
int value_b = (row_b[x] >> (channel*8)) & 0xff; |
| 125 |
guint diff; |
| 126 |
|
| 127 |
diff = ABS (value_a - value_b); |
| 128 |
diff *= 4; /* emphasize */ |
| 129 |
if (diff) |
| 130 |
diff += 128; /* make sure it's visible */ |
| 131 |
if (diff > 255) |
| 132 |
diff = 255; |
| 133 |
diff_pixel |= diff << (channel*8); |
| 134 |
} |
| 135 |
|
| 136 |
if ((diff_pixel & 0x00ffffff) == 0) |
| 137 |
{ |
| 138 |
/* alpha only difference, convert to luminance */ |
| 139 |
guint8 alpha = diff_pixel >> 24; |
| 140 |
diff_pixel = alpha * 0x010101; |
| 141 |
} |
| 142 |
|
| 143 |
row[x] = diff_pixel; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return diff; |
| 148 |
} |
| 149 |
|
| 150 |
cairo_surface_t * |
| 151 |
reftest_compare_surfaces (cairo_surface_t *surface1, |
| 152 |
cairo_surface_t *surface2) |
| 153 |
{ |
| 154 |
int w1, h1, w2, h2, w, h; |
| 155 |
cairo_surface_t *diff; |
| 156 |
|
| 157 |
get_surface_size (surface1, &w1, &h1); |
| 158 |
get_surface_size (surface2, &w2, &h2); |
| 159 |
w = MAX (w1, w2); |
| 160 |
h = MAX (h1, h2); |
| 161 |
surface1 = coerce_surface_for_comparison (surface1, w, h); |
| 162 |
surface2 = coerce_surface_for_comparison (surface2, w, h); |
| 163 |
|
| 164 |
diff = buffer_diff_core (cairo_image_surface_get_data (surface1), |
| 165 |
cairo_image_surface_get_stride (surface1), |
| 166 |
cairo_image_surface_get_data (surface2), |
| 167 |
cairo_image_surface_get_stride (surface2), |
| 168 |
w, h); |
| 169 |
|
| 170 |
return diff; |
| 171 |
} |
| 172 |
|