ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.11
Committed: Sun Aug 9 23:05:17 1992 UTC (32 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +30 -29 lines
Log Message:
slight optimization in rpiece()

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