ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.39
Committed: Thu Jun 26 00:58:11 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.38: +7 -5 lines
Log Message:
Abstracted process and path handling for Windows.
Renamed FLOAT to RREAL because of conflict on Windows.
Added conditional compiles for some signal handlers.

File Contents

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