ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dctimestep.c
Revision: 2.29
Committed: Mon Jan 20 22:18:29 2014 UTC (8 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.28: +4 -9 lines
Log Message:
Fixed improper handling of diffuse transmission matrix

File Contents

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