ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/radcompare.c
Revision: 2.35
Committed: Mon Dec 9 00:29:30 2024 UTC (2 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.34: +2 -1 lines
Log Message:
fix(radcompare): Added "TILED" to the list of ignored header variables

File Contents

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