ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/radcompare.c
Revision: 2.3
Committed: Mon Oct 15 18:31:15 2018 UTC (5 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +4 -3 lines
Log Message:
Fixed innaccurate header line count

File Contents

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