14 |
|
|
15 |
|
#include "color.h" |
16 |
|
|
17 |
< |
#define pscan(y) (ourpict+(y)*ourview.hresolu) |
18 |
< |
#define zscan(y) (ourzbuf+(y)*ourview.hresolu) |
17 |
> |
#define pscan(y) (ourpict+(y)*hresolu) |
18 |
> |
#define zscan(y) (ourzbuf+(y)*hresolu) |
19 |
|
|
20 |
|
#define F_FORE 1 /* fill foreground */ |
21 |
|
#define F_BACK 2 /* fill background */ |
28 |
|
|
29 |
|
struct position {int x,y; float z;}; |
30 |
|
|
31 |
< |
VIEW ourview = STDVIEW(512); /* desired view */ |
31 |
> |
VIEW ourview = STDVIEW; /* desired view */ |
32 |
> |
int hresolu = 512; /* horizontal resolution */ |
33 |
> |
int vresolu = 512; /* vertical resolution */ |
34 |
> |
double pixaspect = 1.0; /* pixel aspect ratio */ |
35 |
|
|
36 |
|
double zeps = .02; /* allowed z epsilon */ |
37 |
|
|
48 |
|
int normdist = 1; /* normalized distance? */ |
49 |
|
double ourexp = -1; /* output picture exposure */ |
50 |
|
|
51 |
< |
VIEW theirview = STDVIEW(512); /* input view */ |
51 |
> |
VIEW theirview = STDVIEW; /* input view */ |
52 |
|
int gotview; /* got input view? */ |
53 |
< |
double theirs2ours[4][4]; /* transformation matrix */ |
53 |
> |
int thresolu, tvresolu; /* input resolution */ |
54 |
|
double theirexp; /* input picture exposure */ |
55 |
+ |
double theirs2ours[4][4]; /* transformation matrix */ |
56 |
|
|
57 |
|
int childpid = -1; /* id of fill process */ |
58 |
|
FILE *psend, *precv; /* pipes to/from fill calculation */ |
69 |
|
int gotvfile = 0; |
70 |
|
char *zfile = NULL; |
71 |
|
char *err; |
72 |
< |
int i; |
72 |
> |
int i, rval; |
73 |
|
|
74 |
|
progname = argv[0]; |
75 |
|
|
76 |
< |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
76 |
> |
for (i = 1; i < argc && argv[i][0] == '-'; i++) { |
77 |
> |
rval = getviewopt(&ourview, argc-i, argv+i); |
78 |
> |
if (rval >= 0) { |
79 |
> |
i += rval; |
80 |
> |
continue; |
81 |
> |
} |
82 |
|
switch (argv[i][1]) { |
83 |
|
case 't': /* threshold */ |
84 |
|
check(2,1); |
133 |
|
break; |
134 |
|
case 'x': /* x resolution */ |
135 |
|
check(2,1); |
136 |
< |
ourview.hresolu = atoi(argv[++i]); |
136 |
> |
hresolu = atoi(argv[++i]); |
137 |
|
break; |
138 |
|
case 'y': /* y resolution */ |
139 |
|
check(2,1); |
140 |
< |
ourview.vresolu = atoi(argv[++i]); |
140 |
> |
vresolu = atoi(argv[++i]); |
141 |
|
break; |
142 |
< |
case 'v': /* view */ |
143 |
< |
switch (argv[i][2]) { |
144 |
< |
case 't': /* type */ |
145 |
< |
check(4,0); |
146 |
< |
ourview.type = argv[i][3]; |
147 |
< |
break; |
139 |
< |
case 'p': /* point */ |
140 |
< |
check(3,3); |
141 |
< |
ourview.vp[0] = atof(argv[++i]); |
142 |
< |
ourview.vp[1] = atof(argv[++i]); |
143 |
< |
ourview.vp[2] = atof(argv[++i]); |
144 |
< |
break; |
145 |
< |
case 'd': /* direction */ |
146 |
< |
check(3,3); |
147 |
< |
ourview.vdir[0] = atof(argv[++i]); |
148 |
< |
ourview.vdir[1] = atof(argv[++i]); |
149 |
< |
ourview.vdir[2] = atof(argv[++i]); |
150 |
< |
break; |
151 |
< |
case 'u': /* up */ |
152 |
< |
check(3,3); |
153 |
< |
ourview.vup[0] = atof(argv[++i]); |
154 |
< |
ourview.vup[1] = atof(argv[++i]); |
155 |
< |
ourview.vup[2] = atof(argv[++i]); |
156 |
< |
break; |
157 |
< |
case 'h': /* horizontal */ |
158 |
< |
check(3,1); |
159 |
< |
ourview.horiz = atof(argv[++i]); |
160 |
< |
break; |
161 |
< |
case 'v': /* vertical */ |
162 |
< |
check(3,1); |
163 |
< |
ourview.vert = atof(argv[++i]); |
164 |
< |
break; |
165 |
< |
case 'f': /* file */ |
166 |
< |
check(3,1); |
167 |
< |
gotvfile = viewfile(argv[++i], &ourview); |
168 |
< |
if (gotvfile < 0) { |
169 |
< |
perror(argv[i]); |
170 |
< |
exit(1); |
171 |
< |
} else if (gotvfile == 0) { |
172 |
< |
fprintf(stderr, "%s: bad view file\n", |
173 |
< |
argv[i]); |
174 |
< |
exit(1); |
175 |
< |
} |
176 |
< |
break; |
177 |
< |
default: |
142 |
> |
case 'p': /* pixel aspect */ |
143 |
> |
check(2,1); |
144 |
> |
pixaspect = atof(argv[++i]); |
145 |
> |
break; |
146 |
> |
case 'v': /* view file */ |
147 |
> |
if (argv[i][2] != 'f') |
148 |
|
goto badopt; |
149 |
+ |
check(3,1); |
150 |
+ |
gotvfile = viewfile(argv[++i], &ourview); |
151 |
+ |
if (gotvfile < 0) { |
152 |
+ |
perror(argv[i]); |
153 |
+ |
exit(1); |
154 |
+ |
} else if (gotvfile == 0) { |
155 |
+ |
fprintf(stderr, "%s: bad view file\n", |
156 |
+ |
argv[i]); |
157 |
+ |
exit(1); |
158 |
|
} |
159 |
|
break; |
160 |
|
default: |
163 |
|
progname, argv[i]); |
164 |
|
goto userr; |
165 |
|
} |
166 |
+ |
} |
167 |
|
/* check arguments */ |
168 |
|
if ((argc-i)%2) |
169 |
|
goto userr; |
172 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
173 |
|
exit(1); |
174 |
|
} |
175 |
+ |
normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu); |
176 |
|
/* allocate frame */ |
177 |
< |
ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR)); |
178 |
< |
ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float)); |
177 |
> |
ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR)); |
178 |
> |
ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float)); |
179 |
|
if (ourpict == NULL || ourzbuf == NULL) |
180 |
|
syserror(); |
181 |
|
/* get input */ |
195 |
|
fprintview(&ourview, stdout); |
196 |
|
printf("\n"); |
197 |
|
} |
198 |
< |
if (ourexp > 0 && ourexp != 1.0) |
198 |
> |
if (pixaspect < .99 || pixaspect > 1.01) |
199 |
> |
fputaspect(pixaspect, stdout); |
200 |
> |
if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005)) |
201 |
|
fputexpos(ourexp, stdout); |
202 |
|
printf("\n"); |
203 |
|
/* write picture */ |
209 |
|
exit(0); |
210 |
|
userr: |
211 |
|
fprintf(stderr, |
212 |
< |
"Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n", |
212 |
> |
"Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n", |
213 |
|
progname); |
214 |
|
exit(1); |
215 |
|
#undef check |
230 |
|
} |
231 |
|
for (an = altname; *an != NULL; an++) |
232 |
|
if (!strncmp(*an, s, strlen(*an))) { |
233 |
< |
if (sscanview(&theirview, s+strlen(*an)) == 0) |
233 |
> |
if (sscanview(&theirview, s+strlen(*an)) > 0) |
234 |
|
gotview++; |
235 |
|
break; |
236 |
|
} |
257 |
|
gotview = 0; |
258 |
|
printf("%s:\n", pfile); |
259 |
|
getheader(pfp, headline); |
260 |
< |
if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp) |
260 |
> |
if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp) |
261 |
|
!= (YMAJOR|YDECR)) { |
262 |
|
fprintf(stderr, "%s: picture view error\n", pfile); |
263 |
|
exit(1); |
273 |
|
/* compute transformation */ |
274 |
|
pixform(theirs2ours, &theirview, &ourview); |
275 |
|
/* allocate scanlines */ |
276 |
< |
scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR)); |
277 |
< |
zin = (float *)malloc(theirview.hresolu*sizeof(float)); |
278 |
< |
plast = (struct position *)calloc(theirview.hresolu, |
296 |
< |
sizeof(struct position)); |
276 |
> |
scanin = (COLR *)malloc(thresolu*sizeof(COLR)); |
277 |
> |
zin = (float *)malloc(thresolu*sizeof(float)); |
278 |
> |
plast = (struct position *)calloc(thresolu, sizeof(struct position)); |
279 |
|
if (scanin == NULL || zin == NULL || plast == NULL) |
280 |
|
syserror(); |
281 |
|
/* get z specification or file */ |
286 |
|
perror(zspec); |
287 |
|
exit(1); |
288 |
|
} |
289 |
< |
for (x = 0; x < theirview.hresolu; x++) |
289 |
> |
for (x = 0; x < thresolu; x++) |
290 |
|
zin[x] = zvalue; |
291 |
|
} |
292 |
|
/* load image */ |
293 |
< |
for (y = theirview.vresolu-1; y >= 0; y--) { |
294 |
< |
if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) { |
293 |
> |
for (y = tvresolu-1; y >= 0; y--) { |
294 |
> |
if (freadcolrs(scanin, thresolu, pfp) < 0) { |
295 |
|
fprintf(stderr, "%s: read error\n", pfile); |
296 |
|
exit(1); |
297 |
|
} |
298 |
|
if (zfp != NULL |
299 |
< |
&& fread(zin,sizeof(float),theirview.hresolu,zfp) |
318 |
< |
< theirview.hresolu) { |
299 |
> |
&& fread(zin,sizeof(float),thresolu,zfp) < thresolu) { |
300 |
|
fprintf(stderr, "%s: read error\n", zspec); |
301 |
|
exit(1); |
302 |
|
} |
319 |
|
double m4t[4][4]; |
320 |
|
|
321 |
|
setident4(xfmat); |
322 |
< |
xfmat[0][0] = vw1->vhinc[0]; |
323 |
< |
xfmat[0][1] = vw1->vhinc[1]; |
324 |
< |
xfmat[0][2] = vw1->vhinc[2]; |
325 |
< |
xfmat[1][0] = vw1->vvinc[0]; |
326 |
< |
xfmat[1][1] = vw1->vvinc[1]; |
327 |
< |
xfmat[1][2] = vw1->vvinc[2]; |
322 |
> |
xfmat[0][0] = vw1->hvec[0]; |
323 |
> |
xfmat[0][1] = vw1->hvec[1]; |
324 |
> |
xfmat[0][2] = vw1->hvec[2]; |
325 |
> |
xfmat[1][0] = vw1->vvec[0]; |
326 |
> |
xfmat[1][1] = vw1->vvec[1]; |
327 |
> |
xfmat[1][2] = vw1->vvec[2]; |
328 |
|
xfmat[2][0] = vw1->vdir[0]; |
329 |
|
xfmat[2][1] = vw1->vdir[1]; |
330 |
|
xfmat[2][2] = vw1->vdir[2]; |
332 |
|
xfmat[3][1] = vw1->vp[1]; |
333 |
|
xfmat[3][2] = vw1->vp[2]; |
334 |
|
setident4(m4t); |
335 |
< |
m4t[0][0] = vw2->vhinc[0]/vw2->vhn2; |
336 |
< |
m4t[1][0] = vw2->vhinc[1]/vw2->vhn2; |
337 |
< |
m4t[2][0] = vw2->vhinc[2]/vw2->vhn2; |
338 |
< |
m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2; |
339 |
< |
m4t[0][1] = vw2->vvinc[0]/vw2->vvn2; |
340 |
< |
m4t[1][1] = vw2->vvinc[1]/vw2->vvn2; |
341 |
< |
m4t[2][1] = vw2->vvinc[2]/vw2->vvn2; |
342 |
< |
m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2; |
335 |
> |
m4t[0][0] = vw2->hvec[0]/vw2->hn2; |
336 |
> |
m4t[1][0] = vw2->hvec[1]/vw2->hn2; |
337 |
> |
m4t[2][0] = vw2->hvec[2]/vw2->hn2; |
338 |
> |
m4t[3][0] = -DOT(vw2->vp,vw2->hvec)/vw2->hn2; |
339 |
> |
m4t[0][1] = vw2->vvec[0]/vw2->vn2; |
340 |
> |
m4t[1][1] = vw2->vvec[1]/vw2->vn2; |
341 |
> |
m4t[2][1] = vw2->vvec[2]/vw2->vn2; |
342 |
> |
m4t[3][1] = -DOT(vw2->vp,vw2->vvec)/vw2->vn2; |
343 |
|
m4t[0][2] = vw2->vdir[0]; |
344 |
|
m4t[1][2] = vw2->vdir[1]; |
345 |
|
m4t[2][2] = vw2->vdir[2]; |
359 |
|
struct position lastx, newpos; |
360 |
|
register int x; |
361 |
|
|
362 |
< |
for (x = theirview.hresolu-1; x >= 0; x--) { |
363 |
< |
pos[0] = x - .5*(theirview.hresolu-1); |
364 |
< |
pos[1] = y - .5*(theirview.vresolu-1); |
362 |
> |
for (x = thresolu-1; x >= 0; x--) { |
363 |
> |
pos[0] = (x+.5)/thresolu + theirview.hoff - .5; |
364 |
> |
pos[1] = (y+.5)/tvresolu + theirview.voff - .5; |
365 |
|
pos[2] = zline[x]; |
366 |
|
if (theirview.type == VT_PER) { |
367 |
|
if (normdist) /* adjust for eye-ray distance */ |
368 |
|
pos[2] /= sqrt( 1. |
369 |
< |
+ pos[0]*pos[0]*theirview.vhn2 |
370 |
< |
+ pos[1]*pos[1]*theirview.vvn2 ); |
369 |
> |
+ pos[0]*pos[0]*theirview.hn2 |
370 |
> |
+ pos[1]*pos[1]*theirview.vn2 ); |
371 |
|
pos[0] *= pos[2]; |
372 |
|
pos[1] *= pos[2]; |
373 |
|
} |
380 |
|
pos[0] /= pos[2]; |
381 |
|
pos[1] /= pos[2]; |
382 |
|
} |
383 |
< |
pos[0] += .5*ourview.hresolu; |
384 |
< |
pos[1] += .5*ourview.vresolu; |
385 |
< |
newpos.x = pos[0]; |
386 |
< |
newpos.y = pos[1]; |
383 |
> |
pos[0] += .5 - ourview.hoff; |
384 |
> |
pos[1] += .5 - ourview.voff; |
385 |
> |
newpos.x = pos[0] * hresolu; |
386 |
> |
newpos.y = pos[1] * vresolu; |
387 |
|
newpos.z = zline[x]; |
388 |
|
/* add pixel to our image */ |
389 |
< |
if (pos[0] >= 0 && newpos.x < ourview.hresolu |
390 |
< |
&& pos[1] >= 0 && newpos.y < ourview.vresolu) { |
389 |
> |
if (pos[0] >= 0 && newpos.x < hresolu |
390 |
> |
&& pos[1] >= 0 && newpos.y < vresolu) { |
391 |
|
addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]); |
392 |
|
lasty[x].x = lastx.x = newpos.x; |
393 |
|
lasty[x].y = lastx.y = newpos.y; |
458 |
|
COLR pfill; |
459 |
|
register int x, i; |
460 |
|
/* get back buffer */ |
461 |
< |
yback = (int *)malloc(ourview.hresolu*sizeof(int)); |
461 |
> |
yback = (int *)malloc(hresolu*sizeof(int)); |
462 |
|
if (yback == NULL) |
463 |
|
syserror(); |
464 |
< |
for (x = 0; x < ourview.hresolu; x++) |
464 |
> |
for (x = 0; x < hresolu; x++) |
465 |
|
yback[x] = -2; |
466 |
|
/* |
467 |
|
* Xback and yback are the pixel locations of suitable |
470 |
|
* that there is no suitable background in this direction. |
471 |
|
*/ |
472 |
|
/* fill image */ |
473 |
< |
for (y = 0; y < ourview.vresolu; y++) { |
473 |
> |
for (y = 0; y < vresolu; y++) { |
474 |
|
xback = -2; |
475 |
< |
for (x = 0; x < ourview.hresolu; x++) |
475 |
> |
for (x = 0; x < hresolu; x++) |
476 |
|
if (zscan(y)[x] <= 0) { /* empty pixel */ |
477 |
|
/* |
478 |
|
* First, find background from above or below. |
479 |
|
* (farthest assigned pixel) |
480 |
|
*/ |
481 |
|
if (yback[x] == -2) { |
482 |
< |
for (i = y+1; i < ourview.vresolu; i++) |
482 |
> |
for (i = y+1; i < vresolu; i++) |
483 |
|
if (zscan(i)[x] > 0) |
484 |
|
break; |
485 |
< |
if (i < ourview.vresolu |
485 |
> |
if (i < vresolu |
486 |
|
&& (y <= 0 || zscan(y-1)[x] < zscan(i)[x])) |
487 |
|
yback[x] = i; |
488 |
|
else |
492 |
|
* Next, find background from left or right. |
493 |
|
*/ |
494 |
|
if (xback == -2) { |
495 |
< |
for (i = x+1; i < ourview.hresolu; i++) |
495 |
> |
for (i = x+1; i < hresolu; i++) |
496 |
|
if (zscan(y)[i] > 0) |
497 |
|
break; |
498 |
< |
if (i < ourview.hresolu |
498 |
> |
if (i < hresolu |
499 |
|
&& (x <= 0 || zscan(y)[x-1] < zscan(y)[i])) |
500 |
|
xback = i; |
501 |
|
else |
537 |
|
{ |
538 |
|
register int x, y; |
539 |
|
|
540 |
< |
for (y = 0; y < ourview.vresolu; y++) |
541 |
< |
for (x = 0; x < ourview.hresolu; x++) |
540 |
> |
for (y = 0; y < vresolu; y++) |
541 |
> |
for (x = 0; x < hresolu; x++) |
542 |
|
if (zscan(y)[x] <= 0) |
543 |
|
(*deffill)(x,y); |
544 |
|
} |
548 |
|
{ |
549 |
|
int y; |
550 |
|
|
551 |
< |
fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); |
552 |
< |
for (y = ourview.vresolu-1; y >= 0; y--) |
553 |
< |
if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) |
551 |
> |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
552 |
> |
for (y = vresolu-1; y >= 0; y--) |
553 |
> |
if (fwritecolrs(pscan(y), hresolu, stdout) < 0) |
554 |
|
syserror(); |
555 |
|
} |
556 |
|
|
569 |
|
exit(1); |
570 |
|
} |
571 |
|
if (donorm |
572 |
< |
&& (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) |
572 |
> |
&& (zout = (float *)malloc(hresolu*sizeof(float))) == NULL) |
573 |
|
syserror(); |
574 |
< |
for (y = ourview.vresolu-1; y >= 0; y--) { |
574 |
> |
for (y = vresolu-1; y >= 0; y--) { |
575 |
|
if (donorm) { |
576 |
|
double vx, yzn2; |
577 |
|
register int x; |
578 |
< |
yzn2 = y - .5*(ourview.vresolu-1); |
579 |
< |
yzn2 = 1. + yzn2*yzn2*ourview.vvn2; |
580 |
< |
for (x = 0; x < ourview.hresolu; x++) { |
581 |
< |
vx = x - .5*(ourview.hresolu-1); |
578 |
> |
yzn2 = y - .5*(vresolu-1); |
579 |
> |
yzn2 = 1. + yzn2*yzn2*ourview.vn2; |
580 |
> |
for (x = 0; x < hresolu; x++) { |
581 |
> |
vx = x - .5*(hresolu-1); |
582 |
|
zout[x] = zscan(y)[x] |
583 |
< |
* sqrt(vx*vx*ourview.vhn2 + yzn2); |
583 |
> |
* sqrt(vx*vx*ourview.hn2 + yzn2); |
584 |
|
} |
585 |
|
} else |
586 |
|
zout = zscan(y); |
587 |
< |
if (fwrite(zout, sizeof(float), ourview.hresolu, fp) |
607 |
< |
< ourview.hresolu) { |
587 |
> |
if (fwrite(zout, sizeof(float), hresolu, fp) < hresolu) { |
588 |
|
perror(fname); |
589 |
|
exit(1); |
590 |
|
} |
684 |
|
clearqueue(); |
685 |
|
} |
686 |
|
/* send new ray */ |
687 |
< |
rayview(orig, dir, &ourview, x+.5, y+.5); |
687 |
> |
viewray(orig, dir, &ourview, (x+.5)/hresolu, (y+.5)/vresolu); |
688 |
|
outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2]; |
689 |
|
outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2]; |
690 |
|
if (fwrite(outbuf, sizeof(float), 6, psend) < 6) |