| 21 |
|
|
| 22 |
|
VIEW ourview = STDVIEW(512); /* desired view */ |
| 23 |
|
|
| 24 |
< |
double zeps = 0.001; /* allowed z epsilon */ |
| 24 |
> |
double zeps = .02; /* allowed z epsilon */ |
| 25 |
|
|
| 26 |
|
COLR *ourpict; /* output picture */ |
| 27 |
|
float *ourzbuf; /* corresponding z-buffer */ |
| 32 |
|
int gotview; /* got input view? */ |
| 33 |
|
|
| 34 |
|
double theirs2ours[4][4]; /* transformation matrix */ |
| 35 |
+ |
int regdist = 0; /* regular distance? */ |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
main(argc, argv) /* interpolate pictures */ |
| 52 |
|
check(2,1); |
| 53 |
|
zeps = atof(argv[++i]); |
| 54 |
|
break; |
| 55 |
+ |
case 'r': /* regular distance */ |
| 56 |
+ |
check(2,0); |
| 57 |
+ |
regdist = !regdist; |
| 58 |
+ |
break; |
| 59 |
+ |
case 'x': /* x resolution */ |
| 60 |
+ |
check(2,1); |
| 61 |
+ |
ourview.hresolu = atoi(argv[++i]); |
| 62 |
+ |
break; |
| 63 |
+ |
case 'y': /* y resolution */ |
| 64 |
+ |
check(2,1); |
| 65 |
+ |
ourview.vresolu = atoi(argv[++i]); |
| 66 |
+ |
break; |
| 67 |
|
case 'v': /* view */ |
| 68 |
|
switch (argv[i][2]) { |
| 69 |
|
case 't': /* type */ |
| 114 |
|
break; |
| 115 |
|
default: |
| 116 |
|
badopt: |
| 117 |
< |
fprintf(stderr, "%s: unknown option '%s'\n", |
| 117 |
> |
fprintf(stderr, "%s: command line error at '%s'\n", |
| 118 |
|
progname, argv[i]); |
| 119 |
< |
exit(1); |
| 119 |
> |
goto userr; |
| 120 |
|
} |
| 121 |
|
/* check arguments */ |
| 122 |
< |
if (argc-i < 2 || (argc-i)%2) { |
| 123 |
< |
fprintf(stderr, "Usage: %s [view args] pfile zfile ..\n", |
| 111 |
< |
progname); |
| 112 |
< |
exit(1); |
| 113 |
< |
} |
| 122 |
> |
if (argc-i < 2 || (argc-i)%2) |
| 123 |
> |
goto userr; |
| 124 |
|
/* set view */ |
| 125 |
|
if (err = setview(&ourview)) { |
| 126 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
| 150 |
|
writepicture(); |
| 151 |
|
|
| 152 |
|
exit(0); |
| 153 |
+ |
userr: |
| 154 |
+ |
fprintf(stderr, |
| 155 |
+ |
"Usage: %s [view opts][-t zthresh][-r] pfile zspec ..\n", |
| 156 |
+ |
progname); |
| 157 |
+ |
exit(1); |
| 158 |
|
#undef check |
| 159 |
|
} |
| 160 |
|
|
| 176 |
|
} |
| 177 |
|
|
| 178 |
|
|
| 179 |
< |
addpicture(pfile, zfile) /* add picture to output */ |
| 180 |
< |
char *pfile, *zfile; |
| 179 |
> |
addpicture(pfile, zspec) /* add picture to output */ |
| 180 |
> |
char *pfile, *zspec; |
| 181 |
|
{ |
| 182 |
+ |
extern double atof(); |
| 183 |
|
FILE *pfp, *zfp; |
| 168 |
– |
COLR *scanin; |
| 169 |
– |
float *zin; |
| 184 |
|
char *err; |
| 185 |
< |
int xres, yres; |
| 185 |
> |
COLR *scanin; |
| 186 |
> |
float *zin, *zlast; |
| 187 |
> |
int *plast; |
| 188 |
|
int y; |
| 189 |
< |
/* open input files */ |
| 189 |
> |
/* open picture file */ |
| 190 |
|
if ((pfp = fopen(pfile, "r")) == NULL) { |
| 191 |
|
perror(pfile); |
| 192 |
|
exit(1); |
| 193 |
|
} |
| 178 |
– |
if ((zfp = fopen(zfile, "r")) == NULL) { |
| 179 |
– |
perror(zfile); |
| 180 |
– |
exit(1); |
| 181 |
– |
} |
| 194 |
|
/* get header and view */ |
| 195 |
|
printf("%s:\n", pfile); |
| 196 |
|
gotview = 0; |
| 197 |
|
getheader(pfp, headline); |
| 198 |
< |
if (!gotview || fgetresolu(&xres, &yres, pfp) != (YMAJOR|YDECR)) { |
| 198 |
> |
if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp) |
| 199 |
> |
!= (YMAJOR|YDECR)) { |
| 200 |
|
fprintf(stderr, "%s: picture view error\n", pfile); |
| 201 |
|
exit(1); |
| 202 |
|
} |
| 190 |
– |
theirview.hresolu = xres; |
| 191 |
– |
theirview.vresolu = yres; |
| 203 |
|
if (err = setview(&theirview)) { |
| 204 |
|
fprintf(stderr, "%s: %s\n", pfile, err); |
| 205 |
|
exit(1); |
| 207 |
|
/* compute transformation */ |
| 208 |
|
pixform(theirs2ours, &theirview, &ourview); |
| 209 |
|
/* allocate scanlines */ |
| 210 |
< |
scanin = (COLR *)malloc(xres*sizeof(COLR)); |
| 211 |
< |
zin = (float *)malloc(xres*sizeof(float)); |
| 212 |
< |
if (scanin == NULL || zin == NULL) { |
| 210 |
> |
scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR)); |
| 211 |
> |
zin = (float *)malloc(theirview.hresolu*sizeof(float)); |
| 212 |
> |
plast = (int *)calloc(theirview.hresolu, sizeof(int)); |
| 213 |
> |
zlast = (float *)calloc(theirview.hresolu, sizeof(float)); |
| 214 |
> |
if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) { |
| 215 |
|
perror(progname); |
| 216 |
|
exit(1); |
| 217 |
|
} |
| 218 |
+ |
/* get z specification or file */ |
| 219 |
+ |
if ((zfp = fopen(zspec, "r")) == NULL) { |
| 220 |
+ |
double zvalue; |
| 221 |
+ |
register int x; |
| 222 |
+ |
if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) { |
| 223 |
+ |
perror(zspec); |
| 224 |
+ |
exit(1); |
| 225 |
+ |
} |
| 226 |
+ |
for (x = 0; x < theirview.hresolu; x++) |
| 227 |
+ |
zin[x] = zvalue; |
| 228 |
+ |
} |
| 229 |
|
/* load image */ |
| 230 |
< |
for (y = yres-1; y >= 0; y--) { |
| 231 |
< |
if (freadcolrs(scanin, xres, pfp) < 0) { |
| 230 |
> |
for (y = theirview.vresolu-1; y >= 0; y--) { |
| 231 |
> |
if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) { |
| 232 |
|
fprintf(stderr, "%s: read error\n", pfile); |
| 233 |
|
exit(1); |
| 234 |
|
} |
| 235 |
< |
if (fread(zin, sizeof(float), xres, zfp) < xres) { |
| 236 |
< |
fprintf(stderr, "%s: read error\n", zfile); |
| 235 |
> |
if (zfp != NULL |
| 236 |
> |
&& fread(zin,sizeof(float),theirview.hresolu,zfp) |
| 237 |
> |
< theirview.hresolu) { |
| 238 |
> |
fprintf(stderr, "%s: read error\n", zspec); |
| 239 |
|
exit(1); |
| 240 |
|
} |
| 241 |
< |
addscanline(y, scanin, zin); |
| 241 |
> |
addscanline(y, scanin, zin, plast, zlast); |
| 242 |
|
} |
| 243 |
|
/* clean up */ |
| 244 |
|
free((char *)scanin); |
| 245 |
|
free((char *)zin); |
| 246 |
+ |
free((char *)plast); |
| 247 |
+ |
free((char *)zlast); |
| 248 |
|
fclose(pfp); |
| 249 |
< |
fclose(zfp); |
| 249 |
> |
if (zfp != NULL) |
| 250 |
> |
fclose(zfp); |
| 251 |
|
} |
| 252 |
|
|
| 253 |
|
|
| 287 |
|
} |
| 288 |
|
|
| 289 |
|
|
| 290 |
< |
addscanline(y, pline, zline) /* add scanline to output */ |
| 290 |
> |
addscanline(y, pline, zline, lasty, lastyz) /* add scanline to output */ |
| 291 |
|
int y; |
| 292 |
|
COLR *pline; |
| 293 |
|
float *zline; |
| 294 |
+ |
int *lasty; /* input/output */ |
| 295 |
+ |
float *lastyz; /* input/output */ |
| 296 |
|
{ |
| 297 |
< |
extern double sqrt(); |
| 297 |
> |
extern double sqrt(), fabs(); |
| 298 |
|
double pos[3]; |
| 299 |
+ |
int lastx = 0; |
| 300 |
+ |
double lastxz = 0; |
| 301 |
+ |
double zt; |
| 302 |
+ |
int xpos, ypos; |
| 303 |
|
register int x; |
| 269 |
– |
register int xpos, ypos; |
| 304 |
|
|
| 305 |
< |
for (x = 0; x < theirview.hresolu; x++) { |
| 305 |
> |
for (x = theirview.hresolu-1; x >= 0; x--) { |
| 306 |
|
pos[0] = x - .5*(theirview.hresolu-1); |
| 307 |
|
pos[1] = y - .5*(theirview.vresolu-1); |
| 308 |
|
pos[2] = zline[x]; |
| 309 |
|
if (theirview.type == VT_PER) { |
| 310 |
< |
pos[2] /= sqrt( 1. |
| 310 |
> |
if (!regdist) /* adjust for eye-ray distance */ |
| 311 |
> |
pos[2] /= sqrt( 1. |
| 312 |
|
+ pos[0]*pos[0]*theirview.vhn2 |
| 313 |
|
+ pos[1]*pos[1]*theirview.vvn2 ); |
| 314 |
|
pos[0] *= pos[2]; |
| 315 |
|
pos[1] *= pos[2]; |
| 316 |
|
} |
| 317 |
|
multp3(pos, pos, theirs2ours); |
| 318 |
< |
if (pos[2] <= 0.0) |
| 318 |
> |
if (pos[2] <= 0) |
| 319 |
|
continue; |
| 320 |
|
if (ourview.type == VT_PER) { |
| 321 |
|
pos[0] /= pos[2]; |
| 323 |
|
} |
| 324 |
|
pos[0] += .5*ourview.hresolu; |
| 325 |
|
pos[1] += .5*ourview.vresolu; |
| 326 |
< |
if (pos[0] < 0 || pos[0] > ourview.hresolu |
| 327 |
< |
|| pos[1] < 0 || pos[1] > ourview.vresolu) |
| 326 |
> |
if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu |
| 327 |
> |
|| pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu) |
| 328 |
|
continue; |
| 329 |
< |
/* check current value at pos */ |
| 330 |
< |
xpos = pos[0]; |
| 331 |
< |
ypos = pos[1]; |
| 332 |
< |
if (zscan(ypos)[xpos] <= 0.0 |
| 333 |
< |
|| zscan(ypos)[xpos] - pos[2] |
| 334 |
< |
> zeps*zscan(ypos)[xpos]) { |
| 335 |
< |
zscan(ypos)[xpos] = pos[2]; |
| 336 |
< |
copycolr(pscan(ypos)[xpos], pline[x]); |
| 337 |
< |
} |
| 329 |
> |
/* add pixel to our image */ |
| 330 |
> |
zt = 2.*zeps*zline[x]; |
| 331 |
> |
addpixel(xpos, ypos, |
| 332 |
> |
(fabs(zline[x]-lastxz) <= zt) ? lastx - xpos : 1, |
| 333 |
> |
(fabs(zline[x]-lastyz[x]) <= zt) ? lasty[x] - ypos : 1, |
| 334 |
> |
pline[x], pos[2]); |
| 335 |
> |
lastx = xpos; |
| 336 |
> |
lasty[x] = ypos; |
| 337 |
> |
lastxz = lastyz[x] = zline[x]; |
| 338 |
|
} |
| 339 |
|
} |
| 340 |
|
|
| 341 |
|
|
| 342 |
+ |
addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */ |
| 343 |
+ |
int xstart, ystart; |
| 344 |
+ |
int width, height; |
| 345 |
+ |
COLR pix; |
| 346 |
+ |
double z; |
| 347 |
+ |
{ |
| 348 |
+ |
register int x, y; |
| 349 |
+ |
/* make width and height positive */ |
| 350 |
+ |
if (width < 0) { |
| 351 |
+ |
width = -width; |
| 352 |
+ |
xstart = xstart-width+1; |
| 353 |
+ |
} else if (width == 0) |
| 354 |
+ |
width = 1; |
| 355 |
+ |
if (height < 0) { |
| 356 |
+ |
height = -height; |
| 357 |
+ |
ystart = ystart-height+1; |
| 358 |
+ |
} else if (height == 0) |
| 359 |
+ |
height = 1; |
| 360 |
+ |
/* fill pixel(s) within rectangle */ |
| 361 |
+ |
for (y = ystart; y < ystart+height; y++) |
| 362 |
+ |
for (x = xstart; x < xstart+width; x++) |
| 363 |
+ |
if (zscan(y)[x] <= 0 |
| 364 |
+ |
|| zscan(y)[x]-z > zeps*zscan(y)[x]) { |
| 365 |
+ |
zscan(y)[x] = z; |
| 366 |
+ |
copycolr(pscan(y)[x], pix); |
| 367 |
+ |
} |
| 368 |
+ |
} |
| 369 |
+ |
|
| 370 |
+ |
|
| 371 |
|
fillpicture() /* fill in empty spaces */ |
| 372 |
|
{ |
| 373 |
|
int *yback, xback; |
| 382 |
|
} |
| 383 |
|
for (x = 0; x < ourview.hresolu; x++) |
| 384 |
|
yback[x] = -2; |
| 385 |
+ |
/* |
| 386 |
+ |
* Xback and yback are the pixel locations of suitable |
| 387 |
+ |
* background values in each direction. |
| 388 |
+ |
* A value of -2 means unassigned, and -1 means |
| 389 |
+ |
* that there is no suitable background in this direction. |
| 390 |
+ |
*/ |
| 391 |
|
/* fill image */ |
| 392 |
|
for (y = 0; y < ourview.vresolu; y++) { |
| 393 |
|
xback = -2; |
| 394 |
|
for (x = 0; x < ourview.hresolu; x++) |
| 395 |
< |
if (zscan(y)[x] <= 0.0) { /* empty pixel */ |
| 395 |
> |
if (zscan(y)[x] <= 0) { /* empty pixel */ |
| 396 |
> |
/* |
| 397 |
> |
* First, find background from above or below. |
| 398 |
> |
* (farthest assigned pixel) |
| 399 |
> |
*/ |
| 400 |
|
if (yback[x] == -2) { |
| 401 |
|
for (i = y+1; i < ourview.vresolu; i++) |
| 402 |
< |
if (zscan(i)[x] > 0.0) |
| 402 |
> |
if (zscan(i)[x] > 0) |
| 403 |
|
break; |
| 404 |
|
if (i < ourview.vresolu |
| 405 |
|
&& (y <= 0 || zscan(y-1)[x] < zscan(i)[x])) |
| 407 |
|
else |
| 408 |
|
yback[x] = y-1; |
| 409 |
|
} |
| 410 |
+ |
/* |
| 411 |
+ |
* Next, find background from left or right. |
| 412 |
+ |
*/ |
| 413 |
|
if (xback == -2) { |
| 414 |
|
for (i = x+1; x < ourview.hresolu; i++) |
| 415 |
< |
if (zscan(y)[i] > 0.0) |
| 415 |
> |
if (zscan(y)[i] > 0) |
| 416 |
|
break; |
| 417 |
|
if (i < ourview.hresolu |
| 418 |
|
&& (x <= 0 || zscan(y)[x-1] < zscan(y)[i])) |
| 421 |
|
xback = x-1; |
| 422 |
|
} |
| 423 |
|
if (xback < 0 && yback[x] < 0) |
| 424 |
< |
continue; |
| 424 |
> |
continue; /* no background */ |
| 425 |
> |
/* |
| 426 |
> |
* Compare, and use the background that is |
| 427 |
> |
* farther, unless one of them is next to us. |
| 428 |
> |
*/ |
| 429 |
|
if (yback[x] < 0 || ABS(x-xback) <= 1 |
| 430 |
|
|| ( ABS(y-yback[x]) > 1 |
| 431 |
|
&& zscan(yback[x])[x] < zscan(y)[xback] )) |
| 451 |
|
perror(progname); |
| 452 |
|
exit(1); |
| 453 |
|
} |
| 454 |
+ |
} |
| 455 |
+ |
|
| 456 |
+ |
|
| 457 |
+ |
isfloat(s) /* see if string is floating number */ |
| 458 |
+ |
register char *s; |
| 459 |
+ |
{ |
| 460 |
+ |
for ( ; *s; s++) |
| 461 |
+ |
if ((*s < '0' || *s > '9') && *s != '.' && *s != '-' |
| 462 |
+ |
&& *s != 'e' && *s != 'E' && *s != '+') |
| 463 |
+ |
return(0); |
| 464 |
+ |
return(1); |
| 465 |
|
} |