ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.5
Committed: Fri Aug 7 11:05:30 1992 UTC (31 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +67 -10 lines
Log Message:
added -F option for sync file

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