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