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.24 by greg, Fri Jun 17 16:07:18 2005 UTC vs.
Revision 1.35 by greg, Tue Oct 11 16:54:26 2005 UTC

# Line 25 | Line 25 | int    treebufsiz = BUFSIZ;            /* current tree buffer size
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 is non-zero.  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 46 | 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 > #define CNT_UNKNOWN     0               /* unknown record length */
50 > #define CNT_PIPE        (-1)            /* output to a pipe */
51 > /*
52 > * The STREAMOUT structure holds an open FILE pointer and a count of
53 > * the number of RGB triplets per record, or CNT_UNKNOWN (0) if
54 > * unknown or CNT_PIPE (-1) if writing to a command.
55 > */
56 > typedef struct {
57 >        FILE            *ofp;           /* output file pointer */
58 >        int             reclen;         /* triplets/record */
59 > } STREAMOUT;
60  
61 < LUTAB   ofiletab = LU_SINIT(free,closefile);    /* output file table */
61 > /* close output stream and free record */
62 > static void
63 > closestream(void *p)
64 > {
65 >        STREAMOUT       *sop = (STREAMOUT *)p;
66 >        int             status;
67 >        if (sop->reclen == CNT_PIPE)
68 >                status = pclose(sop->ofp);
69 >        else
70 >                status = fclose(sop->ofp);
71 >        if (status)
72 >                error(SYSTEM, "error closing output stream");
73 >        free(p);
74 > }
75  
76 + LUTAB   ofiletab = LU_SINIT(free,closestream);  /* output file table */
77 +
78   #define OF_MODIFIER     01
79   #define OF_BIN          02
80  
81 < FILE *getofile(const char *ospec, const char *mname, int bn);
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);
85  
86   /*
87   * The rcont structure is used to manage i/o with a particular
# Line 98 | Line 123 | int            inpfmt = 'a';           /* input format */
123   int             outfmt = 'a';           /* output format */
124  
125   int             header = 1;             /* output header? */
126 + int             force_open = 0;         /* truncate existing output? */
127   int             xres = 0;               /* horiz. output resolution */
128   int             yres = 0;               /* vert. output resolution */
129  
# Line 112 | Line 138 | int            using_stdout = 0;       /* are we using stdout? */
138   const char      *modname[MAXMODLIST];   /* ordered modifier name list */
139   int             nmods = 0;              /* number of modifiers */
140  
141 < MODCONT *addmodifier(char *modn, char *outf, char *binv);
116 < void addmodfile(char *fname, char *outf, char *binv);
141 > #define queue_length()          (lastray - lastdone)
142  
143 + MODCONT *growmodifier(MODCONT *mp, int nb);
144 + MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt);
145 + void addmodfile(char *fname, char *outf, char *binv, int bincnt);
146 +
147   void init(int np);
148   int done_rprocs(struct rtproc *rtp);
149   void recover_output(FILE *fin);
# Line 128 | Line 157 | void put_contrib(const DCOLOR cnt, FILE *fout);
157   void add_contrib(const char *modn);
158   void done_contrib(void);
159  
160 + /* return number of open rtrace processes */
161 + static int
162 + nrtprocs(void)
163 + {
164 +        int     nrtp = 0;
165 +        struct rtproc   *rtp;
166 +
167 +        for (rtp = &rt0; rtp != NULL; rtp = rtp->next)
168 +                nrtp += rtp->pd.running;
169 +        return(nrtp);
170 + }
171 +
172   /* set input/output format */
173   static void
174   setformat(const char *fmt)
# Line 171 | Line 212 | main(int argc, char *argv[])
212          int     recover = 0;
213          char    *curout = NULL;
214          char    *binval = NULL;
215 +        int     bincnt = 0;
216          char    fmt[8];
217          int     i, j;
218 +                                /* need at least one argument */
219 +        if (argc < 2) {
220 +                fprintf(stderr,
221 + "Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
222 +                        argv[0]);
223 +                exit(1);
224 +        }
225                                  /* global program name */
226          gargv = argv;
227                                  /* initialize calcomp routines */
# Line 218 | Line 267 | main(int argc, char *argv[])
267                                          continue;
268                                  }
269                                  break;
270 <                        case 'f':               /* file or i/o format */
270 >                        case 'f':               /* file or force or format */
271                                  if (!argv[i][2]) {
272                                          char    *fpath;
273                                          if (i >= argc-2) break;
# Line 233 | Line 282 | main(int argc, char *argv[])
282                                          fcompile(fpath);
283                                          continue;
284                                  }
285 +                                if (argv[i][2] == 'o') {
286 +                                        force_open++;
287 +                                        continue;
288 +                                }
289                                  setformat(argv[i]+2);
290                                  continue;
291                          case 'e':               /* expression */
# Line 251 | Line 304 | main(int argc, char *argv[])
304                                  if (argv[i][2] || i >= argc-2) break;
305                                  yres = atoi(argv[++i]);
306                                  continue;
307 <                        case 'b':               /* bin expression */
308 <                                if (argv[i][2] || i >= argc-2) break;
307 >                        case 'b':               /* bin expression/count */
308 >                                if (i >= argc-2) break;
309 >                                if (argv[i][2] == 'n') {
310 >                                        bincnt = atoi(argv[++i]);
311 >                                        continue;
312 >                                }
313 >                                if (argv[i][2]) break;
314                                  binval = argv[++i];
315                                  continue;
316                          case 'm':               /* modifier name */
317                                  if (argv[i][2] || i >= argc-2) break;
318                                  rtargv[rtargc++] = "-ti";
319                                  rtargv[rtargc++] = argv[++i];
320 <                                addmodifier(argv[i], curout, binval);
320 >                                addmodifier(argv[i], curout, binval, bincnt);
321                                  continue;
322                          case 'M':               /* modifier file */
323                                  if (argv[i][2] || i >= argc-2) break;
324                                  rtargv[rtargc++] = "-tI";
325                                  rtargv[rtargc++] = argv[++i];
326 <                                addmodfile(argv[i], curout, binval);
326 >                                addmodfile(argv[i], curout, binval, bincnt);
327                                  continue;
328                          }
329                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
# Line 323 | Line 381 | main(int argc, char *argv[])
381          quit(0);
382   }
383  
384 + #ifndef SIGALRM
385 + #define SIGALRM SIGTERM
386 + #endif
387   /* kill persistent rtrace process */
388   static void
389   killpersist(void)
390   {
391          FILE    *fp = fopen(persistfn, "r");
392 <        int     pid;
392 >        RT_PID  pid;
393  
394          if (fp == NULL)
395                  return;
# Line 432 | Line 493 | init(int np)
493          waitflush = xres;
494   }
495  
496 + /* grow modifier to accommodate more bins */
497 + MODCONT *
498 + growmodifier(MODCONT *mp, int nb)
499 + {
500 +        if (nb <= mp->nbins)
501 +                return mp;
502 +        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1));
503 +        if (mp == NULL)
504 +                error(SYSTEM, "out of memory in growmodifier");
505 +        memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins));
506 +        mp->nbins = nb;
507 +        return mp;
508 + }
509 +
510   /* add modifier to our list to track */
511   MODCONT *
512 < addmodifier(char *modn, char *outf, char *binv)
512 > addmodifier(char *modn, char *outf, char *binv, int bincnt)
513   {
514          LUENT   *lep = lu_find(&modconttab, modn);
515          MODCONT *mp;
516 +        int     i;
517          
518          if (lep->data != NULL) {
519                  sprintf(errmsg, "duplicate modifier '%s'", modn);
# Line 450 | Line 526 | addmodifier(char *modn, char *outf, char *binv)
526          mp = (MODCONT *)malloc(sizeof(MODCONT));
527          if (mp == NULL)
528                  error(SYSTEM, "out of memory in addmodifier");
453        lep->data = (char *)mp;
529          mp->outspec = outf;             /* XXX assumes static string */
530          mp->modname = modn;             /* XXX assumes static string */
531          if (binv != NULL)
# Line 459 | Line 534 | addmodifier(char *modn, char *outf, char *binv)
534                  mp->binv = eparse("0");
535          mp->nbins = 1;
536          setcolor(mp->cbin[0], 0., 0., 0.);
537 +        if (mp->binv->type == NUM)      /* assume one bin if constant */
538 +                bincnt = 1;
539 +        else if (bincnt > 1)
540 +                mp = growmodifier(mp, bincnt);
541 +        lep->data = (char *)mp;
542 +                                        /* allocate output streams */
543 +        for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; )
544 +                getostream(mp->outspec, mp->modname, i, 1);
545          return mp;
546   }
547  
548   /* add modifiers from a file list */
549   void
550 < addmodfile(char *fname, char *outf, char *binv)
550 > addmodfile(char *fname, char *outf, char *binv, int bincnt)
551   {
552          char    *mname[MAXMODLIST];
553          int     i;
# Line 474 | Line 557 | addmodfile(char *fname, char *outf, char *binv)
557                  error(SYSTEM, errmsg);
558          }
559          for (i = 0; mname[i]; i++)      /* add each one */
560 <                addmodifier(mname[i], outf, binv);
560 >                addmodifier(mname[i], outf, binv, bincnt);
561   }
562  
563   /* put string to stderr */
# Line 574 | Line 657 | printheader(FILE *fout, const char *info)
657                  break;
658          }
659          fputc('\n', fout);
660 + }
661 +
662 + /* write resolution string to given output stream */
663 + void
664 + printresolu(FILE *fout)
665 + {
666          if (xres > 0) {
667                  if (yres > 0)                   /* resolution string */
668                          fprtresolu(xres, yres, fout);
# Line 581 | Line 670 | printheader(FILE *fout, const char *info)
670          }
671   }
672  
673 < /* Get output file pointer (open and write header if new) */
674 < FILE *
675 < getofile(const char *ospec, const char *mname, int bn)
673 > /* Get output stream pointer (open and write header if new and noopen==0) */
674 > STREAMOUT *
675 > getostream(const char *ospec, const char *mname, int bn, int noopen)
676   {
677 <        int             ofl;
678 <        char            oname[1024];
679 <        LUENT           *lep;
677 >        static STREAMOUT        stdos;
678 >        int                     ofl;
679 >        char                    oname[1024];
680 >        LUENT                   *lep;
681 >        STREAMOUT               *sop;
682          
683          if (ospec == NULL) {                    /* use stdout? */
684 <                if (!using_stdout) {
684 >                if (!noopen && !using_stdout) {
685 >                        stdos.reclen = 0;
686                          if (outfmt != 'a')
687                                  SET_FILE_BINARY(stdout);
688                          if (header)
689                                  printheader(stdout, NULL);
690 +                        printresolu(stdout);
691 +                        using_stdout = 1;
692                  }
693 <                using_stdout = 1;
694 <                return stdout;
693 >                stdos.ofp = stdout;
694 >                stdos.reclen += noopen;
695 >                return &stdos;
696          }
697          ofl = ofname(oname, ospec, mname, bn);  /* get output name */
698          if (ofl < 0) {
# Line 607 | Line 702 | getofile(const char *ospec, const char *mname, int bn)
702          lep = lu_find(&ofiletab, oname);        /* look it up */
703          if (lep->key == NULL)                   /* new entry */
704                  lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
705 <        if (lep->data == NULL) {                /* open output file */
706 <                FILE            *fp;
707 <                int             i;
705 >        sop = (STREAMOUT *)lep->data;
706 >        if (sop == NULL) {                      /* allocate stream */
707 >                sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
708 >                if (sop == NULL)
709 >                        error(SYSTEM, "out of memory in getostream");
710 >                sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN;
711 >                sop->ofp = NULL;                /* open iff noopen==0 */
712 >                lep->data = (char *)sop;
713 >        }
714 >        if (!noopen && sop->ofp == NULL) {      /* open output stream */
715 >                long            i;
716                  if (oname[0] == '!')            /* output to command */
717 <                        fp = popen(oname+1, "w");
718 <                else
719 <                        fp = fopen(oname, "w");
720 <                if (fp == NULL) {
717 >                        sop->ofp = popen(oname+1, "w");
718 >                else if (!force_open && access(oname, F_OK) == 0)
719 >                        errno = EEXIST;         /* file exists */
720 >                else                            /* else open it */
721 >                        sop->ofp = fopen(oname, "w");
722 >                if (sop->ofp == NULL) {
723                          sprintf(errmsg, "cannot open '%s' for writing", oname);
724                          error(SYSTEM, errmsg);
725                  }
726                  if (outfmt != 'a')
727 <                        SET_FILE_BINARY(fp);
727 >                        SET_FILE_BINARY(sop->ofp);
728                  if (header) {
729                          char    info[512];
730                          char    *cp = info;
731 <                        sprintf(cp, "MODIFIER=%s\n", mname);
732 <                        while (*cp) ++cp;
731 >                        if (ofl & OF_MODIFIER || sop->reclen == 1) {
732 >                                sprintf(cp, "MODIFIER=%s\n", mname);
733 >                                while (*cp) ++cp;
734 >                        }
735                          if (ofl & OF_BIN) {
736                                  sprintf(cp, "BIN=%d\n", bn);
737                                  while (*cp) ++cp;
738                          }
739                          *cp = '\0';
740 <                        printheader(fp, info);
740 >                        printheader(sop->ofp, info);
741                  }
742 +                printresolu(sop->ofp);
743                                                  /* play catch-up */
744 <                for (i = 0; i < lastdone; i++) {
744 >                for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone;
745 >                                                                i--; ) {
746                          static const DCOLOR     nocontrib = BLKCOLOR;
747 <                        put_contrib(nocontrib, fp);
747 >                        put_contrib(nocontrib, sop->ofp);
748                          if (outfmt == 'a')
749 <                                putc('\n', fp);
749 >                                putc('\n', sop->ofp);
750                  }
751                  if (xres > 0)
752 <                        fflush(fp);
644 <                lep->data = (char *)fp;
752 >                        fflush(sop->ofp);
753          }
754 <        return (FILE *)lep->data;               /* return open file pointer */
754 >        if (sop->reclen != CNT_PIPE)            /* add to length if noopen */
755 >                sop->reclen += noopen;
756 >        return sop;                             /* return open stream */
757   }
758  
759   /* read input ray into buffer */
# Line 706 | Line 816 | add_contrib(const char *modn)
816          bn = (int)(evalue(mp->binv) + .5);
817          if (bn <= 0)
818                  bn = 0;
819 <        else if (bn > mp->nbins) {      /* new bin */
820 <                mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
711 <                                                bn*sizeof(DCOLOR));
712 <                if (mp == NULL)
713 <                        error(SYSTEM, "out of memory in add_contrib");
714 <                memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins));
715 <                mp->nbins = bn+1;
716 <                le->data = (char *)mp;
717 <        }
819 >        else if (bn >= mp->nbins)       /* new bin */
820 >                le->data = (char *)(mp = growmodifier(mp, bn+1));
821          addcolor(mp->cbin[bn], rparams);
822   }
823  
# Line 722 | Line 825 | add_contrib(const char *modn)
825   static int
826   puteol(const LUENT *e, void *p)
827   {
828 <        FILE    *fp = (FILE *)e->data;
828 >        STREAMOUT       *sop = (STREAMOUT *)e->data;
829  
830          if (outfmt == 'a')
831 <                putc('\n', fp);
831 >                putc('\n', sop->ofp);
832          if (!waitflush)
833 <                fflush(fp);
834 <        if (ferror(fp)) {
833 >                fflush(sop->ofp);
834 >        if (ferror(sop->ofp)) {
835                  sprintf(errmsg, "write error on file '%s'", e->key);
836                  error(SYSTEM, errmsg);
837          }
# Line 768 | Line 871 | put_contrib(const DCOLOR cnt, FILE *fout)
871   void
872   done_contrib(void)
873   {
874 <        int     i, j;
875 <        MODCONT *mp;
876 <        FILE    *fp;
874 >        int             i, j;
875 >        MODCONT         *mp;
876 >        STREAMOUT       *sop;
877                                                  /* output modifiers in order */
878          for (i = 0; i < nmods; i++) {
879                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
880 <                fp = getofile(mp->outspec, mp->modname, 0);
881 <                put_contrib(mp->cbin[0], fp);
880 >                sop = getostream(mp->outspec, mp->modname, 0,0);
881 >                put_contrib(mp->cbin[0], sop->ofp);
882                  if (mp->nbins > 3 &&            /* minor optimization */
883 <                                fp == getofile(mp->outspec, mp->modname, 1))
883 >                                sop == getostream(mp->outspec, mp->modname, 1,0))
884                          for (j = 1; j < mp->nbins; j++)
885 <                                put_contrib(mp->cbin[j], fp);
885 >                                put_contrib(mp->cbin[j], sop->ofp);
886                  else
887                          for (j = 1; j < mp->nbins; j++)
888                                  put_contrib(mp->cbin[j],
889 <                                        getofile(mp->outspec, mp->modname, j));
889 >                                    getostream(mp->outspec,mp->modname,j,0)->ofp);
890                                                  /* clear for next ray tree */
891                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
892          }
# Line 905 | Line 1008 | wait_rproc(void)
1008                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
1009                                          rt->bsiz = treebufsiz;
1010                                  else
1011 <                                        rt->bsiz = treebufsiz += BUFSIZ;
1011 >                                        treebufsiz = rt->bsiz += BUFSIZ;
1012                                  rt->buf = (char *)realloc(rt->buf, rt->bsiz);
1013                          }
1014                          if (rt->buf == NULL)
# Line 946 | Line 1049 | trace_contribs(FILE *fin)
1049          struct rtproc   *rtp;
1050                                                  /* loop over input */
1051          while ((iblen = getinp(inpbuf, fin)) > 0) {
1052 <                if (lastray+1 < lastray) {      /* counter rollover? */
1052 >                if (lastray+1 < lastray ||      /* need reset? */
1053 >                                queue_length() > 10*nrtprocs()) {
1054                          while (wait_rproc() != NULL)
1055                                  process_queue();
1056 <                        lastdone = lastray = 0;
1056 >                        if (lastray+1 < lastray)
1057 >                                lastdone = lastray = 0;
1058                  }
1059                  rtp = get_rproc();              /* get avail. rtrace process */
1060                  rtp->raynum = ++lastray;        /* assign ray to it */
# Line 969 | Line 1074 | trace_contribs(FILE *fin)
1074   static int
1075   myseeko(const LUENT *e, void *p)
1076   {
1077 <        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
1077 >        STREAMOUT       *sop = (STREAMOUT *)e->data;
1078 >        off_t           nbytes = *(off_t *)p;
1079 >        
1080 >        if (sop->reclen > 1)
1081 >                nbytes = nbytes * sop->reclen;
1082 >        if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1083                  sprintf(errmsg, "seek error on file '%s'", e->key);
1084                  error(SYSTEM, errmsg);
1085          }
# Line 979 | Line 1089 | myseeko(const LUENT *e, void *p)
1089   void
1090   recover_output(FILE *fin)
1091   {
1092 <        off_t   lastout = -1;
1093 <        int     outvsiz;
1094 <        char    *outvfmt;
1095 <        int     i, j;
1096 <        MODCONT *mp;
1097 <        int     ofl;
1098 <        char    oname[1024];
1099 <        LUENT   *ment, *oent;
1100 <        FILE    *fp;
1101 <        off_t   nvals;
1092 >        off_t           lastout = -1;
1093 >        int             outvsiz, recsiz;
1094 >        char            *outvfmt;
1095 >        int             i, j;
1096 >        MODCONT         *mp;
1097 >        int             ofl;
1098 >        char            oname[1024];
1099 >        LUENT           *ment, *oent;
1100 >        STREAMOUT       sout;
1101 >        off_t           nvals;
1102 >        int             xr, yr;
1103  
1104          switch (outfmt) {
1105          case 'a':
# Line 1016 | Line 1127 | recover_output(FILE *fin)
1127                  mp = (MODCONT *)ment->data;
1128                  if (mp->outspec == NULL)
1129                          error(USER, "cannot recover from stdout");
1130 +                if (mp->outspec[0] == '!')
1131 +                        error(USER, "cannot recover from command");
1132                  for (j = 0; ; j++) {            /* check each bin's file */
1133                          ofl = ofname(oname, mp->outspec, mp->modname, j);
1134                          if (ofl < 0)
1135                                  error(USER, "bad output file specification");
1136                          oent = lu_find(&ofiletab, oname);
1137 <                        if (oent->data != NULL) { /* saw this one already */
1137 >                        if (oent->data != NULL) {
1138 >                                sout = *(STREAMOUT *)oent->data;
1139 >                        } else {
1140 >                                sout.reclen = CNT_UNKNOWN;
1141 >                                sout.ofp = NULL;
1142 >                        }
1143 >                        if (sout.ofp != NULL) { /* already open? */
1144                                  if (ofl & OF_BIN)
1145                                          continue;
1146                                  break;
1147                          }
1029                        if (oname[0] == '!')
1030                                error(USER, "cannot recover from command");
1148                                                  /* open output */
1149 <                        fp = fopen(oname, "rb+");
1150 <                        if (fp == NULL) {
1149 >                        sout.ofp = fopen(oname, "rb+");
1150 >                        if (sout.ofp == NULL) {
1151                                  if (j)
1152                                          break;  /* assume end of modifier */
1153                                  sprintf(errmsg, "missing recover file '%s'",
1154                                                  oname);
1155                                  error(USER, errmsg);
1156                          }
1157 <                        nvals = lseek(fileno(fp), 0, SEEK_END);
1157 >                        nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1158                          if (nvals <= 0) {
1159                                  lastout = 0;    /* empty output, quit here */
1160 <                                fclose(fp);
1160 >                                fclose(sout.ofp);
1161                                  break;
1162                          }
1163 <                        lseek(fileno(fp), 0, SEEK_SET);
1164 <                        if (header) {
1048 <                                int     xr, yr;
1049 <                                if (checkheader(fp, outvfmt, NULL) != 1) {
1163 >                        if (sout.reclen == CNT_UNKNOWN) {
1164 >                                if (!(ofl & OF_BIN)) {
1165                                          sprintf(errmsg,
1166 <                                                "format mismatch for '%s'",
1166 >                                                "need -bn to recover file '%s'",
1167                                                          oname);
1168                                          error(USER, errmsg);
1169                                  }
1170 <                                if (xres > 0 && yres > 0 &&
1171 <                                                (!fscnresolu(&xr, &yr, fp) ||
1172 <                                                        xr != xres ||
1173 <                                                        yr != yres)) {
1174 <                                        sprintf(errmsg,
1175 <                                                "resolution mismatch for '%s'",
1176 <                                                        oname);
1177 <                                        error(USER, errmsg);
1178 <                                }
1170 >                                recsiz = outvsiz;
1171 >                        } else
1172 >                                recsiz = outvsiz * sout.reclen;
1173 >
1174 >                        lseek(fileno(sout.ofp), 0, SEEK_SET);
1175 >                        if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1176 >                                sprintf(errmsg, "format mismatch for '%s'",
1177 >                                                oname);
1178 >                                error(USER, errmsg);
1179                          }
1180 <                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1181 <                        if (lastout < 0 || nvals < lastout)
1180 >                        if ((xres > 0) & (yres > 0) &&
1181 >                                        (!fscnresolu(&xr, &yr, sout.ofp) ||
1182 >                                                xr != xres ||
1183 >                                                yr != yres)) {
1184 >                                sprintf(errmsg, "resolution mismatch for '%s'",
1185 >                                                oname);
1186 >                                error(USER, errmsg);
1187 >                        }
1188 >                        nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz;
1189 >                        if ((lastout < 0) | (nvals < lastout))
1190                                  lastout = nvals;
1191 <                        if (oent->key == NULL) /* new entry */
1191 >                        if (oent->key == NULL)  /* new entry */
1192                                  oent->key = strcpy((char *)
1193                                                  malloc(strlen(oname)+1), oname);
1194 <                        oent->data = (char *)fp;
1194 >                        if (oent->data == NULL)
1195 >                                oent->data = (char *)malloc(sizeof(STREAMOUT));
1196 >                        *(STREAMOUT *)oent->data = sout;
1197                          if (!(ofl & OF_BIN))
1198                                  break;          /* no bin separation */
1199                  }
# Line 1077 | Line 1202 | recover_output(FILE *fin)
1202                          lu_done(&ofiletab);     /* reclose all outputs */
1203                          return;
1204                  }
1205 <                if (j > mp->nbins) {            /* preallocate modifier bins */
1206 <                        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
1082 <                                                        (j-1)*sizeof(DCOLOR));
1083 <                        if (mp == NULL)
1084 <                                error(SYSTEM, "out of memory in recover_output");
1085 <                        memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1086 <                        mp->nbins = j;
1087 <                        ment->data = (char *)mp;
1088 <                }
1205 >                if (j > mp->nbins)              /* reallocate modifier bins */
1206 >                        ment->data = (char *)(mp = growmodifier(mp, j));
1207          }
1208          if (lastout < 0) {
1209                  error(WARNING, "no output files to recover");
# Line 1099 | Line 1217 | recover_output(FILE *fin)
1217                                                  /* seek on all files */
1218          nvals = lastout * outvsiz;
1219          lu_doall(&ofiletab, myseeko, &nvals);
1220 <                                                /* discard input */
1220 >                                                /* skip repeated input */
1221          for (nvals = 0; nvals < lastout; nvals++)
1222                  if (getinp(oname, fin) <= 0)
1223                          error(USER, "unexpected EOF on input");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines