| 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 */ | 
| 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 |  |  | 
| 73 |  | "SOFTWARE", | 
| 74 |  | "CAPDATE", | 
| 75 |  | "GMT", | 
| 76 | + | "FRAME", | 
| 77 |  | NULL    /* terminator */ | 
| 78 |  | }; | 
| 79 |  | /* header variable settings */ | 
| 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; | 
| 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; | 
| 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]*nv2[2]; | 
| 228 | + | int     imax = 2; | 
| 229 | + | int     i = 2; | 
| 230 | + | /* identify largest component */ | 
| 231 | + | while (i--) { | 
| 232 | + | double  tm2 = nv1[i]*nv2[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) | 
| 317 |  | static int | 
| 318 |  | setheadvar(char *val, void *p) | 
| 319 |  | { | 
| 320 | + | char    newval[128]; | 
| 321 |  | LUTAB   *htp = (LUTAB *)p; | 
| 322 |  | LUENT   *tep; | 
| 323 |  | char    *key; | 
| 327 |  | adv_linecnt(htp);       /* side-effect is to count lines */ | 
| 328 |  | if (!isalpha(*val))     /* key must start line */ | 
| 329 |  | return(0); | 
| 330 | + | /* check if we need to swap binary data */ | 
| 331 | + | if ((n = isbigendian(val)) >= 0) { | 
| 332 | + | if (nativebigendian() == n) | 
| 333 | + | return(0); | 
| 334 | + | f1swap += (htp == &hdr1); | 
| 335 | + | f2swap += (htp == &hdr2); | 
| 336 | + | return(0); | 
| 337 | + | } | 
| 338 |  | key = val++; | 
| 339 |  | while (*val && !isspace(*val) & (*val != '=')) | 
| 340 |  | val++; | 
| 360 |  | return(-1);     /* memory allocation error */ | 
| 361 |  | if (!tep->key) | 
| 362 |  | tep->key = strcpy(malloc(kln+1), key); | 
| 363 | < | if (tep->data) | 
| 363 | > | if (tep->data) {        /* check for special cases */ | 
| 364 | > | if (!strcmp(key, "EXPOSURE")) { | 
| 365 | > | sprintf(newval, "%f", atof(tep->data)*atof(val)); | 
| 366 | > | vln = strlen(val = newval); | 
| 367 | > | } | 
| 368 |  | free(tep->data); | 
| 369 | + | } | 
| 370 |  | tep->data = strcpy(malloc(vln+1), val); | 
| 371 |  | return(1); | 
| 372 |  | } | 
| 614 |  | return(good_RMS());                     /* final check for reals */ | 
| 615 |  | } | 
| 616 |  |  | 
| 617 | + | /* Check image/map resolutions */ | 
| 618 | + | static int | 
| 619 | + | check_resolu(const char *class, RESOLU *r1p, RESOLU *r2p) | 
| 620 | + | { | 
| 621 | + | if (r1p->rt != r2p->rt) { | 
| 622 | + | if (report != REP_QUIET) | 
| 623 | + | printf( | 
| 624 | + | "%s: %ss '%s' and '%s' have different pixel ordering\n", | 
| 625 | + | progname, class, f1name, f2name); | 
| 626 | + | return(0); | 
| 627 | + | } | 
| 628 | + | if ((r1p->xr != r2p->xr) | (r1p->yr != r2p->yr)) { | 
| 629 | + | if (report != REP_QUIET) | 
| 630 | + | printf( | 
| 631 | + | "%s: %ss '%s' and '%s' are different sizes\n", | 
| 632 | + | progname, class, f1name, f2name); | 
| 633 | + | return(0); | 
| 634 | + | } | 
| 635 | + | return(1); | 
| 636 | + | } | 
| 637 | + |  | 
| 638 |  | /* Compare two inputs that are known to be RGBE or XYZE images */ | 
| 639 |  | static int | 
| 640 |  | compare_hdr() | 
| 649 |  | } | 
| 650 |  | fgetsresolu(&rs1, f1in); | 
| 651 |  | fgetsresolu(&rs2, f2in); | 
| 652 | < | 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); | 
| 652 | > | if (!check_resolu("HDR image", &rs1, &rs2)) | 
| 653 |  | 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 | – | } | 
| 654 |  | scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR)); | 
| 655 |  | scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR)); | 
| 656 |  | if (!scan1 | !scan2) { | 
| 695 |  | return(good_RMS());                     /* final check of RMS */ | 
| 696 |  | } | 
| 697 |  |  | 
| 698 | + | /* Set reference depth based on header variable */ | 
| 699 | + | static int | 
| 700 | + | set_refdepth(DEPTHCODEC *dcp, LUTAB *htp) | 
| 701 | + | { | 
| 702 | + | static char     depthvar[] = DEPTHSTR; | 
| 703 | + | const char      *drval; | 
| 704 | + |  | 
| 705 | + | depthvar[LDEPTHSTR-1] = '\0'; | 
| 706 | + | drval = (const char *)lu_find(htp, depthvar)->data; | 
| 707 | + | if (!drval) | 
| 708 | + | return(0); | 
| 709 | + | dcp->refdepth = atof(drval); | 
| 710 | + | if (dcp->refdepth <= 0) { | 
| 711 | + | if (report != REP_QUIET) { | 
| 712 | + | fputs(dcp->inpname, stderr); | 
| 713 | + | fputs(": bad reference depth '", stderr); | 
| 714 | + | fputs(drval, stderr); | 
| 715 | + | fputs("'\n", stderr); | 
| 716 | + | } | 
| 717 | + | return(-1); | 
| 718 | + | } | 
| 719 | + | return(1); | 
| 720 | + | } | 
| 721 | + |  | 
| 722 | + | /* Compare two encoded depth maps */ | 
| 723 | + | static int | 
| 724 | + | compare_depth() | 
| 725 | + | { | 
| 726 | + | long            nread = 0; | 
| 727 | + | DEPTHCODEC      dc1, dc2; | 
| 728 | + |  | 
| 729 | + | if (report >= REP_VERBOSE) { | 
| 730 | + | fputs(progname, stdout); | 
| 731 | + | fputs(": comparing inputs as depth maps\n", stdout); | 
| 732 | + | } | 
| 733 | + | set_dc_defaults(&dc1); | 
| 734 | + | dc1.hdrflags = HF_RESIN; | 
| 735 | + | dc1.finp = f1in; | 
| 736 | + | dc1.inpname = f1name; | 
| 737 | + | set_dc_defaults(&dc2); | 
| 738 | + | dc2.hdrflags = HF_RESIN; | 
| 739 | + | dc2.finp = f2in; | 
| 740 | + | dc2.inpname = f2name; | 
| 741 | + | if (report != REP_QUIET) { | 
| 742 | + | dc1.hdrflags |= HF_STDERR; | 
| 743 | + | dc2.hdrflags |= HF_STDERR; | 
| 744 | + | } | 
| 745 | + | if (!process_dc_header(&dc1, 0, NULL)) | 
| 746 | + | return(0); | 
| 747 | + | if (!process_dc_header(&dc2, 0, NULL)) | 
| 748 | + | return(0); | 
| 749 | + | if (!check_resolu("Depth map", &dc1.res, &dc2.res)) | 
| 750 | + | return(0); | 
| 751 | + | if (set_refdepth(&dc1, &hdr1) < 0) | 
| 752 | + | return(0); | 
| 753 | + | if (set_refdepth(&dc2, &hdr2) < 0) | 
| 754 | + | return(0); | 
| 755 | + | while (nread < dc1.res.xr*dc1.res.yr) { | 
| 756 | + | double  d1 = decode_depth_next(&dc1); | 
| 757 | + | double  d2 = decode_depth_next(&dc2); | 
| 758 | + | if ((d1 < 0) | (d2 < 0)) { | 
| 759 | + | if (report != REP_QUIET) | 
| 760 | + | printf("%s: unexpected end-of-file\n", | 
| 761 | + | progname); | 
| 762 | + | return(0); | 
| 763 | + | } | 
| 764 | + | ++nread; | 
| 765 | + | if (real_check(d1, d2)) | 
| 766 | + | continue; | 
| 767 | + | if (report != REP_QUIET) | 
| 768 | + | printf("%s: %ld%s depth values differ\n", | 
| 769 | + | progname, nread, num_sfx(nread)); | 
| 770 | + | return(0); | 
| 771 | + | } | 
| 772 | + | return(good_RMS());             /* final check of RMS */ | 
| 773 | + | } | 
| 774 | + |  | 
| 775 | + | /* Compare two encoded normal maps */ | 
| 776 | + | static int | 
| 777 | + | compare_norm() | 
| 778 | + | { | 
| 779 | + | long            nread = 0; | 
| 780 | + | NORMCODEC       nc1, nc2; | 
| 781 | + |  | 
| 782 | + | if (report >= REP_VERBOSE) { | 
| 783 | + | fputs(progname, stdout); | 
| 784 | + | fputs(": comparing inputs as normal maps\n", stdout); | 
| 785 | + | } | 
| 786 | + | set_nc_defaults(&nc1); | 
| 787 | + | nc1.hdrflags = HF_RESIN; | 
| 788 | + | nc1.finp = f1in; | 
| 789 | + | nc1.inpname = f1name; | 
| 790 | + | set_nc_defaults(&nc2); | 
| 791 | + | nc2.hdrflags = HF_RESIN; | 
| 792 | + | nc2.finp = f2in; | 
| 793 | + | nc2.inpname = f2name; | 
| 794 | + | if (report != REP_QUIET) { | 
| 795 | + | nc1.hdrflags |= HF_STDERR; | 
| 796 | + | nc2.hdrflags |= HF_STDERR; | 
| 797 | + | } | 
| 798 | + | if (!process_nc_header(&nc1, 0, NULL)) | 
| 799 | + | return(0); | 
| 800 | + | if (!process_nc_header(&nc2, 0, NULL)) | 
| 801 | + | return(0); | 
| 802 | + | if (!check_resolu("Normal map", &nc1.res, &nc2.res)) | 
| 803 | + | return(0); | 
| 804 | + | while (nread < nc1.res.xr*nc1.res.yr) { | 
| 805 | + | FVECT   nv1, nv2; | 
| 806 | + | int     rv1 = decode_normal_next(nv1, &nc1); | 
| 807 | + | int     rv2 = decode_normal_next(nv2, &nc2); | 
| 808 | + | if ((rv1 < 0) | (rv2 < 0)) { | 
| 809 | + | if (report != REP_QUIET) | 
| 810 | + | printf("%s: unexpected end-of-file\n", | 
| 811 | + | progname); | 
| 812 | + | return(0); | 
| 813 | + | } | 
| 814 | + | ++nread; | 
| 815 | + | if (rv1 == rv2 && (!rv1 || norm_check(nv1, nv2))) | 
| 816 | + | continue; | 
| 817 | + | if (report != REP_QUIET) | 
| 818 | + | printf("%s: %ld%s normal vectors differ\n", | 
| 819 | + | progname, nread, num_sfx(nread)); | 
| 820 | + | return(0); | 
| 821 | + | } | 
| 822 | + | return(good_RMS());             /* final check of RMS */ | 
| 823 | + | } | 
| 824 | + |  | 
| 825 |  | /* Compare two inputs that are known to be 32-bit floating-point data */ | 
| 826 |  | static int | 
| 827 |  | compare_float() | 
| 837 |  | if (!getbinary(&f2, sizeof(f2), 1, f2in)) | 
| 838 |  | goto badeof; | 
| 839 |  | ++nread; | 
| 840 | + | if (f1swap) swap32((char *)&f1, 1); | 
| 841 | + | if (f2swap) swap32((char *)&f2, 1); | 
| 842 |  | if (real_check(f1, f2)) | 
| 843 |  | continue; | 
| 844 |  | if (report != REP_QUIET) | 
| 869 |  | if (!getbinary(&f2, sizeof(f2), 1, f2in)) | 
| 870 |  | goto badeof; | 
| 871 |  | ++nread; | 
| 872 | + | if (f1swap) swap64((char *)&f1, 1); | 
| 873 | + | if (f2swap) swap64((char *)&f2, 1); | 
| 874 |  | if (real_check(f1, f2)) | 
| 875 |  | continue; | 
| 876 |  | if (report != REP_QUIET) | 
| 959 |  | return(2); | 
| 960 |  | if (typ1 != typ2) { | 
| 961 |  | if (report != REP_QUIET) | 
| 962 | < | printf("%s: '%s' is %s and '%s' is %s\n", | 
| 962 | > | printf("%s: '%s' format is %s and '%s' is %s\n", | 
| 963 |  | progname, f1name, file_type[typ1], | 
| 964 |  | f2name, file_type[typ2]); | 
| 965 |  | return(1); | 
| 967 |  | ign_header |= !has_header(typ1);        /* check headers if indicated */ | 
| 968 |  | if (!ign_header && !headers_match()) | 
| 969 |  | return(1); | 
| 780 | – | lu_done(&hdr1); lu_done(&hdr2);         /* done with header info. */ | 
| 970 |  | if (!ign_header & (report >= REP_WARN)) { | 
| 971 |  | if (lin1cnt != lin2cnt) | 
| 972 |  | printf("%s: warning - headers are different lengths\n", | 
| 975 |  | printf("%s: warning - unrecognized format\n", | 
| 976 |  | progname); | 
| 977 |  | } | 
| 978 | < | if (report >= REP_VERBOSE) | 
| 979 | < | printf("%s: input file type is %s\n", | 
| 980 | < | progname, file_type[typ1]); | 
| 981 | < |  | 
| 978 | > | if (report >= REP_VERBOSE) { | 
| 979 | > | printf("%s: data format is %s\n", progname, file_type[typ1]); | 
| 980 | > | if ((typ1 == TYP_FLOAT) | (typ1 == TYP_DOUBLE)) { | 
| 981 | > | if (f1swap) | 
| 982 | > | printf("%s: input '%s' is byte-swapped\n", | 
| 983 | > | progname, f1name); | 
| 984 | > | if (f2swap) | 
| 985 | > | printf("%s: input '%s' is byte-swapped\n", | 
| 986 | > | progname, f2name); | 
| 987 | > | } | 
| 988 | > | } | 
| 989 |  | switch (typ1) {                         /* compare based on type */ | 
| 990 |  | case TYP_BINARY: | 
| 991 |  | case TYP_TMESH: | 
| 992 |  | case TYP_OCTREE: | 
| 993 |  | case TYP_RBFMESH: | 
| 994 | + | case TYP_ID8: | 
| 995 | + | case TYP_ID16: | 
| 996 | + | case TYP_ID24: | 
| 997 |  | case TYP_UNKNOWN: | 
| 998 |  | return( !compare_binary() ); | 
| 999 |  | case TYP_TEXT: | 
| 1002 |  | case TYP_RGBE: | 
| 1003 |  | case TYP_XYZE: | 
| 1004 |  | return( !compare_hdr() ); | 
| 1005 | + | case TYP_DEPTH: | 
| 1006 | + | return( !compare_depth() ); | 
| 1007 | + | case TYP_NORM: | 
| 1008 | + | return( !compare_norm() ); | 
| 1009 |  | case TYP_FLOAT: | 
| 1010 |  | return( !compare_float() ); | 
| 1011 |  | case TYP_DOUBLE: |