ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.34
Committed: Wed May 28 17:22:52 1997 UTC (26 years, 10 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 2.33: +5 -3 lines
Log Message:
fix for Alpha Linux

File Contents

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