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