16 |
|
#include "lookup.h" |
17 |
|
#include "calcomp.h" |
18 |
|
|
19 |
+ |
#ifndef MAXMODLIST |
20 |
|
#define MAXMODLIST 1024 /* maximum modifiers we'll track */ |
21 |
+ |
#endif |
22 |
|
|
23 |
|
int treebufsiz = BUFSIZ; /* current tree buffer size */ |
24 |
|
|
25 |
|
typedef double DCOLOR[3]; /* double-precision color */ |
26 |
|
|
27 |
|
/* |
28 |
< |
* The modcont structure is used to accumulate ray contributions |
28 |
> |
* The MODCONT structure is used to accumulate ray contributions |
29 |
|
* for a particular modifier, which may be subdivided into bins |
30 |
< |
* if binv is non-NULL. If outspec contains a %s in it, this will |
30 |
> |
* if binv evaluates > 0. If outspec contains a %s in it, this will |
31 |
|
* be replaced with the modifier name. If outspec contains a %d in it, |
32 |
|
* this will be used to create one output file per bin, otherwise all bins |
33 |
|
* will be written to the same file, in order. If the global outfmt |
46 |
|
|
47 |
|
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ |
48 |
|
|
49 |
< |
/* close output stream */ |
50 |
< |
static void closefile(void *p) { fclose((FILE *)p); } |
49 |
> |
/* |
50 |
> |
* The STREAMOUT structure holds an open FILE pointer and a count of |
51 |
> |
* the number of RGB triplets per record, or 0 if unknown. |
52 |
> |
*/ |
53 |
> |
typedef struct { |
54 |
> |
FILE *ofp; /* output file pointer */ |
55 |
> |
int outpipe; /* output is to a pipe */ |
56 |
> |
int reclen; /* triplets/record */ |
57 |
> |
int xr, yr; /* output resolution for picture */ |
58 |
> |
} STREAMOUT; |
59 |
|
|
60 |
< |
LUTAB ofiletab = LU_SINIT(free,closefile); /* output file table */ |
60 |
> |
/* close output stream and free record */ |
61 |
> |
static void |
62 |
> |
closestream(void *p) |
63 |
> |
{ |
64 |
> |
STREAMOUT *sop = (STREAMOUT *)p; |
65 |
> |
int status; |
66 |
> |
if (sop->outpipe) |
67 |
> |
status = pclose(sop->ofp); |
68 |
> |
else |
69 |
> |
status = fclose(sop->ofp); |
70 |
> |
if (status) |
71 |
> |
error(SYSTEM, "error closing output stream"); |
72 |
> |
free(p); |
73 |
> |
} |
74 |
|
|
75 |
< |
FILE *getofile(const char *ospec, const char *mname, int bn); |
75 |
> |
LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ |
76 |
|
|
77 |
+ |
#define OF_MODIFIER 01 |
78 |
+ |
#define OF_BIN 02 |
79 |
+ |
|
80 |
+ |
STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen); |
81 |
+ |
int ofname(char *oname, const char *ospec, const char *mname, int bn); |
82 |
+ |
void printheader(FILE *fout, const char *info); |
83 |
+ |
void printresolu(FILE *fout, int xr, int yr); |
84 |
+ |
|
85 |
|
/* |
86 |
|
* The rcont structure is used to manage i/o with a particular |
87 |
|
* rtrace child process. Input is passed unchanged from stdin, |
98 |
|
}; /* rtrace process buffer */ |
99 |
|
|
100 |
|
/* rtrace command and defaults */ |
101 |
< |
char *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3", |
102 |
< |
"-ab", "1", "-ad", "128", }; |
101 |
> |
char *rtargv[256+2*MAXMODLIST] = { "rtrace", |
102 |
> |
"-dj", ".5", "-dr", "3", |
103 |
> |
"-ab", "1", "-ad", "350", }; |
104 |
> |
|
105 |
|
int rtargc = 9; |
106 |
|
/* overriding rtrace options */ |
107 |
< |
char *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0", |
107 |
> |
char *myrtopts[] = { "-h-", "-x", "1", "-y", "0", |
108 |
|
"-dt", "0", "-as", "0", "-aa", "0", NULL }; |
109 |
|
|
110 |
+ |
#define RTCOEFF "-o~~TmWdp" /* compute coefficients only */ |
111 |
+ |
#define RTCONTRIB "-o~~TmVdp" /* compute ray contributions */ |
112 |
+ |
|
113 |
|
struct rtproc rt0; /* head of rtrace process list */ |
114 |
|
|
115 |
|
struct rtproc *rt_unproc = NULL; /* unprocessed ray trees */ |
116 |
|
|
117 |
< |
char persistfn[] = "pfXXXXXX"; /* persist file name */ |
117 |
> |
#define PERSIST_NONE 0 /* no persist file */ |
118 |
> |
#define PERSIST_SINGLE 1 /* user set -P persist */ |
119 |
> |
#define PERSIST_PARALL 2 /* user set -PP persist */ |
120 |
> |
#define PERSIST_OURS 3 /* -PP persist belongs to us */ |
121 |
> |
int persist_state = PERSIST_NONE; /* persist file state */ |
122 |
> |
char persistfn[] = "pfXXXXXX"; /* our persist file name, if set */ |
123 |
|
|
124 |
|
int gargc; /* global argc */ |
125 |
|
char **gargv; /* global argv */ |
131 |
|
int outfmt = 'a'; /* output format */ |
132 |
|
|
133 |
|
int header = 1; /* output header? */ |
134 |
+ |
int force_open = 0; /* truncate existing output? */ |
135 |
+ |
int recover = 0; /* recover previous output? */ |
136 |
+ |
int accumulate = 1; /* input rays per output record */ |
137 |
|
int xres = 0; /* horiz. output resolution */ |
138 |
|
int yres = 0; /* vert. output resolution */ |
139 |
|
|
140 |
< |
long raysleft; /* number of rays left to trace */ |
140 |
> |
int account; /* current accumulation count */ |
141 |
> |
unsigned long raysleft; /* number of rays left to trace */ |
142 |
|
long waitflush; /* how long until next flush */ |
143 |
|
|
144 |
|
unsigned long lastray = 0; /* last ray number sent */ |
149 |
|
const char *modname[MAXMODLIST]; /* ordered modifier name list */ |
150 |
|
int nmods = 0; /* number of modifiers */ |
151 |
|
|
152 |
< |
MODCONT *addmodifier(char *modn, char *outf, char *binv); |
152 |
> |
#define queue_length() (lastray - lastdone) |
153 |
|
|
154 |
+ |
MODCONT *growmodifier(MODCONT *mp, int nb); |
155 |
+ |
MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt); |
156 |
+ |
void addmodfile(char *fname, char *outf, char *binv, int bincnt); |
157 |
+ |
|
158 |
|
void init(int np); |
159 |
|
int done_rprocs(struct rtproc *rtp); |
160 |
< |
void trace_contribs(FILE *fp); |
160 |
> |
void reload_output(void); |
161 |
> |
void recover_output(FILE *fin); |
162 |
> |
void trace_contribs(FILE *fin); |
163 |
|
struct rtproc *wait_rproc(void); |
164 |
|
struct rtproc *get_rproc(void); |
165 |
|
void queue_raytree(struct rtproc *rtp); |
166 |
|
void process_queue(void); |
167 |
|
|
168 |
< |
void putcontrib(const DCOLOR cnt, FILE *fout); |
168 |
> |
void put_contrib(const DCOLOR cnt, FILE *fout); |
169 |
|
void add_contrib(const char *modn); |
170 |
< |
void done_contrib(void); |
170 |
> |
void done_contrib(int navg); |
171 |
|
|
172 |
+ |
/* return number of open rtrace processes */ |
173 |
+ |
static int |
174 |
+ |
nrtprocs(void) |
175 |
+ |
{ |
176 |
+ |
int nrtp = 0; |
177 |
+ |
struct rtproc *rtp; |
178 |
+ |
|
179 |
+ |
for (rtp = &rt0; rtp != NULL; rtp = rtp->next) |
180 |
+ |
nrtp += rtp->pd.running; |
181 |
+ |
return(nrtp); |
182 |
+ |
} |
183 |
+ |
|
184 |
|
/* set input/output format */ |
185 |
|
static void |
186 |
|
setformat(const char *fmt) |
220 |
|
int |
221 |
|
main(int argc, char *argv[]) |
222 |
|
{ |
223 |
+ |
int contrib = 0; |
224 |
|
int nprocs = 1; |
225 |
|
char *curout = NULL; |
226 |
|
char *binval = NULL; |
227 |
+ |
int bincnt = 0; |
228 |
|
char fmt[8]; |
229 |
|
int i, j; |
230 |
+ |
/* need at least one argument */ |
231 |
+ |
if (argc < 2) { |
232 |
+ |
fprintf(stderr, |
233 |
+ |
"Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n", |
234 |
+ |
argv[0]); |
235 |
+ |
exit(1); |
236 |
+ |
} |
237 |
|
/* global program name */ |
238 |
|
gargv = argv; |
239 |
|
/* initialize calcomp routines */ |
253 |
|
if (argv[i][0] == '-') |
254 |
|
switch (argv[i][1]) { |
255 |
|
case 'n': /* number of processes */ |
256 |
< |
if (argv[i][2] || i >= argc-1) break; |
256 |
> |
if (argv[i][2] || i >= argc-2) break; |
257 |
|
nprocs = atoi(argv[++i]); |
258 |
|
if (nprocs <= 0) |
259 |
|
error(USER, "illegal number of processes"); |
260 |
|
continue; |
261 |
+ |
case 'V': /* output contributions */ |
262 |
+ |
switch (argv[i][2]) { |
263 |
+ |
case '\0': |
264 |
+ |
contrib = !contrib; |
265 |
+ |
continue; |
266 |
+ |
case '+': case '1': |
267 |
+ |
case 'T': case 't': |
268 |
+ |
case 'Y': case 'y': |
269 |
+ |
contrib = 1; |
270 |
+ |
continue; |
271 |
+ |
case '-': case '0': |
272 |
+ |
case 'F': case 'f': |
273 |
+ |
case 'N': case 'n': |
274 |
+ |
contrib = 0; |
275 |
+ |
continue; |
276 |
+ |
} |
277 |
+ |
break; |
278 |
+ |
case 'c': /* input rays per output */ |
279 |
+ |
if (argv[i][2] || i >= argc-2) break; |
280 |
+ |
accumulate = atoi(argv[++i]); |
281 |
+ |
continue; |
282 |
+ |
case 'r': /* recover output */ |
283 |
+ |
if (argv[i][2]) break; |
284 |
+ |
recover = 1; |
285 |
+ |
continue; |
286 |
|
case 'h': /* output header? */ |
287 |
|
switch (argv[i][2]) { |
288 |
|
case '\0': |
300 |
|
continue; |
301 |
|
} |
302 |
|
break; |
303 |
< |
case 'f': /* file or i/o format */ |
303 |
> |
case 'f': /* file or force or format */ |
304 |
|
if (!argv[i][2]) { |
305 |
|
char *fpath; |
306 |
< |
if (i >= argc-1) break; |
306 |
> |
if (i >= argc-2) break; |
307 |
|
fpath = getpath(argv[++i], |
308 |
|
getrlibpath(), R_OK); |
309 |
|
if (fpath == NULL) { |
315 |
|
fcompile(fpath); |
316 |
|
continue; |
317 |
|
} |
318 |
+ |
if (argv[i][2] == 'o') { |
319 |
+ |
force_open = 1; |
320 |
+ |
continue; |
321 |
+ |
} |
322 |
|
setformat(argv[i]+2); |
323 |
|
continue; |
324 |
|
case 'e': /* expression */ |
325 |
< |
if (argv[i][2] || i >= argc-1) break; |
325 |
> |
if (argv[i][2] || i >= argc-2) break; |
326 |
|
scompile(argv[++i], NULL, 0); |
327 |
|
continue; |
328 |
|
case 'o': /* output file spec. */ |
329 |
< |
if (argv[i][2] || i >= argc-1) break; |
329 |
> |
if (argv[i][2] || i >= argc-2) break; |
330 |
|
curout = argv[++i]; |
331 |
|
continue; |
332 |
|
case 'x': /* horiz. output resolution */ |
333 |
< |
if (argv[i][2] || i >= argc-1) break; |
333 |
> |
if (argv[i][2] || i >= argc-2) break; |
334 |
|
xres = atoi(argv[++i]); |
335 |
|
continue; |
336 |
|
case 'y': /* vert. output resolution */ |
337 |
< |
if (argv[i][2] || i >= argc-1) break; |
337 |
> |
if (argv[i][2] || i >= argc-2) break; |
338 |
|
yres = atoi(argv[++i]); |
339 |
|
continue; |
340 |
< |
case 'b': /* bin expression */ |
341 |
< |
if (argv[i][2] || i >= argc-1) break; |
340 |
> |
case 'b': /* bin expression/count */ |
341 |
> |
if (i >= argc-2) break; |
342 |
> |
if (argv[i][2] == 'n') { |
343 |
> |
bincnt = atoi(argv[++i]); |
344 |
> |
continue; |
345 |
> |
} |
346 |
> |
if (argv[i][2]) break; |
347 |
|
binval = argv[++i]; |
348 |
|
continue; |
349 |
|
case 'm': /* modifier name */ |
350 |
< |
if (argv[i][2] || i >= argc-1) break; |
350 |
> |
if (argv[i][2] || i >= argc-2) break; |
351 |
|
rtargv[rtargc++] = "-ti"; |
352 |
|
rtargv[rtargc++] = argv[++i]; |
353 |
< |
addmodifier(argv[i], curout, binval); |
353 |
> |
addmodifier(argv[i], curout, binval, bincnt); |
354 |
|
continue; |
355 |
+ |
case 'M': /* modifier file */ |
356 |
+ |
if (argv[i][2] || i >= argc-2) break; |
357 |
+ |
rtargv[rtargc++] = "-tI"; |
358 |
+ |
rtargv[rtargc++] = argv[++i]; |
359 |
+ |
addmodfile(argv[i], curout, binval, bincnt); |
360 |
+ |
continue; |
361 |
+ |
case 'P': /* persist file */ |
362 |
+ |
if (i >= argc-2) break; |
363 |
+ |
persist_state = (argv[i][2] == 'P') ? |
364 |
+ |
PERSIST_PARALL : PERSIST_SINGLE; |
365 |
+ |
rtargv[rtargc++] = argv[i]; |
366 |
+ |
rtargv[rtargc++] = argv[++i]; |
367 |
+ |
continue; |
368 |
|
} |
369 |
|
rtargv[rtargc++] = argv[i]; /* assume rtrace option */ |
370 |
|
} |
371 |
+ |
if (accumulate <= 0) /* no output flushing for single record */ |
372 |
+ |
xres = yres = 0; |
373 |
|
/* set global argument list */ |
374 |
|
gargc = argc; gargv = argv; |
375 |
|
/* add "mandatory" rtrace settings */ |
376 |
|
for (j = 0; myrtopts[j] != NULL; j++) |
377 |
|
rtargv[rtargc++] = myrtopts[j]; |
378 |
+ |
rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF; |
379 |
|
/* just asking for defaults? */ |
380 |
|
if (!strcmp(argv[i], "-defaults")) { |
381 |
|
char sxres[16], syres[16]; |
382 |
|
char *rtpath; |
383 |
< |
printf("-n %-2d\t\t\t\t# number of processes\n", nprocs); |
383 |
> |
printf("-n %-2d\t\t\t\t# number of processes\n", nprocs); |
384 |
> |
printf("-c %-5d\t\t\t# accumulated rays per record\n", |
385 |
> |
accumulate); |
386 |
> |
printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-', |
387 |
> |
contrib ? "contributions" : "coefficients"); |
388 |
|
fflush(stdout); /* report OUR options */ |
389 |
|
rtargv[rtargc++] = header ? "-h+" : "-h-"; |
390 |
|
sprintf(fmt, "-f%c%c", inpfmt, outfmt); |
395 |
|
rtargv[rtargc++] = "-y"; |
396 |
|
sprintf(syres, "%d", yres); |
397 |
|
rtargv[rtargc++] = syres; |
272 |
– |
rtargv[rtargc++] = "-oTW"; |
398 |
|
rtargv[rtargc++] = "-defaults"; |
399 |
|
rtargv[rtargc] = NULL; |
400 |
|
rtpath = getpath(rtargv[0], getenv("PATH"), X_OK); |
408 |
|
exit(1); |
409 |
|
} |
410 |
|
if (nprocs > 1) { /* add persist file if parallel */ |
411 |
< |
rtargv[rtargc++] = "-PP"; |
412 |
< |
rtargv[rtargc++] = mktemp(persistfn); |
411 |
> |
if (persist_state == PERSIST_SINGLE) |
412 |
> |
error(USER, "use -PP option for multiple processes"); |
413 |
> |
if (persist_state == PERSIST_NONE) { |
414 |
> |
rtargv[rtargc++] = "-PP"; |
415 |
> |
rtargv[rtargc++] = mktemp(persistfn); |
416 |
> |
persist_state = PERSIST_OURS; |
417 |
> |
} |
418 |
|
} |
419 |
|
/* add format string */ |
420 |
|
sprintf(fmt, "-f%cf", inpfmt); |
424 |
|
error(USER, "missing octree argument"); |
425 |
|
rtargv[rtargc++] = octree = argv[i]; |
426 |
|
rtargv[rtargc] = NULL; |
427 |
< |
/* start rtrace & compute contributions */ |
427 |
> |
/* start rtrace & recover if requested */ |
428 |
|
init(nprocs); |
429 |
+ |
/* compute contributions */ |
430 |
|
trace_contribs(stdin); |
431 |
+ |
/* clean up */ |
432 |
|
quit(0); |
433 |
|
} |
434 |
|
|
435 |
+ |
#ifndef SIGALRM |
436 |
+ |
#define SIGALRM SIGTERM |
437 |
+ |
#endif |
438 |
|
/* kill persistent rtrace process */ |
439 |
|
static void |
440 |
|
killpersist(void) |
441 |
|
{ |
442 |
|
FILE *fp = fopen(persistfn, "r"); |
443 |
< |
int pid; |
443 |
> |
RT_PID pid; |
444 |
|
|
445 |
|
if (fp == NULL) |
446 |
|
return; |
474 |
|
{ |
475 |
|
int rtstat; |
476 |
|
|
477 |
< |
if (rt0.next != NULL) /* terminate persistent rtrace */ |
477 |
> |
if (persist_state == PERSIST_OURS) /* terminate waiting rtrace */ |
478 |
|
killpersist(); |
479 |
|
/* clean up rtrace process(es) */ |
480 |
|
rtstat = done_rprocs(&rt0); |
536 |
|
rtp->next = NULL; /* terminate list */ |
537 |
|
if (yres > 0) { |
538 |
|
if (xres > 0) |
539 |
< |
raysleft = xres*yres; |
539 |
> |
raysleft = (unsigned long)xres*yres; |
540 |
|
else |
541 |
|
raysleft = yres; |
542 |
|
} else |
543 |
|
raysleft = 0; |
544 |
+ |
if ((account = accumulate) > 0) |
545 |
+ |
raysleft *= accumulate; |
546 |
|
waitflush = xres; |
547 |
+ |
if (!recover) |
548 |
+ |
return; |
549 |
+ |
/* recover previous values */ |
550 |
+ |
if (accumulate <= 0) |
551 |
+ |
reload_output(); |
552 |
+ |
else |
553 |
+ |
recover_output(stdin); |
554 |
|
} |
555 |
|
|
556 |
+ |
/* grow modifier to accommodate more bins */ |
557 |
+ |
MODCONT * |
558 |
+ |
growmodifier(MODCONT *mp, int nb) |
559 |
+ |
{ |
560 |
+ |
if (nb <= mp->nbins) |
561 |
+ |
return mp; |
562 |
+ |
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1)); |
563 |
+ |
if (mp == NULL) |
564 |
+ |
error(SYSTEM, "out of memory in growmodifier"); |
565 |
+ |
memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins)); |
566 |
+ |
mp->nbins = nb; |
567 |
+ |
return mp; |
568 |
+ |
} |
569 |
+ |
|
570 |
|
/* add modifier to our list to track */ |
571 |
|
MODCONT * |
572 |
< |
addmodifier(char *modn, char *outf, char *binv) |
572 |
> |
addmodifier(char *modn, char *outf, char *binv, int bincnt) |
573 |
|
{ |
574 |
|
LUENT *lep = lu_find(&modconttab, modn); |
575 |
|
MODCONT *mp; |
576 |
+ |
int i; |
577 |
|
|
578 |
|
if (lep->data != NULL) { |
579 |
|
sprintf(errmsg, "duplicate modifier '%s'", modn); |
586 |
|
mp = (MODCONT *)malloc(sizeof(MODCONT)); |
587 |
|
if (mp == NULL) |
588 |
|
error(SYSTEM, "out of memory in addmodifier"); |
430 |
– |
lep->data = (char *)mp; |
589 |
|
mp->outspec = outf; /* XXX assumes static string */ |
590 |
|
mp->modname = modn; /* XXX assumes static string */ |
591 |
|
if (binv != NULL) |
594 |
|
mp->binv = eparse("0"); |
595 |
|
mp->nbins = 1; |
596 |
|
setcolor(mp->cbin[0], 0., 0., 0.); |
597 |
+ |
if (mp->binv->type == NUM) /* assume one bin if constant */ |
598 |
+ |
bincnt = 1; |
599 |
+ |
else if (bincnt > 1) |
600 |
+ |
mp = growmodifier(mp, bincnt); |
601 |
+ |
lep->data = (char *)mp; |
602 |
+ |
/* allocate output streams */ |
603 |
+ |
for (i = bincnt; i-- > 0; ) |
604 |
+ |
getostream(mp->outspec, mp->modname, i, 1); |
605 |
|
return mp; |
606 |
|
} |
607 |
|
|
608 |
+ |
/* add modifiers from a file list */ |
609 |
+ |
void |
610 |
+ |
addmodfile(char *fname, char *outf, char *binv, int bincnt) |
611 |
+ |
{ |
612 |
+ |
char *mname[MAXMODLIST]; |
613 |
+ |
int i; |
614 |
+ |
/* find the file & store strings */ |
615 |
+ |
if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { |
616 |
+ |
sprintf(errmsg, "cannot find modifier file '%s'", fname); |
617 |
+ |
error(SYSTEM, errmsg); |
618 |
+ |
} |
619 |
+ |
for (i = 0; mname[i]; i++) /* add each one */ |
620 |
+ |
addmodifier(mname[i], outf, binv, bincnt); |
621 |
+ |
} |
622 |
+ |
|
623 |
|
/* put string to stderr */ |
624 |
|
void |
625 |
|
eputs(char *s) |
635 |
|
midline = s[strlen(s)-1] != '\n'; |
636 |
|
} |
637 |
|
|
638 |
+ |
/* construct output file name and return flags whether modifier/bin present */ |
639 |
+ |
int |
640 |
+ |
ofname(char *oname, const char *ospec, const char *mname, int bn) |
641 |
+ |
{ |
642 |
+ |
const char *mnp = NULL; |
643 |
+ |
const char *bnp = NULL; |
644 |
+ |
const char *cp; |
645 |
+ |
|
646 |
+ |
if (ospec == NULL) |
647 |
+ |
return -1; |
648 |
+ |
for (cp = ospec; *cp; cp++) /* check format position(s) */ |
649 |
+ |
if (*cp == '%') { |
650 |
+ |
do |
651 |
+ |
++cp; |
652 |
+ |
while (isdigit(*cp)); |
653 |
+ |
switch (*cp) { |
654 |
+ |
case '%': |
655 |
+ |
break; |
656 |
+ |
case 's': |
657 |
+ |
if (mnp != NULL) |
658 |
+ |
return -1; |
659 |
+ |
mnp = cp; |
660 |
+ |
break; |
661 |
+ |
case 'd': |
662 |
+ |
if (bnp != NULL) |
663 |
+ |
return -1; |
664 |
+ |
bnp = cp; |
665 |
+ |
break; |
666 |
+ |
default: |
667 |
+ |
return -1; |
668 |
+ |
} |
669 |
+ |
} |
670 |
+ |
if (mnp != NULL) { /* create file name */ |
671 |
+ |
if (bnp != NULL) { |
672 |
+ |
if (bnp > mnp) |
673 |
+ |
sprintf(oname, ospec, mname, bn); |
674 |
+ |
else |
675 |
+ |
sprintf(oname, ospec, bn, mname); |
676 |
+ |
return OF_MODIFIER|OF_BIN; |
677 |
+ |
} |
678 |
+ |
sprintf(oname, ospec, mname); |
679 |
+ |
return OF_MODIFIER; |
680 |
+ |
} |
681 |
+ |
if (bnp != NULL) { |
682 |
+ |
sprintf(oname, ospec, bn); |
683 |
+ |
return OF_BIN; |
684 |
+ |
} |
685 |
+ |
strcpy(oname, ospec); |
686 |
+ |
return 0; |
687 |
+ |
} |
688 |
+ |
|
689 |
|
/* write header to the given output stream */ |
690 |
|
void |
691 |
< |
printheader(FILE *fout) |
691 |
> |
printheader(FILE *fout, const char *info) |
692 |
|
{ |
693 |
|
extern char VersionID[]; |
694 |
|
FILE *fin = fopen(octree, "r"); |
700 |
|
printargs(gargc-1, gargv, fout); /* add our command */ |
701 |
|
fprintf(fout, "SOFTWARE= %s\n", VersionID); |
702 |
|
fputnow(fout); |
703 |
+ |
if (info != NULL) /* add extra info if given */ |
704 |
+ |
fputs(info, fout); |
705 |
|
switch (outfmt) { /* add output format */ |
706 |
|
case 'a': |
707 |
|
fputformat("ascii", fout); |
717 |
|
break; |
718 |
|
} |
719 |
|
fputc('\n', fout); |
720 |
< |
if (xres > 0) { |
721 |
< |
if (yres > 0) /* resolution string */ |
722 |
< |
fprtresolu(xres, yres, fout); |
720 |
> |
} |
721 |
> |
|
722 |
> |
/* write resolution string to given output stream */ |
723 |
> |
void |
724 |
> |
printresolu(FILE *fout, int xr, int yr) |
725 |
> |
{ |
726 |
> |
if ((xr > 0) & (yr > 0)) /* resolution string */ |
727 |
> |
fprtresolu(xr, yr, fout); |
728 |
> |
if (xres > 0) /* global flush flag */ |
729 |
|
fflush(fout); |
490 |
– |
} |
730 |
|
} |
731 |
|
|
732 |
< |
/* Get output file pointer (open and write header if new) */ |
733 |
< |
FILE * |
734 |
< |
getofile(const char *ospec, const char *mname, int bn) |
732 |
> |
/* Get output stream pointer (open and write header if new and noopen==0) */ |
733 |
> |
STREAMOUT * |
734 |
> |
getostream(const char *ospec, const char *mname, int bn, int noopen) |
735 |
|
{ |
736 |
< |
const char *mnp = NULL; |
737 |
< |
const char *bnp = NULL; |
738 |
< |
const char *cp; |
739 |
< |
char ofname[1024]; |
740 |
< |
LUENT *lep; |
736 |
> |
static const DCOLOR nocontrib = BLKCOLOR; |
737 |
> |
static STREAMOUT stdos; |
738 |
> |
int ofl; |
739 |
> |
char oname[1024]; |
740 |
> |
LUENT *lep; |
741 |
> |
STREAMOUT *sop; |
742 |
|
|
743 |
|
if (ospec == NULL) { /* use stdout? */ |
744 |
< |
if (!using_stdout) { |
744 |
> |
if (!noopen && !using_stdout) { |
745 |
|
if (outfmt != 'a') |
746 |
|
SET_FILE_BINARY(stdout); |
747 |
|
if (header) |
748 |
< |
printheader(stdout); |
748 |
> |
printheader(stdout, NULL); |
749 |
> |
printresolu(stdout, xres, yres); |
750 |
> |
stdos.xr = xres; stdos.yr = yres; |
751 |
> |
using_stdout = 1; |
752 |
|
} |
753 |
< |
using_stdout = 1; |
754 |
< |
return stdout; |
753 |
> |
stdos.ofp = stdout; |
754 |
> |
stdos.reclen += noopen; |
755 |
> |
return &stdos; |
756 |
|
} |
757 |
< |
for (cp = ospec; *cp; cp++) /* check format position(s) */ |
758 |
< |
if (*cp == '%') { |
759 |
< |
do |
760 |
< |
++cp; |
761 |
< |
while (isdigit(*cp)); |
762 |
< |
switch (*cp) { |
519 |
< |
case '%': |
520 |
< |
break; |
521 |
< |
case 's': |
522 |
< |
if (mnp != NULL) |
523 |
< |
goto badspec; |
524 |
< |
mnp = cp; |
525 |
< |
break; |
526 |
< |
case 'd': |
527 |
< |
if (bnp != NULL) |
528 |
< |
goto badspec; |
529 |
< |
bnp = cp; |
530 |
< |
break; |
531 |
< |
default: |
532 |
< |
goto badspec; |
533 |
< |
} |
534 |
< |
} |
535 |
< |
if (mnp != NULL) { /* create file name */ |
536 |
< |
if (bnp != NULL) { |
537 |
< |
if (bnp > mnp) |
538 |
< |
sprintf(ofname, ospec, mname, bn); |
539 |
< |
else |
540 |
< |
sprintf(ofname, ospec, bn, mname); |
541 |
< |
} else |
542 |
< |
sprintf(ofname, ospec, mname); |
543 |
< |
} else if (bnp != NULL) |
544 |
< |
sprintf(ofname, ospec, bn); |
545 |
< |
else |
546 |
< |
strcpy(ofname, ospec); |
547 |
< |
lep = lu_find(&ofiletab, ofname); /* look it up */ |
757 |
> |
ofl = ofname(oname, ospec, mname, bn); /* get output name */ |
758 |
> |
if (ofl < 0) { |
759 |
> |
sprintf(errmsg, "bad output format '%s'", ospec); |
760 |
> |
error(USER, errmsg); |
761 |
> |
} |
762 |
> |
lep = lu_find(&ofiletab, oname); /* look it up */ |
763 |
|
if (lep->key == NULL) /* new entry */ |
764 |
< |
lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname); |
765 |
< |
if (lep->data == NULL) { /* open output file */ |
766 |
< |
FILE *fp; |
767 |
< |
int i; |
768 |
< |
if (ofname[0] == '!') /* output to command */ |
769 |
< |
fp = popen(ofname+1, "w"); |
770 |
< |
else |
771 |
< |
fp = fopen(ofname, "w"); |
772 |
< |
if (fp == NULL) { |
773 |
< |
sprintf(errmsg, "cannot open '%s' for writing", ofname); |
774 |
< |
error(SYSTEM, errmsg); |
764 |
> |
lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); |
765 |
> |
sop = (STREAMOUT *)lep->data; |
766 |
> |
if (sop == NULL) { /* allocate stream */ |
767 |
> |
sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); |
768 |
> |
if (sop == NULL) |
769 |
> |
error(SYSTEM, "out of memory in getostream"); |
770 |
> |
sop->outpipe = oname[0] == '!'; |
771 |
> |
sop->reclen = 0; |
772 |
> |
sop->ofp = NULL; /* open iff noopen==0 */ |
773 |
> |
sop->xr = xres; sop->yr = yres; |
774 |
> |
lep->data = (char *)sop; |
775 |
> |
if (!sop->outpipe & !force_open & !recover && |
776 |
> |
access(oname, F_OK) == 0) { |
777 |
> |
errno = EEXIST; /* file exists */ |
778 |
> |
goto openerr; |
779 |
|
} |
780 |
+ |
} |
781 |
+ |
if (!noopen && sop->ofp == NULL) { /* open output stream */ |
782 |
+ |
long i; |
783 |
+ |
if (oname[0] == '!') /* output to command */ |
784 |
+ |
sop->ofp = popen(oname+1, "w"); |
785 |
+ |
else /* else open file */ |
786 |
+ |
sop->ofp = fopen(oname, "w"); |
787 |
+ |
if (sop->ofp == NULL) |
788 |
+ |
goto openerr; |
789 |
|
if (outfmt != 'a') |
790 |
< |
SET_FILE_BINARY(fp); |
791 |
< |
if (header) |
792 |
< |
printheader(fp); |
790 |
> |
SET_FILE_BINARY(sop->ofp); |
791 |
> |
if (header) { |
792 |
> |
char info[512]; |
793 |
> |
char *cp = info; |
794 |
> |
if (ofl & OF_MODIFIER || sop->reclen == 1) { |
795 |
> |
sprintf(cp, "MODIFIER=%s\n", mname); |
796 |
> |
while (*cp) ++cp; |
797 |
> |
} |
798 |
> |
if (ofl & OF_BIN) { |
799 |
> |
sprintf(cp, "BIN=%d\n", bn); |
800 |
> |
while (*cp) ++cp; |
801 |
> |
} |
802 |
> |
*cp = '\0'; |
803 |
> |
printheader(sop->ofp, info); |
804 |
> |
} |
805 |
> |
if (accumulate > 0) { /* global resolution */ |
806 |
> |
sop->xr = xres; sop->yr = yres; |
807 |
> |
} |
808 |
> |
printresolu(sop->ofp, sop->xr, sop->yr); |
809 |
|
/* play catch-up */ |
810 |
< |
for (i = 0; i < lastdone; i++) { |
811 |
< |
static const DCOLOR nocontrib = BLKCOLOR; |
812 |
< |
putcontrib(nocontrib, fp); |
810 |
> |
for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) { |
811 |
> |
int j = sop->reclen; |
812 |
> |
if (j <= 0) j = 1; |
813 |
> |
while (j--) |
814 |
> |
put_contrib(nocontrib, sop->ofp); |
815 |
|
if (outfmt == 'a') |
816 |
< |
putc('\n', fp); |
816 |
> |
putc('\n', sop->ofp); |
817 |
|
} |
818 |
|
if (xres > 0) |
819 |
< |
fflush(fp); |
574 |
< |
lep->data = (char *)fp; |
819 |
> |
fflush(sop->ofp); |
820 |
|
} |
821 |
< |
return (FILE *)lep->data; /* return open file pointer */ |
822 |
< |
badspec: |
823 |
< |
sprintf(errmsg, "bad output format '%s'", ospec); |
824 |
< |
error(USER, errmsg); |
825 |
< |
return NULL; /* pro forma return */ |
821 |
> |
sop->reclen += noopen; /* add to length if noopen */ |
822 |
> |
return sop; /* return output stream */ |
823 |
> |
openerr: |
824 |
> |
sprintf(errmsg, "cannot open '%s' for writing", oname); |
825 |
> |
error(SYSTEM, errmsg); |
826 |
> |
return NULL; /* pro forma return */ |
827 |
|
} |
828 |
|
|
829 |
|
/* read input ray into buffer */ |
830 |
|
int |
831 |
|
getinp(char *buf, FILE *fp) |
832 |
|
{ |
833 |
+ |
double dv[3], *dvp; |
834 |
+ |
float *fvp; |
835 |
|
char *cp; |
836 |
|
int i; |
837 |
|
|
840 |
|
cp = buf; /* make sure we get 6 floats */ |
841 |
|
for (i = 0; i < 6; i++) { |
842 |
|
if (fgetword(cp, buf+127-cp, fp) == NULL) |
843 |
< |
return 0; |
843 |
> |
return -1; |
844 |
> |
if (i >= 3) |
845 |
> |
dv[i-3] = atof(cp); |
846 |
|
if ((cp = fskip(cp)) == NULL || *cp) |
847 |
< |
return 0; |
847 |
> |
return -1; |
848 |
|
*cp++ = ' '; |
849 |
|
} |
850 |
|
getc(fp); /* get/put eol */ |
851 |
|
*cp-- = '\0'; *cp = '\n'; |
852 |
+ |
if (DOT(dv,dv) <= FTINY*FTINY) |
853 |
+ |
return 0; /* dummy ray */ |
854 |
|
return strlen(buf); |
855 |
|
case 'f': |
856 |
|
if (fread(buf, sizeof(float), 6, fp) < 6) |
857 |
< |
return 0; |
857 |
> |
return -1; |
858 |
> |
fvp = (float *)buf + 3; |
859 |
> |
if (DOT(fvp,fvp) <= FTINY*FTINY) |
860 |
> |
return 0; /* dummy ray */ |
861 |
|
return sizeof(float)*6; |
862 |
|
case 'd': |
863 |
|
if (fread(buf, sizeof(double), 6, fp) < 6) |
864 |
< |
return 0; |
864 |
> |
return -1; |
865 |
> |
dvp = (double *)buf + 3; |
866 |
> |
if (DOT(dvp,dvp) <= FTINY*FTINY) |
867 |
> |
return 0; /* dummy ray */ |
868 |
|
return sizeof(double)*6; |
869 |
|
} |
870 |
|
error(INTERNAL, "botched input format"); |
871 |
< |
return 0; /* pro forma return */ |
871 |
> |
return -1; /* pro forma return */ |
872 |
|
} |
873 |
|
|
874 |
|
static float rparams[9]; /* traced ray parameters */ |
898 |
|
bn = (int)(evalue(mp->binv) + .5); |
899 |
|
if (bn <= 0) |
900 |
|
bn = 0; |
901 |
< |
else if (bn > mp->nbins) { /* new bin */ |
902 |
< |
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + |
645 |
< |
bn*sizeof(DCOLOR)); |
646 |
< |
if (mp == NULL) |
647 |
< |
error(SYSTEM, "out of memory in add_contrib"); |
648 |
< |
memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins)); |
649 |
< |
mp->nbins = bn+1; |
650 |
< |
le->data = (char *)mp; |
651 |
< |
} |
901 |
> |
else if (bn >= mp->nbins) /* new bin */ |
902 |
> |
le->data = (char *)(mp = growmodifier(mp, bn+1)); |
903 |
|
addcolor(mp->cbin[bn], rparams); |
904 |
|
} |
905 |
|
|
907 |
|
static int |
908 |
|
puteol(const LUENT *e, void *p) |
909 |
|
{ |
910 |
< |
FILE *fp = (FILE *)e->data; |
910 |
> |
STREAMOUT *sop = (STREAMOUT *)e->data; |
911 |
|
|
912 |
|
if (outfmt == 'a') |
913 |
< |
putc('\n', fp); |
913 |
> |
putc('\n', sop->ofp); |
914 |
|
if (!waitflush) |
915 |
< |
fflush(fp); |
916 |
< |
if (ferror(fp)) { |
915 |
> |
fflush(sop->ofp); |
916 |
> |
if (ferror(sop->ofp)) { |
917 |
|
sprintf(errmsg, "write error on file '%s'", e->key); |
918 |
|
error(SYSTEM, errmsg); |
919 |
|
} |
922 |
|
|
923 |
|
/* put out ray contribution to file */ |
924 |
|
void |
925 |
< |
putcontrib(const DCOLOR cnt, FILE *fout) |
925 |
> |
put_contrib(const DCOLOR cnt, FILE *fout) |
926 |
|
{ |
927 |
|
float fv[3]; |
928 |
|
COLR cv; |
932 |
|
fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]); |
933 |
|
break; |
934 |
|
case 'f': |
935 |
< |
fv[0] = cnt[0]; |
685 |
< |
fv[1] = cnt[1]; |
686 |
< |
fv[2] = cnt[2]; |
935 |
> |
copycolor(fv, cnt); |
936 |
|
fwrite(fv, sizeof(float), 3, fout); |
937 |
|
break; |
938 |
|
case 'd': |
947 |
|
} |
948 |
|
} |
949 |
|
|
950 |
< |
/* output ray tallies and clear for next primary */ |
950 |
> |
/* output ray tallies and clear for next accumulation */ |
951 |
|
void |
952 |
< |
done_contrib(void) |
952 |
> |
done_contrib(int navg) |
953 |
|
{ |
954 |
< |
int i, j; |
955 |
< |
MODCONT *mp; |
954 |
> |
double sf = 1.; |
955 |
> |
int i, j; |
956 |
> |
MODCONT *mp; |
957 |
> |
STREAMOUT *sop; |
958 |
> |
/* set average scaling */ |
959 |
> |
if (navg > 1) |
960 |
> |
sf = 1. / (double)navg; |
961 |
|
/* output modifiers in order */ |
962 |
|
for (i = 0; i < nmods; i++) { |
709 |
– |
FILE *fp; |
963 |
|
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
964 |
< |
fp = getofile(mp->outspec, mp->modname, 0); |
965 |
< |
putcontrib(mp->cbin[0], fp); |
964 |
> |
if (navg > 1) /* average scaling */ |
965 |
> |
for (j = mp->nbins; j--; ) |
966 |
> |
scalecolor(mp->cbin[j], sf); |
967 |
> |
sop = getostream(mp->outspec, mp->modname, 0,0); |
968 |
> |
put_contrib(mp->cbin[0], sop->ofp); |
969 |
|
if (mp->nbins > 3 && /* minor optimization */ |
970 |
< |
fp == getofile(mp->outspec, mp->modname, 1)) |
970 |
> |
sop == getostream(mp->outspec, mp->modname, 1,0)) |
971 |
|
for (j = 1; j < mp->nbins; j++) |
972 |
< |
putcontrib(mp->cbin[j], fp); |
972 |
> |
put_contrib(mp->cbin[j], sop->ofp); |
973 |
|
else |
974 |
|
for (j = 1; j < mp->nbins; j++) |
975 |
< |
putcontrib(mp->cbin[j], |
976 |
< |
getofile(mp->outspec, mp->modname, j)); |
977 |
< |
/* clear for next ray tree */ |
975 |
> |
put_contrib(mp->cbin[j], |
976 |
> |
getostream(mp->outspec,mp->modname,j,0)->ofp); |
977 |
> |
/* clear for next time */ |
978 |
|
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
979 |
|
} |
980 |
|
--waitflush; /* terminate records */ |
1033 |
|
if (!n || !(isalpha(*cp) | (*cp == '_'))) |
1034 |
|
error(USER, "bad modifier name from rtrace"); |
1035 |
|
/* get modifier name */ |
1036 |
< |
while (n > 0 && *cp != '\t') { |
1036 |
> |
while (n > 1 && *cp != '\t') { |
1037 |
> |
if (mnp - modname >= sizeof(modname)-2) |
1038 |
> |
error(INTERNAL, "modifier name too long"); |
1039 |
|
*mnp++ = *cp++; n--; |
1040 |
|
} |
1041 |
|
*mnp = '\0'; |
1047 |
|
cp += sizeof(float)*9; n -= sizeof(float)*9; |
1048 |
|
add_contrib(modname); |
1049 |
|
} |
1050 |
< |
done_contrib(); /* sum up contributions & output */ |
1050 |
> |
/* time to produce record? */ |
1051 |
> |
if (account > 0 && !--account) |
1052 |
> |
done_contrib(account = accumulate); |
1053 |
|
lastdone = rtp->raynum; |
1054 |
< |
free(rtp->buf); /* free up buffer space */ |
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 |
|
} |
1100 |
|
if (rt->bsiz + BUFSIZ <= treebufsiz) |
1101 |
|
rt->bsiz = treebufsiz; |
1102 |
|
else |
1103 |
< |
rt->bsiz = treebufsiz += BUFSIZ; |
1103 |
> |
treebufsiz = rt->bsiz += BUFSIZ; |
1104 |
|
rt->buf = (char *)realloc(rt->buf, rt->bsiz); |
1105 |
|
} |
1106 |
|
if (rt->buf == NULL) |
1136 |
|
void |
1137 |
|
trace_contribs(FILE *fin) |
1138 |
|
{ |
1139 |
+ |
static int ignore_warning_given = 0; |
1140 |
|
char inpbuf[128]; |
1141 |
|
int iblen; |
1142 |
|
struct rtproc *rtp; |
1143 |
|
/* loop over input */ |
1144 |
< |
while ((iblen = getinp(inpbuf, fin)) > 0) { |
1145 |
< |
if (lastray+1 < lastray) { /* counter rollover? */ |
1144 |
> |
while ((iblen = getinp(inpbuf, fin)) >= 0) { |
1145 |
> |
if (!iblen && accumulate != 1) { |
1146 |
> |
if (!ignore_warning_given++) |
1147 |
> |
error(WARNING, |
1148 |
> |
"dummy ray(s) ignored during accumulation\n"); |
1149 |
> |
continue; |
1150 |
> |
} |
1151 |
> |
if (!iblen || /* need reset? */ |
1152 |
> |
queue_length() > 10*nrtprocs() || |
1153 |
> |
lastray+1 < lastray) { |
1154 |
|
while (wait_rproc() != NULL) |
1155 |
|
process_queue(); |
1156 |
< |
lastdone = lastray = 0; |
1156 |
> |
if (lastray+1 < lastray) |
1157 |
> |
lastdone = lastray = 0; |
1158 |
|
} |
1159 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
1160 |
< |
rtp->raynum = ++lastray; /* assign ray to it */ |
1161 |
< |
writebuf(rtp->pd.w, inpbuf, iblen); |
1162 |
< |
if (!--raysleft) |
1163 |
< |
break; |
1160 |
> |
rtp->raynum = ++lastray; /* assign ray */ |
1161 |
> |
if (iblen) { /* trace ray if valid */ |
1162 |
> |
writebuf(rtp->pd.w, inpbuf, iblen); |
1163 |
> |
} else { /* else bypass dummy ray */ |
1164 |
> |
queue_raytree(rtp); /* empty tree */ |
1165 |
> |
if ((yres <= 0) | (waitflush > 1)) |
1166 |
> |
waitflush = 1; /* flush after this */ |
1167 |
> |
} |
1168 |
|
process_queue(); /* catch up with results */ |
1169 |
+ |
if (raysleft && !--raysleft) |
1170 |
+ |
break; /* preemptive EOI */ |
1171 |
|
} |
1172 |
|
while (wait_rproc() != NULL) /* process outstanding rays */ |
1173 |
|
process_queue(); |
1174 |
< |
if (raysleft > 0) |
1174 |
> |
if (accumulate <= 0) |
1175 |
> |
done_contrib(0); /* output tallies */ |
1176 |
> |
else if (account < accumulate) { |
1177 |
> |
error(WARNING, "partial accumulation in final record"); |
1178 |
> |
done_contrib(accumulate - account); |
1179 |
> |
} |
1180 |
> |
if (raysleft) |
1181 |
|
error(USER, "unexpected EOF on input"); |
1182 |
+ |
lu_done(&ofiletab); /* close output files */ |
1183 |
+ |
} |
1184 |
+ |
|
1185 |
+ |
/* get ray contribution from previous file */ |
1186 |
+ |
static int |
1187 |
+ |
get_contrib(DCOLOR cnt, FILE *finp) |
1188 |
+ |
{ |
1189 |
+ |
COLOR fv; |
1190 |
+ |
COLR cv; |
1191 |
+ |
|
1192 |
+ |
switch (outfmt) { |
1193 |
+ |
case 'a': |
1194 |
+ |
return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3); |
1195 |
+ |
case 'f': |
1196 |
+ |
if (fread(fv, sizeof(fv[0]), 3, finp) != 3) |
1197 |
+ |
return(0); |
1198 |
+ |
copycolor(cnt, fv); |
1199 |
+ |
return(1); |
1200 |
+ |
case 'd': |
1201 |
+ |
return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3); |
1202 |
+ |
case 'c': |
1203 |
+ |
if (fread(cv, sizeof(cv), 1, finp) != 1) |
1204 |
+ |
return(0); |
1205 |
+ |
colr_color(fv, cv); |
1206 |
+ |
copycolor(cnt, fv); |
1207 |
+ |
return(1); |
1208 |
+ |
default: |
1209 |
+ |
error(INTERNAL, "botched output format"); |
1210 |
+ |
} |
1211 |
+ |
return(0); /* pro forma return */ |
1212 |
+ |
} |
1213 |
+ |
|
1214 |
+ |
/* close output file opened for input */ |
1215 |
+ |
static int |
1216 |
+ |
myclose(const LUENT *e, void *p) |
1217 |
+ |
{ |
1218 |
+ |
STREAMOUT *sop = (STREAMOUT *)e->data; |
1219 |
+ |
|
1220 |
+ |
if (sop->ofp == NULL) |
1221 |
+ |
return; |
1222 |
+ |
fclose(sop->ofp); |
1223 |
+ |
sop->ofp = NULL; |
1224 |
+ |
} |
1225 |
+ |
|
1226 |
+ |
/* load previously accumulated values */ |
1227 |
+ |
void |
1228 |
+ |
reload_output(void) |
1229 |
+ |
{ |
1230 |
+ |
int i, j; |
1231 |
+ |
MODCONT *mp; |
1232 |
+ |
int ofl; |
1233 |
+ |
char oname[1024]; |
1234 |
+ |
char *fmode = "rb"; |
1235 |
+ |
char *outvfmt; |
1236 |
+ |
LUENT *ment, *oent; |
1237 |
+ |
int xr, yr; |
1238 |
+ |
STREAMOUT sout; |
1239 |
+ |
DCOLOR rgbv; |
1240 |
+ |
|
1241 |
+ |
switch (outfmt) { |
1242 |
+ |
case 'a': |
1243 |
+ |
outvfmt = "ascii"; |
1244 |
+ |
fmode = "r"; |
1245 |
+ |
break; |
1246 |
+ |
case 'f': |
1247 |
+ |
outvfmt = "float"; |
1248 |
+ |
break; |
1249 |
+ |
case 'd': |
1250 |
+ |
outvfmt = "double"; |
1251 |
+ |
break; |
1252 |
+ |
case 'c': |
1253 |
+ |
outvfmt = COLRFMT; |
1254 |
+ |
break; |
1255 |
+ |
default: |
1256 |
+ |
error(INTERNAL, "botched output format"); |
1257 |
+ |
return; |
1258 |
+ |
} |
1259 |
+ |
/* reload modifier values */ |
1260 |
+ |
for (i = 0; i < nmods; i++) { |
1261 |
+ |
ment = lu_find(&modconttab,modname[i]); |
1262 |
+ |
mp = (MODCONT *)ment->data; |
1263 |
+ |
if (mp->outspec == NULL) |
1264 |
+ |
error(USER, "cannot reload from stdout"); |
1265 |
+ |
if (mp->outspec[0] == '!') |
1266 |
+ |
error(USER, "cannot reload from command"); |
1267 |
+ |
for (j = 0; ; j++) { /* load each modifier bin */ |
1268 |
+ |
ofl = ofname(oname, mp->outspec, mp->modname, j); |
1269 |
+ |
if (ofl < 0) |
1270 |
+ |
error(USER, "bad output file specification"); |
1271 |
+ |
oent = lu_find(&ofiletab, oname); |
1272 |
+ |
if (oent->data != NULL) { |
1273 |
+ |
sout = *(STREAMOUT *)oent->data; |
1274 |
+ |
} else { |
1275 |
+ |
sout.reclen = 0; |
1276 |
+ |
sout.outpipe = 0; |
1277 |
+ |
sout.xr = xres; sout.yr = yres; |
1278 |
+ |
sout.ofp = NULL; |
1279 |
+ |
} |
1280 |
+ |
if (sout.ofp == NULL) { /* open output as input */ |
1281 |
+ |
sout.ofp = fopen(oname, fmode); |
1282 |
+ |
if (sout.ofp == NULL) { |
1283 |
+ |
if (j) |
1284 |
+ |
break; /* assume end of modifier */ |
1285 |
+ |
sprintf(errmsg, "missing reload file '%s'", |
1286 |
+ |
oname); |
1287 |
+ |
error(WARNING, errmsg); |
1288 |
+ |
break; |
1289 |
+ |
} |
1290 |
+ |
if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { |
1291 |
+ |
sprintf(errmsg, "format mismatch for '%s'", |
1292 |
+ |
oname); |
1293 |
+ |
error(USER, errmsg); |
1294 |
+ |
} |
1295 |
+ |
if ((sout.xr > 0) & (sout.yr > 0) && |
1296 |
+ |
(!fscnresolu(&xr, &yr, sout.ofp) || |
1297 |
+ |
(xr != sout.xr) | |
1298 |
+ |
(yr != sout.yr))) { |
1299 |
+ |
sprintf(errmsg, "resolution mismatch for '%s'", |
1300 |
+ |
oname); |
1301 |
+ |
error(USER, errmsg); |
1302 |
+ |
} |
1303 |
+ |
} |
1304 |
+ |
/* read in RGB value */ |
1305 |
+ |
if (!get_contrib(rgbv, sout.ofp)) { |
1306 |
+ |
if (!j) { |
1307 |
+ |
fclose(sout.ofp); |
1308 |
+ |
break; /* ignore empty file */ |
1309 |
+ |
} |
1310 |
+ |
if (j < mp->nbins) { |
1311 |
+ |
sprintf(errmsg, "missing data in '%s'", |
1312 |
+ |
oname); |
1313 |
+ |
error(USER, errmsg); |
1314 |
+ |
} |
1315 |
+ |
break; |
1316 |
+ |
} |
1317 |
+ |
if (j >= mp->nbins) /* grow modifier size */ |
1318 |
+ |
ment->data = (char *)(mp = growmodifier(mp, j+1)); |
1319 |
+ |
copycolor(mp->cbin[j], rgbv); |
1320 |
+ |
if (oent->key == NULL) /* new file entry */ |
1321 |
+ |
oent->key = strcpy((char *) |
1322 |
+ |
malloc(strlen(oname)+1), oname); |
1323 |
+ |
if (oent->data == NULL) |
1324 |
+ |
oent->data = (char *)malloc(sizeof(STREAMOUT)); |
1325 |
+ |
*(STREAMOUT *)oent->data = sout; |
1326 |
+ |
} |
1327 |
+ |
} |
1328 |
+ |
lu_doall(&ofiletab, myclose, NULL); /* close all files */ |
1329 |
+ |
} |
1330 |
+ |
|
1331 |
+ |
/* seek on the given output file */ |
1332 |
+ |
static int |
1333 |
+ |
myseeko(const LUENT *e, void *p) |
1334 |
+ |
{ |
1335 |
+ |
STREAMOUT *sop = (STREAMOUT *)e->data; |
1336 |
+ |
off_t nbytes = *(off_t *)p; |
1337 |
+ |
|
1338 |
+ |
if (sop->reclen > 1) |
1339 |
+ |
nbytes = nbytes * sop->reclen; |
1340 |
+ |
if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) { |
1341 |
+ |
sprintf(errmsg, "seek error on file '%s'", e->key); |
1342 |
+ |
error(SYSTEM, errmsg); |
1343 |
+ |
} |
1344 |
+ |
return 0; |
1345 |
+ |
} |
1346 |
+ |
|
1347 |
+ |
/* recover output if possible */ |
1348 |
+ |
void |
1349 |
+ |
recover_output(FILE *fin) |
1350 |
+ |
{ |
1351 |
+ |
off_t lastout = -1; |
1352 |
+ |
int outvsiz, recsiz; |
1353 |
+ |
char *outvfmt; |
1354 |
+ |
int i, j; |
1355 |
+ |
MODCONT *mp; |
1356 |
+ |
int ofl; |
1357 |
+ |
char oname[1024]; |
1358 |
+ |
LUENT *ment, *oent; |
1359 |
+ |
STREAMOUT sout; |
1360 |
+ |
off_t nvals; |
1361 |
+ |
int xr, yr; |
1362 |
+ |
|
1363 |
+ |
switch (outfmt) { |
1364 |
+ |
case 'a': |
1365 |
+ |
error(USER, "cannot recover ASCII output"); |
1366 |
+ |
return; |
1367 |
+ |
case 'f': |
1368 |
+ |
outvsiz = sizeof(float)*3; |
1369 |
+ |
outvfmt = "float"; |
1370 |
+ |
break; |
1371 |
+ |
case 'd': |
1372 |
+ |
outvsiz = sizeof(double)*3; |
1373 |
+ |
outvfmt = "double"; |
1374 |
+ |
break; |
1375 |
+ |
case 'c': |
1376 |
+ |
outvsiz = sizeof(COLR); |
1377 |
+ |
outvfmt = COLRFMT; |
1378 |
+ |
break; |
1379 |
+ |
default: |
1380 |
+ |
error(INTERNAL, "botched output format"); |
1381 |
+ |
return; |
1382 |
+ |
} |
1383 |
+ |
/* check modifier outputs */ |
1384 |
+ |
for (i = 0; i < nmods; i++) { |
1385 |
+ |
ment = lu_find(&modconttab,modname[i]); |
1386 |
+ |
mp = (MODCONT *)ment->data; |
1387 |
+ |
if (mp->outspec == NULL) |
1388 |
+ |
error(USER, "cannot recover from stdout"); |
1389 |
+ |
if (mp->outspec[0] == '!') |
1390 |
+ |
error(USER, "cannot recover from command"); |
1391 |
+ |
for (j = 0; ; j++) { /* check each bin's file */ |
1392 |
+ |
ofl = ofname(oname, mp->outspec, mp->modname, j); |
1393 |
+ |
if (ofl < 0) |
1394 |
+ |
error(USER, "bad output file specification"); |
1395 |
+ |
oent = lu_find(&ofiletab, oname); |
1396 |
+ |
if (oent->data != NULL) { |
1397 |
+ |
sout = *(STREAMOUT *)oent->data; |
1398 |
+ |
} else { |
1399 |
+ |
sout.reclen = 0; |
1400 |
+ |
sout.outpipe = 0; |
1401 |
+ |
sout.ofp = NULL; |
1402 |
+ |
} |
1403 |
+ |
if (sout.ofp != NULL) { /* already open? */ |
1404 |
+ |
if (ofl & OF_BIN) |
1405 |
+ |
continue; |
1406 |
+ |
break; |
1407 |
+ |
} |
1408 |
+ |
/* open output */ |
1409 |
+ |
sout.ofp = fopen(oname, "rb+"); |
1410 |
+ |
if (sout.ofp == NULL) { |
1411 |
+ |
if (j) |
1412 |
+ |
break; /* assume end of modifier */ |
1413 |
+ |
sprintf(errmsg, "missing recover file '%s'", |
1414 |
+ |
oname); |
1415 |
+ |
error(WARNING, errmsg); |
1416 |
+ |
break; |
1417 |
+ |
} |
1418 |
+ |
nvals = lseek(fileno(sout.ofp), 0, SEEK_END); |
1419 |
+ |
if (nvals <= 0) { |
1420 |
+ |
lastout = 0; /* empty output, quit here */ |
1421 |
+ |
fclose(sout.ofp); |
1422 |
+ |
break; |
1423 |
+ |
} |
1424 |
+ |
if (!sout.reclen) { |
1425 |
+ |
if (!(ofl & OF_BIN)) { |
1426 |
+ |
sprintf(errmsg, |
1427 |
+ |
"need -bn to recover file '%s'", |
1428 |
+ |
oname); |
1429 |
+ |
error(USER, errmsg); |
1430 |
+ |
} |
1431 |
+ |
recsiz = outvsiz; |
1432 |
+ |
} else |
1433 |
+ |
recsiz = outvsiz * sout.reclen; |
1434 |
+ |
|
1435 |
+ |
lseek(fileno(sout.ofp), 0, SEEK_SET); |
1436 |
+ |
if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { |
1437 |
+ |
sprintf(errmsg, "format mismatch for '%s'", |
1438 |
+ |
oname); |
1439 |
+ |
error(USER, errmsg); |
1440 |
+ |
} |
1441 |
+ |
sout.xr = xres; sout.yr = yres; |
1442 |
+ |
if ((sout.xr > 0) & (sout.yr > 0) && |
1443 |
+ |
(!fscnresolu(&xr, &yr, sout.ofp) || |
1444 |
+ |
(xr != sout.xr) | |
1445 |
+ |
(yr != sout.yr))) { |
1446 |
+ |
sprintf(errmsg, "resolution mismatch for '%s'", |
1447 |
+ |
oname); |
1448 |
+ |
error(USER, errmsg); |
1449 |
+ |
} |
1450 |
+ |
nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz; |
1451 |
+ |
if ((lastout < 0) | (nvals < lastout)) |
1452 |
+ |
lastout = nvals; |
1453 |
+ |
if (oent->key == NULL) /* new entry */ |
1454 |
+ |
oent->key = strcpy((char *) |
1455 |
+ |
malloc(strlen(oname)+1), oname); |
1456 |
+ |
if (oent->data == NULL) |
1457 |
+ |
oent->data = (char *)malloc(sizeof(STREAMOUT)); |
1458 |
+ |
*(STREAMOUT *)oent->data = sout; |
1459 |
+ |
if (!(ofl & OF_BIN)) |
1460 |
+ |
break; /* no bin separation */ |
1461 |
+ |
} |
1462 |
+ |
if (!lastout) { /* empty output */ |
1463 |
+ |
error(WARNING, "no previous data to recover"); |
1464 |
+ |
lu_done(&ofiletab); /* reclose all outputs */ |
1465 |
+ |
return; |
1466 |
+ |
} |
1467 |
+ |
if (j > mp->nbins) /* reallocate modifier bins */ |
1468 |
+ |
ment->data = (char *)(mp = growmodifier(mp, j)); |
1469 |
+ |
} |
1470 |
+ |
if (lastout < 0) { |
1471 |
+ |
error(WARNING, "no output files to recover"); |
1472 |
+ |
return; |
1473 |
+ |
} |
1474 |
+ |
if (raysleft && lastout >= raysleft/accumulate) { |
1475 |
+ |
error(WARNING, "output appears to be complete"); |
1476 |
+ |
/* XXX should read & discard input? */ |
1477 |
+ |
quit(0); |
1478 |
+ |
} |
1479 |
+ |
/* seek on all files */ |
1480 |
+ |
nvals = lastout * outvsiz; |
1481 |
+ |
lu_doall(&ofiletab, myseeko, &nvals); |
1482 |
+ |
/* skip repeated input */ |
1483 |
+ |
for (nvals = 0; nvals < lastout; nvals++) |
1484 |
+ |
if (getinp(oname, fin) < 0) |
1485 |
+ |
error(USER, "unexpected EOF on input"); |
1486 |
+ |
lastray = lastdone = (unsigned long)lastout * accumulate; |
1487 |
+ |
if (raysleft) |
1488 |
+ |
raysleft -= lastray; |
1489 |
|
} |