1 |
– |
/* Copyright (c) 1993 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Generate sections of a picture. |
6 |
|
*/ |
7 |
|
|
8 |
+ |
|
9 |
+ |
#include <stdio.h> |
10 |
+ |
#include <signal.h> |
11 |
+ |
#include <sys/types.h> |
12 |
+ |
#ifndef NON_POSIX /* XXX need abstraction for process management */ |
13 |
+ |
#include <sys/wait.h> |
14 |
+ |
#endif |
15 |
+ |
|
16 |
+ |
#include "platform.h" |
17 |
|
#include "standard.h" |
18 |
+ |
#include "color.h" |
19 |
+ |
#include "view.h" |
20 |
+ |
#include "rtprocess.h" |
21 |
|
|
22 |
|
#ifndef F_SETLKW |
23 |
|
|
24 |
< |
main(argc, argv) |
25 |
< |
int argc; |
26 |
< |
char *argv[]; |
24 |
> |
int |
25 |
> |
main( |
26 |
> |
int argc, |
27 |
> |
char *argv[] |
28 |
> |
) |
29 |
|
{ |
30 |
|
fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]); |
31 |
|
exit(1); |
33 |
|
|
34 |
|
#else |
35 |
|
|
25 |
– |
#include <signal.h> |
26 |
– |
#include "color.h" |
27 |
– |
#include "view.h" |
28 |
– |
#include "resolu.h" |
29 |
– |
|
36 |
|
#ifndef NFS |
37 |
|
#define NFS 1 |
38 |
|
#endif |
50 |
|
#define unguard() sigrelse(SIGALRM) |
51 |
|
#endif |
52 |
|
#ifndef guard_io |
53 |
< |
#define guard_io() 0 |
54 |
< |
#define unguard() 0 |
53 |
> |
#define guard_io() |
54 |
> |
#define unguard() |
55 |
|
#endif |
56 |
+ |
|
57 |
+ |
extern char *strerror(); |
58 |
+ |
|
59 |
|
/* rpict command */ |
60 |
|
char *rpargv[128] = {"rpict", "-S", "1"}; |
61 |
|
int rpargc = 3; |
53 |
– |
int rpd[3]; |
62 |
|
FILE *torp, *fromrp; |
63 |
|
COLR *pbuf; |
64 |
|
/* our view parameters */ |
77 |
|
|
78 |
|
char *progname; |
79 |
|
int verbose = 0; |
80 |
+ |
unsigned timelim = 0; |
81 |
|
int rvrlim = -1; |
82 |
|
|
74 |
– |
extern long lseek(), ftell(); |
75 |
– |
|
83 |
|
int gotalrm = 0; |
84 |
< |
int onalrm() { gotalrm++; } |
84 |
> |
void onalrm(int i) { gotalrm++; } |
85 |
|
|
86 |
+ |
static void dolock(int fd, int ltyp); |
87 |
+ |
static void init(int ac, char **av); |
88 |
+ |
static int nextpiece(int *xp, int *yp); |
89 |
+ |
static int rvrpiece(int *xp, int *yp); |
90 |
+ |
static int cleanup(int rstat); |
91 |
+ |
static void rpiece(void); |
92 |
+ |
static int putpiece(int xpos, int ypos); |
93 |
+ |
static void filerr(char *t); |
94 |
|
|
95 |
< |
main(argc, argv) |
96 |
< |
int argc; |
97 |
< |
char *argv[]; |
95 |
> |
|
96 |
> |
int |
97 |
> |
main( |
98 |
> |
int argc, |
99 |
> |
char *argv[] |
100 |
> |
) |
101 |
|
{ |
102 |
|
register int i, rval; |
103 |
|
|
104 |
|
progname = argv[0]; |
105 |
|
for (i = 1; i < argc; i++) { |
106 |
|
/* expand arguments */ |
107 |
< |
while (rval = expandarg(&argc, &argv, i)) |
108 |
< |
if (rval < 0) { |
109 |
< |
fprintf(stderr, "%s: cannot expand '%s'", |
110 |
< |
argv[0], argv[i]); |
111 |
< |
exit(1); |
112 |
< |
} |
107 |
> |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
108 |
> |
; |
109 |
> |
if (rval < 0) { |
110 |
> |
fprintf(stderr, "%s: cannot expand '%s'", |
111 |
> |
argv[0], argv[i]); |
112 |
> |
exit(1); |
113 |
> |
} |
114 |
|
if (argv[i][0] == '-') |
115 |
|
switch (argv[i][1]) { |
116 |
|
case 'v': |
137 |
|
case 'p': /* pixel aspect ratio? */ |
138 |
|
if (argv[i][2] != 'a' || argv[i][3]) |
139 |
|
break; |
140 |
< |
pixaspect = atof(argv[i+1]); |
140 |
> |
pixaspect = atof(argv[++i]); |
141 |
|
continue; |
142 |
+ |
case 'T': /* time limit (hours) */ |
143 |
+ |
if (argv[i][2]) |
144 |
+ |
break; |
145 |
+ |
timelim = atof(argv[++i])*3600. + .5; |
146 |
+ |
break; |
147 |
|
case 'x': /* overall x resolution */ |
148 |
|
if (argv[i][2]) |
149 |
|
break; |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
< |
dolock(fd, ltyp) /* lock or unlock a file */ |
210 |
< |
int fd; |
211 |
< |
int ltyp; |
209 |
> |
static void |
210 |
> |
dolock( /* lock or unlock a file */ |
211 |
> |
int fd, |
212 |
> |
int ltyp |
213 |
> |
) |
214 |
|
{ |
215 |
|
static struct flock fls; /* static so initialized to zeroes */ |
190 |
– |
extern char *sys_errlist[]; |
216 |
|
|
217 |
|
fls.l_type = ltyp; |
218 |
|
if (fcntl(fd, F_SETLKW, &fls) < 0) { |
219 |
|
fprintf(stderr, "%s: cannot lock/unlock file: %s\n", |
220 |
< |
progname, sys_errlist[errno]); |
220 |
> |
progname, strerror(errno)); |
221 |
|
exit(1); |
222 |
|
} |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
< |
init(ac, av) /* set up output file and start rpict */ |
227 |
< |
int ac; |
228 |
< |
char **av; |
226 |
> |
static void |
227 |
> |
init( /* set up output file and start rpict */ |
228 |
> |
int ac, |
229 |
> |
char **av |
230 |
> |
) |
231 |
|
{ |
232 |
|
static char hrbuf[16], vrbuf[16]; |
233 |
|
extern char VersionID[]; |
234 |
|
char *err; |
235 |
|
FILE *fp; |
236 |
|
int hr, vr; |
237 |
+ |
SUBPROC rpd; /* since we don't close_process(), this can be local */ |
238 |
|
/* set up view */ |
239 |
|
if ((err = setview(&ourview)) != NULL) { |
240 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
261 |
|
dolock(outfd, F_WRLCK); |
262 |
|
if ((fp = fdopen(dup(outfd), "w")) == NULL) |
263 |
|
goto filerr; |
264 |
< |
printargs(ac, av, fp); /* write header */ |
264 |
> |
newheader("RADIANCE", fp); /* create header */ |
265 |
> |
printargs(ac, av, fp); |
266 |
|
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
267 |
|
fputs(VIEWSTR, fp); |
268 |
|
fprintview(&ourview, fp); |
293 |
|
goto filerr; |
294 |
|
dolock(outfd, F_UNLCK); |
295 |
|
/* start rpict process */ |
296 |
< |
if (open_process(rpd, rpargv) <= 0) { |
296 |
> |
if (open_process(&rpd, rpargv) <= 0) { |
297 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
298 |
|
exit(1); |
299 |
|
} |
300 |
< |
if ((fromrp = fdopen(rpd[0], "r")) == NULL || |
301 |
< |
(torp = fdopen(rpd[1], "w")) == NULL) { |
300 |
> |
if ((fromrp = fdopen(rpd.r, "r")) == NULL || |
301 |
> |
(torp = fdopen(rpd.w, "w")) == NULL) { |
302 |
|
fprintf(stderr, "%s: cannot open stream to %s\n", |
303 |
|
progname, rpargv[0]); |
304 |
|
exit(1); |
308 |
|
exit(1); |
309 |
|
} |
310 |
|
signal(SIGALRM, onalrm); |
311 |
+ |
if (timelim) |
312 |
+ |
alarm(timelim); |
313 |
|
return; |
314 |
|
filerr: |
315 |
|
fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile); |
317 |
|
} |
318 |
|
|
319 |
|
|
320 |
< |
int |
321 |
< |
nextpiece(xp, yp) /* get next piece assignment */ |
322 |
< |
int *xp, *yp; |
320 |
> |
static int |
321 |
> |
nextpiece( /* get next piece assignment */ |
322 |
> |
int *xp, |
323 |
> |
int *yp |
324 |
> |
) |
325 |
|
{ |
326 |
|
if (gotalrm) /* someone wants us to quit */ |
327 |
|
return(0); |
361 |
|
} |
362 |
|
|
363 |
|
|
364 |
< |
int |
365 |
< |
rvrpiece(xp, yp) /* check for recoverable pieces */ |
366 |
< |
register int *xp, *yp; |
364 |
> |
static int |
365 |
> |
rvrpiece( /* check for recoverable pieces */ |
366 |
> |
register int *xp, |
367 |
> |
register int *yp |
368 |
> |
) |
369 |
|
{ |
370 |
|
static char *pdone = NULL; /* which pieces are done */ |
371 |
|
static long readpos = -1; /* how far we've read */ |
379 |
|
return(0); /* only check if asked */ |
380 |
|
if (pdone == NULL) /* first call */ |
381 |
|
pdone = calloc(hmult*vmult, sizeof(char)); |
382 |
+ |
if (pdone == NULL) { |
383 |
+ |
fprintf(stderr, "%s: out of memory\n", progname); |
384 |
+ |
exit(1); |
385 |
+ |
} |
386 |
|
if (readpos != -1) /* mark what's been done */ |
387 |
|
fseek(syncfp, readpos, 0); |
388 |
|
while (fscanf(syncfp, "%d %d", xp, yp) == 2) |
407 |
|
} |
408 |
|
|
409 |
|
|
410 |
< |
int |
411 |
< |
cleanup(rstat) /* close rpict process and clean up */ |
412 |
< |
int rstat; |
410 |
> |
static int |
411 |
> |
cleanup( /* close rpict process and clean up */ |
412 |
> |
int rstat |
413 |
> |
) |
414 |
|
{ |
415 |
|
int status; |
416 |
|
|
424 |
|
} |
425 |
|
|
426 |
|
|
427 |
< |
rpiece() /* render picture piece by piece */ |
427 |
> |
static void |
428 |
> |
rpiece(void) /* render picture piece by piece */ |
429 |
|
{ |
430 |
|
VIEW pview; |
431 |
|
int xorg, yorg; |
432 |
|
/* compute view parameters */ |
433 |
< |
copystruct(&pview, &ourview); |
433 |
> |
pview = ourview; |
434 |
|
switch (ourview.type) { |
435 |
|
case VT_PER: |
436 |
|
pview.horiz = 2.*180./PI*atan( |
443 |
|
pview.horiz = ourview.horiz / hmult; |
444 |
|
pview.vert = ourview.vert / vmult; |
445 |
|
break; |
446 |
+ |
case VT_CYL: |
447 |
+ |
pview.horiz = ourview.horiz / hmult; |
448 |
+ |
pview.vert = 2.*180./PI*atan( |
449 |
+ |
tan(PI/180./2.*ourview.vert)/vmult ); |
450 |
+ |
break; |
451 |
|
case VT_HEM: |
452 |
|
pview.horiz = 2.*180./PI*asin( |
453 |
|
sin(PI/180./2.*ourview.horiz)/hmult ); |
461 |
|
} |
462 |
|
/* render each piece */ |
463 |
|
while (nextpiece(&xorg, &yorg)) { |
464 |
< |
pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1); |
465 |
< |
pview.voff = ourview.voff + yorg - 0.5*(vmult-1); |
464 |
> |
pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1); |
465 |
> |
pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1); |
466 |
|
fputs(VIEWSTR, torp); |
467 |
|
fprintview(&pview, torp); |
468 |
|
putc('\n', torp); |
472 |
|
} |
473 |
|
|
474 |
|
|
475 |
< |
int |
476 |
< |
putpiece(xpos, ypos) /* get next piece from rpict */ |
477 |
< |
int xpos, ypos; |
475 |
> |
static int |
476 |
> |
putpiece( /* get next piece from rpict */ |
477 |
> |
int xpos, |
478 |
> |
int ypos |
479 |
> |
) |
480 |
|
{ |
481 |
|
struct flock fls; |
482 |
|
int pid, status; |
483 |
|
int hr, vr; |
484 |
|
register int y; |
485 |
|
/* check bounds */ |
486 |
< |
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
486 |
> |
if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) { |
487 |
|
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
488 |
|
progname, xpos, ypos); |
489 |
|
exit(cleanup(1)); |
491 |
|
/* check header from rpict */ |
492 |
|
guard_io(); |
493 |
|
getheader(fromrp, NULL, NULL); |
494 |
< |
if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) { |
494 |
> |
if (!fscnresolu(&hr, &vr, fromrp) || (hr != hres) | (vr != vres)) { |
495 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
496 |
|
progname, rpargv[0]); |
497 |
|
exit(cleanup(1)); |
536 |
|
filerr("lock"); |
537 |
|
#endif |
538 |
|
/* write new piece to file */ |
539 |
< |
if (lseek(outfd, fls.l_start, 0) == -1) |
539 |
> |
if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0) |
540 |
|
filerr("seek"); |
541 |
|
if (hmult == 1) { |
542 |
|
if (writebuf(outfd, (char *)pbuf, |
548 |
|
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
549 |
|
filerr("write"); |
550 |
|
if (y < vr-1 && lseek(outfd, |
551 |
< |
(long)(hmult-1)*hr*sizeof(COLR), |
552 |
< |
1) == -1) |
551 |
> |
(off_t)(hmult-1)*hr*sizeof(COLR), |
552 |
> |
SEEK_CUR) < 0) |
553 |
|
filerr("seek"); |
554 |
|
} |
555 |
|
#if NFS |
557 |
|
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
558 |
|
filerr("lock"); |
559 |
|
#endif |
560 |
+ |
if (verbose) { /* notify caller */ |
561 |
+ |
printf("%d %d done\n", xpos, ypos); |
562 |
+ |
fflush(stdout); |
563 |
+ |
} |
564 |
|
if (syncfp != NULL) { /* record what's been done */ |
565 |
|
sflock(F_WRLCK); |
566 |
|
fseek(syncfp, 0L, 2); /* append index */ |
569 |
|
/*** Unlock not necessary, since |
570 |
|
sflock(F_UNLCK); _exit() or nextpiece() is next ***/ |
571 |
|
} |
520 |
– |
if (verbose) { /* notify caller */ |
521 |
– |
printf("%d %d done\n", xpos, ypos); |
522 |
– |
fflush(stdout); |
523 |
– |
} |
572 |
|
if (pid == -1) /* didn't fork or fork failed */ |
573 |
|
return(0); |
574 |
|
_exit(0); /* else exit child process (releasing locks) */ |
575 |
|
} |
576 |
|
|
577 |
|
|
578 |
< |
filerr(t) /* report file error and exit */ |
579 |
< |
char *t; |
578 |
> |
static void |
579 |
> |
filerr( /* report file error and exit */ |
580 |
> |
char *t |
581 |
> |
) |
582 |
|
{ |
533 |
– |
extern char *sys_errlist[]; |
534 |
– |
|
583 |
|
fprintf(stderr, "%s: %s error on file \"%s\": %s\n", |
584 |
< |
progname, t, outfile, sys_errlist[errno]); |
584 |
> |
progname, t, outfile, strerror(errno)); |
585 |
|
_exit(1); |
586 |
|
} |
587 |
|
|