1 /* imagediff - Compare two images
2 *
3 * Copyright © 2004 Richard D. Worth
4 *
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.
15 *
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.
23 *
24 * Author: Richard D. Worth <richard@theworths.org> */
25
26 #if HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #include <errno.h>
36 #include <string.h>
37 #include <pixman.h>
38
39 #include "cairo-test.h"
40
41 #include "pdiff.h"
42 #include "buffer-diff.h"
43
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 25
47
48 /* Compare two buffers, returning the number of pixels that are
49 * different and the maximum difference of any single color channel in
50 * result_ret.
51 *
52 * This function should be rewritten to compare all formats supported by
53 * cairo_format_t instead of taking a mask as a parameter.
54 */
55 static void
buffer_diff_core(const unsigned char * _buf_a,int stride_a,const unsigned char * _buf_b,int stride_b,unsigned char * _buf_diff,int stride_diff,int width,int height,uint32_t mask,buffer_diff_result_t * result_ret)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,
59 int width,
60 int height,
61 uint32_t mask,
62 buffer_diff_result_t *result_ret)
63 {
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;
67 int x, y;
68 buffer_diff_result_t result = {0, 0};
69
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;
77
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)) {
81 int channel;
82 uint32_t diff_pixel = 0;
83
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;
88 unsigned int diff;
89 diff = abs (value_a - value_b);
90 if (diff > result.max_diff)
91 result.max_diff = diff;
92 diff *= 4; /* emphasize */
93 if (diff)
94 diff += 128; /* make sure it's visible */
95 if (diff > 255)
96 diff = 255;
97 diff_pixel |= diff << (channel*8);
98 }
99
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;
105 }
106 row[x] = diff_pixel;
107 } else {
108 row[x] = 0;
109 }
110 row[x] |= 0xff000000; /* Set ALPHA to 100% (opaque) */
111 }
112 }
113
114 *result_ret = result;
115 }
116
117 /* Compares two image surfaces
118 *
119 * Provides number of pixels changed and maximum single-channel
120 * difference in result.
121 *
122 * Also fills in a "diff" surface intended to visually show where the
123 * images differ.
124 */
125 static void
compare_surfaces(const cairo_test_context_t * ctx,cairo_surface_t * surface_a,cairo_surface_t * surface_b,cairo_surface_t * surface_diff,buffer_diff_result_t * result)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)
131 {
132 /* These default values were taken straight from the
133 * perceptualdiff program. We'll probably want to tune these as
134 * necessary. */
135 double gamma = 2.2;
136 double luminance = 100.0;
137 double field_of_view = 45.0;
138 int discernible_pixels_changed;
139
140 /* First, we run cairo's old buffer_diff algorithm which looks for
141 * pixel-perfect images, (we do this first since the test suite
142 * runs about 3x slower if we run pdiff_compare first).
143 */
144 buffer_diff_core (cairo_image_surface_get_data (surface_a),
145 cairo_image_surface_get_stride (surface_a),
146 cairo_image_surface_get_data (surface_b),
147 cairo_image_surface_get_stride (surface_b),
148 cairo_image_surface_get_data (surface_diff),
149 cairo_image_surface_get_stride (surface_diff),
150 cairo_image_surface_get_width (surface_a),
151 cairo_image_surface_get_height (surface_a),
152 cairo_surface_get_content (surface_a) & CAIRO_CONTENT_ALPHA ? 0xffffffff : 0x00ffffff,
153 result);
154 if (result->pixels_changed == 0)
155 return;
156
157 cairo_test_log (ctx,
158 "%d pixels differ (with maximum difference of %d) from reference image\n",
159 result->pixels_changed, result->max_diff);
160
161 /* Then, if there are any different pixels, we give the pdiff code
162 * a crack at the images. If it decides that there are no visually
163 * discernible differences in any pixels, then we accept this
164 * result as good enough.
165 *
166 * Only let pdiff have a crack at the comparison if the max difference
167 * is lower than a threshold, otherwise some problems could be masked.
168 */
169 if (result->max_diff < PERCEPTUAL_DIFF_THRESHOLD) {
170 discernible_pixels_changed = pdiff_compare (surface_a, surface_b,
171 gamma, luminance, field_of_view);
172 if (discernible_pixels_changed == 0) {
173 result->pixels_changed = 0;
174 cairo_test_log (ctx,
175 "But perceptual diff finds no visually discernible difference.\n"
176 "Accepting result.\n");
177 }
178 }
179 }
180
181 void
buffer_diff_noalpha(const unsigned char * buf_a,const unsigned char * buf_b,unsigned char * buf_diff,int width,int height,int stride,buffer_diff_result_t * result)182 buffer_diff_noalpha (const unsigned char *buf_a,
183 const unsigned char *buf_b,
184 unsigned char *buf_diff,
185 int width,
186 int height,
187 int stride,
188 buffer_diff_result_t *result)
189 {
190 buffer_diff_core(buf_a, stride,
191 buf_b, stride,
192 buf_diff, stride,
193 width, height,
194 0x00ffffff,
195 result);
196 }
197
198 static cairo_bool_t
same_size(cairo_surface_t * a,cairo_surface_t * b)199 same_size (cairo_surface_t *a, cairo_surface_t *b)
200 {
201 unsigned int width_a, height_a;
202 unsigned int width_b, height_b;
203
204 width_a = cairo_image_surface_get_width (a);
205 height_a = cairo_image_surface_get_height (a);
206
207 width_b = cairo_image_surface_get_width (b);
208 height_b = cairo_image_surface_get_height (b);
209
210 return width_a == width_b && height_a == height_b;
211 }
212
213 /* Image comparison code courtesy of Richard Worth <richard@theworths.org>
214 * Returns number of pixels changed, (or -1 on error).
215 * Also saves a "diff" image intended to visually show where the
216 * images differ.
217 *
218 * The return value simply indicates whether a check was successfully
219 * made, (as opposed to a file-not-found condition or similar). It
220 * does not indicate anything about how much the images differ. For
221 * that, see result.
222 *
223 * One failure mode is if the two images provided do not have the same
224 * dimensions. In this case, this function will return
225 * CAIRO_STATUS_SURFACE_TYPE_MISMATCH (which is a bit of an abuse, but
226 * oh well).
227 */
228 cairo_status_t
image_diff(const cairo_test_context_t * ctx,cairo_surface_t * surface_a,cairo_surface_t * surface_b,cairo_surface_t * surface_diff,buffer_diff_result_t * result)229 image_diff (const cairo_test_context_t *ctx,
230 cairo_surface_t *surface_a,
231 cairo_surface_t *surface_b,
232 cairo_surface_t *surface_diff,
233 buffer_diff_result_t *result)
234 {
235 if (cairo_surface_status (surface_a))
236 return cairo_surface_status (surface_a);
237
238 if (cairo_surface_status (surface_b))
239 return cairo_surface_status (surface_b);
240
241 if (cairo_surface_status (surface_diff))
242 return cairo_surface_status (surface_diff);
243
244 if (! same_size (surface_a, surface_b) ||
245 ! same_size (surface_a, surface_diff))
246 {
247 cairo_test_log (ctx, "Error: Image size mismatch\n");
248 return CAIRO_STATUS_SURFACE_TYPE_MISMATCH;
249 }
250
251 compare_surfaces (ctx, surface_a, surface_b, surface_diff, result);
252
253 return CAIRO_STATUS_SUCCESS;
254 }
255
256 cairo_bool_t
image_diff_is_failure(const buffer_diff_result_t * result,unsigned int tolerance)257 image_diff_is_failure (const buffer_diff_result_t *result,
258 unsigned int tolerance)
259 {
260 return result->pixels_changed &&
261 result->max_diff > tolerance;
262 }
263