ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/radcompare.c
Revision: 2.32
Committed: Wed Mar 8 19:59:48 2023 UTC (13 months, 2 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4
Changes since 2.31: +7 -4 lines
Log Message:
feat: made it so line number in ASCII file is not repeated if the same

File Contents

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