| 1 | 
#ifndef lint | 
| 2 | 
static const char RCSid[] = "$Id: radcompare.c,v 2.1 2018/10/15 17:52:52 greg Exp $"; | 
| 3 | 
#endif | 
| 4 | 
/* | 
| 5 | 
 * Compare Radiance files for significant differences | 
| 6 | 
 * | 
| 7 | 
 *      G. Ward | 
| 8 | 
 */ | 
| 9 | 
 | 
| 10 | 
#include <stdlib.h> | 
| 11 | 
#include <math.h> | 
| 12 | 
#include <ctype.h> | 
| 13 | 
#include "platform.h" | 
| 14 | 
#include "rtio.h" | 
| 15 | 
#include "resolu.h" | 
| 16 | 
#include "color.h" | 
| 17 | 
#include "lookup.h" | 
| 18 | 
                        /* Reporting levels */ | 
| 19 | 
#define REP_QUIET       0       /* no reporting */ | 
| 20 | 
#define REP_ERROR       1       /* report errors only */ | 
| 21 | 
#define REP_WARN        2       /* report warnings as well */ | 
| 22 | 
#define REP_VERBOSE     3       /* verbose reporting */ | 
| 23 | 
 | 
| 24 | 
int     report = REP_WARN;      /* reporting level */ | 
| 25 | 
 | 
| 26 | 
int     ign_header = 0;         /* ignore header differences? */ | 
| 27 | 
 | 
| 28 | 
double  rel_min = 1e-5;         /* positive for relative comparisons */ | 
| 29 | 
 | 
| 30 | 
double  rms_lim = 0.01;         /* RMS difference limit */ | 
| 31 | 
 | 
| 32 | 
double  max_lim = 0.25;         /* difference limit if non-negative */ | 
| 33 | 
 | 
| 34 | 
int     lin1cnt=0, lin2cnt=0;   /* file line position */ | 
| 35 | 
 | 
| 36 | 
                                /* file types */ | 
| 37 | 
const char      *file_type[] = { | 
| 38 | 
                        "Unrecognized", | 
| 39 | 
                        "TEXT_generic", | 
| 40 | 
                        "ascii", | 
| 41 | 
                        COLRFMT, | 
| 42 | 
                        CIEFMT, | 
| 43 | 
                        "BINARY_float", | 
| 44 | 
                        "BINARY_double", | 
| 45 | 
                        "BSDF_RBFmesh", | 
| 46 | 
                        "Radiance_octree", | 
| 47 | 
                        "Radiance_tmesh", | 
| 48 | 
                        "BINARY_unknown", | 
| 49 | 
                        NULL    /* terminator */ | 
| 50 | 
                }; | 
| 51 | 
 | 
| 52 | 
enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, TYP_FLOAT, | 
| 53 | 
                TYP_DOUBLE, TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, TYP_BINARY}; | 
| 54 | 
                 | 
| 55 | 
#define has_header(t)   (!( 1L<<(t) & (1L<<TYP_TEXT | 1L<<TYP_BINARY) )) | 
| 56 | 
 | 
| 57 | 
                                /* header variables to always ignore */ | 
| 58 | 
const char      *hdr_ignkey[] = { | 
| 59 | 
                        "SOFTWARE", | 
| 60 | 
                        "CAPDATE", | 
| 61 | 
                        "GMT", | 
| 62 | 
                        NULL    /* terminator */ | 
| 63 | 
                }; | 
| 64 | 
                                /* header variable settings */ | 
| 65 | 
LUTAB   hdr1 = LU_SINIT(free,free); | 
| 66 | 
LUTAB   hdr2 = LU_SINIT(free,free); | 
| 67 | 
 | 
| 68 | 
                                /* advance appropriate file line count */ | 
| 69 | 
#define adv_linecnt(htp)        (lin1cnt += (htp == &hdr1), \ | 
| 70 | 
                                        lin2cnt += (htp == &hdr2)) | 
| 71 | 
 | 
| 72 | 
                                /* input files */ | 
| 73 | 
char            *progname = NULL; | 
| 74 | 
const char      stdin_name[] = "<stdin>"; | 
| 75 | 
const char      *f1name=NULL, *f2name=NULL; | 
| 76 | 
FILE            *f1in=NULL, *f2in=NULL; | 
| 77 | 
                                /* running real differences */ | 
| 78 | 
double  diff2sum = 0; | 
| 79 | 
int     nsum = 0; | 
| 80 | 
 | 
| 81 | 
/* Report usage and exit */ | 
| 82 | 
static void | 
| 83 | 
usage() | 
| 84 | 
{ | 
| 85 | 
        fputs("Usage: ", stderr); | 
| 86 | 
        fputs(progname, stderr); | 
| 87 | 
        fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] file1 file2\n", | 
| 88 | 
                        stderr); | 
| 89 | 
        exit(1); | 
| 90 | 
} | 
| 91 | 
 | 
