#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
#include <gd.h>
#define ABS(x) (((x) < 0) ? -(x) : (x))
-static void imageDiff (gdImagePtr dst, gdImagePtr src,
- int dstX, int dstY,
- int srcX, int srcY,
- int w, int h)
-{
- int s, d;
- int x, y;
-
- for (y = 0; (y < h); y++) {
- for (x = 0; (x < w); x++) {
- s = gdImageGetTrueColorPixel (src, srcX + x, srcY + y);
- d = gdImageGetTrueColorPixel (dst, dstX + x, dstY + y);
-
- d = gdTrueColorAlpha(
- ABS(gdTrueColorGetRed(s) - gdTrueColorGetRed(d)),
- ABS(gdTrueColorGetGreen(s) - gdTrueColorGetGreen(d)),
- ABS(gdTrueColorGetBlue(s) - gdTrueColorGetBlue(d)),
- ABS(gdTrueColorGetAlpha(s) - gdTrueColorGetAlpha(d)));
-
- gdImageSetPixel (dst, dstX + x, dstY + y, d);
- }
- }
-}
+static char *pstopng="gs -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=- -q -";
static gdImagePtr imageLoad (char *filename)
{
FILE *f;
- char *ext;
+ char *ext, *cmd, *tmp, *base;
gdImagePtr im;
+ int rc;
+ struct stat statbuf;
- f = fopen(filename, "rb");
- if (!f) {
- fprintf(stderr, "Input file \"%s\" does not exist!\n", filename);
- exit(1);
- }
ext = strrchr(filename, '.');
if (!ext) {
fprintf(stderr, "Filename \"%s\" has no file extension.\n", filename);
exit(1);
}
+ rc = stat(filename, &statbuf);
+ if (rc) {
+ fprintf(stderr, "Failed to stat \"%s\"\n", filename);
+ exit(1);
+ }
+ if (strcasecmp(ext, ".ps") == 0) {
+ ext = ".png";
+ base = strrchr(filename, '/');
+ if (base != filename)
+ base++;
+ tmp = malloc(strlen(base) + 20 + strlen(ext));
+ strcpy(tmp,"/tmp/diffimg-");
+ strcat(tmp,base);
+ strcat(tmp,ext);
+
+ cmd = malloc(strlen(pstopng) + strlen(filename) + strlen(tmp) + 10);
+ strcpy(cmd,pstopng);
+ strcat(cmd," <");
+ strcat(cmd,filename);
+ strcat(cmd," >");
+ strcat(cmd,tmp);
+ rc = system(cmd);
+
+ f = fopen(tmp, "rb");
+ free(cmd);
+ free(tmp);
+ }
+ else
+ f = fopen(filename, "rb");
+ if (!f) {
+ fprintf(stderr, "Failed to open \"%s\"\n", filename);
+ exit(1);
+ }
im = 0;
if (strcasecmp(ext, ".png") == 0)
im = gdImageCreateFromPng(f);
return im;
}
+static void imageDiff (gdImagePtr dst, gdImagePtr src,
+ int dstX, int dstY,
+ int srcX, int srcY,
+ int w, int h)
+{
+ int s, d;
+ int x, y;
+
+ for (y = 0; (y < h); y++) {
+ for (x = 0; (x < w); x++) {
+ s = gdImageGetTrueColorPixel (src, srcX + x, srcY + y);
+ d = gdImageGetTrueColorPixel (dst, dstX + x, dstY + y);
+
+ d = gdTrueColorAlpha(
+ ABS(gdTrueColorGetRed(s) - gdTrueColorGetRed(d)),
+ ABS(gdTrueColorGetGreen(s) - gdTrueColorGetGreen(d)),
+ ABS(gdTrueColorGetBlue(s) - gdTrueColorGetBlue(d)),
+ ABS(gdTrueColorGetAlpha(s) - gdTrueColorGetAlpha(d)));
+
+ gdImageSetPixel (dst, dstX + x, dstY + y, d);
+ }
+ }
+}
+
int main(int argc, char **argv)
{
gdImagePtr im1, im2, im3;