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