1 |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Generate sections of a picture. |
9 |
*/ |
10 |
|
11 |
#include "standard.h" |
12 |
|
13 |
#ifndef F_SETLKW |
14 |
|
15 |
main(argc, argv) |
16 |
int argc; |
17 |
char *argv[]; |
18 |
{ |
19 |
fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]); |
20 |
exit(1); |
21 |
} |
22 |
|
23 |
#else |
24 |
|
25 |
#include <signal.h> |
26 |
#include "color.h" |
27 |
#include "view.h" |
28 |
#include "resolu.h" |
29 |
|
30 |
#ifndef NFS |
31 |
#define NFS 1 |
32 |
#endif |
33 |
/* set the following to 0 to forgo forking */ |
34 |
#ifndef MAXFORK |
35 |
#if NFS |
36 |
#define MAXFORK 3 /* allotment of duped processes */ |
37 |
#else |
38 |
#define MAXFORK 0 |
39 |
#endif |
40 |
#endif |
41 |
/* protection from SYSV signals(!) */ |
42 |
#if defined(sgi) |
43 |
#define guard_io() sighold(SIGALRM) |
44 |
#define unguard() sigrelse(SIGALRM) |
45 |
#endif |
46 |
#ifndef guard_io |
47 |
#define guard_io() 0 |
48 |
#define unguard() 0 |
49 |
#endif |
50 |
|
51 |
#ifndef linux |
52 |
extern char *sys_errlist[]; |
53 |
#endif |
54 |
|
55 |
/* rpict command */ |
56 |
char *rpargv[128] = {"rpict", "-S", "1"}; |
57 |
int rpargc = 3; |
58 |
int rpd[3]; |
59 |
FILE *torp, *fromrp; |
60 |
COLR *pbuf; |
61 |
/* our view parameters */ |
62 |
VIEW ourview = STDVIEW; |
63 |
double pixaspect = 1.0; |
64 |
int hres = 1024, vres = 1024, hmult = 4, vmult = 4; |
65 |
/* output file */ |
66 |
char *outfile = NULL; |
67 |
int outfd; |
68 |
long scanorig; |
69 |
FILE *syncfp = NULL; /* synchronization file pointer */ |
70 |
int synclst = F_UNLCK; /* synchronization file lock status */ |
71 |
int nforked = 0; |
72 |
|
73 |
#define sflock(t) if ((t)!=synclst) dolock(fileno(syncfp),synclst=t) |
74 |
|
75 |
char *progname; |
76 |
int verbose = 0; |
77 |
unsigned timelim = 0; |
78 |
int rvrlim = -1; |
79 |
|
80 |
int gotalrm = 0; |
81 |
int onalrm() { gotalrm++; } |
82 |
|
83 |
|
84 |
main(argc, argv) |
85 |
int argc; |
86 |
char *argv[]; |
87 |
{ |
88 |
register int i, rval; |
89 |
|
90 |
progname = argv[0]; |
91 |
for (i = 1; i < argc; i++) { |
92 |
/* expand arguments */ |
93 |
while (rval = expandarg(&argc, &argv, i)) |
94 |
if (rval < 0) { |
95 |
fprintf(stderr, "%s: cannot expand '%s'", |
96 |
argv[0], argv[i]); |
97 |
exit(1); |
98 |
} |
99 |
if (argv[i][0] == '-') |
100 |
switch (argv[i][1]) { |
101 |
case 'v': |
102 |
switch (argv[i][2]) { |
103 |
case '\0': /* verbose option */ |
104 |
verbose = !verbose; |
105 |
continue; |
106 |
case 'f': /* view file */ |
107 |
if (viewfile(argv[++i], &ourview, NULL) <= 0) { |
108 |
fprintf(stderr, |
109 |
"%s: not a view file\n", argv[i]); |
110 |
exit(1); |
111 |
} |
112 |
continue; |
113 |
default: /* view option? */ |
114 |
rval = getviewopt(&ourview, argc-i, argv+i); |
115 |
if (rval >= 0) { |
116 |
i += rval; |
117 |
continue; |
118 |
} |
119 |
break; |
120 |
} |
121 |
break; |
122 |
case 'p': /* pixel aspect ratio? */ |
123 |
if (argv[i][2] != 'a' || argv[i][3]) |
124 |
break; |
125 |
pixaspect = atof(argv[++i]); |
126 |
continue; |
127 |
case 'T': /* time limit (hours) */ |
128 |
if (argv[i][2]) |
129 |
break; |
130 |
timelim = atof(argv[++i])*3600. + .5; |
131 |
break; |
132 |
case 'x': /* overall x resolution */ |
133 |
if (argv[i][2]) |
134 |
break; |
135 |
hres = atoi(argv[++i]); |
136 |
continue; |
137 |
case 'y': /* overall y resolution */ |
138 |
if (argv[i][2]) |
139 |
break; |
140 |
vres = atoi(argv[++i]); |
141 |
continue; |
142 |
case 'X': /* horizontal multiplier */ |
143 |
if (argv[i][2]) |
144 |
break; |
145 |
hmult = atoi(argv[++i]); |
146 |
continue; |
147 |
case 'Y': /* vertical multiplier */ |
148 |
if (argv[i][2]) |
149 |
break; |
150 |
vmult = atoi(argv[++i]); |
151 |
continue; |
152 |
case 'R': /* recover */ |
153 |
if (argv[i][2]) |
154 |
break; |
155 |
rvrlim = 0; |
156 |
/* fall through */ |
157 |
case 'F': /* syncronization file */ |
158 |
if (argv[i][2]) |
159 |
break; |
160 |
if ((syncfp = |
161 |
fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) { |
162 |
fprintf(stderr, "%s: cannot open\n", |
163 |
argv[i]); |
164 |
exit(1); |
165 |
} |
166 |
continue; |
167 |
case 'z': /* z-file ist verbotten */ |
168 |
fprintf(stderr, "%s: -z option not allowed\n", |
169 |
argv[0]); |
170 |
exit(1); |
171 |
case 'o': /* output file */ |
172 |
if (argv[i][2]) |
173 |
break; |
174 |
outfile = argv[++i]; |
175 |
continue; |
176 |
} else if (i >= argc-1) |
177 |
break; |
178 |
rpargv[rpargc++] = argv[i]; |
179 |
} |
180 |
if (i >= argc) { |
181 |
fprintf(stderr, "%s: missing octree argument\n", argv[0]); |
182 |
exit(1); |
183 |
} |
184 |
if (outfile == NULL) { |
185 |
fprintf(stderr, "%s: missing output file\n", argv[0]); |
186 |
exit(1); |
187 |
} |
188 |
init(argc, argv); |
189 |
rpiece(); |
190 |
exit(cleanup(0)); |
191 |
} |
192 |
|
193 |
|
194 |
dolock(fd, ltyp) /* lock or unlock a file */ |
195 |
int fd; |
196 |
int ltyp; |
197 |
{ |
198 |
static struct flock fls; /* static so initialized to zeroes */ |
199 |
|
200 |
fls.l_type = ltyp; |
201 |
if (fcntl(fd, F_SETLKW, &fls) < 0) { |
202 |
fprintf(stderr, "%s: cannot lock/unlock file: %s\n", |
203 |
progname, sys_errlist[errno]); |
204 |
exit(1); |
205 |
} |
206 |
} |
207 |
|
208 |
|
209 |
init(ac, av) /* set up output file and start rpict */ |
210 |
int ac; |
211 |
char **av; |
212 |
{ |
213 |
static char hrbuf[16], vrbuf[16]; |
214 |
extern char VersionID[]; |
215 |
char *err; |
216 |
FILE *fp; |
217 |
int hr, vr; |
218 |
/* set up view */ |
219 |
if ((err = setview(&ourview)) != NULL) { |
220 |
fprintf(stderr, "%s: %s\n", progname, err); |
221 |
exit(1); |
222 |
} |
223 |
if (syncfp != NULL) { |
224 |
sflock(F_RDLCK); |
225 |
fscanf(syncfp, "%d %d", &hmult, &vmult); |
226 |
sflock(F_UNLCK); |
227 |
} |
228 |
/* compute piece size */ |
229 |
hres /= hmult; |
230 |
vres /= vmult; |
231 |
normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres); |
232 |
sprintf(hrbuf, "%d", hres); |
233 |
rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf; |
234 |
sprintf(vrbuf, "%d", vres); |
235 |
rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf; |
236 |
rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0"; |
237 |
rpargv[rpargc++] = av[ac-1]; |
238 |
rpargv[rpargc] = NULL; |
239 |
/* open output file */ |
240 |
if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) { |
241 |
dolock(outfd, F_WRLCK); |
242 |
if ((fp = fdopen(dup(outfd), "w")) == NULL) |
243 |
goto filerr; |
244 |
newheader("RADIANCE", fp); /* create header */ |
245 |
printargs(ac, av, fp); |
246 |
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
247 |
fputs(VIEWSTR, fp); |
248 |
fprintview(&ourview, fp); |
249 |
putc('\n', fp); |
250 |
if (pixaspect < .99 || pixaspect > 1.01) |
251 |
fputaspect(pixaspect, fp); |
252 |
fputformat(COLRFMT, fp); |
253 |
putc('\n', fp); |
254 |
fprtresolu(hres*hmult, vres*vmult, fp); |
255 |
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
256 |
dolock(outfd, F_RDLCK); |
257 |
if ((fp = fdopen(dup(outfd), "r+")) == NULL) |
258 |
goto filerr; |
259 |
getheader(fp, NULL, NULL); /* skip header */ |
260 |
if (!fscnresolu(&hr, &vr, fp) || /* check resolution */ |
261 |
hr != hres*hmult || vr != vres*vmult) { |
262 |
fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n", |
263 |
progname, outfile); |
264 |
exit(1); |
265 |
} |
266 |
} else { |
267 |
fprintf(stderr, "%s: cannot open file \"%s\"\n", |
268 |
progname, outfile); |
269 |
exit(1); |
270 |
} |
271 |
scanorig = ftell(fp); /* record position of first scanline */ |
272 |
if (fclose(fp) == -1) /* done with stream i/o */ |
273 |
goto filerr; |
274 |
dolock(outfd, F_UNLCK); |
275 |
/* start rpict process */ |
276 |
if (open_process(rpd, rpargv) <= 0) { |
277 |
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
278 |
exit(1); |
279 |
} |
280 |
if ((fromrp = fdopen(rpd[0], "r")) == NULL || |
281 |
(torp = fdopen(rpd[1], "w")) == NULL) { |
282 |
fprintf(stderr, "%s: cannot open stream to %s\n", |
283 |
progname, rpargv[0]); |
284 |
exit(1); |
285 |
} |
286 |
if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) { |
287 |
fprintf(stderr, "%s: out of memory\n", progname); |
288 |
exit(1); |
289 |
} |
290 |
signal(SIGALRM, onalrm); |
291 |
if (timelim) |
292 |
alarm(timelim); |
293 |
return; |
294 |
filerr: |
295 |
fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile); |
296 |
exit(1); |
297 |
} |
298 |
|
299 |
|
300 |
int |
301 |
nextpiece(xp, yp) /* get next piece assignment */ |
302 |
int *xp, *yp; |
303 |
{ |
304 |
if (gotalrm) /* someone wants us to quit */ |
305 |
return(0); |
306 |
if (syncfp != NULL) { /* use sync file */ |
307 |
/* |
308 |
* So we don't necessarily have to lock and unlock the file |
309 |
* multiple times (very slow), we establish an exclusive |
310 |
* lock at the beginning on our synchronization file and |
311 |
* maintain it in the subroutine rvrpiece(). |
312 |
*/ |
313 |
sflock(F_WRLCK); |
314 |
fseek(syncfp, 0L, 0); /* read position */ |
315 |
if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) { |
316 |
*xp = hmult-1; |
317 |
*yp = vmult; |
318 |
} |
319 |
if (rvrlim == 0) /* initialize recovery limit */ |
320 |
rvrlim = *xp*vmult + *yp; |
321 |
if (rvrpiece(xp, yp)) { /* do stragglers first */ |
322 |
sflock(F_UNLCK); |
323 |
return(1); |
324 |
} |
325 |
if (--(*yp) < 0) { /* decrement position */ |
326 |
*yp = vmult-1; |
327 |
if (--(*xp) < 0) { /* all done */ |
328 |
sflock(F_UNLCK); |
329 |
return(0); |
330 |
} |
331 |
} |
332 |
fseek(syncfp, 0L, 0); /* write new position */ |
333 |
fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp); |
334 |
fflush(syncfp); |
335 |
sflock(F_UNLCK); /* release sync file */ |
336 |
return(1); |
337 |
} |
338 |
return(scanf("%d %d", xp, yp) == 2); /* use stdin */ |
339 |
} |
340 |
|
341 |
|
342 |
int |
343 |
rvrpiece(xp, yp) /* check for recoverable pieces */ |
344 |
register int *xp, *yp; |
345 |
{ |
346 |
static char *pdone = NULL; /* which pieces are done */ |
347 |
static long readpos = -1; /* how far we've read */ |
348 |
register int i; |
349 |
/* |
350 |
* This routine is called by nextpiece() with an |
351 |
* exclusive lock on syncfp and the file pointer at the |
352 |
* appropriate position to read in the finished pieces. |
353 |
*/ |
354 |
if (rvrlim < 0) |
355 |
return(0); /* only check if asked */ |
356 |
if (pdone == NULL) /* first call */ |
357 |
pdone = calloc(hmult*vmult, sizeof(char)); |
358 |
if (pdone == NULL) { |
359 |
fprintf(stderr, "%s: out of memory\n", progname); |
360 |
exit(1); |
361 |
} |
362 |
if (readpos != -1) /* mark what's been done */ |
363 |
fseek(syncfp, readpos, 0); |
364 |
while (fscanf(syncfp, "%d %d", xp, yp) == 2) |
365 |
pdone[*xp*vmult+*yp] = 1; |
366 |
if (!feof(syncfp)) { |
367 |
fprintf(stderr, "%s: format error in sync file\n", progname); |
368 |
exit(1); |
369 |
} |
370 |
readpos = ftell(syncfp); |
371 |
i = hmult*vmult; /* find an unaccounted for piece */ |
372 |
while (i-- > rvrlim) |
373 |
if (!pdone[i]) { |
374 |
*xp = i / vmult; |
375 |
*yp = i % vmult; |
376 |
pdone[i] = 1; /* consider it done */ |
377 |
return(1); |
378 |
} |
379 |
rvrlim = -1; /* nothing left to recover */ |
380 |
free(pdone); |
381 |
pdone = NULL; |
382 |
return(0); |
383 |
} |
384 |
|
385 |
|
386 |
int |
387 |
cleanup(rstat) /* close rpict process and clean up */ |
388 |
int rstat; |
389 |
{ |
390 |
int status; |
391 |
|
392 |
bfree((char *)pbuf, hres*vres*sizeof(COLR)); |
393 |
fclose(torp); |
394 |
fclose(fromrp); |
395 |
while (wait(&status) != -1) |
396 |
if (rstat == 0) |
397 |
rstat = status>>8 & 0xff; |
398 |
return(rstat); |
399 |
} |
400 |
|
401 |
|
402 |
rpiece() /* render picture piece by piece */ |
403 |
{ |
404 |
VIEW pview; |
405 |
int xorg, yorg; |
406 |
/* compute view parameters */ |
407 |
copystruct(&pview, &ourview); |
408 |
switch (ourview.type) { |
409 |
case VT_PER: |
410 |
pview.horiz = 2.*180./PI*atan( |
411 |
tan(PI/180./2.*ourview.horiz)/hmult ); |
412 |
pview.vert = 2.*180./PI*atan( |
413 |
tan(PI/180./2.*ourview.vert)/vmult ); |
414 |
break; |
415 |
case VT_PAR: |
416 |
case VT_ANG: |
417 |
pview.horiz = ourview.horiz / hmult; |
418 |
pview.vert = ourview.vert / vmult; |
419 |
break; |
420 |
case VT_CYL: |
421 |
pview.horiz = ourview.horiz / hmult; |
422 |
pview.vert = 2.*180./PI*atan( |
423 |
tan(PI/180./2.*ourview.vert)/vmult ); |
424 |
break; |
425 |
case VT_HEM: |
426 |
pview.horiz = 2.*180./PI*asin( |
427 |
sin(PI/180./2.*ourview.horiz)/hmult ); |
428 |
pview.vert = 2.*180./PI*asin( |
429 |
sin(PI/180./2.*ourview.vert)/vmult ); |
430 |
break; |
431 |
default: |
432 |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
433 |
progname, ourview.type); |
434 |
exit(cleanup(1)); |
435 |
} |
436 |
/* render each piece */ |
437 |
while (nextpiece(&xorg, &yorg)) { |
438 |
pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1); |
439 |
pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1); |
440 |
fputs(VIEWSTR, torp); |
441 |
fprintview(&pview, torp); |
442 |
putc('\n', torp); |
443 |
fflush(torp); /* assigns piece to rpict */ |
444 |
putpiece(xorg, yorg); /* place piece in output */ |
445 |
} |
446 |
} |
447 |
|
448 |
|
449 |
int |
450 |
putpiece(xpos, ypos) /* get next piece from rpict */ |
451 |
int xpos, ypos; |
452 |
{ |
453 |
struct flock fls; |
454 |
int pid, status; |
455 |
int hr, vr; |
456 |
register int y; |
457 |
/* check bounds */ |
458 |
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
459 |
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
460 |
progname, xpos, ypos); |
461 |
exit(cleanup(1)); |
462 |
} |
463 |
/* check header from rpict */ |
464 |
guard_io(); |
465 |
getheader(fromrp, NULL, NULL); |
466 |
if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) { |
467 |
fprintf(stderr, "%s: resolution mismatch from %s\n", |
468 |
progname, rpargv[0]); |
469 |
exit(cleanup(1)); |
470 |
} |
471 |
if (verbose) { /* notify caller */ |
472 |
printf("%d %d begun\n", xpos, ypos); |
473 |
fflush(stdout); |
474 |
} |
475 |
unguard(); |
476 |
/* load new piece into buffer */ |
477 |
for (y = 0; y < vr; y++) { |
478 |
guard_io(); |
479 |
if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) { |
480 |
fprintf(stderr, "%s: read error from %s\n", |
481 |
progname, rpargv[0]); |
482 |
exit(cleanup(1)); |
483 |
} |
484 |
unguard(); |
485 |
} |
486 |
#if MAXFORK |
487 |
/* fork so we don't slow rpict down */ |
488 |
if ((pid = fork()) > 0) { |
489 |
if (++nforked >= MAXFORK) { |
490 |
wait(&status); /* reap a child */ |
491 |
if (status) |
492 |
exit(cleanup(status>>8 & 0xff)); |
493 |
nforked--; |
494 |
} |
495 |
return(pid); |
496 |
} |
497 |
#else |
498 |
pid = -1; /* no forking */ |
499 |
#endif |
500 |
fls.l_start = scanorig + |
501 |
((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR); |
502 |
#if NFS |
503 |
fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR); |
504 |
/* lock file section so NFS doesn't mess up */ |
505 |
fls.l_whence = 0; |
506 |
fls.l_type = F_WRLCK; |
507 |
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
508 |
filerr("lock"); |
509 |
#endif |
510 |
/* write new piece to file */ |
511 |
if (lseek(outfd, fls.l_start, 0) < 0) |
512 |
filerr("seek"); |
513 |
if (hmult == 1) { |
514 |
if (writebuf(outfd, (char *)pbuf, |
515 |
vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR)) |
516 |
filerr("write"); |
517 |
} else |
518 |
for (y = 0; y < vr; y++) { |
519 |
if (writebuf(outfd, (char *)(pbuf+y*hr), |
520 |
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
521 |
filerr("write"); |
522 |
if (y < vr-1 && lseek(outfd, |
523 |
(long)(hmult-1)*hr*sizeof(COLR), |
524 |
1) < 0) |
525 |
filerr("seek"); |
526 |
} |
527 |
#if NFS |
528 |
fls.l_type = F_UNLCK; /* release lock */ |
529 |
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
530 |
filerr("lock"); |
531 |
#endif |
532 |
if (verbose) { /* notify caller */ |
533 |
printf("%d %d done\n", xpos, ypos); |
534 |
fflush(stdout); |
535 |
} |
536 |
if (syncfp != NULL) { /* record what's been done */ |
537 |
sflock(F_WRLCK); |
538 |
fseek(syncfp, 0L, 2); /* append index */ |
539 |
fprintf(syncfp, "%4d %4d\n", xpos, ypos); |
540 |
fflush(syncfp); |
541 |
/*** Unlock not necessary, since |
542 |
sflock(F_UNLCK); _exit() or nextpiece() is next ***/ |
543 |
} |
544 |
if (pid == -1) /* didn't fork or fork failed */ |
545 |
return(0); |
546 |
_exit(0); /* else exit child process (releasing locks) */ |
547 |
} |
548 |
|
549 |
|
550 |
filerr(t) /* report file error and exit */ |
551 |
char *t; |
552 |
{ |
553 |
fprintf(stderr, "%s: %s error on file \"%s\": %s\n", |
554 |
progname, t, outfile, sys_errlist[errno]); |
555 |
_exit(1); |
556 |
} |
557 |
|
558 |
#endif |