ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
(Generate patch)

Comparing ray/src/px/pinterp.c (file contents):
Revision 2.1 by greg, Tue Nov 12 16:04:39 1991 UTC vs.
Revision 2.7 by greg, Fri Jun 19 12:54:41 1992 UTC

# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include <fcntl.h>
16  
17 + #include <ctype.h>
18 +
19   #include "view.h"
20  
21   #include "color.h"
# Line 30 | Line 32 | static char SCCSid[] = "$SunId$ LBL";
32   #define F_FORE          1               /* fill foreground */
33   #define F_BACK          2               /* fill background */
34  
35 < #define PACKSIZ         42              /* calculation packet size */
35 > #define PACKSIZ         256             /* max. calculation packet size */
36  
37 < #define RTCOM           "rtrace -h -ovl -fff %s"
37 > #define RTCOM           "rtrace -h- -ovl -fff "
38  
39   #define ABS(x)          ((x)>0?(x):-(x))
40  
# Line 67 | Line 69 | double theirexp;                       /* input picture exposure */
69   double  theirs2ours[4][4];              /* transformation matrix */
70   int     hasmatrix = 0;                  /* has transformation matrix */
71  
72 < int     childpid = -1;                  /* id of fill process */
73 < FILE    *psend, *precv;                 /* pipes to/from fill calculation */
74 < int     queue[PACKSIZ][2];              /* pending pixels */
72 > int     PDesc[3] = {-1,-1,-1};          /* rtrace process descriptor */
73 > #define childpid        (PDesc[2])
74 > unsigned short  queue[PACKSIZ][2];      /* pending pixels */
75 > int     packsiz;                        /* actual packet size */
76   int     queuesiz;                       /* number of pixels pending */
77  
78  
# Line 77 | Line 80 | main(argc, argv)                       /* interpolate pictures */
80   int     argc;
81   char    *argv[];
82   {
83 < #define check(olen,narg)        if (argv[i][olen] || narg >= argc-i) goto badopt
84 <        extern double   atof();
83 > #define  check(ol,al)           if (argv[i][ol] || \
84 >                                badarg(argc-i-1,argv+i+1,al)) \
85 >                                goto badopt
86          int     gotvfile = 0;
87          char    *zfile = NULL;
88          char    *err;
# Line 94 | Line 98 | char   *argv[];
98                  }
99                  switch (argv[i][1]) {
100                  case 't':                               /* threshold */
101 <                        check(2,1);
101 >                        check(2,"f");
102                          zeps = atof(argv[++i]);
103                          break;
104                  case 'n':                               /* dist. normalized? */
105 <                        check(2,0);
105 >                        check(2,NULL);
106                          normdist = !normdist;
107                          break;
108                  case 'f':                               /* fill type */
109                          switch (argv[i][2]) {
110                          case '0':                               /* none */
111 <                                check(3,0);
111 >                                check(3,NULL);
112                                  fillo = 0;
113                                  break;
114                          case 'f':                               /* foreground */
115 <                                check(3,0);
115 >                                check(3,NULL);
116                                  fillo = F_FORE;
117                                  break;
118                          case 'b':                               /* background */
119 <                                check(3,0);
119 >                                check(3,NULL);
120                                  fillo = F_BACK;
121                                  break;
122                          case 'a':                               /* all */
123 <                                check(3,0);
123 >                                check(3,NULL);
124                                  fillo = F_FORE|F_BACK;
125                                  break;
126                          case 's':                               /* sample */
127 <                                check(3,1);
127 >                                check(3,"i");
128                                  fillsamp = atoi(argv[++i]);
129                                  break;
130                          case 'c':                               /* color */
131 <                                check(3,3);
131 >                                check(3,"fff");
132                                  fillfunc = backfill;
133                                  setcolr(backcolr, atof(argv[i+1]),
134                                          atof(argv[i+2]), atof(argv[i+3]));
135                                  i += 3;
136                                  break;
137                          case 'z':                               /* z value */
138 <                                check(3,1);
138 >                                check(3,"f");
139                                  fillfunc = backfill;
140                                  backz = atof(argv[++i]);
141                                  break;
142                          case 'r':                               /* rtrace */
143 <                                check(3,1);
143 >                                check(3,"s");
144                                  fillfunc = rcalfill;
145                                  calstart(RTCOM, argv[++i]);
146                                  break;
# Line 145 | Line 149 | char   *argv[];
149                          }
150                          break;
151                  case 'z':                               /* z file */
152 <                        check(2,1);
152 >                        check(2,"s");
153                          zfile = argv[++i];
154                          break;
155                  case 'x':                               /* x resolution */
156 <                        check(2,1);
156 >                        check(2,"i");
157                          hresolu = atoi(argv[++i]);
158                          break;
159                  case 'y':                               /* y resolution */
160 <                        check(2,1);
160 >                        check(2,"i");
161                          vresolu = atoi(argv[++i]);
162                          break;
163                  case 'p':                               /* pixel aspect */
164 <                        check(2,1);
164 >                        if (argv[i][2] != 'a')
165 >                                goto badopt;
166 >                        check(3,"f");
167                          pixaspect = atof(argv[++i]);
168                          break;
169                  case 'v':                               /* view file */
170                          if (argv[i][2] != 'f')
171                                  goto badopt;
172 <                        check(3,1);
172 >                        check(3,"s");
173                          gotvfile = viewfile(argv[++i], &ourview, 0, 0);
174                          if (gotvfile < 0) {
175                                  perror(argv[i]);
# Line 184 | Line 190 | char   *argv[];
190                                                  /* check arguments */
191          if ((argc-i)%2)
192                  goto userr;
193 +        if (fillsamp == 1)
194 +                fillo &= ~F_BACK;
195                                                  /* set view */
196          if (err = setview(&ourview)) {
197                  fprintf(stderr, "%s: %s\n", progname, err);
# Line 191 | Line 199 | char   *argv[];
199          }
200          normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
201                                                  /* allocate frame */
202 <        ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR));
203 <        ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float));
202 >        ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
203 >        ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
204          if (ourpict == NULL || ourzbuf == NULL)
205                  syserror();
206 +        bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
207                                                          /* get input */
208          for ( ; i < argc; i += 2)
209                  addpicture(argv[i], argv[i+1]);
# Line 237 | Line 246 | userr:
246   headline(s)                             /* process header string */
247   char    *s;
248   {
240        static char     *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL};
241        register char   **an;
249          char    fmt[32];
250  
251          if (isformat(s)) {
# Line 253 | Line 260 | char   *s;
260                  theirexp *= exposval(s);
261                  return;
262          }
263 <        for (an = altname; *an != NULL; an++)
264 <                if (!strncmp(*an, s, strlen(*an))) {
258 <                        if (sscanview(&theirview, s+strlen(*an)) > 0)
259 <                                gotview++;
260 <                        break;
261 <                }
263 >        if (isview(s) && sscanview(&theirview, s) > 0)
264 >                gotview++;
265   }
266  
267  
268   addpicture(pfile, zspec)                /* add picture to output */
269   char    *pfile, *zspec;
270   {
268        extern double   atof();
271          FILE    *pfp;
272          int     zfd;
273          char    *err;
# Line 693 | Line 695 | int    x, y;
695   }
696  
697  
698 < calstart(prog, args)                    /* start fill calculation */
698 > calstart(prog, args)                    /* start fill calculation */
699   char    *prog, *args;
700   {
701          char    combuf[512];
702 <        int     p0[2], p1[2];
702 >        char    *argv[64];
703 >        int     rval;
704 >        register char   **wp, *cp;
705  
706          if (childpid != -1) {
707                  fprintf(stderr, "%s: too many calculations\n", progname);
708                  exit(1);
709          }
710 <        sprintf(combuf, prog, args);
711 <        if (pipe(p0) < 0 || pipe(p1) < 0)
712 <                syserror();
713 <        if ((childpid = vfork()) == 0) {        /* fork calculation */
714 <                close(p0[1]);
715 <                close(p1[0]);
716 <                if (p0[0] != 0) {
717 <                        dup2(p0[0], 0);
718 <                        close(p0[0]);
719 <                }
720 <                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);
710 >        strcpy(combuf, prog);
711 >        strcat(combuf, args);
712 >        cp = combuf;
713 >        wp = argv;
714 >        for ( ; ; ) {
715 >                while (isspace(*cp)) cp++;
716 >                if (!*cp) break;
717 >                *wp++ = cp;
718 >                while (!isspace(*cp))
719 >                        if (!*cp++) goto done;
720 >                *cp++ = '\0';
721          }
722 <        if (childpid == -1)
722 > done:
723 >        *wp = NULL;
724 >                                                /* start process */
725 >        if ((rval = open_process(PDesc, argv)) < 0)
726                  syserror();
727 <        close(p0[0]);
728 <        close(p1[1]);
729 <        if ((psend = fdopen(p0[1], "w")) == NULL)
730 <                syserror();
731 <        if ((precv = fdopen(p1[0], "r")) == NULL)
732 <                syserror();
727 >        if (rval == 0) {
728 >                fprintf(stderr, "%s: command not found\n", argv[0]);
729 >                exit(1);
730 >        }
731 >        packsiz = rval/(6*sizeof(float)) - 1;
732 >        if (packsiz > PACKSIZ)
733 >                packsiz = PACKSIZ;
734          queuesiz = 0;
735   }
736  
737  
738 < caldone()                               /* done with calculation */
738 > caldone()                               /* done with calculation */
739   {
738        int     pid;
739
740          if (childpid == -1)
741                  return;
742          clearqueue();
743 <        fclose(psend);
744 <        fclose(precv);
745 <        while ((pid = wait(0)) != -1 && pid != childpid)
746 <                ;
743 >        close_process(PDesc);
744          childpid = -1;
745   }
746  
# Line 751 | Line 748 | caldone()                              /* done with calculation */
748   rcalfill(x, y)                          /* fill with ray-calculated pixel */
749   int     x, y;
750   {
751 <        if (queuesiz >= PACKSIZ)        /* flush queue if needed */
751 >        if (queuesiz >= packsiz)        /* flush queue if needed */
752                  clearqueue();
753                                          /* add position to queue */
754          queue[queuesiz][0] = x;
# Line 763 | Line 760 | int    x, y;
760   clearqueue()                            /* process queue */
761   {
762          FVECT   orig, dir;
763 <        float   fbuf[6];
763 >        float   fbuf[6*(PACKSIZ+1)];
764 >        register float  *fbp;
765          register int    i;
766  
767 +        fbp = fbuf;
768          for (i = 0; i < queuesiz; i++) {
769                  viewray(orig, dir, &ourview,
770                                  (queue[i][0]+.5)/hresolu,
771                                  (queue[i][1]+.5)/vresolu);
772 <                fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2];
773 <                fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
775 <                fwrite((char *)fbuf, sizeof(float), 6, psend);
772 >                *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
773 >                *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
774          }
775 <                                        /* flush output and get results */
776 <        fbuf[3] = fbuf[4] = fbuf[5] = 0.0;              /* mark */
777 <        fwrite((char *)fbuf, sizeof(float), 6, psend);
778 <        if (fflush(psend) == EOF)
779 <                syserror();
775 >                                        /* mark end and get results */
776 >        bzero((char *)fbp, 6*sizeof(float));
777 >        if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz,
778 >                        6*sizeof(float)*(queuesiz+1)) !=
779 >                        4*sizeof(float)*queuesiz) {
780 >                fprintf(stderr, "%s: error reading from rtrace process\n",
781 >                                progname);
782 >                exit(1);
783 >        }
784 >        fbp = fbuf;
785          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                }
786                  if (ourexp > 0 && ourexp != 1.0) {
787 <                        fbuf[0] *= ourexp;
788 <                        fbuf[1] *= ourexp;
789 <                        fbuf[2] *= ourexp;
787 >                        fbp[0] *= ourexp;
788 >                        fbp[1] *= ourexp;
789 >                        fbp[2] *= ourexp;
790                  }
791                  setcolr(pscan(queue[i][1])[queue[i][0]],
792 <                                fbuf[0], fbuf[1], fbuf[2]);
793 <                zscan(queue[i][1])[queue[i][0]] = fbuf[3];
792 >                                fbp[0], fbp[1], fbp[2]);
793 >                zscan(queue[i][1])[queue[i][0]] = fbp[3];
794 >                fbp += 4;
795          }
796          queuesiz = 0;
797   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines