ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.6
Committed: Fri Aug 7 11:34:27 1992 UTC (31 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +9 -2 lines
Log Message:
added SIGALRM handling for graceful bowing out

File Contents

# User Rev Content
1 greg 2.1 /* Copyright (c) 1992 Regents of the University of California */
2    
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.6 #include <signal.h>
14 greg 2.1 #include "color.h"
15     #include "view.h"
16     #include "resolu.h"
17    
18     /* rpict command */
19 greg 2.4 char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"};
20     int rpargc = 9;
21 greg 2.1 int rpd[3];
22     FILE *torp, *fromrp;
23     COLR *scanline;
24     /* our view parameters */
25     VIEW ourview = STDVIEW;
26 greg 2.4 double pixaspect = 1.0;
27 greg 2.1 int hres = 512, vres = 512, hmult = 2, vmult = 2;
28     /* output file */
29     char *outfile = NULL;
30     int outfd;
31     long scanorig;
32 greg 2.5 int syncfd = -1; /* lock file descriptor */
33 greg 2.1
34     char *progname;
35     int verbose = 0;
36    
37     extern long lseek(), ftell();
38    
39 greg 2.6 int gotalrm = 0;
40     int onalrm() { gotalrm++; }
41 greg 2.1
42 greg 2.6
43 greg 2.1 main(argc, argv)
44     int argc;
45     char *argv[];
46     {
47     register int i, rval;
48    
49     progname = argv[0];
50     for (i = 1; i < argc; i++) {
51     if (argv[i][0] == '-')
52     switch (argv[i][1]) {
53     case 'v':
54     switch (argv[i][2]) {
55     case '\0': /* verbose option */
56     verbose = !verbose;
57     continue;
58     case 'f': /* view file */
59     if (viewfile(argv[++i], &ourview, NULL) <= 0) {
60     fprintf(stderr,
61     "%s: not a view file\n", argv[i]);
62     exit(1);
63     }
64     continue;
65     default: /* view option? */
66     rval = getviewopt(&ourview, argc-i, argv+i);
67     if (rval >= 0) {
68     i += rval;
69     continue;
70     }
71     break;
72     }
73     break;
74     case 'p': /* pixel aspect ratio? */
75     if (argv[i][2] == 'a' && !argv[i][3])
76     pixaspect = atof(argv[i+1]);
77     break;
78     case 'x': /* piece x resolution */
79     if (!argv[i][2])
80     hres = atoi(argv[i+1]);
81     break;
82     case 'y': /* piece y resolution */
83     if (!argv[i][2])
84     vres = atoi(argv[i+1]);
85     break;
86     case 'X': /* horizontal multiplier */
87     if (argv[i][2])
88     break;
89     hmult = atoi(argv[++i]);
90     continue;
91     case 'Y': /* vertical multiplier */
92     if (argv[i][2])
93     break;
94     vmult = atoi(argv[++i]);
95     continue;
96 greg 2.5 case 'F': /* syncronization file */
97     if (argv[i][2])
98     break;
99     if ((syncfd = open(argv[++i], O_RDWR)) < 0) {
100     fprintf(stderr, "%s: cannot open\n",
101     argv[i]);
102     exit(1);
103     }
104     continue;
105 greg 2.1 case 'o': /* output file */
106     if (argv[i][2])
107     break;
108     outfile = argv[++i];
109     continue;
110     }
111     rpargv[rpargc++] = argv[i];
112     }
113     rpargv[rpargc] = NULL;
114     if (outfile == NULL) {
115     fprintf(stderr, "%s: missing output file\n", argv[0]);
116     exit(1);
117     }
118     init(argc, argv);
119     rpiece();
120     rval = cleanup();
121     exit(rval);
122     }
123    
124    
125     init(ac, av) /* set up output file and start rpict */
126     int ac;
127     char **av;
128     {
129 greg 2.2 extern char VersionID[];
130 greg 2.1 char *err;
131     FILE *fp;
132     int hr, vr;
133     /* set up view */
134     if ((err = setview(&ourview)) != NULL) {
135     fprintf(stderr, "%s: %s\n", progname, err);
136     exit(1);
137     }
138 greg 2.5 if (syncfd != -1) {
139     char buf[32];
140     buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
141     sscanf(buf, "%d %d", &hmult, &vmult);
142     }
143 greg 2.1 normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
144     /* open output file */
145     if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
146     if ((fp = fdopen(dup(outfd), "w")) == NULL)
147     goto filerr;
148     printargs(ac, av, fp); /* write header */
149 greg 2.2 fprintf(fp, "SOFTWARE= %s\n", VersionID);
150 greg 2.1 fputs(VIEWSTR, fp);
151     fprintview(&ourview, fp);
152     putc('\n', fp);
153     if (pixaspect < .99 || pixaspect > 1.01)
154     fputaspect(pixaspect, fp);
155     fputformat(COLRFMT, fp);
156     putc('\n', fp);
157     fprtresolu(hres*hmult, vres*vmult, fp);
158     } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
159     if ((fp = fdopen(dup(outfd), "r+")) == NULL)
160     goto filerr;
161     getheader(fp, NULL); /* skip header */
162     if (fscnresolu(&hr, &vr, fp) < 0 || /* check resolution */
163     hr != hres*hmult || vr != vres*vmult) {
164     fprintf(stderr, "%s: picture resolution mismatch\n",
165     outfile);
166     exit(1);
167     }
168     } else {
169     fprintf(stderr, "%s: cannot open\n", outfile);
170     exit(1);
171     }
172     scanorig = ftell(fp); /* record position of first scanline */
173     if (fclose(fp) == -1) /* done with stream i/o */
174     goto filerr;
175 greg 2.5 sync(); /* avoid NFS buffering */
176 greg 2.1 /* start rpict process */
177     if (open_process(rpd, rpargv) <= 0) {
178 greg 2.3 fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
179 greg 2.1 exit(1);
180     }
181     if ((fromrp = fdopen(rpd[0], "r")) == NULL ||
182     (torp = fdopen(rpd[1], "w")) == NULL) {
183     fprintf(stderr, "%s: cannot open stream to %s\n",
184     progname, rpargv[0]);
185     exit(1);
186     }
187     if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) {
188     fprintf(stderr, "%s: out of memory\n", progname);
189     exit(1);
190     }
191 greg 2.6 signal(SIGALRM, onalrm);
192 greg 2.1 return;
193     filerr:
194     fprintf(stderr, "%s: file i/o error\n", outfile);
195     exit(1);
196     }
197    
198    
199     int
200 greg 2.5 nextpiece(xp, yp) /* get next piece assignment */
201     int *xp, *yp;
202     {
203     extern char *fgets();
204     struct flock fls;
205     char buf[64];
206    
207 greg 2.6 if (gotalrm) /* someone wants us to quit */
208     return(0);
209     if (syncfd != -1) { /* use sync file */
210 greg 2.5 fls.l_type = F_WRLCK; /* gain exclusive access */
211     fls.l_whence = 0;
212     fls.l_start = 0L;
213     fls.l_len = 0L;
214     fcntl(syncfd, F_SETLKW, &fls);
215     lseek(syncfd, 0L, 0);
216     buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
217     if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
218     *xp = hmult;
219     *yp = vmult-1;
220     }
221     if (--(*xp) < 0) { /* decrement position */
222     *xp = hmult-1;
223     if (--(*yp) < 0) { /* all done! */
224     close(syncfd);
225     return(0);
226     }
227     }
228     sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp);
229     lseek(syncfd, 0L, 0); /* write new position */
230     write(syncfd, buf, strlen(buf));
231     fls.l_type = F_UNLCK; /* release sync file */
232     fcntl(syncfd, F_SETLKW, &fls);
233     return(1);
234     }
235 greg 2.6 if (fgets(buf, sizeof(buf), stdin) == NULL) /* use stdin */
236 greg 2.5 return(0);
237     if (sscanf(buf, "%d %d", xp, yp) == 2)
238     return(1);
239     fprintf(stderr, "%s: input format error\n", progname);
240     exit(1);
241     }
242    
243    
244     int
245 greg 2.1 cleanup() /* close rpict process and clean up */
246     {
247     register int rpstat;
248    
249     free((char *)scanline);
250     fclose(torp);
251     fclose(fromrp);
252     rpstat = close_process(rpd);
253     if (rpstat == -1)
254     rpstat = 0;
255     return(rpstat);
256     }
257    
258    
259     rpiece() /* render picture piece by piece */
260     {
261     VIEW pview;
262     int xorg, yorg;
263    
264 greg 2.5 while (nextpiece(&xorg, &yorg)) {
265 greg 2.1 copystruct(&pview, &ourview); /* compute view for piece */
266     switch (ourview.type) {
267     case VT_PER:
268     pview.horiz = 2.*180./PI*atan(
269     tan(PI/180./2.*ourview.horiz)/hmult );
270     pview.vert = 2.*180./PI*atan(
271     tan(PI/180./2.*ourview.vert)/vmult );
272     break;
273     case VT_PAR:
274     case VT_ANG:
275     pview.horiz = ourview.horiz / hmult;
276     pview.vert = ourview.vert / vmult;
277     break;
278     case VT_HEM:
279 greg 2.3 pview.horiz = 2.*180./PI*asin(
280     sin(PI/180./2.*ourview.horiz)/hmult );
281     pview.vert = 2.*180./PI*asin(
282     sin(PI/180./2.*ourview.vert)/vmult );
283     break;
284 greg 2.1 default:
285     fprintf(stderr, "%s: unknown view type '-vt%c'\n",
286     progname, ourview.type);
287     exit(1);
288     }
289     pview.hoff += xorg - 0.5*(hmult-1);
290     pview.voff += yorg - 0.5*(vmult-1);
291     fputs(VIEWSTR, torp);
292     fprintview(&pview, torp);
293     putc('\n', torp);
294     fflush(torp); /* assign piece to rpict */
295     putpiece(xorg, yorg); /* place piece in output */
296     if (verbose) { /* notify caller */
297 greg 2.5 printf("%d %d done\n", xorg, yorg);
298     fflush(stdout);
299 greg 2.1 }
300     }
301     }
302    
303    
304     putpiece(xpos, ypos) /* get next piece from rpict */
305     int xpos, ypos;
306     {
307     int hr, vr;
308     int y;
309    
310 greg 2.5 if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
311     fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
312     progname, xpos, ypos);
313     exit(1);
314     }
315 greg 2.1 getheader(fromrp, NULL); /* discard header info. */
316     if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */
317     hr != hres || vr != vres) {
318 greg 2.3 fprintf(stderr, "%s: resolution mismatch from %s\n",
319     progname, rpargv[0]);
320 greg 2.1 exit(1);
321     }
322     for (y = 0; y < vr; y++) { /* transfer scanlines */
323     if (freadcolrs(scanline, hr, fromrp) < 0) {
324 greg 2.3 fprintf(stderr, "%s: read error from %s\n",
325     progname, rpargv[0]);
326 greg 2.1 exit(1);
327     }
328     if ((y == 0 || hmult != 1) && lseek(outfd,
329     scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR),
330     0) == -1) {
331     fprintf(stderr, "%s: seek error\n", outfile);
332     exit(1);
333     }
334     if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) !=
335     hr*sizeof(COLR)) {
336     fprintf(stderr, "%s: write error\n", outfile);
337     exit(1);
338     }
339     }
340     }