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.17 by greg, Sat Nov 17 20:22:17 2018 UTC vs.
Revision 2.28 by greg, Mon Sep 27 19:00:04 2021 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 33 | Line 35 | double max_lim = 0.25;         /* difference limit if non-neg
35  
36   int     lin1cnt=0, lin2cnt=0;   /* file line position */
37  
38 + int     comment_c = '\0';       /* comment delimiter for text files */
39 +
40   const char      nsuffix[10][3] = {              /* 1st, 2nd, 3rd, etc. */
41                          "th","st","nd","rd","th","th","th","th","th","th"
42                  };
# Line 45 | Line 49 | const char     *file_type[] = {
49                          "ascii",
50                          COLRFMT,
51                          CIEFMT,
52 +                        DEPTH16FMT,
53 +                        NORMAL32FMT,
54                          "float",
55                          "double",
56                          "BSDF_RBFmesh",
57                          "Radiance_octree",
58                          "Radiance_tmesh",
59 +                        "8-bit_indexed_name",
60 +                        "16-bit_indexed_name",
61 +                        "24-bit_indexed_name",
62                          "BINARY_unknown",
63                          NULL    /* terminator */
64                  };
65                                  /* keep consistent with above */
66 < enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, TYP_FLOAT,
67 <                TYP_DOUBLE, TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, TYP_BINARY};
66 > enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE,
67 >                TYP_DEPTH, TYP_NORM, TYP_FLOAT, TYP_DOUBLE,
68 >                TYP_RBFMESH, TYP_OCTREE, TYP_TMESH,
69 >                TYP_ID8, TYP_ID16, TYP_ID24, TYP_BINARY};
70                  
71   #define has_header(t)   (!( 1L<<(t) & (1L<<TYP_TEXT | 1L<<TYP_BINARY) ))
72  
# Line 64 | Line 75 | const char     *hdr_ignkey[] = {
75                          "SOFTWARE",
76                          "CAPDATE",
77                          "GMT",
78 +                        "FRAME",
79                          NULL    /* terminator */
80                  };
81                                  /* header variable settings */
# Line 89 | Line 101 | char           *progname = NULL;
101   const char      stdin_name[] = "<stdin>";
102   const char      *f1name=NULL, *f2name=NULL;
103   FILE            *f1in=NULL, *f2in=NULL;
104 + int             f1swap=0, f2swap=0;
105  
106                                  /* running real differences */
107   double  diff2sum = 0;
# Line 100 | Line 113 | usage()
113   {
114          fputs("Usage: ", stderr);
115          fputs(progname, stderr);
116 <        fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n",
116 >        fputs(" [-h][-c#][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n",
117                          stderr);
118          exit(2);
119   }
# Line 138 | Line 151 | read_line(LINEBUF *bp, FILE *fp)
151                  if (!bp->str)
152                          goto memerr;
153          }
154 +        if (comment_c) {                /* elide comment? */
155 +                char    *cp = sskip2(bp->str,0);
156 +                if (*cp == comment_c) {
157 +                        *cp++ = '\n';
158 +                        *cp = '\0';
159 +                        bp->len = cp - bp->str;
160 +                }
161 +        }
162          return(bp->len);
163   memerr:
164          fprintf(stderr,
# Line 176 | Line 197 | real_check(double r1, double r2)
197  
198          if (rel_min > 0) {      /* doing relative differences? */
199                  double  av2 = .25*(r1*r1 + 2.*fabs(r1*r2) + r2*r2);
200 <                if (av2 > rel_min*rel_min)
201 <                        diff2 /= av2;
200 >                if (av2 < rel_min*rel_min)
201 >                        av2 = rel_min*rel_min;
202 >                diff2 /= av2;
203          }
204          if (max_lim >= 0 && diff2 > max_lim*max_lim) {
205                  if (report != REP_QUIET)
# Line 209 | Line 231 | color_check(COLOR c1, COLOR c2)
231          return(real_check(colval(c1,p), colval(c2,p)));
232   }
233  
234 + /* Compare two normal directions for equivalence */
235 + static int
236 + norm_check(FVECT nv1, FVECT nv2)
237 + {
238 +        double  max2 = nv1[2]*nv2[2];
239 +        int     imax = 2;
240 +        int     i = 2;
241 +                                        /* identify largest component */
242 +        while (i--) {
243 +                double  tm2 = nv1[i]*nv2[i];
244 +                if (tm2 > max2) {
245 +                        imax = i;
246 +                        max2 = tm2;
247 +                }
248 +        }
249 +        i = 3;                          /* compare smaller components */
250 +        while (i--) {
251 +                if (i == imax)
252 +                        continue;
253 +                if (!real_check(nv1[i], nv2[i]))
254 +                        return(0);
255 +        }
256 +        return(1);
257 + }
258 +
259   /* Compare two strings for equivalence */
260   static int
261   equiv_string(char *s1, char *s2)
# Line 281 | Line 328 | equiv_string(char *s1, char *s2)
328   static int
329   setheadvar(char *val, void *p)
330   {
331 +        char    newval[128];
332          LUTAB   *htp = (LUTAB *)p;
333          LUENT   *tep;
334          char    *key;
# Line 290 | Line 338 | setheadvar(char *val, void *p)
338          adv_linecnt(htp);       /* side-effect is to count lines */
339          if (!isalpha(*val))     /* key must start line */
340                  return(0);
341 +                                /* check if we need to swap binary data */
342 +        if ((n = isbigendian(val)) >= 0) {
343 +                if (nativebigendian() == n)
344 +                        return(0);
345 +                f1swap += (htp == &hdr1);
346 +                f2swap += (htp == &hdr2);
347 +                return(0);
348 +        }
349          key = val++;
350          while (*val && !isspace(*val) & (*val != '='))
351                  val++;
# Line 315 | Line 371 | setheadvar(char *val, void *p)
371                  return(-1);     /* memory allocation error */
372          if (!tep->key)
373                  tep->key = strcpy(malloc(kln+1), key);
374 <        if (tep->data)
374 >        if (tep->data) {        /* check for special cases */
375 >                if (!strcmp(key, "EXPOSURE")) {
376 >                        sprintf(newval, "%.6e", atof(tep->data)*atof(val));
377 >                        vln = strlen(val = newval);
378 >                }
379                  free(tep->data);
380 +        }
381          tep->data = strcpy(malloc(vln+1), val);
382          return(1);
383   }
# Line 425 | Line 486 | identify_type(const char *name, FILE *fin, LUTAB *htp)
486          }
487          if (c)
488                  return(TYP_BINARY);
428        SET_FILE_TEXT(fin);                     /* originally set to binary */
489          return(TYP_TEXT);
490   badeof:
491          if (report != REP_QUIET) {
# Line 508 | Line 568 | compare_text()
568  
569          if (report >= REP_VERBOSE) {
570                  fputs(progname, stdout);
571 <                fputs(": comparing inputs as ASCII text\n", stdout);
571 >                fputs(": comparing inputs as ASCII text", stdout);
572 >                if (comment_c) {
573 >                        fputs(", ignoring comments starting with '", stdout);
574 >                        fputc(comment_c, stdout);
575 >                        fputc('\'', stdout);
576 >                }
577 >                fputc('\n', stdout);
578          }
579 +        SET_FILE_TEXT(f1in);                    /* originally set to binary */
580 +        SET_FILE_TEXT(f2in);
581          init_line(&l1buf); init_line(&l2buf);   /* compare a line at a time */
582          while (read_line(&l1buf, f1in)) {
583                  lin1cnt++;
# Line 564 | Line 632 | compare_text()
632          return(good_RMS());                     /* final check for reals */
633   }
634  
635 + /* Check image/map resolutions */
636 + static int
637 + check_resolu(const char *class, RESOLU *r1p, RESOLU *r2p)
638 + {
639 +        if (r1p->rt != r2p->rt) {
640 +                if (report != REP_QUIET)
641 +                        printf(
642 +                        "%s: %ss '%s' and '%s' have different pixel ordering\n",
643 +                                        progname, class, f1name, f2name);
644 +                return(0);
645 +        }
646 +        if ((r1p->xr != r2p->xr) | (r1p->yr != r2p->yr)) {
647 +                if (report != REP_QUIET)
648 +                        printf(
649 +                        "%s: %ss '%s' and '%s' are different sizes\n",
650 +                                        progname, class, f1name, f2name);
651 +                return(0);
652 +        }
653 +        return(1);
654 + }
655 +
656   /* Compare two inputs that are known to be RGBE or XYZE images */
657   static int
658   compare_hdr()
# Line 578 | Line 667 | compare_hdr()
667          }
668          fgetsresolu(&rs1, f1in);
669          fgetsresolu(&rs2, f2in);
670 <        if (rs1.rt != rs2.rt) {
582 <                if (report != REP_QUIET)
583 <                        printf(
584 <                        "%s: Images '%s' and '%s' have different pixel ordering\n",
585 <                                        progname, f1name, f2name);
670 >        if (!check_resolu("HDR image", &rs1, &rs2))
671                  return(0);
587        }
588        if ((rs1.xr != rs2.xr) | (rs1.yr != rs2.yr)) {
589                if (report != REP_QUIET)
590                        printf(
591                        "%s: Images '%s' and '%s' are different sizes\n",
592                                        progname, f1name, f2name);
593                return(0);
594        }
672          scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR));
673          scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR));
674          if (!scan1 | !scan2) {
# Line 636 | Line 713 | compare_hdr()
713          return(good_RMS());                     /* final check of RMS */
714   }
715  
716 + /* Set reference depth based on header variable */
717 + static int
718 + set_refdepth(DEPTHCODEC *dcp, LUTAB *htp)
719 + {
720 +        static char     depthvar[] = DEPTHSTR;
721 +        const char      *drval;
722 +
723 +        depthvar[LDEPTHSTR-1] = '\0';
724 +        drval = (const char *)lu_find(htp, depthvar)->data;
725 +        if (!drval)
726 +                return(0);
727 +        dcp->refdepth = atof(drval);
728 +        if (dcp->refdepth <= 0) {
729 +                if (report != REP_QUIET) {
730 +                        fputs(dcp->inpname, stderr);
731 +                        fputs(": bad reference depth '", stderr);
732 +                        fputs(drval, stderr);
733 +                        fputs("'\n", stderr);
734 +                }
735 +                return(-1);
736 +        }
737 +        return(1);
738 + }
739 +
740 + /* Compare two encoded depth maps */
741 + static int
742 + compare_depth()
743 + {
744 +        long            nread = 0;
745 +        DEPTHCODEC      dc1, dc2;
746 +
747 +        if (report >= REP_VERBOSE) {
748 +                fputs(progname, stdout);
749 +                fputs(": comparing inputs as depth maps\n", stdout);
750 +        }
751 +        set_dc_defaults(&dc1);
752 +        dc1.hdrflags = HF_RESIN;
753 +        dc1.finp = f1in;
754 +        dc1.inpname = f1name;
755 +        set_dc_defaults(&dc2);
756 +        dc2.hdrflags = HF_RESIN;
757 +        dc2.finp = f2in;
758 +        dc2.inpname = f2name;
759 +        if (report != REP_QUIET) {
760 +                dc1.hdrflags |= HF_STDERR;
761 +                dc2.hdrflags |= HF_STDERR;
762 +        }
763 +        if (!process_dc_header(&dc1, 0, NULL))
764 +                return(0);
765 +        if (!process_dc_header(&dc2, 0, NULL))
766 +                return(0);
767 +        if (!check_resolu("Depth map", &dc1.res, &dc2.res))
768 +                return(0);
769 +        if (set_refdepth(&dc1, &hdr1) < 0)
770 +                return(0);
771 +        if (set_refdepth(&dc2, &hdr2) < 0)
772 +                return(0);
773 +        while (nread < dc1.res.xr*dc1.res.yr) {
774 +                double  d1 = decode_depth_next(&dc1);
775 +                double  d2 = decode_depth_next(&dc2);
776 +                if ((d1 < 0) | (d2 < 0)) {
777 +                        if (report != REP_QUIET)
778 +                                printf("%s: unexpected end-of-file\n",
779 +                                                progname);
780 +                        return(0);
781 +                }
782 +                ++nread;
783 +                if (real_check(d1, d2))
784 +                        continue;
785 +                if (report != REP_QUIET)
786 +                        printf("%s: %ld%s depth values differ\n",
787 +                                        progname, nread, num_sfx(nread));
788 +                return(0);
789 +        }
790 +        return(good_RMS());             /* final check of RMS */
791 + }
792 +
793 + /* Compare two encoded normal maps */
794 + static int
795 + compare_norm()
796 + {
797 +        long            nread = 0;
798 +        NORMCODEC       nc1, nc2;
799 +
800 +        if (report >= REP_VERBOSE) {
801 +                fputs(progname, stdout);
802 +                fputs(": comparing inputs as normal maps\n", stdout);
803 +        }
804 +        set_nc_defaults(&nc1);
805 +        nc1.hdrflags = HF_RESIN;
806 +        nc1.finp = f1in;
807 +        nc1.inpname = f1name;
808 +        set_nc_defaults(&nc2);
809 +        nc2.hdrflags = HF_RESIN;
810 +        nc2.finp = f2in;
811 +        nc2.inpname = f2name;
812 +        if (report != REP_QUIET) {
813 +                nc1.hdrflags |= HF_STDERR;
814 +                nc2.hdrflags |= HF_STDERR;
815 +        }
816 +        if (!process_nc_header(&nc1, 0, NULL))
817 +                return(0);
818 +        if (!process_nc_header(&nc2, 0, NULL))
819 +                return(0);
820 +        if (!check_resolu("Normal map", &nc1.res, &nc2.res))
821 +                return(0);
822 +        while (nread < nc1.res.xr*nc1.res.yr) {
823 +                FVECT   nv1, nv2;
824 +                int     rv1 = decode_normal_next(nv1, &nc1);
825 +                int     rv2 = decode_normal_next(nv2, &nc2);
826 +                if ((rv1 < 0) | (rv2 < 0)) {
827 +                        if (report != REP_QUIET)
828 +                                printf("%s: unexpected end-of-file\n",
829 +                                                progname);
830 +                        return(0);
831 +                }
832 +                ++nread;
833 +                if (rv1 == rv2 && (!rv1 || norm_check(nv1, nv2)))
834 +                        continue;
835 +                if (report != REP_QUIET)
836 +                        printf("%s: %ld%s normal vectors differ\n",
837 +                                        progname, nread, num_sfx(nread));
838 +                return(0);
839 +        }
840 +        return(good_RMS());             /* final check of RMS */
841 + }
842 +
843   /* Compare two inputs that are known to be 32-bit floating-point data */
844   static int
845   compare_float()
# Line 651 | Line 855 | compare_float()
855                  if (!getbinary(&f2, sizeof(f2), 1, f2in))
856                          goto badeof;
857                  ++nread;
858 +                if (f1swap) swap32((char *)&f1, 1);
859 +                if (f2swap) swap32((char *)&f2, 1);
860                  if (real_check(f1, f2))
861                          continue;
862                  if (report != REP_QUIET)
# Line 681 | Line 887 | compare_double()
887                  if (!getbinary(&f2, sizeof(f2), 1, f2in))
888                          goto badeof;
889                  ++nread;
890 +                if (f1swap) swap64((char *)&f1, 1);
891 +                if (f2swap) swap64((char *)&f2, 1);
892                  if (real_check(f1, f2))
893                          continue;
894                  if (report != REP_QUIET)
# Line 709 | Line 917 | main(int argc, char *argv[])
917                  case 'h':                       /* ignore header info. */
918                          ign_header = !ign_header;
919                          continue;
920 +                case 'c':                       /* ignore comments */
921 +                        comment_c = argv[a][2];
922 +                        continue;
923                  case 's':                       /* silent operation */
924                          report = REP_QUIET;
925                          continue;
# Line 769 | Line 980 | main(int argc, char *argv[])
980                  return(2);
981          if (typ1 != typ2) {
982                  if (report != REP_QUIET)
983 <                        printf("%s: '%s' is %s and '%s' is %s\n",
983 >                        printf("%s: '%s' format is %s and '%s' is %s\n",
984                                          progname, f1name, file_type[typ1],
985                                          f2name, file_type[typ2]);
986                  return(1);
# Line 777 | Line 988 | main(int argc, char *argv[])
988          ign_header |= !has_header(typ1);        /* check headers if indicated */
989          if (!ign_header && !headers_match())
990                  return(1);
780        lu_done(&hdr1); lu_done(&hdr2);         /* done with header info. */
991          if (!ign_header & (report >= REP_WARN)) {
992                  if (lin1cnt != lin2cnt)
993                          printf("%s: warning - headers are different lengths\n",
# Line 786 | Line 996 | main(int argc, char *argv[])
996                          printf("%s: warning - unrecognized format\n",
997                                          progname);
998          }
999 <        if (report >= REP_VERBOSE)
1000 <                printf("%s: input file type is %s\n",
1001 <                                progname, file_type[typ1]);
1002 <
999 >        if (report >= REP_VERBOSE) {
1000 >                printf("%s: data format is %s\n", progname, file_type[typ1]);
1001 >                if ((typ1 == TYP_FLOAT) | (typ1 == TYP_DOUBLE)) {
1002 >                        if (f1swap)
1003 >                                printf("%s: input '%s' is byte-swapped\n",
1004 >                                                progname, f1name);
1005 >                        if (f2swap)
1006 >                                printf("%s: input '%s' is byte-swapped\n",
1007 >                                                progname, f2name);
1008 >                }
1009 >        }
1010          switch (typ1) {                         /* compare based on type */
1011          case TYP_BINARY:
1012          case TYP_TMESH:
1013          case TYP_OCTREE:
1014          case TYP_RBFMESH:
1015 +        case TYP_ID8:
1016 +        case TYP_ID16:
1017 +        case TYP_ID24:
1018          case TYP_UNKNOWN:
1019                  return( !compare_binary() );
1020          case TYP_TEXT:
# Line 803 | Line 1023 | main(int argc, char *argv[])
1023          case TYP_RGBE:
1024          case TYP_XYZE:
1025                  return( !compare_hdr() );
1026 +        case TYP_DEPTH:
1027 +                return( !compare_depth() );
1028 +        case TYP_NORM:
1029 +                return( !compare_norm() );
1030          case TYP_FLOAT:
1031                  return( !compare_float() );
1032          case TYP_DOUBLE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines