1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rpiece.c,v 2.37 2003/02/22 02:07:30 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Generate sections of a picture. |
6 |
*/ |
7 |
|
8 |
#include "standard.h" |
9 |
|
10 |
#ifndef F_SETLKW |
11 |
|
12 |
main(argc, argv) |
13 |
int argc; |
14 |
char *argv[]; |
15 |
{ |
16 |
fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]); |
17 |
exit(1); |
18 |
} |
19 |
|
20 |
#else |
21 |
|
22 |
#include <signal.h> |
23 |
#include "color.h" |
24 |
#include "view.h" |
25 |
|
26 |
#ifndef NFS |
27 |
#define NFS 1 |
28 |
#endif |
29 |
/* set the following to 0 to forgo forking */ |
30 |
#ifndef MAXFORK |
31 |
#if NFS |
32 |
#define MAXFORK 3 /* allotment of duped processes */ |
33 |
#else |
34 |
#define MAXFORK 0 |
35 |
#endif |
36 |
#endif |
37 |
/* protection from SYSV signals(!) */ |
38 |
#if defined(sgi) |
39 |
#define guard_io() sighold(SIGALRM) |
40 |
#define unguard() sigrelse(SIGALRM) |
41 |
#endif |
42 |
#ifndef guard_io |
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; |
52 |
int rpd[3]; |
53 |
FILE *torp, *fromrp; |
54 |
COLR *pbuf; |
55 |
/* our view parameters */ |
56 |
VIEW ourview = STDVIEW; |
57 |
double pixaspect = 1.0; |
58 |
int hres = 1024, vres = 1024, hmult = 4, vmult = 4; |
59 |
/* output file */ |
60 |
char *outfile = NULL; |
61 |
int outfd; |
62 |
long scanorig; |
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 |
|
74 |
int gotalrm = 0; |
75 |
void onalrm(int i) { gotalrm++; } |
76 |
|
77 |
|
78 |
main(argc, argv) |
79 |
int argc; |
80 |
char *argv[]; |
81 |
{ |
82 |
register int i, rval; |
83 |
|
84 |
progname = argv[0]; |
85 |
for (i = 1; i < argc; i++) { |
86 |
/* expand arguments */ |
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': |
97 |
switch (argv[i][2]) { |
98 |
case '\0': /* verbose option */ |
99 |
verbose = !verbose; |
100 |
continue; |
101 |
case 'f': /* view file */ |
102 |
if (viewfile(argv[++i], &ourview, NULL) <= 0) { |
103 |
fprintf(stderr, |
104 |
"%s: not a view file\n", argv[i]); |
105 |
exit(1); |
106 |
} |
107 |
continue; |
108 |
default: /* view option? */ |
109 |
rval = getviewopt(&ourview, argc-i, argv+i); |
110 |
if (rval >= 0) { |
111 |
i += rval; |
112 |
continue; |
113 |
} |
114 |
break; |
115 |
} |
116 |
break; |
117 |
case 'p': /* pixel aspect ratio? */ |
118 |
if (argv[i][2] != 'a' || argv[i][3]) |
119 |
break; |
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; |
130 |
hres = atoi(argv[++i]); |
131 |
continue; |
132 |
case 'y': /* overall y resolution */ |
133 |
if (argv[i][2]) |
134 |
break; |
135 |
vres = atoi(argv[++i]); |
136 |
continue; |
137 |
case 'X': /* horizontal multiplier */ |
138 |
if (argv[i][2]) |
139 |
break; |
140 |
hmult = atoi(argv[++i]); |
141 |
continue; |
142 |
case 'Y': /* vertical multiplier */ |
143 |
if (argv[i][2]) |
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 ((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); |
160 |
} |
161 |
continue; |
162 |
case 'z': /* z-file ist verbotten */ |
163 |
fprintf(stderr, "%s: -z option not allowed\n", |
164 |
argv[0]); |
165 |
exit(1); |
166 |
case 'o': /* output file */ |
167 |
if (argv[i][2]) |
168 |
break; |
169 |
outfile = argv[++i]; |
170 |
continue; |
171 |
} else if (i >= argc-1) |
172 |
break; |
173 |
rpargv[rpargc++] = argv[i]; |
174 |
} |
175 |
if (i >= argc) { |
176 |
fprintf(stderr, "%s: missing octree argument\n", argv[0]); |
177 |
exit(1); |
178 |
} |
179 |
if (outfile == NULL) { |
180 |
fprintf(stderr, "%s: missing output file\n", argv[0]); |
181 |
exit(1); |
182 |
} |
183 |
init(argc, argv); |
184 |
rpiece(); |
185 |
exit(cleanup(0)); |
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; |
207 |
{ |
208 |
static char hrbuf[16], vrbuf[16]; |
209 |
extern char VersionID[]; |
210 |
char *err; |
211 |
FILE *fp; |
212 |
int hr, vr; |
213 |
/* set up view */ |
214 |
if ((err = setview(&ourview)) != NULL) { |
215 |
fprintf(stderr, "%s: %s\n", progname, err); |
216 |
exit(1); |
217 |
} |
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; |
225 |
vres /= vmult; |
226 |
normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres); |
227 |
sprintf(hrbuf, "%d", hres); |
228 |
rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf; |
229 |
sprintf(vrbuf, "%d", vres); |
230 |
rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf; |
231 |
rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0"; |
232 |
rpargv[rpargc++] = av[ac-1]; |
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 |
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); |
244 |
putc('\n', fp); |
245 |
if (pixaspect < .99 || pixaspect > 1.01) |
246 |
fputaspect(pixaspect, fp); |
247 |
fputformat(COLRFMT, 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 */ |
255 |
if (!fscnresolu(&hr, &vr, fp) || /* check resolution */ |
256 |
hr != hres*hmult || vr != vres*vmult) { |
257 |
fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n", |
258 |
progname, outfile); |
259 |
exit(1); |
260 |
} |
261 |
} else { |
262 |
fprintf(stderr, "%s: cannot open file \"%s\"\n", |
263 |
progname, outfile); |
264 |
exit(1); |
265 |
} |
266 |
scanorig = ftell(fp); /* record position of first scanline */ |
267 |
if (fclose(fp) == -1) /* done with stream i/o */ |
268 |
goto filerr; |
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]); |
273 |
exit(1); |
274 |
} |
275 |
if ((fromrp = fdopen(rpd[0], "r")) == NULL || |
276 |
(torp = fdopen(rpd[1], "w")) == NULL) { |
277 |
fprintf(stderr, "%s: cannot open stream to %s\n", |
278 |
progname, rpargv[0]); |
279 |
exit(1); |
280 |
} |
281 |
if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) { |
282 |
fprintf(stderr, "%s: out of memory\n", progname); |
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); |
291 |
exit(1); |
292 |
} |
293 |
|
294 |
|
295 |
int |
296 |
nextpiece(xp, yp) /* get next piece assignment */ |
297 |
int *xp, *yp; |
298 |
{ |
299 |
if (gotalrm) /* someone wants us to quit */ |
300 |
return(0); |
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 |
sflock(F_UNLCK); |
324 |
return(0); |
325 |
} |
326 |
} |
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 |
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 |
{ |
385 |
int status; |
386 |
|
387 |
bfree((char *)pbuf, hres*vres*sizeof(COLR)); |
388 |
fclose(torp); |
389 |
fclose(fromrp); |
390 |
while (wait(&status) != -1) |
391 |
if (rstat == 0) |
392 |
rstat = status>>8 & 0xff; |
393 |
return(rstat); |
394 |
} |
395 |
|
396 |
|
397 |
rpiece() /* render picture piece by piece */ |
398 |
{ |
399 |
VIEW pview; |
400 |
int xorg, yorg; |
401 |
/* compute view parameters */ |
402 |
copystruct(&pview, &ourview); |
403 |
switch (ourview.type) { |
404 |
case VT_PER: |
405 |
pview.horiz = 2.*180./PI*atan( |
406 |
tan(PI/180./2.*ourview.horiz)/hmult ); |
407 |
pview.vert = 2.*180./PI*atan( |
408 |
tan(PI/180./2.*ourview.vert)/vmult ); |
409 |
break; |
410 |
case VT_PAR: |
411 |
case VT_ANG: |
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 ); |
423 |
pview.vert = 2.*180./PI*asin( |
424 |
sin(PI/180./2.*ourview.vert)/vmult ); |
425 |
break; |
426 |
default: |
427 |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
428 |
progname, ourview.type); |
429 |
exit(cleanup(1)); |
430 |
} |
431 |
/* render each piece */ |
432 |
while (nextpiece(&xorg, &yorg)) { |
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); |
438 |
fflush(torp); /* assigns piece to rpict */ |
439 |
putpiece(xorg, yorg); /* place piece in output */ |
440 |
} |
441 |
} |
442 |
|
443 |
|
444 |
int |
445 |
putpiece(xpos, ypos) /* get next piece from rpict */ |
446 |
int xpos, ypos; |
447 |
{ |
448 |
struct flock fls; |
449 |
int pid, status; |
450 |
int hr, vr; |
451 |
register int y; |
452 |
/* check bounds */ |
453 |
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
454 |
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
455 |
progname, xpos, ypos); |
456 |
exit(cleanup(1)); |
457 |
} |
458 |
/* check header from rpict */ |
459 |
guard_io(); |
460 |
getheader(fromrp, NULL, NULL); |
461 |
if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) { |
462 |
fprintf(stderr, "%s: resolution mismatch from %s\n", |
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++) { |
473 |
guard_io(); |
474 |
if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) { |
475 |
fprintf(stderr, "%s: read error from %s\n", |
476 |
progname, rpargv[0]); |
477 |
exit(cleanup(1)); |
478 |
} |
479 |
unguard(); |
480 |
} |
481 |
#if MAXFORK |
482 |
/* fork so we don't slow rpict down */ |
483 |
if ((pid = fork()) > 0) { |
484 |
if (++nforked >= MAXFORK) { |
485 |
wait(&status); /* reap a child */ |
486 |
if (status) |
487 |
exit(cleanup(status>>8 & 0xff)); |
488 |
nforked--; |
489 |
} |
490 |
return(pid); |
491 |
} |
492 |
#else |
493 |
pid = -1; /* no forking */ |
494 |
#endif |
495 |
fls.l_start = scanorig + |
496 |
((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR); |
497 |
#if NFS |
498 |
fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR); |
499 |
/* lock file section so NFS doesn't mess up */ |
500 |
fls.l_whence = 0; |
501 |
fls.l_type = F_WRLCK; |
502 |
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
503 |
filerr("lock"); |
504 |
#endif |
505 |
/* write new piece to file */ |
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 |
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 |
filerr("write"); |
517 |
if (y < vr-1 && lseek(outfd, |
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 (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 |
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 |
|
553 |
#endif |