diff options
author | Carl Worth <cworth@cworth.org> | 2006-02-13 16:47:13 -0800 |
---|---|---|
committer | Carl Worth <cworth@cworth.org> | 2006-02-13 16:47:13 -0800 |
commit | 5de154bcdb659618d723bcec14e0315630c62c7e (patch) | |
tree | 462e5c5d5a444c9388da1d33de1399e2fa98c9ed /test/buffer-diff.c | |
parent | 6c38e238e5daab5df4c11027d28e48e62bbd4bc8 (diff) | |
parent | f030aec810d74a60a44c35bf7815b9e94743cd65 (diff) |
Remove pixman from SNAPSHOT_0_5_0SNAPSHOT_0_5_0
Diffstat (limited to 'test/buffer-diff.c')
-rw-r--r-- | test/buffer-diff.c | 105 |
1 files changed, 91 insertions, 14 deletions
diff --git a/test/buffer-diff.c b/test/buffer-diff.c index 07abd62a3..e540d2f9b 100644 --- a/test/buffer-diff.c +++ b/test/buffer-diff.c @@ -23,20 +23,39 @@ * * Author: Richard D. Worth <richard@theworths.org> */ -#include "buffer_diff.h" +#include <stdio.h> +#include <unistd.h> +#include <errno.h> +#include <string.h> + +#include "cairo-test.h" + +#include "buffer-diff.h" +#include "read-png.h" +#include "write-png.h" +#include "xmalloc.h" + +static void +xunlink (const char *pathname) +{ + if (unlink (pathname) < 0 && errno != ENOENT) { + cairo_test_log (" Error: Cannot remove %s: %s\n", + pathname, strerror (errno)); + exit (1); + } +} -/* Image comparison code courttesy of Richard Worth. - * Returns number of pixels changed. - * Also fills out a "diff" image intended to visually show where the - * images differ. - */ int -buffer_diff (char *buf_a, char *buf_b, char *buf_diff, - int width, int height, int stride) +buffer_diff (unsigned char *buf_a, + unsigned char *buf_b, + unsigned char *buf_diff, + int width, + int height, + int stride) { int x, y; - int total_pixels_changed = 0; unsigned char *row_a, *row_b, *row; + int pixels_changed = 0; for (y = 0; y < height; y++) { @@ -47,19 +66,19 @@ buffer_diff (char *buf_a, char *buf_b, char *buf_diff, { int channel; unsigned char value_a, value_b; - int pixel_changed = 0; + int pixel_differs = 0; for (channel = 0; channel < 4; channel++) { double diff; value_a = row_a[x * 4 + channel]; value_b = row_b[x * 4 + channel]; if (value_a != value_b) - pixel_changed = 1; + pixel_differs = 1; diff = value_a - value_b; row[x * 4 + channel] = 128 + diff / 3.0; } - if (pixel_changed) { - total_pixels_changed++; + if (pixel_differs) { + pixels_changed++; } else { row[x*4+0] = 0; row[x*4+1] = 0; @@ -69,5 +88,63 @@ buffer_diff (char *buf_a, char *buf_b, char *buf_diff, } } - return total_pixels_changed; + return pixels_changed; +} + +/* 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. + */ +int +image_diff (const char *filename_a, + const char *filename_b, + const char *filename_diff) +{ + int pixels_changed; + unsigned int width_a, height_a, stride_a; + unsigned int width_b, height_b, stride_b; + unsigned char *buf_a, *buf_b, *buf_diff; + read_png_status_t status; + + status = read_png_argb32 (filename_a, &buf_a, &width_a, &height_a, &stride_a); + if (status) + return -1; + + status = read_png_argb32 (filename_b, &buf_b, &width_b, &height_b, &stride_b); + if (status) + return -1; + + if (width_a != width_b || + height_a != height_b || + stride_a != stride_b) + { + cairo_test_log ("Error: Image size mismatch: (%dx%d@%d) vs. (%dx%d@%d)\n" + " for %s vs. %s\n", + width_a, height_a, stride_a, + width_b, height_b, stride_b, + filename_a, filename_b); + free (buf_a); + free (buf_b); + return -1; + } + + buf_diff = xcalloc (stride_a * height_a, 1); + + pixels_changed = buffer_diff (buf_a, buf_b, buf_diff, + width_a, height_a, stride_a); + + if (pixels_changed) { + FILE *png_file = fopen (filename_diff, "wb"); + write_png_argb32 (buf_diff, png_file, width_a, height_a, stride_a); + fclose (png_file); + } else { + xunlink (filename_diff); + } + + free (buf_a); + free (buf_b); + free (buf_diff); + + return pixels_changed; } |