pgmdiff.c
Go to the documentation of this file.
00001 /* Copyright (C) 2001-2007 Peter Selinger.
00002    This file is part of Potrace. It is free software and it is covered
00003    by the GNU General Public License. See the file COPYING for details. */
00004 
00005 /* $Id: pgmdiff.c 147 2007-04-09 00:44:09Z selinger $ */
00006 
00007 /* This program compares two equal sized PGM files and outputs a
00008    numerical difference, which is proportional to the sum of the
00009    squares of all pixel differences. Return value is 0 on success, 1
00010    if the pixmaps were different sizes, or 2 on other error. */
00011 
00012 #include <stdio.h>
00013 #include <string.h>
00014 #include <errno.h>
00015 
00016 #include "../src/greymap.h"
00017 #include "../src/platform.h"
00018 
00019 int main(int ac, char **av) {
00020   char *file1, *file2;
00021   FILE *f;
00022   greymap_t *g1, *g2;
00023   int r;
00024   double diff;
00025   int x, y, d;
00026 
00027   platform_init();
00028 
00029   if (ac != 3) {
00030     fprintf(stderr, "pgmdiff: wrong number of arguments\n");
00031     fprintf(stderr, "Usage: pgmdiff file1 file2\n");
00032     return 2;
00033   }
00034 
00035   file1 = av[1];
00036   file2 = av[2];
00037 
00038   /* read the greymaps */
00039 
00040   if (strcmp(file1, "-")==0) {
00041     r = gm_read(stdin, &g1);
00042   } else {
00043     f = fopen(file1, "rb");
00044     if (!f) {
00045       fprintf(stderr, "pgmdiff: %s: %s\n", file1, strerror(errno));
00046       return 2;
00047     }
00048     r = gm_read(f, &g1);
00049     fclose(f);
00050   }
00051   if (r==-1) {
00052     fprintf(stderr, "pgmdiff: %s: %s\n", file1, strerror(errno));
00053     return 2;
00054   } else if (r) {
00055     fprintf(stderr, "pgmdiff: %s: bad pgm file\n", file1);
00056     return 2;
00057   }
00058 
00059   if (strcmp(file2, "-")==0) {
00060     r = gm_read(stdin, &g2);
00061   } else {
00062     f = fopen(file2, "rb");
00063     if (!f) {
00064       fprintf(stderr, "pgmdiff: %s: %s\n", file2, strerror(errno));
00065       return 2;
00066     }
00067     r = gm_read(f, &g2);
00068     fclose(f);
00069   }
00070   if (r==-1) {
00071     fprintf(stderr, "pgmdiff: %s: %s\n", file2, strerror(errno));
00072     return 2;
00073   } else if (r) {
00074     fprintf(stderr, "pgmdiff: %s: bad pgm file\n", file2);
00075     return 2;
00076   }
00077 
00078   if (g1->h != g2->h || g1->w != g2->w) {
00079     fprintf(stderr, "pgmdiff: images have differing dimensions\n");
00080     return 1;
00081   }
00082 
00083   /* compare them */
00084   diff = 0;
00085 
00086   for (y=0; y<g1->h; y++) {
00087     for (x=0; x<g1->w; x++) {
00088       d = GM_UGET(g1, x, y) - GM_UGET(g2, x, y);
00089       if (d) {
00090         diff += d*d;
00091       }
00092     }
00093   }
00094 
00095   /* normalize */
00096   diff /= g1->h * g1->w;
00097   
00098   printf("%ld\n", (long)diff);
00099   return 0;
00100 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


portrait_painter
Author(s): Niklas Meinzer, Ina Baumgarten
autogenerated on Wed Dec 26 2012 16:00:43