1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
/* Copyright (C) 2001-2019 Peter Selinger.
This file is part of Potrace. It is free software and it is covered
by the GNU General Public License. See the file COPYING for details. */
/* This program compares two equal sized PGM files and outputs a
numerical difference, which is proportional to the sum of the
squares of all pixel differences. Return value is 0 on success, 1
if the pixmaps were different sizes, or 2 on other error. */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "../src/greymap.h"
#include "../src/platform.h"
int main(int ac, char **av) {
char *file1, *file2;
FILE *f;
greymap_t *g1, *g2;
int r;
double diff;
int x, y, d;
platform_init();
if (ac != 3) {
fprintf(stderr, "pgmdiff: wrong number of arguments\n");
fprintf(stderr, "Usage: pgmdiff file1 file2\n");
return 2;
}
file1 = av[1];
file2 = av[2];
/* read the greymaps */
if (strcmp(file1, "-")==0) {
r = gm_read(stdin, &g1);
} else {
f = fopen(file1, "rb");
if (!f) {
fprintf(stderr, "pgmdiff: %s: %s\n", file1, strerror(errno));
return 2;
}
r = gm_read(f, &g1);
fclose(f);
}
if (r==-1) {
fprintf(stderr, "pgmdiff: %s: %s\n", file1, strerror(errno));
return 2;
} else if (r) {
fprintf(stderr, "pgmdiff: %s: bad pgm file\n", file1);
return 2;
}
if (strcmp(file2, "-")==0) {
r = gm_read(stdin, &g2);
} else {
f = fopen(file2, "rb");
if (!f) {
fprintf(stderr, "pgmdiff: %s: %s\n", file2, strerror(errno));
return 2;
}
r = gm_read(f, &g2);
fclose(f);
}
if (r==-1) {
fprintf(stderr, "pgmdiff: %s: %s\n", file2, strerror(errno));
return 2;
} else if (r) {
fprintf(stderr, "pgmdiff: %s: bad pgm file\n", file2);
return 2;
}
if (g1->h != g2->h || g1->w != g2->w) {
fprintf(stderr, "pgmdiff: images have differing dimensions\n");
return 1;
}
/* compare them */
diff = 0;
for (y=0; y<g1->h; y++) {
for (x=0; x<g1->w; x++) {
d = GM_UGET(g1, x, y) - GM_UGET(g2, x, y);
if (d) {
diff += d*d;
}
}
}
/* normalize */
diff /= g1->h * g1->w;
printf("%ld\n", (long)diff);
gm_free(g1);
gm_free(g2);
return 0;
}
|