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 |
+ |
|
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 */ |
78 |
|
|
79 |
|
char *progname; |
80 |
|
int verbose = 0; |
81 |
+ |
int nowarn = 0; |
82 |
+ |
unsigned timelim = 0; |
83 |
|
int rvrlim = -1; |
84 |
|
|
74 |
– |
extern long lseek(), ftell(); |
75 |
– |
|
85 |
|
int gotalrm = 0; |
86 |
< |
int onalrm() { gotalrm++; } |
86 |
> |
void onalrm(int i) { gotalrm++; } |
87 |
|
|
88 |
+ |
static void dolock(int fd, int ltyp); |
89 |
+ |
static void init(int ac, char **av); |
90 |
+ |
static int nextpiece(int *xp, int *yp); |
91 |
+ |
static int rvrpiece(int *xp, int *yp); |
92 |
+ |
static int cleanup(int rstat); |
93 |
+ |
static void rpiece(void); |
94 |
+ |
static int putpiece(int xpos, int ypos); |
95 |
+ |
static void filerr(char *t); |
96 |
|
|
97 |
< |
main(argc, argv) |
98 |
< |
int argc; |
99 |
< |
char *argv[]; |
97 |
> |
|
98 |
> |
int |
99 |
> |
main( |
100 |
> |
int argc, |
101 |
> |
char *argv[] |
102 |
> |
) |
103 |
|
{ |
104 |
|
register int i, rval; |
105 |
|
|
106 |
|
progname = argv[0]; |
107 |
|
for (i = 1; i < argc; i++) { |
108 |
|
/* expand arguments */ |
109 |
< |
while (rval = expandarg(&argc, &argv, i)) |
110 |
< |
if (rval < 0) { |
111 |
< |
fprintf(stderr, "%s: cannot expand '%s'", |
112 |
< |
argv[0], argv[i]); |
113 |
< |
exit(1); |
114 |
< |
} |
109 |
> |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
110 |
> |
; |
111 |
> |
if (rval < 0) { |
112 |
> |
fprintf(stderr, "%s: cannot expand '%s'\n", |
113 |
> |
argv[0], argv[i]); |
114 |
> |
exit(1); |
115 |
> |
} |
116 |
|
if (argv[i][0] == '-') |
117 |
|
switch (argv[i][1]) { |
118 |
+ |
case 'w': |
119 |
+ |
if (!argv[i][2]) |
120 |
+ |
nowarn = !nowarn; |
121 |
+ |
else if (argv[i][2] == '+') |
122 |
+ |
nowarn = 0; |
123 |
+ |
else if (argv[i][2] == '-') |
124 |
+ |
nowarn = 1; |
125 |
+ |
break; |
126 |
|
case 'v': |
127 |
|
switch (argv[i][2]) { |
128 |
|
case '\0': /* verbose option */ |
145 |
|
} |
146 |
|
break; |
147 |
|
case 'p': /* pixel aspect ratio? */ |
148 |
+ |
if (argv[i][2] == 'm') { |
149 |
+ |
fprintf(stderr, "%s: -pm unsupported\n", |
150 |
+ |
argv[0]); |
151 |
+ |
++i; |
152 |
+ |
continue; |
153 |
+ |
} |
154 |
|
if (argv[i][2] != 'a' || argv[i][3]) |
155 |
|
break; |
156 |
< |
pixaspect = atof(argv[i+1]); |
156 |
> |
pixaspect = atof(argv[++i]); |
157 |
|
continue; |
158 |
+ |
case 'T': /* time limit (hours) */ |
159 |
+ |
if (argv[i][2]) |
160 |
+ |
break; |
161 |
+ |
timelim = atof(argv[++i])*3600. + .5; |
162 |
+ |
continue; |
163 |
|
case 'x': /* overall x resolution */ |
164 |
|
if (argv[i][2]) |
165 |
|
break; |
188 |
|
case 'F': /* syncronization file */ |
189 |
|
if (argv[i][2]) |
190 |
|
break; |
191 |
< |
if ((syncfp = fopen(argv[++i],"r+")) == NULL) { |
191 |
> |
if ((syncfp = |
192 |
> |
fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) { |
193 |
|
fprintf(stderr, "%s: cannot open\n", |
194 |
|
argv[i]); |
195 |
|
exit(1); |
204 |
|
break; |
205 |
|
outfile = argv[++i]; |
206 |
|
continue; |
207 |
< |
} else if (i >= argc-1) |
208 |
< |
break; |
207 |
> |
} |
208 |
> |
else if (i >= argc-1) |
209 |
> |
break; |
210 |
|
rpargv[rpargc++] = argv[i]; |
211 |
|
} |
212 |
|
if (i >= argc) { |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
< |
dolock(fd, ltyp) /* lock or unlock a file */ |
227 |
< |
int fd; |
228 |
< |
int ltyp; |
226 |
> |
static void |
227 |
> |
dolock( /* lock or unlock a file */ |
228 |
> |
int fd, |
229 |
> |
int ltyp |
230 |
> |
) |
231 |
|
{ |
232 |
|
static struct flock fls; /* static so initialized to zeroes */ |
233 |
|
|
234 |
|
fls.l_type = ltyp; |
235 |
|
if (fcntl(fd, F_SETLKW, &fls) < 0) { |
236 |
< |
fprintf(stderr, "%s: cannot lock/unlock file\n", progname); |
236 |
> |
fprintf(stderr, "%s: cannot lock/unlock file: %s\n", |
237 |
> |
progname, strerror(errno)); |
238 |
|
exit(1); |
239 |
|
} |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
< |
init(ac, av) /* set up output file and start rpict */ |
244 |
< |
int ac; |
245 |
< |
char **av; |
243 |
> |
static void |
244 |
> |
init( /* set up output file and start rpict */ |
245 |
> |
int ac, |
246 |
> |
char **av |
247 |
> |
) |
248 |
|
{ |
249 |
|
static char hrbuf[16], vrbuf[16]; |
250 |
|
extern char VersionID[]; |
251 |
|
char *err; |
252 |
|
FILE *fp; |
253 |
|
int hr, vr; |
254 |
+ |
SUBPROC rpd; /* since we don't close_process(), this can be local */ |
255 |
|
/* set up view */ |
256 |
|
if ((err = setview(&ourview)) != NULL) { |
257 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
263 |
|
sflock(F_UNLCK); |
264 |
|
} |
265 |
|
/* compute piece size */ |
266 |
+ |
hr = hres; vr = vres; |
267 |
+ |
if (pixaspect > FTINY) |
268 |
+ |
normaspect(viewaspect(&ourview), &pixaspect, &hr, &vr); |
269 |
|
hres /= hmult; |
270 |
|
vres /= vmult; |
271 |
+ |
if (hres <= 0 || vres <= 0) { |
272 |
+ |
fprintf(stderr, "%s: illegal resolution/subdivision\n", progname); |
273 |
+ |
exit(1); |
274 |
+ |
} |
275 |
|
normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres); |
276 |
+ |
if (!nowarn && (hr != hres*hmult) | (vr != vres*vmult)) |
277 |
+ |
fprintf(stderr, |
278 |
+ |
"%s: warning - resolution changed from %dx%d to %dx%d\n", |
279 |
+ |
progname, hr, vr, hres*hmult, vres*vmult); |
280 |
|
sprintf(hrbuf, "%d", hres); |
281 |
|
rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf; |
282 |
|
sprintf(vrbuf, "%d", vres); |
289 |
|
dolock(outfd, F_WRLCK); |
290 |
|
if ((fp = fdopen(dup(outfd), "w")) == NULL) |
291 |
|
goto filerr; |
292 |
< |
printargs(ac, av, fp); /* write header */ |
292 |
> |
newheader("RADIANCE", fp); /* create header */ |
293 |
> |
printargs(ac, av, fp); |
294 |
|
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
295 |
|
fputs(VIEWSTR, fp); |
296 |
|
fprintview(&ourview, fp); |
297 |
|
putc('\n', fp); |
298 |
+ |
fputnow(fp); |
299 |
|
if (pixaspect < .99 || pixaspect > 1.01) |
300 |
|
fputaspect(pixaspect, fp); |
301 |
|
fputformat(COLRFMT, fp); |
322 |
|
goto filerr; |
323 |
|
dolock(outfd, F_UNLCK); |
324 |
|
/* start rpict process */ |
325 |
< |
if (open_process(rpd, rpargv) <= 0) { |
325 |
> |
if (open_process(&rpd, rpargv) <= 0) { |
326 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
327 |
|
exit(1); |
328 |
|
} |
329 |
< |
if ((fromrp = fdopen(rpd[0], "r")) == NULL || |
330 |
< |
(torp = fdopen(rpd[1], "w")) == NULL) { |
329 |
> |
if ((fromrp = fdopen(rpd.r, "r")) == NULL || |
330 |
> |
(torp = fdopen(rpd.w, "w")) == NULL) { |
331 |
|
fprintf(stderr, "%s: cannot open stream to %s\n", |
332 |
|
progname, rpargv[0]); |
333 |
|
exit(1); |
337 |
|
exit(1); |
338 |
|
} |
339 |
|
signal(SIGALRM, onalrm); |
340 |
+ |
if (timelim) |
341 |
+ |
alarm(timelim); |
342 |
|
return; |
343 |
|
filerr: |
344 |
|
fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile); |
346 |
|
} |
347 |
|
|
348 |
|
|
349 |
< |
int |
350 |
< |
nextpiece(xp, yp) /* get next piece assignment */ |
351 |
< |
int *xp, *yp; |
349 |
> |
static int |
350 |
> |
nextpiece( /* get next piece assignment */ |
351 |
> |
int *xp, |
352 |
> |
int *yp |
353 |
> |
) |
354 |
|
{ |
355 |
|
if (gotalrm) /* someone wants us to quit */ |
356 |
|
return(0); |
390 |
|
} |
391 |
|
|
392 |
|
|
393 |
< |
int |
394 |
< |
rvrpiece(xp, yp) /* check for recoverable pieces */ |
395 |
< |
register int *xp, *yp; |
393 |
> |
static int |
394 |
> |
rvrpiece( /* check for recoverable pieces */ |
395 |
> |
register int *xp, |
396 |
> |
register int *yp |
397 |
> |
) |
398 |
|
{ |
399 |
|
static char *pdone = NULL; /* which pieces are done */ |
400 |
|
static long readpos = -1; /* how far we've read */ |
408 |
|
return(0); /* only check if asked */ |
409 |
|
if (pdone == NULL) /* first call */ |
410 |
|
pdone = calloc(hmult*vmult, sizeof(char)); |
411 |
+ |
if (pdone == NULL) { |
412 |
+ |
fprintf(stderr, "%s: out of memory\n", progname); |
413 |
+ |
exit(1); |
414 |
+ |
} |
415 |
|
if (readpos != -1) /* mark what's been done */ |
416 |
|
fseek(syncfp, readpos, 0); |
417 |
|
while (fscanf(syncfp, "%d %d", xp, yp) == 2) |
436 |
|
} |
437 |
|
|
438 |
|
|
439 |
< |
int |
440 |
< |
cleanup(rstat) /* close rpict process and clean up */ |
441 |
< |
int rstat; |
439 |
> |
static int |
440 |
> |
cleanup( /* close rpict process and clean up */ |
441 |
> |
int rstat |
442 |
> |
) |
443 |
|
{ |
444 |
|
int status; |
445 |
|
|
453 |
|
} |
454 |
|
|
455 |
|
|
456 |
< |
rpiece() /* render picture piece by piece */ |
456 |
> |
static void |
457 |
> |
rpiece(void) /* render picture piece by piece */ |
458 |
|
{ |
459 |
|
VIEW pview; |
460 |
|
int xorg, yorg; |
461 |
|
/* compute view parameters */ |
462 |
< |
copystruct(&pview, &ourview); |
462 |
> |
pview = ourview; |
463 |
|
switch (ourview.type) { |
464 |
|
case VT_PER: |
465 |
< |
pview.horiz = 2.*180./PI*atan( |
466 |
< |
tan(PI/180./2.*ourview.horiz)/hmult ); |
467 |
< |
pview.vert = 2.*180./PI*atan( |
468 |
< |
tan(PI/180./2.*ourview.vert)/vmult ); |
465 |
> |
pview.horiz = (2.*180./PI)*atan( |
466 |
> |
tan((PI/180./2.)*ourview.horiz)/hmult ); |
467 |
> |
pview.vert = (2.*180./PI)*atan( |
468 |
> |
tan((PI/180./2.)*ourview.vert)/vmult ); |
469 |
|
break; |
470 |
|
case VT_PAR: |
471 |
|
case VT_ANG: |
472 |
|
pview.horiz = ourview.horiz / hmult; |
473 |
|
pview.vert = ourview.vert / vmult; |
474 |
|
break; |
475 |
+ |
case VT_CYL: |
476 |
+ |
pview.horiz = ourview.horiz / hmult; |
477 |
+ |
pview.vert = (2.*180./PI)*atan( |
478 |
+ |
tan((PI/180./2.)*ourview.vert)/vmult ); |
479 |
+ |
break; |
480 |
|
case VT_HEM: |
481 |
< |
pview.horiz = 2.*180./PI*asin( |
482 |
< |
sin(PI/180./2.*ourview.horiz)/hmult ); |
483 |
< |
pview.vert = 2.*180./PI*asin( |
484 |
< |
sin(PI/180./2.*ourview.vert)/vmult ); |
481 |
> |
pview.horiz = (2.*180./PI)*asin( |
482 |
> |
sin((PI/180./2.)*ourview.horiz)/hmult ); |
483 |
> |
pview.vert = (2.*180./PI)*asin( |
484 |
> |
sin((PI/180./2.)*ourview.vert)/vmult ); |
485 |
|
break; |
486 |
+ |
case VT_PLS: |
487 |
+ |
pview.horiz = sin((PI/180./2.)*ourview.horiz) / |
488 |
+ |
(1.0 + cos((PI/180./2.)*ourview.horiz)) / hmult; |
489 |
+ |
pview.horiz *= pview.horiz; |
490 |
+ |
pview.horiz = (2.*180./PI)*acos((1. - pview.horiz) / |
491 |
+ |
(1. + pview.horiz)); |
492 |
+ |
pview.vert = sin((PI/180./2.)*ourview.vert) / |
493 |
+ |
(1.0 + cos((PI/180./2.)*ourview.vert)) / vmult; |
494 |
+ |
pview.vert *= pview.vert; |
495 |
+ |
pview.vert = (2.*180./PI)*acos((1. - pview.vert) / |
496 |
+ |
(1. + pview.vert)); |
497 |
+ |
break; |
498 |
|
default: |
499 |
|
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
500 |
|
progname, ourview.type); |
502 |
|
} |
503 |
|
/* render each piece */ |
504 |
|
while (nextpiece(&xorg, &yorg)) { |
505 |
< |
pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1); |
506 |
< |
pview.voff = ourview.voff + yorg - 0.5*(vmult-1); |
505 |
> |
pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1); |
506 |
> |
pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1); |
507 |
|
fputs(VIEWSTR, torp); |
508 |
|
fprintview(&pview, torp); |
509 |
|
putc('\n', torp); |
513 |
|
} |
514 |
|
|
515 |
|
|
516 |
< |
int |
517 |
< |
putpiece(xpos, ypos) /* get next piece from rpict */ |
518 |
< |
int xpos, ypos; |
516 |
> |
static int |
517 |
> |
putpiece( /* get next piece from rpict */ |
518 |
> |
int xpos, |
519 |
> |
int ypos |
520 |
> |
) |
521 |
|
{ |
522 |
|
struct flock fls; |
523 |
|
int pid, status; |
524 |
|
int hr, vr; |
525 |
|
register int y; |
526 |
|
/* check bounds */ |
527 |
< |
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
527 |
> |
if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) { |
528 |
|
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
529 |
|
progname, xpos, ypos); |
530 |
|
exit(cleanup(1)); |
532 |
|
/* check header from rpict */ |
533 |
|
guard_io(); |
534 |
|
getheader(fromrp, NULL, NULL); |
535 |
< |
if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) { |
535 |
> |
if (!fscnresolu(&hr, &vr, fromrp) || (hr != hres) | (vr != vres)) { |
536 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
537 |
|
progname, rpargv[0]); |
538 |
|
exit(cleanup(1)); |
577 |
|
filerr("lock"); |
578 |
|
#endif |
579 |
|
/* write new piece to file */ |
580 |
< |
if (lseek(outfd, fls.l_start, 0) == -1) |
580 |
> |
if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0) |
581 |
|
filerr("seek"); |
582 |
|
if (hmult == 1) { |
583 |
|
if (writebuf(outfd, (char *)pbuf, |
589 |
|
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
590 |
|
filerr("write"); |
591 |
|
if (y < vr-1 && lseek(outfd, |
592 |
< |
(long)(hmult-1)*hr*sizeof(COLR), |
593 |
< |
1) == -1) |
592 |
> |
(off_t)(hmult-1)*hr*sizeof(COLR), |
593 |
> |
SEEK_CUR) < 0) |
594 |
|
filerr("seek"); |
595 |
|
} |
596 |
|
#if NFS |
598 |
|
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
599 |
|
filerr("lock"); |
600 |
|
#endif |
601 |
+ |
if (verbose) { /* notify caller */ |
602 |
+ |
printf("%d %d done\n", xpos, ypos); |
603 |
+ |
fflush(stdout); |
604 |
+ |
} |
605 |
|
if (syncfp != NULL) { /* record what's been done */ |
606 |
|
sflock(F_WRLCK); |
607 |
|
fseek(syncfp, 0L, 2); /* append index */ |
610 |
|
/*** Unlock not necessary, since |
611 |
|
sflock(F_UNLCK); _exit() or nextpiece() is next ***/ |
612 |
|
} |
517 |
– |
if (verbose) { /* notify caller */ |
518 |
– |
printf("%d %d done\n", xpos, ypos); |
519 |
– |
fflush(stdout); |
520 |
– |
} |
613 |
|
if (pid == -1) /* didn't fork or fork failed */ |
614 |
|
return(0); |
615 |
|
_exit(0); /* else exit child process (releasing locks) */ |
616 |
|
} |
617 |
|
|
618 |
|
|
619 |
< |
filerr(t) /* report file error and exit */ |
620 |
< |
char *t; |
619 |
> |
static void |
620 |
> |
filerr( /* report file error and exit */ |
621 |
> |
char *t |
622 |
> |
) |
623 |
|
{ |
530 |
– |
extern char *sys_errlist[]; |
531 |
– |
|
624 |
|
fprintf(stderr, "%s: %s error on file \"%s\": %s\n", |
625 |
< |
progname, t, outfile, sys_errlist[errno]); |
625 |
> |
progname, t, outfile, strerror(errno)); |
626 |
|
_exit(1); |
627 |
|
} |
628 |
|
|