ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/radcompare.c
Revision: 2.19
Committed: Wed Aug 14 04:18:12 2019 UTC (4 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.18: +194 -18 lines
Log Message:
Added depth, normal, and ID file types to radcompare

File Contents

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