#ifndef lint static const char RCSid[] = "$Id: dctimestep.c,v 2.28 2014/01/20 21:29:04 greg Exp $"; #endif /* * Compute time-step result using Daylight Coefficient method. * * G. Ward */ #include #include "standard.h" #include "cmatrix.h" #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) { int myDT = DTfromHeader; COLOR *scanline = NULL; CMATRIX *pmat = NULL; int myXR=0, myYR=0; int i, y; 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); char fname[1024]; FILE *fp; int dt, xr, yr; COLORV *psp; /* 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) { sprintf(errmsg, "cannot open picture '%s'", fname); error(SYSTEM, errmsg); } SET_FILE_BINARY(fp); dt = getDTfromHeader(fp); if ((dt != DTrgbe) & (dt != DTxyze) || !fscnresolu(&xr, &yr, fp)) { sprintf(errmsg, "file '%s' not a picture", fname); error(USER, errmsg); } if (myDT == DTfromHeader) { /* on first one */ myDT = dt; myXR = xr; myYR = yr; scanline = (COLOR *)malloc(sizeof(COLOR)*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(myDT==DTrgbe ? COLRFMT : CIEFMT, fout); fputc('\n', fout); fprtresolu(myXR, myYR, fout); fflush(fout); } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) { sprintf(errmsg, "picture '%s' format/size mismatch", fname); error(USER, errmsg); } psp = pmat->cmem; for (y = 0; y < yr; y++) { /* read it in */ int x; if (freadscan(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]); } } fclose(fp); /* done this picture */ } free(scanline); /* write scanlines */ for (y = 0; y < myYR; y++) if (fwritescan((COLOR *)cm_lval(pmat, y, 0), myXR, fout) < 0) return(0); cm_free(pmat); /* all done */ return(fflush(fout) == 0); } /* check to see if a string contains a %d or %o specification */ static int hasNumberFormat(const char *s) { if (s == NULL) return(0); while (*s) { while (*s != '%') if (!*s++) return(0); if (*++s == '%') { /* ignore "%%" */ ++s; continue; } while (isdigit(*s)) /* field length */ ++s; /* field we'll use? */ if ((*s == 'd') | (*s == 'i') | (*s == 'o') | (*s == 'x') | (*s == 'X')) return(1); } return(0); /* didn't find one */ } int main(int argc, char *argv[]) { int skyfmt = DTascii; int nsteps = 1; char *ofspec = NULL; FILE *ofp = stdout; CMATRIX *cmtx; /* component vector/matrix result */ char fnbuf[256]; int a, i; progname = argv[0]; /* get options */ for (a = 1; a < argc && argv[a][0] == '-'; a++) switch (argv[a][1]) { case 'n': nsteps = atoi(argv[++a]); if (nsteps <= 0) goto userr; break; case 'o': ofspec = argv[++a]; break; case 'i': switch (argv[a][2]) { case 'f': skyfmt = DTfloat; break; case 'd': skyfmt = DTdouble; break; case 'a': skyfmt = DTascii; break; default: goto userr; } break; default: goto userr; } if ((argc-a < 1) | (argc-a > 4)) goto userr; if (argc-a > 2) { /* VTDs expression */ CMATRIX *smtx, *Dmat, *Tmat, *imtx; COLOR tLamb; /* get sky vector/matrix */ smtx = cm_load(argv[a+3], 0, nsteps, skyfmt); /* load BSDF */ Tmat = cm_loadBSDF(argv[a+1], tLamb); /* load Daylight matrix */ Dmat = cm_load(argv[a+2], Tmat==NULL ? 0 : Tmat->ncols, smtx->nrows, DTfromHeader); /* multiply vector through */ imtx = cm_multiply(Dmat, smtx); cm_free(Dmat); cm_free(smtx); if (Tmat == NULL) { /* diffuse only */ cmtx = cm_scale(imtx, tLamb); } else { /* else apply BTDF matrix */ cmtx = cm_multiply(Tmat, imtx); cm_free(Tmat); } cm_free(imtx); } else { /* sky vector/matrix only */ cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt); } /* prepare output stream */ if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) { sprintf(fnbuf, ofspec, 1); ofspec = fnbuf; } if (ofspec != NULL && !hasNumberFormat(ofspec)) { if ((ofp = fopen(ofspec, "w")) == NULL) { fprintf(stderr, "%s: cannot open '%s' for output\n", progname, ofspec); return(1); } ofspec = NULL; /* only need to open once */ } if (hasNumberFormat(argv[a])) { /* generating image(s) */ if (ofspec == NULL) { SET_FILE_BINARY(ofp); newheader("RADIANCE", ofp); printargs(argc, argv, ofp); fputnow(ofp); } if (nsteps > 1) /* multiple output frames? */ for (i = 0; i < nsteps; i++) { CMATRIX *cvec = cm_column(cmtx, i); if (ofspec != NULL) { sprintf(fnbuf, ofspec, i+1); if ((ofp = fopen(fnbuf, "wb")) == NULL) { fprintf(stderr, "%s: cannot open '%s' for output\n", progname, fnbuf); return(1); } newheader("RADIANCE", ofp); printargs(argc, argv, ofp); fputnow(ofp); } fprintf(ofp, "FRAME=%d\n", i+1); if (!sum_images(argv[a], cvec, ofp)) return(1); if (ofspec != NULL) { if (fclose(ofp) == EOF) { fprintf(stderr, "%s: error writing to '%s'\n", progname, fnbuf); return(1); } ofp = stdout; } cm_free(cvec); } else if (!sum_images(argv[a], cmtx, ofp)) return(1); } else { /* generating vector/matrix */ CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader); CMATRIX *rmtx = cm_multiply(Vmat, cmtx); cm_free(Vmat); if (ofspec != NULL) /* multiple vector files? */ for (i = 0; i < nsteps; i++) { CMATRIX *rvec = cm_column(rmtx, i); sprintf(fnbuf, ofspec, i+1); if ((ofp = fopen(fnbuf, "w")) == NULL) { fprintf(stderr, "%s: cannot open '%s' for output\n", progname, fnbuf); return(1); } cm_print(rvec, ofp); if (fclose(ofp) == EOF) { fprintf(stderr, "%s: error writing to '%s'\n", progname, fnbuf); return(1); } ofp = stdout; cm_free(rvec); } else cm_print(rmtx, ofp); cm_free(rmtx); } if (fflush(ofp) == EOF) { /* final clean-up */ fprintf(stderr, "%s: write error on output\n", progname); return(1); } cm_free(cmtx); return(0); userr: fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d}] DCspec [skyf]\n", progname); fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d}] Vspec Tbsdf.xml Dmat.dat [skyf]\n", progname); return(1); }