--- ray/src/util/dctimestep.c 2022/03/11 02:28:51 2.48 +++ ray/src/util/dctimestep.c 2025/06/07 05:09:46 2.55 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: dctimestep.c,v 2.48 2022/03/11 02:28:51 greg Exp $"; +static const char RCSid[] = "$Id: dctimestep.c,v 2.55 2025/06/07 05:09:46 greg Exp $"; #endif /* * Compute time-step result using Daylight Coefficient method. @@ -14,12 +14,11 @@ static const char RCSid[] = "$Id: dctimestep.c,v 2.48 #include "platform.h" #include "resolu.h" -char *progname; /* global argv[0] */ - /* Sum together a set of images and write result to fout */ static int sum_images(const char *fspec, const CMATRIX *cv, FILE *fout) { + static int runcnt = 0; int myDT = DTfromHeader; COLR *scanline = NULL; CMATRIX *pmat = NULL; @@ -28,8 +27,9 @@ sum_images(const char *fspec, const CMATRIX *cv, FILE if (cv->ncols != 1) error(INTERNAL, "expected vector in sum_images()"); - for (i = 0; i < cv->nrows; i++) { - const COLORV *scv = cv_lval(cv,i); + for (i = cv->nrows; i-- > 0; ) { + const int r = runcnt&1 ? i : cv->nrows-1 - i; + const COLORV *scv = cv_lval(cv,r); int flat_file = 0; char fname[1024]; FILE *fp; @@ -39,14 +39,17 @@ sum_images(const char *fspec, const CMATRIX *cv, FILE char *err; /* check for zero */ if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) && - (myDT != DTfromHeader) | (i < cv->nrows-1)) + (myDT != DTfromHeader) | (i > 0)) continue; /* open next picture */ - sprintf(fname, fspec, i); + sprintf(fname, fspec, r); if ((fp = fopen(fname, "rb")) == NULL) { sprintf(errmsg, "cannot open picture '%s'", fname); error(SYSTEM, errmsg); } +#ifdef getc_unlocked + flockfile(fp); +#endif dt = DTfromHeader; if ((err = cm_getheader(&dt, NULL, NULL, NULL, NULL, fp)) != NULL) error(USER, err); @@ -74,7 +77,7 @@ sum_images(const char *fspec, const CMATRIX *cv, FILE } /* 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); + 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); @@ -104,9 +107,39 @@ sum_images(const char *fspec, const CMATRIX *cv, FILE free(scanline); i = cm_write(pmat, myDT, fout); /* write picture */ cm_free(pmat); /* free data */ + ++runcnt; /* for zig-zagging */ 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) @@ -145,8 +178,8 @@ main(int argc, char *argv[]) CMATRIX *cmtx; /* component vector/matrix result */ char fnbuf[256]; int a, i; - - progname = argv[0]; + /* set global progname */ + fixargv0(argv[0]); /* get options */ for (a = 1; a < argc && argv[a][0] == '-'; a++) switch (argv[a][1]) { @@ -215,7 +248,7 @@ main(int argc, char *argv[]) nsteps = smtx->ncols; /* load BSDF */ if (argv[a+1][0] != '!' && - (ccp = strrchr(argv[a+1], '.')) != NULL && + (ccp = strrchr(argv[a+1], '.')) > argv[a+1] && !strcasecmp(ccp+1, "XML")) Tmat = cm_loadBTDF(argv[a+1]); else @@ -235,7 +268,7 @@ main(int argc, char *argv[]) } /* prepare output stream */ if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) { - sprintf(fnbuf, ofspec, 1); + sprintf(fnbuf, ofspec, 0); ofspec = fnbuf; } if (ofspec != NULL && !hasNumberFormat(ofspec)) { @@ -246,7 +279,7 @@ main(int argc, char *argv[]) } ofspec = NULL; /* only need to open once */ } - if (hasNumberFormat(argv[a])) { /* generating image(s) */ + if (hasNumberFormat(argv[a])) { /* loading image vector(s) */ if (outfmt != DTrgbe) { error(WARNING, "changing output type to -oc"); outfmt = DTrgbe; @@ -288,7 +321,7 @@ main(int argc, char *argv[]) } else if (!sum_images(argv[a], cmtx, ofp)) return(1); - } else { /* generating vector/matrix */ + } else { /* loading view matrix */ CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader); CMATRIX *rmtx = cm_multiply(Vmat, cmtx); cm_free(Vmat); @@ -296,27 +329,8 @@ main(int argc, char *argv[]) const char *wtype = (outfmt==DTascii) ? "w" : "wb"; for (i = 0; i < nsteps; i++) { CMATRIX *rvec = cm_column(rmtx, i); - 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; - } + if (alt_dim(rvec, yres, xres) < 0) + return(1); sprintf(fnbuf, ofspec, i); if ((ofp = fopen(fnbuf, wtype)) == NULL) { fprintf(stderr, @@ -335,7 +349,7 @@ main(int argc, char *argv[]) if ((outfmt != DTrgbe) & (outfmt != DTxyze)) { fprintf(ofp, "NROWS=%d\n", rvec->nrows); fprintf(ofp, "NCOLS=%d\n", rvec->ncols); - fputs("NCOMP=3\n", ofp); + fputncomp(3, ofp); } if ((outfmt == DTfloat) | (outfmt == DTdouble)) fputendian(ofp); @@ -358,6 +372,8 @@ 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); @@ -365,7 +381,7 @@ main(int argc, char *argv[]) if ((outfmt != DTrgbe) & (outfmt != DTxyze)) { fprintf(ofp, "NROWS=%d\n", rmtx->nrows); fprintf(ofp, "NCOLS=%d\n", rmtx->ncols); - fputs("NCOMP=3\n", ofp); + fputncomp(3, ofp); } if ((outfmt == DTfloat) | (outfmt == DTdouble)) fputendian(ofp);