f3d90ca0dfed064d6cda0ec10d24581cc1b2cad5
[platform/core/graphics/cairo.git] / test / buffer-diff.c
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 256
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
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
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     float pixels_changed_percentage = 0.05;
139     int discernible_pixels_changed;
140     int pixels_tolerance;
141     int width, height;
142
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).
146      */
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,
156                       result);
157     if (result->pixels_changed == 0)
158         return;
159
160     cairo_test_log (ctx,
161                     "%d pixels differ (with maximum difference of %d) from reference image\n",
162                     result->pixels_changed, result->max_diff);
163
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.
168      *
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.
171      */
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;
180             cairo_test_log (ctx,
181                             "But perceptual diff finds no visually discernible difference.\n"
182                             "Accepting result.\n");
183         }
184     }
185 }
186
187 void
188 buffer_diff_noalpha (const unsigned char *buf_a,
189                      const unsigned char *buf_b,
190                      unsigned char *buf_diff,
191                      int           width,
192                      int           height,
193                      int           stride,
194                      buffer_diff_result_t *result)
195 {
196     buffer_diff_core(buf_a, stride,
197                      buf_b, stride,
198                      buf_diff, stride,
199                      width, height,
200                      0x00ffffff,
201                      result);
202 }
203
204 static cairo_bool_t
205 same_size (cairo_surface_t *a, cairo_surface_t *b)
206 {
207     unsigned int width_a, height_a;
208     unsigned int width_b, height_b;
209
210     width_a = cairo_image_surface_get_width (a);
211     height_a = cairo_image_surface_get_height (a);
212
213     width_b = cairo_image_surface_get_width (b);
214     height_b = cairo_image_surface_get_height (b);
215
216     return width_a == width_b && height_a == height_b;
217 }
218
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
222  * images differ.
223  *
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
227  * that, see result.
228  *
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
232  * oh well).
233  */
234 cairo_status_t
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)
240 {
241     if (cairo_surface_status (surface_a))
242         return cairo_surface_status (surface_a);
243
244     if (cairo_surface_status (surface_b))
245         return cairo_surface_status (surface_b);
246
247     if (cairo_surface_status (surface_diff))
248         return cairo_surface_status (surface_diff);
249
250     if (! same_size (surface_a, surface_b) ||
251         ! same_size (surface_a, surface_diff))
252     {
253         cairo_test_log (ctx, "Error: Image size mismatch\n");
254         return CAIRO_STATUS_SURFACE_TYPE_MISMATCH;
255     }
256
257     compare_surfaces (ctx, surface_a, surface_b, surface_diff, result);
258
259     return CAIRO_STATUS_SUCCESS;
260 }
261
262 cairo_bool_t
263 image_diff_is_failure (const buffer_diff_result_t *result,
264                        unsigned int                tolerance)
265 {
266   return result->pixels_changed &&
267          result->max_diff > tolerance;
268 }