| 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; |
| 23 |
> |
COLR *pbuf; |
| 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; |
| 126 |
|
int ac; |
| 127 |
|
char **av; |
| 128 |
|
{ |
| 129 |
+ |
extern char VersionID[]; |
| 130 |
|
char *err; |
| 131 |
|
FILE *fp; |
| 132 |
|
int hr, vr; |
| 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); |
| 156 |
|
putc('\n', fp); |
| 157 |
|
fprtresolu(hres*hmult, vres*vmult, fp); |
| 158 |
|
} else if ((outfd = open(outfile, O_RDWR)) >= 0) { |
| 138 |
– |
sleep(30); /* wait for header */ |
| 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); |
| 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(); /* flush NFS buffers */ |
| 177 |
|
/* start rpict process */ |
| 178 |
|
if (open_process(rpd, rpargv) <= 0) { |
| 179 |
< |
fprintf(stderr, "%s: cannot start\n", rpargv[0]); |
| 179 |
> |
fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]); |
| 180 |
|
exit(1); |
| 181 |
|
} |
| 182 |
|
if ((fromrp = fdopen(rpd[0], "r")) == NULL || |
| 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 |
|
|
| 199 |
|
|
| 200 |
|
int |
| 201 |
+ |
nextpiece(xp, yp) /* get next piece assignment */ |
| 202 |
+ |
int *xp, *yp; |
| 203 |
+ |
{ |
| 204 |
+ |
extern char *fgets(); |
| 205 |
+ |
struct flock fls; |
| 206 |
+ |
char buf[64]; |
| 207 |
+ |
|
| 208 |
+ |
if (gotalrm) /* someone wants us to quit */ |
| 209 |
+ |
return(0); |
| 210 |
+ |
if (syncfd != -1) { /* use sync file */ |
| 211 |
+ |
fls.l_type = F_WRLCK; /* gain exclusive access */ |
| 212 |
+ |
fls.l_whence = 0; |
| 213 |
+ |
fls.l_start = 0L; |
| 214 |
+ |
fls.l_len = 0L; |
| 215 |
+ |
fcntl(syncfd, F_SETLKW, &fls); |
| 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) { |
| 219 |
+ |
*xp = hmult-1; |
| 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 |
+ |
} |
| 228 |
+ |
} |
| 229 |
+ |
sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp); |
| 230 |
+ |
lseek(syncfd, 0L, 0); /* write new position */ |
| 231 |
+ |
write(syncfd, buf, strlen(buf)); |
| 232 |
+ |
fls.l_type = F_UNLCK; /* release sync file */ |
| 233 |
+ |
fcntl(syncfd, F_SETLKW, &fls); |
| 234 |
+ |
return(1); |
| 235 |
+ |
} |
| 236 |
+ |
if (fgets(buf, sizeof(buf), stdin) == NULL) /* use stdin */ |
| 237 |
+ |
return(0); |
| 238 |
+ |
if (sscanf(buf, "%d %d", xp, yp) == 2) |
| 239 |
+ |
return(1); |
| 240 |
+ |
fprintf(stderr, "%s: input format error\n", progname); |
| 241 |
+ |
exit(1); |
| 242 |
+ |
} |
| 243 |
+ |
|
| 244 |
+ |
|
| 245 |
+ |
int |
| 246 |
|
cleanup() /* close rpict process and clean up */ |
| 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); |
| 259 |
|
|
| 260 |
|
rpiece() /* render picture piece by piece */ |
| 261 |
|
{ |
| 194 |
– |
extern char *gets(), *strcat(); |
| 262 |
|
VIEW pview; |
| 196 |
– |
char buf[48]; |
| 263 |
|
int xorg, yorg; |
| 264 |
|
|
| 265 |
< |
while (gets(buf) != NULL) { |
| 200 |
< |
if (sscanf(buf, "%d %d", &xorg, &yorg) != 2) { |
| 201 |
< |
fprintf(stderr, "%s: input format error\n", progname); |
| 202 |
< |
exit(1); |
| 203 |
< |
} |
| 265 |
> |
while (nextpiece(&xorg, &yorg)) { |
| 266 |
|
copystruct(&pview, &ourview); /* compute view for piece */ |
| 267 |
|
switch (ourview.type) { |
| 268 |
|
case VT_PER: |
| 277 |
|
pview.vert = ourview.vert / vmult; |
| 278 |
|
break; |
| 279 |
|
case VT_HEM: |
| 280 |
+ |
pview.horiz = 2.*180./PI*asin( |
| 281 |
+ |
sin(PI/180./2.*ourview.horiz)/hmult ); |
| 282 |
+ |
pview.vert = 2.*180./PI*asin( |
| 283 |
+ |
sin(PI/180./2.*ourview.vert)/vmult ); |
| 284 |
+ |
break; |
| 285 |
|
default: |
| 286 |
|
fprintf(stderr, "%s: unknown view type '-vt%c'\n", |
| 287 |
|
progname, ourview.type); |
| 295 |
|
fflush(torp); /* assign piece to rpict */ |
| 296 |
|
putpiece(xorg, yorg); /* place piece in output */ |
| 297 |
|
if (verbose) { /* notify caller */ |
| 298 |
< |
strcat(buf, " done\n"); |
| 299 |
< |
writebuf(fileno(stdout), buf, strlen(buf)); |
| 298 |
> |
printf("%d %d done\n", xorg, yorg); |
| 299 |
> |
fflush(stdout); |
| 300 |
|
} |
| 301 |
|
} |
| 302 |
|
} |
| 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 |
< |
|
| 312 |
< |
getheader(fromrp, NULL); /* discard header info. */ |
| 313 |
< |
if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */ |
| 314 |
< |
hr != hres || vr != vres) { |
| 247 |
< |
fprintf(stderr, "%s: resolution mismatch from rpict\n", |
| 248 |
< |
progname); |
| 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 |
< |
for (y = 0; y < vr; y++) { /* transfer scanlines */ |
| 318 |
< |
if (freadcolrs(scanline, hr, fromrp) < 0) { |
| 319 |
< |
fprintf(stderr, "%s: read error from rpict\n", |
| 320 |
< |
progname); |
| 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 |
> |
/* 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 |
|
} |