--- ray/src/util/radcompare.c 2020/07/27 16:49:56 2.26 +++ ray/src/util/radcompare.c 2023/11/21 20:01:39 2.34 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: radcompare.c,v 2.26 2020/07/27 16:49:56 greg Exp $"; +static const char RCSid[] = "$Id: radcompare.c,v 2.34 2023/11/21 20:01:39 greg Exp $"; #endif /* * Compare Radiance files for significant differences @@ -27,6 +27,8 @@ int report = REP_WARN; /* reporting level */ int ign_header = 0; /* ignore header differences? */ +int escape_newlines = 0; /* allow backslash to skip newlines */ + double rel_min = 1e-5; /* positive for relative comparisons */ double rms_lim = 0.01; /* RMS difference limit */ @@ -49,6 +51,7 @@ const char *file_type[] = { "ascii", COLRFMT, CIEFMT, + SPECFMT, DEPTH16FMT, NORMAL32FMT, "float", @@ -63,7 +66,7 @@ const char *file_type[] = { NULL /* terminator */ }; /* keep consistent with above */ -enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, +enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, TYP_SPEC, TYP_DEPTH, TYP_NORM, TYP_FLOAT, TYP_DOUBLE, TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, TYP_ID8, TYP_ID16, TYP_ID24, TYP_BINARY}; @@ -132,8 +135,19 @@ read_line(LINEBUF *bp, FILE *fp) } 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') + if (bp->str[bp->len-1] == '\n') { + if (bp->len > 1 && bp->str[bp->len-2] == '\r') { + bp->str[--bp->len] = '\0'; + bp->str[bp->len-1] = '\n'; + } + if (escape_newlines && bp->len > 1 && + bp->str[bp->len-2] == '\\') { + bp->str[--bp->len] = '\0'; + bp->str[bp->len-1] = ' '; + continue; + } break; /* found EOL */ + } if (bp->len < bp->siz - 4) continue; /* at EOF? */ if (bp->siz >= MAXBUF) { @@ -197,8 +211,9 @@ real_check(double r1, double r2) if (rel_min > 0) { /* doing relative differences? */ double av2 = .25*(r1*r1 + 2.*fabs(r1*r2) + r2*r2); - if (av2 > rel_min*rel_min) - diff2 /= av2; + if (av2 < rel_min*rel_min) + av2 = rel_min*rel_min; + diff2 /= av2; } if (max_lim >= 0 && diff2 > max_lim*max_lim) { if (report != REP_QUIET) @@ -230,6 +245,44 @@ color_check(COLOR c1, COLOR c2) return(real_check(colval(c1,p), colval(c2,p))); } +#if 1 +/* Compare two color spectra for equivalence */ +static int +spec_check(COLORV *sc1, COLORV *sc2) +{ + int p, k; + + if (!real_check(scolor_mean(sc1), scolor_mean(sc2))) + return(0); + + p = 0; /* find max. component */ + for (k = NCSAMP; --k; ) + if (sc1[k] > sc1[p]) + p = k; + + return(real_check(sc1[p], sc2[p])); +} +#else +/* Compare two color spectra for equivalence */ +static int +spec_check(COLORV *sc1, COLORV *sc2) +{ + COLOR c1, c2; + int p; + + if (!real_check(scolor_mean(sc1), scolor_mean(sc2))) + return(0); + /* do comparisons in RGB space */ + scolor_rgb(c1, sc1); + scolor_rgb(c2, sc2); + + 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))); +} +#endif + /* Compare two normal directions for equivalence */ static int norm_check(FVECT nv1, FVECT nv2) @@ -372,7 +425,7 @@ setheadvar(char *val, void *p) tep->key = strcpy(malloc(kln+1), key); if (tep->data) { /* check for special cases */ if (!strcmp(key, "EXPOSURE")) { - sprintf(newval, "%f", atof(tep->data)*atof(val)); + sprintf(newval, "%.6e", atof(tep->data)*atof(val)); vln = strlen(val = newval); } free(tep->data); @@ -568,6 +621,8 @@ compare_text() if (report >= REP_VERBOSE) { fputs(progname, stdout); fputs(": comparing inputs as ASCII text", stdout); + if (escape_newlines) + fputs(", allowing escaped newlines", stdout); if (comment_c) { fputs(", ignoring comments starting with '", stdout); fputc(comment_c, stdout); @@ -599,9 +654,12 @@ compare_text() /* compare non-empty lines */ if (!equiv_string(l1buf.str, l2buf.str)) { if (report != REP_QUIET) { - printf("%s: inputs '%s' and '%s' differ at line %d|%d\n", - progname, f1name, f2name, - lin1cnt, lin2cnt); + printf("%s: inputs '%s' and '%s' differ at line %d", + progname, f1name, f2name, lin1cnt); + if (lin1cnt != lin2cnt) + printf("|%d\n", lin2cnt); + else + putchar('\n'); if ( report >= REP_VERBOSE && (l1buf.len < 256) & (l2buf.len < 256) ) { @@ -631,6 +689,24 @@ compare_text() return(good_RMS()); /* final check for reals */ } +/* Set resolution based on NROWS, NCOLS in header */ +static int +set_resolu(RESOLU *rs, LUTAB *htp) +{ + const char *val; + + rs->rt = PIXSTANDARD; + val = (const char *)lu_find(htp, "NROWS")->data; + if (!val) return(0); + rs->yr = atoi(val); + if (rs->yr <= 0) return(-1); + val = (const char *)lu_find(htp, "NCOLS")->data; + if (!val) return(0); + rs->xr = atoi(val); + if (rs->xr <= 0) return(-1); + return(1); +} + /* Check image/map resolutions */ static int check_resolu(const char *class, RESOLU *r1p, RESOLU *r2p) @@ -688,8 +764,7 @@ compare_hdr() if (color_check(scan1[x], scan2[x])) continue; if (report != REP_QUIET) { - printf( - "%s: pixels at scanline %d offset %d differ\n", + printf("%s: pixels at scanline %d offset %d differ\n", progname, y, x); if (report >= REP_VERBOSE) { printf("%s: (R,G,B)=(%g,%g,%g)\n", @@ -712,6 +787,82 @@ compare_hdr() return(good_RMS()); /* final check of RMS */ } +/* Compare two inputs that are known to be spectral images */ +static int +compare_spec() +{ + static char NCstr[] = NCOMPSTR; + RESOLU rs1, rs2; + COLORV *scan1, *scan2; + const char *val; + int x, y; + + if (report >= REP_VERBOSE) { + fputs(progname, stdout); + fputs(": comparing inputs as spectral images\n", stdout); + } + if (!(set_resolu(&rs1, &hdr1) || fgetsresolu(&rs1, f1in))) + return(0); + if (!(set_resolu(&rs2, &hdr2) || fgetsresolu(&rs2, f2in))) + return(0); + if (!check_resolu("Spectral image", &rs1, &rs2)) + return(0); + NCstr[LNCOMPSTR-1] = '\0'; + val = (const char *)lu_find(&hdr1, NCstr)->data; + if (!val || (NCSAMP = atoi(val)) < 3) { + if (report != REP_QUIET) { + if (val) + printf("%s: illegal # components (%d) for spectral image\n", + progname, NCSAMP); + else + printf("%s: missing %s in header for spectral image\n", + progname, NCstr); + } + return(0); + } + scan1 = (COLORV *)malloc(sizeof(COLORV)*NCSAMP*scanlen(&rs1)); + scan2 = (COLORV *)malloc(sizeof(COLORV)*NCSAMP*scanlen(&rs2)); + if (!scan1 | !scan2) { + fprintf(stderr, "%s: out of memory in compare_hdr()\n", progname); + exit(2); + } + for (y = 0; y < numscans(&rs1); y++) { + if ((freadsscan(scan1, NCSAMP, scanlen(&rs1), f1in) < 0) | + (freadsscan(scan2, NCSAMP, scanlen(&rs2), f2in) < 0)) { + if (report != REP_QUIET) + printf("%s: unexpected end-of-file\n", progname); + free(scan1); + free(scan2); + return(0); + } + for (x = 0; x < scanlen(&rs1); x++) { + if (spec_check(scan1+x*NCSAMP, scan2+x*NCSAMP)) + continue; + if (report != REP_QUIET) { + int k; + printf("%s: spectra at scanline %d offset %d differ\n", + progname, y, x); + if (report >= REP_VERBOSE) { + printf("%s: spectrum =", f1name); + for (k = 0; k < NCSAMP; k++) + printf(" %g", scan1[x*NCSAMP+k]); + fputc('\n', stdout); + printf("%s: spectrum =", f2name); + for (k = 0; k < NCSAMP; k++) + printf(" %g", scan2[x*NCSAMP+k]); + fputc('\n', stdout); + } + } + free(scan1); + free(scan2); + return(0); + } + } + free(scan1); + free(scan2); + return(good_RMS()); /* final check of RMS */ +} + /* Set reference depth based on header variable */ static int set_refdepth(DEPTHCODEC *dcp, LUTAB *htp) @@ -916,6 +1067,9 @@ main(int argc, char *argv[]) case 'h': /* ignore header info. */ ign_header = !ign_header; continue; + case 'n': /* allow newline escapes */ + escape_newlines = !escape_newlines; + continue; case 'c': /* ignore comments */ comment_c = argv[a][2]; continue; @@ -1022,6 +1176,8 @@ main(int argc, char *argv[]) case TYP_RGBE: case TYP_XYZE: return( !compare_hdr() ); + case TYP_SPEC: + return( !compare_spec() ); case TYP_DEPTH: return( !compare_depth() ); case TYP_NORM: