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