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.33 by greg, Thu Apr 18 14:35:26 1991 UTC vs.
Revision 2.14 by greg, Wed Dec 21 16:59:03 1994 UTC

# Line 1 | Line 1
1 + /* Copyright (c) 1994 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 < #ifndef BSD
20 < #define vfork           fork
21 < #endif
21 > #include "resolu.h"
22  
23   #define pscan(y)        (ourpict+(y)*hresolu)
24   #define zscan(y)        (ourzbuf+(y)*hresolu)
# Line 26 | Line 26 | static char SCCSid[] = "$SunId$ LBL";
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  
# Line 58 | Line 58 | double ourexp = -1;                    /* output picture exposure */
58   VIEW    theirview = STDVIEW;            /* input view */
59   int     gotview;                        /* got input view? */
60   int     wrongformat = 0;                /* input in another format? */
61 < int     thresolu, tvresolu;             /* input resolution */
61 > RESOLU  tresolu;                        /* input resolution */
62   double  theirexp;                       /* input picture exposure */
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  
# Line 73 | Line 74 | main(argc, argv)                       /* interpolate pictures */
74   int     argc;
75   char    *argv[];
76   {
77 < #define check(olen,narg)        if (argv[i][olen] || narg >= argc-i) goto badopt
78 <        extern double   atof();
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;
# Line 90 | Line 92 | char   *argv[];
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;
# Line 141 | Line 143 | char   *argv[];
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);
167 <                        } 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);
# Line 180 | Line 183 | char   *argv[];
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)) {
189 >        if (ourview.vaft > FTINY)
190 >                err = "no aft clipping plane allowed";
191 >        else
192 >                err = setview(&ourview);
193 >        if (err != NULL) {
194                  fprintf(stderr, "%s: %s\n", progname, err);
195                  exit(1);
196          }
197          normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
198                                                  /* allocate frame */
199 <        ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR));
200 <        ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float));
199 >        ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
200 >        ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
201          if (ourpict == NULL || ourzbuf == NULL)
202 <                syserror();
202 >                syserror(progname);
203 >        bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
204 >                                                        /* new header */
205 >        newheader("RADIANCE", stdout);
206                                                          /* get input */
207          for ( ; i < argc; i += 2)
208                  addpicture(argv[i], argv[i+1]);
# Line 233 | Line 245 | userr:
245   headline(s)                             /* process header string */
246   char    *s;
247   {
236        static char     *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL};
237        register char   **an;
248          char    fmt[32];
249  
250 <        if (isformat(s)) {
251 <                formatval(fmt, s);
250 >        if (isheadid(s))
251 >                return;
252 >        if (formatval(fmt, s)) {
253                  wrongformat = strcmp(fmt, COLRFMT);
254                  return;
255          }
# Line 249 | 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))) {
254 <                        if (sscanview(&theirview, s+strlen(*an)) > 0)
255 <                                gotview++;
256 <                        break;
257 <                }
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   {
264        extern double   atof();
271          FILE    *pfp;
272          int     zfd;
273          char    *err;
# Line 270 | Line 276 | char   *pfile, *zspec;
276          struct position *plast;
277          int     y;
278                                          /* open picture file */
279 <        if ((pfp = fopen(pfile, "r")) == NULL) {
280 <                perror(pfile);
275 <                exit(1);
276 <        }
279 >        if ((pfp = fopen(pfile, "r")) == NULL)
280 >                syserror(pfile);
281                                          /* get header with exposure and view */
282          theirexp = 1.0;
283          gotview = 0;
284          printf("%s:\n", pfile);
285          getheader(pfp, headline, NULL);
286 <        if (wrongformat || !gotview ||
283 <                fgetresolu(&thresolu, &tvresolu, pfp) != (YMAJOR|YDECR)) {
284 <
286 >        if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
287                  fprintf(stderr, "%s: picture format error\n", pfile);
288                  exit(1);
289          }
# Line 296 | 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();
306 >                syserror(progname);
307                                          /* get z specification or file */
308          if ((zfd = open(zspec, O_RDONLY)) == -1) {
309                  double  zvalue;
310                  register int    x;
311 <                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
312 <                        perror(zspec);
313 <                        exit(1);
311 <                }
312 <                for (x = 0; x < thresolu; x++)
311 >                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0)
312 >                        syserror(zspec);
313 >                for (x = scanlen(&tresolu); x-- > 0; )
314                          zin[x] = zvalue;
315          }
316                                          /* load image */
317 <        for (y = tvresolu-1; y >= 0; y--) {
318 <                if (freadcolrs(scanin, thresolu, pfp) < 0) {
317 >        for (y = 0; y < numscans(&tresolu); y++) {
318 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
319                          fprintf(stderr, "%s: read error\n", pfile);
320                          exit(1);
321                  }
322 <                if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float))
323 <                                < thresolu*sizeof(float)) {
322 >                if (zfd != -1 && read(zfd,(char *)zin,
323 >                                scanlen(&tresolu)*sizeof(float))
324 >                                < scanlen(&tresolu)*sizeof(float)) {
325                          fprintf(stderr, "%s: read error\n", zspec);
326                          exit(1);
327                  }
# Line 382 | Line 384 | COLR   *pline;
384   float   *zline;
385   struct position *lasty;         /* input/output */
386   {
385        extern double   sqrt();
387          FVECT   pos;
388          struct position lastx, newpos;
389          register int    x;
390  
391          lastx.z = 0;
392 <        for (x = thresolu-1; x >= 0; x--) {
393 <                pos[0] = (x+.5)/thresolu;
393 <                pos[1] = (y+.5)/tvresolu;
392 >        for (x = scanlen(&tresolu); x-- > 0; ) {
393 >                pix2loc(pos, &tresolu, x, y);
394                  pos[2] = zline[x];
395                  if (movepixel(pos) < 0) {
396                          lasty[x].z = lastx.z = 0;       /* mark invalid */
# Line 498 | Line 498 | FVECT  pos;
498                  pos[1] += .5 - ourview.voff;
499                  return(0);
500          }
501 <        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0)
501 >        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < -FTINY)
502                  return(-1);
503          pt[0] += direc[0]*pos[2];
504          pt[1] += direc[1]*pos[2];
505          pt[2] += direc[2]*pos[2];
506 <        viewpixel(&pos[0], &pos[1], &pos[2], &ourview, pt);
506 >        viewloc(pos, &ourview, pt);
507          if (pos[2] <= 0)
508                  return(-1);
509          return(0);
# Line 520 | Line 520 | int    samp;
520                                                          /* get back buffer */
521          yback = (int *)malloc(hresolu*sizeof(int));
522          if (yback == NULL)
523 <                syserror();
523 >                syserror(progname);
524          for (x = 0; x < hresolu; x++)
525                  yback[x] = -2;
526          /*
# Line 621 | Line 621 | writepicture()                         /* write out picture */
621   {
622          int     y;
623  
624 <        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
624 >        fprtresolu(hresolu, vresolu, stdout);
625          for (y = vresolu-1; y >= 0; y--)
626                  if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
627 <                        syserror();
627 >                        syserror(progname);
628   }
629  
630  
631   writedistance(fname)                    /* write out z file */
632   char    *fname;
633   {
634        extern double   sqrt();
634          int     donorm = normdist && ourview.type == VT_PER;
635          int     fd;
636          int     y;
637          float   *zout;
638  
639 <        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
640 <                perror(fname);
642 <                exit(1);
643 <        }
639 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
640 >                syserror(fname);
641          if (donorm
642          && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
643 <                syserror();
643 >                syserror(progname);
644          for (y = vresolu-1; y >= 0; y--) {
645                  if (donorm) {
646                          double  vx, yzn2;
# Line 658 | Line 655 | char   *fname;
655                  } else
656                          zout = zscan(y);
657                  if (write(fd, (char *)zout, hresolu*sizeof(float))
658 <                                < hresolu*sizeof(float)) {
659 <                        perror(fname);
663 <                        exit(1);
664 <                }
658 >                                < hresolu*sizeof(float))
659 >                        syserror(fname);
660          }
661          if (donorm)
662                  free((char *)zout);
# Line 690 | Line 685 | int    x, y;
685   }
686  
687  
688 < calstart(prog, args)                    /* start fill calculation */
688 > calstart(prog, args)                    /* start fill calculation */
689   char    *prog, *args;
690   {
691          char    combuf[512];
692 <        int     p0[2], p1[2];
692 >        char    *argv[64];
693 >        int     rval;
694 >        register char   **wp, *cp;
695  
696          if (childpid != -1) {
697                  fprintf(stderr, "%s: too many calculations\n", progname);
698                  exit(1);
699          }
700 <        sprintf(combuf, prog, args);
701 <        if (pipe(p0) < 0 || pipe(p1) < 0)
702 <                syserror();
703 <        if ((childpid = vfork()) == 0) {        /* fork calculation */
704 <                close(p0[1]);
705 <                close(p1[0]);
706 <                if (p0[0] != 0) {
707 <                        dup2(p0[0], 0);
708 <                        close(p0[0]);
709 <                }
710 <                if (p1[1] != 1) {
711 <                        dup2(p1[1], 1);
715 <                        close(p1[1]);
716 <                }
717 <                execl("/bin/sh", "sh", "-c", combuf, 0);
718 <                perror("/bin/sh");
719 <                _exit(127);
700 >        strcpy(combuf, prog);
701 >        strcat(combuf, args);
702 >        cp = combuf;
703 >        wp = argv;
704 >        for ( ; ; ) {
705 >                while (isspace(*cp))    /* nullify spaces */
706 >                        *cp++ = '\0';
707 >                if (!*cp)               /* all done? */
708 >                        break;
709 >                *wp++ = cp;             /* add argument to list */
710 >                while (*++cp && !isspace(*cp))
711 >                        ;
712          }
713 <        if (childpid == -1)
714 <                syserror();
715 <        close(p0[0]);
716 <        close(p1[1]);
717 <        if ((psend = fdopen(p0[1], "w")) == NULL)
718 <                syserror();
719 <        if ((precv = fdopen(p1[0], "r")) == NULL)
720 <                syserror();
713 >        *wp = NULL;
714 >                                                /* start process */
715 >        if ((rval = open_process(PDesc, argv)) < 0)
716 >                syserror(progname);
717 >        if (rval == 0) {
718 >                fprintf(stderr, "%s: command not found\n", argv[0]);
719 >                exit(1);
720 >        }
721 >        packsiz = rval/(6*sizeof(float)) - 1;
722 >        if (packsiz > PACKSIZ)
723 >                packsiz = PACKSIZ;
724          queuesiz = 0;
725   }
726  
727  
728 < caldone()                               /* done with calculation */
728 > caldone()                               /* done with calculation */
729   {
735        int     pid;
736
730          if (childpid == -1)
731                  return;
732          clearqueue();
733 <        fclose(psend);
741 <        fclose(precv);
742 <        while ((pid = wait(0)) != -1 && pid != childpid)
743 <                ;
733 >        close_process(PDesc);
734          childpid = -1;
735   }
736  
# Line 748 | Line 738 | caldone()                              /* done with calculation */
738   rcalfill(x, y)                          /* fill with ray-calculated pixel */
739   int     x, y;
740   {
741 <        if (queuesiz >= PACKSIZ)        /* flush queue if needed */
741 >        if (queuesiz >= packsiz)        /* flush queue if needed */
742                  clearqueue();
743                                          /* add position to queue */
744          queue[queuesiz][0] = x;
# Line 760 | Line 750 | int    x, y;
750   clearqueue()                            /* process queue */
751   {
752          FVECT   orig, dir;
753 <        float   fbuf[6];
753 >        float   fbuf[6*(PACKSIZ+1)];
754 >        register float  *fbp;
755          register int    i;
756  
757 +        if (queuesiz == 0)
758 +                return;
759 +        fbp = fbuf;
760          for (i = 0; i < queuesiz; i++) {
761                  viewray(orig, dir, &ourview,
762                                  (queue[i][0]+.5)/hresolu,
763                                  (queue[i][1]+.5)/vresolu);
764 <                fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2];
765 <                fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
772 <                fwrite((char *)fbuf, sizeof(float), 6, psend);
764 >                *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
765 >                *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
766          }
767 <                                        /* flush output and get results */
768 <        fbuf[3] = fbuf[4] = fbuf[5] = 0.0;              /* mark */
769 <        fwrite((char *)fbuf, sizeof(float), 6, psend);
770 <        if (fflush(psend) == EOF)
771 <                syserror();
767 >                                        /* mark end and get results */
768 >        bzero((char *)fbp, 6*sizeof(float));
769 >        if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz,
770 >                        6*sizeof(float)*(queuesiz+1)) !=
771 >                        4*sizeof(float)*queuesiz) {
772 >                fprintf(stderr, "%s: error reading from rtrace process\n",
773 >                                progname);
774 >                exit(1);
775 >        }
776 >        fbp = fbuf;
777          for (i = 0; i < queuesiz; i++) {
780                if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) {
781                        fprintf(stderr, "%s: read error in clearqueue\n",
782                                        progname);
783                        exit(1);
784                }
778                  if (ourexp > 0 && ourexp != 1.0) {
779 <                        fbuf[0] *= ourexp;
780 <                        fbuf[1] *= ourexp;
781 <                        fbuf[2] *= ourexp;
779 >                        fbp[0] *= ourexp;
780 >                        fbp[1] *= ourexp;
781 >                        fbp[2] *= ourexp;
782                  }
783                  setcolr(pscan(queue[i][1])[queue[i][0]],
784 <                                fbuf[0], fbuf[1], fbuf[2]);
785 <                zscan(queue[i][1])[queue[i][0]] = fbuf[3];
784 >                                fbp[0], fbp[1], fbp[2]);
785 >                zscan(queue[i][1])[queue[i][0]] = fbp[3];
786 >                fbp += 4;
787          }
788          queuesiz = 0;
789   }
790  
791  
792 < syserror()                      /* report error and exit */
792 > syserror(s)                     /* report error and exit */
793 > char    *s;
794   {
795 <        perror(progname);
795 >        perror(s);
796          exit(1);
797   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines