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.11 by greg, Fri Oct 19 20:32:16 2018 UTC vs.
Revision 2.19 by greg, Wed Aug 14 04:18:12 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 74 | Line 84 | LUTAB  hdr2 = LU_SINIT(free,free);
84   #define adv_linecnt(htp)        (lin1cnt += (htp == &hdr1), \
85                                          lin2cnt += (htp == &hdr2))
86  
87 + typedef struct {                /* dynamic line buffer */
88 +        char    *str;
89 +        int     len;
90 +        int     siz;
91 + } LINEBUF;
92 +
93 + #define init_line(bp)   ((bp)->str = NULL, (bp)->siz = 0)
94 +                                /* 100 MByte limit on line buffer */
95 + #define MAXBUF          (100L<<20)
96 +
97                                  /* input files */
98   char            *progname = NULL;
99   const char      stdin_name[] = "<stdin>";
# Line 95 | Line 115 | usage()
115          exit(2);
116   }
117  
118 + /* Read a text line, increasing buffer size as necessary */
119 + static int
120 + read_line(LINEBUF *bp, FILE *fp)
121 + {
122 +        static int      doneWarn = 0;
123 +
124 +        bp->len = 0;
125 +        if (!bp->str) {
126 +                bp->str = (char *)malloc(bp->siz = 512);
127 +                if (!bp->str)
128 +                        goto memerr;
129 +        }
130 +        while (fgets(bp->str + bp->len, bp->siz - bp->len, fp)) {
131 +                bp->len += strlen(bp->str + bp->len);
132 +                if (bp->str[bp->len-1] == '\n')
133 +                        break;          /* found EOL */
134 +                if (bp->len < bp->siz - 4)
135 +                        continue;       /* at EOF? */
136 +                if (bp->siz >= MAXBUF) {
137 +                        if ((report >= REP_WARN) & !doneWarn) {
138 +                                fprintf(stderr,
139 +                        "%s: warning - input line(s) past %ld MByte limit\n",
140 +                                        progname, MAXBUF>>20);
141 +                                doneWarn++;
142 +                        }
143 +                        break;          /* return MAXBUF partial line */
144 +                }
145 +                if ((bp->siz += bp->siz/2) > MAXBUF)
146 +                        bp->siz = MAXBUF;
147 +                bp->str = (char *)realloc(bp->str, bp->siz);
148 +                if (!bp->str)
149 +                        goto memerr;
150 +        }
151 +        return(bp->len);
152 + memerr:
153 +        fprintf(stderr,
154 +                "%s: out of memory in read_line() allocating %d byte buffer\n",
155 +                        progname, bp->siz);
156 +        exit(2);
157 + }
158 +
159 + /* Free line buffer */
160 + static void
161 + free_line(LINEBUF *bp)
162 + {
163 +        if (bp->str) free(bp->str);
164 +        init_line(bp);
165 + }
166 +
167   /* Get type ID from name (or 0 if not found) */
168   static int
169   xlate_type(const char *nm)
# Line 140 | Line 209 | color_check(COLOR c1, COLOR c2)
209   {
210          int     p;
211  
212 <        if (!real_check(colval(c1,RED)+colval(c1,GRN)+colval(c1,BLU)*(1./3.),
213 <                        colval(c2,RED)+colval(c2,GRN)+colval(c2,BLU))*(1./3.))
212 >        if (!real_check((colval(c1,RED)+colval(c1,GRN)+colval(c1,BLU))*(1./3.),
213 >                        (colval(c2,RED)+colval(c2,GRN)+colval(c2,BLU))*(1./3.)))
214                  return(0);
215  
216          p = (colval(c1,GRN) > colval(c1,RED)) ? GRN : RED;
# Line 150 | Line 219 | color_check(COLOR c1, COLOR c2)
219          return(real_check(colval(c1,p), colval(c2,p)));
220   }
221  
222 + /* Compare two normal directions for equivalence */
223 + static int
224 + norm_check(FVECT nv1, FVECT nv2)
225 + {
226 +        double  max2 = nv1[2]*nv1[2];
227 +        int     imax = 2;
228 +        int     i = 2;
229 +                                        /* identify largest component */
230 +        while (i--) {
231 +                double  tm2 = nv1[i]*nv1[i];
232 +                if (tm2 > max2) {
233 +                        imax = i;
234 +                        max2 = tm2;
235 +                }
236 +        }
237 +        i = 3;                          /* compare smaller components */
238 +        while (i--) {
239 +                if (i == imax)
240 +                        continue;
241 +                if (!real_check(nv1[i], nv2[i]))
242 +                        return(0);
243 +        }
244 +        return(1);
245 + }
246 +
247   /* Compare two strings for equivalence */
248   static int
249   equiv_string(char *s1, char *s2)
# Line 441 | Line 535 | compare_binary()
535          return(0);
536   }
537  
444 /* If line is continued (no newline), then back up to word boundary if one */
445 static int
446 leftover(char *buf)
447 {
448        int     minlen = strlen(buf);
449        char    *bend = buf + minlen;
450        char    *cp = bend-1;
451
452        if (*cp == '\r') *cp = '\n';            /* AARG Windoze! */
453        if (*cp == '\n')                        /* complete line? */
454                return(0);
455
456        minlen >>= 2;                           /* back over partial word */
457        while (!isspace(*cp))
458                if (--cp <= buf+minlen)
459                        return(0);
460        *cp++ = '\0';                           /* break line here */
461        return(bend - cp);
462 }
463
538   /* Compare two inputs as generic text files */
539   static int
540   compare_text()
541   {
542 <        char    l1buf[4096], l2buf[4096];
469 <        int     l1over=0, l2over=0;
542 >        LINEBUF l1buf, l2buf;
543  
544          if (report >= REP_VERBOSE) {
545                  fputs(progname, stdout);
546                  fputs(": comparing inputs as ASCII text\n", stdout);
547          }
548 <                                                /* compare a line at a time */
549 <        while (fgets(l1buf+l1over, sizeof(l1buf)-l1over, f1in)) {
550 <                lin1cnt += !(l1over = leftover(l1buf));
551 <                if (!*sskip2(l1buf,0)) {
479 <                        memmove(l1buf, l1buf+sizeof(l1buf)-l1over, l1over);
548 >        init_line(&l1buf); init_line(&l2buf);   /* compare a line at a time */
549 >        while (read_line(&l1buf, f1in)) {
550 >                lin1cnt++;
551 >                if (!*sskip2(l1buf.str,0))
552                          continue;               /* ignore empty lines */
553 <                }
554 <                while (fgets(l2buf+l2over, sizeof(l2buf)-l2over, f2in)) {
555 <                        lin2cnt += !(l2over = leftover(l2buf));
556 <                        if (*sskip2(l2buf,0))
553 >
554 >                while (read_line(&l2buf, f2in)) {
555 >                        lin2cnt++;
556 >                        if (*sskip2(l2buf.str,0))
557                                  break;          /* found other non-empty line */
486                        memmove(l2buf, l2buf+sizeof(l2buf)-l2over, l2over);
558                  }
559 <                if (feof(f2in)) {
559 >                if (!l2buf.len) {               /* input 2 EOF? */
560                          if (report != REP_QUIET) {
561                                  fputs(f2name, stdout);
562                                  fputs(": unexpected end-of-file\n", stdout);
563                          }
564 +                        free_line(&l1buf); free_line(&l2buf);
565                          return(0);
566                  }
567                                                  /* compare non-empty lines */
568 <                if (!equiv_string(l1buf, l2buf)) {
568 >                if (!equiv_string(l1buf.str, l2buf.str)) {
569                          if (report != REP_QUIET) {
570                                  printf("%s: inputs '%s' and '%s' differ at line %d|%d\n",
571                                                  progname, f1name, f2name,
572                                                  lin1cnt, lin2cnt);
573 <                                if (report >= REP_VERBOSE) {
573 >                                if ( report >= REP_VERBOSE &&
574 >                                                (l1buf.len < 256) &
575 >                                                (l2buf.len < 256) ) {
576                                          fputs("------------- Mismatch -------------\n", stdout);
577                                          printf("%s@%d:\t%s", f1name,
578 <                                                        lin1cnt, l1buf);
578 >                                                        lin1cnt, l1buf.str);
579                                          printf("%s@%d:\t%s", f2name,
580 <                                                        lin2cnt, l2buf);
580 >                                                        lin2cnt, l2buf.str);
581                                  }
582                          }
583 +                        free_line(&l1buf); free_line(&l2buf);
584                          return(0);
585                  }
511                if (l1over) memmove(l1buf, l1buf+sizeof(l1buf)-l1over, l1over);
512                if (l2over) memmove(l2buf, l2buf+sizeof(l2buf)-l2over, l2over);
586          }
587 <                                                /* check for EOF on input 2 */
588 <        while (fgets(l2buf, sizeof(l2buf), f2in)) {
589 <                if (!*sskip2(l2buf,0))
587 >        free_line(&l1buf);                      /* check for EOF on input 2 */
588 >        while (read_line(&l2buf, f2in)) {
589 >                if (!*sskip2(l2buf.str,0))
590                          continue;
591                  if (report != REP_QUIET) {
592                          fputs(f1name, stdout);
593                          fputs(": unexpected end-of-file\n", stdout);
594                  }
595 +                free_line(&l2buf);
596                  return(0);
597          }
598 +        free_line(&l2buf);
599          return(good_RMS());                     /* final check for reals */
600   }
601  
602 + /* Check image/map resolutions */
603 + static int
604 + check_resolu(const char *class, RESOLU *r1p, RESOLU *r2p)
605 + {
606 +        if (r1p->rt != r2p->rt) {
607 +                if (report != REP_QUIET)
608 +                        printf(
609 +                        "%s: %ss '%s' and '%s' have different pixel ordering\n",
610 +                                        progname, class, f1name, f2name);
611 +                return(0);
612 +        }
613 +        if ((r1p->xr != r2p->xr) | (r1p->yr != r2p->yr)) {
614 +                if (report != REP_QUIET)
615 +                        printf(
616 +                        "%s: %ss '%s' and '%s' are different sizes\n",
617 +                                        progname, class, f1name, f2name);
618 +                return(0);
619 +        }
620 +        return(1);
621 + }
622 +
623   /* Compare two inputs that are known to be RGBE or XYZE images */
624   static int
625   compare_hdr()
# Line 538 | Line 634 | compare_hdr()
634          }
635          fgetsresolu(&rs1, f1in);
636          fgetsresolu(&rs2, f2in);
637 <        if (rs1.rt != rs2.rt) {
542 <                if (report != REP_QUIET)
543 <                        printf(
544 <                        "%s: Images '%s' and '%s' have different pixel ordering\n",
545 <                                        progname, f1name, f2name);
637 >        if (!check_resolu("HDR image", &rs1, &rs2))
638                  return(0);
547        }
548        if ((rs1.xr != rs2.xr) | (rs1.yr != rs2.yr)) {
549                if (report != REP_QUIET)
550                        printf(
551                        "%s: Images '%s' and '%s' are different sizes\n",
552                                        progname, f1name, f2name);
553                return(0);
554        }
639          scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR));
640          scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR));
641          if (!scan1 | !scan2) {
# Line 596 | Line 680 | compare_hdr()
680          return(good_RMS());                     /* final check of RMS */
681   }
682  
683 + /* Set reference depth based on header variable */
684 + static int
685 + set_refdepth(DEPTHCODEC *dcp, LUTAB *htp)
686 + {
687 +        static char     depthvar[] = DEPTHSTR;
688 +        const char      *drval;
689 +
690 +        depthvar[LDEPTHSTR] = '\0';
691 +        drval = (const char *)lu_find(htp, depthvar)->data;
692 +        if (!drval)
693 +                return(0);
694 +        dcp->refdepth = atof(drval);
695 +        if (dcp->refdepth <= 0) {
696 +                if (report != REP_QUIET) {
697 +                        fputs(dcp->inpname, stderr);
698 +                        fputs(": bad reference depth '", stderr);
699 +                        fputs(drval, stderr);
700 +                        fputs("'\n", stderr);
701 +                }
702 +                return(-1);
703 +        }
704 +        return(1);
705 + }
706 +
707 + /* Compare two encoded depth maps */
708 + static int
709 + compare_depth()
710 + {
711 +        long            nread = 0;
712 +        DEPTHCODEC      dc1, dc2;
713 +
714 +        if (report >= REP_VERBOSE) {
715 +                fputs(progname, stdout);
716 +                fputs(": comparing inputs as depth maps\n", stdout);
717 +        }
718 +        set_dc_defaults(&dc1);
719 +        dc1.hdrflags = HF_RESIN;
720 +        dc1.finp = f1in;
721 +        dc1.inpname = f1name;
722 +        set_dc_defaults(&dc2);
723 +        dc2.hdrflags = HF_RESIN;
724 +        dc2.finp = f2in;
725 +        dc2.inpname = f2name;
726 +        if (report != REP_QUIET) {
727 +                dc1.hdrflags |= HF_STDERR;
728 +                dc2.hdrflags |= HF_STDERR;
729 +        }
730 +        if (!process_dc_header(&dc1, 0, NULL))
731 +                return(0);
732 +        if (!process_dc_header(&dc2, 0, NULL))
733 +                return(0);
734 +        if (!check_resolu("Depth map", &dc1.res, &dc2.res))
735 +                return(0);
736 +        if (set_refdepth(&dc1, &hdr1) < 0)
737 +                return(0);
738 +        if (set_refdepth(&dc2, &hdr2) < 0)
739 +                return(0);
740 +        while (nread < dc1.res.xr*dc1.res.yr) {
741 +                double  d1 = decode_depth_next(&dc1);
742 +                double  d2 = decode_depth_next(&dc2);
743 +                if ((d1 < 0) | (d2 < 0)) {
744 +                        if (report != REP_QUIET)
745 +                                printf("%s: unexpected end-of-file\n",
746 +                                                progname);
747 +                        return(0);
748 +                }
749 +                ++nread;
750 +                if (real_check(d1, d2))
751 +                        continue;
752 +                if (report != REP_QUIET)
753 +                        printf("%s: %ld%s depth values differ\n",
754 +                                        progname, nread, num_sfx(nread));
755 +                return(0);
756 +        }
757 +        return(good_RMS());             /* final check of RMS */
758 + }
759 +
760 + /* Compare two encoded normal maps */
761 + static int
762 + compare_norm()
763 + {
764 +        long            nread = 0;
765 +        NORMCODEC       nc1, nc2;
766 +
767 +        if (report >= REP_VERBOSE) {
768 +                fputs(progname, stdout);
769 +                fputs(": comparing inputs as normal maps\n", stdout);
770 +        }
771 +        set_nc_defaults(&nc1);
772 +        nc1.hdrflags = HF_RESIN;
773 +        nc1.finp = f1in;
774 +        nc1.inpname = f1name;
775 +        set_nc_defaults(&nc2);
776 +        nc2.hdrflags = HF_RESIN;
777 +        nc2.finp = f2in;
778 +        nc2.inpname = f2name;
779 +        if (report != REP_QUIET) {
780 +                nc1.hdrflags |= HF_STDERR;
781 +                nc2.hdrflags |= HF_STDERR;
782 +        }
783 +        if (!process_nc_header(&nc1, 0, NULL))
784 +                return(0);
785 +        if (!process_nc_header(&nc2, 0, NULL))
786 +                return(0);
787 +        if (!check_resolu("Normal map", &nc1.res, &nc2.res))
788 +                return(0);
789 +        while (nread < nc1.res.xr*nc1.res.yr) {
790 +                FVECT   nv1, nv2;
791 +                int     rv1 = decode_normal_next(nv1, &nc1);
792 +                int     rv2 = decode_normal_next(nv2, &nc2);
793 +                if ((rv1 < 0) | (rv2 < 0)) {
794 +                        if (report != REP_QUIET)
795 +                                printf("%s: unexpected end-of-file\n",
796 +                                                progname);
797 +                        return(0);
798 +                }
799 +                ++nread;
800 +                if (rv1 == rv2 && (!rv1 || norm_check(nv1, nv2)))
801 +                        continue;
802 +                if (report != REP_QUIET)
803 +                        printf("%s: %ld%s normal vectors differ\n",
804 +                                        progname, nread, num_sfx(nread));
805 +                return(0);
806 +        }
807 +        return(good_RMS());             /* final check of RMS */
808 + }
809 +
810   /* Compare two inputs that are known to be 32-bit floating-point data */
811   static int
812   compare_float()
# Line 737 | Line 948 | main(int argc, char *argv[])
948          ign_header |= !has_header(typ1);        /* check headers if indicated */
949          if (!ign_header && !headers_match())
950                  return(1);
740        lu_done(&hdr1); lu_done(&hdr2);
951          if (!ign_header & (report >= REP_WARN)) {
742                if (typ1 == TYP_UNKNOWN)
743                        printf("%s: warning - unrecognized format, comparing as binary\n",
744                                        progname);
952                  if (lin1cnt != lin2cnt)
953                          printf("%s: warning - headers are different lengths\n",
954                                          progname);
955 +                if (typ1 == TYP_UNKNOWN)
956 +                        printf("%s: warning - unrecognized format\n",
957 +                                        progname);
958          }
959          if (report >= REP_VERBOSE)
960                  printf("%s: input file type is %s\n",
# Line 755 | Line 965 | main(int argc, char *argv[])
965          case TYP_TMESH:
966          case TYP_OCTREE:
967          case TYP_RBFMESH:
968 +        case TYP_ID8:
969 +        case TYP_ID16:
970 +        case TYP_ID24:
971          case TYP_UNKNOWN:
972                  return( !compare_binary() );
973          case TYP_TEXT:
# Line 763 | Line 976 | main(int argc, char *argv[])
976          case TYP_RGBE:
977          case TYP_XYZE:
978                  return( !compare_hdr() );
979 +        case TYP_DEPTH:
980 +                return( !compare_depth() );
981 +        case TYP_NORM:
982 +                return( !compare_norm() );
983          case TYP_FLOAT:
984                  return( !compare_float() );
985          case TYP_DOUBLE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines