14 |
|
|
15 |
|
#include "color.h" |
16 |
|
|
17 |
< |
#define pscan(y) (ourpict+(y)*ourview.hresolu) |
18 |
< |
#define zscan(y) (ourzbuf+(y)*ourview.hresolu) |
17 |
> |
#define pscan(y) (ourpict+(y)*hresolu) |
18 |
> |
#define zscan(y) (ourzbuf+(y)*hresolu) |
19 |
|
|
20 |
|
#define F_FORE 1 /* fill foreground */ |
21 |
|
#define F_BACK 2 /* fill background */ |
26 |
|
|
27 |
|
#define ABS(x) ((x)>0?(x):-(x)) |
28 |
|
|
29 |
< |
VIEW ourview = STDVIEW(512); /* desired view */ |
29 |
> |
struct position {int x,y; float z;}; |
30 |
|
|
31 |
+ |
VIEW ourview = STDVIEW; /* desired view */ |
32 |
+ |
int hresolu = 512; /* horizontal resolution */ |
33 |
+ |
int vresolu = 512; /* vertical resolution */ |
34 |
+ |
double pixaspect = 1.0; /* pixel aspect ratio */ |
35 |
+ |
|
36 |
|
double zeps = .02; /* allowed z epsilon */ |
37 |
|
|
38 |
|
COLR *ourpict; /* output picture */ |
40 |
|
|
41 |
|
char *progname; |
42 |
|
|
38 |
– |
VIEW theirview = STDVIEW(512); /* input view */ |
39 |
– |
int gotview; /* got input view? */ |
40 |
– |
|
43 |
|
int fill = F_FORE|F_BACK; /* selected fill algorithm */ |
44 |
< |
extern int backfill(), calfill(); /* fill functions */ |
44 |
> |
extern int backfill(), rcalfill(); /* fill functions */ |
45 |
|
int (*deffill)() = backfill; /* selected fill function */ |
46 |
|
COLR backcolr = BLKCOLR; /* background color */ |
47 |
|
double backz = 0.0; /* background z value */ |
48 |
+ |
int normdist = 1; /* normalized distance? */ |
49 |
+ |
double ourexp = -1; /* output picture exposure */ |
50 |
|
|
51 |
+ |
VIEW theirview = STDVIEW; /* input view */ |
52 |
+ |
int gotview; /* got input view? */ |
53 |
+ |
int thresolu, tvresolu; /* input resolution */ |
54 |
+ |
double theirexp; /* input picture exposure */ |
55 |
|
double theirs2ours[4][4]; /* transformation matrix */ |
48 |
– |
int normdist = 1; /* normalized distance? */ |
56 |
|
|
57 |
+ |
int childpid = -1; /* id of fill process */ |
58 |
|
FILE *psend, *precv; /* pipes to/from fill calculation */ |
51 |
– |
int childpid; /* child's process id */ |
59 |
|
int queue[PACKSIZ][2]; /* pending pixels */ |
60 |
|
int queuesiz; /* number of pixels pending */ |
61 |
|
|
69 |
|
int gotvfile = 0; |
70 |
|
char *zfile = NULL; |
71 |
|
char *err; |
72 |
< |
int i; |
72 |
> |
int i, rval; |
73 |
|
|
74 |
|
progname = argv[0]; |
75 |
|
|
76 |
< |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
76 |
> |
for (i = 1; i < argc && argv[i][0] == '-'; i++) { |
77 |
> |
rval = getviewopt(&ourview, argc-i, argv+i); |
78 |
> |
if (rval >= 0) { |
79 |
> |
i += rval; |
80 |
> |
continue; |
81 |
> |
} |
82 |
|
switch (argv[i][1]) { |
83 |
|
case 't': /* threshold */ |
84 |
|
check(2,1); |
120 |
|
break; |
121 |
|
case 'r': /* rtrace */ |
122 |
|
check(3,1); |
123 |
< |
deffill = calfill; |
123 |
> |
deffill = rcalfill; |
124 |
|
calstart(RTCOM, argv[++i]); |
125 |
|
break; |
126 |
|
default: |
133 |
|
break; |
134 |
|
case 'x': /* x resolution */ |
135 |
|
check(2,1); |
136 |
< |
ourview.hresolu = atoi(argv[++i]); |
136 |
> |
hresolu = atoi(argv[++i]); |
137 |
|
break; |
138 |
|
case 'y': /* y resolution */ |
139 |
|
check(2,1); |
140 |
< |
ourview.vresolu = atoi(argv[++i]); |
140 |
> |
vresolu = atoi(argv[++i]); |
141 |
|
break; |
142 |
< |
case 'v': /* view */ |
143 |
< |
switch (argv[i][2]) { |
144 |
< |
case 't': /* type */ |
145 |
< |
check(4,0); |
146 |
< |
ourview.type = argv[i][3]; |
147 |
< |
break; |
136 |
< |
case 'p': /* point */ |
137 |
< |
check(3,3); |
138 |
< |
ourview.vp[0] = atof(argv[++i]); |
139 |
< |
ourview.vp[1] = atof(argv[++i]); |
140 |
< |
ourview.vp[2] = atof(argv[++i]); |
141 |
< |
break; |
142 |
< |
case 'd': /* direction */ |
143 |
< |
check(3,3); |
144 |
< |
ourview.vdir[0] = atof(argv[++i]); |
145 |
< |
ourview.vdir[1] = atof(argv[++i]); |
146 |
< |
ourview.vdir[2] = atof(argv[++i]); |
147 |
< |
break; |
148 |
< |
case 'u': /* up */ |
149 |
< |
check(3,3); |
150 |
< |
ourview.vup[0] = atof(argv[++i]); |
151 |
< |
ourview.vup[1] = atof(argv[++i]); |
152 |
< |
ourview.vup[2] = atof(argv[++i]); |
153 |
< |
break; |
154 |
< |
case 'h': /* horizontal */ |
155 |
< |
check(3,1); |
156 |
< |
ourview.horiz = atof(argv[++i]); |
157 |
< |
break; |
158 |
< |
case 'v': /* vertical */ |
159 |
< |
check(3,1); |
160 |
< |
ourview.vert = atof(argv[++i]); |
161 |
< |
break; |
162 |
< |
case 'f': /* file */ |
163 |
< |
check(3,1); |
164 |
< |
gotvfile = viewfile(argv[++i], &ourview); |
165 |
< |
if (gotvfile < 0) { |
166 |
< |
perror(argv[i]); |
167 |
< |
exit(1); |
168 |
< |
} else if (gotvfile == 0) { |
169 |
< |
fprintf(stderr, "%s: bad view file\n", |
170 |
< |
argv[i]); |
171 |
< |
exit(1); |
172 |
< |
} |
173 |
< |
break; |
174 |
< |
default: |
142 |
> |
case 'p': /* pixel aspect */ |
143 |
> |
check(2,1); |
144 |
> |
pixaspect = atof(argv[++i]); |
145 |
> |
break; |
146 |
> |
case 'v': /* view file */ |
147 |
> |
if (argv[i][2] != 'f') |
148 |
|
goto badopt; |
149 |
+ |
check(3,1); |
150 |
+ |
gotvfile = viewfile(argv[++i], &ourview); |
151 |
+ |
if (gotvfile < 0) { |
152 |
+ |
perror(argv[i]); |
153 |
+ |
exit(1); |
154 |
+ |
} else if (gotvfile == 0) { |
155 |
+ |
fprintf(stderr, "%s: bad view file\n", |
156 |
+ |
argv[i]); |
157 |
+ |
exit(1); |
158 |
|
} |
159 |
|
break; |
160 |
|
default: |
163 |
|
progname, argv[i]); |
164 |
|
goto userr; |
165 |
|
} |
166 |
+ |
} |
167 |
|
/* check arguments */ |
168 |
|
if ((argc-i)%2) |
169 |
|
goto userr; |
172 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
173 |
|
exit(1); |
174 |
|
} |
175 |
+ |
normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu); |
176 |
|
/* allocate frame */ |
177 |
< |
ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR)); |
178 |
< |
ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float)); |
179 |
< |
if (ourpict == NULL || ourzbuf == NULL) { |
180 |
< |
perror(progname); |
197 |
< |
exit(1); |
198 |
< |
} |
177 |
> |
ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR)); |
178 |
> |
ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float)); |
179 |
> |
if (ourpict == NULL || ourzbuf == NULL) |
180 |
> |
syserror(); |
181 |
|
/* get input */ |
182 |
|
for ( ; i < argc; i += 2) |
183 |
|
addpicture(argv[i], argv[i+1]); |
187 |
|
else |
188 |
|
fillpicture(); |
189 |
|
/* close calculation */ |
190 |
< |
if (deffill == calfill) |
209 |
< |
caldone(); |
190 |
> |
caldone(); |
191 |
|
/* add to header */ |
192 |
|
printargs(argc, argv, stdout); |
193 |
|
if (gotvfile) { |
195 |
|
fprintview(&ourview, stdout); |
196 |
|
printf("\n"); |
197 |
|
} |
198 |
+ |
if (pixaspect < .99 || pixaspect > 1.01) |
199 |
+ |
fputaspect(pixaspect, stdout); |
200 |
+ |
if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005)) |
201 |
+ |
fputexpos(ourexp, stdout); |
202 |
|
printf("\n"); |
203 |
|
/* write picture */ |
204 |
|
writepicture(); |
209 |
|
exit(0); |
210 |
|
userr: |
211 |
|
fprintf(stderr, |
212 |
< |
"Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n", |
212 |
> |
"Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n", |
213 |
|
progname); |
214 |
|
exit(1); |
215 |
|
#undef check |
224 |
|
|
225 |
|
printf("\t%s", s); |
226 |
|
|
227 |
+ |
if (isexpos(s)) { |
228 |
+ |
theirexp *= exposval(s); |
229 |
+ |
return; |
230 |
+ |
} |
231 |
|
for (an = altname; *an != NULL; an++) |
232 |
|
if (!strncmp(*an, s, strlen(*an))) { |
233 |
< |
if (sscanview(&theirview, s+strlen(*an)) == 0) |
233 |
> |
if (sscanview(&theirview, s+strlen(*an)) > 0) |
234 |
|
gotview++; |
235 |
|
break; |
236 |
|
} |
244 |
|
FILE *pfp, *zfp; |
245 |
|
char *err; |
246 |
|
COLR *scanin; |
247 |
< |
float *zin, *zlast; |
248 |
< |
int *plast; |
247 |
> |
float *zin; |
248 |
> |
struct position *plast; |
249 |
|
int y; |
250 |
|
/* open picture file */ |
251 |
|
if ((pfp = fopen(pfile, "r")) == NULL) { |
252 |
|
perror(pfile); |
253 |
|
exit(1); |
254 |
|
} |
255 |
< |
/* get header and view */ |
256 |
< |
printf("%s:\n", pfile); |
255 |
> |
/* get header with exposure and view */ |
256 |
> |
theirexp = 1.0; |
257 |
|
gotview = 0; |
258 |
+ |
printf("%s:\n", pfile); |
259 |
|
getheader(pfp, headline); |
260 |
< |
if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp) |
260 |
> |
if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp) |
261 |
|
!= (YMAJOR|YDECR)) { |
262 |
|
fprintf(stderr, "%s: picture view error\n", pfile); |
263 |
|
exit(1); |
264 |
|
} |
265 |
+ |
if (ourexp <= 0) |
266 |
+ |
ourexp = theirexp; |
267 |
+ |
else if (ABS(theirexp-ourexp) > .01*ourexp) |
268 |
+ |
fprintf(stderr, "%s: different exposure (warning)\n", pfile); |
269 |
|
if (err = setview(&theirview)) { |
270 |
|
fprintf(stderr, "%s: %s\n", pfile, err); |
271 |
|
exit(1); |
273 |
|
/* compute transformation */ |
274 |
|
pixform(theirs2ours, &theirview, &ourview); |
275 |
|
/* allocate scanlines */ |
276 |
< |
scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR)); |
277 |
< |
zin = (float *)malloc(theirview.hresolu*sizeof(float)); |
278 |
< |
plast = (int *)calloc(theirview.hresolu, sizeof(int)); |
279 |
< |
zlast = (float *)calloc(theirview.hresolu, sizeof(float)); |
280 |
< |
if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) { |
287 |
< |
perror(progname); |
288 |
< |
exit(1); |
289 |
< |
} |
276 |
> |
scanin = (COLR *)malloc(thresolu*sizeof(COLR)); |
277 |
> |
zin = (float *)malloc(thresolu*sizeof(float)); |
278 |
> |
plast = (struct position *)calloc(thresolu, sizeof(struct position)); |
279 |
> |
if (scanin == NULL || zin == NULL || plast == NULL) |
280 |
> |
syserror(); |
281 |
|
/* get z specification or file */ |
282 |
|
if ((zfp = fopen(zspec, "r")) == NULL) { |
283 |
|
double zvalue; |
286 |
|
perror(zspec); |
287 |
|
exit(1); |
288 |
|
} |
289 |
< |
for (x = 0; x < theirview.hresolu; x++) |
289 |
> |
for (x = 0; x < thresolu; x++) |
290 |
|
zin[x] = zvalue; |
291 |
|
} |
292 |
|
/* load image */ |
293 |
< |
for (y = theirview.vresolu-1; y >= 0; y--) { |
294 |
< |
if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) { |
293 |
> |
for (y = tvresolu-1; y >= 0; y--) { |
294 |
> |
if (freadcolrs(scanin, thresolu, pfp) < 0) { |
295 |
|
fprintf(stderr, "%s: read error\n", pfile); |
296 |
|
exit(1); |
297 |
|
} |
298 |
|
if (zfp != NULL |
299 |
< |
&& fread(zin,sizeof(float),theirview.hresolu,zfp) |
309 |
< |
< theirview.hresolu) { |
299 |
> |
&& fread(zin,sizeof(float),thresolu,zfp) < thresolu) { |
300 |
|
fprintf(stderr, "%s: read error\n", zspec); |
301 |
|
exit(1); |
302 |
|
} |
303 |
< |
addscanline(y, scanin, zin, plast, zlast); |
303 |
> |
addscanline(y, scanin, zin, plast); |
304 |
|
} |
305 |
|
/* clean up */ |
306 |
|
free((char *)scanin); |
307 |
|
free((char *)zin); |
308 |
|
free((char *)plast); |
319 |
– |
free((char *)zlast); |
309 |
|
fclose(pfp); |
310 |
|
if (zfp != NULL) |
311 |
|
fclose(zfp); |
319 |
|
double m4t[4][4]; |
320 |
|
|
321 |
|
setident4(xfmat); |
322 |
< |
xfmat[0][0] = vw1->vhinc[0]; |
323 |
< |
xfmat[0][1] = vw1->vhinc[1]; |
324 |
< |
xfmat[0][2] = vw1->vhinc[2]; |
325 |
< |
xfmat[1][0] = vw1->vvinc[0]; |
326 |
< |
xfmat[1][1] = vw1->vvinc[1]; |
327 |
< |
xfmat[1][2] = vw1->vvinc[2]; |
322 |
> |
xfmat[0][0] = vw1->hvec[0]; |
323 |
> |
xfmat[0][1] = vw1->hvec[1]; |
324 |
> |
xfmat[0][2] = vw1->hvec[2]; |
325 |
> |
xfmat[1][0] = vw1->vvec[0]; |
326 |
> |
xfmat[1][1] = vw1->vvec[1]; |
327 |
> |
xfmat[1][2] = vw1->vvec[2]; |
328 |
|
xfmat[2][0] = vw1->vdir[0]; |
329 |
|
xfmat[2][1] = vw1->vdir[1]; |
330 |
|
xfmat[2][2] = vw1->vdir[2]; |
332 |
|
xfmat[3][1] = vw1->vp[1]; |
333 |
|
xfmat[3][2] = vw1->vp[2]; |
334 |
|
setident4(m4t); |
335 |
< |
m4t[0][0] = vw2->vhinc[0]/vw2->vhn2; |
336 |
< |
m4t[1][0] = vw2->vhinc[1]/vw2->vhn2; |
337 |
< |
m4t[2][0] = vw2->vhinc[2]/vw2->vhn2; |
338 |
< |
m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2; |
339 |
< |
m4t[0][1] = vw2->vvinc[0]/vw2->vvn2; |
340 |
< |
m4t[1][1] = vw2->vvinc[1]/vw2->vvn2; |
341 |
< |
m4t[2][1] = vw2->vvinc[2]/vw2->vvn2; |
342 |
< |
m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2; |
335 |
> |
m4t[0][0] = vw2->hvec[0]/vw2->hn2; |
336 |
> |
m4t[1][0] = vw2->hvec[1]/vw2->hn2; |
337 |
> |
m4t[2][0] = vw2->hvec[2]/vw2->hn2; |
338 |
> |
m4t[3][0] = -DOT(vw2->vp,vw2->hvec)/vw2->hn2; |
339 |
> |
m4t[0][1] = vw2->vvec[0]/vw2->vn2; |
340 |
> |
m4t[1][1] = vw2->vvec[1]/vw2->vn2; |
341 |
> |
m4t[2][1] = vw2->vvec[2]/vw2->vn2; |
342 |
> |
m4t[3][1] = -DOT(vw2->vp,vw2->vvec)/vw2->vn2; |
343 |
|
m4t[0][2] = vw2->vdir[0]; |
344 |
|
m4t[1][2] = vw2->vdir[1]; |
345 |
|
m4t[2][2] = vw2->vdir[2]; |
348 |
|
} |
349 |
|
|
350 |
|
|
351 |
< |
addscanline(y, pline, zline, lasty, lastyz) /* add scanline to output */ |
351 |
> |
addscanline(y, pline, zline, lasty) /* add scanline to output */ |
352 |
|
int y; |
353 |
|
COLR *pline; |
354 |
|
float *zline; |
355 |
< |
int *lasty; /* input/output */ |
367 |
< |
float *lastyz; /* input/output */ |
355 |
> |
struct position *lasty; /* input/output */ |
356 |
|
{ |
357 |
|
extern double sqrt(); |
358 |
|
double pos[3]; |
359 |
< |
int lastx = 0; |
372 |
< |
double lastxz = 0; |
373 |
< |
double zt; |
374 |
< |
int xpos, ypos; |
359 |
> |
struct position lastx, newpos; |
360 |
|
register int x; |
361 |
|
|
362 |
< |
for (x = theirview.hresolu-1; x >= 0; x--) { |
363 |
< |
pos[0] = x - .5*(theirview.hresolu-1); |
364 |
< |
pos[1] = y - .5*(theirview.vresolu-1); |
362 |
> |
for (x = thresolu-1; x >= 0; x--) { |
363 |
> |
pos[0] = (x+.5)/thresolu + theirview.hoff - .5; |
364 |
> |
pos[1] = (y+.5)/tvresolu + theirview.voff - .5; |
365 |
|
pos[2] = zline[x]; |
366 |
|
if (theirview.type == VT_PER) { |
367 |
|
if (normdist) /* adjust for eye-ray distance */ |
368 |
|
pos[2] /= sqrt( 1. |
369 |
< |
+ pos[0]*pos[0]*theirview.vhn2 |
370 |
< |
+ pos[1]*pos[1]*theirview.vvn2 ); |
369 |
> |
+ pos[0]*pos[0]*theirview.hn2 |
370 |
> |
+ pos[1]*pos[1]*theirview.vn2 ); |
371 |
|
pos[0] *= pos[2]; |
372 |
|
pos[1] *= pos[2]; |
373 |
|
} |
374 |
|
multp3(pos, pos, theirs2ours); |
375 |
< |
if (pos[2] <= 0) |
375 |
> |
if (pos[2] <= 0) { |
376 |
> |
lasty[x].z = lastx.z = 0; /* mark invalid */ |
377 |
|
continue; |
378 |
+ |
} |
379 |
|
if (ourview.type == VT_PER) { |
380 |
|
pos[0] /= pos[2]; |
381 |
|
pos[1] /= pos[2]; |
382 |
|
} |
383 |
< |
pos[0] += .5*ourview.hresolu; |
384 |
< |
pos[1] += .5*ourview.vresolu; |
385 |
< |
if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu |
386 |
< |
|| pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu) |
387 |
< |
continue; |
383 |
> |
pos[0] += .5 - ourview.hoff; |
384 |
> |
pos[1] += .5 - ourview.voff; |
385 |
> |
newpos.x = pos[0] * hresolu; |
386 |
> |
newpos.y = pos[1] * vresolu; |
387 |
> |
newpos.z = zline[x]; |
388 |
|
/* add pixel to our image */ |
389 |
< |
zt = 2.*zeps*zline[x]; |
390 |
< |
addpixel(xpos, ypos, |
391 |
< |
(fill&F_FORE && ABS(zline[x]-lastxz) <= zt) |
392 |
< |
? lastx - xpos : 1, |
393 |
< |
(fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt) |
394 |
< |
? lasty[x] - ypos : 1, |
395 |
< |
pline[x], pos[2]); |
396 |
< |
lastx = xpos; |
410 |
< |
lasty[x] = ypos; |
411 |
< |
lastxz = lastyz[x] = zline[x]; |
389 |
> |
if (pos[0] >= 0 && newpos.x < hresolu |
390 |
> |
&& pos[1] >= 0 && newpos.y < vresolu) { |
391 |
> |
addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]); |
392 |
> |
lasty[x].x = lastx.x = newpos.x; |
393 |
> |
lasty[x].y = lastx.y = newpos.y; |
394 |
> |
lasty[x].z = lastx.z = newpos.z; |
395 |
> |
} else |
396 |
> |
lasty[x].z = lastx.z = 0; /* mark invalid */ |
397 |
|
} |
398 |
|
} |
399 |
|
|
400 |
|
|
401 |
< |
addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */ |
402 |
< |
int xstart, ystart; |
418 |
< |
int width, height; |
401 |
> |
addpixel(p0, p1, p2, pix, z) /* fill in pixel parallelogram */ |
402 |
> |
struct position *p0, *p1, *p2; |
403 |
|
COLR pix; |
404 |
|
double z; |
405 |
|
{ |
406 |
< |
register int x, y; |
407 |
< |
/* make width and height positive */ |
408 |
< |
if (width < 0) { |
409 |
< |
width = -width; |
410 |
< |
xstart = xstart-width+1; |
411 |
< |
} else if (width == 0) |
412 |
< |
width = 1; |
413 |
< |
if (height < 0) { |
414 |
< |
height = -height; |
415 |
< |
ystart = ystart-height+1; |
416 |
< |
} else if (height == 0) |
417 |
< |
height = 1; |
418 |
< |
/* fill pixel(s) within rectangle */ |
419 |
< |
for (y = ystart; y < ystart+height; y++) |
420 |
< |
for (x = xstart; x < xstart+width; x++) |
421 |
< |
if (zscan(y)[x] <= 0 |
422 |
< |
|| zscan(y)[x]-z > zeps*zscan(y)[x]) { |
406 |
> |
double zt = 2.*zeps*p0->z; /* threshold */ |
407 |
> |
int s1x, s1y, s2x, s2y; /* step sizes */ |
408 |
> |
int l1, l2, c1, c2; /* side lengths and counters */ |
409 |
> |
int p1isy; /* p0p1 along y? */ |
410 |
> |
int x1, y1; /* p1 position */ |
411 |
> |
register int x, y; /* final position */ |
412 |
> |
|
413 |
> |
/* compute vector p0p1 */ |
414 |
> |
if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) { |
415 |
> |
s1x = p1->x - p0->x; |
416 |
> |
s1y = p1->y - p0->y; |
417 |
> |
l1 = ABS(s1x); |
418 |
> |
if (p1isy = (ABS(s1y) > l1)) |
419 |
> |
l1 = ABS(s1y); |
420 |
> |
} else { |
421 |
> |
l1 = s1x = s1y = 1; |
422 |
> |
p1isy = -1; |
423 |
> |
} |
424 |
> |
/* compute vector p0p2 */ |
425 |
> |
if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) { |
426 |
> |
s2x = p2->x - p0->x; |
427 |
> |
s2y = p2->y - p0->y; |
428 |
> |
if (p1isy == 1) |
429 |
> |
l2 = ABS(s2x); |
430 |
> |
else { |
431 |
> |
l2 = ABS(s2y); |
432 |
> |
if (p1isy != 0 && ABS(s2x) > l2) |
433 |
> |
l2 = ABS(s2x); |
434 |
> |
} |
435 |
> |
} else |
436 |
> |
l2 = s2x = s2y = 1; |
437 |
> |
/* fill the parallelogram */ |
438 |
> |
for (c1 = l1; c1-- > 0; ) { |
439 |
> |
x1 = p0->x + c1*s1x/l1; |
440 |
> |
y1 = p0->y + c1*s1y/l1; |
441 |
> |
for (c2 = l2; c2-- > 0; ) { |
442 |
> |
x = x1 + c2*s2x/l2; |
443 |
> |
y = y1 + c2*s2y/l2; |
444 |
> |
if (zscan(y)[x] <= 0 || zscan(y)[x]-z |
445 |
> |
> zeps*zscan(y)[x]) { |
446 |
|
zscan(y)[x] = z; |
447 |
|
copycolr(pscan(y)[x], pix); |
448 |
|
} |
449 |
+ |
} |
450 |
+ |
} |
451 |
|
} |
452 |
|
|
453 |
|
|
458 |
|
COLR pfill; |
459 |
|
register int x, i; |
460 |
|
/* get back buffer */ |
461 |
< |
yback = (int *)malloc(ourview.hresolu*sizeof(int)); |
462 |
< |
if (yback == NULL) { |
463 |
< |
perror(progname); |
464 |
< |
return; |
456 |
< |
} |
457 |
< |
for (x = 0; x < ourview.hresolu; x++) |
461 |
> |
yback = (int *)malloc(hresolu*sizeof(int)); |
462 |
> |
if (yback == NULL) |
463 |
> |
syserror(); |
464 |
> |
for (x = 0; x < hresolu; x++) |
465 |
|
yback[x] = -2; |
466 |
|
/* |
467 |
|
* Xback and yback are the pixel locations of suitable |
470 |
|
* that there is no suitable background in this direction. |
471 |
|
*/ |
472 |
|
/* fill image */ |
473 |
< |
for (y = 0; y < ourview.vresolu; y++) { |
473 |
> |
for (y = 0; y < vresolu; y++) { |
474 |
|
xback = -2; |
475 |
< |
for (x = 0; x < ourview.hresolu; x++) |
475 |
> |
for (x = 0; x < hresolu; x++) |
476 |
|
if (zscan(y)[x] <= 0) { /* empty pixel */ |
477 |
|
/* |
478 |
|
* First, find background from above or below. |
479 |
|
* (farthest assigned pixel) |
480 |
|
*/ |
481 |
|
if (yback[x] == -2) { |
482 |
< |
for (i = y+1; i < ourview.vresolu; i++) |
482 |
> |
for (i = y+1; i < vresolu; i++) |
483 |
|
if (zscan(i)[x] > 0) |
484 |
|
break; |
485 |
< |
if (i < ourview.vresolu |
485 |
> |
if (i < vresolu |
486 |
|
&& (y <= 0 || zscan(y-1)[x] < zscan(i)[x])) |
487 |
|
yback[x] = i; |
488 |
|
else |
492 |
|
* Next, find background from left or right. |
493 |
|
*/ |
494 |
|
if (xback == -2) { |
495 |
< |
for (i = x+1; i < ourview.hresolu; i++) |
495 |
> |
for (i = x+1; i < hresolu; i++) |
496 |
|
if (zscan(y)[i] > 0) |
497 |
|
break; |
498 |
< |
if (i < ourview.hresolu |
498 |
> |
if (i < hresolu |
499 |
|
&& (x <= 0 || zscan(y)[x-1] < zscan(y)[i])) |
500 |
|
xback = i; |
501 |
|
else |
537 |
|
{ |
538 |
|
register int x, y; |
539 |
|
|
540 |
< |
for (y = 0; y < ourview.vresolu; y++) |
541 |
< |
for (x = 0; x < ourview.hresolu; x++) |
540 |
> |
for (y = 0; y < vresolu; y++) |
541 |
> |
for (x = 0; x < hresolu; x++) |
542 |
|
if (zscan(y)[x] <= 0) |
543 |
|
(*deffill)(x,y); |
544 |
|
} |
548 |
|
{ |
549 |
|
int y; |
550 |
|
|
551 |
< |
fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); |
552 |
< |
for (y = ourview.vresolu-1; y >= 0; y--) |
553 |
< |
if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) { |
554 |
< |
perror(progname); |
548 |
< |
exit(1); |
549 |
< |
} |
551 |
> |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
552 |
> |
for (y = vresolu-1; y >= 0; y--) |
553 |
> |
if (fwritecolrs(pscan(y), hresolu, stdout) < 0) |
554 |
> |
syserror(); |
555 |
|
} |
556 |
|
|
557 |
|
|
569 |
|
exit(1); |
570 |
|
} |
571 |
|
if (donorm |
572 |
< |
&& (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) { |
573 |
< |
perror(progname); |
574 |
< |
exit(1); |
570 |
< |
} |
571 |
< |
for (y = ourview.vresolu-1; y >= 0; y--) { |
572 |
> |
&& (zout = (float *)malloc(hresolu*sizeof(float))) == NULL) |
573 |
> |
syserror(); |
574 |
> |
for (y = vresolu-1; y >= 0; y--) { |
575 |
|
if (donorm) { |
576 |
|
double vx, yzn2; |
577 |
|
register int x; |
578 |
< |
yzn2 = y - .5*(ourview.vresolu-1); |
579 |
< |
yzn2 = 1. + yzn2*yzn2*ourview.vvn2; |
580 |
< |
for (x = 0; x < ourview.hresolu; x++) { |
581 |
< |
vx = x - .5*(ourview.hresolu-1); |
578 |
> |
yzn2 = y - .5*(vresolu-1); |
579 |
> |
yzn2 = 1. + yzn2*yzn2*ourview.vn2; |
580 |
> |
for (x = 0; x < hresolu; x++) { |
581 |
> |
vx = x - .5*(hresolu-1); |
582 |
|
zout[x] = zscan(y)[x] |
583 |
< |
* sqrt(vx*vx*ourview.vhn2 + yzn2); |
583 |
> |
* sqrt(vx*vx*ourview.hn2 + yzn2); |
584 |
|
} |
585 |
|
} else |
586 |
|
zout = zscan(y); |
587 |
< |
if (fwrite(zout, sizeof(float), ourview.hresolu, fp) |
585 |
< |
< ourview.hresolu) { |
587 |
> |
if (fwrite(zout, sizeof(float), hresolu, fp) < hresolu) { |
588 |
|
perror(fname); |
589 |
|
exit(1); |
590 |
|
} |
622 |
|
char combuf[512]; |
623 |
|
int p0[2], p1[2]; |
624 |
|
|
625 |
+ |
if (childpid != -1) { |
626 |
+ |
fprintf(stderr, "%s: too many calculations\n", progname); |
627 |
+ |
exit(1); |
628 |
+ |
} |
629 |
|
sprintf(combuf, prog, PACKSIZ, args); |
630 |
|
if (pipe(p0) < 0 || pipe(p1) < 0) |
631 |
< |
goto syserr; |
631 |
> |
syserror(); |
632 |
|
if ((childpid = vfork()) == 0) { /* fork calculation */ |
633 |
|
close(p0[1]); |
634 |
|
close(p1[0]); |
644 |
|
perror("/bin/sh"); |
645 |
|
_exit(127); |
646 |
|
} |
647 |
< |
if (childpid < 0) |
648 |
< |
goto syserr; |
647 |
> |
if (childpid == -1) |
648 |
> |
syserror(); |
649 |
|
close(p0[0]); |
650 |
|
close(p1[1]); |
651 |
|
if ((psend = fdopen(p0[1], "w")) == NULL) |
652 |
< |
goto syserr; |
652 |
> |
syserror(); |
653 |
|
if ((precv = fdopen(p1[0], "r")) == NULL) |
654 |
< |
goto syserr; |
654 |
> |
syserror(); |
655 |
|
queuesiz = 0; |
650 |
– |
return; |
651 |
– |
syserr: |
652 |
– |
perror(progname); |
653 |
– |
exit(1); |
656 |
|
} |
657 |
|
|
658 |
|
|
660 |
|
{ |
661 |
|
int pid; |
662 |
|
|
663 |
< |
fclose(psend); |
663 |
> |
if (childpid == -1) |
664 |
> |
return; |
665 |
> |
if (fclose(psend) == EOF) |
666 |
> |
syserror(); |
667 |
|
clearqueue(); |
668 |
|
fclose(precv); |
669 |
|
while ((pid = wait(0)) != -1 && pid != childpid) |
670 |
|
; |
671 |
+ |
childpid = -1; |
672 |
|
} |
673 |
|
|
674 |
|
|
675 |
< |
calfill(x, y) /* fill with calculated pixel */ |
675 |
> |
rcalfill(x, y) /* fill with ray-calculated pixel */ |
676 |
|
int x, y; |
677 |
|
{ |
678 |
|
FVECT orig, dir; |
679 |
|
float outbuf[6]; |
680 |
|
|
681 |
|
if (queuesiz >= PACKSIZ) { /* flush queue */ |
682 |
< |
fflush(psend); |
682 |
> |
if (fflush(psend) == EOF) |
683 |
> |
syserror(); |
684 |
|
clearqueue(); |
685 |
|
} |
686 |
|
/* send new ray */ |
687 |
< |
rayview(orig, dir, &ourview, x+.5, y+.5); |
687 |
> |
viewray(orig, dir, &ourview, (x+.5)/hresolu, (y+.5)/vresolu); |
688 |
|
outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2]; |
689 |
|
outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2]; |
690 |
< |
fwrite(outbuf, sizeof(float), 6, psend); |
690 |
> |
if (fwrite(outbuf, sizeof(float), 6, psend) < 6) |
691 |
> |
syserror(); |
692 |
|
/* remember it */ |
693 |
|
queue[queuesiz][0] = x; |
694 |
|
queue[queuesiz][1] = y; |
702 |
|
register int i; |
703 |
|
|
704 |
|
for (i = 0; i < queuesiz; i++) { |
705 |
< |
fread(inbuf, sizeof(float), 4, precv); |
705 |
> |
if (fread(inbuf, sizeof(float), 4, precv) < 4) { |
706 |
> |
fprintf(stderr, "%s: read error in clearqueue\n", |
707 |
> |
progname); |
708 |
> |
exit(1); |
709 |
> |
} |
710 |
> |
if (ourexp > 0 && ourexp != 1.0) { |
711 |
> |
inbuf[0] *= ourexp; |
712 |
> |
inbuf[1] *= ourexp; |
713 |
> |
inbuf[2] *= ourexp; |
714 |
> |
} |
715 |
|
setcolr(pscan(queue[i][1])[queue[i][0]], |
716 |
|
inbuf[0], inbuf[1], inbuf[2]); |
717 |
|
zscan(queue[i][1])[queue[i][0]] = inbuf[3]; |
718 |
|
} |
719 |
|
queuesiz = 0; |
720 |
+ |
} |
721 |
+ |
|
722 |
+ |
|
723 |
+ |
syserror() /* report error and exit */ |
724 |
+ |
{ |
725 |
+ |
perror(progname); |
726 |
+ |
exit(1); |
727 |
|
} |