2005-01-25 14:45:31 +00:00
|
|
|
/* imagediff - Compare two images
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2004 Richard D. Worth
|
|
|
|
|
*
|
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software
|
|
|
|
|
* and its documentation for any purpose is hereby granted without
|
|
|
|
|
* fee, provided that the above copyright notice appear in all copies
|
|
|
|
|
* and that both that copyright notice and this permission notice
|
|
|
|
|
* appear in supporting documentation, and that the name of Richard Worth
|
|
|
|
|
* not be used in advertising or publicity pertaining to distribution
|
|
|
|
|
* of the software without specific, written prior permission.
|
|
|
|
|
* Richard Worth makes no representations about the suitability of this
|
|
|
|
|
* software for any purpose. It is provided "as is" without express
|
|
|
|
|
* or implied warranty.
|
2006-06-06 15:35:48 -07:00
|
|
|
*
|
2005-01-25 14:45:31 +00:00
|
|
|
* RICHARD WORTH DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
|
|
|
|
|
* NO EVENT SHALL RICHARD WORTH BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
|
|
|
|
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
|
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
|
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
* Author: Richard D. Worth <richard@theworths.org> */
|
|
|
|
|
|
2005-08-05 10:05:29 +00:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-04-27 13:33:25 +00:00
|
|
|
#include <stdio.h>
|
2006-08-22 22:00:58 -04:00
|
|
|
#include <stdlib.h>
|
2005-08-05 07:48:18 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2005-04-27 13:33:25 +00:00
|
|
|
#include <unistd.h>
|
2005-08-05 07:48:18 +00:00
|
|
|
#endif
|
2005-04-27 13:33:25 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
2007-08-06 19:24:31 -04:00
|
|
|
#include <pixman.h>
|
2005-04-27 13:33:25 +00:00
|
|
|
|
2005-05-10 20:25:38 +00:00
|
|
|
#include "cairo-test.h"
|
|
|
|
|
|
2006-12-14 04:17:07 -08:00
|
|
|
#include "pdiff.h"
|
2005-03-29 00:02:19 +00:00
|
|
|
#include "buffer-diff.h"
|
2005-04-27 13:33:25 +00:00
|
|
|
|
2008-02-25 21:06:35 -05:00
|
|
|
/* Don't allow any differences greater than this value, even if pdiff
|
|
|
|
|
* claims that the images are identical */
|
|
|
|
|
#define PERCEPTUAL_DIFF_THRESHOLD 25
|
|
|
|
|
|
2006-08-31 04:01:10 -07:00
|
|
|
/* Compare two buffers, returning the number of pixels that are
|
|
|
|
|
* different and the maximum difference of any single color channel in
|
|
|
|
|
* result_ret.
|
|
|
|
|
*
|
|
|
|
|
* This function should be rewritten to compare all formats supported by
|
2005-08-05 11:15:04 +00:00
|
|
|
* cairo_format_t instead of taking a mask as a parameter.
|
|
|
|
|
*/
|
2006-08-31 04:01:10 -07:00
|
|
|
static void
|
2008-08-18 12:53:43 +01:00
|
|
|
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,
|
2005-08-05 11:15:04 +00:00
|
|
|
int width,
|
|
|
|
|
int height,
|
2007-06-13 00:15:34 -04:00
|
|
|
uint32_t mask,
|
2006-08-31 04:01:10 -07:00
|
|
|
buffer_diff_result_t *result_ret)
|
2005-01-25 14:45:31 +00:00
|
|
|
{
|
2008-08-18 12:53:43 +01:00
|
|
|
const uint32_t *buf_a = (const uint32_t*) _buf_a;
|
|
|
|
|
const uint32_t *buf_b = (const uint32_t*) _buf_b;
|
|
|
|
|
uint32_t *buf_diff = (uint32_t*) _buf_diff;
|
2005-01-25 14:45:31 +00:00
|
|
|
int x, y;
|
2006-08-31 04:01:10 -07:00
|
|
|
buffer_diff_result_t result = {0, 0};
|
2005-01-25 14:45:31 +00:00
|
|
|
|
2008-08-18 12:53:43 +01:00
|
|
|
stride_a /= sizeof (uint32_t);
|
|
|
|
|
stride_b /= sizeof (uint32_t);
|
|
|
|
|
stride_diff /= sizeof (uint32_t);
|
Remove clip handling from generic surface layer.
Handling clip as part of the surface state, as opposed to being part of
the operation state, is cumbersome and a hindrance to providing true proxy
surface support. For example, the clip must be copied from the surface
onto the fallback image, but this was forgotten causing undue hassle in
each backend. Another example is the contortion the meta surface
endures to ensure the clip is correctly recorded. By contrast passing the
clip along with the operation is quite simple and enables us to write
generic handlers for providing surface wrappers. (And in the future, we
should be able to write more esoteric wrappers, e.g. automatic 2x FSAA,
trivially.)
In brief, instead of the surface automatically applying the clip before
calling the backend, the backend can call into a generic helper to apply
clipping. For raster surfaces, clip regions are handled automatically as
part of the composite interface. For vector surfaces, a clip helper is
introduced to replay and callback into an intersect_clip_path() function
as necessary.
Whilst this is not primarily a performance related change (the change
should just move the computation of the clip from the moment it is applied
by the user to the moment it is required by the backend), it is important
to track any potential regression:
ppc:
Speedups
========
image-rgba evolution-20090607-0 1026085.22 0.18% -> 672972.07 0.77%: 1.52x speedup
▌
image-rgba evolution-20090618-0 680579.98 0.12% -> 573237.66 0.16%: 1.19x speedup
▎
image-rgba swfdec-fill-rate-4xaa-0 460296.92 0.36% -> 407464.63 0.42%: 1.13x speedup
▏
image-rgba swfdec-fill-rate-2xaa-0 128431.95 0.47% -> 115051.86 0.42%: 1.12x speedup
▏
Slowdowns
=========
image-rgba firefox-periodic-table-0 56837.61 0.78% -> 66055.17 3.20%: 1.09x slowdown
▏
2009-07-23 15:32:13 +01:00
|
|
|
for (y = 0; y < height; y++) {
|
2008-08-18 12:53:43 +01:00
|
|
|
const uint32_t *row_a = buf_a + y * stride_a;
|
|
|
|
|
const uint32_t *row_b = buf_b + y * stride_b;
|
|
|
|
|
uint32_t *row = buf_diff + y * stride_diff;
|
Remove clip handling from generic surface layer.
Handling clip as part of the surface state, as opposed to being part of
the operation state, is cumbersome and a hindrance to providing true proxy
surface support. For example, the clip must be copied from the surface
onto the fallback image, but this was forgotten causing undue hassle in
each backend. Another example is the contortion the meta surface
endures to ensure the clip is correctly recorded. By contrast passing the
clip along with the operation is quite simple and enables us to write
generic handlers for providing surface wrappers. (And in the future, we
should be able to write more esoteric wrappers, e.g. automatic 2x FSAA,
trivially.)
In brief, instead of the surface automatically applying the clip before
calling the backend, the backend can call into a generic helper to apply
clipping. For raster surfaces, clip regions are handled automatically as
part of the composite interface. For vector surfaces, a clip helper is
introduced to replay and callback into an intersect_clip_path() function
as necessary.
Whilst this is not primarily a performance related change (the change
should just move the computation of the clip from the moment it is applied
by the user to the moment it is required by the backend), it is important
to track any potential regression:
ppc:
Speedups
========
image-rgba evolution-20090607-0 1026085.22 0.18% -> 672972.07 0.77%: 1.52x speedup
▌
image-rgba evolution-20090618-0 680579.98 0.12% -> 573237.66 0.16%: 1.19x speedup
▎
image-rgba swfdec-fill-rate-4xaa-0 460296.92 0.36% -> 407464.63 0.42%: 1.13x speedup
▏
image-rgba swfdec-fill-rate-2xaa-0 128431.95 0.47% -> 115051.86 0.42%: 1.12x speedup
▏
Slowdowns
=========
image-rgba firefox-periodic-table-0 56837.61 0.78% -> 66055.17 3.20%: 1.09x slowdown
▏
2009-07-23 15:32:13 +01:00
|
|
|
|
|
|
|
|
for (x = 0; x < width; x++) {
|
2005-08-05 11:15:04 +00:00
|
|
|
/* check if the pixels are the same */
|
|
|
|
|
if ((row_a[x] & mask) != (row_b[x] & mask)) {
|
|
|
|
|
int channel;
|
2007-06-13 00:15:34 -04:00
|
|
|
uint32_t diff_pixel = 0;
|
2005-08-05 11:15:04 +00:00
|
|
|
|
|
|
|
|
/* calculate a difference value for all 4 channels */
|
|
|
|
|
for (channel = 0; channel < 4; channel++) {
|
2006-08-30 13:17:08 -04:00
|
|
|
int value_a = (row_a[x] >> (channel*8)) & 0xff;
|
|
|
|
|
int value_b = (row_b[x] >> (channel*8)) & 0xff;
|
2006-08-08 01:16:49 -07:00
|
|
|
unsigned int diff;
|
2006-08-22 22:00:58 -04:00
|
|
|
diff = abs (value_a - value_b);
|
2006-08-31 04:01:10 -07:00
|
|
|
if (diff > result.max_diff)
|
|
|
|
|
result.max_diff = diff;
|
2006-08-30 13:17:08 -04:00
|
|
|
diff *= 4; /* emphasize */
|
|
|
|
|
if (diff)
|
|
|
|
|
diff += 128; /* make sure it's visible */
|
2006-08-01 15:20:39 -04:00
|
|
|
if (diff > 255)
|
2006-08-30 13:17:08 -04:00
|
|
|
diff = 255;
|
2006-08-01 15:20:39 -04:00
|
|
|
diff_pixel |= diff << (channel*8);
|
2005-08-05 11:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
2006-08-31 04:01:10 -07:00
|
|
|
result.pixels_changed++;
|
Remove clip handling from generic surface layer.
Handling clip as part of the surface state, as opposed to being part of
the operation state, is cumbersome and a hindrance to providing true proxy
surface support. For example, the clip must be copied from the surface
onto the fallback image, but this was forgotten causing undue hassle in
each backend. Another example is the contortion the meta surface
endures to ensure the clip is correctly recorded. By contrast passing the
clip along with the operation is quite simple and enables us to write
generic handlers for providing surface wrappers. (And in the future, we
should be able to write more esoteric wrappers, e.g. automatic 2x FSAA,
trivially.)
In brief, instead of the surface automatically applying the clip before
calling the backend, the backend can call into a generic helper to apply
clipping. For raster surfaces, clip regions are handled automatically as
part of the composite interface. For vector surfaces, a clip helper is
introduced to replay and callback into an intersect_clip_path() function
as necessary.
Whilst this is not primarily a performance related change (the change
should just move the computation of the clip from the moment it is applied
by the user to the moment it is required by the backend), it is important
to track any potential regression:
ppc:
Speedups
========
image-rgba evolution-20090607-0 1026085.22 0.18% -> 672972.07 0.77%: 1.52x speedup
▌
image-rgba evolution-20090618-0 680579.98 0.12% -> 573237.66 0.16%: 1.19x speedup
▎
image-rgba swfdec-fill-rate-4xaa-0 460296.92 0.36% -> 407464.63 0.42%: 1.13x speedup
▏
image-rgba swfdec-fill-rate-2xaa-0 128431.95 0.47% -> 115051.86 0.42%: 1.12x speedup
▏
Slowdowns
=========
image-rgba firefox-periodic-table-0 56837.61 0.78% -> 66055.17 3.20%: 1.09x slowdown
▏
2009-07-23 15:32:13 +01:00
|
|
|
if ((diff_pixel & 0x00ffffff) == 0) {
|
|
|
|
|
/* alpha only difference, convert to luminance */
|
|
|
|
|
uint8_t alpha = diff_pixel >> 24;
|
|
|
|
|
diff_pixel = alpha * 0x010101;
|
|
|
|
|
}
|
2005-08-05 11:15:04 +00:00
|
|
|
row[x] = diff_pixel;
|
2005-01-25 14:45:31 +00:00
|
|
|
} else {
|
2005-08-05 11:15:04 +00:00
|
|
|
row[x] = 0;
|
2005-01-25 14:45:31 +00:00
|
|
|
}
|
2005-08-05 11:15:04 +00:00
|
|
|
row[x] |= 0xff000000; /* Set ALPHA to 100% (opaque) */
|
2005-01-25 14:45:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-31 04:01:10 -07:00
|
|
|
*result_ret = result;
|
2005-04-27 13:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-18 12:53:43 +01:00
|
|
|
/* Compares two image surfaces
|
|
|
|
|
*
|
|
|
|
|
* Provides number of pixels changed and maximum single-channel
|
|
|
|
|
* difference in result.
|
|
|
|
|
*
|
|
|
|
|
* Also fills in a "diff" surface intended to visually show where the
|
|
|
|
|
* images differ.
|
|
|
|
|
*/
|
|
|
|
|
static void
|
2008-08-11 21:12:45 +01:00
|
|
|
compare_surfaces (const cairo_test_context_t *ctx,
|
|
|
|
|
cairo_surface_t *surface_a,
|
2006-12-12 16:37:35 -08:00
|
|
|
cairo_surface_t *surface_b,
|
|
|
|
|
cairo_surface_t *surface_diff,
|
|
|
|
|
buffer_diff_result_t *result)
|
2005-08-05 11:15:04 +00:00
|
|
|
{
|
2006-12-14 04:17:07 -08:00
|
|
|
/* These default values were taken straight from the
|
|
|
|
|
* perceptualdiff program. We'll probably want to tune these as
|
|
|
|
|
* necessary. */
|
|
|
|
|
double gamma = 2.2;
|
|
|
|
|
double luminance = 100.0;
|
|
|
|
|
double field_of_view = 45.0;
|
|
|
|
|
int discernible_pixels_changed;
|
|
|
|
|
|
|
|
|
|
/* First, we run cairo's old buffer_diff algorithm which looks for
|
|
|
|
|
* pixel-perfect images, (we do this first since the test suite
|
|
|
|
|
* runs about 3x slower if we run pdiff_compare first).
|
|
|
|
|
*/
|
2006-12-12 16:37:35 -08:00
|
|
|
buffer_diff_core (cairo_image_surface_get_data (surface_a),
|
2008-08-18 12:53:43 +01:00
|
|
|
cairo_image_surface_get_stride (surface_a),
|
2006-12-12 16:37:35 -08:00
|
|
|
cairo_image_surface_get_data (surface_b),
|
2008-08-18 12:53:43 +01:00
|
|
|
cairo_image_surface_get_stride (surface_b),
|
2006-12-12 16:37:35 -08:00
|
|
|
cairo_image_surface_get_data (surface_diff),
|
2008-08-18 12:53:43 +01:00
|
|
|
cairo_image_surface_get_stride (surface_diff),
|
2006-12-12 16:37:35 -08:00
|
|
|
cairo_image_surface_get_width (surface_a),
|
|
|
|
|
cairo_image_surface_get_height (surface_a),
|
2008-08-18 12:53:43 +01:00
|
|
|
cairo_surface_get_content (surface_a) & CAIRO_CONTENT_ALPHA ? 0xffffffff : 0x00ffffff,
|
2006-12-12 16:37:35 -08:00
|
|
|
result);
|
2006-12-14 04:17:07 -08:00
|
|
|
if (result->pixels_changed == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_log (ctx,
|
|
|
|
|
"%d pixels differ (with maximum difference of %d) from reference image\n",
|
2006-12-14 04:17:07 -08:00
|
|
|
result->pixels_changed, result->max_diff);
|
|
|
|
|
|
|
|
|
|
/* Then, if there are any different pixels, we give the pdiff code
|
|
|
|
|
* a crack at the images. If it decides that there are no visually
|
|
|
|
|
* discernible differences in any pixels, then we accept this
|
2008-02-25 21:06:35 -05:00
|
|
|
* result as good enough.
|
2008-08-11 21:12:45 +01:00
|
|
|
*
|
2008-02-25 21:06:35 -05:00
|
|
|
* Only let pdiff have a crack at the comparison if the max difference
|
|
|
|
|
* is lower than a threshold, otherwise some problems could be masked.
|
|
|
|
|
*/
|
|
|
|
|
if (result->max_diff < PERCEPTUAL_DIFF_THRESHOLD) {
|
|
|
|
|
discernible_pixels_changed = pdiff_compare (surface_a, surface_b,
|
|
|
|
|
gamma, luminance, field_of_view);
|
|
|
|
|
if (discernible_pixels_changed == 0) {
|
|
|
|
|
result->pixels_changed = 0;
|
2008-08-11 21:12:45 +01:00
|
|
|
cairo_test_log (ctx,
|
|
|
|
|
"But perceptual diff finds no visually discernible difference.\n"
|
2008-02-25 21:06:35 -05:00
|
|
|
"Accepting result.\n");
|
|
|
|
|
}
|
2006-12-14 04:17:07 -08:00
|
|
|
}
|
2005-08-05 11:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
2006-08-31 04:01:10 -07:00
|
|
|
void
|
2008-08-18 12:53:43 +01:00
|
|
|
buffer_diff_noalpha (const unsigned char *buf_a,
|
|
|
|
|
const unsigned char *buf_b,
|
2005-08-05 11:15:04 +00:00
|
|
|
unsigned char *buf_diff,
|
|
|
|
|
int width,
|
|
|
|
|
int height,
|
2006-12-12 02:17:19 -08:00
|
|
|
int stride,
|
2006-08-31 04:01:10 -07:00
|
|
|
buffer_diff_result_t *result)
|
2005-08-05 11:15:04 +00:00
|
|
|
{
|
2008-08-18 12:53:43 +01:00
|
|
|
buffer_diff_core(buf_a, stride,
|
|
|
|
|
buf_b, stride,
|
|
|
|
|
buf_diff, stride,
|
|
|
|
|
width, height,
|
|
|
|
|
0x00ffffff,
|
2006-08-31 04:01:10 -07:00
|
|
|
result);
|
2005-08-05 11:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-18 12:53:43 +01:00
|
|
|
static cairo_bool_t
|
|
|
|
|
same_size (cairo_surface_t *a, cairo_surface_t *b)
|
|
|
|
|
{
|
|
|
|
|
unsigned int width_a, height_a;
|
|
|
|
|
unsigned int width_b, height_b;
|
|
|
|
|
|
|
|
|
|
width_a = cairo_image_surface_get_width (a);
|
|
|
|
|
height_a = cairo_image_surface_get_height (a);
|
|
|
|
|
|
|
|
|
|
width_b = cairo_image_surface_get_width (b);
|
|
|
|
|
height_b = cairo_image_surface_get_height (b);
|
|
|
|
|
|
|
|
|
|
return width_a == width_b && height_a == height_b;
|
|
|
|
|
}
|
|
|
|
|
|
2005-04-27 13:33:25 +00:00
|
|
|
/* Image comparison code courtesy of Richard Worth <richard@theworths.org>
|
|
|
|
|
* Returns number of pixels changed, (or -1 on error).
|
|
|
|
|
* Also saves a "diff" image intended to visually show where the
|
|
|
|
|
* images differ.
|
2006-08-31 04:01:10 -07:00
|
|
|
*
|
|
|
|
|
* The return value simply indicates whether a check was successfully
|
|
|
|
|
* made, (as opposed to a file-not-found condition or similar). It
|
|
|
|
|
* does not indicate anything about how much the images differ. For
|
|
|
|
|
* that, see result.
|
|
|
|
|
*
|
|
|
|
|
* One failure mode is if the two images provided do not have the same
|
|
|
|
|
* dimensions. In this case, this function will return
|
|
|
|
|
* CAIRO_STATUS_SURFACE_TYPE_MISMATCH (which is a bit of an abuse, but
|
|
|
|
|
* oh well).
|
2005-04-27 13:33:25 +00:00
|
|
|
*/
|
2008-08-18 12:53:43 +01:00
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
if (cairo_surface_status (surface_a))
|
|
|
|
|
return cairo_surface_status (surface_a);
|
|
|
|
|
|
|
|
|
|
if (cairo_surface_status (surface_b))
|
|
|
|
|
return cairo_surface_status (surface_b);
|
|
|
|
|
|
|
|
|
|
if (cairo_surface_status (surface_diff))
|
|
|
|
|
return cairo_surface_status (surface_diff);
|
|
|
|
|
|
|
|
|
|
if (! same_size (surface_a, surface_b) ||
|
|
|
|
|
! same_size (surface_a, surface_diff))
|
|
|
|
|
{
|
|
|
|
|
cairo_test_log (ctx, "Error: Image size mismatch\n");
|
|
|
|
|
return CAIRO_STATUS_SURFACE_TYPE_MISMATCH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
compare_surfaces (ctx, surface_a, surface_b, surface_diff, result);
|
|
|
|
|
|
|
|
|
|
return CAIRO_STATUS_SUCCESS;
|
|
|
|
|
}
|
2010-04-28 12:54:54 +02:00
|
|
|
|
|
|
|
|
cairo_bool_t
|
|
|
|
|
image_diff_is_failure (const buffer_diff_result_t *result,
|
|
|
|
|
unsigned int tolerance)
|
|
|
|
|
{
|
|
|
|
|
return result->pixels_changed &&
|
|
|
|
|
result->max_diff > tolerance;
|
|
|
|
|
}
|