--- ray/src/util/radcompare.c 2018/10/15 22:21:45 2.5 +++ ray/src/util/radcompare.c 2018/10/19 20:32:16 2.11 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: radcompare.c,v 2.5 2018/10/15 22:21:45 greg Exp $"; +static const char RCSid[] = "$Id: radcompare.c,v 2.11 2018/10/19 20:32:16 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}; @@ -77,7 +82,7 @@ FILE *f1in=NULL, *f2in=NULL; /* running real differences */ double diff2sum = 0; -int nsum = 0; +long nsum = 0; /* Report usage and exit */ static void @@ -87,7 +92,7 @@ usage() fputs(progname, stderr); fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n", stderr); - exit(1); + exit(2); } /* Get type ID from name (or 0 if not found) */ @@ -129,6 +134,22 @@ real_check(double r1, double r2) return(1); } +/* Compare two color values for equivalence */ +static int +color_check(COLOR c1, COLOR c2) +{ + int p; + + if (!real_check(colval(c1,RED)+colval(c1,GRN)+colval(c1,BLU)*(1./3.), + colval(c2,RED)+colval(c2,GRN)+colval(c2,BLU))*(1./3.)) + return(0); + + p = (colval(c1,GRN) > colval(c1,RED)) ? GRN : RED; + if (colval(c1,BLU) > colval(c1,p)) p = BLU; + + return(real_check(colval(c1,p), colval(c2,p))); +} + /* Compare two strings for equivalence */ static int equiv_string(char *s1, char *s2) @@ -223,14 +244,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) @@ -270,13 +291,13 @@ match_val(const LUENT *ep1, void *p2) /* Compare two sets of header variables */ static int -headers_match(LUTAB *hp1, LUTAB *hp2) +headers_match() { - int ne = lu_doall(hp1, match_val, hp2); + int ne = lu_doall(&hdr1, match_val, &hdr2); if (ne < 0) return(0); /* something didn't match! */ /* non-fatal if second header has extra */ - if (report >= REP_WARN && (ne = lu_doall(hp2, NULL, NULL) - ne)) + if (report >= REP_WARN && (ne = lu_doall(&hdr2, NULL, NULL) - ne)) printf("%s: warning - '%s' has %d extra header setting(s)\n", progname, f2name, ne); return(1); /* good match */ @@ -331,7 +352,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 */ @@ -350,7 +371,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); } @@ -396,14 +417,14 @@ compare_binary() 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); } @@ -420,30 +441,54 @@ compare_binary() return(0); } +/* If line is continued (no newline), then back up to word boundary if one */ +static int +leftover(char *buf) +{ + int minlen = strlen(buf); + char *bend = buf + minlen; + char *cp = bend-1; + + if (*cp == '\r') *cp = '\n'; /* AARG Windoze! */ + if (*cp == '\n') /* complete line? */ + return(0); + + minlen >>= 2; /* back over partial word */ + while (!isspace(*cp)) + if (--cp <= buf+minlen) + return(0); + *cp++ = '\0'; /* break line here */ + return(bend - cp); +} + /* Compare two inputs as generic text files */ static int compare_text() { char l1buf[4096], l2buf[4096]; + int l1over=0, l2over=0; if (report >= REP_VERBOSE) { fputs(progname, stdout); fputs(": comparing inputs as ASCII text\n", stdout); } /* compare a line at a time */ - while (fgets(l1buf, sizeof(l1buf), f1in)) { - lin1cnt++; - if (!*sskip2(l1buf,0)) + while (fgets(l1buf+l1over, sizeof(l1buf)-l1over, f1in)) { + lin1cnt += !(l1over = leftover(l1buf)); + if (!*sskip2(l1buf,0)) { + memmove(l1buf, l1buf+sizeof(l1buf)-l1over, l1over); continue; /* ignore empty lines */ - while (fgets(l2buf, sizeof(l2buf), f2in)) { - lin2cnt++; + } + while (fgets(l2buf+l2over, sizeof(l2buf)-l2over, f2in)) { + lin2cnt += !(l2over = leftover(l2buf)); if (*sskip2(l2buf,0)) break; /* found other non-empty line */ + memmove(l2buf, l2buf+sizeof(l2buf)-l2over, l2over); } 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); } @@ -463,6 +508,8 @@ compare_text() } return(0); } + if (l1over) memmove(l1buf, l1buf+sizeof(l1buf)-l1over, l1over); + if (l2over) memmove(l2buf, l2buf+sizeof(l2buf)-l2over, l2over); } /* check for EOF on input 2 */ while (fgets(l2buf, sizeof(l2buf), f2in)) { @@ -470,7 +517,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); } @@ -485,6 +532,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) { @@ -511,19 +562,14 @@ 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 = 0; x < scanlen(&rs1); x++) { - if (real_check(colval(scan1[x],RED), - colval(scan2[x],RED)) & - real_check(colval(scan1[x],GRN), - colval(scan2[x],GRN)) & - real_check(colval(scan1[x],BLU), - colval(scan2[x],BLU))) + if (color_check(scan1[x], scan2[x])) continue; if (report != REP_QUIET) { printf( @@ -550,10 +596,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() @@ -561,6 +603,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; @@ -569,14 +615,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); } @@ -587,6 +633,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; @@ -594,15 +644,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); } @@ -649,35 +699,34 @@ 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"))) { fprintf(stderr, "%s: cannot open for reading\n", f1name); - return(1); + return(2); } if (!strcmp(f2name, "-")) { f2in = stdin; f2name = stdin_name; } else if (!(f2in = fopen(f2name, "rb"))) { fprintf(stderr, "%s: cannot open for reading\n", f2name); - return(1); + return(2); } /* load headers */ if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0) - return(1); + return(2); if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0) - return(1); + return(2); if (typ1 != typ2) { if (report != REP_QUIET) printf("%s: '%s' is %s and '%s' is %s\n", @@ -686,7 +735,7 @@ main(int argc, char *argv[]) return(1); } ign_header |= !has_header(typ1); /* check headers if indicated */ - if (!ign_header && !headers_match(&hdr1, &hdr2)) + if (!ign_header && !headers_match()) return(1); lu_done(&hdr1); lu_done(&hdr2); if (!ign_header & (report >= REP_WARN)) {