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", }; |
20 |
< |
int rpargc = 7; |
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 = 0.0; |
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[]; |
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; |
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) { |
156 |
|
putc('\n', fp); |
157 |
|
fprtresolu(hres*hmult, vres*vmult, fp); |
158 |
|
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
140 |
– |
sleep(30); /* wait for header */ |
159 |
|
if ((fp = fdopen(dup(outfd), "r+")) == NULL) |
160 |
|
goto filerr; |
161 |
|
getheader(fp, NULL); /* skip header */ |
174 |
|
goto filerr; |
175 |
|
/* start rpict process */ |
176 |
|
if (open_process(rpd, rpargv) <= 0) { |
177 |
< |
fprintf(stderr, "%s: cannot start\n", 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 || |
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); |
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; |
257 |
|
|
258 |
|
rpiece() /* render picture piece by piece */ |
259 |
|
{ |
196 |
– |
extern char *gets(), *strcat(); |
260 |
|
VIEW pview; |
198 |
– |
char buf[48]; |
261 |
|
int xorg, yorg; |
262 |
|
|
263 |
< |
while (gets(buf) != NULL) { |
202 |
< |
if (sscanf(buf, "%d %d", &xorg, &yorg) != 2) { |
203 |
< |
fprintf(stderr, "%s: input format error\n", progname); |
204 |
< |
exit(1); |
205 |
< |
} |
263 |
> |
while (nextpiece(&xorg, &yorg)) { |
264 |
|
copystruct(&pview, &ourview); /* compute view for piece */ |
265 |
|
switch (ourview.type) { |
266 |
|
case VT_PER: |
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); |
293 |
|
fflush(torp); /* assign piece to rpict */ |
294 |
|
putpiece(xorg, yorg); /* place piece in output */ |
295 |
|
if (verbose) { /* notify caller */ |
296 |
< |
strcat(buf, " done\n"); |
297 |
< |
writebuf(fileno(stdout), buf, strlen(buf)); |
296 |
> |
printf("%d %d done\n", xorg, yorg); |
297 |
> |
fflush(stdout); |
298 |
|
} |
299 |
|
} |
300 |
|
} |
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 rpict\n", |
319 |
< |
progname); |
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 rpict\n", |
328 |
< |
progname); |
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, |
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 |
|
} |