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 (8 years, 6 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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: dctimestep.c,v 2.30 2014/02/08 01:28:06 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 matrix 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 char *err;
37 /* check for zero */
38 if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) &&
39 (myDT != DTfromHeader) | (i < cv->nrows-1))
40 continue;
41 /* open next picture */
42 sprintf(fname, fspec, i);
43 if ((fp = fopen(fname, "rb")) == NULL) {
44 sprintf(errmsg, "cannot open picture '%s'", fname);
45 error(SYSTEM, errmsg);
46 }
47 dt = DTfromHeader;
48 if ((err = cm_getheader(&dt, NULL, NULL, fp)) != NULL)
49 error(USER, err);
50 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 /* finish header */
64 fputformat((char *)cm_fmt_id[myDT], fout);
65 fputc('\n', fout);
66 fflush(fout);
67 } 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 i = cm_write(pmat, myDT, fout); /* write picture */
90 cm_free(pmat); /* free data */
91 return(i);
92 }
93
94 /* check to see if a string contains a %d or %o specification */
95 static int
96 hasNumberFormat(const char *s)
97 {
98 if (s == NULL)
99 return(0);
100
101 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 }
118
119 int
120 main(int argc, char *argv[])
121 {
122 int skyfmt = DTascii;
123 int outfmt = DTascii;
124 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
131 progname = argv[0];
132 /* get options */
133 for (a = 1; a < argc && argv[a][0] == '-'; a++)
134 switch (argv[a][1]) {
135 case 'n':
136 nsteps = atoi(argv[++a]);
137 if (nsteps < 0)
138 goto userr;
139 break;
140 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 case 'h':
152 skyfmt = DTfromHeader;
153 break;
154 default:
155 goto userr;
156 }
157 break;
158 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 default:
177 goto userr;
178 }
179 if ((argc-a < 1) | (argc-a > 4))
180 goto userr;
181
182 if (argc-a > 2) { /* VTDs expression */
183 CMATRIX *smtx, *Dmat, *Tmat, *imtx;
184 /* get sky vector/matrix */
185 smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
186 /* load BSDF */
187 Tmat = cm_loadBTDF(argv[a+1]);
188 /* load Daylight matrix */
189 Dmat = cm_load(argv[a+2], Tmat==NULL ? 0 : Tmat->ncols,
190 smtx->nrows, DTfromHeader);
191 /* multiply vector through */
192 imtx = cm_multiply(Dmat, smtx);
193 cm_free(Dmat); cm_free(smtx);
194 cmtx = cm_multiply(Tmat, imtx);
195 cm_free(Tmat);
196 cm_free(imtx);
197 } else { /* sky vector/matrix only */
198 cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
199 }
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 }
213 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 return(1);
251 } else { /* generating vector/matrix */
252 CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
253 CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
254 cm_free(Vmat);
255 if (ofspec != NULL) { /* multiple vector files? */
256 const char *wtype = (outfmt==DTascii) ? "w" : "wb";
257 for (i = 0; i < nsteps; i++) {
258 CMATRIX *rvec = cm_column(rmtx, i);
259 sprintf(fnbuf, ofspec, i+1);
260 if ((ofp = fopen(fnbuf, wtype)) == NULL) {
261 fprintf(stderr,
262 "%s: cannot open '%s' for output\n",
263 progname, fnbuf);
264 return(1);
265 }
266 #ifdef getc_unlocked
267 flockfile(ofp);
268 #endif
269 cm_write(rvec, outfmt, ofp);
270 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 } else {
280 #ifdef getc_unlocked
281 flockfile(ofp);
282 #endif
283 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 fprintf(ofp, "NCOLS=%d\n", rmtx->ncols);
290 fputs("NCOMP=3\n", ofp);
291 fputformat((char *)cm_fmt_id[outfmt], ofp);
292 fputc('\n', ofp);
293 }
294 cm_write(rmtx, outfmt, ofp);
295 }
296 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 }
302 cm_free(cmtx);
303 return(0);
304 userr:
305 fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d}][-o{f|d}] DCspec [skyf]\n",
306 progname);
307 fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d}][-o{f|d}] Vspec Tbsdf.xml Dmat.dat [skyf]\n",
308 progname);
309 return(1);
310 }