ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.8
Committed: Sun Aug 9 12:12:48 1992 UTC (31 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +45 -34 lines
Log Message:
improved locking with use of local buffer for picture piece

File Contents

# Content
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 #include <signal.h>
14 #include "color.h"
15 #include "view.h"
16 #include "resolu.h"
17
18 /* rpict command */
19 char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"};
20 int rpargc = 9;
21 int rpd[3];
22 FILE *torp, *fromrp;
23 COLR *pbuf;
24 /* our view parameters */
25 VIEW ourview = STDVIEW;
26 double pixaspect = 1.0;
27 int hres = 512, vres = 512, hmult = 2, vmult = 2;
28 /* output file */
29 char *outfile = NULL;
30 int outfd;
31 long scanorig;
32 int syncfd = -1; /* lock file descriptor */
33
34 char *progname;
35 int verbose = 0;
36
37 extern long lseek(), ftell();
38
39 int gotalrm = 0;
40 int onalrm() { gotalrm++; }
41
42
43 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 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 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 extern char VersionID[];
130 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 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 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 fprintf(fp, "SOFTWARE= %s\n", VersionID);
150 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: resolution mismatch on file \"%s\"\n",
165 progname, outfile);
166 exit(1);
167 }
168 } else {
169 fprintf(stderr, "%s: cannot open file \"%s\"\n",
170 progname, outfile);
171 exit(1);
172 }
173 scanorig = ftell(fp); /* record position of first scanline */
174 if (fclose(fp) == -1) /* done with stream i/o */
175 goto filerr;
176 sync(); /* flush NFS buffers */
177 /* start rpict process */
178 if (open_process(rpd, rpargv) <= 0) {
179 fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
180 exit(1);
181 }
182 if ((fromrp = fdopen(rpd[0], "r")) == NULL ||
183 (torp = fdopen(rpd[1], "w")) == NULL) {
184 fprintf(stderr, "%s: cannot open stream to %s\n",
185 progname, rpargv[0]);
186 exit(1);
187 }
188 if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) {
189 fprintf(stderr, "%s: out of memory\n", progname);
190 exit(1);
191 }
192 signal(SIGALRM, onalrm);
193 return;
194 filerr:
195 fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
196 exit(1);
197 }
198
199
200 int
201 nextpiece(xp, yp) /* get next piece assignment */
202 int *xp, *yp;
203 {
204 extern char *fgets();
205 struct flock fls;
206 char buf[64];
207
208 if (gotalrm) /* someone wants us to quit */
209 return(0);
210 if (syncfd != -1) { /* use sync file */
211 fls.l_type = F_WRLCK; /* gain exclusive access */
212 fls.l_whence = 0;
213 fls.l_start = 0L;
214 fls.l_len = 0L;
215 fcntl(syncfd, F_SETLKW, &fls);
216 lseek(syncfd, 0L, 0);
217 buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
218 if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
219 *xp = hmult-1;
220 *yp = vmult;
221 }
222 if (--(*yp) < 0) { /* decrement position */
223 *yp = vmult-1;
224 if (--(*xp) < 0) { /* all done! */
225 close(syncfd);
226 return(0);
227 }
228 }
229 sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp);
230 lseek(syncfd, 0L, 0); /* write new position */
231 write(syncfd, buf, strlen(buf));
232 fls.l_type = F_UNLCK; /* release sync file */
233 fcntl(syncfd, F_SETLKW, &fls);
234 return(1);
235 }
236 if (fgets(buf, sizeof(buf), stdin) == NULL) /* use stdin */
237 return(0);
238 if (sscanf(buf, "%d %d", xp, yp) == 2)
239 return(1);
240 fprintf(stderr, "%s: input format error\n", progname);
241 exit(1);
242 }
243
244
245 int
246 cleanup() /* close rpict process and clean up */
247 {
248 register int rpstat;
249
250 free((char *)pbuf);
251 fclose(torp);
252 fclose(fromrp);
253 rpstat = close_process(rpd);
254 if (rpstat == -1)
255 rpstat = 0;
256 return(rpstat);
257 }
258
259
260 rpiece() /* render picture piece by piece */
261 {
262 VIEW pview;
263 int xorg, yorg;
264
265 while (nextpiece(&xorg, &yorg)) {
266 copystruct(&pview, &ourview); /* compute view for piece */
267 switch (ourview.type) {
268 case VT_PER:
269 pview.horiz = 2.*180./PI*atan(
270 tan(PI/180./2.*ourview.horiz)/hmult );
271 pview.vert = 2.*180./PI*atan(
272 tan(PI/180./2.*ourview.vert)/vmult );
273 break;
274 case VT_PAR:
275 case VT_ANG:
276 pview.horiz = ourview.horiz / hmult;
277 pview.vert = ourview.vert / vmult;
278 break;
279 case VT_HEM:
280 pview.horiz = 2.*180./PI*asin(
281 sin(PI/180./2.*ourview.horiz)/hmult );
282 pview.vert = 2.*180./PI*asin(
283 sin(PI/180./2.*ourview.vert)/vmult );
284 break;
285 default:
286 fprintf(stderr, "%s: unknown view type '-vt%c'\n",
287 progname, ourview.type);
288 exit(1);
289 }
290 pview.hoff += xorg - 0.5*(hmult-1);
291 pview.voff += yorg - 0.5*(vmult-1);
292 fputs(VIEWSTR, torp);
293 fprintview(&pview, torp);
294 putc('\n', torp);
295 fflush(torp); /* assign piece to rpict */
296 putpiece(xorg, yorg); /* place piece in output */
297 if (verbose) { /* notify caller */
298 printf("%d %d done\n", xorg, yorg);
299 fflush(stdout);
300 }
301 }
302 }
303
304
305 putpiece(xpos, ypos) /* get next piece from rpict */
306 int xpos, ypos;
307 {
308 struct flock fls;
309 int hr, vr;
310 register int y;
311 /* check bounds */
312 if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
313 fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
314 progname, xpos, ypos);
315 exit(1);
316 }
317 /* check header from rpict */
318 getheader(fromrp, NULL);
319 if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres || vr != vres) {
320 fprintf(stderr, "%s: resolution mismatch from %s\n",
321 progname, rpargv[0]);
322 exit(1);
323 }
324 /* load new piece into buffer */
325 for (y = 0; y < vr; y++)
326 if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
327 fprintf(stderr, "%s: read error from %s\n",
328 progname, rpargv[0]);
329 exit(1);
330 }
331 /* lock file section so NFS doesn't mess up */
332 fls.l_whence = 0;
333 fls.l_len = (long)vres*hmult*hres*sizeof(COLR);
334 fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len;
335 fls.l_type = F_WRLCK;
336 fcntl(outfd, F_SETLKW, &fls);
337 /* write new piece to file */
338 if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1)
339 goto seekerr;
340 for (y = 0; y < vr; y++) {
341 if (writebuf(outfd, (char *)(pbuf+y*hr), hr*sizeof(COLR)) !=
342 hr*sizeof(COLR)) {
343 fprintf(stderr, "%s: write error on file \"%s\"\n",
344 progname, outfile);
345 exit(1);
346 }
347 if (hmult != 1 && y < vr-1 && lseek(outfd,
348 (long)(hmult-1)*hr*sizeof(COLR), 1) == -1)
349 goto seekerr;
350 }
351 /* unlock file section */
352 fls.l_type = F_UNLCK;
353 fcntl(outfd, F_SETLKW, &fls);
354 return;
355 seekerr:
356 fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
357 exit(1);
358 }