ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/radcompare.c
(Generate patch)

Comparing ray/src/util/radcompare.c (file contents):
Revision 2.1 by greg, Mon Oct 15 17:52:52 2018 UTC vs.
Revision 2.33 by greg, Tue Nov 21 19:22:38 2023 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   */
9  
10   #include <stdlib.h>
11 #include <math.h>
11   #include <ctype.h>
12 + #include "rtmath.h"
13   #include "platform.h"
14   #include "rtio.h"
15   #include "resolu.h"
16   #include "color.h"
17 + #include "depthcodec.h"
18 + #include "normcodec.h"
19   #include "lookup.h"
20                          /* Reporting levels */
21   #define REP_QUIET       0       /* no reporting */
# Line 25 | Line 27 | int    report = REP_WARN;      /* reporting level */
27  
28   int     ign_header = 0;         /* ignore header differences? */
29  
30 + int     escape_newlines = 0;    /* allow backslash to skip newlines */
31 +
32   double  rel_min = 1e-5;         /* positive for relative comparisons */
33  
34   double  rms_lim = 0.01;         /* RMS difference limit */
# Line 33 | Line 37 | double max_lim = 0.25;         /* difference limit if non-neg
37  
38   int     lin1cnt=0, lin2cnt=0;   /* file line position */
39  
40 + int     comment_c = '\0';       /* comment delimiter for text files */
41 +
42 + 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                                  /* file types */
48   const char      *file_type[] = {
49                          "Unrecognized",
# Line 40 | Line 51 | const char     *file_type[] = {
51                          "ascii",
52                          COLRFMT,
53                          CIEFMT,
54 <                        "BINARY_float",
55 <                        "BINARY_double",
54 >                        SPECFMT,
55 >                        DEPTH16FMT,
56 >                        NORMAL32FMT,
57 >                        "float",
58 >                        "double",
59                          "BSDF_RBFmesh",
60                          "Radiance_octree",
61                          "Radiance_tmesh",
62 +                        "8-bit_indexed_name",
63 +                        "16-bit_indexed_name",
64 +                        "24-bit_indexed_name",
65                          "BINARY_unknown",
66                          NULL    /* terminator */
67                  };
68 <
69 < enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, TYP_FLOAT,
70 <                TYP_DOUBLE, TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, TYP_BINARY};
68 >                                /* keep consistent with above */
69 > enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, TYP_SPEC,
70 >                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                  
74   #define has_header(t)   (!( 1L<<(t) & (1L<<TYP_TEXT | 1L<<TYP_BINARY) ))
75  
# Line 59 | Line 78 | const char     *hdr_ignkey[] = {
78                          "SOFTWARE",
79                          "CAPDATE",
80                          "GMT",
81 +                        "FRAME",
82                          NULL    /* terminator */
83                  };
84                                  /* header variable settings */
# Line 69 | Line 89 | LUTAB  hdr2 = LU_SINIT(free,free);
89   #define adv_linecnt(htp)        (lin1cnt += (htp == &hdr1), \
90                                          lin2cnt += (htp == &hdr2))
91  
92 + typedef struct {                /* dynamic line buffer */
93 +        char    *str;
94 +        int     len;
95 +        int     siz;
96 + } LINEBUF;
97 +
98 + #define init_line(bp)   ((bp)->str = NULL, (bp)->siz = 0)
99 +                                /* 100 MByte limit on line buffer */
100 + #define MAXBUF          (100L<<20)
101 +
102                                  /* 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 + int             f1swap=0, f2swap=0;
108 +
109                                  /* running real differences */
110   double  diff2sum = 0;
111 < int     nsum = 0;
111 > long    nsum = 0;
112  
113   /* Report usage and exit */
114   static void
# Line 84 | Line 116 | usage()
116   {
117          fputs("Usage: ", stderr);
118          fputs(progname, stderr);
119 <        fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] file1 file2\n",
119 >        fputs(" [-h][-c#][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n",
120                          stderr);
121 <        exit(1);
121 >        exit(2);
122   }
123  
124 + /* Read a text line, increasing buffer size as necessary */
125 + static int
126 + read_line(LINEBUF *bp, FILE *fp)
127 + {
128 +        static int      doneWarn = 0;
129 +
130 +        bp->len = 0;
131 +        if (!bp->str) {
132 +                bp->str = (char *)malloc(bp->siz = 512);
133 +                if (!bp->str)
134 +                        goto memerr;
135 +        }
136 +        while (fgets(bp->str + bp->len, bp->siz - bp->len, fp)) {
137 +                bp->len += strlen(bp->str + bp->len);
138 +                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 +                        break;          /* found EOL */
150 +                }
151 +                if (bp->len < bp->siz - 4)
152 +                        continue;       /* at EOF? */
153 +                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 +                if ((bp->siz += bp->siz/2) > MAXBUF)
163 +                        bp->siz = MAXBUF;
164 +                bp->str = (char *)realloc(bp->str, bp->siz);
165 +                if (!bp->str)
166 +                        goto memerr;
167 +        }
168 +        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 +        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 +        if (bp->str) free(bp->str);
189 +        init_line(bp);
190 + }
191 +
192   /* Get type ID from name (or 0 if not found) */
193   static int
194   xlate_type(const char *nm)
# Line 111 | Line 211 | real_check(double r1, double r2)
211  
212          if (rel_min > 0) {      /* doing relative differences? */
213                  double  av2 = .25*(r1*r1 + 2.*fabs(r1*r2) + r2*r2);
214 <                if (av2 > rel_min*rel_min)
215 <                        diff2 /= av2;
214 >                if (av2 < rel_min*rel_min)
215 >                        av2 = rel_min*rel_min;
216 >                diff2 /= av2;
217          }
218          if (max_lim >= 0 && diff2 > max_lim*max_lim) {
219                  if (report != REP_QUIET)
220 <                        fprintf(stderr,
221 <                        "%s: %sdifference between %.8g and %.8g exceeds epsilon\n",
220 >                        printf(
221 >                        "%s: %sdifference between %.8g and %.8g exceeds epsilon of %.8g\n",
222                                          progname,
223                                          (rel_min > 0) ? "relative " : "",
224 <                                        r1, r2);
224 >                                        r1, r2, max_lim);
225                  return(0);
226          }
227          diff2sum += diff2;
# Line 128 | Line 229 | real_check(double r1, double r2)
229          return(1);
230   }
231  
232 + /* Compare two color values for equivalence */
233 + static int
234 + color_check(COLOR c1, COLOR c2)
235 + {
236 +        int     p;
237 +
238 +        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 +                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 + /* Compare two color spectra for equivalence */
249 + static int
250 + spec_check(COLORV *sc1, COLORV *sc2)
251 + {
252 +        int     p, k;
253 +
254 +        if (!real_check(scolor_mean(sc1), scolor_mean(sc2)))
255 +                return(0);
256 +
257 +        p = 0;                          /* find max. component */
258 +        for (k = NCSAMP; --k; )
259 +                if (sc1[k] > sc1[p])
260 +                        p = k;
261 +
262 +        return(real_check(sc1[p], sc2[p]));
263 + }
264 +
265 + /* Compare two normal directions for equivalence */
266 + static int
267 + norm_check(FVECT nv1, FVECT nv2)
268 + {
269 +        double  max2 = nv1[2]*nv2[2];
270 +        int     imax = 2;
271 +        int     i = 2;
272 +                                        /* identify largest component */
273 +        while (i--) {
274 +                double  tm2 = nv1[i]*nv2[i];
275 +                if (tm2 > max2) {
276 +                        imax = i;
277 +                        max2 = tm2;
278 +                }
279 +        }
280 +        i = 3;                          /* compare smaller components */
281 +        while (i--) {
282 +                if (i == imax)
283 +                        continue;
284 +                if (!real_check(nv1[i], nv2[i]))
285 +                        return(0);
286 +        }
287 +        return(1);
288 + }
289 +
290   /* Compare two strings for equivalence */
291   static int
292   equiv_string(char *s1, char *s2)
# Line 138 | Line 297 | equiv_string(char *s1, char *s2)
297                                  /* skip whitespace at beginning */
298          while (isspace(*s1)) s1++;
299          while (isspace(*s2)) s2++;
141        if (!strcmp(s1, s2))    /* quick check */
142                return(1);
300          while (*s1) {           /* check each word */
301 <                int     inquote = *s1;
301 >                int     inquote;
302 >                if (!*s2)       /* unexpected EOL in s2? */
303 >                        return(0);
304 >                inquote = *s1;
305                  if ((inquote != '\'') & (inquote != '"'))
306                          inquote = 0;
307                  if (inquote) {  /* quoted text must match exactly */
# Line 199 | Line 359 | equiv_string(char *s1, char *s2)
359   static int
360   setheadvar(char *val, void *p)
361   {
362 +        char    newval[128];
363          LUTAB   *htp = (LUTAB *)p;
364          LUENT   *tep;
365          char    *key;
366          int     kln, vln;
367          int     n;
368  
369 +        adv_linecnt(htp);       /* side-effect is to count lines */
370          if (!isalpha(*val))     /* key must start line */
371                  return(0);
372 +                                /* check if we need to swap binary data */
373 +        if ((n = isbigendian(val)) >= 0) {
374 +                if (nativebigendian() == n)
375 +                        return(0);
376 +                f1swap += (htp == &hdr1);
377 +                f2swap += (htp == &hdr2);
378 +                return(0);
379 +        }
380          key = val++;
381          while (*val && !isspace(*val) & (*val != '='))
382                  val++;
# Line 220 | Line 390 | setheadvar(char *val, void *p)
390                  val++;
391          if (!*val)              /* nothing set? */
392                  return(0);
223        vln = strlen(val);      /* eliminate space and newline at end */
224        while (--vln > 0 && isspace(val[vln]))
225                ;
226        val[++vln] = '\0';
393                                  /* check if key to ignore */
394          for (n = 0; hdr_ignkey[n]; n++)
395                  if (!strcmp(key, hdr_ignkey[n]))
396                          return(0);
397 +        vln = strlen(val);      /* eliminate space and newline at end */
398 +        while (isspace(val[--vln]))
399 +                ;
400 +        val[++vln] = '\0';
401          if (!(tep = lu_find(htp, key)))
402 <                return(-1);
402 >                return(-1);     /* memory allocation error */
403          if (!tep->key)
404                  tep->key = strcpy(malloc(kln+1), key);
405 <        if (tep->data)
405 >        if (tep->data) {        /* check for special cases */
406 >                if (!strcmp(key, "EXPOSURE")) {
407 >                        sprintf(newval, "%.6e", atof(tep->data)*atof(val));
408 >                        vln = strlen(val = newval);
409 >                }
410                  free(tep->data);
411 +        }
412          tep->data = strcpy(malloc(vln+1), val);
238        adv_linecnt(htp);
413          return(1);
414   }
415  
# Line 246 | Line 420 | match_val(const LUENT *ep1, void *p2)
420          const LUENT     *ep2 = lu_find((LUTAB *)p2, ep1->key);
421          if (!ep2 || !ep2->data) {
422                  if (report != REP_QUIET)
423 <                        fprintf(stderr, "%s: Variable '%s' missing in '%s'\n",
423 >                        printf("%s: variable '%s' missing in '%s'\n",
424                                          progname, ep1->key, f2name);
425                  return(-1);
426          }
427          if (!equiv_string((char *)ep1->data, (char *)ep2->data)) {
428 <                if (report != REP_QUIET)
429 <                        fprintf(stderr, "%s: Header variables '%s' have different values\n",
428 >                if (report != REP_QUIET) {
429 >                        printf("%s: header variable '%s' has different values\n",
430                                          progname, ep1->key);
431 +                        if (report >= REP_VERBOSE) {
432 +                                printf("%s: %s=%s\n", f1name,
433 +                                                ep1->key, (char *)ep1->data);
434 +                                printf("%s: %s=%s\n", f2name,
435 +                                                ep2->key, (char *)ep2->data);
436 +                        }
437 +                }
438                  return(-1);
439          }
440          return(1);              /* good match */
# Line 261 | Line 442 | match_val(const LUENT *ep1, void *p2)
442  
443   /* Compare two sets of header variables */
444   static int
445 < headers_match(LUTAB *hp1, LUTAB *hp2)
445 > headers_match()
446   {
447 <        int     ne = lu_doall(hp1, match_val, hp2);
447 >        int     ne = lu_doall(&hdr1, match_val, &hdr2);
448          if (ne < 0)
449                  return(0);      /* something didn't match! */
450 <        ne = lu_doall(hp2, NULL, NULL) - ne;
451 <        if (ne) {
452 <                if (report != REP_QUIET)
272 <                        fprintf(stderr, "%s: '%s' has %d extra header variable(s)\n",
450 >                                /* non-fatal if second header has extra */
451 >        if (report >= REP_WARN && (ne = lu_doall(&hdr2, NULL, NULL) - ne))
452 >                printf("%s: warning - '%s' has %d extra header setting(s)\n",
453                                          progname, f2name, ne);
274                return(0);
275        }
454          return(1);              /* good match */
455   }
456  
# Line 284 | Line 462 | input_is_binary(FILE *fin)
462          int     c = 0;
463  
464          while ((c = getc(fin)) != EOF) {
465 +                ++n;
466                  if (!c | (c > 127))
467                          break;                  /* non-ascii character */
468 <                if (++n >= 10240)
468 >                if (n >= 10240)
469                          break;                  /* enough to be confident */
470          }
471          if (!n)
# Line 316 | Line 495 | identify_type(const char *name, FILE *fin, LUTAB *htp)
495                  char    sbuf[32];
496                  if (!fgets(sbuf, sizeof(sbuf), fin))
497                          goto badeof;
498 +                adv_linecnt(htp);               /* for #?ID string */
499                  if (report >= REP_WARN && strncmp(sbuf, "RADIANCE", 8)) {
500 <                        fputs(name, stderr);
501 <                        fputs(": warning - unexpected header ID: ", stderr);
502 <                        fputs(sbuf, stderr);
500 >                        fputs(name, stdout);
501 >                        fputs(": warning - unexpected header ID: ", stdout);
502 >                        fputs(sbuf, stdout);
503                  }
324                adv_linecnt(htp);               /* for #?ID string */
504                  if (getheader(fin, setheadvar, htp) < 0) {
505                          fputs(name, stderr);
506 <                        fputs(": Unknown error reading header\n", stderr);
506 >                        fputs(": unknown error reading header\n", stderr);
507                          return(-1);
508                  }
509                  adv_linecnt(htp);               /* for trailing emtpy line */
# Line 338 | Line 517 | identify_type(const char *name, FILE *fin, LUTAB *htp)
517          }
518          if (c)
519                  return(TYP_BINARY);
341        SET_FILE_TEXT(fin);                     /* originally set to binary */
520          return(TYP_TEXT);
521   badeof:
522          if (report != REP_QUIET) {
523 <                fputs(name, stderr);
524 <                fputs(": Unexpected end-of-file\n", stderr);
523 >                fputs(name, stdout);
524 >                fputs(": unexpected end-of-file\n", stdout);
525          }
526          return(-1);
527   }
# Line 356 | Line 534 | good_RMS()
534                  return(1);
535          if (diff2sum/(double)nsum > rms_lim*rms_lim) {
536                  if (report != REP_QUIET)
537 <                        fprintf(stderr,
538 <        "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit\n",
537 >                        printf(
538 >        "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit of %.5g\n",
539                                          progname,
540                                          (rel_min > 0) ? "relative " : "",
541                                          f1name, f2name,
542 <                                        sqrt(diff2sum/(double)nsum));
542 >                                        sqrt(diff2sum/(double)nsum), rms_lim);
543                  return(0);
544          }
545          if (report >= REP_VERBOSE)
546 <                fprintf(stderr,
369 <                        "%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n",
546 >                printf("%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n",
547                                  progname, (rel_min > 0) ? "relative " : "",
548                                  f1name, f2name, sqrt(diff2sum/(double)nsum));
549          return(1);
# Line 376 | Line 553 | good_RMS()
553   static int
554   compare_binary()
555   {
556 +        int     c1=0, c2=0;
557 +
558          if (report >= REP_VERBOSE) {
559 <                fputs(progname, stderr);
560 <                fputs(": comparing inputs as binary\n", stderr);
559 >                fputs(progname, stdout);
560 >                fputs(": comparing inputs as binary\n", stdout);
561          }
562          for ( ; ; ) {                           /* exact byte matching */
563 <                int     c1 = getc(f1in);
564 <                int     c2 = getc(f2in);
563 >                c1 = getc(f1in);
564 >                c2 = getc(f2in);
565                  if (c1 == EOF) {
566                          if (c2 == EOF)
567                                  return(1);      /* success! */
568                          if (report != REP_QUIET) {
569 <                                fputs(f1name, stderr);
570 <                                fputs(": Unexpected end-of-file\n", stderr);
569 >                                fputs(f1name, stdout);
570 >                                fputs(": unexpected end-of-file\n", stdout);
571                          }
572                          return(0);
573                  }
574                  if (c2 == EOF) {
575                          if (report != REP_QUIET) {
576 <                                fputs(f2name, stderr);
577 <                                fputs(": Unexpected end-of-file\n", stderr);
576 >                                fputs(f2name, stdout);
577 >                                fputs(": unexpected end-of-file\n", stdout);
578                          }
579                          return(0);
580                  }
# Line 404 | Line 583 | compare_binary()
583          }
584          if (report == REP_QUIET)
585                  return(0);
586 <        fprintf(stderr, "%s: binary files '%s' and '%s' differ at offset %ld|%ld\n",
586 >        printf("%s: binary files '%s' and '%s' differ at byte offset %ld|%ld\n",
587                          progname, f1name, f2name, ftell(f1in), ftell(f2in));
588 +        if (report >= REP_VERBOSE)
589 +                printf("%s: byte in '%s' is 0x%X, byte in '%s' is 0x%X\n",
590 +                                progname, f1name, c1, f2name, c2);
591          return(0);
592   }
593  
# Line 413 | Line 595 | compare_binary()
595   static int
596   compare_text()
597   {
598 <        char    l1buf[4096], l2buf[4096];
598 >        LINEBUF l1buf, l2buf;
599  
600          if (report >= REP_VERBOSE) {
601 <                fputs(progname, stderr);
602 <                fputs(": comparing inputs as ASCII text\n", stderr);
601 >                fputs(progname, stdout);
602 >                fputs(": comparing inputs as ASCII text", stdout);
603 >                if (escape_newlines)
604 >                        fputs(", allowing escaped newlines", stdout);
605 >                if (comment_c) {
606 >                        fputs(", ignoring comments starting with '", stdout);
607 >                        fputc(comment_c, stdout);
608 >                        fputc('\'', stdout);
609 >                }
610 >                fputc('\n', stdout);
611          }
612 <                                                /* compare a line at a time */
613 <        while (fgets(l1buf, sizeof(l1buf), f1in)) {
612 >        SET_FILE_TEXT(f1in);                    /* originally set to binary */
613 >        SET_FILE_TEXT(f2in);
614 >        init_line(&l1buf); init_line(&l2buf);   /* compare a line at a time */
615 >        while (read_line(&l1buf, f1in)) {
616                  lin1cnt++;
617 <                if (!*sskip2(l1buf,0))
617 >                if (!*sskip2(l1buf.str,0))
618                          continue;               /* ignore empty lines */
619 <                while (fgets(l2buf, sizeof(l2buf), f2in)) {
619 >
620 >                while (read_line(&l2buf, f2in)) {
621                          lin2cnt++;
622 <                        if (*sskip2(l2buf,0))
622 >                        if (*sskip2(l2buf.str,0))
623                                  break;          /* found other non-empty line */
624                  }
625 <                if (feof(f2in)) {
625 >                if (!l2buf.len) {               /* input 2 EOF? */
626                          if (report != REP_QUIET) {
627 <                                fputs(f2name, stderr);
628 <                                fputs(": Unexpected end-of-file\n", stderr);
627 >                                fputs(f2name, stdout);
628 >                                fputs(": unexpected end-of-file\n", stdout);
629                          }
630 +                        free_line(&l1buf); free_line(&l2buf);
631                          return(0);
632                  }
633                                                  /* compare non-empty lines */
634 <                if (!equiv_string(l1buf, l2buf)) {
634 >                if (!equiv_string(l1buf.str, l2buf.str)) {
635                          if (report != REP_QUIET) {
636 <                                fprintf(stderr, "%s: inputs '%s' and '%s' differ at line %d|%d\n",
637 <                                                progname, f1name, f2name,
638 <                                                lin1cnt, lin2cnt);
639 <                                if (report >= REP_VERBOSE) {
640 <                                        fputs("------------- Mismatch -------------\n", stderr);
641 <                                        fprintf(stderr, "%s(%d):\t%s", f1name,
642 <                                                        lin1cnt, l1buf);
643 <                                        fprintf(stderr, "%s(%d):\t%s", f2name,
644 <                                                        lin2cnt, l2buf);
636 >                                printf("%s: inputs '%s' and '%s' differ at line %d",
637 >                                                progname, f1name, f2name, lin1cnt);
638 >                                if (lin1cnt != lin2cnt)
639 >                                        printf("|%d\n", lin2cnt);
640 >                                else
641 >                                        putchar('\n');
642 >                                if ( report >= REP_VERBOSE &&
643 >                                                (l1buf.len < 256) &
644 >                                                (l2buf.len < 256) ) {
645 >                                        fputs("------------- Mismatch -------------\n", stdout);
646 >                                        printf("%s@%d:\t%s", f1name,
647 >                                                        lin1cnt, l1buf.str);
648 >                                        printf("%s@%d:\t%s", f2name,
649 >                                                        lin2cnt, l2buf.str);
650                                  }
651                          }
652 +                        free_line(&l1buf); free_line(&l2buf);
653                          return(0);
654                  }
655          }
656 <                                                /* check for EOF on input 2 */
657 <        while (fgets(l2buf, sizeof(l2buf), f2in)) {
658 <                if (!*sskip2(l2buf,0))
656 >        free_line(&l1buf);                      /* check for EOF on input 2 */
657 >        while (read_line(&l2buf, f2in)) {
658 >                if (!*sskip2(l2buf.str,0))
659                          continue;
660                  if (report != REP_QUIET) {
661 <                        fputs(f1name, stderr);
662 <                        fputs(": Unexpected end-of-file\n", stderr);
661 >                        fputs(f1name, stdout);
662 >                        fputs(": unexpected end-of-file\n", stdout);
663                  }
664 +                free_line(&l2buf);
665                  return(0);
666          }
667 +        free_line(&l2buf);
668          return(good_RMS());                     /* final check for reals */
669   }
670  
671 + /* Set resolution based on NROWS, NCOLS in header */
672 + static int
673 + set_resolu(RESOLU *rs, LUTAB *htp)
674 + {
675 +        const char      *val;
676 +
677 +        rs->rt = PIXSTANDARD;
678 +        val = (const char *)lu_find(htp, "NROWS")->data;
679 +        if (!val) return(0);
680 +        rs->yr = atoi(val);
681 +        if (rs->yr <= 0) return(-1);
682 +        val = (const char *)lu_find(htp, "NCOLS")->data;
683 +        if (!val) return(0);
684 +        rs->xr = atoi(val);
685 +        if (rs->xr <= 0) return(-1);
686 +        return(1);
687 + }
688 +
689 + /* Check image/map resolutions */
690 + static int
691 + check_resolu(const char *class, RESOLU *r1p, RESOLU *r2p)
692 + {
693 +        if (r1p->rt != r2p->rt) {
694 +                if (report != REP_QUIET)
695 +                        printf(
696 +                        "%s: %ss '%s' and '%s' have different pixel ordering\n",
697 +                                        progname, class, f1name, f2name);
698 +                return(0);
699 +        }
700 +        if ((r1p->xr != r2p->xr) | (r1p->yr != r2p->yr)) {
701 +                if (report != REP_QUIET)
702 +                        printf(
703 +                        "%s: %ss '%s' and '%s' are different sizes\n",
704 +                                        progname, class, f1name, f2name);
705 +                return(0);
706 +        }
707 +        return(1);
708 + }
709 +
710   /* Compare two inputs that are known to be RGBE or XYZE images */
711   static int
712   compare_hdr()
# Line 474 | Line 715 | compare_hdr()
715          COLOR   *scan1, *scan2;
716          int     x, y;
717  
718 +        if (report >= REP_VERBOSE) {
719 +                fputs(progname, stdout);
720 +                fputs(": comparing inputs as HDR images\n", stdout);
721 +        }
722          fgetsresolu(&rs1, f1in);
723          fgetsresolu(&rs2, f2in);
724 <        if (rs1.rt != rs2.rt) {
480 <                if (report != REP_QUIET)
481 <                        fprintf(stderr,
482 <                        "%s: Images '%s' and '%s' have different pixel ordering\n",
483 <                                        progname, f1name, f2name);
724 >        if (!check_resolu("HDR image", &rs1, &rs2))
725                  return(0);
485        }
486        if ((rs1.xr != rs2.xr) | (rs1.yr != rs2.yr)) {
487                if (report != REP_QUIET)
488                        fprintf(stderr,
489                        "%s: Images '%s' and '%s' are different sizes\n",
490                                        progname, f1name, f2name);
491                return(0);
492        }
726          scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR));
727          scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR));
728          if (!scan1 | !scan2) {
# Line 500 | Line 733 | compare_hdr()
733                  if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) |
734                                  (freadscan(scan2, scanlen(&rs2), f2in) < 0)) {
735                          if (report != REP_QUIET)
736 <                                fprintf(stderr, "%s: Unexpected end-of-file\n",
736 >                                printf("%s: unexpected end-of-file\n",
737                                                  progname);
738                          free(scan1);
739                          free(scan2);
740                          return(0);
741                  }
742 <                for (x = scanlen(&rs1); x--; ) {
743 <                        if (real_check(colval(scan1[x],RED),
511 <                                                colval(scan2[x],RED)) &
512 <                                        real_check(colval(scan1[x],GRN),
513 <                                                colval(scan2[x],GRN)) &
514 <                                        real_check(colval(scan1[x],BLU),
515 <                                                colval(scan2[x],BLU)))
742 >                for (x = 0; x < scanlen(&rs1); x++) {
743 >                        if (color_check(scan1[x], scan2[x]))
744                                  continue;
745                          if (report != REP_QUIET) {
746 <                                fprintf(stderr,
519 <                                "%s: pixels at scanline %d offset %d differ\n",
746 >                                printf("%s: pixels at scanline %d offset %d differ\n",
747                                          progname, y, x);
748                                  if (report >= REP_VERBOSE) {
749 <                                        fprintf(stderr, "%s: (R,G,B)=(%g,%g,%g)\n",
749 >                                        printf("%s: (R,G,B)=(%g,%g,%g)\n",
750                                                  f1name, colval(scan1[x],RED),
751                                                  colval(scan1[x],GRN),
752                                                  colval(scan1[x],BLU));
753 <                                        fprintf(stderr, "%s: (R,G,B)=(%g,%g,%g)\n",
753 >                                        printf("%s: (R,G,B)=(%g,%g,%g)\n",
754                                                  f2name, colval(scan2[x],RED),
755                                                  colval(scan2[x],GRN),
756                                                  colval(scan2[x],BLU));
# Line 539 | Line 766 | compare_hdr()
766          return(good_RMS());                     /* final check of RMS */
767   }
768  
769 < const char      nsuffix[10][3] = {              /* 1st, 2nd, 3rd, etc. */
770 <                        "th","st","nd","rd","th","th","th","th","th","th"
771 <                };
769 > /* Compare two inputs that are known to be spectral images */
770 > static int
771 > compare_spec()
772 > {
773 >        static char     NCstr[] = NCOMPSTR;
774 >        RESOLU          rs1, rs2;
775 >        COLORV          *scan1, *scan2;
776 >        const char      *val;
777 >        int             x, y;
778  
779 +        if (report >= REP_VERBOSE) {
780 +                fputs(progname, stdout);
781 +                fputs(": comparing inputs as spectral images\n", stdout);
782 +        }
783 +        if (!(set_resolu(&rs1, &hdr1) || fgetsresolu(&rs1, f1in)))
784 +                return(0);
785 +        if (!(set_resolu(&rs2, &hdr2) || fgetsresolu(&rs2, f2in)))
786 +                return(0);
787 +        if (!check_resolu("Spectral image", &rs1, &rs2))
788 +                return(0);
789 +        NCstr[LNCOMPSTR-1] = '\0';
790 +        val = (const char *)lu_find(&hdr1, NCstr)->data;
791 +        if (!val || (NCSAMP = atoi(val)) < 3) {
792 +                if (report != REP_QUIET) {
793 +                        if (val)
794 +                                printf("%s: illegal # components (%d) for spectral image\n",
795 +                                        progname, NCSAMP);
796 +                        else
797 +                                printf("%s: missing %s in header for spectral image\n",
798 +                                        progname, NCstr);
799 +                }
800 +                return(0);
801 +        }
802 +        scan1 = (COLORV *)malloc(sizeof(COLORV)*NCSAMP*scanlen(&rs1));
803 +        scan2 = (COLORV *)malloc(sizeof(COLORV)*NCSAMP*scanlen(&rs2));
804 +        if (!scan1 | !scan2) {
805 +                fprintf(stderr, "%s: out of memory in compare_hdr()\n", progname);
806 +                exit(2);
807 +        }
808 +        for (y = 0; y < numscans(&rs1); y++) {
809 +                if ((freadsscan(scan1, NCSAMP, scanlen(&rs1), f1in) < 0) |
810 +                                (freadsscan(scan2, NCSAMP, scanlen(&rs2), f2in) < 0)) {
811 +                        if (report != REP_QUIET)
812 +                                printf("%s: unexpected end-of-file\n", progname);
813 +                        free(scan1);
814 +                        free(scan2);
815 +                        return(0);
816 +                }
817 +                for (x = 0; x < scanlen(&rs1); x++) {
818 +                        if (spec_check(scan1+x*NCSAMP, scan2+x*NCSAMP))
819 +                                continue;
820 +                        if (report != REP_QUIET) {
821 +                                int     k;
822 +                                printf("%s: spectra at scanline %d offset %d differ\n",
823 +                                        progname, y, x);
824 +                                if (report >= REP_VERBOSE) {
825 +                                        printf("%s: spectrum =", f1name);
826 +                                        for (k = 0; k < NCSAMP; k++)
827 +                                                printf(" %g", scan1[x*NCSAMP+k]);
828 +                                        fputc('\n', stdout);
829 +                                        printf("%s: spectrum =", f2name);
830 +                                        for (k = 0; k < NCSAMP; k++)
831 +                                                printf(" %g", scan2[x*NCSAMP+k]);
832 +                                        fputc('\n', stdout);
833 +                                }
834 +                        }
835 +                        free(scan1);
836 +                        free(scan2);
837 +                        return(0);
838 +                }
839 +        }
840 +        free(scan1);
841 +        free(scan2);
842 +        return(good_RMS());                     /* final check of RMS */
843 + }
844 +
845 + /* Set reference depth based on header variable */
846 + static int
847 + set_refdepth(DEPTHCODEC *dcp, LUTAB *htp)
848 + {
849 +        static char     depthvar[] = DEPTHSTR;
850 +        const char      *drval;
851 +
852 +        depthvar[LDEPTHSTR-1] = '\0';
853 +        drval = (const char *)lu_find(htp, depthvar)->data;
854 +        if (!drval)
855 +                return(0);
856 +        dcp->refdepth = atof(drval);
857 +        if (dcp->refdepth <= 0) {
858 +                if (report != REP_QUIET) {
859 +                        fputs(dcp->inpname, stderr);
860 +                        fputs(": bad reference depth '", stderr);
861 +                        fputs(drval, stderr);
862 +                        fputs("'\n", stderr);
863 +                }
864 +                return(-1);
865 +        }
866 +        return(1);
867 + }
868 +
869 + /* Compare two encoded depth maps */
870 + static int
871 + compare_depth()
872 + {
873 +        long            nread = 0;
874 +        DEPTHCODEC      dc1, dc2;
875 +
876 +        if (report >= REP_VERBOSE) {
877 +                fputs(progname, stdout);
878 +                fputs(": comparing inputs as depth maps\n", stdout);
879 +        }
880 +        set_dc_defaults(&dc1);
881 +        dc1.hdrflags = HF_RESIN;
882 +        dc1.finp = f1in;
883 +        dc1.inpname = f1name;
884 +        set_dc_defaults(&dc2);
885 +        dc2.hdrflags = HF_RESIN;
886 +        dc2.finp = f2in;
887 +        dc2.inpname = f2name;
888 +        if (report != REP_QUIET) {
889 +                dc1.hdrflags |= HF_STDERR;
890 +                dc2.hdrflags |= HF_STDERR;
891 +        }
892 +        if (!process_dc_header(&dc1, 0, NULL))
893 +                return(0);
894 +        if (!process_dc_header(&dc2, 0, NULL))
895 +                return(0);
896 +        if (!check_resolu("Depth map", &dc1.res, &dc2.res))
897 +                return(0);
898 +        if (set_refdepth(&dc1, &hdr1) < 0)
899 +                return(0);
900 +        if (set_refdepth(&dc2, &hdr2) < 0)
901 +                return(0);
902 +        while (nread < dc1.res.xr*dc1.res.yr) {
903 +                double  d1 = decode_depth_next(&dc1);
904 +                double  d2 = decode_depth_next(&dc2);
905 +                if ((d1 < 0) | (d2 < 0)) {
906 +                        if (report != REP_QUIET)
907 +                                printf("%s: unexpected end-of-file\n",
908 +                                                progname);
909 +                        return(0);
910 +                }
911 +                ++nread;
912 +                if (real_check(d1, d2))
913 +                        continue;
914 +                if (report != REP_QUIET)
915 +                        printf("%s: %ld%s depth values differ\n",
916 +                                        progname, nread, num_sfx(nread));
917 +                return(0);
918 +        }
919 +        return(good_RMS());             /* final check of RMS */
920 + }
921 +
922 + /* Compare two encoded normal maps */
923 + static int
924 + compare_norm()
925 + {
926 +        long            nread = 0;
927 +        NORMCODEC       nc1, nc2;
928 +
929 +        if (report >= REP_VERBOSE) {
930 +                fputs(progname, stdout);
931 +                fputs(": comparing inputs as normal maps\n", stdout);
932 +        }
933 +        set_nc_defaults(&nc1);
934 +        nc1.hdrflags = HF_RESIN;
935 +        nc1.finp = f1in;
936 +        nc1.inpname = f1name;
937 +        set_nc_defaults(&nc2);
938 +        nc2.hdrflags = HF_RESIN;
939 +        nc2.finp = f2in;
940 +        nc2.inpname = f2name;
941 +        if (report != REP_QUIET) {
942 +                nc1.hdrflags |= HF_STDERR;
943 +                nc2.hdrflags |= HF_STDERR;
944 +        }
945 +        if (!process_nc_header(&nc1, 0, NULL))
946 +                return(0);
947 +        if (!process_nc_header(&nc2, 0, NULL))
948 +                return(0);
949 +        if (!check_resolu("Normal map", &nc1.res, &nc2.res))
950 +                return(0);
951 +        while (nread < nc1.res.xr*nc1.res.yr) {
952 +                FVECT   nv1, nv2;
953 +                int     rv1 = decode_normal_next(nv1, &nc1);
954 +                int     rv2 = decode_normal_next(nv2, &nc2);
955 +                if ((rv1 < 0) | (rv2 < 0)) {
956 +                        if (report != REP_QUIET)
957 +                                printf("%s: unexpected end-of-file\n",
958 +                                                progname);
959 +                        return(0);
960 +                }
961 +                ++nread;
962 +                if (rv1 == rv2 && (!rv1 || norm_check(nv1, nv2)))
963 +                        continue;
964 +                if (report != REP_QUIET)
965 +                        printf("%s: %ld%s normal vectors differ\n",
966 +                                        progname, nread, num_sfx(nread));
967 +                return(0);
968 +        }
969 +        return(good_RMS());             /* final check of RMS */
970 + }
971 +
972   /* Compare two inputs that are known to be 32-bit floating-point data */
973   static int
974   compare_float()
# Line 550 | Line 976 | compare_float()
976          long    nread = 0;
977          float   f1, f2;
978  
979 +        if (report >= REP_VERBOSE) {
980 +                fputs(progname, stdout);
981 +                fputs(": comparing inputs as 32-bit IEEE floats\n", stdout);
982 +        }
983          while (getbinary(&f1, sizeof(f1), 1, f1in)) {
984                  if (!getbinary(&f2, sizeof(f2), 1, f2in))
985                          goto badeof;
986                  ++nread;
987 +                if (f1swap) swap32((char *)&f1, 1);
988 +                if (f2swap) swap32((char *)&f2, 1);
989                  if (real_check(f1, f2))
990                          continue;
991                  if (report != REP_QUIET)
992 <                        fprintf(stderr, "%s: %ld%s float values differ\n",
993 <                                        progname, nread, nsuffix[nread%10]);
992 >                        printf("%s: %ld%s float values differ\n",
993 >                                        progname, nread, num_sfx(nread));
994                  return(0);
995          }
996          if (!getbinary(&f2, sizeof(f2), 1, f2in))
997                  return(good_RMS());             /* final check of RMS */
998   badeof:
999          if (report != REP_QUIET)
1000 <                fprintf(stderr, "%s: Unexpected end-of-file\n", progname);
1000 >                printf("%s: unexpected end-of-file\n", progname);
1001          return(0);
1002   }
1003  
# Line 576 | Line 1008 | compare_double()
1008          long    nread = 0;
1009          double  f1, f2;
1010  
1011 +        if (report >= REP_VERBOSE) {
1012 +                fputs(progname, stdout);
1013 +                fputs(": comparing inputs as 64-bit IEEE doubles\n", stdout);
1014 +        }
1015          while (getbinary(&f1, sizeof(f1), 1, f1in)) {
1016                  if (!getbinary(&f2, sizeof(f2), 1, f2in))
1017                          goto badeof;
1018                  ++nread;
1019 +                if (f1swap) swap64((char *)&f1, 1);
1020 +                if (f2swap) swap64((char *)&f2, 1);
1021                  if (real_check(f1, f2))
1022                          continue;
1023                  if (report != REP_QUIET)
1024 <                        fprintf(stderr, "%s: %ld%s float values differ\n",
1025 <                                        progname, nread, nsuffix[nread%10]);
1024 >                        printf("%s: %ld%s double values differ\n",
1025 >                                        progname, nread, num_sfx(nread));
1026                  return(0);
1027          }
1028          if (!getbinary(&f2, sizeof(f2), 1, f2in))
1029                  return(good_RMS());             /* final check of RMS */
1030   badeof:
1031          if (report != REP_QUIET)
1032 <                fprintf(stderr, "%s: Unexpected end-of-file\n", progname);
1032 >                printf("%s: unexpected end-of-file\n", progname);
1033          return(0);
1034   }
1035  
# Line 608 | Line 1046 | main(int argc, char *argv[])
1046                  case 'h':                       /* ignore header info. */
1047                          ign_header = !ign_header;
1048                          continue;
1049 +                case 'n':                       /* allow newline escapes */
1050 +                        escape_newlines = !escape_newlines;
1051 +                        continue;
1052 +                case 'c':                       /* ignore comments */
1053 +                        comment_c = argv[a][2];
1054 +                        continue;
1055                  case 's':                       /* silent operation */
1056                          report = REP_QUIET;
1057                          continue;
# Line 638 | Line 1082 | main(int argc, char *argv[])
1082                  }
1083                  break;
1084          }
1085 <        if (a != argc-2)
1085 >        if (a != argc-2)                        /* make sure of two inputs */
1086                  usage();
1087 <        if (!f1name) f1name = argv[a];
644 <        if (!f2name) f2name = argv[a+1];
645 <
646 <        if (!strcmp(f1name, f2name)) {          /* inputs are same? */
1087 >        if (!strcmp(argv[a], argv[a+1])) {      /* inputs are same? */
1088                  if (report >= REP_WARN)
1089 <                        fprintf(stderr, "%s: warning - identical inputs given\n",
1089 >                        printf("%s: warning - identical inputs given\n",
1090                                          progname);
1091                  return(0);
1092          }
1093 +        if (!f1name) f1name = argv[a];
1094 +        if (!f2name) f2name = argv[a+1];
1095                                                  /* open inputs */
1096          SET_FILE_BINARY(stdin);                 /* in case we're using it */
1097          if (!f1in && !(f1in = fopen(f1name, "rb"))) {
1098                  fprintf(stderr, "%s: cannot open for reading\n", f1name);
1099 <                return(1);
1099 >                return(2);
1100          }
1101          if (!strcmp(f2name, "-")) {
1102                  f2in = stdin;
1103                  f2name = stdin_name;
1104          } else if (!(f2in = fopen(f2name, "rb"))) {
1105                  fprintf(stderr, "%s: cannot open for reading\n", f2name);
1106 <                return(1);
1106 >                return(2);
1107          }
1108                                                  /* load headers */
1109          if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0)
1110 <                return(1);
1110 >                return(2);
1111          if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0)
1112 <                return(1);
1112 >                return(2);
1113          if (typ1 != typ2) {
1114                  if (report != REP_QUIET)
1115 <                        fprintf(stderr, "%s: '%s' is %s and '%s' is %s\n",
1115 >                        printf("%s: '%s' format is %s and '%s' is %s\n",
1116                                          progname, f1name, file_type[typ1],
1117                                          f2name, file_type[typ2]);
1118                  return(1);
1119          }
1120          ign_header |= !has_header(typ1);        /* check headers if indicated */
1121 <        if (!ign_header && !headers_match(&hdr1, &hdr2))
1121 >        if (!ign_header && !headers_match())
1122                  return(1);
680        lu_done(&hdr1); lu_done(&hdr2);
1123          if (!ign_header & (report >= REP_WARN)) {
682                if (typ1 == TYP_UNKNOWN)
683                        fprintf(stderr, "%s: warning - unrecognized format, comparing as binary\n",
684                                        progname);
1124                  if (lin1cnt != lin2cnt)
1125 <                        fprintf(stderr, "%s: warning - headers are different lengths\n",
1125 >                        printf("%s: warning - headers are different lengths\n",
1126                                          progname);
1127 +                if (typ1 == TYP_UNKNOWN)
1128 +                        printf("%s: warning - unrecognized format\n",
1129 +                                        progname);
1130          }
1131 <        if (report >= REP_VERBOSE)
1132 <                fprintf(stderr, "%s: input file type is %s\n",
1133 <                                progname, file_type[typ1]);
1134 <
1131 >        if (report >= REP_VERBOSE) {
1132 >                printf("%s: data format is %s\n", progname, file_type[typ1]);
1133 >                if ((typ1 == TYP_FLOAT) | (typ1 == TYP_DOUBLE)) {
1134 >                        if (f1swap)
1135 >                                printf("%s: input '%s' is byte-swapped\n",
1136 >                                                progname, f1name);
1137 >                        if (f2swap)
1138 >                                printf("%s: input '%s' is byte-swapped\n",
1139 >                                                progname, f2name);
1140 >                }
1141 >        }
1142          switch (typ1) {                         /* compare based on type */
1143          case TYP_BINARY:
1144          case TYP_TMESH:
1145          case TYP_OCTREE:
1146          case TYP_RBFMESH:
1147 +        case TYP_ID8:
1148 +        case TYP_ID16:
1149 +        case TYP_ID24:
1150          case TYP_UNKNOWN:
1151                  return( !compare_binary() );
1152          case TYP_TEXT:
# Line 703 | Line 1155 | main(int argc, char *argv[])
1155          case TYP_RGBE:
1156          case TYP_XYZE:
1157                  return( !compare_hdr() );
1158 +        case TYP_SPEC:
1159 +                return( !compare_spec() );
1160 +        case TYP_DEPTH:
1161 +                return( !compare_depth() );
1162 +        case TYP_NORM:
1163 +                return( !compare_norm() );
1164          case TYP_FLOAT:
1165                  return( !compare_float() );
1166          case TYP_DOUBLE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines