ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dctimestep.c
(Generate patch)

Comparing ray/src/util/dctimestep.c (file contents):
Revision 2.29 by greg, Mon Jan 20 22:18:29 2014 UTC vs.
Revision 2.38 by greg, Tue Apr 10 23:22:42 2018 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   */
9  
10   #include <ctype.h>
11 + #include "platform.h"
12   #include "standard.h"
13   #include "cmatrix.h"
14   #include "platform.h"
# Line 20 | Line 21 | static int
21   sum_images(const char *fspec, const CMATRIX *cv, FILE *fout)
22   {
23          int     myDT = DTfromHeader;
24 <        COLOR   *scanline = NULL;
24 >        COLR    *scanline = NULL;
25          CMATRIX *pmat = NULL;
26          int     myXR=0, myYR=0;
27          int     i, y;
# Line 33 | Line 34 | sum_images(const char *fspec, const CMATRIX *cv, FILE
34                  FILE            *fp;
35                  int             dt, xr, yr;
36                  COLORV          *psp;
37 +                char            *err;
38                                                          /* check for zero */
39                  if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) &&
40                                  (myDT != DTfromHeader) | (i < cv->nrows-1))
41                          continue;
42                                                          /* open next picture */
43                  sprintf(fname, fspec, i);
44 <                if ((fp = fopen(fname, "r")) == NULL) {
44 >                if ((fp = fopen(fname, "rb")) == NULL) {
45                          sprintf(errmsg, "cannot open picture '%s'", fname);
46                          error(SYSTEM, errmsg);
47                  }
48 <                SET_FILE_BINARY(fp);
49 <                dt = getDTfromHeader(fp);
48 >                dt = DTfromHeader;
49 >                if ((err = cm_getheader(&dt, NULL, NULL, fp)) != NULL)
50 >                        error(USER, err);
51                  if ((dt != DTrgbe) & (dt != DTxyze) ||
52                                  !fscnresolu(&xr, &yr, fp)) {
53                          sprintf(errmsg, "file '%s' not a picture", fname);
# Line 53 | Line 56 | sum_images(const char *fspec, const CMATRIX *cv, FILE
56                  if (myDT == DTfromHeader) {             /* on first one */
57                          myDT = dt;
58                          myXR = xr; myYR = yr;
59 <                        scanline = (COLOR *)malloc(sizeof(COLOR)*myXR);
59 >                        scanline = (COLR *)malloc(sizeof(COLR)*myXR);
60                          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                                                          /* finish header */
65 <                        fputformat(myDT==DTrgbe ? COLRFMT : CIEFMT, fout);
65 >                        fputformat((char *)cm_fmt_id[myDT], fout);
66                          fputc('\n', fout);
64                        fprtresolu(myXR, myYR, fout);
67                          fflush(fout);
68                  } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) {
69                          sprintf(errmsg, "picture '%s' format/size mismatch",
# Line 70 | Line 72 | sum_images(const char *fspec, const CMATRIX *cv, FILE
72                  }
73                  psp = pmat->cmem;
74                  for (y = 0; y < yr; y++) {              /* read it in */
75 +                        COLOR   col;
76                          int     x;
77 <                        if (freadscan(scanline, xr, fp) < 0) {
77 >                        if (freadcolrs(scanline, xr, fp) < 0) {
78                                  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 <                                multcolor(scanline[x], scv);
85 <                                addcolor(psp, scanline[x]);
84 >                                if (!scanline[x][EXP])
85 >                                        continue;       /* skip zeroes */
86 >                                colr_color(col, scanline[x]);
87 >                                multcolor(col, scv);
88 >                                addcolor(psp, col);
89                          }
90                  }
91                  fclose(fp);                             /* done this picture */
92          }
93          free(scanline);
94 <                                                        /* write scanlines */
95 <        for (y = 0; y < myYR; y++)
96 <                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 >        i = cm_write(pmat, myDT, fout);                 /* write picture */
95 >        cm_free(pmat);                                  /* free data */
96 >        return(i);
97   }
98  
99   /* check to see if a string contains a %d or %o specification */
# Line 121 | Line 124 | hasNumberFormat(const char *s)
124   int
125   main(int argc, char *argv[])
126   {
127 <        int             skyfmt = DTascii;
128 <        int             nsteps = 1;
127 >        int             skyfmt = DTfromHeader;
128 >        int             outfmt = DTascii;
129 >        int             headout = 1;
130 >        int             nsteps = 0;
131          char            *ofspec = NULL;
132          FILE            *ofp = stdout;
133          CMATRIX         *cmtx;          /* component vector/matrix result */
# Line 135 | Line 140 | main(int argc, char *argv[])
140                  switch (argv[a][1]) {
141                  case 'n':
142                          nsteps = atoi(argv[++a]);
143 <                        if (nsteps <= 0)
143 >                        if (nsteps < 0)
144                                  goto userr;
145 +                        skyfmt = nsteps ? DTascii : DTfromHeader;
146                          break;
147 <                case 'o':
148 <                        ofspec = argv[++a];
147 >                case 'h':
148 >                        headout = !headout;
149                          break;
150                  case 'i':
151                          switch (argv[a][2]) {
# Line 156 | Line 162 | main(int argc, char *argv[])
162                                  goto userr;
163                          }
164                          break;
165 +                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                  default:
184                          goto userr;
185                  }
# Line 163 | Line 187 | main(int argc, char *argv[])
187                  goto userr;
188  
189          if (argc-a > 2) {                       /* VTDs expression */
190 <                CMATRIX *smtx, *Dmat, *Tmat, *imtx;
190 >                CMATRIX         *smtx, *Dmat, *Tmat, *imtx;
191 >                const char      *ccp;
192                                                  /* get sky vector/matrix */
193                  smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
194 +                nsteps = smtx->ncols;
195                                                  /* load BSDF */
196 <                Tmat = cm_loadBTDF(argv[a+1]);
196 >                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                                                  /* load Daylight matrix */
203 <                Dmat = cm_load(argv[a+2], Tmat==NULL ? 0 : Tmat->ncols,
203 >                Dmat = cm_load(argv[a+2], Tmat->ncols,
204                                          smtx->nrows, DTfromHeader);
205                                                  /* multiply vector through */
206                  imtx = cm_multiply(Dmat, smtx);
# Line 179 | Line 210 | main(int argc, char *argv[])
210                  cm_free(imtx);
211          } else {                                /* sky vector/matrix only */
212                  cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
213 +                nsteps = cmtx->ncols;
214          }
215                                                  /* prepare output stream */
216          if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) {
# Line 235 | Line 267 | main(int argc, char *argv[])
267                  CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
268                  CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
269                  cm_free(Vmat);
270 <                if (ofspec != NULL)             /* multiple vector files? */
270 >                if (ofspec != NULL) {           /* multiple vector files? */
271 >                        const char      *wtype = (outfmt==DTascii) ? "w" : "wb";
272                          for (i = 0; i < nsteps; i++) {
273                                  CMATRIX *rvec = cm_column(rmtx, i);
274                                  sprintf(fnbuf, ofspec, i+1);
275 <                                if ((ofp = fopen(fnbuf, "w")) == NULL) {
275 >                                if ((ofp = fopen(fnbuf, wtype)) == NULL) {
276                                          fprintf(stderr,
277                                          "%s: cannot open '%s' for output\n",
278                                                          progname, fnbuf);
279                                          return(1);
280                                  }
281 <                                cm_print(rvec, ofp);
281 > #ifdef getc_unlocked
282 >                                flockfile(ofp);
283 > #endif
284 >                                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 >                                cm_write(rvec, outfmt, ofp);
295                                  if (fclose(ofp) == EOF) {
296                                          fprintf(stderr,
297                                                  "%s: error writing to '%s'\n",
# Line 255 | Line 301 | main(int argc, char *argv[])
301                                  ofp = stdout;
302                                  cm_free(rvec);
303                          }
304 <                else
305 <                        cm_print(rmtx, ofp);
304 >                } else {
305 > #ifdef getc_unlocked
306 >                        flockfile(ofp);
307 > #endif
308 >                        if (outfmt != DTascii)
309 >                                SET_FILE_BINARY(ofp);
310 >                        if (headout) {          /* header output */
311 >                                newheader("RADIANCE", ofp);
312 >                                printargs(argc, argv, ofp);
313 >                                fputnow(ofp);
314 >                                fprintf(ofp, "NROWS=%d\n", rmtx->nrows);
315 >                                fprintf(ofp, "NCOLS=%d\n", rmtx->ncols);
316 >                                fputs("NCOMP=3\n", ofp);
317 >                                fputformat((char *)cm_fmt_id[outfmt], ofp);
318 >                                fputc('\n', ofp);
319 >                        }
320 >                        cm_write(rmtx, outfmt, ofp);
321 >                }
322                  cm_free(rmtx);
323          }
324          if (fflush(ofp) == EOF) {               /* final clean-up */
# Line 266 | Line 328 | main(int argc, char *argv[])
328          cm_free(cmtx);
329          return(0);
330   userr:
331 <        fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d}] DCspec [skyf]\n",
331 >        fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] DCspec [skyf]\n",
332                                  progname);
333 <        fprintf(stderr, "   or: %s [-n nsteps][-o ospec][-i{f|d}] Vspec Tbsdf.xml Dmat.dat [skyf]\n",
333 >        fprintf(stderr, "   or: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] Vspec Tbsdf Dmat.dat [skyf]\n",
334                                  progname);
335          return(1);
336   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines