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 |
|
|
22 |
– |
#include <signal.h> |
23 |
– |
|
24 |
– |
#include "color.h" |
25 |
– |
#include "view.h" |
26 |
– |
#include "rtprocess.h" |
27 |
– |
|
37 |
|
#ifndef NFS |
38 |
|
#define NFS 1 |
39 |
|
#endif |
40 |
< |
/* set the following to 0 to forgo forking */ |
40 |
> |
|
41 |
> |
#ifndef RHAS_FORK_EXEC |
42 |
> |
#undef MAXFORK |
43 |
> |
#define MAXFORK 0 |
44 |
> |
#endif |
45 |
|
#ifndef MAXFORK |
46 |
|
#if NFS |
47 |
|
#define MAXFORK 3 /* allotment of duped processes */ |
55 |
|
#define unguard() sigrelse(SIGALRM) |
56 |
|
#endif |
57 |
|
#ifndef guard_io |
58 |
< |
#define guard_io() 0 |
59 |
< |
#define unguard() 0 |
58 |
> |
#define guard_io() |
59 |
> |
#define unguard() |
60 |
|
#endif |
61 |
|
|
49 |
– |
extern char *strerror(); |
50 |
– |
|
62 |
|
/* rpict command */ |
63 |
|
char *rpargv[128] = {"rpict", "-S", "1"}; |
64 |
|
int rpargc = 3; |
80 |
|
|
81 |
|
char *progname; |
82 |
|
int verbose = 0; |
83 |
+ |
int nowarn = 0; |
84 |
|
unsigned timelim = 0; |
85 |
|
int rvrlim = -1; |
86 |
|
|
87 |
|
int gotalrm = 0; |
88 |
|
void onalrm(int i) { gotalrm++; } |
89 |
|
|
90 |
+ |
static void dolock(int fd, int ltyp); |
91 |
+ |
static void init(int ac, char **av); |
92 |
+ |
static int nextpiece(int *xp, int *yp); |
93 |
+ |
static int rvrpiece(int *xp, int *yp); |
94 |
+ |
static int cleanup(int rstat); |
95 |
+ |
static void rpiece(void); |
96 |
+ |
static int putpiece(int xpos, int ypos); |
97 |
+ |
static void filerr(char *t); |
98 |
|
|
99 |
< |
main(argc, argv) |
100 |
< |
int argc; |
101 |
< |
char *argv[]; |
99 |
> |
|
100 |
> |
int |
101 |
> |
main( |
102 |
> |
int argc, |
103 |
> |
char *argv[] |
104 |
> |
) |
105 |
|
{ |
106 |
< |
register int i, rval; |
106 |
> |
int i, rval; |
107 |
|
|
108 |
|
progname = argv[0]; |
109 |
|
for (i = 1; i < argc; i++) { |
111 |
|
while ((rval = expandarg(&argc, &argv, i)) > 0) |
112 |
|
; |
113 |
|
if (rval < 0) { |
114 |
< |
fprintf(stderr, "%s: cannot expand '%s'", |
114 |
> |
fprintf(stderr, "%s: cannot expand '%s'\n", |
115 |
|
argv[0], argv[i]); |
116 |
|
exit(1); |
117 |
|
} |
118 |
|
if (argv[i][0] == '-') |
119 |
|
switch (argv[i][1]) { |
120 |
+ |
case 'w': |
121 |
+ |
if (!argv[i][2]) |
122 |
+ |
nowarn = !nowarn; |
123 |
+ |
else if (argv[i][2] == '+') |
124 |
+ |
nowarn = 0; |
125 |
+ |
else if (argv[i][2] == '-') |
126 |
+ |
nowarn = 1; |
127 |
+ |
break; |
128 |
|
case 'v': |
129 |
|
switch (argv[i][2]) { |
130 |
|
case '\0': /* verbose option */ |
147 |
|
} |
148 |
|
break; |
149 |
|
case 'p': /* pixel aspect ratio? */ |
150 |
+ |
if (argv[i][2] == 'm') { |
151 |
+ |
fprintf(stderr, "%s: -pm unsupported\n", |
152 |
+ |
argv[0]); |
153 |
+ |
++i; |
154 |
+ |
continue; |
155 |
+ |
} |
156 |
+ |
if (argv[i][2] == 'X') { |
157 |
+ |
fprintf(stderr, "%s: -pXYZ unsupported\n", |
158 |
+ |
argv[0]); |
159 |
+ |
++i; |
160 |
+ |
continue; |
161 |
+ |
} |
162 |
+ |
if (argv[i][2] == 'c') { |
163 |
+ |
fprintf(stderr, "%s: -pc unsupported\n", |
164 |
+ |
argv[0]); |
165 |
+ |
i += 9; |
166 |
+ |
continue; |
167 |
+ |
} |
168 |
|
if (argv[i][2] != 'a' || argv[i][3]) |
169 |
|
break; |
170 |
|
pixaspect = atof(argv[++i]); |
171 |
|
continue; |
172 |
+ |
case 'S': |
173 |
+ |
fprintf(stderr, "%s: -S unsupported\n", argv[0]); |
174 |
+ |
i++; |
175 |
+ |
continue; |
176 |
|
case 'T': /* time limit (hours) */ |
177 |
|
if (argv[i][2]) |
178 |
|
break; |
179 |
|
timelim = atof(argv[++i])*3600. + .5; |
180 |
< |
break; |
180 |
> |
continue; |
181 |
|
case 'x': /* overall x resolution */ |
182 |
|
if (argv[i][2]) |
183 |
|
break; |
222 |
|
break; |
223 |
|
outfile = argv[++i]; |
224 |
|
continue; |
225 |
< |
} else if (i >= argc-1) |
226 |
< |
break; |
225 |
> |
} |
226 |
> |
else if (i >= argc-1) |
227 |
> |
break; |
228 |
|
rpargv[rpargc++] = argv[i]; |
229 |
|
} |
230 |
|
if (i >= argc) { |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
< |
dolock(fd, ltyp) /* lock or unlock a file */ |
245 |
< |
int fd; |
246 |
< |
int ltyp; |
244 |
> |
static void |
245 |
> |
dolock( /* lock or unlock a file */ |
246 |
> |
int fd, |
247 |
> |
int ltyp |
248 |
> |
) |
249 |
|
{ |
250 |
|
static struct flock fls; /* static so initialized to zeroes */ |
251 |
|
|
253 |
|
if (fcntl(fd, F_SETLKW, &fls) < 0) { |
254 |
|
fprintf(stderr, "%s: cannot lock/unlock file: %s\n", |
255 |
|
progname, strerror(errno)); |
256 |
< |
exit(1); |
256 |
> |
_exit(1); |
257 |
|
} |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
< |
init(ac, av) /* set up output file and start rpict */ |
262 |
< |
int ac; |
263 |
< |
char **av; |
261 |
> |
static void |
262 |
> |
init( /* set up output file and start rpict */ |
263 |
> |
int ac, |
264 |
> |
char **av |
265 |
> |
) |
266 |
|
{ |
267 |
|
static char hrbuf[16], vrbuf[16]; |
268 |
|
extern char VersionID[]; |
281 |
|
sflock(F_UNLCK); |
282 |
|
} |
283 |
|
/* compute piece size */ |
284 |
+ |
hr = hres; vr = vres; |
285 |
+ |
if (pixaspect > FTINY) |
286 |
+ |
normaspect(viewaspect(&ourview), &pixaspect, &hr, &vr); |
287 |
|
hres /= hmult; |
288 |
|
vres /= vmult; |
289 |
+ |
if (hres <= 0 || vres <= 0) { |
290 |
+ |
fprintf(stderr, "%s: illegal resolution/subdivision\n", progname); |
291 |
+ |
exit(1); |
292 |
+ |
} |
293 |
|
normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres); |
294 |
+ |
if (!nowarn && (hr != hres*hmult) | (vr != vres*vmult)) |
295 |
+ |
fprintf(stderr, |
296 |
+ |
"%s: warning - changed resolution from %dx%d to %dx%d\n", |
297 |
+ |
progname, hr, vr, hres*hmult, vres*vmult); |
298 |
|
sprintf(hrbuf, "%d", hres); |
299 |
|
rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf; |
300 |
|
sprintf(vrbuf, "%d", vres); |
301 |
|
rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf; |
302 |
|
rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0"; |
303 |
+ |
rpargv[rpargc++] = "-pm"; rpargv[rpargc++] = "0"; |
304 |
|
rpargv[rpargc++] = av[ac-1]; |
305 |
|
rpargv[rpargc] = NULL; |
306 |
|
/* open output file */ |
313 |
|
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
314 |
|
fputs(VIEWSTR, fp); |
315 |
|
fprintview(&ourview, fp); |
316 |
< |
putc('\n', fp); |
316 |
> |
fputc('\n', fp); |
317 |
> |
fputnow(fp); |
318 |
|
if (pixaspect < .99 || pixaspect > 1.01) |
319 |
|
fputaspect(pixaspect, fp); |
320 |
+ |
fputprims(stdprims, fp); |
321 |
|
fputformat(COLRFMT, fp); |
322 |
< |
putc('\n', fp); |
322 |
> |
fputc('\n', fp); |
323 |
|
fprtresolu(hres*hmult, vres*vmult, fp); |
324 |
|
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
325 |
|
dolock(outfd, F_RDLCK); |
342 |
|
goto filerr; |
343 |
|
dolock(outfd, F_UNLCK); |
344 |
|
/* start rpict process */ |
345 |
+ |
rpd = sp_inactive; |
346 |
|
if (open_process(&rpd, rpargv) <= 0) { |
347 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
348 |
|
exit(1); |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
< |
int |
371 |
< |
nextpiece(xp, yp) /* get next piece assignment */ |
372 |
< |
int *xp, *yp; |
370 |
> |
static int |
371 |
> |
nextpiece( /* get next piece assignment */ |
372 |
> |
int *xp, |
373 |
> |
int *yp |
374 |
> |
) |
375 |
|
{ |
376 |
|
if (gotalrm) /* someone wants us to quit */ |
377 |
|
return(0); |
411 |
|
} |
412 |
|
|
413 |
|
|
414 |
< |
int |
415 |
< |
rvrpiece(xp, yp) /* check for recoverable pieces */ |
416 |
< |
register int *xp, *yp; |
414 |
> |
static int |
415 |
> |
rvrpiece( /* check for recoverable pieces */ |
416 |
> |
int *xp, |
417 |
> |
int *yp |
418 |
> |
) |
419 |
|
{ |
420 |
|
static char *pdone = NULL; /* which pieces are done */ |
421 |
|
static long readpos = -1; /* how far we've read */ |
422 |
< |
register int i; |
422 |
> |
int px, py, i; |
423 |
|
/* |
424 |
|
* This routine is called by nextpiece() with an |
425 |
|
* exclusive lock on syncfp and the file pointer at the |
428 |
|
if (rvrlim < 0) |
429 |
|
return(0); /* only check if asked */ |
430 |
|
if (pdone == NULL) /* first call */ |
431 |
< |
pdone = calloc(hmult*vmult, sizeof(char)); |
431 |
> |
pdone = (char *)calloc(hmult*vmult, sizeof(char)); |
432 |
|
if (pdone == NULL) { |
433 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
434 |
|
exit(1); |
435 |
|
} |
436 |
|
if (readpos != -1) /* mark what's been done */ |
437 |
|
fseek(syncfp, readpos, 0); |
438 |
< |
while (fscanf(syncfp, "%d %d", xp, yp) == 2) |
439 |
< |
pdone[*xp*vmult+*yp] = 1; |
438 |
> |
while (fscanf(syncfp, "%d %d", &px, &py) == 2) |
439 |
> |
pdone[px*vmult+py] = 1; |
440 |
|
if (!feof(syncfp)) { |
441 |
|
fprintf(stderr, "%s: format error in sync file\n", progname); |
442 |
|
exit(1); |
457 |
|
} |
458 |
|
|
459 |
|
|
460 |
< |
int |
461 |
< |
cleanup(rstat) /* close rpict process and clean up */ |
462 |
< |
int rstat; |
460 |
> |
static int |
461 |
> |
cleanup( /* close rpict process and clean up */ |
462 |
> |
int rstat |
463 |
> |
) |
464 |
|
{ |
465 |
|
int status; |
466 |
|
|
474 |
|
} |
475 |
|
|
476 |
|
|
477 |
< |
rpiece() /* render picture piece by piece */ |
477 |
> |
static void |
478 |
> |
rpiece(void) /* render picture piece by piece */ |
479 |
|
{ |
480 |
+ |
char *err; |
481 |
|
VIEW pview; |
482 |
|
int xorg, yorg; |
403 |
– |
/* compute view parameters */ |
404 |
– |
pview = ourview; |
405 |
– |
switch (ourview.type) { |
406 |
– |
case VT_PER: |
407 |
– |
pview.horiz = 2.*180./PI*atan( |
408 |
– |
tan(PI/180./2.*ourview.horiz)/hmult ); |
409 |
– |
pview.vert = 2.*180./PI*atan( |
410 |
– |
tan(PI/180./2.*ourview.vert)/vmult ); |
411 |
– |
break; |
412 |
– |
case VT_PAR: |
413 |
– |
case VT_ANG: |
414 |
– |
pview.horiz = ourview.horiz / hmult; |
415 |
– |
pview.vert = ourview.vert / vmult; |
416 |
– |
break; |
417 |
– |
case VT_CYL: |
418 |
– |
pview.horiz = ourview.horiz / hmult; |
419 |
– |
pview.vert = 2.*180./PI*atan( |
420 |
– |
tan(PI/180./2.*ourview.vert)/vmult ); |
421 |
– |
break; |
422 |
– |
case VT_HEM: |
423 |
– |
pview.horiz = 2.*180./PI*asin( |
424 |
– |
sin(PI/180./2.*ourview.horiz)/hmult ); |
425 |
– |
pview.vert = 2.*180./PI*asin( |
426 |
– |
sin(PI/180./2.*ourview.vert)/vmult ); |
427 |
– |
break; |
428 |
– |
default: |
429 |
– |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
430 |
– |
progname, ourview.type); |
431 |
– |
exit(cleanup(1)); |
432 |
– |
} |
483 |
|
/* render each piece */ |
484 |
|
while (nextpiece(&xorg, &yorg)) { |
485 |
< |
pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1); |
486 |
< |
pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1); |
485 |
> |
pview = ourview; |
486 |
> |
err = cropview(&pview, (double)xorg/hmult, (double)yorg/vmult, |
487 |
> |
(xorg+1.)/hmult, (yorg+1.)/vmult); |
488 |
> |
if (err != NULL) { |
489 |
> |
fprintf(stderr, "%s: %s\n", progname, err); |
490 |
> |
exit(cleanup(1)); |
491 |
> |
} |
492 |
|
fputs(VIEWSTR, torp); |
493 |
|
fprintview(&pview, torp); |
494 |
< |
putc('\n', torp); |
494 |
> |
fputc('\n', torp); |
495 |
|
fflush(torp); /* assigns piece to rpict */ |
496 |
|
putpiece(xorg, yorg); /* place piece in output */ |
497 |
|
} |
498 |
|
} |
499 |
|
|
500 |
|
|
501 |
< |
int |
502 |
< |
putpiece(xpos, ypos) /* get next piece from rpict */ |
503 |
< |
int xpos, ypos; |
501 |
> |
static int |
502 |
> |
putpiece( /* get next piece from rpict */ |
503 |
> |
int xpos, |
504 |
> |
int ypos |
505 |
> |
) |
506 |
|
{ |
507 |
|
struct flock fls; |
508 |
|
int pid, status; |
509 |
|
int hr, vr; |
510 |
< |
register int y; |
510 |
> |
int y; |
511 |
|
/* check bounds */ |
512 |
|
if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) { |
513 |
|
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
558 |
|
/* lock file section so NFS doesn't mess up */ |
559 |
|
fls.l_whence = 0; |
560 |
|
fls.l_type = F_WRLCK; |
561 |
+ |
#if 0 |
562 |
|
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
563 |
|
filerr("lock"); |
564 |
+ |
#else |
565 |
+ |
dolock(outfd, F_WRLCK); |
566 |
|
#endif |
567 |
+ |
#endif |
568 |
|
/* write new piece to file */ |
569 |
< |
if (lseek(outfd, (off_t)fls.l_start, 0) < 0) |
569 |
> |
if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0) |
570 |
|
filerr("seek"); |
571 |
|
if (hmult == 1) { |
572 |
< |
if (writebuf(outfd, (char *)pbuf, |
572 |
> |
if (writebuf(outfd, pbuf, |
573 |
|
vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR)) |
574 |
|
filerr("write"); |
575 |
|
} else |
576 |
|
for (y = 0; y < vr; y++) { |
577 |
< |
if (writebuf(outfd, (char *)(pbuf+y*hr), |
577 |
> |
if (writebuf(outfd, pbuf+y*hr, |
578 |
|
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
579 |
|
filerr("write"); |
580 |
|
if (y < vr-1 && lseek(outfd, |
581 |
|
(off_t)(hmult-1)*hr*sizeof(COLR), |
582 |
< |
1) < 0) |
582 |
> |
SEEK_CUR) < 0) |
583 |
|
filerr("seek"); |
584 |
|
} |
585 |
|
#if NFS |
586 |
|
fls.l_type = F_UNLCK; /* release lock */ |
587 |
+ |
#if 0 |
588 |
|
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
589 |
|
filerr("lock"); |
590 |
+ |
#else |
591 |
+ |
dolock(outfd, F_UNLCK); |
592 |
|
#endif |
593 |
+ |
#endif |
594 |
|
if (verbose) { /* notify caller */ |
595 |
|
printf("%d %d done\n", xpos, ypos); |
596 |
|
fflush(stdout); |
609 |
|
} |
610 |
|
|
611 |
|
|
612 |
< |
filerr(t) /* report file error and exit */ |
613 |
< |
char *t; |
612 |
> |
static void |
613 |
> |
filerr( /* report file error and exit */ |
614 |
> |
char *t |
615 |
> |
) |
616 |
|
{ |
617 |
|
fprintf(stderr, "%s: %s error on file \"%s\": %s\n", |
618 |
|
progname, t, outfile, strerror(errno)); |