| 92 | 
/* Get type ID from name (or 0 if not found) */ | 
| 93 | 
static int | 
| 94 | 
xlate_type(const char *nm) | 
| 95 | 
{ | 
| 96 | 
        int     i; | 
| 97 | 
 | 
| 98 | 
        if (!nm || !*nm) | 
| 99 | 
                return(TYP_UNKNOWN); | 
| 100 | 
        for (i = 1; file_type[i]; i++) | 
| 101 | 
                if (!strcmp(nm, file_type[i])) | 
| 102 | 
                        return(i); | 
| 103 | 
        return(TYP_UNKNOWN); | 
| 104 | 
} | 
| 105 | 
 | 
| 106 | 
/* Compare real values and keep track of differences */ | 
| 107 | 
static int | 
| 108 | 
real_check(double r1, double r2) | 
| 109 | 
{ | 
| 110 | 
        double  diff2 = (r1 - r2)*(r1 - r2); | 
| 111 | 
 | 
| 112 | 
        if (rel_min > 0) {      /* doing relative differences? */ | 
| 113 | 
                double  av2 = .25*(r1*r1 + 2.*fabs(r1*r2) + r2*r2); | 
| 114 | 
                if (av2 > rel_min*rel_min) | 
| 115 | 
                        diff2 /= av2; | 
| 116 | 
        } | 
| 117 | 
        if (max_lim >= 0 && diff2 > max_lim*max_lim) { | 
| 118 | 
                if (report != REP_QUIET) | 
| 119 | 
                        fprintf(stderr, | 
| 120 | 
                        "%s: %sdifference between %.8g and %.8g exceeds epsilon\n", | 
| 121 | 
                                        progname, | 
| 122 | 
                                        (rel_min > 0) ? "relative " : "", | 
| 123 | 
                                        r1, r2); | 
| 124 | 
                return(0); | 
| 125 | 
        } | 
| 126 | 
        diff2sum += diff2; | 
| 127 | 
        nsum++; | 
| 128 | 
        return(1); | 
| 129 | 
} | 
| 130 | 
 | 
| 131 | 
/* Compare two strings for equivalence */ | 
| 132 | 
static int | 
| 133 | 
equiv_string(char *s1, char *s2) | 
| 134 | 
{ | 
| 135 | 
#define CLS_STR         0 | 
| 136 | 
#define CLS_INT         1 | 
| 137 | 
#define CLS_FLT         2 | 
| 138 | 
                                /* skip whitespace at beginning */ | 
| 139 | 
        while (isspace(*s1)) s1++; | 
| 140 | 
        while (isspace(*s2)) s2++; | 
| 141 | 
        while (*s1) {           /* check each word */ | 
| 142 | 
                int     inquote; | 
| 143 | 
                if (!*s2)       /* unexpected EOL in s2? */ | 
| 144 | 
                        return(0); | 
| 145 | 
                inquote = *s1; | 
| 146 | 
                if ((inquote != '\'') & (inquote != '"')) | 
| 147 | 
                        inquote = 0; | 
| 148 | 
                if (inquote) {  /* quoted text must match exactly */ | 
| 149 | 
                        if (*s1++ != *s2++) | 
| 150 | 
                                return(0); | 
| 151 | 
                        while (*s1 != inquote) { | 
| 152 | 
                                if (!*s1) | 
| 153 | 
                                        return(0); | 
| 154 | 
                                if (*s1++ != *s2++) | 
| 155 | 
                                        return(0); | 
| 156 | 
                        } | 
| 157 | 
                        s1++; | 
| 158 | 
                        if (*s2++ != inquote) | 
| 159 | 
                                return(0); | 
| 160 | 
                } else {        /* else classify word type */ | 
| 161 | 
                        char    *s1s = s1; | 
| 162 | 
                        char    *s2s = s2; | 
| 163 | 
                        int     cls = CLS_STR; | 
| 164 | 
                        s1 = sskip(s1); | 
| 165 | 
                        s2 = sskip(s2); | 
| 166 | 
                        if (iskip(s1s) == s1) { | 
| 167 | 
                                if (iskip(s2s) == s2) | 
| 168 | 
                                        cls = CLS_INT; | 
| 169 | 
                                else if (fskip(s2s) == s2) | 
| 170 | 
                                        cls = CLS_FLT; | 
| 171 | 
                        } else if (fskip(s1s) == s1) { | 
| 172 | 
                                if (fskip(s2s) != s2) | 
| 173 | 
                                        return(0); | 
| 174 | 
                                cls = CLS_FLT; | 
| 175 | 
                        } | 
| 176 | 
                        switch (cls) { | 
| 177 | 
                        case CLS_INT:           /* strncmp() faster */ | 
| 178 | 
                        case CLS_STR: | 
| 179 | 
                                if (s1 - s1s != s2 - s2s) | 
| 180 | 
                                        return(0); | 
| 181 | 
                                if (strncmp(s1s, s2s, s1 - s1s)) | 
| 182 | 
                                        return(0); | 
| 183 | 
                                break; | 
| 184 | 
                        case CLS_FLT: | 
| 185 | 
                                if (!real_check(atof(s1s), atof(s2s))) | 
| 186 | 
                                        return(0); | 
| 187 | 
                                break; | 
| 188 | 
                        } | 
| 189 | 
                } | 
| 190 | 
                while (isspace(*s1)) s1++; | 
| 191 | 
                while (isspace(*s2)) s2++; | 
| 192 | 
        } | 
| 193 | 
        return(!*s2);           /* match if we reached the end of s2, too */ | 
| 194 | 
#undef CLS_STR | 
| 195 | 
#undef CLS_INT | 
| 196 | 
#undef CLS_FLT | 
| 197 | 
} | 
| 198 | 
 | 
