ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/radcompare.c
Revision: 2.6
Committed: Mon Oct 15 22:38:31 2018 UTC (5 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +35 -22 lines
Log Message:
Additional reporting tweaks

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.6 static const char RCSid[] = "$Id: radcompare.c,v 2.5 2018/10/15 22:21:45 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 <math.h>
12     #include <ctype.h>
13     #include "platform.h"
14     #include "rtio.h"
15     #include "resolu.h"
16     #include "color.h"
17     #include "lookup.h"
18     /* Reporting levels */
19     #define REP_QUIET 0 /* no reporting */
20     #define REP_ERROR 1 /* report errors only */
21     #define REP_WARN 2 /* report warnings as well */
22     #define REP_VERBOSE 3 /* verbose reporting */
23    
24     int report = REP_WARN; /* reporting level */
25    
26     int ign_header = 0; /* ignore header differences? */
27    
28     double rel_min = 1e-5; /* positive for relative comparisons */
29    
30     double rms_lim = 0.01; /* RMS difference limit */
31    
32     double max_lim = 0.25; /* difference limit if non-negative */
33    
34     int lin1cnt=0, lin2cnt=0; /* file line position */
35    
36 greg 2.6 const char nsuffix[10][3] = { /* 1st, 2nd, 3rd, etc. */
37     "th","st","nd","rd","th","th","th","th","th","th"
38     };
39     #define num_sfx(n) nsuffix[(n)%10]
40    
41 greg 2.1 /* file types */
42     const char *file_type[] = {
43     "Unrecognized",
44     "TEXT_generic",
45     "ascii",
46     COLRFMT,
47     CIEFMT,
48 greg 2.4 "float",
49     "double",
50 greg 2.1 "BSDF_RBFmesh",
51     "Radiance_octree",
52     "Radiance_tmesh",
53     "BINARY_unknown",
54     NULL /* terminator */
55     };
56 greg 2.6 /* keep consistent with above */
57 greg 2.1 enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, TYP_FLOAT,
58     TYP_DOUBLE, TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, TYP_BINARY};
59    
60     #define has_header(t) (!( 1L<<(t) & (1L<<TYP_TEXT | 1L<<TYP_BINARY) ))
61    
62     /* header variables to always ignore */
63     const char *hdr_ignkey[] = {
64     "SOFTWARE",
65     "CAPDATE",
66     "GMT",
67     NULL /* terminator */
68     };
69     /* header variable settings */
70     LUTAB hdr1 = LU_SINIT(free,free);
71     LUTAB hdr2 = LU_SINIT(free,free);
72    
73     /* advance appropriate file line count */
74     #define adv_linecnt(htp) (lin1cnt += (htp == &hdr1), \
75     lin2cnt += (htp == &hdr2))
76    
77     /* input files */
78     char *progname = NULL;
79     const char stdin_name[] = "<stdin>";
80     const char *f1name=NULL, *f2name=NULL;
81     FILE *f1in=NULL, *f2in=NULL;
82 greg 2.3
83 greg 2.1 /* running real differences */
84     double diff2sum = 0;
85     int nsum = 0;
86    
87     /* Report usage and exit */
88     static void
89     usage()
90     {
91     fputs("Usage: ", stderr);
92     fputs(progname, stderr);
93 greg 2.4 fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n",
94 greg 2.1 stderr);
95     exit(1);
96     }
97    
98     /* Get type ID from name (or 0 if not found) */
99     static int
100     xlate_type(const char *nm)
101     {
102     int i;
103    
104     if (!nm || !*nm)
105     return(TYP_UNKNOWN);
106     for (i = 1; file_type[i]; i++)
107     if (!strcmp(nm, file_type[i]))
108     return(i);
109     return(TYP_UNKNOWN);
110     }
111    
112     /* Compare real values and keep track of differences */
113     static int
114     real_check(double r1, double r2)
115     {
116     double diff2 = (r1 - r2)*(r1 - r2);
117    
118     if (rel_min > 0) { /* doing relative differences? */
119     double av2 = .25*(r1*r1 + 2.*fabs(r1*r2) + r2*r2);
120     if (av2 > rel_min*rel_min)
121     diff2 /= av2;
122     }
123     if (max_lim >= 0 && diff2 > max_lim*max_lim) {
124     if (report != REP_QUIET)
125 greg 2.4 printf(
126 greg 2.5 "%s: %sdifference between %.8g and %.8g exceeds epsilon of %.8g\n",
127 greg 2.1 progname,
128     (rel_min > 0) ? "relative " : "",
129 greg 2.5 r1, r2, max_lim);
130 greg 2.1 return(0);
131     }
132     diff2sum += diff2;
133     nsum++;
134     return(1);
135     }
136    
137     /* Compare two strings for equivalence */
138     static int
139     equiv_string(char *s1, char *s2)
140     {
141     #define CLS_STR 0
142     #define CLS_INT 1
143     #define CLS_FLT 2
144     /* skip whitespace at beginning */
145     while (isspace(*s1)) s1++;
146     while (isspace(*s2)) s2++;
147     while (*s1) { /* check each word */
148 greg 2.2 int inquote;
149     if (!*s2) /* unexpected EOL in s2? */
150     return(0);
151     inquote = *s1;
152 greg 2.1 if ((inquote != '\'') & (inquote != '"'))
153     inquote = 0;
154     if (inquote) { /* quoted text must match exactly */
155     if (*s1++ != *s2++)
156     return(0);
157     while (*s1 != inquote) {
158     if (!*s1)
159     return(0);
160     if (*s1++ != *s2++)
161     return(0);
162     }
163     s1++;
164     if (*s2++ != inquote)
165     return(0);
166     } else { /* else classify word type */
167     char *s1s = s1;
168     char *s2s = s2;
169     int cls = CLS_STR;
170     s1 = sskip(s1);
171     s2 = sskip(s2);
172     if (iskip(s1s) == s1) {
173     if (iskip(s2s) == s2)
174     cls = CLS_INT;
175     else if (fskip(s2s) == s2)
176     cls = CLS_FLT;
177     } else if (fskip(s1s) == s1) {
178     if (fskip(s2s) != s2)
179     return(0);
180     cls = CLS_FLT;
181     }
182     switch (cls) {
183     case CLS_INT: /* strncmp() faster */
184     case CLS_STR:
185     if (s1 - s1s != s2 - s2s)
186     return(0);
187     if (strncmp(s1s, s2s, s1 - s1s))
188     return(0);
189     break;
190     case CLS_FLT:
191     if (!real_check(atof(s1s), atof(s2s)))
192     return(0);
193     break;
194     }
195     }
196     while (isspace(*s1)) s1++;
197     while (isspace(*s2)) s2++;
198     }
199     return(!*s2); /* match if we reached the end of s2, too */
200     #undef CLS_STR
201     #undef CLS_INT
202     #undef CLS_FLT
203     }
204    
205     /* Check if string is var=value pair and set if not in ignore list */
206     static int
207     setheadvar(char *val, void *p)
208     {
209     LUTAB *htp = (LUTAB *)p;
210     LUENT *tep;
211     char *key;
212     int kln, vln;
213     int n;
214    
215 greg 2.3 adv_linecnt(htp); /* side-effect is to count lines */
216 greg 2.1 if (!isalpha(*val)) /* key must start line */
217     return(0);
218     key = val++;
219     while (*val && !isspace(*val) & (*val != '='))
220     val++;
221     kln = val - key;
222     while (isspace(*val)) /* check for value */
223     *val++ = '\0';
224     if (*val != '=')
225     return(0);
226     *val++ = '\0';
227     while (isspace(*val))
228     val++;
229     if (!*val) /* nothing set? */
230     return(0);
231     /* check if key to ignore */
232     for (n = 0; hdr_ignkey[n]; n++)
233     if (!strcmp(key, hdr_ignkey[n]))
234     return(0);
235 greg 2.6 vln = strlen(val); /* eliminate space and newline at end */
236     while (isspace(val[--vln]))
237     ;
238     val[++vln] = '\0';
239 greg 2.1 if (!(tep = lu_find(htp, key)))
240 greg 2.4 return(-1); /* memory allocation error */
241 greg 2.1 if (!tep->key)
242     tep->key = strcpy(malloc(kln+1), key);
243     if (tep->data)
244     free(tep->data);
245     tep->data = strcpy(malloc(vln+1), val);
246     return(1);
247     }
248    
249     /* Lookup correspondent in other header */
250     static int
251     match_val(const LUENT *ep1, void *p2)
252     {
253     const LUENT *ep2 = lu_find((LUTAB *)p2, ep1->key);
254     if (!ep2 || !ep2->data) {
255     if (report != REP_QUIET)
256 greg 2.5 printf("%s: variable '%s' missing in '%s'\n",
257 greg 2.1 progname, ep1->key, f2name);
258     return(-1);
259     }
260     if (!equiv_string((char *)ep1->data, (char *)ep2->data)) {
261 greg 2.4 if (report != REP_QUIET) {
262 greg 2.5 printf("%s: header variable '%s' has different values\n",
263 greg 2.1 progname, ep1->key);
264 greg 2.4 if (report >= REP_VERBOSE) {
265     printf("%s: %s=%s\n", f1name,
266     ep1->key, (char *)ep1->data);
267     printf("%s: %s=%s\n", f2name,
268     ep2->key, (char *)ep2->data);
269     }
270     }
271 greg 2.1 return(-1);
272     }
273     return(1); /* good match */
274     }
275    
276     /* Compare two sets of header variables */
277     static int
278     headers_match(LUTAB *hp1, LUTAB *hp2)
279     {
280     int ne = lu_doall(hp1, match_val, hp2);
281     if (ne < 0)
282     return(0); /* something didn't match! */
283 greg 2.4 /* non-fatal if second header has extra */
284     if (report >= REP_WARN && (ne = lu_doall(hp2, NULL, NULL) - ne))
285 greg 2.5 printf("%s: warning - '%s' has %d extra header setting(s)\n",
286 greg 2.1 progname, f2name, ne);
287     return(1); /* good match */
288     }
289    
290     /* Check generic input to determine if it is binary, -1 on error */
291     static int
292     input_is_binary(FILE *fin)
293     {
294     int n = 0;
295     int c = 0;
296    
297     while ((c = getc(fin)) != EOF) {
298 greg 2.5 ++n;
299 greg 2.1 if (!c | (c > 127))
300     break; /* non-ascii character */
301 greg 2.5 if (n >= 10240)
302 greg 2.1 break; /* enough to be confident */
303     }
304     if (!n)
305     return(-1); /* first read failed */
306     if (fseek(fin, 0L, 0) < 0)
307     return(-1); /* rewind failed */
308     return(!c | (c > 127));
309     }
310    
311     /* Identify and return data type from header (if one) */
312     static int
313     identify_type(const char *name, FILE *fin, LUTAB *htp)
314     {
315     extern const char HDRSTR[];
316     int c;
317     /* check magic header start */
318     if ((c = getc(fin)) != HDRSTR[0]) {
319     if (c == EOF) goto badeof;
320     ungetc(c, fin);
321     c = 0;
322     } else if ((c = getc(fin)) != HDRSTR[1]) {
323     if (c == EOF) goto badeof;
324     ungetc(c, fin); ungetc(HDRSTR[0], fin);
325     c = 0;
326     }
327     if (c) { /* appears to have a header */
328     char sbuf[32];
329     if (!fgets(sbuf, sizeof(sbuf), fin))
330     goto badeof;
331 greg 2.3 adv_linecnt(htp); /* for #?ID string */
332 greg 2.1 if (report >= REP_WARN && strncmp(sbuf, "RADIANCE", 8)) {
333 greg 2.4 fputs(name, stdout);
334     fputs(": warning - unexpected header ID: ", stdout);
335     fputs(sbuf, stdout);
336 greg 2.1 }
337     if (getheader(fin, setheadvar, htp) < 0) {
338     fputs(name, stderr);
339 greg 2.6 fputs(": unknown error reading header\n", stderr);
340 greg 2.1 return(-1);
341     }
342     adv_linecnt(htp); /* for trailing emtpy line */
343     return(xlate_type((const char *)lu_find(htp,"FORMAT")->data));
344     }
345     c = input_is_binary(fin); /* else peek to see if binary */
346     if (c < 0) {
347     fputs(name, stderr);
348     fputs(": read/seek error\n", stderr);
349     return(-1);
350     }
351     if (c)
352     return(TYP_BINARY);
353     SET_FILE_TEXT(fin); /* originally set to binary */
354     return(TYP_TEXT);
355     badeof:
356     if (report != REP_QUIET) {
357 greg 2.4 fputs(name, stdout);
358 greg 2.6 fputs(": unexpected end-of-file\n", stdout);
359 greg 2.1 }
360     return(-1);
361     }
362    
363     /* Check that overall RMS error is below threshold */
364     static int
365     good_RMS()
366     {
367     if (!nsum)
368     return(1);
369     if (diff2sum/(double)nsum > rms_lim*rms_lim) {
370     if (report != REP_QUIET)
371 greg 2.4 printf(
372 greg 2.5 "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit of %.5g\n",
373 greg 2.1 progname,
374     (rel_min > 0) ? "relative " : "",
375     f1name, f2name,
376 greg 2.5 sqrt(diff2sum/(double)nsum), rms_lim);
377 greg 2.1 return(0);
378     }
379     if (report >= REP_VERBOSE)
380 greg 2.4 printf("%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n",
381 greg 2.1 progname, (rel_min > 0) ? "relative " : "",
382     f1name, f2name, sqrt(diff2sum/(double)nsum));
383     return(1);
384     }
385    
386     /* Compare two inputs as generic binary files */
387     static int
388     compare_binary()
389     {
390 greg 2.5 int c1=0, c2=0;
391    
392 greg 2.1 if (report >= REP_VERBOSE) {
393 greg 2.4 fputs(progname, stdout);
394     fputs(": comparing inputs as binary\n", stdout);
395 greg 2.1 }
396     for ( ; ; ) { /* exact byte matching */
397 greg 2.5 c1 = getc(f1in);
398     c2 = getc(f2in);
399 greg 2.1 if (c1 == EOF) {
400     if (c2 == EOF)
401     return(1); /* success! */
402     if (report != REP_QUIET) {
403 greg 2.4 fputs(f1name, stdout);
404 greg 2.6 fputs(": unexpected end-of-file\n", stdout);
405 greg 2.1 }
406     return(0);
407     }
408     if (c2 == EOF) {
409     if (report != REP_QUIET) {
410 greg 2.4 fputs(f2name, stdout);
411 greg 2.6 fputs(": unexpected end-of-file\n", stdout);
412 greg 2.1 }
413     return(0);
414     }
415     if (c1 != c2)
416     break; /* quit and report difference */
417     }
418     if (report == REP_QUIET)
419     return(0);
420 greg 2.5 printf("%s: binary files '%s' and '%s' differ at byte offset %ld|%ld\n",
421 greg 2.1 progname, f1name, f2name, ftell(f1in), ftell(f2in));
422 greg 2.5 if (report >= REP_VERBOSE)
423     printf("%s: byte in '%s' is 0x%X, byte in '%s' is 0x%X\n",
424     progname, f1name, c1, f2name, c2);
425 greg 2.1 return(0);
426     }
427    
428     /* Compare two inputs as generic text files */
429     static int
430     compare_text()
431     {
432     char l1buf[4096], l2buf[4096];
433    
434     if (report >= REP_VERBOSE) {
435 greg 2.4 fputs(progname, stdout);
436     fputs(": comparing inputs as ASCII text\n", stdout);
437 greg 2.1 }
438     /* compare a line at a time */
439     while (fgets(l1buf, sizeof(l1buf), f1in)) {
440     lin1cnt++;
441     if (!*sskip2(l1buf,0))
442     continue; /* ignore empty lines */
443     while (fgets(l2buf, sizeof(l2buf), f2in)) {
444     lin2cnt++;
445     if (*sskip2(l2buf,0))
446     break; /* found other non-empty line */
447     }
448     if (feof(f2in)) {
449     if (report != REP_QUIET) {
450 greg 2.4 fputs(f2name, stdout);
451 greg 2.6 fputs(": unexpected end-of-file\n", stdout);
452 greg 2.1 }
453     return(0);
454     }
455     /* compare non-empty lines */
456     if (!equiv_string(l1buf, l2buf)) {
457     if (report != REP_QUIET) {
458 greg 2.4 printf("%s: inputs '%s' and '%s' differ at line %d|%d\n",
459 greg 2.1 progname, f1name, f2name,
460     lin1cnt, lin2cnt);
461     if (report >= REP_VERBOSE) {
462 greg 2.4 fputs("------------- Mismatch -------------\n", stdout);
463 greg 2.5 printf("%s@%d:\t%s", f1name,
464 greg 2.1 lin1cnt, l1buf);
465 greg 2.5 printf("%s@%d:\t%s", f2name,
466 greg 2.1 lin2cnt, l2buf);
467     }
468     }
469     return(0);
470     }
471     }
472     /* check for EOF on input 2 */
473     while (fgets(l2buf, sizeof(l2buf), f2in)) {
474     if (!*sskip2(l2buf,0))
475     continue;
476     if (report != REP_QUIET) {
477 greg 2.4 fputs(f1name, stdout);
478 greg 2.6 fputs(": unexpected end-of-file\n", stdout);
479 greg 2.1 }
480     return(0);
481     }
482     return(good_RMS()); /* final check for reals */
483     }
484    
485     /* Compare two inputs that are known to be RGBE or XYZE images */
486     static int
487     compare_hdr()
488     {
489     RESOLU rs1, rs2;
490     COLOR *scan1, *scan2;
491     int x, y;
492    
493 greg 2.6 if (report >= REP_VERBOSE) {
494     fputs(progname, stdout);
495     fputs(": comparing inputs as HDR images\n", stdout);
496     }
497 greg 2.1 fgetsresolu(&rs1, f1in);
498     fgetsresolu(&rs2, f2in);
499     if (rs1.rt != rs2.rt) {
500     if (report != REP_QUIET)
501 greg 2.4 printf(
502 greg 2.1 "%s: Images '%s' and '%s' have different pixel ordering\n",
503     progname, f1name, f2name);
504     return(0);
505     }
506     if ((rs1.xr != rs2.xr) | (rs1.yr != rs2.yr)) {
507     if (report != REP_QUIET)
508 greg 2.4 printf(
509 greg 2.1 "%s: Images '%s' and '%s' are different sizes\n",
510     progname, f1name, f2name);
511     return(0);
512     }
513     scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR));
514     scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR));
515     if (!scan1 | !scan2) {
516     fprintf(stderr, "%s: out of memory in compare_hdr()\n", progname);
517     exit(2);
518     }
519     for (y = 0; y < numscans(&rs1); y++) {
520     if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) |
521     (freadscan(scan2, scanlen(&rs2), f2in) < 0)) {
522     if (report != REP_QUIET)
523 greg 2.6 printf("%s: unexpected end-of-file\n",
524 greg 2.1 progname);
525     free(scan1);
526     free(scan2);
527     return(0);
528     }
529 greg 2.5 for (x = 0; x < scanlen(&rs1); x++) {
530 greg 2.1 if (real_check(colval(scan1[x],RED),
531     colval(scan2[x],RED)) &
532     real_check(colval(scan1[x],GRN),
533     colval(scan2[x],GRN)) &
534     real_check(colval(scan1[x],BLU),
535     colval(scan2[x],BLU)))
536     continue;
537     if (report != REP_QUIET) {
538 greg 2.4 printf(
539 greg 2.1 "%s: pixels at scanline %d offset %d differ\n",
540     progname, y, x);
541     if (report >= REP_VERBOSE) {
542 greg 2.4 printf("%s: (R,G,B)=(%g,%g,%g)\n",
543 greg 2.1 f1name, colval(scan1[x],RED),
544     colval(scan1[x],GRN),
545     colval(scan1[x],BLU));
546 greg 2.4 printf("%s: (R,G,B)=(%g,%g,%g)\n",
547 greg 2.1 f2name, colval(scan2[x],RED),
548     colval(scan2[x],GRN),
549     colval(scan2[x],BLU));
550     }
551     }
552     free(scan1);
553     free(scan2);
554     return(0);
555     }
556     }
557     free(scan1);
558     free(scan2);
559     return(good_RMS()); /* final check of RMS */
560     }
561    
562     /* Compare two inputs that are known to be 32-bit floating-point data */
563     static int
564     compare_float()
565     {
566     long nread = 0;
567     float f1, f2;
568    
569 greg 2.6 if (report >= REP_VERBOSE) {
570     fputs(progname, stdout);
571     fputs(": comparing inputs as 32-bit IEEE float\n", stdout);
572     }
573 greg 2.1 while (getbinary(&f1, sizeof(f1), 1, f1in)) {
574     if (!getbinary(&f2, sizeof(f2), 1, f2in))
575     goto badeof;
576     ++nread;
577     if (real_check(f1, f2))
578     continue;
579     if (report != REP_QUIET)
580 greg 2.4 printf("%s: %ld%s float values differ\n",
581 greg 2.6 progname, nread, num_sfx(nread));
582 greg 2.1 return(0);
583     }
584     if (!getbinary(&f2, sizeof(f2), 1, f2in))
585     return(good_RMS()); /* final check of RMS */
586     badeof:
587     if (report != REP_QUIET)
588 greg 2.6 printf("%s: unexpected end-of-file\n", progname);
589 greg 2.1 return(0);
590     }
591    
592     /* Compare two inputs that are known to be 64-bit floating-point data */
593     static int
594     compare_double()
595     {
596     long nread = 0;
597     double f1, f2;
598    
599 greg 2.6 if (report >= REP_VERBOSE) {
600     fputs(progname, stdout);
601     fputs(": comparing inputs as 64-bit IEEE double\n", stdout);
602     }
603 greg 2.1 while (getbinary(&f1, sizeof(f1), 1, f1in)) {
604     if (!getbinary(&f2, sizeof(f2), 1, f2in))
605     goto badeof;
606     ++nread;
607     if (real_check(f1, f2))
608     continue;
609     if (report != REP_QUIET)
610 greg 2.6 printf("%s: %ld%s double values differ\n",
611     progname, nread, num_sfx(nread));
612 greg 2.1 return(0);
613     }
614     if (!getbinary(&f2, sizeof(f2), 1, f2in))
615     return(good_RMS()); /* final check of RMS */
616     badeof:
617     if (report != REP_QUIET)
618 greg 2.6 printf("%s: unexpected end-of-file\n", progname);
619 greg 2.1 return(0);
620     }
621    
622     /* Compare two Radiance files for equivalence */
623     int
624     main(int argc, char *argv[])
625     {
626     int typ1, typ2;
627     int a;
628    
629     progname = argv[0];
630     for (a = 1; a < argc && argv[a][0] == '-'; a++) {
631     switch (argv[a][1]) {
632     case 'h': /* ignore header info. */
633     ign_header = !ign_header;
634     continue;
635     case 's': /* silent operation */
636     report = REP_QUIET;
637     continue;
638     case 'w': /* turn off warnings */
639     if (report > REP_ERROR)
640     report = REP_ERROR;
641     continue;
642     case 'v': /* turn on verbose mode */
643     report = REP_VERBOSE;
644     continue;
645     case 'm': /* maximum epsilon */
646     max_lim = atof(argv[++a]);
647     continue;
648     case 'r':
649     if (argv[a][2] == 'e') /* relative difference */
650     rel_min = atof(argv[++a]);
651     else if (argv[a][2] == 'm') /* RMS limit */
652     rms_lim = atof(argv[++a]);
653     else
654     usage();
655     continue;
656     case '\0': /* first file from stdin */
657     f1in = stdin;
658     f1name = stdin_name;
659     break;
660     default:
661     usage();
662     }
663     break;
664     }
665     if (a != argc-2)
666     usage();
667     if (!f1name) f1name = argv[a];
668     if (!f2name) f2name = argv[a+1];
669    
670     if (!strcmp(f1name, f2name)) { /* inputs are same? */
671     if (report >= REP_WARN)
672 greg 2.4 printf("%s: warning - identical inputs given\n",
673 greg 2.1 progname);
674     return(0);
675     }
676     /* open inputs */
677     SET_FILE_BINARY(stdin); /* in case we're using it */
678     if (!f1in && !(f1in = fopen(f1name, "rb"))) {
679     fprintf(stderr, "%s: cannot open for reading\n", f1name);
680     return(1);
681     }
682     if (!strcmp(f2name, "-")) {
683     f2in = stdin;
684     f2name = stdin_name;
685     } else if (!(f2in = fopen(f2name, "rb"))) {
686     fprintf(stderr, "%s: cannot open for reading\n", f2name);
687     return(1);
688     }
689     /* load headers */
690     if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0)
691     return(1);
692     if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0)
693     return(1);
694     if (typ1 != typ2) {
695     if (report != REP_QUIET)
696 greg 2.4 printf("%s: '%s' is %s and '%s' is %s\n",
697 greg 2.1 progname, f1name, file_type[typ1],
698     f2name, file_type[typ2]);
699     return(1);
700     }
701     ign_header |= !has_header(typ1); /* check headers if indicated */
702     if (!ign_header && !headers_match(&hdr1, &hdr2))
703     return(1);
704     lu_done(&hdr1); lu_done(&hdr2);
705     if (!ign_header & (report >= REP_WARN)) {
706     if (typ1 == TYP_UNKNOWN)
707 greg 2.4 printf("%s: warning - unrecognized format, comparing as binary\n",
708 greg 2.1 progname);
709     if (lin1cnt != lin2cnt)
710 greg 2.4 printf("%s: warning - headers are different lengths\n",
711 greg 2.1 progname);
712     }
713     if (report >= REP_VERBOSE)
714 greg 2.4 printf("%s: input file type is %s\n",
715 greg 2.1 progname, file_type[typ1]);
716    
717     switch (typ1) { /* compare based on type */
718     case TYP_BINARY:
719     case TYP_TMESH:
720     case TYP_OCTREE:
721     case TYP_RBFMESH:
722     case TYP_UNKNOWN:
723     return( !compare_binary() );
724     case TYP_TEXT:
725     case TYP_ASCII:
726     return( !compare_text() );
727     case TYP_RGBE:
728     case TYP_XYZE:
729     return( !compare_hdr() );
730     case TYP_FLOAT:
731     return( !compare_float() );
732     case TYP_DOUBLE:
733     return( !compare_double() );
734     }
735     return(1);
736     }