ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.51
Committed: Mon May 17 17:03:53 2010 UTC (13 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.50: +8 -2 lines
Log Message:
Prevent use of -pm option, which can mess up results

File Contents

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