| 17 |
|
#define pscan(y) (ourpict+(y)*ourview.hresolu) |
| 18 |
|
#define zscan(y) (ourzbuf+(y)*ourview.hresolu) |
| 19 |
|
|
| 20 |
+ |
#define F_FORE 1 /* fill foreground */ |
| 21 |
+ |
#define F_BACK 2 /* fill background */ |
| 22 |
+ |
|
| 23 |
|
#define ABS(x) ((x)>0?(x):-(x)) |
| 24 |
|
|
| 25 |
|
VIEW ourview = STDVIEW(512); /* desired view */ |
| 34 |
|
VIEW theirview = STDVIEW(512); /* input view */ |
| 35 |
|
int gotview; /* got input view? */ |
| 36 |
|
|
| 37 |
+ |
int fill = F_FORE|F_BACK; /* fill level */ |
| 38 |
+ |
COLR backcolr = BLKCOLR; /* background color */ |
| 39 |
+ |
|
| 40 |
|
double theirs2ours[4][4]; /* transformation matrix */ |
| 41 |
+ |
int normdist = 1; /* normalized distance? */ |
| 42 |
|
|
| 43 |
|
|
| 44 |
|
main(argc, argv) /* interpolate pictures */ |
| 46 |
|
char *argv[]; |
| 47 |
|
{ |
| 48 |
|
#define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt |
| 49 |
+ |
extern double atof(); |
| 50 |
|
int gotvfile = 0; |
| 51 |
+ |
char *zfile = NULL; |
| 52 |
|
char *err; |
| 53 |
|
int i; |
| 54 |
|
|
| 60 |
|
check(2,1); |
| 61 |
|
zeps = atof(argv[++i]); |
| 62 |
|
break; |
| 63 |
+ |
case 'n': /* dist. normalized? */ |
| 64 |
+ |
check(2,0); |
| 65 |
+ |
normdist = !normdist; |
| 66 |
+ |
break; |
| 67 |
+ |
case 'f': /* fill type */ |
| 68 |
+ |
switch (argv[i][2]) { |
| 69 |
+ |
case '0': /* none */ |
| 70 |
+ |
check(3,0); |
| 71 |
+ |
fill = 0; |
| 72 |
+ |
break; |
| 73 |
+ |
case 'f': /* foreground */ |
| 74 |
+ |
check(3,0); |
| 75 |
+ |
fill = F_FORE; |
| 76 |
+ |
break; |
| 77 |
+ |
case 'b': /* background */ |
| 78 |
+ |
check(3,0); |
| 79 |
+ |
fill = F_BACK; |
| 80 |
+ |
break; |
| 81 |
+ |
case 'a': /* all */ |
| 82 |
+ |
check(3,0); |
| 83 |
+ |
fill = F_FORE|F_BACK; |
| 84 |
+ |
break; |
| 85 |
+ |
case 'c': /* color */ |
| 86 |
+ |
check(3,3); |
| 87 |
+ |
setcolr(backcolr, atof(argv[i+1]), |
| 88 |
+ |
atof(argv[i+2]), atof(argv[i+3])); |
| 89 |
+ |
i += 3; |
| 90 |
+ |
break; |
| 91 |
+ |
default: |
| 92 |
+ |
goto badopt; |
| 93 |
+ |
} |
| 94 |
+ |
break; |
| 95 |
+ |
case 'z': /* z file */ |
| 96 |
+ |
check(2,1); |
| 97 |
+ |
zfile = argv[++i]; |
| 98 |
+ |
break; |
| 99 |
|
case 'x': /* x resolution */ |
| 100 |
|
check(2,1); |
| 101 |
|
ourview.hresolu = atoi(argv[++i]); |
| 154 |
|
break; |
| 155 |
|
default: |
| 156 |
|
badopt: |
| 157 |
< |
fprintf(stderr, "%s: unknown option '%s'\n", |
| 157 |
> |
fprintf(stderr, "%s: command line error at '%s'\n", |
| 158 |
|
progname, argv[i]); |
| 159 |
< |
exit(1); |
| 159 |
> |
goto userr; |
| 160 |
|
} |
| 161 |
|
/* check arguments */ |
| 162 |
< |
if (argc-i < 2 || (argc-i)%2) { |
| 163 |
< |
fprintf(stderr, "Usage: %s [view args] pfile zfile ..\n", |
| 119 |
< |
progname); |
| 120 |
< |
exit(1); |
| 121 |
< |
} |
| 162 |
> |
if (argc-i < 2 || (argc-i)%2) |
| 163 |
> |
goto userr; |
| 164 |
|
/* set view */ |
| 165 |
|
if (err = setview(&ourview)) { |
| 166 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
| 177 |
|
for ( ; i < argc; i += 2) |
| 178 |
|
addpicture(argv[i], argv[i+1]); |
| 179 |
|
/* fill in spaces */ |
| 180 |
< |
fillpicture(); |
| 180 |
> |
if (fill&F_BACK) |
| 181 |
> |
fillpicture(); |
| 182 |
> |
else |
| 183 |
> |
backpicture(); |
| 184 |
|
/* add to header */ |
| 185 |
|
printargs(argc, argv, stdout); |
| 186 |
|
if (gotvfile) { |
| 189 |
|
printf("\n"); |
| 190 |
|
} |
| 191 |
|
printf("\n"); |
| 192 |
< |
/* write output */ |
| 192 |
> |
/* write picture */ |
| 193 |
|
writepicture(); |
| 194 |
+ |
/* write z file */ |
| 195 |
+ |
if (zfile != NULL) |
| 196 |
+ |
writedistance(zfile); |
| 197 |
|
|
| 198 |
|
exit(0); |
| 199 |
+ |
userr: |
| 200 |
+ |
fprintf(stderr, |
| 201 |
+ |
"Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n", |
| 202 |
+ |
progname); |
| 203 |
+ |
exit(1); |
| 204 |
|
#undef check |
| 205 |
|
} |
| 206 |
|
|
| 222 |
|
} |
| 223 |
|
|
| 224 |
|
|
| 225 |
< |
addpicture(pfile, zfile) /* add picture to output */ |
| 226 |
< |
char *pfile, *zfile; |
| 225 |
> |
addpicture(pfile, zspec) /* add picture to output */ |
| 226 |
> |
char *pfile, *zspec; |
| 227 |
|
{ |
| 228 |
+ |
extern double atof(); |
| 229 |
|
FILE *pfp, *zfp; |
| 230 |
|
char *err; |
| 231 |
|
COLR *scanin; |
| 232 |
|
float *zin, *zlast; |
| 233 |
|
int *plast; |
| 234 |
|
int y; |
| 235 |
< |
/* open input files */ |
| 235 |
> |
/* open picture file */ |
| 236 |
|
if ((pfp = fopen(pfile, "r")) == NULL) { |
| 237 |
|
perror(pfile); |
| 238 |
|
exit(1); |
| 239 |
|
} |
| 186 |
– |
if ((zfp = fopen(zfile, "r")) == NULL) { |
| 187 |
– |
perror(zfile); |
| 188 |
– |
exit(1); |
| 189 |
– |
} |
| 240 |
|
/* get header and view */ |
| 241 |
|
printf("%s:\n", pfile); |
| 242 |
|
gotview = 0; |
| 255 |
|
/* allocate scanlines */ |
| 256 |
|
scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR)); |
| 257 |
|
zin = (float *)malloc(theirview.hresolu*sizeof(float)); |
| 208 |
– |
zlast = (float *)calloc(theirview.hresolu, sizeof(float)); |
| 258 |
|
plast = (int *)calloc(theirview.hresolu, sizeof(int)); |
| 259 |
< |
if (scanin == NULL || zin == NULL || zlast == NULL || plast == NULL) { |
| 259 |
> |
zlast = (float *)calloc(theirview.hresolu, sizeof(float)); |
| 260 |
> |
if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) { |
| 261 |
|
perror(progname); |
| 262 |
|
exit(1); |
| 263 |
|
} |
| 264 |
+ |
/* get z specification or file */ |
| 265 |
+ |
if ((zfp = fopen(zspec, "r")) == NULL) { |
| 266 |
+ |
double zvalue; |
| 267 |
+ |
register int x; |
| 268 |
+ |
if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) { |
| 269 |
+ |
perror(zspec); |
| 270 |
+ |
exit(1); |
| 271 |
+ |
} |
| 272 |
+ |
for (x = 0; x < theirview.hresolu; x++) |
| 273 |
+ |
zin[x] = zvalue; |
| 274 |
+ |
} |
| 275 |
|
/* load image */ |
| 276 |
|
for (y = theirview.vresolu-1; y >= 0; y--) { |
| 277 |
|
if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) { |
| 278 |
|
fprintf(stderr, "%s: read error\n", pfile); |
| 279 |
|
exit(1); |
| 280 |
|
} |
| 281 |
< |
if (fread(zin, sizeof(float), theirview.hresolu, zfp) |
| 281 |
> |
if (zfp != NULL |
| 282 |
> |
&& fread(zin,sizeof(float),theirview.hresolu,zfp) |
| 283 |
|
< theirview.hresolu) { |
| 284 |
< |
fprintf(stderr, "%s: read error\n", zfile); |
| 284 |
> |
fprintf(stderr, "%s: read error\n", zspec); |
| 285 |
|
exit(1); |
| 286 |
|
} |
| 287 |
|
addscanline(y, scanin, zin, plast, zlast); |
| 292 |
|
free((char *)plast); |
| 293 |
|
free((char *)zlast); |
| 294 |
|
fclose(pfp); |
| 295 |
< |
fclose(zfp); |
| 295 |
> |
if (zfp != NULL) |
| 296 |
> |
fclose(zfp); |
| 297 |
|
} |
| 298 |
|
|
| 299 |
|
|
| 340 |
|
int *lasty; /* input/output */ |
| 341 |
|
float *lastyz; /* input/output */ |
| 342 |
|
{ |
| 343 |
< |
extern double sqrt(), fabs(); |
| 343 |
> |
extern double sqrt(); |
| 344 |
|
double pos[3]; |
| 345 |
|
int lastx = 0; |
| 346 |
|
double lastxz = 0; |
| 353 |
|
pos[1] = y - .5*(theirview.vresolu-1); |
| 354 |
|
pos[2] = zline[x]; |
| 355 |
|
if (theirview.type == VT_PER) { |
| 356 |
< |
/* |
| 357 |
< |
* The following (single) statement can go |
| 295 |
< |
* if z is along the view direction rather |
| 296 |
< |
* than an eye ray. |
| 297 |
< |
*/ |
| 298 |
< |
pos[2] /= sqrt( 1. |
| 356 |
> |
if (normdist) /* adjust for eye-ray distance */ |
| 357 |
> |
pos[2] /= sqrt( 1. |
| 358 |
|
+ pos[0]*pos[0]*theirview.vhn2 |
| 359 |
|
+ pos[1]*pos[1]*theirview.vvn2 ); |
| 360 |
|
pos[0] *= pos[2]; |
| 375 |
|
/* add pixel to our image */ |
| 376 |
|
zt = 2.*zeps*zline[x]; |
| 377 |
|
addpixel(xpos, ypos, |
| 378 |
< |
(fabs(zline[x]-lastxz) <= zt) ? lastx - xpos : 1, |
| 379 |
< |
(fabs(zline[x]-lastyz[x]) <= zt) ? lasty[x] - ypos : 1, |
| 378 |
> |
(fill&F_FORE && ABS(zline[x]-lastxz) <= zt) |
| 379 |
> |
? lastx - xpos : 1, |
| 380 |
> |
(fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt) |
| 381 |
> |
? lasty[x] - ypos : 1, |
| 382 |
|
pline[x], pos[2]); |
| 383 |
|
lastx = xpos; |
| 384 |
|
lasty[x] = ypos; |
| 459 |
|
* Next, find background from left or right. |
| 460 |
|
*/ |
| 461 |
|
if (xback == -2) { |
| 462 |
< |
for (i = x+1; x < ourview.hresolu; i++) |
| 462 |
> |
for (i = x+1; i < ourview.hresolu; i++) |
| 463 |
|
if (zscan(y)[i] > 0) |
| 464 |
|
break; |
| 465 |
|
if (i < ourview.hresolu |
| 468 |
|
else |
| 469 |
|
xback = x-1; |
| 470 |
|
} |
| 410 |
– |
if (xback < 0 && yback[x] < 0) |
| 411 |
– |
continue; /* no background */ |
| 471 |
|
/* |
| 472 |
+ |
* Check to see if we have no background for |
| 473 |
+ |
* this pixel. If not, use background color. |
| 474 |
+ |
*/ |
| 475 |
+ |
if (xback < 0 && yback[x] < 0) { |
| 476 |
+ |
copycolr(pscan(y)[x], backcolr); |
| 477 |
+ |
continue; |
| 478 |
+ |
} |
| 479 |
+ |
/* |
| 480 |
|
* Compare, and use the background that is |
| 481 |
|
* farther, unless one of them is next to us. |
| 482 |
|
*/ |
| 483 |
< |
if (yback[x] < 0 || ABS(x-xback) <= 1 |
| 483 |
> |
if ( yback[x] < 0 |
| 484 |
> |
|| (xback >= 0 && ABS(x-xback) <= 1) |
| 485 |
|
|| ( ABS(y-yback[x]) > 1 |
| 486 |
< |
&& zscan(yback[x])[x] < zscan(y)[xback] )) |
| 486 |
> |
&& zscan(yback[x])[x] |
| 487 |
> |
< zscan(y)[xback] ) ) |
| 488 |
|
copycolr(pscan(y)[x],pscan(y)[xback]); |
| 489 |
|
else |
| 490 |
|
copycolr(pscan(y)[x],pscan(yback[x])[x]); |
| 497 |
|
} |
| 498 |
|
|
| 499 |
|
|
| 500 |
+ |
backpicture() /* paint in empty pixels */ |
| 501 |
+ |
{ |
| 502 |
+ |
register int x, y; |
| 503 |
+ |
|
| 504 |
+ |
for (y = 0; y < ourview.vresolu; y++) |
| 505 |
+ |
for (x = 0; x < ourview.hresolu; x++) |
| 506 |
+ |
if (zscan(y)[x] <= 0) |
| 507 |
+ |
copycolr(pscan(y)[x], backcolr); |
| 508 |
+ |
} |
| 509 |
+ |
|
| 510 |
+ |
|
| 511 |
|
writepicture() /* write out picture */ |
| 512 |
|
{ |
| 513 |
|
int y; |
| 518 |
|
perror(progname); |
| 519 |
|
exit(1); |
| 520 |
|
} |
| 521 |
+ |
} |
| 522 |
+ |
|
| 523 |
+ |
|
| 524 |
+ |
writedistance(fname) /* write out z file */ |
| 525 |
+ |
char *fname; |
| 526 |
+ |
{ |
| 527 |
+ |
extern double sqrt(); |
| 528 |
+ |
int donorm = normdist && ourview.type == VT_PER; |
| 529 |
+ |
FILE *fp; |
| 530 |
+ |
int y; |
| 531 |
+ |
float *zout; |
| 532 |
+ |
|
| 533 |
+ |
if ((fp = fopen(fname, "w")) == NULL) { |
| 534 |
+ |
perror(fname); |
| 535 |
+ |
exit(1); |
| 536 |
+ |
} |
| 537 |
+ |
if (donorm |
| 538 |
+ |
&& (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) { |
| 539 |
+ |
perror(progname); |
| 540 |
+ |
exit(1); |
| 541 |
+ |
} |
| 542 |
+ |
for (y = ourview.vresolu-1; y >= 0; y--) { |
| 543 |
+ |
if (donorm) { |
| 544 |
+ |
double vx, yzn2; |
| 545 |
+ |
register int x; |
| 546 |
+ |
yzn2 = y - .5*(ourview.vresolu-1); |
| 547 |
+ |
yzn2 = 1. + yzn2*yzn2*ourview.vvn2; |
| 548 |
+ |
for (x = 0; x < ourview.hresolu; x++) { |
| 549 |
+ |
vx = x - .5*(ourview.hresolu-1); |
| 550 |
+ |
zout[x] = zscan(y)[x] |
| 551 |
+ |
* sqrt(vx*vx*ourview.vhn2 + yzn2); |
| 552 |
+ |
} |
| 553 |
+ |
} else |
| 554 |
+ |
zout = zscan(y); |
| 555 |
+ |
if (fwrite(zout, sizeof(float), ourview.hresolu, fp) |
| 556 |
+ |
< ourview.hresolu) { |
| 557 |
+ |
perror(fname); |
| 558 |
+ |
exit(1); |
| 559 |
+ |
} |
| 560 |
+ |
} |
| 561 |
+ |
if (donorm) |
| 562 |
+ |
free((char *)zout); |
| 563 |
+ |
fclose(fp); |
| 564 |
+ |
} |
| 565 |
+ |
|
| 566 |
+ |
|
| 567 |
+ |
isfloat(s) /* see if string is floating number */ |
| 568 |
+ |
register char *s; |
| 569 |
+ |
{ |
| 570 |
+ |
for ( ; *s; s++) |
| 571 |
+ |
if ((*s < '0' || *s > '9') && *s != '.' && *s != '-' |
| 572 |
+ |
&& *s != 'e' && *s != 'E' && *s != '+') |
| 573 |
+ |
return(0); |
| 574 |
+ |
return(1); |
| 575 |
|
} |