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

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.30 static const char RCSid[] = "$Id: dctimestep.c,v 2.29 2014/01/20 22:18:29 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.30 fputformat((char *)cm_fmt_id[myDT], fout);
63 greg 2.13 fputc('\n', fout);
64     fflush(fout);
65 greg 2.1 } 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 greg 2.30 i = cm_write(pmat, myDT, fout); /* write picture */
88     cm_free(pmat); /* free data */
89     return(i);
90 greg 2.1 }
91    
92 greg 2.11 /* check to see if a string contains a %d or %o specification */
93     static int
94     hasNumberFormat(const char *s)
95 greg 2.1 {
96 greg 2.24 if (s == NULL)
97     return(0);
98    
99 greg 2.18 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 greg 2.1 }
116    
117     int
118     main(int argc, char *argv[])
119     {
120 greg 2.25 int skyfmt = DTascii;
121 greg 2.30 int outfmt = DTascii;
122 greg 2.24 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 greg 2.1
129     progname = argv[0];
130 greg 2.24 /* get options */
131 greg 2.25 for (a = 1; a < argc && argv[a][0] == '-'; a++)
132     switch (argv[a][1]) {
133 greg 2.24 case 'n':
134     nsteps = atoi(argv[++a]);
135     if (nsteps <= 0)
136     goto userr;
137     break;
138 greg 2.25 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 greg 2.30 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 greg 2.24 default:
172     goto userr;
173     }
174     if ((argc-a < 1) | (argc-a > 4))
175     goto userr;
176 greg 2.1
177 greg 2.24 if (argc-a > 2) { /* VTDs expression */
178     CMATRIX *smtx, *Dmat, *Tmat, *imtx;
179     /* get sky vector/matrix */
180 greg 2.25 smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
181 greg 2.12 /* load BSDF */
182 greg 2.29 Tmat = cm_loadBTDF(argv[a+1]);
183 greg 2.5 /* load Daylight matrix */
184 greg 2.24 Dmat = cm_load(argv[a+2], Tmat==NULL ? 0 : Tmat->ncols,
185     smtx->nrows, DTfromHeader);
186 greg 2.1 /* multiply vector through */
187 greg 2.24 imtx = cm_multiply(Dmat, smtx);
188     cm_free(Dmat); cm_free(smtx);
189 greg 2.29 cmtx = cm_multiply(Tmat, imtx);
190     cm_free(Tmat);
191 greg 2.24 cm_free(imtx);
192     } else { /* sky vector/matrix only */
193 greg 2.25 cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
194 greg 2.24 }
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 greg 2.12 }
208 greg 2.24 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 greg 2.1 return(1);
246 greg 2.24 } else { /* generating vector/matrix */
247     CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
248     CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
249 greg 2.1 cm_free(Vmat);
250 greg 2.30 if (ofspec != NULL) { /* multiple vector files? */
251     const char *wtype = (outfmt==DTascii) ? "w" : "wb";
252 greg 2.24 for (i = 0; i < nsteps; i++) {
253     CMATRIX *rvec = cm_column(rmtx, i);
254     sprintf(fnbuf, ofspec, i+1);
255 greg 2.30 if ((ofp = fopen(fnbuf, wtype)) == NULL) {
256 greg 2.24 fprintf(stderr,
257     "%s: cannot open '%s' for output\n",
258     progname, fnbuf);
259     return(1);
260     }
261 greg 2.30 cm_write(rvec, outfmt, ofp);
262 greg 2.24 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 greg 2.30 } 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 greg 2.24 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 greg 2.1 }
289 greg 2.24 cm_free(cmtx);
290 greg 2.1 return(0);
291 greg 2.24 userr:
292 greg 2.30 fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d}][-o{f|d}] DCspec [skyf]\n",
293 greg 2.24 progname);
294 greg 2.30 fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d}][-o{f|d}] Vspec Tbsdf.xml Dmat.dat [skyf]\n",
295 greg 2.24 progname);
296     return(1);
297 greg 2.1 }