From 48b18362f7f884e965cb94d8fd798e54106b2635 Mon Sep 17 00:00:00 2001 From: ellson Date: Tue, 18 Oct 2005 18:53:29 +0000 Subject: [PATCH] extend diffimg to support .gif and .jpg in addition to .png --- contrib/diffimg/diffimg.c | 101 +++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 45 deletions(-) diff --git a/contrib/diffimg/diffimg.c b/contrib/diffimg/diffimg.c index 5055bf326..a5eca9472 100644 --- a/contrib/diffimg/diffimg.c +++ b/contrib/diffimg/diffimg.c @@ -15,78 +15,89 @@ **********************************************************/ /* - * 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. + * 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 only supports PNG images. + * Currently supports: .png, .gif, .jpg * * John Ellson */ #include #include +#include #include #define ABS(x) (((x) < 0) ? -(x) : (x)) -void imageDiff (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, - int srcY, int w, int h) +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; -/* assert (dst->trueColor); */ - 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 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) +static gdImagePtr imageLoad (char *filename) { - gdImagePtr im1, im2, im3; - FILE *in; + FILE *f; + char *ext; + gdImagePtr im; - if (argc != 3) { - fprintf(stderr, "Usage: diffimg file1.png file2.png \n"); + f = fopen(filename, "rb"); + if (!f) { + fprintf(stderr, "Input file \"%s\" does not exist!\n", filename); exit(1); } - in = fopen(argv[1], "rb"); - if (!in) { - fprintf(stderr, "Input file1 does not exist!\n"); + ext = strrchr(filename, '.'); + if (!ext) { + fprintf(stderr, "Filename \"%s\" has no file extension.\n", filename); exit(1); } - im1 = gdImageCreateFromPng(in); - fclose(in); - if (!im1) { - fprintf(stderr, "Input file1 is not in PNG format!\n"); + im = 0; + if (strcasecmp(ext, ".png") == 0) + im = gdImageCreateFromPng(f); + else if (strcasecmp(ext, ".gif") == 0) + im = gdImageCreateFromGif(f); + else if (strcasecmp(ext, ".jpg") == 0) + im = gdImageCreateFromJpeg(f); + fclose(f); + if (!im) { + fprintf(stderr, "Loading image from file \"%s\" failed!\n", filename); exit(1); } + return im; +} - in = fopen(argv[2], "rb"); - if (!in) { - fprintf(stderr, "Input file2 does not exist!\n"); - exit(1); - } - im2 = gdImageCreateFromPng(in); - fclose(in); - if (!im2) { - fprintf(stderr, "Input file2 is not in PNG format!\n"); +int main(int argc, char **argv) +{ + gdImagePtr im1, im2, im3; + + if (argc != 3) { + fprintf(stderr, "Usage: diffimg image1 image2\n"); exit(1); } + im1 = imageLoad(argv[1]); + im2 = imageLoad(argv[2]); im3 = gdImageCreateTrueColor ( (gdImageSX(im1) > gdImageSX(im2)) ? gdImageSX(im1) : gdImageSX(im2), -- 2.40.0