ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rtcontrib.c
Revision: 1.46
Committed: Sat Nov 17 06:21:28 2007 UTC (16 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.45: +116 -49 lines
Log Message:
Got -r option working with -c

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rtcontrib.c,v 1.45 2007/11/17 01:13:51 greg Exp $";
3 #endif
4 /*
5 * Gather rtrace output to compute contributions from particular sources
6 */
7
8 #include "standard.h"
9 #include <ctype.h>
10 #include <signal.h>
11 #include "platform.h"
12 #include "rtprocess.h"
13 #include "selcall.h"
14 #include "color.h"
15 #include "resolu.h"
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
29 * for a particular modifier, which may be subdivided into bins
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
34 * is 'c', then a 4-byte RGBE pixel will be output for each bin value
35 * and the file will conform to a RADIANCE image if xres & yres are set.
36 */
37 typedef struct {
38 const char *outspec; /* output file specification */
39 const char *modname; /* modifier name */
40 EPNODE *binv; /* bin value expression */
41 int nbins; /* number of accumulation bins */
42 DCOLOR cbin[1]; /* contribution bins (extends struct) */
43 } MODCONT; /* modifier contribution */
44
45 static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); }
46
47 LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */
48
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 /* close output stream and free record */
61 static void
62 closestream(void *p)
63 {
64 STREAMOUT *sop = (STREAMOUT *)p;
65 int status;
66 if (sop->outpipe)
67 status = pclose(sop->ofp);
68 else
69 status = fclose(sop->ofp);
70 if (status)
71 error(SYSTEM, "error closing output stream");
72 free(p);
73 }
74
75 LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */
76
77 #define OF_MODIFIER 01
78 #define OF_BIN 02
79
80 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,
88 * and output is processed in input order and accumulated according
89 * to the corresponding modifier and bin number.
90 */
91 struct rtproc {
92 struct rtproc *next; /* next in list of processes */
93 SUBPROC pd; /* rtrace pipe descriptors */
94 unsigned long raynum; /* ray number for this tree */
95 int bsiz; /* ray tree buffer length */
96 char *buf; /* ray tree buffer */
97 int nbr; /* number of bytes from rtrace */
98 }; /* rtrace process buffer */
99
100 /* rtrace command and defaults */
101 char *rtargv[256+2*MAXMODLIST] = { "rtrace",
102 "-dj", ".5", "-dr", "3",
103 "-ab", "1", "-ad", "350", };
104
105 int rtargc = 9;
106 /* overriding rtrace options */
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 #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 */
126 #define progname gargv[0]
127
128 char *octree; /* global octree argument */
129
130 int inpfmt = 'a'; /* input format */
131 int outfmt = 'a'; /* output format */
132
133 int header = 1; /* output header? */
134 int accumulate = 0; /* accumulate ray values? */
135 int force_open = 0; /* truncate existing output? */
136 int recover = 0; /* recover previous output? */
137 int xres = 0; /* horiz. output resolution */
138 int yres = 0; /* vert. output resolution */
139
140 unsigned long raysleft; /* number of rays left to trace */
141 long waitflush; /* how long until next flush */
142
143 unsigned long lastray = 0; /* last ray number sent */
144 unsigned long lastdone = 0; /* last ray processed */
145
146 int using_stdout = 0; /* are we using stdout? */
147
148 const char *modname[MAXMODLIST]; /* ordered modifier name list */
149 int nmods = 0; /* number of modifiers */
150
151 #define queue_length() (lastray - lastdone)
152
153 MODCONT *growmodifier(MODCONT *mp, int nb);
154 MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt);
155 void addmodfile(char *fname, char *outf, char *binv, int bincnt);
156
157 void init(int np);
158 int done_rprocs(struct rtproc *rtp);
159 void reload_output(void);
160 void recover_output(FILE *fin);
161 void trace_contribs(FILE *fin);
162 struct rtproc *wait_rproc(void);
163 struct rtproc *get_rproc(void);
164 void queue_raytree(struct rtproc *rtp);
165 void process_queue(void);
166
167 void put_contrib(const DCOLOR cnt, FILE *fout);
168 void add_contrib(const char *modn);
169 void done_contrib(void);
170
171 /* return number of open rtrace processes */
172 static int
173 nrtprocs(void)
174 {
175 int nrtp = 0;
176 struct rtproc *rtp;
177
178 for (rtp = &rt0; rtp != NULL; rtp = rtp->next)
179 nrtp += rtp->pd.running;
180 return(nrtp);
181 }
182
183 /* set input/output format */
184 static void
185 setformat(const char *fmt)
186 {
187 switch (fmt[0]) {
188 case 'f':
189 case 'd':
190 SET_FILE_BINARY(stdin);
191 /* fall through */
192 case 'a':
193 inpfmt = fmt[0];
194 break;
195 default:
196 goto fmterr;
197 }
198 switch (fmt[1]) {
199 case '\0':
200 outfmt = inpfmt;
201 return;
202 case 'a':
203 case 'f':
204 case 'd':
205 case 'c':
206 outfmt = fmt[1];
207 break;
208 default:
209 goto fmterr;
210 }
211 if (!fmt[2])
212 return;
213 fmterr:
214 sprintf(errmsg, "Illegal i/o format: -f%s", fmt);
215 error(USER, errmsg);
216 }
217
218 /* gather rays from rtrace and output contributions */
219 int
220 main(int argc, char *argv[])
221 {
222 int contrib = 0;
223 int nprocs = 1;
224 char *curout = NULL;
225 char *binval = NULL;
226 int bincnt = 0;
227 char fmt[8];
228 int i, j;
229 /* need at least one argument */
230 if (argc < 2) {
231 fprintf(stderr,
232 "Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
233 argv[0]);
234 exit(1);
235 }
236 /* global program name */
237 gargv = argv;
238 /* initialize calcomp routines */
239 esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
240 esupport &= ~(E_OUTCHAN);
241 varset("PI", ':', PI);
242 /* get our options */
243 for (i = 1; i < argc-1; i++) {
244 /* expand arguments */
245 while ((j = expandarg(&argc, &argv, i)) > 0)
246 ;
247 if (j < 0) {
248 fprintf(stderr, "%s: cannot expand '%s'",
249 argv[0], argv[i]);
250 exit(1);
251 }
252 if (argv[i][0] == '-')
253 switch (argv[i][1]) {
254 case 'n': /* number of processes */
255 if (argv[i][2] || i >= argc-2) break;
256 nprocs = atoi(argv[++i]);
257 if (nprocs <= 0)
258 error(USER, "illegal number of processes");
259 continue;
260 case 'V': /* output contributions */
261 switch (argv[i][2]) {
262 case '\0':
263 contrib = !contrib;
264 continue;
265 case '+': case '1':
266 case 'T': case 't':
267 case 'Y': case 'y':
268 contrib = 1;
269 continue;
270 case '-': case '0':
271 case 'F': case 'f':
272 case 'N': case 'n':
273 contrib = 0;
274 continue;
275 }
276 break;
277 case 'c': /* accumulate ray values */
278 switch (argv[i][2]) {
279 case '\0':
280 accumulate = !accumulate;
281 continue;
282 case '+': case '1':
283 case 'T': case 't':
284 case 'Y': case 'y':
285 accumulate = 1;
286 continue;
287 case '-': case '0':
288 case 'F': case 'f':
289 case 'N': case 'n':
290 accumulate = 0;
291 continue;
292 }
293 break;
294 case 'r': /* recover output */
295 if (argv[i][2]) break;
296 recover = 1;
297 continue;
298 case 'h': /* output header? */
299 switch (argv[i][2]) {
300 case '\0':
301 header = !header;
302 continue;
303 case '+': case '1':
304 case 'T': case 't':
305 case 'Y': case 'y':
306 header = 1;
307 continue;
308 case '-': case '0':
309 case 'F': case 'f':
310 case 'N': case 'n':
311 header = 0;
312 continue;
313 }
314 break;
315 case 'f': /* file or force or format */
316 if (!argv[i][2]) {
317 char *fpath;
318 if (i >= argc-2) break;
319 fpath = getpath(argv[++i],
320 getrlibpath(), R_OK);
321 if (fpath == NULL) {
322 sprintf(errmsg,
323 "cannot find file '%s'",
324 argv[i]);
325 error(USER, errmsg);
326 }
327 fcompile(fpath);
328 continue;
329 }
330 if (argv[i][2] == 'o') {
331 force_open = 1;
332 continue;
333 }
334 setformat(argv[i]+2);
335 continue;
336 case 'e': /* expression */
337 if (argv[i][2] || i >= argc-2) break;
338 scompile(argv[++i], NULL, 0);
339 continue;
340 case 'o': /* output file spec. */
341 if (argv[i][2] || i >= argc-2) break;
342 curout = argv[++i];
343 continue;
344 case 'x': /* horiz. output resolution */
345 if (argv[i][2] || i >= argc-2) break;
346 xres = atoi(argv[++i]);
347 continue;
348 case 'y': /* vert. output resolution */
349 if (argv[i][2] || i >= argc-2) break;
350 yres = atoi(argv[++i]);
351 continue;
352 case 'b': /* bin expression/count */
353 if (i >= argc-2) break;
354 if (argv[i][2] == 'n') {
355 bincnt = atoi(argv[++i]);
356 continue;
357 }
358 if (argv[i][2]) break;
359 binval = argv[++i];
360 continue;
361 case 'm': /* modifier name */
362 if (argv[i][2] || i >= argc-2) break;
363 rtargv[rtargc++] = "-ti";
364 rtargv[rtargc++] = argv[++i];
365 addmodifier(argv[i], curout, binval, bincnt);
366 continue;
367 case 'M': /* modifier file */
368 if (argv[i][2] || i >= argc-2) break;
369 rtargv[rtargc++] = "-tI";
370 rtargv[rtargc++] = argv[++i];
371 addmodfile(argv[i], curout, binval, bincnt);
372 continue;
373 case 'P': /* persist file */
374 if (i >= argc-2) break;
375 persist_state = (argv[i][2] == 'P') ?
376 PERSIST_PARALL : PERSIST_SINGLE;
377 rtargv[rtargc++] = argv[i];
378 rtargv[rtargc++] = argv[++i];
379 continue;
380 }
381 rtargv[rtargc++] = argv[i]; /* assume rtrace option */
382 }
383 if (accumulate) /* no output flushing for single record */
384 xres = yres = 0;
385 /* set global argument list */
386 gargc = argc; gargv = argv;
387 /* add "mandatory" rtrace settings */
388 for (j = 0; myrtopts[j] != NULL; j++)
389 rtargv[rtargc++] = myrtopts[j];
390 rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
391 /* just asking for defaults? */
392 if (!strcmp(argv[i], "-defaults")) {
393 char sxres[16], syres[16];
394 char *rtpath;
395 printf("-n %-2d\t\t\t\t# number of processes\n", nprocs);
396 printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
397 contrib ? "contributions" : "coefficients");
398 printf("-c%c\t\t\t\t# %s\n", accumulate ? '+' : '-',
399 accumulate ? "accumulate ray values" :
400 "one output record per ray");
401 fflush(stdout); /* report OUR options */
402 rtargv[rtargc++] = header ? "-h+" : "-h-";
403 sprintf(fmt, "-f%c%c", inpfmt, outfmt);
404 rtargv[rtargc++] = fmt;
405 rtargv[rtargc++] = "-x";
406 sprintf(sxres, "%d", xres);
407 rtargv[rtargc++] = sxres;
408 rtargv[rtargc++] = "-y";
409 sprintf(syres, "%d", yres);
410 rtargv[rtargc++] = syres;
411 rtargv[rtargc++] = "-defaults";
412 rtargv[rtargc] = NULL;
413 rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
414 if (rtpath == NULL) {
415 eputs(rtargv[0]);
416 eputs(": command not found\n");
417 exit(1);
418 }
419 execv(rtpath, rtargv);
420 perror(rtpath); /* execv() should not return */
421 exit(1);
422 }
423 if (nprocs > 1) { /* add persist file if parallel */
424 if (persist_state == PERSIST_SINGLE)
425 error(USER, "use -PP option for multiple processes");
426 if (persist_state == PERSIST_NONE) {
427 rtargv[rtargc++] = "-PP";
428 rtargv[rtargc++] = mktemp(persistfn);
429 persist_state = PERSIST_OURS;
430 }
431 }
432 /* add format string */
433 sprintf(fmt, "-f%cf", inpfmt);
434 rtargv[rtargc++] = fmt;
435 /* octree argument is last */
436 if (i <= 0 || i != argc-1 || argv[i][0] == '-')
437 error(USER, "missing octree argument");
438 rtargv[rtargc++] = octree = argv[i];
439 rtargv[rtargc] = NULL;
440 /* start rtrace & recover if requested */
441 init(nprocs);
442 /* compute contributions */
443 trace_contribs(stdin);
444 /* clean up */
445 quit(0);
446 }
447
448 #ifndef SIGALRM
449 #define SIGALRM SIGTERM
450 #endif
451 /* kill persistent rtrace process */
452 static void
453 killpersist(void)
454 {
455 FILE *fp = fopen(persistfn, "r");
456 RT_PID pid;
457
458 if (fp == NULL)
459 return;
460 if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
461 unlink(persistfn);
462 fclose(fp);
463 }
464
465 /* close rtrace processes and clean up */
466 int
467 done_rprocs(struct rtproc *rtp)
468 {
469 int st0, st1 = 0;
470
471 if (rtp->next != NULL) { /* close last opened first! */
472 st1 = done_rprocs(rtp->next);
473 free((void *)rtp->next);
474 rtp->next = NULL;
475 }
476 st0 = close_process(&rtp->pd);
477 if (st0 < 0)
478 error(WARNING, "unknown return status from rtrace process");
479 else if (st0 > 0)
480 return(st0);
481 return(st1);
482 }
483
484 /* exit with status */
485 void
486 quit(int status)
487 {
488 int rtstat;
489
490 if (persist_state == PERSIST_OURS) /* terminate waiting rtrace */
491 killpersist();
492 /* clean up rtrace process(es) */
493 rtstat = done_rprocs(&rt0);
494 if (status == 0)
495 status = rtstat;
496 exit(status); /* flushes all output streams */
497 }
498
499 /* start rtrace processes and initialize */
500 void
501 init(int np)
502 {
503 struct rtproc *rtp;
504 int i;
505 int maxbytes;
506 /* make sure we have something to do */
507 if (!nmods)
508 error(USER, "No modifiers specified");
509 /* assign ray variables */
510 scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
511 scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0);
512 /* set up signal handling */
513 signal(SIGINT, quit);
514 #ifdef SIGHUP
515 signal(SIGHUP, quit);
516 #endif
517 #ifdef SIGTERM
518 signal(SIGTERM, quit);
519 #endif
520 #ifdef SIGPIPE
521 signal(SIGPIPE, quit);
522 #endif
523 rtp = &rt0; /* start rtrace process(es) */
524 for (i = 0; i++ < np; ) {
525 errno = 0;
526 maxbytes = open_process(&rtp->pd, rtargv);
527 if (maxbytes == 0) {
528 eputs(rtargv[0]);
529 eputs(": command not found\n");
530 exit(1);
531 }
532 if (maxbytes < 0)
533 error(SYSTEM, "cannot start rtrace process");
534 if (maxbytes > treebufsiz)
535 treebufsiz = maxbytes;
536 rtp->raynum = 0;
537 rtp->bsiz = 0;
538 rtp->buf = NULL;
539 rtp->nbr = 0;
540 if (i == np) /* last process? */
541 break;
542 if (i == 1)
543 sleep(2); /* wait for persist file */
544 rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc));
545 if (rtp->next == NULL)
546 error(SYSTEM, "out of memory in init");
547 rtp = rtp->next;
548 }
549 rtp->next = NULL; /* terminate list */
550 if (yres > 0) {
551 if (xres > 0)
552 raysleft = (unsigned long)xres*yres;
553 else
554 raysleft = yres;
555 } else
556 raysleft = 0;
557 waitflush = xres;
558 if (!recover)
559 return;
560 /* recover previous values */
561 if (accumulate)
562 reload_output();
563 else
564 recover_output(stdin);
565 }
566
567 /* grow modifier to accommodate more bins */
568 MODCONT *
569 growmodifier(MODCONT *mp, int nb)
570 {
571 if (nb <= mp->nbins)
572 return mp;
573 mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1));
574 if (mp == NULL)
575 error(SYSTEM, "out of memory in growmodifier");
576 memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins));
577 mp->nbins = nb;
578 return mp;
579 }
580
581 /* add modifier to our list to track */
582 MODCONT *
583 addmodifier(char *modn, char *outf, char *binv, int bincnt)
584 {
585 LUENT *lep = lu_find(&modconttab, modn);
586 MODCONT *mp;
587 int i;
588
589 if (lep->data != NULL) {
590 sprintf(errmsg, "duplicate modifier '%s'", modn);
591 error(USER, errmsg);
592 }
593 if (nmods >= MAXMODLIST)
594 error(USER, "too many modifiers");
595 modname[nmods++] = modn; /* XXX assumes static string */
596 lep->key = modn; /* XXX assumes static string */
597 mp = (MODCONT *)malloc(sizeof(MODCONT));
598 if (mp == NULL)
599 error(SYSTEM, "out of memory in addmodifier");
600 mp->outspec = outf; /* XXX assumes static string */
601 mp->modname = modn; /* XXX assumes static string */
602 if (binv != NULL)
603 mp->binv = eparse(binv);
604 else
605 mp->binv = eparse("0");
606 mp->nbins = 1;
607 setcolor(mp->cbin[0], 0., 0., 0.);
608 if (mp->binv->type == NUM) /* assume one bin if constant */
609 bincnt = 1;
610 else if (bincnt > 1)
611 mp = growmodifier(mp, bincnt);
612 lep->data = (char *)mp;
613 /* allocate output streams */
614 for (i = bincnt; i-- > 0; )
615 getostream(mp->outspec, mp->modname, i, 1);
616 return mp;
617 }
618
619 /* add modifiers from a file list */
620 void
621 addmodfile(char *fname, char *outf, char *binv, int bincnt)
622 {
623 char *mname[MAXMODLIST];
624 int i;
625 /* find the file & store strings */
626 if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
627 sprintf(errmsg, "cannot find modifier file '%s'", fname);
628 error(SYSTEM, errmsg);
629 }
630 for (i = 0; mname[i]; i++) /* add each one */
631 addmodifier(mname[i], outf, binv, bincnt);
632 }
633
634 /* put string to stderr */
635 void
636 eputs(char *s)
637 {
638 static int midline = 0;
639
640 if (!*s) return;
641 if (!midline) {
642 fputs(progname, stderr);
643 fputs(": ", stderr);
644 }
645 fputs(s, stderr);
646 midline = s[strlen(s)-1] != '\n';
647 }
648
649 /* construct output file name and return flags whether modifier/bin present */
650 int
651 ofname(char *oname, const char *ospec, const char *mname, int bn)
652 {
653 const char *mnp = NULL;
654 const char *bnp = NULL;
655 const char *cp;
656
657 if (ospec == NULL)
658 return -1;
659 for (cp = ospec; *cp; cp++) /* check format position(s) */
660 if (*cp == '%') {
661 do
662 ++cp;
663 while (isdigit(*cp));
664 switch (*cp) {
665 case '%':
666 break;
667 case 's':
668 if (mnp != NULL)
669 return -1;
670 mnp = cp;
671 break;
672 case 'd':
673 if (bnp != NULL)
674 return -1;
675 bnp = cp;
676 break;
677 default:
678 return -1;
679 }
680 }
681 if (mnp != NULL) { /* create file name */
682 if (bnp != NULL) {
683 if (bnp > mnp)
684 sprintf(oname, ospec, mname, bn);
685 else
686 sprintf(oname, ospec, bn, mname);
687 return OF_MODIFIER|OF_BIN;
688 }
689 sprintf(oname, ospec, mname);
690 return OF_MODIFIER;
691 }
692 if (bnp != NULL) {
693 sprintf(oname, ospec, bn);
694 return OF_BIN;
695 }
696 strcpy(oname, ospec);
697 return 0;
698 }
699
700 /* write header to the given output stream */
701 void
702 printheader(FILE *fout, const char *info)
703 {
704 extern char VersionID[];
705 FILE *fin = fopen(octree, "r");
706
707 if (fin == NULL)
708 quit(1);
709 checkheader(fin, "ignore", fout); /* copy octree header */
710 fclose(fin);
711 printargs(gargc-1, gargv, fout); /* add our command */
712 fprintf(fout, "SOFTWARE= %s\n", VersionID);
713 fputnow(fout);
714 if (info != NULL) /* add extra info if given */
715 fputs(info, fout);
716 switch (outfmt) { /* add output format */
717 case 'a':
718 fputformat("ascii", fout);
719 break;
720 case 'f':
721 fputformat("float", fout);
722 break;
723 case 'd':
724 fputformat("double", fout);
725 break;
726 case 'c':
727 fputformat(COLRFMT, fout);
728 break;
729 }
730 fputc('\n', fout);
731 }
732
733 /* write resolution string to given output stream */
734 void
735 printresolu(FILE *fout, int xr, int yr)
736 {
737 if ((xr > 0) & (yr > 0)) /* resolution string */
738 fprtresolu(xr, yr, fout);
739 if (xres > 0) /* global flush flag */
740 fflush(fout);
741 }
742
743 /* Get output stream pointer (open and write header if new and noopen==0) */
744 STREAMOUT *
745 getostream(const char *ospec, const char *mname, int bn, int noopen)
746 {
747 static const DCOLOR nocontrib = BLKCOLOR;
748 static STREAMOUT stdos;
749 int ofl;
750 char oname[1024];
751 LUENT *lep;
752 STREAMOUT *sop;
753
754 if (ospec == NULL) { /* use stdout? */
755 if (!noopen && !using_stdout) {
756 if (outfmt != 'a')
757 SET_FILE_BINARY(stdout);
758 if (header)
759 printheader(stdout, NULL);
760 printresolu(stdout, xres, yres);
761 stdos.xr = xres; stdos.yr = yres;
762 using_stdout = 1;
763 }
764 stdos.ofp = stdout;
765 stdos.reclen += noopen;
766 return &stdos;
767 }
768 ofl = ofname(oname, ospec, mname, bn); /* get output name */
769 if (ofl < 0) {
770 sprintf(errmsg, "bad output format '%s'", ospec);
771 error(USER, errmsg);
772 }
773 lep = lu_find(&ofiletab, oname); /* look it up */
774 if (lep->key == NULL) /* new entry */
775 lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
776 sop = (STREAMOUT *)lep->data;
777 if (sop == NULL) { /* allocate stream */
778 sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
779 if (sop == NULL)
780 error(SYSTEM, "out of memory in getostream");
781 sop->outpipe = oname[0] == '!';
782 sop->reclen = 0;
783 sop->ofp = NULL; /* open iff noopen==0 */
784 sop->xr = xres; sop->yr = yres;
785 lep->data = (char *)sop;
786 if (!sop->outpipe & !force_open & !recover &&
787 access(oname, F_OK) == 0) {
788 errno = EEXIST; /* file exists */
789 goto openerr;
790 }
791 }
792 if (!noopen && sop->ofp == NULL) { /* open output stream */
793 long i;
794 if (oname[0] == '!') /* output to command */
795 sop->ofp = popen(oname+1, "w");
796 else /* else open file */
797 sop->ofp = fopen(oname, "w");
798 if (sop->ofp == NULL)
799 goto openerr;
800 if (outfmt != 'a')
801 SET_FILE_BINARY(sop->ofp);
802 if (header) {
803 char info[512];
804 char *cp = info;
805 if (ofl & OF_MODIFIER || sop->reclen == 1) {
806 sprintf(cp, "MODIFIER=%s\n", mname);
807 while (*cp) ++cp;
808 }
809 if (ofl & OF_BIN) {
810 sprintf(cp, "BIN=%d\n", bn);
811 while (*cp) ++cp;
812 }
813 *cp = '\0';
814 printheader(sop->ofp, info);
815 }
816 printresolu(sop->ofp, sop->xr, sop->yr);
817 /* play catch-up */
818 for (i = accumulate ? 0 : lastdone; i--; ) {
819 int j = sop->reclen;
820 if (j <= 0) j = 1;
821 while (j--)
822 put_contrib(nocontrib, sop->ofp);
823 if (outfmt == 'a')
824 putc('\n', sop->ofp);
825 }
826 if (xres > 0)
827 fflush(sop->ofp);
828 }
829 sop->reclen += noopen; /* add to length if noopen */
830 return sop; /* return output stream */
831 openerr:
832 sprintf(errmsg, "cannot open '%s' for writing", oname);
833 error(SYSTEM, errmsg);
834 return NULL; /* pro forma return */
835 }
836
837 /* read input ray into buffer */
838 int
839 getinp(char *buf, FILE *fp)
840 {
841 double dv[3], *dvp;
842 float *fvp;
843 char *cp;
844 int i;
845
846 switch (inpfmt) {
847 case 'a':
848 cp = buf; /* make sure we get 6 floats */
849 for (i = 0; i < 6; i++) {
850 if (fgetword(cp, buf+127-cp, fp) == NULL)
851 return -1;
852 if (i >= 3)
853 dv[i-3] = atof(cp);
854 if ((cp = fskip(cp)) == NULL || *cp)
855 return -1;
856 *cp++ = ' ';
857 }
858 getc(fp); /* get/put eol */
859 *cp-- = '\0'; *cp = '\n';
860 if (DOT(dv,dv) <= FTINY*FTINY)
861 return 0; /* dummy ray */
862 return strlen(buf);
863 case 'f':
864 if (fread(buf, sizeof(float), 6, fp) < 6)
865 return -1;
866 fvp = (float *)buf + 3;
867 if (DOT(fvp,fvp) <= FTINY*FTINY)
868 return 0; /* dummy ray */
869 return sizeof(float)*6;
870 case 'd':
871 if (fread(buf, sizeof(double), 6, fp) < 6)
872 return -1;
873 dvp = (double *)buf + 3;
874 if (DOT(dvp,dvp) <= FTINY*FTINY)
875 return 0; /* dummy ray */
876 return sizeof(double)*6;
877 }
878 error(INTERNAL, "botched input format");
879 return -1; /* pro forma return */
880 }
881
882 static float rparams[9]; /* traced ray parameters */
883
884 /* return channel (ray) value */
885 double
886 chanvalue(int n)
887 {
888 if (--n < 0 || n >= 6)
889 error(USER, "illegal channel number ($N)");
890 return rparams[n+3];
891 }
892
893 /* add current ray contribution to the appropriate modifier bin */
894 void
895 add_contrib(const char *modn)
896 {
897 LUENT *le = lu_find(&modconttab, modn);
898 MODCONT *mp = (MODCONT *)le->data;
899 int bn;
900
901 if (mp == NULL) {
902 sprintf(errmsg, "unexpected modifier '%s' from rtrace", modn);
903 error(USER, errmsg);
904 }
905 eclock++; /* get bin number */
906 bn = (int)(evalue(mp->binv) + .5);
907 if (bn <= 0)
908 bn = 0;
909 else if (bn >= mp->nbins) /* new bin */
910 le->data = (char *)(mp = growmodifier(mp, bn+1));
911 addcolor(mp->cbin[bn], rparams);
912 }
913
914 /* output newline to ASCII file and/or flush as requested */
915 static int
916 puteol(const LUENT *e, void *p)
917 {
918 STREAMOUT *sop = (STREAMOUT *)e->data;
919
920 if (outfmt == 'a')
921 putc('\n', sop->ofp);
922 if (!waitflush)
923 fflush(sop->ofp);
924 if (ferror(sop->ofp)) {
925 sprintf(errmsg, "write error on file '%s'", e->key);
926 error(SYSTEM, errmsg);
927 }
928 return 0;
929 }
930
931 /* put out ray contribution to file */
932 void
933 put_contrib(const DCOLOR cnt, FILE *fout)
934 {
935 float fv[3];
936 COLR cv;
937
938 switch (outfmt) {
939 case 'a':
940 fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
941 break;
942 case 'f':
943 copycolor(fv, cnt);
944 fwrite(fv, sizeof(float), 3, fout);
945 break;
946 case 'd':
947 fwrite(cnt, sizeof(double), 3, fout);
948 break;
949 case 'c':
950 setcolr(cv, cnt[0], cnt[1], cnt[2]);
951 fwrite(cv, sizeof(cv), 1, fout);
952 break;
953 default:
954 error(INTERNAL, "botched output format");
955 }
956 }
957
958 /* output ray tallies and clear for next accumulation */
959 void
960 done_contrib(void)
961 {
962 int i, j;
963 MODCONT *mp;
964 STREAMOUT *sop;
965 /* output modifiers in order */
966 for (i = 0; i < nmods; i++) {
967 mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
968 sop = getostream(mp->outspec, mp->modname, 0,0);
969 put_contrib(mp->cbin[0], sop->ofp);
970 if (mp->nbins > 3 && /* minor optimization */
971 sop == getostream(mp->outspec, mp->modname, 1,0))
972 for (j = 1; j < mp->nbins; j++)
973 put_contrib(mp->cbin[j], sop->ofp);
974 else
975 for (j = 1; j < mp->nbins; j++)
976 put_contrib(mp->cbin[j],
977 getostream(mp->outspec,mp->modname,j,0)->ofp);
978 /* clear for next time */
979 memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
980 }
981 --waitflush; /* terminate records */
982 lu_doall(&ofiletab, puteol, NULL);
983 if (using_stdout & (outfmt == 'a'))
984 putc('\n', stdout);
985 if (!waitflush) {
986 waitflush = xres;
987 if (using_stdout)
988 fflush(stdout);
989 }
990 }
991
992 /* queue completed ray tree produced by rtrace process */
993 void
994 queue_raytree(struct rtproc *rtp)
995 {
996 struct rtproc *rtu, *rtl = NULL;
997 /* insert following ray order */
998 for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
999 if (rtp->raynum < rtu->raynum)
1000 break;
1001 rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
1002 if (rtu == NULL)
1003 error(SYSTEM, "out of memory in queue_raytree");
1004 *rtu = *rtp;
1005 if (rtl == NULL) {
1006 rtu->next = rt_unproc;
1007 rt_unproc = rtu;
1008 } else {
1009 rtu->next = rtl->next;
1010 rtl->next = rtu;
1011 }
1012 rtp->raynum = 0; /* clear path for next ray tree */
1013 rtp->bsiz = 0;
1014 rtp->buf = NULL;
1015 rtp->nbr = 0;
1016 }
1017
1018 /* process completed ray trees from our queue */
1019 void
1020 process_queue(void)
1021 {
1022 char modname[128];
1023 /* ray-ordered queue */
1024 while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
1025 struct rtproc *rtp = rt_unproc;
1026 int n = rtp->nbr;
1027 const char *cp = rtp->buf;
1028 while (n > 0) { /* process rays */
1029 register char *mnp = modname;
1030 /* skip leading tabs */
1031 while (n > 0 && *cp == '\t') {
1032 cp++; n--;
1033 }
1034 if (!n || !(isalpha(*cp) | (*cp == '_')))
1035 error(USER, "bad modifier name from rtrace");
1036 /* get modifier name */
1037 while (n > 1 && *cp != '\t') {
1038 if (mnp - modname >= sizeof(modname)-2)
1039 error(INTERNAL, "modifier name too long");
1040 *mnp++ = *cp++; n--;
1041 }
1042 *mnp = '\0';
1043 cp++; n--; /* eat following tab */
1044 if (n < (int)(sizeof(float)*9))
1045 error(USER, "incomplete ray value from rtrace");
1046 /* add ray contribution */
1047 memcpy(rparams, cp, sizeof(float)*9);
1048 cp += sizeof(float)*9; n -= sizeof(float)*9;
1049 add_contrib(modname);
1050 }
1051 if (!accumulate)
1052 done_contrib(); /* sum up contributions & output */
1053 lastdone = rtp->raynum;
1054 if (rtp->buf != NULL) /* free up buffer space */
1055 free(rtp->buf);
1056 rt_unproc = rtp->next;
1057 free(rtp); /* done with this ray tree */
1058 }
1059 }
1060
1061 /* wait for rtrace process to finish with ray tree */
1062 struct rtproc *
1063 wait_rproc(void)
1064 {
1065 struct rtproc *rtfree = NULL;
1066 fd_set readset, errset;
1067 int nr;
1068 struct rtproc *rt;
1069 int n;
1070
1071 do {
1072 nr = 0; /* prepare select call */
1073 FD_ZERO(&readset); FD_ZERO(&errset); n = 0;
1074 for (rt = &rt0; rt != NULL; rt = rt->next) {
1075 if (rt->raynum) {
1076 FD_SET(rt->pd.r, &readset);
1077 ++nr;
1078 }
1079 FD_SET(rt->pd.r, &errset);
1080 if (rt->pd.r >= n)
1081 n = rt->pd.r + 1;
1082 }
1083 if (!nr) /* no rays pending */
1084 break;
1085 if (nr > 1) { /* call select for multiple proc's */
1086 errno = 0;
1087 if (select(n, &readset, NULL, &errset, NULL) < 0)
1088 error(SYSTEM, "select call error in wait_rproc()");
1089 } else
1090 FD_ZERO(&errset);
1091 nr = 0;
1092 for (rt = &rt0; rt != NULL; rt = rt->next) {
1093 if (!FD_ISSET(rt->pd.r, &readset) &&
1094 !FD_ISSET(rt->pd.r, &errset))
1095 continue;
1096 if (rt->buf == NULL) {
1097 rt->bsiz = treebufsiz;
1098 rt->buf = (char *)malloc(treebufsiz);
1099 } else if (rt->nbr + BUFSIZ > rt->bsiz) {
1100 if (rt->bsiz + BUFSIZ <= treebufsiz)
1101 rt->bsiz = treebufsiz;
1102 else
1103 treebufsiz = rt->bsiz += BUFSIZ;
1104 rt->buf = (char *)realloc(rt->buf, rt->bsiz);
1105 }
1106 if (rt->buf == NULL)
1107 error(SYSTEM, "out of memory in wait_rproc");
1108 nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
1109 if (nr <= 0)
1110 error(USER, "rtrace process died");
1111 rt->nbr += nr; /* advance & check */
1112 if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
1113 "~\t~\t", 4)) {
1114 rt->nbr -= 4; /* elide terminator */
1115 queue_raytree(rt);
1116 rtfree = rt; /* ready for next ray */
1117 }
1118 }
1119 } while ((rtfree == NULL) & (nr > 0)); /* repeat until ready or out */
1120 return rtfree;
1121 }
1122
1123 /* return next available rtrace process */
1124 struct rtproc *
1125 get_rproc(void)
1126 {
1127 struct rtproc *rtp;
1128 /* check for idle rtrace */
1129 for (rtp = &rt0; rtp != NULL; rtp = rtp->next)
1130 if (!rtp->raynum)
1131 return rtp;
1132 return wait_rproc(); /* need to wait for one */
1133 }
1134
1135 /* trace ray contributions (main loop) */
1136 void
1137 trace_contribs(FILE *fin)
1138 {
1139 char inpbuf[128];
1140 int iblen;
1141 struct rtproc *rtp;
1142 /* loop over input */
1143 while ((iblen = getinp(inpbuf, fin)) >= 0) {
1144 if (!iblen || /* need reset? */
1145 queue_length() > 10*nrtprocs() ||
1146 lastray+1 < lastray) {
1147 while (wait_rproc() != NULL)
1148 process_queue();
1149 if (lastray+1 < lastray)
1150 lastdone = lastray = 0;
1151 }
1152 rtp = get_rproc(); /* get avail. rtrace process */
1153 rtp->raynum = ++lastray; /* assign ray */
1154 if (iblen) { /* trace ray if valid */
1155 writebuf(rtp->pd.w, inpbuf, iblen);
1156 } else { /* else bypass dummy ray */
1157 queue_raytree(rtp); /* empty tree */
1158 if ((yres <= 0) | (waitflush > 1))
1159 waitflush = 1; /* flush after this */
1160 }
1161 process_queue(); /* catch up with results */
1162 if (raysleft && !--raysleft)
1163 break; /* preemptive EOI */
1164 }
1165 while (wait_rproc() != NULL) /* process outstanding rays */
1166 process_queue();
1167 if (accumulate)
1168 done_contrib(); /* output tallies */
1169 if (raysleft)
1170 error(USER, "unexpected EOF on input");
1171 lu_done(&ofiletab); /* close output files */
1172 }
1173
1174 /* get ray contribution from previous file */
1175 static int
1176 get_contrib(DCOLOR cnt, FILE *finp)
1177 {
1178 COLOR fv;
1179 COLR cv;
1180
1181 switch (outfmt) {
1182 case 'a':
1183 return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
1184 case 'f':
1185 if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
1186 return(0);
1187 copycolor(cnt, fv);
1188 return(1);
1189 case 'd':
1190 return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
1191 case 'c':
1192 if (fread(cv, sizeof(cv), 1, finp) != 1)
1193 return(0);
1194 colr_color(fv, cv);
1195 copycolor(cnt, fv);
1196 return(1);
1197 default:
1198 error(INTERNAL, "botched output format");
1199 }
1200 return(0); /* pro forma return */
1201 }
1202
1203 /* close output file opened for input */
1204 static int
1205 myclose(const LUENT *e, void *p)
1206 {
1207 STREAMOUT *sop = (STREAMOUT *)e->data;
1208
1209 if (sop->ofp == NULL)
1210 return;
1211 fclose(sop->ofp);
1212 sop->ofp = NULL;
1213 }
1214
1215 /* load previously accumulated values */
1216 void
1217 reload_output(void)
1218 {
1219 int i, j;
1220 MODCONT *mp;
1221 int ofl;
1222 char oname[1024];
1223 char *fmode = "rb";
1224 char *outvfmt;
1225 LUENT *ment, *oent;
1226 int xr, yr;
1227 STREAMOUT sout;
1228 DCOLOR rgbv;
1229
1230 switch (outfmt) {
1231 case 'a':
1232 outvfmt = "ascii";
1233 fmode = "r";
1234 break;
1235 case 'f':
1236 outvfmt = "float";
1237 break;
1238 case 'd':
1239 outvfmt = "double";
1240 break;
1241 case 'c':
1242 outvfmt = COLRFMT;
1243 break;
1244 default:
1245 error(INTERNAL, "botched output format");
1246 return;
1247 }
1248 /* reload modifier values */
1249 for (i = 0; i < nmods; i++) {
1250 ment = lu_find(&modconttab,modname[i]);
1251 mp = (MODCONT *)ment->data;
1252 if (mp->outspec == NULL)
1253 error(USER, "cannot reload from stdout");
1254 if (mp->outspec[0] == '!')
1255 error(USER, "cannot reload from command");
1256 for (j = 0; ; j++) { /* load each modifier bin */
1257 ofl = ofname(oname, mp->outspec, mp->modname, j);
1258 if (ofl < 0)
1259 error(USER, "bad output file specification");
1260 oent = lu_find(&ofiletab, oname);
1261 if (oent->data != NULL) {
1262 sout = *(STREAMOUT *)oent->data;
1263 } else {
1264 sout.reclen = 0;
1265 sout.outpipe = 0;
1266 sout.xr = xres; sout.yr = yres;
1267 sout.ofp = NULL;
1268 }
1269 if (sout.ofp == NULL) { /* open output as input */
1270 sout.ofp = fopen(oname, fmode);
1271 if (sout.ofp == NULL) {
1272 if (j)
1273 break; /* assume end of modifier */
1274 sprintf(errmsg, "missing reload file '%s'",
1275 oname);
1276 error(WARNING, errmsg);
1277 break;
1278 }
1279 if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1280 sprintf(errmsg, "format mismatch for '%s'",
1281 oname);
1282 error(USER, errmsg);
1283 }
1284 if ((sout.xr > 0) & (sout.yr > 0) &&
1285 (!fscnresolu(&xr, &yr, sout.ofp) ||
1286 xr != sout.xr ||
1287 yr != sout.yr)) {
1288 sprintf(errmsg, "resolution mismatch for '%s'",
1289 oname);
1290 error(USER, errmsg);
1291 }
1292 }
1293 if (oent->key == NULL) /* new file entry */
1294 oent->key = strcpy((char *)
1295 malloc(strlen(oname)+1), oname);
1296 if (oent->data == NULL)
1297 oent->data = (char *)malloc(sizeof(STREAMOUT));
1298 *(STREAMOUT *)oent->data = sout;
1299 /* read in RGB value */
1300 if (!get_contrib(rgbv, sout.ofp)) {
1301 if (!j)
1302 break; /* ignore empty file */
1303 if (j && j < mp->nbins) {
1304 sprintf(errmsg, "missing data in '%s'",
1305 oname);
1306 error(USER, errmsg);
1307 }
1308 break;
1309 }
1310 if (j >= mp->nbins) /* grow modifier size */
1311 ment->data = (char *)(mp = growmodifier(mp, j+1));
1312 copycolor(mp->cbin[j], rgbv);
1313 }
1314 }
1315 lu_doall(&ofiletab, myclose, NULL); /* close all files */
1316 }
1317
1318 /* seek on the given output file */
1319 static int
1320 myseeko(const LUENT *e, void *p)
1321 {
1322 STREAMOUT *sop = (STREAMOUT *)e->data;
1323 off_t nbytes = *(off_t *)p;
1324
1325 if (sop->reclen > 1)
1326 nbytes = nbytes * sop->reclen;
1327 if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
1328 sprintf(errmsg, "seek error on file '%s'", e->key);
1329 error(SYSTEM, errmsg);
1330 }
1331 return 0;
1332 }
1333
1334 /* recover output if possible */
1335 void
1336 recover_output(FILE *fin)
1337 {
1338 off_t lastout = -1;
1339 int outvsiz, recsiz;
1340 char *outvfmt;
1341 int i, j;
1342 MODCONT *mp;
1343 int ofl;
1344 char oname[1024];
1345 LUENT *ment, *oent;
1346 STREAMOUT sout;
1347 off_t nvals;
1348 int xr, yr;
1349
1350 switch (outfmt) {
1351 case 'a':
1352 error(USER, "cannot recover ASCII output");
1353 return;
1354 case 'f':
1355 outvsiz = sizeof(float)*3;
1356 outvfmt = "float";
1357 break;
1358 case 'd':
1359 outvsiz = sizeof(double)*3;
1360 outvfmt = "double";
1361 break;
1362 case 'c':
1363 outvsiz = sizeof(COLR);
1364 outvfmt = COLRFMT;
1365 break;
1366 default:
1367 error(INTERNAL, "botched output format");
1368 return;
1369 }
1370 /* check modifier outputs */
1371 for (i = 0; i < nmods; i++) {
1372 ment = lu_find(&modconttab,modname[i]);
1373 mp = (MODCONT *)ment->data;
1374 if (mp->outspec == NULL)
1375 error(USER, "cannot recover from stdout");
1376 if (mp->outspec[0] == '!')
1377 error(USER, "cannot recover from command");
1378 for (j = 0; ; j++) { /* check each bin's file */
1379 ofl = ofname(oname, mp->outspec, mp->modname, j);
1380 if (ofl < 0)
1381 error(USER, "bad output file specification");
1382 oent = lu_find(&ofiletab, oname);
1383 if (oent->data != NULL) {
1384 sout = *(STREAMOUT *)oent->data;
1385 } else {
1386 sout.reclen = 0;
1387 sout.outpipe = 0;
1388 sout.xr = xres; sout.yr = yres;
1389 sout.ofp = NULL;
1390 }
1391 if (sout.ofp != NULL) { /* already open? */
1392 if (ofl & OF_BIN)
1393 continue;
1394 break;
1395 }
1396 /* open output */
1397 sout.ofp = fopen(oname, "rb+");
1398 if (sout.ofp == NULL) {
1399 if (j)
1400 break; /* assume end of modifier */
1401 sprintf(errmsg, "missing recover file '%s'",
1402 oname);
1403 error(WARNING, errmsg);
1404 break;
1405 }
1406 nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
1407 if (nvals <= 0) {
1408 lastout = 0; /* empty output, quit here */
1409 fclose(sout.ofp);
1410 break;
1411 }
1412 if (!sout.reclen) {
1413 if (!(ofl & OF_BIN)) {
1414 sprintf(errmsg,
1415 "need -bn to recover file '%s'",
1416 oname);
1417 error(USER, errmsg);
1418 }
1419 recsiz = outvsiz;
1420 } else
1421 recsiz = outvsiz * sout.reclen;
1422
1423 lseek(fileno(sout.ofp), 0, SEEK_SET);
1424 if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
1425 sprintf(errmsg, "format mismatch for '%s'",
1426 oname);
1427 error(USER, errmsg);
1428 }
1429 if ((xres > 0) & (yres > 0) &&
1430 (!fscnresolu(&xr, &yr, sout.ofp) ||
1431 xr != xres ||
1432 yr != yres)) {
1433 sprintf(errmsg, "resolution mismatch for '%s'",
1434 oname);
1435 error(USER, errmsg);
1436 }
1437 nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz;
1438 if ((lastout < 0) | (nvals < lastout))
1439 lastout = nvals;
1440 if (oent->key == NULL) /* new entry */
1441 oent->key = strcpy((char *)
1442 malloc(strlen(oname)+1), oname);
1443 if (oent->data == NULL)
1444 oent->data = (char *)malloc(sizeof(STREAMOUT));
1445 *(STREAMOUT *)oent->data = sout;
1446 if (!(ofl & OF_BIN))
1447 break; /* no bin separation */
1448 }
1449 if (!lastout) { /* empty output */
1450 error(WARNING, "no previous data to recover");
1451 lu_done(&ofiletab); /* reclose all outputs */
1452 return;
1453 }
1454 if (j > mp->nbins) /* reallocate modifier bins */
1455 ment->data = (char *)(mp = growmodifier(mp, j));
1456 }
1457 if (lastout < 0) {
1458 error(WARNING, "no output files to recover");
1459 return;
1460 }
1461 if (raysleft && lastout >= raysleft) {
1462 error(WARNING, "output appears to be complete");
1463 /* XXX should read & discard input? */
1464 quit(0);
1465 }
1466 /* seek on all files */
1467 nvals = lastout * outvsiz;
1468 lu_doall(&ofiletab, myseeko, &nvals);
1469 /* skip repeated input */
1470 for (nvals = 0; nvals < lastout; nvals++)
1471 if (getinp(oname, fin) < 0)
1472 error(USER, "unexpected EOF on input");
1473 lastray = lastdone = (unsigned long)lastout;
1474 if (raysleft)
1475 raysleft -= lastray;
1476 }