From 66953954df20c49d1fb5246c29b514ed77d97ee8 Mon Sep 17 00:00:00 2001 From: ellson Date: Tue, 18 Oct 2005 18:53:33 +0000 Subject: [PATCH] add .ps to diffimg by using system(gs ...) for the conversion --- contrib/diffimg/diffimg.c | 93 +++++++++++++++++++++++++++------------ 1 file changed, 64 insertions(+), 29 deletions(-) diff --git a/contrib/diffimg/diffimg.c b/contrib/diffimg/diffimg.c index a5eca9472..bda190e75 100644 --- a/contrib/diffimg/diffimg.c +++ b/contrib/diffimg/diffimg.c @@ -29,50 +29,61 @@ #include #include #include +#include +#include +#include #include #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); @@ -88,6 +99,30 @@ static gdImagePtr imageLoad (char *filename) 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; -- 2.40.0