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.7 by greg, Tue Oct 16 00:20:52 2018 UTC

# Line 33 | Line 33 | double max_lim = 0.25;         /* difference limit if non-neg
33  
34   int     lin1cnt=0, lin2cnt=0;   /* file line position */
35  
36 + const char      nsuffix[10][3] = {              /* 1st, 2nd, 3rd, etc. */
37 +                        "th","st","nd","rd","th","th","th","th","th","th"
38 +                };
39 + #define num_sfx(n)      nsuffix[(n)%10]
40 +
41                                  /* file types */
42   const char      *file_type[] = {
43                          "Unrecognized",
# Line 40 | Line 45 | const char     *file_type[] = {
45                          "ascii",
46                          COLRFMT,
47                          CIEFMT,
48 <                        "BINARY_float",
49 <                        "BINARY_double",
48 >                        "float",
49 >                        "double",
50                          "BSDF_RBFmesh",
51                          "Radiance_octree",
52                          "Radiance_tmesh",
53                          "BINARY_unknown",
54                          NULL    /* terminator */
55                  };
56 <
56 >                                /* keep consistent with above */
57   enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, TYP_FLOAT,
58                  TYP_DOUBLE, TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, TYP_BINARY};
59                  
# Line 74 | Line 79 | char           *progname = NULL;
79   const char      stdin_name[] = "<stdin>";
80   const char      *f1name=NULL, *f2name=NULL;
81   FILE            *f1in=NULL, *f2in=NULL;
82 +
83                                  /* running real differences */
84   double  diff2sum = 0;
85   int     nsum = 0;
# Line 84 | Line 90 | usage()
90   {
91          fputs("Usage: ", stderr);
92          fputs(progname, stderr);
93 <        fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] file1 file2\n",
93 >        fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n",
94                          stderr);
95          exit(1);
96   }
# Line 116 | Line 122 | real_check(double r1, double r2)
122          }
123          if (max_lim >= 0 && diff2 > max_lim*max_lim) {
124                  if (report != REP_QUIET)
125 <                        fprintf(stderr,
126 <                        "%s: %sdifference between %.8g and %.8g exceeds epsilon\n",
125 >                        printf(
126 >                        "%s: %sdifference between %.8g and %.8g exceeds epsilon of %.8g\n",
127                                          progname,
128                                          (rel_min > 0) ? "relative " : "",
129 <                                        r1, r2);
129 >                                        r1, r2, max_lim);
130                  return(0);
131          }
132          diff2sum += diff2;
# Line 138 | Line 144 | equiv_string(char *s1, char *s2)
144                                  /* skip whitespace at beginning */
145          while (isspace(*s1)) s1++;
146          while (isspace(*s2)) s2++;
141        if (!strcmp(s1, s2))    /* quick check */
142                return(1);
147          while (*s1) {           /* check each word */
148 <                int     inquote = *s1;
148 >                int     inquote;
149 >                if (!*s2)       /* unexpected EOL in s2? */
150 >                        return(0);
151 >                inquote = *s1;
152                  if ((inquote != '\'') & (inquote != '"'))
153                          inquote = 0;
154                  if (inquote) {  /* quoted text must match exactly */
# Line 205 | Line 212 | setheadvar(char *val, void *p)
212          int     kln, vln;
213          int     n;
214  
215 +        adv_linecnt(htp);       /* side-effect is to count lines */
216          if (!isalpha(*val))     /* key must start line */
217                  return(0);
218          key = val++;
# Line 220 | Line 228 | setheadvar(char *val, void *p)
228                  val++;
229          if (!*val)              /* nothing set? */
230                  return(0);
223        vln = strlen(val);      /* eliminate space and newline at end */
224        while (--vln > 0 && isspace(val[vln]))
225                ;
226        val[++vln] = '\0';
231                                  /* check if key to ignore */
232          for (n = 0; hdr_ignkey[n]; n++)
233                  if (!strcmp(key, hdr_ignkey[n]))
234                          return(0);
235 +        vln = strlen(val);      /* eliminate space and newline at end */
236 +        while (isspace(val[--vln]))
237 +                ;
238 +        val[++vln] = '\0';
239          if (!(tep = lu_find(htp, key)))
240 <                return(-1);
240 >                return(-1);     /* memory allocation error */
241          if (!tep->key)
242                  tep->key = strcpy(malloc(kln+1), key);
243          if (tep->data)
244                  free(tep->data);
245          tep->data = strcpy(malloc(vln+1), val);
238        adv_linecnt(htp);
246          return(1);
247   }
248  
# Line 246 | Line 253 | match_val(const LUENT *ep1, void *p2)
253          const LUENT     *ep2 = lu_find((LUTAB *)p2, ep1->key);
254          if (!ep2 || !ep2->data) {
255                  if (report != REP_QUIET)
256 <                        fprintf(stderr, "%s: Variable '%s' missing in '%s'\n",
256 >                        printf("%s: variable '%s' missing in '%s'\n",
257                                          progname, ep1->key, f2name);
258                  return(-1);
259          }
260          if (!equiv_string((char *)ep1->data, (char *)ep2->data)) {
261 <                if (report != REP_QUIET)
262 <                        fprintf(stderr, "%s: Header variables '%s' have different values\n",
261 >                if (report != REP_QUIET) {
262 >                        printf("%s: header variable '%s' has different values\n",
263                                          progname, ep1->key);
264 +                        if (report >= REP_VERBOSE) {
265 +                                printf("%s: %s=%s\n", f1name,
266 +                                                ep1->key, (char *)ep1->data);
267 +                                printf("%s: %s=%s\n", f2name,
268 +                                                ep2->key, (char *)ep2->data);
269 +                        }
270 +                }
271                  return(-1);
272          }
273          return(1);              /* good match */
# Line 266 | Line 280 | headers_match(LUTAB *hp1, LUTAB *hp2)
280          int     ne = lu_doall(hp1, match_val, hp2);
281          if (ne < 0)
282                  return(0);      /* something didn't match! */
283 <        ne = lu_doall(hp2, NULL, NULL) - ne;
284 <        if (ne) {
285 <                if (report != REP_QUIET)
272 <                        fprintf(stderr, "%s: '%s' has %d extra header variable(s)\n",
283 >                                /* non-fatal if second header has extra */
284 >        if (report >= REP_WARN && (ne = lu_doall(hp2, NULL, NULL) - ne))
285 >                printf("%s: warning - '%s' has %d extra header setting(s)\n",
286                                          progname, f2name, ne);
274                return(0);
275        }
287          return(1);              /* good match */
288   }
289  
# Line 284 | Line 295 | input_is_binary(FILE *fin)
295          int     c = 0;
296  
297          while ((c = getc(fin)) != EOF) {
298 +                ++n;
299                  if (!c | (c > 127))
300                          break;                  /* non-ascii character */
301 <                if (++n >= 10240)
301 >                if (n >= 10240)
302                          break;                  /* enough to be confident */
303          }
304          if (!n)
# Line 316 | Line 328 | identify_type(const char *name, FILE *fin, LUTAB *htp)
328                  char    sbuf[32];
329                  if (!fgets(sbuf, sizeof(sbuf), fin))
330                          goto badeof;
331 +                adv_linecnt(htp);               /* for #?ID string */
332                  if (report >= REP_WARN && strncmp(sbuf, "RADIANCE", 8)) {
333 <                        fputs(name, stderr);
334 <                        fputs(": warning - unexpected header ID: ", stderr);
335 <                        fputs(sbuf, stderr);
333 >                        fputs(name, stdout);
334 >                        fputs(": warning - unexpected header ID: ", stdout);
335 >                        fputs(sbuf, stdout);
336                  }
324                adv_linecnt(htp);               /* for #?ID string */
337                  if (getheader(fin, setheadvar, htp) < 0) {
338                          fputs(name, stderr);
339 <                        fputs(": Unknown error reading header\n", stderr);
339 >                        fputs(": unknown error reading header\n", stderr);
340                          return(-1);
341                  }
342                  adv_linecnt(htp);               /* for trailing emtpy line */
# Line 342 | Line 354 | identify_type(const char *name, FILE *fin, LUTAB *htp)
354          return(TYP_TEXT);
355   badeof:
356          if (report != REP_QUIET) {
357 <                fputs(name, stderr);
358 <                fputs(": Unexpected end-of-file\n", stderr);
357 >                fputs(name, stdout);
358 >                fputs(": unexpected end-of-file\n", stdout);
359          }
360          return(-1);
361   }
# Line 356 | Line 368 | good_RMS()
368                  return(1);
369          if (diff2sum/(double)nsum > rms_lim*rms_lim) {
370                  if (report != REP_QUIET)
371 <                        fprintf(stderr,
372 <        "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit\n",
371 >                        printf(
372 >        "%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit of %.5g\n",
373                                          progname,
374                                          (rel_min > 0) ? "relative " : "",
375                                          f1name, f2name,
376 <                                        sqrt(diff2sum/(double)nsum));
376 >                                        sqrt(diff2sum/(double)nsum), rms_lim);
377                  return(0);
378          }
379          if (report >= REP_VERBOSE)
380 <                fprintf(stderr,
369 <                        "%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n",
380 >                printf("%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n",
381                                  progname, (rel_min > 0) ? "relative " : "",
382                                  f1name, f2name, sqrt(diff2sum/(double)nsum));
383          return(1);
# Line 376 | Line 387 | good_RMS()
387   static int
388   compare_binary()
389   {
390 +        int     c1=0, c2=0;
391 +
392          if (report >= REP_VERBOSE) {
393 <                fputs(progname, stderr);
394 <                fputs(": comparing inputs as binary\n", stderr);
393 >                fputs(progname, stdout);
394 >                fputs(": comparing inputs as binary\n", stdout);
395          }
396          for ( ; ; ) {                           /* exact byte matching */
397 <                int     c1 = getc(f1in);
398 <                int     c2 = getc(f2in);
397 >                c1 = getc(f1in);
398 >                c2 = getc(f2in);
399                  if (c1 == EOF) {
400                          if (c2 == EOF)
401                                  return(1);      /* success! */
402                          if (report != REP_QUIET) {
403 <                                fputs(f1name, stderr);
404 <                                fputs(": Unexpected end-of-file\n", stderr);
403 >                                fputs(f1name, stdout);
404 >                                fputs(": unexpected end-of-file\n", stdout);
405                          }
406                          return(0);
407                  }
408                  if (c2 == EOF) {
409                          if (report != REP_QUIET) {
410 <                                fputs(f2name, stderr);
411 <                                fputs(": Unexpected end-of-file\n", stderr);
410 >                                fputs(f2name, stdout);
411 >                                fputs(": unexpected end-of-file\n", stdout);
412                          }
413                          return(0);
414                  }
# Line 404 | Line 417 | compare_binary()
417          }
418          if (report == REP_QUIET)
419                  return(0);
420 <        fprintf(stderr, "%s: binary files '%s' and '%s' differ at offset %ld|%ld\n",
420 >        printf("%s: binary files '%s' and '%s' differ at byte offset %ld|%ld\n",
421                          progname, f1name, f2name, ftell(f1in), ftell(f2in));
422 +        if (report >= REP_VERBOSE)
423 +                printf("%s: byte in '%s' is 0x%X, byte in '%s' is 0x%X\n",
424 +                                progname, f1name, c1, f2name, c2);
425          return(0);
426   }
427  
# Line 416 | Line 432 | compare_text()
432          char    l1buf[4096], l2buf[4096];
433  
434          if (report >= REP_VERBOSE) {
435 <                fputs(progname, stderr);
436 <                fputs(": comparing inputs as ASCII text\n", stderr);
435 >                fputs(progname, stdout);
436 >                fputs(": comparing inputs as ASCII text\n", stdout);
437          }
438                                                  /* compare a line at a time */
439          while (fgets(l1buf, sizeof(l1buf), f1in)) {
# Line 431 | Line 447 | compare_text()
447                  }
448                  if (feof(f2in)) {
449                          if (report != REP_QUIET) {
450 <                                fputs(f2name, stderr);
451 <                                fputs(": Unexpected end-of-file\n", stderr);
450 >                                fputs(f2name, stdout);
451 >                                fputs(": unexpected end-of-file\n", stdout);
452                          }
453                          return(0);
454                  }
455                                                  /* compare non-empty lines */
456                  if (!equiv_string(l1buf, l2buf)) {
457                          if (report != REP_QUIET) {
458 <                                fprintf(stderr, "%s: inputs '%s' and '%s' differ at line %d|%d\n",
458 >                                printf("%s: inputs '%s' and '%s' differ at line %d|%d\n",
459                                                  progname, f1name, f2name,
460                                                  lin1cnt, lin2cnt);
461                                  if (report >= REP_VERBOSE) {
462 <                                        fputs("------------- Mismatch -------------\n", stderr);
463 <                                        fprintf(stderr, "%s(%d):\t%s", f1name,
462 >                                        fputs("------------- Mismatch -------------\n", stdout);
463 >                                        printf("%s@%d:\t%s", f1name,
464                                                          lin1cnt, l1buf);
465 <                                        fprintf(stderr, "%s(%d):\t%s", f2name,
465 >                                        printf("%s@%d:\t%s", f2name,
466                                                          lin2cnt, l2buf);
467                                  }
468                          }
# Line 458 | Line 474 | compare_text()
474                  if (!*sskip2(l2buf,0))
475                          continue;
476                  if (report != REP_QUIET) {
477 <                        fputs(f1name, stderr);
478 <                        fputs(": Unexpected end-of-file\n", stderr);
477 >                        fputs(f1name, stdout);
478 >                        fputs(": unexpected end-of-file\n", stdout);
479                  }
480                  return(0);
481          }
# Line 474 | Line 490 | compare_hdr()
490          COLOR   *scan1, *scan2;
491          int     x, y;
492  
493 +        if (report >= REP_VERBOSE) {
494 +                fputs(progname, stdout);
495 +                fputs(": comparing inputs as HDR images\n", stdout);
496 +        }
497          fgetsresolu(&rs1, f1in);
498          fgetsresolu(&rs2, f2in);
499          if (rs1.rt != rs2.rt) {
500                  if (report != REP_QUIET)
501 <                        fprintf(stderr,
501 >                        printf(
502                          "%s: Images '%s' and '%s' have different pixel ordering\n",
503                                          progname, f1name, f2name);
504                  return(0);
505          }
506          if ((rs1.xr != rs2.xr) | (rs1.yr != rs2.yr)) {
507                  if (report != REP_QUIET)
508 <                        fprintf(stderr,
508 >                        printf(
509                          "%s: Images '%s' and '%s' are different sizes\n",
510                                          progname, f1name, f2name);
511                  return(0);
# Line 500 | Line 520 | compare_hdr()
520                  if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) |
521                                  (freadscan(scan2, scanlen(&rs2), f2in) < 0)) {
522                          if (report != REP_QUIET)
523 <                                fprintf(stderr, "%s: Unexpected end-of-file\n",
523 >                                printf("%s: unexpected end-of-file\n",
524                                                  progname);
525                          free(scan1);
526                          free(scan2);
527                          return(0);
528                  }
529 <                for (x = scanlen(&rs1); x--; ) {
529 >                for (x = 0; x < scanlen(&rs1); x++) {
530                          if (real_check(colval(scan1[x],RED),
531                                                  colval(scan2[x],RED)) &
532                                          real_check(colval(scan1[x],GRN),
# Line 515 | Line 535 | compare_hdr()
535                                                  colval(scan2[x],BLU)))
536                                  continue;
537                          if (report != REP_QUIET) {
538 <                                fprintf(stderr,
538 >                                printf(
539                                  "%s: pixels at scanline %d offset %d differ\n",
540                                          progname, y, x);
541                                  if (report >= REP_VERBOSE) {
542 <                                        fprintf(stderr, "%s: (R,G,B)=(%g,%g,%g)\n",
542 >                                        printf("%s: (R,G,B)=(%g,%g,%g)\n",
543                                                  f1name, colval(scan1[x],RED),
544                                                  colval(scan1[x],GRN),
545                                                  colval(scan1[x],BLU));
546 <                                        fprintf(stderr, "%s: (R,G,B)=(%g,%g,%g)\n",
546 >                                        printf("%s: (R,G,B)=(%g,%g,%g)\n",
547                                                  f2name, colval(scan2[x],RED),
548                                                  colval(scan2[x],GRN),
549                                                  colval(scan2[x],BLU));
# Line 539 | Line 559 | compare_hdr()
559          return(good_RMS());                     /* final check of RMS */
560   }
561  
542 const char      nsuffix[10][3] = {              /* 1st, 2nd, 3rd, etc. */
543                        "th","st","nd","rd","th","th","th","th","th","th"
544                };
545
562   /* Compare two inputs that are known to be 32-bit floating-point data */
563   static int
564   compare_float()
# Line 550 | Line 566 | compare_float()
566          long    nread = 0;
567          float   f1, f2;
568  
569 +        if (report >= REP_VERBOSE) {
570 +                fputs(progname, stdout);
571 +                fputs(": comparing inputs as 32-bit IEEE floats\n", stdout);
572 +        }
573          while (getbinary(&f1, sizeof(f1), 1, f1in)) {
574                  if (!getbinary(&f2, sizeof(f2), 1, f2in))
575                          goto badeof;
# Line 557 | Line 577 | compare_float()
577                  if (real_check(f1, f2))
578                          continue;
579                  if (report != REP_QUIET)
580 <                        fprintf(stderr, "%s: %ld%s float values differ\n",
581 <                                        progname, nread, nsuffix[nread%10]);
580 >                        printf("%s: %ld%s float values differ\n",
581 >                                        progname, nread, num_sfx(nread));
582                  return(0);
583          }
584          if (!getbinary(&f2, sizeof(f2), 1, f2in))
585                  return(good_RMS());             /* final check of RMS */
586   badeof:
587          if (report != REP_QUIET)
588 <                fprintf(stderr, "%s: Unexpected end-of-file\n", progname);
588 >                printf("%s: unexpected end-of-file\n", progname);
589          return(0);
590   }
591  
# Line 576 | Line 596 | compare_double()
596          long    nread = 0;
597          double  f1, f2;
598  
599 +        if (report >= REP_VERBOSE) {
600 +                fputs(progname, stdout);
601 +                fputs(": comparing inputs as 64-bit IEEE doubles\n", stdout);
602 +        }
603          while (getbinary(&f1, sizeof(f1), 1, f1in)) {
604                  if (!getbinary(&f2, sizeof(f2), 1, f2in))
605                          goto badeof;
# Line 583 | Line 607 | compare_double()
607                  if (real_check(f1, f2))
608                          continue;
609                  if (report != REP_QUIET)
610 <                        fprintf(stderr, "%s: %ld%s float values differ\n",
611 <                                        progname, nread, nsuffix[nread%10]);
610 >                        printf("%s: %ld%s double values differ\n",
611 >                                        progname, nread, num_sfx(nread));
612                  return(0);
613          }
614          if (!getbinary(&f2, sizeof(f2), 1, f2in))
615                  return(good_RMS());             /* final check of RMS */
616   badeof:
617          if (report != REP_QUIET)
618 <                fprintf(stderr, "%s: Unexpected end-of-file\n", progname);
618 >                printf("%s: unexpected end-of-file\n", progname);
619          return(0);
620   }
621  
# Line 638 | Line 662 | main(int argc, char *argv[])
662                  }
663                  break;
664          }
665 <        if (a != argc-2)
665 >        if (a != argc-2)                        /* make sure of two inputs */
666                  usage();
667 <        if (!f1name) f1name = argv[a];
644 <        if (!f2name) f2name = argv[a+1];
645 <
646 <        if (!strcmp(f1name, f2name)) {          /* inputs are same? */
667 >        if (!strcmp(argv[a], argv[a+1])) {      /* inputs are same? */
668                  if (report >= REP_WARN)
669 <                        fprintf(stderr, "%s: warning - identical inputs given\n",
669 >                        printf("%s: warning - identical inputs given\n",
670                                          progname);
671                  return(0);
672          }
673 +        if (!f1name) f1name = argv[a];
674 +        if (!f2name) f2name = argv[a+1];
675                                                  /* open inputs */
676          SET_FILE_BINARY(stdin);                 /* in case we're using it */
677          if (!f1in && !(f1in = fopen(f1name, "rb"))) {
# Line 669 | Line 692 | main(int argc, char *argv[])
692                  return(1);
693          if (typ1 != typ2) {
694                  if (report != REP_QUIET)
695 <                        fprintf(stderr, "%s: '%s' is %s and '%s' is %s\n",
695 >                        printf("%s: '%s' is %s and '%s' is %s\n",
696                                          progname, f1name, file_type[typ1],
697                                          f2name, file_type[typ2]);
698                  return(1);
# Line 680 | Line 703 | main(int argc, char *argv[])
703          lu_done(&hdr1); lu_done(&hdr2);
704          if (!ign_header & (report >= REP_WARN)) {
705                  if (typ1 == TYP_UNKNOWN)
706 <                        fprintf(stderr, "%s: warning - unrecognized format, comparing as binary\n",
706 >                        printf("%s: warning - unrecognized format, comparing as binary\n",
707                                          progname);
708                  if (lin1cnt != lin2cnt)
709 <                        fprintf(stderr, "%s: warning - headers are different lengths\n",
709 >                        printf("%s: warning - headers are different lengths\n",
710                                          progname);
711          }
712          if (report >= REP_VERBOSE)
713 <                fprintf(stderr, "%s: input file type is %s\n",
713 >                printf("%s: input file type is %s\n",
714                                  progname, file_type[typ1]);
715  
716          switch (typ1) {                         /* compare based on type */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines