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.12 by greg, Fri Oct 19 22:15:30 2018 UTC vs.
Revision 2.20 by greg, Wed Aug 14 18:20:02 2019 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 45 | Line 47 | const char     *file_type[] = {
47                          "ascii",
48                          COLRFMT,
49                          CIEFMT,
50 +                        DEPTH16FMT,
51 +                        NORMAL32FMT,
52                          "float",
53                          "double",
54                          "BSDF_RBFmesh",
55                          "Radiance_octree",
56                          "Radiance_tmesh",
57 +                        "8-bit_indexed_name",
58 +                        "16-bit_indexed_name",
59 +                        "24-bit_indexed_name",
60                          "BINARY_unknown",
61                          NULL    /* terminator */
62                  };
63                                  /* keep consistent with above */
64 < enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, TYP_FLOAT,
65 <                TYP_DOUBLE, TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, TYP_BINARY};
64 > enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE,
65 >                TYP_DEPTH, TYP_NORM, TYP_FLOAT, TYP_DOUBLE,
66 >                TYP_RBFMESH, TYP_OCTREE, TYP_TMESH,
67 >                TYP_ID8, TYP_ID16, TYP_ID24, TYP_BINARY};
68                  
69   #define has_header(t)   (!( 1L<<(t) & (1L<<TYP_TEXT | 1L<<TYP_BINARY) ))
70  
# Line 64 | Line 73 | const char     *hdr_ignkey[] = {
73                          "SOFTWARE",
74                          "CAPDATE",
75                          "GMT",
76 +                        "FRAME",
77                          NULL    /* terminator */
78                  };
79                                  /* header variable settings */
# Line 75 | Line 85 | LUTAB  hdr2 = LU_SINIT(free,free);
85                                          lin2cnt += (htp == &hdr2))
86  
87   typedef struct {                /* dynamic line buffer */
88 <        char    *ptr;
88 >        char    *str;
89          int     len;
90          int     siz;
91   } LINEBUF;
92  
93 < #define init_line(bp)   ((bp)->ptr = NULL, (bp)->siz = 0)
93 > #define init_line(bp)   ((bp)->str = NULL, (bp)->siz = 0)
94                                  /* 100 MByte limit on line buffer */
95   #define MAXBUF          (100L<<20)
96  
# Line 89 | Line 99 | char           *progname = NULL;
99   const char      stdin_name[] = "<stdin>";
100   const char      *f1name=NULL, *f2name=NULL;
101   FILE            *f1in=NULL, *f2in=NULL;
102 + int             f1swap=0, f2swap=0;
103  
104                                  /* running real differences */
105   double  diff2sum = 0;
# Line 109 | Line 120 | usage()
120   static int
121   read_line(LINEBUF *bp, FILE *fp)
122   {
123 +        static int      doneWarn = 0;
124 +
125          bp->len = 0;
126 <        if (!bp->ptr) {
127 <                bp->ptr = (char *)malloc(bp->siz = 512);
128 <                if (!bp->ptr)
126 >        if (!bp->str) {
127 >                bp->str = (char *)malloc(bp->siz = 512);
128 >                if (!bp->str)
129                          goto memerr;
130          }
131 <        while (fgets(bp->ptr + bp->len, bp->siz - bp->len, fp)) {
132 <                bp->len += strlen(bp->ptr + bp->len);
133 <                if (bp->ptr[bp->len-1] == '\n')
131 >        while (fgets(bp->str + bp->len, bp->siz - bp->len, fp)) {
132 >                bp->len += strlen(bp->str + bp->len);
133 >                if (bp->str[bp->len-1] == '\n')
134                          break;          /* found EOL */
135                  if (bp->len < bp->siz - 4)
136                          continue;       /* at EOF? */
137 <                if (bp->siz >= MAXBUF)
138 <                        break;          /* don't go to extremes */
137 >                if (bp->siz >= MAXBUF) {
138 >                        if ((report >= REP_WARN) & !doneWarn) {
139 >                                fprintf(stderr,
140 >                        "%s: warning - input line(s) past %ld MByte limit\n",
141 >                                        progname, MAXBUF>>20);
142 >                                doneWarn++;
143 >                        }
144 >                        break;          /* return MAXBUF partial line */
145 >                }
146                  if ((bp->siz += bp->siz/2) > MAXBUF)
147                          bp->siz = MAXBUF;
148 <                bp->ptr = (char *)realloc(bp->ptr, bp->siz);
149 <                if (!bp->ptr)
148 >                bp->str = (char *)realloc(bp->str, bp->siz);
149 >                if (!bp->str)
150                          goto memerr;
151          }
152          return(bp->len);
# Line 141 | Line 161 | memerr:
161   static void
162   free_line(LINEBUF *bp)
163   {
164 <        bp->len = 0;
165 <        if (!bp->ptr) return;
146 <        free(bp->ptr);
147 <        bp->ptr = NULL;
164 >        if (bp->str) free(bp->str);
165 >        init_line(bp);
166   }
167  
168   /* Get type ID from name (or 0 if not found) */
# Line 192 | Line 210 | color_check(COLOR c1, COLOR c2)
210   {
211          int     p;
212  
213 <        if (!real_check(colval(c1,RED)+colval(c1,GRN)+colval(c1,BLU)*(1./3.),
214 <                        colval(c2,RED)+colval(c2,GRN)+colval(c2,BLU))*(1./3.))
213 >        if (!real_check((colval(c1,RED)+colval(c1,GRN)+colval(c1,BLU))*(1./3.),
214 >                        (colval(c2,RED)+colval(c2,GRN)+colval(c2,BLU))*(1./3.)))
215                  return(0);
216  
217          p = (colval(c1,GRN) > colval(c1,RED)) ? GRN : RED;
# Line 202 | Line 220 | color_check(COLOR c1, COLOR c2)
220          return(real_check(colval(c1,p), colval(c2,p)));
221   }
222  
223 + /* Compare two normal directions for equivalence */
224 + static int
225 + norm_check(FVECT nv1, FVECT nv2)
226 + {
227 +        double  max2 = nv1[2]*nv1[2];
228 +        int     imax = 2;
229 +        int     i = 2;
230 +                                        /* identify largest component */
231 +        while (i--) {
232 +                double  tm2 = nv1[i]*nv1[i];
233 +                if (tm2 > max2) {
234 +                        imax = i;
235 +                        max2 = tm2;
236 +                }
237 +        }
238 +        i = 3;                          /* compare smaller components */
239 +        while (i--) {
240 +                if (i == imax)
241 +                        continue;
242 +                if (!real_check(nv1[i], nv2[i]))
243 +                        return(0);
244 +        }
245 +        return(1);
246 + }
247 +
248   /* Compare two strings for equivalence */
249   static int
250   equiv_string(char *s1, char *s2)
# Line 283 | Line 326 | setheadvar(char *val, void *p)
326          adv_linecnt(htp);       /* side-effect is to count lines */
327          if (!isalpha(*val))     /* key must start line */
328                  return(0);
329 +                                /* check if we need to swap binary data */
330 +        if ((n = isbigendian(val)) >= 0) {
331 +                if (nativebigendian() == n)
332 +                        return(0);
333 +                f1swap += (htp == &hdr1);
334 +                f2swap += (htp == &hdr2);
335 +                return(0);
336 +        }
337          key = val++;
338          while (*val && !isspace(*val) & (*val != '='))
339                  val++;
# Line 506 | Line 557 | compare_text()
557          init_line(&l1buf); init_line(&l2buf);   /* compare a line at a time */
558          while (read_line(&l1buf, f1in)) {
559                  lin1cnt++;
560 <                if (!*sskip2(l1buf.ptr,0))
560 >                if (!*sskip2(l1buf.str,0))
561                          continue;               /* ignore empty lines */
562  
563                  while (read_line(&l2buf, f2in)) {
564                          lin2cnt++;
565 <                        if (*sskip2(l2buf.ptr,0))
565 >                        if (*sskip2(l2buf.str,0))
566                                  break;          /* found other non-empty line */
567                  }
568 <                if (feof(f2in)) {
568 >                if (!l2buf.len) {               /* input 2 EOF? */
569                          if (report != REP_QUIET) {
570                                  fputs(f2name, stdout);
571                                  fputs(": unexpected end-of-file\n", stdout);
# Line 523 | Line 574 | compare_text()
574                          return(0);
575                  }
576                                                  /* compare non-empty lines */
577 <                if (!equiv_string(l1buf.ptr, l2buf.ptr)) {
577 >                if (!equiv_string(l1buf.str, l2buf.str)) {
578                          if (report != REP_QUIET) {
579                                  printf("%s: inputs '%s' and '%s' differ at line %d|%d\n",
580                                                  progname, f1name, f2name,
# Line 533 | Line 584 | compare_text()
584                                                  (l2buf.len < 256) ) {
585                                          fputs("------------- Mismatch -------------\n", stdout);
586                                          printf("%s@%d:\t%s", f1name,
587 <                                                        lin1cnt, l1buf.ptr);
587 >                                                        lin1cnt, l1buf.str);
588                                          printf("%s@%d:\t%s", f2name,
589 <                                                        lin2cnt, l2buf.ptr);
589 >                                                        lin2cnt, l2buf.str);
590                                  }
591                          }
592                          free_line(&l1buf); free_line(&l2buf);
593                          return(0);
594                  }
595          }
596 <                                                /* check for EOF on input 2 */
596 >        free_line(&l1buf);                      /* check for EOF on input 2 */
597          while (read_line(&l2buf, f2in)) {
598 <                if (!*sskip2(l2buf.ptr,0))
598 >                if (!*sskip2(l2buf.str,0))
599                          continue;
600                  if (report != REP_QUIET) {
601                          fputs(f1name, stdout);
602                          fputs(": unexpected end-of-file\n", stdout);
603                  }
604 <                free_line(&l1buf); free_line(&l2buf);
604 >                free_line(&l2buf);
605                  return(0);
606          }
607 <        free_line(&l1buf); free_line(&l2buf);
607 >        free_line(&l2buf);
608          return(good_RMS());                     /* final check for reals */
609   }
610  
611 + /* Check image/map resolutions */
612 + static int
613 + check_resolu(const char *class, RESOLU *r1p, RESOLU *r2p)
614 + {
615 +        if (r1p->rt != r2p->rt) {
616 +                if (report != REP_QUIET)
617 +                        printf(
618 +                        "%s: %ss '%s' and '%s' have different pixel ordering\n",
619 +                                        progname, class, f1name, f2name);
620 +                return(0);
621 +        }
622 +        if ((r1p->xr != r2p->xr) | (r1p->yr != r2p->yr)) {
623 +                if (report != REP_QUIET)
624 +                        printf(
625 +                        "%s: %ss '%s' and '%s' are different sizes\n",
626 +                                        progname, class, f1name, f2name);
627 +                return(0);
628 +        }
629 +        return(1);
630 + }
631 +
632   /* Compare two inputs that are known to be RGBE or XYZE images */
633   static int
634   compare_hdr()
# Line 571 | Line 643 | compare_hdr()
643          }
644          fgetsresolu(&rs1, f1in);
645          fgetsresolu(&rs2, f2in);
646 <        if (rs1.rt != rs2.rt) {
575 <                if (report != REP_QUIET)
576 <                        printf(
577 <                        "%s: Images '%s' and '%s' have different pixel ordering\n",
578 <                                        progname, f1name, f2name);
646 >        if (!check_resolu("HDR image", &rs1, &rs2))
647                  return(0);
580        }
581        if ((rs1.xr != rs2.xr) | (rs1.yr != rs2.yr)) {
582                if (report != REP_QUIET)
583                        printf(
584                        "%s: Images '%s' and '%s' are different sizes\n",
585                                        progname, f1name, f2name);
586                return(0);
587        }
648          scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR));
649          scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR));
650          if (!scan1 | !scan2) {
# Line 629 | Line 689 | compare_hdr()
689          return(good_RMS());                     /* final check of RMS */
690   }
691  
692 + /* Set reference depth based on header variable */
693 + static int
694 + set_refdepth(DEPTHCODEC *dcp, LUTAB *htp)
695 + {
696 +        static char     depthvar[] = DEPTHSTR;
697 +        const char      *drval;
698 +
699 +        depthvar[LDEPTHSTR] = '\0';
700 +        drval = (const char *)lu_find(htp, depthvar)->data;
701 +        if (!drval)
702 +                return(0);
703 +        dcp->refdepth = atof(drval);
704 +        if (dcp->refdepth <= 0) {
705 +                if (report != REP_QUIET) {
706 +                        fputs(dcp->inpname, stderr);
707 +                        fputs(": bad reference depth '", stderr);
708 +                        fputs(drval, stderr);
709 +                        fputs("'\n", stderr);
710 +                }
711 +                return(-1);
712 +        }
713 +        return(1);
714 + }
715 +
716 + /* Compare two encoded depth maps */
717 + static int
718 + compare_depth()
719 + {
720 +        long            nread = 0;
721 +        DEPTHCODEC      dc1, dc2;
722 +
723 +        if (report >= REP_VERBOSE) {
724 +                fputs(progname, stdout);
725 +                fputs(": comparing inputs as depth maps\n", stdout);
726 +        }
727 +        set_dc_defaults(&dc1);
728 +        dc1.hdrflags = HF_RESIN;
729 +        dc1.finp = f1in;
730 +        dc1.inpname = f1name;
731 +        set_dc_defaults(&dc2);
732 +        dc2.hdrflags = HF_RESIN;
733 +        dc2.finp = f2in;
734 +        dc2.inpname = f2name;
735 +        if (report != REP_QUIET) {
736 +                dc1.hdrflags |= HF_STDERR;
737 +                dc2.hdrflags |= HF_STDERR;
738 +        }
739 +        if (!process_dc_header(&dc1, 0, NULL))
740 +                return(0);
741 +        if (!process_dc_header(&dc2, 0, NULL))
742 +                return(0);
743 +        if (!check_resolu("Depth map", &dc1.res, &dc2.res))
744 +                return(0);
745 +        if (set_refdepth(&dc1, &hdr1) < 0)
746 +                return(0);
747 +        if (set_refdepth(&dc2, &hdr2) < 0)
748 +                return(0);
749 +        while (nread < dc1.res.xr*dc1.res.yr) {
750 +                double  d1 = decode_depth_next(&dc1);
751 +                double  d2 = decode_depth_next(&dc2);
752 +                if ((d1 < 0) | (d2 < 0)) {
753 +                        if (report != REP_QUIET)
754 +                                printf("%s: unexpected end-of-file\n",
755 +                                                progname);
756 +                        return(0);
757 +                }
758 +                ++nread;
759 +                if (real_check(d1, d2))
760 +                        continue;
761 +                if (report != REP_QUIET)
762 +                        printf("%s: %ld%s depth values differ\n",
763 +                                        progname, nread, num_sfx(nread));
764 +                return(0);
765 +        }
766 +        return(good_RMS());             /* final check of RMS */
767 + }
768 +
769 + /* Compare two encoded normal maps */
770 + static int
771 + compare_norm()
772 + {
773 +        long            nread = 0;
774 +        NORMCODEC       nc1, nc2;
775 +
776 +        if (report >= REP_VERBOSE) {
777 +                fputs(progname, stdout);
778 +                fputs(": comparing inputs as normal maps\n", stdout);
779 +        }
780 +        set_nc_defaults(&nc1);
781 +        nc1.hdrflags = HF_RESIN;
782 +        nc1.finp = f1in;
783 +        nc1.inpname = f1name;
784 +        set_nc_defaults(&nc2);
785 +        nc2.hdrflags = HF_RESIN;
786 +        nc2.finp = f2in;
787 +        nc2.inpname = f2name;
788 +        if (report != REP_QUIET) {
789 +                nc1.hdrflags |= HF_STDERR;
790 +                nc2.hdrflags |= HF_STDERR;
791 +        }
792 +        if (!process_nc_header(&nc1, 0, NULL))
793 +                return(0);
794 +        if (!process_nc_header(&nc2, 0, NULL))
795 +                return(0);
796 +        if (!check_resolu("Normal map", &nc1.res, &nc2.res))
797 +                return(0);
798 +        while (nread < nc1.res.xr*nc1.res.yr) {
799 +                FVECT   nv1, nv2;
800 +                int     rv1 = decode_normal_next(nv1, &nc1);
801 +                int     rv2 = decode_normal_next(nv2, &nc2);
802 +                if ((rv1 < 0) | (rv2 < 0)) {
803 +                        if (report != REP_QUIET)
804 +                                printf("%s: unexpected end-of-file\n",
805 +                                                progname);
806 +                        return(0);
807 +                }
808 +                ++nread;
809 +                if (rv1 == rv2 && (!rv1 || norm_check(nv1, nv2)))
810 +                        continue;
811 +                if (report != REP_QUIET)
812 +                        printf("%s: %ld%s normal vectors differ\n",
813 +                                        progname, nread, num_sfx(nread));
814 +                return(0);
815 +        }
816 +        return(good_RMS());             /* final check of RMS */
817 + }
818 +
819   /* Compare two inputs that are known to be 32-bit floating-point data */
820   static int
821   compare_float()
# Line 644 | Line 831 | compare_float()
831                  if (!getbinary(&f2, sizeof(f2), 1, f2in))
832                          goto badeof;
833                  ++nread;
834 +                if (f1swap) swap32((char *)&f1, 1);
835 +                if (f2swap) swap32((char *)&f2, 1);
836                  if (real_check(f1, f2))
837                          continue;
838                  if (report != REP_QUIET)
# Line 674 | Line 863 | compare_double()
863                  if (!getbinary(&f2, sizeof(f2), 1, f2in))
864                          goto badeof;
865                  ++nread;
866 +                if (f1swap) swap64((char *)&f1, 1);
867 +                if (f2swap) swap64((char *)&f2, 1);
868                  if (real_check(f1, f2))
869                          continue;
870                  if (report != REP_QUIET)
# Line 770 | Line 961 | main(int argc, char *argv[])
961          ign_header |= !has_header(typ1);        /* check headers if indicated */
962          if (!ign_header && !headers_match())
963                  return(1);
773        lu_done(&hdr1); lu_done(&hdr2);
964          if (!ign_header & (report >= REP_WARN)) {
775                if (typ1 == TYP_UNKNOWN)
776                        printf("%s: warning - unrecognized format, comparing as binary\n",
777                                        progname);
965                  if (lin1cnt != lin2cnt)
966                          printf("%s: warning - headers are different lengths\n",
967                                          progname);
968 +                if (typ1 == TYP_UNKNOWN)
969 +                        printf("%s: warning - unrecognized format\n",
970 +                                        progname);
971          }
972          if (report >= REP_VERBOSE)
973                  printf("%s: input file type is %s\n",
# Line 788 | Line 978 | main(int argc, char *argv[])
978          case TYP_TMESH:
979          case TYP_OCTREE:
980          case TYP_RBFMESH:
981 +        case TYP_ID8:
982 +        case TYP_ID16:
983 +        case TYP_ID24:
984          case TYP_UNKNOWN:
985                  return( !compare_binary() );
986          case TYP_TEXT:
# Line 796 | Line 989 | main(int argc, char *argv[])
989          case TYP_RGBE:
990          case TYP_XYZE:
991                  return( !compare_hdr() );
992 +        case TYP_DEPTH:
993 +                return( !compare_depth() );
994 +        case TYP_NORM:
995 +                return( !compare_norm() );
996          case TYP_FLOAT:
997                  return( !compare_float() );
998          case TYP_DOUBLE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines