ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/radcompare.c
Revision: 2.4
Committed: Mon Oct 15 21:47:52 2018 UTC (5 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +58 -55 lines
Log Message:
Fixed detection of float/double inputs and changed all regular output to stdout

File Contents

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