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