ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dcglare.c
Revision: 2.6
Committed: Fri Mar 11 02:27:02 2022 UTC (2 years ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, HEAD
Changes since 2.5: +2 -2 lines
Log Message:
feat(dctimestep): Added -x and -y options to specify picture output dimensions

File Contents

# User Rev Content
1 greg 2.1 /*
2     * Compute time-step glare using imageless DGP calculation method.
3     *
4     * N. Jones
5     */
6    
7     /*
8     * Copyright (c) 2017-2019 Nathaniel Jones
9     *
10     * Permission is hereby granted, free of charge, to any person obtaining a copy
11     * of this software and associated documentation files (the "Software"), to deal
12     * in the Software without restriction, including without limitation the rights
13     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14     * copies of the Software, and to permit persons to whom the Software is
15     * furnished to do so, subject to the following conditions:
16     *
17     * The above copyright notice and this permission notice shall be included in all
18     * copies or substantial portions of the Software.
19     *
20     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26     * SOFTWARE.
27     */
28    
29     #include <ctype.h>
30     #include "platform.h"
31     #include "standard.h"
32     #include "cmatrix.h"
33     #include "resolu.h"
34     #include "cmglare.h"
35    
36     char *progname; /* global argv[0] */
37    
38     /* Sum together a set of images and write result to fout */
39     static int
40     sum_images(const char *fspec, const CMATRIX *cv, FILE *fout)
41     {
42     int myDT = DTfromHeader;
43     COLR *scanline = NULL;
44     CMATRIX *pmat = NULL;
45     int myXR=0, myYR=0;
46     int i, y;
47    
48     if (cv->ncols != 1)
49     error(INTERNAL, "expected vector in sum_images()");
50     for (i = 0; i < cv->nrows; i++) {
51     const COLORV *scv = cv_lval(cv,i);
52     int flat_file = 0;
53     char fname[1024];
54     FILE *fp;
55     long data_start;
56     int dt, xr, yr;
57     COLORV *psp;
58     char *err;
59     /* check for zero */
60     if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) &&
61     (myDT != DTfromHeader) | (i < cv->nrows-1))
62     continue;
63     /* open next picture */
64     sprintf(fname, fspec, i);
65     if ((fp = fopen(fname, "rb")) == NULL) {
66     sprintf(errmsg, "cannot open picture '%s'", fname);
67     error(SYSTEM, errmsg);
68     }
69     dt = DTfromHeader;
70 greg 2.3 if ((err = cm_getheader(&dt, NULL, NULL, NULL, NULL, fp)) != NULL)
71 greg 2.1 error(USER, err);
72     if ((dt != DTrgbe) & (dt != DTxyze) ||
73     !fscnresolu(&xr, &yr, fp)) {
74     sprintf(errmsg, "file '%s' not a picture", fname);
75     error(USER, errmsg);
76     }
77     if (myDT == DTfromHeader) { /* on first one */
78     myDT = dt;
79     myXR = xr; myYR = yr;
80     scanline = (COLR *)malloc(sizeof(COLR)*myXR);
81     if (scanline == NULL)
82     error(SYSTEM, "out of memory in sum_images()");
83     pmat = cm_alloc(myYR, myXR);
84     memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR);
85     /* finish header */
86 greg 2.5 fputformat(cm_fmt_id[myDT], fout);
87 greg 2.1 fputc('\n', fout);
88     fflush(fout);
89     } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) {
90     sprintf(errmsg, "picture '%s' format/size mismatch",
91     fname);
92     error(USER, errmsg);
93     }
94     /* flat file check */
95     if ((data_start = ftell(fp)) > 0 && fseek(fp, 0L, SEEK_END) == 0) {
96     flat_file = (ftell(fp) == data_start + sizeof(COLR)*xr*yr);
97     if (fseek(fp, data_start, SEEK_SET) < 0) {
98     sprintf(errmsg, "cannot seek on picture '%s'", fname);
99     error(SYSTEM, errmsg);
100     }
101     }
102     psp = pmat->cmem;
103     for (y = 0; y < yr; y++) { /* read it in */
104     COLOR col;
105     int x;
106     if (flat_file ? getbinary(scanline, sizeof(COLR), xr, fp) != xr :
107     freadcolrs(scanline, xr, fp) < 0) {
108     sprintf(errmsg, "error reading picture '%s'",
109     fname);
110     error(SYSTEM, errmsg);
111     }
112     /* sum in scanline */
113     for (x = 0; x < xr; x++, psp += 3) {
114     if (!scanline[x][EXP])
115     continue; /* skip zeroes */
116     colr_color(col, scanline[x]);
117     multcolor(col, scv);
118     addcolor(psp, col);
119     }
120     }
121     fclose(fp); /* done this picture */
122     }
123     free(scanline);
124     i = cm_write(pmat, myDT, fout); /* write picture */
125     cm_free(pmat); /* free data */
126     return(i);
127     }
128    
129     /* check to see if a string contains a %d or %o specification */
130     static int
131     hasNumberFormat(const char *s)
132     {
133     if (s == NULL)
134     return(0);
135    
136     while (*s) {
137     while (*s != '%')
138     if (!*s++)
139     return(0);
140     if (*++s == '%') { /* ignore "%%" */
141     ++s;
142     continue;
143     }
144     while (isdigit(*s)) /* field length */
145     ++s;
146     /* field we'll use? */
147     if ((*s == 'd') | (*s == 'i') | (*s == 'o') |
148     (*s == 'x') | (*s == 'X'))
149     return(1);
150     }
151     return(0); /* didn't find one */
152     }
153    
154     int
155     main(int argc, char *argv[])
156     {
157     int skyfmt = DTfromHeader;
158     int outfmt = DTascii;
159     int headout = 1;
160     int nsteps = 0;
161     char *ofspec = NULL;
162     FILE *ofp = stdout;
163     CMATRIX *cmtx; /* component vector/matrix result */
164     char fnbuf[256];
165     int a, i;
166     #ifdef DC_GLARE
167     char *direct_path = NULL;
168     char *schedule_path = NULL;
169     int *occupancy = NULL;
170     int start_hour = 0;
171     int end_hour = 24;
172     double dgp_limit = -1;
173     double dgp_threshold = 2000;
174     char *view_path = NULL;
175     FILE *fp;
176     float *dgp_values = NULL;
177     FVECT vdir, vup;
178     FVECT *views = NULL;
179     int viewfmt = DTascii;
180    
181     vdir[0] = vdir[1] = vdir[2] = vup[0] = vup[1] = 0;
182     vup[2] = 1;
183    
184     clock_t timer = clock();
185     #endif /* DC_GLARE */
186    
187     progname = argv[0];
188     /* get options */
189     for (a = 1; a < argc && argv[a][0] == '-'; a++)
190     switch (argv[a][1]) {
191     case 'n':
192     nsteps = atoi(argv[++a]);
193     if (nsteps < 0)
194     goto userr;
195     skyfmt = nsteps ? DTascii : DTfromHeader;
196     break;
197     case 'h':
198     headout = !headout;
199     break;
200     case 'i':
201     switch (argv[a][2]) {
202     case 'f':
203     skyfmt = DTfloat;
204     break;
205     case 'd':
206     skyfmt = DTdouble;
207     break;
208     case 'a':
209     skyfmt = DTascii;
210     break;
211     default:
212     goto userr;
213     }
214     break;
215     case 'o':
216     switch (argv[a][2]) {
217     #ifndef DC_GLARE
218     case '\0': /* output specification (not format) */
219     ofspec = argv[++a];
220     break;
221     #endif /* DC_GLARE */
222     case 'f':
223     outfmt = DTfloat;
224     break;
225     case 'd':
226     outfmt = DTdouble;
227     break;
228     case 'a':
229     outfmt = DTascii;
230     break;
231     default:
232     goto userr;
233     }
234     break;
235     #ifdef DC_GLARE
236     case 's':
237     switch (argv[a][2]) {
238     case 'f': /* occupancy schedule file */
239     schedule_path = argv[++a];
240     break;
241     case 's': /* occupancy start hour */
242     start_hour = atoi(argv[++a]);
243     break;
244     case 'e': /* occupancy end hour */
245     end_hour = atoi(argv[++a]);
246     break;
247     default:
248     goto userr;
249     }
250     break;
251     case 'l': /* perceptible glare threshold */
252     dgp_limit = atof(argv[++a]);
253     break;
254     case 'b': /* luminance threshold */
255     dgp_threshold = atof(argv[++a]);
256     break;
257     case 'v':
258     switch (argv[a][2]) {
259     case 'd': /* forward */
260     //check(3, "fff");
261     vdir[0] = atof(argv[++a]);
262     vdir[1] = atof(argv[++a]);
263     vdir[2] = atof(argv[++a]);
264     if (normalize(vdir) == 0.0) goto userr;
265     break;
266     case 'u': /* up */
267     //check(3, "fff");
268     vup[0] = atof(argv[++a]);
269     vup[1] = atof(argv[++a]);
270     vup[2] = atof(argv[++a]);
271     if (normalize(vup) == 0.0) goto userr;
272     break;
273     case 'f': /* view directions file */
274     //check(2, "s");
275     view_path = argv[++a];
276     break;
277     case 'i':
278     switch (argv[a][3]) {
279     case 'f':
280     viewfmt = DTfloat;
281     break;
282     case 'd':
283     viewfmt = DTdouble;
284     break;
285     case 'a':
286     viewfmt = DTascii;
287     break;
288     default:
289     goto userr;
290     }
291     default:
292     goto userr;
293     }
294     break;
295     #endif /* DC_GLARE */
296     default:
297     goto userr;
298     }
299     #ifdef DC_GLARE
300     if ((argc-a < 2) | (argc-a > 5))
301     goto userr;
302     /* single bounce daylight coefficients file */
303 greg 2.4 direct_path = argv[a++];
304 greg 2.1 #else
305     if ((argc-a < 1) | (argc-a > 4))
306     goto userr;
307     #endif /* DC_GLARE */
308    
309     if (argc-a > 2) { /* VTDs expression */
310     CMATRIX *smtx, *Dmat, *Tmat, *imtx;
311     const char *ccp;
312     /* get sky vector/matrix */
313     smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
314     nsteps = smtx->ncols;
315     /* load BSDF */
316     if (argv[a+1][0] != '!' &&
317     (ccp = strrchr(argv[a+1], '.')) != NULL &&
318     !strcasecmp(ccp+1, "XML"))
319     Tmat = cm_loadBTDF(argv[a+1]);
320     else
321     Tmat = cm_load(argv[a+1], 0, 0, DTfromHeader);
322     /* load Daylight matrix */
323     Dmat = cm_load(argv[a+2], Tmat->ncols,
324     smtx->nrows, DTfromHeader);
325     /* multiply vector through */
326     imtx = cm_multiply(Dmat, smtx);
327     cm_free(Dmat); cm_free(smtx);
328     cmtx = cm_multiply(Tmat, imtx);
329     cm_free(Tmat);
330     cm_free(imtx);
331     } else { /* sky vector/matrix only */
332     //TIMER(timer, "read args");
333     cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
334     nsteps = cmtx->ncols;
335     //TIMER(timer, "load sky matrix");
336     }
337     /* prepare output stream */
338     if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) {
339     sprintf(fnbuf, ofspec, 1);
340     ofspec = fnbuf;
341     }
342     if (ofspec != NULL && !hasNumberFormat(ofspec)) {
343     if ((ofp = fopen(ofspec, "w")) == NULL) {
344     fprintf(stderr, "%s: cannot open '%s' for output\n",
345     progname, ofspec);
346     return(1);
347     }
348     ofspec = NULL; /* only need to open once */
349     }
350     if (hasNumberFormat(argv[a])) { /* generating image(s) */
351     if (ofspec == NULL) {
352     SET_FILE_BINARY(ofp);
353     newheader("RADIANCE", ofp);
354     printargs(argc, argv, ofp);
355     fputnow(ofp);
356     }
357     if (nsteps > 1) /* multiple output frames? */
358     for (i = 0; i < nsteps; i++) {
359     CMATRIX *cvec = cm_column(cmtx, i);
360     if (ofspec != NULL) {
361     sprintf(fnbuf, ofspec, i);
362     if ((ofp = fopen(fnbuf, "wb")) == NULL) {
363     fprintf(stderr,
364     "%s: cannot open '%s' for output\n",
365     progname, fnbuf);
366     return(1);
367     }
368     newheader("RADIANCE", ofp);
369     printargs(argc, argv, ofp);
370     fputnow(ofp);
371     }
372     fprintf(ofp, "FRAME=%d\n", i);
373     if (!sum_images(argv[a], cvec, ofp))
374     return(1);
375     if (ofspec != NULL) {
376     if (fclose(ofp) == EOF) {
377     fprintf(stderr,
378     "%s: error writing to '%s'\n",
379     progname, fnbuf);
380     return(1);
381     }
382     ofp = stdout;
383     }
384     cm_free(cvec);
385     }
386     else if (!sum_images(argv[a], cmtx, ofp))
387     return(1);
388     } else { /* generating vector/matrix */
389     CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
390     //TIMER(timer, "load view matrix");
391     CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
392     //TIMER(timer, "multiply");
393     cm_free(Vmat);
394     #ifdef DC_GLARE
395     if (direct_path) { /* Do glare autonomy calculation */
396     /* Load occupancy schedule */
397     occupancy = (int*)malloc(nsteps * sizeof(int));
398     if (!occupancy) {
399     fprintf(stderr,
400     "%s: out of memory for schedule\n",
401     progname);
402     return(1);
403     }
404     if (schedule_path) {
405     if ((fp = fopen(schedule_path, "r")) == NULL) {
406     fprintf(stderr,
407     "%s: cannot open input file \"%s\"\n",
408     progname, schedule_path);
409     return(1);
410     }
411     if (cm_load_schedule(nsteps, occupancy, fp) < 0) return(1);
412     }
413     else {
414     for (i = 0; i < nsteps; i++) {
415     /* Assume hourly spacing */
416     int hour = i % 24;
417     occupancy[i] = ((hour >= start_hour) & (hour < end_hour));
418     }
419     }
420     //TIMER(timer, "load occupancy schedule");
421    
422     /* Load view directions */
423     if (view_path == NULL) {
424     if (vdir[0] == 0.0f && vdir[1] == 0.0f && vdir[2] == 0.0f) {
425     fprintf(stderr,
426     "%s: missing view direction\n",
427     progname);
428     return(1);
429     }
430     }
431     else {
432     if ((fp = fopen(view_path, "r")) == NULL) {
433     fprintf(stderr,
434     "%s: cannot open input file \"%s\"\n",
435     progname, view_path);
436     return(1);
437     }
438     if (viewfmt != DTascii)
439     SET_FILE_BINARY(fp);
440     views = cm_load_views(rmtx->nrows, viewfmt, fp);
441     if (!views) return(1);
442     //TIMER(timer, "load views");
443     }
444    
445     /* Calculate glare values */
446     Vmat = cm_load(direct_path, 0, cmtx->nrows, DTfromHeader);
447     //TIMER(timer, "load direct matrix");
448     dgp_values = cm_glare(Vmat, rmtx, cmtx, occupancy, dgp_limit, dgp_threshold, views, vdir, vup);
449     //TIMER(timer, "calculate dgp");
450     free(views);
451     cm_free(Vmat);
452    
453     /* Check successful calculation */
454     if (!dgp_values) return(1);
455     }
456     #endif /* DC_GLARE */
457     if (ofspec != NULL) { /* multiple vector files? */
458     const char *wtype = (outfmt==DTascii) ? "w" : "wb";
459     for (i = 0; i < nsteps; i++) {
460     CMATRIX *rvec = cm_column(rmtx, i);
461     sprintf(fnbuf, ofspec, i);
462     if ((ofp = fopen(fnbuf, wtype)) == NULL) {
463     fprintf(stderr,
464     "%s: cannot open '%s' for output\n",
465     progname, fnbuf);
466     return(1);
467     }
468     #ifdef getc_unlocked
469     flockfile(ofp);
470     #endif
471     if (headout) { /* header output */
472     newheader("RADIANCE", ofp);
473     printargs(argc, argv, ofp);
474     fputnow(ofp);
475     fprintf(ofp, "FRAME=%d\n", i);
476     fprintf(ofp, "NROWS=%d\n", rvec->nrows);
477     fputs("NCOLS=1\nNCOMP=3\n", ofp);
478 greg 2.6 if ((outfmt == DTfloat) | (outfmt == DTdouble))
479 greg 2.1 fputendian(ofp);
480 greg 2.5 fputformat(cm_fmt_id[outfmt], ofp);
481 greg 2.1 fputc('\n', ofp);
482     }
483     cm_write(rvec, outfmt, ofp);
484     if (fclose(ofp) == EOF) {
485     fprintf(stderr,
486     "%s: error writing to '%s'\n",
487     progname, fnbuf);
488     return(1);
489     }
490     ofp = stdout;
491     cm_free(rvec);
492     }
493     } else {
494     #ifdef getc_unlocked
495     flockfile(ofp);
496     #endif
497     if (outfmt != DTascii)
498     SET_FILE_BINARY(ofp);
499     if (headout) { /* header output */
500     newheader("RADIANCE", ofp);
501     printargs(argc, argv, ofp);
502     fputnow(ofp);
503     fprintf(ofp, "NROWS=%d\n", rmtx->nrows);
504     #ifdef DC_GLARE
505     fprintf(ofp, "NCOLS=%d\n", (!dgp_values || dgp_limit < 0) ? rmtx->ncols : 1);
506     fprintf(ofp, "NCOMP=%d\n", dgp_values ? 1 : 3);
507     #else
508     fprintf(ofp, "NCOLS=%d\n", rmtx->ncols);
509     fputs("NCOMP=3\n", ofp);
510     #endif /* DC_GLARE */
511 greg 2.6 if ((outfmt == DTfloat) | (outfmt == DTdouble))
512 greg 2.1 fputendian(ofp);
513 greg 2.5 fputformat(cm_fmt_id[outfmt], ofp);
514 greg 2.1 fputc('\n', ofp);
515     }
516     #ifdef DC_GLARE
517     if (dgp_values) { /* Write glare autonomy */
518     cm_write_glare(dgp_values, rmtx->nrows, dgp_limit < 0 ? rmtx->ncols : 1, outfmt, ofp);
519     free(dgp_values);
520     //TIMER(timer, "write");
521     }
522     else
523     #endif /* DC_GLARE */
524     cm_write(rmtx, outfmt, ofp);
525     }
526     cm_free(rmtx);
527     }
528     if (fflush(ofp) == EOF) { /* final clean-up */
529     fprintf(stderr, "%s: write error on output\n", progname);
530     return(1);
531     }
532     cm_free(cmtx);
533     return(0);
534     userr:
535     #ifdef DC_GLARE
536     fprintf(stderr, "Usage: %s [-n nsteps][-i{f|d|h}][-o{f|d}][-l limit][-b threshold][{-sf occupancy|-ss start -se end}]{-vf views [-vi{f|d}]|-vd x y z}[-vu x y z] DCdirect DCtotal [skyf]\n",
537     progname);
538     fprintf(stderr, " or: %s [-n nsteps][-i{f|d|h}][-o{f|d}][-l limit][-b threshold][{-sf occupancy|-ss start -se end}]{-vf views [-vi{f|d}]|-vd x y z}[-vu x y z] DCdirect Vspec Tbsdf Dmat.dat [skyf]\n",
539     progname);
540     #else
541     fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] DCspec [skyf]\n",
542     progname);
543     fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] Vspec Tbsdf Dmat.dat [skyf]\n",
544     progname);
545     #endif /* DC_GLARE */
546     return(1);
547     }