20 |
|
int rpargc = 9; |
21 |
|
int rpd[3]; |
22 |
|
FILE *torp, *fromrp; |
23 |
< |
COLR *scanline; |
23 |
> |
COLR *pbuf; |
24 |
|
/* our view parameters */ |
25 |
|
VIEW ourview = STDVIEW; |
26 |
|
double pixaspect = 1.0; |
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); |
164 |
> |
fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n", |
165 |
> |
progname, outfile); |
166 |
|
exit(1); |
167 |
|
} |
168 |
|
} else { |
169 |
< |
fprintf(stderr, "%s: cannot open\n", outfile); |
169 |
> |
fprintf(stderr, "%s: cannot open file \"%s\"\n", |
170 |
> |
progname, outfile); |
171 |
|
exit(1); |
172 |
|
} |
173 |
|
scanorig = ftell(fp); /* record position of first scanline */ |
174 |
|
if (fclose(fp) == -1) /* done with stream i/o */ |
175 |
|
goto filerr; |
176 |
< |
sync(); /* avoid NFS buffering */ |
176 |
> |
sync(); /* flush NFS buffers */ |
177 |
|
/* start rpict process */ |
178 |
|
if (open_process(rpd, rpargv) <= 0) { |
179 |
|
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
185 |
|
progname, rpargv[0]); |
186 |
|
exit(1); |
187 |
|
} |
188 |
< |
if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) { |
188 |
> |
if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) { |
189 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
190 |
|
exit(1); |
191 |
|
} |
192 |
|
signal(SIGALRM, onalrm); |
193 |
|
return; |
194 |
|
filerr: |
195 |
< |
fprintf(stderr, "%s: file i/o error\n", outfile); |
195 |
> |
fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile); |
196 |
|
exit(1); |
197 |
|
} |
198 |
|
|
216 |
|
lseek(syncfd, 0L, 0); |
217 |
|
buf[read(syncfd, buf, sizeof(buf)-1)] = '\0'; |
218 |
|
if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) { |
218 |
– |
*xp = hmult; |
219 |
– |
*yp = vmult-1; |
220 |
– |
} |
221 |
– |
if (--(*xp) < 0) { /* decrement position */ |
219 |
|
*xp = hmult-1; |
220 |
< |
if (--(*yp) < 0) { /* all done! */ |
220 |
> |
*yp = vmult; |
221 |
> |
} |
222 |
> |
if (--(*yp) < 0) { /* decrement position */ |
223 |
> |
*yp = vmult-1; |
224 |
> |
if (--(*xp) < 0) { /* all done! */ |
225 |
|
close(syncfd); |
226 |
|
return(0); |
227 |
|
} |
247 |
|
{ |
248 |
|
register int rpstat; |
249 |
|
|
250 |
< |
free((char *)scanline); |
250 |
> |
free((char *)pbuf); |
251 |
|
fclose(torp); |
252 |
|
fclose(fromrp); |
253 |
|
rpstat = close_process(rpd); |
305 |
|
putpiece(xpos, ypos) /* get next piece from rpict */ |
306 |
|
int xpos, ypos; |
307 |
|
{ |
308 |
+ |
struct flock fls; |
309 |
|
int hr, vr; |
310 |
< |
int y; |
311 |
< |
|
310 |
> |
register int y; |
311 |
> |
/* check bounds */ |
312 |
|
if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) { |
313 |
|
fprintf(stderr, "%s: requested piece (%d,%d) out of range\n", |
314 |
|
progname, xpos, ypos); |
315 |
|
exit(1); |
316 |
|
} |
317 |
< |
getheader(fromrp, NULL); /* discard header info. */ |
318 |
< |
if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */ |
319 |
< |
hr != hres || vr != vres) { |
317 |
> |
/* check header from rpict */ |
318 |
> |
getheader(fromrp, NULL); |
319 |
> |
if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres || vr != vres) { |
320 |
|
fprintf(stderr, "%s: resolution mismatch from %s\n", |
321 |
|
progname, rpargv[0]); |
322 |
|
exit(1); |
323 |
|
} |
324 |
< |
for (y = 0; y < vr; y++) { /* transfer scanlines */ |
325 |
< |
if (freadcolrs(scanline, hr, fromrp) < 0) { |
324 |
> |
/* load new piece into buffer */ |
325 |
> |
for (y = 0; y < vr; y++) |
326 |
> |
if (freadcolrs(pbuf+y*hr, 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 |
< |
if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) != |
331 |
> |
/* lock file section so NFS doesn't mess up */ |
332 |
> |
fls.l_whence = 0; |
333 |
> |
fls.l_len = (long)vres*hmult*hres*sizeof(COLR); |
334 |
> |
fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len; |
335 |
> |
fls.l_type = F_WRLCK; |
336 |
> |
fcntl(outfd, F_SETLKW, &fls); |
337 |
> |
/* write new piece to file */ |
338 |
> |
if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1) |
339 |
> |
goto seekerr; |
340 |
> |
for (y = 0; y < vr; y++) { |
341 |
> |
if (writebuf(outfd, (char *)(pbuf+y*hr), hr*sizeof(COLR)) != |
342 |
|
hr*sizeof(COLR)) { |
343 |
< |
fprintf(stderr, "%s: write error\n", outfile); |
343 |
> |
fprintf(stderr, "%s: write error on file \"%s\"\n", |
344 |
> |
progname, outfile); |
345 |
|
exit(1); |
346 |
|
} |
347 |
+ |
if (hmult != 1 && y < vr-1 && lseek(outfd, |
348 |
+ |
(long)(hmult-1)*hr*sizeof(COLR), 1) == -1) |
349 |
+ |
goto seekerr; |
350 |
|
} |
351 |
+ |
/* unlock file section */ |
352 |
+ |
fls.l_type = F_UNLCK; |
353 |
+ |
fcntl(outfd, F_SETLKW, &fls); |
354 |
+ |
return; |
355 |
+ |
seekerr: |
356 |
+ |
fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile); |
357 |
+ |
exit(1); |
358 |
|
} |