--- ray/src/util/radcompare.c 2018/10/15 21:47:52 2.4 +++ ray/src/util/radcompare.c 2018/10/16 00:20:52 2.7 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: radcompare.c,v 2.4 2018/10/15 21:47:52 greg Exp $"; +static const char RCSid[] = "$Id: radcompare.c,v 2.7 2018/10/16 00:20:52 greg Exp $"; #endif /* * Compare Radiance files for significant differences @@ -33,6 +33,11 @@ double max_lim = 0.25; /* difference limit if non-neg int lin1cnt=0, lin2cnt=0; /* file line position */ +const char nsuffix[10][3] = { /* 1st, 2nd, 3rd, etc. */ + "th","st","nd","rd","th","th","th","th","th","th" + }; +#define num_sfx(n) nsuffix[(n)%10] + /* file types */ const char *file_type[] = { "Unrecognized", @@ -48,7 +53,7 @@ const char *file_type[] = { "BINARY_unknown", NULL /* terminator */ }; - + /* keep consistent with above */ enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, TYP_FLOAT, TYP_DOUBLE, TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, TYP_BINARY}; @@ -118,10 +123,10 @@ real_check(double r1, double r2) if (max_lim >= 0 && diff2 > max_lim*max_lim) { if (report != REP_QUIET) printf( - "%s: %sdifference between %.8g and %.8g exceeds epsilon\n", + "%s: %sdifference between %.8g and %.8g exceeds epsilon of %.8g\n", progname, (rel_min > 0) ? "relative " : "", - r1, r2); + r1, r2, max_lim); return(0); } diff2sum += diff2; @@ -223,14 +228,14 @@ setheadvar(char *val, void *p) val++; if (!*val) /* nothing set? */ return(0); - vln = strlen(val); /* eliminate space and newline at end */ - while (--vln > 0 && isspace(val[vln])) - ; - val[++vln] = '\0'; /* check if key to ignore */ for (n = 0; hdr_ignkey[n]; n++) if (!strcmp(key, hdr_ignkey[n])) return(0); + vln = strlen(val); /* eliminate space and newline at end */ + while (isspace(val[--vln])) + ; + val[++vln] = '\0'; if (!(tep = lu_find(htp, key))) return(-1); /* memory allocation error */ if (!tep->key) @@ -248,13 +253,13 @@ match_val(const LUENT *ep1, void *p2) const LUENT *ep2 = lu_find((LUTAB *)p2, ep1->key); if (!ep2 || !ep2->data) { if (report != REP_QUIET) - printf("%s: Variable '%s' missing in '%s'\n", + printf("%s: variable '%s' missing in '%s'\n", progname, ep1->key, f2name); return(-1); } if (!equiv_string((char *)ep1->data, (char *)ep2->data)) { if (report != REP_QUIET) { - printf("%s: Header variable '%s' has different values\n", + printf("%s: header variable '%s' has different values\n", progname, ep1->key); if (report >= REP_VERBOSE) { printf("%s: %s=%s\n", f1name, @@ -277,7 +282,7 @@ headers_match(LUTAB *hp1, LUTAB *hp2) return(0); /* something didn't match! */ /* non-fatal if second header has extra */ if (report >= REP_WARN && (ne = lu_doall(hp2, NULL, NULL) - ne)) - printf("%s: '%s' has %d extra header setting(s)\n", + printf("%s: warning - '%s' has %d extra header setting(s)\n", progname, f2name, ne); return(1); /* good match */ } @@ -290,9 +295,10 @@ input_is_binary(FILE *fin) int c = 0; while ((c = getc(fin)) != EOF) { + ++n; if (!c | (c > 127)) break; /* non-ascii character */ - if (++n >= 10240) + if (n >= 10240) break; /* enough to be confident */ } if (!n) @@ -330,7 +336,7 @@ identify_type(const char *name, FILE *fin, LUTAB *htp) } if (getheader(fin, setheadvar, htp) < 0) { fputs(name, stderr); - fputs(": Unknown error reading header\n", stderr); + fputs(": unknown error reading header\n", stderr); return(-1); } adv_linecnt(htp); /* for trailing emtpy line */ @@ -349,7 +355,7 @@ identify_type(const char *name, FILE *fin, LUTAB *htp) badeof: if (report != REP_QUIET) { fputs(name, stdout); - fputs(": Unexpected end-of-file\n", stdout); + fputs(": unexpected end-of-file\n", stdout); } return(-1); } @@ -363,11 +369,11 @@ good_RMS() if (diff2sum/(double)nsum > rms_lim*rms_lim) { if (report != REP_QUIET) printf( - "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit\n", + "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit of %.5g\n", progname, (rel_min > 0) ? "relative " : "", f1name, f2name, - sqrt(diff2sum/(double)nsum)); + sqrt(diff2sum/(double)nsum), rms_lim); return(0); } if (report >= REP_VERBOSE) @@ -381,26 +387,28 @@ good_RMS() static int compare_binary() { + int c1=0, c2=0; + if (report >= REP_VERBOSE) { fputs(progname, stdout); fputs(": comparing inputs as binary\n", stdout); } for ( ; ; ) { /* exact byte matching */ - int c1 = getc(f1in); - int c2 = getc(f2in); + c1 = getc(f1in); + c2 = getc(f2in); if (c1 == EOF) { if (c2 == EOF) return(1); /* success! */ if (report != REP_QUIET) { fputs(f1name, stdout); - fputs(": Unexpected end-of-file\n", stdout); + fputs(": unexpected end-of-file\n", stdout); } return(0); } if (c2 == EOF) { if (report != REP_QUIET) { fputs(f2name, stdout); - fputs(": Unexpected end-of-file\n", stdout); + fputs(": unexpected end-of-file\n", stdout); } return(0); } @@ -409,8 +417,11 @@ compare_binary() } if (report == REP_QUIET) return(0); - printf("%s: binary files '%s' and '%s' differ at offset %ld|%ld\n", + printf("%s: binary files '%s' and '%s' differ at byte offset %ld|%ld\n", progname, f1name, f2name, ftell(f1in), ftell(f2in)); + if (report >= REP_VERBOSE) + printf("%s: byte in '%s' is 0x%X, byte in '%s' is 0x%X\n", + progname, f1name, c1, f2name, c2); return(0); } @@ -437,7 +448,7 @@ compare_text() if (feof(f2in)) { if (report != REP_QUIET) { fputs(f2name, stdout); - fputs(": Unexpected end-of-file\n", stdout); + fputs(": unexpected end-of-file\n", stdout); } return(0); } @@ -449,9 +460,9 @@ compare_text() lin1cnt, lin2cnt); if (report >= REP_VERBOSE) { fputs("------------- Mismatch -------------\n", stdout); - printf("%s(%d):\t%s", f1name, + printf("%s@%d:\t%s", f1name, lin1cnt, l1buf); - printf("%s(%d):\t%s", f2name, + printf("%s@%d:\t%s", f2name, lin2cnt, l2buf); } } @@ -464,7 +475,7 @@ compare_text() continue; if (report != REP_QUIET) { fputs(f1name, stdout); - fputs(": Unexpected end-of-file\n", stdout); + fputs(": unexpected end-of-file\n", stdout); } return(0); } @@ -479,6 +490,10 @@ compare_hdr() COLOR *scan1, *scan2; int x, y; + if (report >= REP_VERBOSE) { + fputs(progname, stdout); + fputs(": comparing inputs as HDR images\n", stdout); + } fgetsresolu(&rs1, f1in); fgetsresolu(&rs2, f2in); if (rs1.rt != rs2.rt) { @@ -505,13 +520,13 @@ compare_hdr() if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) | (freadscan(scan2, scanlen(&rs2), f2in) < 0)) { if (report != REP_QUIET) - printf("%s: Unexpected end-of-file\n", + printf("%s: unexpected end-of-file\n", progname); free(scan1); free(scan2); return(0); } - for (x = scanlen(&rs1); x--; ) { + for (x = 0; x < scanlen(&rs1); x++) { if (real_check(colval(scan1[x],RED), colval(scan2[x],RED)) & real_check(colval(scan1[x],GRN), @@ -544,10 +559,6 @@ compare_hdr() return(good_RMS()); /* final check of RMS */ } -const char nsuffix[10][3] = { /* 1st, 2nd, 3rd, etc. */ - "th","st","nd","rd","th","th","th","th","th","th" - }; - /* Compare two inputs that are known to be 32-bit floating-point data */ static int compare_float() @@ -555,6 +566,10 @@ compare_float() long nread = 0; float f1, f2; + if (report >= REP_VERBOSE) { + fputs(progname, stdout); + fputs(": comparing inputs as 32-bit IEEE floats\n", stdout); + } while (getbinary(&f1, sizeof(f1), 1, f1in)) { if (!getbinary(&f2, sizeof(f2), 1, f2in)) goto badeof; @@ -563,14 +578,14 @@ compare_float() continue; if (report != REP_QUIET) printf("%s: %ld%s float values differ\n", - progname, nread, nsuffix[nread%10]); + progname, nread, num_sfx(nread)); return(0); } if (!getbinary(&f2, sizeof(f2), 1, f2in)) return(good_RMS()); /* final check of RMS */ badeof: if (report != REP_QUIET) - printf("%s: Unexpected end-of-file\n", progname); + printf("%s: unexpected end-of-file\n", progname); return(0); } @@ -581,6 +596,10 @@ compare_double() long nread = 0; double f1, f2; + if (report >= REP_VERBOSE) { + fputs(progname, stdout); + fputs(": comparing inputs as 64-bit IEEE doubles\n", stdout); + } while (getbinary(&f1, sizeof(f1), 1, f1in)) { if (!getbinary(&f2, sizeof(f2), 1, f2in)) goto badeof; @@ -588,15 +607,15 @@ compare_double() if (real_check(f1, f2)) continue; if (report != REP_QUIET) - printf("%s: %ld%s float values differ\n", - progname, nread, nsuffix[nread%10]); + printf("%s: %ld%s double values differ\n", + progname, nread, num_sfx(nread)); return(0); } if (!getbinary(&f2, sizeof(f2), 1, f2in)) return(good_RMS()); /* final check of RMS */ badeof: if (report != REP_QUIET) - printf("%s: Unexpected end-of-file\n", progname); + printf("%s: unexpected end-of-file\n", progname); return(0); } @@ -643,17 +662,16 @@ main(int argc, char *argv[]) } break; } - if (a != argc-2) + if (a != argc-2) /* make sure of two inputs */ usage(); - if (!f1name) f1name = argv[a]; - if (!f2name) f2name = argv[a+1]; - - if (!strcmp(f1name, f2name)) { /* inputs are same? */ + if (!strcmp(argv[a], argv[a+1])) { /* inputs are same? */ if (report >= REP_WARN) printf("%s: warning - identical inputs given\n", progname); return(0); } + if (!f1name) f1name = argv[a]; + if (!f2name) f2name = argv[a+1]; /* open inputs */ SET_FILE_BINARY(stdin); /* in case we're using it */ if (!f1in && !(f1in = fopen(f1name, "rb"))) {