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