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.28 by greg, Fri Mar 16 19:19:52 1990 UTC vs.
Revision 2.5 by greg, Tue Apr 28 09:40:39 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 12 | 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"
22  
23 + #include "resolu.h"
24 +
25   #ifndef BSD
26   #define vfork           fork
27   #endif
# Line 26 | 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 57 | Line 63 | double ourexp = -1;                    /* output picture exposure */
63  
64   VIEW    theirview = STDVIEW;            /* input view */
65   int     gotview;                        /* got input view? */
66 < int     thresolu, tvresolu;             /* input resolution */
66 > int     wrongformat = 0;                /* input in another format? */
67 > RESOLU  tresolu;                        /* input resolution */
68   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 71 | 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 88 | 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 139 | 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 >                        check(2,"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 178 | 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 210 | Line 222 | char   *argv[];
222                  fputaspect(pixaspect, stdout);
223          if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
224                  fputexpos(ourexp, stdout);
225 +        fputformat(COLRFMT, stdout);
226          putc('\n', stdout);
227                                                          /* write picture */
228          writepicture();
# Line 230 | Line 243 | userr:
243   headline(s)                             /* process header string */
244   char    *s;
245   {
246 <        static char     *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL};
234 <        register char   **an;
246 >        char    fmt[32];
247  
248 +        if (isformat(s)) {
249 +                formatval(fmt, s);
250 +                wrongformat = strcmp(fmt, COLRFMT);
251 +                return;
252 +        }
253          putc('\t', stdout);
254          fputs(s, stdout);
255  
# Line 240 | Line 257 | char   *s;
257                  theirexp *= exposval(s);
258                  return;
259          }
260 <        for (an = altname; *an != NULL; an++)
261 <                if (!strncmp(*an, s, strlen(*an))) {
245 <                        if (sscanview(&theirview, s+strlen(*an)) > 0)
246 <                                gotview++;
247 <                        break;
248 <                }
260 >        if (isview(s) && sscanview(&theirview, s) > 0)
261 >                gotview++;
262   }
263  
264  
265   addpicture(pfile, zspec)                /* add picture to output */
266   char    *pfile, *zspec;
267   {
255        extern double   atof();
268          FILE    *pfp;
269          int     zfd;
270          char    *err;
# Line 269 | Line 281 | char   *pfile, *zspec;
281          theirexp = 1.0;
282          gotview = 0;
283          printf("%s:\n", pfile);
284 <        getheader(pfp, headline);
285 <        if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp)
286 <                        != (YMAJOR|YDECR)) {
275 <                fprintf(stderr, "%s: picture view error\n", pfile);
284 >        getheader(pfp, headline, NULL);
285 >        if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
286 >                fprintf(stderr, "%s: picture format error\n", pfile);
287                  exit(1);
288          }
289          if (ourexp <= 0)
# Line 284 | Line 295 | char   *pfile, *zspec;
295                  exit(1);
296          }
297                                          /* compute transformation */
298 <        pixform(theirs2ours, &theirview, &ourview);
298 >        hasmatrix = pixform(theirs2ours, &theirview, &ourview);
299                                          /* allocate scanlines */
300 <        scanin = (COLR *)malloc(thresolu*sizeof(COLR));
301 <        zin = (float *)malloc(thresolu*sizeof(float));
302 <        plast = (struct position *)calloc(thresolu, sizeof(struct position));
300 >        scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
301 >        zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
302 >        plast = (struct position *)calloc(scanlen(&tresolu),
303 >                        sizeof(struct position));
304          if (scanin == NULL || zin == NULL || plast == NULL)
305                  syserror();
306                                          /* get z specification or file */
# Line 299 | Line 311 | char   *pfile, *zspec;
311                          perror(zspec);
312                          exit(1);
313                  }
314 <                for (x = 0; x < thresolu; x++)
314 >                for (x = scanlen(&tresolu); x-- > 0; )
315                          zin[x] = zvalue;
316          }
317                                          /* load image */
318 <        for (y = tvresolu-1; y >= 0; y--) {
319 <                if (freadcolrs(scanin, thresolu, pfp) < 0) {
318 >        for (y = 0; y < numscans(&tresolu); y++) {
319 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
320                          fprintf(stderr, "%s: read error\n", pfile);
321                          exit(1);
322                  }
323 <                if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float))
324 <                                < thresolu*sizeof(float)) {
323 >                if (zfd != -1 && read(zfd,(char *)zin,
324 >                                scanlen(&tresolu)*sizeof(float))
325 >                                < scanlen(&tresolu)*sizeof(float)) {
326                          fprintf(stderr, "%s: read error\n", zspec);
327                          exit(1);
328                  }
# Line 331 | Line 344 | register VIEW  *vw1, *vw2;
344   {
345          double  m4t[4][4];
346  
347 +        if (vw1->type != VT_PER && vw1->type != VT_PAR)
348 +                return(0);
349 +        if (vw2->type != VT_PER && vw2->type != VT_PAR)
350 +                return(0);
351          setident4(xfmat);
352          xfmat[0][0] = vw1->hvec[0];
353          xfmat[0][1] = vw1->hvec[1];
# Line 358 | Line 375 | register VIEW  *vw1, *vw2;
375          m4t[2][2] = vw2->vdir[2];
376          m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
377          multmat4(xfmat, xfmat, m4t);
378 +        return(1);
379   }
380  
381  
# Line 368 | Line 386 | float  *zline;
386   struct position *lasty;         /* input/output */
387   {
388          extern double   sqrt();
389 <        double  pos[3];
389 >        FVECT   pos;
390          struct position lastx, newpos;
391          register int    x;
392  
393          lastx.z = 0;
394 <        for (x = thresolu-1; x >= 0; x--) {
395 <                pos[0] = (x+.5)/thresolu + theirview.hoff - .5;
378 <                pos[1] = (y+.5)/tvresolu + theirview.voff - .5;
394 >        for (x = scanlen(&tresolu); x-- > 0; ) {
395 >                pix2loc(pos, &tresolu, x, y);
396                  pos[2] = zline[x];
397 <                if (theirview.type == VT_PER) {
381 <                        if (normdist)   /* adjust for eye-ray distance */
382 <                                pos[2] /= sqrt( 1.
383 <                                        + pos[0]*pos[0]*theirview.hn2
384 <                                        + pos[1]*pos[1]*theirview.vn2 );
385 <                        pos[0] *= pos[2];
386 <                        pos[1] *= pos[2];
387 <                }
388 <                multp3(pos, pos, theirs2ours);
389 <                if (pos[2] <= 0) {
397 >                if (movepixel(pos) < 0) {
398                          lasty[x].z = lastx.z = 0;       /* mark invalid */
399                          continue;
400                  }
393                if (ourview.type == VT_PER) {
394                        pos[0] /= pos[2];
395                        pos[1] /= pos[2];
396                }
397                pos[0] += .5 - ourview.hoff;
398                pos[1] += .5 - ourview.voff;
401                  newpos.x = pos[0] * hresolu;
402                  newpos.y = pos[1] * vresolu;
403                  newpos.z = zline[x];
# Line 469 | Line 471 | double z;
471   }
472  
473  
474 + movepixel(pos)                          /* reposition image point */
475 + FVECT   pos;
476 + {
477 +        FVECT   pt, direc;
478 +        
479 +        if (pos[2] <= 0)                /* empty pixel */
480 +                return(-1);
481 +        if (hasmatrix) {
482 +                pos[0] += theirview.hoff - .5;
483 +                pos[1] += theirview.voff - .5;
484 +                if (theirview.type == VT_PER) {
485 +                        if (normdist)   /* adjust for eye-ray distance */
486 +                                pos[2] /= sqrt( 1.
487 +                                        + pos[0]*pos[0]*theirview.hn2
488 +                                        + pos[1]*pos[1]*theirview.vn2 );
489 +                        pos[0] *= pos[2];
490 +                        pos[1] *= pos[2];
491 +                }
492 +                multp3(pos, pos, theirs2ours);
493 +                if (pos[2] <= 0)
494 +                        return(-1);
495 +                if (ourview.type == VT_PER) {
496 +                        pos[0] /= pos[2];
497 +                        pos[1] /= pos[2];
498 +                }
499 +                pos[0] += .5 - ourview.hoff;
500 +                pos[1] += .5 - ourview.voff;
501 +                return(0);
502 +        }
503 +        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0)
504 +                return(-1);
505 +        pt[0] += direc[0]*pos[2];
506 +        pt[1] += direc[1]*pos[2];
507 +        pt[2] += direc[2]*pos[2];
508 +        viewloc(pos, &ourview, pt);
509 +        if (pos[2] <= 0)
510 +                return(-1);
511 +        return(0);
512 + }
513 +
514 +
515   backpicture(fill, samp)                 /* background fill algorithm */
516   int     (*fill)();
517   int     samp;
518   {
519          int     *yback, xback;
520          int     y;
478        COLR    pfill;
521          register int    x, i;
522                                                          /* get back buffer */
523          yback = (int *)malloc(hresolu*sizeof(int));
# Line 581 | Line 623 | writepicture()                         /* write out picture */
623   {
624          int     y;
625  
626 <        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
626 >        fprtresolu(hresolu, vresolu, stdout);
627          for (y = vresolu-1; y >= 0; y--)
628                  if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
629                          syserror();
# Line 608 | Line 650 | char   *fname;
650                  if (donorm) {
651                          double  vx, yzn2;
652                          register int    x;
653 <                        yzn2 = y - .5*(vresolu-1);
653 >                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
654                          yzn2 = 1. + yzn2*yzn2*ourview.vn2;
655                          for (x = 0; x < hresolu; x++) {
656 <                                vx = x - .5*(hresolu-1);
656 >                                vx = (x+.5)/hresolu + ourview.hoff - .5;
657                                  zout[x] = zscan(y)[x]
658                                          * sqrt(vx*vx*ourview.hn2 + yzn2);
659                          }
# Line 650 | Line 692 | int    x, y;
692   }
693  
694  
695 < calstart(prog, args)                    /* start fill calculation */
695 > calstart(prog, args)                    /* start fill calculation */
696   char    *prog, *args;
697   {
698          char    combuf[512];
699 <        int     p0[2], p1[2];
699 >        char    *argv[64];
700 >        int     rval;
701 >        register char   **wp, *cp;
702  
703          if (childpid != -1) {
704                  fprintf(stderr, "%s: too many calculations\n", progname);
705                  exit(1);
706          }
707 <        sprintf(combuf, prog, args);
708 <        if (pipe(p0) < 0 || pipe(p1) < 0)
709 <                syserror();
710 <        if ((childpid = vfork()) == 0) {        /* fork calculation */
711 <                close(p0[1]);
712 <                close(p1[0]);
713 <                if (p0[0] != 0) {
714 <                        dup2(p0[0], 0);
715 <                        close(p0[0]);
716 <                }
717 <                if (p1[1] != 1) {
674 <                        dup2(p1[1], 1);
675 <                        close(p1[1]);
676 <                }
677 <                execl("/bin/sh", "sh", "-c", combuf, 0);
678 <                perror("/bin/sh");
679 <                _exit(127);
707 >        strcpy(combuf, prog);
708 >        strcat(combuf, args);
709 >        cp = combuf;
710 >        wp = argv;
711 >        for ( ; ; ) {
712 >                while (isspace(*cp)) cp++;
713 >                if (!*cp) break;
714 >                *wp++ = cp;
715 >                while (!isspace(*cp))
716 >                        if (!*cp++) goto done;
717 >                *cp++ = '\0';
718          }
719 <        if (childpid == -1)
719 > done:
720 >        *wp = NULL;
721 >                                                /* start process */
722 >        if ((rval = open_process(PDesc, argv)) < 0)
723                  syserror();
724 <        close(p0[0]);
725 <        close(p1[1]);
726 <        if ((psend = fdopen(p0[1], "w")) == NULL)
727 <                syserror();
728 <        if ((precv = fdopen(p1[0], "r")) == NULL)
729 <                syserror();
724 >        if (rval == 0) {
725 >                fprintf(stderr, "%s: command not found\n", argv[0]);
726 >                exit(1);
727 >        }
728 >        packsiz = rval/(6*sizeof(float)) - 1;
729 >        if (packsiz > PACKSIZ)
730 >                packsiz = PACKSIZ;
731          queuesiz = 0;
732   }
733  
734  
735 < caldone()                               /* done with calculation */
735 > caldone()                               /* done with calculation */
736   {
695        int     pid;
696
737          if (childpid == -1)
738                  return;
739          clearqueue();
740 <        fclose(psend);
701 <        fclose(precv);
702 <        while ((pid = wait(0)) != -1 && pid != childpid)
703 <                ;
740 >        close_process(PDesc);
741          childpid = -1;
742   }
743  
# Line 708 | Line 745 | caldone()                              /* done with calculation */
745   rcalfill(x, y)                          /* fill with ray-calculated pixel */
746   int     x, y;
747   {
748 <        if (queuesiz >= PACKSIZ)        /* flush queue if needed */
748 >        if (queuesiz >= packsiz)        /* flush queue if needed */
749                  clearqueue();
750                                          /* add position to queue */
751          queue[queuesiz][0] = x;
# Line 720 | Line 757 | int    x, y;
757   clearqueue()                            /* process queue */
758   {
759          FVECT   orig, dir;
760 <        float   fbuf[6];
760 >        float   fbuf[6*(PACKSIZ+1)];
761 >        register float  *fbp;
762          register int    i;
763  
764 +        fbp = fbuf;
765          for (i = 0; i < queuesiz; i++) {
766                  viewray(orig, dir, &ourview,
767                                  (queue[i][0]+.5)/hresolu,
768                                  (queue[i][1]+.5)/vresolu);
769 <                fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2];
770 <                fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
732 <                fwrite((char *)fbuf, sizeof(float), 6, psend);
769 >                *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
770 >                *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
771          }
772 <                                        /* flush output and get results */
773 <        fbuf[3] = fbuf[4] = fbuf[5] = 0.0;              /* mark */
774 <        fwrite((char *)fbuf, sizeof(float), 6, psend);
775 <        if (fflush(psend) == EOF)
776 <                syserror();
772 >                                        /* mark end and get results */
773 >        bzero((char *)fbp, 6*sizeof(float));
774 >        if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz,
775 >                        6*sizeof(float)*(queuesiz+1)) !=
776 >                        4*sizeof(float)*queuesiz) {
777 >                fprintf(stderr, "%s: error reading from rtrace process\n",
778 >                                progname);
779 >                exit(1);
780 >        }
781 >        fbp = fbuf;
782          for (i = 0; i < queuesiz; i++) {
740                if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) {
741                        fprintf(stderr, "%s: read error in clearqueue\n",
742                                        progname);
743                        exit(1);
744                }
783                  if (ourexp > 0 && ourexp != 1.0) {
784 <                        fbuf[0] *= ourexp;
785 <                        fbuf[1] *= ourexp;
786 <                        fbuf[2] *= ourexp;
784 >                        fbp[0] *= ourexp;
785 >                        fbp[1] *= ourexp;
786 >                        fbp[2] *= ourexp;
787                  }
788                  setcolr(pscan(queue[i][1])[queue[i][0]],
789 <                                fbuf[0], fbuf[1], fbuf[2]);
790 <                zscan(queue[i][1])[queue[i][0]] = fbuf[3];
789 >                                fbp[0], fbp[1], fbp[2]);
790 >                zscan(queue[i][1])[queue[i][0]] = fbp[3];
791 >                fbp += 4;
792          }
793          queuesiz = 0;
794   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines