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
|
/* $Id: diffimg.c,v 1.11 2006/01/28 00:20:45 ellson Exp $ $Revision: 1.11 $ */
/* vim:set shiftwidth=4 ts=8: */
/**********************************************************
* This software is part of the graphviz package *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2004 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
**********************************************************/
/*
* 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>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sysexits.h>
#include <gd.h>
#if defined HAVE_STDBOOL_H && ! defined __cplusplus
#include <stdbool.h>
#endif
#define NOT(v) (!(v))
#if ! defined HAVE_BOOL && ! defined __cplusplus
typedef unsigned char bool;
#define false 0
#define true NOT(false)
#endif
static char *pstopng="gs -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=- -q -";
static gdImagePtr imageLoad (char *filename)
{
FILE *f;
char *ext, *cmd, *tmp;
gdImagePtr im;
int rc;
struct stat statbuf;
ext = strrchr(filename, '.');
if (!ext) {
fprintf(stderr, "Filename \"%s\" has no file extension.\n", filename);
exit(EX_USAGE);
}
rc = stat(filename, &statbuf);
if (rc) {
fprintf(stderr, "Failed to stat \"%s\"\n", filename);
exit(EX_NOINPUT);
}
if (strcasecmp(ext, ".ps") == 0) {
ext = ".png";
tmp = malloc(strlen(filename) + strlen(ext) + 1);
strcpy(tmp,filename);
strcat(tmp,ext);
cmd = malloc(strlen(pstopng) + 2 + strlen(filename) + 2 + strlen(tmp) + 1);
strcpy(cmd,pstopng);
strcat(cmd," <");
strcat(cmd,filename);
strcat(cmd," >");
strcat(cmd,tmp);
rc = system(cmd);
free(cmd);
f = fopen(tmp, "rb");
free(tmp);
if (!f) {
fprintf(stderr, "Failed to open converted \"%s%s\"\n", filename, ext);
exit(EX_NOINPUT);
}
}
else {
f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "Failed to open \"%s\"\n", filename);
exit(EX_NOINPUT);
}
}
im = 0;
if (strcasecmp(ext, ".png") == 0) {
#ifdef HAVE_GD_PNG
im = gdImageCreateFromPng(f);
#else
fprintf(stderr, "PNG support is not available\n");
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");
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");
exit(EX_UNAVAILABLE);
#endif
}
fclose(f);
if (!im) {
fprintf(stderr, "Loading image from file \"%s\" failed!\n", filename);
exit(EX_DATAERR);
}
return im;
}
static bool imageDiff (gdImagePtr A, gdImagePtr B, gdImagePtr C,
unsigned int w, unsigned int h,
unsigned char black, unsigned char white)
{
unsigned int x, y;
bool d, rc;
rc = false;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
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)
{
gdImagePtr A, B, C;
unsigned char black, white;
unsigned int minSX, minSY, maxSX, maxSY;
bool rc;
#ifdef HAVE_GD_PNG
FILE *f;
#endif
if (argc < 3) {
fprintf(stderr, "Usage: diffimg image1 image2 [outimage]\n");
exit(EX_USAGE);
}
A = imageLoad(argv[1]);
B = imageLoad(argv[2]);
minSX = (gdImageSX(A) < gdImageSX(B)) ? gdImageSX(A) : gdImageSX(B);
minSY = (gdImageSY(A) < gdImageSY(B)) ? gdImageSY(A) : gdImageSY(B);
maxSX = (gdImageSX(A) > gdImageSX(B)) ? gdImageSX(A) : gdImageSX(B);
maxSY = (gdImageSY(A) > gdImageSY(B)) ? gdImageSY(A) : gdImageSY(B);
C = gdImageCreatePalette (maxSX, maxSY);
white = gdImageColorAllocate(C, gdRedMax, gdGreenMax, gdBlueMax);
black = gdImageColorAllocate(C, 0, 0, 0);
if (maxSX > minSX && maxSY > minSY)
gdImageFilledRectangle(C, minSX, minSY, maxSX-1, maxSY-1, black);
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);
return (rc ? EXIT_FAILURE : EXIT_SUCCESS);
}
|