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 */ |
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 |
< |
VIEW ourview = STDVIEW(512); /* desired view */ |
29 |
> |
struct position {int x,y; float z;}; |
30 |
|
|
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 |
|
|
38 |
|
COLR *ourpict; /* output picture */ |
40 |
|
|
41 |
|
char *progname; |
42 |
|
|
43 |
< |
VIEW theirview = STDVIEW(512); /* input view */ |
44 |
< |
int gotview; /* got input view? */ |
43 |
> |
int fill = F_FORE|F_BACK; /* selected fill algorithm */ |
44 |
> |
extern int backfill(), rcalfill(); /* fill functions */ |
45 |
> |
int (*deffill)() = backfill; /* selected fill function */ |
46 |
> |
COLR backcolr = BLKCOLR; /* background color */ |
47 |
> |
double backz = 0.0; /* background z value */ |
48 |
> |
int normdist = 1; /* normalized distance? */ |
49 |
> |
double ourexp = -1; /* output picture exposure */ |
50 |
|
|
51 |
+ |
VIEW theirview = STDVIEW; /* input view */ |
52 |
+ |
int gotview; /* got input view? */ |
53 |
+ |
int thresolu, tvresolu; /* input resolution */ |
54 |
+ |
double theirexp; /* input picture exposure */ |
55 |
|
double theirs2ours[4][4]; /* transformation matrix */ |
35 |
– |
int regdist = 0; /* regular distance? */ |
56 |
|
|
57 |
+ |
int childpid = -1; /* id of fill process */ |
58 |
+ |
FILE *psend, *precv; /* pipes to/from fill calculation */ |
59 |
+ |
int queue[PACKSIZ][2]; /* pending pixels */ |
60 |
+ |
int queuesiz; /* number of pixels pending */ |
61 |
|
|
62 |
+ |
|
63 |
|
main(argc, argv) /* interpolate pictures */ |
64 |
|
int argc; |
65 |
|
char *argv[]; |
66 |
|
{ |
67 |
|
#define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt |
68 |
+ |
extern double atof(); |
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); |
85 |
|
zeps = atof(argv[++i]); |
86 |
|
break; |
87 |
< |
case 'r': /* regular distance */ |
87 |
> |
case 'n': /* dist. normalized? */ |
88 |
|
check(2,0); |
89 |
< |
regdist = !regdist; |
89 |
> |
normdist = !normdist; |
90 |
|
break; |
91 |
< |
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 */ |
91 |
> |
case 'f': /* fill type */ |
92 |
|
switch (argv[i][2]) { |
93 |
< |
case 't': /* type */ |
94 |
< |
check(4,0); |
95 |
< |
ourview.type = argv[i][3]; |
93 |
> |
case '0': /* none */ |
94 |
> |
check(3,0); |
95 |
> |
fill = 0; |
96 |
|
break; |
97 |
< |
case 'p': /* point */ |
98 |
< |
check(3,3); |
99 |
< |
ourview.vp[0] = atof(argv[++i]); |
76 |
< |
ourview.vp[1] = atof(argv[++i]); |
77 |
< |
ourview.vp[2] = atof(argv[++i]); |
97 |
> |
case 'f': /* foreground */ |
98 |
> |
check(3,0); |
99 |
> |
fill = F_FORE; |
100 |
|
break; |
101 |
< |
case 'd': /* direction */ |
102 |
< |
check(3,3); |
103 |
< |
ourview.vdir[0] = atof(argv[++i]); |
82 |
< |
ourview.vdir[1] = atof(argv[++i]); |
83 |
< |
ourview.vdir[2] = atof(argv[++i]); |
101 |
> |
case 'b': /* background */ |
102 |
> |
check(3,0); |
103 |
> |
fill = F_BACK; |
104 |
|
break; |
105 |
< |
case 'u': /* up */ |
105 |
> |
case 'a': /* all */ |
106 |
> |
check(3,0); |
107 |
> |
fill = F_FORE|F_BACK; |
108 |
> |
break; |
109 |
> |
case 'c': /* color */ |
110 |
|
check(3,3); |
111 |
< |
ourview.vup[0] = atof(argv[++i]); |
112 |
< |
ourview.vup[1] = atof(argv[++i]); |
113 |
< |
ourview.vup[2] = atof(argv[++i]); |
111 |
> |
deffill = backfill; |
112 |
> |
setcolr(backcolr, atof(argv[i+1]), |
113 |
> |
atof(argv[i+2]), atof(argv[i+3])); |
114 |
> |
i += 3; |
115 |
|
break; |
116 |
< |
case 'h': /* horizontal */ |
116 |
> |
case 'z': /* z value */ |
117 |
|
check(3,1); |
118 |
< |
ourview.horiz = atof(argv[++i]); |
118 |
> |
deffill = backfill; |
119 |
> |
backz = atof(argv[++i]); |
120 |
|
break; |
121 |
< |
case 'v': /* vertical */ |
121 |
> |
case 'r': /* rtrace */ |
122 |
|
check(3,1); |
123 |
< |
ourview.vert = atof(argv[++i]); |
123 |
> |
deffill = rcalfill; |
124 |
> |
calstart(RTCOM, argv[++i]); |
125 |
|
break; |
99 |
– |
case 'f': /* file */ |
100 |
– |
check(3,1); |
101 |
– |
gotvfile = viewfile(argv[++i], &ourview); |
102 |
– |
if (gotvfile < 0) { |
103 |
– |
perror(argv[i]); |
104 |
– |
exit(1); |
105 |
– |
} else if (gotvfile == 0) { |
106 |
– |
fprintf(stderr, "%s: bad view file\n", |
107 |
– |
argv[i]); |
108 |
– |
exit(1); |
109 |
– |
} |
110 |
– |
break; |
126 |
|
default: |
127 |
|
goto badopt; |
128 |
|
} |
129 |
|
break; |
130 |
+ |
case 'z': /* z file */ |
131 |
+ |
check(2,1); |
132 |
+ |
zfile = argv[++i]; |
133 |
+ |
break; |
134 |
+ |
case 'x': /* x resolution */ |
135 |
+ |
check(2,1); |
136 |
+ |
hresolu = atoi(argv[++i]); |
137 |
+ |
break; |
138 |
+ |
case 'y': /* y resolution */ |
139 |
+ |
check(2,1); |
140 |
+ |
vresolu = atoi(argv[++i]); |
141 |
+ |
break; |
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: |
161 |
|
badopt: |
162 |
|
fprintf(stderr, "%s: command line error at '%s'\n", |
163 |
|
progname, argv[i]); |
164 |
|
goto userr; |
165 |
|
} |
166 |
+ |
} |
167 |
|
/* check arguments */ |
168 |
< |
if (argc-i < 2 || (argc-i)%2) |
168 |
> |
if ((argc-i)%2) |
169 |
|
goto userr; |
170 |
|
/* set view */ |
171 |
|
if (err = setview(&ourview)) { |
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 *)calloc(ourview.hresolu*ourview.vresolu,sizeof(COLR)); |
178 |
< |
ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float)); |
179 |
< |
if (ourpict == NULL || ourzbuf == NULL) { |
180 |
< |
perror(progname); |
134 |
< |
exit(1); |
135 |
< |
} |
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 */ |
182 |
|
for ( ; i < argc; i += 2) |
183 |
|
addpicture(argv[i], argv[i+1]); |
184 |
|
/* fill in spaces */ |
185 |
< |
fillpicture(); |
185 |
> |
if (fill&F_BACK) |
186 |
> |
backpicture(); |
187 |
> |
else |
188 |
> |
fillpicture(); |
189 |
> |
/* close calculation */ |
190 |
> |
caldone(); |
191 |
|
/* add to header */ |
192 |
|
printargs(argc, argv, stdout); |
193 |
|
if (gotvfile) { |
195 |
|
fprintview(&ourview, stdout); |
196 |
|
printf("\n"); |
197 |
|
} |
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 output */ |
203 |
> |
/* write picture */ |
204 |
|
writepicture(); |
205 |
+ |
/* write z file */ |
206 |
+ |
if (zfile != NULL) |
207 |
+ |
writedistance(zfile); |
208 |
|
|
209 |
|
exit(0); |
210 |
|
userr: |
211 |
|
fprintf(stderr, |
212 |
< |
"Usage: %s [view opts][-t zthresh][-r] 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 |
224 |
|
|
225 |
|
printf("\t%s", s); |
226 |
|
|
227 |
+ |
if (isexpos(s)) { |
228 |
+ |
theirexp *= exposval(s); |
229 |
+ |
return; |
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 |
|
} |
244 |
|
FILE *pfp, *zfp; |
245 |
|
char *err; |
246 |
|
COLR *scanin; |
247 |
< |
float *zin, *zlast; |
248 |
< |
int *plast; |
247 |
> |
float *zin; |
248 |
> |
struct position *plast; |
249 |
|
int y; |
250 |
|
/* open picture file */ |
251 |
|
if ((pfp = fopen(pfile, "r")) == NULL) { |
252 |
|
perror(pfile); |
253 |
|
exit(1); |
254 |
|
} |
255 |
< |
/* get header and view */ |
256 |
< |
printf("%s:\n", pfile); |
255 |
> |
/* get header with exposure and view */ |
256 |
> |
theirexp = 1.0; |
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); |
264 |
|
} |
265 |
+ |
if (ourexp <= 0) |
266 |
+ |
ourexp = theirexp; |
267 |
+ |
else if (ABS(theirexp-ourexp) > .01*ourexp) |
268 |
+ |
fprintf(stderr, "%s: different exposure (warning)\n", pfile); |
269 |
|
if (err = setview(&theirview)) { |
270 |
|
fprintf(stderr, "%s: %s\n", pfile, err); |
271 |
|
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 = (int *)calloc(theirview.hresolu, sizeof(int)); |
279 |
< |
zlast = (float *)calloc(theirview.hresolu, sizeof(float)); |
280 |
< |
if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) { |
215 |
< |
perror(progname); |
216 |
< |
exit(1); |
217 |
< |
} |
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 */ |
282 |
|
if ((zfp = fopen(zspec, "r")) == NULL) { |
283 |
|
double zvalue; |
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) |
237 |
< |
< theirview.hresolu) { |
299 |
> |
&& fread(zin,sizeof(float),thresolu,zfp) < thresolu) { |
300 |
|
fprintf(stderr, "%s: read error\n", zspec); |
301 |
|
exit(1); |
302 |
|
} |
303 |
< |
addscanline(y, scanin, zin, plast, zlast); |
303 |
> |
addscanline(y, scanin, zin, plast); |
304 |
|
} |
305 |
|
/* clean up */ |
306 |
|
free((char *)scanin); |
307 |
|
free((char *)zin); |
308 |
|
free((char *)plast); |
247 |
– |
free((char *)zlast); |
309 |
|
fclose(pfp); |
310 |
|
if (zfp != NULL) |
311 |
|
fclose(zfp); |
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]; |
348 |
|
} |
349 |
|
|
350 |
|
|
351 |
< |
addscanline(y, pline, zline, lasty, lastyz) /* add scanline to output */ |
351 |
> |
addscanline(y, pline, zline, lasty) /* add scanline to output */ |
352 |
|
int y; |
353 |
|
COLR *pline; |
354 |
|
float *zline; |
355 |
< |
int *lasty; /* input/output */ |
295 |
< |
float *lastyz; /* input/output */ |
355 |
> |
struct position *lasty; /* input/output */ |
356 |
|
{ |
357 |
< |
extern double sqrt(), fabs(); |
357 |
> |
extern double sqrt(); |
358 |
|
double pos[3]; |
359 |
< |
int lastx = 0; |
300 |
< |
double lastxz = 0; |
301 |
< |
double zt; |
302 |
< |
int xpos, ypos; |
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 (!regdist) /* adjust for eye-ray distance */ |
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 |
|
} |
374 |
|
multp3(pos, pos, theirs2ours); |
375 |
< |
if (pos[2] <= 0) |
375 |
> |
if (pos[2] <= 0) { |
376 |
> |
lasty[x].z = lastx.z = 0; /* mark invalid */ |
377 |
|
continue; |
378 |
+ |
} |
379 |
|
if (ourview.type == VT_PER) { |
380 |
|
pos[0] /= pos[2]; |
381 |
|
pos[1] /= pos[2]; |
382 |
|
} |
383 |
< |
pos[0] += .5*ourview.hresolu; |
384 |
< |
pos[1] += .5*ourview.vresolu; |
385 |
< |
if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu |
386 |
< |
|| pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu) |
387 |
< |
continue; |
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 |
< |
zt = 2.*zeps*zline[x]; |
390 |
< |
addpixel(xpos, ypos, |
391 |
< |
(fabs(zline[x]-lastxz) <= zt) ? lastx - xpos : 1, |
392 |
< |
(fabs(zline[x]-lastyz[x]) <= zt) ? lasty[x] - ypos : 1, |
393 |
< |
pline[x], pos[2]); |
394 |
< |
lastx = xpos; |
395 |
< |
lasty[x] = ypos; |
396 |
< |
lastxz = lastyz[x] = zline[x]; |
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; |
394 |
> |
lasty[x].z = lastx.z = newpos.z; |
395 |
> |
} else |
396 |
> |
lasty[x].z = lastx.z = 0; /* mark invalid */ |
397 |
|
} |
398 |
|
} |
399 |
|
|
400 |
|
|
401 |
< |
addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */ |
402 |
< |
int xstart, ystart; |
344 |
< |
int width, height; |
401 |
> |
addpixel(p0, p1, p2, pix, z) /* fill in pixel parallelogram */ |
402 |
> |
struct position *p0, *p1, *p2; |
403 |
|
COLR pix; |
404 |
|
double z; |
405 |
|
{ |
406 |
< |
register int x, y; |
407 |
< |
/* make width and height positive */ |
408 |
< |
if (width < 0) { |
409 |
< |
width = -width; |
410 |
< |
xstart = xstart-width+1; |
411 |
< |
} else if (width == 0) |
412 |
< |
width = 1; |
413 |
< |
if (height < 0) { |
414 |
< |
height = -height; |
415 |
< |
ystart = ystart-height+1; |
416 |
< |
} else if (height == 0) |
417 |
< |
height = 1; |
418 |
< |
/* fill pixel(s) within rectangle */ |
419 |
< |
for (y = ystart; y < ystart+height; y++) |
420 |
< |
for (x = xstart; x < xstart+width; x++) |
421 |
< |
if (zscan(y)[x] <= 0 |
422 |
< |
|| zscan(y)[x]-z > zeps*zscan(y)[x]) { |
406 |
> |
double zt = 2.*zeps*p0->z; /* threshold */ |
407 |
> |
int s1x, s1y, s2x, s2y; /* step sizes */ |
408 |
> |
int l1, l2, c1, c2; /* side lengths and counters */ |
409 |
> |
int p1isy; /* p0p1 along y? */ |
410 |
> |
int x1, y1; /* p1 position */ |
411 |
> |
register int x, y; /* final position */ |
412 |
> |
|
413 |
> |
/* compute vector p0p1 */ |
414 |
> |
if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) { |
415 |
> |
s1x = p1->x - p0->x; |
416 |
> |
s1y = p1->y - p0->y; |
417 |
> |
l1 = ABS(s1x); |
418 |
> |
if (p1isy = (ABS(s1y) > l1)) |
419 |
> |
l1 = ABS(s1y); |
420 |
> |
} else { |
421 |
> |
l1 = s1x = s1y = 1; |
422 |
> |
p1isy = -1; |
423 |
> |
} |
424 |
> |
/* compute vector p0p2 */ |
425 |
> |
if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) { |
426 |
> |
s2x = p2->x - p0->x; |
427 |
> |
s2y = p2->y - p0->y; |
428 |
> |
if (p1isy == 1) |
429 |
> |
l2 = ABS(s2x); |
430 |
> |
else { |
431 |
> |
l2 = ABS(s2y); |
432 |
> |
if (p1isy != 0 && ABS(s2x) > l2) |
433 |
> |
l2 = ABS(s2x); |
434 |
> |
} |
435 |
> |
} else |
436 |
> |
l2 = s2x = s2y = 1; |
437 |
> |
/* fill the parallelogram */ |
438 |
> |
for (c1 = l1; c1-- > 0; ) { |
439 |
> |
x1 = p0->x + c1*s1x/l1; |
440 |
> |
y1 = p0->y + c1*s1y/l1; |
441 |
> |
for (c2 = l2; c2-- > 0; ) { |
442 |
> |
x = x1 + c2*s2x/l2; |
443 |
> |
y = y1 + c2*s2y/l2; |
444 |
> |
if (zscan(y)[x] <= 0 || zscan(y)[x]-z |
445 |
> |
> zeps*zscan(y)[x]) { |
446 |
|
zscan(y)[x] = z; |
447 |
|
copycolr(pscan(y)[x], pix); |
448 |
|
} |
449 |
+ |
} |
450 |
+ |
} |
451 |
|
} |
452 |
|
|
453 |
|
|
454 |
< |
fillpicture() /* fill in empty spaces */ |
454 |
> |
backpicture() /* background fill algorithm */ |
455 |
|
{ |
456 |
|
int *yback, xback; |
457 |
|
int y; |
458 |
|
COLR pfill; |
459 |
|
register int x, i; |
460 |
|
/* get back buffer */ |
461 |
< |
yback = (int *)malloc(ourview.hresolu*sizeof(int)); |
462 |
< |
if (yback == NULL) { |
463 |
< |
perror(progname); |
464 |
< |
return; |
382 |
< |
} |
383 |
< |
for (x = 0; x < ourview.hresolu; x++) |
461 |
> |
yback = (int *)malloc(hresolu*sizeof(int)); |
462 |
> |
if (yback == NULL) |
463 |
> |
syserror(); |
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; x < 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 |
502 |
|
xback = x-1; |
503 |
|
} |
423 |
– |
if (xback < 0 && yback[x] < 0) |
424 |
– |
continue; /* no background */ |
504 |
|
/* |
505 |
+ |
* Check to see if we have no background for |
506 |
+ |
* this pixel. If not, use background color. |
507 |
+ |
*/ |
508 |
+ |
if (xback < 0 && yback[x] < 0) { |
509 |
+ |
(*deffill)(x,y); |
510 |
+ |
continue; |
511 |
+ |
} |
512 |
+ |
/* |
513 |
|
* Compare, and use the background that is |
514 |
|
* farther, unless one of them is next to us. |
515 |
|
*/ |
516 |
< |
if (yback[x] < 0 || ABS(x-xback) <= 1 |
516 |
> |
if ( yback[x] < 0 |
517 |
> |
|| (xback >= 0 && ABS(x-xback) <= 1) |
518 |
|
|| ( ABS(y-yback[x]) > 1 |
519 |
< |
&& zscan(yback[x])[x] < zscan(y)[xback] )) |
519 |
> |
&& zscan(yback[x])[x] |
520 |
> |
< zscan(y)[xback] ) ) { |
521 |
|
copycolr(pscan(y)[x],pscan(y)[xback]); |
522 |
< |
else |
522 |
> |
zscan(y)[x] = zscan(y)[xback]; |
523 |
> |
} else { |
524 |
|
copycolr(pscan(y)[x],pscan(yback[x])[x]); |
525 |
+ |
zscan(y)[x] = zscan(yback[x])[x]; |
526 |
+ |
} |
527 |
|
} else { /* full pixel */ |
528 |
|
yback[x] = -2; |
529 |
|
xback = -2; |
533 |
|
} |
534 |
|
|
535 |
|
|
536 |
+ |
fillpicture() /* paint in empty pixels with default */ |
537 |
+ |
{ |
538 |
+ |
register int x, y; |
539 |
+ |
|
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 |
+ |
} |
545 |
+ |
|
546 |
+ |
|
547 |
|
writepicture() /* write out picture */ |
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) { |
554 |
< |
perror(progname); |
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 |
> |
|
557 |
> |
|
558 |
> |
writedistance(fname) /* write out z file */ |
559 |
> |
char *fname; |
560 |
> |
{ |
561 |
> |
extern double sqrt(); |
562 |
> |
int donorm = normdist && ourview.type == VT_PER; |
563 |
> |
FILE *fp; |
564 |
> |
int y; |
565 |
> |
float *zout; |
566 |
> |
|
567 |
> |
if ((fp = fopen(fname, "w")) == NULL) { |
568 |
> |
perror(fname); |
569 |
> |
exit(1); |
570 |
> |
} |
571 |
> |
if (donorm |
572 |
> |
&& (zout = (float *)malloc(hresolu*sizeof(float))) == NULL) |
573 |
> |
syserror(); |
574 |
> |
for (y = vresolu-1; y >= 0; y--) { |
575 |
> |
if (donorm) { |
576 |
> |
double vx, yzn2; |
577 |
> |
register int x; |
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.hn2 + yzn2); |
584 |
> |
} |
585 |
> |
} else |
586 |
> |
zout = zscan(y); |
587 |
> |
if (fwrite(zout, sizeof(float), hresolu, fp) < hresolu) { |
588 |
> |
perror(fname); |
589 |
|
exit(1); |
590 |
|
} |
591 |
+ |
} |
592 |
+ |
if (donorm) |
593 |
+ |
free((char *)zout); |
594 |
+ |
fclose(fp); |
595 |
|
} |
596 |
|
|
597 |
|
|
603 |
|
&& *s != 'e' && *s != 'E' && *s != '+') |
604 |
|
return(0); |
605 |
|
return(1); |
606 |
+ |
} |
607 |
+ |
|
608 |
+ |
|
609 |
+ |
backfill(x, y) /* fill pixel with background */ |
610 |
+ |
int x, y; |
611 |
+ |
{ |
612 |
+ |
register BYTE *dest = pscan(y)[x]; |
613 |
+ |
|
614 |
+ |
copycolr(dest, backcolr); |
615 |
+ |
zscan(y)[x] = backz; |
616 |
+ |
} |
617 |
+ |
|
618 |
+ |
|
619 |
+ |
calstart(prog, args) /* start fill calculation */ |
620 |
+ |
char *prog, *args; |
621 |
+ |
{ |
622 |
+ |
char combuf[512]; |
623 |
+ |
int p0[2], p1[2]; |
624 |
+ |
|
625 |
+ |
if (childpid != -1) { |
626 |
+ |
fprintf(stderr, "%s: too many calculations\n", progname); |
627 |
+ |
exit(1); |
628 |
+ |
} |
629 |
+ |
sprintf(combuf, prog, PACKSIZ, args); |
630 |
+ |
if (pipe(p0) < 0 || pipe(p1) < 0) |
631 |
+ |
syserror(); |
632 |
+ |
if ((childpid = vfork()) == 0) { /* fork calculation */ |
633 |
+ |
close(p0[1]); |
634 |
+ |
close(p1[0]); |
635 |
+ |
if (p0[0] != 0) { |
636 |
+ |
dup2(p0[0], 0); |
637 |
+ |
close(p0[0]); |
638 |
+ |
} |
639 |
+ |
if (p1[1] != 1) { |
640 |
+ |
dup2(p1[1], 1); |
641 |
+ |
close(p1[1]); |
642 |
+ |
} |
643 |
+ |
execl("/bin/sh", "sh", "-c", combuf, 0); |
644 |
+ |
perror("/bin/sh"); |
645 |
+ |
_exit(127); |
646 |
+ |
} |
647 |
+ |
if (childpid == -1) |
648 |
+ |
syserror(); |
649 |
+ |
close(p0[0]); |
650 |
+ |
close(p1[1]); |
651 |
+ |
if ((psend = fdopen(p0[1], "w")) == NULL) |
652 |
+ |
syserror(); |
653 |
+ |
if ((precv = fdopen(p1[0], "r")) == NULL) |
654 |
+ |
syserror(); |
655 |
+ |
queuesiz = 0; |
656 |
+ |
} |
657 |
+ |
|
658 |
+ |
|
659 |
+ |
caldone() /* done with calculation */ |
660 |
+ |
{ |
661 |
+ |
int pid; |
662 |
+ |
|
663 |
+ |
if (childpid == -1) |
664 |
+ |
return; |
665 |
+ |
if (fclose(psend) == EOF) |
666 |
+ |
syserror(); |
667 |
+ |
clearqueue(); |
668 |
+ |
fclose(precv); |
669 |
+ |
while ((pid = wait(0)) != -1 && pid != childpid) |
670 |
+ |
; |
671 |
+ |
childpid = -1; |
672 |
+ |
} |
673 |
+ |
|
674 |
+ |
|
675 |
+ |
rcalfill(x, y) /* fill with ray-calculated pixel */ |
676 |
+ |
int x, y; |
677 |
+ |
{ |
678 |
+ |
FVECT orig, dir; |
679 |
+ |
float outbuf[6]; |
680 |
+ |
|
681 |
+ |
if (queuesiz >= PACKSIZ) { /* flush queue */ |
682 |
+ |
if (fflush(psend) == EOF) |
683 |
+ |
syserror(); |
684 |
+ |
clearqueue(); |
685 |
+ |
} |
686 |
+ |
/* send new ray */ |
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) |
691 |
+ |
syserror(); |
692 |
+ |
/* remember it */ |
693 |
+ |
queue[queuesiz][0] = x; |
694 |
+ |
queue[queuesiz][1] = y; |
695 |
+ |
queuesiz++; |
696 |
+ |
} |
697 |
+ |
|
698 |
+ |
|
699 |
+ |
clearqueue() /* get results from queue */ |
700 |
+ |
{ |
701 |
+ |
float inbuf[4]; |
702 |
+ |
register int i; |
703 |
+ |
|
704 |
+ |
for (i = 0; i < queuesiz; i++) { |
705 |
+ |
if (fread(inbuf, sizeof(float), 4, precv) < 4) { |
706 |
+ |
fprintf(stderr, "%s: read error in clearqueue\n", |
707 |
+ |
progname); |
708 |
+ |
exit(1); |
709 |
+ |
} |
710 |
+ |
if (ourexp > 0 && ourexp != 1.0) { |
711 |
+ |
inbuf[0] *= ourexp; |
712 |
+ |
inbuf[1] *= ourexp; |
713 |
+ |
inbuf[2] *= ourexp; |
714 |
+ |
} |
715 |
+ |
setcolr(pscan(queue[i][1])[queue[i][0]], |
716 |
+ |
inbuf[0], inbuf[1], inbuf[2]); |
717 |
+ |
zscan(queue[i][1])[queue[i][0]] = inbuf[3]; |
718 |
+ |
} |
719 |
+ |
queuesiz = 0; |
720 |
+ |
} |
721 |
+ |
|
722 |
+ |
|
723 |
+ |
syserror() /* report error and exit */ |
724 |
+ |
{ |
725 |
+ |
perror(progname); |
726 |
+ |
exit(1); |
727 |
|
} |