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

Comparing ray/src/util/rtcontrib.c (file contents):
Revision 1.12 by greg, Fri Jun 3 14:29:42 2005 UTC vs.
Revision 1.67 by greg, Tue Apr 10 05:16:50 2012 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * Gather rtrace output to compute contributions from particular sources
6   */
7  
8 + /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
9 +        Need to refactor code by forking a subprocess for each
10 +        rtrace call to take output and accumulate it into bins
11 +        for the parent process.  This will avoid our current
12 +        bottleneck around processing output queues, computing
13 +        bins and the associated buffer growth, which can be crazy
14 +        (gigabytes/subprocess).  Each child process will return
15 +        a ray number and a fully computed and ready-to-output
16 +        record of modifiers and their summed bins.  These will
17 +        be queued and sorted by the parent for ordered output.
18 + XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
19 +
20   #include  "standard.h"
21   #include  <ctype.h>
22   #include  <signal.h>
# Line 16 | Line 28 | static const char RCSid[] = "$Id$";
28   #include  "lookup.h"
29   #include  "calcomp.h"
30  
31 + #ifdef _WIN32
32 + typedef int     ssize_t;
33 + #endif
34 +
35 + #ifndef MAXMODLIST
36   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
37 + #endif
38  
39 < int     treebufsiz = BUFSIZ;            /* current tree buffer size */
39 > ssize_t treebufsiz = BUFSIZ;            /* current tree buffer size */
40  
41   typedef double  DCOLOR[3];              /* double-precision color */
42  
43   /*
44 < * The modcont structure is used to accumulate ray contributions
44 > * The MODCONT structure is used to accumulate ray contributions
45   * for a particular modifier, which may be subdivided into bins
46 < * if binv is non-NULL.  If outspec contains a %s in it, this will
46 > * if binv evaluates > 0.  If outspec contains a %s in it, this will
47   * be replaced with the modifier name.  If outspec contains a %d in it,
48   * this will be used to create one output file per bin, otherwise all bins
49   * will be written to the same file, in order.  If the global outfmt
# Line 44 | Line 62 | static void mcfree(void *p) { epfree((*(MODCONT *)p).b
62  
63   LUTAB   modconttab = LU_SINIT(NULL,mcfree);     /* modifier lookup table */
64  
65 < /* close output stream */
66 < static void closefile(void *p) { fclose((FILE *)p); }
65 > /*
66 > * The STREAMOUT structure holds an open FILE pointer and a count of
67 > * the number of RGB triplets per record, or 0 if unknown.
68 > */
69 > typedef struct {
70 >        FILE            *ofp;           /* output file pointer */
71 >        int             outpipe;        /* output is to a pipe */
72 >        int             reclen;         /* triplets/record */
73 >        int             xr, yr;         /* output resolution for picture */
74 > } STREAMOUT;
75  
76 < LUTAB   ofiletab = LU_SINIT(free,closefile);    /* output file table */
76 > /* close output stream and free record */
77 > static void
78 > closestream(void *p)
79 > {
80 >        STREAMOUT       *sop = (STREAMOUT *)p;
81 >        int             status;
82 >        if (sop->outpipe)
83 >                status = pclose(sop->ofp);
84 >        else
85 >                status = fclose(sop->ofp);
86 >        if (status)
87 >                error(SYSTEM, "error closing output stream");
88 >        free(p);
89 > }
90  
91 < FILE *getofile(const char *ospec, const char *mname, int bn);
91 > LUTAB   ofiletab = LU_SINIT(free,closestream);  /* output file table */
92  
93 + #define OF_MODIFIER     01
94 + #define OF_BIN          02
95 +
96 + STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen);
97 + int ofname(char *oname, const char *ospec, const char *mname, int bn);
98 + void printheader(FILE *fout, const char *info);
99 + void printresolu(FILE *fout, int xr, int yr);
100 +
101   /*
102   * The rcont structure is used to manage i/o with a particular
103   * rtrace child process.  Input is passed unchanged from stdin,
# Line 61 | Line 108 | struct rtproc {
108          struct rtproc   *next;          /* next in list of processes */
109          SUBPROC         pd;             /* rtrace pipe descriptors */
110          unsigned long   raynum;         /* ray number for this tree */
111 <        int             bsiz;           /* ray tree buffer length */
111 >        size_t          bsiz;           /* ray tree buffer length */
112          char            *buf;           /* ray tree buffer */
113 <        int             nbr;            /* number of bytes from rtrace */
113 >        size_t          nbr;            /* number of bytes from rtrace */
114   };                              /* rtrace process buffer */
115  
116                                          /* rtrace command and defaults */
117 < char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
118 <                                "-ab", "1", "-ad", "128", "-lr", "-10", };
119 < int  rtargc = 11;
117 > char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
118 >                                "-dj", ".9", "-dr", "3",
119 >                                "-ab", "1", "-ad", "350", };
120 >
121 > int  rtargc = 9;
122                                          /* overriding rtrace options */
123 < char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
123 > char            *myrtopts[] = { "-h-", "-x", "1", "-y", "0",
124                                  "-dt", "0", "-as", "0", "-aa", "0", NULL };
125  
126 + #define RTCOEFF         "-o~~~TmWdp"    /* compute coefficients only */
127 + #define RTCONTRIB       "-o~~~TmVdp"    /* compute ray contributions */
128 +
129   struct rtproc   rt0;                    /* head of rtrace process list */
130  
131   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
132  
133 < char    persistfn[] = "pfXXXXXX";       /* persist file name */
133 > #define PERSIST_NONE    0               /* no persist file */
134 > #define PERSIST_SINGLE  1               /* user set -P persist */
135 > #define PERSIST_PARALL  2               /* user set -PP persist */
136 > #define PERSIST_OURS    3               /* -PP persist belongs to us */
137 > int     persist_state = PERSIST_NONE;   /* persist file state */
138 > char    persistfn[] = "pfXXXXXX";       /* our persist file name, if set */
139  
140   int             gargc;                  /* global argc */
141   char            **gargv;                /* global argv */
# Line 90 | Line 147 | int            inpfmt = 'a';           /* input format */
147   int             outfmt = 'a';           /* output format */
148  
149   int             header = 1;             /* output header? */
150 + int             force_open = 0;         /* truncate existing output? */
151 + int             recover = 0;            /* recover previous output? */
152 + int             accumulate = 1;         /* input rays per output record */
153   int             xres = 0;               /* horiz. output resolution */
154   int             yres = 0;               /* vert. output resolution */
155  
156 < long            raysleft;               /* number of rays left to trace */
156 > int             account;                /* current accumulation count */
157 > unsigned long   raysleft;               /* number of rays left to trace */
158   long            waitflush;              /* how long until next flush */
159  
160   unsigned long   lastray = 0;            /* last ray number sent */
# Line 104 | Line 165 | int            using_stdout = 0;       /* are we using stdout? */
165   const char      *modname[MAXMODLIST];   /* ordered modifier name list */
166   int             nmods = 0;              /* number of modifiers */
167  
168 < MODCONT *addmodifier(char *modn, char *outf, char *binv);
168 > #define queue_length()          (lastray - lastdone)
169  
170 + MODCONT *growmodifier(MODCONT *mp, int nb);
171 + MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt);
172 + void addmodfile(char *fname, char *outf, char *binv, int bincnt);
173 +
174   void init(int np);
175   int done_rprocs(struct rtproc *rtp);
176 < void trace_contribs(FILE *fp);
176 > void reload_output(void);
177 > void recover_output(FILE *fin);
178 > void trace_contribs(FILE *fin);
179   struct rtproc *wait_rproc(void);
180   struct rtproc *get_rproc(void);
181   void queue_raytree(struct rtproc *rtp);
182   void process_queue(void);
183  
184 < void putcontrib(const DCOLOR cnt, FILE *fout);
184 > void put_contrib(const DCOLOR cnt, FILE *fout);
185   void add_contrib(const char *modn);
186 < void done_contrib(void);
186 > void done_contrib(int navg);
187  
188 + #ifdef getc_unlocked                    /* avoid nasty overheads */
189 + #undef getc
190 + #define getc    getc_unlocked
191 + #undef putc
192 + #define putc    putc_unlocked
193 + #undef ferror
194 + #define ferror  ferror_unlocked
195 + static int
196 + fread_unl(void *ptr, int size, int nitems, FILE *fp)
197 + {
198 +        char    *p = (char *)ptr;
199 +        int     len = size*nitems;
200 +        while (len-- > 0) {
201 +                int     c = getc_unlocked(fp);
202 +                if (c == EOF)
203 +                        return((p - (char *)ptr)/size);
204 +                *p++ = c;
205 +        }
206 +        return(nitems);
207 + }
208 + #undef fread
209 + #define fread   fread_unl
210 + static int
211 + fwrite_unl(const void *ptr, int size, int nitems, FILE *fp)
212 + {
213 +        const char      *p = (const char *)ptr;
214 +        int             len = size*nitems;
215 +        while (len-- > 0)
216 +                putc_unlocked(*p++, fp);
217 +        if (ferror_unlocked(fp))
218 +                return(0);
219 +        return(nitems);
220 + }
221 + #undef fwrite
222 + #define fwrite  fwrite_unl
223 + #endif
224 +
225 + /* return number of open rtrace processes */
226 + static int
227 + nrtprocs(void)
228 + {
229 +        int     nrtp = 0;
230 +        struct rtproc   *rtp;
231 +
232 +        for (rtp = &rt0; rtp != NULL; rtp = rtp->next)
233 +                nrtp += rtp->pd.running;
234 +        return(nrtp);
235 + }
236 +
237   /* set input/output format */
238   static void
239   setformat(const char *fmt)
# Line 157 | Line 273 | fmterr:
273   int
274   main(int argc, char *argv[])
275   {
276 +        int     contrib = 0;
277          int     nprocs = 1;
278          char    *curout = NULL;
279          char    *binval = NULL;
280 +        int     bincnt = 0;
281          char    fmt[8];
282          int     i, j;
283 +                                /* need at least one argument */
284 +        if (argc < 2) {
285 +                fprintf(stderr,
286 + "Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
287 +                        argv[0]);
288 +                exit(1);
289 +        }
290                                  /* global program name */
291          gargv = argv;
292                                  /* initialize calcomp routines */
# Line 174 | Line 299 | main(int argc, char *argv[])
299                  while ((j = expandarg(&argc, &argv, i)) > 0)
300                          ;
301                  if (j < 0) {
302 <                        fprintf(stderr, "%s: cannot expand '%s'",
302 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
303                                          argv[0], argv[i]);
304                          exit(1);
305                  }
306                  if (argv[i][0] == '-')
307                          switch (argv[i][1]) {
308                          case 'n':               /* number of processes */
309 <                                if (argv[i][2] || i >= argc-1) break;
309 >                                if (argv[i][2] || i >= argc-2) break;
310                                  nprocs = atoi(argv[++i]);
311                                  if (nprocs <= 0)
312                                          error(USER, "illegal number of processes");
313                                  continue;
314 +                        case 'V':               /* output contributions */
315 +                                switch (argv[i][2]) {
316 +                                case '\0':
317 +                                        contrib = !contrib;
318 +                                        continue;
319 +                                case '+': case '1':
320 +                                case 'T': case 't':
321 +                                case 'Y': case 'y':
322 +                                        contrib = 1;
323 +                                        continue;
324 +                                case '-': case '0':
325 +                                case 'F': case 'f':
326 +                                case 'N': case 'n':
327 +                                        contrib = 0;
328 +                                        continue;
329 +                                }
330 +                                break;
331 +                        case 'c':               /* input rays per output */
332 +                                if (argv[i][2] || i >= argc-2) break;
333 +                                accumulate = atoi(argv[++i]);
334 +                                continue;
335 +                        case 'r':               /* recover output */
336 +                                if (argv[i][2]) break;
337 +                                recover = 1;
338 +                                continue;
339                          case 'h':               /* output header? */
340                                  switch (argv[i][2]) {
341                                  case '\0':
# Line 203 | Line 353 | main(int argc, char *argv[])
353                                          continue;
354                                  }
355                                  break;
356 <                        case 'f':               /* file or i/o format */
356 >                        case 'f':               /* file or force or format */
357                                  if (!argv[i][2]) {
358                                          char    *fpath;
359 <                                        if (i >= argc-1) break;
359 >                                        if (i >= argc-2) break;
360                                          fpath = getpath(argv[++i],
361                                                          getrlibpath(), R_OK);
362                                          if (fpath == NULL) {
# Line 218 | Line 368 | main(int argc, char *argv[])
368                                          fcompile(fpath);
369                                          continue;
370                                  }
371 +                                if (argv[i][2] == 'o') {
372 +                                        force_open = 1;
373 +                                        continue;
374 +                                }
375                                  setformat(argv[i]+2);
376                                  continue;
377                          case 'e':               /* expression */
378 <                                if (argv[i][2] || i >= argc-1) break;
378 >                                if (argv[i][2] || i >= argc-2) break;
379                                  scompile(argv[++i], NULL, 0);
380                                  continue;
381                          case 'o':               /* output file spec. */
382 <                                if (argv[i][2] || i >= argc-1) break;
382 >                                if (argv[i][2] || i >= argc-2) break;
383                                  curout = argv[++i];
384                                  continue;
385                          case 'x':               /* horiz. output resolution */
386 <                                if (argv[i][2] || i >= argc-1) break;
386 >                                if (argv[i][2] || i >= argc-2) break;
387                                  xres = atoi(argv[++i]);
388                                  continue;
389                          case 'y':               /* vert. output resolution */
390 <                                if (argv[i][2] || i >= argc-1) break;
390 >                                if (argv[i][2] || i >= argc-2) break;
391                                  yres = atoi(argv[++i]);
392                                  continue;
393 <                        case 'b':               /* bin expression */
394 <                                if (argv[i][2] || i >= argc-1) break;
393 >                        case 'b':               /* bin expression/count */
394 >                                if (i >= argc-2) break;
395 >                                if (argv[i][2] == 'n') {
396 >                                        bincnt = (int)(eval(argv[++i]) + .5);
397 >                                        continue;
398 >                                }
399 >                                if (argv[i][2]) break;
400                                  binval = argv[++i];
401                                  continue;
402                          case 'm':               /* modifier name */
403 <                                if (argv[i][2] || i >= argc-1) break;
403 >                                if (argv[i][2] || i >= argc-2) break;
404                                  rtargv[rtargc++] = "-ti";
405                                  rtargv[rtargc++] = argv[++i];
406 <                                addmodifier(argv[i], curout, binval);
406 >                                addmodifier(argv[i], curout, binval, bincnt);
407                                  continue;
408 +                        case 'M':               /* modifier file */
409 +                                if (argv[i][2] || i >= argc-2) break;
410 +                                rtargv[rtargc++] = "-tI";
411 +                                rtargv[rtargc++] = argv[++i];
412 +                                addmodfile(argv[i], curout, binval, bincnt);
413 +                                continue;
414 +                        case 'P':               /* persist file */
415 +                                if (i >= argc-2) break;
416 +                                persist_state = (argv[i][2] == 'P') ?
417 +                                                PERSIST_PARALL : PERSIST_SINGLE;
418 +                                rtargv[rtargc++] = argv[i];
419 +                                rtargv[rtargc++] = argv[++i];
420 +                                continue;
421                          }
422                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
423          }
424 +        if (accumulate <= 0)    /* no output flushing for single record */
425 +                xres = yres = 0;
426                                  /* set global argument list */
427          gargc = argc; gargv = argv;
428                                  /* add "mandatory" rtrace settings */
429          for (j = 0; myrtopts[j] != NULL; j++)
430                  rtargv[rtargc++] = myrtopts[j];
431 +        rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
432                                  /* just asking for defaults? */
433          if (!strcmp(argv[i], "-defaults")) {
434 <                char    sxres[16], syres[16];
434 >                char    nps[8], sxres[16], syres[16];
435                  char    *rtpath;
436 <                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
436 >                printf("-c %-5d\t\t\t# accumulated rays per record\n",
437 >                                accumulate);
438 >                printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
439 >                                contrib ? "contributions" : "coefficients");
440                  fflush(stdout);                 /* report OUR options */
441 +                rtargv[rtargc++] = "-n";
442 +                sprintf(nps, "%d", nprocs);
443 +                rtargv[rtargc++] = nps;
444                  rtargv[rtargc++] = header ? "-h+" : "-h-";
445                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
446                  rtargv[rtargc++] = fmt;
# Line 269 | Line 450 | main(int argc, char *argv[])
450                  rtargv[rtargc++] = "-y";
451                  sprintf(syres, "%d", yres);
452                  rtargv[rtargc++] = syres;
272                rtargv[rtargc++] = "-oTW";
453                  rtargv[rtargc++] = "-defaults";
454                  rtargv[rtargc] = NULL;
455                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 283 | Line 463 | main(int argc, char *argv[])
463                  exit(1);
464          }
465          if (nprocs > 1) {       /* add persist file if parallel */
466 <                rtargv[rtargc++] = "-PP";
467 <                rtargv[rtargc++] = mktemp(persistfn);
466 >                if (persist_state == PERSIST_SINGLE)
467 >                        error(USER, "use -PP option for multiple processes");
468 >                if (persist_state == PERSIST_NONE) {
469 >                        rtargv[rtargc++] = "-PP";
470 >                        rtargv[rtargc++] = mktemp(persistfn);
471 >                        persist_state = PERSIST_OURS;
472 >                }
473          }
474                                  /* add format string */
475          sprintf(fmt, "-f%cf", inpfmt);
# Line 294 | Line 479 | main(int argc, char *argv[])
479                  error(USER, "missing octree argument");
480          rtargv[rtargc++] = octree = argv[i];
481          rtargv[rtargc] = NULL;
482 <                                /* start rtrace & compute contributions */
482 >                                /* start rtrace & recover if requested */
483          init(nprocs);
484 +                                /* compute contributions */
485          trace_contribs(stdin);
486 +                                /* clean up */
487          quit(0);
488   }
489  
490 + #ifndef SIGALRM
491 + #define SIGALRM SIGTERM
492 + #endif
493   /* kill persistent rtrace process */
494   static void
495   killpersist(void)
496   {
497          FILE    *fp = fopen(persistfn, "r");
498 <        int     pid;
498 >        RT_PID  pid;
499  
500          if (fp == NULL)
501                  return;
# Line 339 | Line 529 | quit(int status)
529   {
530          int     rtstat;
531  
532 <        if (rt0.next != NULL)           /* terminate persistent rtrace */
532 >        if (persist_state == PERSIST_OURS)  /* terminate waiting rtrace */
533                  killpersist();
534                                          /* clean up rtrace process(es) */
535          rtstat = done_rprocs(&rt0);
# Line 401 | Line 591 | init(int np)
591          rtp->next = NULL;               /* terminate list */
592          if (yres > 0) {
593                  if (xres > 0)
594 <                        raysleft = xres*yres;
594 >                        raysleft = (unsigned long)xres*yres;
595                  else
596                          raysleft = yres;
597          } else
598                  raysleft = 0;
599 <        waitflush = xres;
599 >        if ((account = accumulate) > 0)
600 >                raysleft *= accumulate;
601 >        waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
602 >        if (!recover)
603 >                return;
604 >                                        /* recover previous values */
605 >        if (accumulate <= 0)
606 >                reload_output();
607 >        else
608 >                recover_output(stdin);
609   }
610  
611 + /* grow modifier to accommodate more bins */
612 + MODCONT *
613 + growmodifier(MODCONT *mp, int nb)
614 + {
615 +        if (nb <= mp->nbins)
616 +                return mp;
617 +        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1));
618 +        if (mp == NULL)
619 +                error(SYSTEM, "out of memory in growmodifier");
620 +        memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins));
621 +        mp->nbins = nb;
622 +        return mp;
623 + }
624 +
625   /* add modifier to our list to track */
626   MODCONT *
627 < addmodifier(char *modn, char *outf, char *binv)
627 > addmodifier(char *modn, char *outf, char *binv, int bincnt)
628   {
629          LUENT   *lep = lu_find(&modconttab, modn);
630          MODCONT *mp;
631 +        int     i;
632          
633          if (lep->data != NULL) {
634                  sprintf(errmsg, "duplicate modifier '%s'", modn);
635                  error(USER, errmsg);
636          }
637          if (nmods >= MAXMODLIST)
638 <                error(USER, "too many modifiers");
638 >                error(INTERNAL, "too many modifiers");
639          modname[nmods++] = modn;        /* XXX assumes static string */
640          lep->key = modn;                /* XXX assumes static string */
641          mp = (MODCONT *)malloc(sizeof(MODCONT));
642          if (mp == NULL)
643                  error(SYSTEM, "out of memory in addmodifier");
430        lep->data = (char *)mp;
644          mp->outspec = outf;             /* XXX assumes static string */
645          mp->modname = modn;             /* XXX assumes static string */
646 <        if (binv != NULL)
647 <                mp->binv = eparse(binv);
648 <        else
649 <                mp->binv = eparse("0");
650 <        mp->nbins = 1;
646 >        if (binv == NULL)
647 >                binv = "0";             /* use single bin if unspecified */
648 >        mp->binv = eparse(binv);
649 >        if (mp->binv->type == NUM) {    /* check value if constant */
650 >                bincnt = (int)(evalue(mp->binv) + 1.5);
651 >                if (bincnt != 1) {
652 >                        sprintf(errmsg, "illegal non-zero constant for bin (%s)",
653 >                                        binv);
654 >                        error(USER, errmsg);
655 >                }
656 >        }
657 >        mp->nbins = 1;                  /* initialize results holder */
658          setcolor(mp->cbin[0], 0., 0., 0.);
659 +        if (bincnt > 1)
660 +                mp = growmodifier(mp, bincnt);
661 +        lep->data = (char *)mp;
662 +                                        /* allocate output streams */
663 +        for (i = bincnt; i-- > 0; )
664 +                getostream(mp->outspec, mp->modname, i, 1);
665          return mp;
666   }
667  
668 + /* add modifiers from a file list */
669 + void
670 + addmodfile(char *fname, char *outf, char *binv, int bincnt)
671 + {
672 +        char    *mname[MAXMODLIST];
673 +        int     i;
674 +                                        /* find the file & store strings */
675 +        if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
676 +                sprintf(errmsg, "cannot find modifier file '%s'", fname);
677 +                error(SYSTEM, errmsg);
678 +        }
679 +        for (i = 0; mname[i]; i++)      /* add each one */
680 +                addmodifier(mname[i], outf, binv, bincnt);
681 + }
682 +
683   /* put string to stderr */
684   void
685   eputs(char  *s)
# Line 454 | Line 695 | eputs(char  *s)
695          midline = s[strlen(s)-1] != '\n';
696   }
697  
698 + /* construct output file name and return flags whether modifier/bin present */
699 + int
700 + ofname(char *oname, const char *ospec, const char *mname, int bn)
701 + {
702 +        const char      *mnp = NULL;
703 +        const char      *bnp = NULL;
704 +        const char      *cp;
705 +        
706 +        if (ospec == NULL)
707 +                return -1;
708 +        for (cp = ospec; *cp; cp++)             /* check format position(s) */
709 +                if (*cp == '%') {
710 +                        do
711 +                                ++cp;
712 +                        while (isdigit(*cp));
713 +                        switch (*cp) {
714 +                        case '%':
715 +                                break;
716 +                        case 's':
717 +                                if (mnp != NULL)
718 +                                        return -1;
719 +                                mnp = cp;
720 +                                break;
721 +                        case 'd':
722 +                        case 'i':
723 +                        case 'o':
724 +                        case 'x':
725 +                        case 'X':
726 +                                if (bnp != NULL)
727 +                                        return -1;
728 +                                bnp = cp;
729 +                                break;
730 +                        default:
731 +                                return -1;
732 +                        }
733 +                }
734 +        if (mnp != NULL) {                      /* create file name */
735 +                if (bnp != NULL) {
736 +                        if (bnp > mnp)
737 +                                sprintf(oname, ospec, mname, bn);
738 +                        else
739 +                                sprintf(oname, ospec, bn, mname);
740 +                        return OF_MODIFIER|OF_BIN;
741 +                }
742 +                sprintf(oname, ospec, mname);
743 +                return OF_MODIFIER;
744 +        }
745 +        if (bnp != NULL) {
746 +                sprintf(oname, ospec, bn);
747 +                return OF_BIN;
748 +        }
749 +        strcpy(oname, ospec);
750 +        return 0;
751 + }
752 +
753   /* write header to the given output stream */
754   void
755 < printheader(FILE *fout)
755 > printheader(FILE *fout, const char *info)
756   {
757          extern char     VersionID[];
758 <        FILE            *fin = fopen(octree, "r");
759 <        
760 <        if (fin == NULL)
761 <                quit(1);
762 <        checkheader(fin, "ignore", fout);       /* copy octree header */
763 <        fclose(fin);
758 >                                                /* copy octree header */
759 >        if (octree[0] == '!') {
760 >                newheader("RADIANCE", fout);
761 >                fputs(octree+1, fout);
762 >                if (octree[strlen(octree)-1] != '\n')
763 >                        fputc('\n', fout);
764 >        } else {
765 >                FILE    *fin = fopen(octree, "r");
766 >                if (fin == NULL)
767 >                        quit(1);
768 >                checkheader(fin, "ignore", fout);
769 >                fclose(fin);
770 >        }
771          printargs(gargc-1, gargv, fout);        /* add our command */
772          fprintf(fout, "SOFTWARE= %s\n", VersionID);
773          fputnow(fout);
774 +        if (info != NULL)                       /* add extra info if given */
775 +                fputs(info, fout);
776          switch (outfmt) {                       /* add output format */
777          case 'a':
778                  fputformat("ascii", fout);
# Line 483 | Line 788 | printheader(FILE *fout)
788                  break;
789          }
790          fputc('\n', fout);
486        if (xres > 0) {
487                if (yres > 0)                   /* resolution string */
488                        fprtresolu(xres, yres, fout);
489                fflush(fout);
490        }
791   }
792  
793 < /* Get output file pointer (open and write header if new) */
794 < FILE *
795 < getofile(const char *ospec, const char *mname, int bn)
793 > /* write resolution string to given output stream */
794 > void
795 > printresolu(FILE *fout, int xr, int yr)
796   {
797 <        const char      *mnp = NULL;
798 <        const char      *bnp = NULL;
799 <        const char      *cp;
800 <        char            ofname[1024];
801 <        LUENT           *lep;
797 >        if ((xr > 0) & (yr > 0))        /* resolution string */
798 >                fprtresolu(xr, yr, fout);
799 > }
800 >
801 > /* Get output stream pointer (open and write header if new and noopen==0) */
802 > STREAMOUT *
803 > getostream(const char *ospec, const char *mname, int bn, int noopen)
804 > {
805 >        static const DCOLOR     nocontrib = BLKCOLOR;
806 >        static STREAMOUT        stdos;
807 >        int                     ofl;
808 >        char                    oname[1024];
809 >        LUENT                   *lep;
810 >        STREAMOUT               *sop;
811          
812          if (ospec == NULL) {                    /* use stdout? */
813 <                if (!using_stdout) {
813 >                if (!noopen && !using_stdout) {
814                          if (outfmt != 'a')
815                                  SET_FILE_BINARY(stdout);
816                          if (header)
817 <                                printheader(stdout);
817 >                                printheader(stdout, NULL);
818 >                        printresolu(stdout, xres, yres);
819 >                        if (waitflush > 0)
820 >                                fflush(stdout);
821 >                        stdos.xr = xres; stdos.yr = yres;
822 >                        using_stdout = 1;
823                  }
824 <                using_stdout = 1;
825 <                return stdout;
824 >                stdos.ofp = stdout;
825 >                stdos.reclen += noopen;
826 >                return &stdos;
827          }
828 <        for (cp = ospec; *cp; cp++)             /* check format position(s) */
829 <                if (*cp == '%') {
830 <                        do
831 <                                ++cp;
832 <                        while (isdigit(*cp));
833 <                        switch (*cp) {
519 <                        case '%':
520 <                                break;
521 <                        case 's':
522 <                                if (mnp != NULL)
523 <                                        goto badspec;
524 <                                mnp = cp;
525 <                                break;
526 <                        case 'd':
527 <                                if (bnp != NULL)
528 <                                        goto badspec;
529 <                                bnp = cp;
530 <                                break;
531 <                        default:
532 <                                goto badspec;
533 <                        }
534 <                }
535 <        if (mnp != NULL) {                      /* create file name */
536 <                if (bnp != NULL) {
537 <                        if (bnp > mnp)
538 <                                sprintf(ofname, ospec, mname, bn);
539 <                        else
540 <                                sprintf(ofname, ospec, bn, mname);
541 <                } else
542 <                        sprintf(ofname, ospec, mname);
543 <        } else if (bnp != NULL)
544 <                sprintf(ofname, ospec, bn);
545 <        else
546 <                strcpy(ofname, ospec);
547 <        lep = lu_find(&ofiletab, ofname);       /* look it up */
828 >        ofl = ofname(oname, ospec, mname, bn);  /* get output name */
829 >        if (ofl < 0) {
830 >                sprintf(errmsg, "bad output format '%s'", ospec);
831 >                error(USER, errmsg);
832 >        }
833 >        lep = lu_find(&ofiletab, oname);        /* look it up */
834          if (lep->key == NULL)                   /* new entry */
835 <                lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
836 <        if (lep->data == NULL) {                /* open output file */
837 <                FILE            *fp;
838 <                int             i;
839 <                if (ofname[0] == '!')           /* output to command */
840 <                        fp = popen(ofname+1, "w");
841 <                else
842 <                        fp = fopen(ofname, "w");
843 <                if (fp == NULL) {
844 <                        sprintf(errmsg, "cannot open '%s' for writing", ofname);
845 <                        error(SYSTEM, errmsg);
835 >                lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
836 >        sop = (STREAMOUT *)lep->data;
837 >        if (sop == NULL) {                      /* allocate stream */
838 >                sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
839 >                if (sop == NULL)
840 >                        error(SYSTEM, "out of memory in getostream");
841 >                sop->outpipe = oname[0] == '!';
842 >                sop->reclen = 0;
843 >                sop->ofp = NULL;                /* open iff noopen==0 */
844 >                sop->xr = xres; sop->yr = yres;
845 >                lep->data = (char *)sop;
846 >                if (!sop->outpipe & !force_open & !recover &&
847 >                                access(oname, F_OK) == 0) {
848 >                        errno = EEXIST;         /* file exists */
849 >                        goto openerr;
850                  }
851 +        }
852 +        if (!noopen && sop->ofp == NULL) {      /* open output stream */
853 +                long            i;
854 +                if (oname[0] == '!')            /* output to command */
855 +                        sop->ofp = popen(oname+1, "w");
856 +                else                            /* else open file */
857 +                        sop->ofp = fopen(oname, "w");
858 +                if (sop->ofp == NULL)
859 +                        goto openerr;
860                  if (outfmt != 'a')
861 <                        SET_FILE_BINARY(fp);
862 <                if (header)
863 <                        printheader(fp);
861 >                        SET_FILE_BINARY(sop->ofp);
862 > #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
863 >                flockfile(sop->ofp);
864 > #endif
865 >                if (header) {
866 >                        char    info[512];
867 >                        char    *cp = info;
868 >                        if (ofl & OF_MODIFIER || sop->reclen == 1) {
869 >                                sprintf(cp, "MODIFIER=%s\n", mname);
870 >                                while (*cp) ++cp;
871 >                        }
872 >                        if (ofl & OF_BIN) {
873 >                                sprintf(cp, "BIN=%d\n", bn);
874 >                                while (*cp) ++cp;
875 >                        }
876 >                        *cp = '\0';
877 >                        printheader(sop->ofp, info);
878 >                }
879 >                if (accumulate > 0) {           /* global resolution */
880 >                        sop->xr = xres; sop->yr = yres;
881 >                }
882 >                printresolu(sop->ofp, sop->xr, sop->yr);
883                                                  /* play catch-up */
884 <                for (i = 0; i < lastdone; i++) {
885 <                        static const DCOLOR     nocontrib = BLKCOLOR;
886 <                        putcontrib(nocontrib, fp);
884 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
885 >                        int     j = sop->reclen;
886 >                        if (j <= 0) j = 1;
887 >                        while (j--)
888 >                                put_contrib(nocontrib, sop->ofp);
889                          if (outfmt == 'a')
890 <                                putc('\n', fp);
890 >                                putc('\n', sop->ofp);
891                  }
892 <                if (xres > 0)
893 <                        fflush(fp);
574 <                lep->data = (char *)fp;
892 >                if (waitflush > 0)
893 >                        fflush(sop->ofp);
894          }
895 <        return (FILE *)lep->data;               /* return open file pointer */
896 < badspec:
897 <        sprintf(errmsg, "bad output format '%s'", ospec);
898 <        error(USER, errmsg);
899 <        return NULL;            /* pro forma return */
895 >        sop->reclen += noopen;                  /* add to length if noopen */
896 >        return sop;                             /* return output stream */
897 > openerr:
898 >        sprintf(errmsg, "cannot open '%s' for writing", oname);
899 >        error(SYSTEM, errmsg);
900 >        return NULL;    /* pro forma return */
901   }
902  
903   /* read input ray into buffer */
904   int
905   getinp(char *buf, FILE *fp)
906   {
907 +        double  dv[3], *dvp;
908 +        float   *fvp;
909          char    *cp;
910          int     i;
911  
# Line 592 | Line 914 | getinp(char *buf, FILE *fp)
914                  cp = buf;               /* make sure we get 6 floats */
915                  for (i = 0; i < 6; i++) {
916                          if (fgetword(cp, buf+127-cp, fp) == NULL)
917 <                                return 0;
917 >                                return -1;
918 >                        if (i >= 3)
919 >                                dv[i-3] = atof(cp);
920                          if ((cp = fskip(cp)) == NULL || *cp)
921 <                                return 0;
921 >                                return -1;
922                          *cp++ = ' ';
923                  }
924                  getc(fp);               /* get/put eol */
925                  *cp-- = '\0'; *cp = '\n';
926 +                if (DOT(dv,dv) <= FTINY*FTINY)
927 +                        return 0;       /* dummy ray */
928                  return strlen(buf);
929          case 'f':
930 <                if (fread(buf, sizeof(float), 6, fp) < 6)
931 <                        return 0;
930 >                if (fread(buf, sizeof(float), 6, fp) != 6)
931 >                        return -1;
932 >                fvp = (float *)buf + 3;
933 >                if (DOT(fvp,fvp) <= FTINY*FTINY)
934 >                        return 0;       /* dummy ray */
935                  return sizeof(float)*6;
936          case 'd':
937 <                if (fread(buf, sizeof(double), 6, fp) < 6)
938 <                        return 0;
937 >                if (fread(buf, sizeof(double), 6, fp) != 6)
938 >                        return -1;
939 >                dvp = (double *)buf + 3;
940 >                if (DOT(dvp,dvp) <= FTINY*FTINY)
941 >                        return 0;       /* dummy ray */
942                  return sizeof(double)*6;
943          }
944          error(INTERNAL, "botched input format");
945 <        return 0;       /* pro forma return */
945 >        return -1;      /* pro forma return */
946   }
947  
948   static float    rparams[9];             /* traced ray parameters */
# Line 640 | Line 972 | add_contrib(const char *modn)
972          bn = (int)(evalue(mp->binv) + .5);
973          if (bn <= 0)
974                  bn = 0;
975 <        else if (bn > mp->nbins) {      /* new bin */
976 <                mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
645 <                                                bn*sizeof(DCOLOR));
646 <                if (mp == NULL)
647 <                        error(SYSTEM, "out of memory in add_contrib");
648 <                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
649 <                mp->nbins = bn+1;
650 <                le->data = (char *)mp;
651 <        }
975 >        else if (bn >= mp->nbins)       /* new bin */
976 >                le->data = (char *)(mp = growmodifier(mp, bn+1));
977          addcolor(mp->cbin[bn], rparams);
978   }
979  
# Line 656 | Line 981 | add_contrib(const char *modn)
981   static int
982   puteol(const LUENT *e, void *p)
983   {
984 <        FILE    *fp = (FILE *)e->data;
984 >        STREAMOUT       *sop = (STREAMOUT *)e->data;
985  
986          if (outfmt == 'a')
987 <                putc('\n', fp);
987 >                putc('\n', sop->ofp);
988          if (!waitflush)
989 <                fflush(fp);
990 <        if (ferror(fp)) {
989 >                fflush(sop->ofp);
990 >        if (ferror(sop->ofp)) {
991                  sprintf(errmsg, "write error on file '%s'", e->key);
992                  error(SYSTEM, errmsg);
993          }
# Line 671 | Line 996 | puteol(const LUENT *e, void *p)
996  
997   /* put out ray contribution to file */
998   void
999 < putcontrib(const DCOLOR cnt, FILE *fout)
999 > put_contrib(const DCOLOR cnt, FILE *fout)
1000   {
1001          float   fv[3];
1002          COLR    cv;
# Line 681 | Line 1006 | putcontrib(const DCOLOR cnt, FILE *fout)
1006                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
1007                  break;
1008          case 'f':
1009 <                fv[0] = cnt[0];
685 <                fv[1] = cnt[1];
686 <                fv[2] = cnt[2];
1009 >                copycolor(fv, cnt);
1010                  fwrite(fv, sizeof(float), 3, fout);
1011                  break;
1012          case 'd':
# Line 698 | Line 1021 | putcontrib(const DCOLOR cnt, FILE *fout)
1021          }
1022   }
1023  
1024 < /* output ray tallies and clear for next primary */
1024 > /* output ray tallies and clear for next accumulation */
1025   void
1026 < done_contrib(void)
1026 > done_contrib(int navg)
1027   {
1028 <        int     i, j;
1029 <        MODCONT *mp;
1028 >        double          sf = 1.;
1029 >        int             i, j;
1030 >        MODCONT         *mp;
1031 >        STREAMOUT       *sop;
1032 >                                                /* set average scaling */
1033 >        if (navg > 1)
1034 >                sf = 1. / (double)navg;
1035                                                  /* output modifiers in order */
1036          for (i = 0; i < nmods; i++) {
709                FILE    *fp;
1037                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
1038 <                fp = getofile(mp->outspec, mp->modname, 0);
1039 <                putcontrib(mp->cbin[0], fp);
1038 >                if (navg > 1)                   /* average scaling */
1039 >                        for (j = mp->nbins; j--; )
1040 >                                scalecolor(mp->cbin[j], sf);
1041 >                sop = getostream(mp->outspec, mp->modname, 0,0);
1042 >                put_contrib(mp->cbin[0], sop->ofp);
1043                  if (mp->nbins > 3 &&            /* minor optimization */
1044 <                                fp == getofile(mp->outspec, mp->modname, 1))
1044 >                                sop == getostream(mp->outspec, mp->modname, 1,0))
1045                          for (j = 1; j < mp->nbins; j++)
1046 <                                putcontrib(mp->cbin[j], fp);
1046 >                                put_contrib(mp->cbin[j], sop->ofp);
1047                  else
1048                          for (j = 1; j < mp->nbins; j++)
1049 <                                putcontrib(mp->cbin[j],
1050 <                                        getofile(mp->outspec, mp->modname, j));
1051 <                                                /* clear for next ray tree */
1049 >                                put_contrib(mp->cbin[j],
1050 >                                    getostream(mp->outspec,mp->modname,j,0)->ofp);
1051 >                                                /* clear for next time */
1052                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1053          }
1054          --waitflush;                            /* terminate records */
# Line 726 | Line 1056 | done_contrib(void)
1056          if (using_stdout & (outfmt == 'a'))
1057                  putc('\n', stdout);
1058          if (!waitflush) {
1059 <                waitflush = xres;
1059 >                waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
1060                  if (using_stdout)
1061                          fflush(stdout);
1062          }
# Line 777 | Line 1107 | process_queue(void)
1107                          if (!n || !(isalpha(*cp) | (*cp == '_')))
1108                                  error(USER, "bad modifier name from rtrace");
1109                                          /* get modifier name */
1110 <                        while (n > 0 && *cp != '\t') {
1110 >                        while (n > 1 && *cp != '\t') {
1111 >                                if (mnp - modname >= sizeof(modname)-2)
1112 >                                        error(INTERNAL, "modifier name too long");
1113                                  *mnp++ = *cp++; n--;
1114                          }
1115                          *mnp = '\0';
# Line 789 | Line 1121 | process_queue(void)
1121                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1122                          add_contrib(modname);
1123                  }
1124 <                done_contrib();         /* sum up contributions & output */
1124 >                                        /* time to produce record? */
1125 >                if (account > 0 && !--account)
1126 >                        done_contrib(account = accumulate);
1127                  lastdone = rtp->raynum;
1128 <                free(rtp->buf);         /* free up buffer space */
1128 >                if (rtp->buf != NULL)   /* free up buffer space */
1129 >                        free(rtp->buf);
1130                  rt_unproc = rtp->next;
1131                  free(rtp);              /* done with this ray tree */
1132          }
# Line 803 | Line 1138 | wait_rproc(void)
1138   {
1139          struct rtproc   *rtfree = NULL;
1140          fd_set          readset, errset;
1141 <        int             nr;
1141 >        ssize_t         nr;
1142          struct rtproc   *rt;
1143          int             n;
1144          
# Line 834 | Line 1169 | wait_rproc(void)
1169                                  continue;
1170                          if (rt->buf == NULL) {
1171                                  rt->bsiz = treebufsiz;
1172 <                                rt->buf = (char *)malloc(treebufsiz);
1172 >                                rt->buf = (char *)malloc(rt->bsiz);
1173                          } else if (rt->nbr + BUFSIZ > rt->bsiz) {
1174                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
1175                                          rt->bsiz = treebufsiz;
1176 <                                else
1177 <                                        rt->bsiz = treebufsiz += BUFSIZ;
1176 >                                else if ((treebufsiz = rt->bsiz += BUFSIZ) < 0)
1177 >                                        error(INTERNAL,
1178 >                                            "ray buffer does not fit memory");
1179                                  rt->buf = (char *)realloc(rt->buf, rt->bsiz);
1180                          }
1181                          if (rt->buf == NULL)
1182                                  error(SYSTEM, "out of memory in wait_rproc");
1183 <                        nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
1184 <                        if (nr <= 0)
1183 >                        nr = rt->bsiz - rt->nbr;
1184 >                        if (nr & ~0x7fffffff)   /* avoid 32-bit OS issues */
1185 >                                nr = 0x7fffffff;
1186 >                        nr = read(rt->pd.r, rt->buf+rt->nbr, nr);
1187 >                        if (nr < 0)
1188 >                                error(SYSTEM, "read error from rtrace");
1189 >                        if (!nr)
1190                                  error(USER, "rtrace process died");
1191                          rt->nbr += nr;          /* advance & check */
1192 <                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
1193 <                                                        "~\t~\t", 4)) {
1194 <                                rt->nbr -= 4;   /* elide terminator */
1192 >                        if (rt->nbr >= 6 && !memcmp(rt->buf+rt->nbr-6,
1193 >                                                        "~\t~\t~\t", 6)) {
1194 >                                rt->nbr -= 6;   /* elide terminator */
1195                                  queue_raytree(rt);
1196                                  rtfree = rt;    /* ready for next ray */
1197                          }
# Line 875 | Line 1216 | get_rproc(void)
1216   void
1217   trace_contribs(FILE *fin)
1218   {
1219 +        static int      ignore_warning_given = 0;
1220          char            inpbuf[128];
1221          int             iblen;
1222          struct rtproc   *rtp;
1223                                                  /* loop over input */
1224 <        while ((iblen = getinp(inpbuf, fin)) > 0) {
1225 <                if (lastray+1 < lastray) {      /* counter rollover? */
1224 >        while ((iblen = getinp(inpbuf, fin)) >= 0) {
1225 >                if (!iblen && accumulate != 1) {
1226 >                        if (!ignore_warning_given++)
1227 >                                error(WARNING,
1228 >                                "dummy ray(s) ignored during accumulation\n");
1229 >                        continue;
1230 >                }
1231 >                if (!iblen ||                   /* need flush/reset? */
1232 >                                queue_length() > 10*nrtprocs() ||
1233 >                                lastray+1 < lastray) {
1234                          while (wait_rproc() != NULL)
1235                                  process_queue();
1236                          lastdone = lastray = 0;
1237                  }
1238                  rtp = get_rproc();              /* get avail. rtrace process */
1239 <                rtp->raynum = ++lastray;        /* assign ray to it */
1240 <                writebuf(rtp->pd.w, inpbuf, iblen);
1241 <                if (!--raysleft)
1242 <                        break;
1239 >                rtp->raynum = ++lastray;        /* assign ray */
1240 >                if (iblen) {                    /* trace ray if valid */
1241 >                        writebuf(rtp->pd.w, inpbuf, iblen);
1242 >                } else {                        /* else bypass dummy ray */
1243 >                        queue_raytree(rtp);     /* queue empty ray/record */
1244 >                        if ((yres <= 0) | (xres <= 0))
1245 >                                waitflush = 1;  /* flush right after */
1246 >                }
1247                  process_queue();                /* catch up with results */
1248 +                if (raysleft && !--raysleft)
1249 +                        break;                  /* preemptive EOI */
1250          }
1251          while (wait_rproc() != NULL)            /* process outstanding rays */
1252                  process_queue();
1253 <        if (raysleft > 0)
1253 >        if (accumulate <= 0)
1254 >                done_contrib(0);                /* output tallies */
1255 >        else if (account < accumulate) {
1256 >                error(WARNING, "partial accumulation in final record");
1257 >                done_contrib(accumulate - account);
1258 >        }
1259 >        if (raysleft)
1260                  error(USER, "unexpected EOF on input");
1261 +        lu_done(&ofiletab);                     /* close output files */
1262 + }
1263 +
1264 + /* get ray contribution from previous file */
1265 + static int
1266 + get_contrib(DCOLOR cnt, FILE *finp)
1267 + {
1268 +        COLOR   fv;
1269 +        COLR    cv;
1270 +
1271 +        switch (outfmt) {
1272 +        case 'a':
1273 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1274 +        case 'f':
1275 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1276 +                        return(0);
1277 +                copycolor(cnt, fv);
1278 +                return(1);
1279 +        case 'd':
1280 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1281 +        case 'c':
1282 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1283 +                        return(0);
1284 +                colr_color(fv, cv);
1285 +                copycolor(cnt, fv);
1286 +                return(1);
1287 +        default:
1288 +                error(INTERNAL, "botched output format");
1289 +        }
1290 +        return(0);      /* pro forma return */
1291 + }
1292 +
1293 + /* close output file opened for input */
1294 + static int
1295 + myclose(const LUENT *e, void *p)
1296 + {
1297 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1298 +        
1299 +        if (sop->ofp == NULL)
1300 +                return(0);
1301 +        fclose(sop->ofp);
1302 +        sop->ofp = NULL;
1303 +        return(0);
1304 + }
1305 +
1306 + /* load previously accumulated values */
1307 + void
1308 + reload_output(void)
1309 + {
1310 +        int             i, j;
1311 +        MODCONT         *mp;
1312 +        int             ofl;
1313 +        char            oname[1024];
1314 +        char            *fmode = "rb";
1315 +        char            *outvfmt;
1316 +        LUENT           *ment, *oent;
1317 +        int             xr, yr;
1318 +        STREAMOUT       sout;
1319 +        DCOLOR          rgbv;
1320 +
1321 +        switch (outfmt) {
1322 +        case 'a':
1323 +                outvfmt = "ascii";
1324 +                fmode = "r";
1325 +                break;
1326 +        case 'f':
1327 +                outvfmt = "float";
1328 +                break;
1329 +        case 'd':
1330 +                outvfmt = "double";
1331 +                break;
1332 +        case 'c':
1333 +                outvfmt = COLRFMT;
1334 +                break;
1335 +        default:
1336 +                error(INTERNAL, "botched output format");
1337 +                return;
1338 +        }
1339 +                                                /* reload modifier values */
1340 +        for (i = 0; i < nmods; i++) {
1341 +                ment = lu_find(&modconttab,modname[i]);
1342 +                mp = (MODCONT *)ment->data;
1343 +                if (mp->outspec == NULL)
1344 +                        error(USER, "cannot reload from stdout");
1345 +                if (mp->outspec[0] == '!')
1346 +                        error(USER, "cannot reload from command");
1347 +                for (j = 0; ; j++) {            /* load each modifier bin */
1348 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1349 +                        if (ofl < 0)
1350 +                                error(USER, "bad output file specification");
1351 +                        oent = lu_find(&ofiletab, oname);
1352 +                        if (oent->data != NULL) {
1353 +                                sout = *(STREAMOUT *)oent->data;
1354 +                        } else {
1355 +                                sout.reclen = 0;
1356 +                                sout.outpipe = 0;
1357 +                                sout.xr = xres; sout.yr = yres;
1358 +                                sout.ofp = NULL;
1359 +                        }
1360 +                        if (sout.ofp == NULL) { /* open output as input */
1361 +                                sout.ofp = fopen(oname, fmode);
1362 +                                if (sout.ofp == NULL) {
1363 +                                        if (j)
1364 +                                                break;  /* assume end of modifier */
1365 +                                        sprintf(errmsg, "missing reload file '%s'",
1366 +                                                        oname);
1367 +                                        error(WARNING, errmsg);
1368 +                                        break;
1369 +                                }
1370 + #ifdef getc_unlocked                                    /* avoid lock/unlock overhead */
1371 +                                flockfile(sout.ofp);
1372 + #endif
1373 +                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1374 +                                        sprintf(errmsg, "format mismatch for '%s'",
1375 +                                                        oname);
1376 +                                        error(USER, errmsg);
1377 +                                }
1378 +                                if ((sout.xr > 0) & (sout.yr > 0) &&
1379 +                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1380 +                                                        (xr != sout.xr) |
1381 +                                                        (yr != sout.yr))) {
1382 +                                        sprintf(errmsg, "resolution mismatch for '%s'",
1383 +                                                        oname);
1384 +                                        error(USER, errmsg);
1385 +                                }
1386 +                        }
1387 +                                                        /* read in RGB value */
1388 +                        if (!get_contrib(rgbv, sout.ofp)) {
1389 +                                if (!j) {
1390 +                                        fclose(sout.ofp);
1391 +                                        break;          /* ignore empty file */
1392 +                                }
1393 +                                if (j < mp->nbins) {
1394 +                                        sprintf(errmsg, "missing data in '%s'",
1395 +                                                        oname);
1396 +                                        error(USER, errmsg);
1397 +                                }
1398 +                                break;
1399 +                        }
1400 +                        if (j >= mp->nbins)             /* grow modifier size */
1401 +                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1402 +                        copycolor(mp->cbin[j], rgbv);
1403 +                        if (oent->key == NULL)          /* new file entry */
1404 +                                oent->key = strcpy((char *)
1405 +                                                malloc(strlen(oname)+1), oname);
1406 +                        if (oent->data == NULL)
1407 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1408 +                        *(STREAMOUT *)oent->data = sout;
1409 +                }
1410 +        }
1411 +        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1412 + }
1413 +
1414 + /* seek on the given output file */
1415 + static int
1416 + myseeko(const LUENT *e, void *p)
1417 + {
1418 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1419 +        off_t           nbytes = *(off_t *)p;
1420 +        
1421 +        if (sop->reclen > 1)
1422 +                nbytes = nbytes * sop->reclen;
1423 +        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1424 +                sprintf(errmsg, "seek error on file '%s'", e->key);
1425 +                error(SYSTEM, errmsg);
1426 +        }
1427 +        return 0;
1428 + }
1429 +
1430 + /* recover output if possible */
1431 + void
1432 + recover_output(FILE *fin)
1433 + {
1434 +        off_t           lastout = -1;
1435 +        int             outvsiz, recsiz;
1436 +        char            *outvfmt;
1437 +        int             i, j;
1438 +        MODCONT         *mp;
1439 +        int             ofl;
1440 +        char            oname[1024];
1441 +        LUENT           *ment, *oent;
1442 +        STREAMOUT       sout;
1443 +        off_t           nvals;
1444 +        int             xr, yr;
1445 +
1446 +        switch (outfmt) {
1447 +        case 'a':
1448 +                error(USER, "cannot recover ASCII output");
1449 +                return;
1450 +        case 'f':
1451 +                outvsiz = sizeof(float)*3;
1452 +                outvfmt = "float";
1453 +                break;
1454 +        case 'd':
1455 +                outvsiz = sizeof(double)*3;
1456 +                outvfmt = "double";
1457 +                break;
1458 +        case 'c':
1459 +                outvsiz = sizeof(COLR);
1460 +                outvfmt = COLRFMT;
1461 +                break;
1462 +        default:
1463 +                error(INTERNAL, "botched output format");
1464 +                return;
1465 +        }
1466 +                                                /* check modifier outputs */
1467 +        for (i = 0; i < nmods; i++) {
1468 +                ment = lu_find(&modconttab,modname[i]);
1469 +                mp = (MODCONT *)ment->data;
1470 +                if (mp->outspec == NULL)
1471 +                        error(USER, "cannot recover from stdout");
1472 +                if (mp->outspec[0] == '!')
1473 +                        error(USER, "cannot recover from command");
1474 +                for (j = 0; ; j++) {            /* check each bin's file */
1475 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1476 +                        if (ofl < 0)
1477 +                                error(USER, "bad output file specification");
1478 +                        oent = lu_find(&ofiletab, oname);
1479 +                        if (oent->data != NULL) {
1480 +                                sout = *(STREAMOUT *)oent->data;
1481 +                        } else {
1482 +                                sout.reclen = 0;
1483 +                                sout.outpipe = 0;
1484 +                                sout.ofp = NULL;
1485 +                        }
1486 +                        if (sout.ofp != NULL) { /* already open? */
1487 +                                if (ofl & OF_BIN)
1488 +                                        continue;
1489 +                                break;
1490 +                        }
1491 +                                                /* open output */
1492 +                        sout.ofp = fopen(oname, "rb+");
1493 +                        if (sout.ofp == NULL) {
1494 +                                if (j)
1495 +                                        break;  /* assume end of modifier */
1496 +                                sprintf(errmsg, "missing recover file '%s'",
1497 +                                                oname);
1498 +                                error(WARNING, errmsg);
1499 +                                break;
1500 +                        }
1501 +                        nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1502 +                        if (nvals <= 0) {
1503 +                                lastout = 0;    /* empty output, quit here */
1504 +                                fclose(sout.ofp);
1505 +                                break;
1506 +                        }
1507 +                        if (!sout.reclen) {
1508 +                                if (!(ofl & OF_BIN)) {
1509 +                                        sprintf(errmsg,
1510 +                                                "need -bn to recover file '%s'",
1511 +                                                        oname);
1512 +                                        error(USER, errmsg);
1513 +                                }
1514 +                                recsiz = outvsiz;
1515 +                        } else
1516 +                                recsiz = outvsiz * sout.reclen;
1517 +
1518 +                        lseek(fileno(sout.ofp), 0, SEEK_SET);
1519 +                        if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1520 +                                sprintf(errmsg, "format mismatch for '%s'",
1521 +                                                oname);
1522 +                                error(USER, errmsg);
1523 +                        }
1524 +                        sout.xr = xres; sout.yr = yres;
1525 +                        if ((sout.xr > 0) & (sout.yr > 0) &&
1526 +                                        (!fscnresolu(&xr, &yr, sout.ofp) ||
1527 +                                                (xr != sout.xr) |
1528 +                                                (yr != sout.yr))) {
1529 +                                sprintf(errmsg, "resolution mismatch for '%s'",
1530 +                                                oname);
1531 +                                error(USER, errmsg);
1532 +                        }
1533 +                        nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz;
1534 +                        if ((lastout < 0) | (nvals < lastout))
1535 +                                lastout = nvals;
1536 +                        if (oent->key == NULL)  /* new entry */
1537 +                                oent->key = strcpy((char *)
1538 +                                                malloc(strlen(oname)+1), oname);
1539 +                        if (oent->data == NULL)
1540 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1541 +                        *(STREAMOUT *)oent->data = sout;
1542 +                        if (!(ofl & OF_BIN))
1543 +                                break;          /* no bin separation */
1544 +                }
1545 +                if (!lastout) {                 /* empty output */
1546 +                        error(WARNING, "no previous data to recover");
1547 +                        lu_done(&ofiletab);     /* reclose all outputs */
1548 +                        return;
1549 +                }
1550 +                if (j > mp->nbins)              /* reallocate modifier bins */
1551 +                        ment->data = (char *)(mp = growmodifier(mp, j));
1552 +        }
1553 +        if (lastout < 0) {
1554 +                error(WARNING, "no output files to recover");
1555 +                return;
1556 +        }
1557 +        if (raysleft && lastout >= raysleft/accumulate) {
1558 +                error(WARNING, "output appears to be complete");
1559 +                /* XXX should read & discard input? */
1560 +                quit(0);
1561 +        }
1562 +                                                /* seek on all files */
1563 +        nvals = lastout * outvsiz;
1564 +        lu_doall(&ofiletab, myseeko, &nvals);
1565 +                                                /* skip repeated input */
1566 +        for (nvals = 0; nvals < lastout; nvals++)
1567 +                if (getinp(oname, fin) < 0)
1568 +                        error(USER, "unexpected EOF on input");
1569 +        lastray = lastdone = (unsigned long)lastout * accumulate;
1570 +        if (raysleft)
1571 +                raysleft -= lastray;
1572   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines