--- ray/src/util/dctimestep.c 2014/02/08 01:28:06 2.30 +++ ray/src/util/dctimestep.c 2022/03/11 02:28:51 2.48 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: dctimestep.c,v 2.30 2014/02/08 01:28:06 greg Exp $"; +static const char RCSid[] = "$Id: dctimestep.c,v 2.48 2022/03/11 02:28:51 greg Exp $"; #endif /* * Compute time-step result using Daylight Coefficient method. @@ -8,6 +8,7 @@ static const char RCSid[] = "$Id: dctimestep.c,v 2.30 */ #include +#include "platform.h" #include "standard.h" #include "cmatrix.h" #include "platform.h" @@ -20,7 +21,7 @@ static int sum_images(const char *fspec, const CMATRIX *cv, FILE *fout) { int myDT = DTfromHeader; - COLOR *scanline = NULL; + COLR *scanline = NULL; CMATRIX *pmat = NULL; int myXR=0, myYR=0; int i, y; @@ -29,22 +30,26 @@ sum_images(const char *fspec, const CMATRIX *cv, FILE error(INTERNAL, "expected vector in sum_images()"); for (i = 0; i < cv->nrows; i++) { const COLORV *scv = cv_lval(cv,i); + int flat_file = 0; char fname[1024]; FILE *fp; + long data_start; int dt, xr, yr; COLORV *psp; + char *err; /* check for zero */ if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) && (myDT != DTfromHeader) | (i < cv->nrows-1)) continue; /* open next picture */ sprintf(fname, fspec, i); - if ((fp = fopen(fname, "r")) == NULL) { + if ((fp = fopen(fname, "rb")) == NULL) { sprintf(errmsg, "cannot open picture '%s'", fname); error(SYSTEM, errmsg); } - SET_FILE_BINARY(fp); - dt = getDTfromHeader(fp); + dt = DTfromHeader; + if ((err = cm_getheader(&dt, NULL, NULL, NULL, NULL, fp)) != NULL) + error(USER, err); if ((dt != DTrgbe) & (dt != DTxyze) || !fscnresolu(&xr, &yr, fp)) { sprintf(errmsg, "file '%s' not a picture", fname); @@ -53,13 +58,13 @@ sum_images(const char *fspec, const CMATRIX *cv, FILE if (myDT == DTfromHeader) { /* on first one */ myDT = dt; myXR = xr; myYR = yr; - scanline = (COLOR *)malloc(sizeof(COLOR)*myXR); + scanline = (COLR *)malloc(sizeof(COLR)*myXR); if (scanline == NULL) error(SYSTEM, "out of memory in sum_images()"); pmat = cm_alloc(myYR, myXR); memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR); /* finish header */ - fputformat((char *)cm_fmt_id[myDT], fout); + fputformat(cm_fmt_id[myDT], fout); fputc('\n', fout); fflush(fout); } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) { @@ -67,18 +72,31 @@ sum_images(const char *fspec, const CMATRIX *cv, FILE fname); error(USER, errmsg); } + /* flat file check */ + if ((data_start = ftell(fp)) > 0 && fseek(fp, 0L, SEEK_END) == 0) { + flat_file = (ftell(fp) == data_start + sizeof(COLR)*xr*yr); + if (fseek(fp, data_start, SEEK_SET) < 0) { + sprintf(errmsg, "cannot seek on picture '%s'", fname); + error(SYSTEM, errmsg); + } + } psp = pmat->cmem; for (y = 0; y < yr; y++) { /* read it in */ + COLOR col; int x; - if (freadscan(scanline, xr, fp) < 0) { + if (flat_file ? getbinary(scanline, sizeof(COLR), xr, fp) != xr : + freadcolrs(scanline, xr, fp) < 0) { sprintf(errmsg, "error reading picture '%s'", fname); error(SYSTEM, errmsg); } /* sum in scanline */ for (x = 0; x < xr; x++, psp += 3) { - multcolor(scanline[x], scv); - addcolor(psp, scanline[x]); + if (!scanline[x][EXP]) + continue; /* skip zeroes */ + colr_color(col, scanline[x]); + multcolor(col, scv); + addcolor(psp, col); } } fclose(fp); /* done this picture */ @@ -117,11 +135,13 @@ hasNumberFormat(const char *s) int main(int argc, char *argv[]) { - int skyfmt = DTascii; + int skyfmt = DTfromHeader; int outfmt = DTascii; - int nsteps = 1; + int headout = 1; + int nsteps = 0; char *ofspec = NULL; FILE *ofp = stdout; + int xres=0, yres=0; CMATRIX *cmtx; /* component vector/matrix result */ char fnbuf[256]; int a, i; @@ -132,9 +152,13 @@ main(int argc, char *argv[]) switch (argv[a][1]) { case 'n': nsteps = atoi(argv[++a]); - if (nsteps <= 0) + if (nsteps < 0) goto userr; + skyfmt = nsteps ? DTascii : DTfromHeader; break; + case 'h': + headout = !headout; + break; case 'i': switch (argv[a][2]) { case 'f': @@ -164,10 +188,19 @@ main(int argc, char *argv[]) case 'a': outfmt = DTascii; break; + case 'c': + outfmt = DTrgbe; + break; default: goto userr; } break; + case 'x': + xres = atoi(argv[++a]); + break; + case 'y': + yres = atoi(argv[++a]); + break; default: goto userr; } @@ -175,13 +208,20 @@ main(int argc, char *argv[]) goto userr; if (argc-a > 2) { /* VTDs expression */ - CMATRIX *smtx, *Dmat, *Tmat, *imtx; + CMATRIX *smtx, *Dmat, *Tmat, *imtx; + const char *ccp; /* get sky vector/matrix */ smtx = cm_load(argv[a+3], 0, nsteps, skyfmt); + nsteps = smtx->ncols; /* load BSDF */ - Tmat = cm_loadBTDF(argv[a+1]); + if (argv[a+1][0] != '!' && + (ccp = strrchr(argv[a+1], '.')) != NULL && + !strcasecmp(ccp+1, "XML")) + Tmat = cm_loadBTDF(argv[a+1]); + else + Tmat = cm_load(argv[a+1], 0, 0, DTfromHeader); /* load Daylight matrix */ - Dmat = cm_load(argv[a+2], Tmat==NULL ? 0 : Tmat->ncols, + Dmat = cm_load(argv[a+2], Tmat->ncols, smtx->nrows, DTfromHeader); /* multiply vector through */ imtx = cm_multiply(Dmat, smtx); @@ -191,6 +231,7 @@ main(int argc, char *argv[]) cm_free(imtx); } else { /* sky vector/matrix only */ cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt); + nsteps = cmtx->ncols; } /* prepare output stream */ if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) { @@ -206,6 +247,10 @@ main(int argc, char *argv[]) ofspec = NULL; /* only need to open once */ } if (hasNumberFormat(argv[a])) { /* generating image(s) */ + if (outfmt != DTrgbe) { + error(WARNING, "changing output type to -oc"); + outfmt = DTrgbe; + } if (ofspec == NULL) { SET_FILE_BINARY(ofp); newheader("RADIANCE", ofp); @@ -216,7 +261,7 @@ main(int argc, char *argv[]) for (i = 0; i < nsteps; i++) { CMATRIX *cvec = cm_column(cmtx, i); if (ofspec != NULL) { - sprintf(fnbuf, ofspec, i+1); + sprintf(fnbuf, ofspec, i); if ((ofp = fopen(fnbuf, "wb")) == NULL) { fprintf(stderr, "%s: cannot open '%s' for output\n", @@ -227,7 +272,7 @@ main(int argc, char *argv[]) printargs(argc, argv, ofp); fputnow(ofp); } - fprintf(ofp, "FRAME=%d\n", i+1); + fprintf(ofp, "FRAME=%d\n", i); if (!sum_images(argv[a], cvec, ofp)) return(1); if (ofspec != NULL) { @@ -251,13 +296,52 @@ main(int argc, char *argv[]) const char *wtype = (outfmt==DTascii) ? "w" : "wb"; for (i = 0; i < nsteps; i++) { CMATRIX *rvec = cm_column(rmtx, i); - sprintf(fnbuf, ofspec, i+1); + if (yres > 0) { + if (xres <= 0) + xres = rvec->nrows/yres; + if (xres*yres != rvec->nrows) { + fprintf(stderr, "Bad resolution: %d != %dx%d\n", + rvec->nrows, xres, yres); + return(1); + } + rvec->nrows = yres; + rvec->ncols = xres; + } else if (xres > 0) { + yres = rvec->nrows/xres; + if (xres*yres != rvec->nrows) { + fprintf(stderr, + "Bad resolution: %d does not divide %d evenly\n", + xres, rvec->nrows); + return(1); + } + rvec->nrows = yres; + rvec->ncols = xres; + } + sprintf(fnbuf, ofspec, i); if ((ofp = fopen(fnbuf, wtype)) == NULL) { fprintf(stderr, "%s: cannot open '%s' for output\n", progname, fnbuf); return(1); } +#ifdef getc_unlocked + flockfile(ofp); +#endif + if (headout) { /* header output */ + newheader("RADIANCE", ofp); + printargs(argc, argv, ofp); + fputnow(ofp); + fprintf(ofp, "FRAME=%d\n", i); + if ((outfmt != DTrgbe) & (outfmt != DTxyze)) { + fprintf(ofp, "NROWS=%d\n", rvec->nrows); + fprintf(ofp, "NCOLS=%d\n", rvec->ncols); + fputs("NCOMP=3\n", ofp); + } + if ((outfmt == DTfloat) | (outfmt == DTdouble)) + fputendian(ofp); + fputformat(cm_fmt_id[outfmt], ofp); + fputc('\n', ofp); + } cm_write(rvec, outfmt, ofp); if (fclose(ofp) == EOF) { fprintf(stderr, @@ -269,13 +353,23 @@ main(int argc, char *argv[]) cm_free(rvec); } } else { +#ifdef getc_unlocked + flockfile(ofp); +#endif if (outfmt != DTascii) SET_FILE_BINARY(ofp); - if (rmtx->ncols > 1) { /* header if actual matrix */ + if (headout) { /* header output */ newheader("RADIANCE", ofp); printargs(argc, argv, ofp); fputnow(ofp); - fputformat((char *)cm_fmt_id[outfmt], ofp); + if ((outfmt != DTrgbe) & (outfmt != DTxyze)) { + fprintf(ofp, "NROWS=%d\n", rmtx->nrows); + fprintf(ofp, "NCOLS=%d\n", rmtx->ncols); + fputs("NCOMP=3\n", ofp); + } + if ((outfmt == DTfloat) | (outfmt == DTdouble)) + fputendian(ofp); + fputformat(cm_fmt_id[outfmt], ofp); fputc('\n', ofp); } cm_write(rmtx, outfmt, ofp); @@ -289,9 +383,9 @@ main(int argc, char *argv[]) cm_free(cmtx); return(0); userr: - fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d}][-o{f|d}] DCspec [skyf]\n", + fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-x xr][-y yr][-i{f|d|h}][-o{f|d|c}] DCspec [skyf]\n", progname); - fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d}][-o{f|d}] Vspec Tbsdf.xml Dmat.dat [skyf]\n", + fprintf(stderr, " or: %s [-n nsteps][-o ospec][-x xr][-y yr][-i{f|d|h}][-o{f|d|c}] Vspec Tbsdf Dmat.dat [skyf]\n", progname); return(1); }