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