| 1 | #ifndef lint | 
| 2 | static const char RCSid[] = "$Id: radcompare.c,v 2.24 2020/06/30 22:53:05 greg Exp $"; | 
| 3 | #endif | 
| 4 | /* | 
| 5 | * Compare Radiance files for significant differences | 
| 6 | * | 
| 7 | *      G. Ward | 
| 8 | */ | 
| 9 |  | 
| 10 | #include <stdlib.h> | 
| 11 | #include <ctype.h> | 
| 12 | #include "rtmath.h" | 
| 13 | #include "platform.h" | 
| 14 | #include "rtio.h" | 
| 15 | #include "resolu.h" | 
| 16 | #include "color.h" | 
| 17 | #include "depthcodec.h" | 
| 18 | #include "normcodec.h" | 
| 19 | #include "lookup.h" | 
| 20 | /* Reporting levels */ | 
| 21 | #define REP_QUIET       0       /* no reporting */ | 
| 22 | #define REP_ERROR       1       /* report errors only */ | 
| 23 | #define REP_WARN        2       /* report warnings as well */ | 
| 24 | #define REP_VERBOSE     3       /* verbose reporting */ | 
| 25 |  | 
| 26 | int     report = REP_WARN;      /* reporting level */ | 
| 27 |  | 
| 28 | int     ign_header = 0;         /* ignore header differences? */ | 
| 29 |  | 
| 30 | double  rel_min = 1e-5;         /* positive for relative comparisons */ | 
| 31 |  | 
| 32 | double  rms_lim = 0.01;         /* RMS difference limit */ | 
| 33 |  | 
| 34 | double  max_lim = 0.25;         /* difference limit if non-negative */ | 
| 35 |  | 
| 36 | int     lin1cnt=0, lin2cnt=0;   /* file line position */ | 
| 37 |  | 
| 38 | const char      nsuffix[10][3] = {              /* 1st, 2nd, 3rd, etc. */ | 
| 39 | "th","st","nd","rd","th","th","th","th","th","th" | 
| 40 | }; | 
| 41 | #define num_sfx(n)      nsuffix[(n)%10] | 
| 42 |  | 
| 43 | /* file types */ | 
| 44 | const char      *file_type[] = { | 
| 45 | "Unrecognized", | 
| 46 | "TEXT_generic", | 
| 47 | "ascii", | 
| 48 | COLRFMT, | 
| 49 | CIEFMT, | 
| 50 | DEPTH16FMT, | 
| 51 | NORMAL32FMT, | 
| 52 | "float", | 
| 53 | "double", | 
| 54 | "BSDF_RBFmesh", | 
| 55 | "Radiance_octree", | 
| 56 | "Radiance_tmesh", | 
| 57 | "8-bit_indexed_name", | 
| 58 | "16-bit_indexed_name", | 
| 59 | "24-bit_indexed_name", | 
| 60 | "BINARY_unknown", | 
| 61 | NULL    /* terminator */ | 
| 62 | }; | 
| 63 | /* keep consistent with above */ | 
| 64 | enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, | 
| 65 | TYP_DEPTH, TYP_NORM, TYP_FLOAT, TYP_DOUBLE, | 
| 66 | TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, | 
| 67 | TYP_ID8, TYP_ID16, TYP_ID24, TYP_BINARY}; | 
| 68 |  | 
| 69 | #define has_header(t)   (!( 1L<<(t) & (1L<<TYP_TEXT | 1L<<TYP_BINARY) )) | 
| 70 |  | 
| 71 | /* header variables to always ignore */ | 
| 72 | const char      *hdr_ignkey[] = { | 
| 73 | "SOFTWARE", | 
| 74 | "CAPDATE", | 
| 75 | "GMT", | 
| 76 | "FRAME", | 
| 77 | NULL    /* terminator */ | 
| 78 | }; | 
| 79 | /* header variable settings */ | 
| 80 | LUTAB   hdr1 = LU_SINIT(free,free); | 
| 81 | LUTAB   hdr2 = LU_SINIT(free,free); | 
| 82 |  | 
| 83 | /* advance appropriate file line count */ | 
| 84 | #define adv_linecnt(htp)        (lin1cnt += (htp == &hdr1), \ | 
| 85 | lin2cnt += (htp == &hdr2)) | 
| 86 |  | 
| 87 | typedef struct {                /* dynamic line buffer */ | 
| 88 | char    *str; | 
| 89 | int     len; | 
| 90 | int     siz; | 
| 91 | } LINEBUF; | 
| 92 |  | 
| 93 | #define init_line(bp)   ((bp)->str = NULL, (bp)->siz = 0) | 
| 94 | /* 100 MByte limit on line buffer */ | 
| 95 | #define MAXBUF          (100L<<20) | 
| 96 |  | 
| 97 | /* input files */ | 
| 98 | char            *progname = NULL; | 
| 99 | const char      stdin_name[] = "<stdin>"; | 
| 100 | const char      *f1name=NULL, *f2name=NULL; | 
| 101 | FILE            *f1in=NULL, *f2in=NULL; | 
| 102 | int             f1swap=0, f2swap=0; | 
| 103 |  | 
| 104 | /* running real differences */ | 
| 105 | double  diff2sum = 0; | 
| 106 | long    nsum = 0; | 
| 107 |  | 
| 108 | /* Report usage and exit */ | 
| 109 | static void | 
| 110 | usage() | 
| 111 | { | 
| 112 | fputs("Usage: ", stderr); | 
| 113 | fputs(progname, stderr); | 
| 114 | fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n", | 
| 115 | stderr); | 
| 116 | exit(2); | 
| 117 | } | 
| 118 |  | 
| 119 | /* Read a text line, increasing buffer size as necessary */ | 
| 120 | static int | 
| 121 | read_line(LINEBUF *bp, FILE *fp) | 
| 122 | { | 
| 123 | static int      doneWarn = 0; | 
| 124 |  | 
| 125 | bp->len = 0; | 
| 126 | if (!bp->str) { | 
| 127 | bp->str = (char *)malloc(bp->siz = 512); | 
| 128 | if (!bp->str) | 
| 129 | goto memerr; | 
| 130 | } | 
| 131 | while (fgets(bp->str + bp->len, bp->siz - bp->len, fp)) { | 
| 132 | bp->len += strlen(bp->str + bp->len); | 
| 133 | if (bp->str[bp->len-1] == '\n') | 
| 134 | break;          /* found EOL */ | 
| 135 | if (bp->len < bp->siz - 4) | 
| 136 | continue;       /* at EOF? */ | 
| 137 | if (bp->siz >= MAXBUF) { | 
| 138 | if ((report >= REP_WARN) & !doneWarn) { | 
| 139 | fprintf(stderr, | 
| 140 | "%s: warning - input line(s) past %ld MByte limit\n", | 
| 141 | progname, MAXBUF>>20); | 
| 142 | doneWarn++; | 
| 143 | } | 
| 144 | break;          /* return MAXBUF partial line */ | 
| 145 | } | 
| 146 | if ((bp->siz += bp->siz/2) > MAXBUF) | 
| 147 | bp->siz = MAXBUF; | 
| 148 | bp->str = (char *)realloc(bp->str, bp->siz); | 
| 149 | if (!bp->str) | 
| 150 | goto memerr; | 
| 151 | } | 
| 152 | return(bp->len); | 
| 153 | memerr: | 
| 154 | fprintf(stderr, | 
| 155 | "%s: out of memory in read_line() allocating %d byte buffer\n", | 
| 156 | progname, bp->siz); | 
| 157 | exit(2); | 
| 158 | } | 
| 159 |  | 
| 160 | /* Free line buffer */ | 
| 161 | static void | 
| 162 | free_line(LINEBUF *bp) | 
| 163 | { | 
| 164 | if (bp->str) free(bp->str); | 
| 165 | init_line(bp); | 
| 166 | } | 
| 167 |  | 
| 168 | /* Get type ID from name (or 0 if not found) */ | 
| 169 | static int | 
| 170 | xlate_type(const char *nm) | 
| 171 | { | 
| 172 | int     i; | 
| 173 |  | 
| 174 | if (!nm || !*nm) | 
| 175 | return(TYP_UNKNOWN); | 
| 176 | for (i = 1; file_type[i]; i++) | 
| 177 | if (!strcmp(nm, file_type[i])) | 
| 178 | return(i); | 
| 179 | return(TYP_UNKNOWN); | 
| 180 | } | 
| 181 |  | 
| 182 | /* Compare real values and keep track of differences */ | 
| 183 | static int | 
| 184 | real_check(double r1, double r2) | 
| 185 | { | 
| 186 | double  diff2 = (r1 - r2)*(r1 - r2); | 
| 187 |  | 
| 188 | if (rel_min > 0) {      /* doing relative differences? */ | 
| 189 | double  av2 = .25*(r1*r1 + 2.*fabs(r1*r2) + r2*r2); | 
| 190 | if (av2 > rel_min*rel_min) | 
| 191 | diff2 /= av2; | 
| 192 | } | 
| 193 | if (max_lim >= 0 && diff2 > max_lim*max_lim) { | 
| 194 | if (report != REP_QUIET) | 
| 195 | printf( | 
| 196 | "%s: %sdifference between %.8g and %.8g exceeds epsilon of %.8g\n", | 
| 197 | progname, | 
| 198 | (rel_min > 0) ? "relative " : "", | 
| 199 | r1, r2, max_lim); | 
| 200 | return(0); | 
| 201 | } | 
| 202 | diff2sum += diff2; | 
| 203 | nsum++; | 
| 204 | return(1); | 
| 205 | } | 
| 206 |  | 
| 207 | /* Compare two color values for equivalence */ | 
| 208 | static int | 
| 209 | color_check(COLOR c1, COLOR c2) | 
| 210 | { | 
| 211 | int     p; | 
| 212 |  | 
| 213 | if (!real_check((colval(c1,RED)+colval(c1,GRN)+colval(c1,BLU))*(1./3.), | 
| 214 | (colval(c2,RED)+colval(c2,GRN)+colval(c2,BLU))*(1./3.))) | 
| 215 | return(0); | 
| 216 |  | 
| 217 | p = (colval(c1,GRN) > colval(c1,RED)) ? GRN : RED; | 
| 218 | if (colval(c1,BLU) > colval(c1,p)) p = BLU; | 
| 219 |  | 
| 220 | return(real_check(colval(c1,p), colval(c2,p))); | 
| 221 | } | 
| 222 |  | 
| 223 | /* Compare two normal directions for equivalence */ | 
| 224 | static int | 
| 225 | norm_check(FVECT nv1, FVECT nv2) | 
| 226 | { | 
| 227 | double  max2 = nv1[2]*nv2[2]; | 
| 228 | int     imax = 2; | 
| 229 | int     i = 2; | 
| 230 | /* identify largest component */ | 
| 231 | while (i--) { | 
| 232 | double  tm2 = nv1[i]*nv2[i]; | 
| 233 | if (tm2 > max2) { | 
| 234 | imax = i; | 
| 235 | max2 = tm2; | 
| 236 | } | 
| 237 | } | 
| 238 | i = 3;                          /* compare smaller components */ | 
| 239 | while (i--) { | 
| 240 | if (i == imax) | 
| 241 | continue; | 
| 242 | if (!real_check(nv1[i], nv2[i])) | 
| 243 | return(0); | 
| 244 | } | 
| 245 | return(1); | 
| 246 | } | 
| 247 |  | 
| 248 | /* Compare two strings for equivalence */ | 
| 249 | static int | 
| 250 | equiv_string(char *s1, char *s2) | 
| 251 | { | 
| 252 | #define CLS_STR         0 | 
| 253 | #define CLS_INT         1 | 
| 254 | #define CLS_FLT         2 | 
| 255 | /* skip whitespace at beginning */ | 
| 256 | while (isspace(*s1)) s1++; | 
| 257 | while (isspace(*s2)) s2++; | 
| 258 | while (*s1) {           /* check each word */ | 
| 259 | int     inquote; | 
| 260 | if (!*s2)       /* unexpected EOL in s2? */ | 
| 261 | return(0); | 
| 262 | inquote = *s1; | 
| 263 | if ((inquote != '\'') & (inquote != '"')) | 
| 264 | inquote = 0; | 
| 265 | if (inquote) {  /* quoted text must match exactly */ | 
| 266 | if (*s1++ != *s2++) | 
| 267 | return(0); | 
| 268 | while (*s1 != inquote) { | 
| 269 | if (!*s1) | 
| 270 | return(0); | 
| 271 | if (*s1++ != *s2++) | 
| 272 | return(0); | 
| 273 | } | 
| 274 | s1++; | 
| 275 | if (*s2++ != inquote) | 
| 276 | return(0); | 
| 277 | } else {        /* else classify word type */ | 
| 278 | char    *s1s = s1; | 
| 279 | char    *s2s = s2; | 
| 280 | int     cls = CLS_STR; | 
| 281 | s1 = sskip(s1); | 
| 282 | s2 = sskip(s2); | 
| 283 | if (iskip(s1s) == s1) { | 
| 284 | if (iskip(s2s) == s2) | 
| 285 | cls = CLS_INT; | 
| 286 | else if (fskip(s2s) == s2) | 
| 287 | cls = CLS_FLT; | 
| 288 | } else if (fskip(s1s) == s1) { | 
| 289 | if (fskip(s2s) != s2) | 
| 290 | return(0); | 
| 291 | cls = CLS_FLT; | 
| 292 | } | 
| 293 | switch (cls) { | 
| 294 | case CLS_INT:           /* strncmp() faster */ | 
| 295 | case CLS_STR: | 
| 296 | if (s1 - s1s != s2 - s2s) | 
| 297 | return(0); | 
| 298 | if (strncmp(s1s, s2s, s1 - s1s)) | 
| 299 | return(0); | 
| 300 | break; | 
| 301 | case CLS_FLT: | 
| 302 | if (!real_check(atof(s1s), atof(s2s))) | 
| 303 | return(0); | 
| 304 | break; | 
| 305 | } | 
| 306 | } | 
| 307 | while (isspace(*s1)) s1++; | 
| 308 | while (isspace(*s2)) s2++; | 
| 309 | } | 
| 310 | return(!*s2);           /* match if we reached the end of s2, too */ | 
| 311 | #undef CLS_STR | 
| 312 | #undef CLS_INT | 
| 313 | #undef CLS_FLT | 
| 314 | } | 
| 315 |  | 
| 316 | /* Check if string is var=value pair and set if not in ignore list */ | 
| 317 | static int | 
| 318 | setheadvar(char *val, void *p) | 
| 319 | { | 
| 320 | char    newval[128]; | 
| 321 | LUTAB   *htp = (LUTAB *)p; | 
| 322 | LUENT   *tep; | 
| 323 | char    *key; | 
| 324 | int     kln, vln; | 
| 325 | int     n; | 
| 326 |  | 
| 327 | adv_linecnt(htp);       /* side-effect is to count lines */ | 
| 328 | if (!isalpha(*val))     /* key must start line */ | 
| 329 | return(0); | 
| 330 | /* check if we need to swap binary data */ | 
| 331 | if ((n = isbigendian(val)) >= 0) { | 
| 332 | if (nativebigendian() == n) | 
| 333 | return(0); | 
| 334 | f1swap += (htp == &hdr1); | 
| 335 | f2swap += (htp == &hdr2); | 
| 336 | return(0); | 
| 337 | } | 
| 338 | key = val++; | 
| 339 | while (*val && !isspace(*val) & (*val != '=')) | 
| 340 | val++; | 
| 341 | kln = val - key; | 
| 342 | while (isspace(*val))   /* check for value */ | 
| 343 | *val++ = '\0'; | 
| 344 | if (*val != '=') | 
| 345 | return(0); | 
| 346 | *val++ = '\0'; | 
| 347 | while (isspace(*val)) | 
| 348 | val++; | 
| 349 | if (!*val)              /* nothing set? */ | 
| 350 | return(0); | 
| 351 | /* check if key to ignore */ | 
| 352 | for (n = 0; hdr_ignkey[n]; n++) | 
| 353 | if (!strcmp(key, hdr_ignkey[n])) | 
| 354 | return(0); | 
| 355 | vln = strlen(val);      /* eliminate space and newline at end */ | 
| 356 | while (isspace(val[--vln])) | 
| 357 | ; | 
| 358 | val[++vln] = '\0'; | 
| 359 | if (!(tep = lu_find(htp, key))) | 
| 360 | return(-1);     /* memory allocation error */ | 
| 361 | if (!tep->key) | 
| 362 | tep->key = strcpy(malloc(kln+1), key); | 
| 363 | if (tep->data) {        /* check for special cases */ | 
| 364 | if (!strcmp(key, "EXPOSURE")) { | 
| 365 | sprintf(newval, "%f", atof(tep->data)*atof(val)); | 
| 366 | vln = strlen(val = newval); | 
| 367 | } | 
| 368 | free(tep->data); | 
| 369 | } | 
| 370 | tep->data = strcpy(malloc(vln+1), val); | 
| 371 | return(1); | 
| 372 | } | 
| 373 |  | 
| 374 | /* Lookup correspondent in other header */ | 
| 375 | static int | 
| 376 | match_val(const LUENT *ep1, void *p2) | 
| 377 | { | 
| 378 | const LUENT     *ep2 = lu_find((LUTAB *)p2, ep1->key); | 
| 379 | if (!ep2 || !ep2->data) { | 
| 380 | if (report != REP_QUIET) | 
| 381 | printf("%s: variable '%s' missing in '%s'\n", | 
| 382 | progname, ep1->key, f2name); | 
| 383 | return(-1); | 
| 384 | } | 
| 385 | if (!equiv_string((char *)ep1->data, (char *)ep2->data)) { | 
| 386 | if (report != REP_QUIET) { | 
| 387 | printf("%s: header variable '%s' has different values\n", | 
| 388 | progname, ep1->key); | 
| 389 | if (report >= REP_VERBOSE) { | 
| 390 | printf("%s: %s=%s\n", f1name, | 
| 391 | ep1->key, (char *)ep1->data); | 
| 392 | printf("%s: %s=%s\n", f2name, | 
| 393 | ep2->key, (char *)ep2->data); | 
| 394 | } | 
| 395 | } | 
| 396 | return(-1); | 
| 397 | } | 
| 398 | return(1);              /* good match */ | 
| 399 | } | 
| 400 |  | 
| 401 | /* Compare two sets of header variables */ | 
| 402 | static int | 
| 403 | headers_match() | 
| 404 | { | 
| 405 | int     ne = lu_doall(&hdr1, match_val, &hdr2); | 
| 406 | if (ne < 0) | 
| 407 | return(0);      /* something didn't match! */ | 
| 408 | /* non-fatal if second header has extra */ | 
| 409 | if (report >= REP_WARN && (ne = lu_doall(&hdr2, NULL, NULL) - ne)) | 
| 410 | printf("%s: warning - '%s' has %d extra header setting(s)\n", | 
| 411 | progname, f2name, ne); | 
| 412 | return(1);              /* good match */ | 
| 413 | } | 
| 414 |  | 
| 415 | /* Check generic input to determine if it is binary, -1 on error */ | 
| 416 | static int | 
| 417 | input_is_binary(FILE *fin) | 
| 418 | { | 
| 419 | int     n = 0; | 
| 420 | int     c = 0; | 
| 421 |  | 
| 422 | while ((c = getc(fin)) != EOF) { | 
| 423 | ++n; | 
| 424 | if (!c | (c > 127)) | 
| 425 | break;                  /* non-ascii character */ | 
| 426 | if (n >= 10240) | 
| 427 | break;                  /* enough to be confident */ | 
| 428 | } | 
| 429 | if (!n) | 
| 430 | return(-1);                     /* first read failed */ | 
| 431 | if (fseek(fin, 0L, 0) < 0) | 
| 432 | return(-1);                     /* rewind failed */ | 
| 433 | return(!c | (c > 127)); | 
| 434 | } | 
| 435 |  | 
| 436 | /* Identify and return data type from header (if one) */ | 
| 437 | static int | 
| 438 | identify_type(const char *name, FILE *fin, LUTAB *htp) | 
| 439 | { | 
| 440 | extern const char       HDRSTR[]; | 
| 441 | int                     c; | 
| 442 | /* check magic header start */ | 
| 443 | if ((c = getc(fin)) != HDRSTR[0]) { | 
| 444 | if (c == EOF) goto badeof; | 
| 445 | ungetc(c, fin); | 
| 446 | c = 0; | 
| 447 | } else if ((c = getc(fin)) != HDRSTR[1]) { | 
| 448 | if (c == EOF) goto badeof; | 
| 449 | ungetc(c, fin); ungetc(HDRSTR[0], fin); | 
| 450 | c = 0; | 
| 451 | } | 
| 452 | if (c) {                                /* appears to have a header */ | 
| 453 | char    sbuf[32]; | 
| 454 | if (!fgets(sbuf, sizeof(sbuf), fin)) | 
| 455 | goto badeof; | 
| 456 | adv_linecnt(htp);               /* for #?ID string */ | 
| 457 | if (report >= REP_WARN && strncmp(sbuf, "RADIANCE", 8)) { | 
| 458 | fputs(name, stdout); | 
| 459 | fputs(": warning - unexpected header ID: ", stdout); | 
| 460 | fputs(sbuf, stdout); | 
| 461 | } | 
| 462 | if (getheader(fin, setheadvar, htp) < 0) { | 
| 463 | fputs(name, stderr); | 
| 464 | fputs(": unknown error reading header\n", stderr); | 
| 465 | return(-1); | 
| 466 | } | 
| 467 | adv_linecnt(htp);               /* for trailing emtpy line */ | 
| 468 | return(xlate_type((const char *)lu_find(htp,"FORMAT")->data)); | 
| 469 | } | 
| 470 | c = input_is_binary(fin);               /* else peek to see if binary */ | 
| 471 | if (c < 0) { | 
| 472 | fputs(name, stderr); | 
| 473 | fputs(": read/seek error\n", stderr); | 
| 474 | return(-1); | 
| 475 | } | 
| 476 | if (c) | 
| 477 | return(TYP_BINARY); | 
| 478 | return(TYP_TEXT); | 
| 479 | badeof: | 
| 480 | if (report != REP_QUIET) { | 
| 481 | fputs(name, stdout); | 
| 482 | fputs(": unexpected end-of-file\n", stdout); | 
| 483 | } | 
| 484 | return(-1); | 
| 485 | } | 
| 486 |  | 
| 487 | /* Check that overall RMS error is below threshold */ | 
| 488 | static int | 
| 489 | good_RMS() | 
| 490 | { | 
| 491 | if (!nsum) | 
| 492 | return(1); | 
| 493 | if (diff2sum/(double)nsum > rms_lim*rms_lim) { | 
| 494 | if (report != REP_QUIET) | 
| 495 | printf( | 
| 496 | "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit of %.5g\n", | 
| 497 | progname, | 
| 498 | (rel_min > 0) ? "relative " : "", | 
| 499 | f1name, f2name, | 
| 500 | sqrt(diff2sum/(double)nsum), rms_lim); | 
| 501 | return(0); | 
| 502 | } | 
| 503 | if (report >= REP_VERBOSE) | 
| 504 | printf("%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n", | 
| 505 | progname, (rel_min > 0) ? "relative " : "", | 
| 506 | f1name, f2name, sqrt(diff2sum/(double)nsum)); | 
| 507 | return(1); | 
| 508 | } | 
| 509 |  | 
| 510 | /* Compare two inputs as generic binary files */ | 
| 511 | static int | 
| 512 | compare_binary() | 
| 513 | { | 
| 514 | int     c1=0, c2=0; | 
| 515 |  | 
| 516 | if (report >= REP_VERBOSE) { | 
| 517 | fputs(progname, stdout); | 
| 518 | fputs(": comparing inputs as binary\n", stdout); | 
| 519 | } | 
| 520 | for ( ; ; ) {                           /* exact byte matching */ | 
| 521 | c1 = getc(f1in); | 
| 522 | c2 = getc(f2in); | 
| 523 | if (c1 == EOF) { | 
| 524 | if (c2 == EOF) | 
| 525 | return(1);      /* success! */ | 
| 526 | if (report != REP_QUIET) { | 
| 527 | fputs(f1name, stdout); | 
| 528 | fputs(": unexpected end-of-file\n", stdout); | 
| 529 | } | 
| 530 | return(0); | 
| 531 | } | 
| 532 | if (c2 == EOF) { | 
| 533 | if (report != REP_QUIET) { | 
| 534 | fputs(f2name, stdout); | 
| 535 | fputs(": unexpected end-of-file\n", stdout); | 
| 536 | } | 
| 537 | return(0); | 
| 538 | } | 
| 539 | if (c1 != c2) | 
| 540 | break;                  /* quit and report difference */ | 
| 541 | } | 
| 542 | if (report == REP_QUIET) | 
| 543 | return(0); | 
| 544 | printf("%s: binary files '%s' and '%s' differ at byte offset %ld|%ld\n", | 
| 545 | progname, f1name, f2name, ftell(f1in), ftell(f2in)); | 
| 546 | if (report >= REP_VERBOSE) | 
| 547 | printf("%s: byte in '%s' is 0x%X, byte in '%s' is 0x%X\n", | 
| 548 | progname, f1name, c1, f2name, c2); | 
| 549 | return(0); | 
| 550 | } | 
| 551 |  | 
| 552 | /* Compare two inputs as generic text files */ | 
| 553 | static int | 
| 554 | compare_text() | 
| 555 | { | 
| 556 | LINEBUF l1buf, l2buf; | 
| 557 |  | 
| 558 | if (report >= REP_VERBOSE) { | 
| 559 | fputs(progname, stdout); | 
| 560 | fputs(": comparing inputs as ASCII text\n", stdout); | 
| 561 | } | 
| 562 | SET_FILE_TEXT(f1in);                    /* originally set to binary */ | 
| 563 | SET_FILE_TEXT(f2in); | 
| 564 | init_line(&l1buf); init_line(&l2buf);   /* compare a line at a time */ | 
| 565 | while (read_line(&l1buf, f1in)) { | 
| 566 | lin1cnt++; | 
| 567 | if (!*sskip2(l1buf.str,0)) | 
| 568 | continue;               /* ignore empty lines */ | 
| 569 |  | 
| 570 | while (read_line(&l2buf, f2in)) { | 
| 571 | lin2cnt++; | 
| 572 | if (*sskip2(l2buf.str,0)) | 
| 573 | break;          /* found other non-empty line */ | 
| 574 | } | 
| 575 | if (!l2buf.len) {               /* input 2 EOF? */ | 
| 576 | if (report != REP_QUIET) { | 
| 577 | fputs(f2name, stdout); | 
| 578 | fputs(": unexpected end-of-file\n", stdout); | 
| 579 | } | 
| 580 | free_line(&l1buf); free_line(&l2buf); | 
| 581 | return(0); | 
| 582 | } | 
| 583 | /* compare non-empty lines */ | 
| 584 | if (!equiv_string(l1buf.str, l2buf.str)) { | 
| 585 | if (report != REP_QUIET) { | 
| 586 | printf("%s: inputs '%s' and '%s' differ at line %d|%d\n", | 
| 587 | progname, f1name, f2name, | 
| 588 | lin1cnt, lin2cnt); | 
| 589 | if ( report >= REP_VERBOSE && | 
| 590 | (l1buf.len < 256) & | 
| 591 | (l2buf.len < 256) ) { | 
| 592 | fputs("------------- Mismatch -------------\n", stdout); | 
| 593 | printf("%s@%d:\t%s", f1name, | 
| 594 | lin1cnt, l1buf.str); | 
| 595 | printf("%s@%d:\t%s", f2name, | 
| 596 | lin2cnt, l2buf.str); | 
| 597 | } | 
| 598 | } | 
| 599 | free_line(&l1buf); free_line(&l2buf); | 
| 600 | return(0); | 
| 601 | } | 
| 602 | } | 
| 603 | free_line(&l1buf);                      /* check for EOF on input 2 */ | 
| 604 | while (read_line(&l2buf, f2in)) { | 
| 605 | if (!*sskip2(l2buf.str,0)) | 
| 606 | continue; | 
| 607 | if (report != REP_QUIET) { | 
| 608 | fputs(f1name, stdout); | 
| 609 | fputs(": unexpected end-of-file\n", stdout); | 
| 610 | } | 
| 611 | free_line(&l2buf); | 
| 612 | return(0); | 
| 613 | } | 
| 614 | free_line(&l2buf); | 
| 615 | return(good_RMS());                     /* final check for reals */ | 
| 616 | } | 
| 617 |  | 
| 618 | /* Check image/map resolutions */ | 
| 619 | static int | 
| 620 | check_resolu(const char *class, RESOLU *r1p, RESOLU *r2p) | 
| 621 | { | 
| 622 | if (r1p->rt != r2p->rt) { | 
| 623 | if (report != REP_QUIET) | 
| 624 | printf( | 
| 625 | "%s: %ss '%s' and '%s' have different pixel ordering\n", | 
| 626 | progname, class, f1name, f2name); | 
| 627 | return(0); | 
| 628 | } | 
| 629 | if ((r1p->xr != r2p->xr) | (r1p->yr != r2p->yr)) { | 
| 630 | if (report != REP_QUIET) | 
| 631 | printf( | 
| 632 | "%s: %ss '%s' and '%s' are different sizes\n", | 
| 633 | progname, class, f1name, f2name); | 
| 634 | return(0); | 
| 635 | } | 
| 636 | return(1); | 
| 637 | } | 
| 638 |  | 
| 639 | /* Compare two inputs that are known to be RGBE or XYZE images */ | 
| 640 | static int | 
| 641 | compare_hdr() | 
| 642 | { | 
| 643 | RESOLU  rs1, rs2; | 
| 644 | COLOR   *scan1, *scan2; | 
| 645 | int     x, y; | 
| 646 |  | 
| 647 | if (report >= REP_VERBOSE) { | 
| 648 | fputs(progname, stdout); | 
| 649 | fputs(": comparing inputs as HDR images\n", stdout); | 
| 650 | } | 
| 651 | fgetsresolu(&rs1, f1in); | 
| 652 | fgetsresolu(&rs2, f2in); | 
| 653 | if (!check_resolu("HDR image", &rs1, &rs2)) | 
| 654 | return(0); | 
| 655 | scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR)); | 
| 656 | scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR)); | 
| 657 | if (!scan1 | !scan2) { | 
| 658 | fprintf(stderr, "%s: out of memory in compare_hdr()\n", progname); | 
| 659 | exit(2); | 
| 660 | } | 
| 661 | for (y = 0; y < numscans(&rs1); y++) { | 
| 662 | if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) | | 
| 663 | (freadscan(scan2, scanlen(&rs2), f2in) < 0)) { | 
| 664 | if (report != REP_QUIET) | 
| 665 | printf("%s: unexpected end-of-file\n", | 
| 666 | progname); | 
| 667 | free(scan1); | 
| 668 | free(scan2); | 
| 669 | return(0); | 
| 670 | } | 
| 671 | for (x = 0; x < scanlen(&rs1); x++) { | 
| 672 | if (color_check(scan1[x], scan2[x])) | 
| 673 | continue; | 
| 674 | if (report != REP_QUIET) { | 
| 675 | printf( | 
| 676 | "%s: pixels at scanline %d offset %d differ\n", | 
| 677 | progname, y, x); | 
| 678 | if (report >= REP_VERBOSE) { | 
| 679 | printf("%s: (R,G,B)=(%g,%g,%g)\n", | 
| 680 | f1name, colval(scan1[x],RED), | 
| 681 | colval(scan1[x],GRN), | 
| 682 | colval(scan1[x],BLU)); | 
| 683 | printf("%s: (R,G,B)=(%g,%g,%g)\n", | 
| 684 | f2name, colval(scan2[x],RED), | 
| 685 | colval(scan2[x],GRN), | 
| 686 | colval(scan2[x],BLU)); | 
| 687 | } | 
| 688 | } | 
| 689 | free(scan1); | 
| 690 | free(scan2); | 
| 691 | return(0); | 
| 692 | } | 
| 693 | } | 
| 694 | free(scan1); | 
| 695 | free(scan2); | 
| 696 | return(good_RMS());                     /* final check of RMS */ | 
| 697 | } | 
| 698 |  | 
| 699 | /* Set reference depth based on header variable */ | 
| 700 | static int | 
| 701 | set_refdepth(DEPTHCODEC *dcp, LUTAB *htp) | 
| 702 | { | 
| 703 | static char     depthvar[] = DEPTHSTR; | 
| 704 | const char      *drval; | 
| 705 |  | 
| 706 | depthvar[LDEPTHSTR-1] = '\0'; | 
| 707 | drval = (const char *)lu_find(htp, depthvar)->data; | 
| 708 | if (!drval) | 
| 709 | return(0); | 
| 710 | dcp->refdepth = atof(drval); | 
| 711 | if (dcp->refdepth <= 0) { | 
| 712 | if (report != REP_QUIET) { | 
| 713 | fputs(dcp->inpname, stderr); | 
| 714 | fputs(": bad reference depth '", stderr); | 
| 715 | fputs(drval, stderr); | 
| 716 | fputs("'\n", stderr); | 
| 717 | } | 
| 718 | return(-1); | 
| 719 | } | 
| 720 | return(1); | 
| 721 | } | 
| 722 |  | 
| 723 | /* Compare two encoded depth maps */ | 
| 724 | static int | 
| 725 | compare_depth() | 
| 726 | { | 
| 727 | long            nread = 0; | 
| 728 | DEPTHCODEC      dc1, dc2; | 
| 729 |  | 
| 730 | if (report >= REP_VERBOSE) { | 
| 731 | fputs(progname, stdout); | 
| 732 | fputs(": comparing inputs as depth maps\n", stdout); | 
| 733 | } | 
| 734 | set_dc_defaults(&dc1); | 
| 735 | dc1.hdrflags = HF_RESIN; | 
| 736 | dc1.finp = f1in; | 
| 737 | dc1.inpname = f1name; | 
| 738 | set_dc_defaults(&dc2); | 
| 739 | dc2.hdrflags = HF_RESIN; | 
| 740 | dc2.finp = f2in; | 
| 741 | dc2.inpname = f2name; | 
| 742 | if (report != REP_QUIET) { | 
| 743 | dc1.hdrflags |= HF_STDERR; | 
| 744 | dc2.hdrflags |= HF_STDERR; | 
| 745 | } | 
| 746 | if (!process_dc_header(&dc1, 0, NULL)) | 
| 747 | return(0); | 
| 748 | if (!process_dc_header(&dc2, 0, NULL)) | 
| 749 | return(0); | 
| 750 | if (!check_resolu("Depth map", &dc1.res, &dc2.res)) | 
| 751 | return(0); | 
| 752 | if (set_refdepth(&dc1, &hdr1) < 0) | 
| 753 | return(0); | 
| 754 | if (set_refdepth(&dc2, &hdr2) < 0) | 
| 755 | return(0); | 
| 756 | while (nread < dc1.res.xr*dc1.res.yr) { | 
| 757 | double  d1 = decode_depth_next(&dc1); | 
| 758 | double  d2 = decode_depth_next(&dc2); | 
| 759 | if ((d1 < 0) | (d2 < 0)) { | 
| 760 | if (report != REP_QUIET) | 
| 761 | printf("%s: unexpected end-of-file\n", | 
| 762 | progname); | 
| 763 | return(0); | 
| 764 | } | 
| 765 | ++nread; | 
| 766 | if (real_check(d1, d2)) | 
| 767 | continue; | 
| 768 | if (report != REP_QUIET) | 
| 769 | printf("%s: %ld%s depth values differ\n", | 
| 770 | progname, nread, num_sfx(nread)); | 
| 771 | return(0); | 
| 772 | } | 
| 773 | return(good_RMS());             /* final check of RMS */ | 
| 774 | } | 
| 775 |  | 
| 776 | /* Compare two encoded normal maps */ | 
| 777 | static int | 
| 778 | compare_norm() | 
| 779 | { | 
| 780 | long            nread = 0; | 
| 781 | NORMCODEC       nc1, nc2; | 
| 782 |  | 
| 783 | if (report >= REP_VERBOSE) { | 
| 784 | fputs(progname, stdout); | 
| 785 | fputs(": comparing inputs as normal maps\n", stdout); | 
| 786 | } | 
| 787 | set_nc_defaults(&nc1); | 
| 788 | nc1.hdrflags = HF_RESIN; | 
| 789 | nc1.finp = f1in; | 
| 790 | nc1.inpname = f1name; | 
| 791 | set_nc_defaults(&nc2); | 
| 792 | nc2.hdrflags = HF_RESIN; | 
| 793 | nc2.finp = f2in; | 
| 794 | nc2.inpname = f2name; | 
| 795 | if (report != REP_QUIET) { | 
| 796 | nc1.hdrflags |= HF_STDERR; | 
| 797 | nc2.hdrflags |= HF_STDERR; | 
| 798 | } | 
| 799 | if (!process_nc_header(&nc1, 0, NULL)) | 
| 800 | return(0); | 
| 801 | if (!process_nc_header(&nc2, 0, NULL)) | 
| 802 | return(0); | 
| 803 | if (!check_resolu("Normal map", &nc1.res, &nc2.res)) | 
| 804 | return(0); | 
| 805 | while (nread < nc1.res.xr*nc1.res.yr) { | 
| 806 | FVECT   nv1, nv2; | 
| 807 | int     rv1 = decode_normal_next(nv1, &nc1); | 
| 808 | int     rv2 = decode_normal_next(nv2, &nc2); | 
| 809 | if ((rv1 < 0) | (rv2 < 0)) { | 
| 810 | if (report != REP_QUIET) | 
| 811 | printf("%s: unexpected end-of-file\n", | 
| 812 | progname); | 
| 813 | return(0); | 
| 814 | } | 
| 815 | ++nread; | 
| 816 | if (rv1 == rv2 && (!rv1 || norm_check(nv1, nv2))) | 
| 817 | continue; | 
| 818 | if (report != REP_QUIET) | 
| 819 | printf("%s: %ld%s normal vectors differ\n", | 
| 820 | progname, nread, num_sfx(nread)); | 
| 821 | return(0); | 
| 822 | } | 
| 823 | return(good_RMS());             /* final check of RMS */ | 
| 824 | } | 
| 825 |  | 
| 826 | /* Compare two inputs that are known to be 32-bit floating-point data */ | 
| 827 | static int | 
| 828 | compare_float() | 
| 829 | { | 
| 830 | long    nread = 0; | 
| 831 | float   f1, f2; | 
| 832 |  | 
| 833 | if (report >= REP_VERBOSE) { | 
| 834 | fputs(progname, stdout); | 
| 835 | fputs(": comparing inputs as 32-bit IEEE floats\n", stdout); | 
| 836 | } | 
| 837 | while (getbinary(&f1, sizeof(f1), 1, f1in)) { | 
| 838 | if (!getbinary(&f2, sizeof(f2), 1, f2in)) | 
| 839 | goto badeof; | 
| 840 | ++nread; | 
| 841 | if (f1swap) swap32((char *)&f1, 1); | 
| 842 | if (f2swap) swap32((char *)&f2, 1); | 
| 843 | if (real_check(f1, f2)) | 
| 844 | continue; | 
| 845 | if (report != REP_QUIET) | 
| 846 | printf("%s: %ld%s float values differ\n", | 
| 847 | progname, nread, num_sfx(nread)); | 
| 848 | return(0); | 
| 849 | } | 
| 850 | if (!getbinary(&f2, sizeof(f2), 1, f2in)) | 
| 851 | return(good_RMS());             /* final check of RMS */ | 
| 852 | badeof: | 
| 853 | if (report != REP_QUIET) | 
| 854 | printf("%s: unexpected end-of-file\n", progname); | 
| 855 | return(0); | 
| 856 | } | 
| 857 |  | 
| 858 | /* Compare two inputs that are known to be 64-bit floating-point data */ | 
| 859 | static int | 
| 860 | compare_double() | 
| 861 | { | 
| 862 | long    nread = 0; | 
| 863 | double  f1, f2; | 
| 864 |  | 
| 865 | if (report >= REP_VERBOSE) { | 
| 866 | fputs(progname, stdout); | 
| 867 | fputs(": comparing inputs as 64-bit IEEE doubles\n", stdout); | 
| 868 | } | 
| 869 | while (getbinary(&f1, sizeof(f1), 1, f1in)) { | 
| 870 | if (!getbinary(&f2, sizeof(f2), 1, f2in)) | 
| 871 | goto badeof; | 
| 872 | ++nread; | 
| 873 | if (f1swap) swap64((char *)&f1, 1); | 
| 874 | if (f2swap) swap64((char *)&f2, 1); | 
| 875 | if (real_check(f1, f2)) | 
| 876 | continue; | 
| 877 | if (report != REP_QUIET) | 
| 878 | printf("%s: %ld%s double values differ\n", | 
| 879 | progname, nread, num_sfx(nread)); | 
| 880 | return(0); | 
| 881 | } | 
| 882 | if (!getbinary(&f2, sizeof(f2), 1, f2in)) | 
| 883 | return(good_RMS());             /* final check of RMS */ | 
| 884 | badeof: | 
| 885 | if (report != REP_QUIET) | 
| 886 | printf("%s: unexpected end-of-file\n", progname); | 
| 887 | return(0); | 
| 888 | } | 
| 889 |  | 
| 890 | /* Compare two Radiance files for equivalence */ | 
| 891 | int | 
| 892 | main(int argc, char *argv[]) | 
| 893 | { | 
| 894 | int     typ1, typ2; | 
| 895 | int     a; | 
| 896 |  | 
| 897 | progname = argv[0]; | 
| 898 | for (a = 1; a < argc && argv[a][0] == '-'; a++) { | 
| 899 | switch (argv[a][1]) { | 
| 900 | case 'h':                       /* ignore header info. */ | 
| 901 | ign_header = !ign_header; | 
| 902 | continue; | 
| 903 | case 's':                       /* silent operation */ | 
| 904 | report = REP_QUIET; | 
| 905 | continue; | 
| 906 | case 'w':                       /* turn off warnings */ | 
| 907 | if (report > REP_ERROR) | 
| 908 | report = REP_ERROR; | 
| 909 | continue; | 
| 910 | case 'v':                       /* turn on verbose mode */ | 
| 911 | report = REP_VERBOSE; | 
| 912 | continue; | 
| 913 | case 'm':                       /* maximum epsilon */ | 
| 914 | max_lim = atof(argv[++a]); | 
| 915 | continue; | 
| 916 | case 'r': | 
| 917 | if (argv[a][2] == 'e')          /* relative difference */ | 
| 918 | rel_min = atof(argv[++a]); | 
| 919 | else if (argv[a][2] == 'm')     /* RMS limit */ | 
| 920 | rms_lim = atof(argv[++a]); | 
| 921 | else | 
| 922 | usage(); | 
| 923 | continue; | 
| 924 | case '\0':                      /* first file from stdin */ | 
| 925 | f1in = stdin; | 
| 926 | f1name = stdin_name; | 
| 927 | break; | 
| 928 | default: | 
| 929 | usage(); | 
| 930 | } | 
| 931 | break; | 
| 932 | } | 
| 933 | if (a != argc-2)                        /* make sure of two inputs */ | 
| 934 | usage(); | 
| 935 | if (!strcmp(argv[a], argv[a+1])) {      /* inputs are same? */ | 
| 936 | if (report >= REP_WARN) | 
| 937 | printf("%s: warning - identical inputs given\n", | 
| 938 | progname); | 
| 939 | return(0); | 
| 940 | } | 
| 941 | if (!f1name) f1name = argv[a]; | 
| 942 | if (!f2name) f2name = argv[a+1]; | 
| 943 | /* open inputs */ | 
| 944 | SET_FILE_BINARY(stdin);                 /* in case we're using it */ | 
| 945 | if (!f1in && !(f1in = fopen(f1name, "rb"))) { | 
| 946 | fprintf(stderr, "%s: cannot open for reading\n", f1name); | 
| 947 | return(2); | 
| 948 | } | 
| 949 | if (!strcmp(f2name, "-")) { | 
| 950 | f2in = stdin; | 
| 951 | f2name = stdin_name; | 
| 952 | } else if (!(f2in = fopen(f2name, "rb"))) { | 
| 953 | fprintf(stderr, "%s: cannot open for reading\n", f2name); | 
| 954 | return(2); | 
| 955 | } | 
| 956 | /* load headers */ | 
| 957 | if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0) | 
| 958 | return(2); | 
| 959 | if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0) | 
| 960 | return(2); | 
| 961 | if (typ1 != typ2) { | 
| 962 | if (report != REP_QUIET) | 
| 963 | printf("%s: '%s' format is %s and '%s' is %s\n", | 
| 964 | progname, f1name, file_type[typ1], | 
| 965 | f2name, file_type[typ2]); | 
| 966 | return(1); | 
| 967 | } | 
| 968 | ign_header |= !has_header(typ1);        /* check headers if indicated */ | 
| 969 | if (!ign_header && !headers_match()) | 
| 970 | return(1); | 
| 971 | if (!ign_header & (report >= REP_WARN)) { | 
| 972 | if (lin1cnt != lin2cnt) | 
| 973 | printf("%s: warning - headers are different lengths\n", | 
| 974 | progname); | 
| 975 | if (typ1 == TYP_UNKNOWN) | 
| 976 | printf("%s: warning - unrecognized format\n", | 
| 977 | progname); | 
| 978 | } | 
| 979 | if (report >= REP_VERBOSE) { | 
| 980 | printf("%s: data format is %s\n", progname, file_type[typ1]); | 
| 981 | if ((typ1 == TYP_FLOAT) | (typ1 == TYP_DOUBLE)) { | 
| 982 | if (f1swap) | 
| 983 | printf("%s: input '%s' is byte-swapped\n", | 
| 984 | progname, f1name); | 
| 985 | if (f2swap) | 
| 986 | printf("%s: input '%s' is byte-swapped\n", | 
| 987 | progname, f2name); | 
| 988 | } | 
| 989 | } | 
| 990 | switch (typ1) {                         /* compare based on type */ | 
| 991 | case TYP_BINARY: | 
| 992 | case TYP_TMESH: | 
| 993 | case TYP_OCTREE: | 
| 994 | case TYP_RBFMESH: | 
| 995 | case TYP_ID8: | 
| 996 | case TYP_ID16: | 
| 997 | case TYP_ID24: | 
| 998 | case TYP_UNKNOWN: | 
| 999 | return( !compare_binary() ); | 
| 1000 | case TYP_TEXT: | 
| 1001 | case TYP_ASCII: | 
| 1002 | return( !compare_text() ); | 
| 1003 | case TYP_RGBE: | 
| 1004 | case TYP_XYZE: | 
| 1005 | return( !compare_hdr() ); | 
| 1006 | case TYP_DEPTH: | 
| 1007 | return( !compare_depth() ); | 
| 1008 | case TYP_NORM: | 
| 1009 | return( !compare_norm() ); | 
| 1010 | case TYP_FLOAT: | 
| 1011 | return( !compare_float() ); | 
| 1012 | case TYP_DOUBLE: | 
| 1013 | return( !compare_double() ); | 
| 1014 | } | 
| 1015 | return(1); | 
| 1016 | } |