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