| 199 | 
/* Check if string is var=value pair and set if not in ignore list */ | 
| 200 | 
static int | 
| 201 | 
setheadvar(char *val, void *p) | 
| 202 | 
{ | 
| 203 | 
        LUTAB   *htp = (LUTAB *)p; | 
| 204 | 
        LUENT   *tep; | 
| 205 | 
        char    *key; | 
| 206 | 
        int     kln, vln; | 
| 207 | 
        int     n; | 
| 208 | 
 | 
| 209 | 
        if (!isalpha(*val))     /* key must start line */ | 
| 210 | 
                return(0); | 
| 211 | 
        key = val++; | 
| 212 | 
        while (*val && !isspace(*val) & (*val != '=')) | 
| 213 | 
                val++; | 
| 214 | 
        kln = val - key; | 
| 215 | 
        while (isspace(*val))   /* check for value */ | 
| 216 | 
                *val++ = '\0'; | 
| 217 | 
        if (*val != '=') | 
| 218 | 
                return(0); | 
| 219 | 
        *val++ = '\0'; | 
| 220 | 
        while (isspace(*val)) | 
| 221 | 
                val++; | 
| 222 | 
        if (!*val)              /* nothing set? */ | 
| 223 | 
                return(0); | 
| 224 | 
        vln = strlen(val);      /* eliminate space and newline at end */ | 
| 225 | 
        while (--vln > 0 && isspace(val[vln])) | 
| 226 | 
                ; | 
| 227 | 
        val[++vln] = '\0'; | 
| 228 | 
                                /* check if key to ignore */ | 
| 229 | 
        for (n = 0; hdr_ignkey[n]; n++) | 
| 230 | 
                if (!strcmp(key, hdr_ignkey[n])) | 
| 231 | 
                        return(0); | 
| 232 | 
        if (!(tep = lu_find(htp, key))) | 
| 233 | 
                return(-1); | 
| 234 | 
        if (!tep->key) | 
| 235 | 
                tep->key = strcpy(malloc(kln+1), key); | 
| 236 | 
        if (tep->data) | 
| 237 | 
                free(tep->data); | 
| 238 | 
        tep->data = strcpy(malloc(vln+1), val); | 
| 239 | 
        adv_linecnt(htp); | 
| 240 | 
        return(1); | 
| 241 | 
} | 
| 242 | 
 | 
| 243 | 
/* Lookup correspondent in other header */ | 
| 244 | 
static int | 
| 245 | 
match_val(const LUENT *ep1, void *p2) | 
| 246 | 
{ | 
| 247 | 
        const LUENT     *ep2 = lu_find((LUTAB *)p2, ep1->key); | 
| 248 | 
        if (!ep2 || !ep2->data) { | 
| 249 | 
                if (report != REP_QUIET) | 
| 250 | 
                        fprintf(stderr, "%s: Variable '%s' missing in '%s'\n", | 
| 251 | 
                                        progname, ep1->key, f2name); | 
| 252 | 
                return(-1); | 
| 253 | 
        } | 
| 254 | 
        if (!equiv_string((char *)ep1->data, (char *)ep2->data)) { | 
| 255 | 
                if (report != REP_QUIET) | 
| 256 | 
                        fprintf(stderr, "%s: Header variables '%s' have different values\n", | 
| 257 | 
                                        progname, ep1->key); | 
| 258 | 
                return(-1); | 
| 259 | 
        } | 
| 260 | 
        return(1);              /* good match */ | 
| 261 | 
} | 
| 262 | 
 | 
| 263 | 
/* Compare two sets of header variables */ | 
| 264 | 
static int | 
| 265 | 
headers_match(LUTAB *hp1, LUTAB *hp2) | 
| 266 | 
{ | 
| 267 | 
        int     ne = lu_doall(hp1, match_val, hp2); | 
| 268 | 
        if (ne < 0) | 
| 269 | 
                return(0);      /* something didn't match! */ | 
| 270 | 
        ne = lu_doall(hp2, NULL, NULL) - ne; | 
| 271 | 
        if (ne) { | 
| 272 | 
                if (report != REP_QUIET) | 
| 273 | 
                        fprintf(stderr, "%s: '%s' has %d extra header variable(s)\n", | 
| 274 | 
                                        progname, f2name, ne); | 
| 275 | 
                return(0); | 
| 276 | 
        } | 
| 277 | 
        return(1);              /* good match */ | 
| 278 | 
} | 
| 279 | 
 | 
