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

# User Rev Content
1 greg 2.1 #ifndef lint
2 schorsch 2.39 static const char RCSid[] = "$Id: rpiece.c,v 2.38 2003/06/08 12:03:11 schorsch Exp $";
3 greg 2.1 #endif
4     /*
5     * Generate sections of a picture.
6     */
7    
8     #include "standard.h"
9 greg 2.16
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 greg 2.6 #include <signal.h>
23 schorsch 2.39
24 greg 2.1 #include "color.h"
25     #include "view.h"
26 schorsch 2.39 #include "rtprocess.h"
27 greg 2.1
28 greg 2.12 #ifndef NFS
29     #define NFS 1
30     #endif
31 greg 2.9 /* set the following to 0 to forgo forking */
32     #ifndef MAXFORK
33 greg 2.12 #if NFS
34 greg 2.10 #define MAXFORK 3 /* allotment of duped processes */
35 greg 2.12 #else
36     #define MAXFORK 0
37 greg 2.9 #endif
38 greg 2.12 #endif
39 greg 2.18 /* protection from SYSV signals(!) */
40 greg 2.27 #if defined(sgi)
41 greg 2.18 #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 gregl 2.34
49 greg 2.37 extern char *strerror();
50 gregl 2.34
51 greg 2.1 /* rpict command */
52 greg 2.23 char *rpargv[128] = {"rpict", "-S", "1"};
53     int rpargc = 3;
54 greg 2.1 FILE *torp, *fromrp;
55 greg 2.8 COLR *pbuf;
56 greg 2.1 /* our view parameters */
57     VIEW ourview = STDVIEW;
58 greg 2.4 double pixaspect = 1.0;
59 greg 2.23 int hres = 1024, vres = 1024, hmult = 4, vmult = 4;
60 greg 2.1 /* output file */
61     char *outfile = NULL;
62     int outfd;
63     long scanorig;
64 greg 2.25 FILE *syncfp = NULL; /* synchronization file pointer */
65     int synclst = F_UNLCK; /* synchronization file lock status */
66 greg 2.9 int nforked = 0;
67 greg 2.1
68 greg 2.25 #define sflock(t) if ((t)!=synclst) dolock(fileno(syncfp),synclst=t)
69    
70 greg 2.1 char *progname;
71     int verbose = 0;
72 greg 2.31 unsigned timelim = 0;
73 greg 2.25 int rvrlim = -1;
74 greg 2.1
75 greg 2.6 int gotalrm = 0;
76 schorsch 2.38 void onalrm(int i) { gotalrm++; }
77 greg 2.1
78 greg 2.6
79 greg 2.1 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 greg 2.22 /* expand arguments */
88 greg 2.37 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 greg 2.1 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 greg 2.23 if (argv[i][2] != 'a' || argv[i][3])
120     break;
121 gregl 2.35 pixaspect = atof(argv[++i]);
122 greg 2.23 continue;
123 greg 2.31 case 'T': /* time limit (hours) */
124     if (argv[i][2])
125     break;
126     timelim = atof(argv[++i])*3600. + .5;
127     break;
128 greg 2.23 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 greg 2.1 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 greg 2.25 case 'R': /* recover */
149     if (argv[i][2])
150     break;
151     rvrlim = 0;
152     /* fall through */
153 greg 2.5 case 'F': /* syncronization file */
154     if (argv[i][2])
155     break;
156 greg 2.28 if ((syncfp =
157     fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
158 greg 2.5 fprintf(stderr, "%s: cannot open\n",
159     argv[i]);
160     exit(1);
161     }
162     continue;
163 greg 2.15 case 'z': /* z-file ist verbotten */
164     fprintf(stderr, "%s: -z option not allowed\n",
165     argv[0]);
166     exit(1);
167 greg 2.1 case 'o': /* output file */
168     if (argv[i][2])
169     break;
170     outfile = argv[++i];
171     continue;
172 greg 2.23 } else if (i >= argc-1)
173     break;
174 greg 2.1 rpargv[rpargc++] = argv[i];
175     }
176 greg 2.23 if (i >= argc) {
177     fprintf(stderr, "%s: missing octree argument\n", argv[0]);
178     exit(1);
179     }
180 greg 2.1 if (outfile == NULL) {
181     fprintf(stderr, "%s: missing output file\n", argv[0]);
182     exit(1);
183     }
184     init(argc, argv);
185     rpiece();
186 greg 2.15 exit(cleanup(0));
187 greg 2.1 }
188    
189    
190 greg 2.25 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 greg 2.28 fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
199 greg 2.37 progname, strerror(errno));
200 greg 2.25 exit(1);
201     }
202     }
203    
204    
205 greg 2.1 init(ac, av) /* set up output file and start rpict */
206     int ac;
207     char **av;
208     {
209 greg 2.23 static char hrbuf[16], vrbuf[16];
210 greg 2.2 extern char VersionID[];
211 greg 2.1 char *err;
212     FILE *fp;
213     int hr, vr;
214 schorsch 2.39 SUBPROC rpd; /* since we don't close_process(), this can be local */
215 greg 2.1 /* set up view */
216     if ((err = setview(&ourview)) != NULL) {
217     fprintf(stderr, "%s: %s\n", progname, err);
218     exit(1);
219     }
220 greg 2.25 if (syncfp != NULL) {
221     sflock(F_RDLCK);
222     fscanf(syncfp, "%d %d", &hmult, &vmult);
223     sflock(F_UNLCK);
224 greg 2.5 }
225 greg 2.23 /* compute piece size */
226     hres /= hmult;
227     vres /= vmult;
228 greg 2.1 normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
229 greg 2.23 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 greg 2.1 /* open output file */
237     if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
238 greg 2.25 dolock(outfd, F_WRLCK);
239 greg 2.1 if ((fp = fdopen(dup(outfd), "w")) == NULL)
240     goto filerr;
241 greg 2.29 newheader("RADIANCE", fp); /* create header */
242     printargs(ac, av, fp);
243 greg 2.2 fprintf(fp, "SOFTWARE= %s\n", VersionID);
244 greg 2.1 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 greg 2.25 dolock(outfd, F_RDLCK);
254 greg 2.1 if ((fp = fdopen(dup(outfd), "r+")) == NULL)
255     goto filerr;
256 greg 2.13 getheader(fp, NULL, NULL); /* skip header */
257 greg 2.15 if (!fscnresolu(&hr, &vr, fp) || /* check resolution */
258 greg 2.1 hr != hres*hmult || vr != vres*vmult) {
259 greg 2.8 fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
260     progname, outfile);
261 greg 2.1 exit(1);
262     }
263     } else {
264 greg 2.8 fprintf(stderr, "%s: cannot open file \"%s\"\n",
265     progname, outfile);
266 greg 2.1 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 greg 2.25 dolock(outfd, F_UNLCK);
272 greg 2.1 /* start rpict process */
273 schorsch 2.39 if (open_process(&rpd, rpargv) <= 0) {
274 greg 2.3 fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
275 greg 2.1 exit(1);
276     }
277 schorsch 2.39 if ((fromrp = fdopen(rpd.r, "r")) == NULL ||
278     (torp = fdopen(rpd.w, "w")) == NULL) {
279 greg 2.1 fprintf(stderr, "%s: cannot open stream to %s\n",
280     progname, rpargv[0]);
281     exit(1);
282     }
283 greg 2.23 if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) {
284 greg 2.1 fprintf(stderr, "%s: out of memory\n", progname);
285     exit(1);
286     }
287 greg 2.6 signal(SIGALRM, onalrm);
288 greg 2.31 if (timelim)
289     alarm(timelim);
290 greg 2.1 return;
291     filerr:
292 greg 2.8 fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
293 greg 2.1 exit(1);
294     }
295    
296    
297     int
298 greg 2.5 nextpiece(xp, yp) /* get next piece assignment */
299     int *xp, *yp;
300     {
301 greg 2.6 if (gotalrm) /* someone wants us to quit */
302     return(0);
303 greg 2.25 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 greg 2.8 *xp = hmult-1;
314     *yp = vmult;
315     }
316 greg 2.25 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 greg 2.8 if (--(*yp) < 0) { /* decrement position */
323 greg 2.5 *yp = vmult-1;
324 greg 2.25 if (--(*xp) < 0) { /* all done */
325     sflock(F_UNLCK);
326 greg 2.5 return(0);
327     }
328     }
329 greg 2.25 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 greg 2.5 return(1);
334     }
335 greg 2.26 return(scanf("%d %d", xp, yp) == 2); /* use stdin */
336 greg 2.5 }
337    
338    
339     int
340 greg 2.25 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 greg 2.30 if (pdone == NULL) {
356     fprintf(stderr, "%s: out of memory\n", progname);
357     exit(1);
358     }
359 greg 2.25 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 greg 2.10 cleanup(rstat) /* close rpict process and clean up */
385     int rstat;
386 greg 2.1 {
387 greg 2.10 int status;
388 greg 2.1
389 greg 2.23 bfree((char *)pbuf, hres*vres*sizeof(COLR));
390 greg 2.1 fclose(torp);
391     fclose(fromrp);
392 greg 2.10 while (wait(&status) != -1)
393 greg 2.9 if (rstat == 0)
394     rstat = status>>8 & 0xff;
395     return(rstat);
396 greg 2.1 }
397    
398    
399     rpiece() /* render picture piece by piece */
400     {
401     VIEW pview;
402     int xorg, yorg;
403 greg 2.11 /* 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 greg 2.32 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 greg 2.11 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 greg 2.5 while (nextpiece(&xorg, &yorg)) {
435 gregl 2.33 pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1);
436     pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1);
437 greg 2.1 fputs(VIEWSTR, torp);
438     fprintview(&pview, torp);
439     putc('\n', torp);
440 greg 2.11 fflush(torp); /* assigns piece to rpict */
441 greg 2.1 putpiece(xorg, yorg); /* place piece in output */
442     }
443     }
444    
445    
446 greg 2.9 int
447 greg 2.1 putpiece(xpos, ypos) /* get next piece from rpict */
448     int xpos, ypos;
449     {
450 greg 2.7 struct flock fls;
451 greg 2.9 int pid, status;
452 greg 2.1 int hr, vr;
453 greg 2.8 register int y;
454     /* check bounds */
455 greg 2.5 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 greg 2.10 exit(cleanup(1));
459 greg 2.5 }
460 greg 2.8 /* check header from rpict */
461 greg 2.18 guard_io();
462 greg 2.13 getheader(fromrp, NULL, NULL);
463 greg 2.15 if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
464 greg 2.3 fprintf(stderr, "%s: resolution mismatch from %s\n",
465     progname, rpargv[0]);
466 greg 2.10 exit(cleanup(1));
467 greg 2.1 }
468 greg 2.24 if (verbose) { /* notify caller */
469     printf("%d %d begun\n", xpos, ypos);
470     fflush(stdout);
471     }
472 greg 2.18 unguard();
473 greg 2.8 /* load new piece into buffer */
474 greg 2.18 for (y = 0; y < vr; y++) {
475     guard_io();
476 greg 2.8 if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
477 greg 2.3 fprintf(stderr, "%s: read error from %s\n",
478     progname, rpargv[0]);
479 greg 2.10 exit(cleanup(1));
480 greg 2.1 }
481 greg 2.18 unguard();
482     }
483 greg 2.9 #if MAXFORK
484     /* fork so we don't slow rpict down */
485     if ((pid = fork()) > 0) {
486 greg 2.10 if (++nforked >= MAXFORK) {
487 greg 2.9 wait(&status); /* reap a child */
488     if (status)
489 greg 2.10 exit(cleanup(status>>8 & 0xff));
490 greg 2.9 nforked--;
491     }
492     return(pid);
493     }
494     #else
495     pid = -1; /* no forking */
496     #endif
497 greg 2.19 fls.l_start = scanorig +
498     ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
499 greg 2.12 #if NFS
500 greg 2.19 fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
501 greg 2.8 /* lock file section so NFS doesn't mess up */
502     fls.l_whence = 0;
503     fls.l_type = F_WRLCK;
504 greg 2.26 if (fcntl(outfd, F_SETLKW, &fls) < 0)
505     filerr("lock");
506 greg 2.12 #endif
507 greg 2.8 /* write new piece to file */
508 greg 2.37 if (lseek(outfd, (off_t)fls.l_start, 0) < 0)
509 greg 2.26 filerr("seek");
510 greg 2.9 if (hmult == 1) {
511     if (writebuf(outfd, (char *)pbuf,
512     vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
513 greg 2.26 filerr("write");
514 greg 2.9 } else
515     for (y = 0; y < vr; y++) {
516     if (writebuf(outfd, (char *)(pbuf+y*hr),
517     hr*sizeof(COLR)) != hr*sizeof(COLR))
518 greg 2.26 filerr("write");
519 greg 2.9 if (y < vr-1 && lseek(outfd,
520 greg 2.37 (off_t)(hmult-1)*hr*sizeof(COLR),
521 gwlarson 2.36 1) < 0)
522 greg 2.26 filerr("seek");
523 greg 2.1 }
524 greg 2.25 #if NFS
525     fls.l_type = F_UNLCK; /* release lock */
526 greg 2.26 if (fcntl(outfd, F_SETLKW, &fls) < 0)
527     filerr("lock");
528 greg 2.25 #endif
529 greg 2.32 if (verbose) { /* notify caller */
530     printf("%d %d done\n", xpos, ypos);
531     fflush(stdout);
532     }
533 greg 2.25 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 greg 2.20 }
541 greg 2.25 if (pid == -1) /* didn't fork or fork failed */
542 greg 2.9 return(0);
543 greg 2.25 _exit(0); /* else exit child process (releasing locks) */
544 greg 2.26 }
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 greg 2.37 progname, t, outfile, strerror(errno));
552 greg 2.9 _exit(1);
553 greg 2.1 }
554 greg 2.16
555     #endif