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