| 280 | 
/* Check generic input to determine if it is binary, -1 on error */ | 
| 281 | 
static int | 
| 282 | 
input_is_binary(FILE *fin) | 
| 283 | 
{ | 
| 284 | 
        int     n = 0; | 
| 285 | 
        int     c = 0; | 
| 286 | 
 | 
| 287 | 
        while ((c = getc(fin)) != EOF) { | 
| 288 | 
                if (!c | (c > 127)) | 
| 289 | 
                        break;                  /* non-ascii character */ | 
| 290 | 
                if (++n >= 10240) | 
| 291 | 
                        break;                  /* enough to be confident */ | 
| 292 | 
        } | 
| 293 | 
        if (!n) | 
| 294 | 
                return(-1);                     /* first read failed */ | 
| 295 | 
        if (fseek(fin, 0L, 0) < 0) | 
| 296 | 
                return(-1);                     /* rewind failed */ | 
| 297 | 
        return(!c | (c > 127)); | 
| 298 | 
} | 
| 299 | 
 | 
| 300 | 
/* Identify and return data type from header (if one) */ | 
| 301 | 
static int | 
| 302 | 
identify_type(const char *name, FILE *fin, LUTAB *htp) | 
| 303 | 
{ | 
| 304 | 
        extern const char       HDRSTR[]; | 
| 305 | 
        int                     c; | 
| 306 | 
                                                /* check magic header start */ | 
| 307 | 
        if ((c = getc(fin)) != HDRSTR[0]) { | 
| 308 | 
                if (c == EOF) goto badeof; | 
| 309 | 
                ungetc(c, fin); | 
| 310 | 
                c = 0; | 
| 311 | 
        } else if ((c = getc(fin)) != HDRSTR[1]) { | 
| 312 | 
                if (c == EOF) goto badeof; | 
| 313 | 
                ungetc(c, fin); ungetc(HDRSTR[0], fin); | 
| 314 | 
                c = 0; | 
| 315 | 
        } | 
| 316 | 
        if (c) {                                /* appears to have a header */ | 
| 317 | 
                char    sbuf[32]; | 
| 318 | 
                if (!fgets(sbuf, sizeof(sbuf), fin)) | 
| 319 | 
                        goto badeof; | 
| 320 | 
                if (report >= REP_WARN && strncmp(sbuf, "RADIANCE", 8)) { | 
| 321 | 
                        fputs(name, stderr); | 
| 322 | 
                        fputs(": warning - unexpected header ID: ", stderr); | 
| 323 | 
                        fputs(sbuf, stderr); | 
| 324 | 
                } | 
| 325 | 
                adv_linecnt(htp);               /* for #?ID string */ | 
| 326 | 
                if (getheader(fin, setheadvar, htp) < 0) { | 
| 327 | 
                        fputs(name, stderr); | 
| 328 | 
                        fputs(": Unknown error reading header\n", stderr); | 
| 329 | 
                        return(-1); | 
| 330 | 
                } | 
| 331 | 
                adv_linecnt(htp);               /* for trailing emtpy line */ | 
| 332 | 
                return(xlate_type((const char *)lu_find(htp,"FORMAT")->data)); | 
| 333 | 
        } | 
| 334 | 
        c = input_is_binary(fin);               /* else peek to see if binary */ | 
| 335 | 
        if (c < 0) { | 
| 336 | 
                fputs(name, stderr); | 
| 337 | 
                fputs(": read/seek error\n", stderr); | 
| 338 | 
                return(-1); | 
| 339 | 
        } | 
| 340 | 
        if (c) | 
| 341 | 
                return(TYP_BINARY); | 
| 342 | 
        SET_FILE_TEXT(fin);                     /* originally set to binary */ | 
| 343 | 
        return(TYP_TEXT); | 
| 344 | 
badeof: | 
| 345 | 
        if (report != REP_QUIET) { | 
| 346 | 
                fputs(name, stderr); | 
| 347 | 
                fputs(": Unexpected end-of-file\n", stderr); | 
| 348 | 
        } | 
| 349 | 
        return(-1); | 
| 350 | 
} | 
| 351 | 
 | 
| 352 | 
/* Check that overall RMS error is below threshold */ | 
| 353 | 
static int | 
| 354 | 
good_RMS() | 
| 355 | 
{ | 
| 356 | 
        if (!nsum) | 
| 357 | 
                return(1); | 
| 358 | 
        if (diff2sum/(double)nsum > rms_lim*rms_lim) { | 
| 359 | 
                if (report != REP_QUIET) | 
| 360 | 
                        fprintf(stderr, | 
| 361 | 
        "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit\n", | 
| 362 | 
                                        progname, | 
| 363 | 
                                        (rel_min > 0) ? "relative " : "", | 
| 364 | 
                                        f1name, f2name, | 
| 365 | 
                                        sqrt(diff2sum/(double)nsum)); | 
| 366 | 
                return(0); | 
| 367 | 
        } | 
| 368 | 
        if (report >= REP_VERBOSE) | 
| 369 | 
                fprintf(stderr, | 
| 370 | 
                        "%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n", | 
| 371 | 
                                progname, (rel_min > 0) ? "relative " : "", | 
| 372 | 
                                f1name, f2name, sqrt(diff2sum/(double)nsum)); | 
| 373 | 
        return(1); | 
| 374 | 
} | 
| 375 | 
 | 
