ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/radcompare.c
Revision: 2.30
Committed: Tue May 24 22:34:18 2022 UTC (22 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.29: +2 -1 lines
Log Message:
perf(radcompare): Added "FORMAT" to ignored header variables, since it's dealt with separately

File Contents

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