ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/radcompare.c
Revision: 2.34
Committed: Tue Nov 21 20:01:39 2023 UTC (5 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.33: +22 -1 lines
Log Message:
refactor(radcompare): introduced alternate spectral comparison (commented out)

File Contents

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