ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dctimestep.c
Revision: 2.37
Committed: Sun Mar 6 01:13:18 2016 UTC (6 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R1
Changes since 2.36: +2 -1 lines
Log Message:
Prepare for SCons build on Win32 and Win64

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 schorsch 2.37 static const char RCSid[] = "$Id: dctimestep.c,v 2.36 2015/07/14 16:08:28 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.24 COLOR *scanline = NULL;
25     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     char fname[1024];
34     FILE *fp;
35     int dt, xr, yr;
36     COLORV *psp;
37 greg 2.31 char *err;
38 greg 2.14 /* check for zero */
39 greg 2.19 if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) &&
40     (myDT != DTfromHeader) | (i < cv->nrows-1))
41 greg 2.14 continue;
42 greg 2.1 /* open next picture */
43     sprintf(fname, fspec, i);
44 greg 2.31 if ((fp = fopen(fname, "rb")) == NULL) {
45 greg 2.1 sprintf(errmsg, "cannot open picture '%s'", fname);
46     error(SYSTEM, errmsg);
47     }
48 greg 2.31 dt = DTfromHeader;
49     if ((err = cm_getheader(&dt, NULL, NULL, fp)) != NULL)
50     error(USER, err);
51 greg 2.1 if ((dt != DTrgbe) & (dt != DTxyze) ||
52     !fscnresolu(&xr, &yr, fp)) {
53     sprintf(errmsg, "file '%s' not a picture", fname);
54     error(USER, errmsg);
55     }
56     if (myDT == DTfromHeader) { /* on first one */
57     myDT = dt;
58     myXR = xr; myYR = yr;
59     scanline = (COLOR *)malloc(sizeof(COLOR)*myXR);
60     if (scanline == NULL)
61     error(SYSTEM, "out of memory in sum_images()");
62     pmat = cm_alloc(myYR, myXR);
63     memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR);
64 greg 2.2 /* finish header */
65 greg 2.30 fputformat((char *)cm_fmt_id[myDT], fout);
66 greg 2.13 fputc('\n', fout);
67     fflush(fout);
68 greg 2.1 } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) {
69     sprintf(errmsg, "picture '%s' format/size mismatch",
70     fname);
71     error(USER, errmsg);
72     }
73     psp = pmat->cmem;
74     for (y = 0; y < yr; y++) { /* read it in */
75     int x;
76     if (freadscan(scanline, xr, fp) < 0) {
77     sprintf(errmsg, "error reading picture '%s'",
78     fname);
79     error(SYSTEM, errmsg);
80     }
81     /* sum in scanline */
82     for (x = 0; x < xr; x++, psp += 3) {
83     multcolor(scanline[x], scv);
84     addcolor(psp, scanline[x]);
85     }
86     }
87     fclose(fp); /* done this picture */
88     }
89     free(scanline);
90 greg 2.30 i = cm_write(pmat, myDT, fout); /* write picture */
91     cm_free(pmat); /* free data */
92     return(i);
93 greg 2.1 }
94    
95 greg 2.11 /* check to see if a string contains a %d or %o specification */
96     static int
97     hasNumberFormat(const char *s)
98 greg 2.1 {
99 greg 2.24 if (s == NULL)
100     return(0);
101    
102 greg 2.18 while (*s) {
103     while (*s != '%')
104     if (!*s++)
105     return(0);
106     if (*++s == '%') { /* ignore "%%" */
107     ++s;
108     continue;
109     }
110     while (isdigit(*s)) /* field length */
111     ++s;
112     /* field we'll use? */
113     if ((*s == 'd') | (*s == 'i') | (*s == 'o') |
114     (*s == 'x') | (*s == 'X'))
115     return(1);
116     }
117     return(0); /* didn't find one */
118 greg 2.1 }
119    
120     int
121     main(int argc, char *argv[])
122     {
123 greg 2.34 int skyfmt = DTfromHeader;
124 greg 2.30 int outfmt = DTascii;
125 greg 2.34 int headout = 1;
126     int nsteps = 0;
127 greg 2.24 char *ofspec = NULL;
128     FILE *ofp = stdout;
129     CMATRIX *cmtx; /* component vector/matrix result */
130     char fnbuf[256];
131     int a, i;
132 greg 2.1
133     progname = argv[0];
134 greg 2.24 /* get options */
135 greg 2.25 for (a = 1; a < argc && argv[a][0] == '-'; a++)
136     switch (argv[a][1]) {
137 greg 2.24 case 'n':
138     nsteps = atoi(argv[++a]);
139 greg 2.31 if (nsteps < 0)
140 greg 2.24 goto userr;
141 greg 2.34 skyfmt = nsteps ? DTascii : DTfromHeader;
142     break;
143     case 'h':
144     headout = !headout;
145 greg 2.24 break;
146 greg 2.25 case 'i':
147     switch (argv[a][2]) {
148     case 'f':
149     skyfmt = DTfloat;
150     break;
151     case 'd':
152     skyfmt = DTdouble;
153     break;
154     case 'a':
155     skyfmt = DTascii;
156     break;
157     default:
158     goto userr;
159     }
160     break;
161 greg 2.30 case 'o':
162     switch (argv[a][2]) {
163     case '\0': /* output specification (not format) */
164     ofspec = argv[++a];
165     break;
166     case 'f':
167     outfmt = DTfloat;
168     break;
169     case 'd':
170     outfmt = DTdouble;
171     break;
172     case 'a':
173     outfmt = DTascii;
174     break;
175     default:
176     goto userr;
177     }
178     break;
179 greg 2.24 default:
180     goto userr;
181     }
182     if ((argc-a < 1) | (argc-a > 4))
183     goto userr;
184 greg 2.1
185 greg 2.24 if (argc-a > 2) { /* VTDs expression */
186 greg 2.36 CMATRIX *smtx, *Dmat, *Tmat, *imtx;
187     const char *ccp;
188 greg 2.24 /* get sky vector/matrix */
189 greg 2.25 smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
190 greg 2.35 nsteps = smtx->ncols;
191 greg 2.12 /* load BSDF */
192 greg 2.36 if (argv[a+1][0] != '!' &&
193     (ccp = strrchr(argv[a+1], '.')) != NULL &&
194     !strcasecmp(ccp+1, "XML"))
195     Tmat = cm_loadBTDF(argv[a+1]);
196     else
197     Tmat = cm_load(argv[a+1], 0, 0, DTfromHeader);
198 greg 2.5 /* load Daylight matrix */
199 greg 2.36 Dmat = cm_load(argv[a+2], Tmat->ncols,
200 greg 2.24 smtx->nrows, DTfromHeader);
201 greg 2.1 /* multiply vector through */
202 greg 2.24 imtx = cm_multiply(Dmat, smtx);
203     cm_free(Dmat); cm_free(smtx);
204 greg 2.29 cmtx = cm_multiply(Tmat, imtx);
205     cm_free(Tmat);
206 greg 2.24 cm_free(imtx);
207     } else { /* sky vector/matrix only */
208 greg 2.25 cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
209 greg 2.35 nsteps = cmtx->ncols;
210 greg 2.24 }
211     /* prepare output stream */
212     if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) {
213     sprintf(fnbuf, ofspec, 1);
214     ofspec = fnbuf;
215     }
216     if (ofspec != NULL && !hasNumberFormat(ofspec)) {
217     if ((ofp = fopen(ofspec, "w")) == NULL) {
218     fprintf(stderr, "%s: cannot open '%s' for output\n",
219     progname, ofspec);
220     return(1);
221     }
222     ofspec = NULL; /* only need to open once */
223 greg 2.12 }
224 greg 2.24 if (hasNumberFormat(argv[a])) { /* generating image(s) */
225     if (ofspec == NULL) {
226     SET_FILE_BINARY(ofp);
227     newheader("RADIANCE", ofp);
228     printargs(argc, argv, ofp);
229     fputnow(ofp);
230     }
231     if (nsteps > 1) /* multiple output frames? */
232     for (i = 0; i < nsteps; i++) {
233     CMATRIX *cvec = cm_column(cmtx, i);
234     if (ofspec != NULL) {
235     sprintf(fnbuf, ofspec, i+1);
236     if ((ofp = fopen(fnbuf, "wb")) == NULL) {
237     fprintf(stderr,
238     "%s: cannot open '%s' for output\n",
239     progname, fnbuf);
240     return(1);
241     }
242     newheader("RADIANCE", ofp);
243     printargs(argc, argv, ofp);
244     fputnow(ofp);
245     }
246     fprintf(ofp, "FRAME=%d\n", i+1);
247     if (!sum_images(argv[a], cvec, ofp))
248     return(1);
249     if (ofspec != NULL) {
250     if (fclose(ofp) == EOF) {
251     fprintf(stderr,
252     "%s: error writing to '%s'\n",
253     progname, fnbuf);
254     return(1);
255     }
256     ofp = stdout;
257     }
258     cm_free(cvec);
259     }
260     else if (!sum_images(argv[a], cmtx, ofp))
261 greg 2.1 return(1);
262 greg 2.24 } else { /* generating vector/matrix */
263     CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
264     CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
265 greg 2.1 cm_free(Vmat);
266 greg 2.30 if (ofspec != NULL) { /* multiple vector files? */
267     const char *wtype = (outfmt==DTascii) ? "w" : "wb";
268 greg 2.24 for (i = 0; i < nsteps; i++) {
269     CMATRIX *rvec = cm_column(rmtx, i);
270     sprintf(fnbuf, ofspec, i+1);
271 greg 2.30 if ((ofp = fopen(fnbuf, wtype)) == NULL) {
272 greg 2.24 fprintf(stderr,
273     "%s: cannot open '%s' for output\n",
274     progname, fnbuf);
275     return(1);
276     }
277 greg 2.31 #ifdef getc_unlocked
278     flockfile(ofp);
279     #endif
280 greg 2.34 if (headout) { /* header output */
281     newheader("RADIANCE", ofp);
282     printargs(argc, argv, ofp);
283     fputnow(ofp);
284     fprintf(ofp, "FRAME=%d\n", i+1);
285     fprintf(ofp, "NROWS=%d\n", rvec->nrows);
286     fputs("NCOLS=1\nNCOMP=3\n", ofp);
287     fputformat((char *)cm_fmt_id[outfmt], ofp);
288     fputc('\n', ofp);
289     }
290 greg 2.30 cm_write(rvec, outfmt, ofp);
291 greg 2.24 if (fclose(ofp) == EOF) {
292     fprintf(stderr,
293     "%s: error writing to '%s'\n",
294     progname, fnbuf);
295     return(1);
296     }
297     ofp = stdout;
298     cm_free(rvec);
299     }
300 greg 2.30 } else {
301 greg 2.31 #ifdef getc_unlocked
302     flockfile(ofp);
303     #endif
304 greg 2.30 if (outfmt != DTascii)
305     SET_FILE_BINARY(ofp);
306 greg 2.34 if (headout) { /* header output */
307 greg 2.30 newheader("RADIANCE", ofp);
308     printargs(argc, argv, ofp);
309     fputnow(ofp);
310 greg 2.33 fprintf(ofp, "NROWS=%d\n", rmtx->nrows);
311 greg 2.31 fprintf(ofp, "NCOLS=%d\n", rmtx->ncols);
312     fputs("NCOMP=3\n", ofp);
313 greg 2.30 fputformat((char *)cm_fmt_id[outfmt], ofp);
314     fputc('\n', ofp);
315     }
316     cm_write(rmtx, outfmt, ofp);
317     }
318 greg 2.24 cm_free(rmtx);
319     }
320     if (fflush(ofp) == EOF) { /* final clean-up */
321     fprintf(stderr, "%s: write error on output\n", progname);
322     return(1);
323 greg 2.1 }
324 greg 2.24 cm_free(cmtx);
325 greg 2.1 return(0);
326 greg 2.24 userr:
327 greg 2.32 fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] DCspec [skyf]\n",
328 greg 2.24 progname);
329 greg 2.36 fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] Vspec Tbsdf Dmat.dat [skyf]\n",
330 greg 2.24 progname);
331     return(1);
332 greg 2.1 }