ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/radcompare.c
Revision: 2.1
Committed: Mon Oct 15 17:52:52 2018 UTC (5 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Created radcompare utility to check test output during automated unit testing

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2     static const char RCSid[] = "$Id$";
3     #endif
4     /*
5     * Compare Radiance files for significant differences
6     *
7     * G. Ward
8     */
9    
10     #include <stdlib.h>
11     #include <math.h>
12     #include <ctype.h>
13     #include "platform.h"
14     #include "rtio.h"
15     #include "resolu.h"
16     #include "color.h"
17     #include "lookup.h"
18     /* Reporting levels */
19     #define REP_QUIET 0 /* no reporting */
20     #define REP_ERROR 1 /* report errors only */
21     #define REP_WARN 2 /* report warnings as well */
22     #define REP_VERBOSE 3 /* verbose reporting */
23    
24     int report = REP_WARN; /* reporting level */
25    
26     int ign_header = 0; /* ignore header differences? */
27    
28     double rel_min = 1e-5; /* positive for relative comparisons */
29    
30     double rms_lim = 0.01; /* RMS difference limit */
31    
32     double max_lim = 0.25; /* difference limit if non-negative */
33    
34     int lin1cnt=0, lin2cnt=0; /* file line position */
35    
36     /* file types */
37     const char *file_type[] = {
38     "Unrecognized",
39     "TEXT_generic",
40     "ascii",
41     COLRFMT,
42     CIEFMT,
43     "BINARY_float",
44     "BINARY_double",
45     "BSDF_RBFmesh",
46     "Radiance_octree",
47     "Radiance_tmesh",
48     "BINARY_unknown",
49     NULL /* terminator */
50     };
51    
52     enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, TYP_FLOAT,
53     TYP_DOUBLE, TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, TYP_BINARY};
54    
55     #define has_header(t) (!( 1L<<(t) & (1L<<TYP_TEXT | 1L<<TYP_BINARY) ))
56    
57     /* header variables to always ignore */
58     const char *hdr_ignkey[] = {
59     "SOFTWARE",
60     "CAPDATE",
61     "GMT",
62     NULL /* terminator */
63     };
64     /* header variable settings */
65     LUTAB hdr1 = LU_SINIT(free,free);
66     LUTAB hdr2 = LU_SINIT(free,free);
67    
68     /* advance appropriate file line count */
69     #define adv_linecnt(htp) (lin1cnt += (htp == &hdr1), \
70     lin2cnt += (htp == &hdr2))
71    
72     /* input files */
73     char *progname = NULL;
74     const char stdin_name[] = "<stdin>";
75     const char *f1name=NULL, *f2name=NULL;
76     FILE *f1in=NULL, *f2in=NULL;
77     /* running real differences */
78     double diff2sum = 0;
79     int nsum = 0;
80    
81     /* Report usage and exit */
82     static void
83     usage()
84     {
85     fputs("Usage: ", stderr);
86     fputs(progname, stderr);
87     fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] file1 file2\n",
88     stderr);
89     exit(1);
90     }
91    
92     /* Get type ID from name (or 0 if not found) */
93     static int
94     xlate_type(const char *nm)
95     {
96     int i;
97    
98     if (!nm || !*nm)
99     return(TYP_UNKNOWN);
100     for (i = 1; file_type[i]; i++)
101     if (!strcmp(nm, file_type[i]))
102     return(i);
103     return(TYP_UNKNOWN);
104     }
105    
106     /* Compare real values and keep track of differences */
107     static int
108     real_check(double r1, double r2)
109     {
110     double diff2 = (r1 - r2)*(r1 - r2);
111    
112     if (rel_min > 0) { /* doing relative differences? */
113     double av2 = .25*(r1*r1 + 2.*fabs(r1*r2) + r2*r2);
114     if (av2 > rel_min*rel_min)
115     diff2 /= av2;
116     }
117     if (max_lim >= 0 && diff2 > max_lim*max_lim) {
118     if (report != REP_QUIET)
119     fprintf(stderr,
120     "%s: %sdifference between %.8g and %.8g exceeds epsilon\n",
121     progname,
122     (rel_min > 0) ? "relative " : "",
123     r1, r2);
124     return(0);
125     }
126     diff2sum += diff2;
127     nsum++;
128     return(1);
129     }
130    
131     /* Compare two strings for equivalence */
132     static int
133     equiv_string(char *s1, char *s2)
134     {
135     #define CLS_STR 0
136     #define CLS_INT 1
137     #define CLS_FLT 2
138     /* skip whitespace at beginning */
139     while (isspace(*s1)) s1++;
140     while (isspace(*s2)) s2++;
141     if (!strcmp(s1, s2)) /* quick check */
142     return(1);
143     while (*s1) { /* check each word */
144     int inquote = *s1;
145     if ((inquote != '\'') & (inquote != '"'))
146     inquote = 0;
147     if (inquote) { /* quoted text must match exactly */
148     if (*s1++ != *s2++)
149     return(0);
150     while (*s1 != inquote) {
151     if (!*s1)
152     return(0);
153     if (*s1++ != *s2++)
154     return(0);
155     }
156     s1++;
157     if (*s2++ != inquote)
158     return(0);
159     } else { /* else classify word type */
160     char *s1s = s1;
161     char *s2s = s2;
162     int cls = CLS_STR;
163     s1 = sskip(s1);
164     s2 = sskip(s2);
165     if (iskip(s1s) == s1) {
166     if (iskip(s2s) == s2)
167     cls = CLS_INT;
168     else if (fskip(s2s) == s2)
169     cls = CLS_FLT;
170     } else if (fskip(s1s) == s1) {
171     if (fskip(s2s) != s2)
172     return(0);
173     cls = CLS_FLT;
174     }
175     switch (cls) {
176     case CLS_INT: /* strncmp() faster */
177     case CLS_STR:
178     if (s1 - s1s != s2 - s2s)
179     return(0);
180     if (strncmp(s1s, s2s, s1 - s1s))
181     return(0);
182     break;
183     case CLS_FLT:
184     if (!real_check(atof(s1s), atof(s2s)))
185     return(0);
186     break;
187     }
188     }
189     while (isspace(*s1)) s1++;
190     while (isspace(*s2)) s2++;
191     }
192     return(!*s2); /* match if we reached the end of s2, too */
193     #undef CLS_STR
194     #undef CLS_INT
195     #undef CLS_FLT
196     }
197    
198     /* Check if string is var=value pair and set if not in ignore list */
199     static int
200     setheadvar(char *val, void *p)
201     {
202     LUTAB *htp = (LUTAB *)p;
203     LUENT *tep;
204     char *key;
205     int kln, vln;
206     int n;
207    
208     if (!isalpha(*val)) /* key must start line */
209     return(0);
210     key = val++;
211     while (*val && !isspace(*val) & (*val != '='))
212     val++;
213     kln = val - key;
214     while (isspace(*val)) /* check for value */
215     *val++ = '\0';
216     if (*val != '=')
217     return(0);
218     *val++ = '\0';
219     while (isspace(*val))
220     val++;
221     if (!*val) /* nothing set? */
222     return(0);
223     vln = strlen(val); /* eliminate space and newline at end */
224     while (--vln > 0 && isspace(val[vln]))
225     ;
226     val[++vln] = '\0';
227     /* check if key to ignore */
228     for (n = 0; hdr_ignkey[n]; n++)
229     if (!strcmp(key, hdr_ignkey[n]))
230     return(0);
231     if (!(tep = lu_find(htp, key)))
232     return(-1);
233     if (!tep->key)
234     tep->key = strcpy(malloc(kln+1), key);
235     if (tep->data)
236     free(tep->data);
237     tep->data = strcpy(malloc(vln+1), val);
238     adv_linecnt(htp);
239     return(1);
240     }
241    
242     /* Lookup correspondent in other header */
243     static int
244     match_val(const LUENT *ep1, void *p2)
245     {
246     const LUENT *ep2 = lu_find((LUTAB *)p2, ep1->key);
247     if (!ep2 || !ep2->data) {
248     if (report != REP_QUIET)
249     fprintf(stderr, "%s: Variable '%s' missing in '%s'\n",
250     progname, ep1->key, f2name);
251     return(-1);
252     }
253     if (!equiv_string((char *)ep1->data, (char *)ep2->data)) {
254     if (report != REP_QUIET)
255     fprintf(stderr, "%s: Header variables '%s' have different values\n",
256     progname, ep1->key);
257     return(-1);
258     }
259     return(1); /* good match */
260     }
261    
262     /* Compare two sets of header variables */
263     static int
264     headers_match(LUTAB *hp1, LUTAB *hp2)
265     {
266     int ne = lu_doall(hp1, match_val, hp2);
267     if (ne < 0)
268     return(0); /* something didn't match! */
269     ne = lu_doall(hp2, NULL, NULL) - ne;
270     if (ne) {
271     if (report != REP_QUIET)
272     fprintf(stderr, "%s: '%s' has %d extra header variable(s)\n",
273     progname, f2name, ne);
274     return(0);
275     }
276     return(1); /* good match */
277     }
278    
279     /* Check generic input to determine if it is binary, -1 on error */
280     static int
281     input_is_binary(FILE *fin)
282     {
283     int n = 0;
284     int c = 0;
285    
286     while ((c = getc(fin)) != EOF) {
287     if (!c | (c > 127))
288     break; /* non-ascii character */
289     if (++n >= 10240)
290     break; /* enough to be confident */
291     }
292     if (!n)
293     return(-1); /* first read failed */
294     if (fseek(fin, 0L, 0) < 0)
295     return(-1); /* rewind failed */
296     return(!c | (c > 127));
297     }
298    
299     /* Identify and return data type from header (if one) */
300     static int
301     identify_type(const char *name, FILE *fin, LUTAB *htp)
302     {
303     extern const char HDRSTR[];
304     int c;
305     /* check magic header start */
306     if ((c = getc(fin)) != HDRSTR[0]) {
307     if (c == EOF) goto badeof;
308     ungetc(c, fin);
309     c = 0;
310     } else if ((c = getc(fin)) != HDRSTR[1]) {
311     if (c == EOF) goto badeof;
312     ungetc(c, fin); ungetc(HDRSTR[0], fin);
313     c = 0;
314     }
315     if (c) { /* appears to have a header */
316     char sbuf[32];
317     if (!fgets(sbuf, sizeof(sbuf), fin))
318     goto badeof;
319     if (report >= REP_WARN && strncmp(sbuf, "RADIANCE", 8)) {
320     fputs(name, stderr);
321     fputs(": warning - unexpected header ID: ", stderr);
322     fputs(sbuf, stderr);
323     }
324     adv_linecnt(htp); /* for #?ID string */
325     if (getheader(fin, setheadvar, htp) < 0) {
326     fputs(name, stderr);
327     fputs(": Unknown error reading header\n", stderr);
328     return(-1);
329     }
330     adv_linecnt(htp); /* for trailing emtpy line */
331     return(xlate_type((const char *)lu_find(htp,"FORMAT")->data));
332     }
333     c = input_is_binary(fin); /* else peek to see if binary */
334     if (c < 0) {
335     fputs(name, stderr);
336     fputs(": read/seek error\n", stderr);
337     return(-1);
338     }
339     if (c)
340     return(TYP_BINARY);
341     SET_FILE_TEXT(fin); /* originally set to binary */
342     return(TYP_TEXT);
343     badeof:
344     if (report != REP_QUIET) {
345     fputs(name, stderr);
346     fputs(": Unexpected end-of-file\n", stderr);
347     }
348     return(-1);
349     }
350    
351     /* Check that overall RMS error is below threshold */
352     static int
353     good_RMS()
354     {
355     if (!nsum)
356     return(1);
357     if (diff2sum/(double)nsum > rms_lim*rms_lim) {
358     if (report != REP_QUIET)
359     fprintf(stderr,
360     "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit\n",
361     progname,
362     (rel_min > 0) ? "relative " : "",
363     f1name, f2name,
364     sqrt(diff2sum/(double)nsum));
365     return(0);
366     }
367     if (report >= REP_VERBOSE)
368     fprintf(stderr,
369     "%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n",
370     progname, (rel_min > 0) ? "relative " : "",
371     f1name, f2name, sqrt(diff2sum/(double)nsum));
372     return(1);
373     }
374    
375     /* Compare two inputs as generic binary files */
376     static int
377     compare_binary()
378     {
379     if (report >= REP_VERBOSE) {
380     fputs(progname, stderr);
381     fputs(": comparing inputs as binary\n", stderr);
382     }
383     for ( ; ; ) { /* exact byte matching */
384     int c1 = getc(f1in);
385     int c2 = getc(f2in);
386     if (c1 == EOF) {
387     if (c2 == EOF)
388     return(1); /* success! */
389     if (report != REP_QUIET) {
390     fputs(f1name, stderr);
391     fputs(": Unexpected end-of-file\n", stderr);
392     }
393     return(0);
394     }
395     if (c2 == EOF) {
396     if (report != REP_QUIET) {
397     fputs(f2name, stderr);
398     fputs(": Unexpected end-of-file\n", stderr);
399     }
400     return(0);
401     }
402     if (c1 != c2)
403     break; /* quit and report difference */
404     }
405     if (report == REP_QUIET)
406     return(0);
407     fprintf(stderr, "%s: binary files '%s' and '%s' differ at offset %ld|%ld\n",
408     progname, f1name, f2name, ftell(f1in), ftell(f2in));
409     return(0);
410     }
411    
412     /* Compare two inputs as generic text files */
413     static int
414     compare_text()
415     {
416     char l1buf[4096], l2buf[4096];
417    
418     if (report >= REP_VERBOSE) {
419     fputs(progname, stderr);
420     fputs(": comparing inputs as ASCII text\n", stderr);
421     }
422     /* compare a line at a time */
423     while (fgets(l1buf, sizeof(l1buf), f1in)) {
424     lin1cnt++;
425     if (!*sskip2(l1buf,0))
426     continue; /* ignore empty lines */
427     while (fgets(l2buf, sizeof(l2buf), f2in)) {
428     lin2cnt++;
429     if (*sskip2(l2buf,0))
430     break; /* found other non-empty line */
431     }
432     if (feof(f2in)) {
433     if (report != REP_QUIET) {
434     fputs(f2name, stderr);
435     fputs(": Unexpected end-of-file\n", stderr);
436     }
437     return(0);
438     }
439     /* compare non-empty lines */
440     if (!equiv_string(l1buf, l2buf)) {
441     if (report != REP_QUIET) {
442     fprintf(stderr, "%s: inputs '%s' and '%s' differ at line %d|%d\n",
443     progname, f1name, f2name,
444     lin1cnt, lin2cnt);
445     if (report >= REP_VERBOSE) {
446     fputs("------------- Mismatch -------------\n", stderr);
447     fprintf(stderr, "%s(%d):\t%s", f1name,
448     lin1cnt, l1buf);
449     fprintf(stderr, "%s(%d):\t%s", f2name,
450     lin2cnt, l2buf);
451     }
452     }
453     return(0);
454     }
455     }
456     /* check for EOF on input 2 */
457     while (fgets(l2buf, sizeof(l2buf), f2in)) {
458     if (!*sskip2(l2buf,0))
459     continue;
460     if (report != REP_QUIET) {
461     fputs(f1name, stderr);
462     fputs(": Unexpected end-of-file\n", stderr);
463     }
464     return(0);
465     }
466     return(good_RMS()); /* final check for reals */
467     }
468    
469     /* Compare two inputs that are known to be RGBE or XYZE images */
470     static int
471     compare_hdr()
472     {
473     RESOLU rs1, rs2;
474     COLOR *scan1, *scan2;
475     int x, y;
476    
477     fgetsresolu(&rs1, f1in);
478     fgetsresolu(&rs2, f2in);
479     if (rs1.rt != rs2.rt) {
480     if (report != REP_QUIET)
481     fprintf(stderr,
482     "%s: Images '%s' and '%s' have different pixel ordering\n",
483     progname, f1name, f2name);
484     return(0);
485     }
486     if ((rs1.xr != rs2.xr) | (rs1.yr != rs2.yr)) {
487     if (report != REP_QUIET)
488     fprintf(stderr,
489     "%s: Images '%s' and '%s' are different sizes\n",
490     progname, f1name, f2name);
491     return(0);
492     }
493     scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR));
494     scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR));
495     if (!scan1 | !scan2) {
496     fprintf(stderr, "%s: out of memory in compare_hdr()\n", progname);
497     exit(2);
498     }
499     for (y = 0; y < numscans(&rs1); y++) {
500     if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) |
501     (freadscan(scan2, scanlen(&rs2), f2in) < 0)) {
502     if (report != REP_QUIET)
503     fprintf(stderr, "%s: Unexpected end-of-file\n",
504     progname);
505     free(scan1);
506     free(scan2);
507     return(0);
508     }
509     for (x = scanlen(&rs1); x--; ) {
510     if (real_check(colval(scan1[x],RED),
511     colval(scan2[x],RED)) &
512     real_check(colval(scan1[x],GRN),
513     colval(scan2[x],GRN)) &
514     real_check(colval(scan1[x],BLU),
515     colval(scan2[x],BLU)))
516     continue;
517     if (report != REP_QUIET) {
518     fprintf(stderr,
519     "%s: pixels at scanline %d offset %d differ\n",
520     progname, y, x);
521     if (report >= REP_VERBOSE) {
522     fprintf(stderr, "%s: (R,G,B)=(%g,%g,%g)\n",
523     f1name, colval(scan1[x],RED),
524     colval(scan1[x],GRN),
525     colval(scan1[x],BLU));
526     fprintf(stderr, "%s: (R,G,B)=(%g,%g,%g)\n",
527     f2name, colval(scan2[x],RED),
528     colval(scan2[x],GRN),
529     colval(scan2[x],BLU));
530     }
531     }
532     free(scan1);
533     free(scan2);
534     return(0);
535     }
536     }
537     free(scan1);
538     free(scan2);
539     return(good_RMS()); /* final check of RMS */
540     }
541    
542     const char nsuffix[10][3] = { /* 1st, 2nd, 3rd, etc. */
543     "th","st","nd","rd","th","th","th","th","th","th"
544     };
545    
546     /* Compare two inputs that are known to be 32-bit floating-point data */
547     static int
548     compare_float()
549     {
550     long nread = 0;
551     float f1, f2;
552    
553     while (getbinary(&f1, sizeof(f1), 1, f1in)) {
554     if (!getbinary(&f2, sizeof(f2), 1, f2in))
555     goto badeof;
556     ++nread;
557     if (real_check(f1, f2))
558     continue;
559     if (report != REP_QUIET)
560     fprintf(stderr, "%s: %ld%s float values differ\n",
561     progname, nread, nsuffix[nread%10]);
562     return(0);
563     }
564     if (!getbinary(&f2, sizeof(f2), 1, f2in))
565     return(good_RMS()); /* final check of RMS */
566     badeof:
567     if (report != REP_QUIET)
568     fprintf(stderr, "%s: Unexpected end-of-file\n", progname);
569     return(0);
570     }
571    
572     /* Compare two inputs that are known to be 64-bit floating-point data */
573     static int
574     compare_double()
575     {
576     long nread = 0;
577     double f1, f2;
578    
579     while (getbinary(&f1, sizeof(f1), 1, f1in)) {
580     if (!getbinary(&f2, sizeof(f2), 1, f2in))
581     goto badeof;
582     ++nread;
583     if (real_check(f1, f2))
584     continue;
585     if (report != REP_QUIET)
586     fprintf(stderr, "%s: %ld%s float values differ\n",
587     progname, nread, nsuffix[nread%10]);
588     return(0);
589     }
590     if (!getbinary(&f2, sizeof(f2), 1, f2in))
591     return(good_RMS()); /* final check of RMS */
592     badeof:
593     if (report != REP_QUIET)
594     fprintf(stderr, "%s: Unexpected end-of-file\n", progname);
595     return(0);
596     }
597    
598     /* Compare two Radiance files for equivalence */
599     int
600     main(int argc, char *argv[])
601     {
602     int typ1, typ2;
603     int a;
604    
605     progname = argv[0];
606     for (a = 1; a < argc && argv[a][0] == '-'; a++) {
607     switch (argv[a][1]) {
608     case 'h': /* ignore header info. */
609     ign_header = !ign_header;
610     continue;
611     case 's': /* silent operation */
612     report = REP_QUIET;
613     continue;
614     case 'w': /* turn off warnings */
615     if (report > REP_ERROR)
616     report = REP_ERROR;
617     continue;
618     case 'v': /* turn on verbose mode */
619     report = REP_VERBOSE;
620     continue;
621     case 'm': /* maximum epsilon */
622     max_lim = atof(argv[++a]);
623     continue;
624     case 'r':
625     if (argv[a][2] == 'e') /* relative difference */
626     rel_min = atof(argv[++a]);
627     else if (argv[a][2] == 'm') /* RMS limit */
628     rms_lim = atof(argv[++a]);
629     else
630     usage();
631     continue;
632     case '\0': /* first file from stdin */
633     f1in = stdin;
634     f1name = stdin_name;
635     break;
636     default:
637     usage();
638     }
639     break;
640     }
641     if (a != argc-2)
642     usage();
643     if (!f1name) f1name = argv[a];
644     if (!f2name) f2name = argv[a+1];
645    
646     if (!strcmp(f1name, f2name)) { /* inputs are same? */
647     if (report >= REP_WARN)
648     fprintf(stderr, "%s: warning - identical inputs given\n",
649     progname);
650     return(0);
651     }
652     /* open inputs */
653     SET_FILE_BINARY(stdin); /* in case we're using it */
654     if (!f1in && !(f1in = fopen(f1name, "rb"))) {
655     fprintf(stderr, "%s: cannot open for reading\n", f1name);
656     return(1);
657     }
658     if (!strcmp(f2name, "-")) {
659     f2in = stdin;
660     f2name = stdin_name;
661     } else if (!(f2in = fopen(f2name, "rb"))) {
662     fprintf(stderr, "%s: cannot open for reading\n", f2name);
663     return(1);
664     }
665     /* load headers */
666     if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0)
667     return(1);
668     if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0)
669     return(1);
670     if (typ1 != typ2) {
671     if (report != REP_QUIET)
672     fprintf(stderr, "%s: '%s' is %s and '%s' is %s\n",
673     progname, f1name, file_type[typ1],
674     f2name, file_type[typ2]);
675     return(1);
676     }
677     ign_header |= !has_header(typ1); /* check headers if indicated */
678     if (!ign_header && !headers_match(&hdr1, &hdr2))
679     return(1);
680     lu_done(&hdr1); lu_done(&hdr2);
681     if (!ign_header & (report >= REP_WARN)) {
682     if (typ1 == TYP_UNKNOWN)
683     fprintf(stderr, "%s: warning - unrecognized format, comparing as binary\n",
684     progname);
685     if (lin1cnt != lin2cnt)
686     fprintf(stderr, "%s: warning - headers are different lengths\n",
687     progname);
688     }
689     if (report >= REP_VERBOSE)
690     fprintf(stderr, "%s: input file type is %s\n",
691     progname, file_type[typ1]);
692    
693     switch (typ1) { /* compare based on type */
694     case TYP_BINARY:
695     case TYP_TMESH:
696     case TYP_OCTREE:
697     case TYP_RBFMESH:
698     case TYP_UNKNOWN:
699     return( !compare_binary() );
700     case TYP_TEXT:
701     case TYP_ASCII:
702     return( !compare_text() );
703     case TYP_RGBE:
704     case TYP_XYZE:
705     return( !compare_hdr() );
706     case TYP_FLOAT:
707     return( !compare_float() );
708     case TYP_DOUBLE:
709     return( !compare_double() );
710     }
711     return(1);
712     }