1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
12 |
|
|
13 |
|
#include "standard.h" |
14 |
|
|
15 |
< |
#include <fcntl.h> |
15 |
> |
#include <ctype.h> |
16 |
|
|
17 |
|
#include "view.h" |
18 |
|
|
20 |
|
|
21 |
|
#include "resolu.h" |
22 |
|
|
23 |
– |
#ifndef BSD |
24 |
– |
#define vfork fork |
25 |
– |
#endif |
26 |
– |
|
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 */ |
29 |
> |
#define PACKSIZ 256 /* max. calculation packet size */ |
30 |
|
|
31 |
< |
#define RTCOM "rtrace -h -ovl -fff %s" |
31 |
> |
#define RTCOM "rtrace -h- -ovl -fff " |
32 |
|
|
33 |
|
#define ABS(x) ((x)>0?(x):-(x)) |
34 |
|
|
35 |
|
struct position {int x,y; float z;}; |
36 |
|
|
37 |
+ |
#define NSTEPS 64 /* number steps in overlap prescan */ |
38 |
+ |
#define MINSTEP 4 /* minimum worthwhile preview step */ |
39 |
+ |
|
40 |
+ |
struct bound {int min,max;}; |
41 |
+ |
|
42 |
|
VIEW ourview = STDVIEW; /* desired view */ |
43 |
|
int hresolu = 512; /* horizontal resolution */ |
44 |
|
int vresolu = 512; /* vertical resolution */ |
68 |
|
double theirs2ours[4][4]; /* transformation matrix */ |
69 |
|
int hasmatrix = 0; /* has transformation matrix */ |
70 |
|
|
71 |
< |
int childpid = -1; /* id of fill process */ |
72 |
< |
FILE *psend, *precv; /* pipes to/from fill calculation */ |
73 |
< |
int queue[PACKSIZ][2]; /* pending pixels */ |
71 |
> |
int PDesc[3] = {-1,-1,-1}; /* rtrace process descriptor */ |
72 |
> |
#define childpid (PDesc[2]) |
73 |
> |
unsigned short queue[PACKSIZ][2]; /* pending pixels */ |
74 |
> |
int packsiz; /* actual packet size */ |
75 |
|
int queuesiz; /* number of pixels pending */ |
76 |
|
|
77 |
|
|
79 |
|
int argc; |
80 |
|
char *argv[]; |
81 |
|
{ |
82 |
< |
#define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt |
83 |
< |
extern double atof(); |
82 |
> |
#define check(ol,al) if (argv[i][ol] || \ |
83 |
> |
badarg(argc-i-1,argv+i+1,al)) \ |
84 |
> |
goto badopt |
85 |
|
int gotvfile = 0; |
86 |
|
char *zfile = NULL; |
87 |
|
char *err; |
97 |
|
} |
98 |
|
switch (argv[i][1]) { |
99 |
|
case 't': /* threshold */ |
100 |
< |
check(2,1); |
100 |
> |
check(2,"f"); |
101 |
|
zeps = atof(argv[++i]); |
102 |
|
break; |
103 |
|
case 'n': /* dist. normalized? */ |
104 |
< |
check(2,0); |
104 |
> |
check(2,NULL); |
105 |
|
normdist = !normdist; |
106 |
|
break; |
107 |
|
case 'f': /* fill type */ |
108 |
|
switch (argv[i][2]) { |
109 |
|
case '0': /* none */ |
110 |
< |
check(3,0); |
110 |
> |
check(3,NULL); |
111 |
|
fillo = 0; |
112 |
|
break; |
113 |
|
case 'f': /* foreground */ |
114 |
< |
check(3,0); |
114 |
> |
check(3,NULL); |
115 |
|
fillo = F_FORE; |
116 |
|
break; |
117 |
|
case 'b': /* background */ |
118 |
< |
check(3,0); |
118 |
> |
check(3,NULL); |
119 |
|
fillo = F_BACK; |
120 |
|
break; |
121 |
|
case 'a': /* all */ |
122 |
< |
check(3,0); |
122 |
> |
check(3,NULL); |
123 |
|
fillo = F_FORE|F_BACK; |
124 |
|
break; |
125 |
|
case 's': /* sample */ |
126 |
< |
check(3,1); |
126 |
> |
check(3,"i"); |
127 |
|
fillsamp = atoi(argv[++i]); |
128 |
|
break; |
129 |
|
case 'c': /* color */ |
130 |
< |
check(3,3); |
130 |
> |
check(3,"fff"); |
131 |
|
fillfunc = backfill; |
132 |
|
setcolr(backcolr, atof(argv[i+1]), |
133 |
|
atof(argv[i+2]), atof(argv[i+3])); |
134 |
|
i += 3; |
135 |
|
break; |
136 |
|
case 'z': /* z value */ |
137 |
< |
check(3,1); |
137 |
> |
check(3,"f"); |
138 |
|
fillfunc = backfill; |
139 |
|
backz = atof(argv[++i]); |
140 |
|
break; |
141 |
|
case 'r': /* rtrace */ |
142 |
< |
check(3,1); |
142 |
> |
check(3,"s"); |
143 |
|
fillfunc = rcalfill; |
144 |
|
calstart(RTCOM, argv[++i]); |
145 |
|
break; |
148 |
|
} |
149 |
|
break; |
150 |
|
case 'z': /* z file */ |
151 |
< |
check(2,1); |
151 |
> |
check(2,"s"); |
152 |
|
zfile = argv[++i]; |
153 |
|
break; |
154 |
|
case 'x': /* x resolution */ |
155 |
< |
check(2,1); |
155 |
> |
check(2,"i"); |
156 |
|
hresolu = atoi(argv[++i]); |
157 |
|
break; |
158 |
|
case 'y': /* y resolution */ |
159 |
< |
check(2,1); |
159 |
> |
check(2,"i"); |
160 |
|
vresolu = atoi(argv[++i]); |
161 |
|
break; |
162 |
|
case 'p': /* pixel aspect */ |
163 |
< |
check(2,1); |
163 |
> |
if (argv[i][2] != 'a') |
164 |
> |
goto badopt; |
165 |
> |
check(3,"f"); |
166 |
|
pixaspect = atof(argv[++i]); |
167 |
|
break; |
168 |
|
case 'v': /* view file */ |
169 |
|
if (argv[i][2] != 'f') |
170 |
|
goto badopt; |
171 |
< |
check(3,1); |
171 |
> |
check(3,"s"); |
172 |
|
gotvfile = viewfile(argv[++i], &ourview, 0, 0); |
173 |
< |
if (gotvfile < 0) { |
174 |
< |
perror(argv[i]); |
175 |
< |
exit(1); |
171 |
< |
} else if (gotvfile == 0) { |
173 |
> |
if (gotvfile < 0) |
174 |
> |
syserror(argv[i]); |
175 |
> |
else if (gotvfile == 0) { |
176 |
|
fprintf(stderr, "%s: bad view file\n", |
177 |
|
argv[i]); |
178 |
|
exit(1); |
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)) { |
194 |
> |
if ((err = setview(&ourview)) != NULL) { |
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 *)malloc(hresolu*vresolu*sizeof(COLR)); |
201 |
< |
ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float)); |
200 |
> |
ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR)); |
201 |
> |
ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float)); |
202 |
|
if (ourpict == NULL || ourzbuf == NULL) |
203 |
< |
syserror(); |
203 |
> |
syserror(progname); |
204 |
> |
bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float)); |
205 |
> |
/* new header */ |
206 |
> |
newheader("RADIANCE", stdout); |
207 |
|
/* get input */ |
208 |
|
for ( ; i < argc; i += 2) |
209 |
|
addpicture(argv[i], argv[i+1]); |
214 |
|
fillpicture(fillfunc); |
215 |
|
/* close calculation */ |
216 |
|
caldone(); |
217 |
+ |
/* aft clipping */ |
218 |
+ |
clipaft(); |
219 |
|
/* add to header */ |
220 |
|
printargs(argc, argv, stdout); |
221 |
|
if (gotvfile) { |
248 |
|
headline(s) /* process header string */ |
249 |
|
char *s; |
250 |
|
{ |
240 |
– |
static char *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL}; |
241 |
– |
register char **an; |
251 |
|
char fmt[32]; |
252 |
|
|
253 |
< |
if (isformat(s)) { |
254 |
< |
formatval(fmt, s); |
253 |
> |
if (isheadid(s)) |
254 |
> |
return; |
255 |
> |
if (formatval(fmt, s)) { |
256 |
|
wrongformat = strcmp(fmt, COLRFMT); |
257 |
|
return; |
258 |
|
} |
263 |
|
theirexp *= exposval(s); |
264 |
|
return; |
265 |
|
} |
266 |
< |
for (an = altname; *an != NULL; an++) |
267 |
< |
if (!strncmp(*an, s, strlen(*an))) { |
258 |
< |
if (sscanview(&theirview, s+strlen(*an)) > 0) |
259 |
< |
gotview++; |
260 |
< |
break; |
261 |
< |
} |
266 |
> |
if (isview(s) && sscanview(&theirview, s) > 0) |
267 |
> |
gotview++; |
268 |
|
} |
269 |
|
|
270 |
|
|
271 |
|
addpicture(pfile, zspec) /* add picture to output */ |
272 |
|
char *pfile, *zspec; |
273 |
|
{ |
268 |
– |
extern double atof(); |
274 |
|
FILE *pfp; |
275 |
|
int zfd; |
276 |
|
char *err; |
277 |
|
COLR *scanin; |
278 |
|
float *zin; |
279 |
|
struct position *plast; |
280 |
+ |
struct bound *xlim, ylim; |
281 |
|
int y; |
282 |
|
/* open picture file */ |
283 |
< |
if ((pfp = fopen(pfile, "r")) == NULL) { |
284 |
< |
perror(pfile); |
279 |
< |
exit(1); |
280 |
< |
} |
283 |
> |
if ((pfp = fopen(pfile, "r")) == NULL) |
284 |
> |
syserror(pfile); |
285 |
|
/* get header with exposure and view */ |
286 |
|
theirexp = 1.0; |
287 |
|
gotview = 0; |
301 |
|
} |
302 |
|
/* compute transformation */ |
303 |
|
hasmatrix = pixform(theirs2ours, &theirview, &ourview); |
300 |
– |
/* allocate scanlines */ |
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(); |
304 |
|
/* get z specification or file */ |
305 |
+ |
zin = (float *)malloc(scanlen(&tresolu)*sizeof(float)); |
306 |
+ |
if (zin == NULL) |
307 |
+ |
syserror(progname); |
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 |
< |
} |
311 |
> |
if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) |
312 |
> |
syserror(zspec); |
313 |
|
for (x = scanlen(&tresolu); x-- > 0; ) |
314 |
|
zin[x] = zvalue; |
315 |
|
} |
316 |
< |
/* load image */ |
317 |
< |
for (y = 0; y < numscans(&tresolu); y++) { |
316 |
> |
/* compute transferrable perimeter */ |
317 |
> |
xlim = (struct bound *)malloc(numscans(&tresolu)*sizeof(struct bound)); |
318 |
> |
if (xlim == NULL) |
319 |
> |
syserror(progname); |
320 |
> |
if (!getperim(xlim, &ylim, zin, zfd)) { /* overlapping area? */ |
321 |
> |
free((char *)zin); |
322 |
> |
free((char *)xlim); |
323 |
> |
if (zfd != -1) |
324 |
> |
close(zfd); |
325 |
> |
fclose(pfp); |
326 |
> |
return; |
327 |
> |
} |
328 |
> |
/* allocate scanlines */ |
329 |
> |
scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR)); |
330 |
> |
plast = (struct position *)calloc(scanlen(&tresolu), |
331 |
> |
sizeof(struct position)); |
332 |
> |
if (scanin == NULL | plast == NULL) |
333 |
> |
syserror(progname); |
334 |
> |
/* skip to starting point */ |
335 |
> |
for (y = 0; y < ylim.min; y++) |
336 |
|
if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) { |
337 |
|
fprintf(stderr, "%s: read error\n", pfile); |
338 |
|
exit(1); |
339 |
|
} |
340 |
< |
if (zfd != -1 && read(zfd,(char *)zin, |
341 |
< |
scanlen(&tresolu)*sizeof(float)) |
342 |
< |
< scanlen(&tresolu)*sizeof(float)) { |
343 |
< |
fprintf(stderr, "%s: read error\n", zspec); |
340 |
> |
if (zfd != -1 && lseek(zfd, |
341 |
> |
(long)ylim.min*scanlen(&tresolu)*sizeof(float), 0) < 0) |
342 |
> |
syserror(zspec); |
343 |
> |
/* load image */ |
344 |
> |
for (y = ylim.min; y <= ylim.max; y++) { |
345 |
> |
if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) { |
346 |
> |
fprintf(stderr, "%s: read error\n", pfile); |
347 |
|
exit(1); |
348 |
|
} |
349 |
< |
addscanline(y, scanin, zin, plast); |
349 |
> |
if (zfd != -1 && read(zfd, (char *)zin, |
350 |
> |
scanlen(&tresolu)*sizeof(float)) |
351 |
> |
< scanlen(&tresolu)*sizeof(float)) |
352 |
> |
syserror(zspec); |
353 |
> |
addscanline(xlim+y, y, scanin, zin, plast); |
354 |
|
} |
355 |
|
/* clean up */ |
356 |
+ |
free((char *)xlim); |
357 |
|
free((char *)scanin); |
358 |
|
free((char *)zin); |
359 |
|
free((char *)plast); |
404 |
|
} |
405 |
|
|
406 |
|
|
407 |
< |
addscanline(y, pline, zline, lasty) /* add scanline to output */ |
407 |
> |
addscanline(xl, y, pline, zline, lasty) /* add scanline to output */ |
408 |
> |
struct bound *xl; |
409 |
|
int y; |
410 |
|
COLR *pline; |
411 |
|
float *zline; |
412 |
|
struct position *lasty; /* input/output */ |
413 |
|
{ |
389 |
– |
extern double sqrt(); |
414 |
|
FVECT pos; |
415 |
|
struct position lastx, newpos; |
416 |
|
register int x; |
417 |
|
|
418 |
|
lastx.z = 0; |
419 |
< |
for (x = scanlen(&tresolu); x-- > 0; ) { |
419 |
> |
for (x = xl->max; x >= xl->min; x--) { |
420 |
|
pix2loc(pos, &tresolu, x, y); |
421 |
|
pos[2] = zline[x]; |
422 |
|
if (movepixel(pos) < 0) { |
496 |
|
} |
497 |
|
|
498 |
|
|
499 |
+ |
getperim(xl, yl, zline, zfd) /* compute overlapping image area */ |
500 |
+ |
register struct bound *xl; |
501 |
+ |
struct bound *yl; |
502 |
+ |
float *zline; |
503 |
+ |
int zfd; |
504 |
+ |
{ |
505 |
+ |
int step; |
506 |
+ |
FVECT pos; |
507 |
+ |
register int x, y; |
508 |
+ |
/* set up step size */ |
509 |
+ |
if (scanlen(&tresolu) < numscans(&tresolu)) |
510 |
+ |
step = scanlen(&tresolu)/NSTEPS; |
511 |
+ |
else |
512 |
+ |
step = numscans(&tresolu)/NSTEPS; |
513 |
+ |
if (step < MINSTEP) { /* not worth cropping? */ |
514 |
+ |
yl->min = 0; |
515 |
+ |
yl->max = numscans(&tresolu) - 1; |
516 |
+ |
x = scanlen(&tresolu) - 1; |
517 |
+ |
for (y = numscans(&tresolu); y--; ) { |
518 |
+ |
xl[y].min = 0; |
519 |
+ |
xl[y].max = x; |
520 |
+ |
} |
521 |
+ |
return(1); |
522 |
+ |
} |
523 |
+ |
yl->min = 32000; yl->max = 0; /* search for points on image */ |
524 |
+ |
for (y = step - 1; y < numscans(&tresolu); y += step) { |
525 |
+ |
if (zfd != -1) { |
526 |
+ |
if (lseek(zfd, (long)y*scanlen(&tresolu)*sizeof(float), |
527 |
+ |
0) < 0) |
528 |
+ |
syserror("lseek"); |
529 |
+ |
if (read(zfd, (char *)zline, |
530 |
+ |
scanlen(&tresolu)*sizeof(float)) |
531 |
+ |
< scanlen(&tresolu)*sizeof(float)) |
532 |
+ |
syserror("read"); |
533 |
+ |
} |
534 |
+ |
xl[y].min = 32000; xl[y].max = 0; /* x max */ |
535 |
+ |
for (x = scanlen(&tresolu); (x -= step) > 0; ) { |
536 |
+ |
pix2loc(pos, &tresolu, x, y); |
537 |
+ |
pos[2] = zline[x]; |
538 |
+ |
if (movepixel(pos) == 0 && pos[0] >= 0 && |
539 |
+ |
pos[0] < 1 && pos[1] >= 0 && |
540 |
+ |
pos[1] < 1) { |
541 |
+ |
xl[y].max = x + step - 1; |
542 |
+ |
xl[y].min = x - step + 1; /* x min */ |
543 |
+ |
if (xl[y].min < 0) |
544 |
+ |
xl[y].min = 0; |
545 |
+ |
for (x = step - 1; x < xl[y].max; x += step) { |
546 |
+ |
pix2loc(pos, &tresolu, x, y); |
547 |
+ |
pos[2] = zline[x]; |
548 |
+ |
if (movepixel(pos) == 0 && |
549 |
+ |
pos[0] >= 0 && |
550 |
+ |
pos[0] < 1 && |
551 |
+ |
pos[1] >= 0 && |
552 |
+ |
pos[1] < 1) { |
553 |
+ |
xl[y].min = x - step + 1; |
554 |
+ |
break; |
555 |
+ |
} |
556 |
+ |
} |
557 |
+ |
if (y < yl->min) /* y limits */ |
558 |
+ |
yl->min = y - step + 1; |
559 |
+ |
yl->max = y + step - 1; |
560 |
+ |
break; |
561 |
+ |
} |
562 |
+ |
} |
563 |
+ |
/* fill in between */ |
564 |
+ |
if (xl[y].min < xl[y-step].min) |
565 |
+ |
xl[y-1].min = xl[y].min; |
566 |
+ |
else |
567 |
+ |
xl[y-1].min = xl[y-step].min; |
568 |
+ |
if (xl[y].max > xl[y-step].max) |
569 |
+ |
xl[y-1].max = xl[y].max; |
570 |
+ |
else |
571 |
+ |
xl[y-1].max = xl[y-step].max; |
572 |
+ |
for (x = 2; x < step; x++) |
573 |
+ |
copystruct(xl+y-x, xl+y-1); |
574 |
+ |
} |
575 |
+ |
if (yl->max >= numscans(&tresolu)) |
576 |
+ |
yl->max = numscans(&tresolu) - 1; |
577 |
+ |
for (x = numscans(&tresolu) - 1; x > y; x--) /* fill bottom rows */ |
578 |
+ |
copystruct(xl+x, xl+y); |
579 |
+ |
return(yl->max >= yl->min); |
580 |
+ |
} |
581 |
+ |
|
582 |
+ |
|
583 |
|
movepixel(pos) /* reposition image point */ |
584 |
|
FVECT pos; |
585 |
|
{ |
586 |
+ |
double d0, d1; |
587 |
|
FVECT pt, direc; |
588 |
|
|
589 |
|
if (pos[2] <= 0) /* empty pixel */ |
590 |
|
return(-1); |
591 |
+ |
if (normdist && theirview.type == VT_PER) { /* adjust distance */ |
592 |
+ |
d0 = pos[0] + theirview.hoff - .5; |
593 |
+ |
d1 = pos[1] + theirview.voff - .5; |
594 |
+ |
pos[2] /= sqrt(1. + d0*d0*theirview.hn2 + d1*d1*theirview.vn2); |
595 |
+ |
} |
596 |
|
if (hasmatrix) { |
597 |
|
pos[0] += theirview.hoff - .5; |
598 |
|
pos[1] += theirview.voff - .5; |
599 |
|
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 ); |
600 |
|
pos[0] *= pos[2]; |
601 |
|
pos[1] *= pos[2]; |
602 |
|
} |
611 |
|
pos[1] += .5 - ourview.voff; |
612 |
|
return(0); |
613 |
|
} |
614 |
< |
if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0) |
614 |
> |
if (viewray(pt, direc, &theirview, pos[0], pos[1]) < -FTINY) |
615 |
|
return(-1); |
616 |
|
pt[0] += direc[0]*pos[2]; |
617 |
|
pt[1] += direc[1]*pos[2]; |
633 |
|
/* get back buffer */ |
634 |
|
yback = (int *)malloc(hresolu*sizeof(int)); |
635 |
|
if (yback == NULL) |
636 |
< |
syserror(); |
636 |
> |
syserror(progname); |
637 |
|
for (x = 0; x < hresolu; x++) |
638 |
|
yback[x] = -2; |
639 |
|
/* |
730 |
|
} |
731 |
|
|
732 |
|
|
733 |
+ |
clipaft() /* perform aft clipping as indicated */ |
734 |
+ |
{ |
735 |
+ |
register int x, y; |
736 |
+ |
double tstdist; |
737 |
+ |
double yzn2, vx; |
738 |
+ |
|
739 |
+ |
if (ourview.vaft <= FTINY) |
740 |
+ |
return; |
741 |
+ |
tstdist = ourview.vaft; |
742 |
+ |
for (y = 0; y < vresolu; y++) { |
743 |
+ |
if (ourview.type == VT_PER) { /* adjust distance */ |
744 |
+ |
yzn2 = (y+.5)/vresolu + ourview.voff - .5; |
745 |
+ |
yzn2 = 1. + yzn2*yzn2*ourview.vn2; |
746 |
+ |
tstdist = ourview.vaft * sqrt(yzn2); |
747 |
+ |
} |
748 |
+ |
for (x = 0; x < hresolu; x++) |
749 |
+ |
if (zscan(y)[x] > tstdist) { |
750 |
+ |
if (ourview.type == VT_PER) { |
751 |
+ |
vx = (x+.5)/hresolu + ourview.hoff - .5; |
752 |
+ |
if (zscan(y)[x] <= ourview.vaft * |
753 |
+ |
sqrt(vx*vx*ourview.hn2 + yzn2)) |
754 |
+ |
continue; |
755 |
+ |
} |
756 |
+ |
bzero(pscan(y)[x], sizeof(COLR)); |
757 |
+ |
zscan(y)[x] = 0.0; |
758 |
+ |
} |
759 |
+ |
} |
760 |
+ |
} |
761 |
+ |
|
762 |
+ |
|
763 |
|
writepicture() /* write out picture */ |
764 |
|
{ |
765 |
|
int y; |
767 |
|
fprtresolu(hresolu, vresolu, stdout); |
768 |
|
for (y = vresolu-1; y >= 0; y--) |
769 |
|
if (fwritecolrs(pscan(y), hresolu, stdout) < 0) |
770 |
< |
syserror(); |
770 |
> |
syserror(progname); |
771 |
|
} |
772 |
|
|
773 |
|
|
774 |
|
writedistance(fname) /* write out z file */ |
775 |
|
char *fname; |
776 |
|
{ |
637 |
– |
extern double sqrt(); |
777 |
|
int donorm = normdist && ourview.type == VT_PER; |
778 |
|
int fd; |
779 |
|
int y; |
780 |
|
float *zout; |
781 |
|
|
782 |
< |
if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) { |
783 |
< |
perror(fname); |
645 |
< |
exit(1); |
646 |
< |
} |
782 |
> |
if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) |
783 |
> |
syserror(fname); |
784 |
|
if (donorm |
785 |
|
&& (zout = (float *)malloc(hresolu*sizeof(float))) == NULL) |
786 |
< |
syserror(); |
786 |
> |
syserror(progname); |
787 |
|
for (y = vresolu-1; y >= 0; y--) { |
788 |
|
if (donorm) { |
789 |
|
double vx, yzn2; |
798 |
|
} else |
799 |
|
zout = zscan(y); |
800 |
|
if (write(fd, (char *)zout, hresolu*sizeof(float)) |
801 |
< |
< hresolu*sizeof(float)) { |
802 |
< |
perror(fname); |
666 |
< |
exit(1); |
667 |
< |
} |
801 |
> |
< hresolu*sizeof(float)) |
802 |
> |
syserror(fname); |
803 |
|
} |
804 |
|
if (donorm) |
805 |
|
free((char *)zout); |
828 |
|
} |
829 |
|
|
830 |
|
|
831 |
< |
calstart(prog, args) /* start fill calculation */ |
831 |
> |
calstart(prog, args) /* start fill calculation */ |
832 |
|
char *prog, *args; |
833 |
|
{ |
834 |
|
char combuf[512]; |
835 |
< |
int p0[2], p1[2]; |
835 |
> |
char *argv[64]; |
836 |
> |
int rval; |
837 |
> |
register char **wp, *cp; |
838 |
|
|
839 |
|
if (childpid != -1) { |
840 |
|
fprintf(stderr, "%s: too many calculations\n", progname); |
841 |
|
exit(1); |
842 |
|
} |
843 |
< |
sprintf(combuf, prog, args); |
844 |
< |
if (pipe(p0) < 0 || pipe(p1) < 0) |
845 |
< |
syserror(); |
846 |
< |
if ((childpid = vfork()) == 0) { /* fork calculation */ |
847 |
< |
close(p0[1]); |
848 |
< |
close(p1[0]); |
849 |
< |
if (p0[0] != 0) { |
850 |
< |
dup2(p0[0], 0); |
851 |
< |
close(p0[0]); |
852 |
< |
} |
853 |
< |
if (p1[1] != 1) { |
854 |
< |
dup2(p1[1], 1); |
718 |
< |
close(p1[1]); |
719 |
< |
} |
720 |
< |
execl("/bin/sh", "sh", "-c", combuf, 0); |
721 |
< |
perror("/bin/sh"); |
722 |
< |
_exit(127); |
843 |
> |
strcpy(combuf, prog); |
844 |
> |
strcat(combuf, args); |
845 |
> |
cp = combuf; |
846 |
> |
wp = argv; |
847 |
> |
for ( ; ; ) { |
848 |
> |
while (isspace(*cp)) /* nullify spaces */ |
849 |
> |
*cp++ = '\0'; |
850 |
> |
if (!*cp) /* all done? */ |
851 |
> |
break; |
852 |
> |
*wp++ = cp; /* add argument to list */ |
853 |
> |
while (*++cp && !isspace(*cp)) |
854 |
> |
; |
855 |
|
} |
856 |
< |
if (childpid == -1) |
857 |
< |
syserror(); |
858 |
< |
close(p0[0]); |
859 |
< |
close(p1[1]); |
860 |
< |
if ((psend = fdopen(p0[1], "w")) == NULL) |
861 |
< |
syserror(); |
862 |
< |
if ((precv = fdopen(p1[0], "r")) == NULL) |
863 |
< |
syserror(); |
856 |
> |
*wp = NULL; |
857 |
> |
/* start process */ |
858 |
> |
if ((rval = open_process(PDesc, argv)) < 0) |
859 |
> |
syserror(progname); |
860 |
> |
if (rval == 0) { |
861 |
> |
fprintf(stderr, "%s: command not found\n", argv[0]); |
862 |
> |
exit(1); |
863 |
> |
} |
864 |
> |
packsiz = rval/(6*sizeof(float)) - 1; |
865 |
> |
if (packsiz > PACKSIZ) |
866 |
> |
packsiz = PACKSIZ; |
867 |
|
queuesiz = 0; |
868 |
|
} |
869 |
|
|
870 |
|
|
871 |
< |
caldone() /* done with calculation */ |
871 |
> |
caldone() /* done with calculation */ |
872 |
|
{ |
738 |
– |
int pid; |
739 |
– |
|
873 |
|
if (childpid == -1) |
874 |
|
return; |
875 |
|
clearqueue(); |
876 |
< |
fclose(psend); |
744 |
< |
fclose(precv); |
745 |
< |
while ((pid = wait(0)) != -1 && pid != childpid) |
746 |
< |
; |
876 |
> |
close_process(PDesc); |
877 |
|
childpid = -1; |
878 |
|
} |
879 |
|
|
881 |
|
rcalfill(x, y) /* fill with ray-calculated pixel */ |
882 |
|
int x, y; |
883 |
|
{ |
884 |
< |
if (queuesiz >= PACKSIZ) /* flush queue if needed */ |
884 |
> |
if (queuesiz >= packsiz) /* flush queue if needed */ |
885 |
|
clearqueue(); |
886 |
|
/* add position to queue */ |
887 |
|
queue[queuesiz][0] = x; |
893 |
|
clearqueue() /* process queue */ |
894 |
|
{ |
895 |
|
FVECT orig, dir; |
896 |
< |
float fbuf[6]; |
896 |
> |
float fbuf[6*(PACKSIZ+1)]; |
897 |
> |
register float *fbp; |
898 |
|
register int i; |
899 |
|
|
900 |
+ |
if (queuesiz == 0) |
901 |
+ |
return; |
902 |
+ |
fbp = fbuf; |
903 |
|
for (i = 0; i < queuesiz; i++) { |
904 |
|
viewray(orig, dir, &ourview, |
905 |
|
(queue[i][0]+.5)/hresolu, |
906 |
|
(queue[i][1]+.5)/vresolu); |
907 |
< |
fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2]; |
908 |
< |
fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2]; |
775 |
< |
fwrite((char *)fbuf, sizeof(float), 6, psend); |
907 |
> |
*fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2]; |
908 |
> |
*fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2]; |
909 |
|
} |
910 |
< |
/* flush output and get results */ |
911 |
< |
fbuf[3] = fbuf[4] = fbuf[5] = 0.0; /* mark */ |
912 |
< |
fwrite((char *)fbuf, sizeof(float), 6, psend); |
913 |
< |
if (fflush(psend) == EOF) |
914 |
< |
syserror(); |
910 |
> |
/* mark end and get results */ |
911 |
> |
bzero((char *)fbp, 6*sizeof(float)); |
912 |
> |
if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz, |
913 |
> |
6*sizeof(float)*(queuesiz+1)) != |
914 |
> |
4*sizeof(float)*queuesiz) { |
915 |
> |
fprintf(stderr, "%s: error reading from rtrace process\n", |
916 |
> |
progname); |
917 |
> |
exit(1); |
918 |
> |
} |
919 |
> |
fbp = fbuf; |
920 |
|
for (i = 0; i < queuesiz; i++) { |
783 |
– |
if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) { |
784 |
– |
fprintf(stderr, "%s: read error in clearqueue\n", |
785 |
– |
progname); |
786 |
– |
exit(1); |
787 |
– |
} |
921 |
|
if (ourexp > 0 && ourexp != 1.0) { |
922 |
< |
fbuf[0] *= ourexp; |
923 |
< |
fbuf[1] *= ourexp; |
924 |
< |
fbuf[2] *= ourexp; |
922 |
> |
fbp[0] *= ourexp; |
923 |
> |
fbp[1] *= ourexp; |
924 |
> |
fbp[2] *= ourexp; |
925 |
|
} |
926 |
|
setcolr(pscan(queue[i][1])[queue[i][0]], |
927 |
< |
fbuf[0], fbuf[1], fbuf[2]); |
928 |
< |
zscan(queue[i][1])[queue[i][0]] = fbuf[3]; |
927 |
> |
fbp[0], fbp[1], fbp[2]); |
928 |
> |
zscan(queue[i][1])[queue[i][0]] = fbp[3]; |
929 |
> |
fbp += 4; |
930 |
|
} |
931 |
|
queuesiz = 0; |
932 |
|
} |
933 |
|
|
934 |
|
|
935 |
< |
syserror() /* report error and exit */ |
935 |
> |
syserror(s) /* report error and exit */ |
936 |
> |
char *s; |
937 |
|
{ |
938 |
< |
perror(progname); |
938 |
> |
perror(s); |
939 |
|
exit(1); |
940 |
|
} |