1 |
– |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Generate sections of a picture. |
6 |
|
*/ |
7 |
|
|
8 |
|
#include "standard.h" |
9 |
< |
#include <fcntl.h> |
9 |
> |
|
10 |
> |
#ifndef F_SETLKW |
11 |
> |
|
12 |
> |
main(argc, argv) |
13 |
> |
int argc; |
14 |
> |
char *argv[]; |
15 |
> |
{ |
16 |
> |
fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]); |
17 |
> |
exit(1); |
18 |
> |
} |
19 |
> |
|
20 |
> |
#else |
21 |
> |
|
22 |
> |
#include <signal.h> |
23 |
> |
|
24 |
|
#include "color.h" |
25 |
|
#include "view.h" |
26 |
< |
#include "resolu.h" |
26 |
> |
#include "rtprocess.h" |
27 |
|
|
28 |
+ |
#ifndef NFS |
29 |
+ |
#define NFS 1 |
30 |
+ |
#endif |
31 |
+ |
/* set the following to 0 to forgo forking */ |
32 |
+ |
#ifndef MAXFORK |
33 |
+ |
#if NFS |
34 |
+ |
#define MAXFORK 3 /* allotment of duped processes */ |
35 |
+ |
#else |
36 |
+ |
#define MAXFORK 0 |
37 |
+ |
#endif |
38 |
+ |
#endif |
39 |
+ |
/* protection from SYSV signals(!) */ |
40 |
+ |
#if defined(sgi) |
41 |
+ |
#define guard_io() sighold(SIGALRM) |
42 |
+ |
#define unguard() sigrelse(SIGALRM) |
43 |
+ |
#endif |
44 |
+ |
#ifndef guard_io |
45 |
+ |
#define guard_io() 0 |
46 |
+ |
#define unguard() 0 |
47 |
+ |
#endif |
48 |
+ |
|
49 |
+ |
extern char *strerror(); |
50 |
+ |
|
51 |
|
/* rpict command */ |
52 |
< |
char *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"}; |
53 |
< |
int rpargc = 9; |
20 |
< |
int rpd[3]; |
52 |
> |
char *rpargv[128] = {"rpict", "-S", "1"}; |
53 |
> |
int rpargc = 3; |
54 |
|
FILE *torp, *fromrp; |
55 |
< |
COLR *scanline; |
55 |
> |
COLR *pbuf; |
56 |
|
/* our view parameters */ |
57 |
|
VIEW ourview = STDVIEW; |
58 |
|
double pixaspect = 1.0; |
59 |
< |
int hres = 512, vres = 512, hmult = 2, vmult = 2; |
59 |
> |
int hres = 1024, vres = 1024, hmult = 4, vmult = 4; |
60 |
|
/* output file */ |
61 |
|
char *outfile = NULL; |
62 |
|
int outfd; |
63 |
|
long scanorig; |
64 |
< |
int syncfd = -1; /* lock file descriptor */ |
64 |
> |
FILE *syncfp = NULL; /* synchronization file pointer */ |
65 |
> |
int synclst = F_UNLCK; /* synchronization file lock status */ |
66 |
> |
int nforked = 0; |
67 |
|
|
68 |
+ |
#define sflock(t) if ((t)!=synclst) dolock(fileno(syncfp),synclst=t) |
69 |
+ |
|
70 |
|
char *progname; |
71 |
|
int verbose = 0; |
72 |
+ |
unsigned timelim = 0; |
73 |
+ |
int rvrlim = -1; |
74 |
|
|
75 |
< |
extern long lseek(), ftell(); |
75 |
> |
int gotalrm = 0; |
76 |
> |
void onalrm(int i) { gotalrm++; } |
77 |
|
|
78 |
|
|
79 |
|
main(argc, argv) |
84 |
|
|
85 |
|
progname = argv[0]; |
86 |
|
for (i = 1; i < argc; i++) { |
87 |
+ |
/* expand arguments */ |
88 |
+ |
while ((rval = expandarg(&argc, &argv, i)) > 0) |
89 |
+ |
; |
90 |
+ |
if (rval < 0) { |
91 |
+ |
fprintf(stderr, "%s: cannot expand '%s'", |
92 |
+ |
argv[0], argv[i]); |
93 |
+ |
exit(1); |
94 |
+ |
} |
95 |
|
if (argv[i][0] == '-') |
96 |
|
switch (argv[i][1]) { |
97 |
|
case 'v': |
116 |
|
} |
117 |
|
break; |
118 |
|
case 'p': /* pixel aspect ratio? */ |
119 |
< |
if (argv[i][2] == 'a' && !argv[i][3]) |
120 |
< |
pixaspect = atof(argv[i+1]); |
119 |
> |
if (argv[i][2] != 'a' || argv[i][3]) |
120 |
> |
break; |
121 |
> |
pixaspect = atof(argv[++i]); |
122 |
> |
continue; |
123 |
> |
case 'T': /* time limit (hours) */ |
124 |
> |
if (argv[i][2]) |
125 |
> |
break; |
126 |
> |
timelim = atof(argv[++i])*3600. + .5; |
127 |
|
break; |
128 |
< |
case 'x': /* piece x resolution */ |
129 |
< |
if (!argv[i][2]) |
130 |
< |
hres = atoi(argv[i+1]); |
131 |
< |
break; |
132 |
< |
case 'y': /* piece y resolution */ |
133 |
< |
if (!argv[i][2]) |
134 |
< |
vres = atoi(argv[i+1]); |
135 |
< |
break; |
128 |
> |
case 'x': /* overall x resolution */ |
129 |
> |
if (argv[i][2]) |
130 |
> |
break; |
131 |
> |
hres = atoi(argv[++i]); |
132 |
> |
continue; |
133 |
> |
case 'y': /* overall y resolution */ |
134 |
> |
if (argv[i][2]) |
135 |
> |
break; |
136 |
> |
vres = atoi(argv[++i]); |
137 |
> |
continue; |
138 |
|
case 'X': /* horizontal multiplier */ |
139 |
|
if (argv[i][2]) |
140 |
|
break; |
145 |
|
break; |
146 |
|
vmult = atoi(argv[++i]); |
147 |
|
continue; |
148 |
+ |
case 'R': /* recover */ |
149 |
+ |
if (argv[i][2]) |
150 |
+ |
break; |
151 |
+ |
rvrlim = 0; |
152 |
+ |
/* fall through */ |
153 |
|
case 'F': /* syncronization file */ |
154 |
|
if (argv[i][2]) |
155 |
|
break; |
156 |
< |
if ((syncfd = open(argv[++i], O_RDWR)) < 0) { |
156 |
> |
if ((syncfp = |
157 |
> |
fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) { |
158 |
|
fprintf(stderr, "%s: cannot open\n", |
159 |
|
argv[i]); |
160 |
|
exit(1); |
161 |
|
} |
162 |
|
continue; |
163 |
+ |
case 'z': /* z-file ist verbotten */ |
164 |
+ |
fprintf(stderr, "%s: -z option not allowed\n", |
165 |
+ |
argv[0]); |
166 |
+ |
exit(1); |
167 |
|
case 'o': /* output file */ |
168 |
|
if (argv[i][2]) |
169 |
|
break; |
170 |
|
outfile = argv[++i]; |
171 |
|
continue; |
172 |
< |
} |
172 |
> |
} else if (i >= argc-1) |
173 |
> |
break; |
174 |
|
rpargv[rpargc++] = argv[i]; |
175 |
|
} |
176 |
< |
rpargv[rpargc] = NULL; |
176 |
> |
if (i >= argc) { |
177 |
> |
fprintf(stderr, "%s: missing octree argument\n", argv[0]); |
178 |
> |
exit(1); |
179 |
> |
} |
180 |
|
if (outfile == NULL) { |
181 |
|
fprintf(stderr, "%s: missing output file\n", argv[0]); |
182 |
|
exit(1); |
183 |
|
} |
184 |
|
init(argc, argv); |
185 |
|
rpiece(); |
186 |
< |
rval = cleanup(); |
117 |
< |
exit(rval); |
186 |
> |
exit(cleanup(0)); |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
+ |
dolock(fd, ltyp) /* lock or unlock a file */ |
191 |
+ |
int fd; |
192 |
+ |
int ltyp; |
193 |
+ |
{ |
194 |
+ |
static struct flock fls; /* static so initialized to zeroes */ |
195 |
+ |
|
196 |
+ |
fls.l_type = ltyp; |
197 |
+ |
if (fcntl(fd, F_SETLKW, &fls) < 0) { |
198 |
+ |
fprintf(stderr, "%s: cannot lock/unlock file: %s\n", |
199 |
+ |
progname, strerror(errno)); |
200 |
+ |
exit(1); |
201 |
+ |
} |
202 |
+ |
} |
203 |
+ |
|
204 |
+ |
|
205 |
|
init(ac, av) /* set up output file and start rpict */ |
206 |
|
int ac; |
207 |
|
char **av; |
208 |
|
{ |
209 |
+ |
static char hrbuf[16], vrbuf[16]; |
210 |
|
extern char VersionID[]; |
211 |
|
char *err; |
212 |
|
FILE *fp; |
213 |
|
int hr, vr; |
214 |
+ |
SUBPROC rpd; /* since we don't close_process(), this can be local */ |
215 |
|
/* set up view */ |
216 |
|
if ((err = setview(&ourview)) != NULL) { |
217 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
218 |
|
exit(1); |
219 |
|
} |
220 |
< |
if (syncfd != -1) { |
221 |
< |
char buf[32]; |
222 |
< |
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
223 |
< |
sscanf(buf, "%d %d", &hmult, &vmult); |
220 |
> |
if (syncfp != NULL) { |
221 |
> |
sflock(F_RDLCK); |
222 |
> |
fscanf(syncfp, "%d %d", &hmult, &vmult); |
223 |
> |
sflock(F_UNLCK); |
224 |
|
} |
225 |
+ |
/* compute piece size */ |
226 |
+ |
hres /= hmult; |
227 |
+ |
vres /= vmult; |
228 |
|
normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres); |
229 |
+ |
sprintf(hrbuf, "%d", hres); |
230 |
+ |
rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf; |
231 |
+ |
sprintf(vrbuf, "%d", vres); |
232 |
+ |
rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf; |
233 |
+ |
rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0"; |
234 |
+ |
rpargv[rpargc++] = av[ac-1]; |
235 |
+ |
rpargv[rpargc] = NULL; |
236 |
|
/* open output file */ |
237 |
|
if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) { |
238 |
+ |
dolock(outfd, F_WRLCK); |
239 |
|
if ((fp = fdopen(dup(outfd), "w")) == NULL) |
240 |
|
goto filerr; |
241 |
< |
printargs(ac, av, fp); /* write header */ |
241 |
> |
newheader("RADIANCE", fp); /* create header */ |
242 |
> |
printargs(ac, av, fp); |
243 |
|
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
244 |
|
fputs(VIEWSTR, fp); |
245 |
|
fprintview(&ourview, fp); |
250 |
|
putc('\n', fp); |
251 |
|
fprtresolu(hres*hmult, vres*vmult, fp); |
252 |
|
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
253 |
+ |
dolock(outfd, F_RDLCK); |
254 |
|
if ((fp = fdopen(dup(outfd), "r+")) == NULL) |
255 |
|
goto filerr; |
256 |
< |
getheader(fp, NULL); /* skip header */ |
257 |
< |
if (fscnresolu(&hr, &vr, fp) < 0 || /* check resolution */ |
256 |
> |
getheader(fp, NULL, NULL); /* skip header */ |
257 |
> |
if (!fscnresolu(&hr, &vr, fp) || /* check resolution */ |
258 |
|
hr != hres*hmult || vr != vres*vmult) { |
259 |
< |
fprintf(stderr, "%s: picture resolution mismatch\n", |
260 |
< |
outfile); |
259 |
> |
fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n", |
260 |
> |
progname, outfile); |
261 |
|
exit(1); |
262 |
|
} |
263 |
|
} else { |
264 |
< |
fprintf(stderr, "%s: cannot open\n", outfile); |
264 |
> |
fprintf(stderr, "%s: cannot open file \"%s\"\n", |
265 |
> |
progname, outfile); |
266 |
|
exit(1); |
267 |
|
} |
268 |
|
scanorig = ftell(fp); /* record position of first scanline */ |
269 |
|
if (fclose(fp) == -1) /* done with stream i/o */ |
270 |
|
goto filerr; |
271 |
< |
sync(); /* avoid NFS buffering */ |
271 |
> |
dolock(outfd, F_UNLCK); |
272 |
|
/* start rpict process */ |
273 |
< |
if (open_process(rpd, rpargv) <= 0) { |
273 |
> |
if (open_process(&rpd, rpargv) <= 0) { |
274 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
275 |
|
exit(1); |
276 |
|
} |
277 |
< |
if ((fromrp = fdopen(rpd[0], "r")) == NULL || |
278 |
< |
(torp = fdopen(rpd[1], "w")) == NULL) { |
277 |
> |
if ((fromrp = fdopen(rpd.r, "r")) == NULL || |
278 |
> |
(torp = fdopen(rpd.w, "w")) == NULL) { |
279 |
|
fprintf(stderr, "%s: cannot open stream to %s\n", |
280 |
|
progname, rpargv[0]); |
281 |
|
exit(1); |
282 |
|
} |
283 |
< |
if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) { |
283 |
> |
if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) { |
284 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
285 |
|
exit(1); |
286 |
|
} |
287 |
+ |
signal(SIGALRM, onalrm); |
288 |
+ |
if (timelim) |
289 |
+ |
alarm(timelim); |
290 |
|
return; |
291 |
|
filerr: |
292 |
< |
fprintf(stderr, "%s: file i/o error\n", outfile); |
292 |
> |
fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile); |
293 |
|
exit(1); |
294 |
|
} |
295 |
|
|
298 |
|
nextpiece(xp, yp) /* get next piece assignment */ |
299 |
|
int *xp, *yp; |
300 |
|
{ |
301 |
< |
extern char *fgets(); |
302 |
< |
struct flock fls; |
303 |
< |
char buf[64]; |
304 |
< |
|
305 |
< |
if (syncfd != -1) { /* using sync file */ |
306 |
< |
fls.l_type = F_WRLCK; /* gain exclusive access */ |
307 |
< |
fls.l_whence = 0; |
308 |
< |
fls.l_start = 0L; |
309 |
< |
fls.l_len = 0L; |
310 |
< |
fcntl(syncfd, F_SETLKW, &fls); |
311 |
< |
lseek(syncfd, 0L, 0); |
312 |
< |
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
210 |
< |
if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) { |
211 |
< |
*xp = hmult; |
212 |
< |
*yp = vmult-1; |
213 |
< |
} |
214 |
< |
if (--(*xp) < 0) { /* decrement position */ |
301 |
> |
if (gotalrm) /* someone wants us to quit */ |
302 |
> |
return(0); |
303 |
> |
if (syncfp != NULL) { /* use sync file */ |
304 |
> |
/* |
305 |
> |
* So we don't necessarily have to lock and unlock the file |
306 |
> |
* multiple times (very slow), we establish an exclusive |
307 |
> |
* lock at the beginning on our synchronization file and |
308 |
> |
* maintain it in the subroutine rvrpiece(). |
309 |
> |
*/ |
310 |
> |
sflock(F_WRLCK); |
311 |
> |
fseek(syncfp, 0L, 0); /* read position */ |
312 |
> |
if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) { |
313 |
|
*xp = hmult-1; |
314 |
< |
if (--(*yp) < 0) { /* all done! */ |
315 |
< |
close(syncfd); |
314 |
> |
*yp = vmult; |
315 |
> |
} |
316 |
> |
if (rvrlim == 0) /* initialize recovery limit */ |
317 |
> |
rvrlim = *xp*vmult + *yp; |
318 |
> |
if (rvrpiece(xp, yp)) { /* do stragglers first */ |
319 |
> |
sflock(F_UNLCK); |
320 |
> |
return(1); |
321 |
> |
} |
322 |
> |
if (--(*yp) < 0) { /* decrement position */ |
323 |
> |
*yp = vmult-1; |
324 |
> |
if (--(*xp) < 0) { /* all done */ |
325 |
> |
sflock(F_UNLCK); |
326 |
|
return(0); |
327 |
|
} |
328 |
|
} |
329 |
< |
sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp); |
330 |
< |
lseek(syncfd, 0L, 0); /* write new position */ |
331 |
< |
write(syncfd, buf, strlen(buf)); |
332 |
< |
fls.l_type = F_UNLCK; /* release sync file */ |
225 |
< |
fcntl(syncfd, F_SETLKW, &fls); |
329 |
> |
fseek(syncfp, 0L, 0); /* write new position */ |
330 |
> |
fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp); |
331 |
> |
fflush(syncfp); |
332 |
> |
sflock(F_UNLCK); /* release sync file */ |
333 |
|
return(1); |
334 |
|
} |
335 |
< |
if (fgets(buf, sizeof(buf), stdin) == NULL) /* using stdin */ |
229 |
< |
return(0); |
230 |
< |
if (sscanf(buf, "%d %d", xp, yp) == 2) |
231 |
< |
return(1); |
232 |
< |
fprintf(stderr, "%s: input format error\n", progname); |
233 |
< |
exit(1); |
335 |
> |
return(scanf("%d %d", xp, yp) == 2); /* use stdin */ |
336 |
|
} |
337 |
|
|
338 |
|
|
339 |
|
int |
340 |
< |
cleanup() /* close rpict process and clean up */ |
340 |
> |
rvrpiece(xp, yp) /* check for recoverable pieces */ |
341 |
> |
register int *xp, *yp; |
342 |
|
{ |
343 |
< |
register int rpstat; |
343 |
> |
static char *pdone = NULL; /* which pieces are done */ |
344 |
> |
static long readpos = -1; /* how far we've read */ |
345 |
> |
register int i; |
346 |
> |
/* |
347 |
> |
* This routine is called by nextpiece() with an |
348 |
> |
* exclusive lock on syncfp and the file pointer at the |
349 |
> |
* appropriate position to read in the finished pieces. |
350 |
> |
*/ |
351 |
> |
if (rvrlim < 0) |
352 |
> |
return(0); /* only check if asked */ |
353 |
> |
if (pdone == NULL) /* first call */ |
354 |
> |
pdone = calloc(hmult*vmult, sizeof(char)); |
355 |
> |
if (pdone == NULL) { |
356 |
> |
fprintf(stderr, "%s: out of memory\n", progname); |
357 |
> |
exit(1); |
358 |
> |
} |
359 |
> |
if (readpos != -1) /* mark what's been done */ |
360 |
> |
fseek(syncfp, readpos, 0); |
361 |
> |
while (fscanf(syncfp, "%d %d", xp, yp) == 2) |
362 |
> |
pdone[*xp*vmult+*yp] = 1; |
363 |
> |
if (!feof(syncfp)) { |
364 |
> |
fprintf(stderr, "%s: format error in sync file\n", progname); |
365 |
> |
exit(1); |
366 |
> |
} |
367 |
> |
readpos = ftell(syncfp); |
368 |
> |
i = hmult*vmult; /* find an unaccounted for piece */ |
369 |
> |
while (i-- > rvrlim) |
370 |
> |
if (!pdone[i]) { |
371 |
> |
*xp = i / vmult; |
372 |
> |
*yp = i % vmult; |
373 |
> |
pdone[i] = 1; /* consider it done */ |
374 |
> |
return(1); |
375 |
> |
} |
376 |
> |
rvrlim = -1; /* nothing left to recover */ |
377 |
> |
free(pdone); |
378 |
> |
pdone = NULL; |
379 |
> |
return(0); |
380 |
> |
} |
381 |
|
|
382 |
< |
free((char *)scanline); |
382 |
> |
|
383 |
> |
int |
384 |
> |
cleanup(rstat) /* close rpict process and clean up */ |
385 |
> |
int rstat; |
386 |
> |
{ |
387 |
> |
int status; |
388 |
> |
|
389 |
> |
bfree((char *)pbuf, hres*vres*sizeof(COLR)); |
390 |
|
fclose(torp); |
391 |
|
fclose(fromrp); |
392 |
< |
rpstat = close_process(rpd); |
393 |
< |
if (rpstat == -1) |
394 |
< |
rpstat = 0; |
395 |
< |
return(rpstat); |
392 |
> |
while (wait(&status) != -1) |
393 |
> |
if (rstat == 0) |
394 |
> |
rstat = status>>8 & 0xff; |
395 |
> |
return(rstat); |
396 |
|
} |
397 |
|
|
398 |
|
|
400 |
|
{ |
401 |
|
VIEW pview; |
402 |
|
int xorg, yorg; |
403 |
< |
|
403 |
> |
/* compute view parameters */ |
404 |
> |
copystruct(&pview, &ourview); |
405 |
> |
switch (ourview.type) { |
406 |
> |
case VT_PER: |
407 |
> |
pview.horiz = 2.*180./PI*atan( |
408 |
> |
tan(PI/180./2.*ourview.horiz)/hmult ); |
409 |
> |
pview.vert = 2.*180./PI*atan( |
410 |
> |
tan(PI/180./2.*ourview.vert)/vmult ); |
411 |
> |
break; |
412 |
> |
case VT_PAR: |
413 |
> |
case VT_ANG: |
414 |
> |
pview.horiz = ourview.horiz / hmult; |
415 |
> |
pview.vert = ourview.vert / vmult; |
416 |
> |
break; |
417 |
> |
case VT_CYL: |
418 |
> |
pview.horiz = ourview.horiz / hmult; |
419 |
> |
pview.vert = 2.*180./PI*atan( |
420 |
> |
tan(PI/180./2.*ourview.vert)/vmult ); |
421 |
> |
break; |
422 |
> |
case VT_HEM: |
423 |
> |
pview.horiz = 2.*180./PI*asin( |
424 |
> |
sin(PI/180./2.*ourview.horiz)/hmult ); |
425 |
> |
pview.vert = 2.*180./PI*asin( |
426 |
> |
sin(PI/180./2.*ourview.vert)/vmult ); |
427 |
> |
break; |
428 |
> |
default: |
429 |
> |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
430 |
> |
progname, ourview.type); |
431 |
> |
exit(cleanup(1)); |
432 |
> |
} |
433 |
> |
/* render each piece */ |
434 |
|
while (nextpiece(&xorg, &yorg)) { |
435 |
< |
copystruct(&pview, &ourview); /* compute view for piece */ |
436 |
< |
switch (ourview.type) { |
260 |
< |
case VT_PER: |
261 |
< |
pview.horiz = 2.*180./PI*atan( |
262 |
< |
tan(PI/180./2.*ourview.horiz)/hmult ); |
263 |
< |
pview.vert = 2.*180./PI*atan( |
264 |
< |
tan(PI/180./2.*ourview.vert)/vmult ); |
265 |
< |
break; |
266 |
< |
case VT_PAR: |
267 |
< |
case VT_ANG: |
268 |
< |
pview.horiz = ourview.horiz / hmult; |
269 |
< |
pview.vert = ourview.vert / vmult; |
270 |
< |
break; |
271 |
< |
case VT_HEM: |
272 |
< |
pview.horiz = 2.*180./PI*asin( |
273 |
< |
sin(PI/180./2.*ourview.horiz)/hmult ); |
274 |
< |
pview.vert = 2.*180./PI*asin( |
275 |
< |
sin(PI/180./2.*ourview.vert)/vmult ); |
276 |
< |
break; |
277 |
< |
default: |
278 |
< |
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
279 |
< |
progname, ourview.type); |
280 |
< |
exit(1); |
281 |
< |
} |
282 |
< |
pview.hoff += xorg - 0.5*(hmult-1); |
283 |
< |
pview.voff += yorg - 0.5*(vmult-1); |
435 |
> |
pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1); |
436 |
> |
pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1); |
437 |
|
fputs(VIEWSTR, torp); |
438 |
|
fprintview(&pview, torp); |
439 |
|
putc('\n', torp); |
440 |
< |
fflush(torp); /* assign piece to rpict */ |
440 |
> |
fflush(torp); /* assigns piece to rpict */ |
441 |
|
putpiece(xorg, yorg); /* place piece in output */ |
289 |
– |
if (verbose) { /* notify caller */ |
290 |
– |
printf("%d %d done\n", xorg, yorg); |
291 |
– |
fflush(stdout); |
292 |
– |
} |
442 |
|
} |
443 |
|
} |
444 |
|
|
445 |
|
|
446 |
+ |
int |
447 |
|
putpiece(xpos, ypos) /* get next piece from rpict */ |
448 |
|
int xpos, ypos; |
449 |
|
{ |
450 |
+ |
struct flock fls; |
451 |
+ |
int pid, status; |
452 |
|
int hr, vr; |
453 |
< |
int y; |
454 |
< |
|
453 |
> |
register int y; |
454 |
> |
/* check bounds */ |
455 |
|
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
456 |
|
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
457 |
|
progname, xpos, ypos); |
458 |
< |
exit(1); |
458 |
> |
exit(cleanup(1)); |
459 |
|
} |
460 |
< |
getheader(fromrp, NULL); /* discard header info. */ |
461 |
< |
if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */ |
462 |
< |
hr != hres || vr != vres) { |
460 |
> |
/* check header from rpict */ |
461 |
> |
guard_io(); |
462 |
> |
getheader(fromrp, NULL, NULL); |
463 |
> |
if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) { |
464 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
465 |
|
progname, rpargv[0]); |
466 |
< |
exit(1); |
466 |
> |
exit(cleanup(1)); |
467 |
|
} |
468 |
< |
for (y = 0; y < vr; y++) { /* transfer scanlines */ |
469 |
< |
if (freadcolrs(scanline, hr, fromrp) < 0) { |
468 |
> |
if (verbose) { /* notify caller */ |
469 |
> |
printf("%d %d begun\n", xpos, ypos); |
470 |
> |
fflush(stdout); |
471 |
> |
} |
472 |
> |
unguard(); |
473 |
> |
/* load new piece into buffer */ |
474 |
> |
for (y = 0; y < vr; y++) { |
475 |
> |
guard_io(); |
476 |
> |
if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) { |
477 |
|
fprintf(stderr, "%s: read error from %s\n", |
478 |
|
progname, rpargv[0]); |
479 |
< |
exit(1); |
479 |
> |
exit(cleanup(1)); |
480 |
|
} |
481 |
< |
if ((y == 0 || hmult != 1) && lseek(outfd, |
482 |
< |
scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR), |
483 |
< |
0) == -1) { |
484 |
< |
fprintf(stderr, "%s: seek error\n", outfile); |
485 |
< |
exit(1); |
481 |
> |
unguard(); |
482 |
> |
} |
483 |
> |
#if MAXFORK |
484 |
> |
/* fork so we don't slow rpict down */ |
485 |
> |
if ((pid = fork()) > 0) { |
486 |
> |
if (++nforked >= MAXFORK) { |
487 |
> |
wait(&status); /* reap a child */ |
488 |
> |
if (status) |
489 |
> |
exit(cleanup(status>>8 & 0xff)); |
490 |
> |
nforked--; |
491 |
|
} |
492 |
< |
if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) != |
493 |
< |
hr*sizeof(COLR)) { |
494 |
< |
fprintf(stderr, "%s: write error\n", outfile); |
495 |
< |
exit(1); |
492 |
> |
return(pid); |
493 |
> |
} |
494 |
> |
#else |
495 |
> |
pid = -1; /* no forking */ |
496 |
> |
#endif |
497 |
> |
fls.l_start = scanorig + |
498 |
> |
((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR); |
499 |
> |
#if NFS |
500 |
> |
fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR); |
501 |
> |
/* lock file section so NFS doesn't mess up */ |
502 |
> |
fls.l_whence = 0; |
503 |
> |
fls.l_type = F_WRLCK; |
504 |
> |
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
505 |
> |
filerr("lock"); |
506 |
> |
#endif |
507 |
> |
/* write new piece to file */ |
508 |
> |
if (lseek(outfd, (off_t)fls.l_start, 0) < 0) |
509 |
> |
filerr("seek"); |
510 |
> |
if (hmult == 1) { |
511 |
> |
if (writebuf(outfd, (char *)pbuf, |
512 |
> |
vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR)) |
513 |
> |
filerr("write"); |
514 |
> |
} else |
515 |
> |
for (y = 0; y < vr; y++) { |
516 |
> |
if (writebuf(outfd, (char *)(pbuf+y*hr), |
517 |
> |
hr*sizeof(COLR)) != hr*sizeof(COLR)) |
518 |
> |
filerr("write"); |
519 |
> |
if (y < vr-1 && lseek(outfd, |
520 |
> |
(off_t)(hmult-1)*hr*sizeof(COLR), |
521 |
> |
1) < 0) |
522 |
> |
filerr("seek"); |
523 |
|
} |
524 |
+ |
#if NFS |
525 |
+ |
fls.l_type = F_UNLCK; /* release lock */ |
526 |
+ |
if (fcntl(outfd, F_SETLKW, &fls) < 0) |
527 |
+ |
filerr("lock"); |
528 |
+ |
#endif |
529 |
+ |
if (verbose) { /* notify caller */ |
530 |
+ |
printf("%d %d done\n", xpos, ypos); |
531 |
+ |
fflush(stdout); |
532 |
|
} |
533 |
+ |
if (syncfp != NULL) { /* record what's been done */ |
534 |
+ |
sflock(F_WRLCK); |
535 |
+ |
fseek(syncfp, 0L, 2); /* append index */ |
536 |
+ |
fprintf(syncfp, "%4d %4d\n", xpos, ypos); |
537 |
+ |
fflush(syncfp); |
538 |
+ |
/*** Unlock not necessary, since |
539 |
+ |
sflock(F_UNLCK); _exit() or nextpiece() is next ***/ |
540 |
+ |
} |
541 |
+ |
if (pid == -1) /* didn't fork or fork failed */ |
542 |
+ |
return(0); |
543 |
+ |
_exit(0); /* else exit child process (releasing locks) */ |
544 |
|
} |
545 |
+ |
|
546 |
+ |
|
547 |
+ |
filerr(t) /* report file error and exit */ |
548 |
+ |
char *t; |
549 |
+ |
{ |
550 |
+ |
fprintf(stderr, "%s: %s error on file \"%s\": %s\n", |
551 |
+ |
progname, t, outfile, strerror(errno)); |
552 |
+ |
_exit(1); |
553 |
+ |
} |
554 |
+ |
|
555 |
+ |
#endif |