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 */ |
26 |
|
|
27 |
< |
double zeps = 0.001; /* allowed z epsilon */ |
27 |
> |
double zeps = .02; /* allowed z epsilon */ |
28 |
|
|
29 |
|
COLR *ourpict; /* output picture */ |
30 |
|
float *ourzbuf; /* corresponding z-buffer */ |
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; |
176 |
– |
COLR *scanin; |
177 |
– |
float *zin; |
230 |
|
char *err; |
231 |
< |
int xres, yres; |
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; |
243 |
|
getheader(pfp, headline); |
244 |
< |
if (!gotview || fgetresolu(&xres, &yres, pfp) != (YMAJOR|YDECR)) { |
244 |
> |
if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp) |
245 |
> |
!= (YMAJOR|YDECR)) { |
246 |
|
fprintf(stderr, "%s: picture view error\n", pfile); |
247 |
|
exit(1); |
248 |
|
} |
198 |
– |
theirview.hresolu = xres; |
199 |
– |
theirview.vresolu = yres; |
249 |
|
if (err = setview(&theirview)) { |
250 |
|
fprintf(stderr, "%s: %s\n", pfile, err); |
251 |
|
exit(1); |
253 |
|
/* compute transformation */ |
254 |
|
pixform(theirs2ours, &theirview, &ourview); |
255 |
|
/* allocate scanlines */ |
256 |
< |
scanin = (COLR *)malloc(xres*sizeof(COLR)); |
257 |
< |
zin = (float *)malloc(xres*sizeof(float)); |
258 |
< |
if (scanin == NULL || zin == NULL) { |
256 |
> |
scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR)); |
257 |
> |
zin = (float *)malloc(theirview.hresolu*sizeof(float)); |
258 |
> |
plast = (int *)calloc(theirview.hresolu, sizeof(int)); |
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 = yres-1; y >= 0; y--) { |
277 |
< |
if (freadcolrs(scanin, xres, pfp) < 0) { |
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), xres, zfp) < xres) { |
282 |
< |
fprintf(stderr, "%s: read error\n", zfile); |
281 |
> |
if (zfp != NULL |
282 |
> |
&& fread(zin,sizeof(float),theirview.hresolu,zfp) |
283 |
> |
< theirview.hresolu) { |
284 |
> |
fprintf(stderr, "%s: read error\n", zspec); |
285 |
|
exit(1); |
286 |
|
} |
287 |
< |
addscanline(y, scanin, zin); |
287 |
> |
addscanline(y, scanin, zin, plast, zlast); |
288 |
|
} |
289 |
|
/* clean up */ |
290 |
|
free((char *)scanin); |
291 |
|
free((char *)zin); |
292 |
+ |
free((char *)plast); |
293 |
+ |
free((char *)zlast); |
294 |
|
fclose(pfp); |
295 |
< |
fclose(zfp); |
295 |
> |
if (zfp != NULL) |
296 |
> |
fclose(zfp); |
297 |
|
} |
298 |
|
|
299 |
|
|
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
< |
addscanline(y, pline, zline) /* add scanline to output */ |
336 |
> |
addscanline(y, pline, zline, lasty, lastyz) /* add scanline to output */ |
337 |
|
int y; |
338 |
|
COLR *pline; |
339 |
|
float *zline; |
340 |
+ |
int *lasty; /* input/output */ |
341 |
+ |
float *lastyz; /* input/output */ |
342 |
|
{ |
343 |
|
extern double sqrt(); |
344 |
|
double pos[3]; |
345 |
+ |
int lastx = 0; |
346 |
+ |
double lastxz = 0; |
347 |
+ |
double zt; |
348 |
+ |
int xpos, ypos; |
349 |
|
register int x; |
277 |
– |
register int xpos, ypos; |
350 |
|
|
351 |
< |
for (x = 0; x < theirview.hresolu; x++) { |
351 |
> |
for (x = theirview.hresolu-1; x >= 0; x--) { |
352 |
|
pos[0] = x - .5*(theirview.hresolu-1); |
353 |
|
pos[1] = y - .5*(theirview.vresolu-1); |
354 |
|
pos[2] = zline[x]; |
355 |
|
if (theirview.type == VT_PER) { |
356 |
< |
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]; |
361 |
|
pos[1] *= pos[2]; |
362 |
|
} |
363 |
|
multp3(pos, pos, theirs2ours); |
364 |
< |
if (pos[2] <= 0.0) |
364 |
> |
if (pos[2] <= 0) |
365 |
|
continue; |
366 |
|
if (ourview.type == VT_PER) { |
367 |
|
pos[0] /= pos[2]; |
369 |
|
} |
370 |
|
pos[0] += .5*ourview.hresolu; |
371 |
|
pos[1] += .5*ourview.vresolu; |
372 |
< |
if (pos[0] < 0 || pos[0] >= ourview.hresolu |
373 |
< |
|| pos[1] < 0 || pos[1] >= ourview.vresolu) |
372 |
> |
if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu |
373 |
> |
|| pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu) |
374 |
|
continue; |
375 |
< |
/* check current value at pos */ |
376 |
< |
xpos = pos[0]; |
377 |
< |
ypos = pos[1]; |
378 |
< |
if (zscan(ypos)[xpos] <= 0.0 |
379 |
< |
|| zscan(ypos)[xpos] - pos[2] |
380 |
< |
> zeps*zscan(ypos)[xpos]) { |
381 |
< |
zscan(ypos)[xpos] = pos[2]; |
382 |
< |
copycolr(pscan(ypos)[xpos], pline[x]); |
383 |
< |
} |
375 |
> |
/* add pixel to our image */ |
376 |
> |
zt = 2.*zeps*zline[x]; |
377 |
> |
addpixel(xpos, ypos, |
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; |
385 |
> |
lastxz = lastyz[x] = zline[x]; |
386 |
|
} |
387 |
|
} |
388 |
|
|
389 |
|
|
390 |
+ |
addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */ |
391 |
+ |
int xstart, ystart; |
392 |
+ |
int width, height; |
393 |
+ |
COLR pix; |
394 |
+ |
double z; |
395 |
+ |
{ |
396 |
+ |
register int x, y; |
397 |
+ |
/* make width and height positive */ |
398 |
+ |
if (width < 0) { |
399 |
+ |
width = -width; |
400 |
+ |
xstart = xstart-width+1; |
401 |
+ |
} else if (width == 0) |
402 |
+ |
width = 1; |
403 |
+ |
if (height < 0) { |
404 |
+ |
height = -height; |
405 |
+ |
ystart = ystart-height+1; |
406 |
+ |
} else if (height == 0) |
407 |
+ |
height = 1; |
408 |
+ |
/* fill pixel(s) within rectangle */ |
409 |
+ |
for (y = ystart; y < ystart+height; y++) |
410 |
+ |
for (x = xstart; x < xstart+width; x++) |
411 |
+ |
if (zscan(y)[x] <= 0 |
412 |
+ |
|| zscan(y)[x]-z > zeps*zscan(y)[x]) { |
413 |
+ |
zscan(y)[x] = z; |
414 |
+ |
copycolr(pscan(y)[x], pix); |
415 |
+ |
} |
416 |
+ |
} |
417 |
+ |
|
418 |
+ |
|
419 |
|
fillpicture() /* fill in empty spaces */ |
420 |
|
{ |
421 |
|
int *yback, xback; |
440 |
|
for (y = 0; y < ourview.vresolu; y++) { |
441 |
|
xback = -2; |
442 |
|
for (x = 0; x < ourview.hresolu; x++) |
443 |
< |
if (zscan(y)[x] <= 0.0) { /* empty pixel */ |
443 |
> |
if (zscan(y)[x] <= 0) { /* empty pixel */ |
444 |
|
/* |
445 |
|
* First, find background from above or below. |
446 |
|
* (farthest assigned pixel) |
447 |
|
*/ |
448 |
|
if (yback[x] == -2) { |
449 |
|
for (i = y+1; i < ourview.vresolu; i++) |
450 |
< |
if (zscan(i)[x] > 0.0) |
450 |
> |
if (zscan(i)[x] > 0) |
451 |
|
break; |
452 |
|
if (i < ourview.vresolu |
453 |
|
&& (y <= 0 || zscan(y-1)[x] < zscan(i)[x])) |
459 |
|
* Next, find background from left or right. |
460 |
|
*/ |
461 |
|
if (xback == -2) { |
462 |
< |
for (i = x+1; x < ourview.hresolu; i++) |
463 |
< |
if (zscan(y)[i] > 0.0) |
462 |
> |
for (i = x+1; i < ourview.hresolu; i++) |
463 |
> |
if (zscan(y)[i] > 0) |
464 |
|
break; |
465 |
|
if (i < ourview.hresolu |
466 |
|
&& (x <= 0 || zscan(y)[x-1] < zscan(y)[i])) |
468 |
|
else |
469 |
|
xback = x-1; |
470 |
|
} |
367 |
– |
if (xback < 0 && yback[x] < 0) |
368 |
– |
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 |
|
} |