| 376 | 
/* Compare two inputs as generic binary files */ | 
| 377 | 
static int | 
| 378 | 
compare_binary() | 
| 379 | 
{ | 
| 380 | 
        if (report >= REP_VERBOSE) { | 
| 381 | 
                fputs(progname, stderr); | 
| 382 | 
                fputs(": comparing inputs as binary\n", stderr); | 
| 383 | 
        } | 
| 384 | 
        for ( ; ; ) {                           /* exact byte matching */ | 
| 385 | 
                int     c1 = getc(f1in); | 
| 386 | 
                int     c2 = getc(f2in); | 
| 387 | 
                if (c1 == EOF) { | 
| 388 | 
                        if (c2 == EOF) | 
| 389 | 
                                return(1);      /* success! */ | 
| 390 | 
                        if (report != REP_QUIET) { | 
| 391 | 
                                fputs(f1name, stderr); | 
| 392 | 
                                fputs(": Unexpected end-of-file\n", stderr); | 
| 393 | 
                        } | 
| 394 | 
                        return(0); | 
| 395 | 
                } | 
| 396 | 
                if (c2 == EOF) { | 
| 397 | 
                        if (report != REP_QUIET) { | 
| 398 | 
                                fputs(f2name, stderr); | 
| 399 | 
                                fputs(": Unexpected end-of-file\n", stderr); | 
| 400 | 
                        } | 
| 401 | 
                        return(0); | 
| 402 | 
                } | 
| 403 | 
                if (c1 != c2) | 
| 404 | 
                        break;                  /* quit and report difference */ | 
| 405 | 
        } | 
| 406 | 
        if (report == REP_QUIET) | 
| 407 | 
                return(0); | 
| 408 | 
        fprintf(stderr, "%s: binary files '%s' and '%s' differ at offset %ld|%ld\n", | 
| 409 | 
                        progname, f1name, f2name, ftell(f1in), ftell(f2in)); | 
| 410 | 
        return(0); | 
| 411 | 
} | 
| 412 | 
 | 
| 413 | 
/* Compare two inputs as generic text files */ | 
| 414 | 
static int | 
| 415 | 
compare_text() | 
| 416 | 
{ | 
| 417 | 
        char    l1buf[4096], l2buf[4096]; | 
| 418 | 
 | 
| 419 | 
        if (report >= REP_VERBOSE) { | 
| 420 | 
                fputs(progname, stderr); | 
| 421 | 
                fputs(": comparing inputs as ASCII text\n", stderr); | 
| 422 | 
        } | 
| 423 | 
                                                /* compare a line at a time */ | 
| 424 | 
        while (fgets(l1buf, sizeof(l1buf), f1in)) { | 
| 425 | 
                lin1cnt++; | 
| 426 | 
                if (!*sskip2(l1buf,0)) | 
| 427 | 
                        continue;               /* ignore empty lines */ | 
| 428 | 
                while (fgets(l2buf, sizeof(l2buf), f2in)) { | 
| 429 | 
                        lin2cnt++; | 
| 430 | 
                        if (*sskip2(l2buf,0)) | 
| 431 | 
                                break;          /* found other non-empty line */ | 
| 432 | 
                } | 
| 433 | 
                if (feof(f2in)) { | 
| 434 | 
                        if (report != REP_QUIET) { | 
| 435 | 
                                fputs(f2name, stderr); | 
| 436 | 
                                fputs(": Unexpected end-of-file\n", stderr); | 
| 437 | 
                        } | 
| 438 | 
                        return(0); | 
| 439 | 
                } | 
| 440 | 
                                                /* compare non-empty lines */ | 
| 441 | 
                if (!equiv_string(l1buf, l2buf)) { | 
| 442 | 
                        if (report != REP_QUIET) { | 
| 443 | 
                                fprintf(stderr, "%s: inputs '%s' and '%s' differ at line %d|%d\n", | 
| 444 | 
                                                progname, f1name, f2name, | 
| 445 | 
                                                lin1cnt, lin2cnt); | 
| 446 | 
                                if (report >= REP_VERBOSE) { | 
| 447 | 
                                        fputs("------------- Mismatch -------------\n", stderr); | 
| 448 | 
                                        fprintf(stderr, "%s(%d):\t%s", f1name, | 
| 449 | 
                                                        lin1cnt, l1buf); | 
| 450 | 
                                        fprintf(stderr, "%s(%d):\t%s", f2name, | 
| 451 | 
                                                        lin2cnt, l2buf); | 
| 452 | 
                                } | 
| 453 | 
                        } | 
| 454 | 
                        return(0); | 
| 455 | 
                } | 
| 456 | 
        } | 
| 457 | 
                                                /* check for EOF on input 2 */ | 
| 458 | 
        while (fgets(l2buf, sizeof(l2buf), f2in)) { | 
| 459 | 
                if (!*sskip2(l2buf,0)) | 
| 460 | 
                        continue; | 
| 461 | 
                if (report != REP_QUIET) { | 
| 462 | 
                        fputs(f1name, stderr); | 
| 463 | 
                        fputs(": Unexpected end-of-file\n", stderr); | 
| 464 | 
                } | 
| 465 | 
                return(0); | 
| 466 | 
        } | 
| 467 | 
        return(good_RMS());                     /* final check for reals */ | 
| 468 | 
} | 
| 469 | 
 | 
