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.14 by greg, Thu Jun 9 17:27:28 2005 UTC vs.
Revision 1.64 by greg, Sat Apr 16 00:39:07 2011 UTC

# Line 16 | Line 16 | static const char RCSid[] = "$Id$";
16   #include  "lookup.h"
17   #include  "calcomp.h"
18  
19 + #ifndef MAXMODLIST
20   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
21 + #endif
22  
23 < int     treebufsiz = BUFSIZ;            /* current tree buffer size */
23 > ssize_t treebufsiz = BUFSIZ;            /* current tree buffer size */
24  
25   typedef double  DCOLOR[3];              /* double-precision color */
26  
27   /*
28 < * The modcont structure is used to accumulate ray contributions
28 > * The MODCONT structure is used to accumulate ray contributions
29   * for a particular modifier, which may be subdivided into bins
30 < * if binv is non-NULL.  If outspec contains a %s in it, this will
30 > * if binv evaluates > 0.  If outspec contains a %s in it, this will
31   * be replaced with the modifier name.  If outspec contains a %d in it,
32   * this will be used to create one output file per bin, otherwise all bins
33   * will be written to the same file, in order.  If the global outfmt
# Line 44 | Line 46 | static void mcfree(void *p) { epfree((*(MODCONT *)p).b
46  
47   LUTAB   modconttab = LU_SINIT(NULL,mcfree);     /* modifier lookup table */
48  
49 < /* close output stream */
50 < static void closefile(void *p) { fclose((FILE *)p); }
49 > /*
50 > * The STREAMOUT structure holds an open FILE pointer and a count of
51 > * the number of RGB triplets per record, or 0 if unknown.
52 > */
53 > typedef struct {
54 >        FILE            *ofp;           /* output file pointer */
55 >        int             outpipe;        /* output is to a pipe */
56 >        int             reclen;         /* triplets/record */
57 >        int             xr, yr;         /* output resolution for picture */
58 > } STREAMOUT;
59  
60 < LUTAB   ofiletab = LU_SINIT(free,closefile);    /* output file table */
60 > /* close output stream and free record */
61 > static void
62 > closestream(void *p)
63 > {
64 >        STREAMOUT       *sop = (STREAMOUT *)p;
65 >        int             status;
66 >        if (sop->outpipe)
67 >                status = pclose(sop->ofp);
68 >        else
69 >                status = fclose(sop->ofp);
70 >        if (status)
71 >                error(SYSTEM, "error closing output stream");
72 >        free(p);
73 > }
74  
75 + LUTAB   ofiletab = LU_SINIT(free,closestream);  /* output file table */
76 +
77   #define OF_MODIFIER     01
78   #define OF_BIN          02
79  
80 < FILE *getofile(const char *ospec, const char *mname, int bn);
80 > STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen);
81   int ofname(char *oname, const char *ospec, const char *mname, int bn);
82   void printheader(FILE *fout, const char *info);
83 + void printresolu(FILE *fout, int xr, int yr);
84  
85   /*
86   * The rcont structure is used to manage i/o with a particular
# Line 66 | Line 92 | struct rtproc {
92          struct rtproc   *next;          /* next in list of processes */
93          SUBPROC         pd;             /* rtrace pipe descriptors */
94          unsigned long   raynum;         /* ray number for this tree */
95 <        int             bsiz;           /* ray tree buffer length */
95 >        size_t          bsiz;           /* ray tree buffer length */
96          char            *buf;           /* ray tree buffer */
97 <        int             nbr;            /* number of bytes from rtrace */
97 >        size_t          nbr;            /* number of bytes from rtrace */
98   };                              /* rtrace process buffer */
99  
100                                          /* rtrace command and defaults */
101 < char            *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3",
102 <                                "-ab", "1", "-ad", "128", };
101 > char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
102 >                                "-dj", ".9", "-dr", "3",
103 >                                "-ab", "1", "-ad", "350", };
104 >
105   int  rtargc = 9;
106                                          /* overriding rtrace options */
107 < char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
107 > char            *myrtopts[] = { "-h-", "-x", "1", "-y", "0",
108                                  "-dt", "0", "-as", "0", "-aa", "0", NULL };
109  
110 + #define RTCOEFF         "-o~~~TmWdp"    /* compute coefficients only */
111 + #define RTCONTRIB       "-o~~~TmVdp"    /* compute ray contributions */
112 +
113   struct rtproc   rt0;                    /* head of rtrace process list */
114  
115   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
116  
117 < char    persistfn[] = "pfXXXXXX";       /* persist file name */
117 > #define PERSIST_NONE    0               /* no persist file */
118 > #define PERSIST_SINGLE  1               /* user set -P persist */
119 > #define PERSIST_PARALL  2               /* user set -PP persist */
120 > #define PERSIST_OURS    3               /* -PP persist belongs to us */
121 > int     persist_state = PERSIST_NONE;   /* persist file state */
122 > char    persistfn[] = "pfXXXXXX";       /* our persist file name, if set */
123  
124   int             gargc;                  /* global argc */
125   char            **gargv;                /* global argv */
# Line 95 | Line 131 | int            inpfmt = 'a';           /* input format */
131   int             outfmt = 'a';           /* output format */
132  
133   int             header = 1;             /* output header? */
134 + int             force_open = 0;         /* truncate existing output? */
135 + int             recover = 0;            /* recover previous output? */
136 + int             accumulate = 1;         /* input rays per output record */
137   int             xres = 0;               /* horiz. output resolution */
138   int             yres = 0;               /* vert. output resolution */
139  
140 + int             account;                /* current accumulation count */
141   unsigned long   raysleft;               /* number of rays left to trace */
142   long            waitflush;              /* how long until next flush */
143  
# Line 109 | Line 149 | int            using_stdout = 0;       /* are we using stdout? */
149   const char      *modname[MAXMODLIST];   /* ordered modifier name list */
150   int             nmods = 0;              /* number of modifiers */
151  
152 < MODCONT *addmodifier(char *modn, char *outf, char *binv);
152 > #define queue_length()          (lastray - lastdone)
153  
154 + MODCONT *growmodifier(MODCONT *mp, int nb);
155 + MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt);
156 + void addmodfile(char *fname, char *outf, char *binv, int bincnt);
157 +
158   void init(int np);
159   int done_rprocs(struct rtproc *rtp);
160 + void reload_output(void);
161   void recover_output(FILE *fin);
162   void trace_contribs(FILE *fin);
163   struct rtproc *wait_rproc(void);
# Line 122 | Line 167 | void process_queue(void);
167  
168   void put_contrib(const DCOLOR cnt, FILE *fout);
169   void add_contrib(const char *modn);
170 < void done_contrib(void);
170 > void done_contrib(int navg);
171  
172 + /* return number of open rtrace processes */
173 + static int
174 + nrtprocs(void)
175 + {
176 +        int     nrtp = 0;
177 +        struct rtproc   *rtp;
178 +
179 +        for (rtp = &rt0; rtp != NULL; rtp = rtp->next)
180 +                nrtp += rtp->pd.running;
181 +        return(nrtp);
182 + }
183 +
184   /* set input/output format */
185   static void
186   setformat(const char *fmt)
# Line 163 | Line 220 | fmterr:
220   int
221   main(int argc, char *argv[])
222   {
223 +        int     contrib = 0;
224          int     nprocs = 1;
167        int     recover = 0;
225          char    *curout = NULL;
226          char    *binval = NULL;
227 +        int     bincnt = 0;
228          char    fmt[8];
229          int     i, j;
230 +                                /* need at least one argument */
231 +        if (argc < 2) {
232 +                fprintf(stderr,
233 + "Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
234 +                        argv[0]);
235 +                exit(1);
236 +        }
237                                  /* global program name */
238          gargv = argv;
239                                  /* initialize calcomp routines */
# Line 181 | Line 246 | main(int argc, char *argv[])
246                  while ((j = expandarg(&argc, &argv, i)) > 0)
247                          ;
248                  if (j < 0) {
249 <                        fprintf(stderr, "%s: cannot expand '%s'",
249 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
250                                          argv[0], argv[i]);
251                          exit(1);
252                  }
253                  if (argv[i][0] == '-')
254                          switch (argv[i][1]) {
190                        case 'r':               /* recover output */
191                                if (argv[i][2]) break;
192                                recover++;
193                                continue;
255                          case 'n':               /* number of processes */
256 <                                if (argv[i][2] || i >= argc-1) break;
256 >                                if (argv[i][2] || i >= argc-2) break;
257                                  nprocs = atoi(argv[++i]);
258                                  if (nprocs <= 0)
259                                          error(USER, "illegal number of processes");
260                                  continue;
261 +                        case 'V':               /* output contributions */
262 +                                switch (argv[i][2]) {
263 +                                case '\0':
264 +                                        contrib = !contrib;
265 +                                        continue;
266 +                                case '+': case '1':
267 +                                case 'T': case 't':
268 +                                case 'Y': case 'y':
269 +                                        contrib = 1;
270 +                                        continue;
271 +                                case '-': case '0':
272 +                                case 'F': case 'f':
273 +                                case 'N': case 'n':
274 +                                        contrib = 0;
275 +                                        continue;
276 +                                }
277 +                                break;
278 +                        case 'c':               /* input rays per output */
279 +                                if (argv[i][2] || i >= argc-2) break;
280 +                                accumulate = atoi(argv[++i]);
281 +                                continue;
282 +                        case 'r':               /* recover output */
283 +                                if (argv[i][2]) break;
284 +                                recover = 1;
285 +                                continue;
286                          case 'h':               /* output header? */
287                                  switch (argv[i][2]) {
288                                  case '\0':
# Line 214 | Line 300 | main(int argc, char *argv[])
300                                          continue;
301                                  }
302                                  break;
303 <                        case 'f':               /* file or i/o format */
303 >                        case 'f':               /* file or force or format */
304                                  if (!argv[i][2]) {
305                                          char    *fpath;
306 <                                        if (i >= argc-1) break;
306 >                                        if (i >= argc-2) break;
307                                          fpath = getpath(argv[++i],
308                                                          getrlibpath(), R_OK);
309                                          if (fpath == NULL) {
# Line 229 | Line 315 | main(int argc, char *argv[])
315                                          fcompile(fpath);
316                                          continue;
317                                  }
318 +                                if (argv[i][2] == 'o') {
319 +                                        force_open = 1;
320 +                                        continue;
321 +                                }
322                                  setformat(argv[i]+2);
323                                  continue;
324                          case 'e':               /* expression */
325 <                                if (argv[i][2] || i >= argc-1) break;
325 >                                if (argv[i][2] || i >= argc-2) break;
326                                  scompile(argv[++i], NULL, 0);
327                                  continue;
328                          case 'o':               /* output file spec. */
329 <                                if (argv[i][2] || i >= argc-1) break;
329 >                                if (argv[i][2] || i >= argc-2) break;
330                                  curout = argv[++i];
331                                  continue;
332                          case 'x':               /* horiz. output resolution */
333 <                                if (argv[i][2] || i >= argc-1) break;
333 >                                if (argv[i][2] || i >= argc-2) break;
334                                  xres = atoi(argv[++i]);
335                                  continue;
336                          case 'y':               /* vert. output resolution */
337 <                                if (argv[i][2] || i >= argc-1) break;
337 >                                if (argv[i][2] || i >= argc-2) break;
338                                  yres = atoi(argv[++i]);
339                                  continue;
340 <                        case 'b':               /* bin expression */
341 <                                if (argv[i][2] || i >= argc-1) break;
340 >                        case 'b':               /* bin expression/count */
341 >                                if (i >= argc-2) break;
342 >                                if (argv[i][2] == 'n') {
343 >                                        bincnt = (int)(eval(argv[++i]) + .5);
344 >                                        continue;
345 >                                }
346 >                                if (argv[i][2]) break;
347                                  binval = argv[++i];
348                                  continue;
349                          case 'm':               /* modifier name */
350 <                                if (argv[i][2] || i >= argc-1) break;
350 >                                if (argv[i][2] || i >= argc-2) break;
351                                  rtargv[rtargc++] = "-ti";
352                                  rtargv[rtargc++] = argv[++i];
353 <                                addmodifier(argv[i], curout, binval);
353 >                                addmodifier(argv[i], curout, binval, bincnt);
354                                  continue;
355 +                        case 'M':               /* modifier file */
356 +                                if (argv[i][2] || i >= argc-2) break;
357 +                                rtargv[rtargc++] = "-tI";
358 +                                rtargv[rtargc++] = argv[++i];
359 +                                addmodfile(argv[i], curout, binval, bincnt);
360 +                                continue;
361 +                        case 'P':               /* persist file */
362 +                                if (i >= argc-2) break;
363 +                                persist_state = (argv[i][2] == 'P') ?
364 +                                                PERSIST_PARALL : PERSIST_SINGLE;
365 +                                rtargv[rtargc++] = argv[i];
366 +                                rtargv[rtargc++] = argv[++i];
367 +                                continue;
368                          }
369                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
370          }
371 +        if (accumulate <= 0)    /* no output flushing for single record */
372 +                xres = yres = 0;
373                                  /* set global argument list */
374          gargc = argc; gargv = argv;
375                                  /* add "mandatory" rtrace settings */
376          for (j = 0; myrtopts[j] != NULL; j++)
377                  rtargv[rtargc++] = myrtopts[j];
378 +        rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
379                                  /* just asking for defaults? */
380          if (!strcmp(argv[i], "-defaults")) {
381 <                char    sxres[16], syres[16];
381 >                char    nps[8], sxres[16], syres[16];
382                  char    *rtpath;
383 <                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
383 >                printf("-c %-5d\t\t\t# accumulated rays per record\n",
384 >                                accumulate);
385 >                printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
386 >                                contrib ? "contributions" : "coefficients");
387                  fflush(stdout);                 /* report OUR options */
388 +                rtargv[rtargc++] = "-n";
389 +                sprintf(nps, "%d", nprocs);
390 +                rtargv[rtargc++] = nps;
391                  rtargv[rtargc++] = header ? "-h+" : "-h-";
392                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
393                  rtargv[rtargc++] = fmt;
# Line 280 | Line 397 | main(int argc, char *argv[])
397                  rtargv[rtargc++] = "-y";
398                  sprintf(syres, "%d", yres);
399                  rtargv[rtargc++] = syres;
283                rtargv[rtargc++] = "-oTW";
400                  rtargv[rtargc++] = "-defaults";
401                  rtargv[rtargc] = NULL;
402                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 294 | Line 410 | main(int argc, char *argv[])
410                  exit(1);
411          }
412          if (nprocs > 1) {       /* add persist file if parallel */
413 <                rtargv[rtargc++] = "-PP";
414 <                rtargv[rtargc++] = mktemp(persistfn);
413 >                if (persist_state == PERSIST_SINGLE)
414 >                        error(USER, "use -PP option for multiple processes");
415 >                if (persist_state == PERSIST_NONE) {
416 >                        rtargv[rtargc++] = "-PP";
417 >                        rtargv[rtargc++] = mktemp(persistfn);
418 >                        persist_state = PERSIST_OURS;
419 >                }
420          }
421                                  /* add format string */
422          sprintf(fmt, "-f%cf", inpfmt);
# Line 305 | Line 426 | main(int argc, char *argv[])
426                  error(USER, "missing octree argument");
427          rtargv[rtargc++] = octree = argv[i];
428          rtargv[rtargc] = NULL;
429 <                                /* start rtrace & compute contributions */
429 >                                /* start rtrace & recover if requested */
430          init(nprocs);
431 <        if (recover)            /* perform recovery if requested */
311 <                recover_output(stdin);
431 >                                /* compute contributions */
432          trace_contribs(stdin);
433 +                                /* clean up */
434          quit(0);
435   }
436  
437 + #ifndef SIGALRM
438 + #define SIGALRM SIGTERM
439 + #endif
440   /* kill persistent rtrace process */
441   static void
442   killpersist(void)
443   {
444          FILE    *fp = fopen(persistfn, "r");
445 <        int     pid;
445 >        RT_PID  pid;
446  
447          if (fp == NULL)
448                  return;
# Line 352 | Line 476 | quit(int status)
476   {
477          int     rtstat;
478  
479 <        if (rt0.next != NULL)           /* terminate persistent rtrace */
479 >        if (persist_state == PERSIST_OURS)  /* terminate waiting rtrace */
480                  killpersist();
481                                          /* clean up rtrace process(es) */
482          rtstat = done_rprocs(&rt0);
# Line 419 | Line 543 | init(int np)
543                          raysleft = yres;
544          } else
545                  raysleft = 0;
546 <        waitflush = xres;
546 >        if ((account = accumulate) > 0)
547 >                raysleft *= accumulate;
548 >        waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
549 >        if (!recover)
550 >                return;
551 >                                        /* recover previous values */
552 >        if (accumulate <= 0)
553 >                reload_output();
554 >        else
555 >                recover_output(stdin);
556   }
557  
558 + /* grow modifier to accommodate more bins */
559 + MODCONT *
560 + growmodifier(MODCONT *mp, int nb)
561 + {
562 +        if (nb <= mp->nbins)
563 +                return mp;
564 +        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1));
565 +        if (mp == NULL)
566 +                error(SYSTEM, "out of memory in growmodifier");
567 +        memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins));
568 +        mp->nbins = nb;
569 +        return mp;
570 + }
571 +
572   /* add modifier to our list to track */
573   MODCONT *
574 < addmodifier(char *modn, char *outf, char *binv)
574 > addmodifier(char *modn, char *outf, char *binv, int bincnt)
575   {
576          LUENT   *lep = lu_find(&modconttab, modn);
577          MODCONT *mp;
578 +        int     i;
579          
580          if (lep->data != NULL) {
581                  sprintf(errmsg, "duplicate modifier '%s'", modn);
582                  error(USER, errmsg);
583          }
584          if (nmods >= MAXMODLIST)
585 <                error(USER, "too many modifiers");
585 >                error(INTERNAL, "too many modifiers");
586          modname[nmods++] = modn;        /* XXX assumes static string */
587          lep->key = modn;                /* XXX assumes static string */
588          mp = (MODCONT *)malloc(sizeof(MODCONT));
589          if (mp == NULL)
590                  error(SYSTEM, "out of memory in addmodifier");
443        lep->data = (char *)mp;
591          mp->outspec = outf;             /* XXX assumes static string */
592          mp->modname = modn;             /* XXX assumes static string */
593 <        if (binv != NULL)
594 <                mp->binv = eparse(binv);
595 <        else
596 <                mp->binv = eparse("0");
597 <        mp->nbins = 1;
593 >        if (binv == NULL)
594 >                binv = "0";             /* use single bin if unspecified */
595 >        mp->binv = eparse(binv);
596 >        if (mp->binv->type == NUM) {    /* check value if constant */
597 >                bincnt = (int)(evalue(mp->binv) + 1.5);
598 >                if (bincnt != 1) {
599 >                        sprintf(errmsg, "illegal non-zero constant for bin (%s)",
600 >                                        binv);
601 >                        error(USER, errmsg);
602 >                }
603 >        }
604 >        mp->nbins = 1;                  /* initialize results holder */
605          setcolor(mp->cbin[0], 0., 0., 0.);
606 +        if (bincnt > 1)
607 +                mp = growmodifier(mp, bincnt);
608 +        lep->data = (char *)mp;
609 +                                        /* allocate output streams */
610 +        for (i = bincnt; i-- > 0; )
611 +                getostream(mp->outspec, mp->modname, i, 1);
612          return mp;
613   }
614  
615 + /* add modifiers from a file list */
616 + void
617 + addmodfile(char *fname, char *outf, char *binv, int bincnt)
618 + {
619 +        char    *mname[MAXMODLIST];
620 +        int     i;
621 +                                        /* find the file & store strings */
622 +        if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
623 +                sprintf(errmsg, "cannot find modifier file '%s'", fname);
624 +                error(SYSTEM, errmsg);
625 +        }
626 +        for (i = 0; mname[i]; i++)      /* add each one */
627 +                addmodifier(mname[i], outf, binv, bincnt);
628 + }
629 +
630   /* put string to stderr */
631   void
632   eputs(char  *s)
# Line 491 | Line 666 | ofname(char *oname, const char *ospec, const char *mna
666                                  mnp = cp;
667                                  break;
668                          case 'd':
669 +                        case 'i':
670 +                        case 'o':
671 +                        case 'x':
672 +                        case 'X':
673                                  if (bnp != NULL)
674                                          return -1;
675                                  bnp = cp;
# Line 523 | Line 702 | void
702   printheader(FILE *fout, const char *info)
703   {
704          extern char     VersionID[];
705 <        FILE            *fin = fopen(octree, "r");
706 <        
707 <        if (fin == NULL)
708 <                quit(1);
709 <        checkheader(fin, "ignore", fout);       /* copy octree header */
710 <        fclose(fin);
705 >                                                /* copy octree header */
706 >        if (octree[0] == '!') {
707 >                newheader("RADIANCE", fout);
708 >                fputs(octree+1, fout);
709 >                if (octree[strlen(octree)-1] != '\n')
710 >                        fputc('\n', fout);
711 >        } else {
712 >                FILE    *fin = fopen(octree, "r");
713 >                if (fin == NULL)
714 >                        quit(1);
715 >                checkheader(fin, "ignore", fout);
716 >                fclose(fin);
717 >        }
718          printargs(gargc-1, gargv, fout);        /* add our command */
719          fprintf(fout, "SOFTWARE= %s\n", VersionID);
720          fputnow(fout);
# Line 549 | Line 735 | printheader(FILE *fout, const char *info)
735                  break;
736          }
737          fputc('\n', fout);
552        if (xres > 0) {
553                if (yres > 0)                   /* resolution string */
554                        fprtresolu(xres, yres, fout);
555                fflush(fout);
556        }
738   }
739  
740 < /* Get output file pointer (open and write header if new) */
741 < FILE *
742 < getofile(const char *ospec, const char *mname, int bn)
740 > /* write resolution string to given output stream */
741 > void
742 > printresolu(FILE *fout, int xr, int yr)
743   {
744 <        int             ofl;
745 <        char            oname[1024];
746 <        LUENT           *lep;
744 >        if ((xr > 0) & (yr > 0))        /* resolution string */
745 >                fprtresolu(xr, yr, fout);
746 > }
747 >
748 > /* Get output stream pointer (open and write header if new and noopen==0) */
749 > STREAMOUT *
750 > getostream(const char *ospec, const char *mname, int bn, int noopen)
751 > {
752 >        static const DCOLOR     nocontrib = BLKCOLOR;
753 >        static STREAMOUT        stdos;
754 >        int                     ofl;
755 >        char                    oname[1024];
756 >        LUENT                   *lep;
757 >        STREAMOUT               *sop;
758          
759          if (ospec == NULL) {                    /* use stdout? */
760 <                if (!using_stdout) {
760 >                if (!noopen && !using_stdout) {
761                          if (outfmt != 'a')
762                                  SET_FILE_BINARY(stdout);
763                          if (header)
764                                  printheader(stdout, NULL);
765 +                        printresolu(stdout, xres, yres);
766 +                        if (waitflush > 0)
767 +                                fflush(stdout);
768 +                        stdos.xr = xres; stdos.yr = yres;
769 +                        using_stdout = 1;
770                  }
771 <                using_stdout = 1;
772 <                return stdout;
771 >                stdos.ofp = stdout;
772 >                stdos.reclen += noopen;
773 >                return &stdos;
774          }
775          ofl = ofname(oname, ospec, mname, bn);  /* get output name */
776          if (ofl < 0) {
# Line 582 | Line 780 | getofile(const char *ospec, const char *mname, int bn)
780          lep = lu_find(&ofiletab, oname);        /* look it up */
781          if (lep->key == NULL)                   /* new entry */
782                  lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
783 <        if (lep->data == NULL) {                /* open output file */
784 <                FILE            *fp;
785 <                int             i;
786 <                if (oname[0] == '!')            /* output to command */
787 <                        fp = popen(oname+1, "w");
788 <                else
789 <                        fp = fopen(oname, "w");
790 <                if (fp == NULL) {
791 <                        sprintf(errmsg, "cannot open '%s' for writing", oname);
792 <                        error(SYSTEM, errmsg);
783 >        sop = (STREAMOUT *)lep->data;
784 >        if (sop == NULL) {                      /* allocate stream */
785 >                sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
786 >                if (sop == NULL)
787 >                        error(SYSTEM, "out of memory in getostream");
788 >                sop->outpipe = oname[0] == '!';
789 >                sop->reclen = 0;
790 >                sop->ofp = NULL;                /* open iff noopen==0 */
791 >                sop->xr = xres; sop->yr = yres;
792 >                lep->data = (char *)sop;
793 >                if (!sop->outpipe & !force_open & !recover &&
794 >                                access(oname, F_OK) == 0) {
795 >                        errno = EEXIST;         /* file exists */
796 >                        goto openerr;
797                  }
798 +        }
799 +        if (!noopen && sop->ofp == NULL) {      /* open output stream */
800 +                long            i;
801 +                if (oname[0] == '!')            /* output to command */
802 +                        sop->ofp = popen(oname+1, "w");
803 +                else                            /* else open file */
804 +                        sop->ofp = fopen(oname, "w");
805 +                if (sop->ofp == NULL)
806 +                        goto openerr;
807                  if (outfmt != 'a')
808 <                        SET_FILE_BINARY(fp);
808 >                        SET_FILE_BINARY(sop->ofp);
809                  if (header) {
810                          char    info[512];
811                          char    *cp = info;
812 <                        if (ofl & OF_MODIFIER) {
812 >                        if (ofl & OF_MODIFIER || sop->reclen == 1) {
813                                  sprintf(cp, "MODIFIER=%s\n", mname);
814                                  while (*cp) ++cp;
815                          }
# Line 607 | Line 818 | getofile(const char *ospec, const char *mname, int bn)
818                                  while (*cp) ++cp;
819                          }
820                          *cp = '\0';
821 <                        printheader(fp, info);
821 >                        printheader(sop->ofp, info);
822                  }
823 +                if (accumulate > 0) {           /* global resolution */
824 +                        sop->xr = xres; sop->yr = yres;
825 +                }
826 +                printresolu(sop->ofp, sop->xr, sop->yr);
827                                                  /* play catch-up */
828 <                for (i = 0; i < lastdone; i++) {
829 <                        static const DCOLOR     nocontrib = BLKCOLOR;
830 <                        put_contrib(nocontrib, fp);
828 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
829 >                        int     j = sop->reclen;
830 >                        if (j <= 0) j = 1;
831 >                        while (j--)
832 >                                put_contrib(nocontrib, sop->ofp);
833                          if (outfmt == 'a')
834 <                                putc('\n', fp);
834 >                                putc('\n', sop->ofp);
835                  }
836 <                if (xres > 0)
837 <                        fflush(fp);
621 <                lep->data = (char *)fp;
836 >                if (waitflush > 0)
837 >                        fflush(sop->ofp);
838          }
839 <        return (FILE *)lep->data;               /* return open file pointer */
839 >        sop->reclen += noopen;                  /* add to length if noopen */
840 >        return sop;                             /* return output stream */
841 > openerr:
842 >        sprintf(errmsg, "cannot open '%s' for writing", oname);
843 >        error(SYSTEM, errmsg);
844 >        return NULL;    /* pro forma return */
845   }
846  
847   /* read input ray into buffer */
848   int
849   getinp(char *buf, FILE *fp)
850   {
851 +        double  dv[3], *dvp;
852 +        float   *fvp;
853          char    *cp;
854          int     i;
855  
# Line 635 | Line 858 | getinp(char *buf, FILE *fp)
858                  cp = buf;               /* make sure we get 6 floats */
859                  for (i = 0; i < 6; i++) {
860                          if (fgetword(cp, buf+127-cp, fp) == NULL)
861 <                                return 0;
861 >                                return -1;
862 >                        if (i >= 3)
863 >                                dv[i-3] = atof(cp);
864                          if ((cp = fskip(cp)) == NULL || *cp)
865 <                                return 0;
865 >                                return -1;
866                          *cp++ = ' ';
867                  }
868                  getc(fp);               /* get/put eol */
869                  *cp-- = '\0'; *cp = '\n';
870 +                if (DOT(dv,dv) <= FTINY*FTINY)
871 +                        return 0;       /* dummy ray */
872                  return strlen(buf);
873          case 'f':
874                  if (fread(buf, sizeof(float), 6, fp) < 6)
875 <                        return 0;
875 >                        return -1;
876 >                fvp = (float *)buf + 3;
877 >                if (DOT(fvp,fvp) <= FTINY*FTINY)
878 >                        return 0;       /* dummy ray */
879                  return sizeof(float)*6;
880          case 'd':
881                  if (fread(buf, sizeof(double), 6, fp) < 6)
882 <                        return 0;
882 >                        return -1;
883 >                dvp = (double *)buf + 3;
884 >                if (DOT(dvp,dvp) <= FTINY*FTINY)
885 >                        return 0;       /* dummy ray */
886                  return sizeof(double)*6;
887          }
888          error(INTERNAL, "botched input format");
889 <        return 0;       /* pro forma return */
889 >        return -1;      /* pro forma return */
890   }
891  
892   static float    rparams[9];             /* traced ray parameters */
# Line 683 | Line 916 | add_contrib(const char *modn)
916          bn = (int)(evalue(mp->binv) + .5);
917          if (bn <= 0)
918                  bn = 0;
919 <        else if (bn > mp->nbins) {      /* new bin */
920 <                mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
688 <                                                bn*sizeof(DCOLOR));
689 <                if (mp == NULL)
690 <                        error(SYSTEM, "out of memory in add_contrib");
691 <                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
692 <                mp->nbins = bn+1;
693 <                le->data = (char *)mp;
694 <        }
919 >        else if (bn >= mp->nbins)       /* new bin */
920 >                le->data = (char *)(mp = growmodifier(mp, bn+1));
921          addcolor(mp->cbin[bn], rparams);
922   }
923  
# Line 699 | Line 925 | add_contrib(const char *modn)
925   static int
926   puteol(const LUENT *e, void *p)
927   {
928 <        FILE    *fp = (FILE *)e->data;
928 >        STREAMOUT       *sop = (STREAMOUT *)e->data;
929  
930          if (outfmt == 'a')
931 <                putc('\n', fp);
931 >                putc('\n', sop->ofp);
932          if (!waitflush)
933 <                fflush(fp);
934 <        if (ferror(fp)) {
933 >                fflush(sop->ofp);
934 >        if (ferror(sop->ofp)) {
935                  sprintf(errmsg, "write error on file '%s'", e->key);
936                  error(SYSTEM, errmsg);
937          }
# Line 724 | Line 950 | put_contrib(const DCOLOR cnt, FILE *fout)
950                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
951                  break;
952          case 'f':
953 <                fv[0] = cnt[0];
728 <                fv[1] = cnt[1];
729 <                fv[2] = cnt[2];
953 >                copycolor(fv, cnt);
954                  fwrite(fv, sizeof(float), 3, fout);
955                  break;
956          case 'd':
# Line 741 | Line 965 | put_contrib(const DCOLOR cnt, FILE *fout)
965          }
966   }
967  
968 < /* output ray tallies and clear for next primary */
968 > /* output ray tallies and clear for next accumulation */
969   void
970 < done_contrib(void)
970 > done_contrib(int navg)
971   {
972 <        int     i, j;
973 <        MODCONT *mp;
974 <        FILE    *fp;
972 >        double          sf = 1.;
973 >        int             i, j;
974 >        MODCONT         *mp;
975 >        STREAMOUT       *sop;
976 >                                                /* set average scaling */
977 >        if (navg > 1)
978 >                sf = 1. / (double)navg;
979                                                  /* output modifiers in order */
980          for (i = 0; i < nmods; i++) {
981                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
982 <                fp = getofile(mp->outspec, mp->modname, 0);
983 <                put_contrib(mp->cbin[0], fp);
982 >                if (navg > 1)                   /* average scaling */
983 >                        for (j = mp->nbins; j--; )
984 >                                scalecolor(mp->cbin[j], sf);
985 >                sop = getostream(mp->outspec, mp->modname, 0,0);
986 >                put_contrib(mp->cbin[0], sop->ofp);
987                  if (mp->nbins > 3 &&            /* minor optimization */
988 <                                fp == getofile(mp->outspec, mp->modname, 1))
988 >                                sop == getostream(mp->outspec, mp->modname, 1,0))
989                          for (j = 1; j < mp->nbins; j++)
990 <                                put_contrib(mp->cbin[j], fp);
990 >                                put_contrib(mp->cbin[j], sop->ofp);
991                  else
992                          for (j = 1; j < mp->nbins; j++)
993                                  put_contrib(mp->cbin[j],
994 <                                        getofile(mp->outspec, mp->modname, j));
995 <                                                /* clear for next ray tree */
994 >                                    getostream(mp->outspec,mp->modname,j,0)->ofp);
995 >                                                /* clear for next time */
996                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
997          }
998          --waitflush;                            /* terminate records */
# Line 769 | Line 1000 | done_contrib(void)
1000          if (using_stdout & (outfmt == 'a'))
1001                  putc('\n', stdout);
1002          if (!waitflush) {
1003 <                waitflush = xres;
1003 >                waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
1004                  if (using_stdout)
1005                          fflush(stdout);
1006          }
# Line 820 | Line 1051 | process_queue(void)
1051                          if (!n || !(isalpha(*cp) | (*cp == '_')))
1052                                  error(USER, "bad modifier name from rtrace");
1053                                          /* get modifier name */
1054 <                        while (n > 0 && *cp != '\t') {
1054 >                        while (n > 1 && *cp != '\t') {
1055 >                                if (mnp - modname >= sizeof(modname)-2)
1056 >                                        error(INTERNAL, "modifier name too long");
1057                                  *mnp++ = *cp++; n--;
1058                          }
1059                          *mnp = '\0';
# Line 832 | Line 1065 | process_queue(void)
1065                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1066                          add_contrib(modname);
1067                  }
1068 <                done_contrib();         /* sum up contributions & output */
1068 >                                        /* time to produce record? */
1069 >                if (account > 0 && !--account)
1070 >                        done_contrib(account = accumulate);
1071                  lastdone = rtp->raynum;
1072 <                free(rtp->buf);         /* free up buffer space */
1072 >                if (rtp->buf != NULL)   /* free up buffer space */
1073 >                        free(rtp->buf);
1074                  rt_unproc = rtp->next;
1075                  free(rtp);              /* done with this ray tree */
1076          }
# Line 846 | Line 1082 | wait_rproc(void)
1082   {
1083          struct rtproc   *rtfree = NULL;
1084          fd_set          readset, errset;
1085 <        int             nr;
1085 >        ssize_t         nr;
1086          struct rtproc   *rt;
1087          int             n;
1088          
# Line 877 | Line 1113 | wait_rproc(void)
1113                                  continue;
1114                          if (rt->buf == NULL) {
1115                                  rt->bsiz = treebufsiz;
1116 <                                rt->buf = (char *)malloc(treebufsiz);
1116 >                                rt->buf = (char *)malloc(rt->bsiz);
1117                          } else if (rt->nbr + BUFSIZ > rt->bsiz) {
1118                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
1119                                          rt->bsiz = treebufsiz;
1120 <                                else
1121 <                                        rt->bsiz = treebufsiz += BUFSIZ;
1120 >                                else if ((treebufsiz = rt->bsiz += BUFSIZ) < 0)
1121 >                                        error(INTERNAL,
1122 >                                            "ray buffer does not fit memory");
1123                                  rt->buf = (char *)realloc(rt->buf, rt->bsiz);
1124                          }
1125                          if (rt->buf == NULL)
1126                                  error(SYSTEM, "out of memory in wait_rproc");
1127 <                        nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
1128 <                        if (nr <= 0)
1127 >                        nr = rt->bsiz - rt->nbr;
1128 >                        if (nr & ~0x7fffffff)   /* avoid 32-bit OS issues */
1129 >                                nr = 0x7fffffff;
1130 >                        nr = read(rt->pd.r, rt->buf+rt->nbr, nr);
1131 >                        if (nr < 0)
1132 >                                error(SYSTEM, "read error from rtrace");
1133 >                        if (!nr)
1134                                  error(USER, "rtrace process died");
1135                          rt->nbr += nr;          /* advance & check */
1136 <                        if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
1137 <                                                        "~\t~\t", 4)) {
1138 <                                rt->nbr -= 4;   /* elide terminator */
1136 >                        if (rt->nbr >= 6 && !memcmp(rt->buf+rt->nbr-6,
1137 >                                                        "~\t~\t~\t", 6)) {
1138 >                                rt->nbr -= 6;   /* elide terminator */
1139                                  queue_raytree(rt);
1140                                  rtfree = rt;    /* ready for next ray */
1141                          }
# Line 918 | Line 1160 | get_rproc(void)
1160   void
1161   trace_contribs(FILE *fin)
1162   {
1163 +        static int      ignore_warning_given = 0;
1164          char            inpbuf[128];
1165          int             iblen;
1166          struct rtproc   *rtp;
1167                                                  /* loop over input */
1168 <        while ((iblen = getinp(inpbuf, fin)) > 0) {
1169 <                if (lastray+1 < lastray) {      /* counter rollover? */
1168 >        while ((iblen = getinp(inpbuf, fin)) >= 0) {
1169 >                if (!iblen && accumulate != 1) {
1170 >                        if (!ignore_warning_given++)
1171 >                                error(WARNING,
1172 >                                "dummy ray(s) ignored during accumulation\n");
1173 >                        continue;
1174 >                }
1175 >                if (!iblen ||                   /* need flush/reset? */
1176 >                                queue_length() > 10*nrtprocs() ||
1177 >                                lastray+1 < lastray) {
1178                          while (wait_rproc() != NULL)
1179                                  process_queue();
1180                          lastdone = lastray = 0;
1181                  }
1182                  rtp = get_rproc();              /* get avail. rtrace process */
1183 <                rtp->raynum = ++lastray;        /* assign ray to it */
1184 <                writebuf(rtp->pd.w, inpbuf, iblen);
1185 <                if (raysleft && !--raysleft)
1186 <                        break;
1183 >                rtp->raynum = ++lastray;        /* assign ray */
1184 >                if (iblen) {                    /* trace ray if valid */
1185 >                        writebuf(rtp->pd.w, inpbuf, iblen);
1186 >                } else {                        /* else bypass dummy ray */
1187 >                        queue_raytree(rtp);     /* queue empty ray/record */
1188 >                        if ((yres <= 0) | (xres <= 0))
1189 >                                waitflush = 1;  /* flush right after */
1190 >                }
1191                  process_queue();                /* catch up with results */
1192 +                if (raysleft && !--raysleft)
1193 +                        break;                  /* preemptive EOI */
1194          }
1195          while (wait_rproc() != NULL)            /* process outstanding rays */
1196                  process_queue();
1197 +        if (accumulate <= 0)
1198 +                done_contrib(0);                /* output tallies */
1199 +        else if (account < accumulate) {
1200 +                error(WARNING, "partial accumulation in final record");
1201 +                done_contrib(accumulate - account);
1202 +        }
1203          if (raysleft)
1204                  error(USER, "unexpected EOF on input");
1205          lu_done(&ofiletab);                     /* close output files */
1206   }
1207  
1208 + /* get ray contribution from previous file */
1209 + static int
1210 + get_contrib(DCOLOR cnt, FILE *finp)
1211 + {
1212 +        COLOR   fv;
1213 +        COLR    cv;
1214 +
1215 +        switch (outfmt) {
1216 +        case 'a':
1217 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1218 +        case 'f':
1219 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1220 +                        return(0);
1221 +                copycolor(cnt, fv);
1222 +                return(1);
1223 +        case 'd':
1224 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1225 +        case 'c':
1226 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1227 +                        return(0);
1228 +                colr_color(fv, cv);
1229 +                copycolor(cnt, fv);
1230 +                return(1);
1231 +        default:
1232 +                error(INTERNAL, "botched output format");
1233 +        }
1234 +        return(0);      /* pro forma return */
1235 + }
1236 +
1237 + /* close output file opened for input */
1238 + static int
1239 + myclose(const LUENT *e, void *p)
1240 + {
1241 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1242 +        
1243 +        if (sop->ofp == NULL)
1244 +                return(0);
1245 +        fclose(sop->ofp);
1246 +        sop->ofp = NULL;
1247 +        return(0);
1248 + }
1249 +
1250 + /* load previously accumulated values */
1251 + void
1252 + reload_output(void)
1253 + {
1254 +        int             i, j;
1255 +        MODCONT         *mp;
1256 +        int             ofl;
1257 +        char            oname[1024];
1258 +        char            *fmode = "rb";
1259 +        char            *outvfmt;
1260 +        LUENT           *ment, *oent;
1261 +        int             xr, yr;
1262 +        STREAMOUT       sout;
1263 +        DCOLOR          rgbv;
1264 +
1265 +        switch (outfmt) {
1266 +        case 'a':
1267 +                outvfmt = "ascii";
1268 +                fmode = "r";
1269 +                break;
1270 +        case 'f':
1271 +                outvfmt = "float";
1272 +                break;
1273 +        case 'd':
1274 +                outvfmt = "double";
1275 +                break;
1276 +        case 'c':
1277 +                outvfmt = COLRFMT;
1278 +                break;
1279 +        default:
1280 +                error(INTERNAL, "botched output format");
1281 +                return;
1282 +        }
1283 +                                                /* reload modifier values */
1284 +        for (i = 0; i < nmods; i++) {
1285 +                ment = lu_find(&modconttab,modname[i]);
1286 +                mp = (MODCONT *)ment->data;
1287 +                if (mp->outspec == NULL)
1288 +                        error(USER, "cannot reload from stdout");
1289 +                if (mp->outspec[0] == '!')
1290 +                        error(USER, "cannot reload from command");
1291 +                for (j = 0; ; j++) {            /* load each modifier bin */
1292 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1293 +                        if (ofl < 0)
1294 +                                error(USER, "bad output file specification");
1295 +                        oent = lu_find(&ofiletab, oname);
1296 +                        if (oent->data != NULL) {
1297 +                                sout = *(STREAMOUT *)oent->data;
1298 +                        } else {
1299 +                                sout.reclen = 0;
1300 +                                sout.outpipe = 0;
1301 +                                sout.xr = xres; sout.yr = yres;
1302 +                                sout.ofp = NULL;
1303 +                        }
1304 +                        if (sout.ofp == NULL) { /* open output as input */
1305 +                                sout.ofp = fopen(oname, fmode);
1306 +                                if (sout.ofp == NULL) {
1307 +                                        if (j)
1308 +                                                break;  /* assume end of modifier */
1309 +                                        sprintf(errmsg, "missing reload file '%s'",
1310 +                                                        oname);
1311 +                                        error(WARNING, errmsg);
1312 +                                        break;
1313 +                                }
1314 +                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1315 +                                        sprintf(errmsg, "format mismatch for '%s'",
1316 +                                                        oname);
1317 +                                        error(USER, errmsg);
1318 +                                }
1319 +                                if ((sout.xr > 0) & (sout.yr > 0) &&
1320 +                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1321 +                                                        (xr != sout.xr) |
1322 +                                                        (yr != sout.yr))) {
1323 +                                        sprintf(errmsg, "resolution mismatch for '%s'",
1324 +                                                        oname);
1325 +                                        error(USER, errmsg);
1326 +                                }
1327 +                        }
1328 +                                                        /* read in RGB value */
1329 +                        if (!get_contrib(rgbv, sout.ofp)) {
1330 +                                if (!j) {
1331 +                                        fclose(sout.ofp);
1332 +                                        break;          /* ignore empty file */
1333 +                                }
1334 +                                if (j < mp->nbins) {
1335 +                                        sprintf(errmsg, "missing data in '%s'",
1336 +                                                        oname);
1337 +                                        error(USER, errmsg);
1338 +                                }
1339 +                                break;
1340 +                        }
1341 +                        if (j >= mp->nbins)             /* grow modifier size */
1342 +                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1343 +                        copycolor(mp->cbin[j], rgbv);
1344 +                        if (oent->key == NULL)          /* new file entry */
1345 +                                oent->key = strcpy((char *)
1346 +                                                malloc(strlen(oname)+1), oname);
1347 +                        if (oent->data == NULL)
1348 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1349 +                        *(STREAMOUT *)oent->data = sout;
1350 +                }
1351 +        }
1352 +        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1353 + }
1354 +
1355   /* seek on the given output file */
1356   static int
1357   myseeko(const LUENT *e, void *p)
1358   {
1359 <        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
1359 >        STREAMOUT       *sop = (STREAMOUT *)e->data;
1360 >        off_t           nbytes = *(off_t *)p;
1361 >        
1362 >        if (sop->reclen > 1)
1363 >                nbytes = nbytes * sop->reclen;
1364 >        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1365                  sprintf(errmsg, "seek error on file '%s'", e->key);
1366                  error(SYSTEM, errmsg);
1367          }
1368 +        return 0;
1369   }
1370  
1371   /* recover output if possible */
1372   void
1373   recover_output(FILE *fin)
1374   {
1375 <        off_t   lastout = -1;
1376 <        int     outvsiz;
1377 <        char    *outvfmt;
1378 <        int     i, j;
1379 <        MODCONT *mp;
1380 <        int     ofl;
1381 <        char    oname[1024];
1382 <        LUENT   *ment, *oent;
1383 <        FILE    *fp;
1384 <        off_t   nvals;
1375 >        off_t           lastout = -1;
1376 >        int             outvsiz, recsiz;
1377 >        char            *outvfmt;
1378 >        int             i, j;
1379 >        MODCONT         *mp;
1380 >        int             ofl;
1381 >        char            oname[1024];
1382 >        LUENT           *ment, *oent;
1383 >        STREAMOUT       sout;
1384 >        off_t           nvals;
1385 >        int             xr, yr;
1386  
1387          switch (outfmt) {
1388          case 'a':
# Line 993 | Line 1410 | recover_output(FILE *fin)
1410                  mp = (MODCONT *)ment->data;
1411                  if (mp->outspec == NULL)
1412                          error(USER, "cannot recover from stdout");
1413 +                if (mp->outspec[0] == '!')
1414 +                        error(USER, "cannot recover from command");
1415                  for (j = 0; ; j++) {            /* check each bin's file */
1416                          ofl = ofname(oname, mp->outspec, mp->modname, j);
1417                          if (ofl < 0)
1418                                  error(USER, "bad output file specification");
1419                          oent = lu_find(&ofiletab, oname);
1420 <                        if (oent->data != NULL) { /* saw this one already */
1420 >                        if (oent->data != NULL) {
1421 >                                sout = *(STREAMOUT *)oent->data;
1422 >                        } else {
1423 >                                sout.reclen = 0;
1424 >                                sout.outpipe = 0;
1425 >                                sout.ofp = NULL;
1426 >                        }
1427 >                        if (sout.ofp != NULL) { /* already open? */
1428                                  if (ofl & OF_BIN)
1429                                          continue;
1430                                  break;
1431                          }
1006                        if (oname[0] == '!')
1007                                error(USER, "cannot recover from command");
1432                                                  /* open output */
1433 <                        fp = fopen(oname, "rb+");
1434 <                        if (fp == NULL)
1435 <                                break;          /* must be end of modifier */
1436 <                        nvals = lseek(fileno(fp), 0, SEEK_END);
1433 >                        sout.ofp = fopen(oname, "rb+");
1434 >                        if (sout.ofp == NULL) {
1435 >                                if (j)
1436 >                                        break;  /* assume end of modifier */
1437 >                                sprintf(errmsg, "missing recover file '%s'",
1438 >                                                oname);
1439 >                                error(WARNING, errmsg);
1440 >                                break;
1441 >                        }
1442 >                        nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1443                          if (nvals <= 0) {
1444                                  lastout = 0;    /* empty output, quit here */
1445 <                                fclose(fp);
1445 >                                fclose(sout.ofp);
1446                                  break;
1447                          }
1448 <                        lseek(fileno(fp), 0, SEEK_SET);
1449 <                        if (header) {
1020 <                                int     xr, yr;
1021 <                                if (checkheader(fp, outvfmt, NULL) != 1) {
1448 >                        if (!sout.reclen) {
1449 >                                if (!(ofl & OF_BIN)) {
1450                                          sprintf(errmsg,
1451 <                                                "format mismatch for '%s'",
1451 >                                                "need -bn to recover file '%s'",
1452                                                          oname);
1453                                          error(USER, errmsg);
1454                                  }
1455 <                                if (xres > 0 && yres > 0 &&
1456 <                                                (!fscnresolu(&xr, &yr, fp) ||
1457 <                                                        xr != xres ||
1458 <                                                        yr != yres)) {
1459 <                                        sprintf(errmsg,
1460 <                                                "resolution mismatch for '%s'",
1461 <                                                        oname);
1462 <                                        error(USER, errmsg);
1463 <                                }
1455 >                                recsiz = outvsiz;
1456 >                        } else
1457 >                                recsiz = outvsiz * sout.reclen;
1458 >
1459 >                        lseek(fileno(sout.ofp), 0, SEEK_SET);
1460 >                        if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1461 >                                sprintf(errmsg, "format mismatch for '%s'",
1462 >                                                oname);
1463 >                                error(USER, errmsg);
1464                          }
1465 <                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1466 <                        if (lastout < 0 || nvals < lastout)
1465 >                        sout.xr = xres; sout.yr = yres;
1466 >                        if ((sout.xr > 0) & (sout.yr > 0) &&
1467 >                                        (!fscnresolu(&xr, &yr, sout.ofp) ||
1468 >                                                (xr != sout.xr) |
1469 >                                                (yr != sout.yr))) {
1470 >                                sprintf(errmsg, "resolution mismatch for '%s'",
1471 >                                                oname);
1472 >                                error(USER, errmsg);
1473 >                        }
1474 >                        nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz;
1475 >                        if ((lastout < 0) | (nvals < lastout))
1476                                  lastout = nvals;
1477 <                        if (oent->key == NULL) /* new entry */
1477 >                        if (oent->key == NULL)  /* new entry */
1478                                  oent->key = strcpy((char *)
1479                                                  malloc(strlen(oname)+1), oname);
1480 <                        oent->data = (char *)fp;
1480 >                        if (oent->data == NULL)
1481 >                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1482 >                        *(STREAMOUT *)oent->data = sout;
1483                          if (!(ofl & OF_BIN))
1484                                  break;          /* no bin separation */
1485                  }
1486                  if (!lastout) {                 /* empty output */
1487 <                        error(WARNING, "no data to recover");
1487 >                        error(WARNING, "no previous data to recover");
1488                          lu_done(&ofiletab);     /* reclose all outputs */
1489                          return;
1490                  }
1491 <                if (j > mp->nbins) {            /* preallocate modifier bins */
1492 <                        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
1054 <                                                        (j-1)*sizeof(DCOLOR));
1055 <                        if (mp == NULL)
1056 <                                error(SYSTEM, "out of memory in recover_output");
1057 <                        memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1058 <                        mp->nbins = j;
1059 <                        ment->data = (char *)mp;
1060 <                }
1491 >                if (j > mp->nbins)              /* reallocate modifier bins */
1492 >                        ment->data = (char *)(mp = growmodifier(mp, j));
1493          }
1494 +        if (lastout < 0) {
1495 +                error(WARNING, "no output files to recover");
1496 +                return;
1497 +        }
1498 +        if (raysleft && lastout >= raysleft/accumulate) {
1499 +                error(WARNING, "output appears to be complete");
1500 +                /* XXX should read & discard input? */
1501 +                quit(0);
1502 +        }
1503                                                  /* seek on all files */
1504          nvals = lastout * outvsiz;
1505          lu_doall(&ofiletab, myseeko, &nvals);
1506 <                                                /* discard input */
1506 >                                                /* skip repeated input */
1507          for (nvals = 0; nvals < lastout; nvals++)
1508 <                if (getinp(oname, fin) <= 0)
1508 >                if (getinp(oname, fin) < 0)
1509                          error(USER, "unexpected EOF on input");
1510 <        lastray = lastdone = (unsigned long)lastout;
1510 >        lastray = lastdone = (unsigned long)lastout * accumulate;
1511          if (raysleft)
1512                  raysleft -= lastray;
1513   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines