6ebbcfca74b1c6aa471e5c6a0604cd90e7a198e1
[framework/graphics/cairo.git] / test / imagediff.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 #ifdef 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
37 #include "buffer-diff.h"
38
39 static void
40 _xunlink (const char *pathname)
41 {
42     if (unlink (pathname) < 0 && errno != ENOENT) {
43         fprintf (stderr, "  Error: Cannot remove %s: %s\n",
44                         pathname, strerror (errno));
45         exit (1);
46     }
47 }
48
49 void
50 cairo_test_logv (const cairo_test_context_t *ctx,
51                  const char *fmt, va_list va)
52 {
53     vfprintf (stderr, fmt, va);
54 }
55
56 void
57 cairo_test_log (const cairo_test_context_t *ctx, const char *fmt, ...)
58 {
59     va_list va;
60
61     va_start (va, fmt);
62     vfprintf (stderr, fmt, va);
63     va_end (va);
64 }
65
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.
69  *
70  * The original surface will be destroyed.
71  *
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).
75  */
76 static void
77 flatten_surface (cairo_surface_t **surface, int x, int y)
78 {
79     cairo_surface_t *flat;
80     cairo_t *cr;
81
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);
86
87     cr = cairo_create (flat);
88     cairo_surface_destroy (flat);
89
90     cairo_set_source_rgb (cr, 1, 1, 1);
91     cairo_paint (cr);
92
93     cairo_set_source_surface (cr, *surface, 0, 0);
94     cairo_surface_destroy (*surface);
95     cairo_paint (cr);
96
97     *surface = cairo_surface_reference (cairo_get_target (cr));
98     cairo_destroy (cr);
99 }
100
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.
103  *
104  * The original surface will be destroyed.
105  */
106 static void
107 extract_sub_surface (cairo_surface_t **surface, int x, int y)
108 {
109     cairo_surface_t *sub;
110     cairo_t *cr;
111
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);
115
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);
123
124     cairo_set_source_surface (cr, *surface, -x, -y);
125     cairo_surface_destroy (*surface);
126     cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
127     cairo_paint (cr);
128
129     *surface = cairo_surface_reference (cairo_get_target (cr));
130     cairo_destroy (cr);
131 }
132
133 static cairo_status_t
134 stdio_write_func (void *closure, const unsigned char *data, unsigned int length)
135 {
136     FILE *file = closure;
137
138     if (fwrite (data, 1, length, file) != length)
139         return CAIRO_STATUS_WRITE_ERROR;
140
141     return CAIRO_STATUS_SUCCESS;
142 }
143
144 static cairo_status_t
145 write_png (cairo_surface_t *surface, const char *filename)
146 {
147     cairo_status_t status;
148     FILE *png_file;
149
150     if (filename != NULL) {
151         png_file = fopen (filename, "wb");
152         if (png_file == NULL) {
153             switch (errno) {
154             case ENOMEM:
155                 return CAIRO_STATUS_NO_MEMORY;
156             default:
157                 return CAIRO_STATUS_WRITE_ERROR;
158             }
159         }
160     } else
161         png_file = stdout;
162
163     status = cairo_surface_write_to_png_stream (surface,
164                                                 stdio_write_func,
165                                                 png_file);
166
167     if (png_file != stdout)
168         fclose (png_file);
169
170     return status;
171 }
172
173 static cairo_status_t
174 png_diff (const char *filename_a,
175           const char *filename_b,
176           const char *filename_diff,
177           int           ax,
178           int           ay,
179           int           bx,
180           int           by,
181           buffer_diff_result_t *result)
182 {
183     cairo_surface_t *surface_a;
184     cairo_surface_t *surface_b;
185     cairo_surface_t *surface_diff;
186     cairo_status_t status;
187
188     surface_a = cairo_image_surface_create_from_png (filename_a);
189     status = cairo_surface_status (surface_a);
190     if (status) {
191         fprintf (stderr, "Error: Failed to create surface from %s: %s\n",
192                  filename_a, cairo_status_to_string (status));
193         return status;
194     }
195
196     surface_b = cairo_image_surface_create_from_png (filename_b);
197     status = cairo_surface_status (surface_b);
198     if (status) {
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);
202         return status;
203     }
204
205     if (ax || ay) {
206         extract_sub_surface (&surface_a, ax, ay);
207         ax = ay = 0;
208     }
209
210     if (bx || by) {
211         extract_sub_surface (&surface_b, bx, by);
212         bx = by = 0;
213     }
214
215     status = cairo_surface_status (surface_a);
216     if (status) {
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);
221         return status;
222     }
223     status = cairo_surface_status (surface_b);
224     if (status) {
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);
229         return status;
230     }
231
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);
236     if (status) {
237         fprintf (stderr,
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;
242     }
243
244     status = image_diff (NULL,
245                          surface_a, surface_b, surface_diff,
246                          result);
247
248     if (filename_diff)
249         _xunlink (filename_diff);
250
251     if (status == CAIRO_STATUS_SUCCESS &&
252         result->pixels_changed)
253     {
254         status = write_png (surface_diff, filename_diff);
255     }
256
257     cairo_surface_destroy (surface_a);
258     cairo_surface_destroy (surface_b);
259     cairo_surface_destroy (surface_diff);
260
261     return status;
262 }
263
264 int
265 main (int argc, char *argv[])
266 {
267     buffer_diff_result_t result;
268     cairo_status_t status;
269
270     unsigned int ax, ay, bx, by;
271
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");
277         exit (1);
278     }
279
280     if (argc == 7) {
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);
285     } else {
286         ax = ay = bx = by = 0;
287     }
288
289     status = png_diff (argv[1], argv[2], NULL, ax, ay, bx, by, &result);
290
291     if (status) {
292         fprintf (stderr, "Error comparing images: %s\n",
293                  cairo_status_to_string (status));
294         return 1;
295     }
296
297     if (result.pixels_changed)
298         fprintf (stderr, "Total pixels changed: %d with a maximum channel difference of %d.\n",
299                  result.pixels_changed,
300                  result.max_diff);
301
302     return (result.pixels_changed != 0);
303 }