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
|
/*************************************************************************
* Copyright (c) 2011 AT&T Intellectual Property
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Details at http://www.graphviz.org/
*************************************************************************/
/*
* This program generates an image where each pixel is the
* difference between the corresponding pixel in each of the
* two source images. Thus, if the source images are the same
* the resulting image will be black, otherwise it will have
* regions of non-black where the images differ.
*
* Currently supports: .png, .gif, .jpg, and .ps by using ghostscript
*
* John Ellson <ellson@research.att.com>
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef _WIN32
#define EX_USAGE 64
#define EX_DATAERR 65
#define EX_NOINPUT 66
#define EX_UNAVAILABLE 69
#define EX_OSERR 71
#else
#include <sysexits.h>
#endif
#include <gd.h>
#include <stdbool.h>
#include <util/agxbuf.h>
#include <util/alloc.h>
#include <util/exit.h>
static const char pstopng[] =
"gs -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=- -q -";
static gdImagePtr imageLoad(const char *filename) {
FILE *f;
const char *ext = strrchr(filename, '.');
if (!ext) {
fprintf(stderr, "Filename \"%s\" has no file extension.\n", filename);
graphviz_exit(EX_USAGE);
}
int rc = stat(filename, &(struct stat){0});
if (rc) {
fprintf(stderr, "Failed to stat \"%s\"\n", filename);
graphviz_exit(EX_NOINPUT);
}
if (strcasecmp(ext, ".ps") == 0) {
ext = ".png";
agxbuf fname = {0};
agxbprint(&fname, "%s%s", filename, ext);
const char *tmp = agxbuse(&fname);
agxbuf cmd = {0};
agxbprint(&cmd, "%s <%s >%s", pstopng, filename, tmp);
rc = system(agxbuse(&cmd));
agxbfree(&cmd);
if (rc) {
agxbfree(&fname);
fprintf(stderr, "Failed to convert \"%s%s\"\n", filename, ext);
graphviz_exit(EX_USAGE);
}
f = fopen(tmp, "rb");
agxbfree(&fname);
if (!f) {
fprintf(stderr, "Failed to open converted \"%s%s\"\n", filename, ext);
graphviz_exit(EX_NOINPUT);
}
} else {
f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "Failed to open \"%s\"\n", filename);
graphviz_exit(EX_NOINPUT);
}
}
gdImagePtr im = NULL;
if (strcasecmp(ext, ".png") == 0) {
#ifdef HAVE_GD_PNG
im = gdImageCreateFromPng(f);
#else
fprintf(stderr, "PNG support is not available\n");
graphviz_exit(EX_UNAVAILABLE);
#endif
} else if (strcasecmp(ext, ".gif") == 0) {
#ifdef HAVE_GD_GIF
im = gdImageCreateFromGif(f);
#else
fprintf(stderr, "GIF support is not available\n");
graphviz_exit(EX_UNAVAILABLE);
#endif
} else if (strcasecmp(ext, ".jpg") == 0) {
#ifdef HAVE_GD_JPEG
im = gdImageCreateFromJpeg(f);
#else
fprintf(stderr, "JPEG support is not available\n");
graphviz_exit(EX_UNAVAILABLE);
#endif
}
fclose(f);
if (!im) {
fprintf(stderr, "Loading image from file \"%s\" failed!\n", filename);
graphviz_exit(EX_DATAERR);
}
return im;
}
static bool imageDiff(gdImagePtr A, gdImagePtr B, gdImagePtr C, int w, int h,
int black, int white) {
bool rc = false;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
const bool d = (bool)(gdImageGetTrueColorPixel(B, x, y) -
gdImageGetTrueColorPixel(A, x, y));
gdImageSetPixel(C, x, y, d ? white : black);
rc |= d;
}
}
return rc;
}
int main(int argc, char **argv) {
#ifdef HAVE_GD_PNG
FILE *f;
#endif
if (argc == 2 && strcmp(argv[1], "-?") == 0) {
fprintf(stderr, "Usage: diffimg image1 image2 [outimage]\n");
graphviz_exit(0);
}
if (argc < 3) {
fprintf(stderr, "Usage: diffimg image1 image2 [outimage]\n");
graphviz_exit(EX_USAGE);
}
gdImagePtr A = imageLoad(argv[1]);
gdImagePtr B = imageLoad(argv[2]);
const int minSX = (gdImageSX(A) < gdImageSX(B)) ? gdImageSX(A) : gdImageSX(B);
const int minSY = (gdImageSY(A) < gdImageSY(B)) ? gdImageSY(A) : gdImageSY(B);
const int maxSX = (gdImageSX(A) > gdImageSX(B)) ? gdImageSX(A) : gdImageSX(B);
const int maxSY = (gdImageSY(A) > gdImageSY(B)) ? gdImageSY(A) : gdImageSY(B);
gdImagePtr C = gdImageCreatePalette(maxSX, maxSY);
const int white = gdImageColorAllocate(C, gdRedMax, gdGreenMax, gdBlueMax);
const int black = gdImageColorAllocate(C, 0, 0, 0);
if (maxSX > minSX && maxSY > minSY)
gdImageFilledRectangle(C, minSX, minSY, maxSX - 1, maxSY - 1, black);
const bool rc = imageDiff(A, B, C, minSX, minSY, black, white);
#ifdef HAVE_GD_PNG
if (argc > 3 && (f = fopen(argv[3], "wb"))) {
gdImagePng(C, f);
fclose(f);
} else
gdImagePng(C, stdout);
#else
fprintf(stderr, "PNG output support is not available\n");
#endif
gdImageDestroy(A);
gdImageDestroy(B);
gdImageDestroy(C);
graphviz_exit(rc ? EXIT_FAILURE : EXIT_SUCCESS);
}
|