ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dctimestep.c
Revision: 2.38
Committed: Tue Apr 10 23:22:42 2018 UTC (4 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2
Changes since 2.37: +10 -6 lines
Log Message:
Improved speed on zero-valued pixels in summation

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.38 static const char RCSid[] = "$Id: dctimestep.c,v 2.37 2016/03/06 01:13:18 schorsch 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 schorsch 2.37 #include "platform.h"
12 greg 2.1 #include "standard.h"
13 greg 2.28 #include "cmatrix.h"
14 greg 2.1 #include "platform.h"
15     #include "resolu.h"
16    
17     char *progname; /* global argv[0] */
18    
19 greg 2.24 /* Sum together a set of images and write result to fout */
20 greg 2.1 static int
21 greg 2.13 sum_images(const char *fspec, const CMATRIX *cv, FILE *fout)
22 greg 2.1 {
23     int myDT = DTfromHeader;
24 greg 2.38 COLR *scanline = NULL;
25 greg 2.24 CMATRIX *pmat = NULL;
26     int myXR=0, myYR=0;
27 greg 2.1 int i, y;
28    
29     if (cv->ncols != 1)
30 greg 2.35 error(INTERNAL, "expected vector in sum_images()");
31 greg 2.1 for (i = 0; i < cv->nrows; i++) {
32     const COLORV *scv = cv_lval(cv,i);
33     char fname[1024];
34     FILE *fp;
35     int dt, xr, yr;
36     COLORV *psp;
37 greg 2.31 char *err;
38 greg 2.14 /* check for zero */
39 greg 2.19 if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) &&
40     (myDT != DTfromHeader) | (i < cv->nrows-1))
41 greg 2.14 continue;
42 greg 2.1 /* open next picture */
43     sprintf(fname, fspec, i);
44 greg 2.31 if ((fp = fopen(fname, "rb")) == NULL) {
45 greg 2.1 sprintf(errmsg, "cannot open picture '%s'", fname);
46     error(SYSTEM, errmsg);
47     }
48 greg 2.31 dt = DTfromHeader;
49     if ((err = cm_getheader(&dt, NULL, NULL, fp)) != NULL)
50     error(USER, err);
51 greg 2.1 if ((dt != DTrgbe) & (dt != DTxyze) ||
52     !fscnresolu(&xr, &yr, fp)) {
53     sprintf(errmsg, "file '%s' not a picture", fname);
54     error(USER, errmsg);
55     }
56     if (myDT == DTfromHeader) { /* on first one */
57     myDT = dt;
58     myXR = xr; myYR = yr;
59 greg 2.38 scanline = (COLR *)malloc(sizeof(COLR)*myXR);
60 greg 2.1 if (scanline == NULL)
61     error(SYSTEM, "out of memory in sum_images()");
62     pmat = cm_alloc(myYR, myXR);
63     memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR);
64 greg 2.2 /* finish header */
65 greg 2.30 fputformat((char *)cm_fmt_id[myDT], fout);
66 greg 2.13 fputc('\n', fout);
67     fflush(fout);
68 greg 2.1 } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) {
69     sprintf(errmsg, "picture '%s' format/size mismatch",
70     fname);
71     error(USER, errmsg);
72     }
73     psp = pmat->cmem;
74     for (y = 0; y < yr; y++) { /* read it in */
75 greg 2.38 COLOR col;
76 greg 2.1 int x;
77 greg 2.38 if (freadcolrs(scanline, xr, fp) < 0) {
78 greg 2.1 sprintf(errmsg, "error reading picture '%s'",
79     fname);
80     error(SYSTEM, errmsg);
81     }
82     /* sum in scanline */
83     for (x = 0; x < xr; x++, psp += 3) {
84 greg 2.38 if (!scanline[x][EXP])
85     continue; /* skip zeroes */
86     colr_color(col, scanline[x]);
87     multcolor(col, scv);
88     addcolor(psp, col);
89 greg 2.1 }
90     }
91     fclose(fp); /* done this picture */
92     }
93     free(scanline);
94 greg 2.30 i = cm_write(pmat, myDT, fout); /* write picture */
95     cm_free(pmat); /* free data */
96     return(i);
97 greg 2.1 }
98    
99 greg 2.11 /* check to see if a string contains a %d or %o specification */
100     static int
101     hasNumberFormat(const char *s)
102 greg 2.1 {
103 greg 2.24 if (s == NULL)
104     return(0);
105    
106 greg 2.18 while (*s) {
107     while (*s != '%')
108     if (!*s++)
109     return(0);
110     if (*++s == '%') { /* ignore "%%" */
111     ++s;
112     continue;
113     }
114     while (isdigit(*s)) /* field length */
115     ++s;
116     /* field we'll use? */
117     if ((*s == 'd') | (*s == 'i') | (*s == 'o') |
118     (*s == 'x') | (*s == 'X'))
119     return(1);
120     }
121     return(0); /* didn't find one */
122 greg 2.1 }
123    
124     int
125     main(int argc, char *argv[])
126     {
127 greg 2.34 int skyfmt = DTfromHeader;
128 greg 2.30 int outfmt = DTascii;
129 greg 2.34 int headout = 1;
130     int nsteps = 0;
131 greg 2.24 char *ofspec = NULL;
132     FILE *ofp = stdout;
133     CMATRIX *cmtx; /* component vector/matrix result */
134     char fnbuf[256];
135     int a, i;
136 greg 2.1
137     progname = argv[0];
138 greg 2.24 /* get options */
139 greg 2.25 for (a = 1; a < argc && argv[a][0] == '-'; a++)
140     switch (argv[a][1]) {
141 greg 2.24 case 'n':
142     nsteps = atoi(argv[++a]);
143 greg 2.31 if (nsteps < 0)
144 greg 2.24 goto userr;
145 greg 2.34 skyfmt = nsteps ? DTascii : DTfromHeader;
146     break;
147     case 'h':
148     headout = !headout;
149 greg 2.24 break;
150 greg 2.25 case 'i':
151     switch (argv[a][2]) {
152     case 'f':
153     skyfmt = DTfloat;
154     break;
155     case 'd':
156     skyfmt = DTdouble;
157     break;
158     case 'a':
159     skyfmt = DTascii;
160     break;
161     default:
162     goto userr;
163     }
164     break;
165 greg 2.30 case 'o':
166     switch (argv[a][2]) {
167     case '\0': /* output specification (not format) */
168     ofspec = argv[++a];
169     break;
170     case 'f':
171     outfmt = DTfloat;
172     break;
173     case 'd':
174     outfmt = DTdouble;
175     break;
176     case 'a':
177     outfmt = DTascii;
178     break;
179     default:
180     goto userr;
181     }
182     break;
183 greg 2.24 default:
184     goto userr;
185     }
186     if ((argc-a < 1) | (argc-a > 4))
187     goto userr;
188 greg 2.1
189 greg 2.24 if (argc-a > 2) { /* VTDs expression */
190 greg 2.36 CMATRIX *smtx, *Dmat, *Tmat, *imtx;
191     const char *ccp;
192 greg 2.24 /* get sky vector/matrix */
193 greg 2.25 smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
194 greg 2.35 nsteps = smtx->ncols;
195 greg 2.12 /* load BSDF */
196 greg 2.36 if (argv[a+1][0] != '!' &&
197     (ccp = strrchr(argv[a+1], '.')) != NULL &&
198     !strcasecmp(ccp+1, "XML"))
199     Tmat = cm_loadBTDF(argv[a+1]);
200     else
201     Tmat = cm_load(argv[a+1], 0, 0, DTfromHeader);
202 greg 2.5 /* load Daylight matrix */
203 greg 2.36 Dmat = cm_load(argv[a+2], Tmat->ncols,
204 greg 2.24 smtx->nrows, DTfromHeader);
205 greg 2.1 /* multiply vector through */
206 greg 2.24 imtx = cm_multiply(Dmat, smtx);
207     cm_free(Dmat); cm_free(smtx);
208 greg 2.29 cmtx = cm_multiply(Tmat, imtx);
209     cm_free(Tmat);
210 greg 2.24 cm_free(imtx);
211     } else { /* sky vector/matrix only */
212 greg 2.25 cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
213 greg 2.35 nsteps = cmtx->ncols;
214 greg 2.24 }
215     /* prepare output stream */
216     if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) {
217     sprintf(fnbuf, ofspec, 1);
218     ofspec = fnbuf;
219     }
220     if (ofspec != NULL && !hasNumberFormat(ofspec)) {
221     if ((ofp = fopen(ofspec, "w")) == NULL) {
222     fprintf(stderr, "%s: cannot open '%s' for output\n",
223     progname, ofspec);
224     return(1);
225     }
226     ofspec = NULL; /* only need to open once */
227 greg 2.12 }
228 greg 2.24 if (hasNumberFormat(argv[a])) { /* generating image(s) */
229     if (ofspec == NULL) {
230     SET_FILE_BINARY(ofp);
231     newheader("RADIANCE", ofp);
232     printargs(argc, argv, ofp);
233     fputnow(ofp);
234     }
235     if (nsteps > 1) /* multiple output frames? */
236     for (i = 0; i < nsteps; i++) {
237     CMATRIX *cvec = cm_column(cmtx, i);
238     if (ofspec != NULL) {
239     sprintf(fnbuf, ofspec, i+1);
240     if ((ofp = fopen(fnbuf, "wb")) == NULL) {
241     fprintf(stderr,
242     "%s: cannot open '%s' for output\n",
243     progname, fnbuf);
244     return(1);
245     }
246     newheader("RADIANCE", ofp);
247     printargs(argc, argv, ofp);
248     fputnow(ofp);
249     }
250     fprintf(ofp, "FRAME=%d\n", i+1);
251     if (!sum_images(argv[a], cvec, ofp))
252     return(1);
253     if (ofspec != NULL) {
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     }
262     cm_free(cvec);
263     }
264     else if (!sum_images(argv[a], cmtx, ofp))
265 greg 2.1 return(1);
266 greg 2.24 } else { /* generating vector/matrix */
267     CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
268     CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
269 greg 2.1 cm_free(Vmat);
270 greg 2.30 if (ofspec != NULL) { /* multiple vector files? */
271     const char *wtype = (outfmt==DTascii) ? "w" : "wb";
272 greg 2.24 for (i = 0; i < nsteps; i++) {
273     CMATRIX *rvec = cm_column(rmtx, i);
274     sprintf(fnbuf, ofspec, i+1);
275 greg 2.30 if ((ofp = fopen(fnbuf, wtype)) == NULL) {
276 greg 2.24 fprintf(stderr,
277     "%s: cannot open '%s' for output\n",
278     progname, fnbuf);
279     return(1);
280     }
281 greg 2.31 #ifdef getc_unlocked
282     flockfile(ofp);
283     #endif
284 greg 2.34 if (headout) { /* header output */
285     newheader("RADIANCE", ofp);
286     printargs(argc, argv, ofp);
287     fputnow(ofp);
288     fprintf(ofp, "FRAME=%d\n", i+1);
289     fprintf(ofp, "NROWS=%d\n", rvec->nrows);
290     fputs("NCOLS=1\nNCOMP=3\n", ofp);
291     fputformat((char *)cm_fmt_id[outfmt], ofp);
292     fputc('\n', ofp);
293     }
294 greg 2.30 cm_write(rvec, outfmt, ofp);
295 greg 2.24 if (fclose(ofp) == EOF) {
296     fprintf(stderr,
297     "%s: error writing to '%s'\n",
298     progname, fnbuf);
299     return(1);
300     }
301     ofp = stdout;
302     cm_free(rvec);
303     }
304 greg 2.30 } else {
305 greg 2.31 #ifdef getc_unlocked
306     flockfile(ofp);
307     #endif
308 greg 2.30 if (outfmt != DTascii)
309     SET_FILE_BINARY(ofp);
310 greg 2.34 if (headout) { /* header output */
311 greg 2.30 newheader("RADIANCE", ofp);
312     printargs(argc, argv, ofp);
313     fputnow(ofp);
314 greg 2.33 fprintf(ofp, "NROWS=%d\n", rmtx->nrows);
315 greg 2.31 fprintf(ofp, "NCOLS=%d\n", rmtx->ncols);
316     fputs("NCOMP=3\n", ofp);
317 greg 2.30 fputformat((char *)cm_fmt_id[outfmt], ofp);
318     fputc('\n', ofp);
319     }
320     cm_write(rmtx, outfmt, ofp);
321     }
322 greg 2.24 cm_free(rmtx);
323     }
324     if (fflush(ofp) == EOF) { /* final clean-up */
325     fprintf(stderr, "%s: write error on output\n", progname);
326     return(1);
327 greg 2.1 }
328 greg 2.24 cm_free(cmtx);
329 greg 2.1 return(0);
330 greg 2.24 userr:
331 greg 2.32 fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] DCspec [skyf]\n",
332 greg 2.24 progname);
333 greg 2.36 fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] Vspec Tbsdf Dmat.dat [skyf]\n",
334 greg 2.24 progname);
335     return(1);
336 greg 2.1 }