ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dctimestep.c
Revision: 2.31
Committed: Fri May 30 00:00:54 2014 UTC (9 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.30: +19 -6 lines
Log Message:
Added NROWS, NCOLS and NCOMP to matrix headers

File Contents

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