--- ray/src/util/dctimestep.c 2018/04/10 23:22:42 2.38 +++ ray/src/util/dctimestep.c 2022/03/11 02:44:33 2.49 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: dctimestep.c,v 2.38 2018/04/10 23:22:42 greg Exp $"; +static const char RCSid[] = "$Id: dctimestep.c,v 2.49 2022/03/11 02:44:33 greg Exp $"; #endif /* * Compute time-step result using Daylight Coefficient method. @@ -30,8 +30,10 @@ 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; @@ -46,7 +48,7 @@ sum_images(const char *fspec, const CMATRIX *cv, FILE error(SYSTEM, errmsg); } dt = DTfromHeader; - if ((err = cm_getheader(&dt, NULL, NULL, fp)) != NULL) + if ((err = cm_getheader(&dt, NULL, NULL, NULL, NULL, fp)) != NULL) error(USER, err); if ((dt != DTrgbe) & (dt != DTxyze) || !fscnresolu(&xr, &yr, fp)) { @@ -62,7 +64,7 @@ sum_images(const char *fspec, const CMATRIX *cv, FILE 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)) { @@ -70,11 +72,20 @@ 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 (freadcolrs(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); @@ -96,6 +107,35 @@ sum_images(const char *fspec, const CMATRIX *cv, FILE return(i); } +/* adjust matrix dimensions according to user size(s) */ +static int +alt_dim(CMATRIX *cm, int nr, int nc) +{ + if ((nr <= 0) & (nc <= 0)) + return(0); + if ((nr == cm->nrows) & (nc == cm->ncols)) + return(0); + if (nr > 0) { + if (nc <= 0) + nc = cm->nrows*cm->ncols/nr; + if (nr*nc != cm->nrows*cm->ncols) { + fprintf(stderr, "Bad dimensions: %dx%d != %dx%d\n", + nr, nc, cm->nrows, cm->ncols); + return(-1); + } + } else /* nc > 0 */ { + nr = cm->nrows*cm->ncols/nc; + if (nc*nr != cm->nrows*cm->ncols) { + fprintf(stderr, "Bad dimensions: %d does not divide %dx%d evenly\n", + nc, cm->nrows, cm->ncols); + return(-1); + } + } + cm->nrows = nr; + cm->ncols = nc; + return(1); +} + /* check to see if a string contains a %d or %o specification */ static int hasNumberFormat(const char *s) @@ -130,6 +170,7 @@ main(int argc, char *argv[]) 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; @@ -176,10 +217,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; } @@ -226,6 +276,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); @@ -236,7 +290,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", @@ -247,7 +301,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) { @@ -271,7 +325,9 @@ 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 (alt_dim(rvec, yres, xres) < 0) + return(1); + sprintf(fnbuf, ofspec, i); if ((ofp = fopen(fnbuf, wtype)) == NULL) { fprintf(stderr, "%s: cannot open '%s' for output\n", @@ -285,10 +341,15 @@ main(int argc, char *argv[]) newheader("RADIANCE", ofp); printargs(argc, argv, ofp); fputnow(ofp); - fprintf(ofp, "FRAME=%d\n", i+1); - fprintf(ofp, "NROWS=%d\n", rvec->nrows); - fputs("NCOLS=1\nNCOMP=3\n", ofp); - fputformat((char *)cm_fmt_id[outfmt], 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); @@ -307,14 +368,20 @@ main(int argc, char *argv[]) #endif if (outfmt != DTascii) SET_FILE_BINARY(ofp); + if (alt_dim(rmtx, yres, xres) < 0) + return(1); if (headout) { /* header output */ newheader("RADIANCE", ofp); printargs(argc, argv, ofp); fputnow(ofp); - fprintf(ofp, "NROWS=%d\n", rmtx->nrows); - fprintf(ofp, "NCOLS=%d\n", rmtx->ncols); - fputs("NCOMP=3\n", 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); @@ -328,9 +395,9 @@ main(int argc, char *argv[]) cm_free(cmtx); return(0); userr: - fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d|h}][-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|h}][-o{f|d}] Vspec Tbsdf 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); }