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 (10 years, 1 month 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

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.28 static const char RCSid[] = "$Id: dctimestep.c,v 2.27 2013/01/19 22:30:55 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     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 greg 2.14 /* check for zero */
37 greg 2.19 if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) &&
38     (myDT != DTfromHeader) | (i < cv->nrows-1))
39 greg 2.14 continue;
40 greg 2.1 /* 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 greg 2.2 /* finish header */
62 greg 2.13 fputformat(myDT==DTrgbe ? COLRFMT : CIEFMT, fout);
63     fputc('\n', fout);
64     fprtresolu(myXR, myYR, fout);
65     fflush(fout);
66 greg 2.1 } 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 greg 2.13 if (fwritescan((COLOR *)cm_lval(pmat, y, 0), myXR, fout) < 0)
91 greg 2.1 return(0);
92     cm_free(pmat); /* all done */
93 greg 2.13 return(fflush(fout) == 0);
94 greg 2.1 }
95    
96 greg 2.11 /* check to see if a string contains a %d or %o specification */
97     static int
98     hasNumberFormat(const char *s)
99 greg 2.1 {
100 greg 2.24 if (s == NULL)
101     return(0);
102    
103 greg 2.18 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 greg 2.1 }
120    
121     int
122     main(int argc, char *argv[])
123     {
124 greg 2.25 int skyfmt = DTascii;
125 greg 2.24 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 greg 2.1
132     progname = argv[0];
133 greg 2.24 /* get options */
134 greg 2.25 for (a = 1; a < argc && argv[a][0] == '-'; a++)
135     switch (argv[a][1]) {
136 greg 2.24 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 greg 2.25 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 greg 2.24 default:
160     goto userr;
161     }
162     if ((argc-a < 1) | (argc-a > 4))
163     goto userr;
164 greg 2.1
165 greg 2.24 if (argc-a > 2) { /* VTDs expression */
166     CMATRIX *smtx, *Dmat, *Tmat, *imtx;
167 greg 2.20 COLOR tLamb;
168 greg 2.24 /* get sky vector/matrix */
169 greg 2.25 smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
170 greg 2.12 /* load BSDF */
171 greg 2.24 Tmat = cm_loadBSDF(argv[a+1], tLamb);
172 greg 2.5 /* load Daylight matrix */
173 greg 2.24 Dmat = cm_load(argv[a+2], Tmat==NULL ? 0 : Tmat->ncols,
174     smtx->nrows, DTfromHeader);
175 greg 2.1 /* multiply vector through */
176 greg 2.24 imtx = cm_multiply(Dmat, smtx);
177     cm_free(Dmat); cm_free(smtx);
178 greg 2.20 if (Tmat == NULL) { /* diffuse only */
179 greg 2.24 cmtx = cm_scale(imtx, tLamb);
180 greg 2.20 } else { /* else apply BTDF matrix */
181 greg 2.24 cmtx = cm_multiply(Tmat, imtx);
182 greg 2.20 cm_free(Tmat);
183     }
184 greg 2.24 cm_free(imtx);
185     } else { /* sky vector/matrix only */
186 greg 2.25 cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
187 greg 2.24 }
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 greg 2.12 }
201 greg 2.24 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 greg 2.1 return(1);
239 greg 2.24 } else { /* generating vector/matrix */
240     CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
241     CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
242 greg 2.1 cm_free(Vmat);
243 greg 2.24 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 greg 2.1 }
271 greg 2.24 cm_free(cmtx);
272 greg 2.1 return(0);
273 greg 2.24 userr:
274 greg 2.25 fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d}] DCspec [skyf]\n",
275 greg 2.24 progname);
276 greg 2.25 fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d}] Vspec Tbsdf.xml Dmat.dat [skyf]\n",
277 greg 2.24 progname);
278     return(1);
279 greg 2.1 }