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 */ |
31 |
|
VIEW theirview = STDVIEW(512); /* input view */ |
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 */ |
39 |
|
int argc; |
40 |
|
char *argv[]; |
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", |
109 |
< |
progname); |
110 |
< |
exit(1); |
111 |
< |
} |
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; |
166 |
– |
COLR *scanin; |
167 |
– |
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 |
|
} |
176 |
– |
if ((zfp = fopen(zfile, "r")) == NULL) { |
177 |
– |
perror(zfile); |
178 |
– |
exit(1); |
179 |
– |
} |
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 |
|
} |
188 |
– |
theirview.hresolu = xres; |
189 |
– |
theirview.vresolu = yres; |
203 |
|
if (err = setview(&theirview)) { |
204 |
|
fprintf(stderr, "%s: %s\n", pfile, err); |
205 |
|
exit(1); |
206 |
|
} |
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 |
|
|
254 |
< |
addscanline(y, pline, zline) /* add scanline to output */ |
254 |
> |
pixform(xfmat, vw1, vw2) /* compute view1 to view2 matrix */ |
255 |
> |
register double xfmat[4][4]; |
256 |
> |
register VIEW *vw1, *vw2; |
257 |
> |
{ |
258 |
> |
double m4t[4][4]; |
259 |
> |
|
260 |
> |
setident4(xfmat); |
261 |
> |
xfmat[0][0] = vw1->vhinc[0]; |
262 |
> |
xfmat[0][1] = vw1->vhinc[1]; |
263 |
> |
xfmat[0][2] = vw1->vhinc[2]; |
264 |
> |
xfmat[1][0] = vw1->vvinc[0]; |
265 |
> |
xfmat[1][1] = vw1->vvinc[1]; |
266 |
> |
xfmat[1][2] = vw1->vvinc[2]; |
267 |
> |
xfmat[2][0] = vw1->vdir[0]; |
268 |
> |
xfmat[2][1] = vw1->vdir[1]; |
269 |
> |
xfmat[2][2] = vw1->vdir[2]; |
270 |
> |
xfmat[3][0] = vw1->vp[0]; |
271 |
> |
xfmat[3][1] = vw1->vp[1]; |
272 |
> |
xfmat[3][2] = vw1->vp[2]; |
273 |
> |
setident4(m4t); |
274 |
> |
m4t[0][0] = vw2->vhinc[0]/vw2->vhn2; |
275 |
> |
m4t[1][0] = vw2->vhinc[1]/vw2->vhn2; |
276 |
> |
m4t[2][0] = vw2->vhinc[2]/vw2->vhn2; |
277 |
> |
m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2; |
278 |
> |
m4t[0][1] = vw2->vvinc[0]/vw2->vvn2; |
279 |
> |
m4t[1][1] = vw2->vvinc[1]/vw2->vvn2; |
280 |
> |
m4t[2][1] = vw2->vvinc[2]/vw2->vvn2; |
281 |
> |
m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2; |
282 |
> |
m4t[0][2] = vw2->vdir[0]; |
283 |
> |
m4t[1][2] = vw2->vdir[1]; |
284 |
> |
m4t[2][2] = vw2->vdir[2]; |
285 |
> |
m4t[3][2] = -DOT(vw2->vp,vw2->vdir); |
286 |
> |
multmat4(xfmat, xfmat, m4t); |
287 |
> |
} |
288 |
> |
|
289 |
> |
|
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 |
< |
FVECT p, dir; |
298 |
< |
double xnew, ynew, znew; |
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; |
229 |
– |
register int xpos, ypos; |
304 |
|
|
305 |
< |
for (x = 0; x < theirview.hresolu; x++) { |
306 |
< |
rayview(p, dir, &theirview, x+.5, y+.5); |
307 |
< |
p[0] += zline[x]*dir[0]; |
308 |
< |
p[1] += zline[x]*dir[1]; |
309 |
< |
p[2] += zline[x]*dir[2]; |
310 |
< |
pixelview(&xnew, &ynew, &znew, &ourview, p); |
311 |
< |
if (znew <= 0.0 || xnew < 0 || xnew > ourview.hresolu |
312 |
< |
|| ynew < 0 || ynew > ourview.vresolu) |
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 |
> |
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) |
319 |
|
continue; |
320 |
< |
/* check current value at position */ |
321 |
< |
xpos = xnew; |
322 |
< |
ypos = ynew; |
243 |
< |
if (zscan(ypos)[xpos] <= 0.0 |
244 |
< |
|| zscan(ypos)[xpos] - znew |
245 |
< |
> zeps*zscan(ypos)[xpos]) { |
246 |
< |
zscan(ypos)[xpos] = znew; |
247 |
< |
copycolr(pscan(ypos)[xpos], pline[x]); |
320 |
> |
if (ourview.type == VT_PER) { |
321 |
> |
pos[0] /= pos[2]; |
322 |
> |
pos[1] /= pos[2]; |
323 |
|
} |
324 |
+ |
pos[0] += .5*ourview.hresolu; |
325 |
+ |
pos[1] += .5*ourview.vresolu; |
326 |
+ |
if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu |
327 |
+ |
|| pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu) |
328 |
+ |
continue; |
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; |
381 |
|
return; |
382 |
|
} |
383 |
|
for (x = 0; x < ourview.hresolu; x++) |
384 |
< |
yback[x] = -1; |
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++) |
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) { /* found hole */ |
396 |
< |
xback = x-1; |
397 |
< |
do { /* find boundary */ |
398 |
< |
if (yback[x] < 0) { |
399 |
< |
for (i = y+1; |
400 |
< |
i < ourview.vresolu; |
401 |
< |
i++) |
402 |
< |
if (zscan(i)[x] > 0.0) |
403 |
< |
break; |
404 |
< |
if (i < ourview.vresolu |
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) |
403 |
> |
break; |
404 |
> |
if (i < ourview.vresolu |
405 |
|
&& (y <= 0 || zscan(y-1)[x] < zscan(i)[x])) |
406 |
< |
yback[x] = i; |
407 |
< |
else |
408 |
< |
yback[x] = y-1; |
284 |
< |
} |
285 |
< |
} while (++x < ourview.hresolu |
286 |
< |
&& zscan(y)[x] <= 0.0); |
287 |
< |
i = xback; /* pick background */ |
288 |
< |
if (x < ourview.hresolu |
289 |
< |
&& (i < 0 || zscan(y)[i] < zscan(y)[x])) |
290 |
< |
xback = x; |
291 |
< |
/* fill hole */ |
292 |
< |
if (xback < 0) { |
293 |
< |
while (++i < x) |
294 |
< |
if (yback[i] >= 0) |
295 |
< |
copycolr(pscan(y)[i],pscan(yback[i])[i]); |
296 |
< |
} else { |
297 |
< |
while (++i < x) |
298 |
< |
if (yback[i] < 0 |
299 |
< |
|| ABS(i-xback) <= 1 || (ABS(y-yback[i]) > 1 |
300 |
< |
&& zscan(yback[i])[i] < zscan(y)[xback])) |
301 |
< |
copycolr(pscan(y)[i],pscan(y)[xback]); |
302 |
< |
else |
303 |
< |
copycolr(pscan(y)[i],pscan(yback[i])[i]); |
406 |
> |
yback[x] = i; |
407 |
> |
else |
408 |
> |
yback[x] = y-1; |
409 |
|
} |
410 |
< |
} else |
411 |
< |
yback[x] = -1; /* clear boundary */ |
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) |
416 |
> |
break; |
417 |
> |
if (i < ourview.hresolu |
418 |
> |
&& (x <= 0 || zscan(y)[x-1] < zscan(y)[i])) |
419 |
> |
xback = i; |
420 |
> |
else |
421 |
> |
xback = x-1; |
422 |
> |
} |
423 |
> |
if (xback < 0 && yback[x] < 0) |
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] )) |
432 |
> |
copycolr(pscan(y)[x],pscan(y)[xback]); |
433 |
> |
else |
434 |
> |
copycolr(pscan(y)[x],pscan(yback[x])[x]); |
435 |
> |
} else { /* full pixel */ |
436 |
> |
yback[x] = -2; |
437 |
> |
xback = -2; |
438 |
> |
} |
439 |
> |
} |
440 |
|
free((char *)yback); |
441 |
|
} |
442 |
|
|
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 |
|
} |