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 |
|
#include "standard.h" |
12 |
– |
#include <fcntl.h> |
9 |
|
|
10 |
|
#ifndef F_SETLKW |
11 |
|
|
22 |
|
#include <signal.h> |
23 |
|
#include "color.h" |
24 |
|
#include "view.h" |
29 |
– |
#include "resolu.h" |
25 |
|
|
26 |
|
#ifndef NFS |
27 |
|
#define NFS 1 |
35 |
|
#endif |
36 |
|
#endif |
37 |
|
/* protection from SYSV signals(!) */ |
38 |
< |
#if defined(sgi) || defined(hpux) |
38 |
> |
#if defined(sgi) |
39 |
|
#define guard_io() sighold(SIGALRM) |
40 |
|
#define unguard() sigrelse(SIGALRM) |
41 |
|
#endif |
43 |
|
#define guard_io() 0 |
44 |
|
#define unguard() 0 |
45 |
|
#endif |
46 |
+ |
|
47 |
+ |
extern char *strerror(); |
48 |
+ |
|
49 |
|
/* rpict command */ |
50 |
|
char *rpargv[128] = {"rpict", "-S", "1"}; |
51 |
|
int rpargc = 3; |
60 |
|
char *outfile = NULL; |
61 |
|
int outfd; |
62 |
|
long scanorig; |
63 |
< |
int syncfd = -1; /* lock file descriptor */ |
63 |
> |
FILE *syncfp = NULL; /* synchronization file pointer */ |
64 |
> |
int synclst = F_UNLCK; /* synchronization file lock status */ |
65 |
|
int nforked = 0; |
66 |
|
|
67 |
+ |
#define sflock(t) if ((t)!=synclst) dolock(fileno(syncfp),synclst=t) |
68 |
+ |
|
69 |
|
char *progname; |
70 |
|
int verbose = 0; |
71 |
+ |
unsigned timelim = 0; |
72 |
+ |
int rvrlim = -1; |
73 |
|
|
71 |
– |
extern long lseek(), ftell(); |
72 |
– |
|
74 |
|
int gotalrm = 0; |
75 |
< |
int onalrm() { gotalrm++; } |
75 |
> |
void onalrm(int i) { gotalrm++; } |
76 |
|
|
77 |
|
|
78 |
|
main(argc, argv) |
84 |
|
progname = argv[0]; |
85 |
|
for (i = 1; i < argc; i++) { |
86 |
|
/* expand arguments */ |
87 |
< |
while (rval = expandarg(&argc, &argv, i)) |
88 |
< |
if (rval < 0) { |
89 |
< |
fprintf(stderr, "%s: cannot expand '%s'", |
90 |
< |
argv[0], argv[i]); |
91 |
< |
exit(1); |
92 |
< |
} |
87 |
> |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
88 |
> |
; |
89 |
> |
if (rval < 0) { |
90 |
> |
fprintf(stderr, "%s: cannot expand '%s'", |
91 |
> |
argv[0], argv[i]); |
92 |
> |
exit(1); |
93 |
> |
} |
94 |
|
if (argv[i][0] == '-') |
95 |
|
switch (argv[i][1]) { |
96 |
|
case 'v': |
117 |
|
case 'p': /* pixel aspect ratio? */ |
118 |
|
if (argv[i][2] != 'a' || argv[i][3]) |
119 |
|
break; |
120 |
< |
pixaspect = atof(argv[i+1]); |
120 |
> |
pixaspect = atof(argv[++i]); |
121 |
|
continue; |
122 |
+ |
case 'T': /* time limit (hours) */ |
123 |
+ |
if (argv[i][2]) |
124 |
+ |
break; |
125 |
+ |
timelim = atof(argv[++i])*3600. + .5; |
126 |
+ |
break; |
127 |
|
case 'x': /* overall x resolution */ |
128 |
|
if (argv[i][2]) |
129 |
|
break; |
144 |
|
break; |
145 |
|
vmult = atoi(argv[++i]); |
146 |
|
continue; |
147 |
+ |
case 'R': /* recover */ |
148 |
+ |
if (argv[i][2]) |
149 |
+ |
break; |
150 |
+ |
rvrlim = 0; |
151 |
+ |
/* fall through */ |
152 |
|
case 'F': /* syncronization file */ |
153 |
|
if (argv[i][2]) |
154 |
|
break; |
155 |
< |
if ((syncfd = open(argv[++i], |
156 |
< |
O_RDWR|O_CREAT, 0666)) < 0) { |
155 |
> |
if ((syncfp = |
156 |
> |
fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) { |
157 |
|
fprintf(stderr, "%s: cannot open\n", |
158 |
|
argv[i]); |
159 |
|
exit(1); |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
+ |
dolock(fd, ltyp) /* lock or unlock a file */ |
190 |
+ |
int fd; |
191 |
+ |
int ltyp; |
192 |
+ |
{ |
193 |
+ |
static struct flock fls; /* static so initialized to zeroes */ |
194 |
+ |
|
195 |
+ |
fls.l_type = ltyp; |
196 |
+ |
if (fcntl(fd, F_SETLKW, &fls) < 0) { |
197 |
+ |
fprintf(stderr, "%s: cannot lock/unlock file: %s\n", |
198 |
+ |
progname, strerror(errno)); |
199 |
+ |
exit(1); |
200 |
+ |
} |
201 |
+ |
} |
202 |
+ |
|
203 |
+ |
|
204 |
|
init(ac, av) /* set up output file and start rpict */ |
205 |
|
int ac; |
206 |
|
char **av; |
215 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
216 |
|
exit(1); |
217 |
|
} |
218 |
< |
if (syncfd != -1) { |
219 |
< |
char buf[32]; |
220 |
< |
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
221 |
< |
sscanf(buf, "%d %d", &hmult, &vmult); |
218 |
> |
if (syncfp != NULL) { |
219 |
> |
sflock(F_RDLCK); |
220 |
> |
fscanf(syncfp, "%d %d", &hmult, &vmult); |
221 |
> |
sflock(F_UNLCK); |
222 |
|
} |
223 |
|
/* compute piece size */ |
224 |
|
hres /= hmult; |
233 |
|
rpargv[rpargc] = NULL; |
234 |
|
/* open output file */ |
235 |
|
if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) { |
236 |
+ |
dolock(outfd, F_WRLCK); |
237 |
|
if ((fp = fdopen(dup(outfd), "w")) == NULL) |
238 |
|
goto filerr; |
239 |
< |
printargs(ac, av, fp); /* write header */ |
239 |
> |
newheader("RADIANCE", fp); /* create header */ |
240 |
> |
printargs(ac, av, fp); |
241 |
|
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
242 |
|
fputs(VIEWSTR, fp); |
243 |
|
fprintview(&ourview, fp); |
248 |
|
putc('\n', fp); |
249 |
|
fprtresolu(hres*hmult, vres*vmult, fp); |
250 |
|
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
251 |
+ |
dolock(outfd, F_RDLCK); |
252 |
|
if ((fp = fdopen(dup(outfd), "r+")) == NULL) |
253 |
|
goto filerr; |
254 |
|
getheader(fp, NULL, NULL); /* skip header */ |
266 |
|
scanorig = ftell(fp); /* record position of first scanline */ |
267 |
|
if (fclose(fp) == -1) /* done with stream i/o */ |
268 |
|
goto filerr; |
269 |
< |
#if NFS |
240 |
< |
sync(); /* flush NFS buffers */ |
241 |
< |
#endif |
269 |
> |
dolock(outfd, F_UNLCK); |
270 |
|
/* start rpict process */ |
271 |
|
if (open_process(rpd, rpargv) <= 0) { |
272 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
283 |
|
exit(1); |
284 |
|
} |
285 |
|
signal(SIGALRM, onalrm); |
286 |
+ |
if (timelim) |
287 |
+ |
alarm(timelim); |
288 |
|
return; |
289 |
|
filerr: |
290 |
|
fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile); |
296 |
|
nextpiece(xp, yp) /* get next piece assignment */ |
297 |
|
int *xp, *yp; |
298 |
|
{ |
269 |
– |
struct flock fls; |
270 |
– |
char buf[64]; |
271 |
– |
|
299 |
|
if (gotalrm) /* someone wants us to quit */ |
300 |
|
return(0); |
301 |
< |
if (syncfd != -1) { /* use sync file */ |
302 |
< |
fls.l_type = F_WRLCK; /* gain exclusive access */ |
303 |
< |
fls.l_whence = 0; |
304 |
< |
fls.l_start = 0L; |
305 |
< |
fls.l_len = 0L; |
306 |
< |
fcntl(syncfd, F_SETLKW, &fls); |
307 |
< |
lseek(syncfd, 0L, 0); |
308 |
< |
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
309 |
< |
if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) { |
301 |
> |
if (syncfp != NULL) { /* use sync file */ |
302 |
> |
/* |
303 |
> |
* So we don't necessarily have to lock and unlock the file |
304 |
> |
* multiple times (very slow), we establish an exclusive |
305 |
> |
* lock at the beginning on our synchronization file and |
306 |
> |
* maintain it in the subroutine rvrpiece(). |
307 |
> |
*/ |
308 |
> |
sflock(F_WRLCK); |
309 |
> |
fseek(syncfp, 0L, 0); /* read position */ |
310 |
> |
if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) { |
311 |
|
*xp = hmult-1; |
312 |
|
*yp = vmult; |
313 |
|
} |
314 |
+ |
if (rvrlim == 0) /* initialize recovery limit */ |
315 |
+ |
rvrlim = *xp*vmult + *yp; |
316 |
+ |
if (rvrpiece(xp, yp)) { /* do stragglers first */ |
317 |
+ |
sflock(F_UNLCK); |
318 |
+ |
return(1); |
319 |
+ |
} |
320 |
|
if (--(*yp) < 0) { /* decrement position */ |
321 |
|
*yp = vmult-1; |
322 |
< |
if (--(*xp) < 0) { /* all done! */ |
323 |
< |
close(syncfd); |
322 |
> |
if (--(*xp) < 0) { /* all done */ |
323 |
> |
sflock(F_UNLCK); |
324 |
|
return(0); |
325 |
|
} |
326 |
|
} |
327 |
< |
sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp); |
328 |
< |
lseek(syncfd, 0L, 0); /* write new position */ |
329 |
< |
write(syncfd, buf, strlen(buf)); |
330 |
< |
fls.l_type = F_UNLCK; /* release sync file */ |
297 |
< |
fcntl(syncfd, F_SETLKW, &fls); |
327 |
> |
fseek(syncfp, 0L, 0); /* write new position */ |
328 |
> |
fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp); |
329 |
> |
fflush(syncfp); |
330 |
> |
sflock(F_UNLCK); /* release sync file */ |
331 |
|
return(1); |
332 |
|
} |
333 |
< |
if (fgets(buf, sizeof(buf), stdin) == NULL) /* use stdin */ |
301 |
< |
return(0); |
302 |
< |
if (sscanf(buf, "%d %d", xp, yp) == 2) |
303 |
< |
return(1); |
304 |
< |
fprintf(stderr, "%s: input format error\n", progname); |
305 |
< |
exit(cleanup(1)); |
333 |
> |
return(scanf("%d %d", xp, yp) == 2); /* use stdin */ |
334 |
|
} |
335 |
|
|
336 |
|
|
337 |
|
int |
338 |
+ |
rvrpiece(xp, yp) /* check for recoverable pieces */ |
339 |
+ |
register int *xp, *yp; |
340 |
+ |
{ |
341 |
+ |
static char *pdone = NULL; /* which pieces are done */ |
342 |
+ |
static long readpos = -1; /* how far we've read */ |
343 |
+ |
register int i; |
344 |
+ |
/* |
345 |
+ |
* This routine is called by nextpiece() with an |
346 |
+ |
* exclusive lock on syncfp and the file pointer at the |
347 |
+ |
* appropriate position to read in the finished pieces. |
348 |
+ |
*/ |
349 |
+ |
if (rvrlim < 0) |
350 |
+ |
return(0); /* only check if asked */ |
351 |
+ |
if (pdone == NULL) /* first call */ |
352 |
+ |
pdone = calloc(hmult*vmult, sizeof(char)); |
353 |
+ |
if (pdone == NULL) { |
354 |
+ |
fprintf(stderr, "%s: out of memory\n", progname); |
355 |
+ |
exit(1); |
356 |
+ |
} |
357 |
+ |
if (readpos != -1) /* mark what's been done */ |
358 |
+ |
fseek(syncfp, readpos, 0); |
359 |
+ |
while (fscanf(syncfp, "%d %d", xp, yp) == 2) |
360 |
+ |
pdone[*xp*vmult+*yp] = 1; |
361 |
+ |
if (!feof(syncfp)) { |
362 |
+ |
fprintf(stderr, "%s: format error in sync file\n", progname); |
363 |
+ |
exit(1); |
364 |
+ |
} |
365 |
+ |
readpos = ftell(syncfp); |
366 |
+ |
i = hmult*vmult; /* find an unaccounted for piece */ |
367 |
+ |
while (i-- > rvrlim) |
368 |
+ |
if (!pdone[i]) { |
369 |
+ |
*xp = i / vmult; |
370 |
+ |
*yp = i % vmult; |
371 |
+ |
pdone[i] = 1; /* consider it done */ |
372 |
+ |
return(1); |
373 |
+ |
} |
374 |
+ |
rvrlim = -1; /* nothing left to recover */ |
375 |
+ |
free(pdone); |
376 |
+ |
pdone = NULL; |
377 |
+ |
return(0); |
378 |
+ |
} |
379 |
+ |
|
380 |
+ |
|
381 |
+ |
int |
382 |
|
cleanup(rstat) /* close rpict process and clean up */ |
383 |
|
int rstat; |
384 |
|
{ |
412 |
|
pview.horiz = ourview.horiz / hmult; |
413 |
|
pview.vert = ourview.vert / vmult; |
414 |
|
break; |
415 |
+ |
case VT_CYL: |
416 |
+ |
pview.horiz = ourview.horiz / hmult; |
417 |
+ |
pview.vert = 2.*180./PI*atan( |
418 |
+ |
tan(PI/180./2.*ourview.vert)/vmult ); |
419 |
+ |
break; |
420 |
|
case VT_HEM: |
421 |
|
pview.horiz = 2.*180./PI*asin( |
422 |
|
sin(PI/180./2.*ourview.horiz)/hmult ); |
430 |
|
} |
431 |
|
/* render each piece */ |
432 |
|
while (nextpiece(&xorg, &yorg)) { |
433 |
< |
pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1); |
434 |
< |
pview.voff = ourview.voff + yorg - 0.5*(vmult-1); |
433 |
> |
pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1); |
434 |
> |
pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1); |
435 |
|
fputs(VIEWSTR, torp); |
436 |
|
fprintview(&pview, torp); |
437 |
|
putc('\n', torp); |
463 |
|
progname, rpargv[0]); |
464 |
|
exit(cleanup(1)); |
465 |
|
} |
466 |
+ |
if (verbose) { /* notify caller */ |
467 |
+ |
printf("%d %d begun\n", xpos, ypos); |
468 |
+ |
fflush(stdout); |
469 |
+ |
} |
470 |
|
unguard(); |
471 |
|
/* load new piece into buffer */ |
472 |
|
for (y = 0; y < vr; y++) { |
499 |
|
/* lock file section so NFS doesn't mess up */ |
500 |
|
fls.l_whence = 0; |
501 |
|
fls.l_type = F_WRLCK; |
502 |
< |
fcntl(outfd, F_SETLKW, &fls); |
502 |
> |
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
503 |
> |
filerr("lock"); |
504 |
|
#endif |
505 |
|
/* write new piece to file */ |
506 |
< |
if (lseek(outfd, fls.l_start, 0) == -1) |
507 |
< |
goto seekerr; |
506 |
> |
if (lseek(outfd, (off_t)fls.l_start, 0) < 0) |
507 |
> |
filerr("seek"); |
508 |
|
if (hmult == 1) { |
509 |
|
if (writebuf(outfd, (char *)pbuf, |
510 |
|
vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR)) |
511 |
< |
goto writerr; |
511 |
> |
filerr("write"); |
512 |
|
} else |
513 |
|
for (y = 0; y < vr; y++) { |
514 |
|
if (writebuf(outfd, (char *)(pbuf+y*hr), |
515 |
|
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
516 |
< |
goto writerr; |
516 |
> |
filerr("write"); |
517 |
|
if (y < vr-1 && lseek(outfd, |
518 |
< |
(long)(hmult-1)*hr*sizeof(COLR), |
519 |
< |
1) == -1) |
520 |
< |
goto seekerr; |
518 |
> |
(off_t)(hmult-1)*hr*sizeof(COLR), |
519 |
> |
1) < 0) |
520 |
> |
filerr("seek"); |
521 |
|
} |
522 |
+ |
#if NFS |
523 |
+ |
fls.l_type = F_UNLCK; /* release lock */ |
524 |
+ |
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
525 |
+ |
filerr("lock"); |
526 |
+ |
#endif |
527 |
|
if (verbose) { /* notify caller */ |
528 |
|
printf("%d %d done\n", xpos, ypos); |
529 |
|
fflush(stdout); |
530 |
|
} |
531 |
< |
if (pid == -1) { /* didn't fork or fork failed */ |
532 |
< |
#if NFS |
533 |
< |
fls.l_type = F_UNLCK; /* release lock */ |
534 |
< |
fcntl(outfd, F_SETLKW, &fls); |
535 |
< |
#endif |
536 |
< |
return(0); |
531 |
> |
if (syncfp != NULL) { /* record what's been done */ |
532 |
> |
sflock(F_WRLCK); |
533 |
> |
fseek(syncfp, 0L, 2); /* append index */ |
534 |
> |
fprintf(syncfp, "%4d %4d\n", xpos, ypos); |
535 |
> |
fflush(syncfp); |
536 |
> |
/*** Unlock not necessary, since |
537 |
> |
sflock(F_UNLCK); _exit() or nextpiece() is next ***/ |
538 |
|
} |
539 |
< |
_exit(0); /* else exit child process (releasing lock) */ |
540 |
< |
seekerr: |
541 |
< |
fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile); |
542 |
< |
_exit(1); |
543 |
< |
writerr: |
544 |
< |
fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile); |
539 |
> |
if (pid == -1) /* didn't fork or fork failed */ |
540 |
> |
return(0); |
541 |
> |
_exit(0); /* else exit child process (releasing locks) */ |
542 |
> |
} |
543 |
> |
|
544 |
> |
|
545 |
> |
filerr(t) /* report file error and exit */ |
546 |
> |
char *t; |
547 |
> |
{ |
548 |
> |
fprintf(stderr, "%s: %s error on file \"%s\": %s\n", |
549 |
> |
progname, t, outfile, strerror(errno)); |
550 |
|
_exit(1); |
551 |
|
} |
552 |
|
|