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.44 by greg, Thu Nov 15 18:32:34 2007 UTC vs.
Revision 1.59 by greg, Sun Oct 10 01:26:51 2010 UTC

# Line 14 | Line 14 | static const char RCSid[] = "$Id$";
14   #include  "color.h"
15   #include  "resolu.h"
16   #include  "lookup.h"
17 + #include  "random.h"
18   #include  "calcomp.h"
19  
20   #ifndef MAXMODLIST
21   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
22   #endif
23  
24 < int     treebufsiz = BUFSIZ;            /* current tree buffer size */
24 > size_t  treebufsiz = BUFSIZ;            /* current tree buffer size */
25  
26   typedef double  DCOLOR[3];              /* double-precision color */
27  
28   /*
29   * The MODCONT structure is used to accumulate ray contributions
30   * for a particular modifier, which may be subdivided into bins
31 < * if binv is non-zero.  If outspec contains a %s in it, this will
31 > * if binv evaluates > 0.  If outspec contains a %s in it, this will
32   * be replaced with the modifier name.  If outspec contains a %d in it,
33   * this will be used to create one output file per bin, otherwise all bins
34   * will be written to the same file, in order.  If the global outfmt
# Line 46 | Line 47 | static void mcfree(void *p) { epfree((*(MODCONT *)p).b
47  
48   LUTAB   modconttab = LU_SINIT(NULL,mcfree);     /* modifier lookup table */
49  
49 #define CNT_UNKNOWN     0               /* unknown record length */
50 #define CNT_PIPE        (-1)            /* output to a pipe */
50   /*
51   * The STREAMOUT structure holds an open FILE pointer and a count of
52 < * the number of RGB triplets per record, or CNT_UNKNOWN (0) if
54 < * unknown or CNT_PIPE (-1) if writing to a command.
52 > * the number of RGB triplets per record, or 0 if unknown.
53   */
54   typedef struct {
55          FILE            *ofp;           /* output file pointer */
56 +        int             outpipe;        /* output is to a pipe */
57          int             reclen;         /* triplets/record */
58 +        int             xr, yr;         /* output resolution for picture */
59   } STREAMOUT;
60  
61   /* close output stream and free record */
# Line 64 | Line 64 | closestream(void *p)
64   {
65          STREAMOUT       *sop = (STREAMOUT *)p;
66          int             status;
67 <        if (sop->reclen == CNT_PIPE)
67 >        if (sop->outpipe)
68                  status = pclose(sop->ofp);
69          else
70                  status = fclose(sop->ofp);
# Line 81 | Line 81 | LUTAB  ofiletab = LU_SINIT(free,closestream);  /* output
81   STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen);
82   int ofname(char *oname, const char *ospec, const char *mname, int bn);
83   void printheader(FILE *fout, const char *info);
84 < void printresolu(FILE *fout);
84 > void printresolu(FILE *fout, int xr, int yr);
85  
86   /*
87   * The rcont structure is used to manage i/o with a particular
# Line 93 | Line 93 | struct rtproc {
93          struct rtproc   *next;          /* next in list of processes */
94          SUBPROC         pd;             /* rtrace pipe descriptors */
95          unsigned long   raynum;         /* ray number for this tree */
96 <        int             bsiz;           /* ray tree buffer length */
96 >        size_t          bsiz;           /* ray tree buffer length */
97          char            *buf;           /* ray tree buffer */
98 <        int             nbr;            /* number of bytes from rtrace */
98 >        size_t          nbr;            /* number of bytes from rtrace */
99   };                              /* rtrace process buffer */
100  
101                                          /* rtrace command and defaults */
102   char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
103 <                                "-dj", ".5", "-dr", "3",
103 >                                "-dj", ".9", "-dr", "3",
104                                  "-ab", "1", "-ad", "350", };
105  
106   int  rtargc = 9;
# Line 133 | Line 133 | int            outfmt = 'a';           /* output format */
133  
134   int             header = 1;             /* output header? */
135   int             force_open = 0;         /* truncate existing output? */
136 + int             recover = 0;            /* recover previous output? */
137 + int             accumulate = 1;         /* input rays per output record */
138   int             xres = 0;               /* horiz. output resolution */
139   int             yres = 0;               /* vert. output resolution */
140  
141 + int             account;                /* current accumulation count */
142   unsigned long   raysleft;               /* number of rays left to trace */
143   long            waitflush;              /* how long until next flush */
144  
# Line 155 | Line 158 | void addmodfile(char *fname, char *outf, char *binv, i
158  
159   void init(int np);
160   int done_rprocs(struct rtproc *rtp);
161 + void reload_output(void);
162   void recover_output(FILE *fin);
163   void trace_contribs(FILE *fin);
164   struct rtproc *wait_rproc(void);
# Line 164 | Line 168 | void process_queue(void);
168  
169   void put_contrib(const DCOLOR cnt, FILE *fout);
170   void add_contrib(const char *modn);
171 < void done_contrib(void);
171 > void done_contrib(int navg);
172  
173   /* return number of open rtrace processes */
174   static int
# Line 219 | Line 223 | main(int argc, char *argv[])
223   {
224          int     contrib = 0;
225          int     nprocs = 1;
222        int     recover = 0;
226          char    *curout = NULL;
227          char    *binval = NULL;
228          int     bincnt = 0;
# Line 244 | Line 247 | main(int argc, char *argv[])
247                  while ((j = expandarg(&argc, &argv, i)) > 0)
248                          ;
249                  if (j < 0) {
250 <                        fprintf(stderr, "%s: cannot expand '%s'",
250 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
251                                          argv[0], argv[i]);
252                          exit(1);
253                  }
# Line 273 | Line 276 | main(int argc, char *argv[])
276                                          continue;
277                                  }
278                                  break;
279 +                        case 'c':               /* input rays per output */
280 +                                if (argv[i][2] || i >= argc-2) break;
281 +                                accumulate = atoi(argv[++i]);
282 +                                continue;
283                          case 'r':               /* recover output */
284                                  if (argv[i][2]) break;
285 <                                recover++;
285 >                                recover = 1;
286                                  continue;
287                          case 'h':               /* output header? */
288                                  switch (argv[i][2]) {
# Line 310 | Line 317 | main(int argc, char *argv[])
317                                          continue;
318                                  }
319                                  if (argv[i][2] == 'o') {
320 <                                        force_open++;
320 >                                        force_open = 1;
321                                          continue;
322                                  }
323                                  setformat(argv[i]+2);
# Line 334 | Line 341 | main(int argc, char *argv[])
341                          case 'b':               /* bin expression/count */
342                                  if (i >= argc-2) break;
343                                  if (argv[i][2] == 'n') {
344 <                                        bincnt = atoi(argv[++i]);
344 >                                        bincnt = (int)(eval(argv[++i]) + .5);
345                                          continue;
346                                  }
347                                  if (argv[i][2]) break;
# Line 362 | Line 369 | main(int argc, char *argv[])
369                          }
370                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
371          }
372 +        if (accumulate <= 0)    /* no output flushing for single record */
373 +                xres = yres = 0;
374                                  /* set global argument list */
375          gargc = argc; gargv = argv;
376                                  /* add "mandatory" rtrace settings */
# Line 370 | Line 379 | main(int argc, char *argv[])
379          rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
380                                  /* just asking for defaults? */
381          if (!strcmp(argv[i], "-defaults")) {
382 <                char    sxres[16], syres[16];
382 >                char    nps[8], sxres[16], syres[16];
383                  char    *rtpath;
384 <                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++] = "-n";
390 +                sprintf(nps, "%d", nprocs);
391 +                rtargv[rtargc++] = nps;
392                  rtargv[rtargc++] = header ? "-h+" : "-h-";
393                  sprintf(fmt, "-f%c%c", inpfmt, outfmt);
394                  rtargv[rtargc++] = fmt;
# Line 414 | Line 427 | main(int argc, char *argv[])
427                  error(USER, "missing octree argument");
428          rtargv[rtargc++] = octree = argv[i];
429          rtargv[rtargc] = NULL;
430 <                                /* start rtrace */
430 >                                /* start rtrace & recover if requested */
431          init(nprocs);
432 <        if (recover)            /* perform recovery if requested */
433 <                recover_output(stdin);
434 <        trace_contribs(stdin);  /* compute contributions */
432 >                                /* compute contributions */
433 >        trace_contribs(stdin);
434 >                                /* clean up */
435          quit(0);
436   }
437  
# Line 464 | Line 477 | quit(int status)
477   {
478          int     rtstat;
479  
480 <        if (persist_state == PERSIST_OURS)  /* terminate persistent rtrace */
480 >        if (persist_state == PERSIST_OURS)  /* terminate waiting rtrace */
481                  killpersist();
482                                          /* clean up rtrace process(es) */
483          rtstat = done_rprocs(&rt0);
# Line 531 | Line 544 | init(int np)
544                          raysleft = yres;
545          } else
546                  raysleft = 0;
547 <        waitflush = xres;
547 >        if ((account = accumulate) > 0)
548 >                raysleft *= accumulate;
549 >        waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
550 >        if (!recover)
551 >                return;
552 >                                        /* recover previous values */
553 >        if (accumulate <= 0)
554 >                reload_output();
555 >        else
556 >                recover_output(stdin);
557   }
558  
559   /* grow modifier to accommodate more bins */
# Line 561 | Line 583 | addmodifier(char *modn, char *outf, char *binv, int bi
583                  error(USER, errmsg);
584          }
585          if (nmods >= MAXMODLIST)
586 <                error(USER, "too many modifiers");
586 >                error(INTERNAL, "too many modifiers");
587          modname[nmods++] = modn;        /* XXX assumes static string */
588          lep->key = modn;                /* XXX assumes static string */
589          mp = (MODCONT *)malloc(sizeof(MODCONT));
# Line 569 | Line 591 | addmodifier(char *modn, char *outf, char *binv, int bi
591                  error(SYSTEM, "out of memory in addmodifier");
592          mp->outspec = outf;             /* XXX assumes static string */
593          mp->modname = modn;             /* XXX assumes static string */
594 <        if (binv != NULL)
595 <                mp->binv = eparse(binv);
596 <        else
597 <                mp->binv = eparse("0");
598 <        mp->nbins = 1;
594 >        if (binv == NULL)
595 >                binv = "0";             /* use single bin if unspecified */
596 >        mp->binv = eparse(binv);
597 >        if (mp->binv->type == NUM) {    /* check value if constant */
598 >                bincnt = (int)(evalue(mp->binv) + 1.5);
599 >                if (bincnt != 1) {
600 >                        sprintf(errmsg, "illegal non-zero constant for bin (%s)",
601 >                                        binv);
602 >                        error(USER, errmsg);
603 >                }
604 >        }
605 >        mp->nbins = 1;                  /* initialize results holder */
606          setcolor(mp->cbin[0], 0., 0., 0.);
607 <        if (mp->binv->type == NUM)      /* assume one bin if constant */
579 <                bincnt = 1;
580 <        else if (bincnt > 1)
607 >        if (bincnt > 1)
608                  mp = growmodifier(mp, bincnt);
609          lep->data = (char *)mp;
610                                          /* allocate output streams */
611 <        for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i-- > 0; )
611 >        for (i = bincnt; i-- > 0; )
612                  getostream(mp->outspec, mp->modname, i, 1);
613          return mp;
614   }
# Line 640 | Line 667 | ofname(char *oname, const char *ospec, const char *mna
667                                  mnp = cp;
668                                  break;
669                          case 'd':
670 +                        case 'i':
671 +                        case 'o':
672 +                        case 'x':
673 +                        case 'X':
674                                  if (bnp != NULL)
675                                          return -1;
676                                  bnp = cp;
# Line 702 | Line 733 | printheader(FILE *fout, const char *info)
733  
734   /* write resolution string to given output stream */
735   void
736 < printresolu(FILE *fout)
736 > printresolu(FILE *fout, int xr, int yr)
737   {
738 <        if (xres > 0) {
739 <                if (yres > 0)                   /* resolution string */
709 <                        fprtresolu(xres, yres, fout);
710 <                fflush(fout);
711 <        }
738 >        if ((xr > 0) & (yr > 0))        /* resolution string */
739 >                fprtresolu(xr, yr, fout);
740   }
741  
742   /* Get output stream pointer (open and write header if new and noopen==0) */
743   STREAMOUT *
744   getostream(const char *ospec, const char *mname, int bn, int noopen)
745   {
746 +        static const DCOLOR     nocontrib = BLKCOLOR;
747          static STREAMOUT        stdos;
748          int                     ofl;
749          char                    oname[1024];
# Line 723 | Line 752 | getostream(const char *ospec, const char *mname, int b
752          
753          if (ospec == NULL) {                    /* use stdout? */
754                  if (!noopen && !using_stdout) {
726                        stdos.reclen = 0;
755                          if (outfmt != 'a')
756                                  SET_FILE_BINARY(stdout);
757                          if (header)
758                                  printheader(stdout, NULL);
759 <                        printresolu(stdout);
759 >                        printresolu(stdout, xres, yres);
760 >                        if (waitflush > 0)
761 >                                fflush(stdout);
762 >                        stdos.xr = xres; stdos.yr = yres;
763                          using_stdout = 1;
764                  }
765                  stdos.ofp = stdout;
# Line 748 | Line 779 | getostream(const char *ospec, const char *mname, int b
779                  sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
780                  if (sop == NULL)
781                          error(SYSTEM, "out of memory in getostream");
782 <                sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN;
782 >                sop->outpipe = oname[0] == '!';
783 >                sop->reclen = 0;
784                  sop->ofp = NULL;                /* open iff noopen==0 */
785 +                sop->xr = xres; sop->yr = yres;
786                  lep->data = (char *)sop;
787 +                if (!sop->outpipe & !force_open & !recover &&
788 +                                access(oname, F_OK) == 0) {
789 +                        errno = EEXIST;         /* file exists */
790 +                        goto openerr;
791 +                }
792          }
793          if (!noopen && sop->ofp == NULL) {      /* open output stream */
794                  long            i;
795                  if (oname[0] == '!')            /* output to command */
796                          sop->ofp = popen(oname+1, "w");
797 <                else if (!force_open && access(oname, F_OK) == 0)
760 <                        errno = EEXIST;         /* file exists */
761 <                else                            /* else open it */
797 >                else                            /* else open file */
798                          sop->ofp = fopen(oname, "w");
799 <                if (sop->ofp == NULL) {
800 <                        sprintf(errmsg, "cannot open '%s' for writing", oname);
765 <                        error(SYSTEM, errmsg);
766 <                }
799 >                if (sop->ofp == NULL)
800 >                        goto openerr;
801                  if (outfmt != 'a')
802                          SET_FILE_BINARY(sop->ofp);
803                  if (header) {
# Line 780 | Line 814 | getostream(const char *ospec, const char *mname, int b
814                          *cp = '\0';
815                          printheader(sop->ofp, info);
816                  }
817 <                printresolu(sop->ofp);
817 >                if (accumulate > 0) {           /* global resolution */
818 >                        sop->xr = xres; sop->yr = yres;
819 >                }
820 >                printresolu(sop->ofp, sop->xr, sop->yr);
821                                                  /* play catch-up */
822 <                for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone;
823 <                                                                i--; ) {
824 <                        static const DCOLOR     nocontrib = BLKCOLOR;
825 <                        put_contrib(nocontrib, sop->ofp);
822 >                for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
823 >                        int     j = sop->reclen;
824 >                        if (j <= 0) j = 1;
825 >                        while (j--)
826 >                                put_contrib(nocontrib, sop->ofp);
827                          if (outfmt == 'a')
828                                  putc('\n', sop->ofp);
829                  }
830 <                if (xres > 0)
830 >                if (waitflush > 0)
831                          fflush(sop->ofp);
832          }
833 <        if (sop->reclen != CNT_PIPE)            /* add to length if noopen */
834 <                sop->reclen += noopen;
835 <        return sop;                             /* return open stream */
833 >        sop->reclen += noopen;                  /* add to length if noopen */
834 >        return sop;                             /* return output stream */
835 > openerr:
836 >        sprintf(errmsg, "cannot open '%s' for writing", oname);
837 >        error(SYSTEM, errmsg);
838 >        return NULL;    /* pro forma return */
839   }
840  
841   /* read input ray into buffer */
842   int
843   getinp(char *buf, FILE *fp)
844   {
845 <        double  dv[3];
846 <        float   fv[3];
845 >        double  dv[3], *dvp;
846 >        float   *fvp;
847          char    *cp;
848          int     i;
849  
# Line 826 | Line 867 | getinp(char *buf, FILE *fp)
867          case 'f':
868                  if (fread(buf, sizeof(float), 6, fp) < 6)
869                          return -1;
870 <                memcpy(fv, buf+3*sizeof(float), 3*sizeof(float));
871 <                if (DOT(fv,fv) <= FTINY*FTINY)
870 >                fvp = (float *)buf + 3;
871 >                if (DOT(fvp,fvp) <= FTINY*FTINY)
872                          return 0;       /* dummy ray */
873                  return sizeof(float)*6;
874          case 'd':
875                  if (fread(buf, sizeof(double), 6, fp) < 6)
876                          return -1;
877 <                memcpy(dv, buf+3*sizeof(double), 3*sizeof(double));
878 <                if (DOT(dv,dv) <= FTINY*FTINY)
877 >                dvp = (double *)buf + 3;
878 >                if (DOT(dvp,dvp) <= FTINY*FTINY)
879                          return 0;       /* dummy ray */
880                  return sizeof(double)*6;
881          }
# Line 903 | Line 944 | put_contrib(const DCOLOR cnt, FILE *fout)
944                  fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
945                  break;
946          case 'f':
947 <                fv[0] = cnt[0];
907 <                fv[1] = cnt[1];
908 <                fv[2] = cnt[2];
947 >                copycolor(fv, cnt);
948                  fwrite(fv, sizeof(float), 3, fout);
949                  break;
950          case 'd':
# Line 920 | Line 959 | put_contrib(const DCOLOR cnt, FILE *fout)
959          }
960   }
961  
962 < /* output ray tallies and clear for next primary */
962 > /* output ray tallies and clear for next accumulation */
963   void
964 < done_contrib(void)
964 > done_contrib(int navg)
965   {
966 +        double          sf = 1.;
967          int             i, j;
968          MODCONT         *mp;
969          STREAMOUT       *sop;
970 +                                                /* set average scaling */
971 +        if (navg > 1)
972 +                sf = 1. / (double)navg;
973                                                  /* output modifiers in order */
974          for (i = 0; i < nmods; i++) {
975                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
976 +                if (navg > 1)                   /* average scaling */
977 +                        for (j = mp->nbins; j--; )
978 +                                scalecolor(mp->cbin[j], sf);
979                  sop = getostream(mp->outspec, mp->modname, 0,0);
980                  put_contrib(mp->cbin[0], sop->ofp);
981                  if (mp->nbins > 3 &&            /* minor optimization */
# Line 940 | Line 986 | done_contrib(void)
986                          for (j = 1; j < mp->nbins; j++)
987                                  put_contrib(mp->cbin[j],
988                                      getostream(mp->outspec,mp->modname,j,0)->ofp);
989 <                                                /* clear for next ray tree */
989 >                                                /* clear for next time */
990                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
991          }
992          --waitflush;                            /* terminate records */
# Line 948 | Line 994 | done_contrib(void)
994          if (using_stdout & (outfmt == 'a'))
995                  putc('\n', stdout);
996          if (!waitflush) {
997 <                waitflush = xres;
997 >                waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
998                  if (using_stdout)
999                          fflush(stdout);
1000          }
# Line 1013 | Line 1059 | process_queue(void)
1059                          cp += sizeof(float)*9; n -= sizeof(float)*9;
1060                          add_contrib(modname);
1061                  }
1062 <                done_contrib();         /* sum up contributions & output */
1062 >                                        /* time to produce record? */
1063 >                if (account > 0 && !--account)
1064 >                        done_contrib(account = accumulate);
1065                  lastdone = rtp->raynum;
1066                  if (rtp->buf != NULL)   /* free up buffer space */
1067                          free(rtp->buf);
# Line 1028 | Line 1076 | wait_rproc(void)
1076   {
1077          struct rtproc   *rtfree = NULL;
1078          fd_set          readset, errset;
1079 <        int             nr;
1079 >        ssize_t         nr;
1080          struct rtproc   *rt;
1081          int             n;
1082          
# Line 1100 | Line 1148 | get_rproc(void)
1148   void
1149   trace_contribs(FILE *fin)
1150   {
1151 +        static int      ignore_warning_given = 0;
1152          char            inpbuf[128];
1153          int             iblen;
1154          struct rtproc   *rtp;
1155                                                  /* loop over input */
1156          while ((iblen = getinp(inpbuf, fin)) >= 0) {
1157 <                if (!iblen ||                   /* need reset? */
1157 >                if (!iblen && accumulate != 1) {
1158 >                        if (!ignore_warning_given++)
1159 >                                error(WARNING,
1160 >                                "dummy ray(s) ignored during accumulation\n");
1161 >                        continue;
1162 >                }
1163 >                if (!iblen ||                   /* need flush/reset? */
1164                                  queue_length() > 10*nrtprocs() ||
1165                                  lastray+1 < lastray) {
1166                          while (wait_rproc() != NULL)
# Line 1118 | Line 1173 | trace_contribs(FILE *fin)
1173                  if (iblen) {                    /* trace ray if valid */
1174                          writebuf(rtp->pd.w, inpbuf, iblen);
1175                  } else {                        /* else bypass dummy ray */
1176 <                        queue_raytree(rtp);     /* empty tree */
1177 <                        if ((yres <= 0) | (waitflush > 1))
1178 <                                waitflush = 1;  /* flush after this */
1176 >                        queue_raytree(rtp);     /* queue empty ray/record */
1177 >                        if ((yres <= 0) | (xres <= 0))
1178 >                                waitflush = 1;  /* flush right after */
1179                  }
1180                  process_queue();                /* catch up with results */
1181                  if (raysleft && !--raysleft)
# Line 1128 | Line 1183 | trace_contribs(FILE *fin)
1183          }
1184          while (wait_rproc() != NULL)            /* process outstanding rays */
1185                  process_queue();
1186 +        if (accumulate <= 0)
1187 +                done_contrib(0);                /* output tallies */
1188 +        else if (account < accumulate) {
1189 +                error(WARNING, "partial accumulation in final record");
1190 +                done_contrib(accumulate - account);
1191 +        }
1192          if (raysleft)
1193                  error(USER, "unexpected EOF on input");
1194          lu_done(&ofiletab);                     /* close output files */
1195   }
1196  
1197 + /* get ray contribution from previous file */
1198 + static int
1199 + get_contrib(DCOLOR cnt, FILE *finp)
1200 + {
1201 +        COLOR   fv;
1202 +        COLR    cv;
1203 +
1204 +        switch (outfmt) {
1205 +        case 'a':
1206 +                return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1207 +        case 'f':
1208 +                if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1209 +                        return(0);
1210 +                copycolor(cnt, fv);
1211 +                return(1);
1212 +        case 'd':
1213 +                return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1214 +        case 'c':
1215 +                if (fread(cv, sizeof(cv), 1, finp) != 1)
1216 +                        return(0);
1217 +                colr_color(fv, cv);
1218 +                copycolor(cnt, fv);
1219 +                return(1);
1220 +        default:
1221 +                error(INTERNAL, "botched output format");
1222 +        }
1223 +        return(0);      /* pro forma return */
1224 + }
1225 +
1226 + /* close output file opened for input */
1227 + static int
1228 + myclose(const LUENT *e, void *p)
1229 + {
1230 +        STREAMOUT       *sop = (STREAMOUT *)e->data;
1231 +        
1232 +        if (sop->ofp == NULL)
1233 +                return(0);
1234 +        fclose(sop->ofp);
1235 +        sop->ofp = NULL;
1236 +        return(0);
1237 + }
1238 +
1239 + /* load previously accumulated values */
1240 + void
1241 + reload_output(void)
1242 + {
1243 +        int             i, j;
1244 +        MODCONT         *mp;
1245 +        int             ofl;
1246 +        char            oname[1024];
1247 +        char            *fmode = "rb";
1248 +        char            *outvfmt;
1249 +        LUENT           *ment, *oent;
1250 +        int             xr, yr;
1251 +        STREAMOUT       sout;
1252 +        DCOLOR          rgbv;
1253 +
1254 +        switch (outfmt) {
1255 +        case 'a':
1256 +                outvfmt = "ascii";
1257 +                fmode = "r";
1258 +                break;
1259 +        case 'f':
1260 +                outvfmt = "float";
1261 +                break;
1262 +        case 'd':
1263 +                outvfmt = "double";
1264 +                break;
1265 +        case 'c':
1266 +                outvfmt = COLRFMT;
1267 +                break;
1268 +        default:
1269 +                error(INTERNAL, "botched output format");
1270 +                return;
1271 +        }
1272 +                                                /* reload modifier values */
1273 +        for (i = 0; i < nmods; i++) {
1274 +                ment = lu_find(&modconttab,modname[i]);
1275 +                mp = (MODCONT *)ment->data;
1276 +                if (mp->outspec == NULL)
1277 +                        error(USER, "cannot reload from stdout");
1278 +                if (mp->outspec[0] == '!')
1279 +                        error(USER, "cannot reload from command");
1280 +                for (j = 0; ; j++) {            /* load each modifier bin */
1281 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1282 +                        if (ofl < 0)
1283 +                                error(USER, "bad output file specification");
1284 +                        oent = lu_find(&ofiletab, oname);
1285 +                        if (oent->data != NULL) {
1286 +                                sout = *(STREAMOUT *)oent->data;
1287 +                        } else {
1288 +                                sout.reclen = 0;
1289 +                                sout.outpipe = 0;
1290 +                                sout.xr = xres; sout.yr = yres;
1291 +                                sout.ofp = NULL;
1292 +                        }
1293 +                        if (sout.ofp == NULL) { /* open output as input */
1294 +                                sout.ofp = fopen(oname, fmode);
1295 +                                if (sout.ofp == NULL) {
1296 +                                        if (j)
1297 +                                                break;  /* assume end of modifier */
1298 +                                        sprintf(errmsg, "missing reload file '%s'",
1299 +                                                        oname);
1300 +                                        error(WARNING, errmsg);
1301 +                                        break;
1302 +                                }
1303 +                                if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1304 +                                        sprintf(errmsg, "format mismatch for '%s'",
1305 +                                                        oname);
1306 +                                        error(USER, errmsg);
1307 +                                }
1308 +                                if ((sout.xr > 0) & (sout.yr > 0) &&
1309 +                                                (!fscnresolu(&xr, &yr, sout.ofp) ||
1310 +                                                        (xr != sout.xr) |
1311 +                                                        (yr != sout.yr))) {
1312 +                                        sprintf(errmsg, "resolution mismatch for '%s'",
1313 +                                                        oname);
1314 +                                        error(USER, errmsg);
1315 +                                }
1316 +                        }
1317 +                                                        /* read in RGB value */
1318 +                        if (!get_contrib(rgbv, sout.ofp)) {
1319 +                                if (!j) {
1320 +                                        fclose(sout.ofp);
1321 +                                        break;          /* ignore empty file */
1322 +                                }
1323 +                                if (j < mp->nbins) {
1324 +                                        sprintf(errmsg, "missing data in '%s'",
1325 +                                                        oname);
1326 +                                        error(USER, errmsg);
1327 +                                }
1328 +                                break;
1329 +                        }
1330 +                        if (j >= mp->nbins)             /* grow modifier size */
1331 +                                ment->data = (char *)(mp = growmodifier(mp, j+1));
1332 +                        copycolor(mp->cbin[j], rgbv);
1333 +                        if (oent->key == NULL)          /* new file entry */
1334 +                                oent->key = strcpy((char *)
1335 +                                                malloc(strlen(oname)+1), oname);
1336 +                        if (oent->data == NULL)
1337 +                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1338 +                        *(STREAMOUT *)oent->data = sout;
1339 +                }
1340 +        }
1341 +        lu_doall(&ofiletab, myclose, NULL);     /* close all files */
1342 + }
1343 +
1344   /* seek on the given output file */
1345   static int
1346   myseeko(const LUENT *e, void *p)
# Line 1201 | Line 1409 | recover_output(FILE *fin)
1409                          if (oent->data != NULL) {
1410                                  sout = *(STREAMOUT *)oent->data;
1411                          } else {
1412 <                                sout.reclen = CNT_UNKNOWN;
1412 >                                sout.reclen = 0;
1413 >                                sout.outpipe = 0;
1414                                  sout.ofp = NULL;
1415                          }
1416                          if (sout.ofp != NULL) { /* already open? */
# Line 1216 | Line 1425 | recover_output(FILE *fin)
1425                                          break;  /* assume end of modifier */
1426                                  sprintf(errmsg, "missing recover file '%s'",
1427                                                  oname);
1428 <                                error(USER, errmsg);
1428 >                                error(WARNING, errmsg);
1429 >                                break;
1430                          }
1431                          nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1432                          if (nvals <= 0) {
# Line 1224 | Line 1434 | recover_output(FILE *fin)
1434                                  fclose(sout.ofp);
1435                                  break;
1436                          }
1437 <                        if (sout.reclen == CNT_UNKNOWN) {
1437 >                        if (!sout.reclen) {
1438                                  if (!(ofl & OF_BIN)) {
1439                                          sprintf(errmsg,
1440                                                  "need -bn to recover file '%s'",
# Line 1241 | Line 1451 | recover_output(FILE *fin)
1451                                                  oname);
1452                                  error(USER, errmsg);
1453                          }
1454 <                        if ((xres > 0) & (yres > 0) &&
1454 >                        sout.xr = xres; sout.yr = yres;
1455 >                        if ((sout.xr > 0) & (sout.yr > 0) &&
1456                                          (!fscnresolu(&xr, &yr, sout.ofp) ||
1457 <                                                xr != xres ||
1458 <                                                yr != yres)) {
1457 >                                                (xr != sout.xr) |
1458 >                                                (yr != sout.yr))) {
1459                                  sprintf(errmsg, "resolution mismatch for '%s'",
1460                                                  oname);
1461                                  error(USER, errmsg);
# Line 1273 | Line 1484 | recover_output(FILE *fin)
1484                  error(WARNING, "no output files to recover");
1485                  return;
1486          }
1487 <        if (raysleft && lastout >= raysleft) {
1487 >        if (raysleft && lastout >= raysleft/accumulate) {
1488                  error(WARNING, "output appears to be complete");
1489                  /* XXX should read & discard input? */
1490                  quit(0);
# Line 1285 | Line 1496 | recover_output(FILE *fin)
1496          for (nvals = 0; nvals < lastout; nvals++)
1497                  if (getinp(oname, fin) < 0)
1498                          error(USER, "unexpected EOF on input");
1499 <        lastray = lastdone = (unsigned long)lastout;
1499 >        lastray = lastdone = (unsigned long)lastout * accumulate;
1500          if (raysleft)
1501                  raysleft -= lastray;
1502   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines