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.31 by greg, Fri May 27 17:27:53 2022 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 >                        DEPTH16FMT,
55 >                        NORMAL32FMT,
56 >                        "float",
57 >                        "double",
58                          "BSDF_RBFmesh",
59                          "Radiance_octree",
60                          "Radiance_tmesh",
61 +                        "8-bit_indexed_name",
62 +                        "16-bit_indexed_name",
63 +                        "24-bit_indexed_name",
64                          "BINARY_unknown",
65                          NULL    /* terminator */
66                  };
67 <
68 < enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, TYP_FLOAT,
69 <                TYP_DOUBLE, TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, TYP_BINARY};
67 >                                /* keep consistent with above */
68 > enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE,
69 >                TYP_DEPTH, TYP_NORM, TYP_FLOAT, TYP_DOUBLE,
70 >                TYP_RBFMESH, TYP_OCTREE, TYP_TMESH,
71 >                TYP_ID8, TYP_ID16, TYP_ID24, TYP_BINARY};
72                  
73   #define has_header(t)   (!( 1L<<(t) & (1L<<TYP_TEXT | 1L<<TYP_BINARY) ))
74  
# Line 59 | Line 77 | const char     *hdr_ignkey[] = {
77                          "SOFTWARE",
78                          "CAPDATE",
79                          "GMT",
80 +                        "FRAME",
81                          NULL    /* terminator */
82                  };
83                                  /* header variable settings */
# Line 69 | Line 88 | LUTAB  hdr2 = LU_SINIT(free,free);
88   #define adv_linecnt(htp)        (lin1cnt += (htp == &hdr1), \
89                                          lin2cnt += (htp == &hdr2))
90  
91 + typedef struct {                /* dynamic line buffer */
92 +        char    *str;
93 +        int     len;
94 +        int     siz;
95 + } LINEBUF;
96 +
97 + #define init_line(bp)   ((bp)->str = NULL, (bp)->siz = 0)
98 +                                /* 100 MByte limit on line buffer */
99 + #define MAXBUF          (100L<<20)
100 +
101                                  /* input files */
102   char            *progname = NULL;
103   const char      stdin_name[] = "<stdin>";
104   const char      *f1name=NULL, *f2name=NULL;
105   FILE            *f1in=NULL, *f2in=NULL;
106 + int             f1swap=0, f2swap=0;
107 +
108                                  /* running real differences */
109   double  diff2sum = 0;
110 < int     nsum = 0;
110 > long    nsum = 0;
111  
112   /* Report usage and exit */
113   static void
# Line 84 | Line 115 | usage()
115   {
116          fputs("Usage: ", stderr);
117          fputs(progname, stderr);
118 <        fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] file1 file2\n",
118 >        fputs(" [-h][-c#][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n",
119                          stderr);
120 <        exit(1);
120 >        exit(2);
121   }
122  
123 + /* Read a text line, increasing buffer size as necessary */
124 + static int
125 + read_line(LINEBUF *bp, FILE *fp)
126 + {
127 +        static int      doneWarn = 0;
128 +
129 +        bp->len = 0;
130 +        if (!bp->str) {
131 +                bp->str = (char *)malloc(bp->siz = 512);
132 +                if (!bp->str)
133 +                        goto memerr;
134 +        }
135 +        while (fgets(bp->str + bp->len, bp->siz - bp->len, fp)) {
136 +                bp->len += strlen(bp->str + bp->len);
137 +                if (bp->str[bp->len-1] == '\n') {
138 +                        if (bp->len > 1 && bp->str[bp->len-2] == '\r') {
139 +                                bp->str[--bp->len] = '\0';
140 +                                bp->str[bp->len-1] = '\n';
141 +                        }
142 +                        if (escape_newlines && bp->len > 1 &&
143 +                                        bp->str[bp->len-2] == '\\') {
144 +                                bp->str[--bp->len] = '\0';
145 +                                bp->str[bp->len-1] = ' ';
146 +                                continue;
147 +                        }
148 +                        break;          /* found EOL */
149 +                }
150 +                if (bp->len < bp->siz - 4)
151 +                        continue;       /* at EOF? */
152 +                if (bp->siz >= MAXBUF) {
153 +                        if ((report >= REP_WARN) & !doneWarn) {
154 +                                fprintf(stderr,
155 +                        "%s: warning - input line(s) past %ld MByte limit\n",
156 +                                        progname, MAXBUF>>20);
157 +                                doneWarn++;
158 +                        }
159 +                        break;          /* return MAXBUF partial line */
160 +                }
161 +                if ((bp->siz += bp->siz/2) > MAXBUF)
162 +                        bp->siz = MAXBUF;
163 +                bp->str = (char *)realloc(bp->str, bp->siz);
164 +                if (!bp->str)
165 +                        goto memerr;
166 +        }
167 +        if (comment_c) {                /* elide comment? */
168 +                char    *cp = sskip2(bp->str,0);
169 +                if (*cp == comment_c) {
170 +                        *cp++ = '\n';
171 +                        *cp = '\0';
172 +                        bp->len = cp - bp->str;
173 +                }
174 +        }
175 +        return(bp->len);
176 + memerr:
177 +        fprintf(stderr,
178 +                "%s: out of memory in read_line() allocating %d byte buffer\n",
179 +                        progname, bp->siz);
180 +        exit(2);
181 + }
182 +
183 + /* Free line buffer */
184 + static void
185 + free_line(LINEBUF *bp)
186 + {
187 +        if (bp->str) free(bp->str);
188 +        init_line(bp);
189 + }
190 +
191   /* Get type ID from name (or 0 if not found) */
192   static int
193   xlate_type(const char *nm)
# Line 111 | Line 210 | real_check(double r1, double r2)
210  
211          if (rel_min > 0) {      /* doing relative differences? */
212                  double  av2 = .25*(r1*r1 + 2.*fabs(r1*r2) + r2*r2);
213 <                if (av2 > rel_min*rel_min)
214 <                        diff2 /= av2;
213 >                if (av2 < rel_min*rel_min)
214 >                        av2 = rel_min*rel_min;
215 >                diff2 /= av2;
216          }
217          if (max_lim >= 0 && diff2 > max_lim*max_lim) {
218                  if (report != REP_QUIET)
219 <                        fprintf(stderr,
220 <                        "%s: %sdifference between %.8g and %.8g exceeds epsilon\n",
219 >                        printf(
220 >                        "%s: %sdifference between %.8g and %.8g exceeds epsilon of %.8g\n",
221                                          progname,
222                                          (rel_min > 0) ? "relative " : "",
223 <                                        r1, r2);
223 >                                        r1, r2, max_lim);
224                  return(0);
225          }
226          diff2sum += diff2;
# Line 128 | Line 228 | real_check(double r1, double r2)
228          return(1);
229   }
230  
231 + /* Compare two color values for equivalence */
232 + static int
233 + color_check(COLOR c1, COLOR c2)
234 + {
235 +        int     p;
236 +
237 +        if (!real_check((colval(c1,RED)+colval(c1,GRN)+colval(c1,BLU))*(1./3.),
238 +                        (colval(c2,RED)+colval(c2,GRN)+colval(c2,BLU))*(1./3.)))
239 +                return(0);
240 +
241 +        p = (colval(c1,GRN) > colval(c1,RED)) ? GRN : RED;
242 +        if (colval(c1,BLU) > colval(c1,p)) p = BLU;
243 +        
244 +        return(real_check(colval(c1,p), colval(c2,p)));
245 + }
246 +
247 + /* Compare two normal directions for equivalence */
248 + static int
249 + norm_check(FVECT nv1, FVECT nv2)
250 + {
251 +        double  max2 = nv1[2]*nv2[2];
252 +        int     imax = 2;
253 +        int     i = 2;
254 +                                        /* identify largest component */
255 +        while (i--) {
256 +                double  tm2 = nv1[i]*nv2[i];
257 +                if (tm2 > max2) {
258 +                        imax = i;
259 +                        max2 = tm2;
260 +                }
261 +        }
262 +        i = 3;                          /* compare smaller components */
263 +        while (i--) {
264 +                if (i == imax)
265 +                        continue;
266 +                if (!real_check(nv1[i], nv2[i]))
267 +                        return(0);
268 +        }
269 +        return(1);
270 + }
271 +
272   /* Compare two strings for equivalence */
273   static int
274   equiv_string(char *s1, char *s2)
# Line 138 | Line 279 | equiv_string(char *s1, char *s2)
279                                  /* skip whitespace at beginning */
280          while (isspace(*s1)) s1++;
281          while (isspace(*s2)) s2++;
141        if (!strcmp(s1, s2))    /* quick check */
142                return(1);
282          while (*s1) {           /* check each word */
283 <                int     inquote = *s1;
283 >                int     inquote;
284 >                if (!*s2)       /* unexpected EOL in s2? */
285 >                        return(0);
286 >                inquote = *s1;
287                  if ((inquote != '\'') & (inquote != '"'))
288                          inquote = 0;
289                  if (inquote) {  /* quoted text must match exactly */
# Line 199 | Line 341 | equiv_string(char *s1, char *s2)
341   static int
342   setheadvar(char *val, void *p)
343   {
344 +        char    newval[128];
345          LUTAB   *htp = (LUTAB *)p;
346          LUENT   *tep;
347          char    *key;
348          int     kln, vln;
349          int     n;
350  
351 +        adv_linecnt(htp);       /* side-effect is to count lines */
352          if (!isalpha(*val))     /* key must start line */
353                  return(0);
354 +                                /* check if we need to swap binary data */
355 +        if ((n = isbigendian(val)) >= 0) {
356 +                if (nativebigendian() == n)
357 +                        return(0);
358 +                f1swap += (htp == &hdr1);
359 +                f2swap += (htp == &hdr2);
360 +                return(0);
361 +        }
362          key = val++;
363          while (*val && !isspace(*val) & (*val != '='))
364                  val++;
# Line 220 | Line 372 | setheadvar(char *val, void *p)
372                  val++;
373          if (!*val)              /* nothing set? */
374                  return(0);
223        vln = strlen(val);      /* eliminate space and newline at end */
224        while (--vln > 0 && isspace(val[vln]))
225                ;
226        val[++vln] = '\0';
375                                  /* check if key to ignore */
376          for (n = 0; hdr_ignkey[n]; n++)
377                  if (!strcmp(key, hdr_ignkey[n]))
378                          return(0);
379 +        vln = strlen(val);      /* eliminate space and newline at end */
380 +        while (isspace(val[--vln]))
381 +                ;
382 +        val[++vln] = '\0';
383          if (!(tep = lu_find(htp, key)))
384 <                return(-1);
384 >                return(-1);     /* memory allocation error */
385          if (!tep->key)
386                  tep->key = strcpy(malloc(kln+1), key);
387 <        if (tep->data)
387 >        if (tep->data) {        /* check for special cases */
388 >                if (!strcmp(key, "EXPOSURE")) {
389 >                        sprintf(newval, "%.6e", atof(tep->data)*atof(val));
390 >                        vln = strlen(val = newval);
391 >                }
392                  free(tep->data);
393 +        }
394          tep->data = strcpy(malloc(vln+1), val);
238        adv_linecnt(htp);
395          return(1);
396   }
397  
# Line 246 | Line 402 | match_val(const LUENT *ep1, void *p2)
402          const LUENT     *ep2 = lu_find((LUTAB *)p2, ep1->key);
403          if (!ep2 || !ep2->data) {
404                  if (report != REP_QUIET)
405 <                        fprintf(stderr, "%s: Variable '%s' missing in '%s'\n",
405 >                        printf("%s: variable '%s' missing in '%s'\n",
406                                          progname, ep1->key, f2name);
407                  return(-1);
408          }
409          if (!equiv_string((char *)ep1->data, (char *)ep2->data)) {
410 <                if (report != REP_QUIET)
411 <                        fprintf(stderr, "%s: Header variables '%s' have different values\n",
410 >                if (report != REP_QUIET) {
411 >                        printf("%s: header variable '%s' has different values\n",
412                                          progname, ep1->key);
413 +                        if (report >= REP_VERBOSE) {
414 +                                printf("%s: %s=%s\n", f1name,
415 +                                                ep1->key, (char *)ep1->data);
416 +                                printf("%s: %s=%s\n", f2name,
417 +                                                ep2->key, (char *)ep2->data);
418 +                        }
419 +                }
420                  return(-1);
421          }
422          return(1);              /* good match */
# Line 261 | Line 424 | match_val(const LUENT *ep1, void *p2)
424  
425   /* Compare two sets of header variables */
426   static int
427 < headers_match(LUTAB *hp1, LUTAB *hp2)
427 > headers_match()
428   {
429 <        int     ne = lu_doall(hp1, match_val, hp2);
429 >        int     ne = lu_doall(&hdr1, match_val, &hdr2);
430          if (ne < 0)
431                  return(0);      /* something didn't match! */
432 <        ne = lu_doall(hp2, NULL, NULL) - ne;
433 <        if (ne) {
434 <                if (report != REP_QUIET)
272 <                        fprintf(stderr, "%s: '%s' has %d extra header variable(s)\n",
432 >                                /* non-fatal if second header has extra */
433 >        if (report >= REP_WARN && (ne = lu_doall(&hdr2, NULL, NULL) - ne))
434 >                printf("%s: warning - '%s' has %d extra header setting(s)\n",
435                                          progname, f2name, ne);
274                return(0);
275        }
436          return(1);              /* good match */
437   }
438  
# Line 284 | Line 444 | input_is_binary(FILE *fin)
444          int     c = 0;
445  
446          while ((c = getc(fin)) != EOF) {
447 +                ++n;
448                  if (!c | (c > 127))
449                          break;                  /* non-ascii character */
450 <                if (++n >= 10240)
450 >                if (n >= 10240)
451                          break;                  /* enough to be confident */
452          }
453          if (!n)
# Line 316 | Line 477 | identify_type(const char *name, FILE *fin, LUTAB *htp)
477                  char    sbuf[32];
478                  if (!fgets(sbuf, sizeof(sbuf), fin))
479                          goto badeof;
480 +                adv_linecnt(htp);               /* for #?ID string */
481                  if (report >= REP_WARN && strncmp(sbuf, "RADIANCE", 8)) {
482 <                        fputs(name, stderr);
483 <                        fputs(": warning - unexpected header ID: ", stderr);
484 <                        fputs(sbuf, stderr);
482 >                        fputs(name, stdout);
483 >                        fputs(": warning - unexpected header ID: ", stdout);
484 >                        fputs(sbuf, stdout);
485                  }
324                adv_linecnt(htp);               /* for #?ID string */
486                  if (getheader(fin, setheadvar, htp) < 0) {
487                          fputs(name, stderr);
488 <                        fputs(": Unknown error reading header\n", stderr);
488 >                        fputs(": unknown error reading header\n", stderr);
489                          return(-1);
490                  }
491                  adv_linecnt(htp);               /* for trailing emtpy line */
# Line 338 | Line 499 | identify_type(const char *name, FILE *fin, LUTAB *htp)
499          }
500          if (c)
501                  return(TYP_BINARY);
341        SET_FILE_TEXT(fin);                     /* originally set to binary */
502          return(TYP_TEXT);
503   badeof:
504          if (report != REP_QUIET) {
505 <                fputs(name, stderr);
506 <                fputs(": Unexpected end-of-file\n", stderr);
505 >                fputs(name, stdout);
506 >                fputs(": unexpected end-of-file\n", stdout);
507          }
508          return(-1);
509   }
# Line 356 | Line 516 | good_RMS()
516                  return(1);
517          if (diff2sum/(double)nsum > rms_lim*rms_lim) {
518                  if (report != REP_QUIET)
519 <                        fprintf(stderr,
520 <        "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit\n",
519 >                        printf(
520 >        "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit of %.5g\n",
521                                          progname,
522                                          (rel_min > 0) ? "relative " : "",
523                                          f1name, f2name,
524 <                                        sqrt(diff2sum/(double)nsum));
524 >                                        sqrt(diff2sum/(double)nsum), rms_lim);
525                  return(0);
526          }
527          if (report >= REP_VERBOSE)
528 <                fprintf(stderr,
369 <                        "%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n",
528 >                printf("%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n",
529                                  progname, (rel_min > 0) ? "relative " : "",
530                                  f1name, f2name, sqrt(diff2sum/(double)nsum));
531          return(1);
# Line 376 | Line 535 | good_RMS()
535   static int
536   compare_binary()
537   {
538 +        int     c1=0, c2=0;
539 +
540          if (report >= REP_VERBOSE) {
541 <                fputs(progname, stderr);
542 <                fputs(": comparing inputs as binary\n", stderr);
541 >                fputs(progname, stdout);
542 >                fputs(": comparing inputs as binary\n", stdout);
543          }
544          for ( ; ; ) {                           /* exact byte matching */
545 <                int     c1 = getc(f1in);
546 <                int     c2 = getc(f2in);
545 >                c1 = getc(f1in);
546 >                c2 = getc(f2in);
547                  if (c1 == EOF) {
548                          if (c2 == EOF)
549                                  return(1);      /* success! */
550                          if (report != REP_QUIET) {
551 <                                fputs(f1name, stderr);
552 <                                fputs(": Unexpected end-of-file\n", stderr);
551 >                                fputs(f1name, stdout);
552 >                                fputs(": unexpected end-of-file\n", stdout);
553                          }
554                          return(0);
555                  }
556                  if (c2 == EOF) {
557                          if (report != REP_QUIET) {
558 <                                fputs(f2name, stderr);
559 <                                fputs(": Unexpected end-of-file\n", stderr);
558 >                                fputs(f2name, stdout);
559 >                                fputs(": unexpected end-of-file\n", stdout);
560                          }
561                          return(0);
562                  }
# Line 404 | Line 565 | compare_binary()
565          }
566          if (report == REP_QUIET)
567                  return(0);
568 <        fprintf(stderr, "%s: binary files '%s' and '%s' differ at offset %ld|%ld\n",
568 >        printf("%s: binary files '%s' and '%s' differ at byte offset %ld|%ld\n",
569                          progname, f1name, f2name, ftell(f1in), ftell(f2in));
570 +        if (report >= REP_VERBOSE)
571 +                printf("%s: byte in '%s' is 0x%X, byte in '%s' is 0x%X\n",
572 +                                progname, f1name, c1, f2name, c2);
573          return(0);
574   }
575  
# Line 413 | Line 577 | compare_binary()
577   static int
578   compare_text()
579   {
580 <        char    l1buf[4096], l2buf[4096];
580 >        LINEBUF l1buf, l2buf;
581  
582          if (report >= REP_VERBOSE) {
583 <                fputs(progname, stderr);
584 <                fputs(": comparing inputs as ASCII text\n", stderr);
583 >                fputs(progname, stdout);
584 >                fputs(": comparing inputs as ASCII text", stdout);
585 >                if (escape_newlines)
586 >                        fputs(", allowing escaped newlines", stdout);
587 >                if (comment_c) {
588 >                        fputs(", ignoring comments starting with '", stdout);
589 >                        fputc(comment_c, stdout);
590 >                        fputc('\'', stdout);
591 >                }
592 >                fputc('\n', stdout);
593          }
594 <                                                /* compare a line at a time */
595 <        while (fgets(l1buf, sizeof(l1buf), f1in)) {
594 >        SET_FILE_TEXT(f1in);                    /* originally set to binary */
595 >        SET_FILE_TEXT(f2in);
596 >        init_line(&l1buf); init_line(&l2buf);   /* compare a line at a time */
597 >        while (read_line(&l1buf, f1in)) {
598                  lin1cnt++;
599 <                if (!*sskip2(l1buf,0))
599 >                if (!*sskip2(l1buf.str,0))
600                          continue;               /* ignore empty lines */
601 <                while (fgets(l2buf, sizeof(l2buf), f2in)) {
601 >
602 >                while (read_line(&l2buf, f2in)) {
603                          lin2cnt++;
604 <                        if (*sskip2(l2buf,0))
604 >                        if (*sskip2(l2buf.str,0))
605                                  break;          /* found other non-empty line */
606                  }
607 <                if (feof(f2in)) {
607 >                if (!l2buf.len) {               /* input 2 EOF? */
608                          if (report != REP_QUIET) {
609 <                                fputs(f2name, stderr);
610 <                                fputs(": Unexpected end-of-file\n", stderr);
609 >                                fputs(f2name, stdout);
610 >                                fputs(": unexpected end-of-file\n", stdout);
611                          }
612 +                        free_line(&l1buf); free_line(&l2buf);
613                          return(0);
614                  }
615                                                  /* compare non-empty lines */
616 <                if (!equiv_string(l1buf, l2buf)) {
616 >                if (!equiv_string(l1buf.str, l2buf.str)) {
617                          if (report != REP_QUIET) {
618 <                                fprintf(stderr, "%s: inputs '%s' and '%s' differ at line %d|%d\n",
618 >                                printf("%s: inputs '%s' and '%s' differ at line %d|%d\n",
619                                                  progname, f1name, f2name,
620                                                  lin1cnt, lin2cnt);
621 <                                if (report >= REP_VERBOSE) {
622 <                                        fputs("------------- Mismatch -------------\n", stderr);
623 <                                        fprintf(stderr, "%s(%d):\t%s", f1name,
624 <                                                        lin1cnt, l1buf);
625 <                                        fprintf(stderr, "%s(%d):\t%s", f2name,
626 <                                                        lin2cnt, l2buf);
621 >                                if ( report >= REP_VERBOSE &&
622 >                                                (l1buf.len < 256) &
623 >                                                (l2buf.len < 256) ) {
624 >                                        fputs("------------- Mismatch -------------\n", stdout);
625 >                                        printf("%s@%d:\t%s", f1name,
626 >                                                        lin1cnt, l1buf.str);
627 >                                        printf("%s@%d:\t%s", f2name,
628 >                                                        lin2cnt, l2buf.str);
629                                  }
630                          }
631 +                        free_line(&l1buf); free_line(&l2buf);
632                          return(0);
633                  }
634          }
635 <                                                /* check for EOF on input 2 */
636 <        while (fgets(l2buf, sizeof(l2buf), f2in)) {
637 <                if (!*sskip2(l2buf,0))
635 >        free_line(&l1buf);                      /* check for EOF on input 2 */
636 >        while (read_line(&l2buf, f2in)) {
637 >                if (!*sskip2(l2buf.str,0))
638                          continue;
639                  if (report != REP_QUIET) {
640 <                        fputs(f1name, stderr);
641 <                        fputs(": Unexpected end-of-file\n", stderr);
640 >                        fputs(f1name, stdout);
641 >                        fputs(": unexpected end-of-file\n", stdout);
642                  }
643 +                free_line(&l2buf);
644                  return(0);
645          }
646 +        free_line(&l2buf);
647          return(good_RMS());                     /* final check for reals */
648   }
649  
650 + /* Check image/map resolutions */
651 + static int
652 + check_resolu(const char *class, RESOLU *r1p, RESOLU *r2p)
653 + {
654 +        if (r1p->rt != r2p->rt) {
655 +                if (report != REP_QUIET)
656 +                        printf(
657 +                        "%s: %ss '%s' and '%s' have different pixel ordering\n",
658 +                                        progname, class, f1name, f2name);
659 +                return(0);
660 +        }
661 +        if ((r1p->xr != r2p->xr) | (r1p->yr != r2p->yr)) {
662 +                if (report != REP_QUIET)
663 +                        printf(
664 +                        "%s: %ss '%s' and '%s' are different sizes\n",
665 +                                        progname, class, f1name, f2name);
666 +                return(0);
667 +        }
668 +        return(1);
669 + }
670 +
671   /* Compare two inputs that are known to be RGBE or XYZE images */
672   static int
673   compare_hdr()
# Line 474 | Line 676 | compare_hdr()
676          COLOR   *scan1, *scan2;
677          int     x, y;
678  
679 +        if (report >= REP_VERBOSE) {
680 +                fputs(progname, stdout);
681 +                fputs(": comparing inputs as HDR images\n", stdout);
682 +        }
683          fgetsresolu(&rs1, f1in);
684          fgetsresolu(&rs2, f2in);
685 <        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);
685 >        if (!check_resolu("HDR image", &rs1, &rs2))
686                  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        }
687          scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR));
688          scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR));
689          if (!scan1 | !scan2) {
# Line 500 | Line 694 | compare_hdr()
694                  if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) |
695                                  (freadscan(scan2, scanlen(&rs2), f2in) < 0)) {
696                          if (report != REP_QUIET)
697 <                                fprintf(stderr, "%s: Unexpected end-of-file\n",
697 >                                printf("%s: unexpected end-of-file\n",
698                                                  progname);
699                          free(scan1);
700                          free(scan2);
701                          return(0);
702                  }
703 <                for (x = scanlen(&rs1); x--; ) {
704 <                        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)))
703 >                for (x = 0; x < scanlen(&rs1); x++) {
704 >                        if (color_check(scan1[x], scan2[x]))
705                                  continue;
706                          if (report != REP_QUIET) {
707 <                                fprintf(stderr,
707 >                                printf(
708                                  "%s: pixels at scanline %d offset %d differ\n",
709                                          progname, y, x);
710                                  if (report >= REP_VERBOSE) {
711 <                                        fprintf(stderr, "%s: (R,G,B)=(%g,%g,%g)\n",
711 >                                        printf("%s: (R,G,B)=(%g,%g,%g)\n",
712                                                  f1name, colval(scan1[x],RED),
713                                                  colval(scan1[x],GRN),
714                                                  colval(scan1[x],BLU));
715 <                                        fprintf(stderr, "%s: (R,G,B)=(%g,%g,%g)\n",
715 >                                        printf("%s: (R,G,B)=(%g,%g,%g)\n",
716                                                  f2name, colval(scan2[x],RED),
717                                                  colval(scan2[x],GRN),
718                                                  colval(scan2[x],BLU));
# Line 539 | Line 728 | compare_hdr()
728          return(good_RMS());                     /* final check of RMS */
729   }
730  
731 < const char      nsuffix[10][3] = {              /* 1st, 2nd, 3rd, etc. */
732 <                        "th","st","nd","rd","th","th","th","th","th","th"
733 <                };
731 > /* Set reference depth based on header variable */
732 > static int
733 > set_refdepth(DEPTHCODEC *dcp, LUTAB *htp)
734 > {
735 >        static char     depthvar[] = DEPTHSTR;
736 >        const char      *drval;
737  
738 +        depthvar[LDEPTHSTR-1] = '\0';
739 +        drval = (const char *)lu_find(htp, depthvar)->data;
740 +        if (!drval)
741 +                return(0);
742 +        dcp->refdepth = atof(drval);
743 +        if (dcp->refdepth <= 0) {
744 +                if (report != REP_QUIET) {
745 +                        fputs(dcp->inpname, stderr);
746 +                        fputs(": bad reference depth '", stderr);
747 +                        fputs(drval, stderr);
748 +                        fputs("'\n", stderr);
749 +                }
750 +                return(-1);
751 +        }
752 +        return(1);
753 + }
754 +
755 + /* Compare two encoded depth maps */
756 + static int
757 + compare_depth()
758 + {
759 +        long            nread = 0;
760 +        DEPTHCODEC      dc1, dc2;
761 +
762 +        if (report >= REP_VERBOSE) {
763 +                fputs(progname, stdout);
764 +                fputs(": comparing inputs as depth maps\n", stdout);
765 +        }
766 +        set_dc_defaults(&dc1);
767 +        dc1.hdrflags = HF_RESIN;
768 +        dc1.finp = f1in;
769 +        dc1.inpname = f1name;
770 +        set_dc_defaults(&dc2);
771 +        dc2.hdrflags = HF_RESIN;
772 +        dc2.finp = f2in;
773 +        dc2.inpname = f2name;
774 +        if (report != REP_QUIET) {
775 +                dc1.hdrflags |= HF_STDERR;
776 +                dc2.hdrflags |= HF_STDERR;
777 +        }
778 +        if (!process_dc_header(&dc1, 0, NULL))
779 +                return(0);
780 +        if (!process_dc_header(&dc2, 0, NULL))
781 +                return(0);
782 +        if (!check_resolu("Depth map", &dc1.res, &dc2.res))
783 +                return(0);
784 +        if (set_refdepth(&dc1, &hdr1) < 0)
785 +                return(0);
786 +        if (set_refdepth(&dc2, &hdr2) < 0)
787 +                return(0);
788 +        while (nread < dc1.res.xr*dc1.res.yr) {
789 +                double  d1 = decode_depth_next(&dc1);
790 +                double  d2 = decode_depth_next(&dc2);
791 +                if ((d1 < 0) | (d2 < 0)) {
792 +                        if (report != REP_QUIET)
793 +                                printf("%s: unexpected end-of-file\n",
794 +                                                progname);
795 +                        return(0);
796 +                }
797 +                ++nread;
798 +                if (real_check(d1, d2))
799 +                        continue;
800 +                if (report != REP_QUIET)
801 +                        printf("%s: %ld%s depth values differ\n",
802 +                                        progname, nread, num_sfx(nread));
803 +                return(0);
804 +        }
805 +        return(good_RMS());             /* final check of RMS */
806 + }
807 +
808 + /* Compare two encoded normal maps */
809 + static int
810 + compare_norm()
811 + {
812 +        long            nread = 0;
813 +        NORMCODEC       nc1, nc2;
814 +
815 +        if (report >= REP_VERBOSE) {
816 +                fputs(progname, stdout);
817 +                fputs(": comparing inputs as normal maps\n", stdout);
818 +        }
819 +        set_nc_defaults(&nc1);
820 +        nc1.hdrflags = HF_RESIN;
821 +        nc1.finp = f1in;
822 +        nc1.inpname = f1name;
823 +        set_nc_defaults(&nc2);
824 +        nc2.hdrflags = HF_RESIN;
825 +        nc2.finp = f2in;
826 +        nc2.inpname = f2name;
827 +        if (report != REP_QUIET) {
828 +                nc1.hdrflags |= HF_STDERR;
829 +                nc2.hdrflags |= HF_STDERR;
830 +        }
831 +        if (!process_nc_header(&nc1, 0, NULL))
832 +                return(0);
833 +        if (!process_nc_header(&nc2, 0, NULL))
834 +                return(0);
835 +        if (!check_resolu("Normal map", &nc1.res, &nc2.res))
836 +                return(0);
837 +        while (nread < nc1.res.xr*nc1.res.yr) {
838 +                FVECT   nv1, nv2;
839 +                int     rv1 = decode_normal_next(nv1, &nc1);
840 +                int     rv2 = decode_normal_next(nv2, &nc2);
841 +                if ((rv1 < 0) | (rv2 < 0)) {
842 +                        if (report != REP_QUIET)
843 +                                printf("%s: unexpected end-of-file\n",
844 +                                                progname);
845 +                        return(0);
846 +                }
847 +                ++nread;
848 +                if (rv1 == rv2 && (!rv1 || norm_check(nv1, nv2)))
849 +                        continue;
850 +                if (report != REP_QUIET)
851 +                        printf("%s: %ld%s normal vectors differ\n",
852 +                                        progname, nread, num_sfx(nread));
853 +                return(0);
854 +        }
855 +        return(good_RMS());             /* final check of RMS */
856 + }
857 +
858   /* Compare two inputs that are known to be 32-bit floating-point data */
859   static int
860   compare_float()
# Line 550 | Line 862 | compare_float()
862          long    nread = 0;
863          float   f1, f2;
864  
865 +        if (report >= REP_VERBOSE) {
866 +                fputs(progname, stdout);
867 +                fputs(": comparing inputs as 32-bit IEEE floats\n", stdout);
868 +        }
869          while (getbinary(&f1, sizeof(f1), 1, f1in)) {
870                  if (!getbinary(&f2, sizeof(f2), 1, f2in))
871                          goto badeof;
872                  ++nread;
873 +                if (f1swap) swap32((char *)&f1, 1);
874 +                if (f2swap) swap32((char *)&f2, 1);
875                  if (real_check(f1, f2))
876                          continue;
877                  if (report != REP_QUIET)
878 <                        fprintf(stderr, "%s: %ld%s float values differ\n",
879 <                                        progname, nread, nsuffix[nread%10]);
878 >                        printf("%s: %ld%s float values differ\n",
879 >                                        progname, nread, num_sfx(nread));
880                  return(0);
881          }
882          if (!getbinary(&f2, sizeof(f2), 1, f2in))
883                  return(good_RMS());             /* final check of RMS */
884   badeof:
885          if (report != REP_QUIET)
886 <                fprintf(stderr, "%s: Unexpected end-of-file\n", progname);
886 >                printf("%s: unexpected end-of-file\n", progname);
887          return(0);
888   }
889  
# Line 576 | Line 894 | compare_double()
894          long    nread = 0;
895          double  f1, f2;
896  
897 +        if (report >= REP_VERBOSE) {
898 +                fputs(progname, stdout);
899 +                fputs(": comparing inputs as 64-bit IEEE doubles\n", stdout);
900 +        }
901          while (getbinary(&f1, sizeof(f1), 1, f1in)) {
902                  if (!getbinary(&f2, sizeof(f2), 1, f2in))
903                          goto badeof;
904                  ++nread;
905 +                if (f1swap) swap64((char *)&f1, 1);
906 +                if (f2swap) swap64((char *)&f2, 1);
907                  if (real_check(f1, f2))
908                          continue;
909                  if (report != REP_QUIET)
910 <                        fprintf(stderr, "%s: %ld%s float values differ\n",
911 <                                        progname, nread, nsuffix[nread%10]);
910 >                        printf("%s: %ld%s double values differ\n",
911 >                                        progname, nread, num_sfx(nread));
912                  return(0);
913          }
914          if (!getbinary(&f2, sizeof(f2), 1, f2in))
915                  return(good_RMS());             /* final check of RMS */
916   badeof:
917          if (report != REP_QUIET)
918 <                fprintf(stderr, "%s: Unexpected end-of-file\n", progname);
918 >                printf("%s: unexpected end-of-file\n", progname);
919          return(0);
920   }
921  
# Line 608 | Line 932 | main(int argc, char *argv[])
932                  case 'h':                       /* ignore header info. */
933                          ign_header = !ign_header;
934                          continue;
935 +                case 'n':                       /* allow newline escapes */
936 +                        escape_newlines = !escape_newlines;
937 +                        continue;
938 +                case 'c':                       /* ignore comments */
939 +                        comment_c = argv[a][2];
940 +                        continue;
941                  case 's':                       /* silent operation */
942                          report = REP_QUIET;
943                          continue;
# Line 638 | Line 968 | main(int argc, char *argv[])
968                  }
969                  break;
970          }
971 <        if (a != argc-2)
971 >        if (a != argc-2)                        /* make sure of two inputs */
972                  usage();
973 <        if (!f1name) f1name = argv[a];
644 <        if (!f2name) f2name = argv[a+1];
645 <
646 <        if (!strcmp(f1name, f2name)) {          /* inputs are same? */
973 >        if (!strcmp(argv[a], argv[a+1])) {      /* inputs are same? */
974                  if (report >= REP_WARN)
975 <                        fprintf(stderr, "%s: warning - identical inputs given\n",
975 >                        printf("%s: warning - identical inputs given\n",
976                                          progname);
977                  return(0);
978          }
979 +        if (!f1name) f1name = argv[a];
980 +        if (!f2name) f2name = argv[a+1];
981                                                  /* open inputs */
982          SET_FILE_BINARY(stdin);                 /* in case we're using it */
983          if (!f1in && !(f1in = fopen(f1name, "rb"))) {
984                  fprintf(stderr, "%s: cannot open for reading\n", f1name);
985 <                return(1);
985 >                return(2);
986          }
987          if (!strcmp(f2name, "-")) {
988                  f2in = stdin;
989                  f2name = stdin_name;
990          } else if (!(f2in = fopen(f2name, "rb"))) {
991                  fprintf(stderr, "%s: cannot open for reading\n", f2name);
992 <                return(1);
992 >                return(2);
993          }
994                                                  /* load headers */
995          if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0)
996 <                return(1);
996 >                return(2);
997          if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0)
998 <                return(1);
998 >                return(2);
999          if (typ1 != typ2) {
1000                  if (report != REP_QUIET)
1001 <                        fprintf(stderr, "%s: '%s' is %s and '%s' is %s\n",
1001 >                        printf("%s: '%s' format is %s and '%s' is %s\n",
1002                                          progname, f1name, file_type[typ1],
1003                                          f2name, file_type[typ2]);
1004                  return(1);
1005          }
1006          ign_header |= !has_header(typ1);        /* check headers if indicated */
1007 <        if (!ign_header && !headers_match(&hdr1, &hdr2))
1007 >        if (!ign_header && !headers_match())
1008                  return(1);
680        lu_done(&hdr1); lu_done(&hdr2);
1009          if (!ign_header & (report >= REP_WARN)) {
682                if (typ1 == TYP_UNKNOWN)
683                        fprintf(stderr, "%s: warning - unrecognized format, comparing as binary\n",
684                                        progname);
1010                  if (lin1cnt != lin2cnt)
1011 <                        fprintf(stderr, "%s: warning - headers are different lengths\n",
1011 >                        printf("%s: warning - headers are different lengths\n",
1012                                          progname);
1013 +                if (typ1 == TYP_UNKNOWN)
1014 +                        printf("%s: warning - unrecognized format\n",
1015 +                                        progname);
1016          }
1017 <        if (report >= REP_VERBOSE)
1018 <                fprintf(stderr, "%s: input file type is %s\n",
1019 <                                progname, file_type[typ1]);
1020 <
1017 >        if (report >= REP_VERBOSE) {
1018 >                printf("%s: data format is %s\n", progname, file_type[typ1]);
1019 >                if ((typ1 == TYP_FLOAT) | (typ1 == TYP_DOUBLE)) {
1020 >                        if (f1swap)
1021 >                                printf("%s: input '%s' is byte-swapped\n",
1022 >                                                progname, f1name);
1023 >                        if (f2swap)
1024 >                                printf("%s: input '%s' is byte-swapped\n",
1025 >                                                progname, f2name);
1026 >                }
1027 >        }
1028          switch (typ1) {                         /* compare based on type */
1029          case TYP_BINARY:
1030          case TYP_TMESH:
1031          case TYP_OCTREE:
1032          case TYP_RBFMESH:
1033 +        case TYP_ID8:
1034 +        case TYP_ID16:
1035 +        case TYP_ID24:
1036          case TYP_UNKNOWN:
1037                  return( !compare_binary() );
1038          case TYP_TEXT:
# Line 703 | Line 1041 | main(int argc, char *argv[])
1041          case TYP_RGBE:
1042          case TYP_XYZE:
1043                  return( !compare_hdr() );
1044 +        case TYP_DEPTH:
1045 +                return( !compare_depth() );
1046 +        case TYP_NORM:
1047 +                return( !compare_norm() );
1048          case TYP_FLOAT:
1049                  return( !compare_float() );
1050          case TYP_DOUBLE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines