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