ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dctimestep.c
Revision: 2.30
Committed: Sat Feb 8 01:28:06 2014 UTC (8 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.29: +42 -19 lines
Log Message:
Added -od and -of options for binary output

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: dctimestep.c,v 2.29 2014/01/20 22:18:29 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((char *)cm_fmt_id[myDT], fout);
63 fputc('\n', fout);
64 fflush(fout);
65 } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) {
66 sprintf(errmsg, "picture '%s' format/size mismatch",
67 fname);
68 error(USER, errmsg);
69 }
70 psp = pmat->cmem;
71 for (y = 0; y < yr; y++) { /* read it in */
72 int x;
73 if (freadscan(scanline, xr, fp) < 0) {
74 sprintf(errmsg, "error reading picture '%s'",
75 fname);
76 error(SYSTEM, errmsg);
77 }
78 /* sum in scanline */
79 for (x = 0; x < xr; x++, psp += 3) {
80 multcolor(scanline[x], scv);
81 addcolor(psp, scanline[x]);
82 }
83 }
84 fclose(fp); /* done this picture */
85 }
86 free(scanline);
87 i = cm_write(pmat, myDT, fout); /* write picture */
88 cm_free(pmat); /* free data */
89 return(i);
90 }
91
92 /* check to see if a string contains a %d or %o specification */
93 static int
94 hasNumberFormat(const char *s)
95 {
96 if (s == NULL)
97 return(0);
98
99 while (*s) {
100 while (*s != '%')
101 if (!*s++)
102 return(0);
103 if (*++s == '%') { /* ignore "%%" */
104 ++s;
105 continue;
106 }
107 while (isdigit(*s)) /* field length */
108 ++s;
109 /* field we'll use? */
110 if ((*s == 'd') | (*s == 'i') | (*s == 'o') |
111 (*s == 'x') | (*s == 'X'))
112 return(1);
113 }
114 return(0); /* didn't find one */
115 }
116
117 int
118 main(int argc, char *argv[])
119 {
120 int skyfmt = DTascii;
121 int outfmt = DTascii;
122 int nsteps = 1;
123 char *ofspec = NULL;
124 FILE *ofp = stdout;
125 CMATRIX *cmtx; /* component vector/matrix result */
126 char fnbuf[256];
127 int a, i;
128
129 progname = argv[0];
130 /* get options */
131 for (a = 1; a < argc && argv[a][0] == '-'; a++)
132 switch (argv[a][1]) {
133 case 'n':
134 nsteps = atoi(argv[++a]);
135 if (nsteps <= 0)
136 goto userr;
137 break;
138 case 'i':
139 switch (argv[a][2]) {
140 case 'f':
141 skyfmt = DTfloat;
142 break;
143 case 'd':
144 skyfmt = DTdouble;
145 break;
146 case 'a':
147 skyfmt = DTascii;
148 break;
149 default:
150 goto userr;
151 }
152 break;
153 case 'o':
154 switch (argv[a][2]) {
155 case '\0': /* output specification (not format) */
156 ofspec = argv[++a];
157 break;
158 case 'f':
159 outfmt = DTfloat;
160 break;
161 case 'd':
162 outfmt = DTdouble;
163 break;
164 case 'a':
165 outfmt = DTascii;
166 break;
167 default:
168 goto userr;
169 }
170 break;
171 default:
172 goto userr;
173 }
174 if ((argc-a < 1) | (argc-a > 4))
175 goto userr;
176
177 if (argc-a > 2) { /* VTDs expression */
178 CMATRIX *smtx, *Dmat, *Tmat, *imtx;
179 /* get sky vector/matrix */
180 smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
181 /* load BSDF */
182 Tmat = cm_loadBTDF(argv[a+1]);
183 /* load Daylight matrix */
184 Dmat = cm_load(argv[a+2], Tmat==NULL ? 0 : Tmat->ncols,
185 smtx->nrows, DTfromHeader);
186 /* multiply vector through */
187 imtx = cm_multiply(Dmat, smtx);
188 cm_free(Dmat); cm_free(smtx);
189 cmtx = cm_multiply(Tmat, imtx);
190 cm_free(Tmat);
191 cm_free(imtx);
192 } else { /* sky vector/matrix only */
193 cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
194 }
195 /* prepare output stream */
196 if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) {
197 sprintf(fnbuf, ofspec, 1);
198 ofspec = fnbuf;
199 }
200 if (ofspec != NULL && !hasNumberFormat(ofspec)) {
201 if ((ofp = fopen(ofspec, "w")) == NULL) {
202 fprintf(stderr, "%s: cannot open '%s' for output\n",
203 progname, ofspec);
204 return(1);
205 }
206 ofspec = NULL; /* only need to open once */
207 }
208 if (hasNumberFormat(argv[a])) { /* generating image(s) */
209 if (ofspec == NULL) {
210 SET_FILE_BINARY(ofp);
211 newheader("RADIANCE", ofp);
212 printargs(argc, argv, ofp);
213 fputnow(ofp);
214 }
215 if (nsteps > 1) /* multiple output frames? */
216 for (i = 0; i < nsteps; i++) {
217 CMATRIX *cvec = cm_column(cmtx, i);
218 if (ofspec != NULL) {
219 sprintf(fnbuf, ofspec, i+1);
220 if ((ofp = fopen(fnbuf, "wb")) == NULL) {
221 fprintf(stderr,
222 "%s: cannot open '%s' for output\n",
223 progname, fnbuf);
224 return(1);
225 }
226 newheader("RADIANCE", ofp);
227 printargs(argc, argv, ofp);
228 fputnow(ofp);
229 }
230 fprintf(ofp, "FRAME=%d\n", i+1);
231 if (!sum_images(argv[a], cvec, ofp))
232 return(1);
233 if (ofspec != NULL) {
234 if (fclose(ofp) == EOF) {
235 fprintf(stderr,
236 "%s: error writing to '%s'\n",
237 progname, fnbuf);
238 return(1);
239 }
240 ofp = stdout;
241 }
242 cm_free(cvec);
243 }
244 else if (!sum_images(argv[a], cmtx, ofp))
245 return(1);
246 } else { /* generating vector/matrix */
247 CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
248 CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
249 cm_free(Vmat);
250 if (ofspec != NULL) { /* multiple vector files? */
251 const char *wtype = (outfmt==DTascii) ? "w" : "wb";
252 for (i = 0; i < nsteps; i++) {
253 CMATRIX *rvec = cm_column(rmtx, i);
254 sprintf(fnbuf, ofspec, i+1);
255 if ((ofp = fopen(fnbuf, wtype)) == NULL) {
256 fprintf(stderr,
257 "%s: cannot open '%s' for output\n",
258 progname, fnbuf);
259 return(1);
260 }
261 cm_write(rvec, outfmt, ofp);
262 if (fclose(ofp) == EOF) {
263 fprintf(stderr,
264 "%s: error writing to '%s'\n",
265 progname, fnbuf);
266 return(1);
267 }
268 ofp = stdout;
269 cm_free(rvec);
270 }
271 } else {
272 if (outfmt != DTascii)
273 SET_FILE_BINARY(ofp);
274 if (rmtx->ncols > 1) { /* header if actual matrix */
275 newheader("RADIANCE", ofp);
276 printargs(argc, argv, ofp);
277 fputnow(ofp);
278 fputformat((char *)cm_fmt_id[outfmt], ofp);
279 fputc('\n', ofp);
280 }
281 cm_write(rmtx, outfmt, ofp);
282 }
283 cm_free(rmtx);
284 }
285 if (fflush(ofp) == EOF) { /* final clean-up */
286 fprintf(stderr, "%s: write error on output\n", progname);
287 return(1);
288 }
289 cm_free(cmtx);
290 return(0);
291 userr:
292 fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d}][-o{f|d}] DCspec [skyf]\n",
293 progname);
294 fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d}][-o{f|d}] Vspec Tbsdf.xml Dmat.dat [skyf]\n",
295 progname);
296 return(1);
297 }