ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/radcompare.c
Revision: 2.5
Committed: Mon Oct 15 22:21:45 2018 UTC (6 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +21 -15 lines
Log Message:
Fix for files whose first character is binary, and a few output tweaks

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: radcompare.c,v 2.4 2018/10/15 21:47:52 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 of %.8g\n",
122 progname,
123 (rel_min > 0) ? "relative " : "",
124 r1, r2, max_lim);
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: warning - '%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 ++n;
294 if (!c | (c > 127))
295 break; /* non-ascii character */
296 if (n >= 10240)
297 break; /* enough to be confident */
298 }
299 if (!n)
300 return(-1); /* first read failed */
301 if (fseek(fin, 0L, 0) < 0)
302 return(-1); /* rewind failed */
303 return(!c | (c > 127));
304 }
305
306 /* Identify and return data type from header (if one) */
307 static int
308 identify_type(const char *name, FILE *fin, LUTAB *htp)
309 {
310 extern const char HDRSTR[];
311 int c;
312 /* check magic header start */
313 if ((c = getc(fin)) != HDRSTR[0]) {
314 if (c == EOF) goto badeof;
315 ungetc(c, fin);
316 c = 0;
317 } else if ((c = getc(fin)) != HDRSTR[1]) {
318 if (c == EOF) goto badeof;
319 ungetc(c, fin); ungetc(HDRSTR[0], fin);
320 c = 0;
321 }
322 if (c) { /* appears to have a header */
323 char sbuf[32];
324 if (!fgets(sbuf, sizeof(sbuf), fin))
325 goto badeof;
326 adv_linecnt(htp); /* for #?ID string */
327 if (report >= REP_WARN && strncmp(sbuf, "RADIANCE", 8)) {
328 fputs(name, stdout);
329 fputs(": warning - unexpected header ID: ", stdout);
330 fputs(sbuf, stdout);
331 }
332 if (getheader(fin, setheadvar, htp) < 0) {
333 fputs(name, stderr);
334 fputs(": Unknown error reading header\n", stderr);
335 return(-1);
336 }
337 adv_linecnt(htp); /* for trailing emtpy line */
338 return(xlate_type((const char *)lu_find(htp,"FORMAT")->data));
339 }
340 c = input_is_binary(fin); /* else peek to see if binary */
341 if (c < 0) {
342 fputs(name, stderr);
343 fputs(": read/seek error\n", stderr);
344 return(-1);
345 }
346 if (c)
347 return(TYP_BINARY);
348 SET_FILE_TEXT(fin); /* originally set to binary */
349 return(TYP_TEXT);
350 badeof:
351 if (report != REP_QUIET) {
352 fputs(name, stdout);
353 fputs(": Unexpected end-of-file\n", stdout);
354 }
355 return(-1);
356 }
357
358 /* Check that overall RMS error is below threshold */
359 static int
360 good_RMS()
361 {
362 if (!nsum)
363 return(1);
364 if (diff2sum/(double)nsum > rms_lim*rms_lim) {
365 if (report != REP_QUIET)
366 printf(
367 "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit of %.5g\n",
368 progname,
369 (rel_min > 0) ? "relative " : "",
370 f1name, f2name,
371 sqrt(diff2sum/(double)nsum), rms_lim);
372 return(0);
373 }
374 if (report >= REP_VERBOSE)
375 printf("%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n",
376 progname, (rel_min > 0) ? "relative " : "",
377 f1name, f2name, sqrt(diff2sum/(double)nsum));
378 return(1);
379 }
380
381 /* Compare two inputs as generic binary files */
382 static int
383 compare_binary()
384 {
385 int c1=0, c2=0;
386
387 if (report >= REP_VERBOSE) {
388 fputs(progname, stdout);
389 fputs(": comparing inputs as binary\n", stdout);
390 }
391 for ( ; ; ) { /* exact byte matching */
392 c1 = getc(f1in);
393 c2 = getc(f2in);
394 if (c1 == EOF) {
395 if (c2 == EOF)
396 return(1); /* success! */
397 if (report != REP_QUIET) {
398 fputs(f1name, stdout);
399 fputs(": Unexpected end-of-file\n", stdout);
400 }
401 return(0);
402 }
403 if (c2 == EOF) {
404 if (report != REP_QUIET) {
405 fputs(f2name, stdout);
406 fputs(": Unexpected end-of-file\n", stdout);
407 }
408 return(0);
409 }
410 if (c1 != c2)
411 break; /* quit and report difference */
412 }
413 if (report == REP_QUIET)
414 return(0);
415 printf("%s: binary files '%s' and '%s' differ at byte offset %ld|%ld\n",
416 progname, f1name, f2name, ftell(f1in), ftell(f2in));
417 if (report >= REP_VERBOSE)
418 printf("%s: byte in '%s' is 0x%X, byte in '%s' is 0x%X\n",
419 progname, f1name, c1, f2name, c2);
420 return(0);
421 }
422
423 /* Compare two inputs as generic text files */
424 static int
425 compare_text()
426 {
427 char l1buf[4096], l2buf[4096];
428
429 if (report >= REP_VERBOSE) {
430 fputs(progname, stdout);
431 fputs(": comparing inputs as ASCII text\n", stdout);
432 }
433 /* compare a line at a time */
434 while (fgets(l1buf, sizeof(l1buf), f1in)) {
435 lin1cnt++;
436 if (!*sskip2(l1buf,0))
437 continue; /* ignore empty lines */
438 while (fgets(l2buf, sizeof(l2buf), f2in)) {
439 lin2cnt++;
440 if (*sskip2(l2buf,0))
441 break; /* found other non-empty line */
442 }
443 if (feof(f2in)) {
444 if (report != REP_QUIET) {
445 fputs(f2name, stdout);
446 fputs(": Unexpected end-of-file\n", stdout);
447 }
448 return(0);
449 }
450 /* compare non-empty lines */
451 if (!equiv_string(l1buf, l2buf)) {
452 if (report != REP_QUIET) {
453 printf("%s: inputs '%s' and '%s' differ at line %d|%d\n",
454 progname, f1name, f2name,
455 lin1cnt, lin2cnt);
456 if (report >= REP_VERBOSE) {
457 fputs("------------- Mismatch -------------\n", stdout);
458 printf("%s@%d:\t%s", f1name,
459 lin1cnt, l1buf);
460 printf("%s@%d:\t%s", f2name,
461 lin2cnt, l2buf);
462 }
463 }
464 return(0);
465 }
466 }
467 /* check for EOF on input 2 */
468 while (fgets(l2buf, sizeof(l2buf), f2in)) {
469 if (!*sskip2(l2buf,0))
470 continue;
471 if (report != REP_QUIET) {
472 fputs(f1name, stdout);
473 fputs(": Unexpected end-of-file\n", stdout);
474 }
475 return(0);
476 }
477 return(good_RMS()); /* final check for reals */
478 }
479
480 /* Compare two inputs that are known to be RGBE or XYZE images */
481 static int
482 compare_hdr()
483 {
484 RESOLU rs1, rs2;
485 COLOR *scan1, *scan2;
486 int x, y;
487
488 fgetsresolu(&rs1, f1in);
489 fgetsresolu(&rs2, f2in);
490 if (rs1.rt != rs2.rt) {
491 if (report != REP_QUIET)
492 printf(
493 "%s: Images '%s' and '%s' have different pixel ordering\n",
494 progname, f1name, f2name);
495 return(0);
496 }
497 if ((rs1.xr != rs2.xr) | (rs1.yr != rs2.yr)) {
498 if (report != REP_QUIET)
499 printf(
500 "%s: Images '%s' and '%s' are different sizes\n",
501 progname, f1name, f2name);
502 return(0);
503 }
504 scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR));
505 scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR));
506 if (!scan1 | !scan2) {
507 fprintf(stderr, "%s: out of memory in compare_hdr()\n", progname);
508 exit(2);
509 }
510 for (y = 0; y < numscans(&rs1); y++) {
511 if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) |
512 (freadscan(scan2, scanlen(&rs2), f2in) < 0)) {
513 if (report != REP_QUIET)
514 printf("%s: Unexpected end-of-file\n",
515 progname);
516 free(scan1);
517 free(scan2);
518 return(0);
519 }
520 for (x = 0; x < scanlen(&rs1); x++) {
521 if (real_check(colval(scan1[x],RED),
522 colval(scan2[x],RED)) &
523 real_check(colval(scan1[x],GRN),
524 colval(scan2[x],GRN)) &
525 real_check(colval(scan1[x],BLU),
526 colval(scan2[x],BLU)))
527 continue;
528 if (report != REP_QUIET) {
529 printf(
530 "%s: pixels at scanline %d offset %d differ\n",
531 progname, y, x);
532 if (report >= REP_VERBOSE) {
533 printf("%s: (R,G,B)=(%g,%g,%g)\n",
534 f1name, colval(scan1[x],RED),
535 colval(scan1[x],GRN),
536 colval(scan1[x],BLU));
537 printf("%s: (R,G,B)=(%g,%g,%g)\n",
538 f2name, colval(scan2[x],RED),
539 colval(scan2[x],GRN),
540 colval(scan2[x],BLU));
541 }
542 }
543 free(scan1);
544 free(scan2);
545 return(0);
546 }
547 }
548 free(scan1);
549 free(scan2);
550 return(good_RMS()); /* final check of RMS */
551 }
552
553 const char nsuffix[10][3] = { /* 1st, 2nd, 3rd, etc. */
554 "th","st","nd","rd","th","th","th","th","th","th"
555 };
556
557 /* Compare two inputs that are known to be 32-bit floating-point data */
558 static int
559 compare_float()
560 {
561 long nread = 0;
562 float f1, f2;
563
564 while (getbinary(&f1, sizeof(f1), 1, f1in)) {
565 if (!getbinary(&f2, sizeof(f2), 1, f2in))
566 goto badeof;
567 ++nread;
568 if (real_check(f1, f2))
569 continue;
570 if (report != REP_QUIET)
571 printf("%s: %ld%s float values differ\n",
572 progname, nread, nsuffix[nread%10]);
573 return(0);
574 }
575 if (!getbinary(&f2, sizeof(f2), 1, f2in))
576 return(good_RMS()); /* final check of RMS */
577 badeof:
578 if (report != REP_QUIET)
579 printf("%s: Unexpected end-of-file\n", progname);
580 return(0);
581 }
582
583 /* Compare two inputs that are known to be 64-bit floating-point data */
584 static int
585 compare_double()
586 {
587 long nread = 0;
588 double f1, f2;
589
590 while (getbinary(&f1, sizeof(f1), 1, f1in)) {
591 if (!getbinary(&f2, sizeof(f2), 1, f2in))
592 goto badeof;
593 ++nread;
594 if (real_check(f1, f2))
595 continue;
596 if (report != REP_QUIET)
597 printf("%s: %ld%s float values differ\n",
598 progname, nread, nsuffix[nread%10]);
599 return(0);
600 }
601 if (!getbinary(&f2, sizeof(f2), 1, f2in))
602 return(good_RMS()); /* final check of RMS */
603 badeof:
604 if (report != REP_QUIET)
605 printf("%s: Unexpected end-of-file\n", progname);
606 return(0);
607 }
608
609 /* Compare two Radiance files for equivalence */
610 int
611 main(int argc, char *argv[])
612 {
613 int typ1, typ2;
614 int a;
615
616 progname = argv[0];
617 for (a = 1; a < argc && argv[a][0] == '-'; a++) {
618 switch (argv[a][1]) {
619 case 'h': /* ignore header info. */
620 ign_header = !ign_header;
621 continue;
622 case 's': /* silent operation */
623 report = REP_QUIET;
624 continue;
625 case 'w': /* turn off warnings */
626 if (report > REP_ERROR)
627 report = REP_ERROR;
628 continue;
629 case 'v': /* turn on verbose mode */
630 report = REP_VERBOSE;
631 continue;
632 case 'm': /* maximum epsilon */
633 max_lim = atof(argv[++a]);
634 continue;
635 case 'r':
636 if (argv[a][2] == 'e') /* relative difference */
637 rel_min = atof(argv[++a]);
638 else if (argv[a][2] == 'm') /* RMS limit */
639 rms_lim = atof(argv[++a]);
640 else
641 usage();
642 continue;
643 case '\0': /* first file from stdin */
644 f1in = stdin;
645 f1name = stdin_name;
646 break;
647 default:
648 usage();
649 }
650 break;
651 }
652 if (a != argc-2)
653 usage();
654 if (!f1name) f1name = argv[a];
655 if (!f2name) f2name = argv[a+1];
656
657 if (!strcmp(f1name, f2name)) { /* inputs are same? */
658 if (report >= REP_WARN)
659 printf("%s: warning - identical inputs given\n",
660 progname);
661 return(0);
662 }
663 /* open inputs */
664 SET_FILE_BINARY(stdin); /* in case we're using it */
665 if (!f1in && !(f1in = fopen(f1name, "rb"))) {
666 fprintf(stderr, "%s: cannot open for reading\n", f1name);
667 return(1);
668 }
669 if (!strcmp(f2name, "-")) {
670 f2in = stdin;
671 f2name = stdin_name;
672 } else if (!(f2in = fopen(f2name, "rb"))) {
673 fprintf(stderr, "%s: cannot open for reading\n", f2name);
674 return(1);
675 }
676 /* load headers */
677 if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0)
678 return(1);
679 if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0)
680 return(1);
681 if (typ1 != typ2) {
682 if (report != REP_QUIET)
683 printf("%s: '%s' is %s and '%s' is %s\n",
684 progname, f1name, file_type[typ1],
685 f2name, file_type[typ2]);
686 return(1);
687 }
688 ign_header |= !has_header(typ1); /* check headers if indicated */
689 if (!ign_header && !headers_match(&hdr1, &hdr2))
690 return(1);
691 lu_done(&hdr1); lu_done(&hdr2);
692 if (!ign_header & (report >= REP_WARN)) {
693 if (typ1 == TYP_UNKNOWN)
694 printf("%s: warning - unrecognized format, comparing as binary\n",
695 progname);
696 if (lin1cnt != lin2cnt)
697 printf("%s: warning - headers are different lengths\n",
698 progname);
699 }
700 if (report >= REP_VERBOSE)
701 printf("%s: input file type is %s\n",
702 progname, file_type[typ1]);
703
704 switch (typ1) { /* compare based on type */
705 case TYP_BINARY:
706 case TYP_TMESH:
707 case TYP_OCTREE:
708 case TYP_RBFMESH:
709 case TYP_UNKNOWN:
710 return( !compare_binary() );
711 case TYP_TEXT:
712 case TYP_ASCII:
713 return( !compare_text() );
714 case TYP_RGBE:
715 case TYP_XYZE:
716 return( !compare_hdr() );
717 case TYP_FLOAT:
718 return( !compare_float() );
719 case TYP_DOUBLE:
720 return( !compare_double() );
721 }
722 return(1);
723 }