ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dctimestep.c
Revision: 2.28
Committed: Mon Jan 20 21:29:04 2014 UTC (8 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.27: +2 -499 lines
Log Message:
Split out color coefficient matrix routines from dctimestep

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: dctimestep.c,v 2.27 2013/01/19 22:30:55 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 COLOR tLamb;
168 /* get sky vector/matrix */
169 smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
170 /* load BSDF */
171 Tmat = cm_loadBSDF(argv[a+1], tLamb);
172 /* load Daylight matrix */
173 Dmat = cm_load(argv[a+2], Tmat==NULL ? 0 : Tmat->ncols,
174 smtx->nrows, DTfromHeader);
175 /* multiply vector through */
176 imtx = cm_multiply(Dmat, smtx);
177 cm_free(Dmat); cm_free(smtx);
178 if (Tmat == NULL) { /* diffuse only */
179 cmtx = cm_scale(imtx, tLamb);
180 } else { /* else apply BTDF matrix */
181 cmtx = cm_multiply(Tmat, imtx);
182 cm_free(Tmat);
183 }
184 cm_free(imtx);
185 } else { /* sky vector/matrix only */
186 cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
187 }
188 /* prepare output stream */
189 if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) {
190 sprintf(fnbuf, ofspec, 1);
191 ofspec = fnbuf;
192 }
193 if (ofspec != NULL && !hasNumberFormat(ofspec)) {
194 if ((ofp = fopen(ofspec, "w")) == NULL) {
195 fprintf(stderr, "%s: cannot open '%s' for output\n",
196 progname, ofspec);
197 return(1);
198 }
199 ofspec = NULL; /* only need to open once */
200 }
201 if (hasNumberFormat(argv[a])) { /* generating image(s) */
202 if (ofspec == NULL) {
203 SET_FILE_BINARY(ofp);
204 newheader("RADIANCE", ofp);
205 printargs(argc, argv, ofp);
206 fputnow(ofp);
207 }
208 if (nsteps > 1) /* multiple output frames? */
209 for (i = 0; i < nsteps; i++) {
210 CMATRIX *cvec = cm_column(cmtx, i);
211 if (ofspec != NULL) {
212 sprintf(fnbuf, ofspec, i+1);
213 if ((ofp = fopen(fnbuf, "wb")) == NULL) {
214 fprintf(stderr,
215 "%s: cannot open '%s' for output\n",
216 progname, fnbuf);
217 return(1);
218 }
219 newheader("RADIANCE", ofp);
220 printargs(argc, argv, ofp);
221 fputnow(ofp);
222 }
223 fprintf(ofp, "FRAME=%d\n", i+1);
224 if (!sum_images(argv[a], cvec, ofp))
225 return(1);
226 if (ofspec != NULL) {
227 if (fclose(ofp) == EOF) {
228 fprintf(stderr,
229 "%s: error writing to '%s'\n",
230 progname, fnbuf);
231 return(1);
232 }
233 ofp = stdout;
234 }
235 cm_free(cvec);
236 }
237 else if (!sum_images(argv[a], cmtx, ofp))
238 return(1);
239 } else { /* generating vector/matrix */
240 CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
241 CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
242 cm_free(Vmat);
243 if (ofspec != NULL) /* multiple vector files? */
244 for (i = 0; i < nsteps; i++) {
245 CMATRIX *rvec = cm_column(rmtx, i);
246 sprintf(fnbuf, ofspec, i+1);
247 if ((ofp = fopen(fnbuf, "w")) == NULL) {
248 fprintf(stderr,
249 "%s: cannot open '%s' for output\n",
250 progname, fnbuf);
251 return(1);
252 }
253 cm_print(rvec, ofp);
254 if (fclose(ofp) == EOF) {
255 fprintf(stderr,
256 "%s: error writing to '%s'\n",
257 progname, fnbuf);
258 return(1);
259 }
260 ofp = stdout;
261 cm_free(rvec);
262 }
263 else
264 cm_print(rmtx, ofp);
265 cm_free(rmtx);
266 }
267 if (fflush(ofp) == EOF) { /* final clean-up */
268 fprintf(stderr, "%s: write error on output\n", progname);
269 return(1);
270 }
271 cm_free(cmtx);
272 return(0);
273 userr:
274 fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d}] DCspec [skyf]\n",
275 progname);
276 fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d}] Vspec Tbsdf.xml Dmat.dat [skyf]\n",
277 progname);
278 return(1);
279 }