1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1993 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
10 |
|
|
11 |
|
#include "standard.h" |
12 |
|
#include <fcntl.h> |
13 |
+ |
|
14 |
+ |
#ifndef F_SETLKW |
15 |
+ |
|
16 |
+ |
main(argc, argv) |
17 |
+ |
int argc; |
18 |
+ |
char *argv[]; |
19 |
+ |
{ |
20 |
+ |
fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]); |
21 |
+ |
exit(1); |
22 |
+ |
} |
23 |
+ |
|
24 |
+ |
#else |
25 |
+ |
|
26 |
|
#include <signal.h> |
27 |
|
#include "color.h" |
28 |
|
#include "view.h" |
29 |
|
#include "resolu.h" |
30 |
|
|
31 |
+ |
#ifndef NFS |
32 |
+ |
#define NFS 1 |
33 |
+ |
#endif |
34 |
|
/* set the following to 0 to forgo forking */ |
35 |
|
#ifndef MAXFORK |
36 |
< |
#define MAXFORK 2 /* max. unreaped child processes */ |
36 |
> |
#if NFS |
37 |
> |
#define MAXFORK 3 /* allotment of duped processes */ |
38 |
> |
#else |
39 |
> |
#define MAXFORK 0 |
40 |
|
#endif |
41 |
< |
|
41 |
> |
#endif |
42 |
> |
/* protection from SYSV signals(!) */ |
43 |
> |
#if defined(sgi) || defined(hpux) |
44 |
> |
#define guard_io() sighold(SIGALRM) |
45 |
> |
#define unguard() sigrelse(SIGALRM) |
46 |
> |
#endif |
47 |
> |
#ifndef guard_io |
48 |
> |
#define guard_io() 0 |
49 |
> |
#define unguard() 0 |
50 |
> |
#endif |
51 |
|
/* rpict command */ |
52 |
< |
char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"}; |
53 |
< |
int rpargc = 9; |
52 |
> |
char *rpargv[128] = {"rpict", "-S", "1"}; |
53 |
> |
int rpargc = 3; |
54 |
|
int rpd[3]; |
55 |
|
FILE *torp, *fromrp; |
56 |
|
COLR *pbuf; |
57 |
|
/* our view parameters */ |
58 |
|
VIEW ourview = STDVIEW; |
59 |
|
double pixaspect = 1.0; |
60 |
< |
int hres = 512, vres = 512, hmult = 2, vmult = 2; |
60 |
> |
int hres = 1024, vres = 1024, hmult = 4, vmult = 4; |
61 |
|
/* output file */ |
62 |
|
char *outfile = NULL; |
63 |
|
int outfd; |
82 |
|
|
83 |
|
progname = argv[0]; |
84 |
|
for (i = 1; i < argc; i++) { |
85 |
+ |
/* expand arguments */ |
86 |
+ |
while (rval = expandarg(&argc, &argv, i)) |
87 |
+ |
if (rval < 0) { |
88 |
+ |
fprintf(stderr, "%s: cannot expand '%s'", |
89 |
+ |
argv[0], argv[i]); |
90 |
+ |
exit(1); |
91 |
+ |
} |
92 |
|
if (argv[i][0] == '-') |
93 |
|
switch (argv[i][1]) { |
94 |
|
case 'v': |
113 |
|
} |
114 |
|
break; |
115 |
|
case 'p': /* pixel aspect ratio? */ |
116 |
< |
if (argv[i][2] == 'a' && !argv[i][3]) |
117 |
< |
pixaspect = atof(argv[i+1]); |
118 |
< |
break; |
119 |
< |
case 'x': /* piece x resolution */ |
120 |
< |
if (!argv[i][2]) |
121 |
< |
hres = atoi(argv[i+1]); |
122 |
< |
break; |
123 |
< |
case 'y': /* piece y resolution */ |
124 |
< |
if (!argv[i][2]) |
125 |
< |
vres = atoi(argv[i+1]); |
126 |
< |
break; |
116 |
> |
if (argv[i][2] != 'a' || argv[i][3]) |
117 |
> |
break; |
118 |
> |
pixaspect = atof(argv[i+1]); |
119 |
> |
continue; |
120 |
> |
case 'x': /* overall x resolution */ |
121 |
> |
if (argv[i][2]) |
122 |
> |
break; |
123 |
> |
hres = atoi(argv[++i]); |
124 |
> |
continue; |
125 |
> |
case 'y': /* overall y resolution */ |
126 |
> |
if (argv[i][2]) |
127 |
> |
break; |
128 |
> |
vres = atoi(argv[++i]); |
129 |
> |
continue; |
130 |
|
case 'X': /* horizontal multiplier */ |
131 |
|
if (argv[i][2]) |
132 |
|
break; |
140 |
|
case 'F': /* syncronization file */ |
141 |
|
if (argv[i][2]) |
142 |
|
break; |
143 |
< |
if ((syncfd = open(argv[++i], O_RDWR)) < 0) { |
143 |
> |
if ((syncfd = open(argv[++i], |
144 |
> |
O_RDWR|O_CREAT, 0666)) < 0) { |
145 |
|
fprintf(stderr, "%s: cannot open\n", |
146 |
|
argv[i]); |
147 |
|
exit(1); |
148 |
|
} |
149 |
|
continue; |
150 |
+ |
case 'z': /* z-file ist verbotten */ |
151 |
+ |
fprintf(stderr, "%s: -z option not allowed\n", |
152 |
+ |
argv[0]); |
153 |
+ |
exit(1); |
154 |
|
case 'o': /* output file */ |
155 |
|
if (argv[i][2]) |
156 |
|
break; |
157 |
|
outfile = argv[++i]; |
158 |
|
continue; |
159 |
< |
} |
159 |
> |
} else if (i >= argc-1) |
160 |
> |
break; |
161 |
|
rpargv[rpargc++] = argv[i]; |
162 |
|
} |
163 |
< |
rpargv[rpargc] = NULL; |
163 |
> |
if (i >= argc) { |
164 |
> |
fprintf(stderr, "%s: missing octree argument\n", argv[0]); |
165 |
> |
exit(1); |
166 |
> |
} |
167 |
|
if (outfile == NULL) { |
168 |
|
fprintf(stderr, "%s: missing output file\n", argv[0]); |
169 |
|
exit(1); |
170 |
|
} |
171 |
|
init(argc, argv); |
172 |
|
rpiece(); |
173 |
< |
rval = cleanup(); |
127 |
< |
exit(rval); |
173 |
> |
exit(cleanup(0)); |
174 |
|
} |
175 |
|
|
176 |
|
|
178 |
|
int ac; |
179 |
|
char **av; |
180 |
|
{ |
181 |
+ |
static char hrbuf[16], vrbuf[16]; |
182 |
|
extern char VersionID[]; |
183 |
|
char *err; |
184 |
|
FILE *fp; |
193 |
|
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
194 |
|
sscanf(buf, "%d %d", &hmult, &vmult); |
195 |
|
} |
196 |
+ |
/* compute piece size */ |
197 |
+ |
hres /= hmult; |
198 |
+ |
vres /= vmult; |
199 |
|
normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres); |
200 |
+ |
sprintf(hrbuf, "%d", hres); |
201 |
+ |
rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf; |
202 |
+ |
sprintf(vrbuf, "%d", vres); |
203 |
+ |
rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf; |
204 |
+ |
rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0"; |
205 |
+ |
rpargv[rpargc++] = av[ac-1]; |
206 |
+ |
rpargv[rpargc] = NULL; |
207 |
|
/* open output file */ |
208 |
|
if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) { |
209 |
|
if ((fp = fdopen(dup(outfd), "w")) == NULL) |
221 |
|
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
222 |
|
if ((fp = fdopen(dup(outfd), "r+")) == NULL) |
223 |
|
goto filerr; |
224 |
< |
getheader(fp, NULL); /* skip header */ |
225 |
< |
if (fscnresolu(&hr, &vr, fp) < 0 || /* check resolution */ |
224 |
> |
getheader(fp, NULL, NULL); /* skip header */ |
225 |
> |
if (!fscnresolu(&hr, &vr, fp) || /* check resolution */ |
226 |
|
hr != hres*hmult || vr != vres*vmult) { |
227 |
|
fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n", |
228 |
|
progname, outfile); |
236 |
|
scanorig = ftell(fp); /* record position of first scanline */ |
237 |
|
if (fclose(fp) == -1) /* done with stream i/o */ |
238 |
|
goto filerr; |
239 |
+ |
#if NFS |
240 |
|
sync(); /* flush NFS buffers */ |
241 |
+ |
#endif |
242 |
|
/* start rpict process */ |
243 |
|
if (open_process(rpd, rpargv) <= 0) { |
244 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
250 |
|
progname, rpargv[0]); |
251 |
|
exit(1); |
252 |
|
} |
253 |
< |
if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) { |
253 |
> |
if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) { |
254 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
255 |
|
exit(1); |
256 |
|
} |
266 |
|
nextpiece(xp, yp) /* get next piece assignment */ |
267 |
|
int *xp, *yp; |
268 |
|
{ |
210 |
– |
extern char *fgets(); |
269 |
|
struct flock fls; |
270 |
|
char buf[64]; |
271 |
|
|
290 |
|
return(0); |
291 |
|
} |
292 |
|
} |
293 |
< |
sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp); |
293 |
> |
sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp); |
294 |
|
lseek(syncfd, 0L, 0); /* write new position */ |
295 |
|
write(syncfd, buf, strlen(buf)); |
296 |
|
fls.l_type = F_UNLCK; /* release sync file */ |
302 |
|
if (sscanf(buf, "%d %d", xp, yp) == 2) |
303 |
|
return(1); |
304 |
|
fprintf(stderr, "%s: input format error\n", progname); |
305 |
< |
exit(1); |
305 |
> |
exit(cleanup(1)); |
306 |
|
} |
307 |
|
|
308 |
|
|
309 |
|
int |
310 |
< |
cleanup() /* close rpict process and clean up */ |
310 |
> |
cleanup(rstat) /* close rpict process and clean up */ |
311 |
> |
int rstat; |
312 |
|
{ |
313 |
< |
register int pid; |
255 |
< |
int status, rstat = 0; |
313 |
> |
int status; |
314 |
|
|
315 |
< |
free((char *)pbuf); |
315 |
> |
bfree((char *)pbuf, hres*vres*sizeof(COLR)); |
316 |
|
fclose(torp); |
317 |
|
fclose(fromrp); |
318 |
< |
while ((pid = wait(&status)) != -1) |
318 |
> |
while (wait(&status) != -1) |
319 |
|
if (rstat == 0) |
320 |
|
rstat = status>>8 & 0xff; |
321 |
|
return(rstat); |
326 |
|
{ |
327 |
|
VIEW pview; |
328 |
|
int xorg, yorg; |
329 |
< |
|
329 |
> |
/* compute view parameters */ |
330 |
> |
copystruct(&pview, &ourview); |
331 |
> |
switch (ourview.type) { |
332 |
> |
case VT_PER: |
333 |
> |
pview.horiz = 2.*180./PI*atan( |
334 |
> |
tan(PI/180./2.*ourview.horiz)/hmult ); |
335 |
> |
pview.vert = 2.*180./PI*atan( |
336 |
> |
tan(PI/180./2.*ourview.vert)/vmult ); |
337 |
> |
break; |
338 |
> |
case VT_PAR: |
339 |
> |
case VT_ANG: |
340 |
> |
pview.horiz = ourview.horiz / hmult; |
341 |
> |
pview.vert = ourview.vert / vmult; |
342 |
> |
break; |
343 |
> |
case VT_HEM: |
344 |
> |
pview.horiz = 2.*180./PI*asin( |
345 |
> |
sin(PI/180./2.*ourview.horiz)/hmult ); |
346 |
> |
pview.vert = 2.*180./PI*asin( |
347 |
> |
sin(PI/180./2.*ourview.vert)/vmult ); |
348 |
> |
break; |
349 |
> |
default: |
350 |
> |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
351 |
> |
progname, ourview.type); |
352 |
> |
exit(cleanup(1)); |
353 |
> |
} |
354 |
> |
/* render each piece */ |
355 |
|
while (nextpiece(&xorg, &yorg)) { |
356 |
< |
copystruct(&pview, &ourview); /* compute view for piece */ |
357 |
< |
switch (ourview.type) { |
275 |
< |
case VT_PER: |
276 |
< |
pview.horiz = 2.*180./PI*atan( |
277 |
< |
tan(PI/180./2.*ourview.horiz)/hmult ); |
278 |
< |
pview.vert = 2.*180./PI*atan( |
279 |
< |
tan(PI/180./2.*ourview.vert)/vmult ); |
280 |
< |
break; |
281 |
< |
case VT_PAR: |
282 |
< |
case VT_ANG: |
283 |
< |
pview.horiz = ourview.horiz / hmult; |
284 |
< |
pview.vert = ourview.vert / vmult; |
285 |
< |
break; |
286 |
< |
case VT_HEM: |
287 |
< |
pview.horiz = 2.*180./PI*asin( |
288 |
< |
sin(PI/180./2.*ourview.horiz)/hmult ); |
289 |
< |
pview.vert = 2.*180./PI*asin( |
290 |
< |
sin(PI/180./2.*ourview.vert)/vmult ); |
291 |
< |
break; |
292 |
< |
default: |
293 |
< |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
294 |
< |
progname, ourview.type); |
295 |
< |
exit(1); |
296 |
< |
} |
297 |
< |
pview.hoff += xorg - 0.5*(hmult-1); |
298 |
< |
pview.voff += yorg - 0.5*(vmult-1); |
356 |
> |
pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1); |
357 |
> |
pview.voff = ourview.voff + yorg - 0.5*(vmult-1); |
358 |
|
fputs(VIEWSTR, torp); |
359 |
|
fprintview(&pview, torp); |
360 |
|
putc('\n', torp); |
361 |
< |
fflush(torp); /* assign piece to rpict */ |
361 |
> |
fflush(torp); /* assigns piece to rpict */ |
362 |
|
putpiece(xorg, yorg); /* place piece in output */ |
304 |
– |
if (verbose) { /* notify caller */ |
305 |
– |
printf("%d %d done\n", xorg, yorg); |
306 |
– |
fflush(stdout); |
307 |
– |
} |
363 |
|
} |
364 |
|
} |
365 |
|
|
376 |
|
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
377 |
|
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
378 |
|
progname, xpos, ypos); |
379 |
< |
exit(1); |
379 |
> |
exit(cleanup(1)); |
380 |
|
} |
381 |
|
/* check header from rpict */ |
382 |
< |
getheader(fromrp, NULL); |
383 |
< |
if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres || vr != vres) { |
382 |
> |
guard_io(); |
383 |
> |
getheader(fromrp, NULL, NULL); |
384 |
> |
if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) { |
385 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
386 |
|
progname, rpargv[0]); |
387 |
< |
exit(1); |
387 |
> |
exit(cleanup(1)); |
388 |
|
} |
389 |
+ |
if (verbose) { /* notify caller */ |
390 |
+ |
printf("%d %d begun\n", xpos, ypos); |
391 |
+ |
fflush(stdout); |
392 |
+ |
} |
393 |
+ |
unguard(); |
394 |
|
/* load new piece into buffer */ |
395 |
< |
for (y = 0; y < vr; y++) |
395 |
> |
for (y = 0; y < vr; y++) { |
396 |
> |
guard_io(); |
397 |
|
if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) { |
398 |
|
fprintf(stderr, "%s: read error from %s\n", |
399 |
|
progname, rpargv[0]); |
400 |
< |
exit(1); |
400 |
> |
exit(cleanup(1)); |
401 |
|
} |
402 |
+ |
unguard(); |
403 |
+ |
} |
404 |
|
#if MAXFORK |
405 |
|
/* fork so we don't slow rpict down */ |
406 |
|
if ((pid = fork()) > 0) { |
407 |
< |
if (++nforked > MAXFORK) { |
407 |
> |
if (++nforked >= MAXFORK) { |
408 |
|
wait(&status); /* reap a child */ |
409 |
|
if (status) |
410 |
< |
exit(status>>8 & 0xff); |
410 |
> |
exit(cleanup(status>>8 & 0xff)); |
411 |
|
nforked--; |
412 |
|
} |
413 |
|
return(pid); |
415 |
|
#else |
416 |
|
pid = -1; /* no forking */ |
417 |
|
#endif |
418 |
+ |
fls.l_start = scanorig + |
419 |
+ |
((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR); |
420 |
+ |
#if NFS |
421 |
+ |
fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR); |
422 |
|
/* lock file section so NFS doesn't mess up */ |
423 |
|
fls.l_whence = 0; |
356 |
– |
fls.l_len = (long)vres*hmult*hres*sizeof(COLR); |
357 |
– |
fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len; |
424 |
|
fls.l_type = F_WRLCK; |
425 |
|
fcntl(outfd, F_SETLKW, &fls); |
426 |
+ |
#endif |
427 |
|
/* write new piece to file */ |
428 |
< |
if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1) |
428 |
> |
if (lseek(outfd, fls.l_start, 0) == -1) |
429 |
|
goto seekerr; |
430 |
|
if (hmult == 1) { |
431 |
|
if (writebuf(outfd, (char *)pbuf, |
441 |
|
1) == -1) |
442 |
|
goto seekerr; |
443 |
|
} |
444 |
< |
if (pid == -1) { /* fork failed */ |
444 |
> |
if (verbose) { /* notify caller */ |
445 |
> |
printf("%d %d done\n", xpos, ypos); |
446 |
> |
fflush(stdout); |
447 |
> |
} |
448 |
> |
if (pid == -1) { /* didn't fork or fork failed */ |
449 |
> |
#if NFS |
450 |
|
fls.l_type = F_UNLCK; /* release lock */ |
451 |
|
fcntl(outfd, F_SETLKW, &fls); |
452 |
+ |
#endif |
453 |
|
return(0); |
454 |
|
} |
455 |
|
_exit(0); /* else exit child process (releasing lock) */ |
460 |
|
fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile); |
461 |
|
_exit(1); |
462 |
|
} |
463 |
+ |
|
464 |
+ |
#endif |