#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <string.h>
#include <pixman.h>
#include "cairo-test.h"
#include "pdiff.h"
#include "buffer-diff.h"
#define PERCEPTUAL_DIFF_THRESHOLD 25
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)
{
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;
int x, y;
buffer_diff_result_t result = {0, 0};
stride_a /= sizeof (uint32_t);
stride_b /= sizeof (uint32_t);
stride_diff /= sizeof (uint32_t);
for (y = 0; y < height; y++) {
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;
for (x = 0; x < width; x++) {
if ((row_a[x] & mask) != (row_b[x] & mask)) {
int channel;
uint32_t diff_pixel = 0;
for (channel = 0; channel < 4; channel++) {
int value_a = (row_a[x] >> (channel*8)) & 0xff;
int value_b = (row_b[x] >> (channel*8)) & 0xff;
unsigned int diff;
diff = abs (value_a - value_b);
if (diff > result.max_diff)
result.max_diff = diff;
diff *= 4;
if (diff)
diff += 128;
if (diff > 255)
diff = 255;
diff_pixel |= diff << (channel*8);
}
result.pixels_changed++;
if ((diff_pixel & 0x00ffffff) == 0) {
uint8_t alpha = diff_pixel >> 24;
diff_pixel = alpha * 0x010101;
}
row[x] = diff_pixel;
} else {
row[x] = 0;
}
row[x] |= 0xff000000;
}
}
*result_ret = result;
}
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)
{
double gamma = 2.2;
double luminance = 100.0;
double field_of_view = 45.0;
int discernible_pixels_changed;
buffer_diff_core (cairo_image_surface_get_data (surface_a),
cairo_image_surface_get_stride (surface_a),
cairo_image_surface_get_data (surface_b),
cairo_image_surface_get_stride (surface_b),
cairo_image_surface_get_data (surface_diff),
cairo_image_surface_get_stride (surface_diff),
cairo_image_surface_get_width (surface_a),
cairo_image_surface_get_height (surface_a),
cairo_surface_get_content (surface_a) & CAIRO_CONTENT_ALPHA ? 0xffffffff : 0x00ffffff,
result);
if (result->pixels_changed == 0)
return;
cairo_test_log (ctx,
"%d pixels differ (with maximum difference of %d) from reference image\n",
result->pixels_changed, result->max_diff);
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;
cairo_test_log (ctx,
"But perceptual diff finds no visually discernible difference.\n"
"Accepting result.\n");
}
}
}
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)
{
buffer_diff_core(buf_a, stride,
buf_b, stride,
buf_diff, stride,
width, height,
0x00ffffff,
result);
}
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;
}
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;
}
cairo_bool_t
image_diff_is_failure (const buffer_diff_result_t *result,
unsigned int tolerance)
{
return result->pixels_changed &&
result->max_diff > tolerance;
}