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