1 /* imagediff - Compare two images
3 * Copyright © 2004 Richard D. Worth
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.
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.
24 * Author: Richard D. Worth <richard@theworths.org> */
37 #include "buffer-diff.h"
40 _xunlink (const char *pathname)
42 if (unlink (pathname) < 0 && errno != ENOENT) {
43 fprintf (stderr, " Error: Cannot remove %s: %s\n",
44 pathname, strerror (errno));
50 cairo_test_logv (const cairo_test_context_t *ctx,
51 const char *fmt, va_list va)
53 vfprintf (stderr, fmt, va);
57 cairo_test_log (const cairo_test_context_t *ctx, const char *fmt, ...)
62 vfprintf (stderr, fmt, va);
66 /* Flatten an ARGB surface by blending it over white. The resulting
67 * surface, (still in ARGB32 format, but with only alpha==1.0
68 * everywhere) is returned in the same surface pointer.
70 * The original surface will be destroyed.
72 * The (x,y) value specify an origin of interest for the original
73 * image. The flattened image will be generated only from the box
74 * extending from (x,y) to (width,height).
77 flatten_surface (cairo_surface_t **surface, int x, int y)
79 cairo_surface_t *flat;
82 flat = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
83 cairo_image_surface_get_width (*surface) - x,
84 cairo_image_surface_get_height (*surface) - y);
85 cairo_surface_set_device_offset (flat, -x, -y);
87 cr = cairo_create (flat);
88 cairo_surface_destroy (flat);
90 cairo_set_source_rgb (cr, 1, 1, 1);
93 cairo_set_source_surface (cr, *surface, 0, 0);
94 cairo_surface_destroy (*surface);
97 *surface = cairo_surface_reference (cairo_get_target (cr));
101 /* Given an image surface, create a new surface that has the same
102 * contents as the sub-surface with its origin at x,y.
104 * The original surface will be destroyed.
107 extract_sub_surface (cairo_surface_t **surface, int x, int y)
109 cairo_surface_t *sub;
112 sub = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
113 cairo_image_surface_get_width (*surface) - x,
114 cairo_image_surface_get_height (*surface) - y);
116 /* We don't use a device offset like flatten_surface. That's not
117 * for any important reason, (the results should be
118 * identical). This style just seemed more natural to me this
119 * time, so I'm leaving both here so I can look at both to see
120 * which I like better. */
121 cr = cairo_create (sub);
122 cairo_surface_destroy (sub);
124 cairo_set_source_surface (cr, *surface, -x, -y);
125 cairo_surface_destroy (*surface);
126 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
129 *surface = cairo_surface_reference (cairo_get_target (cr));
133 static cairo_status_t
134 stdio_write_func (void *closure, const unsigned char *data, unsigned int length)
136 FILE *file = closure;
138 if (fwrite (data, 1, length, file) != length)
139 return CAIRO_STATUS_WRITE_ERROR;
141 return CAIRO_STATUS_SUCCESS;
144 static cairo_status_t
145 write_png (cairo_surface_t *surface, const char *filename)
147 cairo_status_t status;
150 if (filename != NULL) {
151 png_file = fopen (filename, "wb");
152 if (png_file == NULL) {
155 return CAIRO_STATUS_NO_MEMORY;
157 return CAIRO_STATUS_WRITE_ERROR;
163 status = cairo_surface_write_to_png_stream (surface,
167 if (png_file != stdout)
173 static cairo_status_t
174 png_diff (const char *filename_a,
175 const char *filename_b,
176 const char *filename_diff,
181 buffer_diff_result_t *result)
183 cairo_surface_t *surface_a;
184 cairo_surface_t *surface_b;
185 cairo_surface_t *surface_diff;
186 cairo_status_t status;
188 surface_a = cairo_image_surface_create_from_png (filename_a);
189 status = cairo_surface_status (surface_a);
191 fprintf (stderr, "Error: Failed to create surface from %s: %s\n",
192 filename_a, cairo_status_to_string (status));
196 surface_b = cairo_image_surface_create_from_png (filename_b);
197 status = cairo_surface_status (surface_b);
199 fprintf (stderr, "Error: Failed to create surface from %s: %s\n",
200 filename_b, cairo_status_to_string (status));
201 cairo_surface_destroy (surface_a);
206 extract_sub_surface (&surface_a, ax, ay);
211 extract_sub_surface (&surface_b, bx, by);
215 status = cairo_surface_status (surface_a);
217 fprintf (stderr, "Error: Failed to extract surface from %s: %s\n",
218 filename_a, cairo_status_to_string (status));
219 cairo_surface_destroy (surface_a);
220 cairo_surface_destroy (surface_b);
223 status = cairo_surface_status (surface_b);
225 fprintf (stderr, "Error: Failed to extract surface from %s: %s\n",
226 filename_b, cairo_status_to_string (status));
227 cairo_surface_destroy (surface_a);
228 cairo_surface_destroy (surface_b);
232 surface_diff = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
233 cairo_image_surface_get_width (surface_a),
234 cairo_image_surface_get_height (surface_a));
235 status = cairo_surface_status (surface_diff);
238 "Error: Failed to allocate surface to hold differences\n");
239 cairo_surface_destroy (surface_a);
240 cairo_surface_destroy (surface_b);
241 return CAIRO_STATUS_NO_MEMORY;
244 status = image_diff (NULL,
245 surface_a, surface_b, surface_diff,
249 _xunlink (filename_diff);
251 if (status == CAIRO_STATUS_SUCCESS &&
252 result->pixels_changed)
254 status = write_png (surface_diff, filename_diff);
257 cairo_surface_destroy (surface_a);
258 cairo_surface_destroy (surface_b);
259 cairo_surface_destroy (surface_diff);
265 main (int argc, char *argv[])
267 buffer_diff_result_t result;
268 cairo_status_t status;
270 unsigned int ax, ay, bx, by;
272 if (argc != 3 && argc != 7) {
273 fprintf (stderr, "Usage: %s image1.png image2.png [ax ay bx by]\n", argv[0]);
274 fprintf (stderr, "Computes an output image designed to present a \"visual diff\" such that even\n");
275 fprintf (stderr, "small errors in single pixels are readily apparent in the output.\n");
276 fprintf (stderr, "The output image is written on stdout.\n");
281 ax = strtoul (argv[3], NULL, 0);
282 ay = strtoul (argv[4], NULL, 0);
283 bx = strtoul (argv[5], NULL, 0);
284 by = strtoul (argv[6], NULL, 0);
286 ax = ay = bx = by = 0;
289 status = png_diff (argv[1], argv[2], NULL, ax, ay, bx, by, &result);
292 fprintf (stderr, "Error comparing images: %s\n",
293 cairo_status_to_string (status));
297 if (result.pixels_changed)
298 fprintf (stderr, "Total pixels changed: %d with a maximum channel difference of %d.\n",
299 result.pixels_changed,
302 return (result.pixels_changed != 0);