ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.44
Committed: Tue Dec 9 22:46:41 2003 UTC (20 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.43: +2 -4 lines
Log Message:
Some versions of gcc don't like static f(); followed by exter f();

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: ranimate.c,v 2.43 2003/10/27 10:32:06 schorsch Exp $";
3 #endif
4 /*
5 * Radiance animation control program
6 *
7 * The main difference between this program and ranimove is that
8 * we have many optimizations here for camera motion in static
9 * environments, calling rpict and pinterp on multiple processors,
10 * where ranimove puts its emphasis on object motion, and does
11 * not use any external programs for image generation.
12 *
13 * See the ranimate(1) man page for further details.
14 */
15
16 #include "copyright.h"
17
18 #include <stdio.h>
19 #include <ctype.h>
20 #include <sys/stat.h>
21 #include <time.h>
22
23 /*#include "standard.h"*/
24 #include "platform.h"
25 #include "paths.h"
26 #include "rtio.h"
27 #include "rterror.h"
28 #include "view.h"
29 #include "vars.h"
30 #include "netproc.h"
31 /* default blur samples */
32 #ifndef DEF_NBLUR
33 #define DEF_NBLUR 5
34 #endif
35 /* default remote shell */
36 #define REMSH "rsh"
37 /* input variables (alphabetical by name) */
38 #define ANIMATE 0 /* animation command */
39 #define ARCHIVE 1 /* archiving command */
40 #define BASENAME 2 /* output image base name */
41 #define DIRECTORY 3 /* working (sub)directory */
42 #define DISKSPACE 4 /* how much disk space to use */
43 #define END 5 /* ending frame number */
44 #define EXPOSURE 6 /* how to compute exposure */
45 #define HOST 7 /* rendering host machine */
46 #define INTERP 8 /* # frames to interpolate */
47 #define MBLUR 9 /* motion blur parameters */
48 #define NEXTANIM 10 /* next animation file */
49 #define OCTREE 11 /* octree file name */
50 #define OVERSAMP 12 /* # times to oversample image */
51 #define PFILT 13 /* pfilt options */
52 #define PINTERP 14 /* pinterp options */
53 #define RENDER 15 /* rendering options */
54 #define RESOLUTION 16 /* desired final resolution */
55 #define RIF 17 /* rad input file */
56 #define RSH 18 /* remote shell script or program */
57 #define RTRACE 19 /* use rtrace with pinterp? */
58 #define START 20 /* starting frame number */
59 #define TRANSFER 21 /* frame transfer command */
60 #define VIEWFILE 22 /* animation frame views */
61
62 int NVARS = 23; /* total number of variables */
63
64 VARIABLE vv[] = { /* variable-value pairs */
65 {"ANIMATE", 2, 0, NULL, onevalue},
66 {"ARCHIVE", 2, 0, NULL, onevalue},
67 {"BASENAME", 3, 0, NULL, onevalue},
68 {"DIRECTORY", 3, 0, NULL, onevalue},
69 {"DISKSPACE", 3, 0, NULL, fltvalue},
70 {"END", 2, 0, NULL, intvalue},
71 {"EXPOSURE", 3, 0, NULL, onevalue},
72 {"host", 4, 0, NULL, NULL},
73 {"INTERPOLATE", 3, 0, NULL, intvalue},
74 {"MBLUR", 2, 0, NULL, onevalue},
75 {"NEXTANIM", 3, 0, NULL, onevalue},
76 {"OCTREE", 3, 0, NULL, onevalue},
77 {"OVERSAMPLE", 2, 0, NULL, fltvalue},
78 {"pfilt", 2, 0, NULL, catvalues},
79 {"pinterp", 2, 0, NULL, catvalues},
80 {"render", 3, 0, NULL, catvalues},
81 {"RESOLUTION", 3, 0, NULL, onevalue},
82 {"RIF", 3, 0, NULL, onevalue},
83 {"RSH", 3, 0, NULL, onevalue},
84 {"RTRACE", 2, 0, NULL, boolvalue},
85 {"START", 2, 0, NULL, intvalue},
86 {"TRANSFER", 2, 0, NULL, onevalue},
87 {"VIEWFILE", 2, 0, NULL, onevalue},
88 };
89
90 #define SFNAME "STATUS" /* status file name */
91
92 struct {
93 char host[64]; /* control host name */
94 int pid; /* control process id */
95 char cfname[128]; /* control file name */
96 int rnext; /* next frame to render */
97 int fnext; /* next frame to filter */
98 int tnext; /* next frame to transfer */
99 } astat; /* animation status */
100
101 char *progname; /* our program name */
102 char *cfname; /* our control file name */
103
104 int nowarn = 0; /* turn warnings off? */
105 int silent = 0; /* silent mode? */
106 int noaction = 0; /* take no action? */
107
108 char *remsh; /* remote shell program/script */
109 char rendopt[2048]; /* rendering options */
110 char rresopt[32]; /* rendering resolution options */
111 char fresopt[32]; /* filter resolution options */
112 int pfiltalways; /* always use pfilt? */
113
114 char arcargs[10240]; /* files to archive */
115 char *arcfirst, *arcnext; /* pointers to first and next argument */
116
117 struct pslot {
118 int pid; /* process ID (0 if empty) */
119 int fout; /* output frame number */
120 int (*rcvf)(); /* recover function */
121 } *pslot; /* process slots */
122 int npslots; /* number of process slots */
123
124 #define phostname(ps) ((ps)->hostname[0] ? (ps)->hostname : astat.host)
125 PSERVER *lastpserver; /* last process server with error */
126
127 static struct pslot * findpslot(int pid);
128 static void checkdir(void);
129 static VIEW * getview(int n);
130
131 static char * dirfile(char *df, register char *path);
132 static char * getexp(int n);
133 static int getblur(double *bf);
134 static int getastat(void);
135 static void getradfile(char *rfargs);
136 static void badvalue(int vc);
137 static int rmfile(char *fn);
138 static int runcom(char *cs);
139 static int pruncom(char *com, char *ppins, int maxcopies);
140 static void bwait(int ncoms);
141 static int bruncom(char *com, int fout, int (*rf)());
142 static int serverdown(void);
143 static int donecom(PSERVER *ps, int pn, int status);
144 static int countviews(void);
145 static int dofilt(int frame, int rvr);
146 static void archive(void);
147 static int frecover(int frame);
148 static int recover(int frame);
149 static void sethosts(void);
150 static void walkwait(int first, int last, char *vfn);
151 static void animrend(int frame, VIEW *vp);
152 static void transferframes(void);
153 static void filterframes(void);
154 static void renderframes(int nframes);
155 static void animate(void);
156 static void setdefaults(void);
157 static void putastat(void);
158
159
160 main(argc, argv)
161 int argc;
162 char *argv[];
163 {
164 int explicate = 0;
165 int i;
166
167 progname = argv[0]; /* get arguments */
168 for (i = 1; i < argc && argv[i][0] == '-'; i++)
169 switch (argv[i][1]) {
170 case 'e': /* print variables */
171 explicate++;
172 break;
173 case 'w': /* turn off warnings */
174 nowarn++;
175 break;
176 case 's': /* silent mode */
177 silent++;
178 break;
179 case 'n': /* take no action */
180 noaction++;
181 break;
182 default:
183 goto userr;
184 }
185 if (i != argc-1)
186 goto userr;
187 cfname = argv[i];
188 /* load variables */
189 loadvars(cfname);
190 /* check variables */
191 checkvalues();
192 /* did we get DIRECTORY? */
193 checkdir();
194 /* check status */
195 if (getastat() < 0) {
196 fprintf(stderr, "%s: exiting\n", progname);
197 quit(1);
198 }
199 /* pfilt always if options given */
200 pfiltalways = vdef(PFILT);
201 /* load RIF if any */
202 if (vdef(RIF))
203 getradfile(vval(RIF));
204 /* set defaults */
205 setdefaults();
206 /* print variables */
207 if (explicate)
208 printvars(stdout);
209 /* set up process servers */
210 sethosts();
211 /* run animation */
212 animate();
213 /* all done */
214 if (vdef(NEXTANIM)) {
215 argv[i] = vval(NEXTANIM); /* just change input file */
216 if (!silent)
217 printargs(argc, argv, stdout);
218 if ((argv[0] = getpath(progname,getenv("PATH"),X_OK)) == NULL)
219 fprintf(stderr, "%s: command not found\n", progname);
220 else
221 execv(progname, argv);
222 quit(1);
223 }
224 quit(0);
225 userr:
226 fprintf(stderr, "Usage: %s [-s][-n][-w][-e] anim_file\n", progname);
227 quit(1);
228 }
229
230
231 static int
232 getastat(void) /* check/set animation status */
233 {
234 char sfname[256];
235 FILE *fp;
236
237 sprintf(sfname, "%s/%s", vval(DIRECTORY), SFNAME);
238 if ((fp = fopen(sfname, "r")) == NULL) {
239 if (errno != ENOENT) {
240 perror(sfname);
241 return(-1);
242 }
243 astat.rnext = astat.fnext = astat.tnext = 0;
244 goto setours;
245 }
246 if (fscanf(fp, "Control host: %s\n", astat.host) != 1)
247 goto fmterr;
248 if (fscanf(fp, "Control PID: %d\n", &astat.pid) != 1)
249 goto fmterr;
250 if (fscanf(fp, "Control file: %s\n", astat.cfname) != 1)
251 goto fmterr;
252 if (fscanf(fp, "Next render: %d\n", &astat.rnext) != 1)
253 goto fmterr;
254 if (fscanf(fp, "Next filter: %d\n", &astat.fnext) != 1)
255 goto fmterr;
256 if (fscanf(fp, "Next transfer: %d\n", &astat.tnext) != 1)
257 goto fmterr;
258 fclose(fp);
259 if (astat.pid != 0) { /* thinks it's still running */
260 if (strcmp(myhostname(), astat.host)) {
261 fprintf(stderr,
262 "%s: process %d may still be running on host %s\n",
263 progname, astat.pid, astat.host);
264 return(-1);
265 }
266 if (kill(astat.pid, 0) != -1 || errno != ESRCH) {
267 fprintf(stderr, "%s: process %d is still running\n",
268 progname, astat.pid);
269 return(-1);
270 }
271 /* assume it is dead */
272 }
273 if (strcmp(cfname, astat.cfname) && astat.pid != 0) { /* other's */
274 fprintf(stderr, "%s: unfinished job \"%s\"\n",
275 progname, astat.cfname);
276 return(-1);
277 }
278 /* check control file mods. */
279 if (!nowarn && fdate(cfname) > fdate(sfname))
280 fprintf(stderr,
281 "%s: warning - control file modified since last run\n",
282 progname);
283 setours: /* set our values */
284 strcpy(astat.host, myhostname());
285 astat.pid = getpid();
286 strcpy(astat.cfname, cfname);
287 return(0);
288 fmterr:
289 fprintf(stderr, "%s: format error in status file \"%s\"\n",
290 progname, sfname);
291 fclose(fp);
292 return(-1);
293 }
294
295
296 static void
297 putastat(void) /* put out current status */
298 {
299 char buf[256];
300 FILE *fp;
301
302 if (noaction)
303 return;
304 sprintf(buf, "%s/%s", vval(DIRECTORY), SFNAME);
305 if ((fp = fopen(buf, "w")) == NULL) {
306 perror(buf);
307 quit(1);
308 }
309 fprintf(fp, "Control host: %s\n", astat.host);
310 fprintf(fp, "Control PID: %d\n", astat.pid);
311 fprintf(fp, "Control file: %s\n", astat.cfname);
312 fprintf(fp, "Next render: %d\n", astat.rnext);
313 fprintf(fp, "Next filter: %d\n", astat.fnext);
314 fprintf(fp, "Next transfer: %d\n", astat.tnext);
315 fclose(fp);
316 }
317
318
319 static void
320 checkdir(void) /* make sure we have our directory */
321 {
322 struct stat stb;
323
324 if (!vdef(DIRECTORY)) {
325 fprintf(stderr, "%s: %s undefined\n",
326 progname, vnam(DIRECTORY));
327 quit(1);
328 }
329 if (stat(vval(DIRECTORY), &stb) == -1) {
330 if (errno == ENOENT && mkdir(vval(DIRECTORY), 0777) == 0)
331 return;
332 perror(vval(DIRECTORY));
333 quit(1);
334 }
335 if (!(stb.st_mode & S_IFDIR)) {
336 fprintf(stderr, "%s: not a directory\n", vval(DIRECTORY));
337 quit(1);
338 }
339 }
340
341
342 static void
343 setdefaults(void) /* set default values */
344 {
345 extern char *atos();
346 int decades;
347 char buf[256];
348
349 if (vdef(ANIMATE)) {
350 vval(OCTREE) = NULL;
351 vdef(OCTREE) = 0;
352 } else if (!vdef(OCTREE)) {
353 fprintf(stderr, "%s: either %s or %s must be defined\n",
354 progname, vnam(OCTREE), vnam(ANIMATE));
355 quit(1);
356 }
357 if (!vdef(VIEWFILE)) {
358 fprintf(stderr, "%s: %s undefined\n", progname, vnam(VIEWFILE));
359 quit(1);
360 }
361 if (!vdef(HOST)) {
362 vval(HOST) = LHOSTNAME;
363 vdef(HOST)++;
364 }
365 if (!vdef(START)) {
366 vval(START) = "1";
367 vdef(START)++;
368 }
369 if (!vdef(END)) {
370 sprintf(buf, "%d", countviews()+vint(START)-1);
371 vval(END) = savqstr(buf);
372 vdef(END)++;
373 }
374 if (vint(END) < vint(START)) {
375 fprintf(stderr, "%s: ending frame less than starting frame\n",
376 progname);
377 quit(1);
378 }
379 if (!vdef(BASENAME)) {
380 decades = (int)log10((double)vint(END)) + 1;
381 if (decades < 3) decades = 3;
382 sprintf(buf, "%s/frame%%0%dd", vval(DIRECTORY), decades);
383 vval(BASENAME) = savqstr(buf);
384 vdef(BASENAME)++;
385 }
386 if (!vdef(RESOLUTION)) {
387 vval(RESOLUTION) = "640";
388 vdef(RESOLUTION)++;
389 }
390 if (!vdef(OVERSAMP)) {
391 vval(OVERSAMP) = "2";
392 vdef(OVERSAMP)++;
393 }
394 if (!vdef(INTERP)) {
395 vval(INTERP) = "0";
396 vdef(INTERP)++;
397 }
398 if (!vdef(MBLUR)) {
399 vval(MBLUR) = "0";
400 vdef(MBLUR)++;
401 }
402 if (!vdef(RTRACE)) {
403 vval(RTRACE) = "F";
404 vdef(RTRACE)++;
405 }
406 if (!vdef(DISKSPACE)) {
407 if (!nowarn)
408 fprintf(stderr,
409 "%s: warning - no %s setting, assuming 100 Mbytes available\n",
410 progname, vnam(DISKSPACE));
411 vval(DISKSPACE) = "100";
412 vdef(DISKSPACE)++;
413 }
414 if (!vdef(RSH)) {
415 vval(RSH) = REMSH;
416 vdef(RSH)++;
417 }
418 /* locate remote shell program */
419 atos(buf, sizeof(buf), vval(RSH));
420 if ((remsh = getpath(buf, getenv("PATH"), X_OK)) != NULL)
421 remsh = savqstr(remsh);
422 else
423 remsh = vval(RSH); /* will generate error if used */
424
425 /* append rendering options */
426 if (vdef(RENDER))
427 sprintf(rendopt+strlen(rendopt), " %s", vval(RENDER));
428 }
429
430
431 static void
432 sethosts(void) /* set up process servers */
433 {
434 extern char *iskip();
435 char buf[256], *dir, *uname;
436 int np;
437 register char *cp;
438 int i;
439
440 npslots = 0;
441 if (noaction)
442 return;
443 for (i = 0; i < vdef(HOST); i++) { /* add each host */
444 dir = uname = NULL;
445 np = 1;
446 strcpy(cp=buf, nvalue(HOST, i)); /* copy to buffer */
447 cp = sskip(cp); /* skip host name */
448 while (isspace(*cp))
449 *cp++ = '\0';
450 if (*cp) { /* has # processes? */
451 np = atoi(cp);
452 if ((cp = iskip(cp)) == NULL || (*cp && !isspace(*cp)))
453 badvalue(HOST);
454 while (isspace(*cp))
455 cp++;
456 if (*cp) { /* has directory? */
457 dir = cp;
458 cp = sskip(cp); /* skip dir. */
459 while (isspace(*cp))
460 *cp++ = '\0';
461 if (*cp) { /* has user? */
462 uname = cp;
463 if (*sskip(cp))
464 badvalue(HOST);
465 }
466 }
467 }
468 if (addpserver(buf, dir, uname, np) == NULL) {
469 if (!nowarn)
470 fprintf(stderr,
471 "%s: cannot execute on host \"%s\"\n",
472 progname, buf);
473 } else
474 npslots += np;
475 }
476 if (npslots == 0) {
477 fprintf(stderr, "%s: no working process servers\n", progname);
478 quit(1);
479 }
480 pslot = (struct pslot *)calloc(npslots, sizeof(struct pslot));
481 if (pslot == NULL) {
482 perror("malloc");
483 quit(1);
484 }
485 }
486
487 static void
488 getradfile(char *rfargs) /* run rad and get needed variables */
489 {
490 static short mvar[] = {OCTREE,PFILT,RESOLUTION,EXPOSURE,-1};
491 char combuf[256];
492 register int i;
493 register char *cp;
494 char *pippt;
495 /* create rad command */
496 sprintf(rendopt, " @%s/render.opt", vval(DIRECTORY));
497 sprintf(combuf,
498 "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
499 rfargs, rendopt+2);
500 cp = combuf;
501 while (*cp) {
502 if (*cp == '|') pippt = cp;
503 cp++;
504 } /* match unset variables */
505 for (i = 0; mvar[i] >= 0; i++)
506 if (!vdef(mvar[i])) {
507 *cp++ = '|';
508 strcpy(cp, vnam(mvar[i]));
509 while (*cp) cp++;
510 pippt = NULL;
511 }
512 if (pippt != NULL)
513 strcpy(pippt, "> " NULL_DEVICE); /* nothing to match */
514 else {
515 sprintf(cp, ")[ \t]*=' > %s/radset.var", vval(DIRECTORY));
516 cp += 11; /* point to file name */
517 }
518 system(combuf); /* ignore exit code */
519 if (pippt == NULL) { /* load variables and remove file */
520 loadvars(cp);
521 unlink(cp);
522 }
523 }
524
525
526 static void
527 animate(void) /* run animation */
528 {
529 int xres, yres;
530 float pa, mult;
531 int frames_batch;
532 register int i;
533 double d1, d2;
534 /* compute rpict resolution */
535 i = sscanf(vval(RESOLUTION), "%d %d %f", &xres, &yres, &pa);
536 mult = vflt(OVERSAMP);
537 if (i == 3) {
538 sprintf(rresopt, "-x %d -y %d -pa %.3f", (int)(mult*xres),
539 (int)(mult*yres), pa);
540 sprintf(fresopt, "-x %d -y %d -pa %.3f", xres, yres, pa);
541 } else if (i) {
542 if (i == 1) yres = xres;
543 sprintf(rresopt, "-x %d -y %d", (int)(mult*xres),
544 (int)(mult*yres));
545 sprintf(fresopt, "-x %d -y %d -pa 1", xres, yres);
546 } else
547 badvalue(RESOLUTION);
548 /* consistency checks */
549 if (vdef(ANIMATE)) {
550 if (vint(INTERP)) {
551 if (!nowarn)
552 fprintf(stderr,
553 "%s: resetting %s=0 for animation\n",
554 progname, vnam(INTERP));
555 vval(INTERP) = "0";
556 }
557 if (strcmp(vval(MBLUR),"0")) { /* can't handle this */
558 if (!nowarn)
559 fprintf(stderr,
560 "%s: resetting %s=0 for animation\n",
561 progname, vnam(MBLUR));
562 vval(MBLUR) = "0";
563 }
564 }
565 /* figure # frames per batch */
566 d1 = mult*xres*mult*yres*4; /* space for orig. picture */
567 if ((i=vint(INTERP)) || getblur(NULL) > 1)
568 d1 += mult*xres*mult*yres*sizeof(float); /* Z-buffer */
569 d2 = xres*yres*4; /* space for final picture */
570 frames_batch = (i+1)*(vflt(DISKSPACE)*1048576.-d1)/(d1+i*d2);
571 if (frames_batch < i+2) {
572 fprintf(stderr, "%s: insufficient disk space allocated\n",
573 progname);
574 quit(1);
575 }
576 /* initialize archive argument list */
577 i = vdef(ARCHIVE) ? strlen(vval(ARCHIVE))+132 : 132;
578 arcnext = arcfirst = arcargs + i;
579 /* initialize status file */
580 if (astat.rnext == 0)
581 astat.rnext = astat.fnext = astat.tnext = vint(START);
582 putastat();
583 /* render in batches */
584 while (astat.tnext <= vint(END)) {
585 renderframes(frames_batch);
586 filterframes();
587 transferframes();
588 }
589 /* mark status as finished */
590 astat.pid = 0;
591 putastat();
592 /* close open files */
593 getview(0);
594 getexp(0);
595 }
596
597
598 static void
599 renderframes(int nframes) /* render next nframes frames */
600 {
601 static char vendbuf[16];
602 VIEW *vp;
603 FILE *fp = NULL;
604 char vfname[128];
605 int lastframe;
606 register int i;
607
608 if (astat.tnext < astat.rnext) /* other work to do first */
609 return;
610 /* create batch view file */
611 if (!vdef(ANIMATE)) {
612 sprintf(vfname, "%s/anim.vf", vval(DIRECTORY));
613 if ((fp = fopen(vfname, "w")) == NULL) {
614 perror(vfname);
615 quit(1);
616 }
617 }
618 /* bound batch properly */
619 lastframe = astat.rnext + nframes - 1;
620 if ((lastframe-1) % (vint(INTERP)+1)) /* need even interval */
621 lastframe += vint(INTERP)+1 - ((lastframe-1)%(vint(INTERP)+1));
622 if (lastframe > vint(END)) /* check for end */
623 lastframe = vint(END);
624 /* render each view */
625 for (i = astat.rnext; i <= lastframe; i++) {
626 if ((vp = getview(i)) == NULL) {
627 if (!nowarn)
628 fprintf(stderr,
629 "%s: ran out of views before last frame\n",
630 progname);
631 sprintf(vval(END)=vendbuf, "%d", i-1);
632 lastframe = i - 1;
633 break;
634 }
635 if (vdef(ANIMATE)) /* animate frame */
636 animrend(i, vp);
637 else { /* else record it */
638 fputs(VIEWSTR, fp);
639 fprintview(vp, fp);
640 putc('\n', fp);
641 }
642 }
643 if (vdef(ANIMATE)) /* wait for renderings to finish */
644 bwait(0);
645 else { /* else if walk-through */
646 fclose(fp); /* close view file */
647 walkwait(astat.rnext, lastframe, vfname); /* walk it */
648 unlink(vfname); /* remove view file */
649 }
650 astat.rnext = i; /* update status */
651 putastat();
652 }
653
654
655 static void
656 filterframes(void) /* catch up with filtering */
657 {
658 VIEW *vp;
659 register int i;
660
661 if (astat.tnext < astat.fnext) /* other work to do first */
662 return;
663 /* filter each view */
664 for (i = astat.fnext; i < astat.rnext; i++)
665 dofilt(i, 0);
666
667 bwait(0); /* wait for filter processes */
668 archive(); /* archive originals */
669 astat.fnext = i; /* update status */
670 putastat();
671 }
672
673
674 static void
675 transferframes(void) /* catch up with picture transfers */
676 {
677 char combuf[10240], *fbase;
678 register char *cp;
679 register int i;
680
681 if (astat.tnext >= astat.fnext) /* nothing to do, yet */
682 return;
683 if (!vdef(TRANSFER)) { /* no transfer function -- leave 'em */
684 astat.tnext = astat.fnext;
685 putastat(); /* update status */
686 return;
687 }
688 strcpy(combuf, "cd "); /* start transfer command */
689 fbase = dirfile(cp = combuf+3, vval(BASENAME));
690 if (*cp) {
691 while (*++cp) ;
692 *cp++ = ';'; *cp++ = ' ';
693 } else
694 cp = combuf;
695 strcpy(cp, vval(TRANSFER));
696 while (*cp) cp++;
697 /* make argument list */
698 for (i = astat.tnext; i < astat.fnext; i++) {
699 *cp++ = ' ';
700 sprintf(cp, fbase, i);
701 while (*cp) cp++;
702 strcpy(cp, ".pic");
703 cp += 4;
704 }
705 if (runcom(combuf)) { /* transfer frames */
706 fprintf(stderr, "%s: error running transfer command\n",
707 progname);
708 quit(1);
709 }
710 astat.tnext = i; /* update status */
711 putastat();
712 }
713
714
715 static void
716 animrend( /* start animation frame */
717 int frame,
718 VIEW *vp
719 )
720 {
721 char combuf[2048];
722 char fname[128];
723
724 sprintf(fname, vval(BASENAME), frame);
725 strcat(fname, ".unf");
726 if (access(fname, F_OK) == 0)
727 return;
728 sprintf(combuf, "%s %d | rpict%s%s -w0 %s > %s", vval(ANIMATE), frame,
729 rendopt, viewopt(vp), rresopt, fname);
730 bruncom(combuf, frame, recover); /* run in background */
731 }
732
733
734 static void
735 walkwait( /* walk-through frames */
736 int first,
737 int last,
738 char *vfn
739 )
740 {
741 double blurf;
742 int nblur = getblur(&blurf);
743 char combuf[2048];
744 register char *inspoint;
745 register int i;
746
747 if (!noaction && vint(INTERP)) /* create dummy frames */
748 for (i = first; i <= last; i++)
749 if (i < vint(END) && (i-1) % (vint(INTERP)+1)) {
750 sprintf(combuf, vval(BASENAME), i);
751 strcat(combuf, ".unf");
752 close(open(combuf, O_RDONLY|O_CREAT, 0666));
753 }
754 /* create command */
755 sprintf(combuf, "rpict%s%s -w0", rendopt,
756 viewopt(getview(first>1 ? first-1 : 1)));
757 inspoint = combuf;
758 while (*inspoint) inspoint++;
759 if (nblur) {
760 sprintf(inspoint, " -pm %.3f", blurf/nblur);
761 while (*inspoint) inspoint++;
762 }
763 if (nblur > 1 || vint(INTERP)) {
764 sprintf(inspoint, " -z %s.zbf", vval(BASENAME));
765 while (*inspoint) inspoint++;
766 }
767 sprintf(inspoint, " -o %s.unf %s -S %d",
768 vval(BASENAME), rresopt, first);
769 while (*inspoint) inspoint++;
770 sprintf(inspoint, " %s < %s", vval(OCTREE), vfn);
771 /* run in parallel */
772 i = (last-first+1)/(vint(INTERP)+1);
773 if (i < 1) i = 1;
774 if (pruncom(combuf, inspoint, i)) {
775 fprintf(stderr, "%s: error rendering frames %d through %d\n",
776 progname, first, last);
777 quit(1);
778 }
779 if (!noaction && vint(INTERP)) /* remove dummy frames */
780 for (i = first; i <= last; i++)
781 if (i < vint(END) && (i-1) % (vint(INTERP)+1)) {
782 sprintf(combuf, vval(BASENAME), i);
783 strcat(combuf, ".unf");
784 unlink(combuf);
785 }
786 }
787
788
789 static int
790 recover(int frame) /* recover the specified frame */
791 {
792 static int *rfrm; /* list of recovered frames */
793 static int nrfrms = 0;
794 double blurf;
795 int nblur = getblur(&blurf);
796 char combuf[2048];
797 char fname[128];
798 register char *cp;
799 register int i;
800 /* check to see if recovered already */
801 for (i = nrfrms; i--; )
802 if (rfrm[i] == frame)
803 return(0);
804 /* build command */
805 sprintf(fname, vval(BASENAME), frame);
806 if (vdef(ANIMATE))
807 sprintf(combuf, "%s %d | rpict%s -w0",
808 vval(ANIMATE), frame, rendopt);
809 else
810 sprintf(combuf, "rpict%s -w0", rendopt);
811 cp = combuf;
812 while (*cp) cp++;
813 if (nblur) {
814 sprintf(cp, " -pm %.3f", blurf/nblur);
815 while (*cp) cp++;
816 }
817 if (nblur > 1 || vint(INTERP)) {
818 sprintf(cp, " -z %s.zbf", fname);
819 while (*cp) cp++;
820 }
821 sprintf(cp, " -ro %s.unf", fname);
822 while (*cp) cp++;
823 if (!vdef(ANIMATE)) {
824 *cp++ = ' ';
825 strcpy(cp, vval(OCTREE));
826 }
827 if (runcom(combuf)) /* run command */
828 return(1);
829 /* add frame to recovered list */
830 if (nrfrms)
831 rfrm = (int *)realloc((void *)rfrm, (nrfrms+1)*sizeof(int));
832 else
833 rfrm = (int *)malloc(sizeof(int));
834 if (rfrm == NULL) {
835 perror("malloc");
836 quit(1);
837 }
838 rfrm[nrfrms++] = frame;
839 return(0);
840 }
841
842
843 static int
844 frecover(int frame) /* recover filtered frame */
845 {
846 if (dofilt(frame, 2) && dofilt(frame, 1))
847 return(1);
848 return(0);
849 }
850
851
852 static void
853 archive(void) /* archive and remove renderings */
854 {
855 #define RMCOML (sizeof(rmcom)-1)
856 static char rmcom[] = "rm -f";
857 char basedir[128];
858 int dlen, alen;
859 register int j;
860
861 if (arcnext == arcfirst)
862 return; /* nothing to do */
863 dirfile(basedir, vval(BASENAME));
864 dlen = strlen(basedir);
865 if (vdef(ARCHIVE)) { /* run archive command */
866 alen = strlen(vval(ARCHIVE));
867 if (dlen) {
868 j = alen + dlen + 5;
869 strncpy(arcfirst-j, "cd ", 3);
870 strncpy(arcfirst-j+3, basedir, dlen);
871 (arcfirst-j)[dlen+3] = ';'; (arcfirst-j)[dlen+4] = ' ';
872 } else
873 j = alen;
874 strncpy(arcfirst-alen, vval(ARCHIVE), alen);
875 if (runcom(arcfirst-j)) {
876 fprintf(stderr, "%s: error running archive command\n",
877 progname);
878 quit(1);
879 }
880 }
881 if (dlen) {
882 j = RMCOML + dlen + 5;
883 strncpy(arcfirst-j, "cd ", 3);
884 strncpy(arcfirst-j+3, basedir, dlen);
885 (arcfirst-j)[dlen+3] = ';'; (arcfirst-j)[dlen+4] = ' ';
886 } else
887 j = RMCOML;
888 /* run remove command */
889 strncpy(arcfirst-RMCOML, rmcom, RMCOML);
890 runcom(arcfirst-j);
891 arcnext = arcfirst; /* reset argument list */
892 #undef RMCOML
893 }
894
895
896 static int
897 dofilt( /* filter frame */
898 int frame,
899 int rvr
900 )
901 {
902 static int iter = 0;
903 double blurf;
904 int nblur = getblur(&blurf);
905 VIEW *vp = getview(frame);
906 char *ep = getexp(frame);
907 char fnbefore[128], fnafter[128], *fbase;
908 char combuf[1024], fname0[128], fname1[128];
909 int usepinterp, usepfilt, nora_rgbe;
910 int frseq[2];
911 /* check what is needed */
912 if (vp == NULL) {
913 fprintf(stderr,
914 "%s: unexpected error reading view for frame %d\n",
915 progname, frame);
916 quit(1);
917 }
918 if (ep == NULL) {
919 fprintf(stderr,
920 "%s: unexpected error reading exposure for frame %d\n",
921 progname, frame);
922 quit(1);
923 }
924 usepinterp = (nblur > 1);
925 usepfilt = pfiltalways | (ep==NULL);
926 if (ep != NULL && !strcmp(ep, "1"))
927 ep = "+0";
928 nora_rgbe = strcmp(vval(OVERSAMP),"1") || ep==NULL ||
929 *ep != '+' || *ep != '-' || !isint(ep);
930 /* compute rendered views */
931 frseq[0] = frame - ((frame-1) % (vint(INTERP)+1));
932 frseq[1] = frseq[0] + vint(INTERP) + 1;
933 fbase = dirfile(NULL, vval(BASENAME));
934 if (frseq[1] > vint(END))
935 frseq[1] = vint(END);
936 if (frseq[1] == frame) { /* pfilt only */
937 frseq[0] = frseq[1];
938 usepinterp = 0; /* update what's needed */
939 usepfilt |= nora_rgbe;
940 } else if (frseq[0] == frame) { /* no interpolation needed */
941 if (!rvr && frame > 1+vint(INTERP)) { /* archive previous */
942 *arcnext++ = ' ';
943 sprintf(arcnext, fbase, frame-vint(INTERP)-1);
944 while (*arcnext) arcnext++;
945 strcpy(arcnext, ".unf");
946 arcnext += 4;
947 if (usepinterp || vint(INTERP)) { /* and Z-buf */
948 *arcnext++ = ' ';
949 sprintf(arcnext, fbase, frame-vint(INTERP)-1);
950 while (*arcnext) arcnext++;
951 strcpy(arcnext, ".zbf");
952 arcnext += 4;
953 }
954 }
955 if (!usepinterp) /* update what's needed */
956 usepfilt |= nora_rgbe;
957 } else /* interpolation needed */
958 usepinterp++;
959 if (frseq[1] >= astat.rnext) /* next batch unavailable */
960 frseq[1] = frseq[0];
961 sprintf(fnbefore, vval(BASENAME), frseq[0]);
962 sprintf(fnafter, vval(BASENAME), frseq[1]);
963 if (rvr == 1 && recover(frseq[0])) /* recover before frame? */
964 return(1);
965 /* generate command */
966 if (usepinterp) { /* using pinterp */
967 if (rvr == 2 && recover(frseq[1])) /* recover after? */
968 return(1);
969 if (nblur > 1) { /* with pmblur */
970 sprintf(fname0, "%s/vw0%c", vval(DIRECTORY),
971 'a'+(iter%26));
972 sprintf(fname1, "%s/vw1%c", vval(DIRECTORY),
973 'a'+(iter%26));
974 if (!noaction) {
975 FILE *fp; /* motion blurring */
976 if ((fp = fopen(fname0, "w")) == NULL) {
977 perror(fname0); quit(1);
978 }
979 fputs(VIEWSTR, fp);
980 fprintview(vp, fp);
981 putc('\n', fp); fclose(fp);
982 if ((vp = getview(frame+1)) == NULL) {
983 fprintf(stderr,
984 "%s: unexpected error reading view for frame %d\n",
985 progname, frame+1);
986 quit(1);
987 }
988 if ((fp = fopen(fname1, "w")) == NULL) {
989 perror(fname1); quit(1);
990 }
991 fputs(VIEWSTR, fp);
992 fprintview(vp, fp);
993 putc('\n', fp); fclose(fp);
994 }
995 sprintf(combuf,
996 "(pmblur %.3f %d %s %s; rm -f %s %s) | pinterp -B -a",
997 blurf, nblur,
998 fname0, fname1, fname0, fname1);
999 iter++;
1000 } else /* no blurring */
1001 strcpy(combuf, "pinterp");
1002 strcat(combuf, viewopt(vp));
1003 if (vbool(RTRACE))
1004 sprintf(combuf+strlen(combuf), " -ff -fr '%s -w0 %s'",
1005 rendopt+1, vval(OCTREE));
1006 if (vdef(PINTERP))
1007 sprintf(combuf+strlen(combuf), " %s", vval(PINTERP));
1008 if (usepfilt)
1009 sprintf(combuf+strlen(combuf), " %s", rresopt);
1010 else
1011 sprintf(combuf+strlen(combuf), " -a %s -e %s",
1012 fresopt, ep);
1013 sprintf(combuf+strlen(combuf), " %s.unf %s.zbf",
1014 fnbefore, fnbefore);
1015 if (frseq[1] != frseq[0])
1016 sprintf(combuf+strlen(combuf), " %s.unf %s.zbf",
1017 fnafter, fnafter);
1018 if (usepfilt) { /* also pfilt */
1019 if (vdef(PFILT))
1020 sprintf(combuf+strlen(combuf), " | pfilt %s",
1021 vval(PFILT));
1022 else
1023 strcat(combuf, " | pfilt");
1024 if (ep != NULL)
1025 sprintf(combuf+strlen(combuf), " -1 -e %s %s",
1026 ep, fresopt);
1027 else
1028 sprintf(combuf+strlen(combuf), " %s", fresopt);
1029 }
1030 } else if (usepfilt) { /* pfilt only */
1031 if (rvr == 2)
1032 return(1);
1033 if (vdef(PFILT))
1034 sprintf(combuf, "pfilt %s", vval(PFILT));
1035 else
1036 strcpy(combuf, "pfilt");
1037 if (ep != NULL)
1038 sprintf(combuf+strlen(combuf), " -1 -e %s %s %s.unf",
1039 ep, fresopt, fnbefore);
1040 else
1041 sprintf(combuf+strlen(combuf), " %s %s.unf",
1042 fresopt, fnbefore);
1043 } else { /* else just check it */
1044 if (rvr == 2)
1045 return(1);
1046 sprintf(combuf, "ra_rgbe -e %s -r %s.unf", ep, fnbefore);
1047 }
1048 /* output file name */
1049 sprintf(fname0, vval(BASENAME), frame);
1050 sprintf(combuf+strlen(combuf), " > %s.pic", fname0);
1051 if (rvr) /* in recovery */
1052 return(runcom(combuf));
1053 bruncom(combuf, frame, frecover); /* else run in background */
1054 return(0);
1055 }
1056
1057
1058 static VIEW *
1059 getview(int n) /* get view number n */
1060 {
1061 static FILE *viewfp = NULL; /* view file pointer */
1062 static int viewnum = 0; /* current view number */
1063 static VIEW curview = STDVIEW; /* current view */
1064 char linebuf[256];
1065
1066 if (n == 0) { /* signal to close file and clean up */
1067 if (viewfp != NULL) {
1068 fclose(viewfp);
1069 viewfp = NULL;
1070 viewnum = 0;
1071 curview = stdview;
1072 }
1073 return(NULL);
1074 }
1075 if (viewfp == NULL) { /* open file */
1076 if ((viewfp = fopen(vval(VIEWFILE), "r")) == NULL) {
1077 perror(vval(VIEWFILE));
1078 quit(1);
1079 }
1080 } else if (n > 0 && n < viewnum) { /* rewind file */
1081 if (viewnum == 1 && feof(viewfp))
1082 return(&curview); /* just one view */
1083 if (fseek(viewfp, 0L, 0) == EOF) {
1084 perror(vval(VIEWFILE));
1085 quit(1);
1086 }
1087 curview = stdview;
1088 viewnum = 0;
1089 }
1090 if (n < 0) { /* get next view */
1091 register int c = getc(viewfp);
1092 if (c == EOF)
1093 return((VIEW *)NULL); /* that's it */
1094 ungetc(c, viewfp);
1095 n = viewnum + 1;
1096 }
1097 while (n > viewnum) { /* scan to desired view */
1098 if (fgets(linebuf, sizeof(linebuf), viewfp) == NULL)
1099 return(viewnum==1 ? &curview : (VIEW *)NULL);
1100 if (isview(linebuf) && sscanview(&curview, linebuf) > 0)
1101 viewnum++;
1102 }
1103 return(&curview); /* return it */
1104 }
1105
1106
1107 static int
1108 countviews(void) /* count views in view file */
1109 {
1110 int n;
1111
1112 if (getview(n=1) == NULL)
1113 return(0);
1114 while (getview(-1) != NULL)
1115 n++;
1116 return(n);
1117 }
1118
1119
1120 static char *
1121 getexp(int n) /* get exposure for nth frame */
1122 {
1123 extern char *fskip();
1124 static char expval[32];
1125 static FILE *expfp = NULL;
1126 static long *exppos;
1127 static int curfrm;
1128 register char *cp;
1129
1130 if (n == 0) { /* signal to close file */
1131 if (expfp != NULL) {
1132 fclose(expfp);
1133 free((void *)exppos);
1134 expfp = NULL;
1135 }
1136 return(NULL);
1137 } else if (n > vint(END)) /* request past end (error?) */
1138 return(NULL);
1139 if (!vdef(EXPOSURE)) /* no setting (auto) */
1140 return(NULL);
1141 if (isflt(vval(EXPOSURE))) /* always the same */
1142 return(vval(EXPOSURE));
1143 if (expfp == NULL) { /* open exposure file */
1144 if ((expfp = fopen(vval(EXPOSURE), "r")) == NULL) {
1145 fprintf(stderr,
1146 "%s: cannot open exposure file \"%s\"\n",
1147 progname, vval(EXPOSURE));
1148 quit(1);
1149 }
1150 curfrm = vint(END) + 1; /* init lookup tab. */
1151 exppos = (long *)malloc(curfrm*sizeof(long *));
1152 if (exppos == NULL) {
1153 perror(progname);
1154 quit(1);
1155 }
1156 while (curfrm--)
1157 exppos[curfrm] = -1L;
1158 curfrm = 0;
1159 }
1160 /* find position in file */
1161 if (n-1 != curfrm && n != curfrm && exppos[n-1] >= 0 &&
1162 fseek(expfp, exppos[curfrm=n-1], 0) == EOF) {
1163 fprintf(stderr, "%s: seek error on exposure file\n", progname);
1164 quit(1);
1165 }
1166 while (n > curfrm) { /* read exposure */
1167 if (exppos[curfrm] < 0)
1168 exppos[curfrm] = ftell(expfp);
1169 if (fgets(expval, sizeof(expval), expfp) == NULL) {
1170 fprintf(stderr, "%s: too few exposures\n",
1171 vval(EXPOSURE));
1172 quit(1);
1173 }
1174 curfrm++;
1175 cp = fskip(expval); /* check format */
1176 if (cp != NULL)
1177 while (isspace(*cp))
1178 *cp++ = '\0';
1179 if (cp == NULL || *cp) {
1180 fprintf(stderr,
1181 "%s: exposure format error on line %d\n",
1182 vval(EXPOSURE), curfrm);
1183 quit(1);
1184 }
1185 }
1186 return(expval); /* return value */
1187 }
1188
1189
1190 static struct pslot *
1191 findpslot(int pid) /* find or allocate a process slot */
1192 {
1193 register struct pslot *psempty = NULL;
1194 register int i;
1195
1196 for (i = 0; i < npslots; i++) { /* look for match */
1197 if (pslot[i].pid == pid)
1198 return(pslot+i);
1199 if (psempty == NULL && pslot[i].pid == 0)
1200 psempty = pslot+i;
1201 }
1202 return(psempty); /* return emtpy slot (error if NULL) */
1203 }
1204
1205
1206 static int
1207 donecom( /* clean up after finished process */
1208 PSERVER *ps,
1209 int pn,
1210 int status
1211 )
1212 {
1213 register NETPROC *pp;
1214 register struct pslot *psl;
1215
1216 pp = ps->proc + pn;
1217 if (pp->elen) { /* pass errors */
1218 if (ps->hostname[0])
1219 fprintf(stderr, "%s: ", ps->hostname);
1220 fprintf(stderr, "Error output from: %s\n", pp->com);
1221 fputs(pp->errs, stderr);
1222 fflush(stderr);
1223 if (ps->hostname[0])
1224 status = 1; /* because rsh doesn't return status */
1225 }
1226 lastpserver = NULL;
1227 psl = findpslot(pp->pid); /* check for bruncom() slot */
1228 if (psl->pid) {
1229 if (status) {
1230 if (psl->rcvf != NULL) /* attempt recovery */
1231 status = (*psl->rcvf)(psl->fout);
1232 if (status) {
1233 fprintf(stderr,
1234 "%s: error rendering frame %d\n",
1235 progname, psl->fout);
1236 quit(1);
1237 }
1238 lastpserver = ps;
1239 }
1240 psl->pid = 0; /* free process slot */
1241 } else if (status)
1242 lastpserver = ps;
1243 freestr(pp->com); /* free command string */
1244 return(status);
1245 }
1246
1247
1248 static int
1249 serverdown(void) /* check status of last process server */
1250 {
1251 if (lastpserver == NULL || !lastpserver->hostname[0])
1252 return(0);
1253 if (pserverOK(lastpserver)) /* server still up? */
1254 return(0);
1255 delpserver(lastpserver); /* else delete it */
1256 if (pslist == NULL) {
1257 fprintf(stderr, "%s: all process servers are down\n",
1258 progname);
1259 quit(1);
1260 }
1261 return(1);
1262 }
1263
1264
1265 static int
1266 bruncom( /* run a command in the background */
1267 char *com,
1268 int fout,
1269 int (*rf)()
1270 )
1271 {
1272 int pid;
1273 register struct pslot *psl;
1274
1275 if (noaction) {
1276 if (!silent)
1277 printf("\t%s\n", com); /* echo command */
1278 return(0);
1279 }
1280 com = savestr(com); /* else start it when we can */
1281 while ((pid = startjob(NULL, com, donecom)) == -1)
1282 bwait(1);
1283 if (!silent) { /* echo command */
1284 PSERVER *ps;
1285 int psn = pid;
1286 ps = findjob(&psn);
1287 printf("\t%s\n", com);
1288 printf("\tProcess started on %s\n", phostname(ps));
1289 fflush(stdout);
1290 }
1291 psl = findpslot(pid); /* record info. in appropriate slot */
1292 psl->pid = pid;
1293 psl->fout = fout;
1294 psl->rcvf = rf;
1295 return(pid);
1296 }
1297
1298
1299 static void
1300 bwait(int ncoms) /* wait for batch job(s) to finish */
1301 {
1302 int status;
1303
1304 if (noaction)
1305 return;
1306 while ((status = wait4job(NULL, -1)) != -1) {
1307 serverdown(); /* update server status */
1308 if (--ncoms == 0)
1309 break; /* done enough */
1310 }
1311 }
1312
1313
1314 static int
1315 pruncom( /* run a command in parallel over network */
1316 char *com,
1317 char *ppins,
1318 int maxcopies
1319 )
1320 {
1321 int retstatus = 0;
1322 int hostcopies;
1323 char buf[10240], *com1, *s;
1324 int status;
1325 int pfd;
1326 register int n;
1327 register PSERVER *ps;
1328
1329 if (!silent)
1330 printf("\t%s\n", com); /* echo command */
1331 if (noaction)
1332 return(0);
1333 fflush(stdout);
1334 /* start jobs on each server */
1335 for (ps = pslist; ps != NULL; ps = ps->next) {
1336 hostcopies = 0;
1337 if (maxcopies > 1 && ps->nprocs > 1 && ppins != NULL) {
1338 strcpy(com1=buf, com); /* build -PP command */
1339 sprintf(com1+(ppins-com), " -PP %s/%s.persist",
1340 vval(DIRECTORY), phostname(ps));
1341 unlink(com1+(ppins-com)+5);
1342 strcat(com1, ppins);
1343 } else
1344 com1 = com;
1345 while (maxcopies > 0) {
1346 s = savestr(com1);
1347 if (startjob(ps, s, donecom) != -1) {
1348 sleep(20);
1349 hostcopies++;
1350 maxcopies--;
1351 } else {
1352 freestr(s);
1353 break;
1354 }
1355 }
1356 if (!silent && hostcopies) {
1357 if (hostcopies > 1)
1358 printf("\t%d duplicate processes", hostcopies);
1359 else
1360 printf("\tProcess");
1361 printf(" started on %s\n", phostname(ps));
1362 fflush(stdout);
1363 }
1364 }
1365 /* wait for jobs to finish */
1366 while ((status = wait4job(NULL, -1)) != -1)
1367 retstatus += status && !serverdown();
1368 /* terminate parallel rpict's */
1369 for (ps = pslist; ps != NULL; ps = ps->next) {
1370 sprintf(buf, "%s/%s.persist", vval(DIRECTORY), phostname(ps));
1371 if ((pfd = open(buf, O_RDONLY)) >= 0) {
1372 n = read(pfd, buf, sizeof(buf)-1); /* get PID */
1373 buf[n] = '\0';
1374 close(pfd);
1375 for (n = 0; buf[n] && !isspace(buf[n]); n++)
1376 ;
1377 /* terminate */
1378 sprintf(buf, "kill -ALRM %d", atoi(buf+n));
1379 wait4job(ps, startjob(ps, buf, NULL));
1380 }
1381 }
1382 return(retstatus);
1383 }
1384
1385
1386 static int
1387 runcom(char *cs) /* run a command locally and wait for it */
1388 {
1389 if (!silent) /* echo it */
1390 printf("\t%s\n", cs);
1391 if (noaction)
1392 return(0);
1393 fflush(stdout); /* flush output and pass to shell */
1394 return(system(cs));
1395 }
1396
1397
1398 static int
1399 rmfile(char *fn) /* remove a file */
1400 {
1401 if (!silent)
1402 #ifdef _WIN32
1403 printf("\tdel %s\n", fn);
1404 #else
1405 printf("\trm -f %s\n", fn);
1406 #endif
1407 if (noaction)
1408 return(0);
1409 return(unlink(fn));
1410 }
1411
1412
1413 static void
1414 badvalue(int vc) /* report bad variable value and exit */
1415 {
1416 fprintf(stderr, "%s: bad value for variable '%s'\n",
1417 progname, vnam(vc));
1418 quit(1);
1419 }
1420
1421
1422 static char *
1423 dirfile( /* separate path into directory and file */
1424 char *df,
1425 register char *path
1426 )
1427 {
1428 register int i;
1429 int psep;
1430
1431 for (i = 0, psep = -1; path[i]; i++)
1432 if (path[i] == '/')
1433 psep = i;
1434 if (df != NULL) {
1435 if (psep == 0) {
1436 df[0] = '/';
1437 df[1] = '\0';
1438 } else if (psep > 0) {
1439 strncpy(df, path, psep);
1440 df[psep] = '\0';
1441 } else
1442 df[0] = '\0';
1443 }
1444 return(path+psep+1);
1445 }
1446
1447
1448 static int
1449 getblur(double *bf) /* get # blur samples (and fraction) */
1450 {
1451 double blurf;
1452 int nblur;
1453 char *s;
1454
1455 if (!vdef(MBLUR)) {
1456 if (bf != NULL)
1457 *bf = 0.0;
1458 return(0);
1459 }
1460 blurf = atof(vval(MBLUR));
1461 if (blurf < 0.0)
1462 blurf = 0.0;
1463 if (bf != NULL)
1464 *bf = blurf;
1465 if (blurf <= FTINY)
1466 return(0);
1467 s = sskip(vval(MBLUR));
1468 if (!*s)
1469 return(DEF_NBLUR);
1470 nblur = atoi(s);
1471 if (nblur <= 0)
1472 return(1);
1473 return(nblur);
1474 }