ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.43
Committed: Fri Mar 26 21:36:20 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.42: +68 -37 lines
Log Message:
Continued ANSIfication.

File Contents

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