| 470 | 
/* Compare two inputs that are known to be RGBE or XYZE images */ | 
| 471 | 
static int | 
| 472 | 
compare_hdr() | 
| 473 | 
{ | 
| 474 | 
        RESOLU  rs1, rs2; | 
| 475 | 
        COLOR   *scan1, *scan2; | 
| 476 | 
        int     x, y; | 
| 477 | 
 | 
| 478 | 
        fgetsresolu(&rs1, f1in); | 
| 479 | 
        fgetsresolu(&rs2, f2in); | 
| 480 | 
        if (rs1.rt != rs2.rt) { | 
| 481 | 
                if (report != REP_QUIET) | 
| 482 | 
                        fprintf(stderr, | 
| 483 | 
                        "%s: Images '%s' and '%s' have different pixel ordering\n", | 
| 484 | 
                                        progname, f1name, f2name); | 
| 485 | 
                return(0); | 
| 486 | 
        } | 
| 487 | 
        if ((rs1.xr != rs2.xr) | (rs1.yr != rs2.yr)) { | 
| 488 | 
                if (report != REP_QUIET) | 
| 489 | 
                        fprintf(stderr, | 
| 490 | 
                        "%s: Images '%s' and '%s' are different sizes\n", | 
| 491 | 
                                        progname, f1name, f2name); | 
| 492 | 
                return(0); | 
| 493 | 
        } | 
| 494 | 
        scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR)); | 
| 495 | 
        scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR)); | 
| 496 | 
        if (!scan1 | !scan2) { | 
| 497 | 
                fprintf(stderr, "%s: out of memory in compare_hdr()\n", progname); | 
| 498 | 
                exit(2); | 
| 499 | 
        } | 
| 500 | 
        for (y = 0; y < numscans(&rs1); y++) { | 
| 501 | 
                if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) | | 
| 502 | 
                                (freadscan(scan2, scanlen(&rs2), f2in) < 0)) { | 
| 503 | 
                        if (report != REP_QUIET) | 
| 504 | 
                                fprintf(stderr, "%s: Unexpected end-of-file\n", | 
| 505 | 
                                                progname); | 
| 506 | 
                        free(scan1); | 
| 507 | 
                        free(scan2); | 
| 508 | 
                        return(0); | 
| 509 | 
                } | 
| 510 | 
                for (x = scanlen(&rs1); x--; ) { | 
| 511 | 
                        if (real_check(colval(scan1[x],RED), | 
| 512 | 
                                                colval(scan2[x],RED)) & | 
| 513 | 
                                        real_check(colval(scan1[x],GRN), | 
| 514 | 
                                                colval(scan2[x],GRN)) & | 
| 515 | 
                                        real_check(colval(scan1[x],BLU), | 
| 516 | 
                                                colval(scan2[x],BLU))) | 
| 517 | 
                                continue; | 
| 518 | 
                        if (report != REP_QUIET) { | 
| 519 | 
                                fprintf(stderr, | 
| 520 | 
                                "%s: pixels at scanline %d offset %d differ\n", | 
| 521 | 
                                        progname, y, x); | 
| 522 | 
                                if (report >= REP_VERBOSE) { | 
| 523 | 
                                        fprintf(stderr, "%s: (R,G,B)=(%g,%g,%g)\n", | 
| 524 | 
                                                f1name, colval(scan1[x],RED), | 
| 525 | 
                                                colval(scan1[x],GRN), | 
| 526 | 
                                                colval(scan1[x],BLU)); | 
| 527 | 
                                        fprintf(stderr, "%s: (R,G,B)=(%g,%g,%g)\n", | 
| 528 | 
                                                f2name, colval(scan2[x],RED), | 
| 529 | 
                                                colval(scan2[x],GRN), | 
| 530 | 
                                                colval(scan2[x],BLU)); | 
| 531 | 
                                } | 
| 532 | 
                        } | 
| 533 | 
                        free(scan1); | 
| 534 | 
                        free(scan2); | 
| 535 | 
                        return(0); | 
| 536 | 
                } | 
