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.33 by greg, Sun Jun 1 03:20:29 2014 UTC vs.
Revision 2.47 by greg, Fri Mar 11 02:27:02 2022 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;
28  
29          if (cv->ncols != 1)
30 <                error(INTERNAL, "expected matrix in sum_images()");
30 >                error(INTERNAL, "expected vector in sum_images()");
31          for (i = 0; i < cv->nrows; i++) {
32                  const COLORV    *scv = cv_lval(cv,i);
33 +                int             flat_file = 0;
34                  char            fname[1024];
35                  FILE            *fp;
36 +                long            data_start;
37                  int             dt, xr, yr;
38                  COLORV          *psp;
39                  char            *err;
# Line 45 | Line 48 | sum_images(const char *fspec, const CMATRIX *cv, FILE
48                          error(SYSTEM, errmsg);
49                  }
50                  dt = DTfromHeader;
51 <                if ((err = cm_getheader(&dt, NULL, NULL, fp)) != NULL)
51 >                if ((err = cm_getheader(&dt, NULL, NULL, NULL, NULL, fp)) != NULL)
52                          error(USER, err);
53                  if ((dt != DTrgbe) & (dt != DTxyze) ||
54                                  !fscnresolu(&xr, &yr, fp)) {
# Line 55 | Line 58 | sum_images(const char *fspec, const CMATRIX *cv, FILE
58                  if (myDT == DTfromHeader) {             /* on first one */
59                          myDT = dt;
60                          myXR = xr; myYR = yr;
61 <                        scanline = (COLOR *)malloc(sizeof(COLOR)*myXR);
61 >                        scanline = (COLR *)malloc(sizeof(COLR)*myXR);
62                          if (scanline == NULL)
63                                  error(SYSTEM, "out of memory in sum_images()");
64                          pmat = cm_alloc(myYR, myXR);
65                          memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR);
66                                                          /* finish header */
67 <                        fputformat((char *)cm_fmt_id[myDT], fout);
67 >                        fputformat(cm_fmt_id[myDT], fout);
68                          fputc('\n', fout);
69                          fflush(fout);
70                  } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) {
# Line 69 | Line 72 | sum_images(const char *fspec, const CMATRIX *cv, FILE
72                                          fname);
73                          error(USER, errmsg);
74                  }
75 +                                                        /* flat file check */
76 +                if ((data_start = ftell(fp)) > 0 && fseek(fp, 0L, SEEK_END) == 0) {
77 +                        flat_file = (ftell(fp) == data_start + sizeof(COLR)*xr*yr);
78 +                        if (fseek(fp, data_start, SEEK_SET) < 0) {
79 +                                sprintf(errmsg, "cannot seek on picture '%s'", fname);
80 +                                error(SYSTEM, errmsg);
81 +                        }
82 +                }
83                  psp = pmat->cmem;
84                  for (y = 0; y < yr; y++) {              /* read it in */
85 +                        COLOR   col;
86                          int     x;
87 <                        if (freadscan(scanline, xr, fp) < 0) {
87 >                        if (flat_file ? getbinary(scanline, sizeof(COLR), xr, fp) != xr :
88 >                                        freadcolrs(scanline, xr, fp) < 0) {
89                                  sprintf(errmsg, "error reading picture '%s'",
90                                                  fname);
91                                  error(SYSTEM, errmsg);
92                          }
93                                                          /* sum in scanline */
94                          for (x = 0; x < xr; x++, psp += 3) {
95 <                                multcolor(scanline[x], scv);
96 <                                addcolor(psp, scanline[x]);
95 >                                if (!scanline[x][EXP])
96 >                                        continue;       /* skip zeroes */
97 >                                colr_color(col, scanline[x]);
98 >                                multcolor(col, scv);
99 >                                addcolor(psp, col);
100                          }
101                  }
102                  fclose(fp);                             /* done this picture */
# Line 119 | Line 135 | hasNumberFormat(const char *s)
135   int
136   main(int argc, char *argv[])
137   {
138 <        int             skyfmt = DTascii;
138 >        int             skyfmt = DTfromHeader;
139          int             outfmt = DTascii;
140 <        int             nsteps = 1;
140 >        int             headout = 1;
141 >        int             nsteps = 0;
142          char            *ofspec = NULL;
143          FILE            *ofp = stdout;
144 +        int             xres=0, yres=0;
145          CMATRIX         *cmtx;          /* component vector/matrix result */
146          char            fnbuf[256];
147          int             a, i;
# Line 136 | Line 154 | main(int argc, char *argv[])
154                          nsteps = atoi(argv[++a]);
155                          if (nsteps < 0)
156                                  goto userr;
157 +                        skyfmt = nsteps ? DTascii : DTfromHeader;
158                          break;
159 +                case 'h':
160 +                        headout = !headout;
161 +                        break;
162                  case 'i':
163                          switch (argv[a][2]) {
164                          case 'f':
# Line 148 | Line 170 | main(int argc, char *argv[])
170                          case 'a':
171                                  skyfmt = DTascii;
172                                  break;
151                        case 'h':
152                                skyfmt = DTfromHeader;
153                                break;
173                          default:
174                                  goto userr;
175                          }
# Line 169 | Line 188 | main(int argc, char *argv[])
188                          case 'a':
189                                  outfmt = DTascii;
190                                  break;
191 +                        case 'c':
192 +                                outfmt = DTrgbe;
193 +                                break;
194                          default:
195                                  goto userr;
196                          }
197                          break;
198 +                case 'x':
199 +                        xres = atoi(argv[++a]);
200 +                        break;
201 +                case 'y':
202 +                        yres = atoi(argv[++a]);
203 +                        break;
204                  default:
205                          goto userr;
206                  }
# Line 180 | Line 208 | main(int argc, char *argv[])
208                  goto userr;
209  
210          if (argc-a > 2) {                       /* VTDs expression */
211 <                CMATRIX *smtx, *Dmat, *Tmat, *imtx;
211 >                CMATRIX         *smtx, *Dmat, *Tmat, *imtx;
212 >                const char      *ccp;
213                                                  /* get sky vector/matrix */
214                  smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
215 +                nsteps = smtx->ncols;
216                                                  /* load BSDF */
217 <                Tmat = cm_loadBTDF(argv[a+1]);
217 >                if (argv[a+1][0] != '!' &&
218 >                                (ccp = strrchr(argv[a+1], '.')) != NULL &&
219 >                                !strcasecmp(ccp+1, "XML"))
220 >                        Tmat = cm_loadBTDF(argv[a+1]);
221 >                else
222 >                        Tmat = cm_load(argv[a+1], 0, 0, DTfromHeader);
223                                                  /* load Daylight matrix */
224 <                Dmat = cm_load(argv[a+2], Tmat==NULL ? 0 : Tmat->ncols,
224 >                Dmat = cm_load(argv[a+2], Tmat->ncols,
225                                          smtx->nrows, DTfromHeader);
226                                                  /* multiply vector through */
227                  imtx = cm_multiply(Dmat, smtx);
# Line 196 | Line 231 | main(int argc, char *argv[])
231                  cm_free(imtx);
232          } else {                                /* sky vector/matrix only */
233                  cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
234 +                nsteps = cmtx->ncols;
235          }
236                                                  /* prepare output stream */
237          if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) {
# Line 211 | Line 247 | main(int argc, char *argv[])
247                  ofspec = NULL;                  /* only need to open once */
248          }
249          if (hasNumberFormat(argv[a])) {         /* generating image(s) */
250 +                if (outfmt != DTrgbe) {
251 +                        error(WARNING, "changing output type to -oc");
252 +                        outfmt = DTrgbe;
253 +                }
254                  if (ofspec == NULL) {
255                          SET_FILE_BINARY(ofp);
256                          newheader("RADIANCE", ofp);
# Line 221 | Line 261 | main(int argc, char *argv[])
261                          for (i = 0; i < nsteps; i++) {
262                                  CMATRIX *cvec = cm_column(cmtx, i);
263                                  if (ofspec != NULL) {
264 <                                        sprintf(fnbuf, ofspec, i+1);
264 >                                        sprintf(fnbuf, ofspec, i);
265                                          if ((ofp = fopen(fnbuf, "wb")) == NULL) {
266                                                  fprintf(stderr,
267                                          "%s: cannot open '%s' for output\n",
# Line 232 | Line 272 | main(int argc, char *argv[])
272                                          printargs(argc, argv, ofp);
273                                          fputnow(ofp);
274                                  }
275 <                                fprintf(ofp, "FRAME=%d\n", i+1);
275 >                                fprintf(ofp, "FRAME=%d\n", i);
276                                  if (!sum_images(argv[a], cvec, ofp))
277                                          return(1);
278                                  if (ofspec != NULL) {
# Line 256 | Line 296 | main(int argc, char *argv[])
296                          const char      *wtype = (outfmt==DTascii) ? "w" : "wb";
297                          for (i = 0; i < nsteps; i++) {
298                                  CMATRIX *rvec = cm_column(rmtx, i);
299 <                                sprintf(fnbuf, ofspec, i+1);
299 >                                if (yres > 0) {
300 >                                        if (xres <= 0)
301 >                                                xres = rvec->nrows/yres;
302 >                                        if (xres*yres != rvec->nrows) {
303 >                                                fprintf(stderr, "Bad resolution: %d != %dx%d\n",
304 >                                                                rvec->nrows, xres, yres);
305 >                                                return(1);
306 >                                        }
307 >                                        rvec->nrows = yres;
308 >                                        rvec->ncols = xres;
309 >                                } else if (xres > 0) {
310 >                                        yres = rvec->nrows/xres;
311 >                                        if (xres*yres != rvec->nrows) {
312 >                                                fprintf(stderr,
313 >                                                        "Bad resolution: %d does not divide %d evenly\n",
314 >                                                                        xres, rvec->nrows);
315 >                                                return(1);
316 >                                        }
317 >                                        rvec->nrows = yres;
318 >                                        rvec->ncols = xres;
319 >                                }
320 >                                sprintf(fnbuf, ofspec, i);
321                                  if ((ofp = fopen(fnbuf, wtype)) == NULL) {
322                                          fprintf(stderr,
323                                          "%s: cannot open '%s' for output\n",
# Line 266 | Line 327 | main(int argc, char *argv[])
327   #ifdef getc_unlocked
328                                  flockfile(ofp);
329   #endif
330 +                                if (headout) {  /* header output */
331 +                                        newheader("RADIANCE", ofp);
332 +                                        printargs(argc, argv, ofp);
333 +                                        fputnow(ofp);
334 +                                        fprintf(ofp, "FRAME=%d\n", i);
335 +                                        if ((outfmt != DTrgbe) & (outfmt != DTxyze)) {
336 +                                                fprintf(ofp, "NROWS=%d\n", rvec->nrows);
337 +                                                fprintf(ofp, "NCOLS=%d\n", rvec->ncols);
338 +                                                fputs("NCOMP=3\n", ofp);
339 +                                        }
340 +                                        if ((outfmt == DTfloat) | (outfmt == DTdouble))
341 +                                                fputendian(ofp);
342 +                                        fputformat(cm_fmt_id[outfmt], ofp);
343 +                                        fputc('\n', ofp);
344 +                                }
345                                  cm_write(rvec, outfmt, ofp);
346                                  if (fclose(ofp) == EOF) {
347                                          fprintf(stderr,
# Line 282 | Line 358 | main(int argc, char *argv[])
358   #endif
359                          if (outfmt != DTascii)
360                                  SET_FILE_BINARY(ofp);
361 <                        if (rmtx->ncols > 1) {  /* header if actual matrix */
361 >                        if (headout) {          /* header output */
362                                  newheader("RADIANCE", ofp);
363                                  printargs(argc, argv, ofp);
364                                  fputnow(ofp);
365 <                                fprintf(ofp, "NROWS=%d\n", rmtx->nrows);
366 <                                fprintf(ofp, "NCOLS=%d\n", rmtx->ncols);
367 <                                fputs("NCOMP=3\n", ofp);
368 <                                fputformat((char *)cm_fmt_id[outfmt], ofp);
365 >                                if ((outfmt != DTrgbe) & (outfmt != DTxyze)) {
366 >                                        fprintf(ofp, "NROWS=%d\n", rmtx->nrows);
367 >                                        fprintf(ofp, "NCOLS=%d\n", rmtx->ncols);
368 >                                        fputs("NCOMP=3\n", ofp);
369 >                                }
370 >                                if ((outfmt == DTfloat) | (outfmt == DTdouble))
371 >                                        fputendian(ofp);
372 >                                fputformat(cm_fmt_id[outfmt], ofp);
373                                  fputc('\n', ofp);
374                          }
375                          cm_write(rmtx, outfmt, ofp);
# Line 303 | Line 383 | main(int argc, char *argv[])
383          cm_free(cmtx);
384          return(0);
385   userr:
386 <        fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] DCspec [skyf]\n",
386 >        fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d|c}] DCspec [skyf]\n",
387                                  progname);
388 <        fprintf(stderr, "   or: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] Vspec Tbsdf.xml Dmat.dat [skyf]\n",
388 >        fprintf(stderr, "   or: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d|c}] Vspec Tbsdf Dmat.dat [skyf]\n",
389                                  progname);
390          return(1);
391   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines