ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.53
Committed: Sat Jan 21 22:04:02 2012 UTC (12 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.52: +20 -3 lines
Log Message:
Added warning about altered output resolution

File Contents

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