1 |
– |
/* Copyright (c) 1995 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 |
+ |
|
13 |
+ |
#include "platform.h" |
14 |
+ |
#ifndef NON_POSIX /* XXX need abstraction for process management */ |
15 |
+ |
#include <sys/wait.h> |
16 |
+ |
#endif |
17 |
+ |
|
18 |
|
#include "standard.h" |
19 |
+ |
#include "color.h" |
20 |
+ |
#include "view.h" |
21 |
+ |
#include "rtprocess.h" |
22 |
|
|
23 |
|
#ifndef F_SETLKW |
24 |
|
|
25 |
< |
main(argc, argv) |
26 |
< |
int argc; |
27 |
< |
char *argv[]; |
25 |
> |
int |
26 |
> |
main( |
27 |
> |
int argc, |
28 |
> |
char *argv[] |
29 |
> |
) |
30 |
|
{ |
31 |
|
fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]); |
32 |
|
exit(1); |
34 |
|
|
35 |
|
#else |
36 |
|
|
25 |
– |
#include <signal.h> |
26 |
– |
#include "color.h" |
27 |
– |
#include "view.h" |
28 |
– |
#include "resolu.h" |
29 |
– |
|
37 |
|
#ifndef NFS |
38 |
|
#define NFS 1 |
39 |
|
#endif |
51 |
|
#define unguard() sigrelse(SIGALRM) |
52 |
|
#endif |
53 |
|
#ifndef guard_io |
54 |
< |
#define guard_io() 0 |
55 |
< |
#define unguard() 0 |
54 |
> |
#define guard_io() |
55 |
> |
#define unguard() |
56 |
|
#endif |
57 |
+ |
|
58 |
+ |
extern char *strerror(); |
59 |
+ |
|
60 |
|
/* rpict command */ |
61 |
|
char *rpargv[128] = {"rpict", "-S", "1"}; |
62 |
|
int rpargc = 3; |
53 |
– |
int rpd[3]; |
63 |
|
FILE *torp, *fromrp; |
64 |
|
COLR *pbuf; |
65 |
|
/* our view parameters */ |
81 |
|
unsigned timelim = 0; |
82 |
|
int rvrlim = -1; |
83 |
|
|
75 |
– |
extern long lseek(), ftell(); |
76 |
– |
|
84 |
|
int gotalrm = 0; |
85 |
< |
int onalrm() { gotalrm++; } |
85 |
> |
void onalrm(int i) { gotalrm++; } |
86 |
|
|
87 |
+ |
static void dolock(int fd, int ltyp); |
88 |
+ |
static void init(int ac, char **av); |
89 |
+ |
static int nextpiece(int *xp, int *yp); |
90 |
+ |
static int rvrpiece(int *xp, int *yp); |
91 |
+ |
static int cleanup(int rstat); |
92 |
+ |
static void rpiece(void); |
93 |
+ |
static int putpiece(int xpos, int ypos); |
94 |
+ |
static void filerr(char *t); |
95 |
|
|
96 |
< |
main(argc, argv) |
97 |
< |
int argc; |
98 |
< |
char *argv[]; |
96 |
> |
|
97 |
> |
int |
98 |
> |
main( |
99 |
> |
int argc, |
100 |
> |
char *argv[] |
101 |
> |
) |
102 |
|
{ |
103 |
|
register int i, rval; |
104 |
|
|
105 |
|
progname = argv[0]; |
106 |
|
for (i = 1; i < argc; i++) { |
107 |
|
/* expand arguments */ |
108 |
< |
while (rval = expandarg(&argc, &argv, i)) |
109 |
< |
if (rval < 0) { |
110 |
< |
fprintf(stderr, "%s: cannot expand '%s'", |
111 |
< |
argv[0], argv[i]); |
112 |
< |
exit(1); |
113 |
< |
} |
108 |
> |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
109 |
> |
; |
110 |
> |
if (rval < 0) { |
111 |
> |
fprintf(stderr, "%s: cannot expand '%s'\n", |
112 |
> |
argv[0], argv[i]); |
113 |
> |
exit(1); |
114 |
> |
} |
115 |
|
if (argv[i][0] == '-') |
116 |
|
switch (argv[i][1]) { |
117 |
|
case 'v': |
136 |
|
} |
137 |
|
break; |
138 |
|
case 'p': /* pixel aspect ratio? */ |
139 |
+ |
if (argv[i][2] == 'm') { |
140 |
+ |
fprintf(stderr, "%s: -pm unsupported\n", |
141 |
+ |
argv[0]); |
142 |
+ |
++i; |
143 |
+ |
continue; |
144 |
+ |
} |
145 |
|
if (argv[i][2] != 'a' || argv[i][3]) |
146 |
|
break; |
147 |
< |
pixaspect = atof(argv[i+1]); |
147 |
> |
pixaspect = atof(argv[++i]); |
148 |
|
continue; |
149 |
|
case 'T': /* time limit (hours) */ |
150 |
|
if (argv[i][2]) |
151 |
|
break; |
152 |
|
timelim = atof(argv[++i])*3600. + .5; |
153 |
< |
break; |
153 |
> |
continue; |
154 |
|
case 'x': /* overall x resolution */ |
155 |
|
if (argv[i][2]) |
156 |
|
break; |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
< |
dolock(fd, ltyp) /* lock or unlock a file */ |
217 |
< |
int fd; |
218 |
< |
int ltyp; |
216 |
> |
static void |
217 |
> |
dolock( /* lock or unlock a file */ |
218 |
> |
int fd, |
219 |
> |
int ltyp |
220 |
> |
) |
221 |
|
{ |
222 |
|
static struct flock fls; /* static so initialized to zeroes */ |
196 |
– |
extern char *sys_errlist[]; |
223 |
|
|
224 |
|
fls.l_type = ltyp; |
225 |
|
if (fcntl(fd, F_SETLKW, &fls) < 0) { |
226 |
|
fprintf(stderr, "%s: cannot lock/unlock file: %s\n", |
227 |
< |
progname, sys_errlist[errno]); |
227 |
> |
progname, strerror(errno)); |
228 |
|
exit(1); |
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
< |
init(ac, av) /* set up output file and start rpict */ |
234 |
< |
int ac; |
235 |
< |
char **av; |
233 |
> |
static void |
234 |
> |
init( /* set up output file and start rpict */ |
235 |
> |
int ac, |
236 |
> |
char **av |
237 |
> |
) |
238 |
|
{ |
239 |
|
static char hrbuf[16], vrbuf[16]; |
240 |
|
extern char VersionID[]; |
241 |
|
char *err; |
242 |
|
FILE *fp; |
243 |
|
int hr, vr; |
244 |
+ |
SUBPROC rpd; /* since we don't close_process(), this can be local */ |
245 |
|
/* set up view */ |
246 |
|
if ((err = setview(&ourview)) != NULL) { |
247 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
255 |
|
/* compute piece size */ |
256 |
|
hres /= hmult; |
257 |
|
vres /= vmult; |
258 |
+ |
if (hres <= 0 || vres <= 0) { |
259 |
+ |
fprintf(stderr, "%s: illegal resolution/subdivision\n", progname); |
260 |
+ |
exit(1); |
261 |
+ |
} |
262 |
|
normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres); |
263 |
|
sprintf(hrbuf, "%d", hres); |
264 |
|
rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf; |
278 |
|
fputs(VIEWSTR, fp); |
279 |
|
fprintview(&ourview, fp); |
280 |
|
putc('\n', fp); |
281 |
+ |
fputnow(fp); |
282 |
|
if (pixaspect < .99 || pixaspect > 1.01) |
283 |
|
fputaspect(pixaspect, fp); |
284 |
|
fputformat(COLRFMT, fp); |
305 |
|
goto filerr; |
306 |
|
dolock(outfd, F_UNLCK); |
307 |
|
/* start rpict process */ |
308 |
< |
if (open_process(rpd, rpargv) <= 0) { |
308 |
> |
if (open_process(&rpd, rpargv) <= 0) { |
309 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
310 |
|
exit(1); |
311 |
|
} |
312 |
< |
if ((fromrp = fdopen(rpd[0], "r")) == NULL || |
313 |
< |
(torp = fdopen(rpd[1], "w")) == NULL) { |
312 |
> |
if ((fromrp = fdopen(rpd.r, "r")) == NULL || |
313 |
> |
(torp = fdopen(rpd.w, "w")) == NULL) { |
314 |
|
fprintf(stderr, "%s: cannot open stream to %s\n", |
315 |
|
progname, rpargv[0]); |
316 |
|
exit(1); |
329 |
|
} |
330 |
|
|
331 |
|
|
332 |
< |
int |
333 |
< |
nextpiece(xp, yp) /* get next piece assignment */ |
334 |
< |
int *xp, *yp; |
332 |
> |
static int |
333 |
> |
nextpiece( /* get next piece assignment */ |
334 |
> |
int *xp, |
335 |
> |
int *yp |
336 |
> |
) |
337 |
|
{ |
338 |
|
if (gotalrm) /* someone wants us to quit */ |
339 |
|
return(0); |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
< |
int |
377 |
< |
rvrpiece(xp, yp) /* check for recoverable pieces */ |
378 |
< |
register int *xp, *yp; |
376 |
> |
static int |
377 |
> |
rvrpiece( /* check for recoverable pieces */ |
378 |
> |
register int *xp, |
379 |
> |
register int *yp |
380 |
> |
) |
381 |
|
{ |
382 |
|
static char *pdone = NULL; /* which pieces are done */ |
383 |
|
static long readpos = -1; /* how far we've read */ |
419 |
|
} |
420 |
|
|
421 |
|
|
422 |
< |
int |
423 |
< |
cleanup(rstat) /* close rpict process and clean up */ |
424 |
< |
int rstat; |
422 |
> |
static int |
423 |
> |
cleanup( /* close rpict process and clean up */ |
424 |
> |
int rstat |
425 |
> |
) |
426 |
|
{ |
427 |
|
int status; |
428 |
|
|
436 |
|
} |
437 |
|
|
438 |
|
|
439 |
< |
rpiece() /* render picture piece by piece */ |
439 |
> |
static void |
440 |
> |
rpiece(void) /* render picture piece by piece */ |
441 |
|
{ |
442 |
|
VIEW pview; |
443 |
|
int xorg, yorg; |
444 |
|
/* compute view parameters */ |
445 |
< |
copystruct(&pview, &ourview); |
445 |
> |
pview = ourview; |
446 |
|
switch (ourview.type) { |
447 |
|
case VT_PER: |
448 |
< |
pview.horiz = 2.*180./PI*atan( |
449 |
< |
tan(PI/180./2.*ourview.horiz)/hmult ); |
450 |
< |
pview.vert = 2.*180./PI*atan( |
451 |
< |
tan(PI/180./2.*ourview.vert)/vmult ); |
448 |
> |
pview.horiz = (2.*180./PI)*atan( |
449 |
> |
tan((PI/180./2.)*ourview.horiz)/hmult ); |
450 |
> |
pview.vert = (2.*180./PI)*atan( |
451 |
> |
tan((PI/180./2.)*ourview.vert)/vmult ); |
452 |
|
break; |
453 |
|
case VT_PAR: |
454 |
|
case VT_ANG: |
455 |
|
pview.horiz = ourview.horiz / hmult; |
456 |
|
pview.vert = ourview.vert / vmult; |
457 |
|
break; |
458 |
+ |
case VT_CYL: |
459 |
+ |
pview.horiz = ourview.horiz / hmult; |
460 |
+ |
pview.vert = (2.*180./PI)*atan( |
461 |
+ |
tan((PI/180./2.)*ourview.vert)/vmult ); |
462 |
+ |
break; |
463 |
|
case VT_HEM: |
464 |
< |
pview.horiz = 2.*180./PI*asin( |
465 |
< |
sin(PI/180./2.*ourview.horiz)/hmult ); |
466 |
< |
pview.vert = 2.*180./PI*asin( |
467 |
< |
sin(PI/180./2.*ourview.vert)/vmult ); |
464 |
> |
pview.horiz = (2.*180./PI)*asin( |
465 |
> |
sin((PI/180./2.)*ourview.horiz)/hmult ); |
466 |
> |
pview.vert = (2.*180./PI)*asin( |
467 |
> |
sin((PI/180./2.)*ourview.vert)/vmult ); |
468 |
|
break; |
469 |
+ |
case VT_PLS: |
470 |
+ |
pview.horiz = sin((PI/180./2.)*ourview.horiz) / |
471 |
+ |
(1.0 + cos((PI/180./2.)*ourview.horiz)) / hmult; |
472 |
+ |
pview.horiz *= pview.horiz; |
473 |
+ |
pview.horiz = (2.*180./PI)*acos((1. - pview.horiz) / |
474 |
+ |
(1. + pview.horiz)); |
475 |
+ |
pview.vert = sin((PI/180./2.)*ourview.vert) / |
476 |
+ |
(1.0 + cos((PI/180./2.)*ourview.vert)) / vmult; |
477 |
+ |
pview.vert *= pview.vert; |
478 |
+ |
pview.vert = (2.*180./PI)*acos((1. - pview.vert) / |
479 |
+ |
(1. + pview.vert)); |
480 |
+ |
break; |
481 |
|
default: |
482 |
|
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
483 |
|
progname, ourview.type); |
485 |
|
} |
486 |
|
/* render each piece */ |
487 |
|
while (nextpiece(&xorg, &yorg)) { |
488 |
< |
pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1); |
489 |
< |
pview.voff = ourview.voff + yorg - 0.5*(vmult-1); |
488 |
> |
pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1); |
489 |
> |
pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1); |
490 |
|
fputs(VIEWSTR, torp); |
491 |
|
fprintview(&pview, torp); |
492 |
|
putc('\n', torp); |
496 |
|
} |
497 |
|
|
498 |
|
|
499 |
< |
int |
500 |
< |
putpiece(xpos, ypos) /* get next piece from rpict */ |
501 |
< |
int xpos, ypos; |
499 |
> |
static int |
500 |
> |
putpiece( /* get next piece from rpict */ |
501 |
> |
int xpos, |
502 |
> |
int ypos |
503 |
> |
) |
504 |
|
{ |
505 |
|
struct flock fls; |
506 |
|
int pid, status; |
507 |
|
int hr, vr; |
508 |
|
register int y; |
509 |
|
/* check bounds */ |
510 |
< |
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
510 |
> |
if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) { |
511 |
|
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
512 |
|
progname, xpos, ypos); |
513 |
|
exit(cleanup(1)); |
515 |
|
/* check header from rpict */ |
516 |
|
guard_io(); |
517 |
|
getheader(fromrp, NULL, NULL); |
518 |
< |
if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) { |
518 |
> |
if (!fscnresolu(&hr, &vr, fromrp) || (hr != hres) | (vr != vres)) { |
519 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
520 |
|
progname, rpargv[0]); |
521 |
|
exit(cleanup(1)); |
560 |
|
filerr("lock"); |
561 |
|
#endif |
562 |
|
/* write new piece to file */ |
563 |
< |
if (lseek(outfd, fls.l_start, 0) == -1) |
563 |
> |
if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0) |
564 |
|
filerr("seek"); |
565 |
|
if (hmult == 1) { |
566 |
|
if (writebuf(outfd, (char *)pbuf, |
572 |
|
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
573 |
|
filerr("write"); |
574 |
|
if (y < vr-1 && lseek(outfd, |
575 |
< |
(long)(hmult-1)*hr*sizeof(COLR), |
576 |
< |
1) == -1) |
575 |
> |
(off_t)(hmult-1)*hr*sizeof(COLR), |
576 |
> |
SEEK_CUR) < 0) |
577 |
|
filerr("seek"); |
578 |
|
} |
579 |
|
#if NFS |
581 |
|
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
582 |
|
filerr("lock"); |
583 |
|
#endif |
584 |
+ |
if (verbose) { /* notify caller */ |
585 |
+ |
printf("%d %d done\n", xpos, ypos); |
586 |
+ |
fflush(stdout); |
587 |
+ |
} |
588 |
|
if (syncfp != NULL) { /* record what's been done */ |
589 |
|
sflock(F_WRLCK); |
590 |
|
fseek(syncfp, 0L, 2); /* append index */ |
593 |
|
/*** Unlock not necessary, since |
594 |
|
sflock(F_UNLCK); _exit() or nextpiece() is next ***/ |
595 |
|
} |
533 |
– |
if (verbose) { /* notify caller */ |
534 |
– |
printf("%d %d done\n", xpos, ypos); |
535 |
– |
fflush(stdout); |
536 |
– |
} |
596 |
|
if (pid == -1) /* didn't fork or fork failed */ |
597 |
|
return(0); |
598 |
|
_exit(0); /* else exit child process (releasing locks) */ |
599 |
|
} |
600 |
|
|
601 |
|
|
602 |
< |
filerr(t) /* report file error and exit */ |
603 |
< |
char *t; |
602 |
> |
static void |
603 |
> |
filerr( /* report file error and exit */ |
604 |
> |
char *t |
605 |
> |
) |
606 |
|
{ |
546 |
– |
extern char *sys_errlist[]; |
547 |
– |
|
607 |
|
fprintf(stderr, "%s: %s error on file \"%s\": %s\n", |
608 |
< |
progname, t, outfile, sys_errlist[errno]); |
608 |
> |
progname, t, outfile, strerror(errno)); |
609 |
|
_exit(1); |
610 |
|
} |
611 |
|
|