ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dctimestep.c
Revision: 2.48
Committed: Fri Mar 11 02:28:51 2022 UTC (2 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.47: +3 -3 lines
Log Message:
fix(dctimestep): Forgot to add new options to usage message

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.48 static const char RCSid[] = "$Id: dctimestep.c,v 2.47 2022/03/11 02:27:02 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * Compute time-step result using Daylight Coefficient method.
6     *
7     * G. Ward
8     */
9    
10     #include <ctype.h>
11 schorsch 2.37 #include "platform.h"
12 greg 2.1 #include "standard.h"
13 greg 2.28 #include "cmatrix.h"
14 greg 2.1 #include "platform.h"
15     #include "resolu.h"
16    
17     char *progname; /* global argv[0] */
18    
19 greg 2.24 /* Sum together a set of images and write result to fout */
20 greg 2.1 static int
21 greg 2.13 sum_images(const char *fspec, const CMATRIX *cv, FILE *fout)
22 greg 2.1 {
23     int myDT = DTfromHeader;
24 greg 2.38 COLR *scanline = NULL;
25 greg 2.24 CMATRIX *pmat = NULL;
26     int myXR=0, myYR=0;
27 greg 2.1 int i, y;
28    
29     if (cv->ncols != 1)
30 greg 2.35 error(INTERNAL, "expected vector in sum_images()");
31 greg 2.1 for (i = 0; i < cv->nrows; i++) {
32     const COLORV *scv = cv_lval(cv,i);
33 greg 2.39 int flat_file = 0;
34 greg 2.1 char fname[1024];
35     FILE *fp;
36 greg 2.39 long data_start;
37 greg 2.1 int dt, xr, yr;
38     COLORV *psp;
39 greg 2.31 char *err;
40 greg 2.14 /* check for zero */
41 greg 2.19 if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) &&
42     (myDT != DTfromHeader) | (i < cv->nrows-1))
43 greg 2.14 continue;
44 greg 2.1 /* open next picture */
45     sprintf(fname, fspec, i);
46 greg 2.31 if ((fp = fopen(fname, "rb")) == NULL) {
47 greg 2.1 sprintf(errmsg, "cannot open picture '%s'", fname);
48     error(SYSTEM, errmsg);
49     }
50 greg 2.31 dt = DTfromHeader;
51 greg 2.45 if ((err = cm_getheader(&dt, NULL, NULL, NULL, NULL, fp)) != NULL)
52 greg 2.31 error(USER, err);
53 greg 2.1 if ((dt != DTrgbe) & (dt != DTxyze) ||
54     !fscnresolu(&xr, &yr, fp)) {
55     sprintf(errmsg, "file '%s' not a picture", fname);
56     error(USER, errmsg);
57     }
58     if (myDT == DTfromHeader) { /* on first one */
59     myDT = dt;
60     myXR = xr; myYR = yr;
61 greg 2.38 scanline = (COLR *)malloc(sizeof(COLR)*myXR);
62 greg 2.1 if (scanline == NULL)
63     error(SYSTEM, "out of memory in sum_images()");
64     pmat = cm_alloc(myYR, myXR);
65     memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR);
66 greg 2.2 /* finish header */
67 greg 2.46 fputformat(cm_fmt_id[myDT], fout);
68 greg 2.13 fputc('\n', fout);
69     fflush(fout);
70 greg 2.1 } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) {
71     sprintf(errmsg, "picture '%s' format/size mismatch",
72     fname);
73     error(USER, errmsg);
74     }
75 greg 2.39 /* flat file check */
76     if ((data_start = ftell(fp)) > 0 && fseek(fp, 0L, SEEK_END) == 0) {
77     flat_file = (ftell(fp) == data_start + sizeof(COLR)*xr*yr);
78     if (fseek(fp, data_start, SEEK_SET) < 0) {
79     sprintf(errmsg, "cannot seek on picture '%s'", fname);
80     error(SYSTEM, errmsg);
81     }
82     }
83 greg 2.1 psp = pmat->cmem;
84     for (y = 0; y < yr; y++) { /* read it in */
85 greg 2.38 COLOR col;
86 greg 2.1 int x;
87 greg 2.39 if (flat_file ? getbinary(scanline, sizeof(COLR), xr, fp) != xr :
88     freadcolrs(scanline, xr, fp) < 0) {
89 greg 2.1 sprintf(errmsg, "error reading picture '%s'",
90     fname);
91     error(SYSTEM, errmsg);
92     }
93     /* sum in scanline */
94     for (x = 0; x < xr; x++, psp += 3) {
95 greg 2.38 if (!scanline[x][EXP])
96     continue; /* skip zeroes */
97     colr_color(col, scanline[x]);
98     multcolor(col, scv);
99     addcolor(psp, col);
100 greg 2.1 }
101     }
102     fclose(fp); /* done this picture */
103     }
104     free(scanline);
105 greg 2.30 i = cm_write(pmat, myDT, fout); /* write picture */
106     cm_free(pmat); /* free data */
107     return(i);
108 greg 2.1 }
109    
110 greg 2.11 /* check to see if a string contains a %d or %o specification */
111     static int
112     hasNumberFormat(const char *s)
113 greg 2.1 {
114 greg 2.24 if (s == NULL)
115     return(0);
116    
117 greg 2.18 while (*s) {
118     while (*s != '%')
119     if (!*s++)
120     return(0);
121     if (*++s == '%') { /* ignore "%%" */
122     ++s;
123     continue;
124     }
125     while (isdigit(*s)) /* field length */
126     ++s;
127     /* field we'll use? */
128     if ((*s == 'd') | (*s == 'i') | (*s == 'o') |
129     (*s == 'x') | (*s == 'X'))
130     return(1);
131     }
132     return(0); /* didn't find one */
133 greg 2.1 }
134    
135     int
136     main(int argc, char *argv[])
137     {
138 greg 2.34 int skyfmt = DTfromHeader;
139 greg 2.30 int outfmt = DTascii;
140 greg 2.34 int headout = 1;
141     int nsteps = 0;
142 greg 2.24 char *ofspec = NULL;
143     FILE *ofp = stdout;
144 greg 2.47 int xres=0, yres=0;
145 greg 2.24 CMATRIX *cmtx; /* component vector/matrix result */
146     char fnbuf[256];
147     int a, i;
148 greg 2.1
149     progname = argv[0];
150 greg 2.24 /* get options */
151 greg 2.25 for (a = 1; a < argc && argv[a][0] == '-'; a++)
152     switch (argv[a][1]) {
153 greg 2.24 case 'n':
154     nsteps = atoi(argv[++a]);
155 greg 2.31 if (nsteps < 0)
156 greg 2.24 goto userr;
157 greg 2.34 skyfmt = nsteps ? DTascii : DTfromHeader;
158     break;
159     case 'h':
160     headout = !headout;
161 greg 2.24 break;
162 greg 2.25 case 'i':
163     switch (argv[a][2]) {
164     case 'f':
165     skyfmt = DTfloat;
166     break;
167     case 'd':
168     skyfmt = DTdouble;
169     break;
170     case 'a':
171     skyfmt = DTascii;
172     break;
173     default:
174     goto userr;
175     }
176     break;
177 greg 2.30 case 'o':
178     switch (argv[a][2]) {
179     case '\0': /* output specification (not format) */
180     ofspec = argv[++a];
181     break;
182     case 'f':
183     outfmt = DTfloat;
184     break;
185     case 'd':
186     outfmt = DTdouble;
187     break;
188     case 'a':
189     outfmt = DTascii;
190     break;
191 greg 2.43 case 'c':
192     outfmt = DTrgbe;
193     break;
194 greg 2.30 default:
195     goto userr;
196     }
197     break;
198 greg 2.47 case 'x':
199     xres = atoi(argv[++a]);
200     break;
201     case 'y':
202     yres = atoi(argv[++a]);
203     break;
204 greg 2.24 default:
205     goto userr;
206     }
207     if ((argc-a < 1) | (argc-a > 4))
208     goto userr;
209 greg 2.1
210 greg 2.24 if (argc-a > 2) { /* VTDs expression */
211 greg 2.36 CMATRIX *smtx, *Dmat, *Tmat, *imtx;
212     const char *ccp;
213 greg 2.24 /* get sky vector/matrix */
214 greg 2.25 smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
215 greg 2.35 nsteps = smtx->ncols;
216 greg 2.12 /* load BSDF */
217 greg 2.36 if (argv[a+1][0] != '!' &&
218     (ccp = strrchr(argv[a+1], '.')) != NULL &&
219     !strcasecmp(ccp+1, "XML"))
220     Tmat = cm_loadBTDF(argv[a+1]);
221     else
222     Tmat = cm_load(argv[a+1], 0, 0, DTfromHeader);
223 greg 2.5 /* load Daylight matrix */
224 greg 2.36 Dmat = cm_load(argv[a+2], Tmat->ncols,
225 greg 2.24 smtx->nrows, DTfromHeader);
226 greg 2.1 /* multiply vector through */
227 greg 2.24 imtx = cm_multiply(Dmat, smtx);
228     cm_free(Dmat); cm_free(smtx);
229 greg 2.29 cmtx = cm_multiply(Tmat, imtx);
230     cm_free(Tmat);
231 greg 2.24 cm_free(imtx);
232     } else { /* sky vector/matrix only */
233 greg 2.25 cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
234 greg 2.35 nsteps = cmtx->ncols;
235 greg 2.24 }
236     /* prepare output stream */
237     if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) {
238     sprintf(fnbuf, ofspec, 1);
239     ofspec = fnbuf;
240     }
241     if (ofspec != NULL && !hasNumberFormat(ofspec)) {
242     if ((ofp = fopen(ofspec, "w")) == NULL) {
243     fprintf(stderr, "%s: cannot open '%s' for output\n",
244     progname, ofspec);
245     return(1);
246     }
247     ofspec = NULL; /* only need to open once */
248 greg 2.12 }
249 greg 2.24 if (hasNumberFormat(argv[a])) { /* generating image(s) */
250 greg 2.44 if (outfmt != DTrgbe) {
251     error(WARNING, "changing output type to -oc");
252     outfmt = DTrgbe;
253     }
254 greg 2.24 if (ofspec == NULL) {
255     SET_FILE_BINARY(ofp);
256     newheader("RADIANCE", ofp);
257     printargs(argc, argv, ofp);
258     fputnow(ofp);
259     }
260     if (nsteps > 1) /* multiple output frames? */
261     for (i = 0; i < nsteps; i++) {
262     CMATRIX *cvec = cm_column(cmtx, i);
263     if (ofspec != NULL) {
264 greg 2.40 sprintf(fnbuf, ofspec, i);
265 greg 2.24 if ((ofp = fopen(fnbuf, "wb")) == NULL) {
266     fprintf(stderr,
267     "%s: cannot open '%s' for output\n",
268     progname, fnbuf);
269     return(1);
270     }
271     newheader("RADIANCE", ofp);
272     printargs(argc, argv, ofp);
273     fputnow(ofp);
274     }
275 greg 2.40 fprintf(ofp, "FRAME=%d\n", i);
276 greg 2.24 if (!sum_images(argv[a], cvec, ofp))
277     return(1);
278     if (ofspec != NULL) {
279     if (fclose(ofp) == EOF) {
280     fprintf(stderr,
281     "%s: error writing to '%s'\n",
282     progname, fnbuf);
283     return(1);
284     }
285     ofp = stdout;
286     }
287     cm_free(cvec);
288     }
289     else if (!sum_images(argv[a], cmtx, ofp))
290 greg 2.1 return(1);
291 greg 2.24 } else { /* generating vector/matrix */
292     CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
293     CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
294 greg 2.1 cm_free(Vmat);
295 greg 2.30 if (ofspec != NULL) { /* multiple vector files? */
296     const char *wtype = (outfmt==DTascii) ? "w" : "wb";
297 greg 2.24 for (i = 0; i < nsteps; i++) {
298     CMATRIX *rvec = cm_column(rmtx, i);
299 greg 2.47 if (yres > 0) {
300     if (xres <= 0)
301     xres = rvec->nrows/yres;
302     if (xres*yres != rvec->nrows) {
303     fprintf(stderr, "Bad resolution: %d != %dx%d\n",
304     rvec->nrows, xres, yres);
305     return(1);
306     }
307     rvec->nrows = yres;
308     rvec->ncols = xres;
309     } else if (xres > 0) {
310     yres = rvec->nrows/xres;
311     if (xres*yres != rvec->nrows) {
312     fprintf(stderr,
313     "Bad resolution: %d does not divide %d evenly\n",
314     xres, rvec->nrows);
315     return(1);
316     }
317     rvec->nrows = yres;
318     rvec->ncols = xres;
319     }
320 greg 2.40 sprintf(fnbuf, ofspec, i);
321 greg 2.30 if ((ofp = fopen(fnbuf, wtype)) == NULL) {
322 greg 2.24 fprintf(stderr,
323     "%s: cannot open '%s' for output\n",
324     progname, fnbuf);
325     return(1);
326     }
327 greg 2.31 #ifdef getc_unlocked
328     flockfile(ofp);
329     #endif
330 greg 2.34 if (headout) { /* header output */
331     newheader("RADIANCE", ofp);
332     printargs(argc, argv, ofp);
333     fputnow(ofp);
334 greg 2.40 fprintf(ofp, "FRAME=%d\n", i);
335 greg 2.47 if ((outfmt != DTrgbe) & (outfmt != DTxyze)) {
336     fprintf(ofp, "NROWS=%d\n", rvec->nrows);
337     fprintf(ofp, "NCOLS=%d\n", rvec->ncols);
338     fputs("NCOMP=3\n", ofp);
339     }
340     if ((outfmt == DTfloat) | (outfmt == DTdouble))
341 greg 2.42 fputendian(ofp);
342 greg 2.46 fputformat(cm_fmt_id[outfmt], ofp);
343 greg 2.34 fputc('\n', ofp);
344     }
345 greg 2.30 cm_write(rvec, outfmt, ofp);
346 greg 2.24 if (fclose(ofp) == EOF) {
347     fprintf(stderr,
348     "%s: error writing to '%s'\n",
349     progname, fnbuf);
350     return(1);
351     }
352     ofp = stdout;
353     cm_free(rvec);
354     }
355 greg 2.30 } else {
356 greg 2.31 #ifdef getc_unlocked
357     flockfile(ofp);
358     #endif
359 greg 2.30 if (outfmt != DTascii)
360     SET_FILE_BINARY(ofp);
361 greg 2.34 if (headout) { /* header output */
362 greg 2.30 newheader("RADIANCE", ofp);
363     printargs(argc, argv, ofp);
364     fputnow(ofp);
365 greg 2.47 if ((outfmt != DTrgbe) & (outfmt != DTxyze)) {
366     fprintf(ofp, "NROWS=%d\n", rmtx->nrows);
367     fprintf(ofp, "NCOLS=%d\n", rmtx->ncols);
368     fputs("NCOMP=3\n", ofp);
369     }
370     if ((outfmt == DTfloat) | (outfmt == DTdouble))
371 greg 2.42 fputendian(ofp);
372 greg 2.46 fputformat(cm_fmt_id[outfmt], ofp);
373 greg 2.30 fputc('\n', ofp);
374     }
375     cm_write(rmtx, outfmt, ofp);
376     }
377 greg 2.24 cm_free(rmtx);
378     }
379     if (fflush(ofp) == EOF) { /* final clean-up */
380     fprintf(stderr, "%s: write error on output\n", progname);
381     return(1);
382 greg 2.1 }
383 greg 2.24 cm_free(cmtx);
384 greg 2.1 return(0);
385 greg 2.24 userr:
386 greg 2.48 fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-x xr][-y yr][-i{f|d|h}][-o{f|d|c}] DCspec [skyf]\n",
387 greg 2.24 progname);
388 greg 2.48 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",
389 greg 2.24 progname);
390     return(1);
391 greg 2.1 }