1 /* imagediff - Compare two images
3 * Copyright © 2004 Richard D. Worth
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without
7 * fee, provided that the above copyright notice appear in all copies
8 * and that both that copyright notice and this permission notice
9 * appear in supporting documentation, and that the name of Richard Worth
10 * not be used in advertising or publicity pertaining to distribution
11 * of the software without specific, written prior permission.
12 * Richard Worth makes no representations about the suitability of this
13 * software for any purpose. It is provided "as is" without express
14 * or implied warranty.
16 * RICHARD WORTH DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
18 * NO EVENT SHALL RICHARD WORTH BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
20 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
21 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * Author: Richard D. Worth <richard@theworths.org> */
39 #include "cairo-test.h"
42 #include "buffer-diff.h"
44 /* Don't allow any differences greater than this value, even if pdiff
45 * claims that the images are identical */
46 #define PERCEPTUAL_DIFF_THRESHOLD 256
48 /* Compare two buffers, returning the number of pixels that are
49 * different and the maximum difference of any single color channel in
52 * This function should be rewritten to compare all formats supported by
53 * cairo_format_t instead of taking a mask as a parameter.
56 buffer_diff_core (const unsigned char *_buf_a, int stride_a,
57 const unsigned char *_buf_b, int stride_b,
58 unsigned char *_buf_diff, int stride_diff,
62 buffer_diff_result_t *result_ret)
64 const uint32_t *buf_a = (const uint32_t*) _buf_a;
65 const uint32_t *buf_b = (const uint32_t*) _buf_b;
66 uint32_t *buf_diff = (uint32_t*) _buf_diff;
68 buffer_diff_result_t result = {0, 0};
70 stride_a /= sizeof (uint32_t);
71 stride_b /= sizeof (uint32_t);
72 stride_diff /= sizeof (uint32_t);
73 for (y = 0; y < height; y++) {
74 const uint32_t *row_a = buf_a + y * stride_a;
75 const uint32_t *row_b = buf_b + y * stride_b;
76 uint32_t *row = buf_diff + y * stride_diff;
78 for (x = 0; x < width; x++) {
79 /* check if the pixels are the same */
80 if ((row_a[x] & mask) != (row_b[x] & mask)) {
82 uint32_t diff_pixel = 0;
84 /* calculate a difference value for all 4 channels */
85 for (channel = 0; channel < 4; channel++) {
86 int value_a = (row_a[x] >> (channel*8)) & 0xff;
87 int value_b = (row_b[x] >> (channel*8)) & 0xff;
89 diff = abs (value_a - value_b);
90 if (diff > result.max_diff)
91 result.max_diff = diff;
92 diff *= 4; /* emphasize */
94 diff += 128; /* make sure it's visible */
97 diff_pixel |= diff << (channel*8);
100 result.pixels_changed++;
101 if ((diff_pixel & 0x00ffffff) == 0) {
102 /* alpha only difference, convert to luminance */
103 uint8_t alpha = diff_pixel >> 24;
104 diff_pixel = alpha * 0x010101;
110 row[x] |= 0xff000000; /* Set ALPHA to 100% (opaque) */
114 *result_ret = result;
117 /* Compares two image surfaces
119 * Provides number of pixels changed and maximum single-channel
120 * difference in result.
122 * Also fills in a "diff" surface intended to visually show where the
126 compare_surfaces (const cairo_test_context_t *ctx,
127 cairo_surface_t *surface_a,
128 cairo_surface_t *surface_b,
129 cairo_surface_t *surface_diff,
130 buffer_diff_result_t *result)
132 /* These default values were taken straight from the
133 * perceptualdiff program. We'll probably want to tune these as
136 double luminance = 100.0;
137 double field_of_view = 45.0;
138 float pixels_changed_percentage = 0.05;
139 int discernible_pixels_changed;
140 int pixels_tolerance;
143 /* First, we run cairo's old buffer_diff algorithm which looks for
144 * pixel-perfect images, (we do this first since the test suite
145 * runs about 3x slower if we run pdiff_compare first).
147 buffer_diff_core (cairo_image_surface_get_data (surface_a),
148 cairo_image_surface_get_stride (surface_a),
149 cairo_image_surface_get_data (surface_b),
150 cairo_image_surface_get_stride (surface_b),
151 cairo_image_surface_get_data (surface_diff),
152 cairo_image_surface_get_stride (surface_diff),
153 cairo_image_surface_get_width (surface_a),
154 cairo_image_surface_get_height (surface_a),
155 cairo_surface_get_content (surface_a) & CAIRO_CONTENT_ALPHA ? 0xffffffff : 0x00ffffff,
157 if (result->pixels_changed == 0)
161 "%d pixels differ (with maximum difference of %d) from reference image\n",
162 result->pixels_changed, result->max_diff);
164 /* Then, if there are any different pixels, we give the pdiff code
165 * a crack at the images. If it decides that there are no visually
166 * discernible differences in any pixels, then we accept this
167 * result as good enough.
169 * Only let pdiff have a crack at the comparison if the max difference
170 * is lower than a threshold, otherwise some problems could be masked.
172 if (result->max_diff < PERCEPTUAL_DIFF_THRESHOLD) {
173 width = cairo_image_surface_get_width (surface_a);
174 height = cairo_image_surface_get_height (surface_a);
175 pixels_tolerance = width * height * pixels_changed_percentage;
176 discernible_pixels_changed = pdiff_compare (surface_a, surface_b,
177 gamma, luminance, field_of_view);
178 if (discernible_pixels_changed <= pixels_tolerance) {
179 result->pixels_changed = 0;
181 "But perceptual diff finds no visually discernible difference.\n"
182 "Accepting result.\n");
188 buffer_diff_noalpha (const unsigned char *buf_a,
189 const unsigned char *buf_b,
190 unsigned char *buf_diff,
194 buffer_diff_result_t *result)
196 buffer_diff_core(buf_a, stride,
205 same_size (cairo_surface_t *a, cairo_surface_t *b)
207 unsigned int width_a, height_a;
208 unsigned int width_b, height_b;
210 width_a = cairo_image_surface_get_width (a);
211 height_a = cairo_image_surface_get_height (a);
213 width_b = cairo_image_surface_get_width (b);
214 height_b = cairo_image_surface_get_height (b);
216 return width_a == width_b && height_a == height_b;
219 /* Image comparison code courtesy of Richard Worth <richard@theworths.org>
220 * Returns number of pixels changed, (or -1 on error).
221 * Also saves a "diff" image intended to visually show where the
224 * The return value simply indicates whether a check was successfully
225 * made, (as opposed to a file-not-found condition or similar). It
226 * does not indicate anything about how much the images differ. For
229 * One failure mode is if the two images provided do not have the same
230 * dimensions. In this case, this function will return
231 * CAIRO_STATUS_SURFACE_TYPE_MISMATCH (which is a bit of an abuse, but
235 image_diff (const cairo_test_context_t *ctx,
236 cairo_surface_t *surface_a,
237 cairo_surface_t *surface_b,
238 cairo_surface_t *surface_diff,
239 buffer_diff_result_t *result)
241 if (cairo_surface_status (surface_a))
242 return cairo_surface_status (surface_a);
244 if (cairo_surface_status (surface_b))
245 return cairo_surface_status (surface_b);
247 if (cairo_surface_status (surface_diff))
248 return cairo_surface_status (surface_diff);
250 if (! same_size (surface_a, surface_b) ||
251 ! same_size (surface_a, surface_diff))
253 cairo_test_log (ctx, "Error: Image size mismatch\n");
254 return CAIRO_STATUS_SURFACE_TYPE_MISMATCH;
257 compare_surfaces (ctx, surface_a, surface_b, surface_diff, result);
259 return CAIRO_STATUS_SUCCESS;
263 image_diff_is_failure (const buffer_diff_result_t *result,
264 unsigned int tolerance)
266 return result->pixels_changed &&
267 result->max_diff > tolerance;