--- ray/src/cv/bsdf2klems.c 2019/12/28 18:05:14 2.28 +++ ray/src/cv/bsdf2klems.c 2023/07/26 15:50:03 2.35 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdf2klems.c,v 2.28 2019/12/28 18:05:14 greg Exp $"; +static const char RCSid[] = "$Id: bsdf2klems.c,v 2.35 2023/07/26 15:50:03 greg Exp $"; #endif /* * Load measured BSDF interpolant and write out as XML file with Klems matrix. @@ -10,6 +10,7 @@ static const char RCSid[] = "$Id: bsdf2klems.c,v 2.28 #define _USE_MATH_DEFINES #include #include +#include #include "random.h" #include "platform.h" #include "paths.h" @@ -195,7 +196,7 @@ eval_bsdf(const char *fname) for (n = npsamps; n-- > 0; ) { fo_getvec(vout, j+(n+frandom())/npsamps, abp); fi_getvec(vin, i+urand(n), abp); - ec = SDevalBSDF(&sdv, vout, vin, &bsd); + ec = SDevalBSDF(&sdv, vin, vout, &bsd); if (ec != SDEnone) goto err; sum += sdv.cieY; @@ -244,7 +245,7 @@ eval_bsdf(const char *fname) for (n = npsamps; n-- > 0; ) { bo_getvec(vout, j+(n+frandom())/npsamps, abp); bi_getvec(vin, i+urand(n), abp); - ec = SDevalBSDF(&sdv, vout, vin, &bsd); + ec = SDevalBSDF(&sdv, vin, vout, &bsd); if (ec != SDEnone) goto err; sum += sdv.cieY; @@ -276,7 +277,7 @@ eval_bsdf(const char *fname) } } /* front transmission */ - if (bsd.tf != NULL || bsd.tLamb.cieY > .002) { + if (bsd.tf != NULL || bsd.tLambFront.cieY > .002) { input_orient = 1; output_orient = -1; cfp[CIE_Y] = open_component_file(CIE_Y); if (bsd.tf != NULL && bsd.tf->comp[0].cspec[2].flags) { @@ -292,7 +293,7 @@ eval_bsdf(const char *fname) for (n = npsamps; n-- > 0; ) { bo_getvec(vout, j+(n+frandom())/npsamps, abp); fi_getvec(vin, i+urand(n), abp); - ec = SDevalBSDF(&sdv, vout, vin, &bsd); + ec = SDevalBSDF(&sdv, vin, vout, &bsd); if (ec != SDEnone) goto err; sum += sdv.cieY; @@ -341,7 +342,7 @@ eval_bsdf(const char *fname) for (n = npsamps; n-- > 0; ) { fo_getvec(vout, j+(n+frandom())/npsamps, abp); bi_getvec(vin, i+urand(n), abp); - ec = SDevalBSDF(&sdv, vout, vin, &bsd); + ec = SDevalBSDF(&sdv, vin, vout, &bsd); if (ec != SDEnone) goto err; sum += sdv.cieY; @@ -524,7 +525,7 @@ eval_rbf(void) static int wrap_up(void) { - char cmd[8192]; + char cmd[32700]; if (bsdf_manuf[0]) { add_wbsdf("-f", 1); @@ -575,12 +576,49 @@ wrap_up(void) } #endif +#define HEAD_BUFLEN 10240 +static char head_buf[HEAD_BUFLEN]; +static int cur_headlen = 0; + +/* Record header line as comment associated with this SIR input */ +static int +record2header(char *s) +{ + int len = strlen(s); + + if (cur_headlen+len >= HEAD_BUFLEN-6) + return(0); + /* includes EOL */ + strcpy(head_buf+cur_headlen, s); + cur_headlen += len; + +#if defined(_WIN32) || defined(_WIN64) + if (head_buf[cur_headlen-1] == '\n') + head_buf[cur_headlen-1] = '\t'; +#endif + return(1); +} + +/* Finish off header for this file */ +static void +done_header(void) +{ + while (cur_headlen > 0 && isspace(head_buf[cur_headlen-1])) + --cur_headlen; + head_buf[cur_headlen] = '\0'; + if (!cur_headlen) + return; + add_wbsdf("-C", 1); + add_wbsdf(head_buf, 0); + head_buf[cur_headlen=0] = '\0'; +} + /* Read in BSDF and interpolate as Klems matrix representation */ int main(int argc, char *argv[]) { int dofwd = 0, dobwd = 1; - char buf[2048]; + char buf[1024]; char *cp; int i, na; @@ -601,7 +639,7 @@ main(int argc, char *argv[]) single_plane_incident = 0; break; case 'f': - if (!argv[i][2]) { + if ((argv[i][0] == '-') & !argv[i][2]) { if (strchr(argv[++i], '=') != NULL) { add_wbsdf("-f", 1); add_wbsdf(argv[i], 1); @@ -692,25 +730,31 @@ main(int argc, char *argv[]) if (i < argc) { /* open input files if given */ int nbsdf = 0; for ( ; i < argc; i++) { /* interpolate each component */ - char pbuf[256]; FILE *fpin = fopen(argv[i], "rb"); if (fpin == NULL) { fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", progname, argv[i]); return(1); } + sprintf(buf, "%s:\n", argv[i]); + record2header(buf); + sir_headshare = &record2header; if (!load_bsdf_rep(fpin)) return(1); fclose(fpin); - sprintf(pbuf, "Interpolating component '%s'", argv[i]); - prog_start(pbuf); + done_header(); + sprintf(buf, "Interpolating component '%s'", argv[i]); + prog_start(buf); eval_rbf(); } return(wrap_up()); } SET_FILE_BINARY(stdin); /* load from stdin */ + record2header(":\n"); + sir_headshare = &record2header; if (!load_bsdf_rep(stdin)) return(1); + done_header(); prog_start("Interpolating from standard input"); eval_rbf(); /* resample dist. */ return(wrap_up());