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

File Contents

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