]> granicus.if.org Git - graphviz/commitdiff
add .ps to diffimg by using system(gs ...) for the conversion
authorellson <devnull@localhost>
Tue, 18 Oct 2005 18:53:33 +0000 (18:53 +0000)
committerellson <devnull@localhost>
Tue, 18 Oct 2005 18:53:33 +0000 (18:53 +0000)
contrib/diffimg/diffimg.c

index a5eca94727ae6e0ff73f59b5fdd8871b9c2914c8..bda190e75eb386db6f35150fcd134b82e8cfab11 100644 (file)
 #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);
@@ -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;