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.7 by greg, Tue May 31 03:48:59 2005 UTC vs.
Revision 1.54 by greg, Sun Jun 14 18:21:58 2009 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 */
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 < FILE *getofile(const char *ospec, const char *mname, int bn);
75 > LUTAB   ofiletab = LU_SINIT(free,closestream);  /* output file table */
76  
77 + #define OF_MODIFIER     01
78 + #define OF_BIN          02
79 +
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
87   * rtrace child process.  Input is passed unchanged from stdin,
# Line 67 | Line 98 | struct rtproc {
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 90 | 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 < long            raysleft;               /* number of rays left to trace */
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  
144   unsigned long   lastray = 0;            /* last ray number sent */
# Line 104 | 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 trace_contribs(FILE *fp);
160 > void reload_output(void);
161 > void recover_output(FILE *fin);
162 > void trace_contribs(FILE *fin);
163   struct rtproc *wait_rproc(void);
164   struct rtproc *get_rproc(void);
165   void queue_raytree(struct rtproc *rtp);
166   void process_queue(void);
167  
168 < void putcontrib(const DCOLOR cnt, FILE *fout);
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)
187   {
188          switch (fmt[0]) {
126        case 'a':
189          case 'f':
190          case 'd':
191 +                SET_FILE_BINARY(stdin);
192 +                /* fall through */
193 +        case 'a':
194                  inpfmt = fmt[0];
195                  break;
196          default:
# Line 155 | Line 220 | fmterr:
220   int
221   main(int argc, char *argv[])
222   {
223 +        int     contrib = 0;
224          int     nprocs = 1;
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 <                                /* set up calcomp mode */
239 >                                /* initialize calcomp routines */
240          esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
241          esupport &= ~(E_OUTCHAN);
242 +        varset("PI", ':', PI);
243                                  /* get our options */
244          for (i = 1; i < argc-1; i++) {
245                                                  /* expand arguments */
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]) {
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 200 | 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 <                                        if (i >= argc-1) break;
306 <                                        fcompile(argv[++i]);
305 >                                        char    *fpath;
306 >                                        if (i >= argc-2) break;
307 >                                        fpath = getpath(argv[++i],
308 >                                                        getrlibpath(), R_OK);
309 >                                        if (fpath == NULL) {
310 >                                                sprintf(errmsg,
311 >                                                        "cannot find file '%s'",
312 >                                                                argv[i]);
313 >                                                error(USER, errmsg);
314 >                                        }
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];
382                  char    *rtpath;
383 <                printf("-n  %-2d\t\t\t\t# number of processes\n", nprocs);
383 >                printf("-n %-2d\t\t\t\t# number of processes\n", nprocs);
384 >                printf("-c %-5d\t\t\t# accumulated rays per record\n",
385 >                                accumulate);
386 >                printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
387 >                                contrib ? "contributions" : "coefficients");
388                  fflush(stdout);                 /* report OUR options */
389                  rtargv[rtargc++] = header ? "-h+" : "-h-";
390                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
# Line 257 | Line 395 | main(int argc, char *argv[])
395                  rtargv[rtargc++] = "-y";
396                  sprintf(syres, "%d", yres);
397                  rtargv[rtargc++] = syres;
260                rtargv[rtargc++] = "-oTW";
398                  rtargv[rtargc++] = "-defaults";
399                  rtargv[rtargc] = NULL;
400                  rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
# Line 271 | Line 408 | main(int argc, char *argv[])
408                  exit(1);
409          }
410          if (nprocs > 1) {       /* add persist file if parallel */
411 <                rtargv[rtargc++] = "-PP";
412 <                rtargv[rtargc++] = mktemp(persistfn);
411 >                if (persist_state == PERSIST_SINGLE)
412 >                        error(USER, "use -PP option for multiple processes");
413 >                if (persist_state == PERSIST_NONE) {
414 >                        rtargv[rtargc++] = "-PP";
415 >                        rtargv[rtargc++] = mktemp(persistfn);
416 >                        persist_state = PERSIST_OURS;
417 >                }
418          }
419                                  /* add format string */
420          sprintf(fmt, "-f%cf", inpfmt);
# Line 282 | Line 424 | main(int argc, char *argv[])
424                  error(USER, "missing octree argument");
425          rtargv[rtargc++] = octree = argv[i];
426          rtargv[rtargc] = NULL;
427 <                                /* start rtrace & compute contributions */
427 >                                /* start rtrace & recover if requested */
428          init(nprocs);
429 +                                /* compute contributions */
430          trace_contribs(stdin);
431 +                                /* clean up */
432          quit(0);
433   }
434  
435 + #ifndef SIGALRM
436 + #define SIGALRM SIGTERM
437 + #endif
438   /* kill persistent rtrace process */
439   static void
440   killpersist(void)
441   {
442          FILE    *fp = fopen(persistfn, "r");
443 <        int     pid;
443 >        RT_PID  pid;
444  
445          if (fp == NULL)
446                  return;
# Line 327 | Line 474 | quit(int status)
474   {
475          int     rtstat;
476  
477 <        if (rt0.next != NULL)           /* terminate persistent rtrace */
477 >        if (persist_state == PERSIST_OURS)  /* terminate waiting rtrace */
478                  killpersist();
479                                          /* clean up rtrace process(es) */
480          rtstat = done_rprocs(&rt0);
# Line 389 | Line 536 | init(int np)
536          rtp->next = NULL;               /* terminate list */
537          if (yres > 0) {
538                  if (xres > 0)
539 <                        raysleft = xres*yres;
539 >                        raysleft = (unsigned long)xres*yres;
540                  else
541                          raysleft = yres;
542          } else
543                  raysleft = 0;
544 +        if ((account = accumulate) > 0)
545 +                raysleft *= accumulate;
546          waitflush = xres;
547 +        if (!recover)
548 +                return;
549 +                                        /* recover previous values */
550 +        if (accumulate <= 0)
551 +                reload_output();
552 +        else
553 +                recover_output(stdin);
554   }
555  
556 + /* grow modifier to accommodate more bins */
557 + MODCONT *
558 + growmodifier(MODCONT *mp, int nb)
559 + {
560 +        if (nb <= mp->nbins)
561 +                return mp;
562 +        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1));
563 +        if (mp == NULL)
564 +                error(SYSTEM, "out of memory in growmodifier");
565 +        memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins));
566 +        mp->nbins = nb;
567 +        return mp;
568 + }
569 +
570   /* add modifier to our list to track */
571   MODCONT *
572 < addmodifier(char *modn, char *outf, char *binv)
572 > addmodifier(char *modn, char *outf, char *binv, int bincnt)
573   {
574          LUENT   *lep = lu_find(&modconttab, modn);
575          MODCONT *mp;
576 +        int     i;
577          
578          if (lep->data != NULL) {
579                  sprintf(errmsg, "duplicate modifier '%s'", modn);
580                  error(USER, errmsg);
581          }
582          if (nmods >= MAXMODLIST)
583 <                error(USER, "too many modifiers");
583 >                error(INTERNAL, "too many modifiers");
584          modname[nmods++] = modn;        /* XXX assumes static string */
585          lep->key = modn;                /* XXX assumes static string */
586          mp = (MODCONT *)malloc(sizeof(MODCONT));
587          if (mp == NULL)
588                  error(SYSTEM, "out of memory in addmodifier");
418        lep->data = (char *)mp;
589          mp->outspec = outf;             /* XXX assumes static string */
590          mp->modname = modn;             /* XXX assumes static string */
591 <        if (binv != NULL)
592 <                mp->binv = eparse(binv);
593 <        else
594 <                mp->binv = eparse("0");
595 <        mp->nbins = 1;
591 >        if (binv == NULL)
592 >                binv = "0";             /* use single bin if unspecified */
593 >        mp->binv = eparse(binv);
594 >        if (mp->binv->type == NUM) {    /* check value if constant */
595 >                bincnt = (int)(evalue(mp->binv) + 1.5);
596 >                if (bincnt != 1) {
597 >                        sprintf(errmsg, "illegal non-zero constant for bin (%s)",
598 >                                        binv);
599 >                        error(USER, errmsg);
600 >                }
601 >        }
602 >        mp->nbins = 1;                  /* initialize results holder */
603          setcolor(mp->cbin[0], 0., 0., 0.);
604 +        if (bincnt > 1)
605 +                mp = growmodifier(mp, bincnt);
606 +        lep->data = (char *)mp;
607 +                                        /* allocate output streams */
608 +        for (i = bincnt; i-- > 0; )
609 +                getostream(mp->outspec, mp->modname, i, 1);
610          return mp;
611   }
612  
613 + /* add modifiers from a file list */
614 + void
615 + addmodfile(char *fname, char *outf, char *binv, int bincnt)
616 + {
617 +        char    *mname[MAXMODLIST];
618 +        int     i;
619 +                                        /* find the file & store strings */
620 +        if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
621 +                sprintf(errmsg, "cannot find modifier file '%s'", fname);
622 +                error(SYSTEM, errmsg);
623 +        }
624 +        for (i = 0; mname[i]; i++)      /* add each one */
625 +                addmodifier(mname[i], outf, binv, bincnt);
626 + }
627 +
628   /* put string to stderr */
629   void
630   eputs(char  *s)
# Line 442 | Line 640 | eputs(char  *s)
640          midline = s[strlen(s)-1] != '\n';
641   }
642  
643 + /* construct output file name and return flags whether modifier/bin present */
644 + int
645 + ofname(char *oname, const char *ospec, const char *mname, int bn)
646 + {
647 +        const char      *mnp = NULL;
648 +        const char      *bnp = NULL;
649 +        const char      *cp;
650 +        
651 +        if (ospec == NULL)
652 +                return -1;
653 +        for (cp = ospec; *cp; cp++)             /* check format position(s) */
654 +                if (*cp == '%') {
655 +                        do
656 +                                ++cp;
657 +                        while (isdigit(*cp));
658 +                        switch (*cp) {
659 +                        case '%':
660 +                                break;
661 +                        case 's':
662 +                                if (mnp != NULL)
663 +                                        return -1;
664 +                                mnp = cp;
665 +                                break;
666 +                        case 'd':
667 +                                if (bnp != NULL)
668 +                                        return -1;
669 +                                bnp = cp;
670 +                                break;
671 +                        default:
672 +                                return -1;
673 +                        }
674 +                }
675 +        if (mnp != NULL) {                      /* create file name */
676 +                if (bnp != NULL) {
677 +                        if (bnp > mnp)
678 +                                sprintf(oname, ospec, mname, bn);
679 +                        else
680 +                                sprintf(oname, ospec, bn, mname);
681 +                        return OF_MODIFIER|OF_BIN;
682 +                }
683 +                sprintf(oname, ospec, mname);
684 +                return OF_MODIFIER;
685 +        }
686 +        if (bnp != NULL) {
687 +                sprintf(oname, ospec, bn);
688 +                return OF_BIN;
689 +        }
690 +        strcpy(oname, ospec);
691 +        return 0;
692 + }
693 +
694   /* write header to the given output stream */
695   void
696 < printheader(FILE *fout)
696 > printheader(FILE *fout, const char *info)
697   {
698          extern char     VersionID[];
699          FILE            *fin = fopen(octree, "r");
# Line 456 | Line 705 | printheader(FILE *fout)
705          printargs(gargc-1, gargv, fout);        /* add our command */
706          fprintf(fout, "SOFTWARE= %s\n", VersionID);
707          fputnow(fout);
708 +        if (info != NULL)                       /* add extra info if given */
709 +                fputs(info, fout);
710          switch (outfmt) {                       /* add output format */
711          case 'a':
712                  fputformat("ascii", fout);
# Line 471 | Line 722 | printheader(FILE *fout)
722                  break;
723          }
724          fputc('\n', fout);
725 <        if (xres > 0) {
726 <                if (yres > 0)                   /* resolution string */
727 <                        fprtresolu(xres, yres, fout);
725 > }
726 >
727 > /* write resolution string to given output stream */
728 > void
729 > printresolu(FILE *fout, int xr, int yr)
730 > {
731 >        if ((xr > 0) & (yr > 0))        /* resolution string */
732 >                fprtresolu(xr, yr, fout);
733 >        if (xres > 0)                   /* global flush flag */
734                  fflush(fout);
478        }
735   }
736  
737 < /* Get output file pointer (open and write header if new) */
738 < FILE *
739 < getofile(const char *ospec, const char *mname, int bn)
737 > /* Get output stream pointer (open and write header if new and noopen==0) */
738 > STREAMOUT *
739 > getostream(const char *ospec, const char *mname, int bn, int noopen)
740   {
741 <        const char      *mnp = NULL;
742 <        const char      *bnp = NULL;
743 <        const char      *cp;
744 <        char            ofname[1024];
745 <        LUENT           *lep;
741 >        static const DCOLOR     nocontrib = BLKCOLOR;
742 >        static STREAMOUT        stdos;
743 >        int                     ofl;
744 >        char                    oname[1024];
745 >        LUENT                   *lep;
746 >        STREAMOUT               *sop;
747          
748          if (ospec == NULL) {                    /* use stdout? */
749 <                if (!using_stdout && header)
750 <                        printheader(stdout);
751 <                using_stdout = 1;
752 <                return stdout;
749 >                if (!noopen && !using_stdout) {
750 >                        if (outfmt != 'a')
751 >                                SET_FILE_BINARY(stdout);
752 >                        if (header)
753 >                                printheader(stdout, NULL);
754 >                        printresolu(stdout, xres, yres);
755 >                        stdos.xr = xres; stdos.yr = yres;
756 >                        using_stdout = 1;
757 >                }
758 >                stdos.ofp = stdout;
759 >                stdos.reclen += noopen;
760 >                return &stdos;
761          }
762 <        for (cp = ospec; *cp; cp++)             /* check format position(s) */
763 <                if (*cp == '%') {
764 <                        do
765 <                                ++cp;
766 <                        while (isdigit(*cp));
767 <                        switch (*cp) {
768 <                        case '%':
769 <                                break;
770 <                        case 's':
771 <                                if (mnp != NULL)
772 <                                        goto badspec;
773 <                                mnp = cp;
774 <                                break;
775 <                        case 'd':
776 <                                if (bnp != NULL)
777 <                                        goto badspec;
778 <                                bnp = cp;
779 <                                break;
780 <                        default:
781 <                                goto badspec;
762 >        ofl = ofname(oname, ospec, mname, bn);  /* get output name */
763 >        if (ofl < 0) {
764 >                sprintf(errmsg, "bad output format '%s'", ospec);
765 >                error(USER, errmsg);
766 >        }
767 >        lep = lu_find(&ofiletab, oname);        /* look it up */
768 >        if (lep->key == NULL)                   /* new entry */
769 >                lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
770 >        sop = (STREAMOUT *)lep->data;
771 >        if (sop == NULL) {                      /* allocate stream */
772 >                sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
773 >                if (sop == NULL)
774 >                        error(SYSTEM, "out of memory in getostream");
775 >                sop->outpipe = oname[0] == '!';
776 >                sop->reclen = 0;
777 >                sop->ofp = NULL;                /* open iff noopen==0 */
778 >                sop->xr = xres; sop->yr = yres;
779 >                lep->data = (char *)sop;
780 >                if (!sop->outpipe & !force_open & !recover &&
781 >                                access(oname, F_OK) == 0) {
782 >                        errno = EEXIST;         /* file exists */
783 >                        goto openerr;
784 >                }
785 >        }
786 >        if (!noopen && sop->ofp == NULL) {      /* open output stream */
787 >                long            i;
788 >                if (oname[0] == '!')            /* output to command */
789 >                        sop->ofp = popen(oname+1, "w");
790 >                else                            /* else open file */
791 >                        sop->ofp = fopen(oname, "w");
792 >                if (sop->ofp == NULL)
793 >                        goto openerr;
794 >                if (outfmt != 'a')
795 >                        SET_FILE_BINARY(sop->ofp);
796 >                if (header) {
797 >                        char    info[512];
798 >                        char    *cp = info;
799 >                        if (ofl & OF_MODIFIER || sop->reclen == 1) {
800 >                                sprintf(cp, "MODIFIER=%s\n", mname);
801 >                                while (*cp) ++cp;
802                          }
803 +                        if (ofl & OF_BIN) {
804 +                                sprintf(cp, "BIN=%d\n", bn);
805 +                                while (*cp) ++cp;
806 +                        }
807 +                        *cp = '\0';
808 +                        printheader(sop->ofp, info);
809                  }
810 <        if (mnp != NULL) {                      /* create file name */
811 <                if (bnp != NULL) {
521 <                        if (bnp > mnp)
522 <                                sprintf(ofname, ospec, mname, bn);
523 <                        else
524 <                                sprintf(ofname, ospec, bn, mname);
525 <                } else
526 <                        sprintf(ofname, ospec, mname);
527 <        } else if (bnp != NULL)
528 <                sprintf(ofname, ospec, bn);
529 <        else
530 <                strcpy(ofname, ospec);
531 <        lep = lu_find(&ofiletab, ofname);       /* look it up */
532 <        if (lep->key == NULL)                   /* new entry */
533 <                lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
534 <        if (lep->data == NULL) {                /* open output file */
535 <                FILE            *fp = fopen(ofname, "w");
536 <                int             i;
537 <                if (fp == NULL) {
538 <                        sprintf(errmsg, "cannot open '%s' for writing", ofname);
539 <                        error(SYSTEM, errmsg);
810 >                if (accumulate > 0) {           /* global resolution */
811 >                        sop->xr = xres; sop->yr = yres;
812                  }
813 <                if (header)
542 <                        printheader(fp);
813 >                printresolu(sop->ofp, sop->xr, sop->yr);
814                                                  /* play catch-up */
815 <                for (i = 0; i < lastdone; i++) {
816 <                        static const DCOLOR     nocontrib = BLKCOLOR;
817 <                        putcontrib(nocontrib, fp);
815 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
816 >                        int     j = sop->reclen;
817 >                        if (j <= 0) j = 1;
818 >                        while (j--)
819 >                                put_contrib(nocontrib, sop->ofp);
820                          if (outfmt == 'a')
821 <                                putc('\n', fp);
821 >                                putc('\n', sop->ofp);
822                  }
823                  if (xres > 0)
824 <                        fflush(fp);
552 <                lep->data = (char *)fp;
824 >                        fflush(sop->ofp);
825          }
826 <        return (FILE *)lep->data;               /* return open file pointer */
827 < badspec:
828 <        sprintf(errmsg, "bad output format '%s'", ospec);
829 <        error(USER, errmsg);
830 <        return NULL;            /* pro forma return */
826 >        sop->reclen += noopen;                  /* add to length if noopen */
827 >        return sop;                             /* return output stream */
828 > openerr:
829 >        sprintf(errmsg, "cannot open '%s' for writing", oname);
830 >        error(SYSTEM, errmsg);
831 >        return NULL;    /* pro forma return */
832   }
833  
834   /* read input ray into buffer */
835   int
836   getinp(char *buf, FILE *fp)
837   {
838 +        double  dv[3], *dvp;
839 +        float   *fvp;
840          char    *cp;
841          int     i;
842  
# Line 570 | Line 845 | getinp(char *buf, FILE *fp)
845                  cp = buf;               /* make sure we get 6 floats */
846                  for (i = 0; i < 6; i++) {
847                          if (fgetword(cp, buf+127-cp, fp) == NULL)
848 <                                return 0;
848 >                                return -1;
849 >                        if (i >= 3)
850 >                                dv[i-3] = atof(cp);
851                          if ((cp = fskip(cp)) == NULL || *cp)
852 <                                return 0;
852 >                                return -1;
853                          *cp++ = ' ';
854                  }
855                  getc(fp);               /* get/put eol */
856                  *cp-- = '\0'; *cp = '\n';
857 +                if (DOT(dv,dv) <= FTINY*FTINY)
858 +                        return 0;       /* dummy ray */
859                  return strlen(buf);
860          case 'f':
861                  if (fread(buf, sizeof(float), 6, fp) < 6)
862 <                        return 0;
862 >                        return -1;
863 >                fvp = (float *)buf + 3;
864 >                if (DOT(fvp,fvp) <= FTINY*FTINY)
865 >                        return 0;       /* dummy ray */
866                  return sizeof(float)*6;
867          case 'd':
868                  if (fread(buf, sizeof(double), 6, fp) < 6)
869 <                        return 0;
869 >                        return -1;
870 >                dvp = (double *)buf + 3;
871 >                if (DOT(dvp,dvp) <= FTINY*FTINY)
872 >                        return 0;       /* dummy ray */
873                  return sizeof(double)*6;
874          }
875          error(INTERNAL, "botched input format");
876 <        return 0;       /* pro forma return */
876 >        return -1;      /* pro forma return */
877   }
878  
879   static float    rparams[9];             /* traced ray parameters */
# Line 618 | Line 903 | add_contrib(const char *modn)
903          bn = (int)(evalue(mp->binv) + .5);
904          if (bn <= 0)
905                  bn = 0;
906 <        else if (bn > mp->nbins) {      /* new bin */
907 <                mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
623 <                                                bn*sizeof(DCOLOR));
624 <                if (mp == NULL)
625 <                        error(SYSTEM, "out of memory in add_contrib");
626 <                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
627 <                mp->nbins = bn+1;
628 <                le->data = (char *)mp;
629 <        }
906 >        else if (bn >= mp->nbins)       /* new bin */
907 >                le->data = (char *)(mp = growmodifier(mp, bn+1));
908          addcolor(mp->cbin[bn], rparams);
909   }
910  
# Line 634 | Line 912 | add_contrib(const char *modn)
912   static int
913   puteol(const LUENT *e, void *p)
914   {
915 <        FILE    *fp = (FILE *)e->data;
915 >        STREAMOUT       *sop = (STREAMOUT *)e->data;
916  
917          if (outfmt == 'a')
918 <                putc('\n', fp);
918 >                putc('\n', sop->ofp);
919          if (!waitflush)
920 <                fflush(fp);
921 <        if (ferror(fp)) {
920 >                fflush(sop->ofp);
921 >        if (ferror(sop->ofp)) {
922                  sprintf(errmsg, "write error on file '%s'", e->key);
923                  error(SYSTEM, errmsg);
924          }
# Line 649 | Line 927 | puteol(const LUENT *e, void *p)
927  
928   /* put out ray contribution to file */
929   void
930 < putcontrib(const DCOLOR cnt, FILE *fout)
930 > put_contrib(const DCOLOR cnt, FILE *fout)
931   {
932          float   fv[3];
933          COLR    cv;
# Line 659 | Line 937 | putcontrib(const DCOLOR cnt, FILE *fout)
937                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
938                  break;
939          case 'f':
940 <                fv[0] = cnt[0];
663 <                fv[1] = cnt[1];
664 <                fv[2] = cnt[2];
940 >                copycolor(fv, cnt);
941                  fwrite(fv, sizeof(float), 3, fout);
942                  break;
943          case 'd':
# Line 676 | Line 952 | putcontrib(const DCOLOR cnt, FILE *fout)
952          }
953   }
954  
955 < /* output ray tallies and clear for next primary */
955 > /* output ray tallies and clear for next accumulation */
956   void
957 < done_contrib(void)
957 > done_contrib(int navg)
958   {
959 <        int     i, j;
960 <        MODCONT *mp;
959 >        double          sf = 1.;
960 >        int             i, j;
961 >        MODCONT         *mp;
962 >        STREAMOUT       *sop;
963 >                                                /* set average scaling */
964 >        if (navg > 1)
965 >                sf = 1. / (double)navg;
966                                                  /* output modifiers in order */
967          for (i = 0; i < nmods; i++) {
968                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
969 <                for (j = 0; j < mp->nbins; j++)
970 <                        putcontrib(mp->cbin[j],
971 <                                        getofile(mp->outspec, mp->modname, j));
972 <                                                /* clear for next ray tree */
969 >                if (navg > 1)                   /* average scaling */
970 >                        for (j = mp->nbins; j--; )
971 >                                scalecolor(mp->cbin[j], sf);
972 >                sop = getostream(mp->outspec, mp->modname, 0,0);
973 >                put_contrib(mp->cbin[0], sop->ofp);
974 >                if (mp->nbins > 3 &&            /* minor optimization */
975 >                                sop == getostream(mp->outspec, mp->modname, 1,0))
976 >                        for (j = 1; j < mp->nbins; j++)
977 >                                put_contrib(mp->cbin[j], sop->ofp);
978 >                else
979 >                        for (j = 1; j < mp->nbins; j++)
980 >                                put_contrib(mp->cbin[j],
981 >                                    getostream(mp->outspec,mp->modname,j,0)->ofp);
982 >                                                /* clear for next time */
983                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
984          }
985          --waitflush;                            /* terminate records */
# Line 747 | Line 1038 | process_queue(void)
1038                          if (!n || !(isalpha(*cp) | (*cp == '_')))
1039                                  error(USER, "bad modifier name from rtrace");
1040                                          /* get modifier name */
1041 <                        while (n > 0 && *cp != '\t') {
1041 >                        while (n > 1 && *cp != '\t') {
1042 >                                if (mnp - modname >= sizeof(modname)-2)
1043 >                                        error(INTERNAL, "modifier name too long");
1044                                  *mnp++ = *cp++; n--;
1045                          }
1046                          *mnp = '\0';
# Line 759 | Line 1052 | process_queue(void)
1052                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1053                          add_contrib(modname);
1054                  }
1055 <                done_contrib();         /* sum up contributions & output */
1055 >                                        /* time to produce record? */
1056 >                if (account > 0 && !--account)
1057 >                        done_contrib(account = accumulate);
1058                  lastdone = rtp->raynum;
1059 <                free(rtp->buf);         /* free up buffer space */
1059 >                if (rtp->buf != NULL)   /* free up buffer space */
1060 >                        free(rtp->buf);
1061                  rt_unproc = rtp->next;
1062                  free(rtp);              /* done with this ray tree */
1063          }
# Line 809 | Line 1105 | wait_rproc(void)
1105                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
1106                                          rt->bsiz = treebufsiz;
1107                                  else
1108 <                                        rt->bsiz = treebufsiz += BUFSIZ;
1108 >                                        treebufsiz = rt->bsiz += BUFSIZ;
1109                                  rt->buf = (char *)realloc(rt->buf, rt->bsiz);
1110                          }
1111                          if (rt->buf == NULL)
# Line 845 | Line 1141 | get_rproc(void)
1141   void
1142   trace_contribs(FILE *fin)
1143   {
1144 +        static int      ignore_warning_given = 0;
1145          char            inpbuf[128];
1146          int             iblen;
1147          struct rtproc   *rtp;
1148                                                  /* loop over input */
1149 <        while ((iblen = getinp(inpbuf, fin)) > 0) {
1150 <                if (lastray+1 < lastray) {      /* counter rollover? */
1149 >        while ((iblen = getinp(inpbuf, fin)) >= 0) {
1150 >                if (!iblen && accumulate != 1) {
1151 >                        if (!ignore_warning_given++)
1152 >                                error(WARNING,
1153 >                                "dummy ray(s) ignored during accumulation\n");
1154 >                        continue;
1155 >                }
1156 >                if (!iblen ||                   /* need flush/reset? */
1157 >                                queue_length() > 10*nrtprocs() ||
1158 >                                lastray+1 < lastray) {
1159                          while (wait_rproc() != NULL)
1160                                  process_queue();
1161 <                        lastdone = lastray = 0;
1161 >                        if (lastray+1 < lastray)
1162 >                                lastdone = lastray = 0;
1163                  }
1164                  rtp = get_rproc();              /* get avail. rtrace process */
1165 <                rtp->raynum = ++lastray;        /* assign ray to it */
1166 <                writebuf(rtp->pd.w, inpbuf, iblen);
1167 <                if (!--raysleft)
1168 <                        break;
1165 >                rtp->raynum = ++lastray;        /* assign ray */
1166 >                if (iblen) {                    /* trace ray if valid */
1167 >                        writebuf(rtp->pd.w, inpbuf, iblen);
1168 >                } else {                        /* else bypass dummy ray */
1169 >                        queue_raytree(rtp);     /* queue empty ray/record */
1170 >                        if ((yres <= 0) | (xres <= 0))
1171 >                                waitflush = 1;  /* flush right after */
1172 >                }
1173                  process_queue();                /* catch up with results */
1174 +                if (raysleft && !--raysleft)
1175 +                        break;                  /* preemptive EOI */
1176          }
1177          while (wait_rproc() != NULL)            /* process outstanding rays */
1178                  process_queue();
1179 <        if (raysleft > 0)
1179 >        if (accumulate <= 0)
1180 >                done_contrib(0);                /* output tallies */
1181 >        else if (account < accumulate) {
1182 >                error(WARNING, "partial accumulation in final record");
1183 >                done_contrib(accumulate - account);
1184 >        }
1185 >        if (raysleft)
1186                  error(USER, "unexpected EOF on input");
1187 +        lu_done(&ofiletab);                     /* close output files */
1188 + }
1189 +
1190 + /* get ray contribution from previous file */
1191 + static int
1192 + get_contrib(DCOLOR cnt, FILE *finp)
1193 + {
1194 +        COLOR   fv;
1195 +        COLR    cv;
1196 +
1197 +        switch (outfmt) {
1198 +        case 'a':
1199 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1200 +        case 'f':
1201 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1202 +                        return(0);
1203 +                copycolor(cnt, fv);
1204 +                return(1);
1205 +        case 'd':
1206 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1207 +        case 'c':
1208 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1209 +                        return(0);
1210 +                colr_color(fv, cv);
1211 +                copycolor(cnt, fv);
1212 +                return(1);
1213 +        default:
1214 +                error(INTERNAL, "botched output format");
1215 +        }
1216 +        return(0);      /* pro forma return */
1217 + }
1218 +
1219 + /* close output file opened for input */
1220 + static int
1221 + myclose(const LUENT *e, void *p)
1222 + {
1223 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1224 +        
1225 +        if (sop->ofp == NULL)
1226 +                return(0);
1227 +        fclose(sop->ofp);
1228 +        sop->ofp = NULL;
1229 +        return(0);
1230 + }
1231 +
1232 + /* load previously accumulated values */
1233 + void
1234 + reload_output(void)
1235 + {
1236 +        int             i, j;
1237 +        MODCONT         *mp;
1238 +        int             ofl;
1239 +        char            oname[1024];
1240 +        char            *fmode = "rb";
1241 +        char            *outvfmt;
1242 +        LUENT           *ment, *oent;
1243 +        int             xr, yr;
1244 +        STREAMOUT       sout;
1245 +        DCOLOR          rgbv;
1246 +
1247 +        switch (outfmt) {
1248 +        case 'a':
1249 +                outvfmt = "ascii";
1250 +                fmode = "r";
1251 +                break;
1252 +        case 'f':
1253 +                outvfmt = "float";
1254 +                break;
1255 +        case 'd':
1256 +                outvfmt = "double";
1257 +                break;
1258 +        case 'c':
1259 +                outvfmt = COLRFMT;
1260 +                break;
1261 +        default:
1262 +                error(INTERNAL, "botched output format");
1263 +                return;
1264 +        }
1265 +                                                /* reload modifier values */
1266 +        for (i = 0; i < nmods; i++) {
1267 +                ment = lu_find(&modconttab,modname[i]);
1268 +                mp = (MODCONT *)ment->data;
1269 +                if (mp->outspec == NULL)
1270 +                        error(USER, "cannot reload from stdout");
1271 +                if (mp->outspec[0] == '!')
1272 +                        error(USER, "cannot reload from command");
1273 +                for (j = 0; ; j++) {            /* load each modifier bin */
1274 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1275 +                        if (ofl < 0)
1276 +                                error(USER, "bad output file specification");
1277 +                        oent = lu_find(&ofiletab, oname);
1278 +                        if (oent->data != NULL) {
1279 +                                sout = *(STREAMOUT *)oent->data;
1280 +                        } else {
1281 +                                sout.reclen = 0;
1282 +                                sout.outpipe = 0;
1283 +                                sout.xr = xres; sout.yr = yres;
1284 +                                sout.ofp = NULL;
1285 +                        }
1286 +                        if (sout.ofp == NULL) { /* open output as input */
1287 +                                sout.ofp = fopen(oname, fmode);
1288 +                                if (sout.ofp == NULL) {
1289 +                                        if (j)
1290 +                                                break;  /* assume end of modifier */
1291 +                                        sprintf(errmsg, "missing reload file '%s'",
1292 +                                                        oname);
1293 +                                        error(WARNING, errmsg);
1294 +                                        break;
1295 +                                }
1296 +                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1297 +                                        sprintf(errmsg, "format mismatch for '%s'",
1298 +                                                        oname);
1299 +                                        error(USER, errmsg);
1300 +                                }
1301 +                                if ((sout.xr > 0) & (sout.yr > 0) &&
1302 +                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1303 +                                                        (xr != sout.xr) |
1304 +                                                        (yr != sout.yr))) {
1305 +                                        sprintf(errmsg, "resolution mismatch for '%s'",
1306 +                                                        oname);
1307 +                                        error(USER, errmsg);
1308 +                                }
1309 +                        }
1310 +                                                        /* read in RGB value */
1311 +                        if (!get_contrib(rgbv, sout.ofp)) {
1312 +                                if (!j) {
1313 +                                        fclose(sout.ofp);
1314 +                                        break;          /* ignore empty file */
1315 +                                }
1316 +                                if (j < mp->nbins) {
1317 +                                        sprintf(errmsg, "missing data in '%s'",
1318 +                                                        oname);
1319 +                                        error(USER, errmsg);
1320 +                                }
1321 +                                break;
1322 +                        }
1323 +                        if (j >= mp->nbins)             /* grow modifier size */
1324 +                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1325 +                        copycolor(mp->cbin[j], rgbv);
1326 +                        if (oent->key == NULL)          /* new file entry */
1327 +                                oent->key = strcpy((char *)
1328 +                                                malloc(strlen(oname)+1), oname);
1329 +                        if (oent->data == NULL)
1330 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1331 +                        *(STREAMOUT *)oent->data = sout;
1332 +                }
1333 +        }
1334 +        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1335 + }
1336 +
1337 + /* seek on the given output file */
1338 + static int
1339 + myseeko(const LUENT *e, void *p)
1340 + {
1341 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1342 +        off_t           nbytes = *(off_t *)p;
1343 +        
1344 +        if (sop->reclen > 1)
1345 +                nbytes = nbytes * sop->reclen;
1346 +        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1347 +                sprintf(errmsg, "seek error on file '%s'", e->key);
1348 +                error(SYSTEM, errmsg);
1349 +        }
1350 +        return 0;
1351 + }
1352 +
1353 + /* recover output if possible */
1354 + void
1355 + recover_output(FILE *fin)
1356 + {
1357 +        off_t           lastout = -1;
1358 +        int             outvsiz, recsiz;
1359 +        char            *outvfmt;
1360 +        int             i, j;
1361 +        MODCONT         *mp;
1362 +        int             ofl;
1363 +        char            oname[1024];
1364 +        LUENT           *ment, *oent;
1365 +        STREAMOUT       sout;
1366 +        off_t           nvals;
1367 +        int             xr, yr;
1368 +
1369 +        switch (outfmt) {
1370 +        case 'a':
1371 +                error(USER, "cannot recover ASCII output");
1372 +                return;
1373 +        case 'f':
1374 +                outvsiz = sizeof(float)*3;
1375 +                outvfmt = "float";
1376 +                break;
1377 +        case 'd':
1378 +                outvsiz = sizeof(double)*3;
1379 +                outvfmt = "double";
1380 +                break;
1381 +        case 'c':
1382 +                outvsiz = sizeof(COLR);
1383 +                outvfmt = COLRFMT;
1384 +                break;
1385 +        default:
1386 +                error(INTERNAL, "botched output format");
1387 +                return;
1388 +        }
1389 +                                                /* check modifier outputs */
1390 +        for (i = 0; i < nmods; i++) {
1391 +                ment = lu_find(&modconttab,modname[i]);
1392 +                mp = (MODCONT *)ment->data;
1393 +                if (mp->outspec == NULL)
1394 +                        error(USER, "cannot recover from stdout");
1395 +                if (mp->outspec[0] == '!')
1396 +                        error(USER, "cannot recover from command");
1397 +                for (j = 0; ; j++) {            /* check each bin's file */
1398 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1399 +                        if (ofl < 0)
1400 +                                error(USER, "bad output file specification");
1401 +                        oent = lu_find(&ofiletab, oname);
1402 +                        if (oent->data != NULL) {
1403 +                                sout = *(STREAMOUT *)oent->data;
1404 +                        } else {
1405 +                                sout.reclen = 0;
1406 +                                sout.outpipe = 0;
1407 +                                sout.ofp = NULL;
1408 +                        }
1409 +                        if (sout.ofp != NULL) { /* already open? */
1410 +                                if (ofl & OF_BIN)
1411 +                                        continue;
1412 +                                break;
1413 +                        }
1414 +                                                /* open output */
1415 +                        sout.ofp = fopen(oname, "rb+");
1416 +                        if (sout.ofp == NULL) {
1417 +                                if (j)
1418 +                                        break;  /* assume end of modifier */
1419 +                                sprintf(errmsg, "missing recover file '%s'",
1420 +                                                oname);
1421 +                                error(WARNING, errmsg);
1422 +                                break;
1423 +                        }
1424 +                        nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1425 +                        if (nvals <= 0) {
1426 +                                lastout = 0;    /* empty output, quit here */
1427 +                                fclose(sout.ofp);
1428 +                                break;
1429 +                        }
1430 +                        if (!sout.reclen) {
1431 +                                if (!(ofl & OF_BIN)) {
1432 +                                        sprintf(errmsg,
1433 +                                                "need -bn to recover file '%s'",
1434 +                                                        oname);
1435 +                                        error(USER, errmsg);
1436 +                                }
1437 +                                recsiz = outvsiz;
1438 +                        } else
1439 +                                recsiz = outvsiz * sout.reclen;
1440 +
1441 +                        lseek(fileno(sout.ofp), 0, SEEK_SET);
1442 +                        if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1443 +                                sprintf(errmsg, "format mismatch for '%s'",
1444 +                                                oname);
1445 +                                error(USER, errmsg);
1446 +                        }
1447 +                        sout.xr = xres; sout.yr = yres;
1448 +                        if ((sout.xr > 0) & (sout.yr > 0) &&
1449 +                                        (!fscnresolu(&xr, &yr, sout.ofp) ||
1450 +                                                (xr != sout.xr) |
1451 +                                                (yr != sout.yr))) {
1452 +                                sprintf(errmsg, "resolution mismatch for '%s'",
1453 +                                                oname);
1454 +                                error(USER, errmsg);
1455 +                        }
1456 +                        nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz;
1457 +                        if ((lastout < 0) | (nvals < lastout))
1458 +                                lastout = nvals;
1459 +                        if (oent->key == NULL)  /* new entry */
1460 +                                oent->key = strcpy((char *)
1461 +                                                malloc(strlen(oname)+1), oname);
1462 +                        if (oent->data == NULL)
1463 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1464 +                        *(STREAMOUT *)oent->data = sout;
1465 +                        if (!(ofl & OF_BIN))
1466 +                                break;          /* no bin separation */
1467 +                }
1468 +                if (!lastout) {                 /* empty output */
1469 +                        error(WARNING, "no previous data to recover");
1470 +                        lu_done(&ofiletab);     /* reclose all outputs */
1471 +                        return;
1472 +                }
1473 +                if (j > mp->nbins)              /* reallocate modifier bins */
1474 +                        ment->data = (char *)(mp = growmodifier(mp, j));
1475 +        }
1476 +        if (lastout < 0) {
1477 +                error(WARNING, "no output files to recover");
1478 +                return;
1479 +        }
1480 +        if (raysleft && lastout >= raysleft/accumulate) {
1481 +                error(WARNING, "output appears to be complete");
1482 +                /* XXX should read & discard input? */
1483 +                quit(0);
1484 +        }
1485 +                                                /* seek on all files */
1486 +        nvals = lastout * outvsiz;
1487 +        lu_doall(&ofiletab, myseeko, &nvals);
1488 +                                                /* skip repeated input */
1489 +        for (nvals = 0; nvals < lastout; nvals++)
1490 +                if (getinp(oname, fin) < 0)
1491 +                        error(USER, "unexpected EOF on input");
1492 +        lastray = lastdone = (unsigned long)lastout * accumulate;
1493 +        if (raysleft)
1494 +                raysleft -= lastray;
1495   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines