ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.47
Committed: Wed Aug 22 18:46:36 2007 UTC (16 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.46: +2 -2 lines
Log Message:
Fixed bug in bug fix...

File Contents

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