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