--- ray/src/util/radcompare.c 2018/10/15 17:52:52 2.1 +++ ray/src/util/radcompare.c 2018/11/17 20:22:17 2.17 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: radcompare.c,v 2.1 2018/10/15 17:52:52 greg Exp $"; +static const char RCSid[] = "$Id: radcompare.c,v 2.17 2018/11/17 20:22:17 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", @@ -40,15 +45,15 @@ const char *file_type[] = { "ascii", COLRFMT, CIEFMT, - "BINARY_float", - "BINARY_double", + "float", + "double", "BSDF_RBFmesh", "Radiance_octree", "Radiance_tmesh", "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}; @@ -69,14 +74,25 @@ LUTAB hdr2 = LU_SINIT(free,free); #define adv_linecnt(htp) (lin1cnt += (htp == &hdr1), \ lin2cnt += (htp == &hdr2)) +typedef struct { /* dynamic line buffer */ + char *str; + int len; + int siz; +} LINEBUF; + +#define init_line(bp) ((bp)->str = NULL, (bp)->siz = 0) + /* 100 MByte limit on line buffer */ +#define MAXBUF (100L<<20) + /* input files */ char *progname = NULL; const char stdin_name[] = ""; const char *f1name=NULL, *f2name=NULL; FILE *f1in=NULL, *f2in=NULL; + /* running real differences */ double diff2sum = 0; -int nsum = 0; +long nsum = 0; /* Report usage and exit */ static void @@ -84,11 +100,60 @@ usage() { fputs("Usage: ", stderr); fputs(progname, stderr); - fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] file1 file2\n", + fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n", stderr); - exit(1); + exit(2); } +/* Read a text line, increasing buffer size as necessary */ +static int +read_line(LINEBUF *bp, FILE *fp) +{ + static int doneWarn = 0; + + bp->len = 0; + if (!bp->str) { + bp->str = (char *)malloc(bp->siz = 512); + if (!bp->str) + goto memerr; + } + while (fgets(bp->str + bp->len, bp->siz - bp->len, fp)) { + bp->len += strlen(bp->str + bp->len); + if (bp->str[bp->len-1] == '\n') + break; /* found EOL */ + if (bp->len < bp->siz - 4) + continue; /* at EOF? */ + if (bp->siz >= MAXBUF) { + if ((report >= REP_WARN) & !doneWarn) { + fprintf(stderr, + "%s: warning - input line(s) past %ld MByte limit\n", + progname, MAXBUF>>20); + doneWarn++; + } + break; /* return MAXBUF partial line */ + } + if ((bp->siz += bp->siz/2) > MAXBUF) + bp->siz = MAXBUF; + bp->str = (char *)realloc(bp->str, bp->siz); + if (!bp->str) + goto memerr; + } + return(bp->len); +memerr: + fprintf(stderr, + "%s: out of memory in read_line() allocating %d byte buffer\n", + progname, bp->siz); + exit(2); +} + +/* Free line buffer */ +static void +free_line(LINEBUF *bp) +{ + if (bp->str) free(bp->str); + init_line(bp); +} + /* Get type ID from name (or 0 if not found) */ static int xlate_type(const char *nm) @@ -116,11 +181,11 @@ real_check(double r1, double r2) } if (max_lim >= 0 && diff2 > max_lim*max_lim) { if (report != REP_QUIET) - fprintf(stderr, - "%s: %sdifference between %.8g and %.8g exceeds epsilon\n", + printf( + "%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; @@ -128,6 +193,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) @@ -138,10 +219,11 @@ equiv_string(char *s1, char *s2) /* skip whitespace at beginning */ while (isspace(*s1)) s1++; while (isspace(*s2)) s2++; - if (!strcmp(s1, s2)) /* quick check */ - return(1); while (*s1) { /* check each word */ - int inquote = *s1; + int inquote; + if (!*s2) /* unexpected EOL in s2? */ + return(0); + inquote = *s1; if ((inquote != '\'') & (inquote != '"')) inquote = 0; if (inquote) { /* quoted text must match exactly */ @@ -205,6 +287,7 @@ setheadvar(char *val, void *p) int kln, vln; int n; + adv_linecnt(htp); /* side-effect is to count lines */ if (!isalpha(*val)) /* key must start line */ return(0); key = val++; @@ -220,22 +303,21 @@ 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); + return(-1); /* memory allocation error */ if (!tep->key) tep->key = strcpy(malloc(kln+1), key); if (tep->data) free(tep->data); tep->data = strcpy(malloc(vln+1), val); - adv_linecnt(htp); return(1); } @@ -246,14 +328,21 @@ match_val(const LUENT *ep1, void *p2) const LUENT *ep2 = lu_find((LUTAB *)p2, ep1->key); if (!ep2 || !ep2->data) { if (report != REP_QUIET) - fprintf(stderr, "%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) - fprintf(stderr, "%s: Header variables '%s' have different values\n", + if (report != REP_QUIET) { + printf("%s: header variable '%s' has different values\n", progname, ep1->key); + if (report >= REP_VERBOSE) { + printf("%s: %s=%s\n", f1name, + ep1->key, (char *)ep1->data); + printf("%s: %s=%s\n", f2name, + ep2->key, (char *)ep2->data); + } + } return(-1); } return(1); /* good match */ @@ -261,18 +350,15 @@ 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! */ - ne = lu_doall(hp2, NULL, NULL) - ne; - if (ne) { - if (report != REP_QUIET) - fprintf(stderr, "%s: '%s' has %d extra header variable(s)\n", + /* non-fatal if second header has extra */ + 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(0); - } return(1); /* good match */ } @@ -284,9 +370,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) @@ -316,15 +403,15 @@ identify_type(const char *name, FILE *fin, LUTAB *htp) char sbuf[32]; if (!fgets(sbuf, sizeof(sbuf), fin)) goto badeof; + adv_linecnt(htp); /* for #?ID string */ if (report >= REP_WARN && strncmp(sbuf, "RADIANCE", 8)) { - fputs(name, stderr); - fputs(": warning - unexpected header ID: ", stderr); - fputs(sbuf, stderr); + fputs(name, stdout); + fputs(": warning - unexpected header ID: ", stdout); + fputs(sbuf, stdout); } - adv_linecnt(htp); /* for #?ID string */ 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 */ @@ -342,8 +429,8 @@ identify_type(const char *name, FILE *fin, LUTAB *htp) return(TYP_TEXT); badeof: if (report != REP_QUIET) { - fputs(name, stderr); - fputs(": Unexpected end-of-file\n", stderr); + fputs(name, stdout); + fputs(": unexpected end-of-file\n", stdout); } return(-1); } @@ -356,17 +443,16 @@ good_RMS() return(1); if (diff2sum/(double)nsum > rms_lim*rms_lim) { if (report != REP_QUIET) - fprintf(stderr, - "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit\n", + printf( + "%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) - fprintf(stderr, - "%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n", + printf("%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n", progname, (rel_min > 0) ? "relative " : "", f1name, f2name, sqrt(diff2sum/(double)nsum)); return(1); @@ -376,26 +462,28 @@ good_RMS() static int compare_binary() { + int c1=0, c2=0; + if (report >= REP_VERBOSE) { - fputs(progname, stderr); - fputs(": comparing inputs as binary\n", stderr); + 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, stderr); - fputs(": Unexpected end-of-file\n", stderr); + fputs(f1name, stdout); + fputs(": unexpected end-of-file\n", stdout); } return(0); } if (c2 == EOF) { if (report != REP_QUIET) { - fputs(f2name, stderr); - fputs(": Unexpected end-of-file\n", stderr); + fputs(f2name, stdout); + fputs(": unexpected end-of-file\n", stdout); } return(0); } @@ -404,8 +492,11 @@ compare_binary() } if (report == REP_QUIET) return(0); - fprintf(stderr, "%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); } @@ -413,56 +504,63 @@ compare_binary() static int compare_text() { - char l1buf[4096], l2buf[4096]; + LINEBUF l1buf, l2buf; if (report >= REP_VERBOSE) { - fputs(progname, stderr); - fputs(": comparing inputs as ASCII text\n", stderr); + fputs(progname, stdout); + fputs(": comparing inputs as ASCII text\n", stdout); } - /* compare a line at a time */ - while (fgets(l1buf, sizeof(l1buf), f1in)) { + init_line(&l1buf); init_line(&l2buf); /* compare a line at a time */ + while (read_line(&l1buf, f1in)) { lin1cnt++; - if (!*sskip2(l1buf,0)) + if (!*sskip2(l1buf.str,0)) continue; /* ignore empty lines */ - while (fgets(l2buf, sizeof(l2buf), f2in)) { + + while (read_line(&l2buf, f2in)) { lin2cnt++; - if (*sskip2(l2buf,0)) + if (*sskip2(l2buf.str,0)) break; /* found other non-empty line */ } - if (feof(f2in)) { + if (!l2buf.len) { /* input 2 EOF? */ if (report != REP_QUIET) { - fputs(f2name, stderr); - fputs(": Unexpected end-of-file\n", stderr); + fputs(f2name, stdout); + fputs(": unexpected end-of-file\n", stdout); } + free_line(&l1buf); free_line(&l2buf); return(0); } /* compare non-empty lines */ - if (!equiv_string(l1buf, l2buf)) { + if (!equiv_string(l1buf.str, l2buf.str)) { if (report != REP_QUIET) { - fprintf(stderr, "%s: inputs '%s' and '%s' differ at line %d|%d\n", + printf("%s: inputs '%s' and '%s' differ at line %d|%d\n", progname, f1name, f2name, lin1cnt, lin2cnt); - if (report >= REP_VERBOSE) { - fputs("------------- Mismatch -------------\n", stderr); - fprintf(stderr, "%s(%d):\t%s", f1name, - lin1cnt, l1buf); - fprintf(stderr, "%s(%d):\t%s", f2name, - lin2cnt, l2buf); + if ( report >= REP_VERBOSE && + (l1buf.len < 256) & + (l2buf.len < 256) ) { + fputs("------------- Mismatch -------------\n", stdout); + printf("%s@%d:\t%s", f1name, + lin1cnt, l1buf.str); + printf("%s@%d:\t%s", f2name, + lin2cnt, l2buf.str); } } + free_line(&l1buf); free_line(&l2buf); return(0); } } - /* check for EOF on input 2 */ - while (fgets(l2buf, sizeof(l2buf), f2in)) { - if (!*sskip2(l2buf,0)) + free_line(&l1buf); /* check for EOF on input 2 */ + while (read_line(&l2buf, f2in)) { + if (!*sskip2(l2buf.str,0)) continue; if (report != REP_QUIET) { - fputs(f1name, stderr); - fputs(": Unexpected end-of-file\n", stderr); + fputs(f1name, stdout); + fputs(": unexpected end-of-file\n", stdout); } + free_line(&l2buf); return(0); } + free_line(&l2buf); return(good_RMS()); /* final check for reals */ } @@ -474,18 +572,22 @@ 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) { if (report != REP_QUIET) - fprintf(stderr, + printf( "%s: Images '%s' and '%s' have different pixel ordering\n", progname, f1name, f2name); return(0); } if ((rs1.xr != rs2.xr) | (rs1.yr != rs2.yr)) { if (report != REP_QUIET) - fprintf(stderr, + printf( "%s: Images '%s' and '%s' are different sizes\n", progname, f1name, f2name); return(0); @@ -500,30 +602,25 @@ compare_hdr() if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) | (freadscan(scan2, scanlen(&rs2), f2in) < 0)) { if (report != REP_QUIET) - fprintf(stderr, "%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--; ) { - 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))) + for (x = 0; x < scanlen(&rs1); x++) { + if (color_check(scan1[x], scan2[x])) continue; if (report != REP_QUIET) { - fprintf(stderr, + printf( "%s: pixels at scanline %d offset %d differ\n", progname, y, x); if (report >= REP_VERBOSE) { - fprintf(stderr, "%s: (R,G,B)=(%g,%g,%g)\n", + printf("%s: (R,G,B)=(%g,%g,%g)\n", f1name, colval(scan1[x],RED), colval(scan1[x],GRN), colval(scan1[x],BLU)); - fprintf(stderr, "%s: (R,G,B)=(%g,%g,%g)\n", + printf("%s: (R,G,B)=(%g,%g,%g)\n", f2name, colval(scan2[x],RED), colval(scan2[x],GRN), colval(scan2[x],BLU)); @@ -539,10 +636,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() @@ -550,6 +643,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; @@ -557,15 +654,15 @@ compare_float() if (real_check(f1, f2)) continue; if (report != REP_QUIET) - fprintf(stderr, "%s: %ld%s float values differ\n", - progname, nread, nsuffix[nread%10]); + printf("%s: %ld%s float 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) - fprintf(stderr, "%s: Unexpected end-of-file\n", progname); + printf("%s: unexpected end-of-file\n", progname); return(0); } @@ -576,6 +673,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; @@ -583,15 +684,15 @@ compare_double() if (real_check(f1, f2)) continue; if (report != REP_QUIET) - fprintf(stderr, "%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) - fprintf(stderr, "%s: Unexpected end-of-file\n", progname); + printf("%s: unexpected end-of-file\n", progname); return(0); } @@ -638,56 +739,55 @@ 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) - fprintf(stderr, "%s: warning - identical inputs given\n", + 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) - fprintf(stderr, "%s: '%s' is %s and '%s' is %s\n", + printf("%s: '%s' is %s and '%s' is %s\n", progname, f1name, file_type[typ1], f2name, file_type[typ2]); 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); + lu_done(&hdr1); lu_done(&hdr2); /* done with header info. */ if (!ign_header & (report >= REP_WARN)) { - if (typ1 == TYP_UNKNOWN) - fprintf(stderr, "%s: warning - unrecognized format, comparing as binary\n", - progname); if (lin1cnt != lin2cnt) - fprintf(stderr, "%s: warning - headers are different lengths\n", + printf("%s: warning - headers are different lengths\n", progname); + if (typ1 == TYP_UNKNOWN) + printf("%s: warning - unrecognized format\n", + progname); } if (report >= REP_VERBOSE) - fprintf(stderr, "%s: input file type is %s\n", + printf("%s: input file type is %s\n", progname, file_type[typ1]); switch (typ1) { /* compare based on type */