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 1.30 by greg, Mon Oct 15 11:01:48 1990 UTC vs.
Revision 2.8 by greg, Thu Sep 24 21:33:52 1992 UTC

# Line 1 | Line 1
1 + /* Copyright (c) 1991 Regents of the University of California */
2 +
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
5   #endif
# Line 10 | Line 12 | 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  
19   #include "color.h"
20  
21 + #include "resolu.h"
22 +
23   #ifndef BSD
24   #define vfork           fork
25   #endif
# Line 26 | Line 30 | static char SCCSid[] = "$SunId$ LBL";
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  
# Line 57 | Line 61 | double ourexp = -1;                    /* output picture exposure */
61  
62   VIEW    theirview = STDVIEW;            /* input view */
63   int     gotview;                        /* got input view? */
64 < int     thresolu, tvresolu;             /* input resolution */
64 > int     wrongformat = 0;                /* input in another format? */
65 > RESOLU  tresolu;                        /* input resolution */
66   double  theirexp;                       /* input picture exposure */
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  
# Line 72 | Line 78 | main(argc, argv)                       /* interpolate pictures */
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;
# Line 89 | Line 96 | char   *argv[];
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;
# Line 140 | Line 147 | char   *argv[];
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]);
# Line 179 | Line 188 | char   *argv[];
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);
# Line 186 | Line 197 | char   *argv[];
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]);
# Line 211 | Line 223 | char   *argv[];
223                  fputaspect(pixaspect, stdout);
224          if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
225                  fputexpos(ourexp, stdout);
226 +        fputformat(COLRFMT, stdout);
227          putc('\n', stdout);
228                                                          /* write picture */
229          writepicture();
# Line 231 | Line 244 | userr:
244   headline(s)                             /* process header string */
245   char    *s;
246   {
247 <        static char     *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL};
235 <        register char   **an;
247 >        char    fmt[32];
248  
249 +        if (isformat(s)) {
250 +                formatval(fmt, s);
251 +                wrongformat = strcmp(fmt, COLRFMT);
252 +                return;
253 +        }
254          putc('\t', stdout);
255          fputs(s, stdout);
256  
# Line 241 | Line 258 | char   *s;
258                  theirexp *= exposval(s);
259                  return;
260          }
261 <        for (an = altname; *an != NULL; an++)
262 <                if (!strncmp(*an, s, strlen(*an))) {
246 <                        if (sscanview(&theirview, s+strlen(*an)) > 0)
247 <                                gotview++;
248 <                        break;
249 <                }
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   {
256        extern double   atof();
269          FILE    *pfp;
270          int     zfd;
271          char    *err;
# Line 270 | Line 282 | char   *pfile, *zspec;
282          theirexp = 1.0;
283          gotview = 0;
284          printf("%s:\n", pfile);
285 <        getheader(pfp, headline);
286 <        if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp)
287 <                        != (YMAJOR|YDECR)) {
276 <                fprintf(stderr, "%s: picture view error\n", pfile);
285 >        getheader(pfp, headline, NULL);
286 >        if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
287 >                fprintf(stderr, "%s: picture format error\n", pfile);
288                  exit(1);
289          }
290          if (ourexp <= 0)
# Line 287 | Line 298 | char   *pfile, *zspec;
298                                          /* compute transformation */
299          hasmatrix = pixform(theirs2ours, &theirview, &ourview);
300                                          /* allocate scanlines */
301 <        scanin = (COLR *)malloc(thresolu*sizeof(COLR));
302 <        zin = (float *)malloc(thresolu*sizeof(float));
303 <        plast = (struct position *)calloc(thresolu, sizeof(struct position));
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();
307                                          /* get z specification or file */
# Line 300 | Line 312 | char   *pfile, *zspec;
312                          perror(zspec);
313                          exit(1);
314                  }
315 <                for (x = 0; x < thresolu; x++)
315 >                for (x = scanlen(&tresolu); x-- > 0; )
316                          zin[x] = zvalue;
317          }
318                                          /* load image */
319 <        for (y = tvresolu-1; y >= 0; y--) {
320 <                if (freadcolrs(scanin, thresolu, pfp) < 0) {
319 >        for (y = 0; y < numscans(&tresolu); y++) {
320 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
321                          fprintf(stderr, "%s: read error\n", pfile);
322                          exit(1);
323                  }
324 <                if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float))
325 <                                < thresolu*sizeof(float)) {
324 >                if (zfd != -1 && read(zfd,(char *)zin,
325 >                                scanlen(&tresolu)*sizeof(float))
326 >                                < scanlen(&tresolu)*sizeof(float)) {
327                          fprintf(stderr, "%s: read error\n", zspec);
328                          exit(1);
329                  }
# Line 379 | Line 392 | struct position        *lasty;         /* input/output */
392          register int    x;
393  
394          lastx.z = 0;
395 <        for (x = thresolu-1; x >= 0; x--) {
396 <                pos[0] = (x+.5)/thresolu;
384 <                pos[1] = (y+.5)/tvresolu;
395 >        for (x = scanlen(&tresolu); x-- > 0; ) {
396 >                pix2loc(pos, &tresolu, x, y);
397                  pos[2] = zline[x];
398                  if (movepixel(pos) < 0) {
399                          lasty[x].z = lastx.z = 0;       /* mark invalid */
# Line 465 | Line 477 | FVECT  pos;
477   {
478          FVECT   pt, direc;
479          
480 +        if (pos[2] <= 0)                /* empty pixel */
481 +                return(-1);
482          if (hasmatrix) {
483                  pos[0] += theirview.hoff - .5;
484                  pos[1] += theirview.voff - .5;
# Line 492 | Line 506 | FVECT  pos;
506          pt[0] += direc[0]*pos[2];
507          pt[1] += direc[1]*pos[2];
508          pt[2] += direc[2]*pos[2];
509 <        viewpixel(&pos[0], &pos[1], &pos[2], &ourview, pt);
509 >        viewloc(pos, &ourview, pt);
510          if (pos[2] <= 0)
511                  return(-1);
512          return(0);
# Line 505 | Line 519 | int    samp;
519   {
520          int     *yback, xback;
521          int     y;
508        COLR    pfill;
522          register int    x, i;
523                                                          /* get back buffer */
524          yback = (int *)malloc(hresolu*sizeof(int));
# Line 611 | Line 624 | writepicture()                         /* write out picture */
624   {
625          int     y;
626  
627 <        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
627 >        fprtresolu(hresolu, vresolu, stdout);
628          for (y = vresolu-1; y >= 0; y--)
629                  if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
630                          syserror();
# Line 680 | Line 693 | int    x, y;
693   }
694  
695  
696 < calstart(prog, args)                    /* start fill calculation */
696 > calstart(prog, args)                    /* start fill calculation */
697   char    *prog, *args;
698   {
699          char    combuf[512];
700 <        int     p0[2], p1[2];
700 >        char    *argv[64];
701 >        int     rval;
702 >        register char   **wp, *cp;
703  
704          if (childpid != -1) {
705                  fprintf(stderr, "%s: too many calculations\n", progname);
706                  exit(1);
707          }
708 <        sprintf(combuf, prog, args);
709 <        if (pipe(p0) < 0 || pipe(p1) < 0)
710 <                syserror();
711 <        if ((childpid = vfork()) == 0) {        /* fork calculation */
712 <                close(p0[1]);
713 <                close(p1[0]);
714 <                if (p0[0] != 0) {
715 <                        dup2(p0[0], 0);
716 <                        close(p0[0]);
717 <                }
718 <                if (p1[1] != 1) {
704 <                        dup2(p1[1], 1);
705 <                        close(p1[1]);
706 <                }
707 <                execl("/bin/sh", "sh", "-c", combuf, 0);
708 <                perror("/bin/sh");
709 <                _exit(127);
708 >        strcpy(combuf, prog);
709 >        strcat(combuf, args);
710 >        cp = combuf;
711 >        wp = argv;
712 >        for ( ; ; ) {
713 >                while (isspace(*cp)) cp++;
714 >                if (!*cp) break;
715 >                *wp++ = cp;
716 >                while (!isspace(*cp))
717 >                        if (!*cp++) goto done;
718 >                *cp++ = '\0';
719          }
720 <        if (childpid == -1)
720 > done:
721 >        *wp = NULL;
722 >                                                /* start process */
723 >        if ((rval = open_process(PDesc, argv)) < 0)
724                  syserror();
725 <        close(p0[0]);
726 <        close(p1[1]);
727 <        if ((psend = fdopen(p0[1], "w")) == NULL)
728 <                syserror();
729 <        if ((precv = fdopen(p1[0], "r")) == NULL)
730 <                syserror();
725 >        if (rval == 0) {
726 >                fprintf(stderr, "%s: command not found\n", argv[0]);
727 >                exit(1);
728 >        }
729 >        packsiz = rval/(6*sizeof(float)) - 1;
730 >        if (packsiz > PACKSIZ)
731 >                packsiz = PACKSIZ;
732          queuesiz = 0;
733   }
734  
735  
736 < caldone()                               /* done with calculation */
736 > caldone()                               /* done with calculation */
737   {
725        int     pid;
726
738          if (childpid == -1)
739                  return;
740          clearqueue();
741 <        fclose(psend);
731 <        fclose(precv);
732 <        while ((pid = wait(0)) != -1 && pid != childpid)
733 <                ;
741 >        close_process(PDesc);
742          childpid = -1;
743   }
744  
# Line 738 | Line 746 | caldone()                              /* done with calculation */
746   rcalfill(x, y)                          /* fill with ray-calculated pixel */
747   int     x, y;
748   {
749 <        if (queuesiz >= PACKSIZ)        /* flush queue if needed */
749 >        if (queuesiz >= packsiz)        /* flush queue if needed */
750                  clearqueue();
751                                          /* add position to queue */
752          queue[queuesiz][0] = x;
# Line 750 | Line 758 | int    x, y;
758   clearqueue()                            /* process queue */
759   {
760          FVECT   orig, dir;
761 <        float   fbuf[6];
761 >        float   fbuf[6*(PACKSIZ+1)];
762 >        register float  *fbp;
763          register int    i;
764  
765 +        fbp = fbuf;
766          for (i = 0; i < queuesiz; i++) {
767                  viewray(orig, dir, &ourview,
768                                  (queue[i][0]+.5)/hresolu,
769                                  (queue[i][1]+.5)/vresolu);
770 <                fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2];
771 <                fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
762 <                fwrite((char *)fbuf, sizeof(float), 6, psend);
770 >                *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
771 >                *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
772          }
773 <                                        /* flush output and get results */
774 <        fbuf[3] = fbuf[4] = fbuf[5] = 0.0;              /* mark */
775 <        fwrite((char *)fbuf, sizeof(float), 6, psend);
776 <        if (fflush(psend) == EOF)
777 <                syserror();
773 >                                        /* mark end and get results */
774 >        bzero((char *)fbp, 6*sizeof(float));
775 >        if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz,
776 >                        6*sizeof(float)*(queuesiz+1)) !=
777 >                        4*sizeof(float)*queuesiz) {
778 >                fprintf(stderr, "%s: error reading from rtrace process\n",
779 >                                progname);
780 >                exit(1);
781 >        }
782 >        fbp = fbuf;
783          for (i = 0; i < queuesiz; i++) {
770                if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) {
771                        fprintf(stderr, "%s: read error in clearqueue\n",
772                                        progname);
773                        exit(1);
774                }
784                  if (ourexp > 0 && ourexp != 1.0) {
785 <                        fbuf[0] *= ourexp;
786 <                        fbuf[1] *= ourexp;
787 <                        fbuf[2] *= ourexp;
785 >                        fbp[0] *= ourexp;
786 >                        fbp[1] *= ourexp;
787 >                        fbp[2] *= ourexp;
788                  }
789                  setcolr(pscan(queue[i][1])[queue[i][0]],
790 <                                fbuf[0], fbuf[1], fbuf[2]);
791 <                zscan(queue[i][1])[queue[i][0]] = fbuf[3];
790 >                                fbp[0], fbp[1], fbp[2]);
791 >                zscan(queue[i][1])[queue[i][0]] = fbp[3];
792 >                fbp += 4;
793          }
794          queuesiz = 0;
795   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines