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 PACKSIZ 42 /* calculation packet size */ |
24 |
+ |
|
25 |
+ |
#define RTCOM "rtrace -h -ovl -fff -x %d %s" |
26 |
+ |
|
27 |
+ |
#define ABS(x) ((x)>0?(x):-(x)) |
28 |
+ |
|
29 |
+ |
struct position {int x,y; float z;}; |
30 |
+ |
|
31 |
|
VIEW ourview = STDVIEW(512); /* desired view */ |
32 |
|
|
33 |
< |
double zeps = 0.001; /* allowed z epsilon */ |
33 |
> |
double zeps = .02; /* allowed z epsilon */ |
34 |
|
|
35 |
|
COLR *ourpict; /* output picture */ |
36 |
|
float *ourzbuf; /* corresponding z-buffer */ |
37 |
|
|
38 |
|
char *progname; |
39 |
|
|
40 |
+ |
int fill = F_FORE|F_BACK; /* selected fill algorithm */ |
41 |
+ |
extern int backfill(), rcalfill(); /* fill functions */ |
42 |
+ |
int (*deffill)() = backfill; /* selected fill function */ |
43 |
+ |
COLR backcolr = BLKCOLR; /* background color */ |
44 |
+ |
double backz = 0.0; /* background z value */ |
45 |
+ |
int normdist = 1; /* normalized distance? */ |
46 |
+ |
double ourexp = -1; /* output picture exposure */ |
47 |
+ |
|
48 |
|
VIEW theirview = STDVIEW(512); /* input view */ |
49 |
|
int gotview; /* got input view? */ |
50 |
+ |
double theirs2ours[4][4]; /* transformation matrix */ |
51 |
+ |
double theirexp; /* input picture exposure */ |
52 |
|
|
53 |
+ |
int childpid = -1; /* id of fill process */ |
54 |
+ |
FILE *psend, *precv; /* pipes to/from fill calculation */ |
55 |
+ |
int queue[PACKSIZ][2]; /* pending pixels */ |
56 |
+ |
int queuesiz; /* number of pixels pending */ |
57 |
|
|
58 |
+ |
|
59 |
|
main(argc, argv) /* interpolate pictures */ |
60 |
|
int argc; |
61 |
|
char *argv[]; |
62 |
|
{ |
63 |
|
#define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt |
64 |
+ |
extern double atof(); |
65 |
|
int gotvfile = 0; |
66 |
+ |
char *zfile = NULL; |
67 |
|
char *err; |
68 |
|
int i; |
69 |
|
|
75 |
|
check(2,1); |
76 |
|
zeps = atof(argv[++i]); |
77 |
|
break; |
78 |
+ |
case 'n': /* dist. normalized? */ |
79 |
+ |
check(2,0); |
80 |
+ |
normdist = !normdist; |
81 |
+ |
break; |
82 |
+ |
case 'f': /* fill type */ |
83 |
+ |
switch (argv[i][2]) { |
84 |
+ |
case '0': /* none */ |
85 |
+ |
check(3,0); |
86 |
+ |
fill = 0; |
87 |
+ |
break; |
88 |
+ |
case 'f': /* foreground */ |
89 |
+ |
check(3,0); |
90 |
+ |
fill = F_FORE; |
91 |
+ |
break; |
92 |
+ |
case 'b': /* background */ |
93 |
+ |
check(3,0); |
94 |
+ |
fill = F_BACK; |
95 |
+ |
break; |
96 |
+ |
case 'a': /* all */ |
97 |
+ |
check(3,0); |
98 |
+ |
fill = F_FORE|F_BACK; |
99 |
+ |
break; |
100 |
+ |
case 'c': /* color */ |
101 |
+ |
check(3,3); |
102 |
+ |
deffill = backfill; |
103 |
+ |
setcolr(backcolr, atof(argv[i+1]), |
104 |
+ |
atof(argv[i+2]), atof(argv[i+3])); |
105 |
+ |
i += 3; |
106 |
+ |
break; |
107 |
+ |
case 'z': /* z value */ |
108 |
+ |
check(3,1); |
109 |
+ |
deffill = backfill; |
110 |
+ |
backz = atof(argv[++i]); |
111 |
+ |
break; |
112 |
+ |
case 'r': /* rtrace */ |
113 |
+ |
check(3,1); |
114 |
+ |
deffill = rcalfill; |
115 |
+ |
calstart(RTCOM, argv[++i]); |
116 |
+ |
break; |
117 |
+ |
default: |
118 |
+ |
goto badopt; |
119 |
+ |
} |
120 |
+ |
break; |
121 |
+ |
case 'z': /* z file */ |
122 |
+ |
check(2,1); |
123 |
+ |
zfile = argv[++i]; |
124 |
+ |
break; |
125 |
+ |
case 'x': /* x resolution */ |
126 |
+ |
check(2,1); |
127 |
+ |
ourview.hresolu = atoi(argv[++i]); |
128 |
+ |
break; |
129 |
+ |
case 'y': /* y resolution */ |
130 |
+ |
check(2,1); |
131 |
+ |
ourview.vresolu = atoi(argv[++i]); |
132 |
+ |
break; |
133 |
|
case 'v': /* view */ |
134 |
|
switch (argv[i][2]) { |
135 |
|
case 't': /* type */ |
180 |
|
break; |
181 |
|
default: |
182 |
|
badopt: |
183 |
< |
fprintf(stderr, "%s: unknown option '%s'\n", |
183 |
> |
fprintf(stderr, "%s: command line error at '%s'\n", |
184 |
|
progname, argv[i]); |
185 |
< |
exit(1); |
185 |
> |
goto userr; |
186 |
|
} |
187 |
|
/* check arguments */ |
188 |
< |
if (argc-i < 2 || (argc-i)%2) { |
189 |
< |
fprintf(stderr, "Usage: %s [view args] pfile zfile ..\n", |
107 |
< |
progname); |
108 |
< |
exit(1); |
109 |
< |
} |
188 |
> |
if ((argc-i)%2) |
189 |
> |
goto userr; |
190 |
|
/* set view */ |
191 |
|
if (err = setview(&ourview)) { |
192 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
193 |
|
exit(1); |
194 |
|
} |
195 |
|
/* allocate frame */ |
196 |
< |
ourpict = (COLR *)calloc(ourview.hresolu*ourview.vresolu,sizeof(COLR)); |
196 |
> |
ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR)); |
197 |
|
ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float)); |
198 |
< |
if (ourpict == NULL || ourzbuf == NULL) { |
199 |
< |
perror(progname); |
120 |
< |
exit(1); |
121 |
< |
} |
198 |
> |
if (ourpict == NULL || ourzbuf == NULL) |
199 |
> |
syserror(); |
200 |
|
/* get input */ |
201 |
|
for ( ; i < argc; i += 2) |
202 |
|
addpicture(argv[i], argv[i+1]); |
203 |
|
/* fill in spaces */ |
204 |
< |
fillpicture(); |
204 |
> |
if (fill&F_BACK) |
205 |
> |
backpicture(); |
206 |
> |
else |
207 |
> |
fillpicture(); |
208 |
> |
/* close calculation */ |
209 |
> |
caldone(); |
210 |
|
/* add to header */ |
211 |
|
printargs(argc, argv, stdout); |
212 |
|
if (gotvfile) { |
214 |
|
fprintview(&ourview, stdout); |
215 |
|
printf("\n"); |
216 |
|
} |
217 |
+ |
if (ourexp > 0 && ourexp != 1.0) |
218 |
+ |
fputexpos(ourexp, stdout); |
219 |
|
printf("\n"); |
220 |
< |
/* write output */ |
220 |
> |
/* write picture */ |
221 |
|
writepicture(); |
222 |
+ |
/* write z file */ |
223 |
+ |
if (zfile != NULL) |
224 |
+ |
writedistance(zfile); |
225 |
|
|
226 |
|
exit(0); |
227 |
+ |
userr: |
228 |
+ |
fprintf(stderr, |
229 |
+ |
"Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n", |
230 |
+ |
progname); |
231 |
+ |
exit(1); |
232 |
|
#undef check |
233 |
|
} |
234 |
|
|
241 |
|
|
242 |
|
printf("\t%s", s); |
243 |
|
|
244 |
+ |
if (isexpos(s)) { |
245 |
+ |
theirexp *= exposval(s); |
246 |
+ |
return; |
247 |
+ |
} |
248 |
|
for (an = altname; *an != NULL; an++) |
249 |
|
if (!strncmp(*an, s, strlen(*an))) { |
250 |
|
if (sscanview(&theirview, s+strlen(*an)) == 0) |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
< |
addpicture(pfile, zfile) /* add picture to output */ |
258 |
< |
char *pfile, *zfile; |
257 |
> |
addpicture(pfile, zspec) /* add picture to output */ |
258 |
> |
char *pfile, *zspec; |
259 |
|
{ |
260 |
+ |
extern double atof(); |
261 |
|
FILE *pfp, *zfp; |
262 |
+ |
char *err; |
263 |
|
COLR *scanin; |
264 |
|
float *zin; |
265 |
< |
char *err; |
167 |
< |
int xres, yres; |
265 |
> |
struct position *plast; |
266 |
|
int y; |
267 |
< |
/* open input files */ |
267 |
> |
/* open picture file */ |
268 |
|
if ((pfp = fopen(pfile, "r")) == NULL) { |
269 |
|
perror(pfile); |
270 |
|
exit(1); |
271 |
|
} |
272 |
< |
if ((zfp = fopen(zfile, "r")) == NULL) { |
273 |
< |
perror(zfile); |
176 |
< |
exit(1); |
177 |
< |
} |
178 |
< |
/* get header and view */ |
179 |
< |
printf("%s:\n", pfile); |
272 |
> |
/* get header with exposure and view */ |
273 |
> |
theirexp = 1.0; |
274 |
|
gotview = 0; |
275 |
+ |
printf("%s:\n", pfile); |
276 |
|
getheader(pfp, headline); |
277 |
< |
if (!gotview || fgetresolu(&xres, &yres, pfp) != (YMAJOR|YDECR)) { |
277 |
> |
if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp) |
278 |
> |
!= (YMAJOR|YDECR)) { |
279 |
|
fprintf(stderr, "%s: picture view error\n", pfile); |
280 |
|
exit(1); |
281 |
|
} |
282 |
< |
theirview.hresolu = xres; |
283 |
< |
theirview.vresolu = yres; |
282 |
> |
if (ourexp <= 0) |
283 |
> |
ourexp = theirexp; |
284 |
> |
else if (ABS(theirexp-ourexp) > .01*ourexp) |
285 |
> |
fprintf(stderr, "%s: different exposure (warning)\n", pfile); |
286 |
|
if (err = setview(&theirview)) { |
287 |
|
fprintf(stderr, "%s: %s\n", pfile, err); |
288 |
|
exit(1); |
289 |
|
} |
290 |
+ |
/* compute transformation */ |
291 |
+ |
pixform(theirs2ours, &theirview, &ourview); |
292 |
|
/* allocate scanlines */ |
293 |
< |
scanin = (COLR *)malloc(xres*sizeof(COLR)); |
294 |
< |
zin = (float *)malloc(xres*sizeof(float)); |
295 |
< |
if (scanin == NULL || zin == NULL) { |
296 |
< |
perror(progname); |
297 |
< |
exit(1); |
293 |
> |
scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR)); |
294 |
> |
zin = (float *)malloc(theirview.hresolu*sizeof(float)); |
295 |
> |
plast = (struct position *)calloc(theirview.hresolu, |
296 |
> |
sizeof(struct position)); |
297 |
> |
if (scanin == NULL || zin == NULL || plast == NULL) |
298 |
> |
syserror(); |
299 |
> |
/* get z specification or file */ |
300 |
> |
if ((zfp = fopen(zspec, "r")) == NULL) { |
301 |
> |
double zvalue; |
302 |
> |
register int x; |
303 |
> |
if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) { |
304 |
> |
perror(zspec); |
305 |
> |
exit(1); |
306 |
> |
} |
307 |
> |
for (x = 0; x < theirview.hresolu; x++) |
308 |
> |
zin[x] = zvalue; |
309 |
|
} |
310 |
|
/* load image */ |
311 |
< |
for (y = yres-1; y >= 0; y--) { |
312 |
< |
if (freadcolrs(scanin, xres, pfp) < 0) { |
311 |
> |
for (y = theirview.vresolu-1; y >= 0; y--) { |
312 |
> |
if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) { |
313 |
|
fprintf(stderr, "%s: read error\n", pfile); |
314 |
|
exit(1); |
315 |
|
} |
316 |
< |
if (fread(zin, sizeof(float), xres, zfp) < xres) { |
317 |
< |
fprintf(stderr, "%s: read error\n", zfile); |
316 |
> |
if (zfp != NULL |
317 |
> |
&& fread(zin,sizeof(float),theirview.hresolu,zfp) |
318 |
> |
< theirview.hresolu) { |
319 |
> |
fprintf(stderr, "%s: read error\n", zspec); |
320 |
|
exit(1); |
321 |
|
} |
322 |
< |
addscanline(y, scanin, zin); |
322 |
> |
addscanline(y, scanin, zin, plast); |
323 |
|
} |
324 |
|
/* clean up */ |
325 |
|
free((char *)scanin); |
326 |
|
free((char *)zin); |
327 |
+ |
free((char *)plast); |
328 |
|
fclose(pfp); |
329 |
< |
fclose(zfp); |
329 |
> |
if (zfp != NULL) |
330 |
> |
fclose(zfp); |
331 |
|
} |
332 |
|
|
333 |
|
|
334 |
< |
addscanline(y, pline, zline) /* add scanline to output */ |
334 |
> |
pixform(xfmat, vw1, vw2) /* compute view1 to view2 matrix */ |
335 |
> |
register double xfmat[4][4]; |
336 |
> |
register VIEW *vw1, *vw2; |
337 |
> |
{ |
338 |
> |
double m4t[4][4]; |
339 |
> |
|
340 |
> |
setident4(xfmat); |
341 |
> |
xfmat[0][0] = vw1->vhinc[0]; |
342 |
> |
xfmat[0][1] = vw1->vhinc[1]; |
343 |
> |
xfmat[0][2] = vw1->vhinc[2]; |
344 |
> |
xfmat[1][0] = vw1->vvinc[0]; |
345 |
> |
xfmat[1][1] = vw1->vvinc[1]; |
346 |
> |
xfmat[1][2] = vw1->vvinc[2]; |
347 |
> |
xfmat[2][0] = vw1->vdir[0]; |
348 |
> |
xfmat[2][1] = vw1->vdir[1]; |
349 |
> |
xfmat[2][2] = vw1->vdir[2]; |
350 |
> |
xfmat[3][0] = vw1->vp[0]; |
351 |
> |
xfmat[3][1] = vw1->vp[1]; |
352 |
> |
xfmat[3][2] = vw1->vp[2]; |
353 |
> |
setident4(m4t); |
354 |
> |
m4t[0][0] = vw2->vhinc[0]/vw2->vhn2; |
355 |
> |
m4t[1][0] = vw2->vhinc[1]/vw2->vhn2; |
356 |
> |
m4t[2][0] = vw2->vhinc[2]/vw2->vhn2; |
357 |
> |
m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2; |
358 |
> |
m4t[0][1] = vw2->vvinc[0]/vw2->vvn2; |
359 |
> |
m4t[1][1] = vw2->vvinc[1]/vw2->vvn2; |
360 |
> |
m4t[2][1] = vw2->vvinc[2]/vw2->vvn2; |
361 |
> |
m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2; |
362 |
> |
m4t[0][2] = vw2->vdir[0]; |
363 |
> |
m4t[1][2] = vw2->vdir[1]; |
364 |
> |
m4t[2][2] = vw2->vdir[2]; |
365 |
> |
m4t[3][2] = -DOT(vw2->vp,vw2->vdir); |
366 |
> |
multmat4(xfmat, xfmat, m4t); |
367 |
> |
} |
368 |
> |
|
369 |
> |
|
370 |
> |
addscanline(y, pline, zline, lasty) /* add scanline to output */ |
371 |
|
int y; |
372 |
|
COLR *pline; |
373 |
|
float *zline; |
374 |
+ |
struct position *lasty; /* input/output */ |
375 |
|
{ |
376 |
< |
FVECT p, dir; |
377 |
< |
double xnew, ynew, znew; |
376 |
> |
extern double sqrt(); |
377 |
> |
double pos[3]; |
378 |
> |
struct position lastx, newpos; |
379 |
|
register int x; |
227 |
– |
register int xpos, ypos; |
380 |
|
|
381 |
< |
for (x = 0; x < theirview.hresolu; x++) { |
382 |
< |
rayview(p, dir, &theirview, x+.5, y+.5); |
383 |
< |
p[0] += zline[x]*dir[0]; |
384 |
< |
p[1] += zline[x]*dir[1]; |
385 |
< |
p[2] += zline[x]*dir[2]; |
386 |
< |
pixelview(&xnew, &ynew, &znew, &ourview, p); |
387 |
< |
if (znew <= 0.0 || xnew < 0 || xnew > ourview.hresolu |
388 |
< |
|| ynew < 0 || ynew > ourview.vresolu) |
381 |
> |
for (x = theirview.hresolu-1; x >= 0; x--) { |
382 |
> |
pos[0] = x - .5*(theirview.hresolu-1); |
383 |
> |
pos[1] = y - .5*(theirview.vresolu-1); |
384 |
> |
pos[2] = zline[x]; |
385 |
> |
if (theirview.type == VT_PER) { |
386 |
> |
if (normdist) /* adjust for eye-ray distance */ |
387 |
> |
pos[2] /= sqrt( 1. |
388 |
> |
+ pos[0]*pos[0]*theirview.vhn2 |
389 |
> |
+ pos[1]*pos[1]*theirview.vvn2 ); |
390 |
> |
pos[0] *= pos[2]; |
391 |
> |
pos[1] *= pos[2]; |
392 |
> |
} |
393 |
> |
multp3(pos, pos, theirs2ours); |
394 |
> |
if (pos[2] <= 0) { |
395 |
> |
lasty[x].z = lastx.z = 0; /* mark invalid */ |
396 |
|
continue; |
238 |
– |
/* check current value at position */ |
239 |
– |
xpos = xnew; |
240 |
– |
ypos = ynew; |
241 |
– |
if (zscan(ypos)[xpos] <= 0.0 |
242 |
– |
|| zscan(ypos)[xpos] - znew |
243 |
– |
> zeps*zscan(ypos)[xpos]) { |
244 |
– |
zscan(ypos)[xpos] = znew; |
245 |
– |
copycolr(pscan(ypos)[xpos], pline[x]); |
397 |
|
} |
398 |
+ |
if (ourview.type == VT_PER) { |
399 |
+ |
pos[0] /= pos[2]; |
400 |
+ |
pos[1] /= pos[2]; |
401 |
+ |
} |
402 |
+ |
pos[0] += .5*ourview.hresolu; |
403 |
+ |
pos[1] += .5*ourview.vresolu; |
404 |
+ |
newpos.x = pos[0]; |
405 |
+ |
newpos.y = pos[1]; |
406 |
+ |
newpos.z = zline[x]; |
407 |
+ |
/* add pixel to our image */ |
408 |
+ |
if (pos[0] >= 0 && newpos.x < ourview.hresolu |
409 |
+ |
&& pos[1] >= 0 && newpos.y < ourview.vresolu) { |
410 |
+ |
addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]); |
411 |
+ |
lasty[x].x = lastx.x = newpos.x; |
412 |
+ |
lasty[x].y = lastx.y = newpos.y; |
413 |
+ |
lasty[x].z = lastx.z = newpos.z; |
414 |
+ |
} else |
415 |
+ |
lasty[x].z = lastx.z = 0; /* mark invalid */ |
416 |
|
} |
417 |
|
} |
418 |
|
|
419 |
|
|
420 |
< |
fillpicture() /* fill in empty spaces */ |
420 |
> |
addpixel(p0, p1, p2, pix, z) /* fill in pixel parallelogram */ |
421 |
> |
struct position *p0, *p1, *p2; |
422 |
> |
COLR pix; |
423 |
> |
double z; |
424 |
|
{ |
425 |
+ |
double zt = 2.*zeps*p0->z; /* threshold */ |
426 |
+ |
int s1x, s1y, s2x, s2y; /* step sizes */ |
427 |
+ |
int l1, l2, c1, c2; /* side lengths and counters */ |
428 |
+ |
int p1isy; /* p0p1 along y? */ |
429 |
+ |
int x1, y1; /* p1 position */ |
430 |
+ |
register int x, y; /* final position */ |
431 |
+ |
|
432 |
+ |
/* compute vector p0p1 */ |
433 |
+ |
if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) { |
434 |
+ |
s1x = p1->x - p0->x; |
435 |
+ |
s1y = p1->y - p0->y; |
436 |
+ |
l1 = ABS(s1x); |
437 |
+ |
if (p1isy = (ABS(s1y) > l1)) |
438 |
+ |
l1 = ABS(s1y); |
439 |
+ |
} else { |
440 |
+ |
l1 = s1x = s1y = 1; |
441 |
+ |
p1isy = -1; |
442 |
+ |
} |
443 |
+ |
/* compute vector p0p2 */ |
444 |
+ |
if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) { |
445 |
+ |
s2x = p2->x - p0->x; |
446 |
+ |
s2y = p2->y - p0->y; |
447 |
+ |
if (p1isy == 1) |
448 |
+ |
l2 = ABS(s2x); |
449 |
+ |
else { |
450 |
+ |
l2 = ABS(s2y); |
451 |
+ |
if (p1isy != 0 && ABS(s2x) > l2) |
452 |
+ |
l2 = ABS(s2x); |
453 |
+ |
} |
454 |
+ |
} else |
455 |
+ |
l2 = s2x = s2y = 1; |
456 |
+ |
/* fill the parallelogram */ |
457 |
+ |
for (c1 = l1; c1-- > 0; ) { |
458 |
+ |
x1 = p0->x + c1*s1x/l1; |
459 |
+ |
y1 = p0->y + c1*s1y/l1; |
460 |
+ |
for (c2 = l2; c2-- > 0; ) { |
461 |
+ |
x = x1 + c2*s2x/l2; |
462 |
+ |
y = y1 + c2*s2y/l2; |
463 |
+ |
if (zscan(y)[x] <= 0 || zscan(y)[x]-z |
464 |
+ |
> zeps*zscan(y)[x]) { |
465 |
+ |
zscan(y)[x] = z; |
466 |
+ |
copycolr(pscan(y)[x], pix); |
467 |
+ |
} |
468 |
+ |
} |
469 |
+ |
} |
470 |
+ |
} |
471 |
+ |
|
472 |
+ |
|
473 |
+ |
backpicture() /* background fill algorithm */ |
474 |
+ |
{ |
475 |
+ |
int *yback, xback; |
476 |
|
int y; |
477 |
< |
COLR cfill; |
478 |
< |
register int x, xblank; |
479 |
< |
|
477 |
> |
COLR pfill; |
478 |
> |
register int x, i; |
479 |
> |
/* get back buffer */ |
480 |
> |
yback = (int *)malloc(ourview.hresolu*sizeof(int)); |
481 |
> |
if (yback == NULL) |
482 |
> |
syserror(); |
483 |
> |
for (x = 0; x < ourview.hresolu; x++) |
484 |
> |
yback[x] = -2; |
485 |
> |
/* |
486 |
> |
* Xback and yback are the pixel locations of suitable |
487 |
> |
* background values in each direction. |
488 |
> |
* A value of -2 means unassigned, and -1 means |
489 |
> |
* that there is no suitable background in this direction. |
490 |
> |
*/ |
491 |
> |
/* fill image */ |
492 |
|
for (y = 0; y < ourview.vresolu; y++) { |
493 |
< |
xblank = -1; |
493 |
> |
xback = -2; |
494 |
|
for (x = 0; x < ourview.hresolu; x++) |
495 |
< |
if (zscan(y)[x] <= 0.0) { |
496 |
< |
if (xblank < 0) |
497 |
< |
xblank = x; |
498 |
< |
} else if (xblank >= 0) { |
499 |
< |
if (xblank == 0 || zscan(y)[xblank-1] < zscan(y)[x]) |
500 |
< |
copycolr(cfill, pscan(y)[x]); |
501 |
< |
else |
502 |
< |
copycolr(cfill, pscan(y)[xblank-1]); |
503 |
< |
for ( ; xblank < x; xblank++) |
504 |
< |
copycolr(pscan(y)[xblank], cfill); |
505 |
< |
xblank = -1; |
495 |
> |
if (zscan(y)[x] <= 0) { /* empty pixel */ |
496 |
> |
/* |
497 |
> |
* First, find background from above or below. |
498 |
> |
* (farthest assigned pixel) |
499 |
> |
*/ |
500 |
> |
if (yback[x] == -2) { |
501 |
> |
for (i = y+1; i < ourview.vresolu; i++) |
502 |
> |
if (zscan(i)[x] > 0) |
503 |
> |
break; |
504 |
> |
if (i < ourview.vresolu |
505 |
> |
&& (y <= 0 || zscan(y-1)[x] < zscan(i)[x])) |
506 |
> |
yback[x] = i; |
507 |
> |
else |
508 |
> |
yback[x] = y-1; |
509 |
> |
} |
510 |
> |
/* |
511 |
> |
* Next, find background from left or right. |
512 |
> |
*/ |
513 |
> |
if (xback == -2) { |
514 |
> |
for (i = x+1; i < ourview.hresolu; i++) |
515 |
> |
if (zscan(y)[i] > 0) |
516 |
> |
break; |
517 |
> |
if (i < ourview.hresolu |
518 |
> |
&& (x <= 0 || zscan(y)[x-1] < zscan(y)[i])) |
519 |
> |
xback = i; |
520 |
> |
else |
521 |
> |
xback = x-1; |
522 |
> |
} |
523 |
> |
/* |
524 |
> |
* Check to see if we have no background for |
525 |
> |
* this pixel. If not, use background color. |
526 |
> |
*/ |
527 |
> |
if (xback < 0 && yback[x] < 0) { |
528 |
> |
(*deffill)(x,y); |
529 |
> |
continue; |
530 |
> |
} |
531 |
> |
/* |
532 |
> |
* Compare, and use the background that is |
533 |
> |
* farther, unless one of them is next to us. |
534 |
> |
*/ |
535 |
> |
if ( yback[x] < 0 |
536 |
> |
|| (xback >= 0 && ABS(x-xback) <= 1) |
537 |
> |
|| ( ABS(y-yback[x]) > 1 |
538 |
> |
&& zscan(yback[x])[x] |
539 |
> |
< zscan(y)[xback] ) ) { |
540 |
> |
copycolr(pscan(y)[x],pscan(y)[xback]); |
541 |
> |
zscan(y)[x] = zscan(y)[xback]; |
542 |
> |
} else { |
543 |
> |
copycolr(pscan(y)[x],pscan(yback[x])[x]); |
544 |
> |
zscan(y)[x] = zscan(yback[x])[x]; |
545 |
> |
} |
546 |
> |
} else { /* full pixel */ |
547 |
> |
yback[x] = -2; |
548 |
> |
xback = -2; |
549 |
|
} |
272 |
– |
if (xblank > 0) { |
273 |
– |
copycolr(cfill, pscan(y)[xblank-1]); |
274 |
– |
for ( ; xblank < ourview.hresolu; xblank++) |
275 |
– |
copycolr(pscan(y)[xblank], cfill); |
276 |
– |
} |
550 |
|
} |
551 |
+ |
free((char *)yback); |
552 |
|
} |
553 |
|
|
554 |
|
|
555 |
+ |
fillpicture() /* paint in empty pixels with default */ |
556 |
+ |
{ |
557 |
+ |
register int x, y; |
558 |
+ |
|
559 |
+ |
for (y = 0; y < ourview.vresolu; y++) |
560 |
+ |
for (x = 0; x < ourview.hresolu; x++) |
561 |
+ |
if (zscan(y)[x] <= 0) |
562 |
+ |
(*deffill)(x,y); |
563 |
+ |
} |
564 |
+ |
|
565 |
+ |
|
566 |
|
writepicture() /* write out picture */ |
567 |
|
{ |
568 |
|
int y; |
569 |
|
|
570 |
|
fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); |
571 |
|
for (y = ourview.vresolu-1; y >= 0; y--) |
572 |
< |
if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) { |
573 |
< |
perror(progname); |
572 |
> |
if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) |
573 |
> |
syserror(); |
574 |
> |
} |
575 |
> |
|
576 |
> |
|
577 |
> |
writedistance(fname) /* write out z file */ |
578 |
> |
char *fname; |
579 |
> |
{ |
580 |
> |
extern double sqrt(); |
581 |
> |
int donorm = normdist && ourview.type == VT_PER; |
582 |
> |
FILE *fp; |
583 |
> |
int y; |
584 |
> |
float *zout; |
585 |
> |
|
586 |
> |
if ((fp = fopen(fname, "w")) == NULL) { |
587 |
> |
perror(fname); |
588 |
> |
exit(1); |
589 |
> |
} |
590 |
> |
if (donorm |
591 |
> |
&& (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) |
592 |
> |
syserror(); |
593 |
> |
for (y = ourview.vresolu-1; y >= 0; y--) { |
594 |
> |
if (donorm) { |
595 |
> |
double vx, yzn2; |
596 |
> |
register int x; |
597 |
> |
yzn2 = y - .5*(ourview.vresolu-1); |
598 |
> |
yzn2 = 1. + yzn2*yzn2*ourview.vvn2; |
599 |
> |
for (x = 0; x < ourview.hresolu; x++) { |
600 |
> |
vx = x - .5*(ourview.hresolu-1); |
601 |
> |
zout[x] = zscan(y)[x] |
602 |
> |
* sqrt(vx*vx*ourview.vhn2 + yzn2); |
603 |
> |
} |
604 |
> |
} else |
605 |
> |
zout = zscan(y); |
606 |
> |
if (fwrite(zout, sizeof(float), ourview.hresolu, fp) |
607 |
> |
< ourview.hresolu) { |
608 |
> |
perror(fname); |
609 |
|
exit(1); |
610 |
|
} |
611 |
+ |
} |
612 |
+ |
if (donorm) |
613 |
+ |
free((char *)zout); |
614 |
+ |
fclose(fp); |
615 |
+ |
} |
616 |
+ |
|
617 |
+ |
|
618 |
+ |
isfloat(s) /* see if string is floating number */ |
619 |
+ |
register char *s; |
620 |
+ |
{ |
621 |
+ |
for ( ; *s; s++) |
622 |
+ |
if ((*s < '0' || *s > '9') && *s != '.' && *s != '-' |
623 |
+ |
&& *s != 'e' && *s != 'E' && *s != '+') |
624 |
+ |
return(0); |
625 |
+ |
return(1); |
626 |
+ |
} |
627 |
+ |
|
628 |
+ |
|
629 |
+ |
backfill(x, y) /* fill pixel with background */ |
630 |
+ |
int x, y; |
631 |
+ |
{ |
632 |
+ |
register BYTE *dest = pscan(y)[x]; |
633 |
+ |
|
634 |
+ |
copycolr(dest, backcolr); |
635 |
+ |
zscan(y)[x] = backz; |
636 |
+ |
} |
637 |
+ |
|
638 |
+ |
|
639 |
+ |
calstart(prog, args) /* start fill calculation */ |
640 |
+ |
char *prog, *args; |
641 |
+ |
{ |
642 |
+ |
char combuf[512]; |
643 |
+ |
int p0[2], p1[2]; |
644 |
+ |
|
645 |
+ |
if (childpid != -1) { |
646 |
+ |
fprintf(stderr, "%s: too many calculations\n", progname); |
647 |
+ |
exit(1); |
648 |
+ |
} |
649 |
+ |
sprintf(combuf, prog, PACKSIZ, args); |
650 |
+ |
if (pipe(p0) < 0 || pipe(p1) < 0) |
651 |
+ |
syserror(); |
652 |
+ |
if ((childpid = vfork()) == 0) { /* fork calculation */ |
653 |
+ |
close(p0[1]); |
654 |
+ |
close(p1[0]); |
655 |
+ |
if (p0[0] != 0) { |
656 |
+ |
dup2(p0[0], 0); |
657 |
+ |
close(p0[0]); |
658 |
+ |
} |
659 |
+ |
if (p1[1] != 1) { |
660 |
+ |
dup2(p1[1], 1); |
661 |
+ |
close(p1[1]); |
662 |
+ |
} |
663 |
+ |
execl("/bin/sh", "sh", "-c", combuf, 0); |
664 |
+ |
perror("/bin/sh"); |
665 |
+ |
_exit(127); |
666 |
+ |
} |
667 |
+ |
if (childpid == -1) |
668 |
+ |
syserror(); |
669 |
+ |
close(p0[0]); |
670 |
+ |
close(p1[1]); |
671 |
+ |
if ((psend = fdopen(p0[1], "w")) == NULL) |
672 |
+ |
syserror(); |
673 |
+ |
if ((precv = fdopen(p1[0], "r")) == NULL) |
674 |
+ |
syserror(); |
675 |
+ |
queuesiz = 0; |
676 |
+ |
} |
677 |
+ |
|
678 |
+ |
|
679 |
+ |
caldone() /* done with calculation */ |
680 |
+ |
{ |
681 |
+ |
int pid; |
682 |
+ |
|
683 |
+ |
if (childpid == -1) |
684 |
+ |
return; |
685 |
+ |
if (fclose(psend) == EOF) |
686 |
+ |
syserror(); |
687 |
+ |
clearqueue(); |
688 |
+ |
fclose(precv); |
689 |
+ |
while ((pid = wait(0)) != -1 && pid != childpid) |
690 |
+ |
; |
691 |
+ |
childpid = -1; |
692 |
+ |
} |
693 |
+ |
|
694 |
+ |
|
695 |
+ |
rcalfill(x, y) /* fill with ray-calculated pixel */ |
696 |
+ |
int x, y; |
697 |
+ |
{ |
698 |
+ |
FVECT orig, dir; |
699 |
+ |
float outbuf[6]; |
700 |
+ |
|
701 |
+ |
if (queuesiz >= PACKSIZ) { /* flush queue */ |
702 |
+ |
if (fflush(psend) == EOF) |
703 |
+ |
syserror(); |
704 |
+ |
clearqueue(); |
705 |
+ |
} |
706 |
+ |
/* send new ray */ |
707 |
+ |
rayview(orig, dir, &ourview, x+.5, y+.5); |
708 |
+ |
outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2]; |
709 |
+ |
outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2]; |
710 |
+ |
if (fwrite(outbuf, sizeof(float), 6, psend) < 6) |
711 |
+ |
syserror(); |
712 |
+ |
/* remember it */ |
713 |
+ |
queue[queuesiz][0] = x; |
714 |
+ |
queue[queuesiz][1] = y; |
715 |
+ |
queuesiz++; |
716 |
+ |
} |
717 |
+ |
|
718 |
+ |
|
719 |
+ |
clearqueue() /* get results from queue */ |
720 |
+ |
{ |
721 |
+ |
float inbuf[4]; |
722 |
+ |
register int i; |
723 |
+ |
|
724 |
+ |
for (i = 0; i < queuesiz; i++) { |
725 |
+ |
if (fread(inbuf, sizeof(float), 4, precv) < 4) { |
726 |
+ |
fprintf(stderr, "%s: read error in clearqueue\n", |
727 |
+ |
progname); |
728 |
+ |
exit(1); |
729 |
+ |
} |
730 |
+ |
if (ourexp > 0 && ourexp != 1.0) { |
731 |
+ |
inbuf[0] *= ourexp; |
732 |
+ |
inbuf[1] *= ourexp; |
733 |
+ |
inbuf[2] *= ourexp; |
734 |
+ |
} |
735 |
+ |
setcolr(pscan(queue[i][1])[queue[i][0]], |
736 |
+ |
inbuf[0], inbuf[1], inbuf[2]); |
737 |
+ |
zscan(queue[i][1])[queue[i][0]] = inbuf[3]; |
738 |
+ |
} |
739 |
+ |
queuesiz = 0; |
740 |
+ |
} |
741 |
+ |
|
742 |
+ |
|
743 |
+ |
syserror() /* report error and exit */ |
744 |
+ |
{ |
745 |
+ |
perror(progname); |
746 |
+ |
exit(1); |
747 |
|
} |