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.13 by greg, Sun Feb 27 10:16:57 1994 UTC

# Line 1 | Line 1
1 + /* Copyright (c) 1993 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 57 | Line 57 | double ourexp = -1;                    /* output picture exposure */
57  
58   VIEW    theirview = STDVIEW;            /* input view */
59   int     gotview;                        /* got input view? */
60 < int     thresolu, tvresolu;             /* input resolution */
60 > int     wrongformat = 0;                /* input in another format? */
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 72 | 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 89 | 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 140 | 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);
166 <                        } 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 179 | 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)) {
190                  fprintf(stderr, "%s: %s\n", progname, err);
# Line 186 | Line 192 | char   *argv[];
192          }
193          normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
194                                                  /* allocate frame */
195 <        ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR));
196 <        ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float));
195 >        ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR));
196 >        ourzbuf = (float *)bmalloc(hresolu*vresolu*sizeof(float));
197          if (ourpict == NULL || ourzbuf == NULL)
198 <                syserror();
198 >                syserror(progname);
199 >        bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float));
200 >                                                        /* new header */
201 >        newheader("RADIANCE", stdout);
202                                                          /* get input */
203          for ( ; i < argc; i += 2)
204                  addpicture(argv[i], argv[i+1]);
# Line 211 | Line 220 | char   *argv[];
220                  fputaspect(pixaspect, stdout);
221          if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
222                  fputexpos(ourexp, stdout);
223 +        fputformat(COLRFMT, stdout);
224          putc('\n', stdout);
225                                                          /* write picture */
226          writepicture();
# Line 231 | Line 241 | userr:
241   headline(s)                             /* process header string */
242   char    *s;
243   {
244 <        static char     *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL};
235 <        register char   **an;
244 >        char    fmt[32];
245  
246 +        if (isheadid(s))
247 +                return;
248 +        if (formatval(fmt, s)) {
249 +                wrongformat = strcmp(fmt, COLRFMT);
250 +                return;
251 +        }
252          putc('\t', stdout);
253          fputs(s, stdout);
254  
# Line 241 | Line 256 | char   *s;
256                  theirexp *= exposval(s);
257                  return;
258          }
259 <        for (an = altname; *an != NULL; an++)
260 <                if (!strncmp(*an, s, strlen(*an))) {
246 <                        if (sscanview(&theirview, s+strlen(*an)) > 0)
247 <                                gotview++;
248 <                        break;
249 <                }
259 >        if (isview(s) && sscanview(&theirview, s) > 0)
260 >                gotview++;
261   }
262  
263  
264   addpicture(pfile, zspec)                /* add picture to output */
265   char    *pfile, *zspec;
266   {
256        extern double   atof();
267          FILE    *pfp;
268          int     zfd;
269          char    *err;
# Line 262 | Line 272 | char   *pfile, *zspec;
272          struct position *plast;
273          int     y;
274                                          /* open picture file */
275 <        if ((pfp = fopen(pfile, "r")) == NULL) {
276 <                perror(pfile);
267 <                exit(1);
268 <        }
275 >        if ((pfp = fopen(pfile, "r")) == NULL)
276 >                syserror(pfile);
277                                          /* get header with exposure and view */
278          theirexp = 1.0;
279          gotview = 0;
280          printf("%s:\n", pfile);
281 <        getheader(pfp, headline);
282 <        if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp)
283 <                        != (YMAJOR|YDECR)) {
276 <                fprintf(stderr, "%s: picture view error\n", pfile);
281 >        getheader(pfp, headline, NULL);
282 >        if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) {
283 >                fprintf(stderr, "%s: picture format error\n", pfile);
284                  exit(1);
285          }
286          if (ourexp <= 0)
# Line 287 | Line 294 | char   *pfile, *zspec;
294                                          /* compute transformation */
295          hasmatrix = pixform(theirs2ours, &theirview, &ourview);
296                                          /* allocate scanlines */
297 <        scanin = (COLR *)malloc(thresolu*sizeof(COLR));
298 <        zin = (float *)malloc(thresolu*sizeof(float));
299 <        plast = (struct position *)calloc(thresolu, sizeof(struct position));
297 >        scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR));
298 >        zin = (float *)malloc(scanlen(&tresolu)*sizeof(float));
299 >        plast = (struct position *)calloc(scanlen(&tresolu),
300 >                        sizeof(struct position));
301          if (scanin == NULL || zin == NULL || plast == NULL)
302 <                syserror();
302 >                syserror(progname);
303                                          /* get z specification or file */
304          if ((zfd = open(zspec, O_RDONLY)) == -1) {
305                  double  zvalue;
306                  register int    x;
307 <                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
308 <                        perror(zspec);
309 <                        exit(1);
302 <                }
303 <                for (x = 0; x < thresolu; x++)
307 >                if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0)
308 >                        syserror(zspec);
309 >                for (x = scanlen(&tresolu); x-- > 0; )
310                          zin[x] = zvalue;
311          }
312                                          /* load image */
313 <        for (y = tvresolu-1; y >= 0; y--) {
314 <                if (freadcolrs(scanin, thresolu, pfp) < 0) {
313 >        for (y = 0; y < numscans(&tresolu); y++) {
314 >                if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) {
315                          fprintf(stderr, "%s: read error\n", pfile);
316                          exit(1);
317                  }
318 <                if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float))
319 <                                < thresolu*sizeof(float)) {
318 >                if (zfd != -1 && read(zfd,(char *)zin,
319 >                                scanlen(&tresolu)*sizeof(float))
320 >                                < scanlen(&tresolu)*sizeof(float)) {
321                          fprintf(stderr, "%s: read error\n", zspec);
322                          exit(1);
323                  }
# Line 373 | Line 380 | COLR   *pline;
380   float   *zline;
381   struct position *lasty;         /* input/output */
382   {
376        extern double   sqrt();
383          FVECT   pos;
384          struct position lastx, newpos;
385          register int    x;
386  
387          lastx.z = 0;
388 <        for (x = thresolu-1; x >= 0; x--) {
389 <                pos[0] = (x+.5)/thresolu;
384 <                pos[1] = (y+.5)/tvresolu;
388 >        for (x = scanlen(&tresolu); x-- > 0; ) {
389 >                pix2loc(pos, &tresolu, x, y);
390                  pos[2] = zline[x];
391                  if (movepixel(pos) < 0) {
392                          lasty[x].z = lastx.z = 0;       /* mark invalid */
# Line 465 | Line 470 | FVECT  pos;
470   {
471          FVECT   pt, direc;
472          
473 +        if (pos[2] <= 0)                /* empty pixel */
474 +                return(-1);
475          if (hasmatrix) {
476                  pos[0] += theirview.hoff - .5;
477                  pos[1] += theirview.voff - .5;
# Line 492 | Line 499 | FVECT  pos;
499          pt[0] += direc[0]*pos[2];
500          pt[1] += direc[1]*pos[2];
501          pt[2] += direc[2]*pos[2];
502 <        viewpixel(&pos[0], &pos[1], &pos[2], &ourview, pt);
502 >        viewloc(pos, &ourview, pt);
503          if (pos[2] <= 0)
504                  return(-1);
505          return(0);
# Line 505 | Line 512 | int    samp;
512   {
513          int     *yback, xback;
514          int     y;
508        COLR    pfill;
515          register int    x, i;
516                                                          /* get back buffer */
517          yback = (int *)malloc(hresolu*sizeof(int));
518          if (yback == NULL)
519 <                syserror();
519 >                syserror(progname);
520          for (x = 0; x < hresolu; x++)
521                  yback[x] = -2;
522          /*
# Line 611 | Line 617 | writepicture()                         /* write out picture */
617   {
618          int     y;
619  
620 <        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
620 >        fprtresolu(hresolu, vresolu, stdout);
621          for (y = vresolu-1; y >= 0; y--)
622                  if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
623 <                        syserror();
623 >                        syserror(progname);
624   }
625  
626  
627   writedistance(fname)                    /* write out z file */
628   char    *fname;
629   {
624        extern double   sqrt();
630          int     donorm = normdist && ourview.type == VT_PER;
631          int     fd;
632          int     y;
633          float   *zout;
634  
635 <        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
636 <                perror(fname);
632 <                exit(1);
633 <        }
635 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
636 >                syserror(fname);
637          if (donorm
638          && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
639 <                syserror();
639 >                syserror(progname);
640          for (y = vresolu-1; y >= 0; y--) {
641                  if (donorm) {
642                          double  vx, yzn2;
# Line 648 | Line 651 | char   *fname;
651                  } else
652                          zout = zscan(y);
653                  if (write(fd, (char *)zout, hresolu*sizeof(float))
654 <                                < hresolu*sizeof(float)) {
655 <                        perror(fname);
653 <                        exit(1);
654 <                }
654 >                                < hresolu*sizeof(float))
655 >                        syserror(fname);
656          }
657          if (donorm)
658                  free((char *)zout);
# Line 680 | Line 681 | int    x, y;
681   }
682  
683  
684 < calstart(prog, args)                    /* start fill calculation */
684 > calstart(prog, args)                    /* start fill calculation */
685   char    *prog, *args;
686   {
687          char    combuf[512];
688 <        int     p0[2], p1[2];
688 >        char    *argv[64];
689 >        int     rval;
690 >        register char   **wp, *cp;
691  
692          if (childpid != -1) {
693                  fprintf(stderr, "%s: too many calculations\n", progname);
694                  exit(1);
695          }
696 <        sprintf(combuf, prog, args);
697 <        if (pipe(p0) < 0 || pipe(p1) < 0)
698 <                syserror();
699 <        if ((childpid = vfork()) == 0) {        /* fork calculation */
700 <                close(p0[1]);
701 <                close(p1[0]);
702 <                if (p0[0] != 0) {
703 <                        dup2(p0[0], 0);
704 <                        close(p0[0]);
705 <                }
706 <                if (p1[1] != 1) {
707 <                        dup2(p1[1], 1);
705 <                        close(p1[1]);
706 <                }
707 <                execl("/bin/sh", "sh", "-c", combuf, 0);
708 <                perror("/bin/sh");
709 <                _exit(127);
696 >        strcpy(combuf, prog);
697 >        strcat(combuf, args);
698 >        cp = combuf;
699 >        wp = argv;
700 >        for ( ; ; ) {
701 >                while (isspace(*cp))    /* nullify spaces */
702 >                        *cp++ = '\0';
703 >                if (!*cp)               /* all done? */
704 >                        break;
705 >                *wp++ = cp;             /* add argument to list */
706 >                while (*++cp && !isspace(*cp))
707 >                        ;
708          }
709 <        if (childpid == -1)
710 <                syserror();
711 <        close(p0[0]);
712 <        close(p1[1]);
713 <        if ((psend = fdopen(p0[1], "w")) == NULL)
714 <                syserror();
715 <        if ((precv = fdopen(p1[0], "r")) == NULL)
716 <                syserror();
709 >        *wp = NULL;
710 >                                                /* start process */
711 >        if ((rval = open_process(PDesc, argv)) < 0)
712 >                syserror(progname);
713 >        if (rval == 0) {
714 >                fprintf(stderr, "%s: command not found\n", argv[0]);
715 >                exit(1);
716 >        }
717 >        packsiz = rval/(6*sizeof(float)) - 1;
718 >        if (packsiz > PACKSIZ)
719 >                packsiz = PACKSIZ;
720          queuesiz = 0;
721   }
722  
723  
724 < caldone()                               /* done with calculation */
724 > caldone()                               /* done with calculation */
725   {
725        int     pid;
726
726          if (childpid == -1)
727                  return;
728          clearqueue();
729 <        fclose(psend);
731 <        fclose(precv);
732 <        while ((pid = wait(0)) != -1 && pid != childpid)
733 <                ;
729 >        close_process(PDesc);
730          childpid = -1;
731   }
732  
# Line 738 | Line 734 | caldone()                              /* done with calculation */
734   rcalfill(x, y)                          /* fill with ray-calculated pixel */
735   int     x, y;
736   {
737 <        if (queuesiz >= PACKSIZ)        /* flush queue if needed */
737 >        if (queuesiz >= packsiz)        /* flush queue if needed */
738                  clearqueue();
739                                          /* add position to queue */
740          queue[queuesiz][0] = x;
# Line 750 | Line 746 | int    x, y;
746   clearqueue()                            /* process queue */
747   {
748          FVECT   orig, dir;
749 <        float   fbuf[6];
749 >        float   fbuf[6*(PACKSIZ+1)];
750 >        register float  *fbp;
751          register int    i;
752  
753 +        if (queuesiz == 0)
754 +                return;
755 +        fbp = fbuf;
756          for (i = 0; i < queuesiz; i++) {
757                  viewray(orig, dir, &ourview,
758                                  (queue[i][0]+.5)/hresolu,
759                                  (queue[i][1]+.5)/vresolu);
760 <                fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2];
761 <                fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
762 <                fwrite((char *)fbuf, sizeof(float), 6, psend);
760 >                *fbp++ = orig[0]; *fbp++ = orig[1]; *fbp++ = orig[2];
761 >                *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2];
762          }
763 <                                        /* flush output and get results */
764 <        fbuf[3] = fbuf[4] = fbuf[5] = 0.0;              /* mark */
765 <        fwrite((char *)fbuf, sizeof(float), 6, psend);
766 <        if (fflush(psend) == EOF)
767 <                syserror();
763 >                                        /* mark end and get results */
764 >        bzero((char *)fbp, 6*sizeof(float));
765 >        if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz,
766 >                        6*sizeof(float)*(queuesiz+1)) !=
767 >                        4*sizeof(float)*queuesiz) {
768 >                fprintf(stderr, "%s: error reading from rtrace process\n",
769 >                                progname);
770 >                exit(1);
771 >        }
772 >        fbp = fbuf;
773          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                }
774                  if (ourexp > 0 && ourexp != 1.0) {
775 <                        fbuf[0] *= ourexp;
776 <                        fbuf[1] *= ourexp;
777 <                        fbuf[2] *= ourexp;
775 >                        fbp[0] *= ourexp;
776 >                        fbp[1] *= ourexp;
777 >                        fbp[2] *= ourexp;
778                  }
779                  setcolr(pscan(queue[i][1])[queue[i][0]],
780 <                                fbuf[0], fbuf[1], fbuf[2]);
781 <                zscan(queue[i][1])[queue[i][0]] = fbuf[3];
780 >                                fbp[0], fbp[1], fbp[2]);
781 >                zscan(queue[i][1])[queue[i][0]] = fbp[3];
782 >                fbp += 4;
783          }
784          queuesiz = 0;
785   }
786  
787  
788 < syserror()                      /* report error and exit */
788 > syserror(s)                     /* report error and exit */
789 > char    *s;
790   {
791 <        perror(progname);
791 >        perror(s);
792          exit(1);
793   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines