ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.24
Committed: Fri Aug 6 12:58:43 1993 UTC (30 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.23: +4 -0 lines
Log Message:
added mention of piece beginning to verbose output

File Contents

# User Rev Content
1 greg 2.19 /* Copyright (c) 1993 Regents of the University of California */
2 greg 2.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Generate sections of a picture.
9     */
10    
11     #include "standard.h"
12     #include <fcntl.h>
13 greg 2.16
14     #ifndef F_SETLKW
15    
16     main(argc, argv)
17     int argc;
18     char *argv[];
19     {
20     fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]);
21     exit(1);
22     }
23    
24     #else
25    
26 greg 2.6 #include <signal.h>
27 greg 2.1 #include "color.h"
28     #include "view.h"
29     #include "resolu.h"
30    
31 greg 2.12 #ifndef NFS
32     #define NFS 1
33     #endif
34 greg 2.9 /* set the following to 0 to forgo forking */
35     #ifndef MAXFORK
36 greg 2.12 #if NFS
37 greg 2.10 #define MAXFORK 3 /* allotment of duped processes */
38 greg 2.12 #else
39     #define MAXFORK 0
40 greg 2.9 #endif
41 greg 2.12 #endif
42 greg 2.18 /* protection from SYSV signals(!) */
43     #if defined(sgi) || defined(hpux)
44     #define guard_io() sighold(SIGALRM)
45     #define unguard() sigrelse(SIGALRM)
46     #endif
47     #ifndef guard_io
48     #define guard_io() 0
49     #define unguard() 0
50     #endif
51 greg 2.1 /* rpict command */
52 greg 2.23 char *rpargv[128] = {"rpict", "-S", "1"};
53     int rpargc = 3;
54 greg 2.1 int rpd[3];
55     FILE *torp, *fromrp;
56 greg 2.8 COLR *pbuf;
57 greg 2.1 /* our view parameters */
58     VIEW ourview = STDVIEW;
59 greg 2.4 double pixaspect = 1.0;
60 greg 2.23 int hres = 1024, vres = 1024, hmult = 4, vmult = 4;
61 greg 2.1 /* output file */
62     char *outfile = NULL;
63     int outfd;
64     long scanorig;
65 greg 2.5 int syncfd = -1; /* lock file descriptor */
66 greg 2.9 int nforked = 0;
67 greg 2.1
68     char *progname;
69     int verbose = 0;
70    
71     extern long lseek(), ftell();
72    
73 greg 2.6 int gotalrm = 0;
74     int onalrm() { gotalrm++; }
75 greg 2.1
76 greg 2.6
77 greg 2.1 main(argc, argv)
78     int argc;
79     char *argv[];
80     {
81     register int i, rval;
82    
83     progname = argv[0];
84     for (i = 1; i < argc; i++) {
85 greg 2.22 /* expand arguments */
86     while (rval = expandarg(&argc, &argv, i))
87     if (rval < 0) {
88     fprintf(stderr, "%s: cannot expand '%s'",
89     argv[0], argv[i]);
90     exit(1);
91     }
92 greg 2.1 if (argv[i][0] == '-')
93     switch (argv[i][1]) {
94     case 'v':
95     switch (argv[i][2]) {
96     case '\0': /* verbose option */
97     verbose = !verbose;
98     continue;
99     case 'f': /* view file */
100     if (viewfile(argv[++i], &ourview, NULL) <= 0) {
101     fprintf(stderr,
102     "%s: not a view file\n", argv[i]);
103     exit(1);
104     }
105     continue;
106     default: /* view option? */
107     rval = getviewopt(&ourview, argc-i, argv+i);
108     if (rval >= 0) {
109     i += rval;
110     continue;
111     }
112     break;
113     }
114     break;
115     case 'p': /* pixel aspect ratio? */
116 greg 2.23 if (argv[i][2] != 'a' || argv[i][3])
117     break;
118     pixaspect = atof(argv[i+1]);
119     continue;
120     case 'x': /* overall x resolution */
121     if (argv[i][2])
122     break;
123     hres = atoi(argv[++i]);
124     continue;
125     case 'y': /* overall y resolution */
126     if (argv[i][2])
127     break;
128     vres = atoi(argv[++i]);
129     continue;
130 greg 2.1 case 'X': /* horizontal multiplier */
131     if (argv[i][2])
132     break;
133     hmult = atoi(argv[++i]);
134     continue;
135     case 'Y': /* vertical multiplier */
136     if (argv[i][2])
137     break;
138     vmult = atoi(argv[++i]);
139     continue;
140 greg 2.5 case 'F': /* syncronization file */
141     if (argv[i][2])
142     break;
143 greg 2.14 if ((syncfd = open(argv[++i],
144     O_RDWR|O_CREAT, 0666)) < 0) {
145 greg 2.5 fprintf(stderr, "%s: cannot open\n",
146     argv[i]);
147     exit(1);
148     }
149     continue;
150 greg 2.15 case 'z': /* z-file ist verbotten */
151     fprintf(stderr, "%s: -z option not allowed\n",
152     argv[0]);
153     exit(1);
154 greg 2.1 case 'o': /* output file */
155     if (argv[i][2])
156     break;
157     outfile = argv[++i];
158     continue;
159 greg 2.23 } else if (i >= argc-1)
160     break;
161 greg 2.1 rpargv[rpargc++] = argv[i];
162     }
163 greg 2.23 if (i >= argc) {
164     fprintf(stderr, "%s: missing octree argument\n", argv[0]);
165     exit(1);
166     }
167 greg 2.1 if (outfile == NULL) {
168     fprintf(stderr, "%s: missing output file\n", argv[0]);
169     exit(1);
170     }
171     init(argc, argv);
172     rpiece();
173 greg 2.15 exit(cleanup(0));
174 greg 2.1 }
175    
176    
177     init(ac, av) /* set up output file and start rpict */
178     int ac;
179     char **av;
180     {
181 greg 2.23 static char hrbuf[16], vrbuf[16];
182 greg 2.2 extern char VersionID[];
183 greg 2.1 char *err;
184     FILE *fp;
185     int hr, vr;
186     /* set up view */
187     if ((err = setview(&ourview)) != NULL) {
188     fprintf(stderr, "%s: %s\n", progname, err);
189     exit(1);
190     }
191 greg 2.5 if (syncfd != -1) {
192     char buf[32];
193     buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
194     sscanf(buf, "%d %d", &hmult, &vmult);
195     }
196 greg 2.23 /* compute piece size */
197     hres /= hmult;
198     vres /= vmult;
199 greg 2.1 normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
200 greg 2.23 sprintf(hrbuf, "%d", hres);
201     rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf;
202     sprintf(vrbuf, "%d", vres);
203     rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf;
204     rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0";
205     rpargv[rpargc++] = av[ac-1];
206     rpargv[rpargc] = NULL;
207 greg 2.1 /* open output file */
208     if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
209     if ((fp = fdopen(dup(outfd), "w")) == NULL)
210     goto filerr;
211     printargs(ac, av, fp); /* write header */
212 greg 2.2 fprintf(fp, "SOFTWARE= %s\n", VersionID);
213 greg 2.1 fputs(VIEWSTR, fp);
214     fprintview(&ourview, fp);
215     putc('\n', fp);
216     if (pixaspect < .99 || pixaspect > 1.01)
217     fputaspect(pixaspect, fp);
218     fputformat(COLRFMT, fp);
219     putc('\n', fp);
220     fprtresolu(hres*hmult, vres*vmult, fp);
221     } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
222     if ((fp = fdopen(dup(outfd), "r+")) == NULL)
223     goto filerr;
224 greg 2.13 getheader(fp, NULL, NULL); /* skip header */
225 greg 2.15 if (!fscnresolu(&hr, &vr, fp) || /* check resolution */
226 greg 2.1 hr != hres*hmult || vr != vres*vmult) {
227 greg 2.8 fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
228     progname, outfile);
229 greg 2.1 exit(1);
230     }
231     } else {
232 greg 2.8 fprintf(stderr, "%s: cannot open file \"%s\"\n",
233     progname, outfile);
234 greg 2.1 exit(1);
235     }
236     scanorig = ftell(fp); /* record position of first scanline */
237     if (fclose(fp) == -1) /* done with stream i/o */
238     goto filerr;
239 greg 2.12 #if NFS
240 greg 2.8 sync(); /* flush NFS buffers */
241 greg 2.12 #endif
242 greg 2.1 /* start rpict process */
243     if (open_process(rpd, rpargv) <= 0) {
244 greg 2.3 fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
245 greg 2.1 exit(1);
246     }
247     if ((fromrp = fdopen(rpd[0], "r")) == NULL ||
248     (torp = fdopen(rpd[1], "w")) == NULL) {
249     fprintf(stderr, "%s: cannot open stream to %s\n",
250     progname, rpargv[0]);
251     exit(1);
252     }
253 greg 2.23 if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) {
254 greg 2.1 fprintf(stderr, "%s: out of memory\n", progname);
255     exit(1);
256     }
257 greg 2.6 signal(SIGALRM, onalrm);
258 greg 2.1 return;
259     filerr:
260 greg 2.8 fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
261 greg 2.1 exit(1);
262     }
263    
264    
265     int
266 greg 2.5 nextpiece(xp, yp) /* get next piece assignment */
267     int *xp, *yp;
268     {
269     struct flock fls;
270     char buf[64];
271    
272 greg 2.6 if (gotalrm) /* someone wants us to quit */
273     return(0);
274     if (syncfd != -1) { /* use sync file */
275 greg 2.5 fls.l_type = F_WRLCK; /* gain exclusive access */
276     fls.l_whence = 0;
277     fls.l_start = 0L;
278     fls.l_len = 0L;
279     fcntl(syncfd, F_SETLKW, &fls);
280     lseek(syncfd, 0L, 0);
281     buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
282     if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
283 greg 2.8 *xp = hmult-1;
284     *yp = vmult;
285     }
286     if (--(*yp) < 0) { /* decrement position */
287 greg 2.5 *yp = vmult-1;
288 greg 2.8 if (--(*xp) < 0) { /* all done! */
289 greg 2.5 close(syncfd);
290     return(0);
291     }
292     }
293 greg 2.17 sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp);
294 greg 2.5 lseek(syncfd, 0L, 0); /* write new position */
295     write(syncfd, buf, strlen(buf));
296     fls.l_type = F_UNLCK; /* release sync file */
297     fcntl(syncfd, F_SETLKW, &fls);
298     return(1);
299     }
300 greg 2.6 if (fgets(buf, sizeof(buf), stdin) == NULL) /* use stdin */
301 greg 2.5 return(0);
302     if (sscanf(buf, "%d %d", xp, yp) == 2)
303     return(1);
304     fprintf(stderr, "%s: input format error\n", progname);
305 greg 2.10 exit(cleanup(1));
306 greg 2.5 }
307    
308    
309     int
310 greg 2.10 cleanup(rstat) /* close rpict process and clean up */
311     int rstat;
312 greg 2.1 {
313 greg 2.10 int status;
314 greg 2.1
315 greg 2.23 bfree((char *)pbuf, hres*vres*sizeof(COLR));
316 greg 2.1 fclose(torp);
317     fclose(fromrp);
318 greg 2.10 while (wait(&status) != -1)
319 greg 2.9 if (rstat == 0)
320     rstat = status>>8 & 0xff;
321     return(rstat);
322 greg 2.1 }
323    
324    
325     rpiece() /* render picture piece by piece */
326     {
327     VIEW pview;
328     int xorg, yorg;
329 greg 2.11 /* compute view parameters */
330     copystruct(&pview, &ourview);
331     switch (ourview.type) {
332     case VT_PER:
333     pview.horiz = 2.*180./PI*atan(
334     tan(PI/180./2.*ourview.horiz)/hmult );
335     pview.vert = 2.*180./PI*atan(
336     tan(PI/180./2.*ourview.vert)/vmult );
337     break;
338     case VT_PAR:
339     case VT_ANG:
340     pview.horiz = ourview.horiz / hmult;
341     pview.vert = ourview.vert / vmult;
342     break;
343     case VT_HEM:
344     pview.horiz = 2.*180./PI*asin(
345     sin(PI/180./2.*ourview.horiz)/hmult );
346     pview.vert = 2.*180./PI*asin(
347     sin(PI/180./2.*ourview.vert)/vmult );
348     break;
349     default:
350     fprintf(stderr, "%s: unknown view type '-vt%c'\n",
351     progname, ourview.type);
352     exit(cleanup(1));
353     }
354     /* render each piece */
355 greg 2.5 while (nextpiece(&xorg, &yorg)) {
356 greg 2.11 pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
357     pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
358 greg 2.1 fputs(VIEWSTR, torp);
359     fprintview(&pview, torp);
360     putc('\n', torp);
361 greg 2.11 fflush(torp); /* assigns piece to rpict */
362 greg 2.1 putpiece(xorg, yorg); /* place piece in output */
363     }
364     }
365    
366    
367 greg 2.9 int
368 greg 2.1 putpiece(xpos, ypos) /* get next piece from rpict */
369     int xpos, ypos;
370     {
371 greg 2.7 struct flock fls;
372 greg 2.9 int pid, status;
373 greg 2.1 int hr, vr;
374 greg 2.8 register int y;
375     /* check bounds */
376 greg 2.5 if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
377     fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
378     progname, xpos, ypos);
379 greg 2.10 exit(cleanup(1));
380 greg 2.5 }
381 greg 2.8 /* check header from rpict */
382 greg 2.18 guard_io();
383 greg 2.13 getheader(fromrp, NULL, NULL);
384 greg 2.15 if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
385 greg 2.3 fprintf(stderr, "%s: resolution mismatch from %s\n",
386     progname, rpargv[0]);
387 greg 2.10 exit(cleanup(1));
388 greg 2.1 }
389 greg 2.24 if (verbose) { /* notify caller */
390     printf("%d %d begun\n", xpos, ypos);
391     fflush(stdout);
392     }
393 greg 2.18 unguard();
394 greg 2.8 /* load new piece into buffer */
395 greg 2.18 for (y = 0; y < vr; y++) {
396     guard_io();
397 greg 2.8 if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
398 greg 2.3 fprintf(stderr, "%s: read error from %s\n",
399     progname, rpargv[0]);
400 greg 2.10 exit(cleanup(1));
401 greg 2.1 }
402 greg 2.18 unguard();
403     }
404 greg 2.9 #if MAXFORK
405     /* fork so we don't slow rpict down */
406     if ((pid = fork()) > 0) {
407 greg 2.10 if (++nforked >= MAXFORK) {
408 greg 2.9 wait(&status); /* reap a child */
409     if (status)
410 greg 2.10 exit(cleanup(status>>8 & 0xff));
411 greg 2.9 nforked--;
412     }
413     return(pid);
414     }
415     #else
416     pid = -1; /* no forking */
417     #endif
418 greg 2.19 fls.l_start = scanorig +
419     ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
420 greg 2.12 #if NFS
421 greg 2.19 fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
422 greg 2.8 /* lock file section so NFS doesn't mess up */
423     fls.l_whence = 0;
424     fls.l_type = F_WRLCK;
425     fcntl(outfd, F_SETLKW, &fls);
426 greg 2.12 #endif
427 greg 2.8 /* write new piece to file */
428 greg 2.19 if (lseek(outfd, fls.l_start, 0) == -1)
429 greg 2.8 goto seekerr;
430 greg 2.9 if (hmult == 1) {
431     if (writebuf(outfd, (char *)pbuf,
432     vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
433     goto writerr;
434     } else
435     for (y = 0; y < vr; y++) {
436     if (writebuf(outfd, (char *)(pbuf+y*hr),
437     hr*sizeof(COLR)) != hr*sizeof(COLR))
438     goto writerr;
439     if (y < vr-1 && lseek(outfd,
440     (long)(hmult-1)*hr*sizeof(COLR),
441     1) == -1)
442     goto seekerr;
443 greg 2.1 }
444 greg 2.20 if (verbose) { /* notify caller */
445     printf("%d %d done\n", xpos, ypos);
446     fflush(stdout);
447     }
448     if (pid == -1) { /* didn't fork or fork failed */
449 greg 2.12 #if NFS
450 greg 2.9 fls.l_type = F_UNLCK; /* release lock */
451     fcntl(outfd, F_SETLKW, &fls);
452 greg 2.12 #endif
453 greg 2.9 return(0);
454 greg 2.1 }
455 greg 2.9 _exit(0); /* else exit child process (releasing lock) */
456 greg 2.8 seekerr:
457     fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
458 greg 2.9 _exit(1);
459     writerr:
460     fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
461     _exit(1);
462 greg 2.1 }
463 greg 2.16
464     #endif