| 12 |
|
|
| 13 |
|
#include "standard.h" |
| 14 |
|
|
| 15 |
< |
#include <fcntl.h> |
| 15 |
> |
#include <ctype.h> |
| 16 |
|
|
| 17 |
|
#include "view.h" |
| 18 |
|
|
| 30 |
|
#define F_FORE 1 /* fill foreground */ |
| 31 |
|
#define F_BACK 2 /* fill background */ |
| 32 |
|
|
| 33 |
< |
#define PACKSIZ 42 /* calculation packet size */ |
| 33 |
> |
#define PACKSIZ 256 /* max. calculation packet size */ |
| 34 |
|
|
| 35 |
< |
#define RTCOM "rtrace -h -ovl -fff %s" |
| 35 |
> |
#define RTCOM "rtrace -h- -ovl -fff " |
| 36 |
|
|
| 37 |
|
#define ABS(x) ((x)>0?(x):-(x)) |
| 38 |
|
|
| 67 |
|
double theirs2ours[4][4]; /* transformation matrix */ |
| 68 |
|
int hasmatrix = 0; /* has transformation matrix */ |
| 69 |
|
|
| 70 |
< |
int childpid = -1; /* id of fill process */ |
| 71 |
< |
FILE *psend, *precv; /* pipes to/from fill calculation */ |
| 72 |
< |
int queue[PACKSIZ][2]; /* pending pixels */ |
| 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 |
|
|
| 78 |
|
int argc; |
| 79 |
|
char *argv[]; |
| 80 |
|
{ |
| 81 |
< |
#define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt |
| 82 |
< |
extern double atof(); |
| 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; |
| 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 'n': /* dist. normalized? */ |
| 103 |
< |
check(2,0); |
| 103 |
> |
check(2,NULL); |
| 104 |
|
normdist = !normdist; |
| 105 |
|
break; |
| 106 |
|
case 'f': /* fill type */ |
| 107 |
|
switch (argv[i][2]) { |
| 108 |
|
case '0': /* none */ |
| 109 |
< |
check(3,0); |
| 109 |
> |
check(3,NULL); |
| 110 |
|
fillo = 0; |
| 111 |
|
break; |
| 112 |
|
case 'f': /* foreground */ |
| 113 |
< |
check(3,0); |
| 113 |
> |
check(3,NULL); |
| 114 |
|
fillo = F_FORE; |
| 115 |
|
break; |
| 116 |
|
case 'b': /* background */ |
| 117 |
< |
check(3,0); |
| 117 |
> |
check(3,NULL); |
| 118 |
|
fillo = F_BACK; |
| 119 |
|
break; |
| 120 |
|
case 'a': /* all */ |
| 121 |
< |
check(3,0); |
| 121 |
> |
check(3,NULL); |
| 122 |
|
fillo = F_FORE|F_BACK; |
| 123 |
|
break; |
| 124 |
|
case 's': /* sample */ |
| 125 |
< |
check(3,1); |
| 125 |
> |
check(3,"i"); |
| 126 |
|
fillsamp = atoi(argv[++i]); |
| 127 |
|
break; |
| 128 |
|
case 'c': /* color */ |
| 129 |
< |
check(3,3); |
| 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 'z': /* z value */ |
| 136 |
< |
check(3,1); |
| 136 |
> |
check(3,"f"); |
| 137 |
|
fillfunc = backfill; |
| 138 |
|
backz = atof(argv[++i]); |
| 139 |
|
break; |
| 140 |
|
case 'r': /* rtrace */ |
| 141 |
< |
check(3,1); |
| 141 |
> |
check(3,"s"); |
| 142 |
|
fillfunc = rcalfill; |
| 143 |
|
calstart(RTCOM, argv[++i]); |
| 144 |
|
break; |
| 147 |
|
} |
| 148 |
|
break; |
| 149 |
|
case 'z': /* z file */ |
| 150 |
< |
check(2,1); |
| 150 |
> |
check(2,"s"); |
| 151 |
|
zfile = argv[++i]; |
| 152 |
|
break; |
| 153 |
|
case 'x': /* x resolution */ |
| 154 |
< |
check(2,1); |
| 154 |
> |
check(2,"i"); |
| 155 |
|
hresolu = atoi(argv[++i]); |
| 156 |
|
break; |
| 157 |
|
case 'y': /* y resolution */ |
| 158 |
< |
check(2,1); |
| 158 |
> |
check(2,"i"); |
| 159 |
|
vresolu = atoi(argv[++i]); |
| 160 |
|
break; |
| 161 |
|
case 'p': /* pixel aspect */ |
| 162 |
< |
check(2,1); |
| 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,1); |
| 170 |
> |
check(3,"s"); |
| 171 |
|
gotvfile = viewfile(argv[++i], &ourview, 0, 0); |
| 172 |
|
if (gotvfile < 0) { |
| 173 |
|
perror(argv[i]); |
| 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); |
| 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(); |
| 204 |
+ |
bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float)); |
| 205 |
|
/* get input */ |
| 206 |
|
for ( ; i < argc; i += 2) |
| 207 |
|
addpicture(argv[i], argv[i+1]); |
| 244 |
|
headline(s) /* process header string */ |
| 245 |
|
char *s; |
| 246 |
|
{ |
| 240 |
– |
static char *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL}; |
| 241 |
– |
register char **an; |
| 247 |
|
char fmt[32]; |
| 248 |
|
|
| 249 |
|
if (isformat(s)) { |
| 258 |
|
theirexp *= exposval(s); |
| 259 |
|
return; |
| 260 |
|
} |
| 261 |
< |
for (an = altname; *an != NULL; an++) |
| 262 |
< |
if (!strncmp(*an, s, strlen(*an))) { |
| 258 |
< |
if (sscanview(&theirview, s+strlen(*an)) > 0) |
| 259 |
< |
gotview++; |
| 260 |
< |
break; |
| 261 |
< |
} |
| 261 |
> |
if (isview(s) && sscanview(&theirview, s) > 0) |
| 262 |
> |
gotview++; |
| 263 |
|
} |
| 264 |
|
|
| 265 |
|
|
| 266 |
|
addpicture(pfile, zspec) /* add picture to output */ |
| 267 |
|
char *pfile, *zspec; |
| 268 |
|
{ |
| 268 |
– |
extern double atof(); |
| 269 |
|
FILE *pfp; |
| 270 |
|
int zfd; |
| 271 |
|
char *err; |
| 386 |
|
float *zline; |
| 387 |
|
struct position *lasty; /* input/output */ |
| 388 |
|
{ |
| 389 |
– |
extern double sqrt(); |
| 389 |
|
FVECT pos; |
| 390 |
|
struct position lastx, newpos; |
| 391 |
|
register int x; |
| 633 |
|
writedistance(fname) /* write out z file */ |
| 634 |
|
char *fname; |
| 635 |
|
{ |
| 637 |
– |
extern double sqrt(); |
| 636 |
|
int donorm = normdist && ourview.type == VT_PER; |
| 637 |
|
int fd; |
| 638 |
|
int y; |
| 691 |
|
} |
| 692 |
|
|
| 693 |
|
|
| 694 |
< |
calstart(prog, args) /* start fill calculation */ |
| 694 |
> |
calstart(prog, args) /* start fill calculation */ |
| 695 |
|
char *prog, *args; |
| 696 |
|
{ |
| 697 |
|
char combuf[512]; |
| 698 |
< |
int p0[2], p1[2]; |
| 698 |
> |
char *argv[64]; |
| 699 |
> |
int rval; |
| 700 |
> |
register char **wp, *cp; |
| 701 |
|
|
| 702 |
|
if (childpid != -1) { |
| 703 |
|
fprintf(stderr, "%s: too many calculations\n", progname); |
| 704 |
|
exit(1); |
| 705 |
|
} |
| 706 |
< |
sprintf(combuf, prog, args); |
| 707 |
< |
if (pipe(p0) < 0 || pipe(p1) < 0) |
| 708 |
< |
syserror(); |
| 709 |
< |
if ((childpid = vfork()) == 0) { /* fork calculation */ |
| 710 |
< |
close(p0[1]); |
| 711 |
< |
close(p1[0]); |
| 712 |
< |
if (p0[0] != 0) { |
| 713 |
< |
dup2(p0[0], 0); |
| 714 |
< |
close(p0[0]); |
| 715 |
< |
} |
| 716 |
< |
if (p1[1] != 1) { |
| 717 |
< |
dup2(p1[1], 1); |
| 718 |
< |
close(p1[1]); |
| 719 |
< |
} |
| 720 |
< |
execl("/bin/sh", "sh", "-c", combuf, 0); |
| 721 |
< |
perror("/bin/sh"); |
| 722 |
< |
_exit(127); |
| 706 |
> |
strcpy(combuf, prog); |
| 707 |
> |
strcat(combuf, args); |
| 708 |
> |
cp = combuf; |
| 709 |
> |
wp = argv; |
| 710 |
> |
for ( ; ; ) { |
| 711 |
> |
while (isspace(*cp)) cp++; |
| 712 |
> |
if (!*cp) break; |
| 713 |
> |
*wp++ = cp; |
| 714 |
> |
while (!isspace(*cp)) |
| 715 |
> |
if (!*cp++) goto done; |
| 716 |
> |
*cp++ = '\0'; |
| 717 |
|
} |
| 718 |
< |
if (childpid == -1) |
| 718 |
> |
done: |
| 719 |
> |
*wp = NULL; |
| 720 |
> |
/* start process */ |
| 721 |
> |
if ((rval = open_process(PDesc, argv)) < 0) |
| 722 |
|
syserror(); |
| 723 |
< |
close(p0[0]); |
| 724 |
< |
close(p1[1]); |
| 725 |
< |
if ((psend = fdopen(p0[1], "w")) == NULL) |
| 726 |
< |
syserror(); |
| 727 |
< |
if ((precv = fdopen(p1[0], "r")) == NULL) |
| 728 |
< |
syserror(); |
| 723 |
> |
if (rval == 0) { |
| 724 |
> |
fprintf(stderr, "%s: command not found\n", argv[0]); |
| 725 |
> |
exit(1); |
| 726 |
> |
} |
| 727 |
> |
packsiz = rval/(6*sizeof(float)) - 1; |
| 728 |
> |
if (packsiz > PACKSIZ) |
| 729 |
> |
packsiz = PACKSIZ; |
| 730 |
|
queuesiz = 0; |
| 731 |
|
} |
| 732 |
|
|
| 733 |
|
|
| 734 |
< |
caldone() /* done with calculation */ |
| 734 |
> |
caldone() /* done with calculation */ |
| 735 |
|
{ |
| 738 |
– |
int pid; |
| 739 |
– |
|
| 736 |
|
if (childpid == -1) |
| 737 |
|
return; |
| 738 |
|
clearqueue(); |
| 739 |
< |
fclose(psend); |
| 744 |
< |
fclose(precv); |
| 745 |
< |
while ((pid = wait(0)) != -1 && pid != childpid) |
| 746 |
< |
; |
| 739 |
> |
close_process(PDesc); |
| 740 |
|
childpid = -1; |
| 741 |
|
} |
| 742 |
|
|
| 744 |
|
rcalfill(x, y) /* fill with ray-calculated pixel */ |
| 745 |
|
int x, y; |
| 746 |
|
{ |
| 747 |
< |
if (queuesiz >= PACKSIZ) /* flush queue if needed */ |
| 747 |
> |
if (queuesiz >= packsiz) /* flush queue if needed */ |
| 748 |
|
clearqueue(); |
| 749 |
|
/* add position to queue */ |
| 750 |
|
queue[queuesiz][0] = x; |
| 756 |
|
clearqueue() /* process queue */ |
| 757 |
|
{ |
| 758 |
|
FVECT orig, dir; |
| 759 |
< |
float fbuf[6]; |
| 759 |
> |
float fbuf[6*(PACKSIZ+1)]; |
| 760 |
> |
register float *fbp; |
| 761 |
|
register int i; |
| 762 |
|
|
| 763 |
+ |
fbp = fbuf; |
| 764 |
|
for (i = 0; i < queuesiz; i++) { |
| 765 |
|
viewray(orig, dir, &ourview, |
| 766 |
|
(queue[i][0]+.5)/hresolu, |
| 767 |
|
(queue[i][1]+.5)/vresolu); |
| 768 |
< |
fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2]; |
| 769 |
< |
fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2]; |
| 775 |
< |
fwrite((char *)fbuf, sizeof(float), 6, psend); |
| 768 |
> |
*fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2]; |
| 769 |
> |
*fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2]; |
| 770 |
|
} |
| 771 |
< |
/* flush output and get results */ |
| 772 |
< |
fbuf[3] = fbuf[4] = fbuf[5] = 0.0; /* mark */ |
| 773 |
< |
fwrite((char *)fbuf, sizeof(float), 6, psend); |
| 774 |
< |
if (fflush(psend) == EOF) |
| 775 |
< |
syserror(); |
| 771 |
> |
/* mark end and get results */ |
| 772 |
> |
bzero((char *)fbp, 6*sizeof(float)); |
| 773 |
> |
if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz, |
| 774 |
> |
6*sizeof(float)*(queuesiz+1)) != |
| 775 |
> |
4*sizeof(float)*queuesiz) { |
| 776 |
> |
fprintf(stderr, "%s: error reading from rtrace process\n", |
| 777 |
> |
progname); |
| 778 |
> |
exit(1); |
| 779 |
> |
} |
| 780 |
> |
fbp = fbuf; |
| 781 |
|
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 |
– |
} |
| 782 |
|
if (ourexp > 0 && ourexp != 1.0) { |
| 783 |
< |
fbuf[0] *= ourexp; |
| 784 |
< |
fbuf[1] *= ourexp; |
| 785 |
< |
fbuf[2] *= ourexp; |
| 783 |
> |
fbp[0] *= ourexp; |
| 784 |
> |
fbp[1] *= ourexp; |
| 785 |
> |
fbp[2] *= ourexp; |
| 786 |
|
} |
| 787 |
|
setcolr(pscan(queue[i][1])[queue[i][0]], |
| 788 |
< |
fbuf[0], fbuf[1], fbuf[2]); |
| 789 |
< |
zscan(queue[i][1])[queue[i][0]] = fbuf[3]; |
| 788 |
> |
fbp[0], fbp[1], fbp[2]); |
| 789 |
> |
zscan(queue[i][1])[queue[i][0]] = fbp[3]; |
| 790 |
> |
fbp += 4; |
| 791 |
|
} |
| 792 |
|
queuesiz = 0; |
| 793 |
|
} |