ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.44
Committed: Tue Jun 8 19:48:31 2004 UTC (19 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6
Changes since 2.43: +2 -3 lines
Log Message:
Removed redundant #include's and fixed ordering on some headers

File Contents

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