if (test->width != ref_width || test->height != ref_height) { fprintf (stderr, "  Error: Image size mismatch: (%dx%d) vs. (%dx%d)\n" "         for %s vs %s\n", test->width, test->height, ref_width, ref_height, png_name, ref_name); ret = CAIRO_TEST_FAILURE; goto BAIL; }
pixels_changed = buffer_diff (png_buf, ref_buf, diff_buf, test->width, test->height, stride); if (pixels_changed) { log_file = fopen (log_name, "a"); fprintf (log_file, "Error: %d pixels differ from reference image %s\n", pixels_changed, ref_name); png_file = fopen (diff_name, "w"); write_png_argb32 (diff_buf, png_file, test->width, test->height, stride); fclose (png_file); fclose (log_file);
cairo_test (cairo_test_t *test, cairo_test_draw_function_t draw){    cairo_t *cr;    int stride;    unsigned char *png_buf, *ref_buf, *diff_buf;    char *png_name, *ref_name, *diff_name;    char *srcdir;    int pixels_changed;    int ref_width, ref_height, ref_stride;    read_png_status_t png_status;    cairo_test_status_t ret;    /* The cairo part of the test is the easiest part */    cr = cairo_create ();    stride = 4 * test->width;    png_buf = xcalloc (stride * test->height, 1);    diff_buf = xcalloc (stride * test->height, 1);    cairo_set_target_image (cr, png_buf, CAIRO_FORMAT_ARGB32,			    test->width, test->height, stride);    (draw) (cr, test->width, test->height);    cairo_destroy (cr);    /* Skip image check for tests with no image (width,height == 0,0) */    if (test->width == 0 || test->height == 0) {	free (png_buf);	free (diff_buf);	return CAIRO_TEST_SUCCESS;    }    /* Then we've got a bunch of string manipulation and file I/O for the check */    srcdir = getenv ("srcdir");    if (!srcdir)	srcdir = ".";    xasprintf (&png_name, "%s%s", test->name, CAIRO_TEST_PNG_SUFFIX);    xasprintf (&ref_name, "%s/%s%s", srcdir, test->name, CAIRO_TEST_REF_SUFFIX);    xasprintf (&diff_name, "%s%s", test->name, CAIRO_TEST_DIFF_SUFFIX);    write_png_argb32 (png_buf, png_name, test->width, test->height, stride);    ref_buf = NULL;    png_status = (read_png_argb32 (ref_name, &ref_buf, &ref_width, &ref_height, &ref_stride));    if (png_status) {	switch (png_status)	{	case READ_PNG_FILE_NOT_FOUND:	    fprintf (stderr, "  Error: No reference image found: %s\n", ref_name);	    break;	case READ_PNG_FILE_NOT_PNG:	    fprintf (stderr, "  Error: %s is not a png image\n", ref_name);	    break;	default:	    fprintf (stderr, "  Error: Failed to read %s\n", ref_name);	}			ret = CAIRO_TEST_FAILURE;	goto BAIL;    }    if (test->width != ref_width || test->height != ref_height) {	fprintf (stderr,		 "  Error: Image size mismatch: (%dx%d) vs. (%dx%d)\n"		 "         for %s vs %s\n",		 test->width, test->height,		 ref_width, ref_height,		 png_name, ref_name);	ret = CAIRO_TEST_FAILURE;	goto BAIL;    }    pixels_changed = image_diff (png_buf, ref_buf, diff_buf,				 test->width, test->height, stride);    if (pixels_changed) {	fprintf (stderr, "  Error: %d pixels differ from reference image %s\n",		 pixels_changed, ref_name);	write_png_argb32 (diff_buf, diff_name, test->width, test->height, stride);	ret = CAIRO_TEST_FAILURE;	goto BAIL;    } else {	if (unlink (diff_name) < 0 && errno != ENOENT) {	    fprintf (stderr, "  Error: Cannot remove %s: %s\n",		     diff_name, strerror (errno));	    ret = CAIRO_TEST_FAILURE;	    goto BAIL;	}    }    ret = CAIRO_TEST_SUCCESS;BAIL:    free (png_buf);    free (ref_buf);    free (diff_buf);    free (png_name);    free (ref_name);    free (diff_name);    return ret;}