ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/radcompare.c
Revision: 2.20
Committed: Wed Aug 14 18:20:02 2019 UTC (4 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.19: +14 -1 lines
Log Message:
Added explicit byte-swap checks in headers using BigEndian= line

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: radcompare.c,v 2.19 2019/08/14 04:18:12 greg Exp $";
3 #endif
4 /*
5 * Compare Radiance files for significant differences
6 *
7 * G. Ward
8 */
9
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include "rtmath.h"
13 #include "platform.h"
14 #include "rtio.h"
15 #include "resolu.h"
16 #include "color.h"
17 #include "depthcodec.h"
18 #include "normcodec.h"
19 #include "lookup.h"
20 /* Reporting levels */
21 #define REP_QUIET 0 /* no reporting */
22 #define REP_ERROR 1 /* report errors only */
23 #define REP_WARN 2 /* report warnings as well */
24 #define REP_VERBOSE 3 /* verbose reporting */
25
26 int report = REP_WARN; /* reporting level */
27
28 int ign_header = 0; /* ignore header differences? */
29
30 double rel_min = 1e-5; /* positive for relative comparisons */
31
32 double rms_lim = 0.01; /* RMS difference limit */
33
34 double max_lim = 0.25; /* difference limit if non-negative */
35
36 int lin1cnt=0, lin2cnt=0; /* file line position */
37
38 const char nsuffix[10][3] = { /* 1st, 2nd, 3rd, etc. */
39 "th","st","nd","rd","th","th","th","th","th","th"
40 };
41 #define num_sfx(n) nsuffix[(n)%10]
42
43 /* file types */
44 const char *file_type[] = {
45 "Unrecognized",
46 "TEXT_generic",
47 "ascii",
48 COLRFMT,
49 CIEFMT,
50 DEPTH16FMT,
51 NORMAL32FMT,
52 "float",
53 "double",
54 "BSDF_RBFmesh",
55 "Radiance_octree",
56 "Radiance_tmesh",
57 "8-bit_indexed_name",
58 "16-bit_indexed_name",
59 "24-bit_indexed_name",
60 "BINARY_unknown",
61 NULL /* terminator */
62 };
63 /* keep consistent with above */
64 enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE,
65 TYP_DEPTH, TYP_NORM, TYP_FLOAT, TYP_DOUBLE,
66 TYP_RBFMESH, TYP_OCTREE, TYP_TMESH,
67 TYP_ID8, TYP_ID16, TYP_ID24, TYP_BINARY};
68
69 #define has_header(t) (!( 1L<<(t) & (1L<<TYP_TEXT | 1L<<TYP_BINARY) ))
70
71 /* header variables to always ignore */
72 const char *hdr_ignkey[] = {
73 "SOFTWARE",
74 "CAPDATE",
75 "GMT",
76 "FRAME",
77 NULL /* terminator */
78 };
79 /* header variable settings */
80 LUTAB hdr1 = LU_SINIT(free,free);
81 LUTAB hdr2 = LU_SINIT(free,free);
82
83 /* advance appropriate file line count */
84 #define adv_linecnt(htp) (lin1cnt += (htp == &hdr1), \
85 lin2cnt += (htp == &hdr2))
86
87 typedef struct { /* dynamic line buffer */
88 char *str;
89 int len;
90 int siz;
91 } LINEBUF;
92
93 #define init_line(bp) ((bp)->str = NULL, (bp)->siz = 0)
94 /* 100 MByte limit on line buffer */
95 #define MAXBUF (100L<<20)
96
97 /* input files */
98 char *progname = NULL;
99 const char stdin_name[] = "<stdin>";
100 const char *f1name=NULL, *f2name=NULL;
101 FILE *f1in=NULL, *f2in=NULL;
102 int f1swap=0, f2swap=0;
103
104 /* running real differences */
105 double diff2sum = 0;
106 long nsum = 0;
107
108 /* Report usage and exit */
109 static void
110 usage()
111 {
112 fputs("Usage: ", stderr);
113 fputs(progname, stderr);
114 fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n",
115 stderr);
116 exit(2);
117 }
118
119 /* Read a text line, increasing buffer size as necessary */
120 static int
121 read_line(LINEBUF *bp, FILE *fp)
122 {
123 static int doneWarn = 0;
124
125 bp->len = 0;
126 if (!bp->str) {
127 bp->str = (char *)malloc(bp->siz = 512);
128 if (!bp->str)
129 goto memerr;
130 }
131 while (fgets(bp->str + bp->len, bp->siz - bp->len, fp)) {
132 bp->len += strlen(bp->str + bp->len);
133 if (bp->str[bp->len-1] == '\n')
134 break; /* found EOL */
135 if (bp->len < bp->siz - 4)
136 continue; /* at EOF? */
137 if (bp->siz >= MAXBUF) {
138 if ((report >= REP_WARN) & !doneWarn) {
139 fprintf(stderr,
140 "%s: warning - input line(s) past %ld MByte limit\n",
141 progname, MAXBUF>>20);
142 doneWarn++;
143 }
144 break; /* return MAXBUF partial line */
145 }
146 if ((bp->siz += bp->siz/2) > MAXBUF)
147 bp->siz = MAXBUF;
148 bp->str = (char *)realloc(bp->str, bp->siz);
149 if (!bp->str)
150 goto memerr;
151 }
152 return(bp->len);
153 memerr:
154 fprintf(stderr,
155 "%s: out of memory in read_line() allocating %d byte buffer\n",
156 progname, bp->siz);
157 exit(2);
158 }
159
160 /* Free line buffer */
161 static void
162 free_line(LINEBUF *bp)
163 {
164 if (bp->str) free(bp->str);
165 init_line(bp);
166 }
167
168 /* Get type ID from name (or 0 if not found) */
169 static int
170 xlate_type(const char *nm)
171 {
172 int i;
173
174 if (!nm || !*nm)
175 return(TYP_UNKNOWN);
176 for (i = 1; file_type[i]; i++)
177 if (!strcmp(nm, file_type[i]))
178 return(i);
179 return(TYP_UNKNOWN);
180 }
181
182 /* Compare real values and keep track of differences */
183 static int
184 real_check(double r1, double r2)
185 {
186 double diff2 = (r1 - r2)*(r1 - r2);
187
188 if (rel_min > 0) { /* doing relative differences? */
189 double av2 = .25*(r1*r1 + 2.*fabs(r1*r2) + r2*r2);
190 if (av2 > rel_min*rel_min)
191 diff2 /= av2;
192 }
193 if (max_lim >= 0 && diff2 > max_lim*max_lim) {
194 if (report != REP_QUIET)
195 printf(
196 "%s: %sdifference between %.8g and %.8g exceeds epsilon of %.8g\n",
197 progname,
198 (rel_min > 0) ? "relative " : "",
199 r1, r2, max_lim);
200 return(0);
201 }
202 diff2sum += diff2;
203 nsum++;
204 return(1);
205 }
206
207 /* Compare two color values for equivalence */
208 static int
209 color_check(COLOR c1, COLOR c2)
210 {
211 int p;
212
213 if (!real_check((colval(c1,RED)+colval(c1,GRN)+colval(c1,BLU))*(1./3.),
214 (colval(c2,RED)+colval(c2,GRN)+colval(c2,BLU))*(1./3.)))
215 return(0);
216
217 p = (colval(c1,GRN) > colval(c1,RED)) ? GRN : RED;
218 if (colval(c1,BLU) > colval(c1,p)) p = BLU;
219
220 return(real_check(colval(c1,p), colval(c2,p)));
221 }
222
223 /* Compare two normal directions for equivalence */
224 static int
225 norm_check(FVECT nv1, FVECT nv2)
226 {
227 double max2 = nv1[2]*nv1[2];
228 int imax = 2;
229 int i = 2;
230 /* identify largest component */
231 while (i--) {
232 double tm2 = nv1[i]*nv1[i];
233 if (tm2 > max2) {
234 imax = i;
235 max2 = tm2;
236 }
237 }
238 i = 3; /* compare smaller components */
239 while (i--) {
240 if (i == imax)
241 continue;
242 if (!real_check(nv1[i], nv2[i]))
243 return(0);
244 }
245 return(1);
246 }
247
248 /* Compare two strings for equivalence */
249 static int
250 equiv_string(char *s1, char *s2)
251 {
252 #define CLS_STR 0
253 #define CLS_INT 1
254 #define CLS_FLT 2
255 /* skip whitespace at beginning */
256 while (isspace(*s1)) s1++;
257 while (isspace(*s2)) s2++;
258 while (*s1) { /* check each word */
259 int inquote;
260 if (!*s2) /* unexpected EOL in s2? */
261 return(0);
262 inquote = *s1;
263 if ((inquote != '\'') & (inquote != '"'))
264 inquote = 0;
265 if (inquote) { /* quoted text must match exactly */
266 if (*s1++ != *s2++)
267 return(0);
268 while (*s1 != inquote) {
269 if (!*s1)
270 return(0);
271 if (*s1++ != *s2++)
272 return(0);
273 }
274 s1++;
275 if (*s2++ != inquote)
276 return(0);
277 } else { /* else classify word type */
278 char *s1s = s1;
279 char *s2s = s2;
280 int cls = CLS_STR;
281 s1 = sskip(s1);
282 s2 = sskip(s2);
283 if (iskip(s1s) == s1) {
284 if (iskip(s2s) == s2)
285 cls = CLS_INT;
286 else if (fskip(s2s) == s2)
287 cls = CLS_FLT;
288 } else if (fskip(s1s) == s1) {
289 if (fskip(s2s) != s2)
290 return(0);
291 cls = CLS_FLT;
292 }
293 switch (cls) {
294 case CLS_INT: /* strncmp() faster */
295 case CLS_STR:
296 if (s1 - s1s != s2 - s2s)
297 return(0);
298 if (strncmp(s1s, s2s, s1 - s1s))
299 return(0);
300 break;
301 case CLS_FLT:
302 if (!real_check(atof(s1s), atof(s2s)))
303 return(0);
304 break;
305 }
306 }
307 while (isspace(*s1)) s1++;
308 while (isspace(*s2)) s2++;
309 }
310 return(!*s2); /* match if we reached the end of s2, too */
311 #undef CLS_STR
312 #undef CLS_INT
313 #undef CLS_FLT
314 }
315
316 /* Check if string is var=value pair and set if not in ignore list */
317 static int
318 setheadvar(char *val, void *p)
319 {
320 LUTAB *htp = (LUTAB *)p;
321 LUENT *tep;
322 char *key;
323 int kln, vln;
324 int n;
325
326 adv_linecnt(htp); /* side-effect is to count lines */
327 if (!isalpha(*val)) /* key must start line */
328 return(0);
329 /* check if we need to swap binary data */
330 if ((n = isbigendian(val)) >= 0) {
331 if (nativebigendian() == n)
332 return(0);
333 f1swap += (htp == &hdr1);
334 f2swap += (htp == &hdr2);
335 return(0);
336 }
337 key = val++;
338 while (*val && !isspace(*val) & (*val != '='))
339 val++;
340 kln = val - key;
341 while (isspace(*val)) /* check for value */
342 *val++ = '\0';
343 if (*val != '=')
344 return(0);
345 *val++ = '\0';
346 while (isspace(*val))
347 val++;
348 if (!*val) /* nothing set? */
349 return(0);
350 /* check if key to ignore */
351 for (n = 0; hdr_ignkey[n]; n++)
352 if (!strcmp(key, hdr_ignkey[n]))
353 return(0);
354 vln = strlen(val); /* eliminate space and newline at end */
355 while (isspace(val[--vln]))
356 ;
357 val[++vln] = '\0';
358 if (!(tep = lu_find(htp, key)))
359 return(-1); /* memory allocation error */
360 if (!tep->key)
361 tep->key = strcpy(malloc(kln+1), key);
362 if (tep->data)
363 free(tep->data);
364 tep->data = strcpy(malloc(vln+1), val);
365 return(1);
366 }
367
368 /* Lookup correspondent in other header */
369 static int
370 match_val(const LUENT *ep1, void *p2)
371 {
372 const LUENT *ep2 = lu_find((LUTAB *)p2, ep1->key);
373 if (!ep2 || !ep2->data) {
374 if (report != REP_QUIET)
375 printf("%s: variable '%s' missing in '%s'\n",
376 progname, ep1->key, f2name);
377 return(-1);
378 }
379 if (!equiv_string((char *)ep1->data, (char *)ep2->data)) {
380 if (report != REP_QUIET) {
381 printf("%s: header variable '%s' has different values\n",
382 progname, ep1->key);
383 if (report >= REP_VERBOSE) {
384 printf("%s: %s=%s\n", f1name,
385 ep1->key, (char *)ep1->data);
386 printf("%s: %s=%s\n", f2name,
387 ep2->key, (char *)ep2->data);
388 }
389 }
390 return(-1);
391 }
392 return(1); /* good match */
393 }
394
395 /* Compare two sets of header variables */
396 static int
397 headers_match()
398 {
399 int ne = lu_doall(&hdr1, match_val, &hdr2);
400 if (ne < 0)
401 return(0); /* something didn't match! */
402 /* non-fatal if second header has extra */
403 if (report >= REP_WARN && (ne = lu_doall(&hdr2, NULL, NULL) - ne))
404 printf("%s: warning - '%s' has %d extra header setting(s)\n",
405 progname, f2name, ne);
406 return(1); /* good match */
407 }
408
409 /* Check generic input to determine if it is binary, -1 on error */
410 static int
411 input_is_binary(FILE *fin)
412 {
413 int n = 0;
414 int c = 0;
415
416 while ((c = getc(fin)) != EOF) {
417 ++n;
418 if (!c | (c > 127))
419 break; /* non-ascii character */
420 if (n >= 10240)
421 break; /* enough to be confident */
422 }
423 if (!n)
424 return(-1); /* first read failed */
425 if (fseek(fin, 0L, 0) < 0)
426 return(-1); /* rewind failed */
427 return(!c | (c > 127));
428 }
429
430 /* Identify and return data type from header (if one) */
431 static int
432 identify_type(const char *name, FILE *fin, LUTAB *htp)
433 {
434 extern const char HDRSTR[];
435 int c;
436 /* check magic header start */
437 if ((c = getc(fin)) != HDRSTR[0]) {
438 if (c == EOF) goto badeof;
439 ungetc(c, fin);
440 c = 0;
441 } else if ((c = getc(fin)) != HDRSTR[1]) {
442 if (c == EOF) goto badeof;
443 ungetc(c, fin); ungetc(HDRSTR[0], fin);
444 c = 0;
445 }
446 if (c) { /* appears to have a header */
447 char sbuf[32];
448 if (!fgets(sbuf, sizeof(sbuf), fin))
449 goto badeof;
450 adv_linecnt(htp); /* for #?ID string */
451 if (report >= REP_WARN && strncmp(sbuf, "RADIANCE", 8)) {
452 fputs(name, stdout);
453 fputs(": warning - unexpected header ID: ", stdout);
454 fputs(sbuf, stdout);
455 }
456 if (getheader(fin, setheadvar, htp) < 0) {
457 fputs(name, stderr);
458 fputs(": unknown error reading header\n", stderr);
459 return(-1);
460 }
461 adv_linecnt(htp); /* for trailing emtpy line */
462 return(xlate_type((const char *)lu_find(htp,"FORMAT")->data));
463 }
464 c = input_is_binary(fin); /* else peek to see if binary */
465 if (c < 0) {
466 fputs(name, stderr);
467 fputs(": read/seek error\n", stderr);
468 return(-1);
469 }
470 if (c)
471 return(TYP_BINARY);
472 SET_FILE_TEXT(fin); /* originally set to binary */
473 return(TYP_TEXT);
474 badeof:
475 if (report != REP_QUIET) {
476 fputs(name, stdout);
477 fputs(": unexpected end-of-file\n", stdout);
478 }
479 return(-1);
480 }
481
482 /* Check that overall RMS error is below threshold */
483 static int
484 good_RMS()
485 {
486 if (!nsum)
487 return(1);
488 if (diff2sum/(double)nsum > rms_lim*rms_lim) {
489 if (report != REP_QUIET)
490 printf(
491 "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit of %.5g\n",
492 progname,
493 (rel_min > 0) ? "relative " : "",
494 f1name, f2name,
495 sqrt(diff2sum/(double)nsum), rms_lim);
496 return(0);
497 }
498 if (report >= REP_VERBOSE)
499 printf("%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n",
500 progname, (rel_min > 0) ? "relative " : "",
501 f1name, f2name, sqrt(diff2sum/(double)nsum));
502 return(1);
503 }
504
505 /* Compare two inputs as generic binary files */
506 static int
507 compare_binary()
508 {
509 int c1=0, c2=0;
510
511 if (report >= REP_VERBOSE) {
512 fputs(progname, stdout);
513 fputs(": comparing inputs as binary\n", stdout);
514 }
515 for ( ; ; ) { /* exact byte matching */
516 c1 = getc(f1in);
517 c2 = getc(f2in);
518 if (c1 == EOF) {
519 if (c2 == EOF)
520 return(1); /* success! */
521 if (report != REP_QUIET) {
522 fputs(f1name, stdout);
523 fputs(": unexpected end-of-file\n", stdout);
524 }
525 return(0);
526 }
527 if (c2 == EOF) {
528 if (report != REP_QUIET) {
529 fputs(f2name, stdout);
530 fputs(": unexpected end-of-file\n", stdout);
531 }
532 return(0);
533 }
534 if (c1 != c2)
535 break; /* quit and report difference */
536 }
537 if (report == REP_QUIET)
538 return(0);
539 printf("%s: binary files '%s' and '%s' differ at byte offset %ld|%ld\n",
540 progname, f1name, f2name, ftell(f1in), ftell(f2in));
541 if (report >= REP_VERBOSE)
542 printf("%s: byte in '%s' is 0x%X, byte in '%s' is 0x%X\n",
543 progname, f1name, c1, f2name, c2);
544 return(0);
545 }
546
547 /* Compare two inputs as generic text files */
548 static int
549 compare_text()
550 {
551 LINEBUF l1buf, l2buf;
552
553 if (report >= REP_VERBOSE) {
554 fputs(progname, stdout);
555 fputs(": comparing inputs as ASCII text\n", stdout);
556 }
557 init_line(&l1buf); init_line(&l2buf); /* compare a line at a time */
558 while (read_line(&l1buf, f1in)) {
559 lin1cnt++;
560 if (!*sskip2(l1buf.str,0))
561 continue; /* ignore empty lines */
562
563 while (read_line(&l2buf, f2in)) {
564 lin2cnt++;
565 if (*sskip2(l2buf.str,0))
566 break; /* found other non-empty line */
567 }
568 if (!l2buf.len) { /* input 2 EOF? */
569 if (report != REP_QUIET) {
570 fputs(f2name, stdout);
571 fputs(": unexpected end-of-file\n", stdout);
572 }
573 free_line(&l1buf); free_line(&l2buf);
574 return(0);
575 }
576 /* compare non-empty lines */
577 if (!equiv_string(l1buf.str, l2buf.str)) {
578 if (report != REP_QUIET) {
579 printf("%s: inputs '%s' and '%s' differ at line %d|%d\n",
580 progname, f1name, f2name,
581 lin1cnt, lin2cnt);
582 if ( report >= REP_VERBOSE &&
583 (l1buf.len < 256) &
584 (l2buf.len < 256) ) {
585 fputs("------------- Mismatch -------------\n", stdout);
586 printf("%s@%d:\t%s", f1name,
587 lin1cnt, l1buf.str);
588 printf("%s@%d:\t%s", f2name,
589 lin2cnt, l2buf.str);
590 }
591 }
592 free_line(&l1buf); free_line(&l2buf);
593 return(0);
594 }
595 }
596 free_line(&l1buf); /* check for EOF on input 2 */
597 while (read_line(&l2buf, f2in)) {
598 if (!*sskip2(l2buf.str,0))
599 continue;
600 if (report != REP_QUIET) {
601 fputs(f1name, stdout);
602 fputs(": unexpected end-of-file\n", stdout);
603 }
604 free_line(&l2buf);
605 return(0);
606 }
607 free_line(&l2buf);
608 return(good_RMS()); /* final check for reals */
609 }
610
611 /* Check image/map resolutions */
612 static int
613 check_resolu(const char *class, RESOLU *r1p, RESOLU *r2p)
614 {
615 if (r1p->rt != r2p->rt) {
616 if (report != REP_QUIET)
617 printf(
618 "%s: %ss '%s' and '%s' have different pixel ordering\n",
619 progname, class, f1name, f2name);
620 return(0);
621 }
622 if ((r1p->xr != r2p->xr) | (r1p->yr != r2p->yr)) {
623 if (report != REP_QUIET)
624 printf(
625 "%s: %ss '%s' and '%s' are different sizes\n",
626 progname, class, f1name, f2name);
627 return(0);
628 }
629 return(1);
630 }
631
632 /* Compare two inputs that are known to be RGBE or XYZE images */
633 static int
634 compare_hdr()
635 {
636 RESOLU rs1, rs2;
637 COLOR *scan1, *scan2;
638 int x, y;
639
640 if (report >= REP_VERBOSE) {
641 fputs(progname, stdout);
642 fputs(": comparing inputs as HDR images\n", stdout);
643 }
644 fgetsresolu(&rs1, f1in);
645 fgetsresolu(&rs2, f2in);
646 if (!check_resolu("HDR image", &rs1, &rs2))
647 return(0);
648 scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR));
649 scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR));
650 if (!scan1 | !scan2) {
651 fprintf(stderr, "%s: out of memory in compare_hdr()\n", progname);
652 exit(2);
653 }
654 for (y = 0; y < numscans(&rs1); y++) {
655 if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) |
656 (freadscan(scan2, scanlen(&rs2), f2in) < 0)) {
657 if (report != REP_QUIET)
658 printf("%s: unexpected end-of-file\n",
659 progname);
660 free(scan1);
661 free(scan2);
662 return(0);
663 }
664 for (x = 0; x < scanlen(&rs1); x++) {
665 if (color_check(scan1[x], scan2[x]))
666 continue;
667 if (report != REP_QUIET) {
668 printf(
669 "%s: pixels at scanline %d offset %d differ\n",
670 progname, y, x);
671 if (report >= REP_VERBOSE) {
672 printf("%s: (R,G,B)=(%g,%g,%g)\n",
673 f1name, colval(scan1[x],RED),
674 colval(scan1[x],GRN),
675 colval(scan1[x],BLU));
676 printf("%s: (R,G,B)=(%g,%g,%g)\n",
677 f2name, colval(scan2[x],RED),
678 colval(scan2[x],GRN),
679 colval(scan2[x],BLU));
680 }
681 }
682 free(scan1);
683 free(scan2);
684 return(0);
685 }
686 }
687 free(scan1);
688 free(scan2);
689 return(good_RMS()); /* final check of RMS */
690 }
691
692 /* Set reference depth based on header variable */
693 static int
694 set_refdepth(DEPTHCODEC *dcp, LUTAB *htp)
695 {
696 static char depthvar[] = DEPTHSTR;
697 const char *drval;
698
699 depthvar[LDEPTHSTR] = '\0';
700 drval = (const char *)lu_find(htp, depthvar)->data;
701 if (!drval)
702 return(0);
703 dcp->refdepth = atof(drval);
704 if (dcp->refdepth <= 0) {
705 if (report != REP_QUIET) {
706 fputs(dcp->inpname, stderr);
707 fputs(": bad reference depth '", stderr);
708 fputs(drval, stderr);
709 fputs("'\n", stderr);
710 }
711 return(-1);
712 }
713 return(1);
714 }
715
716 /* Compare two encoded depth maps */
717 static int
718 compare_depth()
719 {
720 long nread = 0;
721 DEPTHCODEC dc1, dc2;
722
723 if (report >= REP_VERBOSE) {
724 fputs(progname, stdout);
725 fputs(": comparing inputs as depth maps\n", stdout);
726 }
727 set_dc_defaults(&dc1);
728 dc1.hdrflags = HF_RESIN;
729 dc1.finp = f1in;
730 dc1.inpname = f1name;
731 set_dc_defaults(&dc2);
732 dc2.hdrflags = HF_RESIN;
733 dc2.finp = f2in;
734 dc2.inpname = f2name;
735 if (report != REP_QUIET) {
736 dc1.hdrflags |= HF_STDERR;
737 dc2.hdrflags |= HF_STDERR;
738 }
739 if (!process_dc_header(&dc1, 0, NULL))
740 return(0);
741 if (!process_dc_header(&dc2, 0, NULL))
742 return(0);
743 if (!check_resolu("Depth map", &dc1.res, &dc2.res))
744 return(0);
745 if (set_refdepth(&dc1, &hdr1) < 0)
746 return(0);
747 if (set_refdepth(&dc2, &hdr2) < 0)
748 return(0);
749 while (nread < dc1.res.xr*dc1.res.yr) {
750 double d1 = decode_depth_next(&dc1);
751 double d2 = decode_depth_next(&dc2);
752 if ((d1 < 0) | (d2 < 0)) {
753 if (report != REP_QUIET)
754 printf("%s: unexpected end-of-file\n",
755 progname);
756 return(0);
757 }
758 ++nread;
759 if (real_check(d1, d2))
760 continue;
761 if (report != REP_QUIET)
762 printf("%s: %ld%s depth values differ\n",
763 progname, nread, num_sfx(nread));
764 return(0);
765 }
766 return(good_RMS()); /* final check of RMS */
767 }
768
769 /* Compare two encoded normal maps */
770 static int
771 compare_norm()
772 {
773 long nread = 0;
774 NORMCODEC nc1, nc2;
775
776 if (report >= REP_VERBOSE) {
777 fputs(progname, stdout);
778 fputs(": comparing inputs as normal maps\n", stdout);
779 }
780 set_nc_defaults(&nc1);
781 nc1.hdrflags = HF_RESIN;
782 nc1.finp = f1in;
783 nc1.inpname = f1name;
784 set_nc_defaults(&nc2);
785 nc2.hdrflags = HF_RESIN;
786 nc2.finp = f2in;
787 nc2.inpname = f2name;
788 if (report != REP_QUIET) {
789 nc1.hdrflags |= HF_STDERR;
790 nc2.hdrflags |= HF_STDERR;
791 }
792 if (!process_nc_header(&nc1, 0, NULL))
793 return(0);
794 if (!process_nc_header(&nc2, 0, NULL))
795 return(0);
796 if (!check_resolu("Normal map", &nc1.res, &nc2.res))
797 return(0);
798 while (nread < nc1.res.xr*nc1.res.yr) {
799 FVECT nv1, nv2;
800 int rv1 = decode_normal_next(nv1, &nc1);
801 int rv2 = decode_normal_next(nv2, &nc2);
802 if ((rv1 < 0) | (rv2 < 0)) {
803 if (report != REP_QUIET)
804 printf("%s: unexpected end-of-file\n",
805 progname);
806 return(0);
807 }
808 ++nread;
809 if (rv1 == rv2 && (!rv1 || norm_check(nv1, nv2)))
810 continue;
811 if (report != REP_QUIET)
812 printf("%s: %ld%s normal vectors differ\n",
813 progname, nread, num_sfx(nread));
814 return(0);
815 }
816 return(good_RMS()); /* final check of RMS */
817 }
818
819 /* Compare two inputs that are known to be 32-bit floating-point data */
820 static int
821 compare_float()
822 {
823 long nread = 0;
824 float f1, f2;
825
826 if (report >= REP_VERBOSE) {
827 fputs(progname, stdout);
828 fputs(": comparing inputs as 32-bit IEEE floats\n", stdout);
829 }
830 while (getbinary(&f1, sizeof(f1), 1, f1in)) {
831 if (!getbinary(&f2, sizeof(f2), 1, f2in))
832 goto badeof;
833 ++nread;
834 if (f1swap) swap32((char *)&f1, 1);
835 if (f2swap) swap32((char *)&f2, 1);
836 if (real_check(f1, f2))
837 continue;
838 if (report != REP_QUIET)
839 printf("%s: %ld%s float values differ\n",
840 progname, nread, num_sfx(nread));
841 return(0);
842 }
843 if (!getbinary(&f2, sizeof(f2), 1, f2in))
844 return(good_RMS()); /* final check of RMS */
845 badeof:
846 if (report != REP_QUIET)
847 printf("%s: unexpected end-of-file\n", progname);
848 return(0);
849 }
850
851 /* Compare two inputs that are known to be 64-bit floating-point data */
852 static int
853 compare_double()
854 {
855 long nread = 0;
856 double f1, f2;
857
858 if (report >= REP_VERBOSE) {
859 fputs(progname, stdout);
860 fputs(": comparing inputs as 64-bit IEEE doubles\n", stdout);
861 }
862 while (getbinary(&f1, sizeof(f1), 1, f1in)) {
863 if (!getbinary(&f2, sizeof(f2), 1, f2in))
864 goto badeof;
865 ++nread;
866 if (f1swap) swap64((char *)&f1, 1);
867 if (f2swap) swap64((char *)&f2, 1);
868 if (real_check(f1, f2))
869 continue;
870 if (report != REP_QUIET)
871 printf("%s: %ld%s double values differ\n",
872 progname, nread, num_sfx(nread));
873 return(0);
874 }
875 if (!getbinary(&f2, sizeof(f2), 1, f2in))
876 return(good_RMS()); /* final check of RMS */
877 badeof:
878 if (report != REP_QUIET)
879 printf("%s: unexpected end-of-file\n", progname);
880 return(0);
881 }
882
883 /* Compare two Radiance files for equivalence */
884 int
885 main(int argc, char *argv[])
886 {
887 int typ1, typ2;
888 int a;
889
890 progname = argv[0];
891 for (a = 1; a < argc && argv[a][0] == '-'; a++) {
892 switch (argv[a][1]) {
893 case 'h': /* ignore header info. */
894 ign_header = !ign_header;
895 continue;
896 case 's': /* silent operation */
897 report = REP_QUIET;
898 continue;
899 case 'w': /* turn off warnings */
900 if (report > REP_ERROR)
901 report = REP_ERROR;
902 continue;
903 case 'v': /* turn on verbose mode */
904 report = REP_VERBOSE;
905 continue;
906 case 'm': /* maximum epsilon */
907 max_lim = atof(argv[++a]);
908 continue;
909 case 'r':
910 if (argv[a][2] == 'e') /* relative difference */
911 rel_min = atof(argv[++a]);
912 else if (argv[a][2] == 'm') /* RMS limit */
913 rms_lim = atof(argv[++a]);
914 else
915 usage();
916 continue;
917 case '\0': /* first file from stdin */
918 f1in = stdin;
919 f1name = stdin_name;
920 break;
921 default:
922 usage();
923 }
924 break;
925 }
926 if (a != argc-2) /* make sure of two inputs */
927 usage();
928 if (!strcmp(argv[a], argv[a+1])) { /* inputs are same? */
929 if (report >= REP_WARN)
930 printf("%s: warning - identical inputs given\n",
931 progname);
932 return(0);
933 }
934 if (!f1name) f1name = argv[a];
935 if (!f2name) f2name = argv[a+1];
936 /* open inputs */
937 SET_FILE_BINARY(stdin); /* in case we're using it */
938 if (!f1in && !(f1in = fopen(f1name, "rb"))) {
939 fprintf(stderr, "%s: cannot open for reading\n", f1name);
940 return(2);
941 }
942 if (!strcmp(f2name, "-")) {
943 f2in = stdin;
944 f2name = stdin_name;
945 } else if (!(f2in = fopen(f2name, "rb"))) {
946 fprintf(stderr, "%s: cannot open for reading\n", f2name);
947 return(2);
948 }
949 /* load headers */
950 if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0)
951 return(2);
952 if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0)
953 return(2);
954 if (typ1 != typ2) {
955 if (report != REP_QUIET)
956 printf("%s: '%s' is %s and '%s' is %s\n",
957 progname, f1name, file_type[typ1],
958 f2name, file_type[typ2]);
959 return(1);
960 }
961 ign_header |= !has_header(typ1); /* check headers if indicated */
962 if (!ign_header && !headers_match())
963 return(1);
964 if (!ign_header & (report >= REP_WARN)) {
965 if (lin1cnt != lin2cnt)
966 printf("%s: warning - headers are different lengths\n",
967 progname);
968 if (typ1 == TYP_UNKNOWN)
969 printf("%s: warning - unrecognized format\n",
970 progname);
971 }
972 if (report >= REP_VERBOSE)
973 printf("%s: input file type is %s\n",
974 progname, file_type[typ1]);
975
976 switch (typ1) { /* compare based on type */
977 case TYP_BINARY:
978 case TYP_TMESH:
979 case TYP_OCTREE:
980 case TYP_RBFMESH:
981 case TYP_ID8:
982 case TYP_ID16:
983 case TYP_ID24:
984 case TYP_UNKNOWN:
985 return( !compare_binary() );
986 case TYP_TEXT:
987 case TYP_ASCII:
988 return( !compare_text() );
989 case TYP_RGBE:
990 case TYP_XYZE:
991 return( !compare_hdr() );
992 case TYP_DEPTH:
993 return( !compare_depth() );
994 case TYP_NORM:
995 return( !compare_norm() );
996 case TYP_FLOAT:
997 return( !compare_float() );
998 case TYP_DOUBLE:
999 return( !compare_double() );
1000 }
1001 return(1);
1002 }