1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
/*
* Copyright (C) 2009 Zan Dobersek <zandobersek@gmail.com>
* Copyright (C) 2010 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <gdk/gdk.h>
using namespace std;
static double tolerance = 0;
static GOptionEntry commandLineOptionEntries[] =
{
{ "tolerance", 0, 0, G_OPTION_ARG_DOUBLE, &tolerance, "Percentage difference between images before considering them different", "T" },
{ 0, 0, 0, G_OPTION_ARG_NONE, 0, 0, 0 },
};
GdkPixbuf* readPixbufFromStdin(long imageSize)
{
unsigned char imageBuffer[2048];
GdkPixbufLoader* loader = gdk_pixbuf_loader_new_with_type("png", 0);
GError* error = 0;
while (imageSize > 0) {
size_t bytesToRead = min<int>(imageSize, 2048);
size_t bytesRead = fread(imageBuffer, 1, bytesToRead, stdin);
if (!gdk_pixbuf_loader_write(loader, reinterpret_cast<const guchar*>(imageBuffer), bytesRead, &error)) {
g_error_free(error);
gdk_pixbuf_loader_close(loader, 0);
g_object_unref(loader);
return 0;
}
imageSize -= static_cast<int>(bytesRead);
}
gdk_pixbuf_loader_close(loader, 0);
GdkPixbuf* decodedImage = gdk_pixbuf_loader_get_pixbuf(loader);
g_object_ref(decodedImage);
return decodedImage;
}
GdkPixbuf* differenceImageFromDifferenceBuffer(unsigned char* buffer, int width, int height)
{
GdkPixbuf* image = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, width, height);
if (!image)
return image;
int rowStride = gdk_pixbuf_get_rowstride(image);
unsigned char* diffPixels = gdk_pixbuf_get_pixels(image);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
unsigned char* diffPixel = diffPixels + (y * rowStride) + (x * 3);
diffPixel[0] = diffPixel[1] = diffPixel[2] = *buffer++;
}
}
return image;
}
float calculateDifference(GdkPixbuf* baselineImage, GdkPixbuf* actualImage, GdkPixbuf** differenceImage)
{
int width = gdk_pixbuf_get_width(actualImage);
int height = gdk_pixbuf_get_height(actualImage);
int numberOfChannels = gdk_pixbuf_get_n_channels(actualImage);
if ((width != gdk_pixbuf_get_width(baselineImage))
|| (height != gdk_pixbuf_get_height(baselineImage))
|| (numberOfChannels != gdk_pixbuf_get_n_channels(baselineImage))
|| (gdk_pixbuf_get_has_alpha(actualImage) != gdk_pixbuf_get_has_alpha(baselineImage))) {
fprintf(stderr, "Error, test and reference image have different properties.\n");
return 100; // Completely different.
}
unsigned char* diffBuffer = static_cast<unsigned char*>(malloc(width * height));
float count = 0;
float sum = 0;
float maxDistance = 0;
int actualRowStride = gdk_pixbuf_get_rowstride(actualImage);
int baseRowStride = gdk_pixbuf_get_rowstride(baselineImage);
unsigned char* actualPixels = gdk_pixbuf_get_pixels(actualImage);
unsigned char* basePixels = gdk_pixbuf_get_pixels(baselineImage);
unsigned char* currentDiffPixel = diffBuffer;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
unsigned char* actualPixel = actualPixels + (y * actualRowStride) + (x * numberOfChannels);
unsigned char* basePixel = basePixels + (y * baseRowStride) + (x * numberOfChannels);
float red = (actualPixel[0] - basePixel[0]) / max<float>(255 - basePixel[0], basePixel[0]);
float green = (actualPixel[1] - basePixel[1]) / max<float>(255 - basePixel[1], basePixel[1]);
float blue = (actualPixel[2] - basePixel[2]) / max<float>(255 - basePixel[2], basePixel[2]);
float alpha = (actualPixel[3] - basePixel[3]) / max<float>(255 - basePixel[3], basePixel[3]);
float distance = sqrtf(red * red + green * green + blue * blue + alpha * alpha) / 2.0f;
*currentDiffPixel++ = (unsigned char)(distance * 255.0f);
if (distance >= 1.0f / 255.0f) {
count += 1.0f;
sum += distance;
maxDistance = max<float>(maxDistance, distance);
}
}
}
// Compute the difference as a percentage combining both the number of
// different pixels and their difference amount i.e. the average distance
// over the entire image
float difference = 0;
if (count > 0.0f)
difference = 100.0f * sum / (height * width);
if (difference <= tolerance)
difference = 0;
else {
difference = roundf(difference * 100.0f) / 100.0f;
difference = max(difference, 0.01f); // round to 2 decimal places
*differenceImage = differenceImageFromDifferenceBuffer(diffBuffer, width, height);
}
free(diffBuffer);
return difference;
}
void printImage(GdkPixbuf* image)
{
char* buffer;
gsize bufferSize;
GError* error = 0;
if (!gdk_pixbuf_save_to_buffer(image, &buffer, &bufferSize, "png", &error, NULL)) {
g_error_free(error);
return; // Don't bail out, as we can still use the percentage output.
}
printf("Content-Length: %" G_GSIZE_FORMAT "\n", bufferSize);
fwrite(buffer, 1, bufferSize, stdout);
}
void printImageDifferences(GdkPixbuf* baselineImage, GdkPixbuf* actualImage)
{
GdkPixbuf* differenceImage = 0;
float difference = calculateDifference(baselineImage, actualImage, &differenceImage);
if (difference > 0.0f) {
if (differenceImage) {
printImage(differenceImage);
g_object_unref(differenceImage);
}
printf("diff: %01.2f%% failed\n", difference);
} else
printf("diff: %01.2f%% passed\n", difference);
}
int main(int argc, char* argv[])
{
GError* error = 0;
GOptionContext* context = g_option_context_new("- compare two image files, printing their percentage difference and the difference image to stdout");
g_option_context_add_main_entries(context, commandLineOptionEntries, 0);
if (!g_option_context_parse(context, &argc, &argv, &error)) {
printf("Option parsing failed: %s\n", error->message);
g_error_free(error);
return 1;
}
GdkPixbuf* actualImage = 0;
GdkPixbuf* baselineImage = 0;
char buffer[2048];
while (fgets(buffer, sizeof(buffer), stdin)) {
// Convert the first newline into a NUL character so that strtok doesn't produce it.
char* newLineCharacter = strchr(buffer, '\n');
if (newLineCharacter)
*newLineCharacter = '\0';
if (!strncmp("Content-Length: ", buffer, 16)) {
gchar** tokens = g_strsplit(buffer, " ", 0);
if (!tokens[1]) {
g_strfreev(tokens);
printf("Error, image size must be specified..\n");
return 1;
}
long imageSize = strtol(tokens[1], 0, 10);
g_strfreev(tokens);
if (imageSize > 0 && !actualImage) {
if (!(actualImage = readPixbufFromStdin(imageSize))) {
printf("Error, could not read actual image.\n");
return 1;
}
} else if (imageSize > 0 && !baselineImage) {
if (!(baselineImage = readPixbufFromStdin(imageSize))) {
printf("Error, could not read baseline image.\n");
return 1;
}
} else {
printf("Error, image size must be specified..\n");
return 1;
}
}
if (actualImage && baselineImage) {
printImageDifferences(baselineImage, actualImage);
g_object_unref(actualImage);
g_object_unref(baselineImage);
actualImage = 0;
baselineImage = 0;
}
fflush(stdout);
}
return 0;
}
|