1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1993 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 |
|
|
63 |
|
double theirs2ours[4][4]; /* transformation matrix */ |
64 |
|
int hasmatrix = 0; /* has transformation matrix */ |
65 |
|
|
66 |
< |
int childpid = -1; /* id of fill process */ |
67 |
< |
FILE *psend, *precv; /* pipes to/from fill calculation */ |
68 |
< |
int queue[PACKSIZ][2]; /* pending pixels */ |
66 |
> |
int PDesc[3] = {-1,-1,-1}; /* rtrace process descriptor */ |
67 |
> |
#define childpid (PDesc[2]) |
68 |
> |
unsigned short queue[PACKSIZ][2]; /* pending pixels */ |
69 |
> |
int packsiz; /* actual packet size */ |
70 |
|
int queuesiz; /* number of pixels pending */ |
71 |
|
|
72 |
|
|
74 |
|
int argc; |
75 |
|
char *argv[]; |
76 |
|
{ |
77 |
< |
#define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt |
77 |
> |
#define check(ol,al) if (argv[i][ol] || \ |
78 |
> |
badarg(argc-i-1,argv+i+1,al)) \ |
79 |
> |
goto badopt |
80 |
|
int gotvfile = 0; |
81 |
|
char *zfile = NULL; |
82 |
|
char *err; |
92 |
|
} |
93 |
|
switch (argv[i][1]) { |
94 |
|
case 't': /* threshold */ |
95 |
< |
check(2,1); |
95 |
> |
check(2,"f"); |
96 |
|
zeps = atof(argv[++i]); |
97 |
|
break; |
98 |
|
case 'n': /* dist. normalized? */ |
99 |
< |
check(2,0); |
99 |
> |
check(2,NULL); |
100 |
|
normdist = !normdist; |
101 |
|
break; |
102 |
|
case 'f': /* fill type */ |
103 |
|
switch (argv[i][2]) { |
104 |
|
case '0': /* none */ |
105 |
< |
check(3,0); |
105 |
> |
check(3,NULL); |
106 |
|
fillo = 0; |
107 |
|
break; |
108 |
|
case 'f': /* foreground */ |
109 |
< |
check(3,0); |
109 |
> |
check(3,NULL); |
110 |
|
fillo = F_FORE; |
111 |
|
break; |
112 |
|
case 'b': /* background */ |
113 |
< |
check(3,0); |
113 |
> |
check(3,NULL); |
114 |
|
fillo = F_BACK; |
115 |
|
break; |
116 |
|
case 'a': /* all */ |
117 |
< |
check(3,0); |
117 |
> |
check(3,NULL); |
118 |
|
fillo = F_FORE|F_BACK; |
119 |
|
break; |
120 |
|
case 's': /* sample */ |
121 |
< |
check(3,1); |
121 |
> |
check(3,"i"); |
122 |
|
fillsamp = atoi(argv[++i]); |
123 |
|
break; |
124 |
|
case 'c': /* color */ |
125 |
< |
check(3,3); |
125 |
> |
check(3,"fff"); |
126 |
|
fillfunc = backfill; |
127 |
|
setcolr(backcolr, atof(argv[i+1]), |
128 |
|
atof(argv[i+2]), atof(argv[i+3])); |
129 |
|
i += 3; |
130 |
|
break; |
131 |
|
case 'z': /* z value */ |
132 |
< |
check(3,1); |
132 |
> |
check(3,"f"); |
133 |
|
fillfunc = backfill; |
134 |
|
backz = atof(argv[++i]); |
135 |
|
break; |
136 |
|
case 'r': /* rtrace */ |
137 |
< |
check(3,1); |
137 |
> |
check(3,"s"); |
138 |
|
fillfunc = rcalfill; |
139 |
|
calstart(RTCOM, argv[++i]); |
140 |
|
break; |
143 |
|
} |
144 |
|
break; |
145 |
|
case 'z': /* z file */ |
146 |
< |
check(2,1); |
146 |
> |
check(2,"s"); |
147 |
|
zfile = argv[++i]; |
148 |
|
break; |
149 |
|
case 'x': /* x resolution */ |
150 |
< |
check(2,1); |
150 |
> |
check(2,"i"); |
151 |
|
hresolu = atoi(argv[++i]); |
152 |
|
break; |
153 |
|
case 'y': /* y resolution */ |
154 |
< |
check(2,1); |
154 |
> |
check(2,"i"); |
155 |
|
vresolu = atoi(argv[++i]); |
156 |
|
break; |
157 |
|
case 'p': /* pixel aspect */ |
158 |
< |
check(2,1); |
158 |
> |
if (argv[i][2] != 'a') |
159 |
> |
goto badopt; |
160 |
> |
check(3,"f"); |
161 |
|
pixaspect = atof(argv[++i]); |
162 |
|
break; |
163 |
|
case 'v': /* view file */ |
164 |
|
if (argv[i][2] != 'f') |
165 |
|
goto badopt; |
166 |
< |
check(3,1); |
166 |
> |
check(3,"s"); |
167 |
|
gotvfile = viewfile(argv[++i], &ourview, 0, 0); |
168 |
< |
if (gotvfile < 0) { |
169 |
< |
perror(argv[i]); |
170 |
< |
exit(1); |
170 |
< |
} else if (gotvfile == 0) { |
168 |
> |
if (gotvfile < 0) |
169 |
> |
syserror(argv[i]); |
170 |
> |
else if (gotvfile == 0) { |
171 |
|
fprintf(stderr, "%s: bad view file\n", |
172 |
|
argv[i]); |
173 |
|
exit(1); |
183 |
|
/* check arguments */ |
184 |
|
if ((argc-i)%2) |
185 |
|
goto userr; |
186 |
+ |
if (fillsamp == 1) |
187 |
+ |
fillo &= ~F_BACK; |
188 |
|
/* set view */ |
189 |
|
if (err = setview(&ourview)) { |
190 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
192 |
|
} |
193 |
|
normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu); |
194 |
|
/* allocate frame */ |
195 |
< |
ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR)); |
196 |
< |
ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float)); |
195 |
> |
ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR)); |
196 |
> |
ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float)); |
197 |
|
if (ourpict == NULL || ourzbuf == NULL) |
198 |
< |
syserror(); |
198 |
> |
syserror(progname); |
199 |
> |
bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float)); |
200 |
|
/* get input */ |
201 |
|
for ( ; i < argc; i += 2) |
202 |
|
addpicture(argv[i], argv[i+1]); |
239 |
|
headline(s) /* process header string */ |
240 |
|
char *s; |
241 |
|
{ |
239 |
– |
static char *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL}; |
240 |
– |
register char **an; |
242 |
|
char fmt[32]; |
243 |
|
|
244 |
|
if (isformat(s)) { |
253 |
|
theirexp *= exposval(s); |
254 |
|
return; |
255 |
|
} |
256 |
< |
for (an = altname; *an != NULL; an++) |
257 |
< |
if (!strncmp(*an, s, strlen(*an))) { |
257 |
< |
if (sscanview(&theirview, s+strlen(*an)) > 0) |
258 |
< |
gotview++; |
259 |
< |
break; |
260 |
< |
} |
256 |
> |
if (isview(s) && sscanview(&theirview, s) > 0) |
257 |
> |
gotview++; |
258 |
|
} |
259 |
|
|
260 |
|
|
269 |
|
struct position *plast; |
270 |
|
int y; |
271 |
|
/* open picture file */ |
272 |
< |
if ((pfp = fopen(pfile, "r")) == NULL) { |
273 |
< |
perror(pfile); |
277 |
< |
exit(1); |
278 |
< |
} |
272 |
> |
if ((pfp = fopen(pfile, "r")) == NULL) |
273 |
> |
syserror(pfile); |
274 |
|
/* get header with exposure and view */ |
275 |
|
theirexp = 1.0; |
276 |
|
gotview = 0; |
296 |
|
plast = (struct position *)calloc(scanlen(&tresolu), |
297 |
|
sizeof(struct position)); |
298 |
|
if (scanin == NULL || zin == NULL || plast == NULL) |
299 |
< |
syserror(); |
299 |
> |
syserror(progname); |
300 |
|
/* get z specification or file */ |
301 |
|
if ((zfd = open(zspec, O_RDONLY)) == -1) { |
302 |
|
double zvalue; |
303 |
|
register int x; |
304 |
< |
if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) { |
305 |
< |
perror(zspec); |
304 |
> |
if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) |
305 |
> |
syserror(zspec); |
306 |
|
exit(1); |
312 |
– |
} |
307 |
|
for (x = scanlen(&tresolu); x-- > 0; ) |
308 |
|
zin[x] = zvalue; |
309 |
|
} |
378 |
|
float *zline; |
379 |
|
struct position *lasty; /* input/output */ |
380 |
|
{ |
387 |
– |
extern double sqrt(); |
381 |
|
FVECT pos; |
382 |
|
struct position lastx, newpos; |
383 |
|
register int x; |
514 |
|
/* get back buffer */ |
515 |
|
yback = (int *)malloc(hresolu*sizeof(int)); |
516 |
|
if (yback == NULL) |
517 |
< |
syserror(); |
517 |
> |
syserror(progname); |
518 |
|
for (x = 0; x < hresolu; x++) |
519 |
|
yback[x] = -2; |
520 |
|
/* |
618 |
|
fprtresolu(hresolu, vresolu, stdout); |
619 |
|
for (y = vresolu-1; y >= 0; y--) |
620 |
|
if (fwritecolrs(pscan(y), hresolu, stdout) < 0) |
621 |
< |
syserror(); |
621 |
> |
syserror(progname); |
622 |
|
} |
623 |
|
|
624 |
|
|
625 |
|
writedistance(fname) /* write out z file */ |
626 |
|
char *fname; |
627 |
|
{ |
635 |
– |
extern double sqrt(); |
628 |
|
int donorm = normdist && ourview.type == VT_PER; |
629 |
|
int fd; |
630 |
|
int y; |
631 |
|
float *zout; |
632 |
|
|
633 |
< |
if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) { |
634 |
< |
perror(fname); |
643 |
< |
exit(1); |
644 |
< |
} |
633 |
> |
if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) |
634 |
> |
syserror(fname); |
635 |
|
if (donorm |
636 |
|
&& (zout = (float *)malloc(hresolu*sizeof(float))) == NULL) |
637 |
< |
syserror(); |
637 |
> |
syserror(progname); |
638 |
|
for (y = vresolu-1; y >= 0; y--) { |
639 |
|
if (donorm) { |
640 |
|
double vx, yzn2; |
649 |
|
} else |
650 |
|
zout = zscan(y); |
651 |
|
if (write(fd, (char *)zout, hresolu*sizeof(float)) |
652 |
< |
< hresolu*sizeof(float)) { |
653 |
< |
perror(fname); |
664 |
< |
exit(1); |
665 |
< |
} |
652 |
> |
< hresolu*sizeof(float)) |
653 |
> |
syserror(fname); |
654 |
|
} |
655 |
|
if (donorm) |
656 |
|
free((char *)zout); |
679 |
|
} |
680 |
|
|
681 |
|
|
682 |
< |
calstart(prog, args) /* start fill calculation */ |
682 |
> |
calstart(prog, args) /* start fill calculation */ |
683 |
|
char *prog, *args; |
684 |
|
{ |
685 |
|
char combuf[512]; |
686 |
< |
int p0[2], p1[2]; |
686 |
> |
char *argv[64]; |
687 |
> |
int rval; |
688 |
> |
register char **wp, *cp; |
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) { |
715 |
< |
dup2(p1[1], 1); |
716 |
< |
close(p1[1]); |
717 |
< |
} |
718 |
< |
execl("/bin/sh", "sh", "-c", combuf, 0); |
719 |
< |
perror("/bin/sh"); |
720 |
< |
_exit(127); |
694 |
> |
strcpy(combuf, prog); |
695 |
> |
strcat(combuf, args); |
696 |
> |
cp = combuf; |
697 |
> |
wp = argv; |
698 |
> |
for ( ; ; ) { |
699 |
> |
while (isspace(*cp)) cp++; |
700 |
> |
if (!*cp) break; |
701 |
> |
*wp++ = cp; |
702 |
> |
while (!isspace(*cp)) |
703 |
> |
if (!*cp++) goto done; |
704 |
> |
*cp++ = '\0'; |
705 |
|
} |
706 |
< |
if (childpid == -1) |
707 |
< |
syserror(); |
708 |
< |
close(p0[0]); |
709 |
< |
close(p1[1]); |
710 |
< |
if ((psend = fdopen(p0[1], "w")) == NULL) |
711 |
< |
syserror(); |
712 |
< |
if ((precv = fdopen(p1[0], "r")) == NULL) |
713 |
< |
syserror(); |
706 |
> |
done: |
707 |
> |
*wp = NULL; |
708 |
> |
/* start process */ |
709 |
> |
if ((rval = open_process(PDesc, argv)) < 0) |
710 |
> |
syserror(progname); |
711 |
> |
if (rval == 0) { |
712 |
> |
fprintf(stderr, "%s: command not found\n", argv[0]); |
713 |
> |
exit(1); |
714 |
> |
} |
715 |
> |
packsiz = rval/(6*sizeof(float)) - 1; |
716 |
> |
if (packsiz > PACKSIZ) |
717 |
> |
packsiz = PACKSIZ; |
718 |
|
queuesiz = 0; |
719 |
|
} |
720 |
|
|
721 |
|
|
722 |
< |
caldone() /* done with calculation */ |
722 |
> |
caldone() /* done with calculation */ |
723 |
|
{ |
736 |
– |
int pid; |
737 |
– |
|
724 |
|
if (childpid == -1) |
725 |
|
return; |
726 |
|
clearqueue(); |
727 |
< |
fclose(psend); |
742 |
< |
fclose(precv); |
743 |
< |
while ((pid = wait(0)) != -1 && pid != childpid) |
744 |
< |
; |
727 |
> |
close_process(PDesc); |
728 |
|
childpid = -1; |
729 |
|
} |
730 |
|
|
732 |
|
rcalfill(x, y) /* fill with ray-calculated pixel */ |
733 |
|
int x, y; |
734 |
|
{ |
735 |
< |
if (queuesiz >= PACKSIZ) /* flush queue if needed */ |
735 |
> |
if (queuesiz >= packsiz) /* flush queue if needed */ |
736 |
|
clearqueue(); |
737 |
|
/* add position to queue */ |
738 |
|
queue[queuesiz][0] = x; |
744 |
|
clearqueue() /* process queue */ |
745 |
|
{ |
746 |
|
FVECT orig, dir; |
747 |
< |
float fbuf[6]; |
747 |
> |
float fbuf[6*(PACKSIZ+1)]; |
748 |
> |
register float *fbp; |
749 |
|
register int i; |
750 |
|
|
751 |
+ |
if (queuesiz == 0) |
752 |
+ |
return; |
753 |
+ |
fbp = fbuf; |
754 |
|
for (i = 0; i < queuesiz; i++) { |
755 |
|
viewray(orig, dir, &ourview, |
756 |
|
(queue[i][0]+.5)/hresolu, |
757 |
|
(queue[i][1]+.5)/vresolu); |
758 |
< |
fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2]; |
759 |
< |
fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2]; |
773 |
< |
fwrite((char *)fbuf, sizeof(float), 6, psend); |
758 |
> |
*fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2]; |
759 |
> |
*fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2]; |
760 |
|
} |
761 |
< |
/* flush output and get results */ |
762 |
< |
fbuf[3] = fbuf[4] = fbuf[5] = 0.0; /* mark */ |
763 |
< |
fwrite((char *)fbuf, sizeof(float), 6, psend); |
764 |
< |
if (fflush(psend) == EOF) |
765 |
< |
syserror(); |
761 |
> |
/* mark end and get results */ |
762 |
> |
bzero((char *)fbp, 6*sizeof(float)); |
763 |
> |
if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz, |
764 |
> |
6*sizeof(float)*(queuesiz+1)) != |
765 |
> |
4*sizeof(float)*queuesiz) { |
766 |
> |
fprintf(stderr, "%s: error reading from rtrace process\n", |
767 |
> |
progname); |
768 |
> |
exit(1); |
769 |
> |
} |
770 |
> |
fbp = fbuf; |
771 |
|
for (i = 0; i < queuesiz; i++) { |
781 |
– |
if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) { |
782 |
– |
fprintf(stderr, "%s: read error in clearqueue\n", |
783 |
– |
progname); |
784 |
– |
exit(1); |
785 |
– |
} |
772 |
|
if (ourexp > 0 && ourexp != 1.0) { |
773 |
< |
fbuf[0] *= ourexp; |
774 |
< |
fbuf[1] *= ourexp; |
775 |
< |
fbuf[2] *= ourexp; |
773 |
> |
fbp[0] *= ourexp; |
774 |
> |
fbp[1] *= ourexp; |
775 |
> |
fbp[2] *= ourexp; |
776 |
|
} |
777 |
|
setcolr(pscan(queue[i][1])[queue[i][0]], |
778 |
< |
fbuf[0], fbuf[1], fbuf[2]); |
779 |
< |
zscan(queue[i][1])[queue[i][0]] = fbuf[3]; |
778 |
> |
fbp[0], fbp[1], fbp[2]); |
779 |
> |
zscan(queue[i][1])[queue[i][0]] = fbp[3]; |
780 |
> |
fbp += 4; |
781 |
|
} |
782 |
|
queuesiz = 0; |
783 |
|
} |
784 |
|
|
785 |
|
|
786 |
< |
syserror() /* report error and exit */ |
786 |
> |
syserror(s) /* report error and exit */ |
787 |
> |
char *s; |
788 |
|
{ |
789 |
< |
perror(progname); |
789 |
> |
perror(s); |
790 |
|
exit(1); |
791 |
|
} |