| 537 | 
        } | 
| 538 | 
        free(scan1); | 
| 539 | 
        free(scan2); | 
| 540 | 
        return(good_RMS());                     /* final check of RMS */ | 
| 541 | 
} | 
| 542 | 
 | 
| 543 | 
const char      nsuffix[10][3] = {              /* 1st, 2nd, 3rd, etc. */ | 
| 544 | 
                        "th","st","nd","rd","th","th","th","th","th","th" | 
| 545 | 
                }; | 
| 546 | 
 | 
| 547 | 
/* Compare two inputs that are known to be 32-bit floating-point data */ | 
| 548 | 
static int | 
| 549 | 
compare_float() | 
| 550 | 
{ | 
| 551 | 
        long    nread = 0; | 
| 552 | 
        float   f1, f2; | 
| 553 | 
 | 
| 554 | 
        while (getbinary(&f1, sizeof(f1), 1, f1in)) { | 
| 555 | 
                if (!getbinary(&f2, sizeof(f2), 1, f2in)) | 
| 556 | 
                        goto badeof; | 
| 557 | 
                ++nread; | 
| 558 | 
                if (real_check(f1, f2)) | 
| 559 | 
                        continue; | 
| 560 | 
                if (report != REP_QUIET) | 
| 561 | 
                        fprintf(stderr, "%s: %ld%s float values differ\n", | 
| 562 | 
                                        progname, nread, nsuffix[nread%10]); | 
| 563 | 
                return(0); | 
| 564 | 
        } | 
| 565 | 
        if (!getbinary(&f2, sizeof(f2), 1, f2in)) | 
| 566 | 
                return(good_RMS());             /* final check of RMS */ | 
| 567 | 
badeof: | 
| 568 | 
        if (report != REP_QUIET) | 
| 569 | 
                fprintf(stderr, "%s: Unexpected end-of-file\n", progname); | 
| 570 | 
        return(0); | 
| 571 | 
} | 
| 572 | 
 | 
| 573 | 
/* Compare two inputs that are known to be 64-bit floating-point data */ | 
| 574 | 
static int | 
| 575 | 
compare_double() | 
| 576 | 
{ | 
| 577 | 
        long    nread = 0; | 
| 578 | 
        double  f1, f2; | 
| 579 | 
 | 
| 580 | 
        while (getbinary(&f1, sizeof(f1), 1, f1in)) { | 
| 581 | 
                if (!getbinary(&f2, sizeof(f2), 1, f2in)) | 
| 582 | 
                        goto badeof; | 
| 583 | 
                ++nread; | 
| 584 | 
                if (real_check(f1, f2)) | 
| 585 | 
                        continue; | 
| 586 | 
                if (report != REP_QUIET) | 
| 587 | 
                        fprintf(stderr, "%s: %ld%s float values differ\n", | 
| 588 | 
                                        progname, nread, nsuffix[nread%10]); | 
| 589 | 
                return(0); | 
| 590 | 
        } | 
| 591 | 
        if (!getbinary(&f2, sizeof(f2), 1, f2in)) | 
| 592 | 
                return(good_RMS());             /* final check of RMS */ | 
| 593 | 
badeof: | 
| 594 | 
        if (report != REP_QUIET) | 
| 595 | 
                fprintf(stderr, "%s: Unexpected end-of-file\n", progname); | 
| 596 | 
        return(0); | 
| 597 | 
} | 
| 598 | 
 | 
| 599 | 
/* Compare two Radiance files for equivalence */ | 
| 600 | 
int | 
| 601 | 
main(int argc, char *argv[]) | 
| 602 | 
{ | 
| 603 | 
        int     typ1, typ2; | 
| 604 | 
        int     a; | 
| 605 | 
 | 
| 606 | 
        progname = argv[0]; | 
| 607 | 
        for (a = 1; a < argc && argv[a][0] == '-'; a++) { | 
| 608 | 
                switch (argv[a][1]) { | 
| 609 | 
                case 'h':                       /* ignore header info. */ | 
| 610 | 
                        ign_header = !ign_header; | 
| 611 | 
                        continue; | 
| 612 | 
                case 's':                       /* silent operation */ | 
| 613 | 
                        report = REP_QUIET; | 
| 614 | 
                        continue; | 
| 615 | 
                case 'w':                       /* turn off warnings */ | 
| 616 | 
                        if (report > REP_ERROR) | 
| 617 | 
                                report = REP_ERROR; | 
| 618 | 
                        continue; | 
| 619 | 
                case 'v':                       /* turn on verbose mode */ | 
| 620 | 
                        report = REP_VERBOSE; | 
| 621 | 
                        continue; | 
| 622 | 
                case 'm':                       /* maximum epsilon */ | 
| 623 | 
                        max_lim = atof(argv[++a]); | 
| 624 | 
                        continue; | 
| 625 | 
                case 'r': | 
| 626 | 
                        if (argv[a][2] == 'e')          /* relative difference */ | 
| 627 | 
                                rel_min = atof(argv[++a]); | 
| 628 | 
                        else if (argv[a][2] == 'm')     /* RMS limit */ | 
| 629 | 
                                rms_lim = atof(argv[++a]); | 
| 630 | 
                        else | 
| 631 | 
                                usage(); | 
| 632 | 
                        continue; | 
| 633 | 
                case '\0':                      /* first file from stdin */ | 
| 634 | 
                        f1in = stdin; | 
| 635 | 
                        f1name = stdin_name; | 
| 636 | 
                        break; | 
| 637 | 
                default: | 
| 638 | 
                        usage(); | 
| 639 | 
                } | 
| 640 | 
                break; | 
| 641 | 
        } | 
| 642 | 
        if (a != argc-2) | 
| 643 | 
                usage(); | 
| 644 | 
        if (!f1name) f1name = argv[a]; | 
| 645 | 
        if (!f2name) f2name = argv[a+1]; | 
| 646 | 
 | 
| 647 | 
        if (!strcmp(f1name, f2name)) {          /* inputs are same? */ | 
| 648 | 
                if (report >= REP_WARN) | 
| 649 | 
                        fprintf(stderr, "%s: warning - identical inputs given\n", | 
| 650 | 
                                        progname); | 
| 651 | 
                return(0); | 
| 652 | 
        } | 
| 653 | 
                                                /* open inputs */ | 
| 654 | 
        SET_FILE_BINARY(stdin);                 /* in case we're using it */ | 
| 655 | 
        if (!f1in && !(f1in = fopen(f1name, "rb"))) { | 
| 656 | 
                fprintf(stderr, "%s: cannot open for reading\n", f1name); | 
| 657 | 
                return(1); | 
| 658 | 
        } | 
| 659 | 
        if (!strcmp(f2name, "-")) { | 
| 660 | 
                f2in = stdin; | 
| 661 | 
                f2name = stdin_name; | 
| 662 | 
        } else if (!(f2in = fopen(f2name, "rb"))) { | 
| 663 | 
                fprintf(stderr, "%s: cannot open for reading\n", f2name); | 
| 664 | 
                return(1); | 
| 665 | 
        } | 
| 666 | 
                                                /* load headers */ | 
| 667 | 
        if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0) | 
| 668 | 
                return(1); | 
| 669 | 
        if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0) | 
| 670 | 
                return(1); | 
| 671 | 
        if (typ1 != typ2) { | 
| 672 | 
                if (report != REP_QUIET) | 
| 673 | 
                        fprintf(stderr, "%s: '%s' is %s and '%s' is %s\n", | 
| 674 | 
                                        progname, f1name, file_type[typ1], | 
| 675 | 
                                        f2name, file_type[typ2]); | 
| 676 | 
                return(1); | 
| 677 | 
        } | 
| 678 | 
        ign_header |= !has_header(typ1);        /* check headers if indicated */ | 
| 679 | 
        if (!ign_header && !headers_match(&hdr1, &hdr2)) | 
| 680 | 
                return(1); | 
| 681 | 
        lu_done(&hdr1); lu_done(&hdr2); | 
| 682 | 
        if (!ign_header & (report >= REP_WARN)) { | 
| 683 | 
                if (typ1 == TYP_UNKNOWN) | 
| 684 | 
                        fprintf(stderr, "%s: warning - unrecognized format, comparing as binary\n", | 
| 685 | 
                                        progname); | 
| 686 | 
                if (lin1cnt != lin2cnt) | 
| 687 | 
                        fprintf(stderr, "%s: warning - headers are different lengths\n", | 
| 688 | 
                                        progname); | 
| 689 | 
        } | 
| 690 | 
        if (report >= REP_VERBOSE) | 
| 691 | 
                fprintf(stderr, "%s: input file type is %s\n", | 
| 692 | 
                                progname, file_type[typ1]); | 
| 693 | 
 | 
| 694 | 
        switch (typ1) {                         /* compare based on type */ | 
| 695 | 
        case TYP_BINARY: | 
| 696 | 
        case TYP_TMESH: | 
| 697 | 
        case TYP_OCTREE: | 
| 698 | 
        case TYP_RBFMESH: | 
| 699 | 
        case TYP_UNKNOWN: | 
| 700 | 
                return( !compare_binary() ); | 
| 701 | 
        case TYP_TEXT: | 
| 702 | 
        case TYP_ASCII: | 
| 703 | 
                return( !compare_text() ); | 
| 704 | 
        case TYP_RGBE: | 
| 705 | 
        case TYP_XYZE: | 
| 706 | 
                return( !compare_hdr() ); | 
| 707 | 
        case TYP_FLOAT: | 
| 708 | 
                return( !compare_float() ); | 
| 709 | 
        case TYP_DOUBLE: | 
| 710 | 
                return( !compare_double() ); | 
| 711 | 
        } | 
| 712 | 
        return(1); | 
| 713 | 
} |